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