1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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"
23 // Arduino WiFi Shield includes
34 * @brief Logging tag for module name.
36 #define ES_NH_TAG "ES_NH"
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
42 * ES_MAX_NETWORK_RETRY sets the default number of retry count for network connection.
44 #define ES_MAX_NETWORK_RETRY (5)
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
49 static IPAddress enrolleeIP;
53 * @brief Retry counter for cancelling network retry. Currently network retry is
54 * limited to 5 attempts
56 static uint16_t g_retryCounter = 0;
60 //-----------------------------------------------------------------------------
61 // Private internal function prototypes
62 //-----------------------------------------------------------------------------
63 int findNetwork(const char *ssid);
64 int ConnectToNetwork(const char *ssid, const char *pass);
65 void printEncryptionType(int thisType);
67 // Arduino WiFi Shield
68 // Note : Arduino WiFi Shield currently does NOT support multicast and therefore
69 // this server will NOT be listening on 224.0.1.187 multicast address.
70 static const char ARDUINO_WIFI_SHIELD_UDP_FW_VER[] = "1.1.0";
72 ESResult ConnectToWiFiNetwork(const char *ssid, const char *pass,
73 ESEnrolleeNetworkEventCallback cb)
76 int status = WL_IDLE_STATUS;
79 // check for the presence of the shield:
80 if (WiFi.status() == WL_NO_SHIELD)
82 OIC_LOG(ERROR, ES_NH_TAG, "WiFi shield not present");
86 // Verify that WiFi Shield is running the firmware with all UDP fixes
87 fwVersion = WiFi.firmwareVersion();
88 OIC_LOG_V(INFO, ES_NH_TAG, "WiFi Shield Firmware version %s", fwVersion);
89 if (strncmp(fwVersion, ARDUINO_WIFI_SHIELD_UDP_FW_VER, sizeof(ARDUINO_WIFI_SHIELD_UDP_FW_VER))
92 OIC_LOG(DEBUG, ES_NH_TAG, "!!!!! Upgrade WiFi Shield Firmware version !!!!!!");
96 //Retry counter is reset everytime the ConnectToWiFiNetwork is invoked
99 OIC_LOG_V(INFO, ES_NH_TAG, "Finding SSID: %s", ssid);
101 while ((findNetwork(ssid) == 0) && g_retryCounter < ES_MAX_NETWORK_RETRY) // found
107 if(g_retryCounter == ES_MAX_NETWORK_RETRY){
108 OIC_LOG_V(ERROR, ES_NH_TAG, "Connection to network failed after %d attempts",
118 if (WiFi.status() == WL_CONNECTED)
121 //Retry counter is reset everytime the ConnectToWiFiNetwork is invoked
124 res = ConnectToNetwork(ssid, pass);
128 return ES_NETWORKCONNECTED;
132 return ES_NETWORKNOTCONNECTED;
136 int findNetwork(const char *ssid)
139 // scan for nearby networks:
140 Serial.println("** Scan Networks **");
141 int numSsid = WiFi.scanNetworks();
144 Serial.println("Couldn't get a wifi connection");
149 // print the list of networks seen:
150 Serial.print("number of available networks:");
151 Serial.println(numSsid);
153 // print the network number and name for each network found:
154 for (int thisNet = 0; thisNet < numSsid; thisNet++)
156 Serial.print(thisNet);
158 Serial.print(WiFi.SSID(thisNet));
159 Serial.print("\tEncryption: ");
160 printEncryptionType(WiFi.encryptionType(thisNet));
162 if (strcmp(WiFi.SSID(thisNet), ssid) == 0)
171 int ConnectToNetwork(const char *ssid, const char *pass)
173 int status = WL_IDLE_STATUS;
175 // attempt to connect to Wifi network:
176 while (status != WL_CONNECTED && g_retryCounter < ES_MAX_NETWORK_RETRY)
178 OIC_LOG_V(INFO, ES_NH_TAG, "Attempting to connect to SSID: %s", ssid);
180 status = WiFi.begin((char *) ssid, (char *) pass);
182 // wait 10 seconds for connection:
188 if(g_retryCounter == ES_MAX_NETWORK_RETRY){
189 OIC_LOG_V(ERROR, ES_NH_TAG, "Connection to network failed after %d attempts",
194 OIC_LOG(DEBUG, ES_NH_TAG, "Connected to wifi");
196 enrolleeIP = WiFi.localIP();
197 OIC_LOG_V(INFO, ES_NH_TAG, "IP Address: %d.%d.%d.%d", enrolleeIP[0], enrolleeIP[1],
198 enrolleeIP[2], enrolleeIP[3]);
201 sprintf(buf, "IP Address: %d.%d.%d.%d", enrolleeIP[0], enrolleeIP[1],
202 enrolleeIP[2], enrolleeIP[3]);
208 ESResult getCurrentNetworkInfo(OCConnectivityType targetType, NetworkInfo *info)
210 if (targetType == CT_ADAPTER_IP && WiFi.status() == WL_CONNECTED)
212 info->type = CT_ADAPTER_IP;
213 info->ipaddr = WiFi.localIP();
214 if((sizeof(info->ssid) >= MAXSSIDLEN) && (strlen(WiFi.SSID()) <= MAXSSIDLEN))
216 strcpy(info->ssid, WiFi.SSID());
228 void printEncryptionType(int thisType)
230 // read the encryption type and print out the name:
234 Serial.println("WEP");
237 Serial.println("WPA");
240 Serial.println("WPA2");
243 Serial.println("None");
246 Serial.println("Auto");