1c940df77ee70b8ff969a7e88d6315f673324563
[platform/core/connectivity/net-config.git] / src / wifi-firmware.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
24 #include "log.h"
25 #include "util.h"
26 #include "netdbus.h"
27 #include "emulator.h"
28 #include "neterror.h"
29 #include "netsupplicant.h"
30 #include "wifi-firmware.h"
31 #include "network-statistics.h"
32
33 #define WLAN_DRIVER_SCRIPT                      "/usr/bin/wlan.sh"
34 #define WLAN_IFACE_NAME                         "wlan0"
35
36 #define WLAN_P2P_IFACE_NAME_TV                  "p2p0"
37 #define WLAN_P2P_IFACE_NAME_COMMON                      "wlan0"
38 #define WLAN_P2P_IFACE_NAME ((TIZEN_TV) ? (WLAN_P2P_IFACE_NAME_TV) : (WLAN_P2P_IFACE_NAME_COMMON))
39
40 static int __netconfig_sta_firmware_start(void)
41 {
42         int rv = 0;
43         const char *path = WLAN_DRIVER_SCRIPT;
44         char *const args[] = { "/usr/bin/wlan.sh", "start", NULL };
45         char *const envs[] = { NULL };
46
47         if (TIZEN_WLAN_BOARD_SPRD) {
48                 rv = netconfig_execute_file(path, args, envs);
49                 if (rv < 0)
50                         return -EIO;
51         }
52
53         rv = netconfig_interface_up(WLAN_IFACE_NAME);
54         if (rv != TRUE)
55                 return -EIO;
56
57         DBG("Successfully loaded wireless device driver");
58         return 0;
59 }
60
61 static int __netconfig_sta_firmware_stop(void)
62 {
63         int rv = 0;
64         const char *path = WLAN_DRIVER_SCRIPT;
65         char *const args[] = { "/usr/bin/wlan.sh", "stop", NULL };
66         char *const envs[] = { NULL };
67
68         /* Update statistics before driver remove */
69         netconfig_wifi_statistics_update_powered_off();
70
71         rv = netconfig_interface_down(WLAN_IFACE_NAME);
72         if (rv != TRUE)
73                 return -EIO;
74
75         if (TIZEN_WLAN_BOARD_SPRD) {
76                 rv = netconfig_execute_file(path, args, envs);
77                 if (rv < 0)
78                         return -EIO;
79         }
80
81         DBG("Successfully removed wireless device driver");
82         return 0;
83 }
84
85 static int __netconfig_p2p_firmware_start(void)
86 {
87 #if defined TIZEN_P2P_ENABLE
88         int rv = 0;
89         const char *path = WLAN_DRIVER_SCRIPT;
90         char *const args[] = { "/usr/bin/wlan.sh", "p2p", NULL };
91         char *const envs[] = { NULL };
92
93         rv = netconfig_execute_file(path, args, envs);
94         if (rv < 0)
95                 return -EIO;
96
97 #if defined TIZEN_WLAN_USE_P2P_INTERFACE
98         rv = netconfig_interface_up(WLAN_P2P_IFACE_NAME);
99         if (rv != TRUE)
100                 return -EIO;
101 #endif
102
103         DBG("Successfully loaded p2p device driver");
104         return 0;
105 #else
106         return -ENODEV;
107 #endif
108 }
109
110 static int __netconfig_p2p_firmware_stop(void)
111 {
112 #if defined TIZEN_P2P_ENABLE
113         int rv = 0;
114         const char *path = WLAN_DRIVER_SCRIPT;
115         char *const args[] = { "/usr/bin/wlan.sh", "stop", NULL };
116         char *const envs[] = { NULL };
117
118         rv = netconfig_interface_down(WLAN_IFACE_NAME);
119         if (rv != TRUE)
120                 return -EIO;
121
122         rv = netconfig_execute_file(path, args, envs);
123         if (rv < 0)
124                 return -EIO;
125
126         DBG("Successfully removed p2p device driver");
127         return 0;
128 #else
129         return -ENODEV;
130 #endif
131 }
132
133 static int __netconfig_softap_firmware_start(void)
134 {
135 #if defined TIZEN_TETHERING_ENABLE
136         int rv = 0;
137         const char *path = WLAN_DRIVER_SCRIPT;
138         char *const args[] = { "/usr/bin/wlan.sh", "softap", NULL };
139         char *const envs[] = { NULL };
140
141         rv = netconfig_execute_file(path, args, envs);
142         if (rv < 0)
143                 return -EIO;
144
145         if (netconfig_interface_up(WLAN_IFACE_NAME) == FALSE)
146                 return -EIO;
147
148         DBG("Successfully loaded softap device driver");
149         return 0;
150 #else
151         return -ENODEV;
152 #endif
153 }
154
155 static int __netconfig_softap_firmware_stop(void)
156 {
157 #if defined TIZEN_TETHERING_ENABLE
158         int rv = 0;
159         const char *path = WLAN_DRIVER_SCRIPT;
160         char *const args[] = { "/usr/bin/wlan.sh", "stop", NULL };
161         char *const envs[] = { NULL };
162
163         rv = netconfig_interface_down(WLAN_IFACE_NAME);
164         if (rv != TRUE)
165                 return -EIO;
166
167         rv = netconfig_execute_file(path, args, envs);
168         if (rv < 0)
169                 return -EIO;
170
171         DBG("Successfully removed softap device driver");
172         return 0;
173 #else
174         return -ENODEV;
175 #endif
176 }
177
178 static int __netconfig_wifi_firmware_start(enum netconfig_wifi_firmware type)
179 {
180         if (emulator_is_emulated() == TRUE)
181                 return -EIO;
182
183         switch (type) {
184         case NETCONFIG_WIFI_STA:
185                 return __netconfig_sta_firmware_start();
186         case NETCONFIG_WIFI_P2P:
187                 return __netconfig_p2p_firmware_start();
188         case NETCONFIG_WIFI_SOFTAP:
189                 return __netconfig_softap_firmware_start();
190         default:
191                 break;
192         }
193
194         return -ENXIO;
195 }
196
197 static int __netconfig_wifi_firmware_stop(enum netconfig_wifi_firmware type)
198 {
199         if (emulator_is_emulated() == TRUE)
200                 return -EIO;
201
202         switch (type) {
203         case NETCONFIG_WIFI_STA:
204                 return __netconfig_sta_firmware_stop();
205         case NETCONFIG_WIFI_P2P:
206                 return __netconfig_p2p_firmware_stop();
207         case NETCONFIG_WIFI_SOFTAP:
208                 return __netconfig_softap_firmware_stop();
209         default:
210                 break;
211         }
212
213         return -ENXIO;
214 }
215
216 int netconfig_wifi_firmware(enum netconfig_wifi_firmware type, gboolean enable)
217 {
218         int err;
219         static enum netconfig_wifi_firmware current_driver = NETCONFIG_WIFI_OFF;
220         enum netconfig_wifi_firmware alias = type;
221
222 #if defined WLAN_CONCURRENT_MODE
223         int flight_mode = 0;
224
225         if (type == NETCONFIG_WIFI_P2P)
226                 alias = NETCONFIG_WIFI_STA;
227 #endif
228
229         DBG("Wi-Fi current firmware %d (type: %d %s)", current_driver, type,
230                                                         enable == TRUE ? "enable" : "disable");
231
232         if (enable == FALSE) {
233                 if (current_driver == NETCONFIG_WIFI_OFF) {
234                         return -EALREADY;
235                 } else if (current_driver == alias) {
236
237 #if defined WLAN_CONCURRENT_MODE
238                         netconfig_vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &flight_mode);
239                         if (flight_mode == 0 && type == NETCONFIG_WIFI_STA &&
240                                         netconfig_is_wifi_direct_on() == TRUE) {
241                                 netconfig_interface_down(WIFI_IFNAME);
242
243                                 return -EALREADY;
244                         }
245
246                         if (type == NETCONFIG_WIFI_P2P && wifi_state_get_technology_state() > NETCONFIG_WIFI_TECH_OFF) {
247                                 netconfig_interface_down(WLAN_P2P_IFACE_NAME);
248
249                                 return -EALREADY;
250                         }
251 #endif
252                         err = __netconfig_wifi_firmware_stop(type);
253                         if (err < 0 && err != -EALREADY)
254                                 return err;
255
256                         current_driver = NETCONFIG_WIFI_OFF;
257
258                         return err;
259                 }
260
261                 return -EIO;
262         }
263
264         if (current_driver > NETCONFIG_WIFI_OFF) {
265                 if (current_driver == alias) {
266
267 #if defined WLAN_CONCURRENT_MODE
268                         if (type == NETCONFIG_WIFI_STA)
269                                 netconfig_interface_up(WIFI_IFNAME);
270 #if defined TIZEN_P2P_ENABLE
271                         else if (type == NETCONFIG_WIFI_P2P)
272                                 netconfig_interface_up(WLAN_P2P_IFACE_NAME);
273 #endif
274 #endif
275                         return -EALREADY;
276                 }
277
278                 return -EIO;
279         }
280
281         err = __netconfig_wifi_firmware_start(type);
282         if (err < 0)
283                 DBG("Failed to execute script file");
284         else
285                 current_driver = alias;
286
287         return err;
288 }
289
290 gboolean handle_start(WifiFirmware *firmware, GDBusMethodInvocation *context, const gchar *device)
291 {
292         int err;
293
294         g_return_val_if_fail(firmware != NULL, FALSE);
295
296         DBG("Wi-Fi firmware start %s", device != NULL ? device : "null");
297
298         if (g_strcmp0("p2p", device) == 0)
299                 err = netconfig_wifi_firmware(NETCONFIG_WIFI_P2P, TRUE);
300         else if (g_strcmp0("softap", device) == 0)
301                 err = netconfig_wifi_firmware(NETCONFIG_WIFI_SOFTAP, TRUE);
302         else
303                 err = -EINVAL;
304
305         if (err < 0) {
306                 if (err == -EALREADY)
307                         netconfig_error_already_exists(context);
308                 else if (g_strcmp0("softap", device) == 0 && err == -EIO && netconfig_is_wifi_direct_on() == FALSE) {
309                         if (netconfig_wifi_firmware(NETCONFIG_WIFI_P2P, FALSE) == 0 && netconfig_wifi_firmware(NETCONFIG_WIFI_SOFTAP, TRUE) == 0) {
310                                 wifi_firmware_complete_start(firmware, context);
311                                 return TRUE;
312                         } else
313                                 netconfig_error_wifi_driver_failed(context);
314                 } else
315                         netconfig_error_wifi_driver_failed(context);
316
317                 return FALSE;
318         }
319
320         wifi_firmware_complete_start(firmware, context);
321         return TRUE;
322 }
323
324 gboolean handle_stop(WifiFirmware *firmware, GDBusMethodInvocation *context, const gchar *device)
325 {
326         int err;
327
328         g_return_val_if_fail(firmware != NULL, FALSE);
329
330         DBG("Wi-Fi firmware stop %s", device != NULL ? device : "null");
331
332         if (g_strcmp0("p2p", device) == 0)
333                 err = netconfig_wifi_firmware(NETCONFIG_WIFI_P2P, FALSE);
334         else if (g_strcmp0("softap", device) == 0)
335                 err = netconfig_wifi_firmware(NETCONFIG_WIFI_SOFTAP, FALSE);
336         else
337                 err = -EINVAL;
338
339         if (err < 0) {
340                 if (err == -EALREADY)
341                         netconfig_error_already_exists(context);
342                 else
343                         netconfig_error_wifi_driver_failed(context);
344
345                 return FALSE;
346         }
347
348         wifi_firmware_complete_stop(firmware, context);
349         return TRUE;
350 }