Don't free dbus method invocation twice
[platform/core/connectivity/net-config.git] / plugin / headed / headed.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <app.h>
21 #include <errno.h>
22 #include <vconf.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <net/if.h>
28 #include <net/route.h>
29 #include <arpa/inet.h>
30 #include <sys/wait.h>
31 #include <sys/stat.h>
32 #include <sys/ioctl.h>
33 #include <vconf-keys.h>
34 #include <syspopup_caller.h>
35 #include <bundle.h>
36 #include <bundle_internal.h>
37 #include <eventsystem.h>
38 #include <tzplatform_config.h>
39 #include <system_info.h>
40
41 #include "plugin.h"
42
43 #ifdef USE_NETCONFIG_LOG
44 #include "log.h"
45 #else
46 #include <dlog.h>
47
48 #define NETCONFIG_TAG           "NETCONFIG"
49 #define __LOG(level, format, arg...) \
50         do { \
51                 SLOG(level, NETCONFIG_TAG, format, ## arg); \
52         } while (0)
53
54 #define DBG(format, arg...)     __LOG(LOG_DEBUG, format, ## arg)
55 #define ERR(format, arg...)     __LOG(LOG_ERROR, format, ## arg)
56 #endif
57
58 const char *_headed_convert_sys_event(int event_val)
59 {
60         switch (event_val) {
61         case SYS_EVT_NETWORK_STATUS:
62                 return SYS_EVENT_NETWORK_STATUS;
63         case SYS_EVT_WIFI_STATE:
64                         return SYS_EVENT_WIFI_STATE;
65         case EKEY_NETWORK_STATUS:
66                         return EVT_KEY_NETWORK_STATUS;
67         case EKEY_WIFI_STATE:
68                         return EVT_KEY_WIFI_STATE;
69         case EVAL_NETWORK_WIFI:
70                         return EVT_VAL_NETWORK_WIFI;
71         case EVAL_NETWORK_CELLULAR:
72                         return EVT_VAL_NETWORK_CELLULAR;
73         case EVAL_NETWORK_ETHERNET:
74                         return EVT_VAL_NETWORK_ETHERNET;
75         case EVAL_NETWORK_BT:
76                         return EVT_VAL_NETWORK_BT;
77         case EVAL_NETWORK_DISCONNECTED:
78                         return EVT_VAL_NETWORK_DISCONNECTED;
79         case EVAL_WIFI_CONNECTED:
80                         return EVT_VAL_WIFI_CONNECTED;
81         case EVAL_WIFI_ON:
82                         return EVT_VAL_WIFI_ON;
83         case EVAL_WIFI_OFF:
84                         return EVT_VAL_WIFI_OFF;
85         default:
86                 break;
87         }
88
89         return NULL;
90 }
91
92 void headed_pop_device_picker(void)
93 {
94 #if defined TIZEN_WEARABLE
95         int ret = 0;
96         app_control_h   control = NULL;
97
98         ret = app_control_create(&control);
99         if (APP_CONTROL_ERROR_NONE != ret) {
100                 DBG("failed to create app control");
101                 return ;
102         }
103
104         app_control_add_extra_data(control, "viewtype", "scanlist");
105
106         app_control_set_app_id(control, "org.tizen.wifi");
107         ret = app_control_send_launch_request(control, NULL, NULL);
108         if (APP_CONTROL_ERROR_NONE == ret)
109                 DBG("Launch request sent successfully");
110
111         app_control_destroy(control);
112 #else
113         bundle *b = NULL;
114         int wifi_ug_state = 0;
115         int ret = 0;
116
117         ret = vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &wifi_ug_state);
118         if (ret != VCONF_OK)
119                 ERR("Failed to get vconfkey [%s] value", VCONFKEY_WIFI_UG_RUN_STATE);
120
121         if (wifi_ug_state == VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND)
122                 return;
123
124         b = bundle_create();
125
126         DBG("Launch Wi-Fi device picker");
127         syspopup_launch("wifi-qs", b);
128
129         bundle_free(b);
130 #endif
131 }
132
133
134 gboolean headed_send_notification_to_net_popup(const char * noti, const char * message)
135 {
136         int ret = 0;
137         bundle *b;
138         static gboolean is_found_noti_exists = FALSE;
139         static gboolean is_portal_noti_exists = FALSE;
140         static gboolean is_conflict_noti_exists = FALSE;
141
142         if (noti == NULL) {
143                 ERR("Invalid notification");
144                 return FALSE;
145         }
146
147         if (g_strcmp0(noti, NETCONFIG_DEL_FOUND_AP_NOTI) == 0) {
148                 if (is_found_noti_exists == FALSE)
149                         return TRUE;
150
151                 is_found_noti_exists = FALSE;
152         } else if (g_strcmp0(noti, NETCONFIG_ADD_FOUND_AP_NOTI) == 0) {
153                 if (is_found_noti_exists == TRUE)
154                         return TRUE;
155
156                 is_found_noti_exists = TRUE;
157         } else if (g_strcmp0(noti, NETCONFIG_ADD_PORTAL_NOTI) == 0) {
158                 if (is_portal_noti_exists == TRUE)
159                         return TRUE;
160
161                 is_portal_noti_exists = TRUE;
162         } else if (g_strcmp0(noti, NETCONFIG_DEL_PORTAL_NOTI) == 0) {
163                 if (is_portal_noti_exists == FALSE)
164                         return TRUE;
165
166                 is_portal_noti_exists = FALSE;
167         } else if (g_strcmp0(noti, NETCONFIG_DEL_IP_CONFLICT_NOTI) == 0) {
168                 if (is_conflict_noti_exists == FALSE)
169                         return TRUE;
170
171                 is_conflict_noti_exists = FALSE;
172         } else if (g_strcmp0(noti, NETCONFIG_ADD_IP_CONFLICT_NOTI) == 0) {
173                 if (is_conflict_noti_exists == TRUE)
174                         return TRUE;
175
176                 is_conflict_noti_exists = TRUE;
177         }
178
179         b = bundle_create();
180         bundle_add(b, "_SYSPOPUP_TYPE_", noti);
181
182         if (message != NULL) {
183                 DBG("message (%s)", message);
184                 if (g_strcmp0(noti, NETCONFIG_ADD_IP_CONFLICT_NOTI) == 0)
185                         bundle_add(b, "_MAC_ADDRESS_", message);
186                 else
187                         bundle_add(b, "_AP_NAME_", message);
188         }
189
190         ret = syspopup_launch("net-popup", b);
191
192         bundle_free(b);
193
194         if (ret < 0) {
195                 ERR("Unable to launch noti-popup. Err = %d", ret);
196                 return FALSE;
197         }
198
199         DBG("Successfully sent notification (%s)", noti);
200         return TRUE;
201 }
202
203 int headed_send_message_to_net_popup(const char *title,
204                 const char *content, const char *type, const char *ssid)
205 {
206         int ret = 0;
207         bundle *b = bundle_create();
208
209         bundle_add(b, "_SYSPOPUP_TITLE_", title);
210         bundle_add(b, "_SYSPOPUP_CONTENT_", content);
211         bundle_add(b, "_SYSPOPUP_TYPE_", type);
212         bundle_add(b, "_AP_NAME_", ssid);
213
214         ret = syspopup_launch("net-popup", b);
215
216         bundle_free(b);
217
218         return ret;
219 }
220
221 int headed_send_restriction_to_net_popup(const char *title,
222                 const char *type, const char *restriction)
223 {
224         int ret = 0;
225         bundle *b = bundle_create();
226
227         bundle_add(b, "_SYSPOPUP_TITLE_", title);
228         bundle_add(b, "_SYSPOPUP_CONTENT_", "security restriction");
229         bundle_add(b, "_SYSPOPUP_TYPE_", type);
230         bundle_add(b, "_RESTRICTED_TYPE_", restriction);
231
232         ret = syspopup_launch("net-popup", b);
233
234         bundle_free(b);
235
236         return ret;
237 }
238
239 void headed_set_system_event(int sys_evt, int evt_key, int evt_val)
240 {
241         bundle *b = NULL;
242         const char *sevent = _headed_convert_sys_event(sys_evt);
243         const char *ekey = _headed_convert_sys_event(evt_key);
244         const char *eval = _headed_convert_sys_event(evt_val);
245
246         DBG("System event set [%s : %s : %s]", sevent, ekey, eval);
247
248         b = bundle_create();
249         bundle_add_str(b, ekey, eval);
250         eventsystem_send_system_event(sevent, b);
251         bundle_free(b);
252 }
253
254 void headed_pop_wifi_connected_poppup(const char *ssid)
255 {
256         bundle *b = NULL;
257
258         if (ssid == NULL)
259                 return;
260
261         b = bundle_create();
262
263         bundle_add(b, "_SYSPOPUP_TITLE_", "Network connection popup");
264         bundle_add(b, "_SYSPOPUP_TYPE_", "toast_popup");
265         bundle_add(b, "_SYSPOPUP_CONTENT_", "wifi connected");
266         bundle_add(b, "_AP_NAME_", ssid);
267
268         DBG("Launch Wi-Fi connected alert network popup");
269         syspopup_launch("net-popup", b);
270
271         bundle_free(b);
272 }
273
274
275 extern struct netconfig_headed_plugin_t netconfig_headed_plugin
276                 __attribute__ ((visibility("default")));
277 struct netconfig_headed_plugin_t netconfig_headed_plugin = {
278                 headed_pop_device_picker,
279                 headed_send_notification_to_net_popup,
280                 headed_send_message_to_net_popup,
281                 headed_send_restriction_to_net_popup,
282                 headed_set_system_event,
283                 headed_pop_wifi_connected_poppup
284 };
285