1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
25 #include "easysetup.h"
26 #include "networkHandler.h"
28 #define LOG_TAG "TIZEN ES"
30 const char *gSsid = "DLNA_LISMORE1";
31 const char *gPass = "dlna@010203";
33 wifi_ap_h connectedWifi;
34 NetworkEventCallback gNetworkEventCb;
35 static void ESActivateWifi();
38 print_state(wifi_connection_state_e state)
42 case WIFI_CONNECTION_STATE_DISCONNECTED:
43 return "Disconnected";
44 case WIFI_CONNECTION_STATE_ASSOCIATION:
46 case WIFI_CONNECTION_STATE_CONNECTED:
48 case WIFI_CONNECTION_STATE_CONFIGURATION:
49 return "Configuration";
53 void __wifi_connected_cb(wifi_error_e error_code, void *user_data)
55 OC_LOG(INFO,LOG_TAG,"#### __connected ");
56 wifi_ap_get_ip_address(connectedWifi, WIFI_ADDRESS_FAMILY_IPV4, &gIpAddress);
57 OC_LOG_V(INFO,LOG_TAG,"#### __connected, Ipaddress=%s", gIpAddress);
58 gNetworkEventCb(ES_OK);
62 bool __wifi_found_ap_cb(wifi_ap_h ap, void *user_data)
64 OC_LOG(INFO,LOG_TAG,"#### __wifi_found_ap_cb received ");
68 wifi_connection_state_e state;
70 error_code = wifi_ap_get_essid(ap, &ap_name);
71 if (error_code != WIFI_ERROR_NONE)
73 OC_LOG(ERROR,LOG_TAG,"#### Fail to get AP name.");
77 error_code = wifi_ap_get_connection_state(ap, &state);
78 if (error_code != WIFI_ERROR_NONE)
80 OC_LOG(ERROR,LOG_TAG,"#### Fail to get state.");
84 OC_LOG_V(INFO,LOG_TAG,"#### AP name : %s, state : %s", ap_name, print_state(state));
86 if (strcmp(ap_name, gSsid) == 0)
88 OC_LOG(INFO,LOG_TAG,"#### network found");
89 wifi_ap_set_passphrase(ap, gPass);
91 error_code = wifi_connect(ap, __wifi_connected_cb, NULL);
92 OC_LOG_V(INFO,LOG_TAG,"Code=%d", error_code);
94 OC_LOG(INFO,LOG_TAG,"#### __wifi_found_ap_cb received ");
97 void __scan_request_cb(wifi_error_e error_code, void *user_data)
99 OC_LOG(INFO, LOG_TAG, "__scan_request_cb");
101 error_code1 = wifi_foreach_found_aps(__wifi_found_ap_cb, NULL);
102 if (error_code1 != WIFI_ERROR_NONE)
103 OC_LOG(INFO,LOG_TAG,"#### Fail to scan");
105 OC_LOG(INFO, LOG_TAG,"#### __scan_request_cb exit ");
108 static void __wifi_activated_cb(wifi_error_e result, void *user_data)
110 OC_LOG(INFO, LOG_TAG, "__wifi_activated_cb");
111 if (result == WIFI_ERROR_NONE)
113 OC_LOG(INFO,LOG_TAG,"#### Success to activate Wi-Fi device!");
115 wifi_scan(__scan_request_cb, NULL);
118 static void ESActivateWifi()
121 error_code = wifi_initialize();
122 OC_LOG_V(INFO,LOG_TAG,"#### WIFI INITIALIZED WITH STATUS :%d", error_code);
124 error_code = wifi_activate(__wifi_activated_cb, NULL);
125 OC_LOG_V(INFO,LOG_TAG,"#### WIFI ACTIVATED WITH STATUS :%d", error_code);
127 bool wifi_activated = false;
128 wifi_is_activated(&wifi_activated);
131 OC_LOG(INFO,LOG_TAG,"#### Success to get Wi-Fi device state.");
132 int scan_result = wifi_scan(__scan_request_cb, NULL);
133 OC_LOG_V(INFO,LOG_TAG,"#### Wifi scan result:%d", scan_result);
137 OC_LOG(ERROR,LOG_TAG, "#### Fail to get Wi-Fi device state.");
143 OC_LOG(INFO, LOG_TAG, "START");
147 void ConnectToWiFiNetwork(const char *ssid, const char *pass, NetworkEventCallback cb)
149 OC_LOG_V(INFO, LOG_TAG, "ConnectToWiFiNetwork %s %s",ssid,pass);
152 gNetworkEventCb = cb;
156 ESResult getCurrentNetworkInfo(OCConnectivityType targetType, NetworkInfo *info)
158 if (targetType == CT_ADAPTER_IP)
160 info->type = CT_ADAPTER_IP;
161 info->ipaddr = gIpAddress;
162 if (strlen(gSsid) <= MAXSSIDLEN)
164 strcpy(info->ssid, gSsid);