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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
30 const char *getResult(OCStackResult result);
31 std::string getIPAddrTBServer(OCClientResponse * clientResponse);
32 std::string getPortTBServer(OCClientResponse * clientResponse);
33 std::string getQueryStrForGetPut(unsigned const char * responsePayload);
35 #define TAG PCF("occlient")
36 #define DEFAULT_CONTEXT_VALUE 0x99
37 #ifndef MAX_LENGTH_IPv4_ADDR
38 #define MAX_LENGTH_IPv4_ADDR 16
50 TEST_UNKNOWN_RESOURCE_GET_DEFAULT,
51 TEST_UNKNOWN_RESOURCE_GET_BATCH,
52 TEST_UNKNOWN_RESOURCE_GET_LINK_LIST,
56 unsigned static int TEST = TEST_INVALID;
60 unsigned char text[30];
64 testToTextMap queryInterface[] = {
65 {"invalid", TEST_INVALID},
66 {"?if=oc.mi.def", TEST_GET_DEFAULT},
67 {"?if=oc.mi.b", TEST_GET_BATCH},
68 {"?if=oc.mi.ll", TEST_GET_LINK_LIST},
69 {"?if=oc.mi.def", TEST_UNKNOWN_RESOURCE_GET_DEFAULT},
70 {"?if=oc.mi.b", TEST_UNKNOWN_RESOURCE_GET_BATCH},
71 {"?if=oc.mi.ll", TEST_UNKNOWN_RESOURCE_GET_LINK_LIST},
72 {"?if=oc.mi.def", TEST_PUT_DEFAULT},
73 {"?if=oc.mi.b", TEST_PUT_BATCH},
74 {"?if=oc.mi.ll", TEST_PUT_LINK_LIST},
77 static std::string putPayload = "{\"state\":\"off\",\"power\":\"0\"}";
79 //The following variable determines the interface (wifi, ethernet etc.)
80 //to be used for sending unicast messages. Default set to WIFI.
81 static OCConnectivityType OC_CONNTYPE = OC_WIFI;
82 static const char * MULTICAST_RESOURCE_DISCOVERY_QUERY = "/oc/core";
84 // The handle for the observe registration
85 OCDoHandle gObserveDoHandle;
86 // After this crosses a threshold client deregisters for further observations
87 int gNumObserveNotifies = 1;
90 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
91 void handleSigInt(int signum)
99 // Forward Declaration
100 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse);
101 int InitGetRequestToUnavailableResource(OCClientResponse * clientResponse);
102 int InitObserveRequest(OCClientResponse * clientResponse);
103 int InitPutRequest(OCClientResponse * clientResponse);
104 int InitGetRequest(OCClientResponse * clientResponse);
109 OC_LOG(INFO, TAG, "Usage : occlientcoll -t <Test Case> -c <CA connectivity Type>");
110 OC_LOG(INFO, TAG, "-c <0|1> : Send messages over Ethernet or WIFI");
111 OC_LOG(INFO, TAG, "Test Case 1 : Discover Resources && Initiate GET Request on an"\
112 "available resource using default interface.");
113 OC_LOG(INFO, TAG, "Test Case 2 : Discover Resources && Initiate GET Request on an"\
114 "available resource using batch interface.");
115 OC_LOG(INFO, TAG, "Test Case 3 : Discover Resources && Initiate GET Request on an"\
116 "available resource using link list interface.");
117 OC_LOG(INFO, TAG, "Test Case 4 : Discover Resources && Initiate GET & PUT Request on an"\
118 "available resource using default interface.");
119 OC_LOG(INFO, TAG, "Test Case 5 : Discover Resources && Initiate GET & PUT Request on an"\
120 "available resource using batch interface.");
121 OC_LOG(INFO, TAG, "Test Case 6 : Discover Resources && Initiate GET & PUT Request on an"\
122 "available resource using link list interface.");
123 OC_LOG(INFO, TAG, "Test Case 7 : Discover Resources && Initiate GET Request on an"\
124 "unavailable resource using default interface.");
125 OC_LOG(INFO, TAG, "Test Case 8 : Discover Resources && Initiate GET Request on an"\
126 "unavailable resource using batch interface.");
127 OC_LOG(INFO, TAG, "Test Case 9 : Discover Resources && Initiate GET Request on an"\
128 "unavailable resource using link list interface.");
131 OCStackApplicationResult putReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
133 if(clientResponse == NULL)
135 OC_LOG(INFO, TAG, "The clientResponse is NULL");
136 return OC_STACK_DELETE_TRANSACTION;
138 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
140 OC_LOG_V(INFO, TAG, "Callback Context for PUT query recvd successfully");
141 OC_LOG_V(INFO, TAG, "JSON = %s =============> Discovered", clientResponse->resJSONPayload);
144 return OC_STACK_KEEP_TRANSACTION;
147 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
149 OC_LOG_V(INFO, TAG, "StackResult: %s",
150 getResult(clientResponse->result));
151 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
153 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
154 if(clientResponse->sequenceNumber == 0)
156 OC_LOG_V(INFO, TAG, "Callback Context for GET query recvd successfully");
157 OC_LOG_V(INFO, TAG, "Fnd' Rsrc': %s", clientResponse->resJSONPayload);
161 OC_LOG_V(INFO, TAG, "Callback Context for Get recvd successfully %d",
162 gNumObserveNotifies);
163 OC_LOG_V(INFO, TAG, "Fnd' Rsrc': %s", clientResponse->resJSONPayload);
164 gNumObserveNotifies++;
165 if (gNumObserveNotifies == 3)
167 if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
169 OC_LOG(ERROR, TAG, "Observe cancel error");
174 if(TEST == TEST_PUT_DEFAULT || TEST == TEST_PUT_BATCH || TEST == TEST_PUT_LINK_LIST)
176 InitPutRequest(clientResponse);
178 return OC_STACK_KEEP_TRANSACTION;
182 // This is a function called back when a device is discovered
183 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
184 OCClientResponse * clientResponse)
186 uint8_t remoteIpAddr[4];
187 uint16_t remotePortNu;
190 "Entering discoveryReqCB (Application Layer CB)");
191 OC_LOG_V(INFO, TAG, "StackResult: %s",
192 getResult(clientResponse->result));
194 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
196 OC_LOG_V(INFO, TAG, "Callback Context recvd successfully");
199 OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr,
200 remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3);
201 OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu);
204 "Device =============> Discovered %s @ %d.%d.%d.%d:%d",
205 clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1],
206 remoteIpAddr[2], remoteIpAddr[3], remotePortNu);
208 if(TEST == TEST_UNKNOWN_RESOURCE_GET_DEFAULT || TEST == TEST_UNKNOWN_RESOURCE_GET_BATCH ||\
209 TEST == TEST_UNKNOWN_RESOURCE_GET_LINK_LIST)
211 InitGetRequestToUnavailableResource(clientResponse);
215 InitGetRequest(clientResponse);
217 return OC_STACK_KEEP_TRANSACTION;
221 int InitGetRequestToUnavailableResource(OCClientResponse * clientResponse)
224 OCCallbackData cbData;
226 std::ostringstream getQuery;
227 getQuery << "coap://" << getIPAddrTBServer(clientResponse) << ":" <<
228 getPortTBServer(clientResponse) << "/SomeUnknownResource";
229 cbData.cb = getReqCB;
230 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
233 ret = OCDoResource(&handle, OC_REST_GET, getQuery.str().c_str(), 0, 0, OC_CONNTYPE, OC_LOW_QOS,
235 if (ret != OC_STACK_OK)
237 OC_LOG(ERROR, TAG, "OCStack resource error");
243 int InitObserveRequest(OCClientResponse * clientResponse)
246 OCCallbackData cbData;
248 std::ostringstream obsReg;
249 obsReg << "coap://" << getIPAddrTBServer(clientResponse) << ":" <<
250 getPortTBServer(clientResponse) <<
251 getQueryStrForGetPut(clientResponse->resJSONPayload);
252 cbData.cb = getReqCB;
253 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
255 OC_LOG_V(INFO, TAG, "OBSERVE payload from client = %s ", putPayload.c_str());
257 ret = OCDoResource(&handle, OC_REST_OBSERVE, obsReg.str().c_str(), 0, 0, OC_CONNTYPE,
258 OC_LOW_QOS, &cbData, NULL, 0);
259 if (ret != OC_STACK_OK)
261 OC_LOG(ERROR, TAG, "OCStack resource error");
265 gObserveDoHandle = handle;
271 int InitPutRequest(OCClientResponse * clientResponse)
274 OCCallbackData cbData;
276 //* Make a PUT query*/
277 std::ostringstream getQuery;
278 getQuery << "coap://" << getIPAddrTBServer(clientResponse) << ":" <<
279 getPortTBServer(clientResponse) <<
280 "/a/room" << queryInterface[TEST].text;
281 cbData.cb = putReqCB;
282 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
284 OC_LOG_V(INFO, TAG, "PUT payload from client = %s ", putPayload.c_str());
286 ret = OCDoResource(&handle, OC_REST_PUT, getQuery.str().c_str(), 0, putPayload.c_str(),
287 OC_CONNTYPE, OC_LOW_QOS, &cbData, NULL, 0);
288 if (ret != OC_STACK_OK)
290 OC_LOG(ERROR, TAG, "OCStack resource error");
296 int InitGetRequest(OCClientResponse * clientResponse)
299 OCCallbackData cbData;
302 uint8_t remoteIpAddr[4];
303 uint16_t remotePortNu;
305 OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr,
306 remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3);
307 OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu);
309 //* Make a GET query*/
310 std::ostringstream getQuery;
311 getQuery << "coap://" << getIPAddrTBServer(clientResponse) << ":" <<
312 getPortTBServer(clientResponse) <<
313 "/a/room" << queryInterface[TEST].text;
315 std::cout << "Get Query: " << getQuery.str() << std::endl;
317 cbData.cb = getReqCB;
318 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
320 ret = OCDoResource(&handle, OC_REST_GET,
321 getQuery.str().c_str(), 0, 0, OC_CONNTYPE, OC_LOW_QOS,
323 if (ret != OC_STACK_OK)
325 OC_LOG(ERROR, TAG, "OCStack resource error");
333 OCCallbackData cbData;
335 /* Start a discovery query*/
336 char szQueryUri[64] = { 0 };
338 strcpy(szQueryUri, MULTICAST_RESOURCE_DISCOVERY_QUERY);
340 cbData.cb = discoveryReqCB;
341 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
343 ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, OC_ALL,
346 if (ret != OC_STACK_OK)
348 OC_LOG(ERROR, TAG, "OCStack resource error");
353 int main(int argc, char* argv[])
357 while ((opt = getopt(argc, argv, "t:c:")) != -1)
365 OC_CONNTYPE = OCConnectivityType(atoi(optarg));
372 if (TEST <= TEST_INVALID || TEST >= MAX_TESTS)
378 /* Initialize OCStack*/
379 if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK)
381 OC_LOG(ERROR, TAG, "OCStack init error");
387 // Break from loop with Ctrl+C
388 OC_LOG(INFO, TAG, "Entering occlient main loop...");
389 signal(SIGINT, handleSigInt);
393 if (OCProcess() != OC_STACK_OK)
395 OC_LOG(ERROR, TAG, "OCStack process error");
400 } OC_LOG(INFO, TAG, "Exiting occlient main loop...");
402 if (OCStop() != OC_STACK_OK)
404 OC_LOG(ERROR, TAG, "OCStack stop error");
410 std::string getIPAddrTBServer(OCClientResponse * clientResponse)
416 if (!clientResponse->addr)
420 uint8_t a, b, c, d = 0;
421 if (0 != OCDevAddrToIPv4Addr(clientResponse->addr, &a, &b, &c, &d))
426 char ipaddr[16] = {'\0'};
427 // ostringstream not working correctly here, hence snprintf
428 snprintf(ipaddr, sizeof(ipaddr), "%d.%d.%d.%d", a,b,c,d);
429 return std::string (ipaddr);
432 std::string getPortTBServer(OCClientResponse * clientResponse)
438 if (!clientResponse->addr)
443 if (0 != OCDevAddrToPort(clientResponse->addr, &p))
447 std::ostringstream ss;
452 std::string getQueryStrForGetPut(unsigned const char * responsePayload)
455 std::string jsonPayload(reinterpret_cast<char*>(const_cast<unsigned char*>(responsePayload)));