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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "iotivity_config.h"
38 #include "ocpayload.h"
39 #include "payload_logging.h"
42 #ifdef ROUTING_GATEWAY
44 * Maximum number of gateway requests to form the routing table.
46 #define MAX_NUM_GATEWAY_REQUEST 20
49 * Sleep duration after every OCProcess().
51 #define SLEEP_DURATION 100000
53 // Tracking user input
54 static int UnicastDiscovery = 0;
55 static int TestCase = 0;
56 static int Connectivity = 0;
58 static const char *DEVICE_DISCOVERY_QUERY = "%s/oic/d";
59 static const char *PLATFORM_DISCOVERY_QUERY = "%s/oic/p";
60 static const char *RESOURCE_DISCOVERY_QUERY = "%s/oic/res";
62 //The following variable determines the interface protocol (IPv4, IPv6, etc)
63 //to be used for sending unicast messages. Default set to IP dual stack.
64 static OCConnectivityType ConnType = CT_ADAPTER_IP;
65 static OCDevAddr serverAddr;
66 static char discoveryAddr[100];
67 static std::string coapServerResource = "/a/light";
70 // The handle for observe registration
71 OCDoHandle gPresenceHandle;
73 // After this crosses a threshold client deregisters for further notifications
74 int gNumObserveNotifies = 0;
77 int gNumPresenceNotifies = 0;
81 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
82 void handleSigInt(int signum)
90 OCPayload* putPayload()
92 OCRepPayload* payload = OCRepPayloadCreate();
96 std::cout << "Failed to create put payload object"<<std::endl;
100 OCRepPayloadSetPropInt(payload, "power", 15);
101 OCRepPayloadSetPropBool(payload, "state", true);
103 return (OCPayload*) payload;
106 static void PrintUsage()
108 OIC_LOG(INFO, TAG, "Usage : occlient -u <0|1> -t <1..20> -c <0|1>");
109 OIC_LOG(INFO, TAG, "-u <0|1> : Perform multicast/unicast discovery of resources");
110 OIC_LOG(INFO, TAG, "-c 0 : Use Default connectivity(IP)");
111 OIC_LOG(INFO, TAG, "-c 1 : IP Connectivity Type");
112 OIC_LOG(INFO, TAG, "-t 1 : Discover Resources");
113 OIC_LOG(INFO, TAG, "-t 2 : Discover Resources and Initiate Nonconfirmable Get Request");
114 OIC_LOG(INFO, TAG, "-t 3 : Discover Resources and Initiate Nonconfirmable Get Request"
115 " with query filter.");
116 OIC_LOG(INFO, TAG, "-t 4 : Discover Resources and Initiate Nonconfirmable Put Requests");
117 OIC_LOG(INFO, TAG, "-t 5 : Discover Resources and Initiate Nonconfirmable Post Requests");
118 OIC_LOG(INFO, TAG, "-t 6 : Discover Resources and Initiate Nonconfirmable Delete Requests");
119 OIC_LOG(INFO, TAG, "-t 7 : Discover Resources and Initiate Nonconfirmable Observe Requests");
120 OIC_LOG(INFO, TAG, "-t 8 : Discover Resources and Initiate Nonconfirmable Get Request "\
121 "for a resource which is unavailable");
122 OIC_LOG(INFO, TAG, "-t 9 : Discover Resources and Initiate Confirmable Get Request");
123 OIC_LOG(INFO, TAG, "-t 10 : Discover Resources and Initiate Confirmable Post Request");
124 OIC_LOG(INFO, TAG, "-t 11 : Discover Resources and Initiate Confirmable Delete Requests");
125 OIC_LOG(INFO, TAG, "-t 12 : Discover Resources and Initiate Confirmable Observe Requests"\
126 " and cancel with Low QoS");
129 OIC_LOG(INFO, TAG, "-t 13 : Discover Resources and Initiate Nonconfirmable presence");
130 OIC_LOG(INFO, TAG, "-t 14 : Discover Resources and Initiate Nonconfirmable presence with "\
132 OIC_LOG(INFO, TAG, "-t 15 : Discover Resources and Initiate Nonconfirmable presence with "\
134 OIC_LOG(INFO, TAG, "-t 16 : Discover Resources and Initiate Nonconfirmable multicast presence.");
137 OIC_LOG(INFO, TAG, "-t 17 : Discover Resources and Initiate Nonconfirmable Observe Requests "\
138 "then cancel immediately with High QOS");
139 OIC_LOG(INFO, TAG, "-t 18 : Discover Resources and Initiate Nonconfirmable Get Request and "\
140 "add vendor specific header options");
141 OIC_LOG(INFO, TAG, "-t 19 : Discover Platform");
142 OIC_LOG(INFO, TAG, "-t 20 : Discover Devices");
145 OCStackResult InvokeOCDoResource(std::ostringstream &query,
146 OCDevAddr *remoteAddr,
148 OCQualityOfService qos,
149 OCClientResponseHandler cb,
150 OCHeaderOption * options,
154 OCCallbackData cbData;
158 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
161 ret = OCDoResource(&handle, method, query.str().c_str(), remoteAddr,
162 (method == OC_REST_PUT) ? putPayload() : NULL,
163 (ConnType), qos, &cbData, options, numOptions);
165 if (ret != OC_STACK_OK)
167 OIC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method);
170 else if (method == OC_REST_PRESENCE)
172 gPresenceHandle = handle;
179 OCStackApplicationResult putReqCB(void* ctx, OCDoHandle /*handle*/,
180 OCClientResponse * clientResponse)
182 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
184 OIC_LOG(INFO, TAG, "Callback Context for PUT recvd successfully");
189 OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
190 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
191 OIC_LOG(INFO, TAG, ("=============> Put Response"));
195 OIC_LOG_V(INFO, TAG, "putReqCB received Null clientResponse");
197 return OC_STACK_DELETE_TRANSACTION;
200 OCStackApplicationResult postReqCB(void *ctx, OCDoHandle /*handle*/,
201 OCClientResponse *clientResponse)
203 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
205 OIC_LOG(INFO, TAG, "Callback Context for POST recvd successfully");
210 OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
211 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
212 OIC_LOG(INFO, TAG, ("=============> Post Response"));
216 OIC_LOG_V(INFO, TAG, "postReqCB received Null clientResponse");
218 return OC_STACK_DELETE_TRANSACTION;
221 OCStackApplicationResult deleteReqCB(void *ctx,
222 OCDoHandle /*handle*/,
223 OCClientResponse *clientResponse)
225 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
227 OIC_LOG(INFO, TAG, "Callback Context for DELETE recvd successfully");
232 OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
233 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
234 OIC_LOG(INFO, TAG, ("=============> Delete Response"));
238 OIC_LOG_V(INFO, TAG, "deleteReqCB received Null clientResponse");
240 return OC_STACK_DELETE_TRANSACTION;
243 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle /*handle*/,
244 OCClientResponse * clientResponse)
246 if (clientResponse == NULL)
248 OIC_LOG(INFO, TAG, "getReqCB received NULL clientResponse");
249 return OC_STACK_DELETE_TRANSACTION;
252 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
254 OIC_LOG(INFO, TAG, "Callback Context for GET query recvd successfully");
257 OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
258 OIC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
259 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
260 OIC_LOG(INFO, TAG, ("=============> Get Response"));
262 if (clientResponse->numRcvdVendorSpecificHeaderOptions > 0)
264 OIC_LOG (INFO, TAG, "Received vendor specific options");
266 OCHeaderOption * rcvdOptions = clientResponse->rcvdVendorSpecificHeaderOptions;
267 for( i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
269 if (((OCHeaderOption)rcvdOptions[i]).protocolID == OC_COAP_ID)
271 OIC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with",
272 ((OCHeaderOption)rcvdOptions[i]).optionID );
274 OIC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData,
275 MAX_HEADER_OPTION_DATA_LENGTH);
279 return OC_STACK_DELETE_TRANSACTION;
282 OCStackApplicationResult obsReqCB(void* ctx, OCDoHandle handle,
283 OCClientResponse * clientResponse)
285 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
287 OIC_LOG(INFO, TAG, "Callback Context for OBS query recvd successfully");
292 if (clientResponse->sequenceNumber <= MAX_SEQUENCE_NUMBER)
294 if (clientResponse->sequenceNumber == OC_OBSERVE_REGISTER)
296 OIC_LOG(INFO, TAG, "This also serves as a registration confirmation.");
299 OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
300 OIC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
301 OIC_LOG_V(INFO, TAG, "Callback Context for OBSERVE notification recvd successfully %d",
302 gNumObserveNotifies);
303 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
304 OIC_LOG(INFO, TAG, ("=============> Obs Response"));
305 gNumObserveNotifies++;
307 if (gNumObserveNotifies > 15) //large number to test observing in DELETE case.
309 if (TestCase == TEST_OBS_REQ_NON || TestCase == TEST_OBS_REQ_CON)
311 OIC_LOG(ERROR, TAG, "Cancelling with LOW QOS");
312 if (OCCancel (handle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
314 OIC_LOG(ERROR, TAG, "Observe cancel error");
316 return OC_STACK_DELETE_TRANSACTION;
318 else if (TestCase == TEST_OBS_REQ_NON_CANCEL_IMM)
320 OIC_LOG(ERROR, TAG, "Cancelling with HIGH QOS");
321 if (OCCancel (handle, OC_HIGH_QOS, NULL, 0) != OC_STACK_OK)
323 OIC_LOG(ERROR, TAG, "Observe cancel error");
330 OIC_LOG(INFO, TAG, "No observe option header is returned in the response.");
331 OIC_LOG(INFO, TAG, "For a registration request, it means the registration failed");
332 return OC_STACK_DELETE_TRANSACTION;
337 OIC_LOG_V(INFO, TAG, "obsReqCB received Null clientResponse");
339 return OC_STACK_KEEP_TRANSACTION;
342 OCStackApplicationResult presenceCB(void* ctx, OCDoHandle /*handle*/,
343 OCClientResponse * clientResponse)
345 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
347 OIC_LOG(INFO, TAG, "Callback Context for Presence recvd successfully");
352 OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
353 OIC_LOG_V(INFO, TAG, "Callback Context for Presence notification recvd successfully %d",
354 gNumPresenceNotifies);
355 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
356 OIC_LOG(INFO, TAG, ("=============> Presence Response"));
357 gNumPresenceNotifies++;
358 if (gNumPresenceNotifies == 20)
360 if (OCCancel(gPresenceHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
362 OIC_LOG(ERROR, TAG, "Presence cancel error");
364 return OC_STACK_DELETE_TRANSACTION;
369 OIC_LOG_V(INFO, TAG, "presenceCB received Null clientResponse");
371 return OC_STACK_KEEP_TRANSACTION;
375 // This is a function called back when a device is discovered
376 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle /*handle*/,
377 OCClientResponse * clientResponse)
379 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
381 OIC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully");
386 OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
388 std::string connectionType = getConnectivityType (clientResponse->connType);
389 OIC_LOG_V(INFO, TAG, "Discovered on %s", connectionType.c_str());
391 "Device =============> Discovered @ %s:%d",
392 clientResponse->devAddr.addr,
393 clientResponse->devAddr.port);
394 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
396 ConnType = clientResponse->connType;
397 serverAddr = clientResponse->devAddr;
399 OCDiscoveryPayload *payload = (OCDiscoveryPayload*) clientResponse->payload;
402 return OC_STACK_DELETE_TRANSACTION;
405 OCResourcePayload *resource = (OCResourcePayload*) payload->resources;
409 if(resource->uri && strcmp(resource->uri, coapServerResource.c_str()) == 0)
414 resource = resource->next;
419 OIC_LOG_V (INFO, TAG, "No %s in payload", coapServerResource.c_str());
420 return OC_STACK_KEEP_TRANSACTION;
425 case TEST_GET_REQ_NON:
426 InitGetRequest(OC_LOW_QOS, 0, 0);
428 case TEST_GET_REQ_NON_WITH_FILTERS:
429 InitGetRequest(OC_LOW_QOS, 0, 1);
431 case TEST_PUT_REQ_NON:
432 InitPutRequest(OC_LOW_QOS);
434 case TEST_POST_REQ_NON:
435 InitPostRequest(OC_LOW_QOS);
437 case TEST_DELETE_REQ_NON:
438 InitDeleteRequest(OC_LOW_QOS);
440 case TEST_OBS_REQ_NON:
441 case TEST_OBS_REQ_NON_CANCEL_IMM:
442 InitObserveRequest(OC_LOW_QOS);
444 case TEST_GET_UNAVAILABLE_RES_REQ_NON:
445 InitGetRequestToUnavailableResource(OC_LOW_QOS);
447 case TEST_GET_REQ_CON:
448 InitGetRequest(OC_HIGH_QOS, 0, 0);
450 case TEST_POST_REQ_CON:
451 InitPostRequest(OC_HIGH_QOS);
453 case TEST_DELETE_REQ_CON:
454 InitDeleteRequest(OC_HIGH_QOS);
456 case TEST_OBS_REQ_CON:
457 InitObserveRequest(OC_HIGH_QOS);
460 case TEST_OBS_PRESENCE:
461 case TEST_OBS_PRESENCE_WITH_FILTER:
462 case TEST_OBS_PRESENCE_WITH_FILTERS:
463 case TEST_OBS_MULTICAST_PRESENCE:
467 case TEST_GET_REQ_NON_WITH_VENDOR_HEADER_OPTIONS:
468 InitGetRequest(OC_LOW_QOS, 1, 0);
470 case TEST_DISCOVER_PLATFORM_REQ:
471 InitPlatformDiscovery(OC_LOW_QOS);
473 case TEST_DISCOVER_DEV_REQ:
474 InitDeviceDiscovery(OC_LOW_QOS);
483 OIC_LOG_V(INFO, TAG, "discoveryReqCB received Null clientResponse");
485 return OC_STACK_KEEP_TRANSACTION;
488 OCStackApplicationResult PlatformDiscoveryReqCB(void* ctx,
489 OCDoHandle /*handle*/,
490 OCClientResponse * clientResponse)
492 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
494 OIC_LOG(INFO, TAG, "Callback Context for Platform DISCOVER query recvd successfully");
499 OIC_LOG(INFO, TAG, ("Discovery Response:"));
500 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
504 OIC_LOG_V(INFO, TAG, "PlatformDiscoveryReqCB received Null clientResponse");
507 return (UnicastDiscovery) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
510 OCStackApplicationResult DeviceDiscoveryReqCB(void* ctx, OCDoHandle /*handle*/,
511 OCClientResponse * clientResponse)
513 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
515 OIC_LOG(INFO, TAG, "Callback Context for Device DISCOVER query recvd successfully");
520 OIC_LOG(INFO, TAG, ("Discovery Response:"));
521 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
525 OIC_LOG_V(INFO, TAG, "PlatformDiscoveryReqCB received Null clientResponse");
528 return (UnicastDiscovery) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
534 OCStackResult result = OC_STACK_OK;
535 OIC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
536 std::ostringstream query;
537 std::ostringstream querySuffix;
538 query << OC_RSRVD_PRESENCE_URI;
539 if (TestCase == TEST_OBS_PRESENCE)
541 result = InvokeOCDoResource(query, &serverAddr, OC_REST_PRESENCE,
542 OC_LOW_QOS, presenceCB, NULL, 0);
544 if (TestCase == TEST_OBS_PRESENCE_WITH_FILTER || TestCase == TEST_OBS_PRESENCE_WITH_FILTERS)
547 querySuffix << query.str() << "?rt=core.led";
548 result = InvokeOCDoResource(querySuffix, &serverAddr, OC_REST_PRESENCE,
549 OC_LOW_QOS, presenceCB, NULL, 0);
551 if (TestCase == TEST_OBS_PRESENCE_WITH_FILTERS)
553 if (result == OC_STACK_OK)
556 querySuffix << query.str() << "?rt=core.fan";
557 result = InvokeOCDoResource(querySuffix, &serverAddr, OC_REST_PRESENCE, OC_LOW_QOS,
558 presenceCB, NULL, 0);
561 if (TestCase == TEST_OBS_MULTICAST_PRESENCE)
563 if (result == OC_STACK_OK)
565 result = InvokeOCDoResource(query, NULL, OC_REST_PRESENCE, OC_LOW_QOS,
566 presenceCB, NULL, 0);
573 int InitGetRequestToUnavailableResource(OCQualityOfService qos)
575 std::ostringstream query;
576 query << "/SomeUnknownResource";
577 OIC_LOG_V(INFO, TAG, "\nExecuting %s with query %s", __func__, query.str().c_str());
578 return (InvokeOCDoResource(query, &serverAddr, OC_REST_GET, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
582 int InitObserveRequest(OCQualityOfService qos)
584 std::ostringstream query;
585 query << coapServerResource;
586 OIC_LOG_V(INFO, TAG, "\nExecuting %s with query %s", __func__, query.str().c_str());
587 return (InvokeOCDoResource(query, &serverAddr, OC_REST_OBSERVE,
588 (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, obsReqCB, NULL, 0));
591 int InitPutRequest(OCQualityOfService qos)
593 std::ostringstream query;
594 query << coapServerResource;
595 OIC_LOG_V(INFO, TAG, "\nExecuting %s with query %s", __func__, query.str().c_str());
596 return (InvokeOCDoResource(query, &serverAddr, OC_REST_PUT, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
600 int InitPostRequest(OCQualityOfService qos)
602 OCStackResult result;
604 std::ostringstream query;
605 query << coapServerResource;
607 OIC_LOG_V(INFO, TAG, "\nExecuting %s with query %s", __func__, query.str().c_str());
608 // First POST operation (to create an Light instance)
609 result = InvokeOCDoResource(query, &serverAddr, OC_REST_POST,
610 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
612 if (OC_STACK_OK != result)
614 // Error can happen if for example, network connectivity is down
615 OIC_LOG(INFO, TAG, "First POST call did not succeed");
618 // Second POST operation (to create an Light instance)
619 result = InvokeOCDoResource(query, &serverAddr, OC_REST_POST,
620 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
622 if (OC_STACK_OK != result)
624 OIC_LOG(INFO, TAG, "Second POST call did not succeed");
627 // This POST operation will update the original resourced /a/light
628 return (InvokeOCDoResource(query, &serverAddr, OC_REST_POST,
629 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
630 postReqCB, NULL, 0));
633 void* RequestDeleteDeathResourceTask(void* myqos)
635 sleep (30);//long enough to give the server time to finish deleting the resource.
636 std::ostringstream query;
637 query << coapServerResource;
639 OIC_LOG_V(INFO, TAG, "\nExecuting %s with query %s", __func__, query.str().c_str());
641 // Second DELETE operation to delete the resource that might have been removed already.
642 OCQualityOfService qos;
652 OCStackResult result = InvokeOCDoResource(query, &serverAddr, OC_REST_DELETE,
654 deleteReqCB, NULL, 0);
656 if (OC_STACK_OK != result)
658 OIC_LOG(INFO, TAG, "Second DELETE call did not succeed");
664 int InitDeleteRequest(OCQualityOfService qos)
666 OCStackResult result;
667 std::ostringstream query;
668 query << coapServerResource;
670 OIC_LOG_V(INFO, TAG, "\nExecuting %s with query %s", __func__, query.str().c_str());
672 // First DELETE operation
673 result = InvokeOCDoResource(query, &serverAddr, OC_REST_DELETE,
675 deleteReqCB, NULL, 0);
676 if (OC_STACK_OK != result)
678 // Error can happen if for example, network connectivity is down
679 OIC_LOG(INFO, TAG, "First DELETE call did not succeed");
683 //Create a thread to delete this resource again
685 pthread_create (&threadId, NULL, RequestDeleteDeathResourceTask, (void*)qos);
688 OIC_LOG_V(INFO, TAG, "\n\nExit %s", __func__);
692 int InitGetRequest(OCQualityOfService qos, uint8_t withVendorSpecificHeaderOptions,
696 OCHeaderOption options[MAX_HEADER_OPTIONS];
698 std::ostringstream query;
699 query << coapServerResource;
701 // ocserver is written to only process "power<X" query.
704 OIC_LOG(INFO, TAG, "Using query power<50");
705 query << "?power<50";
707 OIC_LOG_V(INFO, TAG, "\nExecuting %s with query %s", __func__, query.str().c_str());
709 if (withVendorSpecificHeaderOptions)
711 memset(options, 0, sizeof(OCHeaderOption)* MAX_HEADER_OPTIONS);
712 size_t numOptions = 0;
713 uint8_t option0[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
714 uint16_t optionID = 2048;
715 size_t optionDataSize = sizeof(option0);
716 OCSetHeaderOption(options,
722 uint8_t option1[] = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
724 optionDataSize = sizeof(option1);
725 OCSetHeaderOption(options,
731 if (withVendorSpecificHeaderOptions)
733 return (InvokeOCDoResource(query, &serverAddr, OC_REST_GET,
734 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, options, 2));
738 return (InvokeOCDoResource(query, &serverAddr, OC_REST_GET,
739 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, NULL, 0));
743 int InitPlatformDiscovery(OCQualityOfService qos)
745 OIC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
748 OCCallbackData cbData;
749 char szQueryUri[MAX_QUERY_LENGTH] = { 0 };
751 snprintf(szQueryUri, sizeof (szQueryUri) - 1, PLATFORM_DISCOVERY_QUERY, discoveryAddr);
753 cbData.cb = PlatformDiscoveryReqCB;
754 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
757 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
758 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
760 if (ret != OC_STACK_OK)
762 OIC_LOG(ERROR, TAG, "OCStack device error");
768 int InitDeviceDiscovery(OCQualityOfService qos)
770 OIC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
773 OCCallbackData cbData;
774 char szQueryUri[MAX_QUERY_LENGTH] = { 0 };
776 snprintf(szQueryUri, sizeof (szQueryUri) - 1, DEVICE_DISCOVERY_QUERY, discoveryAddr);
778 cbData.cb = DeviceDiscoveryReqCB;
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 OIC_LOG(ERROR, TAG, "OCStack device error");
793 int InitDiscovery(OCQualityOfService qos)
796 OCCallbackData cbData;
797 char szQueryUri[MAX_QUERY_LENGTH] = { 0 };
799 snprintf(szQueryUri, sizeof (szQueryUri) - 1, RESOURCE_DISCOVERY_QUERY, discoveryAddr);
801 cbData.cb = discoveryReqCB;
802 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
805 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
806 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
808 if (ret != OC_STACK_OK)
810 OIC_LOG(ERROR, TAG, "OCStack resource error");
815 int main(int argc, char* argv[])
819 while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
824 UnicastDiscovery = atoi(optarg);
827 TestCase = atoi(optarg);
830 Connectivity = atoi(optarg);
838 if ((UnicastDiscovery != 0 && UnicastDiscovery != 1) ||
839 (TestCase < TEST_DISCOVER_REQ || TestCase >= MAX_TESTS) ||
840 (Connectivity < CT_ADAPTER_DEFAULT || Connectivity >= MAX_CT))
846 if (OCInit1(OC_CLIENT, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
848 OIC_LOG(ERROR, TAG, "OCStack init error");
852 #ifdef ROUTING_GATEWAY
854 * Before invoking Discover resource, we process the gateway requests
855 * and form the routing table.
857 for (int index = 0; index < MAX_NUM_GATEWAY_REQUEST; index++)
859 if (OC_STACK_OK != OCProcess())
861 OIC_LOG(ERROR, TAG, "OCStack process error");
864 usleep(SLEEP_DURATION);
867 if (Connectivity == CT_ADAPTER_DEFAULT || Connectivity == CT_IP)
869 ConnType = CT_ADAPTER_IP;
873 OIC_LOG(INFO, TAG, "Default Connectivity type selected...");
877 discoveryAddr[0] = '\0';
879 if (UnicastDiscovery)
881 OIC_LOG(INFO, TAG, "Enter IP address of server with optional port number");
882 OIC_LOG(INFO, TAG, "IPv4: 192.168.0.15:45454\n");
883 OIC_LOG(INFO, TAG, "IPv6: [fe80::20c:29ff:fe1b:9c5]:45454\n");
885 if (fgets(discoveryAddr, sizeof (discoveryAddr), stdin))
887 //Strip newline char from ipv4addr
888 StripNewLineChar(discoveryAddr);
892 OIC_LOG(ERROR, TAG, "!! Bad input for IP address. !!");
893 return OC_STACK_INVALID_PARAM;
897 if (UnicastDiscovery == 0 && TestCase == TEST_DISCOVER_DEV_REQ)
899 InitDeviceDiscovery(OC_LOW_QOS);
901 else if (UnicastDiscovery == 0 && TestCase == TEST_DISCOVER_PLATFORM_REQ)
903 InitPlatformDiscovery(OC_LOW_QOS);
907 InitDiscovery(OC_LOW_QOS);
910 // Break from loop with Ctrl+C
911 OIC_LOG(INFO, TAG, "Entering occlient main loop...");
912 signal(SIGINT, handleSigInt);
916 if (OCProcess() != OC_STACK_OK)
918 OIC_LOG(ERROR, TAG, "OCStack process error");
921 #ifndef ROUTING_GATEAWAY
925 OIC_LOG(INFO, TAG, "Exiting occlient main loop...");
927 if (OCStop() != OC_STACK_OK)
929 OIC_LOG(ERROR, TAG, "OCStack stop error");
935 std::string getConnectivityType (OCConnectivityType connType)
937 switch (connType & CT_MASK_ADAPTER)
948 case CT_ADAPTER_GATT_BTLE:
951 case CT_ADAPTER_RFCOMM_BTEDR:
955 return "Incorrect connectivity";