Add support of Firmware "Start/Stop" DBus Method
[platform/core/connectivity/net-config.git] / src / wifi-firmware.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2012-2013 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
21 #include <errno.h>
22 #include "log.h"
23 #include "util.h"
24 #include "netdbus.h"
25 #include "emulator.h"
26 #include "neterror.h"
27 #include "netsupplicant.h"
28 #include "wifi-firmware.h"
29 #include "network-statistics.h"
30
31
32 #define WLAN_P2P_IFACE_NAME                     "p2p0"
33
34 static int __netconfig_sta_firmware_start(void)
35 {
36         int rv = 0;
37
38         rv = netconfig_interface_up(WIFI_IFNAME);
39         if (!rv)
40                 return -EIO;
41
42         INFO("Successfully loaded wireless device driver");
43         return 0;
44 }
45
46 static int __netconfig_sta_firmware_stop(void)
47 {
48
49         int rv = 0;
50
51         /* Update statistics before driver remove */
52         netconfig_wifi_statistics_update_powered_off();
53
54         rv = netconfig_interface_down(WIFI_IFNAME);
55         if (!rv)
56                 return -EIO;
57
58         INFO("Successfully removed wireless device driver");
59         return 0;
60 }
61
62
63 static int __netconfig_p2p_firmware_start(void)
64 {
65
66 #if defined TIZEN_P2P_ENABLE
67         int rv = 0;
68         rv = netconfig_interface_up(WLAN_P2P_IFACE_NAME);
69         if (!rv)
70                 return -EIO;
71
72         INFO("Successfully up  p2p device driver");
73     return 0;
74 #else
75         INFO("P2P Device is not supported");
76         return -ENODEV;
77 #endif
78
79 }
80
81 static int __netconfig_p2p_firmware_stop(void)
82 {
83
84 #if defined TIZEN_P2P_ENABLE
85         int rv = 0;
86         rv = netconfig_interface_down(WLAN_P2P_IFACE_NAME);
87         if (!rv)
88                 return -EIO;
89
90         INFO("Successfully down  p2p device driver");
91     return 0;
92 #else
93         INFO("P2P Device is not supported");
94         return -ENODEV;
95 #endif
96
97 }
98 static int __netconfig_wifi_firmware_start(
99                         enum netconfig_wifi_firmware type)
100 {
101
102         INFO(" %d",type);
103         if (netconfig_emulator_is_emulated() == TRUE)
104                 return -EIO;
105
106         switch (type) {
107         case NETCONFIG_WIFI_STA:
108                 return __netconfig_sta_firmware_start();
109         case NETCONFIG_WIFI_P2P:
110                 return __netconfig_p2p_firmware_start();
111         default:
112                 break;
113         }
114
115         return -ENXIO;
116 }
117
118 static int __netconfig_wifi_firmware_stop(enum netconfig_wifi_firmware type)
119 {
120         if (netconfig_emulator_is_emulated() == TRUE)
121                 return -EIO;
122
123         switch (type) {
124         case NETCONFIG_WIFI_STA:
125                 return __netconfig_sta_firmware_stop();
126         case NETCONFIG_WIFI_P2P:
127                 return __netconfig_p2p_firmware_stop();
128         default:
129                 break;
130         }
131
132         return -ENXIO;
133 }
134
135 int netconfig_wifi_firmware(enum netconfig_wifi_firmware type, gboolean enable)
136 {
137         int err;
138         static enum netconfig_wifi_firmware current_driver = NETCONFIG_WIFI_OFF;
139         enum netconfig_wifi_firmware alias = type;
140
141 #if defined WLAN_CONCURRENT_MODE
142         int flight_mode = 0;
143
144         if (type == NETCONFIG_WIFI_P2P)
145                 alias = NETCONFIG_WIFI_STA;
146 #endif
147
148         INFO("Wi-Fi current firmware %d (type: %d %s)", current_driver, type,
149                                                         enable == TRUE ? "enable" : "disable");
150
151         if (enable == FALSE) {
152                 if (current_driver == NETCONFIG_WIFI_OFF) {
153                         return -EALREADY;
154                 } else if (current_driver == alias) {
155
156 #if defined WLAN_CONCURRENT_MODE
157                         vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &flight_mode);
158
159                         if (flight_mode == 0 && type == NETCONFIG_WIFI_STA &&
160                                         netconfig_is_wifi_direct_on() == TRUE) {
161                                 netconfig_interface_down(WIFI_IFNAME);
162
163                                 return -EALREADY;
164                         }
165
166                         if (type == NETCONFIG_WIFI_P2P &&
167                                         netconfig_wifi_state_get_technology_state() >
168                                                 NETCONFIG_WIFI_TECH_OFF) {
169                                 netconfig_interface_down(WLAN_P2P_IFACE_NAME);
170
171                                 return -EALREADY;
172                         }
173 #endif
174                         err = __netconfig_wifi_firmware_stop(type);
175                         if (err < 0 && err != -EALREADY)
176                                 return err;
177
178                         current_driver = NETCONFIG_WIFI_OFF;
179
180                         return err;
181                 }
182
183                 return -EIO;
184         }
185
186         if (current_driver > NETCONFIG_WIFI_OFF) {
187                 if (current_driver == alias) {
188
189 #if defined WLAN_CONCURRENT_MODE
190                         if (type == NETCONFIG_WIFI_STA)
191                                 netconfig_interface_up(WIFI_IFNAME);
192 #if defined TIZEN_P2P_ENABLE
193                         else if (type == NETCONFIG_WIFI_P2P)
194                                 netconfig_interface_up(WLAN_P2P_IFACE_NAME);
195 #endif
196 #endif
197                         return -EALREADY;
198                 }
199
200                 return -EIO;
201         }
202
203         err = __netconfig_wifi_firmware_start(type);
204         if (err < 0)
205                 INFO("Failed to start the Firmware");
206         else
207                 current_driver = alias;
208
209         return err;
210 }
211
212 gboolean netconfig_iface_wifi_start(
213                 NetconfigWifi *wifi, gchar *device, GError **error)
214 {
215         int err;
216
217         g_return_val_if_fail(wifi != NULL, FALSE);
218
219         INFO("Wi-Fi firmware start %s", device != NULL ? device : "null");
220
221         if (g_strcmp0("p2p", device) == 0)
222                 err = netconfig_wifi_firmware(NETCONFIG_WIFI_P2P, TRUE);
223         else if (g_strcmp0("softap", device) == 0)
224                 err = netconfig_wifi_firmware(NETCONFIG_WIFI_SOFTAP, TRUE);
225         else
226                 err = -EINVAL;
227
228         if (err < 0) {
229                 if (err == -EALREADY)
230                         netconfig_error_already_exists(error);
231                 else
232                         netconfig_error_wifi_driver_failed(error);
233
234                 return FALSE;
235         }
236
237         return TRUE;
238 }
239
240 gboolean netconfig_iface_wifi_stop(
241                 NetconfigWifi *wifi, gchar *device, GError **error)
242 {
243         int err;
244
245         g_return_val_if_fail(wifi != NULL, FALSE);
246
247         INFO("Wi-Fi firmware stop %s", device != NULL ? device : "null");
248
249         if (g_strcmp0("p2p", device) == 0)
250                 err = netconfig_wifi_firmware(NETCONFIG_WIFI_P2P, FALSE);
251         else if (g_strcmp0("softap", device) == 0)
252                 err = netconfig_wifi_firmware(NETCONFIG_WIFI_SOFTAP, FALSE);
253         else
254                 err = -EINVAL;
255
256         if (err < 0) {
257                 if (err == -EALREADY)
258                         netconfig_error_already_exists(error);
259                 else
260                         netconfig_error_wifi_driver_failed(error);
261
262                 return FALSE;
263         }
264
265         return TRUE;
266 }