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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
32 static int UNICAST_DISCOVERY = 0;
33 static int TEST_CASE = 0;
34 static const char * TEST_APP_UNICAST_DISCOVERY_QUERY = "coap://0.0.0.0:5683/oc/core";
35 static const char * TEST_APP_UNICAST_DEVICE_DISCOVERY_QUERY = "coap://0.0.0.0:5683/oc/core/d";
36 static const char * TEST_APP_MULTICAST_DEVICE_DISCOVERY_QUERY = "coap://224.0.1.187:5683/oc/core/d";
37 static std::string putPayload = "{\"state\":\"on\",\"power\":5}";
38 static std::string coapServerIP = "255.255.255.255";
39 static std::string coapServerPort = "5683";
40 static std::string coapServerResource = "/a/light";
42 // The handle for the observe registration
43 OCDoHandle gObserveDoHandle;
45 // The handle for observe registration
46 OCDoHandle gPresenceHandle;
48 // After this crosses a threshold client deregisters for further notifications
49 int gNumObserveNotifies = 0;
52 int gNumPresenceNotifies = 0;
56 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
57 void handleSigInt(int signum) {
58 if (signum == SIGINT) {
63 static void PrintUsage()
65 OC_LOG(INFO, TAG, "Usage : occlient -u <0|1> -t <1|2|3|4|5|6|7>");
66 OC_LOG(INFO, TAG, "-u <0|1> : Perform multicast/unicast discovery of resources");
67 OC_LOG(INFO, TAG, "-t 1 : Discover Resources");
68 OC_LOG(INFO, TAG, "-t 2 : Discover Resources and Initiate Nonconfirmable Get Request");
69 OC_LOG(INFO, TAG, "-t 3 : Discover Resources and Initiate Nonconfirmable Put Requests");
70 OC_LOG(INFO, TAG, "-t 4 : Discover Resources and Initiate Nonconfirmable Post Requests");
71 OC_LOG(INFO, TAG, "-t 5 : Discover Resources and Initiate Nonconfirmable Delete Requests");
72 OC_LOG(INFO, TAG, "-t 6 : Discover Resources and Initiate Nonconfirmable Observe Requests");
73 OC_LOG(INFO, TAG, "-t 7 : Discover Resources and Initiate Nonconfirmable Get Request for a resource which is unavailable");
75 OC_LOG(INFO, TAG, "-t 8 : Discover Resources and Initiate Confirmable Get Request");
76 OC_LOG(INFO, TAG, "-t 9 : Discover Resources and Initiate Confirmable Post Request");
77 OC_LOG(INFO, TAG, "-t 10 : Discover Resources and Initiate Confirmable Delete Requests");
78 OC_LOG(INFO, TAG, "-t 11 : Discover Resources and Initiate Confirmable Observe Requests");
81 OC_LOG(INFO, TAG, "-t 12 : Discover Resources and Initiate Nonconfirmable presence");
82 OC_LOG(INFO, TAG, "-t 13 : Discover Resources and Initiate Nonconfirmable presence with "\
84 OC_LOG(INFO, TAG, "-t 14 : Discover Resources and Initiate Nonconfirmable presence with "\
88 OC_LOG(INFO, TAG, "-t 15 : Discover Resources and Initiate Nonconfirmable Observe Requests "\
89 "then cancel immediately");
90 OC_LOG(INFO, TAG, "-t 16 : Discover Resources and Initiate Nonconfirmable Get Request and "\
91 "add vendor specific header options");
92 OC_LOG(INFO, TAG, "-t 17 : Discover Devices");
95 OCStackResult InvokeOCDoResource(std::ostringstream &query,
96 OCMethod method, OCQualityOfService qos,
97 OCClientResponseHandler cb, OCHeaderOption * options, uint8_t numOptions)
100 OCCallbackData cbData;
104 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
108 ret = OCDoResource(&handle, method, query.str().c_str(), 0,
109 (method == OC_REST_PUT) ? putPayload.c_str() : NULL,
110 (OC_WIFI), qos, &cbData, options, numOptions);
112 ret = OCDoResource(&handle, method, query.str().c_str(), 0,
113 (method == OC_REST_PUT) ? putPayload.c_str() : NULL,
114 qos, &cbData, options, numOptions);
117 if (ret != OC_STACK_OK)
119 OC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method);
121 else if (method == OC_REST_OBSERVE || method == OC_REST_OBSERVE_ALL)
123 gObserveDoHandle = handle;
126 else if (method == OC_REST_PRESENCE)
128 gPresenceHandle = handle;
135 OCStackApplicationResult putReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) {
136 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
138 OC_LOG(INFO, TAG, "Callback Context for PUT recvd successfully");
143 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
144 OC_LOG_V(INFO, TAG, "JSON = %s =============> Put Response", clientResponse->resJSONPayload);
146 return OC_STACK_DELETE_TRANSACTION;
149 OCStackApplicationResult postReqCB(void *ctx, OCDoHandle handle, OCClientResponse *clientResponse)
151 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
153 OC_LOG(INFO, TAG, "Callback Context for POST recvd successfully");
158 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
159 OC_LOG_V(INFO, TAG, "JSON = %s =============> Post Response", clientResponse->resJSONPayload);
161 return OC_STACK_DELETE_TRANSACTION;
164 OCStackApplicationResult deleteReqCB(void *ctx, OCDoHandle handle, OCClientResponse *clientResponse)
166 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
168 OC_LOG(INFO, TAG, "Callback Context for DELETE recvd successfully");
173 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
174 OC_LOG_V(INFO, TAG, "JSON = %s =============> Delete Response", clientResponse->resJSONPayload);
176 return OC_STACK_DELETE_TRANSACTION;
179 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
181 if(clientResponse == NULL)
183 OC_LOG(INFO, TAG, "The clientResponse is NULL");
184 return OC_STACK_DELETE_TRANSACTION;
186 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
188 OC_LOG(INFO, TAG, "Callback Context for GET query recvd successfully");
191 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
192 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
193 OC_LOG_V(INFO, TAG, "JSON = %s =============> Get Response", clientResponse->resJSONPayload);
195 if(clientResponse->rcvdVendorSpecificHeaderOptions &&
196 clientResponse->numRcvdVendorSpecificHeaderOptions)
198 OC_LOG (INFO, TAG, "Received vendor specific options");
200 OCHeaderOption * rcvdOptions = clientResponse->rcvdVendorSpecificHeaderOptions;
201 for( i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
203 if(((OCHeaderOption)rcvdOptions[i]).protocolID == OC_COAP_ID)
205 OC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with",
206 ((OCHeaderOption)rcvdOptions[i]).optionID );
207 OC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData,
208 ((OCHeaderOption)rcvdOptions[i]).optionLength);
212 return OC_STACK_DELETE_TRANSACTION;
215 OCStackApplicationResult obsReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) {
216 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
218 OC_LOG(INFO, TAG, "Callback Context for OBS query recvd successfully");
223 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
224 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
225 OC_LOG_V(INFO, TAG, "Callback Context for OBSERVE notification recvd successfully %d", gNumObserveNotifies);
226 OC_LOG_V(INFO, TAG, "JSON = %s =============> Obs Response", clientResponse->resJSONPayload);
227 gNumObserveNotifies++;
228 if (gNumObserveNotifies == 3) //large number to test observing in DELETE case.
230 if(TEST_CASE == TEST_OBS_REQ_NON || TEST_CASE == TEST_OBS_REQ_CON){
231 if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK){
232 OC_LOG(ERROR, TAG, "Observe cancel error");
234 return OC_STACK_DELETE_TRANSACTION;
235 }else if(TEST_CASE == TEST_OBS_REQ_NON_CANCEL_IMM){
236 if (OCCancel (gObserveDoHandle, OC_HIGH_QOS, NULL, 0) != OC_STACK_OK){
237 OC_LOG(ERROR, TAG, "Observe cancel error");
241 if(clientResponse->sequenceNumber == OC_OBSERVE_REGISTER){
242 OC_LOG(INFO, TAG, "This also serves as a registration confirmation");
243 }else if(clientResponse->sequenceNumber == OC_OBSERVE_DEREGISTER){
244 OC_LOG(INFO, TAG, "This also serves as a deregistration confirmation");
245 return OC_STACK_DELETE_TRANSACTION;
246 }else if(clientResponse->sequenceNumber == OC_OBSERVE_NO_OPTION){
247 OC_LOG(INFO, TAG, "This also tells you that registration/deregistration failed");
248 return OC_STACK_DELETE_TRANSACTION;
251 return OC_STACK_KEEP_TRANSACTION;
254 OCStackApplicationResult presenceCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) {
255 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
257 OC_LOG(INFO, TAG, "Callback Context for Presence recvd successfully");
262 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
263 OC_LOG_V(INFO, TAG, "NONCE NUMBER: %u", clientResponse->sequenceNumber);
264 OC_LOG_V(INFO, TAG, "Callback Context for Presence notification recvd successfully %d", gNumPresenceNotifies);
265 OC_LOG_V(INFO, TAG, "JSON = %s =============> Presence Response", clientResponse->resJSONPayload);
266 gNumPresenceNotifies++;
267 if (gNumPresenceNotifies == 20)
269 if (OCCancel (gPresenceHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK){
270 OC_LOG(ERROR, TAG, "Presence cancel error");
272 return OC_STACK_DELETE_TRANSACTION;
275 return OC_STACK_KEEP_TRANSACTION;
279 // This is a function called back when a device is discovered
280 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
281 OCClientResponse * clientResponse) {
282 uint8_t remoteIpAddr[4];
283 uint16_t remotePortNu;
285 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
287 OC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully");
292 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
294 OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr,
295 remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3);
296 OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu);
300 "Device =============> Discovered %s @ %d.%d.%d.%d:%d",
301 clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1],
302 remoteIpAddr[2], remoteIpAddr[3], remotePortNu);
305 "Device =============> Discovered %s @ %d.%d.%d.%d:%d",
306 clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1],
307 remoteIpAddr[2], remoteIpAddr[3], remotePortNu);
310 parseClientResponse(clientResponse);
314 case TEST_GET_REQ_NON:
315 InitGetRequest(OC_LOW_QOS, 0);
317 case TEST_PUT_REQ_NON:
320 case TEST_POST_REQ_NON:
321 InitPostRequest(OC_LOW_QOS);
323 case TEST_DELETE_REQ_NON:
324 InitDeleteRequest(OC_LOW_QOS);
326 case TEST_OBS_REQ_NON:
327 case TEST_OBS_REQ_NON_CANCEL_IMM:
328 InitObserveRequest(OC_LOW_QOS);
330 case TEST_GET_UNAVAILABLE_RES_REQ_NON:
331 InitGetRequestToUnavailableResource();
333 case TEST_GET_REQ_CON:
334 InitGetRequest(OC_HIGH_QOS, 0);
336 case TEST_POST_REQ_CON:
337 InitPostRequest(OC_HIGH_QOS);
339 case TEST_DELETE_REQ_CON:
340 InitDeleteRequest(OC_HIGH_QOS);
342 case TEST_OBS_REQ_CON:
343 InitObserveRequest(OC_HIGH_QOS);
346 case TEST_OBS_PRESENCE:
347 case TEST_OBS_PRESENCE_WITH_FILTER:
348 case TEST_OBS_PRESENCE_WITH_FILTERS:
352 case TEST_GET_REQ_NON_WITH_VENDOR_HEADER_OPTIONS:
353 InitGetRequest(OC_LOW_QOS, 1);
355 case TEST_DISCOVER_DEV_REQ:
356 InitDeviceDiscovery();
364 return OC_STACK_KEEP_TRANSACTION;
366 return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION ;
371 OCStackApplicationResult DeviceDiscoveryReqCB (void* ctx, OCDoHandle handle,
372 OCClientResponse * clientResponse)
374 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
376 OC_LOG(INFO, TAG, "Callback Context for Device DISCOVER query recvd successfully");
381 //OC_LOG truncates the response as it is too long.
382 fprintf(stderr, "Discovery response: \n %s\n", clientResponse->resJSONPayload);
386 return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
392 OCStackResult result = OC_STACK_OK;
393 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
394 std::ostringstream query;
395 std::ostringstream querySuffix;
396 query << "coap://" << coapServerIP << ":" << coapServerPort << OC_PRESENCE_URI;
397 if(TEST_CASE == TEST_OBS_PRESENCE)
399 result = InvokeOCDoResource(query, OC_REST_PRESENCE, OC_LOW_QOS,
400 presenceCB, NULL, 0);
402 if(TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTER || TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTERS)
405 querySuffix << query.str() << "?rt=core.light";
406 result = InvokeOCDoResource(querySuffix, OC_REST_PRESENCE, OC_LOW_QOS,
407 presenceCB, NULL, 0);
409 if(TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTERS)
411 if(result == OC_STACK_OK)
414 querySuffix << query.str() << "?rt=core.fan";
415 result = InvokeOCDoResource(querySuffix, OC_REST_PRESENCE, OC_LOW_QOS,
416 presenceCB, NULL, 0);
422 int InitGetRequestToUnavailableResource()
424 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
425 std::ostringstream query;
426 query << "coap://" << coapServerIP << ":" << coapServerPort << "/SomeUnknownResource";
427 return (InvokeOCDoResource(query, OC_REST_GET, OC_LOW_QOS, getReqCB, NULL, 0));
430 int InitObserveRequest(OCQualityOfService qos)
432 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
433 std::ostringstream query;
434 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
435 return (InvokeOCDoResource(query, OC_REST_OBSERVE, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, obsReqCB, NULL, 0));
440 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
441 std::ostringstream query;
442 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
443 return (InvokeOCDoResource(query, OC_REST_PUT, OC_LOW_QOS, putReqCB, NULL, 0));
446 int InitPostRequest(OCQualityOfService qos)
448 OCStackResult result;
449 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
450 std::ostringstream query;
451 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
453 // First POST operation (to create an Light instance)
454 result = InvokeOCDoResource(query, OC_REST_POST,
455 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
457 if (OC_STACK_OK != result)
459 // Error can happen if for example, network connectivity is down
460 OC_LOG(INFO, TAG, "First POST call did not succeed");
463 // Second POST operation (to create an Light instance)
464 result = InvokeOCDoResource(query, OC_REST_POST,
465 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
467 if (OC_STACK_OK != result)
469 OC_LOG(INFO, TAG, "Second POST call did not succeed");
472 // This POST operation will update the original resourced /a/light
473 return (InvokeOCDoResource(query, OC_REST_POST,
474 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
475 postReqCB, NULL, 0));
478 void* RequestDeleteDeathResourceTask(void* myqos)
480 sleep (30); //long enough to give the server time to finish deleting the resource.
481 std::ostringstream query;
482 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
484 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
486 // Second DELETE operation to delete the resource that might have been removed already.
487 OCQualityOfService qos;
493 OCStackResult result = InvokeOCDoResource(query, OC_REST_DELETE,
495 deleteReqCB, NULL, 0);
497 if (OC_STACK_OK != result)
499 OC_LOG(INFO, TAG, "Second DELETE call did not succeed");
505 int InitDeleteRequest(OCQualityOfService qos)
507 OCStackResult result;
508 std::ostringstream query;
509 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
511 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
513 // First DELETE operation
514 result = InvokeOCDoResource(query, OC_REST_DELETE,
516 deleteReqCB, NULL, 0);
517 if (OC_STACK_OK != result)
519 // Error can happen if for example, network connectivity is down
520 OC_LOG(INFO, TAG, "First DELETE call did not succeed");
524 //Create a thread to delete this resource again
526 pthread_create (&threadId, NULL, RequestDeleteDeathResourceTask, (void*)qos);
529 OC_LOG_V(INFO, TAG, "\n\nExit %s", __func__);
533 int InitGetRequest(OCQualityOfService qos, uint8_t withVendorSpecificHeaderOptions)
535 OCHeaderOption options[MAX_HEADER_OPTIONS];
537 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
538 std::ostringstream query;
539 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
541 if(withVendorSpecificHeaderOptions)
543 uint8_t option0[] = {1,2,3,4,5,6,7,8,9,10};
544 uint8_t option1[] = {11,12,13,14,15,16,17,18,19,20};
545 memset(options, 0, sizeof(OCHeaderOption) * MAX_HEADER_OPTIONS);
546 options[0].protocolID = OC_COAP_ID;
547 options[0].optionID = 2048;
548 memcpy(options[0].optionData, option0, sizeof(option0));
549 options[0].optionLength = 10;
550 options[1].protocolID = OC_COAP_ID;
551 options[1].optionID = 3000;
552 memcpy(options[1].optionData, option1, sizeof(option1));
553 options[1].optionLength = 10;
555 if(withVendorSpecificHeaderOptions)
557 return (InvokeOCDoResource(query, OC_REST_GET, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, getReqCB, options, 2));
561 return (InvokeOCDoResource(query, OC_REST_GET, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, getReqCB, NULL, 0));
565 int InitDeviceDiscovery()
568 OCCallbackData cbData;
570 char szQueryUri[64] = { 0 };
572 cbData.cb = DeviceDiscoveryReqCB;
573 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
576 if(UNICAST_DISCOVERY)
578 strncpy(szQueryUri, TEST_APP_UNICAST_DEVICE_DISCOVERY_QUERY,
579 (strlen(TEST_APP_UNICAST_DEVICE_DISCOVERY_QUERY) + 1));
583 strncpy(szQueryUri, TEST_APP_MULTICAST_DEVICE_DISCOVERY_QUERY,
584 (strlen(TEST_APP_MULTICAST_DEVICE_DISCOVERY_QUERY) + 1));
588 ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, (OC_ETHERNET | OC_WIFI),
589 OC_LOW_QOS, &cbData, NULL, 0);
591 ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, OC_LOW_QOS, &cbData, NULL, 0);
594 if (ret != OC_STACK_OK)
596 OC_LOG(ERROR, TAG, "OCStack device error");
605 OCCallbackData cbData;
607 /* Start a discovery query*/
608 char szQueryUri[64] = { 0 };
609 if (UNICAST_DISCOVERY)
611 strcpy(szQueryUri, TEST_APP_UNICAST_DISCOVERY_QUERY);
616 // TODO-CA CA is using 5298 for MC. Why 5298?
617 strcpy(szQueryUri, "coap://224.0.1.187:5298/oc/core");
619 strcpy(szQueryUri, OC_WELL_KNOWN_QUERY);
622 cbData.cb = discoveryReqCB;
623 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
626 ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, (OC_ETHERNET | OC_WIFI),
627 OC_LOW_QOS, &cbData, NULL, 0);
629 ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, OC_LOW_QOS, &cbData, NULL, 0);
631 if (ret != OC_STACK_OK)
633 OC_LOG(ERROR, TAG, "OCStack resource error");
638 int main(int argc, char* argv[]) {
639 uint8_t addr[20] = {0};
640 uint8_t* paddr = NULL;
641 uint16_t port = USE_RANDOM_PORT;
642 uint8_t ifname[] = "eth0";
645 while ((opt = getopt(argc, argv, "u:t:")) != -1)
650 UNICAST_DISCOVERY = atoi(optarg);
653 TEST_CASE = atoi(optarg);
661 if ((UNICAST_DISCOVERY != 0 && UNICAST_DISCOVERY != 1) ||
662 (TEST_CASE < TEST_DISCOVER_REQ || TEST_CASE >= MAX_TESTS) )
669 /*Get Ip address on defined interface and initialize coap on it with random port number
670 * this port number will be used as a source port in all coap communications*/
671 if ( OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr,
672 sizeof(addr)) == ERR_SUCCESS)
674 OC_LOG_V(INFO, TAG, "Starting occlient on address %s",addr);
678 /* Initialize OCStack*/
679 if (OCInit((char *) paddr, port, OC_CLIENT) != OC_STACK_OK) {
680 OC_LOG(ERROR, TAG, "OCStack init error");
686 // Break from loop with Ctrl+C
687 OC_LOG(INFO, TAG, "Entering occlient main loop...");
688 signal(SIGINT, handleSigInt);
691 if (OCProcess() != OC_STACK_OK) {
692 OC_LOG(ERROR, TAG, "OCStack process error");
698 OC_LOG(INFO, TAG, "Exiting occlient main loop...");
700 if (OCStop() != OC_STACK_OK) {
701 OC_LOG(ERROR, TAG, "OCStack stop error");
707 std::string getIPAddrTBServer(OCClientResponse * clientResponse) {
708 if(!clientResponse) return "";
709 if(!clientResponse->addr) return "";
710 uint8_t a, b, c, d = 0;
711 if(0 != OCDevAddrToIPv4Addr(clientResponse->addr, &a, &b, &c, &d) ) return "";
713 char ipaddr[16] = {'\0'};
714 snprintf(ipaddr, sizeof(ipaddr), "%d.%d.%d.%d", a,b,c,d); // ostringstream not working correctly here, hence snprintf
715 return std::string (ipaddr);
718 std::string getPortTBServer(OCClientResponse * clientResponse){
719 if(!clientResponse) return "";
720 if(!clientResponse->addr) return "";
722 if(0 != OCDevAddrToPort(clientResponse->addr, &p) ) return "";
723 std::ostringstream ss;
728 std::string getQueryStrForGetPut(OCClientResponse * clientResponse){
733 void parseClientResponse(OCClientResponse * clientResponse){
734 coapServerIP = getIPAddrTBServer(clientResponse);
735 coapServerPort = getPortTBServer(clientResponse);
736 coapServerResource = getQueryStrForGetPut(clientResponse);