Change file name
[platform/core/system/setup-adaptor.git] / src / setup_network.c
1 /*
2  * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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 #include <stdio.h>
17 #include <string.h>
18 #include <stdlib.h>
19 #include "sa_common.h"
20 #include "sa_types.h"
21 #include "sa_network.h"
22 #include "net_connection.h"
23 #include "wifi-manager.h"
24
25
26 static const char *__print_wifi_state(connection_wifi_state_e state)
27 {
28         switch (state) {
29         case CONNECTION_WIFI_STATE_DEACTIVATED:
30                 return "Deactivated";
31         case CONNECTION_WIFI_STATE_DISCONNECTED:
32                 return "Disconnected";
33         case CONNECTION_WIFI_STATE_CONNECTED:
34                 return "Connected";
35         default:
36                 return "Unknown";
37         }
38 }
39
40 static const char *__print_error(connection_error_e error)
41 {
42         switch (error) {
43         case CONNECTION_ERROR_NONE:
44                 return "CONNECTION_ERROR_NONE";
45         case CONNECTION_ERROR_INVALID_PARAMETER:
46                 return "CONNECTION_ERROR_INVALID_PARAMETER";
47         case CONNECTION_ERROR_OUT_OF_MEMORY:
48                 return "CONNECTION_ERROR_OUT_OF_MEMORY";
49         case CONNECTION_ERROR_INVALID_OPERATION:
50                 return "CONNECTION_ERROR_INVALID_OPERATION";
51         case CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED:
52                 return "CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED";
53         case CONNECTION_ERROR_OPERATION_FAILED:
54                 return "CONNECTION_ERROR_OPERATION_FAILED";
55         case CONNECTION_ERROR_ITERATOR_END:
56                 return "CONNECTION_ERROR_ITERATOR_END";
57         case CONNECTION_ERROR_NO_CONNECTION:
58                 return "CONNECTION_ERROR_NO_CONNECTION";
59         case CONNECTION_ERROR_NOW_IN_PROGRESS:
60                 return "CONNECTION_ERROR_NOW_IN_PROGRESS";
61         case CONNECTION_ERROR_ALREADY_EXISTS:
62                 return "CONNECTION_ERROR_ALREADY_EXISTS";
63         case CONNECTION_ERROR_OPERATION_ABORTED:
64                 return "CONNECTION_ERROR_OPERATION_ABORTED";
65         case CONNECTION_ERROR_DHCP_FAILED:
66                 return "CONNECTION_ERROR_DHCP_FAILED";
67         case CONNECTION_ERROR_INVALID_KEY:
68                 return "CONNECTION_ERROR_INVALID_KEY";
69         case CONNECTION_ERROR_NO_REPLY:
70                 return "CONNECTION_ERROR_NO_REPLY";
71         case CONNECTION_ERROR_PERMISSION_DENIED:
72                 return "CONNECTION_ERROR_PERMISSION_DENIED";
73         case CONNECTION_ERROR_NOT_SUPPORTED:
74                 return "CONNECTION_ERROR_NOT_SUPPORTED";
75         default:
76                 return "CONNECTION_ERROR_UNKNOWN";
77         }
78 }
79
80 static const char *__print_connection_type(connection_type_e type)
81 {
82         switch (type) {
83         case CONNECTION_TYPE_DISCONNECTED:
84                 return "Disconnected";
85         case CONNECTION_TYPE_WIFI:
86                 return "Wifi";
87         case CONNECTION_TYPE_CELLULAR:
88                 return "Cellular";
89         case CONNECTION_TYPE_ETHERNET:
90                 return "Ethernet";
91         case CONNECTION_TYPE_BT:
92                 return "BT";
93         case CONNECTION_TYPE_NET_PROXY:
94                 return "Net_Proxy";
95         default:
96                 return "Unknown";
97         }
98 }
99
100 static const char *__print_wifi_error(wifi_manager_error_e err_type)
101 {
102         switch (err_type) {
103         case WIFI_MANAGER_ERROR_NONE:
104                 return "NONE";
105         case WIFI_MANAGER_ERROR_INVALID_PARAMETER:
106                 return "INVALID_PARAMETER";
107         case WIFI_MANAGER_ERROR_OUT_OF_MEMORY:
108                 return "OUT_OF_MEMORY";
109         case WIFI_MANAGER_ERROR_INVALID_OPERATION:
110                 return "INVALID_OPERATION";
111         case WIFI_MANAGER_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED:
112                 return "ADDRESS_FAMILY_NOT_SUPPORTED";
113         case WIFI_MANAGER_ERROR_OPERATION_FAILED:
114                 return "OPERATION_FAILED";
115         case WIFI_MANAGER_ERROR_NO_CONNECTION:
116                 return "NO_CONNECTION";
117         case WIFI_MANAGER_ERROR_NOW_IN_PROGRESS:
118                 return "NOW_IN_PROGRESS";
119         case WIFI_MANAGER_ERROR_ALREADY_EXISTS:
120                 return "ALREADY_EXISTS";
121         case WIFI_MANAGER_ERROR_OPERATION_ABORTED:
122                 return "OPERATION_ABORTED";
123         case WIFI_MANAGER_ERROR_DHCP_FAILED:
124                 return "DHCP_FAILED";
125         case WIFI_MANAGER_ERROR_INVALID_KEY:
126                 return "INVALID_KEY";
127         case WIFI_MANAGER_ERROR_OUT_OF_RANGE:
128                 return "OUT_OF_RANGE";
129         case WIFI_MANAGER_ERROR_PIN_MISSING:
130                 return "PIN_MISSING";
131         case WIFI_MANAGER_ERROR_CONNECT_FAILED:
132                 return "CONNECT_FAILED";
133         case WIFI_MANAGER_ERROR_LOGIN_FAILED:
134                 return "LOGIN_FAILED";
135         case WIFI_MANAGER_ERROR_AUTHENTICATION_FAILED:
136                 return "AUTH_FAILED";
137         case WIFI_MANAGER_ERROR_NO_REPLY:
138                 return "NO_REPLY";
139         case WIFI_MANAGER_ERROR_SECURITY_RESTRICTED:
140                 return "SECURITY_RESTRICTED";
141         case WIFI_MANAGER_ERROR_ALREADY_INITIALIZED:
142                 return "ALREADY_INITIALIZED";
143         case WIFI_MANAGER_ERROR_PERMISSION_DENIED:
144                 return "PERMISSION_DENIED";
145         case WIFI_MANAGER_ERROR_NOT_SUPPORTED:
146                 return "NOT_SUPPORTED";
147         case WIFI_MANAGER_ERROR_WPS_OVERLAP:
148                 return "WPS_OVERLAP";
149         case WIFI_MANAGER_ERROR_WPS_TIMEOUT:
150                 return "WPS_TIMEOUT";
151         case WIFI_MANAGER_ERROR_WPS_WEP_PROHIBITED:
152                 return "WPS_WEP_PROHIBITED";
153         default:
154                 return "UNKNOWN";
155         }
156 }
157
158
159 static int __check_wifi_state(connection_h connection, connection_wifi_state_e wifi_state)
160 {
161         int rv = 0;
162
163         if (connection == NULL) {
164                 return -1;
165         }
166
167         rv = connection_get_wifi_state(connection, &wifi_state);
168         if (rv != CONNECTION_ERROR_NONE) {
169                 _D("Fail to get WiFi state [%s]", __print_error(rv));
170         } else {
171                 _D("Retval = [%s] WiFi state [%s]", __print_error(rv), __print_wifi_state(wifi_state));
172         }
173
174         return 0;
175 }
176
177 static void __activated_callback(wifi_manager_error_e result, void *user_data)
178 {
179         if (result == WIFI_MANAGER_ERROR_NONE)
180                 _D("Wi-Fi Activation Succeeded");
181         else
182                 _D("Wi-Fi Activation Failed! error : %s", __print_wifi_error(result));
183 }
184
185 static void __scan_request_callback(wifi_manager_error_e error_code, void* user_data)
186 {
187         if (user_data != NULL)
188                 _D("user_data : %s", (char *)user_data);
189
190         _D("Scan Completed from scan request, error code : %s", __print_wifi_error(error_code));
191 }
192
193 static int __compare_ap_name(const char *ap_name, const char *ap_name_part)
194 {
195         int ap_name_len = strlen(ap_name);
196         int ap_name_part_len = strlen(ap_name_part);
197
198         if (strncmp(ap_name, ap_name_part,
199                                 ap_name_len > ap_name_part_len ? ap_name_len : ap_name_part_len) == 0)
200                 return TRUE;
201         else
202                 return FALSE;
203 }
204
205 static void __connected_callback(wifi_manager_error_e result, void *user_data)
206 {
207         if (result == WIFI_MANAGER_ERROR_NONE)
208                 _D("Wi-Fi Connection Succeeded");
209         else
210                 _D("Wi-Fi Connection Failed! error : %s", __print_wifi_error(result));
211 }
212
213 static int __found_connect_ap_callback(wifi_manager_ap_h ap, void *user_data)
214 {
215         int rv = 0;
216         char *ap_name = NULL;
217         char *ap_name_part = (char*)user_data;
218
219         rv = wifi_manager_ap_get_essid(ap, &ap_name);
220         if (rv != WIFI_MANAGER_ERROR_NONE) {
221                 _D("Fail to get AP name [%s]\n", __print_wifi_error(rv));
222                 return -1;
223         }
224
225         if (__compare_ap_name(ap_name, ap_name_part)) {
226                 int required = FALSE;
227
228                 rv = wifi_manager_ap_is_passphrase_required(ap, &required);
229                 
230                 if (rv == WIFI_MANAGER_ERROR_NONE) {
231                         if (required) {
232                                 char passphrase[100];
233                                 _D("Input passphrase for %s : ", ap_name);
234                                 rv = scanf("%99s", passphrase);
235
236                                 rv = wifi_manager_ap_set_passphrase(ap, passphrase);
237                                 if (rv != WIFI_MANAGER_ERROR_NONE) {
238                                         _D("Fail to set passphrase : %s", __print_wifi_error(rv));
239                                         if (ap_name != NULL) 
240                                                 free(ap_name);;
241                                         return false;
242                                 }
243                         }
244
245                         //rv = wifi_manager_connect(wifi, ap, __connected_callback, NULL);
246                         if (rv != WIFI_MANAGER_ERROR_NONE)
247                                 _D("Fail to connection request [%s] : %s", ap_name, __print_wifi_error(rv));
248                         else
249                                 _D("Success to connection request [%s]", ap_name);
250                 }
251         
252         }
253
254         if (ap_name != NULL) 
255                 free(ap_name);
256         return true;
257 }
258
259
260
261 static int __connect_wifi(sa_wifi_s *info)
262 {
263         int rv = 0;
264         wifi_manager_h wifi = NULL;
265         bool state = false;
266
267         if (info == NULL) {
268                 return -1;
269         }
270
271         rv = wifi_manager_initialize(&wifi);
272
273         if (rv == WIFI_MANAGER_ERROR_NONE) {
274                 // check wifi
275                 rv = wifi_manager_is_activated(wifi, &state);
276                 if (rv != WIFI_MANAGER_ERROR_NONE) {
277                         printf("Fail to get Wi-Fi device state [%s]", __print_wifi_error(rv));
278                         return -1;
279                 }
280
281                 // if wifi is not activated
282                 if (state == FALSE) {
283                         rv = wifi_manager_activate(wifi, __activated_callback, NULL);
284                         if (rv != WIFI_MANAGER_ERROR_NONE) {
285                                 printf("Fail to activate Wi-Fi device [%s]", __print_wifi_error(rv));
286                                 return -1;
287                         }
288                 } 
289                 
290                 // scan ap
291                 rv = wifi_manager_scan(wifi, __scan_request_callback, NULL);
292
293                 if (rv != WIFI_MANAGER_ERROR_NONE) {
294                         _D("Scan request failed [%s]", __print_wifi_error(rv));
295                         return -1;
296                 }
297
298                 // connect ap
299                 _D("AP : [%s]", info->ssid);
300
301                 rv = wifi_manager_foreach_found_ap(wifi, __found_connect_ap_callback, info->ssid);
302                 if (rv != WIFI_MANAGER_ERROR_NONE) {
303                         _D("Fail to connect (can't get AP list) [%s]", __print_wifi_error(rv));
304                         return -1;
305                 }
306
307         } else {
308                 _E("fail creation wifi-manager handler [%s]", __print_wifi_error(rv));
309         }
310
311         return 0;
312 }
313
314 static int __connect_ethernet(sa_eth_s *info)
315 {
316         int rv = 0;
317         int err = 0;
318         int ret = 0;
319         connection_h connection = NULL;
320
321         if (info == NULL) {
322                 return -1;
323         }
324
325         err = connection_create(&connection);
326         if (CONNECTION_ERROR_NONE == err) {
327                 // check network cable
328                 // check dhcp / static
329                 // set ip
330                 rv = connection_destroy(connection);
331                 if (rv != CONNECTION_ERROR_NONE) {
332                         ret = SA_ERROR_UNKNOWN;
333                         printf("Fail to get network state [%s]\n", __print_error(rv));
334                 }
335
336         } else {
337                 _E("fail creation connection handler [%s]\n", __print_error(err));
338         }
339
340         return 0;
341 }
342
343 int sa_network_get_state(sa_network_state_e *conn_state, sa_network_type_e *conn_type)
344 {
345         sa_error_e ret = SA_ERROR_UNKNOWN;
346         connection_h connection = NULL;
347         connection_type_e net_state;
348         connection_wifi_state_e wifi_state;
349         int rv = 0;
350
351         int err = connection_create(&connection);
352
353         // check network state(eth/wifi)
354         if (CONNECTION_ERROR_NONE == err) {
355                 rv = connection_get_type(connection, &net_state);
356                 if (rv != CONNECTION_ERROR_NONE) {
357                         printf("Fail to get network state [%s]\n", __print_error(rv));
358                 } else {
359                         ret = SA_ERROR_NONE;
360                         _D("Retval = [%s] network connection state [%s]\n", __print_error(rv), __print_connection_type(net_state));     
361                         if (CONNECTION_TYPE_DISCONNECTED == net_state) {
362                                 *conn_state = SA_NETWORK_STATE_DISCONNECTED;
363                         }else if (CONNECTION_TYPE_WIFI == net_state) {
364                                 *conn_type = SA_NETWORK_TYPE_WIFI;
365                                 // check wifi state
366                                 if (!__check_wifi_state(connection, wifi_state)) {
367                                         if (wifi_state == CONNECTION_WIFI_STATE_CONNECTED) {
368                                                 *conn_state = SA_NETWORK_STATE_CONNECTED;
369                                         } else {
370                                                 *conn_state = SA_NETWORK_STATE_DISCONNECTED;
371                                         }
372                                 }
373                         } else if (CONNECTION_TYPE_ETHERNET == net_state) {
374                                 *conn_type = SA_NETWORK_TYPE_ETH;
375                                 *conn_state = SA_NETWORK_STATE_CONNECTED;
376                         } else {
377                                 *conn_state = SA_NETWORK_STATE_UNKNOWN;
378                                 *conn_type = SA_NETWORK_TYPE_NONE;
379                         }
380                 }
381
382                 // destroy connection
383                 rv = connection_destroy(connection);
384                 if (rv != CONNECTION_ERROR_NONE) {
385                         ret = SA_ERROR_UNKNOWN;
386                         printf("Fail to get network state [%s]\n", __print_error(rv));
387                 }
388
389         } else {
390                 _E("fail creation connection handler [%s]\n", __print_error(err));
391         }       
392
393         return ret;
394 }
395
396 int sa_network_activate(sa_network_s *info)
397 {
398         sa_error_e ret = SA_ERROR_NONE;
399         connection_wifi_state_e wifi_state;
400         int retWifi = 0;
401         int retEth = 0;
402
403         if (info == NULL) {
404                 return SA_ERROR_INVALID_PARAMETER;
405         }
406
407         // first priority is wifi
408         if (info->wifi->enabled == TRUE) {
409                 // wifi setting and waiting to get response
410                 retWifi = __connect_wifi(info->wifi);
411
412                 // or if wifi is enabled but fail to connect, try to connect eth
413                 if (wifi_state != CONNECTION_WIFI_STATE_CONNECTED && info->eth->enabled == TRUE) {
414                         retEth = __connect_ethernet(info->eth);
415                 }
416
417         } else {
418                 // second priority is eth
419                 if (info->eth->enabled == TRUE) {
420                         retEth = __connect_ethernet(info->eth);
421                 }
422         }       
423
424         return 0;
425 }
426
427 int sa_network_deactivate(void)
428 {
429         sa_error_e ret = SA_ERROR_NONE;
430
431         return ret;
432 }