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 PROGMEM const char TAG[] = "networkHandler";
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 ES_RESULT 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 while (findNetwork(ssid) == 0) // found
70 if (WiFi.status() == WL_CONNECTED)
73 res = ConnectToNetwork(ssid, pass);
77 return ES_NETWORKCONNECTED;
81 return ES_NETWORKNOTCONNECTED;
85 int findNetwork(const char *ssid)
88 // scan for nearby networks:
89 Serial.println("** Scan Networks **");
90 int numSsid = WiFi.scanNetworks();
93 Serial.println("Couldn't get a wifi connection");
98 // print the list of networks seen:
99 Serial.print("number of available networks:");
100 Serial.println(numSsid);
102 // print the network number and name for each network found:
103 for (int thisNet = 0; thisNet < numSsid; thisNet++)
105 Serial.print(thisNet);
107 Serial.print(WiFi.SSID(thisNet));
108 Serial.print("\tEncryption: ");
109 printEncryptionType(WiFi.encryptionType(thisNet));
111 if (strcmp(WiFi.SSID(thisNet), ssid) == 0)
120 int ConnectToNetwork(const char *ssid, const char *pass)
122 int status = WL_IDLE_STATUS;
124 // attempt to connect to Wifi network:
125 while (status != WL_CONNECTED)
127 OC_LOG_V(INFO, TAG, "Attempting to connect to SSID: %s", ssid);
129 status = WiFi.begin((char *) ssid, (char *) pass);
131 // wait 10 seconds for connection:
134 OC_LOG(DEBUG, TAG, PCF("Connected to wifi"));
136 myIP = WiFi.localIP();
137 OC_LOG_V(INFO, TAG, "IP Address: %d.%d.%d.%d", myIP[0], myIP[1], myIP[2], myIP[3]);
140 sprintf(buf, "IP Address: %d.%d.%d.%d", myIP[0], myIP[1], myIP[2], myIP[3]);
146 int getCurrentNetworkInfo(NetworkType targetType, NetworkInfo *info)
148 if (targetType == ES_WIFI && WiFi.status() == WL_CONNECTED)
150 info->type = ES_WIFI;
151 info->ipaddr = WiFi.localIP();
152 strcpy(info->ssid, WiFi.SSID());
160 void printEncryptionType(int thisType)
162 // read the encryption type and print out the name:
166 Serial.println("WEP");
169 Serial.println("WPA");
172 Serial.println("WPA2");
175 Serial.println("None");
178 Serial.println("Auto");