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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
31 #include "ocpayload.h"
32 #include "payload_logging.h"
34 #ifdef ROUTING_GATEWAY
36 * Maximum number of gateway requests to form the routing table.
38 #define MAX_NUM_GATEWAY_REQUEST 20
41 * Sleep duration after every OCProcess().
43 #define SLEEP_DURATION 100000
45 // Tracking user input
46 static int UnicastDiscovery = 0;
47 static int TestCase = 0;
48 static int Connectivity = 0;
50 static const char *DEVICE_DISCOVERY_QUERY = "%s/oic/d";
51 static const char *PLATFORM_DISCOVERY_QUERY = "%s/oic/p";
52 static const char *RESOURCE_DISCOVERY_QUERY = "%s/oic/res";
54 //The following variable determines the interface protocol (IPv4, IPv6, etc)
55 //to be used for sending unicast messages. Default set to IP dual stack.
56 static OCConnectivityType ConnType = CT_ADAPTER_IP;
57 static OCDevAddr serverAddr;
58 static char discoveryAddr[100];
59 static std::string coapServerResource = "/a/light";
61 void StripNewLineChar(char* str);
64 // The handle for observe registration
65 OCDoHandle gPresenceHandle;
67 // After this crosses a threshold client deregisters for further notifications
68 int gNumObserveNotifies = 0;
71 int gNumPresenceNotifies = 0;
75 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
76 void handleSigInt(int signum)
84 OCPayload* putPayload()
86 OCRepPayload* payload = OCRepPayloadCreate();
90 std::cout << "Failed to create put payload object"<<std::endl;
94 OCRepPayloadSetPropInt(payload, "power", 15);
95 OCRepPayloadSetPropBool(payload, "state", true);
97 return (OCPayload*) payload;
100 static void PrintUsage()
102 OC_LOG(INFO, TAG, "Usage : occlient -u <0|1> -t <1..17> -c <0|1>");
103 OC_LOG(INFO, TAG, "-u <0|1> : Perform multicast/unicast discovery of resources");
104 OC_LOG(INFO, TAG, "-c 0 : Use Default connectivity(IP)");
105 OC_LOG(INFO, TAG, "-c 1 : IP Connectivity Type");
106 OC_LOG(INFO, TAG, "-t 1 : Discover Resources");
107 OC_LOG(INFO, TAG, "-t 2 : Discover Resources and Initiate Nonconfirmable Get Request");
108 OC_LOG(INFO, TAG, "-t 3 : Discover Resources and Initiate Nonconfirmable Get Request"
109 " with query filter.");
110 OC_LOG(INFO, TAG, "-t 4 : Discover Resources and Initiate Nonconfirmable Put Requests");
111 OC_LOG(INFO, TAG, "-t 5 : Discover Resources and Initiate Nonconfirmable Post Requests");
112 OC_LOG(INFO, TAG, "-t 6 : Discover Resources and Initiate Nonconfirmable Delete Requests");
113 OC_LOG(INFO, TAG, "-t 7 : Discover Resources and Initiate Nonconfirmable Observe Requests");
114 OC_LOG(INFO, TAG, "-t 8 : Discover Resources and Initiate Nonconfirmable Get Request "\
115 "for a resource which is unavailable");
116 OC_LOG(INFO, TAG, "-t 9 : Discover Resources and Initiate Confirmable Get Request");
117 OC_LOG(INFO, TAG, "-t 10 : Discover Resources and Initiate Confirmable Post Request");
118 OC_LOG(INFO, TAG, "-t 11 : Discover Resources and Initiate Confirmable Delete Requests");
119 OC_LOG(INFO, TAG, "-t 12 : Discover Resources and Initiate Confirmable Observe Requests"\
120 " and cancel with Low QoS");
123 OC_LOG(INFO, TAG, "-t 13 : Discover Resources and Initiate Nonconfirmable presence");
124 OC_LOG(INFO, TAG, "-t 14 : Discover Resources and Initiate Nonconfirmable presence with "\
126 OC_LOG(INFO, TAG, "-t 15 : Discover Resources and Initiate Nonconfirmable presence with "\
128 OC_LOG(INFO, TAG, "-t 16 : Discover Resources and Initiate Nonconfirmable multicast presence.");
131 OC_LOG(INFO, TAG, "-t 17 : Discover Resources and Initiate Nonconfirmable Observe Requests "\
132 "then cancel immediately with High QOS");
133 OC_LOG(INFO, TAG, "-t 18 : Discover Resources and Initiate Nonconfirmable Get Request and "\
134 "add vendor specific header options");
135 OC_LOG(INFO, TAG, "-t 19 : Discover Platform");
136 OC_LOG(INFO, TAG, "-t 20 : Discover Devices");
139 OCStackResult InvokeOCDoResource(std::ostringstream &query,
140 OCDevAddr *remoteAddr,
142 OCQualityOfService qos,
143 OCClientResponseHandler cb,
144 OCHeaderOption * options,
148 OCCallbackData cbData;
152 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
155 ret = OCDoResource(&handle, method, query.str().c_str(), remoteAddr,
156 (method == OC_REST_PUT) ? putPayload() : NULL,
157 (ConnType), qos, &cbData, options, numOptions);
159 if (ret != OC_STACK_OK)
161 OC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method);
164 else if (method == OC_REST_PRESENCE)
166 gPresenceHandle = handle;
173 OCStackApplicationResult putReqCB(void* ctx, OCDoHandle /*handle*/,
174 OCClientResponse * clientResponse)
176 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
178 OC_LOG(INFO, TAG, "Callback Context for PUT recvd successfully");
183 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
184 OC_LOG_PAYLOAD(INFO, clientResponse->payload);
185 OC_LOG(INFO, TAG, ("=============> Put Response"));
189 OC_LOG_V(INFO, TAG, "putReqCB received Null clientResponse");
191 return OC_STACK_DELETE_TRANSACTION;
194 OCStackApplicationResult postReqCB(void *ctx, OCDoHandle /*handle*/,
195 OCClientResponse *clientResponse)
197 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
199 OC_LOG(INFO, TAG, "Callback Context for POST recvd successfully");
204 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
205 OC_LOG_PAYLOAD(INFO, clientResponse->payload);
206 OC_LOG(INFO, TAG, ("=============> Post Response"));
210 OC_LOG_V(INFO, TAG, "postReqCB received Null clientResponse");
212 return OC_STACK_DELETE_TRANSACTION;
215 OCStackApplicationResult deleteReqCB(void *ctx,
216 OCDoHandle /*handle*/,
217 OCClientResponse *clientResponse)
219 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
221 OC_LOG(INFO, TAG, "Callback Context for DELETE recvd successfully");
226 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
227 OC_LOG_PAYLOAD(INFO, clientResponse->payload);
228 OC_LOG(INFO, TAG, ("=============> Delete Response"));
232 OC_LOG_V(INFO, TAG, "deleteReqCB received Null clientResponse");
234 return OC_STACK_DELETE_TRANSACTION;
237 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle /*handle*/,
238 OCClientResponse * clientResponse)
240 if (clientResponse == NULL)
242 OC_LOG(INFO, TAG, "getReqCB received NULL clientResponse");
243 return OC_STACK_DELETE_TRANSACTION;
246 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
248 OC_LOG(INFO, TAG, "Callback Context for GET query recvd successfully");
251 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
252 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
253 OC_LOG_PAYLOAD(INFO, clientResponse->payload);
254 OC_LOG(INFO, TAG, ("=============> Get Response"));
256 if (clientResponse->numRcvdVendorSpecificHeaderOptions > 0)
258 OC_LOG (INFO, TAG, "Received vendor specific options");
260 OCHeaderOption * rcvdOptions = clientResponse->rcvdVendorSpecificHeaderOptions;
261 for( i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
263 if (((OCHeaderOption)rcvdOptions[i]).protocolID == OC_COAP_ID)
265 OC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with",
266 ((OCHeaderOption)rcvdOptions[i]).optionID );
268 OC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData,
269 MAX_HEADER_OPTION_DATA_LENGTH);
273 return OC_STACK_DELETE_TRANSACTION;
276 OCStackApplicationResult obsReqCB(void* ctx, OCDoHandle handle,
277 OCClientResponse * clientResponse)
279 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
281 OC_LOG(INFO, TAG, "Callback Context for OBS query recvd successfully");
286 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
287 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
288 OC_LOG_V(INFO, TAG, "Callback Context for OBSERVE notification recvd successfully %d",
289 gNumObserveNotifies);
290 OC_LOG_PAYLOAD(INFO, clientResponse->payload);
291 OC_LOG(INFO, TAG, ("=============> Obs Response"));
292 gNumObserveNotifies++;
293 if (gNumObserveNotifies > 15) //large number to test observing in DELETE case.
295 if (TestCase == TEST_OBS_REQ_NON || TestCase == TEST_OBS_REQ_CON)
297 OC_LOG(ERROR, TAG, "Cancelling with LOW QOS");
298 if (OCCancel (handle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
300 OC_LOG(ERROR, TAG, "Observe cancel error");
302 return OC_STACK_DELETE_TRANSACTION;
304 else if (TestCase == TEST_OBS_REQ_NON_CANCEL_IMM)
306 OC_LOG(ERROR, TAG, "Cancelling with HIGH QOS");
307 if (OCCancel (handle, OC_HIGH_QOS, NULL, 0) != OC_STACK_OK)
309 OC_LOG(ERROR, TAG, "Observe cancel error");
313 if (clientResponse->sequenceNumber == OC_OBSERVE_REGISTER)
315 OC_LOG(INFO, TAG, "This also serves as a registration confirmation");
317 else if (clientResponse->sequenceNumber == OC_OBSERVE_DEREGISTER)
319 OC_LOG(INFO, TAG, "This also serves as a deregistration confirmation");
320 return OC_STACK_DELETE_TRANSACTION;
322 else if (clientResponse->sequenceNumber == OC_OBSERVE_NO_OPTION)
324 OC_LOG(INFO, TAG, "This also tells you that registration/deregistration failed");
325 return OC_STACK_DELETE_TRANSACTION;
330 OC_LOG_V(INFO, TAG, "obsReqCB received Null clientResponse");
332 return OC_STACK_KEEP_TRANSACTION;
335 OCStackApplicationResult presenceCB(void* ctx, OCDoHandle /*handle*/,
336 OCClientResponse * clientResponse)
338 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
340 OC_LOG(INFO, TAG, "Callback Context for Presence recvd successfully");
345 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
346 OC_LOG_V(INFO, TAG, "Callback Context for Presence notification recvd successfully %d",
347 gNumPresenceNotifies);
348 OC_LOG_PAYLOAD(INFO, clientResponse->payload);
349 OC_LOG(INFO, TAG, ("=============> Presence Response"));
350 gNumPresenceNotifies++;
351 if (gNumPresenceNotifies == 20)
353 if (OCCancel(gPresenceHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
355 OC_LOG(ERROR, TAG, "Presence cancel error");
357 return OC_STACK_DELETE_TRANSACTION;
362 OC_LOG_V(INFO, TAG, "presenceCB received Null clientResponse");
364 return OC_STACK_KEEP_TRANSACTION;
368 // This is a function called back when a device is discovered
369 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle /*handle*/,
370 OCClientResponse * clientResponse)
372 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
374 OC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully");
379 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
381 std::string connectionType = getConnectivityType (clientResponse->connType);
382 OC_LOG_V(INFO, TAG, "Discovered on %s", connectionType.c_str());
384 "Device =============> Discovered @ %s:%d",
385 clientResponse->devAddr.addr,
386 clientResponse->devAddr.port);
387 OC_LOG_PAYLOAD(INFO, clientResponse->payload);
389 ConnType = clientResponse->connType;
390 serverAddr = clientResponse->devAddr;
392 OCDiscoveryPayload *payload = (OCDiscoveryPayload*) clientResponse->payload;
395 return OC_STACK_DELETE_TRANSACTION;
398 OCResourcePayload *resource = (OCResourcePayload*) payload->resources;
401 OC_LOG_V (INFO, TAG, "No resources in payload");
402 return OC_STACK_DELETE_TRANSACTION;
404 coapServerResource = resource->uri;
408 case TEST_GET_REQ_NON:
409 InitGetRequest(OC_LOW_QOS, 0, 0);
411 case TEST_GET_REQ_NON_WITH_FILTERS:
412 InitGetRequest(OC_LOW_QOS, 0, 1);
414 case TEST_PUT_REQ_NON:
415 InitPutRequest(OC_LOW_QOS);
417 case TEST_POST_REQ_NON:
418 InitPostRequest(OC_LOW_QOS);
420 case TEST_DELETE_REQ_NON:
421 InitDeleteRequest(OC_LOW_QOS);
423 case TEST_OBS_REQ_NON:
424 case TEST_OBS_REQ_NON_CANCEL_IMM:
425 InitObserveRequest(OC_LOW_QOS);
427 case TEST_GET_UNAVAILABLE_RES_REQ_NON:
428 InitGetRequestToUnavailableResource(OC_LOW_QOS);
430 case TEST_GET_REQ_CON:
431 InitGetRequest(OC_HIGH_QOS, 0, 0);
433 case TEST_POST_REQ_CON:
434 InitPostRequest(OC_HIGH_QOS);
436 case TEST_DELETE_REQ_CON:
437 InitDeleteRequest(OC_HIGH_QOS);
439 case TEST_OBS_REQ_CON:
440 InitObserveRequest(OC_HIGH_QOS);
443 case TEST_OBS_PRESENCE:
444 case TEST_OBS_PRESENCE_WITH_FILTER:
445 case TEST_OBS_PRESENCE_WITH_FILTERS:
446 case TEST_OBS_MULTICAST_PRESENCE:
450 case TEST_GET_REQ_NON_WITH_VENDOR_HEADER_OPTIONS:
451 InitGetRequest(OC_LOW_QOS, 1, 0);
453 case TEST_DISCOVER_PLATFORM_REQ:
454 InitPlatformDiscovery(OC_LOW_QOS);
456 case TEST_DISCOVER_DEV_REQ:
457 InitDeviceDiscovery(OC_LOW_QOS);
466 OC_LOG_V(INFO, TAG, "discoveryReqCB received Null clientResponse");
468 return OC_STACK_KEEP_TRANSACTION;
471 OCStackApplicationResult PlatformDiscoveryReqCB(void* ctx,
472 OCDoHandle /*handle*/,
473 OCClientResponse * clientResponse)
475 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
477 OC_LOG(INFO, TAG, "Callback Context for Platform DISCOVER query recvd successfully");
482 OC_LOG(INFO, TAG, ("Discovery Response:"));
483 OC_LOG_PAYLOAD(INFO, clientResponse->payload);
487 OC_LOG_V(INFO, TAG, "PlatformDiscoveryReqCB received Null clientResponse");
490 return (UnicastDiscovery) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
493 OCStackApplicationResult DeviceDiscoveryReqCB(void* ctx, OCDoHandle /*handle*/,
494 OCClientResponse * clientResponse)
496 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
498 OC_LOG(INFO, TAG, "Callback Context for Device DISCOVER query recvd successfully");
503 OC_LOG(INFO, TAG, ("Discovery Response:"));
504 OC_LOG_PAYLOAD(INFO, clientResponse->payload);
508 OC_LOG_V(INFO, TAG, "PlatformDiscoveryReqCB received Null clientResponse");
511 return (UnicastDiscovery) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
517 OCStackResult result = OC_STACK_OK;
518 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
519 std::ostringstream query;
520 std::ostringstream querySuffix;
521 query << OC_RSRVD_PRESENCE_URI;
522 if (TestCase == TEST_OBS_PRESENCE)
524 result = InvokeOCDoResource(query, &serverAddr, OC_REST_PRESENCE,
525 OC_LOW_QOS, presenceCB, NULL, 0);
527 if (TestCase == TEST_OBS_PRESENCE_WITH_FILTER || TestCase == TEST_OBS_PRESENCE_WITH_FILTERS)
530 querySuffix << query.str() << "?rt=core.led";
531 result = InvokeOCDoResource(querySuffix, &serverAddr, OC_REST_PRESENCE,
532 OC_LOW_QOS, presenceCB, NULL, 0);
534 if (TestCase == TEST_OBS_PRESENCE_WITH_FILTERS)
536 if (result == OC_STACK_OK)
539 querySuffix << query.str() << "?rt=core.fan";
540 result = InvokeOCDoResource(querySuffix, &serverAddr, OC_REST_PRESENCE, OC_LOW_QOS,
541 presenceCB, NULL, 0);
544 if (TestCase == TEST_OBS_MULTICAST_PRESENCE)
546 if (result == OC_STACK_OK)
548 std::ostringstream multicastPresenceQuery;
549 multicastPresenceQuery.str("");
550 multicastPresenceQuery << "coap://" << OC_MULTICAST_PREFIX << OC_RSRVD_PRESENCE_URI;
551 result = InvokeOCDoResource(multicastPresenceQuery, &serverAddr, OC_REST_PRESENCE, OC_LOW_QOS,
552 presenceCB, NULL, 0);
559 int InitGetRequestToUnavailableResource(OCQualityOfService qos)
561 std::ostringstream query;
562 query << "/SomeUnknownResource";
563 OC_LOG_V(INFO, TAG, "\nExecuting %s with query %s", __func__, query.str().c_str());
564 return (InvokeOCDoResource(query, &serverAddr, OC_REST_GET, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
568 int InitObserveRequest(OCQualityOfService qos)
570 std::ostringstream query;
571 query << coapServerResource;
572 OC_LOG_V(INFO, TAG, "\nExecuting %s with query %s", __func__, query.str().c_str());
573 return (InvokeOCDoResource(query, &serverAddr, OC_REST_OBSERVE,
574 (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, obsReqCB, NULL, 0));
577 int InitPutRequest(OCQualityOfService qos)
579 std::ostringstream query;
580 query << coapServerResource;
581 OC_LOG_V(INFO, TAG, "\nExecuting %s with query %s", __func__, query.str().c_str());
582 return (InvokeOCDoResource(query, &serverAddr, OC_REST_PUT, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
586 int InitPostRequest(OCQualityOfService qos)
588 OCStackResult result;
590 std::ostringstream query;
591 query << coapServerResource;
593 OC_LOG_V(INFO, TAG, "\nExecuting %s with query %s", __func__, query.str().c_str());
594 // First POST operation (to create an Light instance)
595 result = InvokeOCDoResource(query, &serverAddr, OC_REST_POST,
596 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
598 if (OC_STACK_OK != result)
600 // Error can happen if for example, network connectivity is down
601 OC_LOG(INFO, TAG, "First POST call did not succeed");
604 // Second POST operation (to create an Light instance)
605 result = InvokeOCDoResource(query, &serverAddr, OC_REST_POST,
606 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
608 if (OC_STACK_OK != result)
610 OC_LOG(INFO, TAG, "Second POST call did not succeed");
613 // This POST operation will update the original resourced /a/light
614 return (InvokeOCDoResource(query, &serverAddr, OC_REST_POST,
615 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
616 postReqCB, NULL, 0));
619 void* RequestDeleteDeathResourceTask(void* myqos)
621 sleep (30);//long enough to give the server time to finish deleting the resource.
622 std::ostringstream query;
623 query << coapServerResource;
625 OC_LOG_V(INFO, TAG, "\nExecuting %s with query %s", __func__, query.str().c_str());
627 // Second DELETE operation to delete the resource that might have been removed already.
628 OCQualityOfService qos;
638 OCStackResult result = InvokeOCDoResource(query, &serverAddr, OC_REST_DELETE,
640 deleteReqCB, NULL, 0);
642 if (OC_STACK_OK != result)
644 OC_LOG(INFO, TAG, "Second DELETE call did not succeed");
650 int InitDeleteRequest(OCQualityOfService qos)
652 OCStackResult result;
653 std::ostringstream query;
654 query << coapServerResource;
656 OC_LOG_V(INFO, TAG, "\nExecuting %s with query %s", __func__, query.str().c_str());
658 // First DELETE operation
659 result = InvokeOCDoResource(query, &serverAddr, OC_REST_DELETE,
661 deleteReqCB, NULL, 0);
662 if (OC_STACK_OK != result)
664 // Error can happen if for example, network connectivity is down
665 OC_LOG(INFO, TAG, "First DELETE call did not succeed");
669 //Create a thread to delete this resource again
671 pthread_create (&threadId, NULL, RequestDeleteDeathResourceTask, (void*)qos);
674 OC_LOG_V(INFO, TAG, "\n\nExit %s", __func__);
678 int InitGetRequest(OCQualityOfService qos, uint8_t withVendorSpecificHeaderOptions, bool getWithQuery)
681 OCHeaderOption options[MAX_HEADER_OPTIONS];
683 std::ostringstream query;
684 query << coapServerResource;
686 // ocserver is written to only process "power<X" query.
689 OC_LOG(INFO, TAG, "Using query power<50");
690 query << "?power<50";
692 OC_LOG_V(INFO, TAG, "\nExecuting %s with query %s", __func__, query.str().c_str());
694 if (withVendorSpecificHeaderOptions)
696 uint8_t option0[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
697 uint8_t option1[] = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
698 memset(options, 0, sizeof(OCHeaderOption) * MAX_HEADER_OPTIONS);
699 options[0].protocolID = OC_COAP_ID;
700 options[0].optionID = 2048;
701 memcpy(options[0].optionData, option0, sizeof(option0));
702 options[0].optionLength = 10;
703 options[1].protocolID = OC_COAP_ID;
704 options[1].optionID = 3000;
705 memcpy(options[1].optionData, option1, sizeof(option1));
706 options[1].optionLength = 10;
708 if (withVendorSpecificHeaderOptions)
710 return (InvokeOCDoResource(query, &serverAddr, OC_REST_GET,
711 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, options, 2));
715 return (InvokeOCDoResource(query, &serverAddr, OC_REST_GET,
716 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, NULL, 0));
720 int InitPlatformDiscovery(OCQualityOfService qos)
722 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
725 OCCallbackData cbData;
726 char szQueryUri[64] = { 0 };
728 snprintf(szQueryUri, sizeof (szQueryUri) - 1, PLATFORM_DISCOVERY_QUERY, discoveryAddr);
730 cbData.cb = PlatformDiscoveryReqCB;
731 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
734 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
735 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
737 if (ret != OC_STACK_OK)
739 OC_LOG(ERROR, TAG, "OCStack device error");
745 int InitDeviceDiscovery(OCQualityOfService qos)
747 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
750 OCCallbackData cbData;
751 char szQueryUri[100] = { 0 };
753 snprintf(szQueryUri, sizeof (szQueryUri) - 1, DEVICE_DISCOVERY_QUERY, discoveryAddr);
755 cbData.cb = DeviceDiscoveryReqCB;
756 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
759 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
760 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
762 if (ret != OC_STACK_OK)
764 OC_LOG(ERROR, TAG, "OCStack device error");
770 int InitDiscovery(OCQualityOfService qos)
773 OCCallbackData cbData;
774 char szQueryUri[100] = { 0 };
776 snprintf(szQueryUri, sizeof (szQueryUri) - 1, RESOURCE_DISCOVERY_QUERY, discoveryAddr);
778 cbData.cb = discoveryReqCB;
779 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
782 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
783 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
785 if (ret != OC_STACK_OK)
787 OC_LOG(ERROR, TAG, "OCStack resource error");
792 int main(int argc, char* argv[])
796 while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
801 UnicastDiscovery = atoi(optarg);
804 TestCase = atoi(optarg);
807 Connectivity = atoi(optarg);
815 if ((UnicastDiscovery != 0 && UnicastDiscovery != 1) ||
816 (TestCase < TEST_DISCOVER_REQ || TestCase >= MAX_TESTS) ||
817 (Connectivity < CT_ADAPTER_DEFAULT || Connectivity >= MAX_CT))
823 if (OCInit1(OC_CLIENT, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
825 OC_LOG(ERROR, TAG, "OCStack init error");
829 #ifdef ROUTING_GATEWAY
831 * Before invoking Discover resource, we process the gateway requests
832 * and form the routing table.
834 for (int index = 0; index < MAX_NUM_GATEWAY_REQUEST; index++)
836 if (OC_STACK_OK != OCProcess())
838 OC_LOG(ERROR, TAG, "OCStack process error");
841 usleep(SLEEP_DURATION);
844 if (Connectivity == CT_ADAPTER_DEFAULT || Connectivity == CT_IP)
846 ConnType = CT_ADAPTER_IP;
850 OC_LOG(INFO, TAG, "Default Connectivity type selected...");
854 discoveryAddr[0] = '\0';
856 if (UnicastDiscovery)
858 OC_LOG(INFO, TAG, "Enter IP address of server with optional port number");
859 OC_LOG(INFO, TAG, "IPv4: 192.168.0.15:45454\n");
860 OC_LOG(INFO, TAG, "IPv6: [fe80::20c:29ff:fe1b:9c5]:45454\n");
862 if (fgets(discoveryAddr, sizeof (discoveryAddr), stdin))
864 //Strip newline char from ipv4addr
865 StripNewLineChar(discoveryAddr);
869 OC_LOG(ERROR, TAG, "!! Bad input for IP address. !!");
870 return OC_STACK_INVALID_PARAM;
874 if (UnicastDiscovery == 0 && TestCase == TEST_DISCOVER_DEV_REQ)
876 InitDeviceDiscovery(OC_LOW_QOS);
878 else if (UnicastDiscovery == 0 && TestCase == TEST_DISCOVER_PLATFORM_REQ)
880 InitPlatformDiscovery(OC_LOW_QOS);
884 InitDiscovery(OC_LOW_QOS);
887 // Break from loop with Ctrl+C
888 OC_LOG(INFO, TAG, "Entering occlient main loop...");
889 signal(SIGINT, handleSigInt);
893 if (OCProcess() != OC_STACK_OK)
895 OC_LOG(ERROR, TAG, "OCStack process error");
898 #ifndef ROUTING_GATEAWAY
902 OC_LOG(INFO, TAG, "Exiting occlient main loop...");
904 if (OCStop() != OC_STACK_OK)
906 OC_LOG(ERROR, TAG, "OCStack stop error");
912 std::string getConnectivityType (OCConnectivityType connType)
914 switch (connType & CT_MASK_ADAPTER)
925 case CT_ADAPTER_GATT_BTLE:
928 case CT_ADAPTER_RFCOMM_BTEDR:
932 return "Incorrect connectivity";