Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / soft-sensor-manager / SampleApp / arduino / Reference_Thing / src / oic_lanLib.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 "logger.h"
22 #include "ocstack.h"
23 #include <string.h>
24
25 // proximity code e
26 #ifdef ARDUINOWIFI
27 // Arduino WiFi Shield
28 #include <SPI.h>
29 #include <WiFi.h>
30 #include <WiFiUdp.h>
31 #else
32 // Arduino Ethernet Shield
33 #include <EthernetServer.h>
34 #include <Ethernet.h>
35 #include <Dns.h>
36 #include <EthernetClient.h>
37 #include <util.h>
38 #include <EthernetUdp.h>
39 #include <Dhcp.h>
40 #endif
41
42
43 PROGMEM const char TAG[] = "TrackeeSensor";
44
45 #ifdef ARDUINOWIFI
46 // Arduino WiFi Shield
47 // Note : Arduino WiFi Shield currently does NOT support multicast and therefore
48 // this server will NOT be listening on 224.0.1.187 multicast address.
49
50 /// WiFi Shield firmware with Intel patches
51 static const char INTEL_WIFI_SHIELD_FW_VER[] = "1.2.0";
52
53 /// WiFi network info and credentials
54 //char ssid[] = "SoftSensor_AP";
55 //char pass[] = "1234567890";
56 char ssid[] = "SoftSensor_2.4G";
57 char pass[] = "12344321";
58
59 int ConnectToNetwork()
60 {
61     char *fwVersion;
62     int status = WL_IDLE_STATUS;
63     // check for the presence of the shield:
64     if (WiFi.status() == WL_NO_SHIELD)
65     {
66         OC_LOG(ERROR, TAG, PCF("WiFi shield not present"));
67         return -1;
68     }
69
70     // Verify that WiFi Shield is running the firmware with all UDP fixes
71     fwVersion = WiFi.firmwareVersion();
72     OC_LOG_V(INFO, TAG, "WiFi Shield Firmware version %s", fwVersion);
73     if ( strncmp(fwVersion, INTEL_WIFI_SHIELD_FW_VER, sizeof(INTEL_WIFI_SHIELD_FW_VER)) != 0 )
74     {
75         OC_LOG(DEBUG, TAG, PCF("!!!!! Upgrade WiFi Shield Firmware version !!!!!!"));
76         return -1;
77     }
78
79     // attempt to connect to Wifi network:
80     while (status != WL_CONNECTED)
81     {
82         OC_LOG_V(INFO, TAG, "Attempting to connect to SSID: %s", ssid);
83         status = WiFi.begin(ssid, pass);
84
85         // wait 10 seconds for connection:
86         delay(10000);
87     }
88     OC_LOG(DEBUG, TAG, PCF("Connected to wifi"));
89
90     IPAddress ip = WiFi.localIP();
91     OC_LOG_V(INFO, TAG, "IP Address:  %d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
92     return 0;
93 }
94 #else
95 // Arduino Ethernet Shield
96 int ConnectToNetwork()
97 {
98     // Note: ****Update the MAC address here with your shield's MAC address****
99     uint8_t ETHERNET_MAC[] = {0x90, 0xA2, 0xDA, 0x0E, 0xB8, 0xAC};
100
101     uint8_t error = Ethernet.begin(ETHERNET_MAC);
102     if (error  == 0)
103     {
104         OC_LOG_V(ERROR, TAG, "error is: %d", error);
105         return -1;
106     }
107     IPAddress ip = Ethernet.localIP();
108     OC_LOG_V(INFO, TAG, "IP Address:  %d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
109     return 0;
110 }
111 #endif //ARDUINOWIFI