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