Merge "[net-config] Use gdbus wrapper function to send signal." into tizen
[platform/core/connectivity/net-config.git] / src / wifi-power.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 <errno.h>
21 #include <vconf.h>
22 #include <vconf-keys.h>
23 #include <ITapiSim.h>
24 #include <TapiUtility.h>
25 #include <stdio.h>
26 #include <tzplatform_config.h>
27
28 #if defined TIZEN_P2P_ENABLE && !defined WLAN_CONCURRENT_MODE
29 #include <wifi-direct.h>
30 #endif
31
32 #include "log.h"
33 #include "util.h"
34 #include "netdbus.h"
35 #include "neterror.h"
36 #include "wifi-wps.h"
37 #include "wifi-power.h"
38 #include "wifi-state.h"
39 #include "wifi-tel-intf.h"
40 #include "netsupplicant.h"
41 #include "network-state.h"
42 #include "network-dpm.h"
43 #include "wifi-firmware.h"
44 #include "wifi-background-scan.h"
45
46
47 #define WLAN_SUPPLICANT_SCRIPT          "/usr/sbin/wpa_supp.sh"
48 #define P2P_SUPPLICANT_SCRIPT           "/usr/sbin/p2p_supp.sh"
49
50 #if defined TIZEN_WEARABLE
51 #include <weconn.h>
52 static weconn_h weconn_handle = NULL;
53 #endif
54
55 #define VCONF_WIFI_OFF_STATE_BY_AIRPLANE        "file/private/wifi/wifi_off_by_airplane"
56 #define VCONF_WIFI_OFF_STATE_BY_RESTRICTED      "file/private/wifi/wifi_off_by_restricted"
57 #define VCONF_WIFI_OFF_STATE_BY_EMERGENCY       "file/private/wifi/wifi_off_by_emergency"
58 #if defined TIZEN_WEARABLE
59 #define VCONF_WIFI_WEARABLE_WIFI_USE                    "db/private/wifi/wearable_wifi_use"
60 #endif
61 #if !defined TIZEN_WEARABLE
62 #define VCONFKEY_SETAPPL_NETWORK_PERMIT_WITH_LCD_OFF_LIMIT      "db/setting/network_with_lcd_off_limit"
63 #endif
64
65 #define WLAN_MAC_INFO               tzplatform_mkpath(TZ_SYS_ETC, "/.mac.info")
66 #define WLAN_MAC_ADDR_MAX           20
67 #define VCONF_WIFI_BSSID_ADDRESS        "db/wifi/bssid_address"
68
69 #if defined TIZEN_TV
70 #define ETH_MAC_ADDR_SIZE 6
71 #define VCONF_ETH_MAC_ADDRESS  "db/dnet/mac_address"
72 #define NET_EXEC_PATH "/sbin/ifconfig"
73 #define OS_RANDOM_FILE "/dev/urandom"
74 #endif
75
76 static gboolean connman_wifi_technology_state = FALSE;
77 static gboolean wifi_firmware_recovery_mode = FALSE;
78 static int airplane_mode = 0;
79
80 #if defined TIZEN_WEARABLE
81 static int psmode_wifi_use = 1;
82 #endif
83
84 static gboolean __is_wifi_restricted(void)
85 {
86 #if defined TIZEN_WEARABLE
87         return FALSE;
88 #endif
89         int restricted_mode = 0;
90
91         vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE, &restricted_mode);
92         if (restricted_mode != 0) {
93                 DBG("network restricted mode[%d]", restricted_mode);
94                 return TRUE;
95         }
96
97         return FALSE;
98 }
99
100 static void __technology_reply(GObject *source_object, GAsyncResult *res, gpointer user_data)
101 {
102         GVariant *reply;
103         GDBusConnection *conn = NULL;
104         GError *error = NULL;
105
106         conn = G_DBUS_CONNECTION(source_object);
107         reply = g_dbus_connection_call_finish(conn, res, &error);
108
109         if (reply == NULL) {
110                 if (error != NULL) {
111                         if (g_strcmp0(error->message, CONNMAN_ERROR_INTERFACE ".AlreadyEnabled") == 0)
112                                 wifi_state_update_power_state(TRUE);
113                         else if (g_strcmp0(error->message, CONNMAN_ERROR_INTERFACE ".AlreadyDisabled") == 0)
114                                 wifi_state_update_power_state(FALSE);
115                         else
116                                 ERR("Fail to request status [%d: %s]", error->code, error->message);
117                         g_error_free(error);
118                 } else {
119                         ERR("Fail torequest status");
120                 }
121         } else {
122                 DBG("Successfully requested");
123         }
124
125         g_variant_unref(reply);
126         netconfig_gdbus_pending_call_unref();
127 }
128
129 static int __execute_supplicant(gboolean enable)
130 {
131         int rv = 0;
132         const char *path = WLAN_SUPPLICANT_SCRIPT;
133         char *const args_enable[] = { "/usr/sbin/wpa_supp.sh", "start", NULL };
134         char *const args_disable[] = { "/usr/sbin/wpa_supp.sh", "stop", NULL };
135         char *const envs[] = { NULL };
136         static gboolean enabled = FALSE;
137
138         if (enabled == enable)
139                 return -EALREADY;
140
141         if (enable == TRUE)
142                 rv = netconfig_execute_file(path, args_enable, envs);
143         else
144                 rv = netconfig_execute_file(path, args_disable, envs);
145         if (rv < 0)
146                 return -EIO;
147
148         DBG("wpa_supplicant %s", enable == TRUE ? "started" : "stopped");
149
150         enabled = enable;
151
152         return 0;
153 }
154
155 #if defined TIZEN_P2P_ENABLE && defined WLAN_CONCURRENT_MODE
156 static int __netconfig_p2p_supplicant(gboolean enable)
157 {
158         int rv = 0;
159         const char *path = P2P_SUPPLICANT_SCRIPT;
160         char *const args_enable[] = { P2P_SUPPLICANT_SCRIPT, "start", NULL };
161         char *const args_disable[] = { P2P_SUPPLICANT_SCRIPT, "stop", NULL };
162         char *const envs[] = { NULL };
163
164         if (enable == TRUE)
165                 rv = netconfig_execute_file(path, args_enable, envs);
166         else
167                 rv = netconfig_execute_file(path, args_disable, envs);
168         if (rv < 0)
169                 return -EIO;
170
171         DBG("p2p_supplicant %s", enable == TRUE ? "started" : "stopped");
172
173         return 0;
174 }
175 #endif
176
177 void netconfig_wifi_recover_firmware(void)
178 {
179         wifi_firmware_recovery_mode = TRUE;
180
181         netconfig_wifi_bgscan_stop();
182
183         wifi_power_off();
184 }
185
186 #if defined TIZEN_P2P_ENABLE && !defined WLAN_CONCURRENT_MODE
187 static void __netconfig_wifi_direct_state_cb(int error_code, wifi_direct_device_state_e device_state, void *user_data)
188 {
189         int err;
190
191         wifi_direct_unset_device_state_changed_cb();
192         wifi_direct_deinitialize();
193
194         if (device_state == WIFI_DIRECT_DEVICE_STATE_DEACTIVATED) {
195                 err = wifi_power_on();
196                 if (err < 0) {
197                         if (err == -EALREADY)
198                                 wifi_state_update_power_state(TRUE);
199                         else
200                                 wifi_state_emit_power_failed();
201                 }
202         }
203 }
204
205 static gboolean __netconfig_wifi_direct_power_off(void)
206 {
207         DBG("Wi-Fi direct is turning off");
208
209         if (wifi_direct_initialize() < 0)
210                 return FALSE;
211
212         if (wifi_direct_set_device_state_changed_cb(__netconfig_wifi_direct_state_cb, NULL) < 0)
213                 return FALSE;
214
215         if (wifi_direct_deactivate() < 0)
216                 return FALSE;
217
218         return TRUE;
219 }
220 #endif
221
222 static int _load_driver_and_supplicant(void)
223 {
224         int err = 0;
225         wifi_tech_state_e tech_state;
226
227         tech_state = wifi_state_get_technology_state();
228         if (tech_state > NETCONFIG_WIFI_TECH_OFF)
229                 return -EALREADY;
230
231         err = __execute_supplicant(TRUE);
232         if (err < 0 && err != -EALREADY)
233                 return err;
234
235         err = netconfig_wifi_firmware(NETCONFIG_WIFI_STA, TRUE);
236         if (err < 0 && err != -EALREADY) {
237                 __execute_supplicant(FALSE);
238                 return err;
239         }
240
241         wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_WPS_ONLY);
242
243         return 0;
244 }
245
246 static int _remove_driver_and_supplicant(void)
247 {
248         int err = 0;
249
250         if (wifi_firmware_recovery_mode != TRUE &&
251                                         netconfig_wifi_is_wps_enabled() == TRUE) {
252                 DBG("Wi-Fi WPS mode");
253                 return 0;
254         }
255
256         err = netconfig_wifi_firmware(NETCONFIG_WIFI_STA, FALSE);
257         if (err < 0 && err != -EALREADY)
258                 return err;
259
260         err = __execute_supplicant(FALSE);
261         if (err < 0 && err != -EALREADY)
262                 return err;
263
264         wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_OFF);
265
266         if (wifi_firmware_recovery_mode == TRUE) {
267                 if (wifi_power_on() < 0)
268                         ERR("Failed to recover Wi-Fi firmware");
269
270                 wifi_firmware_recovery_mode = FALSE;
271         }
272
273         return 0;
274 }
275
276 static int _set_connman_technology_power(gboolean enable)
277 {
278         gboolean reply = FALSE;
279         GVariant *param0 = NULL;
280         GVariant *params = NULL;
281         char key[] = "Powered";
282         gboolean value_enable = TRUE;
283         gboolean value_disable = FALSE;
284
285         if (connman_wifi_technology_state == enable)
286                 return -EALREADY;
287
288         if (enable == TRUE)
289                 param0 = g_variant_new_boolean(value_enable);
290         else
291                 param0 = g_variant_new_boolean(value_disable);
292
293         params = g_variant_new("(sv)", key, param0);
294
295         reply = netconfig_invoke_dbus_method_nonblock(CONNMAN_SERVICE,
296                         CONNMAN_WIFI_TECHNOLOGY_PREFIX, CONNMAN_TECHNOLOGY_INTERFACE,
297                         "SetProperty", params, __technology_reply);
298
299         if (reply != TRUE) {
300                 ERR("Fail to set technology %s", enable == TRUE ? "enable" : "disable");
301                 return -ESRCH;
302         }
303
304         /* If Wi-Fi powered off,
305          * Do not remove Wi-Fi driver until ConnMan technology state updated
306          */
307         if (enable == TRUE)
308                 connman_wifi_technology_state = enable;
309
310         /* To be keep safe, early disable Wi-Fi tech state */
311         if (enable != TRUE)
312                 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_WPS_ONLY);
313
314         return 0;
315 }
316
317 static void __netconfig_set_wifi_bssid(void)
318 {
319         int rv = 0;
320         char bssid[WLAN_MAC_ADDR_MAX];
321
322         FILE *fp = fopen(WLAN_MAC_INFO, "r");
323
324         if (fp == NULL) {
325                 ERR("Fail to open file");
326                 return;
327         }
328
329         fseek(fp, 0L, SEEK_SET);
330         rv = fscanf(fp, "%17s", bssid);
331
332         if (rv < 0)
333                 ERR("Fail to read bssid");
334
335         netconfig_set_vconf_str(VCONF_WIFI_BSSID_ADDRESS, bssid);
336
337         fclose(fp);
338 }
339
340 int netconfig_wifi_driver_and_supplicant(gboolean enable)
341 {
342         /* There are 3 thumb rules for Wi-Fi power management
343          *   1. Do not make exposed API to control wpa_supplicant and driver directly.
344          *      It probably breaks ConnMan technology operation.
345          *
346          *   2. Do not remove driver and wpa_supplicant if ConnMan already enabled.
347          *      It breaks ConnMan technology operation.
348          *
349          *   3. Final the best rule: make it as simple as possible.
350          *      Simple code enables easy maintenance and reduces logical errors.
351          */
352         if (enable == TRUE)
353                 return _load_driver_and_supplicant();
354         else {
355                 if (connman_wifi_technology_state == TRUE)
356                         return -ENOSYS;
357
358                 return _load_driver_and_supplicant();
359         }
360 }
361
362 void netconfig_wifi_disable_technology_state_by_only_connman_signal(void)
363 {
364         /* Important: it's only done by ConnMan technology signal update */
365         connman_wifi_technology_state = FALSE;
366 }
367
368 int netconfig_wifi_on(void)
369 {
370         int err = 0;
371         wifi_tech_state_e wifi_tech_state;
372
373         wifi_tech_state = wifi_state_get_technology_state();
374         if (wifi_tech_state >= NETCONFIG_WIFI_TECH_POWERED)
375                 return -EALREADY;
376
377         if (__is_wifi_restricted() == TRUE)
378                 return -EPERM;
379
380         if (netconfig_is_wifi_tethering_on() == TRUE) {
381                 /* TODO: Wi-Fi tethering turns off here */
382                 /* return TRUE; */
383                 ERR("Failed to turn tethering off");
384                 return -EBUSY;
385         }
386
387 #if defined TIZEN_P2P_ENABLE && !defined WLAN_CONCURRENT_MODE
388         if (netconfig_is_wifi_direct_on() == TRUE) {
389                 if (__netconfig_wifi_direct_power_off() == TRUE)
390                         return -EINPROGRESS;
391                 else {
392                         ERR("Failed to turn Wi-Fi direct off");
393                         return -EBUSY;
394                 }
395         }
396 #endif
397
398         err = wifi_power_driver_and_supplicant(TRUE);
399         if (err < 0 && err != -EALREADY)
400                 return err;
401
402         err = _set_connman_technology_power(TRUE);
403
404         __netconfig_set_wifi_bssid();
405
406         return err;
407 }
408
409 int netconfig_wifi_off(void)
410 {
411         int err;
412
413 #if defined TIZEN_P2P_ENABLE && defined WLAN_CONCURRENT_MODE
414         __netconfig_p2p_supplicant(FALSE);
415 #endif
416
417         err = _set_connman_technology_power(FALSE);
418         if (err == -EALREADY)
419                 wifi_state_update_power_state(FALSE);
420
421         return 0;
422 }
423
424 #if defined TIZEN_WEARABLE
425 int netconfig_wifi_on_wearable(gboolean device_picker_test)
426 {
427         int err = 0;
428         int wifi_use;
429         int ps_mode;
430         enum netconfig_wifi_tech_state wifi_tech_state;
431         weconn_service_state_e weconn_state;
432
433         wifi_tech_state = wifi_state_get_technology_state();
434         if (wifi_tech_state >= NETCONFIG_WIFI_TECH_POWERED)
435                 return -EALREADY;
436
437         err = weconn_get_service_state(weconn_handle, W_SERVICE_TYPE_BT,
438                         &weconn_state);
439         if (err == 0 && weconn_state == W_SERVICE_STATE_CONNECTED) {
440                 WARN("Not permitted Wi-Fi on");
441                 return -EPERM;
442         }
443
444         if (vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use) < 0) {
445                 ERR("Fail to get VCONF_WIFI_WEARABLE_WIFI_USE");
446                 return -EIO;
447         }
448
449         if (wifi_use > 0) {
450                 if (vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &ps_mode) < 0) {
451                         ERR("Fail to get VCONFKEY_SETAPPL_PSMODE");
452                         return -EIO;
453                 }
454
455                 if (ps_mode > SETTING_PSMODE_NORMAL) {
456                         WARN("ps mode is on(%d), Not turn on Wi-Fi", ps_mode);
457                         return -EPERM;
458                 }
459         } else {
460                 WARN("Not permitted Wi-Fi on");
461                 return -EPERM;
462         }
463
464         err = wifi_power_driver_and_supplicant(TRUE);
465         if (err < 0 && err != -EALREADY)
466                 return err;
467
468         err = _set_connman_technology_power(TRUE);
469
470         if (device_picker_test == TRUE)
471                 netconfig_wifi_enable_device_picker_test();
472
473         return err;
474 }
475
476 static void __weconn_service_state_changed_cb(weconn_service_state_e state, void *user_data)
477 {
478         if (state == W_SERVICE_STATE_CONNECTED) {
479                 DBG("SAP is connected");
480                 if (wifi_state > VCONFKEY_WIFI_OFF)
481                         wifi_power_off();
482         } else if (state == W_SERVICE_STATE_DISCONNECTED) {
483                 DBG("SAP is disconnected");
484                 wifi_power_on_wearable(FALSE);
485         }
486 }
487
488 static int _weconn_set_state_changed_cb(int service_type, void *user_data)
489 {
490         int ret;
491
492         if (weconn_handle) {
493                 weconn_destroy(weconn_handle);
494                 weconn_handle = NULL;
495         }
496
497         ret = weconn_create(&weconn_handle);
498         if (ret < 0) {
499                 ERR("Failed weconn_create(%d)", ret);
500                 return -1;
501         }
502
503         ret = weconn_set_service_state_change_cb(weconn_handle, __weconn_service_state_changed_cb, service_type, user_data);
504         if (ret < 0) {
505                 ERR("Failed weconn_set_service_state_change_cb(%d)", ret);
506                 return -1;
507         }
508
509         return 0;
510 }
511
512 static void __wearable_wifi_use_changed_cb(keynode_t* node, void* user_data)
513 {
514         int wifi_state;
515         int wifi_use = 1;
516         gboolean wifi_restrict = FALSE;
517
518         if (vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state) < 0) {
519                 ERR("Fail to get VCONFKEY_WIFI_STATE");
520                 return;
521         }
522
523         if (node != NULL)
524                 wifi_use = vconf_keynode_get_int(node);
525         else
526                 vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use);
527
528         if (wifi_use > 0) {
529                 DBG("wifi use on");
530                 if (wifi_state > VCONFKEY_WIFI_OFF) {
531                         WARN("Wi-Fi is already turned on");
532                         return;
533                 }
534
535                 wifi_restrict = netconfig_is_wifi_allowed();
536                 if (wifi_restrict == FALSE) {
537                         DBG("launch wifi restrict popup");
538                         netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 0);
539                         wc_launch_syspopup(WC_POPUP_TYPE_WIFI_RESTRICT);
540                 } else {
541                         wifi_power_on_wearable(TRUE);
542                 }
543         } else {
544                 ERR("## wifi use [OFF]");
545                 if (wifi_state == VCONFKEY_WIFI_OFF) {
546                         WARN("Wi-Fi is already turned off");
547                         return;
548                 }
549
550                 wifi_power_off();
551         }
552 }
553
554 #if defined TIZEN_TELEPHONY_ENABLE
555 static void __netconfig_wifi_wearable_airplane_mode(keynode_t *node,
556                 void *user_data)
557 {
558         int wifi_use = 0, airplane_state = 0;
559         int wifi_use_off_by_airplane = 0;
560
561         vconf_get_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE,
562                         &wifi_use_off_by_airplane);
563
564         vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use);
565
566         if (node != NULL)
567                 airplane_state = vconf_keynode_get_bool(node);
568         else
569                 vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_state);
570
571         DBG("airplane mode %s (prev:%d)", airplane_state > 0 ? "ON" : "OFF", airplane_mode);
572         DBG("Wi-Fi use %d, Wi-Fi was off by flight mode %s", wifi_use,
573                         wifi_use_off_by_airplane ? "Yes" : "No");
574
575         if (airplane_mode == airplane_state)
576                 return ;
577
578         airplane_mode = airplane_state;
579
580         if (airplane_state > 0) {
581                 /* airplane mode on */
582                 if (wifi_use == 0)
583                         return;
584
585                 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 1);
586                 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 0);
587
588         } else {
589                 /* airplane mode off */
590                 if (!wifi_use_off_by_airplane)
591                         return;
592
593                 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
594                 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 1);
595         }
596 }
597 #endif
598 #else
599 #if defined TIZEN_TELEPHONY_ENABLE
600 static void __netconfig_wifi_airplane_mode(keynode_t *node, void *user_data)
601 {
602         int wifi_state = 0, airplane_state = 0;
603         int wifi_off_by_airplane = 0;
604
605         vconf_get_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, &wifi_off_by_airplane);
606
607         vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
608
609         if (node != NULL)
610                 airplane_state = vconf_keynode_get_bool(node);
611         else
612                 vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_state);
613
614         DBG("airplane mode %s (prev:%d)", airplane_state > 0 ? "ON" : "OFF", airplane_mode);
615         DBG("Wi-Fi state %d, Wi-Fi was off by flight mode %s", wifi_state,
616                         wifi_off_by_airplane ? "Yes" : "No");
617
618         if (airplane_mode == airplane_state)
619                 return ;
620
621         airplane_mode = airplane_state;
622
623         if (airplane_state > 0) {
624                 /* airplane mode on */
625                 if (wifi_state == VCONFKEY_WIFI_OFF)
626                         return;
627
628                 wifi_power_off();
629
630                 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 1);
631         } else {
632                 /* airplane mode off */
633                 if (!wifi_off_by_airplane)
634                         return;
635
636                 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
637
638                 if (wifi_state > VCONFKEY_WIFI_OFF)
639                         return;
640
641                 wifi_power_on();
642         }
643 }
644 #endif
645
646 static void __netconfig_wifi_restrict_mode(keynode_t *node, void *user_data)
647 {
648         int wifi_state = 0, restricted = 0;
649         int wifi_off_by_restricted = 0;
650
651         vconf_get_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, &wifi_off_by_restricted);
652
653         vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
654
655         if (node != NULL)
656                 restricted = vconf_keynode_get_bool(node);
657         else
658                 vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE, &restricted);
659
660         DBG("network restricted mode %s", restricted > 0 ? "ON" : "OFF");
661         DBG("Wi-Fi state %d, Wi-Fi was off by restricted mode %s", wifi_state,
662                         wifi_off_by_restricted ? "Yes" : "No");
663
664         if (restricted > 0) {
665                 /* network restricted on */
666                 if (wifi_state == VCONFKEY_WIFI_OFF)
667                         return;
668
669                 wifi_power_off();
670
671                 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, 1);
672         } else {
673                 /* network restricted off */
674                 if (!wifi_off_by_restricted)
675                         return;
676
677                 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, 0);
678
679                 if (wifi_state > VCONFKEY_WIFI_OFF)
680                         return;
681
682                 wifi_power_on();
683         }
684 }
685 #endif
686
687 static void __emergency_mode_changed_cb(keynode_t *node, void *user_data)
688 {
689         int wifi_state = 0, emergency = 0;
690         int wifi_off_by_emergency = 0;
691 #if !defined TIZEN_WEARABLE
692         int emergency_by_fmm = 0;
693 #endif
694 #if defined TIZEN_WEARABLE
695         int wifi_use = 1;
696 #endif
697
698         vconf_get_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, &wifi_off_by_emergency);
699         vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
700
701 #if !defined TIZEN_WEARABLE
702         vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_PERMIT_WITH_LCD_OFF_LIMIT, &emergency_by_fmm);
703         DBG("emergency mode by Find My Mobile (%d)", emergency_by_fmm);
704         if (emergency_by_fmm == 1)
705                 return;
706 #endif
707
708         if (node != NULL)
709                 emergency = vconf_keynode_get_int(node);
710         else
711                 vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &emergency);
712
713         DBG("emergency mode %s", emergency > SETTING_PSMODE_POWERFUL ? "ON" : "OFF");
714         DBG("Wi-Fi state %d, Wi-Fi was off by emergency mode %s", wifi_state, wifi_off_by_emergency ? "Yes" : "No");
715
716 #if defined TIZEN_WEARABLE
717         if (emergency == SETTING_PSMODE_WEARABLE) {
718                 /* basic power saving mode on */
719         } else if (emergency == SETTING_PSMODE_WEARABLE_ENHANCED) {
720                 /* enhanced power saving mode on */
721                 vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use);
722                 psmode_wifi_use = wifi_use;
723                 if (wifi_use != 0)
724                         netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 0);
725
726                 if (wifi_state == VCONFKEY_WIFI_OFF)
727                         return;
728
729                 wifi_power_off();
730                 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 1);
731         } else {
732                 /* power saving mode off */
733                 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, psmode_wifi_use);
734                 if (!wifi_off_by_emergency)
735                         return;
736
737                 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 0);
738                 if (wifi_state > VCONFKEY_WIFI_OFF)
739                         return;
740
741                 wifi_power_on_wearable(TRUE);
742         }
743 #else
744         if (emergency > SETTING_PSMODE_POWERFUL) {
745                 /* emergency mode on */
746                 if (wifi_state == VCONFKEY_WIFI_OFF)
747                         return;
748
749                 wifi_power_off();
750
751                 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 1);
752         } else {
753                 /* emergency mode off */
754                 if (!wifi_off_by_emergency)
755                         return;
756
757                 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 0);
758
759                 if (wifi_state > VCONFKEY_WIFI_OFF)
760                         return;
761
762                 wifi_power_on();
763         }
764 #endif
765
766 }
767
768 static void __pm_state_changed_cb(keynode_t* node, void* user_data)
769 {
770         int new_state = -1;
771         int wifi_state = 0;
772         static int prev_state = VCONFKEY_PM_STATE_NORMAL;
773
774         if (vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state) < 0) {
775                 ERR("Fail to get VCONFKEY_WIFI_STATE");
776                 return;
777         }
778
779         /* PM state
780          *      VCONFKEY_PM_STATE_NORMAL = 1,
781          *      VCONFKEY_PM_STATE_LCDDIM,
782          *      VCONFKEY_PM_STATE_LCDOFF,
783          *      VCONFKEY_PM_STATE_SLEEP
784          */
785         if (node != NULL)
786                 new_state = vconf_keynode_get_int(node);
787         else
788                 vconf_get_int(VCONFKEY_PM_STATE, &new_state);
789
790         DBG("wifi state: %d (0 off / 1 on / 2 connected)", wifi_state);
791         DBG("Old PM state: %d, current: %d", prev_state, new_state);
792
793         if ((new_state == VCONFKEY_PM_STATE_NORMAL) && (prev_state >= VCONFKEY_PM_STATE_LCDOFF)) {
794                 netconfig_wifi_bgscan_stop();
795                 netconfig_wifi_bgscan_start(TRUE);
796         }
797
798         prev_state = new_state;
799 }
800
801 #if defined TIZEN_TELEPHONY_ENABLE
802 static void _tapi_noti_sim_status_cb(TapiHandle *handle, const char *noti_id,
803                                                                                 void *data, void *user_data)
804 {
805         TelSimCardStatus_t *status = data;
806
807         if (*status == TAPI_SIM_STATUS_SIM_INIT_COMPLETED) {
808                 DBG("Turn Wi-Fi on automatically");
809 #if defined TIZEN_WEARABLE
810                 wifi_power_on_wearable(TRUE);
811 #else
812                 wifi_power_on();
813 #endif
814                 netconfig_tel_deinit();
815         }
816 }
817
818 static gboolean netconfig_tapi_check_sim_state(void)
819 {
820         int ret, card_changed;
821         TelSimCardStatus_t status = TAPI_SIM_STATUS_UNKNOWN;
822         TapiHandle *tapi_handle = NULL;
823
824         tapi_handle = (TapiHandle *)netconfig_tel_init();
825         if (tapi_handle == NULL) {
826                 ERR("Failed to tapi init");
827                 return FALSE;
828         }
829
830         ret = tel_get_sim_init_info(tapi_handle, &status, &card_changed);
831         if (ret != TAPI_API_SUCCESS) {
832                 ERR("tel_get_sim_init_info() Failed : [%d]", ret);
833                 netconfig_tel_deinit();
834                 return FALSE;
835         }
836
837         switch (status) {
838         case TAPI_SIM_STATUS_UNKNOWN:
839         case TAPI_SIM_STATUS_CARD_ERROR:
840         case TAPI_SIM_STATUS_CARD_NOT_PRESENT:
841         case TAPI_SIM_STATUS_CARD_BLOCKED:
842         case TAPI_SIM_STATUS_SIM_INIT_COMPLETED:
843                 break;
844         case TAPI_SIM_STATUS_SIM_PIN_REQUIRED:
845         case TAPI_SIM_STATUS_SIM_INITIALIZING:
846         case TAPI_SIM_STATUS_SIM_PUK_REQUIRED:
847         case TAPI_SIM_STATUS_SIM_LOCK_REQUIRED:
848         case TAPI_SIM_STATUS_SIM_NCK_REQUIRED:
849         case TAPI_SIM_STATUS_SIM_NSCK_REQUIRED:
850         case TAPI_SIM_STATUS_SIM_SPCK_REQUIRED:
851         case TAPI_SIM_STATUS_SIM_CCK_REQUIRED:
852                 tel_register_noti_event(tapi_handle, TAPI_NOTI_SIM_STATUS,
853                                 _tapi_noti_sim_status_cb, NULL);
854                 return FALSE;
855         default:
856                 ERR("not defined status(%d)", status);
857                 break;
858         }
859
860         netconfig_tel_deinit();
861
862         return TRUE;
863 }
864
865 static void __netconfig_telephony_ready_changed_cb(keynode_t * node, void *data)
866 {
867         int telephony_ready = 0;
868
869         if (node != NULL)
870                 telephony_ready = vconf_keynode_get_bool(node);
871         else
872                 vconf_get_bool(VCONFKEY_TELEPHONY_READY, &telephony_ready);
873
874         if (telephony_ready != 0) {
875                 if (netconfig_tapi_check_sim_state() == FALSE) {
876                         DBG("Sim is not initialized yet.");
877
878                         goto done;
879                 }
880         } else
881                 return;
882
883         DBG("Turn Wi-Fi on automatically");
884
885 #if defined TIZEN_WEARABLE
886         wifi_power_on_wearable(TRUE);
887 #else
888         wifi_power_on();
889 #endif
890
891 done:
892         vconf_ignore_key_changed(VCONFKEY_TELEPHONY_READY, __netconfig_telephony_ready_changed_cb);
893 }
894 #endif
895
896 int wifi_power_driver_and_supplicant(gboolean enable)
897 {
898         /* There are 3 thumb rules for Wi-Fi power management
899          *   1. Do not make exposed API to control wpa_supplicant and driver directly.
900          *      It probably breaks ConnMan technology operation.
901          *
902          *   2. Do not remove driver and wpa_supplicant if ConnMan already enabled.
903          *      It breaks ConnMan technology operation.
904          *
905          *   3. Final the best rule: make it as simple as possible.
906          *      Simple code enables easy maintenance and reduces logical errors.
907          */
908         if (enable == TRUE) {
909                 return _load_driver_and_supplicant();
910         } else {
911                 if (connman_wifi_technology_state == TRUE)
912                         return -ENOSYS;
913
914                 return _remove_driver_and_supplicant();
915         }
916 }
917
918 void wifi_power_disable_technology_state_by_only_connman_signal(void)
919 {
920         /* Important: it's only done by ConnMan technology signal update */
921         connman_wifi_technology_state = FALSE;
922 }
923
924 void wifi_power_recover_firmware(void)
925 {
926         wifi_firmware_recovery_mode = TRUE;
927
928         netconfig_wifi_bgscan_stop();
929
930         wifi_power_off();
931 }
932
933 int wifi_power_on(void)
934 {
935         int err = 0;
936         wifi_tech_state_e tech_state;
937
938         tech_state = wifi_state_get_technology_state();
939         if (tech_state >= NETCONFIG_WIFI_TECH_POWERED)
940                 return -EALREADY;
941
942         if (__is_wifi_restricted() == TRUE)
943                 return -EPERM;
944
945         if (netconfig_is_wifi_tethering_on() == TRUE) {
946                 /* TODO: Wi-Fi tethering turns off here */
947                 /* return TRUE; */
948                 ERR("Failed to turn tethering off");
949                 return -EBUSY;
950         }
951
952 #if defined TIZEN_P2P_ENABLE && !defined WLAN_CONCURRENT_MODE
953         if (netconfig_is_wifi_direct_on() == TRUE) {
954                 if (__netconfig_wifi_direct_power_off() == TRUE)
955                         return -EINPROGRESS;
956                 else {
957                         ERR("Failed to turn Wi-Fi direct off");
958                         return -EBUSY;
959                 }
960         }
961 #endif
962
963         err = wifi_power_driver_and_supplicant(TRUE);
964         if (err < 0 && err != -EALREADY)
965                 return err;
966
967         err = _set_connman_technology_power(TRUE);
968
969         return err;
970 }
971
972 int wifi_power_off(void)
973 {
974         int err;
975
976         err = _set_connman_technology_power(FALSE);
977         if (err == -EALREADY)
978                 wifi_state_update_power_state(FALSE);
979
980         return 0;
981 }
982
983 #if defined TIZEN_WEARABLE
984 int wifi_power_on_wearable(gboolean device_picker_test)
985 {
986         int err = 0;
987         int wifi_use = 1;
988         wifi_tech_state_e tech_state;
989         weconn_service_state_e weconn_state;
990
991         tech_state = wifi_state_get_technology_state();
992         if (tech_state >= NETCONFIG_WIFI_TECH_POWERED)
993                 return -EALREADY;
994
995         err = weconn_get_service_state(weconn_handle, W_SERVICE_TYPE_BT, &weconn_state);
996         if (err == 0 && weconn_state == W_SERVICE_STATE_CONNECTED) {
997                 WARN("Not permitted Wi-Fi on");
998                 return -EPERM;
999         }
1000
1001         if (vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use) < 0) {
1002                 ERR("Fail to get VCONF_WIFI_WEARABLE_WIFI_USE");
1003                 return -EIO;
1004         }
1005
1006         if (wifi_use == 0) {
1007                 WARN("VCONF_WIFI_WEARABLE_WIFI_USE is OFF");
1008                 return -EPERM;
1009         }
1010
1011         err = wifi_power_driver_and_supplicant(TRUE);
1012         if (err < 0 && err != -EALREADY)
1013                 return err;
1014
1015         err = _set_connman_technology_power(TRUE);
1016
1017         if (device_picker_test == TRUE)
1018                 netconfig_wifi_enable_device_picker_test();
1019
1020         return err;
1021 }
1022 #endif
1023
1024 void wifi_power_initialize(void)
1025 {
1026         int wifi_last_power_state = 0;
1027
1028         /* Initialize Airplane mode */
1029 #if defined TIZEN_TELEPHONY_ENABLE
1030         vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_mode);
1031 #endif
1032         DBG("Airplane[%s]", airplane_mode > 0 ? "ON" : "OFF");
1033
1034         /* Update the last Wi-Fi power state */
1035         vconf_get_int(VCONF_WIFI_LAST_POWER_STATE, &wifi_last_power_state);
1036         if (wifi_last_power_state > VCONFKEY_WIFI_OFF) {
1037 #if defined TIZEN_TELEPHONY_ENABLE
1038                 int telephony_ready = 0;
1039                 vconf_get_bool(VCONFKEY_TELEPHONY_READY, &telephony_ready);
1040                 if (telephony_ready == 0) {
1041                         DBG("Telephony API is not initialized yet");
1042                         vconf_notify_key_changed(VCONFKEY_TELEPHONY_READY,
1043                                         __netconfig_telephony_ready_changed_cb, NULL);
1044                 } else {
1045                         if (netconfig_tapi_check_sim_state() == FALSE)
1046                                 DBG("SIM is not initialized yet");
1047                 }
1048 #endif
1049                 DBG("Turn Wi-Fi on automatically");
1050 #if defined TIZEN_WEARABLE
1051                 wifi_power_on_wearable(TRUE);
1052 #else
1053                 wifi_power_on();
1054 #endif
1055         }
1056
1057 #if defined TIZEN_WEARABLE
1058         _weconn_set_state_changed_cb(W_SERVICE_TYPE_BT, NULL);
1059         vconf_notify_key_changed(VCONF_WIFI_WEARABLE_WIFI_USE, __wearable_wifi_use_changed_cb, NULL);
1060
1061 #if defined TIZEN_TELEPHONY_ENABLE
1062         vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
1063                         __netconfig_wifi_wearable_airplane_mode, NULL);
1064 #endif
1065 #else
1066         vconf_notify_key_changed(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE,
1067                         __netconfig_wifi_restrict_mode, NULL);
1068 #if defined TIZEN_TELEPHONY_ENABLE
1069         vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
1070                         __netconfig_wifi_airplane_mode, NULL);
1071 #endif
1072 #endif
1073
1074         vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE, __emergency_mode_changed_cb, NULL);
1075         vconf_notify_key_changed(VCONFKEY_PM_STATE, __pm_state_changed_cb, NULL);
1076 }
1077
1078 void wifi_power_deinitialize(void)
1079 {
1080 }
1081
1082 gboolean handle_load_driver(Wifi *wifi,
1083                 GDBusMethodInvocation *context, gboolean device_picker_test)
1084 {
1085         int err;
1086
1087         DBG("Wi-Fi power on requested");
1088
1089         g_return_val_if_fail(wifi != NULL, FALSE);
1090
1091         if (!netconfig_dpm_update_from_wifi()) {
1092                 DBG("DPM policy restricts Wi-Fi");
1093                 netconfig_error_permission_denied(context);
1094                 return TRUE;
1095         }
1096
1097 #if defined TIZEN_WEARABLE
1098         err = wifi_power_on_wearable(device_picker_test);
1099 #else
1100         err = wifi_power_on();
1101
1102         if (device_picker_test == TRUE)
1103                 netconfig_wifi_enable_device_picker_test();
1104 #endif
1105         if (err < 0) {
1106                 if (err == -EINPROGRESS)
1107                         netconfig_error_inprogress(context);
1108                 else if (err == -EALREADY)
1109                         netconfig_error_already_exists(context);
1110                 else if (err == -EPERM)
1111                         netconfig_error_permission_denied(context);
1112                 else
1113                         netconfig_error_wifi_driver_failed(context);
1114
1115                 return TRUE;
1116         }
1117
1118
1119         netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
1120         __netconfig_set_wifi_bssid();
1121
1122         wifi_complete_load_driver(wifi, context);
1123         return TRUE;
1124 }
1125
1126 gboolean handle_remove_driver(Wifi *wifi, GDBusMethodInvocation *context)
1127 {
1128         int err;
1129
1130         DBG("Wi-Fi power off requested");
1131
1132         g_return_val_if_fail(wifi != NULL, FALSE);
1133
1134         err = wifi_power_off();
1135         if (err < 0) {
1136                 if (err == -EINPROGRESS)
1137                         netconfig_error_inprogress(context);
1138                 else if (err == -EALREADY)
1139                         netconfig_error_already_exists(context);
1140                 else if (err == -EPERM)
1141                         netconfig_error_permission_denied(context);
1142                 else
1143                         netconfig_error_wifi_driver_failed(context);
1144                 return TRUE;
1145         }
1146
1147         netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
1148
1149         wifi_complete_remove_driver(wifi, context);
1150         return TRUE;
1151 }
1152
1153 gboolean handle_load_p2p_driver(Wifi *wifi, GDBusMethodInvocation *context)
1154 {
1155         ERR("Deprecated");
1156
1157         wifi_complete_load_p2p_driver(wifi, context);
1158         return TRUE;
1159 }
1160
1161 gboolean handle_remove_p2p_driver(Wifi *wifi, GDBusMethodInvocation *context)
1162 {
1163         ERR("Deprecated");
1164
1165         wifi_complete_remove_p2p_driver(wifi, context);
1166         return TRUE;
1167 }
1168
1169 #if defined TIZEN_TV
1170 static int __netconfig_get_random_mac(unsigned char *mac_buf, int mac_len)
1171 {
1172         DBG("Generate Random Mac address of ethernet");
1173         FILE *fp;
1174         int rc;
1175
1176         fp = fopen(OS_RANDOM_FILE, "rb");
1177
1178         if (fp == NULL) {
1179                 ERR("Could not open /dev/urandom");
1180                 return -1;
1181         }
1182         rc = fread(mac_buf, 1, mac_len, fp);
1183         if (fp)
1184                 fclose(fp);
1185
1186         return rc != mac_len ? -1 : 0;
1187 }
1188
1189 void __netconfig_set_ether_macaddr()
1190 {
1191
1192         DBG("Set wired Mac address ");
1193         char *mac_addr = NULL;
1194         int rv = -1;
1195
1196         mac_addr = vconf_get_str(VCONF_ETH_MAC_ADDRESS);
1197         if (mac_addr == NULL) {
1198                 DBG("vconf_get_str Failed\n");
1199                 return;
1200         }
1201         /* Checking Invalid MAC Address */
1202         if ((strlen(mac_addr) == 0)) {
1203                 ERR("Failed to get valid MAC Address from vconf");
1204                 /*Generate the Random Mac address*/
1205                 unsigned char rand_mac_add[ETH_MAC_ADDR_SIZE+1];
1206
1207                 if (__netconfig_get_random_mac(rand_mac_add, ETH_MAC_ADDR_SIZE == -1)) {
1208
1209                         ERR("Could not generate the Random Mac address");
1210                         g_free(mac_addr);
1211                         return;
1212                 }
1213
1214                 rand_mac_add[0] &= 0xFE; /*Clear multicase bit*/
1215                 rand_mac_add[0] |= 0x02; /*set local assignment bit*/
1216
1217                 /*Set the Mac address in Vconf*/
1218                 snprintf(mac_addr, WLAN_MAC_ADDR_MAX, "%x:%x:%x:%x:%x:%x",
1219                                 rand_mac_add[0], rand_mac_add[1],
1220                                 rand_mac_add[2], rand_mac_add[3],
1221                                 rand_mac_add[4], rand_mac_add[5]);
1222
1223                 netconfig_set_vconf_str(VCONF_ETH_MAC_ADDRESS, mac_addr);
1224         }
1225
1226         DBG("MAC Address of eth0 [%s]", mac_addr);
1227         const char *path = NET_EXEC_PATH;
1228         char *const args[] = { "/sbin/ifconfig", "eth0", "hw",
1229                 "ether", mac_addr, "up", NULL};
1230         char *const envs[] = { NULL };
1231         rv = netconfig_execute_file(path, args, envs);
1232
1233         if (rv < 0)
1234                 ERR("Unable to execute system command");
1235         g_free(mac_addr);
1236
1237 }
1238 #endif