b6b9bab938103b9f76989a11377a1cfc45738f92
[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 <stdlib.h>
27 #include <tzplatform_config.h>
28
29 #if defined TIZEN_P2P_ENABLE && !defined WLAN_CONCURRENT_MODE
30 #include <wifi-direct.h>
31 #endif
32
33 #include "log.h"
34 #include "util.h"
35 #include "netdbus.h"
36 #include "neterror.h"
37 #include "wifi-wps.h"
38 #include "wifi-power.h"
39 #include "wifi-state.h"
40 #include "wifi-tel-intf.h"
41 #include "netsupplicant.h"
42 #include "network-state.h"
43 #include "network-dpm.h"
44 #include "wifi-firmware.h"
45 #include "wifi-background-scan.h"
46
47
48 #define WLAN_SUPPLICANT_SCRIPT          "/usr/sbin/wpa_supp.sh"
49 #define P2P_SUPPLICANT_SCRIPT           "/usr/sbin/p2p_supp.sh"
50
51 #define VCONF_WIFI_OFF_STATE_BY_AIRPLANE        "file/private/wifi/wifi_off_by_airplane"
52 #define VCONF_WIFI_OFF_STATE_BY_RESTRICTED      "file/private/wifi/wifi_off_by_restricted"
53 #define VCONF_WIFI_OFF_STATE_BY_EMERGENCY       "file/private/wifi/wifi_off_by_emergency"
54 #if defined TIZEN_WEARABLE
55 #define VCONF_WIFI_WEARABLE_WIFI_USE                    "db/private/wifi/wearable_wifi_use"
56 #endif
57 #if !defined TIZEN_WEARABLE
58 #define VCONFKEY_SETAPPL_NETWORK_PERMIT_WITH_LCD_OFF_LIMIT      "db/setting/network_with_lcd_off_limit"
59 #endif
60
61 #define WLAN_MAC_INFO               tzplatform_mkpath(TZ_SYS_ETC, "/.mac.info")
62 #define WLAN_MAC_ADDRESS_FILEPATH   "/sys/class/net/wlan0/address"
63 #define WLAN_MAC_ADDR_MAX           20
64 #define VCONF_WIFI_BSSID_ADDRESS        "db/wifi/bssid_address"
65
66 #define ETH_MAC_ADDR_SIZE 6
67 #define VCONF_ETH_MAC_ADDRESS  "db/dnet/mac_address"
68 #define NET_EXEC_PATH "/sbin/ifconfig"
69 #define OS_RANDOM_FILE "/dev/urandom"
70
71 static gboolean connman_wifi_technology_state = FALSE;
72 static gboolean wifi_firmware_recovery_mode = FALSE;
73 static int airplane_mode = 0;
74
75 #if defined TIZEN_WEARABLE
76 static int psmode_wifi_use = 1;
77 #endif
78
79 static gboolean __is_wifi_restricted(void)
80 {
81 #if defined TIZEN_WEARABLE
82         return FALSE;
83 #endif
84         int restricted_mode = 0;
85
86         netconfig_vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE, &restricted_mode);
87         if (restricted_mode != 0) {
88                 DBG("network restricted mode[%d]", restricted_mode);
89                 return TRUE;
90         }
91
92         return FALSE;
93 }
94
95 static void __technology_reply(GObject *source_object, GAsyncResult *res, gpointer user_data)
96 {
97         GVariant *reply;
98         GDBusConnection *conn = NULL;
99         GError *error = NULL;
100
101         conn = G_DBUS_CONNECTION(source_object);
102         reply = g_dbus_connection_call_finish(conn, res, &error);
103
104         if (reply == NULL) {
105                 if (error != NULL) {
106                         if (g_strstr_len(error->message, strlen(error->message),
107                                         CONNMAN_ERROR_INTERFACE ".AlreadyEnabled") != NULL) {
108                                 wifi_state_update_power_state(TRUE);
109                         } else if (g_strstr_len(error->message, strlen(error->message),
110                                         CONNMAN_ERROR_INTERFACE ".AlreadyDisabled") != NULL) {
111                                 wifi_state_update_power_state(FALSE);
112                         } else {
113                                 ERR("Fail to request status [%d: %s]", error->code, error->message);
114                                 wifi_state_update_power_state(FALSE);
115                         }
116                         g_error_free(error);
117                 } else {
118                         ERR("Fail to request status");
119                         wifi_state_update_power_state(FALSE);
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 %s", WLAN_MAC_INFO);
326                 fp = fopen(WLAN_MAC_ADDRESS_FILEPATH, "r");
327                 if (fp == NULL) {
328                         ERR("Fail to open %s", WLAN_MAC_ADDRESS_FILEPATH);
329                         return;
330                 }
331         }
332
333         fseek(fp, 0L, SEEK_SET);
334         rv = fscanf(fp, "%17s", bssid);
335
336         if (rv < 0)
337                 ERR("Fail to read bssid");
338
339         netconfig_set_vconf_str(VCONF_WIFI_BSSID_ADDRESS, bssid);
340
341         fclose(fp);
342 }
343
344 void netconfig_wifi_disable_technology_state_by_only_connman_signal(void)
345 {
346         /* Important: it's only done by ConnMan technology signal update */
347         connman_wifi_technology_state = FALSE;
348 }
349
350 #if defined TIZEN_WEARABLE
351 int netconfig_wifi_on_wearable(gboolean device_picker_test)
352 {
353         int err = 0;
354         int wifi_use;
355         int ps_mode;
356
357         if (netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use) < 0) {
358                 ERR("Fail to get VCONF_WIFI_WEARABLE_WIFI_USE");
359                 return -EIO;
360         }
361
362         if (wifi_use > 0) {
363                 if (netconfig_vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &ps_mode) < 0) {
364                         ERR("Fail to get VCONFKEY_SETAPPL_PSMODE");
365                         return -EIO;
366                 }
367
368                 if (ps_mode > SETTING_PSMODE_NORMAL) {
369                         WARN("ps mode is on(%d), Not turn on Wi-Fi", ps_mode);
370                         return -EPERM;
371                 }
372         } else {
373                 WARN("Not permitted Wi-Fi on");
374                 return -EPERM;
375         }
376
377         err = wifi_power_driver_and_supplicant(TRUE);
378         if (err < 0 && err != -EALREADY)
379                 return err;
380
381         err = _set_connman_technology_power(TRUE);
382
383         if (device_picker_test == TRUE)
384                 netconfig_wifi_enable_device_picker_test();
385
386         return err;
387 }
388
389 static void __wearable_wifi_use_changed_cb(keynode_t* node, void* user_data)
390 {
391         int wifi_state;
392         int wifi_use = 1;
393
394         if (netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state) < 0) {
395                 ERR("Fail to get VCONFKEY_WIFI_STATE");
396                 return;
397         }
398
399         if (node != NULL)
400                 wifi_use = vconf_keynode_get_int(node);
401         else
402                 netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use);
403
404         if (wifi_use > 0) {
405                 DBG("wifi use on");
406                 if (wifi_state > VCONFKEY_WIFI_OFF) {
407                         WARN("Wi-Fi is already turned on");
408                         return;
409                 }
410                 wifi_power_on_wearable(TRUE);
411         } else {
412                 ERR("## wifi use [OFF]");
413                 if (wifi_state == VCONFKEY_WIFI_OFF) {
414                         WARN("Wi-Fi is already turned off");
415                         return;
416                 }
417
418                 wifi_power_off();
419         }
420 }
421 #else
422 static void __netconfig_wifi_restrict_mode(keynode_t *node, void *user_data)
423 {
424         int wifi_state = 0, restricted = 0;
425         int wifi_off_by_restricted = 0;
426
427         netconfig_vconf_get_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, &wifi_off_by_restricted);
428
429         netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
430
431         if (node != NULL)
432                 restricted = vconf_keynode_get_bool(node);
433         else
434                 netconfig_vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE, &restricted);
435
436         DBG("network restricted mode %s", restricted > 0 ? "ON" : "OFF");
437         DBG("Wi-Fi state %d, Wi-Fi was off by restricted mode %s", wifi_state,
438                         wifi_off_by_restricted ? "Yes" : "No");
439
440         if (restricted > 0) {
441                 /* network restricted on */
442                 if (wifi_state == VCONFKEY_WIFI_OFF)
443                         return;
444
445                 wifi_power_off();
446
447                 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, 1);
448         } else {
449                 /* network restricted off */
450                 if (!wifi_off_by_restricted)
451                         return;
452
453                 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, 0);
454
455                 if (wifi_state > VCONFKEY_WIFI_OFF)
456                         return;
457
458                 wifi_power_on();
459         }
460 }
461 #endif
462
463 static void __netconfig_wifi_airplane_mode(keynode_t *node, void *user_data)
464 {
465         int wifi_state = 0, airplane_state = 0;
466         int wifi_off_by_airplane = 0;
467
468         netconfig_vconf_get_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, &wifi_off_by_airplane);
469
470 #if defined TIZEN_WEARABLE
471         netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_state)
472 #else
473         netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
474 #endif
475
476         if (node != NULL)
477                 airplane_state = vconf_keynode_get_bool(node);
478         else
479                 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_state);
480
481         DBG("airplane mode %s (prev:%d)", airplane_state > 0 ? "ON" : "OFF", airplane_mode);
482         DBG("Wi-Fi state(or use) %d, Wi-Fi was off by flight mode %s", wifi_state,
483                         wifi_off_by_airplane ? "Yes" : "No");
484
485         if (airplane_mode == airplane_state)
486                 return ;
487
488         airplane_mode = airplane_state;
489
490         if (airplane_state > 0) {
491                 /* airplane mode on */
492                 if (wifi_state == VCONFKEY_WIFI_OFF)
493                         return;
494
495                 wifi_power_off();
496
497                 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 1);
498 #if defined TIZEN_WEARABLE
499                 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 0);
500 #endif
501         } else {
502                 /* airplane mode off */
503                 if (!wifi_off_by_airplane)
504                         return;
505
506                 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
507 #if defined TIZEN_WEARABLE
508                 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 1);
509 #else
510                 if (wifi_state > VCONFKEY_WIFI_OFF)
511                         return;
512
513                 wifi_power_on();
514 #endif
515         }
516 }
517
518 static void __emergency_mode_changed_cb(keynode_t *node, void *user_data)
519 {
520         int wifi_state = 0, emergency = 0;
521         int wifi_off_by_emergency = 0;
522 #if !defined TIZEN_WEARABLE
523         int emergency_by_fmm = 0;
524 #endif
525 #if defined TIZEN_WEARABLE
526         int wifi_use = 1;
527 #endif
528
529         netconfig_vconf_get_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, &wifi_off_by_emergency);
530         netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
531
532 #if !defined TIZEN_WEARABLE
533         netconfig_vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_PERMIT_WITH_LCD_OFF_LIMIT, &emergency_by_fmm);
534         DBG("emergency mode by Find My Mobile (%d)", emergency_by_fmm);
535         if (emergency_by_fmm == 1)
536                 return;
537 #endif
538
539         if (node != NULL)
540                 emergency = vconf_keynode_get_int(node);
541         else
542                 netconfig_vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &emergency);
543
544         DBG("emergency mode %s", emergency > SETTING_PSMODE_POWERFUL ? "ON" : "OFF");
545         DBG("Wi-Fi state %d, Wi-Fi was off by emergency mode %s", wifi_state, wifi_off_by_emergency ? "Yes" : "No");
546
547 #if defined TIZEN_WEARABLE
548         if (emergency == SETTING_PSMODE_WEARABLE) {
549                 /* basic power saving mode on */
550         } else if (emergency == SETTING_PSMODE_WEARABLE_ENHANCED) {
551                 /* enhanced power saving mode on */
552                 netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use);
553                 psmode_wifi_use = wifi_use;
554                 if (wifi_use != 0)
555                         netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, 0);
556
557                 if (wifi_state == VCONFKEY_WIFI_OFF)
558                         return;
559
560                 wifi_power_off();
561                 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 1);
562         } else {
563                 /* power saving mode off */
564                 netconfig_set_vconf_int(VCONF_WIFI_WEARABLE_WIFI_USE, psmode_wifi_use);
565                 if (!wifi_off_by_emergency)
566                         return;
567
568                 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 0);
569                 if (wifi_state > VCONFKEY_WIFI_OFF)
570                         return;
571
572                 wifi_power_on_wearable(TRUE);
573         }
574 #else
575         if (emergency > SETTING_PSMODE_POWERFUL) {
576                 /* emergency mode on */
577                 if (wifi_state == VCONFKEY_WIFI_OFF)
578                         return;
579
580                 wifi_power_off();
581
582                 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 1);
583         } else {
584                 /* emergency mode off */
585                 if (!wifi_off_by_emergency)
586                         return;
587
588                 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 0);
589
590                 if (wifi_state > VCONFKEY_WIFI_OFF)
591                         return;
592
593                 wifi_power_on();
594         }
595 #endif
596
597 }
598
599 static void __pm_state_changed_cb(keynode_t* node, void* user_data)
600 {
601         int new_state = -1;
602         int wifi_state = 0;
603         static int prev_state = VCONFKEY_PM_STATE_NORMAL;
604
605         if (netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state) < 0) {
606                 ERR("Fail to get VCONFKEY_WIFI_STATE");
607                 return;
608         }
609
610         /* PM state
611          *      VCONFKEY_PM_STATE_NORMAL = 1,
612          *      VCONFKEY_PM_STATE_LCDDIM,
613          *      VCONFKEY_PM_STATE_LCDOFF,
614          *      VCONFKEY_PM_STATE_SLEEP
615          */
616         if (node != NULL)
617                 new_state = vconf_keynode_get_int(node);
618         else
619                 netconfig_vconf_get_int(VCONFKEY_PM_STATE, &new_state);
620
621         DBG("wifi state: %d (0 off / 1 on / 2 connected)", wifi_state);
622         DBG("Old PM state: %d, current: %d", prev_state, new_state);
623
624         if ((new_state == VCONFKEY_PM_STATE_NORMAL) && (prev_state >= VCONFKEY_PM_STATE_LCDOFF)) {
625                 netconfig_wifi_bgscan_stop();
626                 netconfig_wifi_bgscan_start(TRUE);
627         }
628
629         prev_state = new_state;
630 }
631
632 static void _tapi_noti_sim_status_cb(TapiHandle *handle, const char *noti_id,
633                                                                                 void *data, void *user_data)
634 {
635         TelSimCardStatus_t *status = data;
636
637         if (*status == TAPI_SIM_STATUS_SIM_INIT_COMPLETED) {
638                 DBG("Turn Wi-Fi on automatically");
639 #if defined TIZEN_WEARABLE
640                 wifi_power_on_wearable(TRUE);
641 #else
642                 wifi_power_on();
643 #endif
644                 netconfig_tel_deinit();
645         }
646 }
647
648 static gboolean netconfig_tapi_check_sim_state(void)
649 {
650         int ret, card_changed;
651         TelSimCardStatus_t status = TAPI_SIM_STATUS_UNKNOWN;
652         TapiHandle *tapi_handle = NULL;
653
654         tapi_handle = (TapiHandle *)netconfig_tel_init();
655         if (tapi_handle == NULL) {
656                 ERR("Failed to tapi init");
657                 return FALSE;
658         }
659
660         ret = tel_get_sim_init_info(tapi_handle, &status, &card_changed);
661         if (ret != TAPI_API_SUCCESS) {
662                 ERR("tel_get_sim_init_info() Failed : [%d]", ret);
663                 netconfig_tel_deinit();
664                 return FALSE;
665         }
666
667         switch (status) {
668         case TAPI_SIM_STATUS_UNKNOWN:
669         case TAPI_SIM_STATUS_CARD_ERROR:
670         case TAPI_SIM_STATUS_CARD_NOT_PRESENT:
671         case TAPI_SIM_STATUS_CARD_BLOCKED:
672         case TAPI_SIM_STATUS_SIM_INIT_COMPLETED:
673                 break;
674         case TAPI_SIM_STATUS_SIM_PIN_REQUIRED:
675         case TAPI_SIM_STATUS_SIM_INITIALIZING:
676         case TAPI_SIM_STATUS_SIM_PUK_REQUIRED:
677         case TAPI_SIM_STATUS_SIM_LOCK_REQUIRED:
678         case TAPI_SIM_STATUS_SIM_NCK_REQUIRED:
679         case TAPI_SIM_STATUS_SIM_NSCK_REQUIRED:
680         case TAPI_SIM_STATUS_SIM_SPCK_REQUIRED:
681         case TAPI_SIM_STATUS_SIM_CCK_REQUIRED:
682                 tel_register_noti_event(tapi_handle, TAPI_NOTI_SIM_STATUS,
683                                 _tapi_noti_sim_status_cb, NULL);
684                 return FALSE;
685         default:
686                 ERR("not defined status(%d)", status);
687                 break;
688         }
689
690         netconfig_tel_deinit();
691
692         return TRUE;
693 }
694
695 static void __netconfig_telephony_ready_changed_cb(keynode_t * node, void *data)
696 {
697         int telephony_ready = 0;
698
699         if (node != NULL)
700                 telephony_ready = vconf_keynode_get_bool(node);
701         else
702                 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_READY, &telephony_ready);
703
704         if (telephony_ready != 0) {
705                 if (netconfig_tapi_check_sim_state() == FALSE) {
706                         DBG("Sim is not initialized yet.");
707
708                         goto done;
709                 }
710         } else
711                 return;
712
713         DBG("Turn Wi-Fi on automatically");
714
715 #if defined TIZEN_WEARABLE
716         wifi_power_on_wearable(TRUE);
717 #else
718         wifi_power_on();
719 #endif
720
721 done:
722         vconf_ignore_key_changed(VCONFKEY_TELEPHONY_READY, __netconfig_telephony_ready_changed_cb);
723 }
724
725 int wifi_power_driver_and_supplicant(gboolean enable)
726 {
727         /* There are 3 thumb rules for Wi-Fi power management
728          *   1. Do not make exposed API to control wpa_supplicant and driver directly.
729          *      It probably breaks ConnMan technology operation.
730          *
731          *   2. Do not remove driver and wpa_supplicant if ConnMan already enabled.
732          *      It breaks ConnMan technology operation.
733          *
734          *   3. Final the best rule: make it as simple as possible.
735          *      Simple code enables easy maintenance and reduces logical errors.
736          */
737         if (enable == TRUE) {
738                 return _load_driver_and_supplicant();
739         } else {
740                 if (connman_wifi_technology_state == TRUE)
741                         return -ENOSYS;
742
743                 return _remove_driver_and_supplicant();
744         }
745 }
746
747 void wifi_power_disable_technology_state_by_only_connman_signal(void)
748 {
749         /* Important: it's only done by ConnMan technology signal update */
750         connman_wifi_technology_state = FALSE;
751 }
752
753 void wifi_power_recover_firmware(void)
754 {
755         wifi_firmware_recovery_mode = TRUE;
756
757         netconfig_wifi_bgscan_stop();
758
759         wifi_power_off();
760 }
761
762 int wifi_power_on(void)
763 {
764         int err = 0;
765         wifi_tech_state_e tech_state;
766
767         tech_state = wifi_state_get_technology_state();
768         if (tech_state >= NETCONFIG_WIFI_TECH_POWERED) {
769                 /* There can be a scenario where wifi is automatically *
770                  * activated by connman if wifi was powered in last boot. *
771                  * So we should update connman_wifi_technology_state variable *
772                  * if it is found that wifi_tech_state variable is *
773                  * NETCONFIG_WIFI_TECH_POWERED and connman_wifi_technology_state *
774                  * variable is FALSE. Earlier connman_wifi_technology_state *
775                  * variable was only updated when wifi was Powered on from *
776                  * net-config resulting in variable not getting updated. *
777                  * This caused wifi to not get deactivated after reboot if *
778                  * last power state was activated */
779                 ERR("Net-Config WiFi connman technology state %d",
780                                 connman_wifi_technology_state);
781                 if (connman_wifi_technology_state == FALSE)
782                         connman_wifi_technology_state = TRUE;
783                 return -EALREADY;
784         }
785
786         if (__is_wifi_restricted() == TRUE)
787                 return -EPERM;
788
789         if (netconfig_is_wifi_tethering_on() == TRUE) {
790                 /* TODO: Wi-Fi tethering turns off here */
791                 /* return TRUE; */
792                 ERR("Failed to turn tethering off");
793                 return -EBUSY;
794         }
795
796 #if defined TIZEN_P2P_ENABLE && !defined WLAN_CONCURRENT_MODE
797         if (netconfig_is_wifi_direct_on() == TRUE) {
798                 if (__netconfig_wifi_direct_power_off() == TRUE)
799                         return -EINPROGRESS;
800                 else {
801                         ERR("Failed to turn Wi-Fi direct off");
802                         return -EBUSY;
803                 }
804         }
805 #endif
806
807         err = wifi_power_driver_and_supplicant(TRUE);
808         if (err < 0 && err != -EALREADY)
809                 return err;
810
811         err = _set_connman_technology_power(TRUE);
812
813         return err;
814 }
815
816 int wifi_power_off(void)
817 {
818         int err;
819
820         err = _set_connman_technology_power(FALSE);
821         if (err == -EALREADY)
822                 wifi_state_update_power_state(FALSE);
823
824         return 0;
825 }
826
827 #if defined TIZEN_WEARABLE
828 int wifi_power_on_wearable(gboolean device_picker_test)
829 {
830         int err = 0;
831         int wifi_use = 1;
832         wifi_tech_state_e tech_state;
833
834         tech_state = wifi_state_get_technology_state();
835         if (tech_state >= NETCONFIG_WIFI_TECH_POWERED)
836                 return -EALREADY;
837
838         if (netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use) < 0) {
839                 ERR("Fail to get VCONF_WIFI_WEARABLE_WIFI_USE");
840                 return -EIO;
841         }
842
843         if (wifi_use == 0) {
844                 WARN("VCONF_WIFI_WEARABLE_WIFI_USE is OFF");
845                 return -EPERM;
846         }
847
848         err = wifi_power_driver_and_supplicant(TRUE);
849         if (err < 0 && err != -EALREADY)
850                 return err;
851
852         err = _set_connman_technology_power(TRUE);
853
854         if (device_picker_test == TRUE)
855                 netconfig_wifi_enable_device_picker_test();
856
857         return err;
858 }
859 #endif
860
861 void wifi_power_initialize(void)
862 {
863         int wifi_last_power_state = 0;
864
865         /* Initialize Airplane mode */
866         netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_mode);
867         DBG("Airplane[%s]", airplane_mode > 0 ? "ON" : "OFF");
868
869         /* Update the last Wi-Fi power state */
870         netconfig_vconf_get_int(VCONF_WIFI_LAST_POWER_STATE, &wifi_last_power_state);
871         if (wifi_last_power_state > VCONFKEY_WIFI_OFF) {
872                 if (TIZEN_TELEPHONY_ENABLE) {
873                         int telephony_ready = 0;
874                         netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_READY, &telephony_ready);
875                         if (telephony_ready == 0) {
876                                 DBG("Telephony API is not initialized yet");
877                                 vconf_notify_key_changed(VCONFKEY_TELEPHONY_READY,
878                                                 __netconfig_telephony_ready_changed_cb, NULL);
879                         } else {
880                                 if (netconfig_tapi_check_sim_state() == FALSE)
881                                         DBG("SIM is not initialized yet");
882                         }
883                 }
884                 DBG("Turn Wi-Fi on automatically");
885 #if defined TIZEN_WEARABLE
886                 wifi_power_on_wearable(TRUE);
887 #else
888                 wifi_power_on();
889 #endif
890         }
891
892 #if defined TIZEN_WEARABLE
893         vconf_notify_key_changed(VCONF_WIFI_WEARABLE_WIFI_USE, __wearable_wifi_use_changed_cb, NULL);
894
895         vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
896                         __netconfig_wifi_airplane_mode, NULL);
897 #else
898         vconf_notify_key_changed(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE,
899                         __netconfig_wifi_restrict_mode, NULL);
900         vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
901                         __netconfig_wifi_airplane_mode, NULL);
902 #endif
903
904         vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE, __emergency_mode_changed_cb, NULL);
905         vconf_notify_key_changed(VCONFKEY_PM_STATE, __pm_state_changed_cb, NULL);
906 }
907
908 void wifi_power_deinitialize(void)
909 {
910 }
911
912 gboolean handle_load_driver(Wifi *wifi,
913                 GDBusMethodInvocation *context, gboolean device_picker_test)
914 {
915         int err;
916
917         DBG("Wi-Fi power on requested");
918
919         g_return_val_if_fail(wifi != NULL, FALSE);
920
921         if (!netconfig_dpm_update_from_wifi()) {
922                 DBG("DPM policy restricts Wi-Fi");
923                 netconfig_error_permission_denied(context);
924                 return TRUE;
925         }
926
927         if (TIZEN_WLAN_BOARD_SPRD)
928                 wifi_firmware_download();
929
930 #if defined TIZEN_WEARABLE
931         err = wifi_power_on_wearable(device_picker_test);
932 #else
933         err = wifi_power_on();
934
935         if (device_picker_test == TRUE)
936                 netconfig_wifi_enable_device_picker_test();
937 #endif
938         if (err < 0) {
939                 if (err == -EINPROGRESS)
940                         netconfig_error_inprogress(context);
941                 else if (err == -EALREADY)
942                         netconfig_error_already_exists(context);
943                 else if (err == -EPERM)
944                         netconfig_error_permission_denied(context);
945                 else
946                         netconfig_error_wifi_driver_failed(context);
947
948                 return TRUE;
949         }
950
951
952         netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
953         __netconfig_set_wifi_bssid();
954
955         wifi_complete_load_driver(wifi, context);
956         return TRUE;
957 }
958
959 gboolean handle_remove_driver(Wifi *wifi, GDBusMethodInvocation *context)
960 {
961         int err;
962
963         DBG("Wi-Fi power off requested");
964
965         g_return_val_if_fail(wifi != NULL, FALSE);
966
967         err = wifi_power_off();
968         if (err < 0) {
969                 if (err == -EINPROGRESS)
970                         netconfig_error_inprogress(context);
971                 else if (err == -EALREADY)
972                         netconfig_error_already_exists(context);
973                 else if (err == -EPERM)
974                         netconfig_error_permission_denied(context);
975                 else
976                         netconfig_error_wifi_driver_failed(context);
977                 return TRUE;
978         }
979
980         netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
981
982         wifi_complete_remove_driver(wifi, context);
983         return TRUE;
984 }
985
986 gboolean handle_load_p2p_driver(Wifi *wifi, GDBusMethodInvocation *context)
987 {
988         ERR("Deprecated");
989
990         wifi_complete_load_p2p_driver(wifi, context);
991         return TRUE;
992 }
993
994 gboolean handle_remove_p2p_driver(Wifi *wifi, GDBusMethodInvocation *context)
995 {
996         ERR("Deprecated");
997
998         wifi_complete_remove_p2p_driver(wifi, context);
999         return TRUE;
1000 }
1001
1002 static int __netconfig_get_random_mac(unsigned char *mac_buf, int mac_len)
1003 {
1004         DBG("Generate Random Mac address of ethernet");
1005         FILE *fp;
1006         int rc;
1007
1008         fp = fopen(OS_RANDOM_FILE, "rb");
1009
1010         if (fp == NULL) {
1011                 ERR("Could not open /dev/urandom");
1012                 return -1;
1013         }
1014         rc = fread(mac_buf, 1, mac_len, fp);
1015         if (fp)
1016                 fclose(fp);
1017
1018         return rc != mac_len ? -1 : 0;
1019 }
1020
1021 void __netconfig_set_ether_macaddr()
1022 {
1023         DBG("Set wired Mac address ");
1024         char *mac_addr = NULL;
1025         char rand_addr[WLAN_MAC_ADDR_MAX];
1026         int rv = -1;
1027
1028         mac_addr = vconf_get_str(VCONF_ETH_MAC_ADDRESS);
1029         if (mac_addr == NULL) {
1030                 DBG("vconf_get_str Failed\n");
1031                 return;
1032         }
1033         /* Checking Invalid MAC Address */
1034         if ((strlen(mac_addr) == 0)) {
1035                 ERR("Failed to get valid MAC Address from vconf");
1036                 /*Generate the Random Mac address*/
1037                 unsigned char rand_mac_add[ETH_MAC_ADDR_SIZE+1];
1038
1039                 if (__netconfig_get_random_mac(rand_mac_add, ETH_MAC_ADDR_SIZE) == -1) {
1040
1041                         ERR("Could not generate the Random Mac address");
1042                         free(mac_addr);
1043                         return;
1044                 }
1045
1046                 rand_mac_add[0] &= 0xFE; /*Clear multicase bit*/
1047                 rand_mac_add[0] |= 0x02; /*set local assignment bit*/
1048
1049                 /*Set the Mac address in Vconf*/
1050                 snprintf(rand_addr, WLAN_MAC_ADDR_MAX, "%x:%x:%x:%x:%x:%x",
1051                                 rand_mac_add[0], rand_mac_add[1],
1052                                 rand_mac_add[2], rand_mac_add[3],
1053                                 rand_mac_add[4], rand_mac_add[5]);
1054
1055                 netconfig_set_vconf_str(VCONF_ETH_MAC_ADDRESS, rand_addr);
1056         } else { /* Valid MAC address */
1057                 strncpy(rand_addr, mac_addr, strlen(mac_addr));
1058                 rand_addr[strlen(mac_addr)] = '\0';
1059         }
1060
1061         DBG("MAC Address of eth0 [%s]", rand_addr);
1062         const char *path = NET_EXEC_PATH;
1063         char *const args[] = { "/sbin/ifconfig", "eth0", "hw",
1064                 "ether", rand_addr, "up", NULL};
1065         char *const envs[] = { NULL };
1066         rv = netconfig_execute_file(path, args, envs);
1067
1068         if (rv < 0)
1069                 ERR("Unable to execute system command");
1070         free(mac_addr);
1071
1072 }