98fab81c526ed06ec5283e7870696e20acdd86cf
[platform/core/connectivity/stc-manager.git] / plugin / appstatus / 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-appstatus.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 && STC_STAT_LOG) {
91                 STC_LOGD("\033[1;34mAPP STATUS\033[0;m: PkgID[\033[0;34m%s\033[0;m] "
92                         "AppID[\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 static stc_error_e __ground_status_monitor_init(stc_s *stc)
128 {
129         guint i = 0;
130
131         ret_value_msg_if(stc == NULL, STC_ERROR_INVALID_PARAMETER, "failed to get stc data");
132
133         for (i = 0; signal_map[i].member != NULL; i++) {
134                 signal_map[i].sub_id =
135                         g_dbus_connection_signal_subscribe(stc->connection,
136                                                            NULL,
137                                                            signal_map[i].interface,
138                                                            signal_map[i].member,
139                                                            signal_map[i].path,
140                                                            NULL,
141                                                            G_DBUS_SIGNAL_FLAGS_NONE,
142                                                            signal_map[i].callback,
143                                                            signal_map[i].user_data,
144                                                            NULL);
145                 STC_LOGI("Successfully subscribed [%s] signal",
146                          signal_map[i].member);
147         }
148
149         return STC_ERROR_NONE;
150 }
151
152 static stc_error_e __ground_status_monitor_deinit(stc_s *stc)
153 {
154         guint i = 0;
155
156         ret_value_msg_if(stc == NULL, STC_ERROR_INVALID_PARAMETER, "failed to get stc data");
157
158         for (i = 0; signal_map[i].member != NULL; i++) {
159                 g_dbus_connection_signal_unsubscribe(stc->connection,
160                                                      signal_map[i].sub_id);
161                 signal_map[i].sub_id = 0;
162                 STC_LOGD("Successfully unsubscribed [%s] signal",
163                          signal_map[i].member);
164         }
165
166         return STC_ERROR_NONE;
167 }
168
169 int stc_plugin_appstatus_register_changed_cb(stc_s *stc,
170                                        stc_plugin_app_state_changed_cb cb,
171                                        void *data)
172 {
173         state_changed_cb = cb;
174         __ground_status_monitor_init(stc);
175
176         return 0;
177 }
178
179 int stc_plugin_appstatus_deregister_changed_cb(stc_s *stc)
180 {
181         state_changed_cb = NULL;
182         __ground_status_monitor_deinit(stc);
183         return 0;
184 }
185
186 int stc_plugin_popup_send_message(const char *content,
187                 const char *type, const char *app_id, const char *iftype, const char *limit)
188 {
189         int ret = 0;
190         bundle *b = bundle_create();
191
192         bundle_add(b, "_SYSPOPUP_CONTENT_", content);
193         bundle_add(b, "_SYSPOPUP_TYPE_", type);
194         bundle_add(b, "_APP_ID_", app_id);
195         bundle_add(b, "_IF_TYPE_", iftype);
196
197         if (g_strcmp0(type, "warning_noti") == 0) {
198                 bundle_add(b, "_WARN_LIMIT_", limit);
199                 STC_LOGD("Warn message : content[%s] type[%s] app_id[%s] limit[%s]",
200                         content, type, app_id, limit);
201         } else {
202                 bundle_add(b, "_RESTRICTION_LIMIT_", limit);
203                 STC_LOGD("Restriction message : content[%s] type[%s] app_id[%s] limit[%s]",
204                         content, type, app_id, limit);
205         }
206
207         ret = syspopup_launch("net-popup", b);
208
209         bundle_free(b);
210
211         return ret;
212 }
213
214 API stc_plugin_appstatus_s stc_plugin_appstatus = {
215         .send_message_to_net_popup =
216                 stc_plugin_popup_send_message,
217         .register_state_changed_cb =
218                 stc_plugin_appstatus_register_changed_cb,
219         .deregister_state_changed_cb =
220                 stc_plugin_appstatus_deregister_changed_cb
221 };
222 //LCOV_EXCL_STOP