c1e4aaf459d0fe8b017c13e858c22a8ce0f30c13
[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 <stdio.h>
24 #include <stdlib.h>
25 #include <glib.h>
26 #include <tzplatform_config.h>
27 #include <net/if.h>
28 #include <sys/ioctl.h>
29
30 #include "log.h"
31 #include "util.h"
32 #include "netdbus.h"
33 #include "neterror.h"
34 #include "wifi-wps.h"
35 #include "wifi-bssid-scan.h"
36 #include "wifi-power.h"
37 #include "wifi-state.h"
38 #include "netsupplicant.h"
39 #include "network-state.h"
40 #include "network-dpm.h"
41 #include "wifi-firmware.h"
42 #include "wifi-background-scan.h"
43
44
45 #define WLAN_SUPPLICANT_SCRIPT          "/usr/bin/wpa_supp.sh"
46 #define P2P_SUPPLICANT_SCRIPT           "/usr/bin/p2p_supp.sh"
47
48 #define VCONF_WIFI_OFF_STATE_BY_AIRPLANE        "file/private/wifi/wifi_off_by_airplane"
49 #define VCONF_WIFI_OFF_STATE_BY_RESTRICTED      "file/private/wifi/wifi_off_by_restricted"
50 #define VCONF_WIFI_OFF_STATE_BY_EMERGENCY       "file/private/wifi/wifi_off_by_emergency"
51 #if !defined TIZEN_WEARABLE
52 #define VCONFKEY_SETAPPL_NETWORK_PERMIT_WITH_LCD_OFF_LIMIT      "db/setting/network_with_lcd_off_limit"
53 #endif
54
55 #define WLAN_MAC_ADDRESS_FILEPATH   "/sys/class/net/wlan0/address"
56 #define WLAN_MAC_ADDR_MAX           20
57 #define VCONF_WIFI_BSSID_ADDRESS        "db/wifi/bssid_address"
58
59 #define ETH_MAC_ADDR_SIZE 6
60 #define VCONF_ETH_MAC_ADDRESS  "db/dnet/mac_address"
61 #define NET_EXEC_PATH "/sbin/ifconfig"
62 #define OS_RANDOM_FILE "/dev/urandom"
63
64 #define NETCONFIG_TECH_WAITING_INTERVAL 500
65 #define NETCONFIG_TECH_WAITING_COUNT 6
66 #define MAX_DRV_CMD_SIZE 248
67 #define WLAN_IOCTL_SUSPEND (SIOCDEVPRIVATE + 1)
68
69 static gboolean connman_wifi_technology_state = FALSE;
70 static gboolean wifi_firmware_recovery_mode = FALSE;
71 static int airplane_mode = 0;
72
73 typedef struct {
74         char *buf;
75         int used_len;
76         int total_len;
77 } netconfig_wifi_priv_cmd;
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                 g_variant_unref(reply);
124         }
125
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/bin/wpa_supp.sh", "start", NULL };
134         char *const args_disable[] = { "/usr/bin/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 void netconfig_wifi_recover_firmware(void)
156 {
157         wifi_firmware_recovery_mode = TRUE;
158
159         netconfig_wifi_bgscan_stop();
160
161         wifi_power_off();
162 }
163
164 static int _load_driver_and_supplicant(void)
165 {
166         int err = 0;
167         wifi_tech_state_e tech_state;
168
169         tech_state = wifi_state_get_technology_state();
170         if (tech_state > NETCONFIG_WIFI_TECH_OFF)
171                 return -EALREADY;
172
173         err = __execute_supplicant(TRUE);
174         if (err < 0 && err != -EALREADY)
175                 return err;
176
177         err = netconfig_wifi_firmware(NETCONFIG_WIFI_STA, TRUE);
178         if (err < 0 && err != -EALREADY) {
179                 __execute_supplicant(FALSE);
180                 return err;
181         }
182
183         wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_WPS_ONLY);
184
185         return 0;
186 }
187
188 static int _remove_driver_and_supplicant(void)
189 {
190         int err = 0;
191
192         DBG("remove driver and supplicant");
193         if (wifi_firmware_recovery_mode != TRUE &&
194                                         netconfig_wifi_is_bssid_scan_started() == TRUE) {
195                 DBG("Wi-Fi WPS mode");
196                 return 0;
197         }
198
199         err = netconfig_wifi_firmware(NETCONFIG_WIFI_STA, FALSE);
200         if (err < 0 && err != -EALREADY)
201                 return err;
202
203         err = __execute_supplicant(FALSE);
204         if (err < 0 && err != -EALREADY)
205                 return err;
206
207         wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_OFF);
208
209         // reset service state
210         wifi_state_set_service_state(NETCONFIG_WIFI_IDLE);
211
212         if (wifi_firmware_recovery_mode == TRUE) {
213                 if (wifi_power_on() < 0)
214                         ERR("Failed to recover Wi-Fi firmware");
215
216                 wifi_firmware_recovery_mode = FALSE;
217         }
218
219         return 0;
220 }
221
222 static gboolean __check_and_set_technology_enable(gpointer data)
223 {
224         static int retry_count = NETCONFIG_TECH_WAITING_COUNT;
225         gboolean value_enable = TRUE;
226         gboolean reply = FALSE;
227         GVariant *params = NULL;
228         char key[] = "Powered";
229
230         if (wifi_state_is_technology_available() == FALSE) {
231                 retry_count--;
232                 if (retry_count > 0)
233                         return TRUE;
234         }
235
236         params = g_variant_new("(sv)", key, g_variant_new_boolean(value_enable));
237
238         reply = netconfig_invoke_dbus_method_nonblock(CONNMAN_SERVICE,
239                                         CONNMAN_WIFI_TECHNOLOGY_PREFIX,
240                                         CONNMAN_TECHNOLOGY_INTERFACE,
241                                         "SetProperty", params, __technology_reply);
242
243         if (reply != TRUE) {
244                 ERR("Fail to set technology enable");
245                 wifi_state_update_power_state(FALSE);
246
247                 retry_count = NETCONFIG_TECH_WAITING_COUNT;
248                 return FALSE;
249         }
250
251         retry_count = NETCONFIG_TECH_WAITING_COUNT;
252         return FALSE;
253 }
254
255 static int _set_connman_technology_power(gboolean enable)
256 {
257         gboolean reply = FALSE;
258         GVariant *params = NULL;
259         char key[] = "Powered";
260         gboolean value_enable = TRUE;
261         gboolean value_disable = FALSE;
262
263         if (connman_wifi_technology_state == enable)
264                 return -EALREADY;
265
266         if (enable && wifi_state_is_technology_available() == FALSE) {
267                 netconfig_start_timer(NETCONFIG_TECH_WAITING_INTERVAL,
268                                 __check_and_set_technology_enable, NULL, NULL);
269                 connman_wifi_technology_state = enable;
270                 return 0;
271         }
272
273         params = g_variant_new("(sv)", key, (enable == TRUE) ?
274                 g_variant_new_boolean(value_enable) : g_variant_new_boolean(value_disable));
275
276         reply = netconfig_invoke_dbus_method_nonblock(CONNMAN_SERVICE,
277                                         CONNMAN_WIFI_TECHNOLOGY_PREFIX, CONNMAN_TECHNOLOGY_INTERFACE,
278                                         "SetProperty", params, __technology_reply);
279
280         if (reply != TRUE) {
281                 ERR("Fail to set technology %s", enable == TRUE ? "enable" : "disable");
282                 return -ESRCH;
283         }
284
285         /* If Wi-Fi powered off,
286          * Do not remove Wi-Fi driver until ConnMan technology state updated
287          */
288         if (enable == TRUE)
289                 connman_wifi_technology_state = enable;
290
291         /* To be keep safe, early disable Wi-Fi tech state */
292         if (enable != TRUE)
293                 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_WPS_ONLY);
294
295         return 0;
296 }
297
298 static void __netconfig_set_wifi_bssid(void)
299 {
300         int rv = 0;
301         char bssid[WLAN_MAC_ADDR_MAX];
302
303         FILE *fp = fopen(WLAN_MAC_ADDRESS_FILEPATH, "r");
304
305         if (fp == NULL) {
306                 ERR("Fail to open %s", WLAN_MAC_ADDRESS_FILEPATH);
307                 return;
308         }
309
310         fseek(fp, 0L, SEEK_SET);
311         rv = fscanf(fp, "%17s", bssid);
312
313         if (rv < 0)
314                 ERR("Fail to read bssid");
315
316         netconfig_set_vconf_str(VCONF_WIFI_BSSID_ADDRESS, bssid);
317
318         fclose(fp);
319 }
320
321 void netconfig_wifi_disable_technology_state_by_only_connman_signal(void)
322 {
323         /* Important: it's only done by ConnMan technology signal update */
324         connman_wifi_technology_state = FALSE;
325 }
326
327 #if defined TIZEN_WEARABLE
328 int netconfig_wifi_on_wearable(gboolean device_picker_test)
329 {
330         int err = 0;
331         int ps_mode;
332
333         if (netconfig_vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &ps_mode) < 0) {
334                 ERR("Fail to get VCONFKEY_SETAPPL_PSMODE");
335                 return -EIO;
336         }
337
338         if (ps_mode > SETTING_PSMODE_NORMAL) {
339                 WARN("ps mode is on(%d), Not turn on Wi-Fi", ps_mode);
340                 return -EPERM;
341         }
342
343         err = wifi_power_driver_and_supplicant(TRUE);
344         if (err < 0 && err != -EALREADY)
345                 return err;
346
347         err = _set_connman_technology_power(TRUE);
348
349         if (device_picker_test == TRUE)
350                 netconfig_wifi_enable_device_picker_test();
351
352         return err;
353 }
354 #else
355 static void __netconfig_wifi_restrict_mode(keynode_t *node, void *user_data)
356 {
357         int wifi_state = 0, restricted = 0;
358         int wifi_off_by_restricted = 0;
359
360         netconfig_vconf_get_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, &wifi_off_by_restricted);
361
362         netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
363
364         if (node != NULL)
365                 restricted = vconf_keynode_get_bool(node);
366         else
367                 netconfig_vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE, &restricted);
368
369         DBG("network restricted mode %s", restricted > 0 ? "ON" : "OFF");
370         DBG("Wi-Fi state %d, Wi-Fi was off by restricted mode %s", wifi_state,
371                         wifi_off_by_restricted ? "Yes" : "No");
372
373         if (restricted > 0) {
374                 /* network restricted on */
375                 if (wifi_state == VCONFKEY_WIFI_OFF)
376                         return;
377
378                 wifi_power_off();
379
380                 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, 1);
381         } else {
382                 /* network restricted off */
383                 if (!wifi_off_by_restricted)
384                         return;
385
386                 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_RESTRICTED, 0);
387
388                 if (wifi_state > VCONFKEY_WIFI_OFF)
389                         return;
390
391                 wifi_power_on();
392         }
393 }
394 #endif
395
396 static void __netconfig_wifi_airplane_mode(keynode_t *node, void *user_data)
397 {
398         int wifi_state = 0, airplane_state = 0;
399         int wifi_off_by_airplane = 0;
400
401         netconfig_vconf_get_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, &wifi_off_by_airplane);
402         netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
403
404         if (node != NULL)
405                 airplane_state = vconf_keynode_get_bool(node);
406         else
407                 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_state);
408
409         DBG("airplane mode %s (prev:%d)", airplane_state > 0 ? "ON" : "OFF", airplane_mode);
410         DBG("Wi-Fi state(or use) %d, Wi-Fi was off by flight mode %s", wifi_state,
411                         wifi_off_by_airplane ? "Yes" : "No");
412
413         if (airplane_mode == airplane_state)
414                 return ;
415
416         airplane_mode = airplane_state;
417
418         if (airplane_state > 0) {
419                 /* airplane mode on */
420                 if (wifi_state == VCONFKEY_WIFI_OFF)
421                         return;
422
423                 wifi_power_off();
424
425                 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 1);
426         } else {
427                 /* airplane mode off */
428                 if (!wifi_off_by_airplane)
429                         return;
430
431                 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
432                 if (wifi_state > VCONFKEY_WIFI_OFF)
433                         return;
434
435                 wifi_power_on();
436         }
437 }
438
439 static void __emergency_mode_changed_cb(keynode_t *node, void *user_data)
440 {
441         int wifi_state = 0, emergency = 0;
442         int wifi_off_by_emergency = 0;
443 #if !defined TIZEN_WEARABLE
444         int emergency_by_fmm = 0;
445 #endif
446         netconfig_vconf_get_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, &wifi_off_by_emergency);
447         netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
448
449 #if !defined TIZEN_WEARABLE
450         netconfig_vconf_get_bool(VCONFKEY_SETAPPL_NETWORK_PERMIT_WITH_LCD_OFF_LIMIT, &emergency_by_fmm);
451         DBG("emergency mode by Find My Mobile (%d)", emergency_by_fmm);
452         if (emergency_by_fmm == 1)
453                 return;
454 #endif
455
456         if (node != NULL)
457                 emergency = vconf_keynode_get_int(node);
458         else
459                 netconfig_vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &emergency);
460
461         DBG("emergency mode %s", emergency > SETTING_PSMODE_POWERFUL ? "ON" : "OFF");
462         DBG("Wi-Fi state %d, Wi-Fi was off by emergency mode %s", wifi_state, wifi_off_by_emergency ? "Yes" : "No");
463
464 #if defined TIZEN_WEARABLE
465         if (emergency == SETTING_PSMODE_WEARABLE) {
466                 /* basic power saving mode on */
467         } else if (emergency == SETTING_PSMODE_WEARABLE_ENHANCED) {
468                 /* enhanced power saving mode on */
469                 if (wifi_state == VCONFKEY_WIFI_OFF)
470                         return;
471
472                 wifi_power_off();
473                 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 1);
474         } else {
475                 /* power saving mode off */
476                 if (!wifi_off_by_emergency)
477                         return;
478
479                 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 0);
480                 if (wifi_state > VCONFKEY_WIFI_OFF)
481                         return;
482
483                 wifi_power_on_wearable(TRUE);
484         }
485 #else
486         if (emergency > SETTING_PSMODE_POWERFUL) {
487                 /* emergency mode on */
488                 if (wifi_state == VCONFKEY_WIFI_OFF)
489                         return;
490
491                 wifi_power_off();
492
493                 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 1);
494         } else {
495                 /* emergency mode off */
496                 if (!wifi_off_by_emergency)
497                         return;
498
499                 netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_EMERGENCY, 0);
500
501                 if (wifi_state > VCONFKEY_WIFI_OFF)
502                         return;
503
504                 wifi_power_on();
505         }
506 #endif
507
508 }
509
510 void wifi_set_early_suspend(gboolean value)
511 {
512         static gboolean old_state = FALSE;
513         struct ifreq ifr;
514         char buf[MAX_DRV_CMD_SIZE];
515         netconfig_wifi_priv_cmd priv_cmd;
516         int ret = 0;
517         int ioctl_sock = 0;
518         int pm_state = 0;
519         wifi_service_state_e wifi_state;
520         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
521
522         if (old_state == value) {
523                 DBG("Old and new states are same");
524                 return;
525         }
526
527         if (netconfig_vconf_get_int(VCONFKEY_PM_STATE, &pm_state) < 0)
528                 ERR("Fail to get VCONFKEY_PM_STATE");
529
530         wifi_state = wifi_state_get_service_state();
531
532         if (value == TRUE &&
533                         (pm_state < VCONFKEY_PM_STATE_LCDOFF ||
534                          wifi_state == NETCONFIG_WIFI_ASSOCIATION ||
535                          wifi_state == NETCONFIG_WIFI_CONFIGURATION)){
536                 DBG("");
537                 return;
538         }
539
540         memset(buf, 0, sizeof(buf));
541         snprintf(buf, sizeof(buf), "SETSUSPENDMODE %d", value);
542
543         memset(&ifr, 0, sizeof(struct ifreq));
544         g_strlcpy((char *)ifr.ifr_name, WIFI_IFNAME, IFNAMSIZ);
545
546         DBG("Early suspend command: [%s]", buf);
547
548         memset(&priv_cmd, 0, sizeof(priv_cmd));
549         priv_cmd.buf = buf;
550         priv_cmd.used_len = sizeof(buf);
551         priv_cmd.total_len = sizeof(buf);
552         ifr.ifr_data = (char *)&priv_cmd;
553
554         ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
555         if (ioctl_sock < 0) {
556                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
557                 DBG("socket(PF_INET,SOCK_DGRAM) failed: %s", error_buf);
558                 return;
559         }
560
561         ret = ioctl(ioctl_sock, WLAN_IOCTL_SUSPEND, &ifr);
562
563         if (ret < 0) {
564                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
565                 ERR("Fail to issue private commands: %d. %s", ret, error_buf);
566         } else {
567                 old_state = value;
568         }
569
570         close(ioctl_sock);
571 }
572
573 static void __pm_state_changed_cb(keynode_t* node, void* user_data)
574 {
575         int new_state = -1;
576         int wifi_state = 0;
577         static int prev_state = VCONFKEY_PM_STATE_NORMAL;
578
579         if (netconfig_vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state) < 0) {
580                 ERR("Fail to get VCONFKEY_WIFI_STATE");
581                 return;
582         }
583
584         /* PM state
585          *      VCONFKEY_PM_STATE_NORMAL = 1,
586          *      VCONFKEY_PM_STATE_LCDDIM,
587          *      VCONFKEY_PM_STATE_LCDOFF,
588          *      VCONFKEY_PM_STATE_SLEEP
589          */
590         if (node != NULL)
591                 new_state = vconf_keynode_get_int(node);
592         else
593                 netconfig_vconf_get_int(VCONFKEY_PM_STATE, &new_state);
594
595         DBG("wifi state: %d (0 off / 1 on / 2 connected)", wifi_state);
596         DBG("Old PM state: %d, current: %d (1 normal / 2 lcddim / 3 lcdoff / 4 sleep)", prev_state, new_state);
597
598         if ((new_state == VCONFKEY_PM_STATE_NORMAL) && (prev_state >= VCONFKEY_PM_STATE_LCDOFF)) {
599                 /* Send early suspend mode based on LCD state and disallow early suspend count */
600                 if (wifi_state != VCONFKEY_WIFI_OFF) {
601                         wifi_set_early_suspend(FALSE);
602                         DBG("Unset early suspend");
603                 }
604
605                 netconfig_wifi_bgscan_stop();
606                 netconfig_wifi_bgscan_set_interval(SCAN_EXPONENTIAL_MIN);
607                 netconfig_wifi_bgscan_start(TRUE);
608         } else if ((new_state == VCONFKEY_PM_STATE_LCDOFF) && (prev_state < VCONFKEY_PM_STATE_LCDOFF) && (wifi_state != VCONFKEY_WIFI_OFF)) {
609                 wifi_set_early_suspend(TRUE);
610                 DBG("Set early suspend");
611         }
612
613         prev_state = new_state;
614 }
615
616 static void __netconfig_telephony_ready_changed_cb(keynode_t * node, void *data)
617 {
618         int telephony_ready = 0;
619
620         if (node != NULL)
621                 telephony_ready = vconf_keynode_get_bool(node);
622         else
623                 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_READY, &telephony_ready);
624
625         if (telephony_ready != 0) {
626                 if (netconfig_tapi_check_sim_state() == FALSE) {
627                         DBG("Sim is not initialized yet.");
628
629                         goto done;
630                 }
631         } else
632                 return;
633
634         DBG("Turn Wi-Fi on automatically");
635
636 #if defined TIZEN_WEARABLE
637         wifi_power_on_wearable(TRUE);
638 #else
639         wifi_power_on();
640 #endif
641
642 done:
643         vconf_ignore_key_changed(VCONFKEY_TELEPHONY_READY, __netconfig_telephony_ready_changed_cb);
644 }
645
646 int wifi_power_driver_and_supplicant(gboolean enable)
647 {
648         /* There are 3 thumb rules for Wi-Fi power management
649          *   1. Do not make exposed API to control wpa_supplicant and driver directly.
650          *      It probably breaks ConnMan technology operation.
651          *
652          *   2. Do not remove driver and wpa_supplicant if ConnMan already enabled.
653          *      It breaks ConnMan technology operation.
654          *
655          *   3. Final the best rule: make it as simple as possible.
656          *      Simple code enables easy maintenance and reduces logical errors.
657          */
658         if (enable == TRUE) {
659                 return _load_driver_and_supplicant();
660         } else {
661                 if (connman_wifi_technology_state == TRUE)
662                         return -ENOSYS;
663
664                 return _remove_driver_and_supplicant();
665         }
666 }
667
668 void wifi_power_disable_technology_state_by_only_connman_signal(void)
669 {
670         /* Important: it's only done by ConnMan technology signal update */
671         connman_wifi_technology_state = FALSE;
672 }
673
674 void wifi_power_recover_firmware(void)
675 {
676         wifi_firmware_recovery_mode = TRUE;
677
678         netconfig_wifi_bgscan_stop();
679
680         wifi_power_off();
681 }
682
683 int wifi_power_on(void)
684 {
685         int err = 0;
686         wifi_tech_state_e tech_state;
687
688         tech_state = wifi_state_get_technology_state();
689         if (tech_state >= NETCONFIG_WIFI_TECH_POWERED) {
690                 /* There can be a scenario where wifi is automatically *
691                  * activated by connman if wifi was powered in last boot. *
692                  * So we should update connman_wifi_technology_state variable *
693                  * if it is found that wifi_tech_state variable is *
694                  * NETCONFIG_WIFI_TECH_POWERED and connman_wifi_technology_state *
695                  * variable is FALSE. Earlier connman_wifi_technology_state *
696                  * variable was only updated when wifi was Powered on from *
697                  * net-config resulting in variable not getting updated. *
698                  * This caused wifi to not get deactivated after reboot if *
699                  * last power state was activated */
700                 ERR("Net-Config WiFi connman technology state %d",
701                                 connman_wifi_technology_state);
702                 if (connman_wifi_technology_state == FALSE)
703                         connman_wifi_technology_state = TRUE;
704                 return -EALREADY;
705         }
706
707         if (__is_wifi_restricted() == TRUE)
708                 return -EPERM;
709
710         if (netconfig_is_wifi_tethering_on() == TRUE) {
711                 /* TODO: Wi-Fi tethering turns off here */
712                 /* return TRUE; */
713                 ERR("Failed to turn tethering off");
714                 return -EBUSY;
715         }
716
717         err = wifi_power_driver_and_supplicant(TRUE);
718         if (err < 0 && err != -EALREADY)
719                 return err;
720
721         err = _set_connman_technology_power(TRUE);
722
723         return err;
724 }
725
726 int wifi_power_off(void)
727 {
728         int err;
729
730         err = _set_connman_technology_power(FALSE);
731         if (err == -EALREADY)
732                 wifi_state_update_power_state(FALSE);
733
734         return 0;
735 }
736
737 #if defined TIZEN_WEARABLE
738 int wifi_power_on_wearable(gboolean device_picker_test)
739 {
740         int err = 0;
741         wifi_tech_state_e tech_state;
742
743         tech_state = wifi_state_get_technology_state();
744         if (tech_state >= NETCONFIG_WIFI_TECH_POWERED)
745                 return -EALREADY;
746
747         err = wifi_power_driver_and_supplicant(TRUE);
748         if (err < 0 && err != -EALREADY)
749                 return err;
750
751         err = _set_connman_technology_power(TRUE);
752
753         if (device_picker_test == TRUE)
754                 netconfig_wifi_enable_device_picker_test();
755
756         return err;
757 }
758 #endif
759
760 void wifi_power_initialize(void)
761 {
762         int wifi_last_power_state = 0;
763
764         /* Initialize Airplane mode */
765         netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_mode);
766         DBG("Airplane[%s]", airplane_mode > 0 ? "ON" : "OFF");
767
768         /* Update the last Wi-Fi power state */
769         netconfig_vconf_get_int(VCONF_WIFI_LAST_POWER_STATE, &wifi_last_power_state);
770         if (wifi_last_power_state > VCONFKEY_WIFI_OFF) {
771                 if (TIZEN_TELEPHONY_ENABLE) {
772                         int telephony_ready = 0;
773                         netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_READY, &telephony_ready);
774                         if (telephony_ready == 0) {
775                                 DBG("Telephony API is not initialized yet");
776                                 vconf_notify_key_changed(VCONFKEY_TELEPHONY_READY,
777                                                 __netconfig_telephony_ready_changed_cb, NULL);
778                         } else {
779                                 if (netconfig_tapi_check_sim_state() == FALSE)
780                                         DBG("SIM is not initialized yet");
781                         }
782                 }
783                 DBG("Turn Wi-Fi on automatically");
784 #if defined TIZEN_WEARABLE
785                 wifi_power_on_wearable(TRUE);
786 #else
787                 wifi_power_on();
788 #endif
789         }
790
791 #if defined TIZEN_WEARABLE
792         vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
793                         __netconfig_wifi_airplane_mode, NULL);
794 #else
795         vconf_notify_key_changed(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE,
796                         __netconfig_wifi_restrict_mode, NULL);
797         vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
798                         __netconfig_wifi_airplane_mode, NULL);
799 #endif
800
801         vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE, __emergency_mode_changed_cb, NULL);
802         vconf_notify_key_changed(VCONFKEY_PM_STATE, __pm_state_changed_cb, NULL);
803 }
804
805 void wifi_power_deinitialize(void)
806 {
807 }
808
809 gboolean handle_load_driver(Wifi *wifi,
810                 GDBusMethodInvocation *context, gboolean device_picker_test)
811 {
812         int err;
813
814         DBG("Wi-Fi power on requested");
815
816         g_return_val_if_fail(wifi != NULL, TRUE);
817
818         if (!netconfig_dpm_update_from_wifi()) {
819                 DBG("DPM policy restricts Wi-Fi");
820                 netconfig_error_permission_denied(context);
821                 return TRUE;
822         }
823
824         if (TIZEN_WLAN_BOARD_SPRD)
825                 wifi_firmware_download();
826
827 #if defined TIZEN_WEARABLE
828         err = wifi_power_on_wearable(device_picker_test);
829 #else
830         err = wifi_power_on();
831
832         if (device_picker_test == TRUE)
833                 netconfig_wifi_enable_device_picker_test();
834 #endif
835         if (err < 0) {
836                 if (err == -EALREADY)
837                         netconfig_error_already_exists(context);
838                 else if (err == -EPERM)
839                         netconfig_error_permission_denied(context);
840                 else
841                         netconfig_error_wifi_driver_failed(context);
842
843                 return TRUE;
844         }
845
846
847         netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
848         __netconfig_set_wifi_bssid();
849
850         wifi_complete_load_driver(wifi, context);
851         return TRUE;
852 }
853
854 gboolean handle_remove_driver(Wifi *wifi, GDBusMethodInvocation *context)
855 {
856         int err;
857
858         DBG("Wi-Fi power off requested");
859
860         g_return_val_if_fail(wifi != NULL, TRUE);
861
862         err = wifi_power_off();
863         if (err < 0) {
864                 if (err == -EINPROGRESS)
865                         netconfig_error_inprogress(context);
866                 else if (err == -EALREADY)
867                         netconfig_error_already_exists(context);
868                 else if (err == -EPERM)
869                         netconfig_error_permission_denied(context);
870                 else
871                         netconfig_error_wifi_driver_failed(context);
872                 return TRUE;
873         }
874
875         netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
876
877         wifi_complete_remove_driver(wifi, context);
878         return TRUE;
879 }
880
881 gboolean handle_load_p2p_driver(Wifi *wifi, GDBusMethodInvocation *context)
882 {
883         ERR("Deprecated");
884
885         wifi_complete_load_p2p_driver(wifi, context);
886         return TRUE;
887 }
888
889 gboolean handle_remove_p2p_driver(Wifi *wifi, GDBusMethodInvocation *context)
890 {
891         ERR("Deprecated");
892
893         wifi_complete_remove_p2p_driver(wifi, context);
894         return TRUE;
895 }
896
897 static int __netconfig_get_random_mac(unsigned char *mac_buf, int mac_len)
898 {
899         DBG("Generate Random Mac address of ethernet");
900         FILE *fp;
901         int rc;
902
903         fp = fopen(OS_RANDOM_FILE, "rb");
904
905         if (fp == NULL) {
906                 ERR("Could not open /dev/urandom");
907                 return -1;
908         }
909         rc = fread(mac_buf, 1, mac_len, fp);
910         if (fp)
911                 fclose(fp);
912
913         return rc != mac_len ? -1 : 0;
914 }
915
916 void __netconfig_set_ether_macaddr()
917 {
918         DBG("Set wired Mac address ");
919         char *mac_addr = NULL;
920         char rand_addr[WLAN_MAC_ADDR_MAX];
921         int rv = -1;
922
923         mac_addr = vconf_get_str(VCONF_ETH_MAC_ADDRESS);
924         if (mac_addr == NULL) {
925                 DBG("vconf_get_str Failed\n");
926                 return;
927         }
928         /* Checking Invalid MAC Address */
929         if ((strlen(mac_addr) == 0)) {
930                 ERR("Failed to get valid MAC Address from vconf");
931                 /*Generate the Random Mac address*/
932                 unsigned char rand_mac_add[ETH_MAC_ADDR_SIZE+1];
933
934                 if (__netconfig_get_random_mac(rand_mac_add, ETH_MAC_ADDR_SIZE) == -1) {
935
936                         ERR("Could not generate the Random Mac address");
937                         free(mac_addr);
938                         return;
939                 }
940
941                 rand_mac_add[0] &= 0xFE; /*Clear multicase bit*/
942                 rand_mac_add[0] |= 0x02; /*set local assignment bit*/
943
944                 /*Set the Mac address in Vconf*/
945                 snprintf(rand_addr, WLAN_MAC_ADDR_MAX, "%x:%x:%x:%x:%x:%x",
946                                 rand_mac_add[0], rand_mac_add[1],
947                                 rand_mac_add[2], rand_mac_add[3],
948                                 rand_mac_add[4], rand_mac_add[5]);
949
950                 netconfig_set_vconf_str(VCONF_ETH_MAC_ADDRESS, rand_addr);
951         } else { /* Valid MAC address */
952                 g_strlcpy(rand_addr, mac_addr, WLAN_MAC_ADDR_MAX);
953         }
954
955         DBG("MAC Address of eth0 [%s]", rand_addr);
956         const char *path = NET_EXEC_PATH;
957         char *const args[] = { "/sbin/ifconfig", "eth0", "hw",
958                 "ether", rand_addr, "up", NULL};
959         char *const envs[] = { NULL };
960         rv = netconfig_execute_file(path, args, envs);
961
962         if (rv < 0)
963                 ERR("Unable to execute system command");
964         free(mac_addr);
965
966 }