Check if ckmc_get_data() provides NULL value
[platform/core/connectivity/net-config.git] / src / signal-handler.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2000 - 2012 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 <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <vconf.h>
24 #include <vconf-keys.h>
25 #include <sys/wait.h>
26
27 #include "log.h"
28 #include "util.h"
29 #include "netdbus.h"
30 #include "neterror.h"
31 #include "wifi-wps.h"
32 #include "wifi-agent.h"
33 #include "wifi-power.h"
34 #include "wifi-state.h"
35 #include "wifi-indicator.h"
36 #include "wifi-scan.h"
37 #include "wifi-bssid-scan.h"
38 #include "wifi-background-scan.h"
39 #include "netsupplicant.h"
40 #include "network-state.h"
41 #include "cellular-state.h"
42 #include "signal-handler.h"
43 #include "wifi-tdls.h"
44 #include "wifi-dpp.h"
45 #include "ip-conflict-detect.h"
46 #include "wifi-key-encryption.h"
47 #include "clatd-handler.h"
48 #if defined TIZEN_DEBUG_ENABLE
49 #include "network-dump.h"
50 #define NETWORK_LOG_DUMP_SCRIPT  "/opt/var/lib/net-config/network_log_dump.sh"
51 #define MAX_SIZE_ERROR_BUFFER 256
52 #endif
53
54 #define DBUS_SERVICE_DBUS                       "org.freedesktop.DBus"
55 #define DBUS_INTERFACE_DBUS                     "org.freedesktop.DBus"
56 #define SIGNAL_INTERFACE_REMOVED                "InterfaceRemoved"
57 #define SIGNAL_SCAN_DONE                        "ScanDone"
58 #define SIGNAL_SCAN_CHANGED                     "ScanChanged"
59 #define SIGNAL_BSS_ADDED                        "BSSAdded"
60 #define SIGNAL_PROPERTIES_CHANGED               "PropertiesChanged"
61 #define SIGNAL_PROPERTIES_DRIVER_HANGED         "DriverHanged"
62 #define SIGNAL_PROPERTIES_SESSION_OVERLAPPED    "SessionOverlapped"
63 #define SIGNAL_TDLS_CONNECTED                           "TDLSConnected"
64 #define SIGNAL_TDLS_DISCONNECTED                        "TDLSDisconnected"
65 #define SIGNAL_TDLS_PEER_FOUND                          "TDLSPeerFound"
66
67 #define SIGNAL_DPP_AUTH_SUCCESS "DPPAuthSuccess"
68 #define SIGNAL_DPP_AUTH_FAILED "DPPAuthFailed"
69 #define SIGNAL_DPP_NOT_COMPATIBLE "DPPNotCompatible"
70 #define SIGNAL_DPP_CONF_FAILED "DPPConfFailed"
71 #define SIGNAL_DPP_SCAN_PEER_QR "DPPScanPeerQrCode"
72 #define SIGNAL_DPP_NETWORK_ID "DPPNetworkID"
73 #define SIGNAL_DPP_CONF_SENT "DPPConfSent"
74 #define SIGNAL_DPP_CONF_RECEIVED "DPPConfReceived"
75 #define SIGNAL_DPP_CONF_OBJ "DPPConfObj"
76
77 #define SIGNAL_WPS_CONNECTED                            "WPSConnected"
78 #define SIGNAL_WPS_EVENT                                        "Event"
79 #define SIGNAL_WPS_CREDENTIALS                          "Credentials"
80
81 #define CONNMAN_SIGNAL_SERVICES_CHANGED         "ServicesChanged"
82 #define CONNMAN_SIGNAL_PROPERTY_CHANGED         "PropertyChanged"
83 #define CONNMAN_SIGNAL_DEVICE_CHANGED           "DeviceChanged"
84 #define CONNMAN_SIGNAL_DEVICE_DETECTED          "DeviceDetected"
85 #define SIGNAL_NAME_OWNER_CHANGED               "NameOwnerChanged"
86 #define NETCONFIG_SIGNAL_VCONF_CHANGED  "VconfChanged"
87
88
89 #define MAX_SIG_LEN 64
90 #define TOTAL_CONN_SIGNALS 7
91 #define MAX_SOCKET_OPEN_RETRY 5
92
93 typedef enum {
94         SIG_INTERFACE_REMOVED = 0,
95         SIG_PROPERTIES_CHANGED,
96         SIG_BSS_ADDED,
97         SIG_SCAN_DONE,
98         SIG_DRIVER_HANGED,
99         SIG_SESSION_OVERLAPPED,
100         SIG_TDLS_CONNECTED,
101         SIG_TDLS_DISCONNECTED,
102         SIG_TDLS_PEER_FOUND,
103         SIG_DPP_AUTH_SUCCESS,
104         SIG_DPP_AUTH_FAILED,
105         SIG_DPP_NOT_COMPATIBLE,
106         SIG_DPP_CONF_FAILED,
107         SIG_DPP_SCAN_PEER_QR,
108         SIG_DPP_NETWORK_ID,
109         SIG_DPP_CONF_SENT,
110         SIG_DPP_CONF_RECEIVED,
111         SIG_DPP_CONF_OBJ,
112         SIG_MAX
113 } SuppSigArrayIndex;
114
115 static int conn_subscription_ids[TOTAL_CONN_SIGNALS] = {0};
116 static const char supplicant_signals[SIG_MAX][MAX_SIG_LEN] = {
117                 SIGNAL_INTERFACE_REMOVED,
118                 SIGNAL_PROPERTIES_CHANGED,
119                 SIGNAL_BSS_ADDED,
120                 SIGNAL_SCAN_DONE,
121                 SIGNAL_PROPERTIES_DRIVER_HANGED,
122                 SIGNAL_PROPERTIES_SESSION_OVERLAPPED,
123                 SIGNAL_TDLS_CONNECTED,
124                 SIGNAL_TDLS_DISCONNECTED,
125                 SIGNAL_TDLS_PEER_FOUND,
126                 SIGNAL_DPP_AUTH_SUCCESS,
127                 SIGNAL_DPP_AUTH_FAILED,
128                 SIGNAL_DPP_NOT_COMPATIBLE,
129                 SIGNAL_DPP_CONF_FAILED,
130                 SIGNAL_DPP_SCAN_PEER_QR,
131                 SIGNAL_DPP_NETWORK_ID,
132                 SIGNAL_DPP_CONF_SENT,
133                 SIGNAL_DPP_CONF_RECEIVED,
134                 SIGNAL_DPP_CONF_OBJ,
135 };
136 static const char *supplicant_iface_for_sig[SIG_MAX] = {
137                 SUPPLICANT_INTERFACE,
138                 SUPPLICANT_IFACE_INTERFACE,
139                 SUPPLICANT_IFACE_INTERFACE,
140                 SUPPLICANT_IFACE_INTERFACE,
141                 SUPPLICANT_IFACE_INTERFACE,
142                 SUPPLICANT_IFACE_INTERFACE,
143                 SUPPLICANT_IFACE_INTERFACE,
144                 SUPPLICANT_IFACE_INTERFACE,
145                 SUPPLICANT_IFACE_INTERFACE,
146                 SUPPLICANT_IFACE_DPP,
147                 SUPPLICANT_IFACE_DPP,
148                 SUPPLICANT_IFACE_DPP,
149                 SUPPLICANT_IFACE_DPP,
150                 SUPPLICANT_IFACE_DPP,
151                 SUPPLICANT_IFACE_DPP,
152                 SUPPLICANT_IFACE_DPP,
153                 SUPPLICANT_IFACE_DPP,
154                 SUPPLICANT_IFACE_DPP,
155 };
156
157 static int supp_subscription_ids[SIG_MAX] = {0};
158 #if defined TIZEN_DEBUG_ENABLE
159 static int dumpservice_subscription_id = 0;
160 #endif
161 static int netconfig_subscription_id = 0;
162
163 typedef void (*supplicant_signal_cb)(GDBusConnection *conn,
164                 const gchar *name, const gchar *path, const gchar *interface,
165                 const gchar *sig, GVariant *param, gpointer user_data);
166 typedef void (*connman_signal_cb)(GDBusConnection *conn,
167                 const gchar *name, const gchar *path, const gchar *interface,
168                 const gchar *sig, GVariant *param, gpointer user_data);
169
170 static void __netconfig_extract_ipv4_signal_data(GVariant *dictionary,
171                 const gchar *profile)
172 {
173         gchar *key = NULL;
174         const gchar *value = NULL;
175         GVariant *var = NULL;
176         GVariantIter iter;
177         GVariantBuilder *builder;
178         GVariant *params;
179
180         g_variant_iter_init(&iter, dictionary);
181         while (g_variant_iter_loop(&iter, "{sv}", &key, &var)) {
182                 if (g_strcmp0(key, "Address") == 0)  {
183                         g_variant_get(var, "&s", &value);
184                         char *old_ip = vconf_get_str(VCONFKEY_NETWORK_IP);
185
186                         DBG("Old IPv4.Address [%s] Received new IPv4.Address [%s]", old_ip,
187                                                   value);
188                         if (g_strcmp0(old_ip, value) != 0) {
189                                 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
190
191                                 if (value != NULL) {
192                                         netconfig_set_default_ipaddress(value);
193                                         netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, value, TRUE);
194                                         g_variant_builder_add(builder, "{sv}", "IPv4Address",
195                                                                   g_variant_new_string(value));
196                                 } else if (old_ip != NULL && strlen(old_ip) > 0) {
197                                         netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "",TRUE);
198                                         g_variant_builder_add(builder, "{sv}", "IPv4Address",
199                                                                   g_variant_new_string(""));
200                                 }
201
202                                 params = g_variant_new("(@a{sv})",
203                                                                            g_variant_builder_end(builder));
204
205                                 g_variant_builder_unref(builder);
206
207                                 netconfig_dbus_emit_signal(NULL, NETCONFIG_NETWORK_PATH,
208                                                    NETCONFIG_NETWORK_INTERFACE, "NetworkConfigChanged",
209                                                    params);
210                         }
211                         free(old_ip);
212                 }
213         }
214 }
215
216 static void __netconfig_extract_ipv6_signal_data(GVariant *dictionary,
217                                                                                                  const gchar *profile)
218 {
219         gchar *key = NULL;
220         const gchar *value = NULL;
221         GVariant *var = NULL;
222         GVariantIter iter;
223         GVariantBuilder *builder;
224         GVariant *params;
225
226         g_variant_iter_init(&iter, dictionary);
227         while (g_variant_iter_loop(&iter, "{sv}", &key, &var)) {
228                 if (g_strcmp0(key, "Address") == 0)  {
229                         g_variant_get(var, "&s", &value);
230                         char *old_ip6 = vconf_get_str(VCONFKEY_NETWORK_IP6);
231
232                         DBG("Old IPv6.Address [%s] Received new IPv6.Address [%s]", old_ip6,
233                                                         value);
234                         if (g_strcmp0(old_ip6, value) != 0) {
235                                 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
236
237                                 if (value != NULL) {
238                                         netconfig_set_default_ipaddress6(value);
239                                         netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, value, TRUE);
240                                         g_variant_builder_add(builder, "{sv}", "IPv6Address",
241                                                                   g_variant_new_string(value));
242                                 } else if (old_ip6 != NULL && strlen(old_ip6) > 0) {
243                                         netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, "", TRUE);
244                                         g_variant_builder_add(builder, "{sv}", "IPv6Address",
245                                                                   g_variant_new_string(""));
246                                 }
247
248                                 params = g_variant_new("(@a{sv})",
249                                                                            g_variant_builder_end(builder));
250                                 g_variant_builder_unref(builder);
251
252                                 netconfig_dbus_emit_signal(NULL, NETCONFIG_NETWORK_PATH,
253                                                    NETCONFIG_NETWORK_INTERFACE, "NetworkConfigChanged",
254                                                    params);
255                         }
256                         free(old_ip6);
257                 }
258         }
259 }
260
261 #if defined TIZEN_DEBUG_ENABLE
262 static int __netconfig_handle_execute_file(const char *file_path,
263                 char *const args[], char *const envs[])
264 {
265         pid_t pid = 0;
266         int status = 0;
267         int rv = 0;
268         errno = 0;
269         register unsigned int index = 0;
270         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
271
272         while (args[index] != NULL) {
273                 DBG("%s", args[index]);
274                 index++;
275         }
276
277         if (!(pid = fork())) {
278                 DBG("pid(%d), ppid (%d)", getpid(), getppid());
279                 DBG("Inside child, exec (%s) command", file_path);
280
281                 errno = 0;
282                 if (execve(file_path, args, envs) == -1) {
283                         DBG("Fail to execute command (%s)",
284                                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
285                         exit(1);
286                 }
287         } else if (pid > 0) {
288                 if (waitpid(pid, &status, 0) == -1)
289                         DBG("wait pid (%u) status (%d)", pid, status);
290
291                 if (WIFEXITED(status)) {
292                         rv = WEXITSTATUS(status);
293                         DBG("exited, status=%d", rv);
294                 } else if (WIFSIGNALED(status)) {
295                         DBG("killed by signal %d", WTERMSIG(status));
296                 } else if (WIFSTOPPED(status)) {
297                         DBG("stopped by signal %d", WSTOPSIG(status));
298                 } else if (WIFCONTINUED(status)) {
299                         DBG("continued");
300                 }
301
302                 return rv;
303         }
304
305         DBG("failed to fork(%s)",
306                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
307         return -EIO;
308 }
309
310 static int _start_dump()
311 {
312         int rv = 0;
313         gchar *path = NETWORK_LOG_DUMP_SCRIPT;
314         char *const args[] = { NETWORK_LOG_DUMP_SCRIPT, NULL };
315         char *const envs[] = { NULL };
316
317         rv = __netconfig_handle_execute_file(path, args, envs);
318         if (rv < 0) {
319                 ERR("Fail to execute network log dump shell");
320                 return -EIO;
321         }
322         return 0;
323 }
324 #endif
325
326 static char *__supplicant_get_interface_name(const gchar *path, const gchar *interface)
327 {
328         GVariant *params = NULL;
329         GVariant *reply = NULL, *var = NULL;;
330         char *interface_name = NULL;
331
332         params = g_variant_new("(ss)", interface, "Ifname");
333
334         reply = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
335                                 path, DBUS_INTERFACE_PROPERTIES, "Get", params);
336         if (reply == NULL) {
337                 ERR("Failed to get interface name");
338                 return NULL;
339         }
340
341         g_variant_get(reply, "(v)", &var);
342
343         if (g_variant_is_of_type(var, G_VARIANT_TYPE_STRING)) {
344                 interface_name = g_variant_dup_string(var, NULL);
345                 DBG("%s [%s]", path, interface_name);
346         }
347
348         g_variant_unref(var);
349         g_variant_unref(reply);
350
351         return interface_name;
352 }
353
354 static void _technology_signal_cb(GDBusConnection *conn,
355                 const gchar *name, const gchar *path, const gchar *interface,
356                 const gchar *sig, GVariant *param, gpointer user_data)
357 {
358         gchar *key = NULL;
359         GVariant *var = NULL;
360         GVariantIter *iter = NULL;
361         const gchar *svalue = NULL;
362         gboolean bvalue = FALSE;
363
364         DBG("%s %s", path, sig);
365
366         if (param == NULL)
367                 return;
368
369         if (g_str_has_prefix(path, CONNMAN_WIFI_TECHNOLOGY_PREFIX) != TRUE)
370                 return;
371
372         if (g_strcmp0(sig, CONNMAN_SIGNAL_DEVICE_CHANGED) == 0) {
373                 g_variant_get(param, "(a{sv})", &iter);
374                 while (g_variant_iter_loop(iter, "{sv}", &key, &var)) {
375                         if (g_strcmp0(key, "Ifname") == 0) {
376                                 svalue = g_variant_get_string(var, NULL);
377                                 DBG("Ifname [%s]", svalue);
378                         } else if (g_strcmp0(key, "Powered") == 0) {
379                                 bvalue = g_variant_get_boolean(var);
380                                 DBG("Powered [%d]", bvalue);
381                                 if (svalue)
382                                         wifi_state_update_power_state(svalue, bvalue);
383                         } else if (g_strcmp0(key, "Connected") == 0) {
384                                 bvalue = g_variant_get_boolean(var);
385                                 DBG("Connected [%d]", bvalue);
386                                 if (svalue) {
387                                         wifi_state_set_connected(svalue, bvalue);
388                                         if (bvalue == TRUE)
389                                                 wifi_state_set_technology_state(svalue, NETCONFIG_WIFI_TECH_CONNECTED);
390                                         else
391                                                 wifi_state_set_technology_state(svalue, NETCONFIG_WIFI_TECH_POWERED);
392                                 }
393                         }
394                 }
395                 g_variant_iter_free(iter);
396         } else if (g_strcmp0(sig, CONNMAN_SIGNAL_DEVICE_DETECTED) == 0) {
397                 wifi_state_update_device_list();
398         }
399 }
400
401 static void _service_signal_cb(GDBusConnection *conn,
402                 const gchar *name, const gchar *path,
403                 const gchar *interface, const gchar *sig,
404                 GVariant *param, gpointer user_data)
405 {
406         gchar *sigvalue = NULL;
407         gchar *property;
408         GVariant *variant = NULL, *var;
409         GVariantIter *iter;
410         const gchar *value = NULL;
411         struct sock_data *sd = NULL;
412         gchar *pf = NULL;
413         int idx = 0;
414         unsigned char strength = 0;
415         const char *interface_name = NULL;
416
417         if (path == NULL || param == NULL)
418                 goto done;
419
420         g_variant_get(param, "(sv)", &sigvalue, &variant);
421         if (sigvalue == NULL)
422                 goto done;
423
424         if (g_strcmp0(sig, CONNMAN_SIGNAL_PROPERTY_CHANGED) != 0)
425                 goto done;
426
427         interface_name = netconfig_get_ifname(path);
428
429         if (g_strcmp0(sigvalue, "State") == 0) {
430                 g_variant_get(variant, "s", &property);
431
432                 DBG("[%s] %s", property, path);
433
434                 if (netconfig_is_wifi_profile(path) == TRUE) {
435                         int wifi_state = 0;
436
437                         netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
438                         if (wifi_state == VCONFKEY_WIFI_OFF) {
439                                 g_free(property);
440                                 goto done;
441                         }
442
443                         if (g_strcmp0(property, "ready") == 0 || g_strcmp0(property, "online") == 0) {
444                                 netconfig_update_default_profile();
445
446                                 wifi_state_set_service_state(interface_name, path, NETCONFIG_WIFI_CONNECTED);
447
448                         } else if (g_strcmp0(property, "failure") == 0 ||
449                                 g_strcmp0(property, "disconnect") == 0 || g_strcmp0(property, "idle") == 0) {
450                                 if (g_strcmp0(property, "failure") == 0)
451                                         wifi_state_set_service_state(interface_name, path, NETCONFIG_WIFI_FAILURE);
452                                 else
453                                         wifi_state_set_service_state(interface_name, path, NETCONFIG_WIFI_IDLE);
454
455                                 if (g_strcmp0(path, netconfig_get_default_profile()) != 0) {
456                                         g_free(property);
457                                         goto done;
458                                 }
459
460                                 netconfig_update_default_profile();
461
462                         } else if (g_strcmp0(property, "association") == 0 || g_strcmp0(property, "configuration") == 0) {
463                                 if (g_strcmp0(property, "association") == 0)
464                                         wifi_state_set_service_state(interface_name, path, NETCONFIG_WIFI_ASSOCIATION);
465                                 else
466                                         wifi_state_set_service_state(interface_name, path, NETCONFIG_WIFI_CONFIGURATION);
467
468                                 if (g_strcmp0(path, netconfig_get_default_profile()) != 0) {
469                                         g_free(property);
470                                         goto done;
471                                 }
472
473                                 netconfig_update_default_profile();
474
475                         }
476                 } else {
477                         if (g_strcmp0(property, "ready") == 0 || g_strcmp0(property, "online") == 0) {
478                                 netconfig_update_default_profile();
479
480                                 if (netconfig_is_cellular_profile(netconfig_get_default_profile())) {
481                                         pf = netconfig_get_connected_cellular_internet_ipv6only_profile(NULL);
482                                         if (pf) {
483                                                 g_free(pf);
484
485                                                 /* Enable clatd if it is not in running state */
486                                                 DBG("Connected to ipv6 only cellular, enable clatd");
487                                                 netconfig_clatd_enable();
488                                         }
489                                 }
490
491                                 if (netconfig_is_cellular_profile(path) && netconfig_is_cellular_internet_profile(path))
492                                         cellular_state_set_service_state(NETCONFIG_CELLULAR_ONLINE);
493
494                         } else if (g_strcmp0(property, "failure") == 0 ||
495                                 g_strcmp0(property, "disconnect") == 0 || g_strcmp0(property, "idle") == 0) {
496                                 if (netconfig_get_default_profile() == NULL) {
497                                         g_free(property);
498                                         goto done;
499                                 }
500
501                                 if (netconfig_is_cellular_profile(path) && netconfig_is_cellular_internet_profile(path)) {
502                                         cellular_state_set_service_state(NETCONFIG_CELLULAR_IDLE);
503
504                                         /* Disable clatd if it is in running state */
505                                         DBG("disable clatd");
506                                         netconfig_clatd_disable();
507                                 }
508
509                                 if (g_strcmp0(path, netconfig_get_default_profile()) != 0) {
510                                         g_free(property);
511                                         goto done;
512                                 }
513
514                                 netconfig_update_default_profile();
515                         } else if (g_strcmp0(property, "association") == 0 || g_strcmp0(property, "configuration") == 0) {
516                                 if (netconfig_get_default_profile() == NULL) {
517                                         g_free(property);
518                                         goto done;
519                                 }
520
521                                 if (netconfig_is_cellular_profile(path) && netconfig_is_cellular_internet_profile(path))
522                                         cellular_state_set_service_state(NETCONFIG_CELLULAR_CONNECTING);
523
524                                 if (g_strcmp0(path, netconfig_get_default_profile()) != 0) {
525                                         g_free(property);
526                                         goto done;
527                                 }
528
529                                 netconfig_update_default_profile();
530                         }
531                 }
532
533                 if (netconfig_is_wifi_profile(path) || netconfig_is_ethernet_profile(path)) {
534                         if (g_strcmp0(property, "ready") == 0) {
535                                 if (g_strcmp0(path, netconfig_get_default_profile()) == 0
536                                                 && netconfig_get_default_is_internet())
537                                         netconfig_set_default_is_internet(FALSE);
538
539                                 for (idx = 0; idx < MAX_SOCKET_OPEN_RETRY; idx++) {
540                                         sd = start_ip_conflict_mon();
541                                         if (sd != NULL)
542                                                 break;
543                                 }
544                         } else if (g_strcmp0(property, "online") == 0) {
545                                 if (g_strcmp0(path, netconfig_get_default_profile()) == 0)
546                                         netconfig_set_default_is_internet(TRUE);
547                         } else {
548                                 stop_ip_conflict_mon();
549                         }
550                 }
551
552                 g_free(property);
553         } else if (g_strcmp0(sigvalue, "Proxy") == 0) {
554                 if (g_strcmp0(path, netconfig_get_default_profile()) != 0)
555                         goto done;
556
557                 if (!g_variant_is_of_type(variant, G_VARIANT_TYPE_ARRAY))
558                         goto done;
559
560                 g_variant_get(variant, "a{sv}", &iter);
561                 while (g_variant_iter_loop(iter, "{sv}", &property, &var)) {
562                         GVariantBuilder *builder;
563                         GVariant *sig_params;
564                         if (g_strcmp0(property, "Servers") == 0) {
565                                 GVariantIter *iter_sub = NULL;
566
567                                 g_variant_get(var, "as", &iter_sub);
568                                 g_variant_iter_loop(iter_sub, "s", &value);
569                                 g_variant_iter_free(iter_sub);
570
571                                 DBG("Proxy - [%s]", value);
572                                 if (g_strcmp0(value, netconfig_get_default_proxy()) != 0) {
573                                         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
574
575                                         netconfig_set_default_proxy(value);
576                                         netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, value, TRUE);
577
578                                         g_variant_builder_add(builder, "{sv}", "ProxyAddress",
579                                                                         g_variant_new_string(value));
580
581                                         sig_params = g_variant_new("(@a{sv})",
582                                                                         g_variant_builder_end(builder));
583                                         g_variant_builder_unref(builder);
584
585                                         netconfig_dbus_emit_signal(NULL, NETCONFIG_NETWORK_PATH,
586                                                            NETCONFIG_NETWORK_INTERFACE, "NetworkConfigChanged",
587                                                            sig_params);
588                                 }
589
590                                 g_free(property);
591                                 g_variant_unref(var);
592                                 break;
593                         } else if (g_strcmp0(property, "Method") == 0) {
594                                 value = g_variant_get_string(var, NULL);
595                                 DBG("Method - [%s]", value);
596
597                                 if (g_strcmp0(value, "direct") == 0) {
598                                         if (g_strcmp0("", netconfig_get_default_proxy()) != 0) {
599                                                 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
600
601                                                 netconfig_set_default_proxy("");
602                                                 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, "", TRUE);
603
604                                                 g_variant_builder_add(builder, "{sv}", "ProxyAddress",
605                                                                         g_variant_new_string(""));
606
607                                                 sig_params = g_variant_new("(@a{sv})",
608                                                                         g_variant_builder_end(builder));
609                                                 g_variant_builder_unref(builder);
610
611                                                 netconfig_dbus_emit_signal(NULL, NETCONFIG_NETWORK_PATH,
612                                                                    NETCONFIG_NETWORK_INTERFACE,
613                                                                    "NetworkConfigChanged", sig_params);
614                                         }
615                                 }
616
617                                 g_free(property);
618                                 g_variant_unref(var);
619                                 break;
620                         }
621                 }
622
623                 g_variant_iter_free(iter);
624         } else if (g_strcmp0(sigvalue, "IPv4") == 0) {
625                 if (g_strcmp0(path, netconfig_get_default_profile()) != 0)
626                         goto done;
627
628                 __netconfig_extract_ipv4_signal_data(variant, path);
629         } else if (g_strcmp0(sigvalue, "IPv6") == 0) {
630                 if (g_strcmp0(path, netconfig_get_default_profile()) != 0)
631                         goto done;
632
633                 __netconfig_extract_ipv6_signal_data(variant, path);
634         } else if (g_strcmp0(sigvalue, "Strength") == 0) {
635                 strength = g_variant_get_byte(variant);
636
637                 netconfig_wifi_indicator_update(interface_name, path, strength - 120);
638         } else if (g_strcmp0(sigvalue, "Error") == 0) {
639                 g_variant_get(variant, "s", &property);
640                 INFO("[%s] Property : %s", sigvalue, property);
641 #if defined TIZEN_DEBUG_ENABLE
642                 if (g_strcmp0(property, "dhcp-failed") == 0
643                         || g_strcmp0(property, "connect-failed") == 0
644                         || g_strcmp0(property, "login-failed") == 0
645                         || g_strcmp0(property, "auth-failed") == 0
646                         || g_strcmp0(property, "assoc-failed") == 0
647                         || g_strcmp0(property, "invalid-key") == 0) {
648
649                         INFO("start dump");
650                         _start_dump();
651                 }
652 #endif
653                 g_free(property);
654         }
655 done:
656         g_free(sigvalue);
657
658         if (variant)
659                 g_variant_unref(variant);
660
661         return;
662 }
663
664 static void _dbus_name_changed_cb(GDBusConnection *conn,
665                 const gchar *Name, const gchar *path, const gchar *interface,
666                 const gchar *sig, GVariant *param, gpointer user_data)
667 {
668         gchar *name = NULL;
669         gchar *old = NULL;
670         gchar *new = NULL;
671
672         if (param == NULL)
673                 return;
674
675         g_variant_get(param, "(sss)", &name, &old, &new);
676
677         if (g_strcmp0(name, CONNMAN_SERVICE) == 0 && *new == '\0') {
678                 DBG("ConnMan destroyed: name %s, old %s, new %s", name, old, new);
679
680                 connman_register_agent();
681         } else if (g_strcmp0(name, CLATD_SERVICE) == 0 && *new == '\0') {
682                 DBG("Clat destroyed: name %s, old %s, new %s", name, old, new);
683
684                 /* If clatd is terminated unexpectedly, reset and enable clat service. */
685                 if (NETCONFIG_CELLULAR_ONLINE == cellular_state_get_service_state())
686                         netconfig_clatd_reset();
687         } else if (g_strcmp0(name, SUPPLICANT_SERVICE) == 0 && *new == '\0') {
688                 DBG("Supplicant destroyed: name %s, old %s, new %s", name, old, new);
689                 __mark_supplicant_stopped();
690         }
691         g_free(name);
692         g_free(old);
693         g_free(new);
694
695         return;
696 }
697
698 static void _dbus_vconf_changed_cb(GDBusConnection *conn,
699                 const gchar *Name, const gchar *path, const gchar *interface,
700                 const gchar *sig, GVariant *param, gpointer user_data)
701 {
702         gchar *key = NULL;
703         gchar *type = NULL;
704         gchar *str_val = NULL;
705         int int_val = 0;
706
707         if (param == NULL)
708                 return;
709
710         g_variant_get(param, "(sssi)", &key, &type, &str_val, &int_val);
711
712         if (!key || key[0] == '\0')
713                 goto done;
714
715         DBG("VconfChanged key: %s, type: %s, str_val: %s, int_val: %d",
716                                                 key, type, str_val, int_val);
717
718         if (g_strcmp0(type, "string") == 0 && str_val)
719                 vconf_set_str(key, str_val);
720         else if (g_strcmp0(type, "int") == 0)
721                 vconf_set_int(key, int_val);
722         else if (g_strcmp0(type, "bool") == 0)
723                 vconf_set_bool(key, int_val);
724
725 done:
726         g_free(key);
727         g_free(type);
728         g_free(str_val);
729 }
730
731 static void _scan_changed_cb(GDBusConnection *conn,
732                 const gchar *name, const gchar *path, const gchar *interface,
733                 const gchar *sig, GVariant *param, gpointer user_data)
734 {
735         const char *key = NULL;
736         GVariant *var;
737         gboolean value = false;
738
739         if (g_strcmp0(sig, SIGNAL_SCAN_CHANGED) == 0) {
740                 g_variant_get(param, "(sv)", &key, &var);
741
742                 if (g_strcmp0(key, "scan_started") == 0)
743                         value = true;
744                 else if (g_strcmp0(key, "scan_done") == 0)
745                         value = false;
746
747                 DBG("Scan changed [%s]", (value == true) ? "Started" : "Done");
748                 netconfig_battery_update_wifi_scan(value);
749
750                 if (var != NULL)
751                         g_variant_unref(var);
752         }
753 }
754
755 static void _supplicant_interface_removed(GDBusConnection *conn,
756                 const gchar *name, const gchar *path, const gchar *interface,
757                 const gchar *sig, GVariant *param, gpointer user_data)
758 {
759         char *interface_name = NULL;
760
761         DBG("Interface removed handling!");
762
763         interface_name = __supplicant_get_interface_name(path, interface);
764         if (interface_name == NULL)
765                 return;
766
767         if (netconfig_wifi_bssidscan_get_mode(interface_name) == TRUE)
768                 netconfig_wifi_bssid_signal_scanaborted(interface_name);
769
770         g_free(interface_name);
771         return;
772 }
773
774 static void _supplicant_properties_changed(GDBusConnection *conn,
775                 const gchar *name, const gchar *path, const gchar *interface,
776                 const gchar *sig, GVariant *param, gpointer user_data)
777 {
778         DBG("Properties changed handling!");
779         gchar *key = NULL;
780         const gchar *state = NULL;
781         GVariantIter *iter = NULL;
782         GVariant *variant = NULL;
783         gboolean scanning = FALSE;
784         char *interface_name = NULL;
785
786         if (param == NULL)
787                 return;
788
789         interface_name = __supplicant_get_interface_name(path, interface);
790         if (interface_name == NULL)
791                 return;
792
793         g_variant_get(param, "(a{sv})", &iter);
794         while (g_variant_iter_loop(iter, "{sv}", &key, &variant)) {
795                 if (g_strcmp0(key, "Scanning") == 0) {
796                         scanning = g_variant_get_boolean(variant);
797                         DBG("setting scanning %s", scanning ? "TRUE" : "FALSE");
798                         if (scanning == TRUE)
799                                 netconfig_wifi_scan_set_scanning(interface_name, TRUE);
800                         else
801                                 netconfig_wifi_scan_set_scanning(interface_name, FALSE);
802
803                         g_variant_unref(variant);
804                         g_free(key);
805                         variant = NULL;
806                         key = NULL;
807                         break;
808                 } else if (g_strcmp0(key, "State") == 0) {
809                         state = g_variant_get_string(variant, NULL);
810                         if (state != NULL)
811                                 DBG("Supplicant state : %s", state);
812
813                         g_variant_unref(variant);
814                         g_free(key);
815                         variant = NULL;
816                         key = NULL;
817                         break;
818                 } else if (g_strcmp0(key, "DisconnectReason") == 0) {
819                         int reason = g_variant_get_int32(variant);
820                         DBG("Supplicant DisconnReason : %d", reason);
821
822                         g_variant_unref(variant);
823                         g_free(key);
824                         variant = NULL;
825                         key = NULL;
826                         break;
827                 } else {
828                         gchar *value;
829                         value = g_variant_print(variant, TRUE);
830                         DBG("Supplicant %s : %s", key, value);
831
832                         g_free(value);
833                 }
834         }
835
836         g_variant_iter_free(iter);
837         g_free(interface_name);
838         return;
839 }
840
841 static void _supplicant_bss_added(GDBusConnection *conn,
842                 const gchar *name, const gchar *path, const gchar *interface,
843                 const gchar *sig, GVariant *param, gpointer user_data)
844 {
845         DBG("BSS added handling!");
846         wifi_state_set_bss_found(TRUE);
847
848         return;
849 }
850
851 static void _supplicant_scan_done(GDBusConnection *conn,
852                 const gchar *name, const gchar *path, const gchar *interface,
853                 const gchar *sig, GVariant *param, gpointer user_data)
854 {
855         char *interface_name = NULL;
856
857         DBG("Scan Done handling!");
858
859         interface_name = __supplicant_get_interface_name(path, interface);
860         if (interface_name == NULL)
861                 return;
862
863         netconfig_wifi_scan_set_scanning(interface_name, FALSE);
864
865         if (netconfig_wifi_bssidscan_get_mode(interface_name) == TRUE) {
866                 DBG("%s bssid scan done!", interface_name);
867                 netconfig_wifi_bssid_signal_scandone(interface_name);
868                 if (wifi_state_get_technology_state(interface_name)
869                                 < NETCONFIG_WIFI_TECH_POWERED) {
870                         g_free(interface_name);
871                         return;
872                 }
873         }
874
875         if (netconfig_wifi_bgscan_get_mode(interface_name) == TRUE) {
876                 DBG("%s bgscan done!", interface_name);
877                 if (wifi_state_get_technology_state(interface_name)
878                                 >= NETCONFIG_WIFI_TECH_POWERED) {
879                         netconfig_wifi_bgscan_start(interface_name, FALSE);
880                 }
881
882                 wifi_start_timer_network_notification(interface_name);
883         }
884
885         g_free(interface_name);
886         return;
887 }
888
889 static void _supplicant_driver_hanged(GDBusConnection *conn,
890                 const gchar *name, const gchar *path, const gchar *interface,
891                 const gchar *sig, GVariant *param, gpointer user_data)
892 {
893         char *interface_name = NULL;
894
895         DBG("Driver Hanged handling!");
896         ERR("Critical. Wi-Fi firmware crashed");
897
898 #if !defined TIZEN_WEARABLE
899         netconfig_send_message_to_net_popup("Wi-Fi Error",
900                 "Wi-Fi Driver Hanged. Please get log dump and report", "popup", NULL);
901 #endif
902
903         interface_name = __supplicant_get_interface_name(path, interface);
904         if (interface_name == NULL)
905                 return;
906
907         wifi_power_recover_firmware(interface_name);
908
909         g_free(interface_name);
910         return;
911
912 }
913
914 static void _supplicant_session_overlapped(GDBusConnection *conn,
915                 const gchar *name, const gchar *path, const gchar *interface,
916                 const gchar *sig, GVariant *param, gpointer user_data)
917 {
918         DBG("Driver session overlapped handling!");
919         ERR("WPS PBC SESSION OVERLAPPED");
920 #if defined TIZEN_WEARABLE
921         return;
922 #else
923         netconfig_send_message_to_net_popup("WPS Error",
924                                         "wps session overlapped", "popup", NULL);
925 #endif
926 }
927
928 #if defined TIZEN_DEBUG_ENABLE
929 static void __netconfig_dumpservice_handler(GDBusConnection *conn,
930                 const gchar *name, const gchar *path, const gchar *interface,
931                 const gchar *sig, GVariant *param, gpointer user_data)
932 {
933         int mode;
934         gchar *signal_path = NULL;
935
936         if (param == NULL)
937                 return;
938
939         g_variant_get(param, "(is)", &mode, &signal_path);
940         DBG("Path: %s and mode: %d", signal_path, mode);
941
942         netconfig_dump_log(signal_path);
943         if (signal_path)
944                 g_free(signal_path);
945
946         return;
947 }
948 #endif
949
950 static void _supplicant_tdls_connected(GDBusConnection *conn,
951                 const gchar *name, const gchar *path, const gchar *interface,
952                 const gchar *sig, GVariant *param, gpointer user_data)
953 {
954         char *interface_name = NULL;
955
956         DBG("Received TDLS Connected Signal");
957
958         interface_name = __supplicant_get_interface_name(path, interface);
959         if (interface_name == NULL)
960                 return;
961
962         netconfig_wifi_tdls_connected_event(interface_name, param);
963
964         g_free(interface_name);
965         return;
966 }
967
968 static void _supplicant_tdls_disconnected(GDBusConnection *conn,
969                 const gchar *name, const gchar *path, const gchar *interface,
970                 const gchar *sig, GVariant *param, gpointer user_data)
971 {
972         char *interface_name = NULL;
973
974         DBG("Received TDLS Disconnected Signal");
975
976         interface_name = __supplicant_get_interface_name(path, interface);
977         if (interface_name == NULL)
978                 return;
979
980         netconfig_wifi_tdls_disconnected_event(interface_name, param);
981
982         g_free(interface_name);
983         return;
984 }
985
986 static void _supplicant_tdls_peer_found(GDBusConnection *conn,
987                 const gchar *name, const gchar *path, const gchar *interface,
988                 const gchar *sig, GVariant *param, gpointer user_data)
989 {
990         char *interface_name = NULL;
991
992         DBG("Received TDLS Peer Found Signal");
993
994         interface_name = __supplicant_get_interface_name(path, interface);
995         if (interface_name == NULL)
996                 return;
997
998         netconfig_wifi_tdls_peer_found_event(interface_name, param);
999
1000         g_free(interface_name);
1001         return;
1002 }
1003
1004 static void _supplicant_dpp_auth_success(GDBusConnection *conn,
1005                 const gchar *name, const gchar *path, const gchar *interface,
1006                 const gchar *sig, GVariant *param, gpointer user_data)
1007 {
1008         DBG("Received DPP Auth Success Signal");
1009         netconfig_wifi_dpp_auth_success_event(param);
1010         return;
1011 }
1012
1013 static void _supplicant_dpp_auth_failed(GDBusConnection *conn,
1014                 const gchar *name, const gchar *path, const gchar *interface,
1015                 const gchar *sig, GVariant *param, gpointer user_data)
1016 {
1017         DBG("Received DPP Auth Failed Signal");
1018         netconfig_wifi_dpp_auth_failed_event(param);
1019         return;
1020 }
1021
1022 static void _supplicant_dpp_not_compatible(GDBusConnection *conn,
1023                 const gchar *name, const gchar *path, const gchar *interface,
1024                 const gchar *sig, GVariant *param, gpointer user_data)
1025 {
1026         DBG("Received DPP Not Compatible Signal");
1027         netconfig_wifi_dpp_not_compatible_event(param);
1028         return;
1029 }
1030
1031 static void _supplicant_dpp_conf_failed(GDBusConnection *conn,
1032                 const gchar *name, const gchar *path, const gchar *interface,
1033                 const gchar *sig, GVariant *param, gpointer user_data)
1034 {
1035         DBG("Received DPP Conf Failed Signal");
1036         netconfig_wifi_dpp_conf_failed_event(param);
1037         return;
1038 }
1039
1040 static void _supplicant_dpp_scan_peer_qr(GDBusConnection *conn,
1041                 const gchar *name, const gchar *path, const gchar *interface,
1042                 const gchar *sig, GVariant *param, gpointer user_data)
1043 {
1044         DBG("Received DPP Scan Peer QR Signal");
1045         netconfig_wifi_dpp_scan_peer_qr_event(param);
1046         return;
1047 }
1048
1049 static void _supplicant_dpp_network_id(GDBusConnection *conn,
1050                 const gchar *name, const gchar *path, const gchar *interface,
1051                 const gchar *sig, GVariant *param, gpointer user_data)
1052 {
1053         DBG("Received DPP Network Id Signal");
1054         netconfig_wifi_dpp_network_id_event(param);
1055         return;
1056 }
1057
1058 static void _supplicant_dpp_conf_sent(GDBusConnection *conn,
1059                 const gchar *name, const gchar *path, const gchar *interface,
1060                 const gchar *sig, GVariant *param, gpointer user_data)
1061 {
1062         DBG("Received DPP Conf Sent Signal");
1063         netconfig_wifi_dpp_conf_sent_event(param);
1064         return;
1065 }
1066
1067 static void _supplicant_dpp_conf_received(GDBusConnection *conn,
1068                 const gchar *name, const gchar *path, const gchar *interface,
1069                 const gchar *sig, GVariant *param, gpointer user_data)
1070 {
1071         DBG("Received DPP Conf Received Signal");
1072         netconfig_wifi_dpp_conf_received_event(param);
1073         return;
1074 }
1075
1076 static void _supplicant_dpp_conf_obj(GDBusConnection *conn,
1077                 const gchar *name, const gchar *path, const gchar *interface,
1078                 const gchar *sig, GVariant *param, gpointer user_data)
1079 {
1080         char *interface_name = NULL;
1081
1082         DBG("Received DPP Conf Obj Signal");
1083
1084         interface_name = __supplicant_get_interface_name(path, interface);
1085         if (interface_name == NULL)
1086                 return;
1087
1088         netconfig_wifi_dpp_conf_obj_event(interface_name, param);
1089
1090         g_free(interface_name);
1091         return;
1092 }
1093
1094 static void _supplicant_wifi_wps_connected(const char *interface_name, GVariant *param)
1095 {
1096         gchar *key;
1097         char ssid[32] = {0, };
1098         gchar *name;
1099         GVariantIter *iter;
1100         GVariant *variant;
1101         int config_error = 0;
1102         int error_indication = 0;
1103         gsize ssid_len = 0;
1104
1105         if (param == NULL) {
1106                 ERR("Param is NULL");
1107                 return;
1108         }
1109
1110         g_variant_get(param, "(sa{sv})", &name, &iter);
1111         INFO("wps Result: %s", name);
1112         while (g_variant_iter_loop(iter, "{sv}", &key, &variant)) {
1113                 INFO("wps Key is %s", key);
1114                 if (g_strcmp0(key, "SSID") == 0) {
1115                         const char *t_key = NULL;
1116                         t_key = g_variant_get_fixed_array(variant, &ssid_len, sizeof(guchar));
1117                         INFO("wps ssid_len is %zd ", ssid_len);
1118                         if (t_key == NULL) {
1119                                 g_free(key);
1120                                 g_variant_unref(variant);
1121                                 ERR("WPS PBC Connection Failed");
1122                                 goto error;
1123                         }
1124                         if (ssid_len > 0 && ssid_len <= 32) {
1125                                 memcpy(ssid, t_key, ssid_len);
1126                         } else {
1127                                 memset(ssid, 0, sizeof(ssid));
1128                                 ssid_len = 0;
1129                         }
1130                         INFO("WPS PBC Connection completed with AP %s", ssid);
1131                         netconfig_wifi_notify_wps_completed(interface_name,
1132                                 ssid, ssid_len);
1133                 }
1134         }
1135
1136         g_variant_iter_free(iter);
1137         g_free(name);
1138         return;
1139
1140 error:
1141         g_variant_iter_free(iter);
1142         g_free(name);
1143         error_indication = WPS_EI_OPERATION_FAILED;
1144         config_error = WPS_CFG_NO_ERROR;
1145         ERR("Error Occured! Notifying Fail Event");
1146         netconfig_wifi_notify_wps_fail_event(interface_name,
1147                 config_error, error_indication);
1148
1149 }
1150
1151 static void _supplicant_wifi_wps_event(const char *interface_name, GVariant *param)
1152 {
1153         gchar *key;
1154         gchar *name;
1155         GVariantIter *iter;
1156         GVariant *variant;
1157         gint32 config_error = 0;
1158         gint32 error_indication = 0;
1159
1160         if (param == NULL) {
1161                 ERR("Param is NULL");
1162                 return;
1163         }
1164
1165         g_variant_get(param, "(sa{sv})", &name, &iter);
1166         INFO("Event Result: %s", name);
1167         if (g_strcmp0(name, "fail") == 0) {
1168                 while (g_variant_iter_loop(iter, "{sv}", &key, &variant)) {
1169                         if (key == NULL)
1170                                 goto error;
1171                         INFO("Key is %s", key);
1172                         if (g_strcmp0(key, "config_error") == 0) {
1173                                 config_error = g_variant_get_int32(variant);
1174                                 ERR("Config Error %d", config_error);
1175                         } else if (g_strcmp0(key, "error_indication") == 0) {
1176                                 error_indication = g_variant_get_int32(variant);
1177                                 ERR("Error Indication %d", error_indication);
1178                         }
1179                 }
1180                 netconfig_wifi_notify_wps_fail_event(interface_name,
1181                         config_error, error_indication);
1182         }
1183
1184         g_variant_iter_free(iter);
1185         g_free(name);
1186         return;
1187
1188 error:
1189         g_variant_iter_free(iter);
1190         g_free(name);
1191         error_indication = WPS_EI_OPERATION_FAILED;
1192         config_error = WPS_CFG_NO_ERROR;
1193         ERR("Error Occured! Notifying Fail Event");
1194         netconfig_wifi_notify_wps_fail_event(interface_name,
1195                 config_error, error_indication);
1196 }
1197
1198 static void _find_service_and_set_passphrase(char *ssid, char *key)
1199 {
1200         GString *str;
1201         GVariant *reply = NULL;
1202         int i, j, ssid_len;
1203         char *service_path;
1204         char *dev_mac_addr;
1205         char ident[13];
1206         gchar *enc_passphrase;
1207
1208         ssid_len = strlen(ssid);
1209
1210         str = g_string_sized_new((ssid_len * 2) + 55);
1211         if (!str)
1212                 return;
1213
1214         dev_mac_addr = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
1215         if (!dev_mac_addr) {
1216                 ERR("Failed to get mac address from vconf key");
1217                 g_string_free(str, TRUE);
1218                 return;
1219         }
1220
1221         j = 0;
1222         for (i = 0; dev_mac_addr[i]; i++) {
1223                 if (dev_mac_addr[i] != ':')
1224                         ident[j++] = dev_mac_addr[i];
1225         }
1226         ident[j] = '\0';
1227
1228         g_string_append_printf(str, "%s%s_", CONNMAN_WIFI_SERVICE_PROFILE_PREFIX,
1229                                                    ident);
1230         free(dev_mac_addr);
1231
1232         for (i = 0; i < ssid_len; i++) {
1233                 if (ssid[i] != '"')
1234                         g_string_append_printf(str, "%02x", ssid[i]);
1235         }
1236
1237         g_string_append_printf(str, "_managed_psk");
1238
1239         service_path = g_string_free(str, FALSE);
1240
1241         enc_passphrase = _netconfig_encrypt_passphrase(key);
1242         if (!enc_passphrase) {
1243                 ERR("Failed to encrypt passphrase");
1244                 g_free(service_path);
1245                 return;
1246         }
1247
1248         INFO("wps service_path %s Passphrase %s", service_path, enc_passphrase);
1249
1250         reply = netconfig_invoke_dbus_method(CONNMAN_SERVICE, service_path,
1251                                          CONNMAN_SERVICE_INTERFACE, "SetProperty",
1252                                          g_variant_new("(sv)", "Passphrase",
1253                                                                    g_variant_new_string(enc_passphrase)));
1254         if (reply != NULL)
1255                 g_variant_unref(reply);
1256         else
1257                 ERR("Failed to set passphrase");
1258
1259         g_free(service_path);
1260
1261         return;
1262 }
1263
1264 static void _supplicant_wifi_wps_credentials(const char *interface_name, GVariant *param)
1265 {
1266         gchar *key;
1267         char ssid[32] = {0, };
1268         char wps_key[100] = {0, };
1269         GVariantIter *iter;
1270         GVariant *variant;
1271         int config_error = 0;
1272         int error_indication = 0;
1273         gsize ssid_len = 0;
1274         char *key_mgmt = NULL;
1275
1276         if (param == NULL) {
1277                 ERR("Param is NULL");
1278                 return;
1279         }
1280
1281         g_variant_get(param, "(a{sv})", &iter);
1282         while (g_variant_iter_loop(iter, "{sv}", &key, &variant)) {
1283                 if (key == NULL)
1284                         goto error;
1285                 INFO("wps Key is %s", key);
1286                 if (g_strcmp0(key, "Key") == 0) {
1287                         gsize key_len = 0;
1288                         const char *t_key = NULL;
1289                         key_len = g_variant_get_size(variant);
1290
1291                         INFO("wps password len %zd ", key_len);
1292                         if (key_len > 0) {
1293                                 t_key = g_variant_get_fixed_array(variant, &key_len,
1294                                                   sizeof(guchar));
1295                                 if (!t_key) {
1296                                         g_free(key);
1297                                         g_variant_unref(variant);
1298                                         goto error;
1299                                 }
1300                                 strncpy(wps_key, t_key, key_len);
1301                                 wps_key[key_len] = '\0';
1302                                 INFO("WPS Key in process credentials %s", wps_key);
1303                         } else
1304                                 SLOGI("WPS AP Security ->Open");
1305                 } else if (g_strcmp0(key, "SSID") == 0) {
1306                         const char *t_key = NULL;
1307                         t_key = g_variant_get_fixed_array(variant, &ssid_len,
1308                                                   sizeof(guchar));
1309                         INFO("wps ssid_len is %zd ", ssid_len);
1310                         if (!t_key) {
1311                                 g_free(key);
1312                                 g_variant_unref(variant);
1313                                 goto error;
1314                         }
1315                         if (ssid_len > 0 && ssid_len <= 32) {
1316                                 memcpy(ssid, t_key, ssid_len);
1317                         } else {
1318                                 memset(ssid, 0, sizeof(ssid));
1319                                 ssid_len = 0;
1320                         }
1321                         INFO("SSID in process credentials %s", ssid);
1322                 } else if (g_strcmp0(key, "AuthType") == 0) {
1323                         gchar *auth_type;
1324                         GVariantIter *var_iter;
1325                         g_variant_get(variant, "as", &var_iter);
1326                         while (g_variant_iter_loop(var_iter, "s", &auth_type)) {
1327                                 INFO("wps auth_type %s", auth_type);
1328                                 if (g_strcmp0(auth_type, "wpa-psk") == 0 ||
1329                                         g_strcmp0(auth_type, "wpa2-psk") == 0)
1330                                         key_mgmt = "psk";
1331                                 else if (g_strcmp0(auth_type, "open") == 0)
1332                                         key_mgmt = "none";
1333                         }
1334                         g_variant_iter_free(var_iter);
1335                 } else if (g_strcmp0(key, "EncrType") == 0) {
1336                         gchar *encr_type;
1337                         GVariantIter *var_iter;
1338                         g_variant_get(variant, "as", &var_iter);
1339                         while (g_variant_iter_loop(var_iter, "s", &encr_type))
1340                                 INFO("wps encr_type %s", encr_type);
1341                         g_variant_iter_free(var_iter);
1342                 } else if (g_strcmp0(key, "BSSID") == 0) {
1343                         const guchar *bssid;
1344                         gsize bssid_len;
1345                         bssid = g_variant_get_fixed_array(variant, &bssid_len,
1346                                                                 sizeof(guchar));
1347                         if (bssid && bssid_len == 6)
1348                                 INFO("wps BSSID %2x:%2x:%2x:%2x:%2x:%2x", bssid[0], bssid[1],
1349                                          bssid[2], bssid[3], bssid[4], bssid[5]);
1350                 }
1351         }
1352
1353         g_variant_iter_free(iter);
1354
1355         if (g_strcmp0(key_mgmt, "psk") == 0)
1356                 _find_service_and_set_passphrase(ssid, wps_key);
1357
1358         netconfig_wifi_notify_wps_credentials(interface_name, ssid, ssid_len, wps_key);
1359         return;
1360
1361 error:
1362         g_variant_iter_free(iter);
1363         error_indication = WPS_EI_OPERATION_FAILED;
1364         config_error = WPS_CFG_NO_ERROR;
1365         ERR("Error Occured! Notifying Fail Event");
1366         netconfig_wifi_notify_wps_fail_event(interface_name, config_error, error_indication);
1367 }
1368
1369 static void __netconfig_wps_signal_filter_handler(GDBusConnection *conn,
1370                 const gchar *name, const gchar *path, const gchar *interface,
1371                 const gchar *sig, GVariant *param, gpointer user_data)
1372 {
1373         char *interface_name = NULL;
1374
1375         interface_name = __supplicant_get_interface_name(path, SUPPLICANT_IFACE_INTERFACE);
1376         if (interface_name == NULL)
1377                 return;
1378
1379         if (g_strcmp0(sig, SIGNAL_WPS_CREDENTIALS) == 0) {
1380                 INFO("Received wps CREDENTIALS Signal from Supplicant");
1381                 _supplicant_wifi_wps_credentials(interface_name, param);
1382         } else if (g_strcmp0(sig, SIGNAL_WPS_EVENT) == 0) {
1383                 INFO("Received wps EVENT Signal from Supplicant");
1384                 _supplicant_wifi_wps_event(interface_name, param);
1385         } else if (g_strcmp0(sig, SIGNAL_WPS_CONNECTED) == 0) {
1386                 INFO("Received WPSConnected Signal from Supplicant");
1387                 _supplicant_wifi_wps_connected(interface_name, param);
1388         }
1389
1390         g_free(interface_name);
1391         return;
1392 }
1393
1394 static void __telephony_svc_cs_changed_cb(keynode_t * node, void *data)
1395 {
1396         int telephony_svc_cs = 0;
1397
1398         if (node != NULL)
1399                 telephony_svc_cs = vconf_keynode_get_int(node);
1400         else
1401                 netconfig_vconf_get_int(VCONFKEY_TELEPHONY_SVC_CS, &telephony_svc_cs);
1402
1403         DBG("Telephony svc cs [%d]", telephony_svc_cs);
1404
1405         switch (telephony_svc_cs) {
1406         case 1:
1407                 netconfig_battery_end_dn();
1408                 break;
1409         case 2:
1410                 netconfig_battery_start_dn();
1411                 break;
1412         default:
1413                 break;
1414         }
1415 }
1416
1417 static void __telephony_rssi_changed_cb(keynode_t * node, void *data)
1418 {
1419         int rssi = 0;
1420
1421         if (node != NULL)
1422                 rssi = vconf_keynode_get_int(node);
1423         else
1424                 netconfig_vconf_get_int(VCONFKEY_TELEPHONY_RSSI, &rssi);
1425
1426         DBG("Telephony rssi [%d]", rssi);
1427
1428         netconfig_battery_update_dn_rssi(rssi);
1429 }
1430
1431 static void _notify_telephony_svc_cs(void)
1432 {
1433         int telephony_svc_cs = 0;
1434
1435         netconfig_vconf_get_int(VCONFKEY_TELEPHONY_SVC_CS, &telephony_svc_cs);
1436         DBG("Telephony svc cs [%d]", telephony_svc_cs);
1437
1438         switch (telephony_svc_cs) {
1439         case 1:
1440                 netconfig_battery_end_dn();
1441                 break;
1442         case 2:
1443                 netconfig_battery_start_dn();
1444                 break;
1445         default:
1446                 break;
1447         }
1448
1449         vconf_notify_key_changed(VCONFKEY_TELEPHONY_SVC_CS, __telephony_svc_cs_changed_cb, NULL);
1450 }
1451
1452 static void _notify_telephony_rssi(void)
1453 {
1454         int rssi = 0;
1455
1456         netconfig_vconf_get_int(VCONFKEY_TELEPHONY_RSSI, &rssi);
1457         DBG("Telephony rssi [%d]", rssi);
1458
1459         netconfig_battery_update_dn_rssi(rssi);
1460
1461         vconf_notify_key_changed(VCONFKEY_TELEPHONY_RSSI, __telephony_rssi_changed_cb, NULL);
1462 }
1463
1464 static void _ignore_telephony_svc_cs(void)
1465 {
1466         vconf_ignore_key_changed(VCONFKEY_TELEPHONY_SVC_CS, __telephony_svc_cs_changed_cb);
1467 }
1468
1469 static void _ignore_telephony_rssi(void)
1470 {
1471         vconf_ignore_key_changed(VCONFKEY_TELEPHONY_RSSI, __telephony_rssi_changed_cb);
1472 }
1473
1474 static supplicant_signal_cb supplicant_cbs[SIG_MAX] = {
1475                 _supplicant_interface_removed,
1476                 _supplicant_properties_changed,
1477                 _supplicant_bss_added,
1478                 _supplicant_scan_done,
1479                 _supplicant_driver_hanged,
1480                 _supplicant_session_overlapped,
1481                 _supplicant_tdls_connected,
1482                 _supplicant_tdls_disconnected,
1483                 _supplicant_tdls_peer_found,
1484                 _supplicant_dpp_auth_success,
1485                 _supplicant_dpp_auth_failed,
1486                 _supplicant_dpp_not_compatible,
1487                 _supplicant_dpp_conf_failed,
1488                 _supplicant_dpp_scan_peer_qr,
1489                 _supplicant_dpp_network_id,
1490                 _supplicant_dpp_conf_sent,
1491                 _supplicant_dpp_conf_received,
1492                 _supplicant_dpp_conf_obj,
1493 };
1494
1495 void register_gdbus_signal_container(void)
1496 {
1497         GDBusConnection *connection = NULL;
1498         connection = netdbus_get_connection();
1499
1500         if (connection == NULL) {
1501                 ERR("Failed to get GDbus Connection");
1502                 return;
1503         }
1504
1505         netconfig_subscription_id = g_dbus_connection_signal_subscribe(
1506                         connection,
1507                         NETCONFIG_SERVICE,
1508                         NETCONFIG_NETWORK_INTERFACE,
1509                         NETCONFIG_SIGNAL_VCONF_CHANGED,
1510                         NETCONFIG_NETWORK_PATH,
1511                         NULL,
1512                         G_DBUS_SIGNAL_FLAGS_NONE,
1513                         _dbus_vconf_changed_cb,
1514                         NULL,
1515                         NULL);
1516
1517         INFO("Successfully register host net-config DBus signal filter");
1518 }
1519
1520 void register_gdbus_signal(void)
1521 {
1522         GDBusConnection *connection = NULL;
1523         SuppSigArrayIndex sig;
1524         connection = netdbus_get_connection();
1525
1526         if (connection == NULL) {
1527                 ERR("Failed to get GDbus Connection");
1528                 return;
1529         }
1530
1531         /* listening to messages from all objects as no path is specified */
1532         /* see signals from the given interface */
1533         conn_subscription_ids[0] = g_dbus_connection_signal_subscribe(
1534                         connection,
1535                         CONNMAN_SERVICE,
1536                         CONNMAN_TECHNOLOGY_INTERFACE,
1537                         NULL,
1538                         NULL,
1539                         NULL,
1540                         G_DBUS_SIGNAL_FLAGS_NONE,
1541                         _technology_signal_cb,
1542                         NULL,
1543                         NULL);
1544
1545         conn_subscription_ids[1] = g_dbus_connection_signal_subscribe(
1546                         connection,
1547                         CONNMAN_SERVICE,
1548                         CONNMAN_SERVICE_INTERFACE,
1549                         CONNMAN_SIGNAL_PROPERTY_CHANGED,
1550                         NULL,
1551                         NULL,
1552                         G_DBUS_SIGNAL_FLAGS_NONE,
1553                         _service_signal_cb,
1554                         NULL,
1555                         NULL);
1556
1557         conn_subscription_ids[2] = g_dbus_connection_signal_subscribe(
1558                         connection,
1559                         DBUS_SERVICE_DBUS,
1560                         DBUS_INTERFACE_DBUS,
1561                         SIGNAL_NAME_OWNER_CHANGED,
1562                         NULL,
1563                         CONNMAN_SERVICE,
1564                         G_DBUS_SIGNAL_FLAGS_NONE,
1565                         _dbus_name_changed_cb,
1566                         NULL,
1567                         NULL);
1568
1569         conn_subscription_ids[3] = g_dbus_connection_signal_subscribe(
1570                         connection,
1571                         CONNMAN_SERVICE,
1572                         CONNMAN_MANAGER_INTERFACE,
1573                         SIGNAL_SCAN_CHANGED,
1574                         CONNMAN_MANAGER_PATH,
1575                         NULL,
1576                         G_DBUS_SIGNAL_FLAGS_NONE,
1577                         _scan_changed_cb,
1578                         NULL,
1579                         NULL);
1580
1581         INFO("Successfully register connman DBus signal filters");
1582
1583         conn_subscription_ids[4] = g_dbus_connection_signal_subscribe(
1584                         connection,
1585                         SUPPLICANT_SERVICE,
1586                         SUPPLICANT_INTERFACE ".Interface.WPS",
1587                         NULL,
1588                         NULL,
1589                         NULL,
1590                         G_DBUS_SIGNAL_FLAGS_NONE,
1591                         __netconfig_wps_signal_filter_handler,
1592                         NULL,
1593                         NULL);
1594
1595         INFO("Successfully register Supplicant WPS DBus signal filters");
1596
1597         conn_subscription_ids[5] = g_dbus_connection_signal_subscribe(
1598                         connection,
1599                         DBUS_SERVICE_DBUS,
1600                         DBUS_INTERFACE_DBUS,
1601                         SIGNAL_NAME_OWNER_CHANGED,
1602                         NULL,
1603                         CLATD_SERVICE,
1604                         G_DBUS_SIGNAL_FLAGS_NONE,
1605                         _dbus_name_changed_cb,
1606                         NULL,
1607                         NULL);
1608
1609         conn_subscription_ids[6] = g_dbus_connection_signal_subscribe(
1610                         connection,
1611                         DBUS_SERVICE_DBUS,
1612                         DBUS_INTERFACE_DBUS,
1613                         SIGNAL_NAME_OWNER_CHANGED,
1614                         NULL,
1615                         SUPPLICANT_SERVICE,
1616                         G_DBUS_SIGNAL_FLAGS_NONE,
1617                         _dbus_name_changed_cb,
1618                         NULL,
1619                         NULL);
1620
1621         INFO("Successfully register clat DBus signal filters");
1622
1623         for (sig = SIG_INTERFACE_REMOVED; sig < SIG_MAX; sig++) {
1624                 supp_subscription_ids[sig] = g_dbus_connection_signal_subscribe(
1625                                 connection,
1626                                 SUPPLICANT_SERVICE,
1627                                 supplicant_iface_for_sig[sig],
1628                                 supplicant_signals[sig],
1629                                 NULL,
1630                                 NULL,
1631                                 G_DBUS_SIGNAL_FLAGS_NONE,
1632                                 supplicant_cbs[sig],
1633                                 NULL,
1634                                 NULL);
1635         }
1636
1637         INFO("Successfully register Supplicant DBus signal filters");
1638
1639 #if defined TIZEN_DEBUG_ENABLE
1640         dumpservice_subscription_id = g_dbus_connection_signal_subscribe(
1641                         connection,
1642                         NULL,
1643                         DUMP_SERVICE_INTERFACE,
1644                         DUMP_SIGNAL,
1645                         NULL,
1646                         NULL,
1647                         G_DBUS_SIGNAL_FLAGS_NONE,
1648                         __netconfig_dumpservice_handler,
1649                         NULL,
1650                         NULL);
1651
1652         INFO("Successfully registered Dumpservice DBus signal filter");
1653 #endif
1654
1655         _notify_telephony_svc_cs();
1656         _notify_telephony_rssi();
1657
1658         INFO("Successfully register Telephony vconf keys");
1659
1660         /* In case ConnMan precedes this signal register,
1661          * net-config should update the default connected profile.
1662          */
1663         netconfig_update_default();
1664 }
1665
1666 void deregister_gdbus_signal_container(void)
1667 {
1668         GDBusConnection *connection = NULL;
1669
1670         connection = netdbus_get_connection();
1671         if (!connection) {
1672                 ERR("Already de-registered. Nothing to be done");
1673                 return;
1674         }
1675
1676         if (netconfig_subscription_id)
1677                 g_dbus_connection_signal_unsubscribe(connection,
1678                                 netconfig_subscription_id);
1679 }
1680
1681 void deregister_gdbus_signal(void)
1682 {
1683         GDBusConnection *connection = NULL;
1684         int signal;
1685         SuppSigArrayIndex sig;
1686         connection = netdbus_get_connection();
1687         if (!connection) {
1688                 ERR("Already de-registered. Nothing to be done");
1689                 return;
1690         }
1691
1692         for (signal = 0; signal < TOTAL_CONN_SIGNALS; signal++) {
1693                 if (conn_subscription_ids[signal]) {
1694                         g_dbus_connection_signal_unsubscribe(connection,
1695                                                 conn_subscription_ids[signal]);
1696                 }
1697         }
1698
1699         for (sig = SIG_INTERFACE_REMOVED; sig < SIG_MAX; sig++) {
1700                 if (supp_subscription_ids[sig]) {
1701                         g_dbus_connection_signal_unsubscribe(connection,
1702                                                 supp_subscription_ids[sig]);
1703                 }
1704         }
1705
1706 #if defined TIZEN_DEBUG_ENABLE
1707         g_dbus_connection_signal_unsubscribe(connection,
1708                         dumpservice_subscription_id);
1709 #endif
1710
1711         _ignore_telephony_svc_cs();
1712         _ignore_telephony_rssi();
1713 }