Merge "Revise background scan routine" into tizen
[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
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_bssid_scan_started() == TRUE)
491                 netconfig_wifi_bssid_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         gchar *key;
501         GVariantIter *iter;
502         GVariant *variant;
503         gboolean scanning = FALSE;
504
505         if (param == NULL)
506                 return;
507
508         g_variant_get(param, "(a{sv})", &iter);
509         while (g_variant_iter_loop(iter, "{sv}", &key, &variant)) {
510                 if (g_strcmp0(key, "Scanning") == 0) {
511                         scanning = g_variant_get_boolean(variant);
512                         DBG("setting scanning %s", scanning ? "TRUE" : "FALSE");
513                         if (scanning == TRUE)
514                                 netconfig_wifi_set_scanning(TRUE);
515                         else
516                                 netconfig_wifi_set_scanning(FALSE);
517
518                         g_variant_unref(variant);
519                         g_free(key);
520                         break;
521                 }
522         }
523
524         g_variant_iter_free(iter);
525
526         return;
527 }
528
529 static void _supplicant_bss_added(GDBusConnection *conn,
530                 const gchar *name, const gchar *path, const gchar *interface,
531                 const gchar *sig, GVariant *param, gpointer user_data)
532 {
533         DBG("BSS added handling!");
534         wifi_state_set_bss_found(TRUE);
535
536         return;
537 }
538
539 static void _supplicant_scan_done(GDBusConnection *conn,
540                 const gchar *name, const gchar *path, const gchar *interface,
541                 const gchar *sig, GVariant *param, gpointer user_data)
542 {
543         DBG("Scan Done handling!");
544         netconfig_wifi_set_scanning(FALSE);
545
546         if (netconfig_wifi_is_bssid_scan_started() == TRUE) {
547                 netconfig_wifi_bssid_signal_scandone();
548                 if (wifi_state_get_technology_state() < NETCONFIG_WIFI_TECH_POWERED)
549                         return;
550         }
551
552         if (netconfig_wifi_get_bgscan_state() == TRUE) {
553                 if (wifi_state_get_technology_state() >=
554                                 NETCONFIG_WIFI_TECH_POWERED)
555                         netconfig_wifi_bgscan_start(FALSE);
556
557                 wifi_start_timer_network_notification();
558         }
559
560         return;
561 }
562
563 static void _supplicant_driver_hanged(GDBusConnection *conn,
564                 const gchar *name, const gchar *path, const gchar *interface,
565                 const gchar *sig, GVariant *param, gpointer user_data)
566 {
567         DBG("Driver Hanged handling!");
568         ERR("Critical. Wi-Fi firmware crashed");
569
570         wifi_power_recover_firmware();
571
572         return;
573 }
574
575 static void _supplicant_session_overlapped(GDBusConnection *conn,
576                 const gchar *name, const gchar *path, const gchar *interface,
577                 const gchar *sig, GVariant *param, gpointer user_data)
578 {
579         DBG("Driver session overlapped handling!");
580         ERR("WPS PBC SESSION OVERLAPPED");
581 #if defined TIZEN_WEARABLE
582         return;
583 #else
584         netconfig_send_message_to_net_popup("WPS Error",
585                                         "wps session overlapped", "popup", NULL);
586 #endif
587 }
588
589 static void _supplicant_tdls_connected(GDBusConnection *conn,
590                 const gchar *name, const gchar *path, const gchar *interface,
591                 const gchar *sig, GVariant *param, gpointer user_data)
592 {
593         DBG("Received TDLS Connected Signal");
594         netconfig_wifi_tdls_connected_event(param);
595
596         return;
597 }
598
599 static void _supplicant_tdls_disconnected(GDBusConnection *conn,
600                 const gchar *name, const gchar *path, const gchar *interface,
601                 const gchar *sig, GVariant *param, gpointer user_data)
602 {
603         DBG("Received TDLS Disconnected Signal");
604         netconfig_wifi_tdls_disconnected_event(param);
605
606         return;
607 }
608
609 static void _supplicant_tdls_peer_found(GDBusConnection *conn,
610                 const gchar *name, const gchar *path, const gchar *interface,
611                 const gchar *sig, GVariant *param, gpointer user_data)
612 {
613         DBG("Received TDLS Peer Found Signal");
614         netconfig_wifi_tdls_peer_found_event(param);
615         return;
616 }
617
618 static void _supplicant_wifi_wps_connected(GVariant *param)
619 {
620         gchar *key;
621         char ssid[32] = {0, };
622         gchar *name;
623         GVariantIter *iter;
624         GVariant *variant;
625         int config_error = 0;
626         int error_indication = 0;
627         gsize ssid_len = 0;
628
629         if (param == NULL) {
630                 ERR("Param is NULL");
631                 return;
632         }
633
634         g_variant_get(param, "(sa{sv})", &name, &iter);
635         INFO("wps Result: %s", name);
636         while (g_variant_iter_loop(iter, "{sv}", &key, &variant)) {
637                 INFO("wps Key is %s", key);
638                 if (g_strcmp0(key, "SSID") == 0) {
639                         const char *t_key = NULL;
640                         t_key = g_variant_get_fixed_array(variant, &ssid_len, sizeof(guchar));
641                         INFO("wps ssid_len is %d ", ssid_len);
642                         if (t_key == NULL) {
643                                 g_free(key);
644                                 g_variant_unref(variant);
645                                 ERR("WPS PBC Connection Failed");
646                                 goto error;
647                         }
648                         if (ssid_len > 0 && ssid_len <= 32) {
649                                 memcpy(ssid, t_key, ssid_len);
650                         } else {
651                                 memset(ssid, 0, sizeof(ssid));
652                                 ssid_len = 0;
653                         }
654                         INFO("WPS PBC Connection completed with AP %s", ssid);
655                         netconfig_wifi_notify_wps_completed(ssid, ssid_len);
656                 }
657         }
658
659         g_variant_iter_free(iter);
660         g_free(name);
661         return;
662
663 error:
664         g_variant_iter_free(iter);
665         g_free(name);
666         error_indication = WPS_EI_OPERATION_FAILED;
667         config_error = WPS_CFG_NO_ERROR;
668         ERR("Error Occured! Notifying Fail Event");
669         netconfig_wifi_notify_wps_fail_event(config_error, error_indication);
670
671 }
672
673 static void _supplicant_wifi_wps_event(GVariant *param)
674 {
675         gchar *key;
676         gchar *name;
677         GVariantIter *iter;
678         GVariant *variant;
679         gint32 config_error = 0;
680         gint32 error_indication = 0;
681
682         if (param == NULL) {
683                 ERR("Param is NULL");
684                 return;
685         }
686
687         g_variant_get(param, "(sa{sv})", &name, &iter);
688         INFO("Event Result: %s", name);
689         if (g_strcmp0(name, "fail") == 0) {
690                 while (g_variant_iter_loop(iter, "{sv}", &key, &variant)) {
691                         if (key == NULL)
692                                 goto error;
693                         INFO("Key is %s", key);
694                         if (g_strcmp0(key, "config_error") == 0) {
695                                 config_error = g_variant_get_int32(variant);
696                                 ERR("Config Error %d", config_error);
697                         } else if (g_strcmp0(key, "error_indication") == 0) {
698                                 error_indication = g_variant_get_int32(variant);
699                                 ERR("Error Indication %d", error_indication);
700                         }
701                 }
702                 netconfig_wifi_notify_wps_fail_event(config_error, error_indication);
703         }
704
705         g_variant_iter_free(iter);
706         g_free(name);
707         return;
708
709 error:
710         g_variant_iter_free(iter);
711         g_free(name);
712         error_indication = WPS_EI_OPERATION_FAILED;
713         config_error = WPS_CFG_NO_ERROR;
714         ERR("Error Occured! Notifying Fail Event");
715         netconfig_wifi_notify_wps_fail_event(config_error, error_indication);
716 }
717
718 static void _supplicant_wifi_wps_credentials(GVariant *param)
719 {
720         gchar *key;
721         char ssid[32];
722         char wps_key[100];
723         GVariantIter *iter;
724         GVariant *variant;
725         int config_error = 0;
726         int error_indication = 0;
727         gsize ssid_len = 0;
728
729         if (param == NULL) {
730                 ERR("Param is NULL");
731                 return;
732         }
733
734         g_variant_get(param, "(a{sv})", &iter);
735         while (g_variant_iter_loop(iter, "{sv}", &key, &variant)) {
736                 if (key == NULL)
737                         goto error;
738                 INFO("wps Key is %s", key);
739                 if (g_strcmp0(key, "Key") == 0) {
740                         gsize key_len = 0;
741                         const char *t_key = NULL;
742                         key_len = g_variant_get_size(variant);
743
744                         INFO("wps password len %d ", key_len);
745                         if (key_len > 0) {
746                                 t_key = g_variant_get_fixed_array(variant, &key_len, sizeof(guchar));
747                                 if (!t_key) {
748                                         g_free(key);
749                                         g_variant_unref(variant);
750                                         goto error;
751                                 }
752                                 strncpy(wps_key, t_key, key_len);
753                                 wps_key[key_len] = '\0';
754                                 INFO("WPS Key in process credentials %s", wps_key);
755                         } else
756                                 SLOGI("WPS AP Security ->Open");
757                 } else if (g_strcmp0(key, "SSID") == 0) {
758                         const char *t_key = NULL;
759                         t_key = g_variant_get_fixed_array(variant, &ssid_len, sizeof(guchar));
760                         INFO("wps ssid_len is %d ", ssid_len);
761                         if (!t_key) {
762                                 g_free(key);
763                                 g_variant_unref(variant);
764                                 goto error;
765                         }
766                         if (ssid_len > 0 && ssid_len <= 32) {
767                                 memcpy(ssid, t_key, ssid_len);
768                         } else {
769                                 memset(ssid, 0, sizeof(ssid));
770                                 ssid_len = 0;
771                         }
772                         INFO("SSID in process credentials %s", ssid);
773                 }
774         }
775
776         g_variant_iter_free(iter);
777
778 #if 0
779         /*
780          * Notify WPS Credentials only when requested through WPS PBC
781          * In case of WPS PIN connman will take care of notification
782          */
783         if (netconfig_get_wps_field() == TRUE)
784 #endif
785         netconfig_wifi_notify_wps_credentials(ssid, ssid_len, wps_key);
786         return;
787
788 error:
789         g_variant_iter_free(iter);
790         error_indication = WPS_EI_OPERATION_FAILED;
791         config_error = WPS_CFG_NO_ERROR;
792         ERR("Error Occured! Notifying Fail Event");
793         netconfig_wifi_notify_wps_fail_event(config_error, error_indication);
794 }
795
796 static void __netconfig_wps_signal_filter_handler(GDBusConnection *conn,
797                 const gchar *name, const gchar *path, const gchar *interface,
798                 const gchar *sig, GVariant *param, gpointer user_data)
799 {
800         if (g_strcmp0(sig, SIGNAL_WPS_CREDENTIALS) == 0) {
801                 INFO("Received wps CREDENTIALS Signal from Supplicant");
802                 _supplicant_wifi_wps_credentials(param);
803         } else if (g_strcmp0(sig, SIGNAL_WPS_EVENT) == 0) {
804                 INFO("Received wps EVENT Signal from Supplicant");
805                 _supplicant_wifi_wps_event(param);
806         } else if (g_strcmp0(sig, SIGNAL_WPS_CONNECTED) == 0) {
807                 INFO("Received WPSConnected Signal from Supplicant");
808                 _supplicant_wifi_wps_connected(param);
809         }
810
811         return;
812 }
813
814 static supplicant_signal_cb supplicant_cbs[SIG_MAX] = {
815                 _supplicant_interface_removed,
816                 _supplicant_properties_changed,
817                 _supplicant_bss_added,
818                 _supplicant_scan_done,
819                 _supplicant_driver_hanged,
820                 _supplicant_session_overlapped,
821                 _supplicant_tdls_connected,
822                 _supplicant_tdls_disconnected,
823                 _supplicant_tdls_peer_found
824 };
825
826 void register_gdbus_signal(void)
827 {
828         GDBusConnection *connection = NULL;
829         const char *interface = NULL;
830         SuppSigArrayIndex sig;
831         connection = netdbus_get_connection();
832
833         if (connection == NULL) {
834                 ERR("Failed to get GDbus Connection");
835                 return;
836         }
837
838         /* listening to messages from all objects as no path is specified */
839         /* see signals from the given interface */
840         conn_subscription_ids[0] = g_dbus_connection_signal_subscribe(
841                         connection,
842                         CONNMAN_SERVICE,
843                         CONNMAN_TECHNOLOGY_INTERFACE,
844                         NULL,
845                         NULL,
846                         NULL,
847                         G_DBUS_SIGNAL_FLAGS_NONE,
848                         _technology_signal_cb,
849                         NULL,
850                         NULL);
851
852         conn_subscription_ids[1] = g_dbus_connection_signal_subscribe(
853                         connection,
854                         CONNMAN_SERVICE,
855                         CONNMAN_SERVICE_INTERFACE,
856                         CONNMAN_SIGNAL_PROPERTY_CHANGED,
857                         NULL,
858                         NULL,
859                         G_DBUS_SIGNAL_FLAGS_NONE,
860                         _service_signal_cb,
861                         NULL,
862                         NULL);
863
864         conn_subscription_ids[2] = g_dbus_connection_signal_subscribe(
865                         connection,
866                         DBUS_SERVICE_DBUS,
867                         DBUS_INTERFACE_DBUS,
868                         CONNMAN_SIGNAL_NAME_CHANGED,
869                         NULL,
870                         CONNMAN_SERVICE,
871                         G_DBUS_SIGNAL_FLAGS_NONE,
872                         _dbus_name_changed_cb,
873                         NULL,
874                         NULL);
875
876         conn_subscription_ids[3] = g_dbus_connection_signal_subscribe(
877                         connection,
878                         CONNMAN_SERVICE,
879                         CONNMAN_MANAGER_INTERFACE,
880                         CONNMAN_SIGNAL_SERVICES_CHANGED,
881                         NULL,
882                         NULL,
883                         G_DBUS_SIGNAL_FLAGS_NONE,
884                         _services_changed_cb,
885                         NULL,
886                         NULL);
887
888         INFO("Successfully register connman DBus signal filters");
889
890         conn_subscription_ids[4] = g_dbus_connection_signal_subscribe(
891                         connection,
892                         SUPPLICANT_SERVICE,
893                         SUPPLICANT_INTERFACE ".Interface.WPS",
894                         NULL,
895                         NULL,
896                         NULL,
897                         G_DBUS_SIGNAL_FLAGS_NONE,
898                         __netconfig_wps_signal_filter_handler,
899                         NULL,
900                         NULL);
901
902         INFO("Successfully register Supplicant WPS DBus signal filters");
903
904         for (sig = SIG_INTERFACE_REMOVED; sig < SIG_MAX; sig++) {
905                 /*
906                  * For SIG_INTERFACE_REMOVED INTERFACE_ADDED
907                  */
908                 interface = (sig == SIG_INTERFACE_REMOVED) ?
909                                 SUPPLICANT_INTERFACE : SUPPLICANT_IFACE_INTERFACE;
910
911                 supp_subscription_ids[sig] = g_dbus_connection_signal_subscribe(
912                                 connection,
913                                 SUPPLICANT_SERVICE,
914                                 interface,
915                                 supplicant_signals[sig],
916                                 NULL,
917                                 NULL,
918                                 G_DBUS_SIGNAL_FLAGS_NONE,
919                                 supplicant_cbs[sig],
920                                 NULL,
921                                 NULL);
922         }
923
924         INFO("Successfully register Supplicant DBus signal filters");
925
926         /* In case ConnMan precedes this signal register,
927          * net-config should update the default connected profile.
928          */
929         netconfig_update_default();
930 }
931
932 void deregister_gdbus_signal(void)
933 {
934         GDBusConnection *connection = NULL;
935         int signal;
936         SuppSigArrayIndex sig;
937         connection = netdbus_get_connection();
938         if (!connection) {
939                 ERR("Already de-registered. Nothing to be done");
940                 return;
941         }
942
943         for (signal = 0; signal < TOTAL_CONN_SIGNALS; signal++) {
944                 if (conn_subscription_ids[signal]) {
945                         g_dbus_connection_signal_unsubscribe(connection,
946                                                 conn_subscription_ids[signal]);
947                 }
948         }
949
950         for (sig = SIG_INTERFACE_REMOVED; sig < SIG_MAX; sig++) {
951                 if (supp_subscription_ids[sig]) {
952                         g_dbus_connection_signal_unsubscribe(connection,
953                                                 supp_subscription_ids[sig]);
954                 }
955         }
956
957 }