08fd2a84e6d088c46adcbdc6c6c7f5501992cca5
[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 #include <sys/wait.h>
26
27 #include "log.h"
28 #include "util.h"
29 #include "netdbus.h"
30 #include "neterror.h"
31 #include "wifi-wps.h"
32 #include "wifi-bssid-scan.h"
33 #include "wifi-agent.h"
34 #include "wifi-power.h"
35 #include "wifi-state.h"
36 #include "netsupplicant.h"
37 #include "network-state.h"
38 #include "cellular-state.h"
39 #include "signal-handler.h"
40 #include "wifi-background-scan.h"
41 #include "wifi-tdls.h"
42 #include "wifi-dpp.h"
43 #include "ip-conflict-detect.h"
44 #include "wifi-key-encryption.h"
45 #include "clatd-handler.h"
46 #if defined TIZEN_DEBUG_ENABLE
47 #include "network-dump.h"
48 #define NETWORK_LOG_DUMP_SCRIPT  "/opt/var/lib/net-config/network_log_dump.sh"
49 #define MAX_SIZE_ERROR_BUFFER 256
50 #endif
51
52 #define DBUS_SERVICE_DBUS                       "org.freedesktop.DBus"
53 #define DBUS_INTERFACE_DBUS                     "org.freedesktop.DBus"
54 #define SIGNAL_INTERFACE_REMOVED                "InterfaceRemoved"
55 #define SIGNAL_SCAN_DONE                        "ScanDone"
56 #define SIGNAL_SCAN_CHANGED                     "ScanChanged"
57 #define SIGNAL_BSS_ADDED                        "BSSAdded"
58 #define SIGNAL_PROPERTIES_CHANGED               "PropertiesChanged"
59 #define SIGNAL_PROPERTIES_DRIVER_HANGED         "DriverHanged"
60 #define SIGNAL_PROPERTIES_SESSION_OVERLAPPED    "SessionOverlapped"
61 #define SIGNAL_TDLS_CONNECTED                           "TDLSConnected"
62 #define SIGNAL_TDLS_DISCONNECTED                        "TDLSDisconnected"
63 #define SIGNAL_TDLS_PEER_FOUND                          "TDLSPeerFound"
64
65 #define SIGNAL_DPP_AUTH_SUCCESS "DPPAuthSuccess"
66 #define SIGNAL_DPP_AUTH_FAILED "DPPAuthFailed"
67 #define SIGNAL_DPP_NOT_COMPATIBLE "DPPNotCompatible"
68 #define SIGNAL_DPP_CONF_FAILED "DPPConfFailed"
69 #define SIGNAL_DPP_SCAN_PEER_QR "DPPScanPeerQrCode"
70 #define SIGNAL_DPP_NETWORK_ID "DPPNetworkID"
71 #define SIGNAL_DPP_CONF_SENT "DPPConfSent"
72 #define SIGNAL_DPP_CONF_RECEIVED "DPPConfReceived"
73 #define SIGNAL_DPP_CONF_OBJ "DPPConfObj"
74
75 #define SIGNAL_WPS_CONNECTED                            "WPSConnected"
76 #define SIGNAL_WPS_EVENT                                        "Event"
77 #define SIGNAL_WPS_CREDENTIALS                          "Credentials"
78
79 #define CONNMAN_SIGNAL_SERVICES_CHANGED         "ServicesChanged"
80 #define CONNMAN_SIGNAL_PROPERTY_CHANGED         "PropertyChanged"
81 #define SIGNAL_NAME_OWNER_CHANGED               "NameOwnerChanged"
82
83 #define MAX_SIG_LEN 64
84 #define TOTAL_CONN_SIGNALS 6
85 #define MAX_SOCKET_OPEN_RETRY 5
86
87 typedef enum {
88         SIG_INTERFACE_REMOVED = 0,
89         SIG_PROPERTIES_CHANGED,
90         SIG_BSS_ADDED,
91         SIG_SCAN_DONE,
92         SIG_DRIVER_HANGED,
93         SIG_SESSION_OVERLAPPED,
94         SIG_TDLS_CONNECTED,
95         SIG_TDLS_DISCONNECTED,
96         SIG_TDLS_PEER_FOUND,
97         SIG_DPP_AUTH_SUCCESS,
98         SIG_DPP_AUTH_FAILED,
99         SIG_DPP_NOT_COMPATIBLE,
100         SIG_DPP_CONF_FAILED,
101         SIG_DPP_SCAN_PEER_QR,
102         SIG_DPP_NETWORK_ID,
103         SIG_DPP_CONF_SENT,
104         SIG_DPP_CONF_RECEIVED,
105         SIG_DPP_CONF_OBJ,
106         SIG_MAX
107 } SuppSigArrayIndex;
108
109 static int conn_subscription_ids[TOTAL_CONN_SIGNALS] = {0};
110 static const char supplicant_signals[SIG_MAX][MAX_SIG_LEN] = {
111                 SIGNAL_INTERFACE_REMOVED,
112                 SIGNAL_PROPERTIES_CHANGED,
113                 SIGNAL_BSS_ADDED,
114                 SIGNAL_SCAN_DONE,
115                 SIGNAL_PROPERTIES_DRIVER_HANGED,
116                 SIGNAL_PROPERTIES_SESSION_OVERLAPPED,
117                 SIGNAL_TDLS_CONNECTED,
118                 SIGNAL_TDLS_DISCONNECTED,
119                 SIGNAL_TDLS_PEER_FOUND,
120                 SIGNAL_DPP_AUTH_SUCCESS,
121                 SIGNAL_DPP_AUTH_FAILED,
122                 SIGNAL_DPP_NOT_COMPATIBLE,
123                 SIGNAL_DPP_CONF_FAILED,
124                 SIGNAL_DPP_SCAN_PEER_QR,
125                 SIGNAL_DPP_NETWORK_ID,
126                 SIGNAL_DPP_CONF_SENT,
127                 SIGNAL_DPP_CONF_RECEIVED,
128                 SIGNAL_DPP_CONF_OBJ,
129 };
130 static const char *supplicant_iface_for_sig[SIG_MAX] = {
131                 SUPPLICANT_INTERFACE,
132                 SUPPLICANT_IFACE_INTERFACE,
133                 SUPPLICANT_IFACE_INTERFACE,
134                 SUPPLICANT_IFACE_INTERFACE,
135                 SUPPLICANT_IFACE_INTERFACE,
136                 SUPPLICANT_IFACE_INTERFACE,
137                 SUPPLICANT_IFACE_INTERFACE,
138                 SUPPLICANT_IFACE_INTERFACE,
139                 SUPPLICANT_IFACE_INTERFACE,
140                 SUPPLICANT_IFACE_DPP,
141                 SUPPLICANT_IFACE_DPP,
142                 SUPPLICANT_IFACE_DPP,
143                 SUPPLICANT_IFACE_DPP,
144                 SUPPLICANT_IFACE_DPP,
145                 SUPPLICANT_IFACE_DPP,
146                 SUPPLICANT_IFACE_DPP,
147                 SUPPLICANT_IFACE_DPP,
148                 SUPPLICANT_IFACE_DPP,
149 };
150
151 static int supp_subscription_ids[SIG_MAX] = {0};
152 #if defined TIZEN_DEBUG_ENABLE
153 static int dumpservice_subscription_id = 0;
154 #endif
155
156 typedef void (*supplicant_signal_cb)(GDBusConnection *conn,
157                 const gchar *name, const gchar *path, const gchar *interface,
158                 const gchar *sig, GVariant *param, gpointer user_data);
159 typedef void (*connman_signal_cb)(GDBusConnection *conn,
160                 const gchar *name, const gchar *path, const gchar *interface,
161                 const gchar *sig, GVariant *param, gpointer user_data);
162
163 static void __netconfig_extract_ipv4_signal_data(GVariant *dictionary,
164                 const gchar *profile)
165 {
166         gchar *key = NULL;
167         const gchar *value = NULL;
168         GVariant *var = NULL;
169         GVariantIter iter;
170         GVariantBuilder *builder;
171         GVariant *params;
172
173         g_variant_iter_init(&iter, dictionary);
174         while (g_variant_iter_loop(&iter, "{sv}", &key, &var)) {
175                 if (g_strcmp0(key, "Address") == 0)  {
176                         g_variant_get(var, "&s", &value);
177                         char *old_ip = vconf_get_str(VCONFKEY_NETWORK_IP);
178
179                         DBG("Old IPv4.Address [%s] Received new IPv4.Address [%s]", old_ip,
180                                                   value);
181                         if (g_strcmp0(old_ip, value) != 0) {
182                                 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
183
184                                 if (value != NULL) {
185                                         vconf_set_str(VCONFKEY_NETWORK_IP, value);
186                                         g_variant_builder_add(builder, "{sv}", "IPv4Address",
187                                                                   g_variant_new_string(value));
188                                 } else if (old_ip != NULL && strlen(old_ip) > 0) {
189                                         vconf_set_str(VCONFKEY_NETWORK_IP, "");
190                                         g_variant_builder_add(builder, "{sv}", "IPv4Address",
191                                                                   g_variant_new_string(""));
192                                 }
193
194                                 params = g_variant_new("(@a{sv})",
195                                                                            g_variant_builder_end(builder));
196
197                                 g_variant_builder_unref(builder);
198
199                                 netconfig_dbus_emit_signal(NULL, NETCONFIG_NETWORK_PATH,
200                                                    NETCONFIG_NETWORK_INTERFACE, "NetworkConfigChanged",
201                                                    params);
202                         }
203                         free(old_ip);
204                 }
205         }
206 }
207
208 static void __netconfig_extract_ipv6_signal_data(GVariant *dictionary,
209                                                                                                  const gchar *profile)
210 {
211         gchar *key = NULL;
212         const gchar *value = NULL;
213         GVariant *var = NULL;
214         GVariantIter iter;
215         GVariantBuilder *builder;
216         GVariant *params;
217
218         g_variant_iter_init(&iter, dictionary);
219         while (g_variant_iter_loop(&iter, "{sv}", &key, &var)) {
220                 if (g_strcmp0(key, "Address") == 0)  {
221                         g_variant_get(var, "&s", &value);
222                         char *old_ip6 = vconf_get_str(VCONFKEY_NETWORK_IP6);
223
224                         DBG("Old IPv6.Address [%s] Received new IPv6.Address [%s]", old_ip6,
225                                                         value);
226                         if (g_strcmp0(old_ip6, value) != 0) {
227                                 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
228
229                                 if (value != NULL) {
230                                         vconf_set_str(VCONFKEY_NETWORK_IP6, value);
231                                         g_variant_builder_add(builder, "{sv}", "IPv6Address",
232                                                                   g_variant_new_string(value));
233                                 } else if (old_ip6 != NULL && strlen(old_ip6) > 0) {
234                                         vconf_set_str(VCONFKEY_NETWORK_IP6, "");
235                                         g_variant_builder_add(builder, "{sv}", "IPv6Address",
236                                                                   g_variant_new_string(""));
237                                 }
238
239                                 params = g_variant_new("(@a{sv})",
240                                                                            g_variant_builder_end(builder));
241                                 g_variant_builder_unref(builder);
242
243                                 netconfig_dbus_emit_signal(NULL, NETCONFIG_NETWORK_PATH,
244                                                    NETCONFIG_NETWORK_INTERFACE, "NetworkConfigChanged",
245                                                    params);
246                         }
247                         free(old_ip6);
248                 }
249         }
250 }
251
252 #if defined TIZEN_DEBUG_ENABLE
253 static int __netconfig_handle_execute_file(const char *file_path,
254                 char *const args[], char *const envs[])
255 {
256         pid_t pid = 0;
257         int status = 0;
258         int rv = 0;
259         errno = 0;
260         register unsigned int index = 0;
261         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
262
263         while (args[index] != NULL) {
264                 DBG("%s", args[index]);
265                 index++;
266         }
267
268         if (!(pid = fork())) {
269                 DBG("pid(%d), ppid (%d)", getpid(), getppid());
270                 DBG("Inside child, exec (%s) command", file_path);
271
272                 errno = 0;
273                 if (execve(file_path, args, envs) == -1) {
274                         DBG("Fail to execute command (%s)",
275                                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
276                         exit(1);
277                 }
278         } else if (pid > 0) {
279                 if (waitpid(pid, &status, 0) == -1)
280                         DBG("wait pid (%u) status (%d)", pid, status);
281
282                 if (WIFEXITED(status)) {
283                         rv = WEXITSTATUS(status);
284                         DBG("exited, status=%d", rv);
285                 } else if (WIFSIGNALED(status)) {
286                         DBG("killed by signal %d", WTERMSIG(status));
287                 } else if (WIFSTOPPED(status)) {
288                         DBG("stopped by signal %d", WSTOPSIG(status));
289                 } else if (WIFCONTINUED(status)) {
290                         DBG("continued");
291                 }
292
293                 return rv;
294         }
295
296         DBG("failed to fork(%s)",
297                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
298         return -EIO;
299 }
300
301 static int _start_dump()
302 {
303         int rv = 0;
304         gchar *path = NETWORK_LOG_DUMP_SCRIPT;
305         char *const args[] = { NETWORK_LOG_DUMP_SCRIPT, NULL };
306         char *const envs[] = { NULL };
307
308         rv = __netconfig_handle_execute_file(path, args, envs);
309         if (rv < 0) {
310                 ERR("Fail to execute network log dump shell");
311                 return -EIO;
312         }
313         return 0;
314 }
315 #endif
316
317 static void _technology_signal_cb(GDBusConnection *conn,
318                 const gchar *name, const gchar *path, const gchar *interface,
319                 const gchar *sig, GVariant *param, gpointer user_data)
320 {
321         gchar *key = NULL;
322         gboolean value = FALSE;
323         GVariant *var = NULL;
324
325         if (param == NULL)
326                 return;
327
328         if (g_str_has_prefix(path, CONNMAN_WIFI_TECHNOLOGY_PREFIX) == TRUE) {
329                 g_variant_get(param, "(sv)", &key, &var);
330                 if (g_strcmp0(key, "Powered") == 0) {
331                         /* Power state */
332                         value = g_variant_get_boolean(var);
333                         if (value == TRUE)
334                                 wifi_state_update_power_state(TRUE);
335                         else
336                                 wifi_state_update_power_state(FALSE);
337                 } else if (g_strcmp0(key, "Connected") == 0) {
338                         /* Connection state */
339                         value = g_variant_get_boolean(var);
340                         if (value == TRUE)
341                                 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_CONNECTED);
342                         else
343                                 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_POWERED);
344                 } else if (g_strcmp0(key, "Tethering") == 0) {
345                         /* Tethering state */
346                         wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_TETHERED);
347                 }
348                 g_free(key);
349                 g_variant_unref(var);
350         }
351 }
352
353 static void _service_signal_cb(GDBusConnection *conn,
354                 const gchar *name, const gchar *path,
355                 const gchar *interface, const gchar *sig,
356                 GVariant *param, gpointer user_data)
357 {
358         gchar *sigvalue = NULL;
359         gchar *property;
360         GVariant *variant = NULL, *var;
361         GVariantIter *iter;
362         const gchar *value = NULL;
363         struct sock_data *sd = NULL;
364         gchar *pf = NULL;
365         int idx = 0;
366
367         if (path == NULL || param == NULL)
368                 goto done;
369
370         g_variant_get(param, "(sv)", &sigvalue, &variant);
371         if (sigvalue == NULL)
372                 goto done;
373
374         if (g_strcmp0(sig, CONNMAN_SIGNAL_PROPERTY_CHANGED) != 0)
375                 goto done;
376
377         if (g_strcmp0(sigvalue, "State") == 0) {
378                 g_variant_get(variant, "s", &property);
379
380                 DBG("[%s] %s", property, path);
381
382                 if (netconfig_is_wifi_profile(path) == TRUE) {
383                         int wifi_state = 0;
384
385                         netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
386                         if (wifi_state == VCONFKEY_WIFI_OFF) {
387                                 g_free(property);
388                                 goto done;
389                         }
390
391                         if (g_strcmp0(property, "ready") == 0 || g_strcmp0(property, "online") == 0) {
392                                 if (wifi_state >= VCONFKEY_WIFI_CONNECTED) {
393                                         g_free(property);
394                                         goto done;
395                                 }
396
397                                 netconfig_update_default_profile(path);
398
399                                 wifi_state_set_service_state(NETCONFIG_WIFI_CONNECTED);
400
401                         } else if (g_strcmp0(property, "failure") == 0 || g_strcmp0(property, "disconnect") == 0 || g_strcmp0(property, "idle") == 0) {
402                                 if (netconfig_get_default_profile() == NULL ||
403                                                 netconfig_is_wifi_profile(netconfig_get_default_profile())
404                                                 != TRUE) {
405                                         if (g_strcmp0(property, "failure") == 0)
406                                                 wifi_state_set_service_state(NETCONFIG_WIFI_FAILURE);
407                                         else
408                                                 wifi_state_set_service_state(NETCONFIG_WIFI_IDLE);
409                                         g_free(property);
410                                         goto done;
411                                 }
412
413                                 if (g_strcmp0(path, netconfig_get_default_profile()) != 0) {
414                                         g_free(property);
415                                         goto done;
416                                 }
417
418                                 netconfig_update_default_profile(NULL);
419
420                                 if (g_strcmp0(property, "failure") == 0)
421                                         wifi_state_set_service_state(NETCONFIG_WIFI_FAILURE);
422                                 else
423                                         wifi_state_set_service_state(NETCONFIG_WIFI_IDLE);
424
425                         } else if (g_strcmp0(property, "association") == 0 || g_strcmp0(property, "configuration") == 0) {
426                                 if (netconfig_get_default_profile() == NULL ||
427                                                 netconfig_is_wifi_profile(netconfig_get_default_profile()) != TRUE) {
428                                         if (g_strcmp0(property, "association") == 0)
429                                                 wifi_state_set_service_state(NETCONFIG_WIFI_ASSOCIATION);
430                                         else
431                                                 wifi_state_set_service_state(NETCONFIG_WIFI_CONFIGURATION);
432                                         g_free(property);
433                                         goto done;
434                                 }
435
436                                 if (g_strcmp0(path, netconfig_get_default_profile()) != 0) {
437                                         g_free(property);
438                                         goto done;
439                                 }
440
441                                 netconfig_update_default_profile(NULL);
442
443                                 if (g_strcmp0(property, "association") == 0)
444                                         wifi_state_set_service_state(NETCONFIG_WIFI_ASSOCIATION);
445                                 else
446                                         wifi_state_set_service_state(NETCONFIG_WIFI_CONFIGURATION);
447
448                         }
449                 } else {
450                         if (g_strcmp0(property, "ready") == 0 || g_strcmp0(property, "online") == 0) {
451                                 if (netconfig_get_default_profile() == NULL) {
452                                         if (!netconfig_is_cellular_profile(path)) {
453                                                 netconfig_update_default_profile(path);
454                                         } else {
455                                                 if (netconfig_is_cellular_internet_profile(path)) {
456                                                         netconfig_update_default_profile(path);
457                                                 }
458                                         }
459                                 } else {
460                                         pf = netconfig_get_connected_cellular_internet_ipv6only_profile(NULL);
461                                         if (pf)
462                                         {
463                                                 g_free(pf);
464
465                                                 /* Enable clatd if it is not in running state */
466                                                 DBG("Connected to ipv6 only cellular, enable clatd");
467                                                 netconfig_clatd_enable();
468                                         }
469                                 }
470
471                                 if (netconfig_is_cellular_profile(path) && netconfig_is_cellular_internet_profile(path))
472                                         cellular_state_set_service_state(NETCONFIG_CELLULAR_ONLINE);
473
474                         } else if (g_strcmp0(property, "failure") == 0 || g_strcmp0(property, "disconnect") == 0 || g_strcmp0(property, "idle") == 0) {
475                                 if (netconfig_get_default_profile() == NULL) {
476                                         g_free(property);
477                                         goto done;
478                                 }
479
480                                 if (netconfig_is_cellular_profile(path) && netconfig_is_cellular_internet_profile(path)) {
481                                         cellular_state_set_service_state(NETCONFIG_CELLULAR_IDLE);
482
483                                         /* Disable clatd if it is in running state */
484                                         DBG("disable clatd");
485                                         netconfig_clatd_disable();
486                                 }
487
488                                 if (g_strcmp0(path, netconfig_get_default_profile()) != 0) {
489                                         g_free(property);
490                                         goto done;
491                                 }
492
493                                 netconfig_update_default_profile(NULL);
494                         } else if (g_strcmp0(property, "association") == 0 || g_strcmp0(property, "configuration") == 0) {
495                                 if (netconfig_get_default_profile() == NULL) {
496                                         g_free(property);
497                                         goto done;
498                                 }
499
500                                 if (netconfig_is_cellular_profile(path) && netconfig_is_cellular_internet_profile(path))
501                                         cellular_state_set_service_state(NETCONFIG_CELLULAR_CONNECTING);
502
503                                 if (g_strcmp0(path, netconfig_get_default_profile()) != 0) {
504                                         g_free(property);
505                                         goto done;
506                                 }
507
508                                 netconfig_update_default_profile(NULL);
509                         }
510                 }
511
512                 if (netconfig_is_wifi_profile(path) || netconfig_is_ethernet_profile(path)) {
513                         if (g_strcmp0(property, "ready") == 0) {
514                                 for (idx = 0; idx < MAX_SOCKET_OPEN_RETRY; idx++) {
515                                         sd = start_ip_conflict_mon();
516                                         if (sd != NULL)
517                                                 break;
518                                 }
519                         } else if (g_strcmp0(property, "online") == 0) {
520                                 // do nothing
521                         } else {
522                                 stop_ip_conflict_mon();
523                         }
524                 }
525
526                 g_free(property);
527         } else if (g_strcmp0(sigvalue, "Proxy") == 0) {
528                 if (netconfig_is_wifi_profile(path) != TRUE || g_strcmp0(path, netconfig_get_default_profile()) != 0)
529                         goto done;
530
531                 if (!g_variant_is_of_type(variant, G_VARIANT_TYPE_ARRAY))
532                         goto done;
533
534                 g_variant_get(variant, "a{sv}", &iter);
535                 while (g_variant_iter_loop(iter, "{sv}", &property, &var)) {
536                         GVariantBuilder *builder;
537                         GVariant *sig_params;
538                         if (g_strcmp0(property, "Servers") == 0) {
539                                 GVariantIter *iter_sub = NULL;
540
541                                 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
542
543                                 g_variant_get(var, "as", &iter_sub);
544                                 g_variant_iter_loop(iter_sub, "s", &value);
545                                 g_variant_iter_free(iter_sub);
546
547                                 DBG("Proxy - [%s]", value);
548                                 vconf_set_str(VCONFKEY_NETWORK_PROXY, value);
549
550                                 g_variant_builder_add(builder, "{sv}", "ProxyAddress",
551                                                                 g_variant_new_string(value));
552
553                                 sig_params = g_variant_new("(@a{sv})",
554                                                                 g_variant_builder_end(builder));
555                                 g_variant_builder_unref(builder);
556
557                                 netconfig_dbus_emit_signal(NULL, NETCONFIG_NETWORK_PATH,
558                                                    NETCONFIG_NETWORK_INTERFACE, "NetworkConfigChanged",
559                                                    sig_params);
560
561                                 g_free(property);
562                                 g_variant_unref(var);
563                                 break;
564                         } else if (g_strcmp0(property, "Method") == 0) {
565                                 value = g_variant_get_string(var, NULL);
566                                 DBG("Method - [%s]", value);
567
568                                 if (g_strcmp0(value, "direct") == 0) {
569                                         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
570
571                                         vconf_set_str(VCONFKEY_NETWORK_PROXY, "");
572                                         g_variant_builder_add(builder, "{sv}", "ProxyAddress",
573                                                                 g_variant_new_string(""));
574
575                                         sig_params = g_variant_new("(@a{sv})",
576                                                                 g_variant_builder_end(builder));
577                                         g_variant_builder_unref(builder);
578
579                                         netconfig_dbus_emit_signal(NULL, NETCONFIG_NETWORK_PATH,
580                                                            NETCONFIG_NETWORK_INTERFACE,
581                                                            "NetworkConfigChanged", sig_params);
582                                 }
583
584                                 g_free(property);
585                                 g_variant_unref(var);
586                                 break;
587                         }
588                 }
589
590                 g_variant_iter_free(iter);
591         } else if (g_strcmp0(sigvalue, "IPv4") == 0) {
592                 __netconfig_extract_ipv4_signal_data(variant, path);
593         } else if (g_strcmp0(sigvalue, "IPv6") == 0) {
594                 __netconfig_extract_ipv6_signal_data(variant, path);
595         } else if (g_strcmp0(sigvalue, "Error") == 0) {
596                 g_variant_get(variant, "s", &property);
597                 INFO("[%s] Property : %s", sigvalue, property);
598 #if defined TIZEN_DEBUG_ENABLE
599                 if (g_strcmp0(property, "dhcp-failed") == 0
600                         || g_strcmp0(property, "connect-failed") == 0
601                         || g_strcmp0(property, "login-failed") == 0
602                         || g_strcmp0(property, "auth-failed") == 0
603                         || g_strcmp0(property, "invalid-key") == 0) {
604
605                         INFO("start dump");
606                         _start_dump();
607                 }
608 #endif
609                 g_free(property);
610         }
611 done:
612         g_free(sigvalue);
613
614         if (variant)
615                 g_variant_unref(variant);
616
617         return;
618 }
619
620 static void _dbus_name_changed_cb(GDBusConnection *conn,
621                 const gchar *Name, const gchar *path, const gchar *interface,
622                 const gchar *sig, GVariant *param, gpointer user_data)
623 {
624         gchar *name = NULL;
625         gchar *old = NULL;
626         gchar *new = NULL;
627
628         if (param == NULL)
629                 return;
630
631         g_variant_get(param, "(sss)", &name, &old, &new);
632
633         if (g_strcmp0(name, CONNMAN_SERVICE) == 0 && *new == '\0') {
634                 DBG("ConnMan destroyed: name %s, old %s, new %s", name, old, new);
635
636                 connman_register_agent();
637         } else if (g_strcmp0(name, CLATD_SERVICE) == 0 && *new == '\0') {
638                 DBG("Clat destroyed: name %s, old %s, new %s", name, old, new);
639
640                 /* If clatd is terminated unexpectedly, reset and enable clat service. */
641                 if (NETCONFIG_CELLULAR_ONLINE == cellular_state_get_service_state())
642                         netconfig_clatd_reset();
643         }
644         g_free(name);
645         g_free(old);
646         g_free(new);
647
648         return;
649 }
650
651 static void _services_changed_cb(GDBusConnection *conn, const gchar *name,
652                 const gchar *path, const gchar *interface, const gchar *sig,
653                 GVariant *param, gpointer user_data)
654 {
655         gchar *property, *value;
656         gchar *service_path;
657         GVariant *variant = NULL;
658         GVariantIter *added = NULL, *removed = NULL, *next = NULL;
659
660         if (path == NULL || param == NULL)
661                 return;
662
663         if (g_strcmp0(sig, CONNMAN_SIGNAL_SERVICES_CHANGED) != 0)
664                 return;
665
666         if (netconfig_get_default_profile() != NULL)
667                 return;
668
669         g_variant_get(param, "(a(oa{sv})ao)", &added, &removed);
670
671         while (g_variant_iter_loop(added, "(oa{sv})", &service_path, &next)) {
672                 gboolean is_wifi_prof, is_cell_prof, is_cell_internet_prof;
673                 is_wifi_prof = netconfig_is_wifi_profile(service_path);
674                 is_cell_prof = netconfig_is_cellular_profile(service_path);
675                 is_cell_internet_prof = netconfig_is_cellular_internet_profile(
676                                 service_path);
677                 if (service_path != NULL) {
678                         while (next && g_variant_iter_loop(next, "{sv}", &property,
679                                                 &variant)) {
680                                 if (g_strcmp0(property, "State") == 0) {
681                                         g_variant_get(variant, "s", &value);
682                                         DBG("Profile %s State %s", service_path,
683                                                         value);
684                                         if (g_strcmp0(value, "ready") != 0 &&
685                                                         g_strcmp0(value,
686                                                                 "online") != 0) {
687                                                 g_free(property);
688                                                 g_free(value);
689                                                 g_variant_unref(variant);
690                                                 break;
691                                         }
692
693                                         if (!is_cell_prof)
694                                                 netconfig_update_default_profile(
695                                                                 service_path);
696                                         else if (is_cell_internet_prof) {
697                                                 netconfig_update_default_profile(
698                                                                 service_path);
699                                         }
700                                         if (is_wifi_prof)
701                                                 wifi_state_set_service_state(
702                                                         NETCONFIG_WIFI_CONNECTED);
703                                         else if (is_cell_prof &&
704                                                         is_cell_internet_prof)
705                                                 cellular_state_set_service_state(
706                                                         NETCONFIG_CELLULAR_ONLINE);
707                                         g_free(property);
708                                         g_free(value);
709                                         g_variant_unref(variant);
710                                         break;
711                                 }
712                         }
713                 }
714         }
715
716         g_variant_iter_free(added);
717
718         if (next)
719                 g_variant_iter_free(next);
720
721         if (removed)
722                 g_variant_iter_free(removed);
723
724         return;
725 }
726
727 static void _scan_changed_cb(GDBusConnection *conn,
728                 const gchar *name, const gchar *path, const gchar *interface,
729                 const gchar *sig, GVariant *param, gpointer user_data)
730 {
731         const char *key = NULL;
732         GVariant *var;
733         gboolean value = false;
734
735         if (g_strcmp0(sig, SIGNAL_SCAN_CHANGED) == 0) {
736                 g_variant_get(param, "(sv)", &key, &var);
737
738                 if (g_strcmp0(key, "scan_started") == 0)
739                         value = true;
740                 else if (g_strcmp0(key, "scan_done") == 0)
741                         value = false;
742
743                 DBG("Scan changed [%s]", (value == true) ? "Started" : "Done");
744                 netconfig_battery_update_wifi_scan(value);
745
746                 if (var != NULL)
747                         g_variant_unref(var);
748         }
749 }
750
751 static void _supplicant_interface_removed(GDBusConnection *conn,
752                 const gchar *name, const gchar *path, const gchar *interface,
753                 const gchar *sig, GVariant *param, gpointer user_data)
754 {
755         DBG("Interface removed handling!");
756         if (netconfig_wifi_is_bssid_scan_started() == TRUE)
757                 netconfig_wifi_bssid_signal_scanaborted();
758
759         return;
760 }
761
762 static void _supplicant_properties_changed(GDBusConnection *conn,
763                 const gchar *name, const gchar *path, const gchar *interface,
764                 const gchar *sig, GVariant *param, gpointer user_data)
765 {
766         DBG("Properties changed handling!");
767         gchar *key = NULL;
768         const gchar *state = NULL;
769         GVariantIter *iter = NULL;
770         GVariant *variant = NULL;
771         gboolean scanning = FALSE;
772
773         if (param == NULL)
774                 return;
775
776         g_variant_get(param, "(a{sv})", &iter);
777         while (g_variant_iter_loop(iter, "{sv}", &key, &variant)) {
778                 if (g_strcmp0(key, "Scanning") == 0) {
779                         scanning = g_variant_get_boolean(variant);
780                         DBG("setting scanning %s", scanning ? "TRUE" : "FALSE");
781                         if (scanning == TRUE)
782                                 netconfig_wifi_set_scanning(TRUE);
783                         else
784                                 netconfig_wifi_set_scanning(FALSE);
785
786                         g_variant_unref(variant);
787                         g_free(key);
788                         variant = NULL;
789                         key = NULL;
790                         break;
791                 } else if (g_strcmp0(key, "State") == 0) {
792                         state = g_variant_get_string(variant, NULL);
793                         if (state != NULL)
794                                 ERR("Supplicant state : %s", state);
795
796                         g_variant_unref(variant);
797                         g_free(key);
798                         variant = NULL;
799                         key = NULL;
800                         break;
801                 } else if (g_strcmp0(key, "DisconnectReason") == 0) {
802                         int reason = g_variant_get_int32(variant);
803                         ERR("Supplicant DisconnReason : %d", reason);
804
805                         g_variant_unref(variant);
806                         g_free(key);
807                         variant = NULL;
808                         key = NULL;
809                         break;
810                 } else {
811                         gchar *value;
812                         value = g_variant_print(variant, TRUE);
813                         DBG("Supplicant %s : %s", key, value);
814
815                         g_free(value);
816                 }
817         }
818
819         g_variant_iter_free(iter);
820
821         return;
822 }
823
824 static void _supplicant_bss_added(GDBusConnection *conn,
825                 const gchar *name, const gchar *path, const gchar *interface,
826                 const gchar *sig, GVariant *param, gpointer user_data)
827 {
828         DBG("BSS added handling!");
829         wifi_state_set_bss_found(TRUE);
830
831         return;
832 }
833
834 static void _supplicant_scan_done(GDBusConnection *conn,
835                 const gchar *name, const gchar *path, const gchar *interface,
836                 const gchar *sig, GVariant *param, gpointer user_data)
837 {
838         DBG("Scan Done handling!");
839         netconfig_wifi_set_scanning(FALSE);
840
841         if (netconfig_wifi_is_bssid_scan_started() == TRUE) {
842                 netconfig_wifi_bssid_signal_scandone();
843                 if (wifi_state_get_technology_state(FALSE) < NETCONFIG_WIFI_TECH_POWERED)
844                         return;
845         }
846
847         if (netconfig_wifi_get_bgscan_state() == TRUE) {
848                 if (wifi_state_get_technology_state(FALSE) >=
849                                 NETCONFIG_WIFI_TECH_POWERED)
850                         netconfig_wifi_bgscan_start(FALSE);
851
852                 wifi_start_timer_network_notification();
853         }
854
855         return;
856 }
857
858 static void _supplicant_driver_hanged(GDBusConnection *conn,
859                 const gchar *name, const gchar *path, const gchar *interface,
860                 const gchar *sig, GVariant *param, gpointer user_data)
861 {
862         DBG("Driver Hanged handling!");
863         ERR("Critical. Wi-Fi firmware crashed");
864
865 #if !defined TIZEN_WEARABLE
866         netconfig_send_message_to_net_popup("Wi-Fi Error",
867                 "Wi-Fi Driver Hanged. Please get log dump and report", "popup", NULL);
868 #endif
869         wifi_power_recover_firmware();
870
871         return;
872
873 }
874
875 static void _supplicant_session_overlapped(GDBusConnection *conn,
876                 const gchar *name, const gchar *path, const gchar *interface,
877                 const gchar *sig, GVariant *param, gpointer user_data)
878 {
879         DBG("Driver session overlapped handling!");
880         ERR("WPS PBC SESSION OVERLAPPED");
881 #if defined TIZEN_WEARABLE
882         return;
883 #else
884         netconfig_send_message_to_net_popup("WPS Error",
885                                         "wps session overlapped", "popup", NULL);
886 #endif
887 }
888
889 #if defined TIZEN_DEBUG_ENABLE
890 static void __netconfig_dumpservice_handler(GDBusConnection *conn,
891                 const gchar *name, const gchar *path, const gchar *interface,
892                 const gchar *sig, GVariant *param, gpointer user_data)
893 {
894         int mode;
895         gchar *signal_path = NULL;
896
897         if (param == NULL)
898                 return;
899
900         g_variant_get(param, "(is)", &mode, &signal_path);
901         DBG("Path: %s and mode: %d", signal_path, mode);
902
903         netconfig_dump_log(signal_path);
904         if (signal_path)
905                 g_free(signal_path);
906
907         return;
908 }
909 #endif
910
911 static void _supplicant_tdls_connected(GDBusConnection *conn,
912                 const gchar *name, const gchar *path, const gchar *interface,
913                 const gchar *sig, GVariant *param, gpointer user_data)
914 {
915         DBG("Received TDLS Connected Signal");
916         netconfig_wifi_tdls_connected_event(param);
917
918         return;
919 }
920
921 static void _supplicant_tdls_disconnected(GDBusConnection *conn,
922                 const gchar *name, const gchar *path, const gchar *interface,
923                 const gchar *sig, GVariant *param, gpointer user_data)
924 {
925         DBG("Received TDLS Disconnected Signal");
926         netconfig_wifi_tdls_disconnected_event(param);
927
928         return;
929 }
930
931 static void _supplicant_tdls_peer_found(GDBusConnection *conn,
932                 const gchar *name, const gchar *path, const gchar *interface,
933                 const gchar *sig, GVariant *param, gpointer user_data)
934 {
935         DBG("Received TDLS Peer Found Signal");
936         netconfig_wifi_tdls_peer_found_event(param);
937         return;
938 }
939
940 static void _supplicant_dpp_auth_success(GDBusConnection *conn,
941                 const gchar *name, const gchar *path, const gchar *interface,
942                 const gchar *sig, GVariant *param, gpointer user_data)
943 {
944         DBG("Received DPP Auth Success Signal");
945         netconfig_wifi_dpp_auth_success_event(param);
946         return;
947 }
948
949 static void _supplicant_dpp_auth_failed(GDBusConnection *conn,
950                 const gchar *name, const gchar *path, const gchar *interface,
951                 const gchar *sig, GVariant *param, gpointer user_data)
952 {
953         DBG("Received DPP Auth Failed Signal");
954         netconfig_wifi_dpp_auth_failed_event(param);
955         return;
956 }
957
958 static void _supplicant_dpp_not_compatible(GDBusConnection *conn,
959                 const gchar *name, const gchar *path, const gchar *interface,
960                 const gchar *sig, GVariant *param, gpointer user_data)
961 {
962         DBG("Received DPP Not Compatible Signal");
963         netconfig_wifi_dpp_not_compatible_event(param);
964         return;
965 }
966
967 static void _supplicant_dpp_conf_failed(GDBusConnection *conn,
968                 const gchar *name, const gchar *path, const gchar *interface,
969                 const gchar *sig, GVariant *param, gpointer user_data)
970 {
971         DBG("Received DPP Conf Failed Signal");
972         netconfig_wifi_dpp_conf_failed_event(param);
973         return;
974 }
975
976 static void _supplicant_dpp_scan_peer_qr(GDBusConnection *conn,
977                 const gchar *name, const gchar *path, const gchar *interface,
978                 const gchar *sig, GVariant *param, gpointer user_data)
979 {
980         DBG("Received DPP Scan Peer QR Signal");
981         netconfig_wifi_dpp_scan_peer_qr_event(param);
982         return;
983 }
984
985 static void _supplicant_dpp_network_id(GDBusConnection *conn,
986                 const gchar *name, const gchar *path, const gchar *interface,
987                 const gchar *sig, GVariant *param, gpointer user_data)
988 {
989         DBG("Received DPP Network Id Signal");
990         netconfig_wifi_dpp_network_id_event(param);
991         return;
992 }
993
994 static void _supplicant_dpp_conf_sent(GDBusConnection *conn,
995                 const gchar *name, const gchar *path, const gchar *interface,
996                 const gchar *sig, GVariant *param, gpointer user_data)
997 {
998         DBG("Received DPP Conf Sent Signal");
999         netconfig_wifi_dpp_conf_sent_event(param);
1000         return;
1001 }
1002
1003 static void _supplicant_dpp_conf_received(GDBusConnection *conn,
1004                 const gchar *name, const gchar *path, const gchar *interface,
1005                 const gchar *sig, GVariant *param, gpointer user_data)
1006 {
1007         DBG("Received DPP Conf Received Signal");
1008         netconfig_wifi_dpp_conf_received_event(param);
1009         return;
1010 }
1011
1012 static void _supplicant_dpp_conf_obj(GDBusConnection *conn,
1013                 const gchar *name, const gchar *path, const gchar *interface,
1014                 const gchar *sig, GVariant *param, gpointer user_data)
1015 {
1016         DBG("Received DPP Conf Obj Signal");
1017         netconfig_wifi_dpp_conf_obj_event(param);
1018         return;
1019 }
1020
1021 static void _supplicant_wifi_wps_connected(GVariant *param)
1022 {
1023         gchar *key;
1024         char ssid[32] = {0, };
1025         gchar *name;
1026         GVariantIter *iter;
1027         GVariant *variant;
1028         int config_error = 0;
1029         int error_indication = 0;
1030         gsize ssid_len = 0;
1031
1032         if (param == NULL) {
1033                 ERR("Param is NULL");
1034                 return;
1035         }
1036
1037         g_variant_get(param, "(sa{sv})", &name, &iter);
1038         INFO("wps Result: %s", name);
1039         while (g_variant_iter_loop(iter, "{sv}", &key, &variant)) {
1040                 INFO("wps Key is %s", key);
1041                 if (g_strcmp0(key, "SSID") == 0) {
1042                         const char *t_key = NULL;
1043                         t_key = g_variant_get_fixed_array(variant, &ssid_len, sizeof(guchar));
1044                         INFO("wps ssid_len is %zd ", ssid_len);
1045                         if (t_key == NULL) {
1046                                 g_free(key);
1047                                 g_variant_unref(variant);
1048                                 ERR("WPS PBC Connection Failed");
1049                                 goto error;
1050                         }
1051                         if (ssid_len > 0 && ssid_len <= 32) {
1052                                 memcpy(ssid, t_key, ssid_len);
1053                         } else {
1054                                 memset(ssid, 0, sizeof(ssid));
1055                                 ssid_len = 0;
1056                         }
1057                         INFO("WPS PBC Connection completed with AP %s", ssid);
1058                         netconfig_wifi_notify_wps_completed(ssid, ssid_len);
1059                 }
1060         }
1061
1062         g_variant_iter_free(iter);
1063         g_free(name);
1064         return;
1065
1066 error:
1067         g_variant_iter_free(iter);
1068         g_free(name);
1069         error_indication = WPS_EI_OPERATION_FAILED;
1070         config_error = WPS_CFG_NO_ERROR;
1071         ERR("Error Occured! Notifying Fail Event");
1072         netconfig_wifi_notify_wps_fail_event(config_error, error_indication);
1073
1074 }
1075
1076 static void _supplicant_wifi_wps_event(GVariant *param)
1077 {
1078         gchar *key;
1079         gchar *name;
1080         GVariantIter *iter;
1081         GVariant *variant;
1082         gint32 config_error = 0;
1083         gint32 error_indication = 0;
1084
1085         if (param == NULL) {
1086                 ERR("Param is NULL");
1087                 return;
1088         }
1089
1090         g_variant_get(param, "(sa{sv})", &name, &iter);
1091         INFO("Event Result: %s", name);
1092         if (g_strcmp0(name, "fail") == 0) {
1093                 while (g_variant_iter_loop(iter, "{sv}", &key, &variant)) {
1094                         if (key == NULL)
1095                                 goto error;
1096                         INFO("Key is %s", key);
1097                         if (g_strcmp0(key, "config_error") == 0) {
1098                                 config_error = g_variant_get_int32(variant);
1099                                 ERR("Config Error %d", config_error);
1100                         } else if (g_strcmp0(key, "error_indication") == 0) {
1101                                 error_indication = g_variant_get_int32(variant);
1102                                 ERR("Error Indication %d", error_indication);
1103                         }
1104                 }
1105                 netconfig_wifi_notify_wps_fail_event(config_error, error_indication);
1106         }
1107
1108         g_variant_iter_free(iter);
1109         g_free(name);
1110         return;
1111
1112 error:
1113         g_variant_iter_free(iter);
1114         g_free(name);
1115         error_indication = WPS_EI_OPERATION_FAILED;
1116         config_error = WPS_CFG_NO_ERROR;
1117         ERR("Error Occured! Notifying Fail Event");
1118         netconfig_wifi_notify_wps_fail_event(config_error, error_indication);
1119 }
1120
1121 static void _find_service_and_set_passphrase(char *ssid, char *key)
1122 {
1123         GString *str;
1124         GVariant *reply = NULL;
1125         int i, j, ssid_len;
1126         char *service_path;
1127         char *dev_mac_addr;
1128         char ident[13];
1129         gchar *enc_passphrase;
1130
1131         ssid_len = strlen(ssid);
1132
1133         str = g_string_sized_new((ssid_len * 2) + 55);
1134         if (!str)
1135                 return;
1136
1137         dev_mac_addr = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
1138         if (!dev_mac_addr) {
1139                 ERR("Failed to get mac address from vconf key");
1140                 g_string_free(str, TRUE);
1141                 return;
1142         }
1143
1144         j = 0;
1145         for (i = 0; dev_mac_addr[i]; i++) {
1146                 if (dev_mac_addr[i] != ':')
1147                         ident[j++] = dev_mac_addr[i];
1148         }
1149         ident[j] = '\0';
1150
1151         g_string_append_printf(str, "%s%s_", CONNMAN_WIFI_SERVICE_PROFILE_PREFIX,
1152                                                    ident);
1153         free(dev_mac_addr);
1154
1155         for (i = 0; i < ssid_len; i++) {
1156                 if (ssid[i] != '"')
1157                         g_string_append_printf(str, "%02x", ssid[i]);
1158         }
1159
1160         g_string_append_printf(str, "_managed_psk");
1161
1162         service_path = g_string_free(str, FALSE);
1163
1164         enc_passphrase = _netconfig_encrypt_passphrase(key);
1165         if (!enc_passphrase) {
1166                 ERR("Failed to encrypt passphrase");
1167                 g_free(service_path);
1168                 return;
1169         }
1170
1171         INFO("wps service_path %s Passphrase %s", service_path, enc_passphrase);
1172
1173         reply = netconfig_invoke_dbus_method(CONNMAN_SERVICE, service_path,
1174                                          CONNMAN_SERVICE_INTERFACE, "SetProperty",
1175                                          g_variant_new("(sv)", "Passphrase",
1176                                                                    g_variant_new_string(enc_passphrase)));
1177         if (reply != NULL)
1178                 g_variant_unref(reply);
1179         else
1180                 ERR("Failed to set passphrase");
1181
1182         g_free(service_path);
1183
1184         return;
1185 }
1186
1187 static void _supplicant_wifi_wps_credentials(GVariant *param)
1188 {
1189         gchar *key;
1190         char ssid[32] = {0, };
1191         char wps_key[100] = {0, };
1192         GVariantIter *iter;
1193         GVariant *variant;
1194         int config_error = 0;
1195         int error_indication = 0;
1196         gsize ssid_len = 0;
1197         char *key_mgmt = NULL;
1198
1199         if (param == NULL) {
1200                 ERR("Param is NULL");
1201                 return;
1202         }
1203
1204         g_variant_get(param, "(a{sv})", &iter);
1205         while (g_variant_iter_loop(iter, "{sv}", &key, &variant)) {
1206                 if (key == NULL)
1207                         goto error;
1208                 INFO("wps Key is %s", key);
1209                 if (g_strcmp0(key, "Key") == 0) {
1210                         gsize key_len = 0;
1211                         const char *t_key = NULL;
1212                         key_len = g_variant_get_size(variant);
1213
1214                         INFO("wps password len %zd ", key_len);
1215                         if (key_len > 0) {
1216                                 t_key = g_variant_get_fixed_array(variant, &key_len,
1217                                                   sizeof(guchar));
1218                                 if (!t_key) {
1219                                         g_free(key);
1220                                         g_variant_unref(variant);
1221                                         goto error;
1222                                 }
1223                                 strncpy(wps_key, t_key, key_len);
1224                                 wps_key[key_len] = '\0';
1225                                 INFO("WPS Key in process credentials %s", wps_key);
1226                         } else
1227                                 SLOGI("WPS AP Security ->Open");
1228                 } else if (g_strcmp0(key, "SSID") == 0) {
1229                         const char *t_key = NULL;
1230                         t_key = g_variant_get_fixed_array(variant, &ssid_len,
1231                                                   sizeof(guchar));
1232                         INFO("wps ssid_len is %zd ", ssid_len);
1233                         if (!t_key) {
1234                                 g_free(key);
1235                                 g_variant_unref(variant);
1236                                 goto error;
1237                         }
1238                         if (ssid_len > 0 && ssid_len <= 32) {
1239                                 memcpy(ssid, t_key, ssid_len);
1240                         } else {
1241                                 memset(ssid, 0, sizeof(ssid));
1242                                 ssid_len = 0;
1243                         }
1244                         INFO("SSID in process credentials %s", ssid);
1245                 } else if (g_strcmp0(key, "AuthType") == 0) {
1246                         gchar *auth_type;
1247                         GVariantIter *var_iter;
1248                         g_variant_get(variant, "as", &var_iter);
1249                         while (g_variant_iter_loop(var_iter, "s", &auth_type)) {
1250                                 INFO("wps auth_type %s", auth_type);
1251                                 if (g_strcmp0(auth_type, "wpa-psk") == 0 ||
1252                                         g_strcmp0(auth_type, "wpa2-psk") == 0)
1253                                         key_mgmt = "psk";
1254                                 else if (g_strcmp0(auth_type, "open") == 0)
1255                                         key_mgmt = "none";
1256                         }
1257                         g_variant_iter_free(var_iter);
1258                 } else if (g_strcmp0(key, "EncrType") == 0) {
1259                         gchar *encr_type;
1260                         GVariantIter *var_iter;
1261                         g_variant_get(variant, "as", &var_iter);
1262                         while (g_variant_iter_loop(var_iter, "s", &encr_type))
1263                                 INFO("wps encr_type %s", encr_type);
1264                         g_variant_iter_free(var_iter);
1265                 } else if (g_strcmp0(key, "BSSID") == 0) {
1266                         const guchar *bssid;
1267                         gsize bssid_len;
1268                         bssid = g_variant_get_fixed_array(variant, &bssid_len,
1269                                                                 sizeof(guchar));
1270                         if (bssid && bssid_len == 6)
1271                                 INFO("wps BSSID %2x:%2x:%2x:%2x:%2x:%2x", bssid[0], bssid[1],
1272                                          bssid[2], bssid[3], bssid[4], bssid[5]);
1273                 }
1274         }
1275
1276         g_variant_iter_free(iter);
1277
1278         if (g_strcmp0(key_mgmt, "psk") == 0)
1279                 _find_service_and_set_passphrase(ssid, wps_key);
1280
1281 #if 0
1282         /*
1283          * Notify WPS Credentials only when requested through WPS PBC
1284          * In case of WPS PIN connman will take care of notification
1285          */
1286         if (netconfig_get_wps_field() == TRUE)
1287 #endif
1288         netconfig_wifi_notify_wps_credentials(ssid, ssid_len, wps_key);
1289         return;
1290
1291 error:
1292         g_variant_iter_free(iter);
1293         error_indication = WPS_EI_OPERATION_FAILED;
1294         config_error = WPS_CFG_NO_ERROR;
1295         ERR("Error Occured! Notifying Fail Event");
1296         netconfig_wifi_notify_wps_fail_event(config_error, error_indication);
1297 }
1298
1299 static void __netconfig_wps_signal_filter_handler(GDBusConnection *conn,
1300                 const gchar *name, const gchar *path, const gchar *interface,
1301                 const gchar *sig, GVariant *param, gpointer user_data)
1302 {
1303         if (g_strcmp0(sig, SIGNAL_WPS_CREDENTIALS) == 0) {
1304                 INFO("Received wps CREDENTIALS Signal from Supplicant");
1305                 _supplicant_wifi_wps_credentials(param);
1306         } else if (g_strcmp0(sig, SIGNAL_WPS_EVENT) == 0) {
1307                 INFO("Received wps EVENT Signal from Supplicant");
1308                 _supplicant_wifi_wps_event(param);
1309         } else if (g_strcmp0(sig, SIGNAL_WPS_CONNECTED) == 0) {
1310                 INFO("Received WPSConnected Signal from Supplicant");
1311                 _supplicant_wifi_wps_connected(param);
1312         }
1313
1314         return;
1315 }
1316
1317 static void __telephony_svc_cs_changed_cb(keynode_t * node, void *data)
1318 {
1319         int telephony_svc_cs = 0;
1320
1321         if (node != NULL)
1322                 telephony_svc_cs = vconf_keynode_get_int(node);
1323         else
1324                 netconfig_vconf_get_int(VCONFKEY_TELEPHONY_SVC_CS, &telephony_svc_cs);
1325
1326         DBG("Telephony svc cs [%d]", telephony_svc_cs);
1327
1328         switch (telephony_svc_cs) {
1329         case 1:
1330                 netconfig_battery_end_dn();
1331                 break;
1332         case 2:
1333                 netconfig_battery_start_dn();
1334                 break;
1335         default:
1336                 break;
1337         }
1338 }
1339
1340 static void __telephony_rssi_changed_cb(keynode_t * node, void *data)
1341 {
1342         int rssi = 0;
1343
1344         if (node != NULL)
1345                 rssi = vconf_keynode_get_int(node);
1346         else
1347                 netconfig_vconf_get_int(VCONFKEY_TELEPHONY_RSSI, &rssi);
1348
1349         DBG("Telephony rssi [%d]", rssi);
1350
1351         netconfig_battery_update_dn_rssi(rssi);
1352 }
1353
1354 static void _notify_telephony_svc_cs(void)
1355 {
1356         int telephony_svc_cs = 0;
1357
1358         netconfig_vconf_get_int(VCONFKEY_TELEPHONY_SVC_CS, &telephony_svc_cs);
1359         DBG("Telephony svc cs [%d]", telephony_svc_cs);
1360
1361         switch (telephony_svc_cs) {
1362         case 1:
1363                 netconfig_battery_end_dn();
1364                 break;
1365         case 2:
1366                 netconfig_battery_start_dn();
1367                 break;
1368         default:
1369                 break;
1370         }
1371
1372         vconf_notify_key_changed(VCONFKEY_TELEPHONY_SVC_CS, __telephony_svc_cs_changed_cb, NULL);
1373 }
1374
1375 static void _notify_telephony_rssi(void)
1376 {
1377         int rssi = 0;
1378
1379         netconfig_vconf_get_int(VCONFKEY_TELEPHONY_RSSI, &rssi);
1380         DBG("Telephony rssi [%d]", rssi);
1381
1382         netconfig_battery_update_dn_rssi(rssi);
1383
1384         vconf_notify_key_changed(VCONFKEY_TELEPHONY_RSSI, __telephony_rssi_changed_cb, NULL);
1385 }
1386
1387 static void _ignore_telephony_svc_cs(void)
1388 {
1389         vconf_ignore_key_changed(VCONFKEY_TELEPHONY_SVC_CS, __telephony_svc_cs_changed_cb);
1390 }
1391
1392 static void _ignore_telephony_rssi(void)
1393 {
1394         vconf_ignore_key_changed(VCONFKEY_TELEPHONY_RSSI, __telephony_rssi_changed_cb);
1395 }
1396
1397 static supplicant_signal_cb supplicant_cbs[SIG_MAX] = {
1398                 _supplicant_interface_removed,
1399                 _supplicant_properties_changed,
1400                 _supplicant_bss_added,
1401                 _supplicant_scan_done,
1402                 _supplicant_driver_hanged,
1403                 _supplicant_session_overlapped,
1404                 _supplicant_tdls_connected,
1405                 _supplicant_tdls_disconnected,
1406                 _supplicant_tdls_peer_found,
1407                 _supplicant_dpp_auth_success,
1408                 _supplicant_dpp_auth_failed,
1409                 _supplicant_dpp_not_compatible,
1410                 _supplicant_dpp_conf_failed,
1411                 _supplicant_dpp_scan_peer_qr,
1412                 _supplicant_dpp_network_id,
1413                 _supplicant_dpp_conf_sent,
1414                 _supplicant_dpp_conf_received,
1415                 _supplicant_dpp_conf_obj,
1416 };
1417
1418 void register_gdbus_signal(void)
1419 {
1420         GDBusConnection *connection = NULL;
1421         SuppSigArrayIndex sig;
1422         connection = netdbus_get_connection();
1423
1424         if (connection == NULL) {
1425                 ERR("Failed to get GDbus Connection");
1426                 return;
1427         }
1428
1429         /* listening to messages from all objects as no path is specified */
1430         /* see signals from the given interface */
1431         conn_subscription_ids[0] = g_dbus_connection_signal_subscribe(
1432                         connection,
1433                         CONNMAN_SERVICE,
1434                         CONNMAN_TECHNOLOGY_INTERFACE,
1435                         NULL,
1436                         NULL,
1437                         NULL,
1438                         G_DBUS_SIGNAL_FLAGS_NONE,
1439                         _technology_signal_cb,
1440                         NULL,
1441                         NULL);
1442
1443         conn_subscription_ids[1] = g_dbus_connection_signal_subscribe(
1444                         connection,
1445                         CONNMAN_SERVICE,
1446                         CONNMAN_SERVICE_INTERFACE,
1447                         CONNMAN_SIGNAL_PROPERTY_CHANGED,
1448                         NULL,
1449                         NULL,
1450                         G_DBUS_SIGNAL_FLAGS_NONE,
1451                         _service_signal_cb,
1452                         NULL,
1453                         NULL);
1454
1455         conn_subscription_ids[2] = g_dbus_connection_signal_subscribe(
1456                         connection,
1457                         DBUS_SERVICE_DBUS,
1458                         DBUS_INTERFACE_DBUS,
1459                         SIGNAL_NAME_OWNER_CHANGED,
1460                         NULL,
1461                         CONNMAN_SERVICE,
1462                         G_DBUS_SIGNAL_FLAGS_NONE,
1463                         _dbus_name_changed_cb,
1464                         NULL,
1465                         NULL);
1466
1467         conn_subscription_ids[3] = g_dbus_connection_signal_subscribe(
1468                         connection,
1469                         CONNMAN_SERVICE,
1470                         CONNMAN_MANAGER_INTERFACE,
1471                         CONNMAN_SIGNAL_SERVICES_CHANGED,
1472                         NULL,
1473                         NULL,
1474                         G_DBUS_SIGNAL_FLAGS_NONE,
1475                         _services_changed_cb,
1476                         NULL,
1477                         NULL);
1478
1479         conn_subscription_ids[4] = g_dbus_connection_signal_subscribe(
1480                         connection,
1481                         CONNMAN_SERVICE,
1482                         CONNMAN_MANAGER_INTERFACE,
1483                         SIGNAL_SCAN_CHANGED,
1484                         CONNMAN_MANAGER_PATH,
1485                         NULL,
1486                         G_DBUS_SIGNAL_FLAGS_NONE,
1487                         _scan_changed_cb,
1488                         NULL,
1489                         NULL);
1490
1491         INFO("Successfully register connman DBus signal filters");
1492
1493         conn_subscription_ids[5] = g_dbus_connection_signal_subscribe(
1494                         connection,
1495                         SUPPLICANT_SERVICE,
1496                         SUPPLICANT_INTERFACE ".Interface.WPS",
1497                         NULL,
1498                         NULL,
1499                         NULL,
1500                         G_DBUS_SIGNAL_FLAGS_NONE,
1501                         __netconfig_wps_signal_filter_handler,
1502                         NULL,
1503                         NULL);
1504
1505         INFO("Successfully register Supplicant WPS DBus signal filters");
1506
1507         conn_subscription_ids[5] = g_dbus_connection_signal_subscribe(
1508                         connection,
1509                         DBUS_SERVICE_DBUS,
1510                         DBUS_INTERFACE_DBUS,
1511                         SIGNAL_NAME_OWNER_CHANGED,
1512                         NULL,
1513                         CLATD_SERVICE,
1514                         G_DBUS_SIGNAL_FLAGS_NONE,
1515                         _dbus_name_changed_cb,
1516                         NULL,
1517                         NULL);
1518
1519         INFO("Successfully register clat DBus signal filters");
1520
1521         for (sig = SIG_INTERFACE_REMOVED; sig < SIG_MAX; sig++) {
1522                 supp_subscription_ids[sig] = g_dbus_connection_signal_subscribe(
1523                                 connection,
1524                                 SUPPLICANT_SERVICE,
1525                                 supplicant_iface_for_sig[sig],
1526                                 supplicant_signals[sig],
1527                                 NULL,
1528                                 NULL,
1529                                 G_DBUS_SIGNAL_FLAGS_NONE,
1530                                 supplicant_cbs[sig],
1531                                 NULL,
1532                                 NULL);
1533         }
1534
1535         INFO("Successfully register Supplicant DBus signal filters");
1536
1537 #if defined TIZEN_DEBUG_ENABLE
1538         dumpservice_subscription_id = g_dbus_connection_signal_subscribe(
1539                         connection,
1540                         NULL,
1541                         DUMP_SERVICE_INTERFACE,
1542                         DUMP_SIGNAL,
1543                         NULL,
1544                         NULL,
1545                         G_DBUS_SIGNAL_FLAGS_NONE,
1546                         __netconfig_dumpservice_handler,
1547                         NULL,
1548                         NULL);
1549
1550         INFO("Successfully registered Dumpservice DBus signal filter");
1551 #endif
1552
1553         _notify_telephony_svc_cs();
1554         _notify_telephony_rssi();
1555
1556         INFO("Successfully register Telephony vconf keys");
1557
1558         /* In case ConnMan precedes this signal register,
1559          * net-config should update the default connected profile.
1560          */
1561         netconfig_update_default();
1562 }
1563
1564 void deregister_gdbus_signal(void)
1565 {
1566         GDBusConnection *connection = NULL;
1567         int signal;
1568         SuppSigArrayIndex sig;
1569         connection = netdbus_get_connection();
1570         if (!connection) {
1571                 ERR("Already de-registered. Nothing to be done");
1572                 return;
1573         }
1574
1575         for (signal = 0; signal < TOTAL_CONN_SIGNALS; signal++) {
1576                 if (conn_subscription_ids[signal]) {
1577                         g_dbus_connection_signal_unsubscribe(connection,
1578                                                 conn_subscription_ids[signal]);
1579                 }
1580         }
1581
1582         for (sig = SIG_INTERFACE_REMOVED; sig < SIG_MAX; sig++) {
1583                 if (supp_subscription_ids[sig]) {
1584                         g_dbus_connection_signal_unsubscribe(connection,
1585                                                 supp_subscription_ids[sig]);
1586                 }
1587         }
1588
1589 #if defined TIZEN_DEBUG_ENABLE
1590         g_dbus_connection_signal_unsubscribe(connection,
1591                         dumpservice_subscription_id);
1592 #endif
1593
1594         _ignore_telephony_svc_cs();
1595         _ignore_telephony_rssi();
1596 }