iotivity 0.9.0
[platform/upstream/iotivity.git] / resource / csdk / ocsocket / test / arduino / ocsocket_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 <SPI.h>
22 #include <Ethernet.h>
23 #include <ocsocket.h>
24 #include <logger.h>
25
26 //Mac address of my ethernet shield
27 static uint8_t ETHERNET_MAC[] = {0x90, 0xA2, 0xDA, 0x0F, 0x2B, 0x72 };
28 //IP address of the peer whom I wish to send some data
29 static uint8_t PEER_IP_ADDR[] = {192, 168, 1, 125};
30 static uint8_t MULTICAST_IP_ADDR[] = {224, 0, 1, 187};
31
32 //Set below to 0 to disable multicast testing
33 //#define MCAST_TESTING_EN 1
34
35 #define TEST_NUM_PKTS (20)
36 #define TEST_PORT_NUM (8888)
37 #define MAX_BUF_SIZE  (256)
38
39 unsigned char TEST_BUF[] = {
40   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
41   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
42   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
43   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
44   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
45   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
46   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
47   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
48   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
49   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
50   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
51   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
52   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
53   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
54   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
55   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
56   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
57   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
58   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
59   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
60   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
61   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
62   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
63   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
64   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
65   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
66   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
67   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
68   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
69   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
70   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
71   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
72   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
73   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
74   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
75   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
76   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
77   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
78   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
79   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
80 };
81 unsigned int TEST_BUF_LEN = sizeof(TEST_BUF);
82
83 #define PCF(str) ((PROGMEM const char *)(F(str)))
84
85 #define MOD_NAME PCF("ocsocket_test")
86
87 #define VERIFY_SUCCESS(op, res) { if (op == res) {OC_LOG(DEBUG, MOD_NAME, PCF(#op " SUCCEEDED"));} \
88      else {OC_LOG(FATAL, MOD_NAME, PCF(#op "!!!!  FAILED FAILED FAILED !!!!"));} }
89
90
91 //OCGetInterfaceAddress tests
92 void test10() {
93     char strAddr[16] = "";
94     VERIFY_SUCCESS(OCGetInterfaceAddress(NULL, 0, AF_INET, (uint8_t*)strAddr, 16), ERR_SUCCESS);
95     OC_LOG(DEBUG, MOD_NAME, PCF("My IP address :"));
96     OC_LOG_BUFFER(INFO, MOD_NAME, (uint8_t*)strAddr, sizeof(strAddr));
97     delay(15000);
98     VERIFY_SUCCESS(OCGetInterfaceAddress(NULL, 0, AF_INET, NULL, 16), ERR_INVALID_INPUT);
99     VERIFY_SUCCESS(OCGetInterfaceAddress(NULL, 0, AF_INET, (uint8_t*)strAddr, 10), ERR_INVALID_INPUT);
100     OC_LOG(DEBUG, MOD_NAME, PCF("test10 - Completed"));
101 }
102
103 //OCBuildIPv4Address tests
104 void test20() {
105     OCDevAddr ipAddr;
106     VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM, &ipAddr), ERR_SUCCESS);
107     VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM, NULL), ERR_INVALID_INPUT);
108     OC_LOG(DEBUG, MOD_NAME, PCF("test20 - Completed"));
109 }
110
111
112 void test30() {
113
114     OCDevAddr ipAddr;
115     uint8_t a, b, c, d;
116     uint16_t port;
117     VERIFY_SUCCESS(OCBuildIPv4Address( 1, 2, 3, 4, TEST_PORT_NUM, &ipAddr), ERR_SUCCESS);
118     VERIFY_SUCCESS(OCDevAddrToIPv4Addr( &ipAddr, &a, &b, &c, &d), ERR_SUCCESS);
119     if ((a != 1) || (b != 2) || (c !=3) || (d !=4)) {
120         OC_LOG(DEBUG, MOD_NAME, PCF("test30 - Failed !!!"));
121         return;
122     }
123
124     VERIFY_SUCCESS(OCDevAddrToPort( &ipAddr, &port), ERR_SUCCESS);
125     if (port != TEST_PORT_NUM) {
126         OC_LOG(DEBUG, MOD_NAME, PCF("test30 - Failed !!!"));
127         return;
128     }
129     VERIFY_SUCCESS(OCDevAddrToIPv4Addr( NULL, &a, &b, &c, &d), ERR_INVALID_INPUT);
130     VERIFY_SUCCESS(OCDevAddrToPort( &ipAddr, NULL), ERR_INVALID_INPUT);
131     OC_LOG(DEBUG, MOD_NAME, PCF("test30 - Completed"));
132 }
133
134 //OCInitUDP tests
135 void test40() {
136     OCDevAddr ipAddr;
137     int32_t sfd1, sfd2, sfd3, sfd4, sfd5;
138
139     //We should be able to successfully open 4 sockets.
140     VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM, &ipAddr), ERR_SUCCESS);
141     VERIFY_SUCCESS(OCInitUDP( &ipAddr, &sfd1), ERR_SUCCESS);
142
143     VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM + 1, &ipAddr), ERR_SUCCESS);
144     VERIFY_SUCCESS(OCInitUDP( &ipAddr, &sfd2), ERR_SUCCESS);
145
146     VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM + 2, &ipAddr), ERR_SUCCESS);
147     VERIFY_SUCCESS(OCInitUDP( &ipAddr, &sfd3), ERR_SUCCESS);
148
149     VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM + 3, &ipAddr), ERR_SUCCESS);
150     VERIFY_SUCCESS(OCInitUDP( &ipAddr, &sfd4), ERR_SUCCESS);
151
152     //5th socket creation call should FAIL
153     VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM + 4, &ipAddr), ERR_SUCCESS);
154     VERIFY_SUCCESS(OCInitUDP( &ipAddr, &sfd5), ERR_UNKNOWN);
155
156     VERIFY_SUCCESS(OCClose( sfd4), ERR_SUCCESS);
157     VERIFY_SUCCESS(OCInitUDP( &ipAddr, &sfd5), ERR_SUCCESS);
158
159     VERIFY_SUCCESS(OCClose( sfd1), ERR_SUCCESS);
160     VERIFY_SUCCESS(OCClose( sfd2), ERR_SUCCESS);
161     VERIFY_SUCCESS(OCClose( sfd3), ERR_SUCCESS);
162     VERIFY_SUCCESS(OCClose( sfd5), ERR_SUCCESS);
163     OC_LOG(DEBUG, MOD_NAME, PCF("test40 - Completed"));
164 }
165
166
167 //OCInitUDPMulticast tests
168 void test50() {
169     OCDevAddr ipAddr;
170     int32_t sfd1, sfd2;
171
172     VERIFY_SUCCESS(OCBuildIPv4Address( MULTICAST_IP_ADDR[0], MULTICAST_IP_ADDR[1],
173                 MULTICAST_IP_ADDR[2], MULTICAST_IP_ADDR[3], TEST_PORT_NUM, &ipAddr), ERR_SUCCESS);
174     VERIFY_SUCCESS(OCInitUDPMulticast( &ipAddr, &sfd1), ERR_SUCCESS);
175
176     VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM + 1, &ipAddr), ERR_SUCCESS);
177     VERIFY_SUCCESS(OCInitUDP( &ipAddr, &sfd2), ERR_SUCCESS);
178
179     VERIFY_SUCCESS(OCClose( sfd1), ERR_SUCCESS);
180     VERIFY_SUCCESS(OCClose( sfd2), ERR_SUCCESS);
181     OC_LOG(DEBUG, MOD_NAME, PCF("test50 - Completed"));
182 }
183
184
185 //OCSendTo -- Unicast tests
186 void test60() {
187     OCDevAddr ipAddr, peerAddr;
188     int32_t sfd;
189
190     VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM, &ipAddr), ERR_SUCCESS);
191     VERIFY_SUCCESS(OCBuildIPv4Address(PEER_IP_ADDR[0], PEER_IP_ADDR[0],
192                 PEER_IP_ADDR[0], PEER_IP_ADDR[0], TEST_PORT_NUM, &peerAddr), ERR_SUCCESS);
193
194     VERIFY_SUCCESS(OCInitUDP( &ipAddr, &sfd), ERR_SUCCESS);
195     for (int i = 2; i < 300; ) {
196         VERIFY_SUCCESS(OCSendTo(sfd, TEST_BUF, i, 0, &peerAddr), i);
197         i = i + 12;
198
199         delay(100);
200     }
201     VERIFY_SUCCESS(OCClose( sfd), ERR_SUCCESS);
202     OC_LOG(DEBUG, MOD_NAME, PCF("test60 - Completed"));
203 }
204
205
206 //OCSendTo -- Multicast tests
207 void test70() {
208     OCDevAddr ipAddrMcast;
209     int32_t sfd;
210
211     VERIFY_SUCCESS(OCBuildIPv4Address( MULTICAST_IP_ADDR[0], MULTICAST_IP_ADDR[1],
212                 MULTICAST_IP_ADDR[2], MULTICAST_IP_ADDR[3], TEST_PORT_NUM, &ipAddrMcast), ERR_SUCCESS);
213     VERIFY_SUCCESS(OCInitUDPMulticast( &ipAddrMcast, &sfd), ERR_SUCCESS);
214     for (int i = 2; i < 300; ) {
215         VERIFY_SUCCESS(OCSendTo(sfd, TEST_BUF, i, 0, &ipAddrMcast), i);
216         i = i + 12;
217
218         delay(100);
219     }
220     VERIFY_SUCCESS(OCClose( sfd), ERR_SUCCESS);
221     OC_LOG(DEBUG, MOD_NAME, PCF("test70 - Completed"));
222 }
223
224
225
226 //End to End - Unicast Send/Recv test
227 //Start sample app 'sendrecv' on different Linux box.
228 void test80() {
229     OCDevAddr ipAddr, peerAddr;
230     int32_t sfd;
231     int32_t recvLen;
232     uint8_t buf[MAX_BUF_SIZE];
233     uint8_t pktrecv = 0;
234
235     //Unicast
236     VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM, &ipAddr), ERR_SUCCESS);
237     VERIFY_SUCCESS(OCInitUDP( &ipAddr, &sfd), ERR_SUCCESS);
238
239     do {
240         recvLen = OCRecvFrom(sfd, buf, MAX_BUF_SIZE, 0, &peerAddr);
241         if (recvLen > 0) {
242             pktrecv++;
243             OC_LOG(DEBUG, MOD_NAME, PCF("Rcvd data from :"));
244             OC_LOG_BUFFER(INFO, MOD_NAME, peerAddr.addr, peerAddr.size);
245             OC_LOG(DEBUG, MOD_NAME, PCF("Data Length :"));
246             OC_LOG_BUFFER(INFO, MOD_NAME, (uint8_t*)&recvLen, sizeof(recvLen));
247
248             VERIFY_SUCCESS(OCSendTo(sfd, buf, recvLen, 0, &peerAddr), recvLen);
249         } else {
250             OC_LOG(DEBUG, MOD_NAME, PCF("No data received"));
251         }
252         delay(500); //delay  secs
253     } while (pktrecv < TEST_NUM_PKTS);
254
255     VERIFY_SUCCESS(OCClose(sfd), ERR_SUCCESS);
256     OC_LOG(DEBUG, MOD_NAME, PCF("test80 - Completed"));
257 }
258
259
260 //End to End - Multicast Send/Recv test
261 //Start sample app 'sendrecv' on different Linux box.
262 void test90() {
263     OCDevAddr ipAddrMcast, peerAddr;
264     int32_t sfd;
265     int32_t recvLen;
266     uint8_t buf[MAX_BUF_SIZE];
267     uint8_t pktrecv = 0;
268
269     //Multicast
270     VERIFY_SUCCESS(OCBuildIPv4Address( MULTICAST_IP_ADDR[0], MULTICAST_IP_ADDR[1],
271                 MULTICAST_IP_ADDR[2], MULTICAST_IP_ADDR[3], TEST_PORT_NUM, &ipAddrMcast), ERR_SUCCESS);
272     VERIFY_SUCCESS(OCInitUDPMulticast( &ipAddrMcast, &sfd), ERR_SUCCESS);
273
274     do {
275
276         recvLen = OCRecvFrom(sfd, buf, MAX_BUF_SIZE, 0, &peerAddr);
277         if (recvLen > 0) {
278             pktrecv++;
279             OC_LOG(DEBUG, MOD_NAME, PCF("Rcvd data from :"));
280             OC_LOG_BUFFER(INFO, MOD_NAME, peerAddr.addr, peerAddr.size);
281             OC_LOG(DEBUG, MOD_NAME, PCF("Data Length :"));
282             OC_LOG_BUFFER(INFO, MOD_NAME, (uint8_t*)&recvLen, sizeof(recvLen));
283
284             VERIFY_SUCCESS(OCSendTo(sfd, buf, recvLen, 0, &ipAddrMcast), recvLen);
285         } else {
286             OC_LOG(DEBUG, MOD_NAME, PCF("No data received"));
287         }
288         delay(500); //delay  secs
289     } while (pktrecv < TEST_NUM_PKTS);
290
291     VERIFY_SUCCESS(OCClose(sfd), ERR_SUCCESS);
292     OC_LOG(DEBUG, MOD_NAME, PCF("test90 - Completed"));
293 }
294
295
296
297 //End to End - 'Simultaneous' Unicast-Multicast Send/Recv test
298 //This tests if Arduino Wiznet shield can open 2 independent sockets listening on same port:
299 //  one for unicast traffic and another for multicast traffic.
300 //Start sample app 'sendrecv' on different Linux box.
301 void test100() {
302     //TBD
303     //TBD
304     //TBD
305 }
306
307
308
309
310
311 void setup() {
312
313     Serial.begin(115200);
314
315     Serial.println("Trying to get an IP address using DHCP");
316     if (Ethernet.begin(ETHERNET_MAC) == 0) {
317         Serial.println("Failed to configure Ethernet using DHCP");
318     }
319 }
320
321 void loop() {
322     test10();
323     test20();
324     test30();
325     test40();
326     test50();
327     test60(); //SendTo --Unicast
328     test70(); //SendTo --Multicast
329     test80(); //End-to-End  --Unicast
330     delay(5000);
331     test90(); //End-to-End  --Multicast
332
333     do {
334         OC_LOG(DEBUG, MOD_NAME, PCF("All tests Completed"));
335         delay(10000);
336     } while (1);
337 }
338
339