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