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