[ACR]Add new APIs for TDLS
[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 <string.h>
22 #include <vconf.h>
23 #include <vconf-keys.h>
24
25 #include "log.h"
26 #include "util.h"
27 #include "netdbus.h"
28 #include "neterror.h"
29 #include "wifi-wps.h"
30 #include "wifi-agent.h"
31 #include "wifi-power.h"
32 #include "wifi-state.h"
33 #include "netsupplicant.h"
34 #include "network-state.h"
35 #include "cellular-state.h"
36 #include "signal-handler.h"
37 #include "wifi-ssid-scan.h"
38 #include "wifi-background-scan.h"
39 #include "wifi-tdls.h"
40
41 #if defined TIZEN_DEBUG_DISABLE
42 #include "wifi-dump.h"
43 #endif
44
45 #define DBUS_SERVICE_DBUS                       "org.freedesktop.DBus"
46 #define DBUS_INTERFACE_DBUS                     "org.freedesktop.DBus"
47 #define SIGNAL_INTERFACE_REMOVED                "InterfaceRemoved"
48 #define SIGNAL_SCAN_DONE                        "ScanDone"
49 #define SIGNAL_BSS_ADDED                        "BSSAdded"
50 #define SIGNAL_PROPERTIES_CHANGED               "PropertiesChanged"
51 #define SIGNAL_PROPERTIES_DRIVER_HANGED         "DriverHanged"
52 #define SIGNAL_PROPERTIES_SESSION_OVERLAPPED    "SessionOverlapped"
53 #define SIGNAL_TDLS_CONNECTED                           "TDLSConnected"
54 #define SIGNAL_TDLS_DISCONNECTED                        "TDLSDisconnected"
55 #define SIGNAL_TDLS_PEER_FOUND                          "TDLSPeerFound"
56 #define CONNMAN_SIGNAL_SERVICES_CHANGED         "ServicesChanged"
57 #define CONNMAN_SIGNAL_PROPERTY_CHANGED         "PropertyChanged"
58 #define CONNMAN_SIGNAL_NAME_CHANGED             "NameOwnerChanged"
59
60 #define MAX_SIG_LEN 64
61 #define TOTAL_CONN_SIGNALS 3
62
63 typedef enum {
64         SIG_INTERFACE_REMOVED = 0,
65         SIG_PROPERTIES_CHANGED,
66         SIG_BSS_ADDED,
67         SIG_SCAN_DONE,
68         SIG_DRIVER_HANGED,
69         SIG_SESSION_OVERLAPPED,
70         SIG_TDLS_CONNECTED,
71         SIG_TDLS_DISCONNECTED,
72         SIG_TDLS_PEER_FOUND,
73         SIG_MAX
74 } SuppSigArrayIndex;
75
76 static int conn_subscription_ids[TOTAL_CONN_SIGNALS] = {0};
77 static const char supplicant_signals[SIG_MAX][MAX_SIG_LEN] = {
78                 SIGNAL_INTERFACE_REMOVED,
79                 SIGNAL_PROPERTIES_CHANGED,
80                 SIGNAL_BSS_ADDED,
81                 SIGNAL_SCAN_DONE,
82                 SIGNAL_PROPERTIES_DRIVER_HANGED,
83                 SIGNAL_PROPERTIES_SESSION_OVERLAPPED,
84                 SIGNAL_TDLS_CONNECTED,
85                 SIGNAL_TDLS_DISCONNECTED,
86                 SIGNAL_TDLS_PEER_FOUND,
87 };
88
89 static int supp_subscription_ids[SIG_MAX] = {0};
90 static int dumpservice_subscription_id = 0;
91
92 typedef void (*supplicant_signal_cb)(GDBusConnection *conn,
93                 const gchar *name, const gchar *path, const gchar *interface,
94                 const gchar *sig, GVariant *param, gpointer user_data);
95 typedef void (*connman_signal_cb)(GDBusConnection *conn,
96                 const gchar *name, const gchar *path, const gchar *interface,
97                 const gchar *sig, GVariant *param, gpointer user_data);
98
99 static void _technology_signal_cb(GDBusConnection *conn,
100                 const gchar *name, const gchar *path, const gchar *interface,
101                 const gchar *sig, GVariant *param, gpointer user_data)
102 {
103         gchar *key = NULL;
104         gboolean value = FALSE;
105         GVariant *var = NULL;
106
107         if (param == NULL)
108                 return;
109
110         if (g_str_has_prefix(path, CONNMAN_WIFI_TECHNOLOGY_PREFIX) == TRUE) {
111                 g_variant_get(param, "(sv)", &key, &var);
112                 if (g_strcmp0(key, "Powered") == 0) {
113                         /* Power state */
114                         value = g_variant_get_boolean(var);
115                         if (value == TRUE) {
116                                 wifi_state_update_power_state(TRUE);
117                         } else {
118                                 wifi_state_update_power_state(FALSE);
119                         }
120                 } else if (g_strcmp0(key, "Connected") == 0) {
121                         /* Connection state */
122                         wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_CONNECTED);
123                 } else if (g_strcmp0(key, "Tethering") == 0) {
124                         /* Tethering state */
125                         wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_TETHERED);
126                 }
127                 if (key)
128                         g_free(key);
129                 if (var)
130                         g_variant_unref(var);
131         }
132 }
133
134 static void _service_signal_cb(GDBusConnection *conn,
135                 const gchar *name, const gchar *path,
136                 const gchar *interface, const gchar *sig, GVariant *param, gpointer user_data)
137 {
138         gchar *sigvalue = NULL;
139         gchar *property;
140         GVariant *variant = NULL, *var;
141         GVariantIter *iter;
142         const gchar *value = NULL;
143
144         if (path == NULL || param == NULL)
145                 goto done;
146
147         g_variant_get(param, "(sv)", &sigvalue, &variant);
148         if (sigvalue == NULL)
149                 goto done;
150
151         if (g_strcmp0(sig, CONNMAN_SIGNAL_PROPERTY_CHANGED) != 0) {
152                 goto done;
153         }
154
155         if (g_strcmp0(sigvalue, "State") == 0) {
156                 g_variant_get(variant, "s", &property);
157
158                 DBG("[%s] %s", property, path);
159                 if (netconfig_is_wifi_profile(path) == TRUE) {
160                         int wifi_state = 0;
161
162                         vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
163                         if (wifi_state == VCONFKEY_WIFI_OFF)
164                                 goto done;
165
166                         if (g_strcmp0(property, "ready") == 0 || g_strcmp0(property, "online") == 0) {
167                                 if (wifi_state >= VCONFKEY_WIFI_CONNECTED)
168                                         goto done;
169
170                                 netconfig_update_default_profile(path);
171
172                                 wifi_state_set_service_state(NETCONFIG_WIFI_CONNECTED);
173
174                         } else if (g_strcmp0(property, "failure") == 0 || g_strcmp0(property, "disconnect") == 0 || g_strcmp0(property, "idle") == 0) {
175                                 if (netconfig_get_default_profile() == NULL ||
176                                                 netconfig_is_wifi_profile(netconfig_get_default_profile())
177                                                 != TRUE) {
178                                         if (g_strcmp0(property, "failure") == 0)
179                                                 wifi_state_set_service_state(NETCONFIG_WIFI_FAILURE);
180                                         else
181                                                 wifi_state_set_service_state(NETCONFIG_WIFI_IDLE);
182                                         goto done;
183                                 }
184
185                                 if (g_strcmp0(path, netconfig_get_default_profile()) != 0)
186                                         goto done;
187
188                                 netconfig_update_default_profile(NULL);
189
190                                 if (g_strcmp0(property, "failure") == 0)
191                                         wifi_state_set_service_state(NETCONFIG_WIFI_FAILURE);
192                                 else
193                                         wifi_state_set_service_state(NETCONFIG_WIFI_IDLE);
194
195                         } else if (g_strcmp0(property, "association") == 0 || g_strcmp0(property, "configuration") == 0) {
196                                 if (netconfig_get_default_profile() == NULL ||
197                                                 netconfig_is_wifi_profile(netconfig_get_default_profile()) != TRUE) {
198                                         if (g_strcmp0(property, "association") == 0)
199                                                 wifi_state_set_service_state(NETCONFIG_WIFI_ASSOCIATION);
200                                         else
201                                                 wifi_state_set_service_state(NETCONFIG_WIFI_CONFIGURATION);
202                                         goto done;
203                                 }
204
205                                 if (g_strcmp0(path, netconfig_get_default_profile()) != 0)
206                                         goto done;
207
208                                 netconfig_update_default_profile(NULL);
209
210                                 if (g_strcmp0(property, "association") == 0)
211                                         wifi_state_set_service_state(NETCONFIG_WIFI_ASSOCIATION);
212                                 else
213                                         wifi_state_set_service_state(NETCONFIG_WIFI_CONFIGURATION);
214
215                         }
216                 } else {
217                         if (g_strcmp0(property, "ready") == 0 || g_strcmp0(property, "online") == 0) {
218                                 if (netconfig_get_default_profile() == NULL) {
219                                         if(!netconfig_is_cellular_profile(path)) {
220                                                 netconfig_update_default_profile(path);
221                                         } else {
222                                                 if (netconfig_is_cellular_internet_profile(path)) {
223                                                         netconfig_update_default_profile(path);
224                                                 }
225                                         }
226                                 }
227
228                                 if (netconfig_is_cellular_profile(path) && netconfig_is_cellular_internet_profile(path))
229                                         cellular_state_set_service_state(NETCONFIG_CELLULAR_ONLINE);
230
231                         } else if (g_strcmp0(property, "failure") == 0 || g_strcmp0(property, "disconnect") == 0 || g_strcmp0(property, "idle") == 0) {
232                                 if (netconfig_get_default_profile() == NULL)
233                                         goto done;
234
235                                 if (netconfig_is_cellular_profile(path) && netconfig_is_cellular_internet_profile(path))
236                                         cellular_state_set_service_state(NETCONFIG_CELLULAR_IDLE);
237
238                                 if (g_strcmp0(path, netconfig_get_default_profile()) != 0)
239                                         goto done;
240
241                                 netconfig_update_default_profile(NULL);
242                         } else if (g_strcmp0(property, "association") == 0 ||   g_strcmp0(property, "configuration") == 0) {
243                                 if (netconfig_get_default_profile() == NULL)
244                                         goto done;
245
246                                 if (netconfig_is_cellular_profile(path) && netconfig_is_cellular_internet_profile(path))
247                                         cellular_state_set_service_state(NETCONFIG_CELLULAR_CONNECTING);
248
249                                 if (g_strcmp0(path, netconfig_get_default_profile()) != 0)
250                                         goto done;
251
252                                 netconfig_update_default_profile(NULL);
253                         }
254                 }
255         } else if (g_strcmp0(sigvalue, "Proxy") == 0) {
256                 if (netconfig_is_wifi_profile(path) != TRUE || g_strcmp0(path, netconfig_get_default_profile()) != 0)
257                         goto done;
258
259                 if (!g_variant_type_equal(variant, G_VARIANT_TYPE_ARRAY))
260                         goto done;
261
262                 g_variant_get(variant, "a{sv}", &iter);
263                 while (g_variant_iter_loop(iter, "{sv}", &property, &var)) {
264                         if (g_strcmp0(property, "Servers") == 0) {
265                                 GVariantIter *iter_sub = NULL;
266
267                                 g_variant_get(var, "as", &iter_sub);
268                                 g_variant_iter_loop(iter_sub, "s", &value);
269                                 g_variant_iter_free(iter_sub);
270
271                                 DBG("Proxy - [%s]", value);
272                                 vconf_set_str(VCONFKEY_NETWORK_PROXY, value);
273
274                                 g_free(property);
275                                 g_variant_unref(var);
276                                 break;
277                         } else if (g_strcmp0(property, "Method") == 0) {
278                                 value = g_variant_get_string(var, NULL);
279                                 DBG("Method - [%s]", value);
280
281                                 if (g_strcmp0(value, "direct") == 0)
282                                         vconf_set_str(VCONFKEY_NETWORK_PROXY, "");
283
284                                 g_free(property);
285                                 g_variant_unref(var);
286                                 break;
287                         }
288                 }
289
290                 g_variant_iter_free(iter);
291         } else if (g_strcmp0(sigvalue, "Error") == 0) {
292                 g_variant_get(variant, "s", &property);
293                 INFO("[%s] Property : %s", sigvalue, property);
294         }
295 done:
296         if (sigvalue)
297                 g_free(sigvalue);
298
299         if (variant)
300                 g_variant_unref(variant);
301
302         return;
303 }
304
305 static void _dbus_name_changed_cb(GDBusConnection *conn,
306                 const gchar *Name, const gchar *path, const gchar *interface,
307                 const gchar *sig, GVariant *param, gpointer user_data)
308 {
309         gchar *name = NULL;
310         gchar *old = NULL;
311         gchar *new = NULL;
312
313         if (param == NULL)
314                 return;
315
316         g_variant_get(param, "(sss)", &name, &old, &new);
317
318         if (g_strcmp0(name, CONNMAN_SERVICE) == 0 && *new == '\0') {
319                 DBG("ConnMan destroyed: name %s, old %s, new %s", name, old, new);
320
321                 connman_register_agent();
322         }
323         if (name)
324                 g_free(name);
325         if (old)
326                 g_free(old);
327         if (new)
328                 g_free(new);
329
330         return;
331 }
332
333 static void _supplicant_interface_removed(GDBusConnection *conn,
334                 const gchar *name, const gchar *path, const gchar *interface,
335                 const gchar *sig, GVariant *param, gpointer user_data)
336 {
337         DBG("Interface removed handling!");
338         if (netconfig_wifi_is_wps_enabled() == TRUE)
339                 netconfig_wifi_wps_signal_scanaborted();
340
341         return;
342 }
343
344 static void _supplicant_properties_changed(GDBusConnection *conn,
345                 const gchar *name, const gchar *path, const gchar *interface,
346                 const gchar *sig, GVariant *param, gpointer user_data)
347 {
348         DBG("Properties changed handling!");
349         gchar *key;
350         GVariantIter *iter;
351         GVariant *variant;
352         gboolean scanning = FALSE;
353
354         if (param == NULL)
355                 return;
356
357         g_variant_get(param, "(a{sv})", &iter);
358         while (g_variant_iter_loop(iter, "{sv}", &key, &variant)) {
359                 if (g_strcmp0(key, "Scanning") == 0) {
360                         scanning = g_variant_get_boolean(variant);
361                         if (scanning == TRUE)
362                                 netconfig_wifi_set_scanning(TRUE);
363
364                         g_variant_unref(variant);
365                         g_free(key);
366                         break;
367                 }
368         }
369
370         g_variant_iter_free(iter);
371
372         return;
373 }
374
375 static void _supplicant_bss_added(GDBusConnection *conn,
376                 const gchar *name, const gchar *path, const gchar *interface,
377                 const gchar *sig, GVariant *param, gpointer user_data)
378 {
379         DBG("BSS added handling!");
380         if (wifi_ssid_scan_get_state() == TRUE)
381                 wifi_ssid_scan_add_bss(param);
382         else
383                 wifi_state_set_bss_found(TRUE);
384
385         return;
386 }
387
388 static void _supplicant_scan_done(GDBusConnection *conn,
389                 const gchar *name, const gchar *path, const gchar *interface,
390                 const gchar *sig, GVariant *param, gpointer user_data)
391 {
392         DBG("Scan Done handling!");
393         netconfig_wifi_set_scanning(FALSE);
394
395         if (netconfig_wifi_is_wps_enabled() == TRUE) {
396                 netconfig_wifi_wps_signal_scandone();
397                 if (wifi_state_get_technology_state() < NETCONFIG_WIFI_TECH_POWERED)
398                         return;
399         }
400
401         if (netconfig_wifi_get_bgscan_state() != TRUE) {
402                 if (wifi_ssid_scan_get_state() == TRUE)
403                         wifi_ssid_scan_emit_scan_completed();
404                 else
405                         wifi_ssid_scan(NULL);
406         } else {
407                 if (wifi_state_get_technology_state() >=
408                                 NETCONFIG_WIFI_TECH_POWERED)
409                         netconfig_wifi_bgscan_start(FALSE);
410
411                 wifi_start_timer_network_notification();
412         }
413
414         return;
415 }
416
417 static void _supplicant_driver_hanged(GDBusConnection *conn,
418                 const gchar *name, const gchar *path, const gchar *interface,
419                 const gchar *sig, GVariant *param, gpointer user_data)
420 {
421         DBG("Driver Hanged handling!");
422         ERR("Critical. Wi-Fi firmware crashed");
423
424         wifi_power_recover_firmware();
425
426         return;
427 }
428
429 static void _supplicant_session_overlapped(GDBusConnection *conn,
430                 const gchar *name, const gchar *path, const gchar *interface,
431                 const gchar *sig, GVariant *param, gpointer user_data)
432 {
433         DBG("Driver session overlapped handling!");
434         ERR("WPS PBC SESSION OVERLAPPED");
435 #if defined TIZEN_WEARABLE
436         wc_launch_syspopup(WC_POPUP_TYPE_SESSION_OVERLAPPED);
437 #else
438         netconfig_send_message_to_net_popup("WPS Error",
439                                         "wps session overlapped", "popup", NULL);
440 #endif
441 }
442
443 static void _supplicant_tdls_connected(GDBusConnection *conn,
444                 const gchar *name, const gchar *path, const gchar *interface,
445                 const gchar *sig, GVariant *param, gpointer user_data)
446 {
447         ERR("Received TDLS Connected Signal");
448         netconfig_wifi_tlds_connected_event(param);
449
450         return;
451 }
452
453 static void _supplicant_tdls_disconnected(GDBusConnection *conn,
454                 const gchar *name, const gchar *path, const gchar *interface,
455                 const gchar *sig, GVariant *param, gpointer user_data)
456 {
457         ERR("Received TDLS Disconnected Signal");
458         netconfig_wifi_tlds_disconnected_event(param);
459
460         return;
461 }
462
463 static void _supplicant_tdls_peer_found(GDBusConnection *conn,
464                 const gchar *name, const gchar *path, const gchar *interface,
465                 const gchar *sig, GVariant *param, gpointer user_data)
466 {
467         ERR("Received TDLS Peer Found Signal");
468         return;
469 }
470
471 static supplicant_signal_cb supplicant_cbs[SIG_MAX] = {
472                 _supplicant_interface_removed,
473                 _supplicant_properties_changed,
474                 _supplicant_bss_added,
475                 _supplicant_scan_done,
476                 _supplicant_driver_hanged,
477                 _supplicant_session_overlapped,
478                 _supplicant_tdls_connected,
479                 _supplicant_tdls_disconnected,
480                 _supplicant_tdls_peer_found
481 };
482
483 #if defined TIZEN_DEBUG_DISABLE
484 static void __netconfig_dumpservice_handler(GDBusConnection *conn,
485                 const gchar *name, const gchar *path, const gchar *interface,
486                 const gchar *sig, GVariant *param, gpointer user_data)
487 {
488         int mode;
489         gchar *signal_path = NULL;
490
491         if (param == NULL)
492                 return;
493
494         g_variant_get(param, "(io)", &mode, &signal_path);
495         DBG("Path: %s and mode: %d", signal_path, mode);
496         netconfig_dump_log(path);
497         if (signal_path)
498                 g_free(signal_path);
499
500         return;
501 }
502 #endif
503
504 void register_gdbus_signal(void)
505 {
506         GDBusConnection *connection = NULL;
507         const char *interface = NULL;
508         SuppSigArrayIndex sig;
509         connection = netdbus_get_connection();
510
511         if (connection == NULL) {
512                 ERR("Failed to get GDbus Connection");
513                 return;
514         }
515
516         /* listening to messages from all objects as no path is specified */
517         /* see signals from the given interface */
518         conn_subscription_ids[0] = g_dbus_connection_signal_subscribe(
519                         connection,
520                         CONNMAN_SERVICE,
521                         CONNMAN_TECHNOLOGY_INTERFACE,
522                         NULL,
523                         NULL,
524                         NULL,
525                         G_DBUS_SIGNAL_FLAGS_NONE,
526                         _technology_signal_cb,
527                         NULL,
528                         NULL);
529
530         conn_subscription_ids[1] = g_dbus_connection_signal_subscribe(
531                         connection,
532                         CONNMAN_SERVICE,
533                         CONNMAN_SERVICE_INTERFACE,
534                         CONNMAN_SIGNAL_PROPERTY_CHANGED,
535                         NULL,
536                         NULL,
537                         G_DBUS_SIGNAL_FLAGS_NONE,
538                         _service_signal_cb,
539                         NULL,
540                         NULL);
541
542         conn_subscription_ids[2] = g_dbus_connection_signal_subscribe(
543                         connection,
544                         DBUS_SERVICE_DBUS,
545                         DBUS_INTERFACE_DBUS,
546                         CONNMAN_SIGNAL_NAME_CHANGED,
547                         NULL,
548                         CONNMAN_SERVICE,
549                         G_DBUS_SIGNAL_FLAGS_NONE,
550                         _dbus_name_changed_cb,
551                         NULL,
552                         NULL);
553
554         INFO("Successfully register connman DBus signal filters");
555
556         for (sig = SIG_INTERFACE_REMOVED; sig < SIG_MAX; sig++) {
557                 /*
558                  * For SIG_INTERFACE_REMOVED INTERFACE_ADDED
559                  */
560                 interface = (sig == SIG_INTERFACE_REMOVED) ?
561                                 SUPPLICANT_INTERFACE : SUPPLICANT_IFACE_INTERFACE;
562
563                 supp_subscription_ids[sig] = g_dbus_connection_signal_subscribe(
564                                 connection,
565                                 SUPPLICANT_SERVICE,
566                                 interface,
567                                 supplicant_signals[sig],
568                                 NULL,
569                                 NULL,
570                                 G_DBUS_SIGNAL_FLAGS_NONE,
571                                 supplicant_cbs[sig],
572                                 NULL,
573                                 NULL);
574         }
575
576         INFO("Successfully register Supplicant DBus signal filters");
577
578 #if defined TIZEN_DEBUG_DISABLE
579         dumpservice_subscription_id = g_dbus_connection_signal_subscribe(
580                         connection,
581                         /*
582                          * Sender => For testing purpose made NULL
583                          *WPA_SUPPLICANT,
584                          */
585                         NULL,
586                         DUMP_SERVICE_INTERFACE,
587                         DUMP_SIGNAL,
588                         NULL,
589                         NULL,
590                         G_DBUS_SIGNAL_FLAGS_NONE,
591                         __netconfig_dumpservice_handler,
592                         NULL,
593                         NULL);
594
595         INFO("Successfully register Dumpservice DBus signal filter");
596 #endif
597         /* In case ConnMan precedes this signal register,
598          * net-config should update the default connected profile.
599          */
600         netconfig_update_default();
601 }
602
603 void deregister_gdbus_signal(void)
604 {
605         GDBusConnection *connection = NULL;
606         int signal;
607         SuppSigArrayIndex sig;
608         connection = netdbus_get_connection();
609         if (!connection) {
610                 ERR("Already de-registered. Nothing to be done");
611                 return;
612         }
613
614         for (signal = 0; signal < TOTAL_CONN_SIGNALS; signal++) {
615                 if (conn_subscription_ids[signal]) {
616                         g_dbus_connection_signal_unsubscribe(connection,
617                                                 conn_subscription_ids[signal]);
618                 }
619         }
620
621         for (sig = SIG_INTERFACE_REMOVED; sig < SIG_MAX; sig++) {
622                 if (supp_subscription_ids[sig]) {
623                         g_dbus_connection_signal_unsubscribe(connection,
624                                                 supp_subscription_ids[sig]);
625                 }
626         }
627
628         g_dbus_connection_signal_unsubscribe(connection,
629                         dumpservice_subscription_id);
630 }