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 limited to 5 attempts
55 static uint16_t g_retryCounter = 0;
59 //-----------------------------------------------------------------------------
60 // Private internal function prototypes
61 //-----------------------------------------------------------------------------
62 int findNetwork(const char *ssid);
63 int ConnectToNetwork(const char *ssid, const char *pass);
64 void printEncryptionType(int thisType);
66 // Arduino WiFi Shield
67 // Note : Arduino WiFi Shield currently does NOT support multicast and therefore
68 // this server will NOT be listening on 224.0.1.187 multicast address.
69 static const char ARDUINO_WIFI_SHIELD_UDP_FW_VER[] = "1.1.0";
71 ESResult ConnectToWiFiNetwork(const char *ssid, const char *pass, NetworkEventCallback cb)
74 int status = WL_IDLE_STATUS;
77 // check for the presence of the shield:
78 if (WiFi.status() == WL_NO_SHIELD)
80 OC_LOG(ERROR, ES_NH_TAG, "WiFi shield not present");
84 // Verify that WiFi Shield is running the firmware with all UDP fixes
85 fwVersion = WiFi.firmwareVersion();
86 OC_LOG_V(INFO, ES_NH_TAG, "WiFi Shield Firmware version %s", fwVersion);
87 if (strncmp(fwVersion, ARDUINO_WIFI_SHIELD_UDP_FW_VER, sizeof(ARDUINO_WIFI_SHIELD_UDP_FW_VER))
90 OC_LOG(DEBUG, ES_NH_TAG, "!!!!! Upgrade WiFi Shield Firmware version !!!!!!");
94 //Retry counter is reset everytime the ConnectToWiFiNetwork is invoked
97 OC_LOG_V(INFO, ES_NH_TAG, "Finding SSID: %s", ssid);
99 while ((findNetwork(ssid) == 0) && g_retryCounter < ES_MAX_NETWORK_RETRY) // found
105 if(g_retryCounter == ES_MAX_NETWORK_RETRY){
106 OC_LOG_V(ERROR, ES_NH_TAG, "Connection to network failed after %d attempts",
116 if (WiFi.status() == WL_CONNECTED)
119 //Retry counter is reset everytime the ConnectToWiFiNetwork is invoked
122 res = ConnectToNetwork(ssid, pass);
126 return ES_NETWORKCONNECTED;
130 return ES_NETWORKNOTCONNECTED;
134 int findNetwork(const char *ssid)
137 // scan for nearby networks:
138 Serial.println("** Scan Networks **");
139 int numSsid = WiFi.scanNetworks();
142 Serial.println("Couldn't get a wifi connection");
147 // print the list of networks seen:
148 Serial.print("number of available networks:");
149 Serial.println(numSsid);
151 // print the network number and name for each network found:
152 for (int thisNet = 0; thisNet < numSsid; thisNet++)
154 Serial.print(thisNet);
156 Serial.print(WiFi.SSID(thisNet));
157 Serial.print("\tEncryption: ");
158 printEncryptionType(WiFi.encryptionType(thisNet));
160 if (strcmp(WiFi.SSID(thisNet), ssid) == 0)
169 int ConnectToNetwork(const char *ssid, const char *pass)
171 int status = WL_IDLE_STATUS;
173 // attempt to connect to Wifi network:
174 while (status != WL_CONNECTED && g_retryCounter < ES_MAX_NETWORK_RETRY)
176 OC_LOG_V(INFO, ES_NH_TAG, "Attempting to connect to SSID: %s", ssid);
178 status = WiFi.begin((char *) ssid, (char *) pass);
180 // wait 10 seconds for connection:
186 if(g_retryCounter == ES_MAX_NETWORK_RETRY){
187 OC_LOG_V(ERROR, ES_NH_TAG, "Connection to network failed after %d attempts",
192 OC_LOG(DEBUG, ES_NH_TAG, "Connected to wifi");
194 enrolleeIP = WiFi.localIP();
195 OC_LOG_V(INFO, ES_NH_TAG, "IP Address: %d.%d.%d.%d", enrolleeIP[0], enrolleeIP[1],
196 enrolleeIP[2], enrolleeIP[3]);
199 sprintf(buf, "IP Address: %d.%d.%d.%d", enrolleeIP[0], enrolleeIP[1],
200 enrolleeIP[2], enrolleeIP[3]);
206 ESResult getCurrentNetworkInfo(OCConnectivityType targetType, NetworkInfo *info)
208 if (targetType == CT_ADAPTER_IP && WiFi.status() == WL_CONNECTED)
210 info->type = CT_ADAPTER_IP;
211 info->ipaddr = WiFi.localIP();
212 if(strlen(WiFi.SSID())<=MAXSSIDLEN)
214 strcpy(info->ssid, WiFi.SSID());
226 void printEncryptionType(int thisType)
228 // read the encryption type and print out the name:
232 Serial.println("WEP");
235 Serial.println("WPA");
238 Serial.println("WPA2");
241 Serial.println("None");
244 Serial.println("Auto");