Merge branch 'master' into easysetup & CBOR changes
[platform/upstream/iotivity.git] / service / easy-setup / sdk / enrollee / arduino / wifi / networkHandler.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "networkHandler.h"
22
23 PROGMEM const char TAG[] = "networkHandler";
24
25 int findNetwork(const char *ssid);
26 int ConnectToNetwork(const char *ssid, const char *pass);
27 void printEncryptionType(int thisType);
28
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.
32
33 static const char ARDUINO_WIFI_SHIELD_UDP_FW_VER[] = "1.1.0";
34
35 IPAddress myIP;
36
37 ES_RESULT ConnectToWiFiNetwork(const char *ssid, const char *pass, NetworkEventCallback cb)
38 {
39     char *fwVersion;
40     int status = WL_IDLE_STATUS;
41     int res;
42
43     // check for the presence of the shield:
44     if (WiFi.status() == WL_NO_SHIELD)
45     {
46         OC_LOG(ERROR, TAG, PCF("WiFi shield not present"));
47         return ES_ERROR;
48     }
49
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))
54             != 0)
55     {
56         OC_LOG(DEBUG, TAG, PCF("!!!!! Upgrade WiFi Shield Firmware version !!!!!!"));
57         //return ES_ERROR;
58     }
59
60     while (findNetwork(ssid) == 0) // found
61     {
62         delay(1000);
63     }
64
65     if (cb != NULL)
66     {
67         cb(ES_NETWORKFOUND);
68     }
69
70     if (WiFi.status() == WL_CONNECTED)
71         WiFi.disconnect();
72
73     res = ConnectToNetwork(ssid, pass);
74
75     if (res == 0)
76     {
77         return ES_NETWORKCONNECTED;
78     }
79     else
80     {
81         return ES_NETWORKNOTCONNECTED;
82     }
83 }
84
85 int findNetwork(const char *ssid)
86 {
87     int res = 0;
88     // scan for nearby networks:
89     Serial.println("** Scan Networks **");
90     int numSsid = WiFi.scanNetworks();
91     if (numSsid == -1)
92     {
93         Serial.println("Couldn't get a wifi connection");
94
95         return res;
96     }
97
98     // print the list of networks seen:
99     Serial.print("number of available networks:");
100     Serial.println(numSsid);
101
102     // print the network number and name for each network found:
103     for (int thisNet = 0; thisNet < numSsid; thisNet++)
104     {
105         Serial.print(thisNet);
106         Serial.print(") ");
107         Serial.print(WiFi.SSID(thisNet));
108         Serial.print("\tEncryption: ");
109         printEncryptionType(WiFi.encryptionType(thisNet));
110
111         if (strcmp(WiFi.SSID(thisNet), ssid) == 0)
112         {
113             res = 1;
114         }
115     }
116
117     return res;
118 }
119
120 int ConnectToNetwork(const char *ssid, const char *pass)
121 {
122     int status = WL_IDLE_STATUS;
123
124     // attempt to connect to Wifi network:
125     while (status != WL_CONNECTED)
126     {
127         OC_LOG_V(INFO, TAG, "Attempting to connect to SSID: %s", ssid);
128
129         status = WiFi.begin((char *) ssid, (char *) pass);
130
131         // wait 10 seconds for connection:
132         delay(10000);
133     }
134     OC_LOG(DEBUG, TAG, PCF("Connected to wifi"));
135
136     myIP = WiFi.localIP();
137     OC_LOG_V(INFO, TAG, "IP Address:  %d.%d.%d.%d", myIP[0], myIP[1], myIP[2], myIP[3]);
138
139     char buf[50];
140     sprintf(buf, "IP Address:  %d.%d.%d.%d", myIP[0], myIP[1], myIP[2], myIP[3]);
141     Serial.println(buf);
142
143     return 0;
144 }
145
146 int getCurrentNetworkInfo(NetworkType targetType, NetworkInfo *info)
147 {
148     if (targetType == ES_WIFI && WiFi.status() == WL_CONNECTED)
149     {
150         info->type = ES_WIFI;
151         info->ipaddr = WiFi.localIP();
152         strcpy(info->ssid, WiFi.SSID());
153
154         return 0;
155     }
156
157     return -1;
158 }
159
160 void printEncryptionType(int thisType)
161 {
162     // read the encryption type and print out the name:
163     switch (thisType)
164     {
165         case ENC_TYPE_WEP:
166             Serial.println("WEP");
167             break;
168         case ENC_TYPE_TKIP:
169             Serial.println("WPA");
170             break;
171         case ENC_TYPE_CCMP:
172             Serial.println("WPA2");
173             break;
174         case ENC_TYPE_NONE:
175             Serial.println("None");
176             break;
177         case ENC_TYPE_AUTO:
178             Serial.println("Auto");
179             break;
180     }
181 }