iotivity 0.9.0
[platform/upstream/iotivity.git] / resource / csdk / ocsocket / test / arduino / ocsocket_wifi_test.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH 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 <ocsocket.h>
22 #include <logger.h>
23 #include <SPI.h>
24 #include <WiFi.h>
25 #include <WiFiUdp.h>
26
27 /// WiFi Shield firmware with Intel patches
28 static const char INTEL_WIFI_SHIELD_FW_VER[] = "1.2.0";
29
30 /// IP address of the peer whom I wish to send/recv some data
31 static uint8_t PEER_IP_ADDR[] = {192, 168, 1, 132};
32
33 /// Port number of the peer whom I wish to send/recv some data
34 #define TEST_PORT_NUM (4097)
35
36 /// Max buffer size
37 #define MAX_BUF_SIZE  (1024)
38
39 /// Some test bytes to send to the peer
40 unsigned char TEST_BUF[] = {
41   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
42   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
43   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
44   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
45   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
46   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
47   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
48   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
49   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
50   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
51   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
52   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
53   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
54   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
55   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
56   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
57   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
58   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
59   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
60   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
61   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
62   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
63   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
64   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
65   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
66   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
67   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
68   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
69   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
70   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
71   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
72   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
73   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
74   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
75   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
76   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
77   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
78   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
79   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
80   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
81 };
82 unsigned int TEST_BUF_LEN = sizeof(TEST_BUF);
83
84 #define MOD_NAME PCF("ocsocket_test")
85
86 #define VERIFY_SUCCESS(op, res) { if (op == res) {OC_LOG(DEBUG, MOD_NAME, PCF(#op " SUCCEEDED"));} \
87      else {OC_LOG(FATAL, MOD_NAME, PCF(#op "!!!!  FAILED FAILED FAILED !!!!"));} }
88
89 /// WiFi network info and credentials
90 char ssid[] = "mDNSAP";
91 char pass[] = "letmein9";
92 int status = WL_IDLE_STATUS;
93
94 //Start sample app 'sendrecv' on different Linux box.
95 void SendReceiveTest()
96 {
97     Serial.println("entering SendReceiveTest...");
98     OCDevAddr ipAddr, peerAddr, recvAddr;
99     int32_t sfd;
100     int32_t recvLen;
101     uint8_t buf[MAX_BUF_SIZE];
102     uint16_t recvPort;
103
104     VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM, &ipAddr), ERR_SUCCESS);
105     VERIFY_SUCCESS(OCBuildIPv4Address(PEER_IP_ADDR[0], PEER_IP_ADDR[1],
106                 PEER_IP_ADDR[2], PEER_IP_ADDR[3], TEST_PORT_NUM, &peerAddr), ERR_SUCCESS);
107     VERIFY_SUCCESS(OCInitUDP( &ipAddr, &sfd), ERR_SUCCESS);
108     OC_LOG(DEBUG, MOD_NAME, PCF("Peer Addr :"));
109     OC_LOG_BUFFER(INFO, MOD_NAME, peerAddr.addr, peerAddr.size);
110     for (int i = 0; i < 300; i++)
111     {
112
113         OC_LOG_V(DEBUG, MOD_NAME, "--------------------- i --------------- %d", i);
114         VERIFY_SUCCESS(OCSendTo(sfd, TEST_BUF, i + 10, 0, &peerAddr), i+10);
115
116         delay(2000);
117         recvLen = OCRecvFrom(sfd, buf, MAX_BUF_SIZE, 0, &recvAddr);
118         if (recvLen > 0)
119         {
120             OC_LOG(DEBUG, MOD_NAME, PCF("Rcvd data from :"));
121             OC_LOG_BUFFER(INFO, MOD_NAME, recvAddr.addr, recvAddr.size);
122             OCDevAddrToPort(&recvAddr, &recvPort);
123             OC_LOG_V(DEBUG, MOD_NAME, "Recv Data from Port %hu", recvPort);
124             OC_LOG(DEBUG, MOD_NAME, PCF("Data Length :"));
125             OC_LOG_BUFFER(INFO, MOD_NAME, (uint8_t*)&recvLen, sizeof(recvLen));
126             OC_LOG(DEBUG, MOD_NAME, PCF("Data :"));
127             if (recvLen < 255)
128             {
129                 OC_LOG_BUFFER(INFO, MOD_NAME, buf, recvLen);
130             }
131             else
132             {
133                 int idx = 0;
134                 int rem = recvLen;
135                 do {
136                     if (rem > 255)
137                     {
138                         OC_LOG_BUFFER(INFO, MOD_NAME, buf + idx, 255);
139                         rem = rem - 255;
140                     }
141                     else
142                     {
143                         OC_LOG_BUFFER(INFO, MOD_NAME, buf + idx, rem);
144                         rem = 0;
145                     }
146                     idx = idx + 255;
147                 }while(rem > 0);
148             }
149
150         }
151     }
152     VERIFY_SUCCESS(OCClose( sfd), ERR_SUCCESS);
153     OC_LOG(DEBUG, MOD_NAME, PCF("WifiTest - Completed"));
154
155 }
156 void setup()
157 {
158     Serial.begin(115200);
159     Serial.println("WiFi test starting...");
160
161     // check for the presence of the shield:
162     if (WiFi.status() == WL_NO_SHIELD)
163     {
164       Serial.println("WiFi shield not present");
165       // don't continue:
166       while(true);
167     }
168
169     Serial.print("WiFi version: ");
170     Serial.println(WiFi.firmwareVersion());
171
172     if (strcmp(WiFi.firmwareVersion(), INTEL_WIFI_SHIELD_FW_VER) != 0)
173     {
174         Serial.println("!!!!! Upgrade WiFi Shield Firmware version !!!!!!");
175     }
176
177     // attempt to connect to Wifi network:
178     while ( status != WL_CONNECTED) {
179       Serial.print("Attempting to connect to SSID: ");
180       Serial.println(ssid);
181       status = WiFi.begin(ssid,pass);
182
183       // wait 10 seconds for connection:
184       delay(10000);
185     }
186     Serial.println("Connected to wifi");
187
188     IPAddress ip = WiFi.localIP();
189     Serial.print("IP Address: ");
190     Serial.println(ip);
191 }
192
193 void loop() {
194     Serial.println("entering loop...");
195     SendReceiveTest();
196     delay(5000);
197
198     do {
199         OC_LOG(DEBUG, MOD_NAME, PCF("All tests Completed"));
200         delay(10000);
201     } while (1);
202 }
203
204