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