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 OCPayload* payload = (method == OC_REST_PUT) ? putPayload() : NULL;
163 ret = OCDoRequest(&handle, method, query.str().c_str(), remoteAddr,
164 payload, (ConnType), qos, &cbData, options, numOptions);
166 OCPayloadDestroy(payload);
168 if (ret != OC_STACK_OK)
170 OIC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method);
173 else if (method == OC_REST_PRESENCE)
175 gPresenceHandle = handle;
182 OCStackApplicationResult putReqCB(void* ctx, OCDoHandle /*handle*/,
183 OCClientResponse * clientResponse)
185 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
187 OIC_LOG(INFO, TAG, "Callback Context for PUT recvd successfully");
192 OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
193 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
194 OIC_LOG(INFO, TAG, ("=============> Put Response"));
198 OIC_LOG_V(INFO, TAG, "putReqCB received Null clientResponse");
200 return OC_STACK_DELETE_TRANSACTION;
203 OCStackApplicationResult postReqCB(void *ctx, OCDoHandle /*handle*/,
204 OCClientResponse *clientResponse)
206 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
208 OIC_LOG(INFO, TAG, "Callback Context for POST recvd successfully");
213 OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
214 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
215 OIC_LOG(INFO, TAG, ("=============> Post Response"));
219 OIC_LOG_V(INFO, TAG, "postReqCB received Null clientResponse");
221 return OC_STACK_DELETE_TRANSACTION;
224 OCStackApplicationResult deleteReqCB(void *ctx,
225 OCDoHandle /*handle*/,
226 OCClientResponse *clientResponse)
228 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
230 OIC_LOG(INFO, TAG, "Callback Context for DELETE recvd successfully");
235 OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
236 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
237 OIC_LOG(INFO, TAG, ("=============> Delete Response"));
241 OIC_LOG_V(INFO, TAG, "deleteReqCB received Null clientResponse");
243 return OC_STACK_DELETE_TRANSACTION;
246 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle /*handle*/,
247 OCClientResponse * clientResponse)
249 if (clientResponse == NULL)
251 OIC_LOG(INFO, TAG, "getReqCB received NULL clientResponse");
252 return OC_STACK_DELETE_TRANSACTION;
255 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
257 OIC_LOG(INFO, TAG, "Callback Context for GET query recvd successfully");
260 OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
261 OIC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
262 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
263 OIC_LOG(INFO, TAG, ("=============> Get Response"));
265 if (clientResponse->numRcvdVendorSpecificHeaderOptions > 0)
267 OIC_LOG (INFO, TAG, "Received vendor specific options");
269 OCHeaderOption * rcvdOptions = clientResponse->rcvdVendorSpecificHeaderOptions;
270 for( i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
272 if (((OCHeaderOption)rcvdOptions[i]).protocolID == OC_COAP_ID)
274 OIC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with",
275 ((OCHeaderOption)rcvdOptions[i]).optionID );
277 OIC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData,
278 MAX_HEADER_OPTION_DATA_LENGTH);
282 return OC_STACK_DELETE_TRANSACTION;
285 OCStackApplicationResult obsReqCB(void* ctx, OCDoHandle handle,
286 OCClientResponse * clientResponse)
288 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
290 OIC_LOG(INFO, TAG, "Callback Context for OBS query recvd successfully");
295 if (clientResponse->sequenceNumber <= MAX_SEQUENCE_NUMBER)
297 if (clientResponse->sequenceNumber == OC_OBSERVE_REGISTER)
299 OIC_LOG(INFO, TAG, "This also serves as a registration confirmation.");
302 OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
303 OIC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
304 OIC_LOG_V(INFO, TAG, "Callback Context for OBSERVE notification recvd successfully %d",
305 gNumObserveNotifies);
306 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
307 OIC_LOG(INFO, TAG, ("=============> Obs Response"));
308 gNumObserveNotifies++;
310 if (gNumObserveNotifies > 15) //large number to test observing in DELETE case.
312 if (TestCase == TEST_OBS_REQ_NON || TestCase == TEST_OBS_REQ_CON)
314 OIC_LOG(ERROR, TAG, "Cancelling with LOW QOS");
315 if (OCCancel (handle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
317 OIC_LOG(ERROR, TAG, "Observe cancel error");
319 return OC_STACK_DELETE_TRANSACTION;
321 else if (TestCase == TEST_OBS_REQ_NON_CANCEL_IMM)
323 OIC_LOG(ERROR, TAG, "Cancelling with HIGH QOS");
324 if (OCCancel (handle, OC_HIGH_QOS, NULL, 0) != OC_STACK_OK)
326 OIC_LOG(ERROR, TAG, "Observe cancel error");
333 OIC_LOG(INFO, TAG, "No observe option header is returned in the response.");
334 OIC_LOG(INFO, TAG, "For a registration request, it means the registration failed");
335 return OC_STACK_DELETE_TRANSACTION;
340 OIC_LOG_V(INFO, TAG, "obsReqCB received Null clientResponse");
342 return OC_STACK_KEEP_TRANSACTION;
345 OCStackApplicationResult presenceCB(void* ctx, OCDoHandle /*handle*/,
346 OCClientResponse * clientResponse)
348 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
350 OIC_LOG(INFO, TAG, "Callback Context for Presence recvd successfully");
355 OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
356 OIC_LOG_V(INFO, TAG, "Callback Context for Presence notification recvd successfully %d",
357 gNumPresenceNotifies);
358 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
359 OIC_LOG(INFO, TAG, ("=============> Presence Response"));
360 gNumPresenceNotifies++;
361 if (gNumPresenceNotifies == 20)
363 if (OCCancel(gPresenceHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
365 OIC_LOG(ERROR, TAG, "Presence cancel error");
367 return OC_STACK_DELETE_TRANSACTION;
372 OIC_LOG_V(INFO, TAG, "presenceCB received Null clientResponse");
374 return OC_STACK_KEEP_TRANSACTION;
378 // This is a function called back when a device is discovered
379 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle /*handle*/,
380 OCClientResponse * clientResponse)
382 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
384 OIC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully");
389 OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
391 std::string connectionType = getConnectivityType (clientResponse->connType);
392 OIC_LOG_V(INFO, TAG, "Discovered on %s", connectionType.c_str());
394 "Device =============> Discovered @ %s:%d",
395 clientResponse->devAddr.addr,
396 clientResponse->devAddr.port);
397 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
399 ConnType = clientResponse->connType;
400 serverAddr = clientResponse->devAddr;
402 OCDiscoveryPayload *payload = (OCDiscoveryPayload*) clientResponse->payload;
405 return OC_STACK_DELETE_TRANSACTION;
408 OCResourcePayload *resource = (OCResourcePayload*) payload->resources;
412 if(resource->uri && strcmp(resource->uri, coapServerResource.c_str()) == 0)
417 resource = resource->next;
422 OIC_LOG_V (INFO, TAG, "No %s in payload", coapServerResource.c_str());
423 return OC_STACK_KEEP_TRANSACTION;
428 case TEST_GET_REQ_NON:
429 InitGetRequest(OC_LOW_QOS, 0, 0);
431 case TEST_GET_REQ_NON_WITH_FILTERS:
432 InitGetRequest(OC_LOW_QOS, 0, 1);
434 case TEST_PUT_REQ_NON:
435 InitPutRequest(OC_LOW_QOS);
437 case TEST_POST_REQ_NON:
438 InitPostRequest(OC_LOW_QOS);
440 case TEST_DELETE_REQ_NON:
441 InitDeleteRequest(OC_LOW_QOS);
443 case TEST_OBS_REQ_NON:
444 case TEST_OBS_REQ_NON_CANCEL_IMM:
445 InitObserveRequest(OC_LOW_QOS);
447 case TEST_GET_UNAVAILABLE_RES_REQ_NON:
448 InitGetRequestToUnavailableResource(OC_LOW_QOS);
450 case TEST_GET_REQ_CON:
451 InitGetRequest(OC_HIGH_QOS, 0, 0);
453 case TEST_POST_REQ_CON:
454 InitPostRequest(OC_HIGH_QOS);
456 case TEST_DELETE_REQ_CON:
457 InitDeleteRequest(OC_HIGH_QOS);
459 case TEST_OBS_REQ_CON:
460 InitObserveRequest(OC_HIGH_QOS);
463 case TEST_OBS_PRESENCE:
464 case TEST_OBS_PRESENCE_WITH_FILTER:
465 case TEST_OBS_PRESENCE_WITH_FILTERS:
466 case TEST_OBS_MULTICAST_PRESENCE:
470 case TEST_GET_REQ_NON_WITH_VENDOR_HEADER_OPTIONS:
471 InitGetRequest(OC_LOW_QOS, 1, 0);
473 case TEST_DISCOVER_PLATFORM_REQ:
474 InitPlatformDiscovery(OC_LOW_QOS);
476 case TEST_DISCOVER_DEV_REQ:
477 InitDeviceDiscovery(OC_LOW_QOS);
486 OIC_LOG_V(INFO, TAG, "discoveryReqCB received Null clientResponse");
488 return OC_STACK_KEEP_TRANSACTION;
491 OCStackApplicationResult PlatformDiscoveryReqCB(void* ctx,
492 OCDoHandle /*handle*/,
493 OCClientResponse * clientResponse)
495 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
497 OIC_LOG(INFO, TAG, "Callback Context for Platform DISCOVER query recvd successfully");
502 OIC_LOG(INFO, TAG, ("Discovery Response:"));
503 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
507 OIC_LOG_V(INFO, TAG, "PlatformDiscoveryReqCB received Null clientResponse");
510 return (UnicastDiscovery) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
513 OCStackApplicationResult DeviceDiscoveryReqCB(void* ctx, OCDoHandle /*handle*/,
514 OCClientResponse * clientResponse)
516 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
518 OIC_LOG(INFO, TAG, "Callback Context for Device DISCOVER query recvd successfully");
523 OIC_LOG(INFO, TAG, ("Discovery Response:"));
524 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
528 OIC_LOG_V(INFO, TAG, "PlatformDiscoveryReqCB received Null clientResponse");
531 return (UnicastDiscovery) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
537 OCStackResult result = OC_STACK_OK;
538 OIC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
539 std::ostringstream query;
540 std::ostringstream querySuffix;
541 query << OC_RSRVD_PRESENCE_URI;
542 if (TestCase == TEST_OBS_PRESENCE)
544 result = InvokeOCDoResource(query, &serverAddr, OC_REST_PRESENCE,
545 OC_LOW_QOS, presenceCB, NULL, 0);
547 if (TestCase == TEST_OBS_PRESENCE_WITH_FILTER || TestCase == TEST_OBS_PRESENCE_WITH_FILTERS)
550 querySuffix << query.str() << "?rt=core.led";
551 result = InvokeOCDoResource(querySuffix, &serverAddr, OC_REST_PRESENCE,
552 OC_LOW_QOS, presenceCB, NULL, 0);
554 if (TestCase == TEST_OBS_PRESENCE_WITH_FILTERS)
556 if (result == OC_STACK_OK)
559 querySuffix << query.str() << "?rt=core.fan";
560 result = InvokeOCDoResource(querySuffix, &serverAddr, OC_REST_PRESENCE, OC_LOW_QOS,
561 presenceCB, NULL, 0);
564 if (TestCase == TEST_OBS_MULTICAST_PRESENCE)
566 if (result == OC_STACK_OK)
568 result = InvokeOCDoResource(query, NULL, OC_REST_PRESENCE, OC_LOW_QOS,
569 presenceCB, NULL, 0);
576 int InitGetRequestToUnavailableResource(OCQualityOfService qos)
578 std::ostringstream query;
579 query << "/SomeUnknownResource";
580 OIC_LOG_V(INFO, TAG, "\nExecuting %s with query %s", __func__, query.str().c_str());
581 return (InvokeOCDoResource(query, &serverAddr, OC_REST_GET, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
585 int InitObserveRequest(OCQualityOfService qos)
587 std::ostringstream query;
588 query << coapServerResource;
589 OIC_LOG_V(INFO, TAG, "\nExecuting %s with query %s", __func__, query.str().c_str());
590 return (InvokeOCDoResource(query, &serverAddr, OC_REST_OBSERVE,
591 (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, obsReqCB, NULL, 0));
594 int InitPutRequest(OCQualityOfService qos)
596 std::ostringstream query;
597 query << coapServerResource;
598 OIC_LOG_V(INFO, TAG, "\nExecuting %s with query %s", __func__, query.str().c_str());
599 return (InvokeOCDoResource(query, &serverAddr, OC_REST_PUT, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
603 int InitPostRequest(OCQualityOfService qos)
605 OCStackResult result;
607 std::ostringstream query;
608 query << coapServerResource;
610 OIC_LOG_V(INFO, TAG, "\nExecuting %s with query %s", __func__, query.str().c_str());
611 // First POST operation (to create an Light instance)
612 result = InvokeOCDoResource(query, &serverAddr, OC_REST_POST,
613 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
615 if (OC_STACK_OK != result)
617 // Error can happen if for example, network connectivity is down
618 OIC_LOG(INFO, TAG, "First POST call did not succeed");
621 // Second POST operation (to create an Light instance)
622 result = InvokeOCDoResource(query, &serverAddr, OC_REST_POST,
623 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
625 if (OC_STACK_OK != result)
627 OIC_LOG(INFO, TAG, "Second POST call did not succeed");
630 // This POST operation will update the original resourced /a/light
631 return (InvokeOCDoResource(query, &serverAddr, OC_REST_POST,
632 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
633 postReqCB, NULL, 0));
636 void* RequestDeleteDeathResourceTask(void* myqos)
638 sleep (30);//long enough to give the server time to finish deleting the resource.
639 std::ostringstream query;
640 query << coapServerResource;
642 OIC_LOG_V(INFO, TAG, "\nExecuting %s with query %s", __func__, query.str().c_str());
644 // Second DELETE operation to delete the resource that might have been removed already.
645 OCQualityOfService qos;
655 OCStackResult result = InvokeOCDoResource(query, &serverAddr, OC_REST_DELETE,
657 deleteReqCB, NULL, 0);
659 if (OC_STACK_OK != result)
661 OIC_LOG(INFO, TAG, "Second DELETE call did not succeed");
667 int InitDeleteRequest(OCQualityOfService qos)
669 OCStackResult result;
670 std::ostringstream query;
671 query << coapServerResource;
673 OIC_LOG_V(INFO, TAG, "\nExecuting %s with query %s", __func__, query.str().c_str());
675 // First DELETE operation
676 result = InvokeOCDoResource(query, &serverAddr, OC_REST_DELETE,
678 deleteReqCB, NULL, 0);
679 if (OC_STACK_OK != result)
681 // Error can happen if for example, network connectivity is down
682 OIC_LOG(INFO, TAG, "First DELETE call did not succeed");
686 //Create a thread to delete this resource again
688 pthread_create (&threadId, NULL, RequestDeleteDeathResourceTask, (void*)qos);
691 OIC_LOG_V(INFO, TAG, "\n\nExit %s", __func__);
695 int InitGetRequest(OCQualityOfService qos, uint8_t withVendorSpecificHeaderOptions,
699 OCHeaderOption options[MAX_HEADER_OPTIONS];
701 std::ostringstream query;
702 query << coapServerResource;
704 // ocserver is written to only process "power<X" query.
707 OIC_LOG(INFO, TAG, "Using query power<50");
708 query << "?power<50";
710 OIC_LOG_V(INFO, TAG, "\nExecuting %s with query %s", __func__, query.str().c_str());
712 if (withVendorSpecificHeaderOptions)
714 memset(options, 0, sizeof(OCHeaderOption)* MAX_HEADER_OPTIONS);
715 size_t numOptions = 0;
716 uint8_t option0[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
717 uint16_t optionID = 2048;
718 size_t optionDataSize = sizeof(option0);
719 OCSetHeaderOption(options,
725 uint8_t option1[] = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
727 optionDataSize = sizeof(option1);
728 OCSetHeaderOption(options,
734 if (withVendorSpecificHeaderOptions)
736 return (InvokeOCDoResource(query, &serverAddr, OC_REST_GET,
737 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, options, 2));
741 return (InvokeOCDoResource(query, &serverAddr, OC_REST_GET,
742 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, NULL, 0));
746 int InitPlatformDiscovery(OCQualityOfService qos)
748 OIC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
751 OCCallbackData cbData;
752 char szQueryUri[MAX_QUERY_LENGTH] = { 0 };
754 snprintf(szQueryUri, sizeof (szQueryUri) - 1, PLATFORM_DISCOVERY_QUERY, discoveryAddr);
756 cbData.cb = PlatformDiscoveryReqCB;
757 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
760 ret = OCDoRequest(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
761 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
763 if (ret != OC_STACK_OK)
765 OIC_LOG(ERROR, TAG, "OCStack device error");
771 int InitDeviceDiscovery(OCQualityOfService qos)
773 OIC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
776 OCCallbackData cbData;
777 char szQueryUri[MAX_QUERY_LENGTH] = { 0 };
779 snprintf(szQueryUri, sizeof (szQueryUri) - 1, DEVICE_DISCOVERY_QUERY, discoveryAddr);
781 cbData.cb = DeviceDiscoveryReqCB;
782 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
785 ret = OCDoRequest(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
786 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
788 if (ret != OC_STACK_OK)
790 OIC_LOG(ERROR, TAG, "OCStack device error");
796 int InitDiscovery(OCQualityOfService qos)
799 OCCallbackData cbData;
800 char szQueryUri[MAX_QUERY_LENGTH] = { 0 };
802 snprintf(szQueryUri, sizeof (szQueryUri) - 1, RESOURCE_DISCOVERY_QUERY, discoveryAddr);
804 cbData.cb = discoveryReqCB;
805 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
808 ret = OCDoRequest(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
809 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
811 if (ret != OC_STACK_OK)
813 OIC_LOG(ERROR, TAG, "OCStack resource error");
818 int main(int argc, char* argv[])
822 while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
827 UnicastDiscovery = atoi(optarg);
830 TestCase = atoi(optarg);
833 Connectivity = atoi(optarg);
841 if ((UnicastDiscovery != 0 && UnicastDiscovery != 1) ||
842 (TestCase < TEST_DISCOVER_REQ || TestCase >= MAX_TESTS) ||
843 (Connectivity < CT_ADAPTER_DEFAULT || Connectivity >= MAX_CT))
849 if (OCInit1(OC_CLIENT, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
851 OIC_LOG(ERROR, TAG, "OCStack init error");
855 #ifdef ROUTING_GATEWAY
857 * Before invoking Discover resource, we process the gateway requests
858 * and form the routing table.
860 for (int index = 0; index < MAX_NUM_GATEWAY_REQUEST; index++)
862 if (OC_STACK_OK != OCProcess())
864 OIC_LOG(ERROR, TAG, "OCStack process error");
867 usleep(SLEEP_DURATION);
870 if (Connectivity == CT_ADAPTER_DEFAULT || Connectivity == CT_IP)
872 ConnType = CT_ADAPTER_IP;
876 OIC_LOG(INFO, TAG, "Default Connectivity type selected...");
880 discoveryAddr[0] = '\0';
882 if (UnicastDiscovery)
884 OIC_LOG(INFO, TAG, "Enter IP address of server with optional port number");
885 OIC_LOG(INFO, TAG, "IPv4: 192.168.0.15:45454\n");
886 OIC_LOG(INFO, TAG, "IPv6: [fe80::20c:29ff:fe1b:9c5]:45454\n");
888 if (fgets(discoveryAddr, sizeof (discoveryAddr), stdin))
890 //Strip newline char from ipv4addr
891 StripNewLineChar(discoveryAddr);
895 OIC_LOG(ERROR, TAG, "!! Bad input for IP address. !!");
896 return OC_STACK_INVALID_PARAM;
900 if (UnicastDiscovery == 0 && TestCase == TEST_DISCOVER_DEV_REQ)
902 InitDeviceDiscovery(OC_LOW_QOS);
904 else if (UnicastDiscovery == 0 && TestCase == TEST_DISCOVER_PLATFORM_REQ)
906 InitPlatformDiscovery(OC_LOW_QOS);
910 InitDiscovery(OC_LOW_QOS);
913 // Break from loop with Ctrl+C
914 OIC_LOG(INFO, TAG, "Entering occlient main loop...");
915 signal(SIGINT, handleSigInt);
919 if (OCProcess() != OC_STACK_OK)
921 OIC_LOG(ERROR, TAG, "OCStack process error");
924 #ifndef ROUTING_GATEAWAY
928 OIC_LOG(INFO, TAG, "Exiting occlient main loop...");
930 if (OCStop() != OC_STACK_OK)
932 OIC_LOG(ERROR, TAG, "OCStack stop error");
938 std::string getConnectivityType (OCConnectivityType connType)
940 switch (connType & CT_MASK_ADAPTER)
951 case CT_ADAPTER_GATT_BTLE:
954 case CT_ADAPTER_RFCOMM_BTEDR:
958 return "Incorrect connectivity";