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 ******************************************************************/
21 #include "networkhandler.h"
27 #include "easysetup.h"
28 #include "oic_string.h"
30 #define LOG_TAG "TIZEN ES"
32 const char *gSsid = "DLNA_LISMORE1";
33 const char *gPass = "dlna@010203";
34 char *gIpAddress = NULL;
35 wifi_ap_h connectedWifi;
36 NetworkEventCallback gNetworkEventCb;
37 static void ESActivateWifi();
40 print_state(wifi_connection_state_e state)
44 case WIFI_CONNECTION_STATE_DISCONNECTED:
45 return "Disconnected";
46 case WIFI_CONNECTION_STATE_ASSOCIATION:
48 case WIFI_CONNECTION_STATE_CONNECTED:
50 case WIFI_CONNECTION_STATE_CONFIGURATION:
51 return "Configuration";
55 void __wifi_connected_cb(wifi_error_e error_code, void *user_data)
57 OIC_LOG(INFO,LOG_TAG,"#### __connected ");
62 wifi_ap_get_ip_address(connectedWifi, WIFI_ADDRESS_FAMILY_IPV4, &gIpAddress);
63 OIC_LOG_V(INFO,LOG_TAG,"#### __connected, Ipaddress=%s", gIpAddress);
64 gNetworkEventCb(ES_OK);
68 bool __wifi_found_ap_cb(wifi_ap_h ap, void *user_data)
70 OIC_LOG(INFO,LOG_TAG,"#### __wifi_found_ap_cb received ");
74 wifi_connection_state_e state;
76 error_code = wifi_ap_get_essid(ap, &ap_name);
77 if (error_code != WIFI_ERROR_NONE)
79 OIC_LOG(ERROR,LOG_TAG,"#### Fail to get AP name.");
83 error_code = wifi_ap_get_connection_state(ap, &state);
84 if (error_code != WIFI_ERROR_NONE)
86 OIC_LOG(ERROR,LOG_TAG,"#### Fail to get state.");
91 OIC_LOG_V(INFO,LOG_TAG,"#### AP name : %s, state : %s", ap_name, print_state(state));
93 if (strcmp(ap_name, gSsid) == 0)
95 OIC_LOG(INFO,LOG_TAG,"#### network found");
96 wifi_ap_set_passphrase(ap, gPass);
98 error_code = wifi_connect(ap, __wifi_connected_cb, NULL);
99 OIC_LOG_V(INFO,LOG_TAG,"Code=%d", error_code);
101 OIC_LOG(INFO,LOG_TAG,"#### __wifi_found_ap_cb received ");
105 void __scan_request_cb(wifi_error_e error_code, void *user_data)
107 OIC_LOG(INFO, LOG_TAG, "__scan_request_cb");
109 error_code1 = wifi_foreach_found_aps(__wifi_found_ap_cb, NULL);
110 if (error_code1 != WIFI_ERROR_NONE)
111 OIC_LOG(INFO,LOG_TAG,"#### Fail to scan");
113 OIC_LOG(INFO, LOG_TAG,"#### __scan_request_cb exit ");
116 static void __wifi_activated_cb(wifi_error_e result, void *user_data)
118 OIC_LOG(INFO, LOG_TAG, "__wifi_activated_cb");
119 if (result == WIFI_ERROR_NONE)
121 OIC_LOG(INFO,LOG_TAG,"#### Success to activate Wi-Fi device!");
123 wifi_scan(__scan_request_cb, NULL);
126 static void ESActivateWifi()
129 error_code = wifi_initialize();
130 OIC_LOG_V(INFO,LOG_TAG,"#### WIFI INITIALIZED WITH STATUS :%d", error_code);
132 error_code = wifi_activate(__wifi_activated_cb, NULL);
133 OIC_LOG_V(INFO,LOG_TAG,"#### WIFI ACTIVATED WITH STATUS :%d", error_code);
135 bool wifi_activated = false;
136 wifi_is_activated(&wifi_activated);
139 OIC_LOG(INFO,LOG_TAG,"#### Success to get Wi-Fi device state.");
140 int scan_result = wifi_scan(__scan_request_cb, NULL);
141 OIC_LOG_V(INFO,LOG_TAG,"#### Wifi scan result:%d", scan_result);
145 OIC_LOG(ERROR,LOG_TAG, "#### Fail to get Wi-Fi device state.");
151 OIC_LOG(INFO, LOG_TAG, "START");
155 void ConnectToWiFiNetwork(const char *ssid, const char *pass, NetworkEventCallback cb)
157 OIC_LOG_V(INFO, LOG_TAG, "ConnectToWiFiNetwork %s %s",ssid,pass);
160 gNetworkEventCb = cb;
164 ESResult getCurrentNetworkInfo(OCConnectivityType targetType, NetworkInfo *info)
166 if (targetType == CT_ADAPTER_IP)
168 info->type = CT_ADAPTER_IP;
169 info->ipaddr = gIpAddress;
170 if (strlen(gSsid) <= MAXSSIDLEN)
172 OICStrcpy(info->ssid, sizeof(info->ssid), gSsid);