4dd968a04f0e537d88d972da80c8f31d3630bdf2
[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         }
118
119         g_variant_unref(reply);
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         INFO("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", 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_start(TRUE);
563         }
564
565         prev_state = new_state;
566 }
567
568 static void __netconfig_telephony_ready_changed_cb(keynode_t * node, void *data)
569 {
570         int telephony_ready = 0;
571
572         if (node != NULL)
573                 telephony_ready = vconf_keynode_get_bool(node);
574         else
575                 netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_READY, &telephony_ready);
576
577         if (telephony_ready != 0) {
578                 if (netconfig_tapi_check_sim_state() == FALSE) {
579                         DBG("Sim is not initialized yet.");
580
581                         goto done;
582                 }
583         } else
584                 return;
585
586         DBG("Turn Wi-Fi on automatically");
587
588 #if defined TIZEN_WEARABLE
589         wifi_power_on_wearable(TRUE);
590 #else
591         wifi_power_on();
592 #endif
593
594 done:
595         vconf_ignore_key_changed(VCONFKEY_TELEPHONY_READY, __netconfig_telephony_ready_changed_cb);
596 }
597
598 int wifi_power_driver_and_supplicant(gboolean enable)
599 {
600         /* There are 3 thumb rules for Wi-Fi power management
601          *   1. Do not make exposed API to control wpa_supplicant and driver directly.
602          *      It probably breaks ConnMan technology operation.
603          *
604          *   2. Do not remove driver and wpa_supplicant if ConnMan already enabled.
605          *      It breaks ConnMan technology operation.
606          *
607          *   3. Final the best rule: make it as simple as possible.
608          *      Simple code enables easy maintenance and reduces logical errors.
609          */
610         if (enable == TRUE) {
611                 return _load_driver_and_supplicant();
612         } else {
613                 if (connman_wifi_technology_state == TRUE)
614                         return -ENOSYS;
615
616                 return _remove_driver_and_supplicant();
617         }
618 }
619
620 void wifi_power_disable_technology_state_by_only_connman_signal(void)
621 {
622         /* Important: it's only done by ConnMan technology signal update */
623         connman_wifi_technology_state = FALSE;
624 }
625
626 void wifi_power_recover_firmware(void)
627 {
628         wifi_firmware_recovery_mode = TRUE;
629
630         netconfig_wifi_bgscan_stop();
631
632         wifi_power_off();
633 }
634
635 int wifi_power_on(void)
636 {
637         int err = 0;
638         wifi_tech_state_e tech_state;
639
640         tech_state = wifi_state_get_technology_state();
641         if (tech_state >= NETCONFIG_WIFI_TECH_POWERED) {
642                 /* There can be a scenario where wifi is automatically *
643                  * activated by connman if wifi was powered in last boot. *
644                  * So we should update connman_wifi_technology_state variable *
645                  * if it is found that wifi_tech_state variable is *
646                  * NETCONFIG_WIFI_TECH_POWERED and connman_wifi_technology_state *
647                  * variable is FALSE. Earlier connman_wifi_technology_state *
648                  * variable was only updated when wifi was Powered on from *
649                  * net-config resulting in variable not getting updated. *
650                  * This caused wifi to not get deactivated after reboot if *
651                  * last power state was activated */
652                 ERR("Net-Config WiFi connman technology state %d",
653                                 connman_wifi_technology_state);
654                 if (connman_wifi_technology_state == FALSE)
655                         connman_wifi_technology_state = TRUE;
656                 return -EALREADY;
657         }
658
659         if (__is_wifi_restricted() == TRUE)
660                 return -EPERM;
661
662         if (netconfig_is_wifi_tethering_on() == TRUE) {
663                 /* TODO: Wi-Fi tethering turns off here */
664                 /* return TRUE; */
665                 ERR("Failed to turn tethering off");
666                 return -EBUSY;
667         }
668
669         err = wifi_power_driver_and_supplicant(TRUE);
670         if (err < 0 && err != -EALREADY)
671                 return err;
672
673         err = _set_connman_technology_power(TRUE);
674
675         return err;
676 }
677
678 int wifi_power_off(void)
679 {
680         int err;
681
682         err = _set_connman_technology_power(FALSE);
683         if (err == -EALREADY)
684                 wifi_state_update_power_state(FALSE);
685
686         return 0;
687 }
688
689 #if defined TIZEN_WEARABLE
690 int wifi_power_on_wearable(gboolean device_picker_test)
691 {
692         int err = 0;
693         int wifi_use = 1;
694         wifi_tech_state_e tech_state;
695
696         tech_state = wifi_state_get_technology_state();
697         if (tech_state >= NETCONFIG_WIFI_TECH_POWERED)
698                 return -EALREADY;
699
700         if (netconfig_vconf_get_int(VCONF_WIFI_WEARABLE_WIFI_USE, &wifi_use) < 0) {
701                 ERR("Fail to get VCONF_WIFI_WEARABLE_WIFI_USE");
702                 return -EIO;
703         }
704
705         if (wifi_use == 0) {
706                 WARN("VCONF_WIFI_WEARABLE_WIFI_USE is OFF");
707                 return -EPERM;
708         }
709
710         err = wifi_power_driver_and_supplicant(TRUE);
711         if (err < 0 && err != -EALREADY)
712                 return err;
713
714         err = _set_connman_technology_power(TRUE);
715
716         if (device_picker_test == TRUE)
717                 netconfig_wifi_enable_device_picker_test();
718
719         return err;
720 }
721 #endif
722
723 void wifi_power_initialize(void)
724 {
725         int wifi_last_power_state = 0;
726
727         /* Initialize Airplane mode */
728         netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &airplane_mode);
729         DBG("Airplane[%s]", airplane_mode > 0 ? "ON" : "OFF");
730
731         /* Update the last Wi-Fi power state */
732         netconfig_vconf_get_int(VCONF_WIFI_LAST_POWER_STATE, &wifi_last_power_state);
733         if (wifi_last_power_state > VCONFKEY_WIFI_OFF) {
734                 if (TIZEN_TELEPHONY_ENABLE) {
735                         int telephony_ready = 0;
736                         netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_READY, &telephony_ready);
737                         if (telephony_ready == 0) {
738                                 DBG("Telephony API is not initialized yet");
739                                 vconf_notify_key_changed(VCONFKEY_TELEPHONY_READY,
740                                                 __netconfig_telephony_ready_changed_cb, NULL);
741                         } else {
742                                 if (netconfig_tapi_check_sim_state() == FALSE)
743                                         DBG("SIM is not initialized yet");
744                         }
745                 }
746                 DBG("Turn Wi-Fi on automatically");
747 #if defined TIZEN_WEARABLE
748                 wifi_power_on_wearable(TRUE);
749 #else
750                 wifi_power_on();
751 #endif
752         }
753
754 #if defined TIZEN_WEARABLE
755         vconf_notify_key_changed(VCONF_WIFI_WEARABLE_WIFI_USE, __wearable_wifi_use_changed_cb, NULL);
756
757         vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
758                         __netconfig_wifi_airplane_mode, NULL);
759 #else
760         vconf_notify_key_changed(VCONFKEY_SETAPPL_NETWORK_RESTRICT_MODE,
761                         __netconfig_wifi_restrict_mode, NULL);
762         vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
763                         __netconfig_wifi_airplane_mode, NULL);
764 #endif
765
766         vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE, __emergency_mode_changed_cb, NULL);
767         vconf_notify_key_changed(VCONFKEY_PM_STATE, __pm_state_changed_cb, NULL);
768 }
769
770 void wifi_power_deinitialize(void)
771 {
772 }
773
774 gboolean handle_load_driver(Wifi *wifi,
775                 GDBusMethodInvocation *context, gboolean device_picker_test)
776 {
777         int err;
778
779         DBG("Wi-Fi power on requested");
780
781         g_return_val_if_fail(wifi != NULL, FALSE);
782
783         if (!netconfig_dpm_update_from_wifi()) {
784                 DBG("DPM policy restricts Wi-Fi");
785                 netconfig_error_permission_denied(context);
786                 return TRUE;
787         }
788
789         if (TIZEN_WLAN_BOARD_SPRD)
790                 wifi_firmware_download();
791
792 #if defined TIZEN_WEARABLE
793         err = wifi_power_on_wearable(device_picker_test);
794 #else
795         err = wifi_power_on();
796
797         if (device_picker_test == TRUE)
798                 netconfig_wifi_enable_device_picker_test();
799 #endif
800         if (err < 0) {
801                 if (err == -EINPROGRESS)
802                         netconfig_error_inprogress(context);
803                 else if (err == -EALREADY)
804                         netconfig_error_already_exists(context);
805                 else if (err == -EPERM)
806                         netconfig_error_permission_denied(context);
807                 else
808                         netconfig_error_wifi_driver_failed(context);
809
810                 return TRUE;
811         }
812
813
814         netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
815         __netconfig_set_wifi_bssid();
816
817         wifi_complete_load_driver(wifi, context);
818         return TRUE;
819 }
820
821 gboolean handle_remove_driver(Wifi *wifi, GDBusMethodInvocation *context)
822 {
823         int err;
824
825         DBG("Wi-Fi power off requested");
826
827         g_return_val_if_fail(wifi != NULL, FALSE);
828
829         err = wifi_power_off();
830         if (err < 0) {
831                 if (err == -EINPROGRESS)
832                         netconfig_error_inprogress(context);
833                 else if (err == -EALREADY)
834                         netconfig_error_already_exists(context);
835                 else if (err == -EPERM)
836                         netconfig_error_permission_denied(context);
837                 else
838                         netconfig_error_wifi_driver_failed(context);
839                 return TRUE;
840         }
841
842         netconfig_set_vconf_int(VCONF_WIFI_OFF_STATE_BY_AIRPLANE, 0);
843
844         wifi_complete_remove_driver(wifi, context);
845         return TRUE;
846 }
847
848 gboolean handle_load_p2p_driver(Wifi *wifi, GDBusMethodInvocation *context)
849 {
850         ERR("Deprecated");
851
852         wifi_complete_load_p2p_driver(wifi, context);
853         return TRUE;
854 }
855
856 gboolean handle_remove_p2p_driver(Wifi *wifi, GDBusMethodInvocation *context)
857 {
858         ERR("Deprecated");
859
860         wifi_complete_remove_p2p_driver(wifi, context);
861         return TRUE;
862 }
863
864 static int __netconfig_get_random_mac(unsigned char *mac_buf, int mac_len)
865 {
866         DBG("Generate Random Mac address of ethernet");
867         FILE *fp;
868         int rc;
869
870         fp = fopen(OS_RANDOM_FILE, "rb");
871
872         if (fp == NULL) {
873                 ERR("Could not open /dev/urandom");
874                 return -1;
875         }
876         rc = fread(mac_buf, 1, mac_len, fp);
877         if (fp)
878                 fclose(fp);
879
880         return rc != mac_len ? -1 : 0;
881 }
882
883 void __netconfig_set_ether_macaddr()
884 {
885         DBG("Set wired Mac address ");
886         char *mac_addr = NULL;
887         char rand_addr[WLAN_MAC_ADDR_MAX];
888         int rv = -1;
889
890         mac_addr = vconf_get_str(VCONF_ETH_MAC_ADDRESS);
891         if (mac_addr == NULL) {
892                 DBG("vconf_get_str Failed\n");
893                 return;
894         }
895         /* Checking Invalid MAC Address */
896         if ((strlen(mac_addr) == 0)) {
897                 ERR("Failed to get valid MAC Address from vconf");
898                 /*Generate the Random Mac address*/
899                 unsigned char rand_mac_add[ETH_MAC_ADDR_SIZE+1];
900
901                 if (__netconfig_get_random_mac(rand_mac_add, ETH_MAC_ADDR_SIZE) == -1) {
902
903                         ERR("Could not generate the Random Mac address");
904                         free(mac_addr);
905                         return;
906                 }
907
908                 rand_mac_add[0] &= 0xFE; /*Clear multicase bit*/
909                 rand_mac_add[0] |= 0x02; /*set local assignment bit*/
910
911                 /*Set the Mac address in Vconf*/
912                 snprintf(rand_addr, WLAN_MAC_ADDR_MAX, "%x:%x:%x:%x:%x:%x",
913                                 rand_mac_add[0], rand_mac_add[1],
914                                 rand_mac_add[2], rand_mac_add[3],
915                                 rand_mac_add[4], rand_mac_add[5]);
916
917                 netconfig_set_vconf_str(VCONF_ETH_MAC_ADDRESS, rand_addr);
918         } else { /* Valid MAC address */
919                 g_strlcpy(rand_addr, mac_addr, WLAN_MAC_ADDR_MAX);
920         }
921
922         DBG("MAC Address of eth0 [%s]", rand_addr);
923         const char *path = NET_EXEC_PATH;
924         char *const args[] = { "/sbin/ifconfig", "eth0", "hw",
925                 "ether", rand_addr, "up", NULL};
926         char *const envs[] = { NULL };
927         rv = netconfig_execute_file(path, args, envs);
928
929         if (rv < 0)
930                 ERR("Unable to execute system command");
931         free(mac_addr);
932
933 }