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