Added dump log for connect fail #2
[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-bssid-scan.h"
33 #include "wifi-agent.h"
34 #include "wifi-power.h"
35 #include "wifi-state.h"
36 #include "netsupplicant.h"
37 #include "network-state.h"
38 #include "cellular-state.h"
39 #include "signal-handler.h"
40 #include "wifi-background-scan.h"
41 #include "wifi-tdls.h"
42 #include "ip-conflict-detect.h"
43 #if defined TIZEN_DEBUG_ENABLE
44 #include "network-dump.h"
45 #define NETWORK_LOG_DUMP_SCRIPT  "/opt/var/lib/net-config/network_log_dump.sh"
46 #define MAX_SIZE_ERROR_BUFFER 256
47 #endif
48
49 #define DBUS_SERVICE_DBUS                       "org.freedesktop.DBus"
50 #define DBUS_INTERFACE_DBUS                     "org.freedesktop.DBus"
51 #define SIGNAL_INTERFACE_REMOVED                "InterfaceRemoved"
52 #define SIGNAL_SCAN_DONE                        "ScanDone"
53 #define SIGNAL_BSS_ADDED                        "BSSAdded"
54 #define SIGNAL_PROPERTIES_CHANGED               "PropertiesChanged"
55 #define SIGNAL_PROPERTIES_DRIVER_HANGED         "DriverHanged"
56 #define SIGNAL_PROPERTIES_SESSION_OVERLAPPED    "SessionOverlapped"
57 #define SIGNAL_TDLS_CONNECTED                           "TDLSConnected"
58 #define SIGNAL_TDLS_DISCONNECTED                        "TDLSDisconnected"
59 #define SIGNAL_TDLS_PEER_FOUND                          "TDLSPeerFound"
60
61 #define SIGNAL_WPS_CONNECTED                            "WPSConnected"
62 #define SIGNAL_WPS_EVENT                                        "Event"
63 #define SIGNAL_WPS_CREDENTIALS                          "Credentials"
64
65 #define CONNMAN_SIGNAL_SERVICES_CHANGED         "ServicesChanged"
66 #define CONNMAN_SIGNAL_PROPERTY_CHANGED         "PropertyChanged"
67 #define CONNMAN_SIGNAL_NAME_CHANGED             "NameOwnerChanged"
68
69 #define MAX_SIG_LEN 64
70 #define TOTAL_CONN_SIGNALS 5
71 #define MAX_SOCKET_OPEN_RETRY 5
72
73 typedef enum {
74         SIG_INTERFACE_REMOVED = 0,
75         SIG_PROPERTIES_CHANGED,
76         SIG_BSS_ADDED,
77         SIG_SCAN_DONE,
78         SIG_DRIVER_HANGED,
79         SIG_SESSION_OVERLAPPED,
80         SIG_TDLS_CONNECTED,
81         SIG_TDLS_DISCONNECTED,
82         SIG_TDLS_PEER_FOUND,
83         SIG_MAX
84 } SuppSigArrayIndex;
85
86 static int conn_subscription_ids[TOTAL_CONN_SIGNALS] = {0};
87 static const char supplicant_signals[SIG_MAX][MAX_SIG_LEN] = {
88                 SIGNAL_INTERFACE_REMOVED,
89                 SIGNAL_PROPERTIES_CHANGED,
90                 SIGNAL_BSS_ADDED,
91                 SIGNAL_SCAN_DONE,
92                 SIGNAL_PROPERTIES_DRIVER_HANGED,
93                 SIGNAL_PROPERTIES_SESSION_OVERLAPPED,
94                 SIGNAL_TDLS_CONNECTED,
95                 SIGNAL_TDLS_DISCONNECTED,
96                 SIGNAL_TDLS_PEER_FOUND,
97 };
98
99 static int supp_subscription_ids[SIG_MAX] = {0};
100 #if defined TIZEN_DEBUG_ENABLE
101 static int dumpservice_subscription_id = 0;
102 #endif
103
104 typedef void (*supplicant_signal_cb)(GDBusConnection *conn,
105                 const gchar *name, const gchar *path, const gchar *interface,
106                 const gchar *sig, GVariant *param, gpointer user_data);
107 typedef void (*connman_signal_cb)(GDBusConnection *conn,
108                 const gchar *name, const gchar *path, const gchar *interface,
109                 const gchar *sig, GVariant *param, gpointer user_data);
110
111 static void __netconfig_extract_ipv4_signal_data(GVariant *dictionary,
112                 const gchar *profile)
113 {
114         gchar *key = NULL;
115         const gchar *value = NULL;
116         GVariant *var = NULL;
117         GVariantIter iter;
118         GVariantBuilder *builder;
119         GVariant *params;
120
121         g_variant_iter_init(&iter, dictionary);
122         while (g_variant_iter_loop(&iter, "{sv}", &key, &var)) {
123                 if (g_strcmp0(key, "Address") == 0)  {
124                         g_variant_get(var, "&s", &value);
125                         char *old_ip = vconf_get_str(VCONFKEY_NETWORK_IP);
126
127                         DBG("Old IPv4.Address [%s] Received new IPv4.Address [%s]", old_ip,
128                                                   value);
129                         if (g_strcmp0(old_ip, value) != 0) {
130                                 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
131
132                                 if (value != NULL) {
133                                         vconf_set_str(VCONFKEY_NETWORK_IP, value);
134                                         g_variant_builder_add(builder, "{sv}", "IPv4Address",
135                                                                   g_variant_new_string(value));
136                                 } else if (old_ip != NULL && strlen(old_ip) > 0) {
137                                         vconf_set_str(VCONFKEY_NETWORK_IP, "");
138                                         g_variant_builder_add(builder, "{sv}", "IPv4Address",
139                                                                   g_variant_new_string(""));
140                                 }
141
142                                 params = g_variant_new("(@a{sv})",
143                                                                            g_variant_builder_end(builder));
144
145                                 netconfig_dbus_emit_signal(NULL, NETCONFIG_NETWORK_PATH,
146                                                    NETCONFIG_NETWORK_INTERFACE, "NetworkConfigChanged",
147                                                    params);
148                         }
149                         free(old_ip);
150                 }
151         }
152 }
153
154 static void __netconfig_extract_ipv6_signal_data(GVariant *dictionary,
155                                                                                                  const gchar *profile)
156 {
157         gchar *key = NULL;
158         const gchar *value = NULL;
159         GVariant *var = NULL;
160         GVariantIter iter;
161         GVariantBuilder *builder;
162         GVariant *params;
163
164         g_variant_iter_init(&iter, dictionary);
165         while (g_variant_iter_loop(&iter, "{sv}", &key, &var)) {
166                 if (g_strcmp0(key, "Address") == 0)  {
167                         g_variant_get(var, "&s", &value);
168                         char *old_ip6 = vconf_get_str(VCONFKEY_NETWORK_IP6);
169
170                         DBG("Old IPv6.Address [%s] Received new IPv6.Address [%s]", old_ip6,
171                                                         value);
172                         if (g_strcmp0(old_ip6, value) != 0) {
173                                 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
174
175                                 if (value != NULL) {
176                                         vconf_set_str(VCONFKEY_NETWORK_IP6, value);
177                                         g_variant_builder_add(builder, "{sv}", "IPv6Address",
178                                                                   g_variant_new_string(value));
179                                 } else if (old_ip6 != NULL && strlen(old_ip6) > 0) {
180                                         vconf_set_str(VCONFKEY_NETWORK_IP6, "");
181                                         g_variant_builder_add(builder, "{sv}", "IPv6Address",
182                                                                   g_variant_new_string(""));
183                                 }
184
185                                 params = g_variant_new("(@a{sv})",
186                                                                            g_variant_builder_end(builder));
187
188                                 netconfig_dbus_emit_signal(NULL, NETCONFIG_NETWORK_PATH,
189                                                    NETCONFIG_NETWORK_INTERFACE, "NetworkConfigChanged",
190                                                    params);
191                         }
192                         free(old_ip6);
193                 }
194         }
195 }
196
197 #if defined TIZEN_DEBUG_ENABLE
198 static int __netconfig_handle_execute_file(const char *file_path,
199                 char *const args[], char *const envs[])
200 {
201         pid_t pid = 0;
202         int status = 0;
203         int rv = 0;
204         errno = 0;
205         register unsigned int index = 0;
206         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
207
208         while (args[index] != NULL) {
209                 DBG("%s", args[index]);
210                 index++;
211         }
212
213         if (!(pid = fork())) {
214                 DBG("pid(%d), ppid (%d)", getpid(), getppid());
215                 DBG("Inside child, exec (%s) command", file_path);
216
217                 errno = 0;
218                 if (execve(file_path, args, envs) == -1) {
219                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
220                         DBG("Fail to execute command (%s)", error_buf);
221                         exit(1);
222                 }
223         } else if (pid > 0) {
224                 if (waitpid(pid, &status, 0) == -1)
225                         DBG("wait pid (%u) status (%d)", pid, status);
226
227                 if (WIFEXITED(status)) {
228                         rv = WEXITSTATUS(status);
229                         DBG("exited, status=%d", rv);
230                 } else if (WIFSIGNALED(status)) {
231                         DBG("killed by signal %d", WTERMSIG(status));
232                 } else if (WIFSTOPPED(status)) {
233                         DBG("stopped by signal %d", WSTOPSIG(status));
234                 } else if (WIFCONTINUED(status)) {
235                         DBG("continued");
236                 }
237
238                 return rv;
239         }
240
241         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
242         DBG("failed to fork(%s)", error_buf);
243         return -EIO;
244 }
245
246 static int _start_dump()
247 {
248         int rv = 0;
249         gchar *path = NETWORK_LOG_DUMP_SCRIPT;
250         char *const args[] = { NETWORK_LOG_DUMP_SCRIPT, NULL };
251         char *const envs[] = { NULL };
252
253         rv = __netconfig_handle_execute_file(path, args, envs);
254         if (rv < 0) {
255                 ERR("Fail to execute network log dump shell");
256                 return -EIO;
257         }
258         return 0;
259 }
260 #endif
261
262 static void _technology_signal_cb(GDBusConnection *conn,
263                 const gchar *name, const gchar *path, const gchar *interface,
264                 const gchar *sig, GVariant *param, gpointer user_data)
265 {
266         gchar *key = NULL;
267         gboolean value = FALSE;
268         GVariant *var = NULL;
269
270         if (param == NULL)
271                 return;
272
273         if (g_str_has_prefix(path, CONNMAN_WIFI_TECHNOLOGY_PREFIX) == TRUE) {
274                 g_variant_get(param, "(sv)", &key, &var);
275                 if (g_strcmp0(key, "Powered") == 0) {
276                         /* Power state */
277                         value = g_variant_get_boolean(var);
278                         if (value == TRUE)
279                                 wifi_state_update_power_state(TRUE);
280                         else
281                                 wifi_state_update_power_state(FALSE);
282                 } else if (g_strcmp0(key, "Connected") == 0) {
283                         /* Connection state */
284                         value = g_variant_get_boolean(var);
285                         if (value == TRUE)
286                                 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_CONNECTED);
287                         else
288                                 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_POWERED);
289                 } else if (g_strcmp0(key, "Tethering") == 0) {
290                         /* Tethering state */
291                         wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_TETHERED);
292                 }
293                 if (key)
294                         g_free(key);
295                 if (var)
296                         g_variant_unref(var);
297         }
298 }
299
300 static void _service_signal_cb(GDBusConnection *conn,
301                 const gchar *name, const gchar *path,
302                 const gchar *interface, const gchar *sig,
303                 GVariant *param, gpointer user_data)
304 {
305         gchar *sigvalue = NULL;
306         gchar *property;
307         GVariant *variant = NULL, *var;
308         GVariantIter *iter;
309         const gchar *value = NULL;
310         struct sock_data *sd = NULL;
311         int idx = 0;
312
313         if (path == NULL || param == NULL)
314                 goto done;
315
316         g_variant_get(param, "(sv)", &sigvalue, &variant);
317         if (sigvalue == NULL)
318                 goto done;
319
320         if (g_strcmp0(sig, CONNMAN_SIGNAL_PROPERTY_CHANGED) != 0)
321                 goto done;
322
323         if (g_strcmp0(sigvalue, "State") == 0) {
324                 g_variant_get(variant, "s", &property);
325
326                 DBG("[%s] %s", property, path);
327                 if (netconfig_is_wifi_profile(path) || netconfig_is_ethernet_profile(path)) {
328                         if (g_strcmp0(property, "ready") == 0) {
329                                 for (idx = 0; idx < MAX_SOCKET_OPEN_RETRY; idx++) {
330                                         sd = start_ip_conflict_mon();
331                                         if (sd != NULL)
332                                                 break;
333                                 }
334                         } else if (g_strcmp0(property, "online") == 0) {
335                                 // do nothing
336                         } else {
337                                 stop_ip_conflict_mon();
338                         }
339                 }
340
341                 if (netconfig_is_wifi_profile(path) == TRUE) {
342                         int wifi_state = 0;
343
344                         netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
345                         if (wifi_state == VCONFKEY_WIFI_OFF) {
346                                 g_free(property);
347                                 goto done;
348                         }
349
350                         if (g_strcmp0(property, "ready") == 0 || g_strcmp0(property, "online") == 0) {
351                                 if (wifi_state >= VCONFKEY_WIFI_CONNECTED) {
352                                         g_free(property);
353                                         goto done;
354                                 }
355
356                                 netconfig_update_default_profile(path);
357
358                                 wifi_state_set_service_state(NETCONFIG_WIFI_CONNECTED);
359
360                         } else if (g_strcmp0(property, "failure") == 0 || g_strcmp0(property, "disconnect") == 0 || g_strcmp0(property, "idle") == 0) {
361                                 if (netconfig_get_default_profile() == NULL ||
362                                                 netconfig_is_wifi_profile(netconfig_get_default_profile())
363                                                 != TRUE) {
364                                         if (g_strcmp0(property, "failure") == 0)
365                                                 wifi_state_set_service_state(NETCONFIG_WIFI_FAILURE);
366                                         else
367                                                 wifi_state_set_service_state(NETCONFIG_WIFI_IDLE);
368                                         g_free(property);
369                                         goto done;
370                                 }
371
372                                 if (g_strcmp0(path, netconfig_get_default_profile()) != 0) {
373                                         g_free(property);
374                                         goto done;
375                                 }
376
377                                 netconfig_update_default_profile(NULL);
378
379                                 if (g_strcmp0(property, "failure") == 0)
380                                         wifi_state_set_service_state(NETCONFIG_WIFI_FAILURE);
381                                 else
382                                         wifi_state_set_service_state(NETCONFIG_WIFI_IDLE);
383
384                         } else if (g_strcmp0(property, "association") == 0 || g_strcmp0(property, "configuration") == 0) {
385                                 if (netconfig_get_default_profile() == NULL ||
386                                                 netconfig_is_wifi_profile(netconfig_get_default_profile()) != TRUE) {
387                                         if (g_strcmp0(property, "association") == 0)
388                                                 wifi_state_set_service_state(NETCONFIG_WIFI_ASSOCIATION);
389                                         else
390                                                 wifi_state_set_service_state(NETCONFIG_WIFI_CONFIGURATION);
391                                         g_free(property);
392                                         goto done;
393                                 }
394
395                                 if (g_strcmp0(path, netconfig_get_default_profile()) != 0) {
396                                         g_free(property);
397                                         goto done;
398                                 }
399
400                                 netconfig_update_default_profile(NULL);
401
402                                 if (g_strcmp0(property, "association") == 0)
403                                         wifi_state_set_service_state(NETCONFIG_WIFI_ASSOCIATION);
404                                 else
405                                         wifi_state_set_service_state(NETCONFIG_WIFI_CONFIGURATION);
406
407                         }
408                 } else {
409                         if (g_strcmp0(property, "ready") == 0 || g_strcmp0(property, "online") == 0) {
410                                 if (netconfig_get_default_profile() == NULL) {
411                                         if (!netconfig_is_cellular_profile(path))
412                                                 netconfig_update_default_profile(path);
413                                         else {
414                                                 if (netconfig_is_cellular_internet_profile(path))
415                                                         netconfig_update_default_profile(path);
416                                         }
417                                 }
418
419                                 if (netconfig_is_cellular_profile(path) && netconfig_is_cellular_internet_profile(path))
420                                         cellular_state_set_service_state(NETCONFIG_CELLULAR_ONLINE);
421
422                         } else if (g_strcmp0(property, "failure") == 0 || g_strcmp0(property, "disconnect") == 0 || g_strcmp0(property, "idle") == 0) {
423                                 if (netconfig_get_default_profile() == NULL) {
424                                         g_free(property);
425                                         goto done;
426                                 }
427
428                                 if (netconfig_is_cellular_profile(path) && netconfig_is_cellular_internet_profile(path))
429                                         cellular_state_set_service_state(NETCONFIG_CELLULAR_IDLE);
430
431                                 if (g_strcmp0(path, netconfig_get_default_profile()) != 0) {
432                                         g_free(property);
433                                         goto done;
434                                 }
435
436                                 netconfig_update_default_profile(NULL);
437                         } else if (g_strcmp0(property, "association") == 0 || g_strcmp0(property, "configuration") == 0) {
438                                 if (netconfig_get_default_profile() == NULL) {
439                                         g_free(property);
440                                         goto done;
441                                 }
442
443                                 if (netconfig_is_cellular_profile(path) && netconfig_is_cellular_internet_profile(path))
444                                         cellular_state_set_service_state(NETCONFIG_CELLULAR_CONNECTING);
445
446                                 if (g_strcmp0(path, netconfig_get_default_profile()) != 0) {
447                                         g_free(property);
448                                         goto done;
449                                 }
450
451                                 netconfig_update_default_profile(NULL);
452                         }
453                 }
454                 g_free(property);
455         } else if (g_strcmp0(sigvalue, "Proxy") == 0) {
456                 if (netconfig_is_wifi_profile(path) != TRUE || g_strcmp0(path, netconfig_get_default_profile()) != 0)
457                         goto done;
458
459                 if (!g_variant_type_equal(variant, G_VARIANT_TYPE_ARRAY))
460                         goto done;
461
462                 g_variant_get(variant, "a{sv}", &iter);
463                 while (g_variant_iter_loop(iter, "{sv}", &property, &var)) {
464                         GVariantBuilder *builder;
465                         GVariant *sig_params;
466                         if (g_strcmp0(property, "Servers") == 0) {
467                                 GVariantIter *iter_sub = NULL;
468
469                                 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
470
471                                 g_variant_get(var, "as", &iter_sub);
472                                 g_variant_iter_loop(iter_sub, "s", &value);
473                                 g_variant_iter_free(iter_sub);
474
475                                 DBG("Proxy - [%s]", value);
476                                 vconf_set_str(VCONFKEY_NETWORK_PROXY, value);
477
478                                 g_variant_builder_add(builder, "{sv}", "ProxyAddress",
479                                                                 g_variant_new_string(value));
480
481                                 sig_params = g_variant_new("(@a{sv})",
482                                                                 g_variant_builder_end(builder));
483
484                                 netconfig_dbus_emit_signal(NULL, NETCONFIG_NETWORK_PATH,
485                                                    NETCONFIG_NETWORK_INTERFACE, "NetworkConfigChanged",
486                                                    sig_params);
487
488                                 g_free(property);
489                                 g_variant_unref(var);
490                                 break;
491                         } else if (g_strcmp0(property, "Method") == 0) {
492                                 value = g_variant_get_string(var, NULL);
493                                 DBG("Method - [%s]", value);
494
495                                 if (g_strcmp0(value, "direct") == 0) {
496                                         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
497
498                                         vconf_set_str(VCONFKEY_NETWORK_PROXY, "");
499                                         g_variant_builder_add(builder, "{sv}", "ProxyAddress",
500                                                                 g_variant_new_string(""));
501
502                                         sig_params = g_variant_new("(@a{sv})",
503                                                                 g_variant_builder_end(builder));
504
505                                         netconfig_dbus_emit_signal(NULL, NETCONFIG_NETWORK_PATH,
506                                                            NETCONFIG_NETWORK_INTERFACE,
507                                                            "NetworkConfigChanged", sig_params);
508                                 }
509
510                                 g_free(property);
511                                 g_variant_unref(var);
512                                 break;
513                         }
514                 }
515
516                 g_variant_iter_free(iter);
517         } else if (g_strcmp0(sigvalue, "IPv4") == 0) {
518                 __netconfig_extract_ipv4_signal_data(variant, path);
519         } else if (g_strcmp0(sigvalue, "IPv6") == 0) {
520                 __netconfig_extract_ipv6_signal_data(variant, path);
521         } else if (g_strcmp0(sigvalue, "Error") == 0) {
522                 g_variant_get(variant, "s", &property);
523                 INFO("[%s] Property : %s", sigvalue, property);
524 #if defined TIZEN_DEBUG_ENABLE
525                 if (g_strcmp0(property, "dhcp-failed") == 0
526                         || g_strcmp0(property, "connect-failed") == 0
527                         || g_strcmp0(property, "login-failed") == 0
528                         || g_strcmp0(property, "auth-failed") == 0
529                         || g_strcmp0(property, "invalid-key") == 0) {
530
531                         INFO("start dump");
532                         _start_dump();
533                 }
534 #endif
535                 g_free(property);
536         }
537 done:
538         if (sigvalue)
539                 g_free(sigvalue);
540
541         if (variant)
542                 g_variant_unref(variant);
543
544         return;
545 }
546
547 static void _dbus_name_changed_cb(GDBusConnection *conn,
548                 const gchar *Name, const gchar *path, const gchar *interface,
549                 const gchar *sig, GVariant *param, gpointer user_data)
550 {
551         gchar *name = NULL;
552         gchar *old = NULL;
553         gchar *new = NULL;
554
555         if (param == NULL)
556                 return;
557
558         g_variant_get(param, "(sss)", &name, &old, &new);
559
560         if (g_strcmp0(name, CONNMAN_SERVICE) == 0 && *new == '\0') {
561                 DBG("ConnMan destroyed: name %s, old %s, new %s", name, old, new);
562
563                 connman_register_agent();
564         }
565         if (name)
566                 g_free(name);
567         if (old)
568                 g_free(old);
569         if (new)
570                 g_free(new);
571
572         return;
573 }
574
575 static void _services_changed_cb(GDBusConnection *conn, const gchar *name,
576                 const gchar *path, const gchar *interface, const gchar *sig,
577                 GVariant *param, gpointer user_data)
578 {
579         gchar *property, *value;
580         gchar *service_path;
581         GVariant *variant = NULL;
582         GVariantIter *added = NULL, *removed = NULL, *next = NULL;
583
584         if (path == NULL || param == NULL)
585                 return;
586
587         if (g_strcmp0(sig, CONNMAN_SIGNAL_SERVICES_CHANGED) != 0)
588                 return;
589
590         if (netconfig_get_default_profile() != NULL)
591                 return;
592
593         g_variant_get(param, "(a(oa{sv})ao)", &added, &removed);
594
595         while (g_variant_iter_loop(added, "(oa{sv})", &service_path, &next)) {
596                 gboolean is_wifi_prof, is_cell_prof, is_cell_internet_prof;
597                 is_wifi_prof = netconfig_is_wifi_profile(service_path);
598                 is_cell_prof = netconfig_is_cellular_profile(service_path);
599                 is_cell_internet_prof = netconfig_is_cellular_internet_profile(
600                                 service_path);
601                 if (service_path != NULL) {
602                         while (next && g_variant_iter_loop(next, "{sv}", &property,
603                                                 &variant)) {
604                                 if (g_strcmp0(property, "State") == 0) {
605                                         g_variant_get(variant, "s", &value);
606                                         DBG("Profile %s State %s", service_path,
607                                                         value);
608                                         if (g_strcmp0(value, "ready") != 0 &&
609                                                         g_strcmp0(value,
610                                                                 "online") != 0) {
611                                                 g_free(property);
612                                                 g_free(value);
613                                                 g_variant_unref(variant);
614                                                 break;
615                                         }
616
617                                         if (!is_cell_prof)
618                                                 netconfig_update_default_profile(
619                                                                 service_path);
620                                         else if (is_cell_internet_prof) {
621                                                 netconfig_update_default_profile(
622                                                                 service_path);
623                                         }
624                                         if (is_wifi_prof)
625                                                 wifi_state_set_service_state(
626                                                         NETCONFIG_WIFI_CONNECTED);
627                                         else if (is_cell_prof &&
628                                                         is_cell_internet_prof)
629                                                 cellular_state_set_service_state(
630                                                         NETCONFIG_CELLULAR_ONLINE);
631                                         g_free(property);
632                                         g_free(value);
633                                         g_variant_unref(variant);
634                                         break;
635                                 }
636                         }
637                 }
638         }
639
640         g_variant_iter_free(added);
641
642         if (next)
643                 g_variant_iter_free(next);
644
645         if (removed)
646                 g_variant_iter_free(removed);
647
648         return;
649 }
650
651 static void _supplicant_interface_removed(GDBusConnection *conn,
652                 const gchar *name, const gchar *path, const gchar *interface,
653                 const gchar *sig, GVariant *param, gpointer user_data)
654 {
655         DBG("Interface removed handling!");
656         if (netconfig_wifi_is_bssid_scan_started() == TRUE)
657                 netconfig_wifi_bssid_signal_scanaborted();
658
659         return;
660 }
661
662 static void _supplicant_properties_changed(GDBusConnection *conn,
663                 const gchar *name, const gchar *path, const gchar *interface,
664                 const gchar *sig, GVariant *param, gpointer user_data)
665 {
666         DBG("Properties changed handling!");
667         gchar *key = NULL;
668         const gchar *state = NULL;
669         GVariantIter *iter = NULL;
670         GVariant *variant = NULL;
671         gboolean scanning = FALSE;
672
673         if (param == NULL)
674                 return;
675
676         g_variant_get(param, "(a{sv})", &iter);
677         while (g_variant_iter_loop(iter, "{sv}", &key, &variant)) {
678                 if (g_strcmp0(key, "Scanning") == 0) {
679                         scanning = g_variant_get_boolean(variant);
680                         DBG("setting scanning %s", scanning ? "TRUE" : "FALSE");
681                         if (scanning == TRUE)
682                                 netconfig_wifi_set_scanning(TRUE);
683                         else
684                                 netconfig_wifi_set_scanning(FALSE);
685
686                         g_variant_unref(variant);
687                         g_free(key);
688                         variant = NULL;
689                         key = NULL;
690                         break;
691                 } else if (g_strcmp0(key, "State") == 0) {
692                         state = g_variant_get_string(variant, NULL);
693                         if (state != NULL)
694                                 ERR("Supplicant state : %s", state);
695
696                         g_variant_unref(variant);
697                         g_free(key);
698                         variant = NULL;
699                         key = NULL;
700                         break;
701                 } else if (g_strcmp0(key, "DisconnectReason") == 0) {
702                         int reason = g_variant_get_int32(variant);
703                         ERR("Supplicant DisconnReason : %d", reason);
704
705                         g_variant_unref(variant);
706                         g_free(key);
707                         variant = NULL;
708                         key = NULL;
709                         break;
710                 } else {
711                         gchar *value;
712                         value = g_variant_print(variant, TRUE);
713                         DBG("Supplicant %s : %s", key, value);
714
715                         g_free(value);
716                 }
717         }
718
719         g_variant_iter_free(iter);
720
721         return;
722 }
723
724 static void _supplicant_bss_added(GDBusConnection *conn,
725                 const gchar *name, const gchar *path, const gchar *interface,
726                 const gchar *sig, GVariant *param, gpointer user_data)
727 {
728         DBG("BSS added handling!");
729         wifi_state_set_bss_found(TRUE);
730
731         return;
732 }
733
734 static void _supplicant_scan_done(GDBusConnection *conn,
735                 const gchar *name, const gchar *path, const gchar *interface,
736                 const gchar *sig, GVariant *param, gpointer user_data)
737 {
738         DBG("Scan Done handling!");
739         netconfig_wifi_set_scanning(FALSE);
740
741         if (netconfig_wifi_is_bssid_scan_started() == TRUE) {
742                 netconfig_wifi_bssid_signal_scandone();
743                 if (wifi_state_get_technology_state() < NETCONFIG_WIFI_TECH_POWERED)
744                         return;
745         }
746
747         if (netconfig_wifi_get_bgscan_state() == TRUE) {
748                 if (wifi_state_get_technology_state() >=
749                                 NETCONFIG_WIFI_TECH_POWERED)
750                         netconfig_wifi_bgscan_start(FALSE);
751
752                 wifi_start_timer_network_notification();
753         }
754
755         return;
756 }
757
758 static void _supplicant_driver_hanged(GDBusConnection *conn,
759                 const gchar *name, const gchar *path, const gchar *interface,
760                 const gchar *sig, GVariant *param, gpointer user_data)
761 {
762         DBG("Driver Hanged handling!");
763         ERR("Critical. Wi-Fi firmware crashed");
764
765 #if !defined TIZEN_WEARABLE
766         netconfig_send_message_to_net_popup("Wi-Fi Error",
767                 "Wi-Fi Driver Hanged. Please get log dump and report", "popup", NULL);
768 #endif
769         wifi_power_recover_firmware();
770
771         return;
772
773 }
774
775 static void _supplicant_session_overlapped(GDBusConnection *conn,
776                 const gchar *name, const gchar *path, const gchar *interface,
777                 const gchar *sig, GVariant *param, gpointer user_data)
778 {
779         DBG("Driver session overlapped handling!");
780         ERR("WPS PBC SESSION OVERLAPPED");
781 #if defined TIZEN_WEARABLE
782         return;
783 #else
784         netconfig_send_message_to_net_popup("WPS Error",
785                                         "wps session overlapped", "popup", NULL);
786 #endif
787 }
788
789 #if defined TIZEN_DEBUG_ENABLE
790 static void __netconfig_dumpservice_handler(GDBusConnection *conn,
791                 const gchar *name, const gchar *path, const gchar *interface,
792                 const gchar *sig, GVariant *param, gpointer user_data)
793 {
794         int mode;
795         gchar *signal_path = NULL;
796
797         if (param == NULL)
798                 return;
799
800         g_variant_get(param, "(is)", &mode, &signal_path);
801         DBG("Path: %s and mode: %d", signal_path, mode);
802
803         netconfig_dump_log(signal_path);
804         if (signal_path)
805                 g_free(signal_path);
806
807         return;
808 }
809 #endif
810
811 static void _supplicant_tdls_connected(GDBusConnection *conn,
812                 const gchar *name, const gchar *path, const gchar *interface,
813                 const gchar *sig, GVariant *param, gpointer user_data)
814 {
815         DBG("Received TDLS Connected Signal");
816         netconfig_wifi_tdls_connected_event(param);
817
818         return;
819 }
820
821 static void _supplicant_tdls_disconnected(GDBusConnection *conn,
822                 const gchar *name, const gchar *path, const gchar *interface,
823                 const gchar *sig, GVariant *param, gpointer user_data)
824 {
825         DBG("Received TDLS Disconnected Signal");
826         netconfig_wifi_tdls_disconnected_event(param);
827
828         return;
829 }
830
831 static void _supplicant_tdls_peer_found(GDBusConnection *conn,
832                 const gchar *name, const gchar *path, const gchar *interface,
833                 const gchar *sig, GVariant *param, gpointer user_data)
834 {
835         DBG("Received TDLS Peer Found Signal");
836         netconfig_wifi_tdls_peer_found_event(param);
837         return;
838 }
839
840 static void _supplicant_wifi_wps_connected(GVariant *param)
841 {
842         gchar *key;
843         char ssid[32] = {0, };
844         gchar *name;
845         GVariantIter *iter;
846         GVariant *variant;
847         int config_error = 0;
848         int error_indication = 0;
849         gsize ssid_len = 0;
850
851         if (param == NULL) {
852                 ERR("Param is NULL");
853                 return;
854         }
855
856         g_variant_get(param, "(sa{sv})", &name, &iter);
857         INFO("wps Result: %s", name);
858         while (g_variant_iter_loop(iter, "{sv}", &key, &variant)) {
859                 INFO("wps Key is %s", key);
860                 if (g_strcmp0(key, "SSID") == 0) {
861                         const char *t_key = NULL;
862                         t_key = g_variant_get_fixed_array(variant, &ssid_len, sizeof(guchar));
863                         INFO("wps ssid_len is %d ", ssid_len);
864                         if (t_key == NULL) {
865                                 g_free(key);
866                                 g_variant_unref(variant);
867                                 ERR("WPS PBC Connection Failed");
868                                 goto error;
869                         }
870                         if (ssid_len > 0 && ssid_len <= 32) {
871                                 memcpy(ssid, t_key, ssid_len);
872                         } else {
873                                 memset(ssid, 0, sizeof(ssid));
874                                 ssid_len = 0;
875                         }
876                         INFO("WPS PBC Connection completed with AP %s", ssid);
877                         netconfig_wifi_notify_wps_completed(ssid, ssid_len);
878                 }
879         }
880
881         g_variant_iter_free(iter);
882         g_free(name);
883         return;
884
885 error:
886         g_variant_iter_free(iter);
887         g_free(name);
888         error_indication = WPS_EI_OPERATION_FAILED;
889         config_error = WPS_CFG_NO_ERROR;
890         ERR("Error Occured! Notifying Fail Event");
891         netconfig_wifi_notify_wps_fail_event(config_error, error_indication);
892
893 }
894
895 static void _supplicant_wifi_wps_event(GVariant *param)
896 {
897         gchar *key;
898         gchar *name;
899         GVariantIter *iter;
900         GVariant *variant;
901         gint32 config_error = 0;
902         gint32 error_indication = 0;
903
904         if (param == NULL) {
905                 ERR("Param is NULL");
906                 return;
907         }
908
909         g_variant_get(param, "(sa{sv})", &name, &iter);
910         INFO("Event Result: %s", name);
911         if (g_strcmp0(name, "fail") == 0) {
912                 while (g_variant_iter_loop(iter, "{sv}", &key, &variant)) {
913                         if (key == NULL)
914                                 goto error;
915                         INFO("Key is %s", key);
916                         if (g_strcmp0(key, "config_error") == 0) {
917                                 config_error = g_variant_get_int32(variant);
918                                 ERR("Config Error %d", config_error);
919                         } else if (g_strcmp0(key, "error_indication") == 0) {
920                                 error_indication = g_variant_get_int32(variant);
921                                 ERR("Error Indication %d", error_indication);
922                         }
923                 }
924                 netconfig_wifi_notify_wps_fail_event(config_error, error_indication);
925         }
926
927         g_variant_iter_free(iter);
928         g_free(name);
929         return;
930
931 error:
932         g_variant_iter_free(iter);
933         g_free(name);
934         error_indication = WPS_EI_OPERATION_FAILED;
935         config_error = WPS_CFG_NO_ERROR;
936         ERR("Error Occured! Notifying Fail Event");
937         netconfig_wifi_notify_wps_fail_event(config_error, error_indication);
938 }
939
940 static void _supplicant_wifi_wps_credentials(GVariant *param)
941 {
942         gchar *key;
943         char ssid[32] = {0, };
944         char wps_key[100] = {0, };
945         GVariantIter *iter;
946         GVariant *variant;
947         int config_error = 0;
948         int error_indication = 0;
949         gsize ssid_len = 0;
950
951         if (param == NULL) {
952                 ERR("Param is NULL");
953                 return;
954         }
955
956         g_variant_get(param, "(a{sv})", &iter);
957         while (g_variant_iter_loop(iter, "{sv}", &key, &variant)) {
958                 if (key == NULL)
959                         goto error;
960                 INFO("wps Key is %s", key);
961                 if (g_strcmp0(key, "Key") == 0) {
962                         gsize key_len = 0;
963                         const char *t_key = NULL;
964                         key_len = g_variant_get_size(variant);
965
966                         INFO("wps password len %d ", key_len);
967                         if (key_len > 0) {
968                                 t_key = g_variant_get_fixed_array(variant, &key_len, sizeof(guchar));
969                                 if (!t_key) {
970                                         g_free(key);
971                                         g_variant_unref(variant);
972                                         goto error;
973                                 }
974                                 strncpy(wps_key, t_key, key_len);
975                                 wps_key[key_len] = '\0';
976                                 INFO("WPS Key in process credentials %s", wps_key);
977                         } else
978                                 SLOGI("WPS AP Security ->Open");
979                 } else if (g_strcmp0(key, "SSID") == 0) {
980                         const char *t_key = NULL;
981                         t_key = g_variant_get_fixed_array(variant, &ssid_len, sizeof(guchar));
982                         INFO("wps ssid_len is %d ", ssid_len);
983                         if (!t_key) {
984                                 g_free(key);
985                                 g_variant_unref(variant);
986                                 goto error;
987                         }
988                         if (ssid_len > 0 && ssid_len <= 32) {
989                                 memcpy(ssid, t_key, ssid_len);
990                         } else {
991                                 memset(ssid, 0, sizeof(ssid));
992                                 ssid_len = 0;
993                         }
994                         INFO("SSID in process credentials %s", ssid);
995                 }
996         }
997
998         g_variant_iter_free(iter);
999
1000 #if 0
1001         /*
1002          * Notify WPS Credentials only when requested through WPS PBC
1003          * In case of WPS PIN connman will take care of notification
1004          */
1005         if (netconfig_get_wps_field() == TRUE)
1006 #endif
1007         netconfig_wifi_notify_wps_credentials(ssid, ssid_len, wps_key);
1008         return;
1009
1010 error:
1011         g_variant_iter_free(iter);
1012         error_indication = WPS_EI_OPERATION_FAILED;
1013         config_error = WPS_CFG_NO_ERROR;
1014         ERR("Error Occured! Notifying Fail Event");
1015         netconfig_wifi_notify_wps_fail_event(config_error, error_indication);
1016 }
1017
1018 static void __netconfig_wps_signal_filter_handler(GDBusConnection *conn,
1019                 const gchar *name, const gchar *path, const gchar *interface,
1020                 const gchar *sig, GVariant *param, gpointer user_data)
1021 {
1022         if (g_strcmp0(sig, SIGNAL_WPS_CREDENTIALS) == 0) {
1023                 INFO("Received wps CREDENTIALS Signal from Supplicant");
1024                 _supplicant_wifi_wps_credentials(param);
1025         } else if (g_strcmp0(sig, SIGNAL_WPS_EVENT) == 0) {
1026                 INFO("Received wps EVENT Signal from Supplicant");
1027                 _supplicant_wifi_wps_event(param);
1028         } else if (g_strcmp0(sig, SIGNAL_WPS_CONNECTED) == 0) {
1029                 INFO("Received WPSConnected Signal from Supplicant");
1030                 _supplicant_wifi_wps_connected(param);
1031         }
1032
1033         return;
1034 }
1035
1036 static supplicant_signal_cb supplicant_cbs[SIG_MAX] = {
1037                 _supplicant_interface_removed,
1038                 _supplicant_properties_changed,
1039                 _supplicant_bss_added,
1040                 _supplicant_scan_done,
1041                 _supplicant_driver_hanged,
1042                 _supplicant_session_overlapped,
1043                 _supplicant_tdls_connected,
1044                 _supplicant_tdls_disconnected,
1045                 _supplicant_tdls_peer_found
1046 };
1047
1048 void register_gdbus_signal(void)
1049 {
1050         GDBusConnection *connection = NULL;
1051         const char *interface = NULL;
1052         SuppSigArrayIndex sig;
1053         connection = netdbus_get_connection();
1054
1055         if (connection == NULL) {
1056                 ERR("Failed to get GDbus Connection");
1057                 return;
1058         }
1059
1060         /* listening to messages from all objects as no path is specified */
1061         /* see signals from the given interface */
1062         conn_subscription_ids[0] = g_dbus_connection_signal_subscribe(
1063                         connection,
1064                         CONNMAN_SERVICE,
1065                         CONNMAN_TECHNOLOGY_INTERFACE,
1066                         NULL,
1067                         NULL,
1068                         NULL,
1069                         G_DBUS_SIGNAL_FLAGS_NONE,
1070                         _technology_signal_cb,
1071                         NULL,
1072                         NULL);
1073
1074         conn_subscription_ids[1] = g_dbus_connection_signal_subscribe(
1075                         connection,
1076                         CONNMAN_SERVICE,
1077                         CONNMAN_SERVICE_INTERFACE,
1078                         CONNMAN_SIGNAL_PROPERTY_CHANGED,
1079                         NULL,
1080                         NULL,
1081                         G_DBUS_SIGNAL_FLAGS_NONE,
1082                         _service_signal_cb,
1083                         NULL,
1084                         NULL);
1085
1086         conn_subscription_ids[2] = g_dbus_connection_signal_subscribe(
1087                         connection,
1088                         DBUS_SERVICE_DBUS,
1089                         DBUS_INTERFACE_DBUS,
1090                         CONNMAN_SIGNAL_NAME_CHANGED,
1091                         NULL,
1092                         CONNMAN_SERVICE,
1093                         G_DBUS_SIGNAL_FLAGS_NONE,
1094                         _dbus_name_changed_cb,
1095                         NULL,
1096                         NULL);
1097
1098         conn_subscription_ids[3] = g_dbus_connection_signal_subscribe(
1099                         connection,
1100                         CONNMAN_SERVICE,
1101                         CONNMAN_MANAGER_INTERFACE,
1102                         CONNMAN_SIGNAL_SERVICES_CHANGED,
1103                         NULL,
1104                         NULL,
1105                         G_DBUS_SIGNAL_FLAGS_NONE,
1106                         _services_changed_cb,
1107                         NULL,
1108                         NULL);
1109
1110         INFO("Successfully register connman DBus signal filters");
1111
1112         conn_subscription_ids[4] = g_dbus_connection_signal_subscribe(
1113                         connection,
1114                         SUPPLICANT_SERVICE,
1115                         SUPPLICANT_INTERFACE ".Interface.WPS",
1116                         NULL,
1117                         NULL,
1118                         NULL,
1119                         G_DBUS_SIGNAL_FLAGS_NONE,
1120                         __netconfig_wps_signal_filter_handler,
1121                         NULL,
1122                         NULL);
1123
1124         INFO("Successfully register Supplicant WPS DBus signal filters");
1125
1126         for (sig = SIG_INTERFACE_REMOVED; sig < SIG_MAX; sig++) {
1127                 /*
1128                  * For SIG_INTERFACE_REMOVED INTERFACE_ADDED
1129                  */
1130                 interface = (sig == SIG_INTERFACE_REMOVED) ?
1131                                 SUPPLICANT_INTERFACE : SUPPLICANT_IFACE_INTERFACE;
1132
1133                 supp_subscription_ids[sig] = g_dbus_connection_signal_subscribe(
1134                                 connection,
1135                                 SUPPLICANT_SERVICE,
1136                                 interface,
1137                                 supplicant_signals[sig],
1138                                 NULL,
1139                                 NULL,
1140                                 G_DBUS_SIGNAL_FLAGS_NONE,
1141                                 supplicant_cbs[sig],
1142                                 NULL,
1143                                 NULL);
1144         }
1145
1146         INFO("Successfully register Supplicant DBus signal filters");
1147
1148 #if defined TIZEN_DEBUG_ENABLE
1149         dumpservice_subscription_id = g_dbus_connection_signal_subscribe(
1150                         connection,
1151                         NULL,
1152                         DUMP_SERVICE_INTERFACE,
1153                         DUMP_SIGNAL,
1154                         NULL,
1155                         NULL,
1156                         G_DBUS_SIGNAL_FLAGS_NONE,
1157                         __netconfig_dumpservice_handler,
1158                         NULL,
1159                         NULL);
1160
1161         INFO("Successfully registered Dumpservice DBus signal filter");
1162 #endif
1163
1164         /* In case ConnMan precedes this signal register,
1165          * net-config should update the default connected profile.
1166          */
1167         netconfig_update_default();
1168 }
1169
1170 void deregister_gdbus_signal(void)
1171 {
1172         GDBusConnection *connection = NULL;
1173         int signal;
1174         SuppSigArrayIndex sig;
1175         connection = netdbus_get_connection();
1176         if (!connection) {
1177                 ERR("Already de-registered. Nothing to be done");
1178                 return;
1179         }
1180
1181         for (signal = 0; signal < TOTAL_CONN_SIGNALS; signal++) {
1182                 if (conn_subscription_ids[signal]) {
1183                         g_dbus_connection_signal_unsubscribe(connection,
1184                                                 conn_subscription_ids[signal]);
1185                 }
1186         }
1187
1188         for (sig = SIG_INTERFACE_REMOVED; sig < SIG_MAX; sig++) {
1189                 if (supp_subscription_ids[sig]) {
1190                         g_dbus_connection_signal_unsubscribe(connection,
1191                                                 supp_subscription_ids[sig]);
1192                 }
1193         }
1194
1195 #if defined TIZEN_DEBUG_ENABLE
1196         g_dbus_connection_signal_unsubscribe(connection,
1197                         dumpservice_subscription_id);
1198 #endif
1199 }