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