Resolved tizen build issue regarding ip network
[platform/upstream/iotivity.git] / service / easy-setup / enrollee / arduino / wifi / networkhandler.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 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 // Arduino WiFi Shield includes
24 #include <SPI.h>
25 #include <WiFi.h>
26 #include <WiFiUdp.h>
27
28 #include <string.h>
29
30 #include "logger.h"
31
32 /**
33  * @var ES_NH_TAG
34  * @brief Logging tag for module name.
35  */
36 #define ES_NH_TAG "ES_NH"
37
38 //-----------------------------------------------------------------------------
39 // Defines
40 //-----------------------------------------------------------------------------
41 /**
42  *  ES_MAX_NETWORK_RETRY sets the default number of retry count for network connection.
43  */
44 #define ES_MAX_NETWORK_RETRY (5)
45
46 //-----------------------------------------------------------------------------
47 // Private variables
48 //-----------------------------------------------------------------------------
49 static IPAddress enrolleeIP;
50
51 /**
52  * @var g_retryCounter
53  * @brief Retry counter for cancelling network retry. Currently network retry is
54  *          limited to 5 attempts
55  */
56 static uint16_t g_retryCounter = 0;
57
58
59
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);
66
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";
71
72 ESResult ConnectToWiFiNetwork(const char *ssid, const char *pass,
73                                                             ESEnrolleeNetworkEventCallback cb)
74 {
75     char *fwVersion;
76     int status = WL_IDLE_STATUS;
77     int res;
78
79     // check for the presence of the shield:
80     if (WiFi.status() == WL_NO_SHIELD)
81     {
82         OIC_LOG(ERROR, ES_NH_TAG, "WiFi shield not present");
83         return ES_ERROR;
84     }
85
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))
90             != 0)
91     {
92         OIC_LOG(DEBUG, ES_NH_TAG, "!!!!! Upgrade WiFi Shield Firmware version !!!!!!");
93         return ES_ERROR;
94     }
95
96     //Retry counter is reset everytime the ConnectToWiFiNetwork is invoked
97     g_retryCounter = 0;
98
99     OIC_LOG_V(INFO, ES_NH_TAG, "Finding SSID: %s", ssid);
100
101     while ((findNetwork(ssid) == 0) && g_retryCounter < ES_MAX_NETWORK_RETRY) // found
102     {
103         delay(1000);
104         g_retryCounter++;
105     }
106
107     if(g_retryCounter == ES_MAX_NETWORK_RETRY){
108         OIC_LOG_V(ERROR, ES_NH_TAG, "Connection to network failed after %d attempts",
109                   g_retryCounter);
110         return ES_ERROR;
111     }
112
113     if (cb != NULL)
114     {
115         cb(ES_OK);
116     }
117
118     if (WiFi.status() == WL_CONNECTED)
119         WiFi.disconnect();
120
121     //Retry counter is reset everytime the ConnectToWiFiNetwork is invoked
122     g_retryCounter = 0;
123
124     res = ConnectToNetwork(ssid, pass);
125
126     if (res == 0)
127     {
128         return ES_NETWORKCONNECTED;
129     }
130     else
131     {
132         return ES_NETWORKNOTCONNECTED;
133     }
134 }
135
136 int findNetwork(const char *ssid)
137 {
138     int res = 0;
139     // scan for nearby networks:
140     Serial.println("** Scan Networks **");
141     int numSsid = WiFi.scanNetworks();
142     if (numSsid == -1)
143     {
144         Serial.println("Couldn't get a wifi connection");
145
146         return res;
147     }
148
149     // print the list of networks seen:
150     Serial.print("number of available networks:");
151     Serial.println(numSsid);
152
153     // print the network number and name for each network found:
154     for (int thisNet = 0; thisNet < numSsid; thisNet++)
155     {
156         Serial.print(thisNet);
157         Serial.print(") ");
158         Serial.print(WiFi.SSID(thisNet));
159         Serial.print("\tEncryption: ");
160         printEncryptionType(WiFi.encryptionType(thisNet));
161
162         if (strcmp(WiFi.SSID(thisNet), ssid) == 0)
163         {
164             res = 1;
165         }
166     }
167
168     return res;
169 }
170
171 int ConnectToNetwork(const char *ssid, const char *pass)
172 {
173     int status = WL_IDLE_STATUS;
174
175     // attempt to connect to Wifi network:
176     while (status != WL_CONNECTED && g_retryCounter < ES_MAX_NETWORK_RETRY)
177     {
178         OIC_LOG_V(INFO, ES_NH_TAG, "Attempting to connect to SSID: %s", ssid);
179
180         status = WiFi.begin((char *) ssid, (char *) pass);
181
182         // wait 10 seconds for connection:
183         delay(10000);
184
185         g_retryCounter++;
186     }
187
188     if(g_retryCounter == ES_MAX_NETWORK_RETRY){
189         OIC_LOG_V(ERROR, ES_NH_TAG, "Connection to network failed after %d attempts",
190                   g_retryCounter);
191         return ES_ERROR;
192     }
193
194     OIC_LOG(DEBUG, ES_NH_TAG, "Connected to wifi");
195
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]);
199
200     char buf[50];
201     sprintf(buf, "IP Address:  %d.%d.%d.%d", enrolleeIP[0], enrolleeIP[1],
202                                              enrolleeIP[2], enrolleeIP[3]);
203     Serial.println(buf);
204
205     return 0;
206 }
207
208 ESResult getCurrentNetworkInfo(OCConnectivityType targetType, NetworkInfo *info)
209 {
210     if (targetType == CT_ADAPTER_IP && WiFi.status() == WL_CONNECTED)
211     {
212         info->type = CT_ADAPTER_IP;
213         info->ipaddr = WiFi.localIP();
214         if((sizeof(info->ssid) >= MAXSSIDLEN) && (strlen(WiFi.SSID()) <= MAXSSIDLEN))
215         {
216             strcpy(info->ssid, WiFi.SSID());
217             return ES_OK;
218         }
219         else
220         {
221             return ES_ERROR;
222         }
223     }
224
225     return ES_ERROR;
226 }
227
228 void printEncryptionType(int thisType)
229 {
230     // read the encryption type and print out the name:
231     switch (thisType)
232     {
233         case ENC_TYPE_WEP:
234             Serial.println("WEP");
235             break;
236         case ENC_TYPE_TKIP:
237             Serial.println("WPA");
238             break;
239         case ENC_TYPE_CCMP:
240             Serial.println("WPA2");
241             break;
242         case ENC_TYPE_NONE:
243             Serial.println("None");
244             break;
245         case ENC_TYPE_AUTO:
246             Serial.println("Auto");
247             break;
248     }
249 }