Moved exception list to plugin
[platform/core/connectivity/stc-manager.git] / plugin / stc-plugin-appstatus.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <errno.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <syspopup_caller.h>
22 #include <bundle.h>
23 #include <bundle_internal.h>
24 #include <dlog.h>
25 #include <gio/gio.h>
26
27 #include "stc-plugin.h"
28
29 //LCOV_EXCL_START
30 #define AUL_APP_STATUS_DBUS_PATH                   "/Org/Tizen/Aul/AppStatus"
31 #define AUL_APP_STATUS_DBUS_SIGNAL_INTERFACE       "org.tizen.aul.AppStatus"
32 #define AUL_APP_STATUS_BUS_NAME                    AUL_APP_STATUS_DBUS_SIGNAL_INTERFACE
33
34 #define AUL_APP_STATUS_DBUS_STATUS_CHANGE          "AppStatusChange"
35 #define AUL_APP_STATUS_DBUS_STATUS_CHANGE_TYPE     "(issss)"
36
37 typedef struct {
38         guint sub_id;
39         const gchar *path;
40         const gchar *interface;
41         const gchar *member;
42         const gchar *param_type;
43         GDBusSignalCallback callback;
44         gpointer user_data;
45 } signal_map_s;
46
47 stc_error_e(*state_changed_cb)(stc_cmd_type_e cmd, pid_t pid,
48                                const gchar *app_id, const gchar *pkg_id,
49                                stc_app_type_e app_type);
50
51 static void __stc_gdbus_handle_aul_changestate(GDBusConnection *connection,
52                                                const gchar *sender_name,
53                                                const gchar *object_path,
54                                                const gchar *interface_name,
55                                                const gchar *signal_name,
56                                                GVariant *parameters,
57                                                gpointer user_data)
58 {
59         pid_t pid;
60         stc_cmd_type_e status;
61         stc_app_type_e apptype;
62         gchar *appid, *pkgid, *statstr, *pkgtype;
63
64         if (g_strcmp0(g_variant_get_type_string(parameters),
65                       AUL_APP_STATUS_DBUS_STATUS_CHANGE_TYPE)) {
66                 STC_LOGE("Dbus type not matching, do not process");
67                 return;
68         }
69
70         g_variant_get(parameters, AUL_APP_STATUS_DBUS_STATUS_CHANGE_TYPE,
71                       &pid, &appid, &pkgid, &statstr, &pkgtype);
72
73         if (!strncmp(statstr, "fg", 2)) {
74                 status = STC_CMD_SET_FOREGRD;
75         } else if (!strncmp(statstr, "bg", 2)) {
76                 status = STC_CMD_SET_BACKGRD;
77         } else {
78                 goto out;
79         }
80
81         if (!strncmp(pkgtype, "svc", 3))
82                 apptype = STC_APP_TYPE_SERVICE;
83         else if (!strncmp(pkgtype, "widget", 6))
84                 apptype = STC_APP_TYPE_WIDGET;
85         else if (!strncmp(pkgtype, "watch", 5))
86                 apptype = STC_APP_TYPE_WATCH;
87         else
88                 apptype = STC_APP_TYPE_GUI;
89
90         if (STC_DEBUG_LOG) {
91                 STC_LOGD("\033[1;36mAPP STATUS\033[0;m: Pkg ID [\033[0;34m%s\033[0;m], "
92                         "App ID [\033[0;32m%s\033[0;m], PID [\033[1;33m%d\033[0;m], Status [%s], Type [%s]",
93                         pkgid, appid, pid, statstr, pkgtype);
94         }
95
96         if (state_changed_cb)
97                 state_changed_cb(status, pid, appid, pkgid, apptype);
98
99 out:
100         FREE(appid);
101         FREE(pkgid);
102         FREE(statstr);
103         FREE(pkgtype);
104 }
105
106 signal_map_s signal_map[] = {
107
108         /* AMD DBUS */
109         {
110                 0,
111                 AUL_APP_STATUS_DBUS_PATH,
112                 AUL_APP_STATUS_DBUS_SIGNAL_INTERFACE,
113                 AUL_APP_STATUS_DBUS_STATUS_CHANGE,
114                 AUL_APP_STATUS_DBUS_STATUS_CHANGE_TYPE,
115                 __stc_gdbus_handle_aul_changestate,
116                 NULL
117         },
118         {
119                 0,
120                 NULL,
121                 NULL,
122                 NULL,
123                 NULL
124         }
125 };
126
127
128 static stc_error_e __ground_status_monitor_init(stc_s *stc)
129 {
130         guint i = 0;
131
132         ret_value_msg_if(stc == NULL, STC_ERROR_INVALID_PARAMETER, "failed to get stc data");
133
134         for (i = 0; signal_map[i].member != NULL; i++) {
135                 signal_map[i].sub_id =
136                         g_dbus_connection_signal_subscribe(stc->connection,
137                                                            NULL,
138                                                            signal_map[i].interface,
139                                                            signal_map[i].member,
140                                                            signal_map[i].path,
141                                                            NULL,
142                                                            G_DBUS_SIGNAL_FLAGS_NONE,
143                                                            signal_map[i].callback,
144                                                            signal_map[i].user_data,
145                                                            NULL);
146                 STC_LOGI("Successfully subscribed [%s] signal",
147                          signal_map[i].member);
148         }
149
150         return STC_ERROR_NONE;
151 }
152
153 static stc_error_e __ground_status_monitor_deinit(stc_s *stc)
154 {
155         guint i = 0;
156
157         ret_value_msg_if(stc == NULL, STC_ERROR_INVALID_PARAMETER, "failed to get stc data");
158
159         for (i = 0; signal_map[i].member != NULL; i++) {
160                 g_dbus_connection_signal_unsubscribe(stc->connection,
161                                                      signal_map[i].sub_id);
162                 signal_map[i].sub_id = 0;
163                 STC_LOGD("Successfully unsubscribed [%s] signal",
164                          signal_map[i].member);
165         }
166
167         return STC_ERROR_NONE;
168 }
169
170 int stc_plugin_appstatus_register_changed_cb(stc_s *stc,
171                                        stc_plugin_app_state_changed_cb cb,
172                                        void *data)
173 {
174         state_changed_cb = cb;
175         __ground_status_monitor_init(stc);
176
177         return 0;
178 }
179
180 int stc_plugin_appstatus_deregister_changed_cb(stc_s *stc)
181 {
182         state_changed_cb = NULL;
183         __ground_status_monitor_deinit(stc);
184         return 0;
185 }
186 //LCOV_EXCL_STOP