8e2f2601340e315fee2c538d1e73fa9368ee3429
[framework/connectivity/libnet-client.git] / src / network-wifi-intf.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.tizenopensource.org/license
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 #ifdef __cplusplus
19 extern "C"
20 {
21 #endif /* __cplusplus */
22
23
24 /*****************************************************************************
25  *      Standard headers
26  *****************************************************************************/
27 #include <stdio.h> 
28 #include <errno.h> 
29 #include <stdlib.h> 
30 #include <string.h>
31 #include <glib.h>
32
33 #include <vconf.h>
34 #include <dbus/dbus.h> 
35
36 /*****************************************************************************
37  *      Platform headers
38  *****************************************************************************/
39
40 #include "network-internal.h"
41 #include "network-signal-handler.h"
42 #include "network-dbus-request.h"
43
44 /*****************************************************************************
45  *      Macros and Typedefs
46  *****************************************************************************/
47
48 /*****************************************************************************
49  *      Local Functions Declaration
50  *****************************************************************************/
51
52 static net_wifi_state_t __net_get_wifi_service_state();
53
54 /*****************************************************************************
55  *      Global Functions
56  *****************************************************************************/
57
58
59 /*****************************************************************************
60  *      Extern Variables
61  *****************************************************************************/
62
63 extern network_info_t NetworkInfo;
64 extern network_request_table_t request_table[NETWORK_REQUEST_TYPE_MAX];
65
66 /*****************************************************************************
67  *      Global Variables
68  *****************************************************************************/
69
70 /*****************************************************************************
71  *      Local Functions Definition
72  *****************************************************************************/
73
74 static net_wifi_state_t __net_get_wifi_service_state(char *profile_name)
75 {
76         __NETWORK_FUNC_ENTER__;
77
78         net_err_t Error = NET_ERR_NONE;
79         net_wifi_state_t wifi_state = NetworkInfo.wifi_state;
80         int profile_count = 0;
81         int i = 0;
82         net_profile_info_t* profile_info = NULL;
83
84         Error = _net_get_profile_list(NET_DEVICE_WIFI, &profile_info, &profile_count);
85
86         if (Error != NET_ERR_NONE) {
87                 NETWORK_LOG(NETWORK_ERROR,
88                                 "Error!!! failed to get service(profile) list. Error [%s]\n",
89                                 _net_print_error(Error));
90
91                 NET_MEMFREE(profile_info);
92
93                 __NETWORK_FUNC_EXIT__;
94                 return wifi_state;
95         }
96
97         for (i = 0;i < profile_count;i++) {
98                 switch (profile_info->ProfileState) {
99                 case NET_STATE_TYPE_ASSOCIATION :
100                 case NET_STATE_TYPE_CONFIGURATION :
101                         wifi_state = WIFI_CONNECTING;
102                         g_strlcpy(profile_name, profile_info->ProfileName,
103                                         sizeof(profile_info->ProfileName));
104                         break;
105                 case NET_STATE_TYPE_READY :
106                 case NET_STATE_TYPE_ONLINE :
107                         wifi_state = WIFI_CONNECTED;
108                         g_strlcpy(profile_name, profile_info->ProfileName,
109                                         sizeof(profile_info->ProfileName));
110                         break;
111                 case NET_STATE_TYPE_UNKNOWN :
112                 case NET_STATE_TYPE_IDLE :
113                 case NET_STATE_TYPE_FAILURE :
114                 case NET_STATE_TYPE_DISCONNECT :
115                         break;
116                 }
117                 profile_info++;
118         }
119
120         if (wifi_state == WIFI_CONNECTED &&
121             request_table[NETWORK_REQUEST_TYPE_CLOSE_CONNECTION].flag == TRUE)
122                 wifi_state = WIFI_DISCONNECTING;
123
124         __NETWORK_FUNC_EXIT__;
125         return wifi_state;
126 }
127
128 /*****************************************************************************
129  *      ConnMan Wi-Fi Client Interface Async API Definition
130  *****************************************************************************/
131
132 EXPORT_API int net_open_connection_with_wifi_info(const net_wifi_connection_info_t *wifi_info)
133 {
134         __NETWORK_FUNC_ENTER__;
135
136         net_err_t Error = NET_ERR_NONE;
137
138         if (wifi_info == NULL) {
139                 NETWORK_LOG(NETWORK_ERROR, "Error!!! Invalid parameter\n");
140                 __NETWORK_FUNC_EXIT__;
141                 return NET_ERR_INVALID_PARAM;
142         }
143
144         if (NetworkInfo.ClientEventCb == NULL) {
145                 NETWORK_LOG(NETWORK_ERROR, "Error!!! Application was not registered\n");
146                 __NETWORK_FUNC_EXIT__;
147                 return NET_ERR_APP_NOT_REGISTERED;
148         }
149
150         if (_net_get_wifi_state() != WIFI_ON) {
151                 NETWORK_LOG( NETWORK_ERROR, "Error!!! wifi is powered off!\n");
152                 __NETWORK_FUNC_EXIT__;
153                 return NET_ERR_INVALID_OPERATION;
154         }
155
156         Error = _net_open_connection_with_wifi_info(wifi_info);
157         if (Error != NET_ERR_NONE) {
158                 NETWORK_LOG(NETWORK_ERROR, "Error!!! net_open_connection_with_wifi_info() failed. Error [%s]\n",
159                                 _net_print_error(Error));
160                 __NETWORK_FUNC_EXIT__;
161                 return Error;
162         }
163
164         __NETWORK_FUNC_EXIT__;
165         return NET_ERR_NONE;
166 }
167
168
169 EXPORT_API int net_wifi_power_on(void)
170 {
171         __NETWORK_FUNC_ENTER__;
172
173         net_err_t Error = NET_ERR_NONE;
174         int hotspot_state = 0;
175
176         if (NetworkInfo.ClientEventCb == NULL) {
177                 NETWORK_LOG(NETWORK_ERROR, "Error!!! Application was not registered\n");
178                 __NETWORK_FUNC_EXIT__;
179                 return NET_ERR_APP_NOT_REGISTERED;
180         }
181
182         vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_MODE, &hotspot_state);
183         if (hotspot_state & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI) {
184                 NETWORK_LOG(NETWORK_ERROR, "Error!!! Wi-Fi hotspot is enabled!\n");
185                 __NETWORK_FUNC_EXIT__;
186                 return NET_ERR_INVALID_OPERATION;
187         }
188
189         if (NetworkInfo.wifi_state != WIFI_OFF) {
190                 NETWORK_LOG(NETWORK_ERROR, "Error!!! wifi is powered on already!\n");
191                 __NETWORK_FUNC_EXIT__;
192                 return NET_ERR_INVALID_OPERATION;
193         }
194
195         if (request_table[NETWORK_REQUEST_TYPE_WIFI_POWER].flag == TRUE) {
196                 NETWORK_LOG(NETWORK_ASSERT, "Error!! Request already in progress\n");
197                 __NETWORK_FUNC_EXIT__;
198                 return NET_ERR_INVALID_OPERATION;
199         }
200
201         request_table[NETWORK_REQUEST_TYPE_WIFI_POWER].flag = TRUE;
202
203         Error = _net_dbus_load_wifi_driver();
204         if (Error != NET_ERR_NONE ) {
205                 NETWORK_LOG(NETWORK_EXCEPTION,
206                                 "Error!!! Failed to request wifi power on/off. Error [%s]\n",
207                                 _net_print_error(Error));
208                 memset(&request_table[NETWORK_REQUEST_TYPE_WIFI_POWER], 0, sizeof(network_request_table_t));
209                 __NETWORK_FUNC_EXIT__;
210                 return Error;
211         }
212
213         __NETWORK_FUNC_EXIT__;
214         return NET_ERR_NONE;
215 }
216
217
218 EXPORT_API int net_wifi_power_off(void)
219 {
220         __NETWORK_FUNC_ENTER__;
221
222         net_err_t Error = NET_ERR_NONE;
223
224         if(NetworkInfo.ClientEventCb == NULL) {
225                 NETWORK_LOG(NETWORK_ERROR, "Error!!! Application was not registered\n");
226                 __NETWORK_FUNC_EXIT__;
227                 return NET_ERR_APP_NOT_REGISTERED;
228         }
229
230         if(NetworkInfo.wifi_state == WIFI_OFF) {
231                 NETWORK_LOG(NETWORK_ERROR, "Error!!! wifi is powered off already!\n");
232                 __NETWORK_FUNC_EXIT__;
233                 return NET_ERR_INVALID_OPERATION;
234         }
235
236         if (request_table[NETWORK_REQUEST_TYPE_WIFI_POWER].flag == TRUE) {
237                 NETWORK_LOG(NETWORK_ASSERT, "Error!! Request already in progress\n");
238                 __NETWORK_FUNC_EXIT__;
239                 return NET_ERR_INVALID_OPERATION;
240         }
241
242         request_table[NETWORK_REQUEST_TYPE_WIFI_POWER].flag = TRUE;
243
244         Error = _net_dbus_remove_wifi_driver();
245         if (Error != NET_ERR_NONE ) {
246                 NETWORK_LOG(NETWORK_EXCEPTION,
247                                 "Error!!! Failed to request wifi power on/off. Error [%s]\n",
248                                 _net_print_error(Error));
249                 memset(&request_table[NETWORK_REQUEST_TYPE_WIFI_POWER], 0, sizeof(network_request_table_t));
250                 __NETWORK_FUNC_EXIT__;
251                 return Error;
252         }
253
254         NETWORK_LOG(NETWORK_HIGH, "Driver remove successfully\n");
255
256         __NETWORK_FUNC_EXIT__;
257         return NET_ERR_NONE;
258 }
259
260
261 EXPORT_API int net_scan_wifi(void)
262 {
263         __NETWORK_FUNC_ENTER__;
264
265         net_err_t Error = NET_ERR_NONE;
266
267         if(NetworkInfo.ClientEventCb == NULL) {
268                 NETWORK_LOG(NETWORK_ERROR, "Error!!! Application was not registered\n");
269                 __NETWORK_FUNC_EXIT__;
270                 return NET_ERR_APP_NOT_REGISTERED;
271         }
272
273         if(request_table[NETWORK_REQUEST_TYPE_SCAN].flag == TRUE) {
274                 NETWORK_LOG(NETWORK_ASSERT, "Error!! Request already in progress\n");
275                 __NETWORK_FUNC_EXIT__;
276                 return NET_ERR_INVALID_OPERATION;
277         }
278
279         if (_net_get_wifi_state() != WIFI_ON) {
280                 NETWORK_LOG( NETWORK_ERROR, "Error!!! wifi is powered off!\n");
281                 __NETWORK_FUNC_EXIT__;
282                 return NET_ERR_INVALID_OPERATION;
283         }
284
285         request_table[NETWORK_REQUEST_TYPE_SCAN].flag = TRUE;
286
287         if ((Error = _net_dbus_scan_request()) != NET_ERR_NONE ) {
288                 NETWORK_LOG(NETWORK_EXCEPTION,
289                                 "Error!!! Failed to request scan. Error [%s]\n",
290                                 _net_print_error(Error));
291                 memset(&request_table[NETWORK_REQUEST_TYPE_SCAN], 0, sizeof(network_request_table_t));
292                 __NETWORK_FUNC_EXIT__;
293                 return Error;;
294         }
295
296         NETWORK_LOG(NETWORK_HIGH, "ConnMan successfully finished Wi-Fi scanning\n");
297
298         __NETWORK_FUNC_EXIT__;
299         return NET_ERR_NONE;
300 }
301
302
303 EXPORT_API int net_wifi_enroll_wps(const char *profile_name, net_wifi_wps_info_t *wps_info)
304 {
305         __NETWORK_FUNC_ENTER__;
306
307         net_err_t Error = NET_ERR_NONE;
308
309         if (_net_check_profile_name(profile_name) != NET_ERR_NONE) {
310                 NETWORK_LOG(NETWORK_ERROR, "Error!!! Invalid ProfileName passed\n");
311                 __NETWORK_FUNC_EXIT__;
312                 return NET_ERR_INVALID_PARAM;
313         }
314
315         if (NetworkInfo.ClientEventCb == NULL) {
316                 NETWORK_LOG(NETWORK_ERROR, "Error!!! Application was not registered\n");
317                 __NETWORK_FUNC_EXIT__;
318                 return NET_ERR_APP_NOT_REGISTERED;
319         }
320
321         if (_net_get_wifi_state() != WIFI_ON) {
322                 NETWORK_LOG( NETWORK_ERROR, "Error!!! wifi is powered off!\n");
323                 __NETWORK_FUNC_EXIT__;
324                 return NET_ERR_INVALID_OPERATION;
325         }
326
327         if (request_table[NETWORK_REQUEST_TYPE_ENROLL_WPS].flag == TRUE) {
328                 NETWORK_LOG(NETWORK_ASSERT, "Error!! Request already in progress\n");
329                 __NETWORK_FUNC_EXIT__;
330                 return NET_ERR_INVALID_OPERATION;
331         }
332         
333         request_table[NETWORK_REQUEST_TYPE_ENROLL_WPS].flag = TRUE;
334         snprintf(request_table[NETWORK_REQUEST_TYPE_ENROLL_WPS].ProfileName,
335                         NET_PROFILE_NAME_LEN_MAX+1, "%s", profile_name);
336
337         if(wps_info->type == WIFI_WPS_PBC) {
338                 Error = _net_dbus_open_connection(profile_name);
339                 if(Error != NET_ERR_NONE)
340                 {
341                         NETWORK_LOG(NETWORK_ERROR,
342                                         "Error!! Failed to request open connection, Error [%s]\n", 
343                                         _net_print_error(Error));
344                         
345                         memset(&request_table[NETWORK_REQUEST_TYPE_ENROLL_WPS], 0, sizeof(network_request_table_t));
346                         
347                         __NETWORK_FUNC_EXIT__;
348                         return Error;
349                 }
350         }
351         else if(wps_info->type == WIFI_WPS_PIN) {
352                 // TODO: handle wps pin
353         }
354         else {
355                 __NETWORK_FUNC_EXIT__;
356                 return NET_ERR_INVALID_PARAM;
357         }
358         
359         __NETWORK_FUNC_EXIT__;
360         return NET_ERR_NONE;
361 }
362
363 /*****************************************************************************
364  *      ConnMan Wi-Fi Client Interface Sync Function Definition
365  *****************************************************************************/
366
367
368 EXPORT_API int net_get_wifi_state(net_wifi_state_t *current_state, net_profile_name_t *profile_name)
369 {
370         __NETWORK_FUNC_ENTER__;
371
372         net_wifi_state_t wifi_state;
373
374         if (NetworkInfo.ClientEventCb == NULL) {
375                 NETWORK_LOG(NETWORK_ERROR, "Error!!! Application was not registered\n");
376                 __NETWORK_FUNC_EXIT__;
377                 return NET_ERR_APP_NOT_REGISTERED;
378         }
379
380         if (profile_name == NULL) {
381                 NETWORK_LOG(NETWORK_ERROR, "Error!!! profile_name is NULL\n");
382                 return NET_ERR_INVALID_PARAM;
383         }
384
385         memset(profile_name, 0, sizeof(net_profile_name_t));
386
387         wifi_state = _net_get_wifi_state();
388
389         if (wifi_state == WIFI_OFF) {
390                 *current_state = WIFI_OFF;
391                 __NETWORK_FUNC_EXIT__;
392                 return NET_ERR_NONE;
393         }
394
395         *current_state = __net_get_wifi_service_state(profile_name->ProfileName);
396         
397         NETWORK_LOG(NETWORK_HIGH, "current state : %d, profile name : %s\n",
398                         *current_state, profile_name->ProfileName);
399
400         __NETWORK_FUNC_EXIT__;
401         return NET_ERR_NONE;
402 }
403
404
405 EXPORT_API int net_wifi_set_background_scan_mode(net_wifi_background_scan_mode_t scan_mode)
406 {
407         __NETWORK_FUNC_ENTER__;
408
409         net_err_t Error = NET_ERR_NONE;
410                 
411         if (NetworkInfo.ClientEventCb == NULL) {
412                 NETWORK_LOG(NETWORK_ERROR, "Error!!! Application not registered\n");
413                 __NETWORK_FUNC_EXIT__;
414                 return NET_ERR_APP_NOT_REGISTERED;
415         }
416
417         if (_net_get_wifi_state() != WIFI_ON) {
418                 NETWORK_LOG( NETWORK_ERROR, "Error!!! wifi is powered off!\n");
419                 __NETWORK_FUNC_EXIT__;
420                 return NET_ERR_INVALID_OPERATION;
421         }
422
423         NETWORK_LOG(NETWORK_HIGH,  "BGScan Mode [%d]\n", scan_mode);
424
425         if ((Error = _net_dbus_set_bgscan_mode(scan_mode)) != NET_ERR_NONE) {
426                 NETWORK_LOG(NETWORK_EXCEPTION,
427                                 "Error!!! Failed to set bgscan mode. Error [%s]\n",
428                                 _net_print_error(Error));
429
430                 __NETWORK_FUNC_EXIT__;
431                 return Error;
432         }
433                 
434         NETWORK_LOG(NETWORK_HIGH, "Set BGScan mode Request to connman is successfull\n");
435         
436         __NETWORK_FUNC_EXIT__;  
437         return NET_ERR_NONE;
438 }
439
440
441 #ifdef __cplusplus
442 }
443 #endif /* __cplusplus */
444