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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
27 /// WiFi Shield firmware with Intel patches
28 static const char INTEL_WIFI_SHIELD_FW_VER[] = "1.2.0";
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};
33 /// Port number of the peer whom I wish to send/recv some data
34 #define TEST_PORT_NUM (4097)
37 #define MAX_BUF_SIZE (1024)
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,
82 unsigned int TEST_BUF_LEN = sizeof(TEST_BUF);
84 #define MOD_NAME PCF("ocsocket_test")
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 !!!!"));} }
89 /// WiFi network info and credentials
90 char ssid[] = "mDNSAP";
91 char pass[] = "letmein9";
92 int status = WL_IDLE_STATUS;
94 //Start sample app 'sendrecv' on different Linux box.
95 void SendReceiveTest()
97 Serial.println("entering SendReceiveTest...");
98 OCDevAddr ipAddr, peerAddr, recvAddr;
101 uint8_t buf[MAX_BUF_SIZE];
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++)
113 OC_LOG_V(DEBUG, MOD_NAME, "--------------------- i --------------- %d", i);
114 VERIFY_SUCCESS(OCSendTo(sfd, TEST_BUF, i + 10, 0, &peerAddr), i+10);
117 recvLen = OCRecvFrom(sfd, buf, MAX_BUF_SIZE, 0, &recvAddr);
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 :"));
129 OC_LOG_BUFFER(INFO, MOD_NAME, buf, recvLen);
138 OC_LOG_BUFFER(INFO, MOD_NAME, buf + idx, 255);
143 OC_LOG_BUFFER(INFO, MOD_NAME, buf + idx, rem);
152 VERIFY_SUCCESS(OCClose( sfd), ERR_SUCCESS);
153 OC_LOG(DEBUG, MOD_NAME, PCF("WifiTest - Completed"));
158 Serial.begin(115200);
159 Serial.println("WiFi test starting...");
161 // check for the presence of the shield:
162 if (WiFi.status() == WL_NO_SHIELD)
164 Serial.println("WiFi shield not present");
169 Serial.print("WiFi version: ");
170 Serial.println(WiFi.firmwareVersion());
172 if (strcmp(WiFi.firmwareVersion(), INTEL_WIFI_SHIELD_FW_VER) != 0)
174 Serial.println("!!!!! Upgrade WiFi Shield Firmware version !!!!!!");
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);
183 // wait 10 seconds for connection:
186 Serial.println("Connected to wifi");
188 IPAddress ip = WiFi.localIP();
189 Serial.print("IP Address: ");
194 Serial.println("entering loop...");
199 OC_LOG(DEBUG, MOD_NAME, PCF("All tests Completed"));