Modify permissions for files in Things Manager
[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 #define TAG PCF("ES_NH")
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 ESResult 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     OC_LOG_V(INFO, TAG, PCF("Finding SSID: %s"), ssid);
61
62     while (findNetwork(ssid) == 0) // found
63     {
64         delay(1000);
65     }
66
67     if (cb != NULL)
68     {
69         cb(ES_NETWORKFOUND);
70     }
71
72     if (WiFi.status() == WL_CONNECTED)
73         WiFi.disconnect();
74
75     res = ConnectToNetwork(ssid, pass);
76
77     if (res == 0)
78     {
79         return ES_NETWORKCONNECTED;
80     }
81     else
82     {
83         return ES_NETWORKNOTCONNECTED;
84     }
85 }
86
87 int findNetwork(const char *ssid)
88 {
89     int res = 0;
90     // scan for nearby networks:
91     Serial.println("** Scan Networks **");
92     int numSsid = WiFi.scanNetworks();
93     if (numSsid == -1)
94     {
95         Serial.println("Couldn't get a wifi connection");
96
97         return res;
98     }
99
100     // print the list of networks seen:
101     Serial.print("number of available networks:");
102     Serial.println(numSsid);
103
104     // print the network number and name for each network found:
105     for (int thisNet = 0; thisNet < numSsid; thisNet++)
106     {
107         Serial.print(thisNet);
108         Serial.print(") ");
109         Serial.print(WiFi.SSID(thisNet));
110         Serial.print("\tEncryption: ");
111         printEncryptionType(WiFi.encryptionType(thisNet));
112
113         if (strcmp(WiFi.SSID(thisNet), ssid) == 0)
114         {
115             res = 1;
116         }
117     }
118
119     return res;
120 }
121
122 int ConnectToNetwork(const char *ssid, const char *pass)
123 {
124     int status = WL_IDLE_STATUS;
125
126     // attempt to connect to Wifi network:
127     while (status != WL_CONNECTED)
128     {
129         OC_LOG_V(INFO, TAG, PCF("Attempting to connect to SSID: %s"), ssid);
130
131         status = WiFi.begin((char *) ssid, (char *) pass);
132
133         // wait 10 seconds for connection:
134         delay(10000);
135     }
136     OC_LOG(DEBUG, TAG, PCF("Connected to wifi"));
137
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]);
140
141     char buf[50];
142     sprintf(buf, "IP Address:  %d.%d.%d.%d", myIP[0], myIP[1], myIP[2], myIP[3]);
143     Serial.println(buf);
144
145     return 0;
146 }
147
148 int getCurrentNetworkInfo(NetworkType targetType, NetworkInfo *info)
149 {
150     if (targetType == ES_WIFI && WiFi.status() == WL_CONNECTED)
151     {
152         info->type = ES_WIFI;
153         info->ipaddr = WiFi.localIP();
154         strcpy(info->ssid, WiFi.SSID());
155
156         return 0;
157     }
158
159     return -1;
160 }
161
162 void printEncryptionType(int thisType)
163 {
164     // read the encryption type and print out the name:
165     switch (thisType)
166     {
167         case ENC_TYPE_WEP:
168             Serial.println("WEP");
169             break;
170         case ENC_TYPE_TKIP:
171             Serial.println("WPA");
172             break;
173         case ENC_TYPE_CCMP:
174             Serial.println("WPA2");
175             break;
176         case ENC_TYPE_NONE:
177             Serial.println("None");
178             break;
179         case ENC_TYPE_AUTO:
180             Serial.println("Auto");
181             break;
182     }
183 }