1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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};
32 //Set below to 0 to disable multicast testing
33 //#define MCAST_TESTING_EN 1
35 #define TEST_NUM_PKTS (20)
36 #define TEST_PORT_NUM (8888)
37 #define MAX_BUF_SIZE (256)
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,
81 unsigned int TEST_BUF_LEN = sizeof(TEST_BUF);
83 #define PCF(str) ((PROGMEM const char *)(F(str)))
85 #define MOD_NAME PCF("ocsocket_test")
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 !!!!"));} }
91 //OCGetInterfaceAddress tests
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));
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"));
103 //OCBuildIPv4Address tests
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"));
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 !!!"));
124 VERIFY_SUCCESS(OCDevAddrToPort( &ipAddr, &port), ERR_SUCCESS);
125 if (port != TEST_PORT_NUM) {
126 OC_LOG(DEBUG, MOD_NAME, PCF("test30 - Failed !!!"));
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"));
137 int32_t sfd1, sfd2, sfd3, sfd4, sfd5;
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);
143 VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM + 1, &ipAddr), ERR_SUCCESS);
144 VERIFY_SUCCESS(OCInitUDP( &ipAddr, &sfd2), ERR_SUCCESS);
146 VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM + 2, &ipAddr), ERR_SUCCESS);
147 VERIFY_SUCCESS(OCInitUDP( &ipAddr, &sfd3), ERR_SUCCESS);
149 VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM + 3, &ipAddr), ERR_SUCCESS);
150 VERIFY_SUCCESS(OCInitUDP( &ipAddr, &sfd4), ERR_SUCCESS);
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);
156 VERIFY_SUCCESS(OCClose( sfd4), ERR_SUCCESS);
157 VERIFY_SUCCESS(OCInitUDP( &ipAddr, &sfd5), ERR_SUCCESS);
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"));
167 //OCInitUDPMulticast tests
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);
176 VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM + 1, &ipAddr), ERR_SUCCESS);
177 VERIFY_SUCCESS(OCInitUDP( &ipAddr, &sfd2), ERR_SUCCESS);
179 VERIFY_SUCCESS(OCClose( sfd1), ERR_SUCCESS);
180 VERIFY_SUCCESS(OCClose( sfd2), ERR_SUCCESS);
181 OC_LOG(DEBUG, MOD_NAME, PCF("test50 - Completed"));
185 //OCSendTo -- Unicast tests
187 OCDevAddr ipAddr, peerAddr;
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);
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);
201 VERIFY_SUCCESS(OCClose( sfd), ERR_SUCCESS);
202 OC_LOG(DEBUG, MOD_NAME, PCF("test60 - Completed"));
206 //OCSendTo -- Multicast tests
208 OCDevAddr ipAddrMcast;
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);
220 VERIFY_SUCCESS(OCClose( sfd), ERR_SUCCESS);
221 OC_LOG(DEBUG, MOD_NAME, PCF("test70 - Completed"));
226 //End to End - Unicast Send/Recv test
227 //Start sample app 'sendrecv' on different Linux box.
229 OCDevAddr ipAddr, peerAddr;
232 uint8_t buf[MAX_BUF_SIZE];
236 VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM, &ipAddr), ERR_SUCCESS);
237 VERIFY_SUCCESS(OCInitUDP( &ipAddr, &sfd), ERR_SUCCESS);
240 recvLen = OCRecvFrom(sfd, buf, MAX_BUF_SIZE, 0, &peerAddr);
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));
248 VERIFY_SUCCESS(OCSendTo(sfd, buf, recvLen, 0, &peerAddr), recvLen);
250 OC_LOG(DEBUG, MOD_NAME, PCF("No data received"));
252 delay(500); //delay secs
253 } while (pktrecv < TEST_NUM_PKTS);
255 VERIFY_SUCCESS(OCClose(sfd), ERR_SUCCESS);
256 OC_LOG(DEBUG, MOD_NAME, PCF("test80 - Completed"));
260 //End to End - Multicast Send/Recv test
261 //Start sample app 'sendrecv' on different Linux box.
263 OCDevAddr ipAddrMcast, peerAddr;
266 uint8_t buf[MAX_BUF_SIZE];
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);
276 recvLen = OCRecvFrom(sfd, buf, MAX_BUF_SIZE, 0, &peerAddr);
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));
284 VERIFY_SUCCESS(OCSendTo(sfd, buf, recvLen, 0, &ipAddrMcast), recvLen);
286 OC_LOG(DEBUG, MOD_NAME, PCF("No data received"));
288 delay(500); //delay secs
289 } while (pktrecv < TEST_NUM_PKTS);
291 VERIFY_SUCCESS(OCClose(sfd), ERR_SUCCESS);
292 OC_LOG(DEBUG, MOD_NAME, PCF("test90 - Completed"));
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.
313 Serial.begin(115200);
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");
327 test60(); //SendTo --Unicast
328 test70(); //SendTo --Multicast
329 test80(); //End-to-End --Unicast
331 test90(); //End-to-End --Multicast
334 OC_LOG(DEBUG, MOD_NAME, PCF("All tests Completed"));