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