1 //******************************************************************
3 // Copyright 2014 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 #define TAG PCF("ES_NH")
25 int findNetwork(const char *ssid);
26 int ConnectToNetwork(const char *ssid, const char *pass);
27 void printEncryptionType(int thisType);
29 // Arduino WiFi Shield
30 // Note : Arduino WiFi Shield currently does NOT support multicast and therefore
31 // this server will NOT be listening on 224.0.1.187 multicast address.
33 static const char ARDUINO_WIFI_SHIELD_UDP_FW_VER[] = "1.1.0";
37 ESResult ConnectToWiFiNetwork(const char *ssid, const char *pass, NetworkEventCallback cb)
40 int status = WL_IDLE_STATUS;
43 // check for the presence of the shield:
44 if (WiFi.status() == WL_NO_SHIELD)
46 OC_LOG(ERROR, TAG, PCF("WiFi shield not present"));
50 // Verify that WiFi Shield is running the firmware with all UDP fixes
51 fwVersion = WiFi.firmwareVersion();
52 OC_LOG_V(INFO, TAG, "WiFi Shield Firmware version %s", fwVersion);
53 if (strncmp(fwVersion, ARDUINO_WIFI_SHIELD_UDP_FW_VER, sizeof(ARDUINO_WIFI_SHIELD_UDP_FW_VER))
56 OC_LOG(DEBUG, TAG, PCF("!!!!! Upgrade WiFi Shield Firmware version !!!!!!"));
60 OC_LOG_V(INFO, TAG, PCF("Finding SSID: %s"), ssid);
62 while (findNetwork(ssid) == 0) // found
72 if (WiFi.status() == WL_CONNECTED)
75 res = ConnectToNetwork(ssid, pass);
79 return ES_NETWORKCONNECTED;
83 return ES_NETWORKNOTCONNECTED;
87 int findNetwork(const char *ssid)
90 // scan for nearby networks:
91 Serial.println("** Scan Networks **");
92 int numSsid = WiFi.scanNetworks();
95 Serial.println("Couldn't get a wifi connection");
100 // print the list of networks seen:
101 Serial.print("number of available networks:");
102 Serial.println(numSsid);
104 // print the network number and name for each network found:
105 for (int thisNet = 0; thisNet < numSsid; thisNet++)
107 Serial.print(thisNet);
109 Serial.print(WiFi.SSID(thisNet));
110 Serial.print("\tEncryption: ");
111 printEncryptionType(WiFi.encryptionType(thisNet));
113 if (strcmp(WiFi.SSID(thisNet), ssid) == 0)
122 int ConnectToNetwork(const char *ssid, const char *pass)
124 int status = WL_IDLE_STATUS;
126 // attempt to connect to Wifi network:
127 while (status != WL_CONNECTED)
129 OC_LOG_V(INFO, TAG, PCF("Attempting to connect to SSID: %s"), ssid);
131 status = WiFi.begin((char *) ssid, (char *) pass);
133 // wait 10 seconds for connection:
136 OC_LOG(DEBUG, TAG, PCF("Connected to wifi"));
138 myIP = WiFi.localIP();
139 OC_LOG_V(INFO, TAG, PCF("IP Address: %d.%d.%d.%d"), myIP[0], myIP[1], myIP[2], myIP[3]);
142 sprintf(buf, "IP Address: %d.%d.%d.%d", myIP[0], myIP[1], myIP[2], myIP[3]);
148 int getCurrentNetworkInfo(NetworkType targetType, NetworkInfo *info)
150 if (targetType == ES_WIFI && WiFi.status() == WL_CONNECTED)
152 info->type = ES_WIFI;
153 info->ipaddr = WiFi.localIP();
154 strcpy(info->ssid, WiFi.SSID());
162 void printEncryptionType(int thisType)
164 // read the encryption type and print out the name:
168 Serial.println("WEP");
171 Serial.println("WPA");
174 Serial.println("WPA2");
177 Serial.println("None");
180 Serial.println("Auto");