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