Added dump log for connect fail
[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, "connect-failed") == 0 || g_strcmp0(property, "invalid-key") == 0 || g_strcmp0(property, "auth-failed") == 0)
526                         _start_dump();
527 #endif
528                 g_free(property);
529         }
530 done:
531         if (sigvalue)
532                 g_free(sigvalue);
533
534         if (variant)
535                 g_variant_unref(variant);
536
537         return;
538 }
539
540 static void _dbus_name_changed_cb(GDBusConnection *conn,
541                 const gchar *Name, const gchar *path, const gchar *interface,
542                 const gchar *sig, GVariant *param, gpointer user_data)
543 {
544         gchar *name = NULL;
545         gchar *old = NULL;
546         gchar *new = NULL;
547
548         if (param == NULL)
549                 return;
550
551         g_variant_get(param, "(sss)", &name, &old, &new);
552
553         if (g_strcmp0(name, CONNMAN_SERVICE) == 0 && *new == '\0') {
554                 DBG("ConnMan destroyed: name %s, old %s, new %s", name, old, new);
555
556                 connman_register_agent();
557         }
558         if (name)
559                 g_free(name);
560         if (old)
561                 g_free(old);
562         if (new)
563                 g_free(new);
564
565         return;
566 }
567
568 static void _services_changed_cb(GDBusConnection *conn, const gchar *name,
569                 const gchar *path, const gchar *interface, const gchar *sig,
570                 GVariant *param, gpointer user_data)
571 {
572         gchar *property, *value;
573         gchar *service_path;
574         GVariant *variant = NULL;
575         GVariantIter *added = NULL, *removed = NULL, *next = NULL;
576
577         if (path == NULL || param == NULL)
578                 return;
579
580         if (g_strcmp0(sig, CONNMAN_SIGNAL_SERVICES_CHANGED) != 0)
581                 return;
582
583         if (netconfig_get_default_profile() != NULL)
584                 return;
585
586         g_variant_get(param, "(a(oa{sv})ao)", &added, &removed);
587
588         while (g_variant_iter_loop(added, "(oa{sv})", &service_path, &next)) {
589                 gboolean is_wifi_prof, is_cell_prof, is_cell_internet_prof;
590                 is_wifi_prof = netconfig_is_wifi_profile(service_path);
591                 is_cell_prof = netconfig_is_cellular_profile(service_path);
592                 is_cell_internet_prof = netconfig_is_cellular_internet_profile(
593                                 service_path);
594                 if (service_path != NULL) {
595                         while (next && g_variant_iter_loop(next, "{sv}", &property,
596                                                 &variant)) {
597                                 if (g_strcmp0(property, "State") == 0) {
598                                         g_variant_get(variant, "s", &value);
599                                         DBG("Profile %s State %s", service_path,
600                                                         value);
601                                         if (g_strcmp0(value, "ready") != 0 &&
602                                                         g_strcmp0(value,
603                                                                 "online") != 0) {
604                                                 g_free(property);
605                                                 g_free(value);
606                                                 g_variant_unref(variant);
607                                                 break;
608                                         }
609
610                                         if (!is_cell_prof)
611                                                 netconfig_update_default_profile(
612                                                                 service_path);
613                                         else if (is_cell_internet_prof) {
614                                                 netconfig_update_default_profile(
615                                                                 service_path);
616                                         }
617                                         if (is_wifi_prof)
618                                                 wifi_state_set_service_state(
619                                                         NETCONFIG_WIFI_CONNECTED);
620                                         else if (is_cell_prof &&
621                                                         is_cell_internet_prof)
622                                                 cellular_state_set_service_state(
623                                                         NETCONFIG_CELLULAR_ONLINE);
624                                         g_free(property);
625                                         g_free(value);
626                                         g_variant_unref(variant);
627                                         break;
628                                 }
629                         }
630                 }
631         }
632
633         g_variant_iter_free(added);
634
635         if (next)
636                 g_variant_iter_free(next);
637
638         if (removed)
639                 g_variant_iter_free(removed);
640
641         return;
642 }
643
644 static void _supplicant_interface_removed(GDBusConnection *conn,
645                 const gchar *name, const gchar *path, const gchar *interface,
646                 const gchar *sig, GVariant *param, gpointer user_data)
647 {
648         DBG("Interface removed handling!");
649         if (netconfig_wifi_is_bssid_scan_started() == TRUE)
650                 netconfig_wifi_bssid_signal_scanaborted();
651
652         return;
653 }
654
655 static void _supplicant_properties_changed(GDBusConnection *conn,
656                 const gchar *name, const gchar *path, const gchar *interface,
657                 const gchar *sig, GVariant *param, gpointer user_data)
658 {
659         DBG("Properties changed handling!");
660         gchar *key = NULL;
661         const gchar *state = NULL;
662         GVariantIter *iter = NULL;
663         GVariant *variant = NULL;
664         gboolean scanning = FALSE;
665
666         if (param == NULL)
667                 return;
668
669         g_variant_get(param, "(a{sv})", &iter);
670         while (g_variant_iter_loop(iter, "{sv}", &key, &variant)) {
671                 if (g_strcmp0(key, "Scanning") == 0) {
672                         scanning = g_variant_get_boolean(variant);
673                         DBG("setting scanning %s", scanning ? "TRUE" : "FALSE");
674                         if (scanning == TRUE)
675                                 netconfig_wifi_set_scanning(TRUE);
676                         else
677                                 netconfig_wifi_set_scanning(FALSE);
678
679                         g_variant_unref(variant);
680                         g_free(key);
681                         variant = NULL;
682                         key = NULL;
683                         break;
684                 } else if (g_strcmp0(key, "State") == 0) {
685                         state = g_variant_get_string(variant, NULL);
686                         if (state != NULL)
687                                 ERR("Supplicant state : %s", state);
688
689                         g_variant_unref(variant);
690                         g_free(key);
691                         variant = NULL;
692                         key = NULL;
693                         break;
694                 } else if (g_strcmp0(key, "DisconnectReason") == 0) {
695                         int reason = g_variant_get_int32(variant);
696                         ERR("Supplicant DisconnReason : %d", reason);
697
698                         g_variant_unref(variant);
699                         g_free(key);
700                         variant = NULL;
701                         key = NULL;
702                         break;
703                 } else {
704                         gchar *value;
705                         value = g_variant_print(variant, TRUE);
706                         DBG("Supplicant %s : %s", key, value);
707
708                         g_free(value);
709                 }
710         }
711
712         g_variant_iter_free(iter);
713
714         return;
715 }
716
717 static void _supplicant_bss_added(GDBusConnection *conn,
718                 const gchar *name, const gchar *path, const gchar *interface,
719                 const gchar *sig, GVariant *param, gpointer user_data)
720 {
721         DBG("BSS added handling!");
722         wifi_state_set_bss_found(TRUE);
723
724         return;
725 }
726
727 static void _supplicant_scan_done(GDBusConnection *conn,
728                 const gchar *name, const gchar *path, const gchar *interface,
729                 const gchar *sig, GVariant *param, gpointer user_data)
730 {
731         DBG("Scan Done handling!");
732         netconfig_wifi_set_scanning(FALSE);
733
734         if (netconfig_wifi_is_bssid_scan_started() == TRUE) {
735                 netconfig_wifi_bssid_signal_scandone();
736                 if (wifi_state_get_technology_state() < NETCONFIG_WIFI_TECH_POWERED)
737                         return;
738         }
739
740         if (netconfig_wifi_get_bgscan_state() == TRUE) {
741                 if (wifi_state_get_technology_state() >=
742                                 NETCONFIG_WIFI_TECH_POWERED)
743                         netconfig_wifi_bgscan_start(FALSE);
744
745                 wifi_start_timer_network_notification();
746         }
747
748         return;
749 }
750
751 static void _supplicant_driver_hanged(GDBusConnection *conn,
752                 const gchar *name, const gchar *path, const gchar *interface,
753                 const gchar *sig, GVariant *param, gpointer user_data)
754 {
755         DBG("Driver Hanged handling!");
756         ERR("Critical. Wi-Fi firmware crashed");
757
758 #if !defined TIZEN_WEARABLE
759         netconfig_send_message_to_net_popup("Wi-Fi Error",
760                 "Wi-Fi Driver Hanged. Please get log dump and report", "popup", NULL);
761 #endif
762         wifi_power_recover_firmware();
763
764         return;
765
766 }
767
768 static void _supplicant_session_overlapped(GDBusConnection *conn,
769                 const gchar *name, const gchar *path, const gchar *interface,
770                 const gchar *sig, GVariant *param, gpointer user_data)
771 {
772         DBG("Driver session overlapped handling!");
773         ERR("WPS PBC SESSION OVERLAPPED");
774 #if defined TIZEN_WEARABLE
775         return;
776 #else
777         netconfig_send_message_to_net_popup("WPS Error",
778                                         "wps session overlapped", "popup", NULL);
779 #endif
780 }
781
782 #if defined TIZEN_DEBUG_ENABLE
783 static void __netconfig_dumpservice_handler(GDBusConnection *conn,
784                 const gchar *name, const gchar *path, const gchar *interface,
785                 const gchar *sig, GVariant *param, gpointer user_data)
786 {
787         int mode;
788         gchar *signal_path = NULL;
789
790         if (param == NULL)
791                 return;
792
793         g_variant_get(param, "(is)", &mode, &signal_path);
794         DBG("Path: %s and mode: %d", signal_path, mode);
795
796         netconfig_dump_log(signal_path);
797         if (signal_path)
798                 g_free(signal_path);
799
800         return;
801 }
802 #endif
803
804 static void _supplicant_tdls_connected(GDBusConnection *conn,
805                 const gchar *name, const gchar *path, const gchar *interface,
806                 const gchar *sig, GVariant *param, gpointer user_data)
807 {
808         DBG("Received TDLS Connected Signal");
809         netconfig_wifi_tdls_connected_event(param);
810
811         return;
812 }
813
814 static void _supplicant_tdls_disconnected(GDBusConnection *conn,
815                 const gchar *name, const gchar *path, const gchar *interface,
816                 const gchar *sig, GVariant *param, gpointer user_data)
817 {
818         DBG("Received TDLS Disconnected Signal");
819         netconfig_wifi_tdls_disconnected_event(param);
820
821         return;
822 }
823
824 static void _supplicant_tdls_peer_found(GDBusConnection *conn,
825                 const gchar *name, const gchar *path, const gchar *interface,
826                 const gchar *sig, GVariant *param, gpointer user_data)
827 {
828         DBG("Received TDLS Peer Found Signal");
829         netconfig_wifi_tdls_peer_found_event(param);
830         return;
831 }
832
833 static void _supplicant_wifi_wps_connected(GVariant *param)
834 {
835         gchar *key;
836         char ssid[32] = {0, };
837         gchar *name;
838         GVariantIter *iter;
839         GVariant *variant;
840         int config_error = 0;
841         int error_indication = 0;
842         gsize ssid_len = 0;
843
844         if (param == NULL) {
845                 ERR("Param is NULL");
846                 return;
847         }
848
849         g_variant_get(param, "(sa{sv})", &name, &iter);
850         INFO("wps Result: %s", name);
851         while (g_variant_iter_loop(iter, "{sv}", &key, &variant)) {
852                 INFO("wps Key is %s", key);
853                 if (g_strcmp0(key, "SSID") == 0) {
854                         const char *t_key = NULL;
855                         t_key = g_variant_get_fixed_array(variant, &ssid_len, sizeof(guchar));
856                         INFO("wps ssid_len is %d ", ssid_len);
857                         if (t_key == NULL) {
858                                 g_free(key);
859                                 g_variant_unref(variant);
860                                 ERR("WPS PBC Connection Failed");
861                                 goto error;
862                         }
863                         if (ssid_len > 0 && ssid_len <= 32) {
864                                 memcpy(ssid, t_key, ssid_len);
865                         } else {
866                                 memset(ssid, 0, sizeof(ssid));
867                                 ssid_len = 0;
868                         }
869                         INFO("WPS PBC Connection completed with AP %s", ssid);
870                         netconfig_wifi_notify_wps_completed(ssid, ssid_len);
871                 }
872         }
873
874         g_variant_iter_free(iter);
875         g_free(name);
876         return;
877
878 error:
879         g_variant_iter_free(iter);
880         g_free(name);
881         error_indication = WPS_EI_OPERATION_FAILED;
882         config_error = WPS_CFG_NO_ERROR;
883         ERR("Error Occured! Notifying Fail Event");
884         netconfig_wifi_notify_wps_fail_event(config_error, error_indication);
885
886 }
887
888 static void _supplicant_wifi_wps_event(GVariant *param)
889 {
890         gchar *key;
891         gchar *name;
892         GVariantIter *iter;
893         GVariant *variant;
894         gint32 config_error = 0;
895         gint32 error_indication = 0;
896
897         if (param == NULL) {
898                 ERR("Param is NULL");
899                 return;
900         }
901
902         g_variant_get(param, "(sa{sv})", &name, &iter);
903         INFO("Event Result: %s", name);
904         if (g_strcmp0(name, "fail") == 0) {
905                 while (g_variant_iter_loop(iter, "{sv}", &key, &variant)) {
906                         if (key == NULL)
907                                 goto error;
908                         INFO("Key is %s", key);
909                         if (g_strcmp0(key, "config_error") == 0) {
910                                 config_error = g_variant_get_int32(variant);
911                                 ERR("Config Error %d", config_error);
912                         } else if (g_strcmp0(key, "error_indication") == 0) {
913                                 error_indication = g_variant_get_int32(variant);
914                                 ERR("Error Indication %d", error_indication);
915                         }
916                 }
917                 netconfig_wifi_notify_wps_fail_event(config_error, error_indication);
918         }
919
920         g_variant_iter_free(iter);
921         g_free(name);
922         return;
923
924 error:
925         g_variant_iter_free(iter);
926         g_free(name);
927         error_indication = WPS_EI_OPERATION_FAILED;
928         config_error = WPS_CFG_NO_ERROR;
929         ERR("Error Occured! Notifying Fail Event");
930         netconfig_wifi_notify_wps_fail_event(config_error, error_indication);
931 }
932
933 static void _supplicant_wifi_wps_credentials(GVariant *param)
934 {
935         gchar *key;
936         char ssid[32] = {0, };
937         char wps_key[100] = {0, };
938         GVariantIter *iter;
939         GVariant *variant;
940         int config_error = 0;
941         int error_indication = 0;
942         gsize ssid_len = 0;
943
944         if (param == NULL) {
945                 ERR("Param is NULL");
946                 return;
947         }
948
949         g_variant_get(param, "(a{sv})", &iter);
950         while (g_variant_iter_loop(iter, "{sv}", &key, &variant)) {
951                 if (key == NULL)
952                         goto error;
953                 INFO("wps Key is %s", key);
954                 if (g_strcmp0(key, "Key") == 0) {
955                         gsize key_len = 0;
956                         const char *t_key = NULL;
957                         key_len = g_variant_get_size(variant);
958
959                         INFO("wps password len %d ", key_len);
960                         if (key_len > 0) {
961                                 t_key = g_variant_get_fixed_array(variant, &key_len, sizeof(guchar));
962                                 if (!t_key) {
963                                         g_free(key);
964                                         g_variant_unref(variant);
965                                         goto error;
966                                 }
967                                 strncpy(wps_key, t_key, key_len);
968                                 wps_key[key_len] = '\0';
969                                 INFO("WPS Key in process credentials %s", wps_key);
970                         } else
971                                 SLOGI("WPS AP Security ->Open");
972                 } else if (g_strcmp0(key, "SSID") == 0) {
973                         const char *t_key = NULL;
974                         t_key = g_variant_get_fixed_array(variant, &ssid_len, sizeof(guchar));
975                         INFO("wps ssid_len is %d ", ssid_len);
976                         if (!t_key) {
977                                 g_free(key);
978                                 g_variant_unref(variant);
979                                 goto error;
980                         }
981                         if (ssid_len > 0 && ssid_len <= 32) {
982                                 memcpy(ssid, t_key, ssid_len);
983                         } else {
984                                 memset(ssid, 0, sizeof(ssid));
985                                 ssid_len = 0;
986                         }
987                         INFO("SSID in process credentials %s", ssid);
988                 }
989         }
990
991         g_variant_iter_free(iter);
992
993 #if 0
994         /*
995          * Notify WPS Credentials only when requested through WPS PBC
996          * In case of WPS PIN connman will take care of notification
997          */
998         if (netconfig_get_wps_field() == TRUE)
999 #endif
1000         netconfig_wifi_notify_wps_credentials(ssid, ssid_len, wps_key);
1001         return;
1002
1003 error:
1004         g_variant_iter_free(iter);
1005         error_indication = WPS_EI_OPERATION_FAILED;
1006         config_error = WPS_CFG_NO_ERROR;
1007         ERR("Error Occured! Notifying Fail Event");
1008         netconfig_wifi_notify_wps_fail_event(config_error, error_indication);
1009 }
1010
1011 static void __netconfig_wps_signal_filter_handler(GDBusConnection *conn,
1012                 const gchar *name, const gchar *path, const gchar *interface,
1013                 const gchar *sig, GVariant *param, gpointer user_data)
1014 {
1015         if (g_strcmp0(sig, SIGNAL_WPS_CREDENTIALS) == 0) {
1016                 INFO("Received wps CREDENTIALS Signal from Supplicant");
1017                 _supplicant_wifi_wps_credentials(param);
1018         } else if (g_strcmp0(sig, SIGNAL_WPS_EVENT) == 0) {
1019                 INFO("Received wps EVENT Signal from Supplicant");
1020                 _supplicant_wifi_wps_event(param);
1021         } else if (g_strcmp0(sig, SIGNAL_WPS_CONNECTED) == 0) {
1022                 INFO("Received WPSConnected Signal from Supplicant");
1023                 _supplicant_wifi_wps_connected(param);
1024         }
1025
1026         return;
1027 }
1028
1029 static supplicant_signal_cb supplicant_cbs[SIG_MAX] = {
1030                 _supplicant_interface_removed,
1031                 _supplicant_properties_changed,
1032                 _supplicant_bss_added,
1033                 _supplicant_scan_done,
1034                 _supplicant_driver_hanged,
1035                 _supplicant_session_overlapped,
1036                 _supplicant_tdls_connected,
1037                 _supplicant_tdls_disconnected,
1038                 _supplicant_tdls_peer_found
1039 };
1040
1041 void register_gdbus_signal(void)
1042 {
1043         GDBusConnection *connection = NULL;
1044         const char *interface = NULL;
1045         SuppSigArrayIndex sig;
1046         connection = netdbus_get_connection();
1047
1048         if (connection == NULL) {
1049                 ERR("Failed to get GDbus Connection");
1050                 return;
1051         }
1052
1053         /* listening to messages from all objects as no path is specified */
1054         /* see signals from the given interface */
1055         conn_subscription_ids[0] = g_dbus_connection_signal_subscribe(
1056                         connection,
1057                         CONNMAN_SERVICE,
1058                         CONNMAN_TECHNOLOGY_INTERFACE,
1059                         NULL,
1060                         NULL,
1061                         NULL,
1062                         G_DBUS_SIGNAL_FLAGS_NONE,
1063                         _technology_signal_cb,
1064                         NULL,
1065                         NULL);
1066
1067         conn_subscription_ids[1] = g_dbus_connection_signal_subscribe(
1068                         connection,
1069                         CONNMAN_SERVICE,
1070                         CONNMAN_SERVICE_INTERFACE,
1071                         CONNMAN_SIGNAL_PROPERTY_CHANGED,
1072                         NULL,
1073                         NULL,
1074                         G_DBUS_SIGNAL_FLAGS_NONE,
1075                         _service_signal_cb,
1076                         NULL,
1077                         NULL);
1078
1079         conn_subscription_ids[2] = g_dbus_connection_signal_subscribe(
1080                         connection,
1081                         DBUS_SERVICE_DBUS,
1082                         DBUS_INTERFACE_DBUS,
1083                         CONNMAN_SIGNAL_NAME_CHANGED,
1084                         NULL,
1085                         CONNMAN_SERVICE,
1086                         G_DBUS_SIGNAL_FLAGS_NONE,
1087                         _dbus_name_changed_cb,
1088                         NULL,
1089                         NULL);
1090
1091         conn_subscription_ids[3] = g_dbus_connection_signal_subscribe(
1092                         connection,
1093                         CONNMAN_SERVICE,
1094                         CONNMAN_MANAGER_INTERFACE,
1095                         CONNMAN_SIGNAL_SERVICES_CHANGED,
1096                         NULL,
1097                         NULL,
1098                         G_DBUS_SIGNAL_FLAGS_NONE,
1099                         _services_changed_cb,
1100                         NULL,
1101                         NULL);
1102
1103         INFO("Successfully register connman DBus signal filters");
1104
1105         conn_subscription_ids[4] = g_dbus_connection_signal_subscribe(
1106                         connection,
1107                         SUPPLICANT_SERVICE,
1108                         SUPPLICANT_INTERFACE ".Interface.WPS",
1109                         NULL,
1110                         NULL,
1111                         NULL,
1112                         G_DBUS_SIGNAL_FLAGS_NONE,
1113                         __netconfig_wps_signal_filter_handler,
1114                         NULL,
1115                         NULL);
1116
1117         INFO("Successfully register Supplicant WPS DBus signal filters");
1118
1119         for (sig = SIG_INTERFACE_REMOVED; sig < SIG_MAX; sig++) {
1120                 /*
1121                  * For SIG_INTERFACE_REMOVED INTERFACE_ADDED
1122                  */
1123                 interface = (sig == SIG_INTERFACE_REMOVED) ?
1124                                 SUPPLICANT_INTERFACE : SUPPLICANT_IFACE_INTERFACE;
1125
1126                 supp_subscription_ids[sig] = g_dbus_connection_signal_subscribe(
1127                                 connection,
1128                                 SUPPLICANT_SERVICE,
1129                                 interface,
1130                                 supplicant_signals[sig],
1131                                 NULL,
1132                                 NULL,
1133                                 G_DBUS_SIGNAL_FLAGS_NONE,
1134                                 supplicant_cbs[sig],
1135                                 NULL,
1136                                 NULL);
1137         }
1138
1139         INFO("Successfully register Supplicant DBus signal filters");
1140
1141 #if defined TIZEN_DEBUG_ENABLE
1142         dumpservice_subscription_id = g_dbus_connection_signal_subscribe(
1143                         connection,
1144                         NULL,
1145                         DUMP_SERVICE_INTERFACE,
1146                         DUMP_SIGNAL,
1147                         NULL,
1148                         NULL,
1149                         G_DBUS_SIGNAL_FLAGS_NONE,
1150                         __netconfig_dumpservice_handler,
1151                         NULL,
1152                         NULL);
1153
1154         INFO("Successfully registered Dumpservice DBus signal filter");
1155 #endif
1156
1157         /* In case ConnMan precedes this signal register,
1158          * net-config should update the default connected profile.
1159          */
1160         netconfig_update_default();
1161 }
1162
1163 void deregister_gdbus_signal(void)
1164 {
1165         GDBusConnection *connection = NULL;
1166         int signal;
1167         SuppSigArrayIndex sig;
1168         connection = netdbus_get_connection();
1169         if (!connection) {
1170                 ERR("Already de-registered. Nothing to be done");
1171                 return;
1172         }
1173
1174         for (signal = 0; signal < TOTAL_CONN_SIGNALS; signal++) {
1175                 if (conn_subscription_ids[signal]) {
1176                         g_dbus_connection_signal_unsubscribe(connection,
1177                                                 conn_subscription_ids[signal]);
1178                 }
1179         }
1180
1181         for (sig = SIG_INTERFACE_REMOVED; sig < SIG_MAX; sig++) {
1182                 if (supp_subscription_ids[sig]) {
1183                         g_dbus_connection_signal_unsubscribe(connection,
1184                                                 supp_subscription_ids[sig]);
1185                 }
1186         }
1187
1188 #if defined TIZEN_DEBUG_ENABLE
1189         g_dbus_connection_signal_unsubscribe(connection,
1190                         dumpservice_subscription_id);
1191 #endif
1192 }