2 * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "sa_common.h"
21 #include "sa_network.h"
22 #include "net_connection.h"
23 #include "wifi-manager.h"
26 static const char *__print_wifi_state(connection_wifi_state_e state)
29 case CONNECTION_WIFI_STATE_DEACTIVATED:
31 case CONNECTION_WIFI_STATE_DISCONNECTED:
32 return "Disconnected";
33 case CONNECTION_WIFI_STATE_CONNECTED:
40 static const char *__print_error(connection_error_e 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";
76 return "CONNECTION_ERROR_UNKNOWN";
80 static const char *__print_connection_type(connection_type_e type)
83 case CONNECTION_TYPE_DISCONNECTED:
84 return "Disconnected";
85 case CONNECTION_TYPE_WIFI:
87 case CONNECTION_TYPE_CELLULAR:
89 case CONNECTION_TYPE_ETHERNET:
91 case CONNECTION_TYPE_BT:
93 case CONNECTION_TYPE_NET_PROXY:
100 static const char *__print_wifi_error(wifi_manager_error_e err_type)
103 case WIFI_MANAGER_ERROR_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:
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";
159 static int __check_wifi_state(connection_h connection, connection_wifi_state_e wifi_state)
163 if (connection == NULL) {
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));
171 _D("Retval = [%s] WiFi state [%s]", __print_error(rv), __print_wifi_state(wifi_state));
177 static void __activated_callback(wifi_manager_error_e result, void *user_data)
179 if (result == WIFI_MANAGER_ERROR_NONE)
180 _D("Wi-Fi Activation Succeeded");
182 _D("Wi-Fi Activation Failed! error : %s", __print_wifi_error(result));
185 static void __scan_request_callback(wifi_manager_error_e error_code, void* user_data)
187 if (user_data != NULL)
188 _D("user_data : %s", (char *)user_data);
190 _D("Scan Completed from scan request, error code : %s", __print_wifi_error(error_code));
193 static int __compare_ap_name(const char *ap_name, const char *ap_name_part)
195 int ap_name_len = strlen(ap_name);
196 int ap_name_part_len = strlen(ap_name_part);
198 if (strncmp(ap_name, ap_name_part,
199 ap_name_len > ap_name_part_len ? ap_name_len : ap_name_part_len) == 0)
205 static void __connected_callback(wifi_manager_error_e result, void *user_data)
207 if (result == WIFI_MANAGER_ERROR_NONE)
208 _D("Wi-Fi Connection Succeeded");
210 _D("Wi-Fi Connection Failed! error : %s", __print_wifi_error(result));
213 static int __found_connect_ap_callback(wifi_manager_ap_h ap, void *user_data)
216 char *ap_name = NULL;
217 char *ap_name_part = (char*)user_data;
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));
225 if (__compare_ap_name(ap_name, ap_name_part)) {
226 int required = FALSE;
228 rv = wifi_manager_ap_is_passphrase_required(ap, &required);
230 if (rv == WIFI_MANAGER_ERROR_NONE) {
232 char passphrase[100];
233 _D("Input passphrase for %s : ", ap_name);
234 rv = scanf("%99s", passphrase);
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));
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));
249 _D("Success to connection request [%s]", ap_name);
261 static int __connect_wifi(sa_wifi_s *info)
264 wifi_manager_h wifi = NULL;
271 rv = wifi_manager_initialize(&wifi);
273 if (rv == WIFI_MANAGER_ERROR_NONE) {
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));
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));
291 rv = wifi_manager_scan(wifi, __scan_request_callback, NULL);
293 if (rv != WIFI_MANAGER_ERROR_NONE) {
294 _D("Scan request failed [%s]", __print_wifi_error(rv));
299 _D("AP : [%s]", info->ssid);
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));
308 _E("fail creation wifi-manager handler [%s]", __print_wifi_error(rv));
314 static int __connect_ethernet(sa_eth_s *info)
319 connection_h connection = NULL;
325 err = connection_create(&connection);
326 if (CONNECTION_ERROR_NONE == err) {
327 // check network cable
328 // check dhcp / static
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));
337 _E("fail creation connection handler [%s]\n", __print_error(err));
343 int sa_network_get_state(sa_network_state_e *conn_state, sa_network_type_e *conn_type)
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;
351 int err = connection_create(&connection);
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));
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;
366 if (!__check_wifi_state(connection, wifi_state)) {
367 if (wifi_state == CONNECTION_WIFI_STATE_CONNECTED) {
368 *conn_state = SA_NETWORK_STATE_CONNECTED;
370 *conn_state = SA_NETWORK_STATE_DISCONNECTED;
373 } else if (CONNECTION_TYPE_ETHERNET == net_state) {
374 *conn_type = SA_NETWORK_TYPE_ETH;
375 *conn_state = SA_NETWORK_STATE_CONNECTED;
377 *conn_state = SA_NETWORK_STATE_UNKNOWN;
378 *conn_type = SA_NETWORK_TYPE_NONE;
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));
390 _E("fail creation connection handler [%s]\n", __print_error(err));
396 int sa_network_activate(sa_network_s *info)
398 sa_error_e ret = SA_ERROR_NONE;
399 connection_wifi_state_e wifi_state;
404 return SA_ERROR_INVALID_PARAMETER;
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);
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);
418 // second priority is eth
419 if (info->eth->enabled == TRUE) {
420 retEth = __connect_ethernet(info->eth);
427 int sa_network_deactivate(void)
429 sa_error_e ret = SA_ERROR_NONE;