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 * UNICAST_DISCOVERY_QUERY = "coap://%s:6298/oc/core";
35 static const char * UNICAST_DEVICE_DISCOVERY_QUERY = "coap://%s:6298/oc/core/d";
36 static const char * MULTICAST_DEVICE_DISCOVERY_QUERY = "/oc/core/d";
37 static const char * MULTICAST_RESOURCE_DISCOVERY_QUERY = "/oc/core";
38 //The following variable determines the interface (wifi, ethernet etc.)
39 //to be used for sending unicast messages. Default set to WIFI.
40 static OCConnectivityType OC_CONNTYPE = OC_WIFI;
41 static std::string putPayload = "{\"oc\":[{\"rep\":{\"power\":15,\"state\":true}}]}";
42 static std::string coapServerIP = "255.255.255.255";
43 static std::string coapServerPort = "5683";
44 static std::string coapServerResource = "/a/light";
45 static const int IPV4_ADDR_SIZE = 16;
46 //Use ipv4addr for both InitDiscovery and InitDeviceDiscovery
47 char ipv4addr[IPV4_ADDR_SIZE];
48 void StripNewLineChar(char* str);
50 // The handle for the observe registration
51 OCDoHandle gObserveDoHandle;
53 // The handle for observe registration
54 OCDoHandle gPresenceHandle;
56 // After this crosses a threshold client deregisters for further notifications
57 int gNumObserveNotifies = 0;
60 int gNumPresenceNotifies = 0;
64 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
65 void handleSigInt(int signum)
73 static void PrintUsage()
75 OC_LOG(INFO, TAG, "Usage : occlient -u <0|1> -t <1..17> -c <0|1>");
76 OC_LOG(INFO, TAG, "-u <0|1> : Perform multicast/unicast discovery of resources");
77 OC_LOG(INFO, TAG, "-c <0|1> : Send unicast messages over Ethernet or WIFI");
78 OC_LOG(INFO, TAG, "-t 1 : Discover Resources");
79 OC_LOG(INFO, TAG, "-t 2 : Discover Resources and Initiate Nonconfirmable Get Request");
80 OC_LOG(INFO, TAG, "-t 3 : Discover Resources and Initiate Nonconfirmable Put Requests");
81 OC_LOG(INFO, TAG, "-t 4 : Discover Resources and Initiate Nonconfirmable Post Requests");
82 OC_LOG(INFO, TAG, "-t 5 : Discover Resources and Initiate Nonconfirmable Delete Requests");
83 OC_LOG(INFO, TAG, "-t 6 : Discover Resources and Initiate Nonconfirmable Observe Requests");
84 OC_LOG(INFO, TAG, "-t 7 : Discover Resources and Initiate Nonconfirmable Get Request "\
85 "for a resource which is unavailable");
86 OC_LOG(INFO, TAG, "-t 8 : Discover Resources and Initiate Confirmable Get Request");
87 OC_LOG(INFO, TAG, "-t 9 : Discover Resources and Initiate Confirmable Post Request");
88 OC_LOG(INFO, TAG, "-t 10 : Discover Resources and Initiate Confirmable Delete Requests");
89 OC_LOG(INFO, TAG, "-t 11 : Discover Resources and Initiate Confirmable Observe Requests"\
90 " and cancel with Low QoS");
93 OC_LOG(INFO, TAG, "-t 12 : Discover Resources and Initiate Nonconfirmable presence");
94 OC_LOG(INFO, TAG, "-t 13 : Discover Resources and Initiate Nonconfirmable presence with "\
96 OC_LOG(INFO, TAG, "-t 14 : Discover Resources and Initiate Nonconfirmable presence with "\
100 OC_LOG(INFO, TAG, "-t 15 : Discover Resources and Initiate Nonconfirmable Observe Requests "\
101 "then cancel immediately with High QOS");
102 OC_LOG(INFO, TAG, "-t 16 : Discover Resources and Initiate Nonconfirmable Get Request and "\
103 "add vendor specific header options");
104 OC_LOG(INFO, TAG, "-t 17 : Discover Devices");
107 OCStackResult InvokeOCDoResource(std::ostringstream &query,
109 OCQualityOfService qos,
110 OCClientResponseHandler cb,
111 OCHeaderOption * options,
115 OCCallbackData cbData;
119 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
122 ret = OCDoResource(&handle, method, query.str().c_str(), 0,
123 (method == OC_REST_PUT) ? putPayload.c_str() : NULL,
124 (OC_CONNTYPE), qos, &cbData, options, numOptions);
126 if (ret != OC_STACK_OK)
128 OC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method);
130 else if (method == OC_REST_OBSERVE || method == OC_REST_OBSERVE_ALL)
132 gObserveDoHandle = handle;
135 else if (method == OC_REST_PRESENCE)
137 gPresenceHandle = handle;
144 OCStackApplicationResult putReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
146 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
148 OC_LOG(INFO, TAG, "Callback Context for PUT recvd successfully");
153 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
154 OC_LOG_V(INFO, TAG, "JSON = %s =============> Put Response",
155 clientResponse->resJSONPayload);
159 OC_LOG_V(INFO, TAG, "putReqCB received Null clientResponse");
161 return OC_STACK_DELETE_TRANSACTION;
164 OCStackApplicationResult postReqCB(void *ctx, OCDoHandle handle, OCClientResponse *clientResponse)
166 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
168 OC_LOG(INFO, TAG, "Callback Context for POST recvd successfully");
173 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
174 OC_LOG_V(INFO, TAG, "JSON = %s =============> Post Response",
175 clientResponse->resJSONPayload);
179 OC_LOG_V(INFO, TAG, "postReqCB received Null clientResponse");
181 return OC_STACK_DELETE_TRANSACTION;
184 OCStackApplicationResult deleteReqCB(void *ctx,
185 OCDoHandle handle, OCClientResponse *clientResponse)
187 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
189 OC_LOG(INFO, TAG, "Callback Context for DELETE recvd successfully");
194 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
195 OC_LOG_V(INFO, TAG, "JSON = %s =============> Delete Response",
196 clientResponse->resJSONPayload);
200 OC_LOG_V(INFO, TAG, "deleteReqCB received Null clientResponse");
202 return OC_STACK_DELETE_TRANSACTION;
205 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
207 if(clientResponse == NULL)
209 OC_LOG(INFO, TAG, "getReqCB received NULL clientResponse");
210 return OC_STACK_DELETE_TRANSACTION;
213 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
215 OC_LOG(INFO, TAG, "Callback Context for GET query recvd successfully");
218 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
219 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
220 OC_LOG_V(INFO, TAG, "JSON = %s =============> Get Response", clientResponse->resJSONPayload);
222 if(clientResponse->rcvdVendorSpecificHeaderOptions &&
223 clientResponse->numRcvdVendorSpecificHeaderOptions)
225 OC_LOG (INFO, TAG, "Received vendor specific options");
227 OCHeaderOption * rcvdOptions = clientResponse->rcvdVendorSpecificHeaderOptions;
228 for( i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
230 if(((OCHeaderOption)rcvdOptions[i]).protocolID == OC_COAP_ID)
232 OC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with",
233 ((OCHeaderOption)rcvdOptions[i]).optionID );
235 OC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData,
236 MAX_HEADER_OPTION_DATA_LENGTH);
240 return OC_STACK_DELETE_TRANSACTION;
243 OCStackApplicationResult obsReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
245 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
247 OC_LOG(INFO, TAG, "Callback Context for OBS query recvd successfully");
252 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
253 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
254 OC_LOG_V(INFO, TAG, "Callback Context for OBSERVE notification recvd successfully %d",
255 gNumObserveNotifies);
256 OC_LOG_V(INFO, TAG, "JSON = %s =============> Obs Response",
257 clientResponse->resJSONPayload);
258 gNumObserveNotifies++;
259 if (gNumObserveNotifies == 15) //large number to test observing in DELETE case.
261 if(TEST_CASE == TEST_OBS_REQ_NON || TEST_CASE == TEST_OBS_REQ_CON)
263 if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
265 OC_LOG(ERROR, TAG, "Observe cancel error");
267 return OC_STACK_DELETE_TRANSACTION;
269 else if(TEST_CASE == TEST_OBS_REQ_NON_CANCEL_IMM)
271 if (OCCancel (gObserveDoHandle, OC_HIGH_QOS, NULL, 0) != OC_STACK_OK)
273 OC_LOG(ERROR, TAG, "Observe cancel error");
277 if(clientResponse->sequenceNumber == OC_OBSERVE_REGISTER)
279 OC_LOG(INFO, TAG, "This also serves as a registration confirmation");
281 else if(clientResponse->sequenceNumber == OC_OBSERVE_DEREGISTER)
283 OC_LOG(INFO, TAG, "This also serves as a deregistration confirmation");
284 return OC_STACK_DELETE_TRANSACTION;
286 else if(clientResponse->sequenceNumber == OC_OBSERVE_NO_OPTION)
288 OC_LOG(INFO, TAG, "This also tells you that registration/deregistration failed");
289 return OC_STACK_DELETE_TRANSACTION;
294 OC_LOG_V(INFO, TAG, "obsReqCB received Null clientResponse");
296 return OC_STACK_KEEP_TRANSACTION;
299 OCStackApplicationResult presenceCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
301 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
303 OC_LOG(INFO, TAG, "Callback Context for Presence recvd successfully");
308 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
309 OC_LOG_V(INFO, TAG, "NONCE NUMBER: %u", clientResponse->sequenceNumber);
310 OC_LOG_V(INFO, TAG, "Callback Context for Presence notification recvd successfully %d",
311 gNumPresenceNotifies);
312 OC_LOG_V(INFO, TAG, "JSON = %s =============> Presence Response",
313 clientResponse->resJSONPayload);
314 gNumPresenceNotifies++;
315 if (gNumPresenceNotifies == 20)
317 if (OCCancel(gPresenceHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
319 OC_LOG(ERROR, TAG, "Presence cancel error");
321 return OC_STACK_DELETE_TRANSACTION;
326 OC_LOG_V(INFO, TAG, "presenceCB received Null clientResponse");
328 return OC_STACK_KEEP_TRANSACTION;
332 // This is a function called back when a device is discovered
333 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
334 OCClientResponse * clientResponse)
336 uint8_t remoteIpAddr[4];
337 uint16_t remotePortNu;
339 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
341 OC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully");
346 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
348 OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr,
349 remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3);
350 OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu);
352 std::string connectionType = getConnectivityType (clientResponse->connType);
353 OC_LOG_V(INFO, TAG, "Discovered on %s", connectionType.c_str());
355 "Device =============> Discovered %s @ %d.%d.%d.%d:%d",
356 clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1],
357 remoteIpAddr[2], remoteIpAddr[3], remotePortNu);
359 parseClientResponse(clientResponse);
363 case TEST_GET_REQ_NON:
364 InitGetRequest(OC_LOW_QOS, 0);
366 case TEST_PUT_REQ_NON:
367 InitPutRequest(OC_LOW_QOS);
369 case TEST_POST_REQ_NON:
370 InitPostRequest(OC_LOW_QOS);
372 case TEST_DELETE_REQ_NON:
373 InitDeleteRequest(OC_LOW_QOS);
375 case TEST_OBS_REQ_NON:
376 case TEST_OBS_REQ_NON_CANCEL_IMM:
377 InitObserveRequest(OC_LOW_QOS);
379 case TEST_GET_UNAVAILABLE_RES_REQ_NON:
380 InitGetRequestToUnavailableResource(OC_LOW_QOS);
382 case TEST_GET_REQ_CON:
383 InitGetRequest(OC_HIGH_QOS, 0);
385 case TEST_POST_REQ_CON:
386 InitPostRequest(OC_HIGH_QOS);
388 case TEST_DELETE_REQ_CON:
389 InitDeleteRequest(OC_HIGH_QOS);
391 case TEST_OBS_REQ_CON:
392 InitObserveRequest(OC_HIGH_QOS);
395 case TEST_OBS_PRESENCE:
396 case TEST_OBS_PRESENCE_WITH_FILTER:
397 case TEST_OBS_PRESENCE_WITH_FILTERS:
401 case TEST_GET_REQ_NON_WITH_VENDOR_HEADER_OPTIONS:
402 InitGetRequest(OC_LOW_QOS, 1);
404 case TEST_DISCOVER_DEV_REQ:
405 InitDeviceDiscovery(OC_LOW_QOS);
414 OC_LOG_V(INFO, TAG, "discoveryReqCB received Null clientResponse");
416 return OC_STACK_KEEP_TRANSACTION;
419 OCStackApplicationResult DeviceDiscoveryReqCB (void* ctx, OCDoHandle handle,
420 OCClientResponse * clientResponse)
422 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
424 OC_LOG(INFO, TAG, "Callback Context for Device DISCOVER query recvd successfully");
429 //OC_LOG truncates the response as it is too long.
430 fprintf(stderr, "Discovery response: \n %s\n", clientResponse->resJSONPayload);
435 OC_LOG_V(INFO, TAG, "DeviceDiscoveryReqCB received Null clientResponse");
438 return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
444 OCStackResult result = OC_STACK_OK;
445 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
446 std::ostringstream query;
447 std::ostringstream querySuffix;
448 query << "coap://" << coapServerIP << ":" << coapServerPort << OC_PRESENCE_URI;
449 if(TEST_CASE == TEST_OBS_PRESENCE)
451 result = InvokeOCDoResource(query, OC_REST_PRESENCE, OC_LOW_QOS,
452 presenceCB, NULL, 0);
454 if(TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTER || TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTERS)
457 querySuffix << query.str() << "?rt=core.led";
458 result = InvokeOCDoResource(querySuffix, OC_REST_PRESENCE, OC_LOW_QOS,
459 presenceCB, NULL, 0);
461 if(TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTERS)
463 if(result == OC_STACK_OK)
466 querySuffix << query.str() << "?rt=core.fan";
467 result = InvokeOCDoResource(querySuffix, OC_REST_PRESENCE, OC_LOW_QOS,
468 presenceCB, NULL, 0);
475 int InitGetRequestToUnavailableResource(OCQualityOfService qos)
477 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
478 std::ostringstream query;
479 query << "coap://" << coapServerIP << ":" << coapServerPort << "/SomeUnknownResource";
480 return (InvokeOCDoResource(query, OC_REST_GET, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
484 int InitObserveRequest(OCQualityOfService qos)
486 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
487 std::ostringstream query;
488 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
489 return (InvokeOCDoResource(query,
490 OC_REST_OBSERVE, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, obsReqCB, NULL, 0));
493 int InitPutRequest(OCQualityOfService qos)
495 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
496 std::ostringstream query;
497 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
498 return (InvokeOCDoResource(query, OC_REST_PUT, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
502 int InitPostRequest(OCQualityOfService qos)
504 OCStackResult result;
505 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
506 std::ostringstream query;
507 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
509 // First POST operation (to create an Light instance)
510 result = InvokeOCDoResource(query, OC_REST_POST,
511 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
513 if (OC_STACK_OK != result)
515 // Error can happen if for example, network connectivity is down
516 OC_LOG(INFO, TAG, "First POST call did not succeed");
519 // Second POST operation (to create an Light instance)
520 result = InvokeOCDoResource(query, OC_REST_POST,
521 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
523 if (OC_STACK_OK != result)
525 OC_LOG(INFO, TAG, "Second POST call did not succeed");
528 // This POST operation will update the original resourced /a/light
529 return (InvokeOCDoResource(query, OC_REST_POST,
530 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
531 postReqCB, NULL, 0));
534 void* RequestDeleteDeathResourceTask(void* myqos)
536 sleep (30);//long enough to give the server time to finish deleting the resource.
537 std::ostringstream query;
538 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
540 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
542 // Second DELETE operation to delete the resource that might have been removed already.
543 OCQualityOfService qos;
553 OCStackResult result = InvokeOCDoResource(query, OC_REST_DELETE,
555 deleteReqCB, NULL, 0);
557 if (OC_STACK_OK != result)
559 OC_LOG(INFO, TAG, "Second DELETE call did not succeed");
565 int InitDeleteRequest(OCQualityOfService qos)
567 OCStackResult result;
568 std::ostringstream query;
569 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
571 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
573 // First DELETE operation
574 result = InvokeOCDoResource(query, OC_REST_DELETE,
576 deleteReqCB, NULL, 0);
577 if (OC_STACK_OK != result)
579 // Error can happen if for example, network connectivity is down
580 OC_LOG(INFO, TAG, "First DELETE call did not succeed");
584 //Create a thread to delete this resource again
586 pthread_create (&threadId, NULL, RequestDeleteDeathResourceTask, (void*)qos);
589 OC_LOG_V(INFO, TAG, "\n\nExit %s", __func__);
593 int InitGetRequest(OCQualityOfService qos, uint8_t withVendorSpecificHeaderOptions)
595 OCHeaderOption options[MAX_HEADER_OPTIONS];
597 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
598 std::ostringstream query;
599 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
601 if (withVendorSpecificHeaderOptions)
603 uint8_t option0[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
604 uint8_t option1[] = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
605 memset(options, 0, sizeof(OCHeaderOption) * MAX_HEADER_OPTIONS);
606 options[0].protocolID = OC_COAP_ID;
607 options[0].optionID = 2048;
608 memcpy(options[0].optionData, option0, sizeof(option0));
609 options[0].optionLength = 10;
610 options[1].protocolID = OC_COAP_ID;
611 options[1].optionID = 3000;
612 memcpy(options[1].optionData, option1, sizeof(option1));
613 options[1].optionLength = 10;
615 if (withVendorSpecificHeaderOptions)
617 return (InvokeOCDoResource(query, OC_REST_GET,
618 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, options, 2));
622 return (InvokeOCDoResource(query, OC_REST_GET,
623 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, NULL, 0));
627 int InitDeviceDiscovery(OCQualityOfService qos)
630 OCCallbackData cbData;
631 char szQueryUri[64] = { 0 };
633 cbData.cb = DeviceDiscoveryReqCB;
634 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
637 if(UNICAST_DISCOVERY)
639 snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_DEVICE_DISCOVERY_QUERY, ipv4addr);
643 strncpy(szQueryUri, MULTICAST_DEVICE_DISCOVERY_QUERY,
644 (strlen(MULTICAST_DEVICE_DISCOVERY_QUERY) + 1));
647 if(UNICAST_DISCOVERY)
649 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, OC_CONNTYPE,
650 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
654 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, (OC_ALL),
655 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
658 if (ret != OC_STACK_OK)
660 OC_LOG(ERROR, TAG, "OCStack device error");
666 int InitDiscovery(OCQualityOfService qos)
669 OCCallbackData cbData;
670 /* Start a discovery query*/
671 char szQueryUri[64] = { 0 };
673 if (UNICAST_DISCOVERY)
675 snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_DISCOVERY_QUERY, ipv4addr);
679 strcpy(szQueryUri, MULTICAST_RESOURCE_DISCOVERY_QUERY);
682 cbData.cb = discoveryReqCB;
683 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
685 if(UNICAST_DISCOVERY)
687 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, OC_CONNTYPE,
688 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
692 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, (OC_ALL),
693 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
695 if (ret != OC_STACK_OK)
697 OC_LOG(ERROR, TAG, "OCStack resource error");
702 int main(int argc, char* argv[])
706 while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
711 UNICAST_DISCOVERY = atoi(optarg);
714 TEST_CASE = atoi(optarg);
717 OC_CONNTYPE = OCConnectivityType(atoi(optarg));
725 if ((UNICAST_DISCOVERY != 0 && UNICAST_DISCOVERY != 1) ||
726 (TEST_CASE < TEST_DISCOVER_REQ || TEST_CASE >= MAX_TESTS) )
732 /* Initialize OCStack*/
733 if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK)
735 OC_LOG(ERROR, TAG, "OCStack init error");
738 if (UNICAST_DISCOVERY)
740 printf("Enter IPv4 address of the Server hosting resource (Ex: 192.168.0.15)\n");
741 if (fgets(ipv4addr, IPV4_ADDR_SIZE, stdin))
743 //Strip newline char from ipv4addr
744 StripNewLineChar(ipv4addr);
748 OC_LOG(ERROR, TAG, "!! Bad input for IPV4 address. !!");
749 return OC_STACK_INVALID_PARAM;
753 if(UNICAST_DISCOVERY == 0 && TEST_CASE == TEST_DISCOVER_DEV_REQ)
755 InitDeviceDiscovery(OC_LOW_QOS);
759 InitDiscovery(OC_LOW_QOS);
762 // Break from loop with Ctrl+C
763 OC_LOG(INFO, TAG, "Entering occlient main loop...");
764 signal(SIGINT, handleSigInt);
768 if (OCProcess() != OC_STACK_OK)
770 OC_LOG(ERROR, TAG, "OCStack process error");
776 OC_LOG(INFO, TAG, "Exiting occlient main loop...");
778 if (OCStop() != OC_STACK_OK)
780 OC_LOG(ERROR, TAG, "OCStack stop error");
786 std::string getIPAddrTBServer(OCClientResponse * clientResponse)
792 if (!clientResponse->addr)
796 uint8_t a, b, c, d = 0;
797 if (0 != OCDevAddrToIPv4Addr(clientResponse->addr, &a, &b, &c, &d))
802 char ipaddr[16] = {'\0'};
803 // ostringstream not working correctly here, hence snprintf
804 snprintf(ipaddr, sizeof(ipaddr), "%d.%d.%d.%d", a,b,c,d);
805 return std::string (ipaddr);
808 std::string getPortTBServer(OCClientResponse * clientResponse)
814 if (!clientResponse->addr)
819 if (0 != OCDevAddrToPort(clientResponse->addr, &p))
823 std::ostringstream ss;
828 std::string getConnectivityType (OCConnectivityType connType)
845 return "Incorrect connectivity";
849 std::string getQueryStrForGetPut(OCClientResponse * clientResponse)
855 void parseClientResponse(OCClientResponse * clientResponse)
857 coapServerIP = getIPAddrTBServer(clientResponse);
858 coapServerPort = getPortTBServer(clientResponse);
859 coapServerResource = getQueryStrForGetPut(clientResponse);