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 !!!!"));} }
90 //OCBuildIPv4Address tests
93 VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM, &ipAddr), ERR_SUCCESS);
94 VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM, NULL), ERR_INVALID_INPUT);
95 OC_LOG(DEBUG, MOD_NAME, PCF("test20 - Completed"));
104 VERIFY_SUCCESS(OCBuildIPv4Address( 1, 2, 3, 4, TEST_PORT_NUM, &ipAddr), ERR_SUCCESS);
105 VERIFY_SUCCESS(OCDevAddrToIPv4Addr( &ipAddr, &a, &b, &c, &d), ERR_SUCCESS);
106 if ((a != 1) || (b != 2) || (c !=3) || (d !=4)) {
107 OC_LOG(DEBUG, MOD_NAME, PCF("test30 - Failed !!!"));
111 VERIFY_SUCCESS(OCDevAddrToPort( &ipAddr, &port), ERR_SUCCESS);
112 if (port != TEST_PORT_NUM) {
113 OC_LOG(DEBUG, MOD_NAME, PCF("test30 - Failed !!!"));
116 VERIFY_SUCCESS(OCDevAddrToIPv4Addr( NULL, &a, &b, &c, &d), ERR_INVALID_INPUT);
117 VERIFY_SUCCESS(OCDevAddrToPort( &ipAddr, NULL), ERR_INVALID_INPUT);
118 OC_LOG(DEBUG, MOD_NAME, PCF("test30 - Completed"));
124 int32_t sfd1, sfd2, sfd3, sfd4, sfd5;
126 //We should be able to successfully open 4 sockets.
127 VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM, &ipAddr), ERR_SUCCESS);
128 VERIFY_SUCCESS(OCInitUDP( &ipAddr, &sfd1), ERR_SUCCESS);
130 VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM + 1, &ipAddr), ERR_SUCCESS);
131 VERIFY_SUCCESS(OCInitUDP( &ipAddr, &sfd2), ERR_SUCCESS);
133 VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM + 2, &ipAddr), ERR_SUCCESS);
134 VERIFY_SUCCESS(OCInitUDP( &ipAddr, &sfd3), ERR_SUCCESS);
136 VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM + 3, &ipAddr), ERR_SUCCESS);
137 VERIFY_SUCCESS(OCInitUDP( &ipAddr, &sfd4), ERR_SUCCESS);
139 //5th socket creation call should FAIL
140 VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM + 4, &ipAddr), ERR_SUCCESS);
141 VERIFY_SUCCESS(OCInitUDP( &ipAddr, &sfd5), ERR_UNKNOWN);
143 VERIFY_SUCCESS(OCClose( sfd4), ERR_SUCCESS);
144 VERIFY_SUCCESS(OCInitUDP( &ipAddr, &sfd5), ERR_SUCCESS);
146 VERIFY_SUCCESS(OCClose( sfd1), ERR_SUCCESS);
147 VERIFY_SUCCESS(OCClose( sfd2), ERR_SUCCESS);
148 VERIFY_SUCCESS(OCClose( sfd3), ERR_SUCCESS);
149 VERIFY_SUCCESS(OCClose( sfd5), ERR_SUCCESS);
150 OC_LOG(DEBUG, MOD_NAME, PCF("test40 - Completed"));
153 //OCSendTo -- Unicast tests
155 OCDevAddr ipAddr, peerAddr;
158 VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM, &ipAddr), ERR_SUCCESS);
159 VERIFY_SUCCESS(OCBuildIPv4Address(PEER_IP_ADDR[0], PEER_IP_ADDR[0],
160 PEER_IP_ADDR[0], PEER_IP_ADDR[0], TEST_PORT_NUM, &peerAddr), ERR_SUCCESS);
162 VERIFY_SUCCESS(OCInitUDP( &ipAddr, &sfd), ERR_SUCCESS);
163 for (int i = 2; i < 300; ) {
164 VERIFY_SUCCESS(OCSendTo(sfd, TEST_BUF, i, 0, &peerAddr), i);
169 VERIFY_SUCCESS(OCClose( sfd), ERR_SUCCESS);
170 OC_LOG(DEBUG, MOD_NAME, PCF("test60 - Completed"));
173 //End to End - Unicast Send/Recv test
174 //Start sample app 'sendrecv' on different Linux box.
176 OCDevAddr ipAddr, peerAddr;
179 uint8_t buf[MAX_BUF_SIZE];
183 VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM, &ipAddr), ERR_SUCCESS);
184 VERIFY_SUCCESS(OCInitUDP( &ipAddr, &sfd), ERR_SUCCESS);
187 recvLen = OCRecvFrom(sfd, buf, MAX_BUF_SIZE, 0, &peerAddr);
190 OC_LOG(DEBUG, MOD_NAME, PCF("Rcvd data from :"));
191 OC_LOG_BUFFER(INFO, MOD_NAME, peerAddr.addr, peerAddr.size);
192 OC_LOG(DEBUG, MOD_NAME, PCF("Data Length :"));
193 OC_LOG_BUFFER(INFO, MOD_NAME, (uint8_t*)&recvLen, sizeof(recvLen));
195 VERIFY_SUCCESS(OCSendTo(sfd, buf, recvLen, 0, &peerAddr), recvLen);
197 OC_LOG(DEBUG, MOD_NAME, PCF("No data received"));
199 delay(500); //delay secs
200 } while (pktrecv < TEST_NUM_PKTS);
202 VERIFY_SUCCESS(OCClose(sfd), ERR_SUCCESS);
203 OC_LOG(DEBUG, MOD_NAME, PCF("test80 - Completed"));
207 //End to End - Multicast Send/Recv test
208 //Start sample app 'sendrecv' on different Linux box.
210 OCDevAddr ipAddrMcast, peerAddr;
213 uint8_t buf[MAX_BUF_SIZE];
217 VERIFY_SUCCESS(OCBuildIPv4Address( MULTICAST_IP_ADDR[0], MULTICAST_IP_ADDR[1],
218 MULTICAST_IP_ADDR[2], MULTICAST_IP_ADDR[3], TEST_PORT_NUM, &ipAddrMcast), ERR_SUCCESS);
222 recvLen = OCRecvFrom(sfd, buf, MAX_BUF_SIZE, 0, &peerAddr);
225 OC_LOG(DEBUG, MOD_NAME, PCF("Rcvd data from :"));
226 OC_LOG_BUFFER(INFO, MOD_NAME, peerAddr.addr, peerAddr.size);
227 OC_LOG(DEBUG, MOD_NAME, PCF("Data Length :"));
228 OC_LOG_BUFFER(INFO, MOD_NAME, (uint8_t*)&recvLen, sizeof(recvLen));
230 VERIFY_SUCCESS(OCSendTo(sfd, buf, recvLen, 0, &ipAddrMcast), recvLen);
232 OC_LOG(DEBUG, MOD_NAME, PCF("No data received"));
234 delay(500); //delay secs
235 } while (pktrecv < TEST_NUM_PKTS);
237 VERIFY_SUCCESS(OCClose(sfd), ERR_SUCCESS);
238 OC_LOG(DEBUG, MOD_NAME, PCF("test90 - Completed"));
243 //End to End - 'Simultaneous' Unicast-Multicast Send/Recv test
244 //This tests if Arduino Wiznet shield can open 2 independent sockets listening on same port:
245 // one for unicast traffic and another for multicast traffic.
246 //Start sample app 'sendrecv' on different Linux box.
259 Serial.begin(115200);
261 Serial.println("Trying to get an IP address using DHCP");
262 if (Ethernet.begin(ETHERNET_MAC) == 0) {
263 Serial.println("Failed to configure Ethernet using DHCP");
273 test60(); //SendTo --Unicast
274 test70(); //SendTo --Multicast
275 test80(); //End-to-End --Unicast
277 test90(); //End-to-End --Multicast
280 OC_LOG(DEBUG, MOD_NAME, PCF("All tests Completed"));