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);
63 // The handle for the observe registration
64 OCDoHandle gObserveDoHandle;
66 // The handle for observe registration
67 OCDoHandle gPresenceHandle;
69 // After this crosses a threshold client deregisters for further notifications
70 int gNumObserveNotifies = 0;
73 int gNumPresenceNotifies = 0;
77 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
78 void handleSigInt(int signum)
86 OCPayload* putPayload()
88 OCRepPayload* payload = OCRepPayloadCreate();
92 std::cout << "Failed to create put payload object"<<std::endl;
96 OCRepPayloadSetPropInt(payload, "power", 15);
97 OCRepPayloadSetPropBool(payload, "state", true);
99 return (OCPayload*) payload;
102 static void PrintUsage()
104 OC_LOG(INFO, TAG, "Usage : occlient -u <0|1> -t <1..17> -c <0|1>");
105 OC_LOG(INFO, TAG, "-u <0|1> : Perform multicast/unicast discovery of resources");
106 OC_LOG(INFO, TAG, "-c 0 : Use Default connectivity(IP)");
107 OC_LOG(INFO, TAG, "-c 1 : IP Connectivity Type");
108 OC_LOG(INFO, TAG, "-t 1 : Discover Resources");
109 OC_LOG(INFO, TAG, "-t 2 : Discover Resources and Initiate Nonconfirmable Get Request");
110 OC_LOG(INFO, TAG, "-t 3 : Discover Resources and Initiate Nonconfirmable Get Request"
111 " with query filter.");
112 OC_LOG(INFO, TAG, "-t 4 : Discover Resources and Initiate Nonconfirmable Put Requests");
113 OC_LOG(INFO, TAG, "-t 5 : Discover Resources and Initiate Nonconfirmable Post Requests");
114 OC_LOG(INFO, TAG, "-t 6 : Discover Resources and Initiate Nonconfirmable Delete Requests");
115 OC_LOG(INFO, TAG, "-t 7 : Discover Resources and Initiate Nonconfirmable Observe Requests");
116 OC_LOG(INFO, TAG, "-t 8 : Discover Resources and Initiate Nonconfirmable Get Request "\
117 "for a resource which is unavailable");
118 OC_LOG(INFO, TAG, "-t 9 : Discover Resources and Initiate Confirmable Get Request");
119 OC_LOG(INFO, TAG, "-t 10 : Discover Resources and Initiate Confirmable Post Request");
120 OC_LOG(INFO, TAG, "-t 11 : Discover Resources and Initiate Confirmable Delete Requests");
121 OC_LOG(INFO, TAG, "-t 12 : Discover Resources and Initiate Confirmable Observe Requests"\
122 " and cancel with Low QoS");
125 OC_LOG(INFO, TAG, "-t 13 : Discover Resources and Initiate Nonconfirmable presence");
126 OC_LOG(INFO, TAG, "-t 14 : Discover Resources and Initiate Nonconfirmable presence with "\
128 OC_LOG(INFO, TAG, "-t 15 : Discover Resources and Initiate Nonconfirmable presence with "\
130 OC_LOG(INFO, TAG, "-t 16 : Discover Resources and Initiate Nonconfirmable multicast presence.");
133 OC_LOG(INFO, TAG, "-t 17 : Discover Resources and Initiate Nonconfirmable Observe Requests "\
134 "then cancel immediately with High QOS");
135 OC_LOG(INFO, TAG, "-t 18 : Discover Resources and Initiate Nonconfirmable Get Request and "\
136 "add vendor specific header options");
137 OC_LOG(INFO, TAG, "-t 19 : Discover Platform");
138 OC_LOG(INFO, TAG, "-t 20 : Discover Devices");
141 OCStackResult InvokeOCDoResource(std::ostringstream &query,
142 OCDevAddr *remoteAddr,
144 OCQualityOfService qos,
145 OCClientResponseHandler cb,
146 OCHeaderOption * options,
150 OCCallbackData cbData;
154 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
157 ret = OCDoResource(&handle, method, query.str().c_str(), remoteAddr,
158 (method == OC_REST_PUT) ? putPayload() : NULL,
159 (ConnType), qos, &cbData, options, numOptions);
161 if (ret != OC_STACK_OK)
163 OC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method);
165 else if (method == OC_REST_OBSERVE || method == OC_REST_OBSERVE_ALL)
167 gObserveDoHandle = handle;
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 OC_LOG(INFO, TAG, "Callback Context for PUT recvd successfully");
189 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
190 OC_LOG_PAYLOAD(INFO, clientResponse->payload);
191 OC_LOG(INFO, TAG, ("=============> Put Response"));
195 OC_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 OC_LOG(INFO, TAG, "Callback Context for POST recvd successfully");
210 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
211 OC_LOG_PAYLOAD(INFO, clientResponse->payload);
212 OC_LOG(INFO, TAG, ("=============> Post Response"));
216 OC_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 OC_LOG(INFO, TAG, "Callback Context for DELETE recvd successfully");
232 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
233 OC_LOG_PAYLOAD(INFO, clientResponse->payload);
234 OC_LOG(INFO, TAG, ("=============> Delete Response"));
238 OC_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 OC_LOG(INFO, TAG, "getReqCB received NULL clientResponse");
249 return OC_STACK_DELETE_TRANSACTION;
252 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
254 OC_LOG(INFO, TAG, "Callback Context for GET query recvd successfully");
257 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
258 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
259 OC_LOG_PAYLOAD(INFO, clientResponse->payload);
260 OC_LOG(INFO, TAG, ("=============> Get Response"));
262 if (clientResponse->numRcvdVendorSpecificHeaderOptions > 0)
264 OC_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 OC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with",
272 ((OCHeaderOption)rcvdOptions[i]).optionID );
274 OC_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 OC_LOG(INFO, TAG, "Callback Context for OBS query recvd successfully");
292 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
293 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
294 OC_LOG_V(INFO, TAG, "Callback Context for OBSERVE notification recvd successfully %d",
295 gNumObserveNotifies);
296 OC_LOG_PAYLOAD(INFO, clientResponse->payload);
297 OC_LOG(INFO, TAG, ("=============> Obs Response"));
298 gNumObserveNotifies++;
299 if (gNumObserveNotifies == 15) //large number to test observing in DELETE case.
301 if (TestCase == TEST_OBS_REQ_NON || TestCase == TEST_OBS_REQ_CON)
303 if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
305 OC_LOG(ERROR, TAG, "Observe cancel error");
307 return OC_STACK_DELETE_TRANSACTION;
309 else if (TestCase == TEST_OBS_REQ_NON_CANCEL_IMM)
311 if (OCCancel (gObserveDoHandle, OC_HIGH_QOS, NULL, 0) != OC_STACK_OK)
313 OC_LOG(ERROR, TAG, "Observe cancel error");
317 if (clientResponse->sequenceNumber == OC_OBSERVE_REGISTER)
319 OC_LOG(INFO, TAG, "This also serves as a registration confirmation");
321 else if (clientResponse->sequenceNumber == OC_OBSERVE_DEREGISTER)
323 OC_LOG(INFO, TAG, "This also serves as a deregistration confirmation");
324 return OC_STACK_DELETE_TRANSACTION;
326 else if (clientResponse->sequenceNumber == OC_OBSERVE_NO_OPTION)
328 OC_LOG(INFO, TAG, "This also tells you that registration/deregistration failed");
329 return OC_STACK_DELETE_TRANSACTION;
334 OC_LOG_V(INFO, TAG, "obsReqCB received Null clientResponse");
336 return OC_STACK_KEEP_TRANSACTION;
339 OCStackApplicationResult presenceCB(void* ctx, OCDoHandle /*handle*/,
340 OCClientResponse * clientResponse)
342 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
344 OC_LOG(INFO, TAG, "Callback Context for Presence recvd successfully");
349 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
350 OC_LOG_V(INFO, TAG, "Callback Context for Presence notification recvd successfully %d",
351 gNumPresenceNotifies);
352 OC_LOG_PAYLOAD(INFO, clientResponse->payload);
353 OC_LOG(INFO, TAG, ("=============> Presence Response"));
354 gNumPresenceNotifies++;
355 if (gNumPresenceNotifies == 20)
357 if (OCCancel(gPresenceHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
359 OC_LOG(ERROR, TAG, "Presence cancel error");
361 return OC_STACK_DELETE_TRANSACTION;
366 OC_LOG_V(INFO, TAG, "presenceCB received Null clientResponse");
368 return OC_STACK_KEEP_TRANSACTION;
372 // This is a function called back when a device is discovered
373 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle /*handle*/,
374 OCClientResponse * clientResponse)
376 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
378 OC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully");
383 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
385 std::string connectionType = getConnectivityType (clientResponse->connType);
386 OC_LOG_V(INFO, TAG, "Discovered on %s", connectionType.c_str());
388 "Device =============> Discovered @ %s:%d",
389 clientResponse->devAddr.addr,
390 clientResponse->devAddr.port);
391 OC_LOG_PAYLOAD(INFO, clientResponse->payload);
393 ConnType = clientResponse->connType;
394 serverAddr = clientResponse->devAddr;
395 parseClientResponse(clientResponse);
399 case TEST_GET_REQ_NON:
400 InitGetRequest(OC_LOW_QOS, 0, 0);
402 case TEST_GET_REQ_NON_WITH_FILTERS:
403 InitGetRequest(OC_LOW_QOS, 0, 1);
405 case TEST_PUT_REQ_NON:
406 InitPutRequest(OC_LOW_QOS);
408 case TEST_POST_REQ_NON:
409 InitPostRequest(OC_LOW_QOS);
411 case TEST_DELETE_REQ_NON:
412 InitDeleteRequest(OC_LOW_QOS);
414 case TEST_OBS_REQ_NON:
415 case TEST_OBS_REQ_NON_CANCEL_IMM:
416 InitObserveRequest(OC_LOW_QOS);
418 case TEST_GET_UNAVAILABLE_RES_REQ_NON:
419 InitGetRequestToUnavailableResource(OC_LOW_QOS);
421 case TEST_GET_REQ_CON:
422 InitGetRequest(OC_HIGH_QOS, 0, 0);
424 case TEST_POST_REQ_CON:
425 InitPostRequest(OC_HIGH_QOS);
427 case TEST_DELETE_REQ_CON:
428 InitDeleteRequest(OC_HIGH_QOS);
430 case TEST_OBS_REQ_CON:
431 InitObserveRequest(OC_HIGH_QOS);
434 case TEST_OBS_PRESENCE:
435 case TEST_OBS_PRESENCE_WITH_FILTER:
436 case TEST_OBS_PRESENCE_WITH_FILTERS:
437 case TEST_OBS_MULTICAST_PRESENCE:
441 case TEST_GET_REQ_NON_WITH_VENDOR_HEADER_OPTIONS:
442 InitGetRequest(OC_LOW_QOS, 1, 0);
444 case TEST_DISCOVER_PLATFORM_REQ:
445 InitPlatformDiscovery(OC_LOW_QOS);
447 case TEST_DISCOVER_DEV_REQ:
448 InitDeviceDiscovery(OC_LOW_QOS);
457 OC_LOG_V(INFO, TAG, "discoveryReqCB received Null clientResponse");
459 return OC_STACK_KEEP_TRANSACTION;
462 OCStackApplicationResult PlatformDiscoveryReqCB(void* ctx,
463 OCDoHandle /*handle*/,
464 OCClientResponse * clientResponse)
466 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
468 OC_LOG(INFO, TAG, "Callback Context for Platform DISCOVER query recvd successfully");
473 OC_LOG(INFO, TAG, ("Discovery Response:"));
474 OC_LOG_PAYLOAD(INFO, clientResponse->payload);
478 OC_LOG_V(INFO, TAG, "PlatformDiscoveryReqCB received Null clientResponse");
481 return (UnicastDiscovery) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
484 OCStackApplicationResult DeviceDiscoveryReqCB(void* ctx, OCDoHandle /*handle*/,
485 OCClientResponse * clientResponse)
487 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
489 OC_LOG(INFO, TAG, "Callback Context for Device DISCOVER query recvd successfully");
494 OC_LOG(INFO, TAG, ("Discovery Response:"));
495 OC_LOG_PAYLOAD(INFO, clientResponse->payload);
499 OC_LOG_V(INFO, TAG, "PlatformDiscoveryReqCB received Null clientResponse");
502 return (UnicastDiscovery) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
508 OCStackResult result = OC_STACK_OK;
509 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
510 std::ostringstream query;
511 std::ostringstream querySuffix;
512 query << OC_RSRVD_PRESENCE_URI;
513 if (TestCase == TEST_OBS_PRESENCE)
515 result = InvokeOCDoResource(query, &serverAddr, OC_REST_PRESENCE,
516 OC_LOW_QOS, presenceCB, NULL, 0);
518 if (TestCase == TEST_OBS_PRESENCE_WITH_FILTER || TestCase == TEST_OBS_PRESENCE_WITH_FILTERS)
521 querySuffix << query.str() << "?rt=core.led";
522 result = InvokeOCDoResource(querySuffix, &serverAddr, OC_REST_PRESENCE,
523 OC_LOW_QOS, presenceCB, NULL, 0);
525 if (TestCase == TEST_OBS_PRESENCE_WITH_FILTERS)
527 if (result == OC_STACK_OK)
530 querySuffix << query.str() << "?rt=core.fan";
531 result = InvokeOCDoResource(querySuffix, &serverAddr, OC_REST_PRESENCE, OC_LOW_QOS,
532 presenceCB, NULL, 0);
535 if (TestCase == TEST_OBS_MULTICAST_PRESENCE)
537 if (result == OC_STACK_OK)
539 std::ostringstream multicastPresenceQuery;
540 multicastPresenceQuery.str("");
541 multicastPresenceQuery << "coap://" << OC_MULTICAST_PREFIX << OC_RSRVD_PRESENCE_URI;
542 result = InvokeOCDoResource(multicastPresenceQuery, &serverAddr, OC_REST_PRESENCE, OC_LOW_QOS,
543 presenceCB, NULL, 0);
550 int InitGetRequestToUnavailableResource(OCQualityOfService qos)
552 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
553 std::ostringstream query;
554 query << "/SomeUnknownResource";
555 return (InvokeOCDoResource(query, &serverAddr, OC_REST_GET, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
559 int InitObserveRequest(OCQualityOfService qos)
561 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
562 std::ostringstream query;
563 query << coapServerResource;
564 return (InvokeOCDoResource(query, &serverAddr, OC_REST_OBSERVE,
565 (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, obsReqCB, NULL, 0));
568 int InitPutRequest(OCQualityOfService qos)
570 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
571 std::ostringstream query;
572 query << coapServerResource;
573 return (InvokeOCDoResource(query, &serverAddr, OC_REST_PUT, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
577 int InitPostRequest(OCQualityOfService qos)
579 OCStackResult result;
580 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
581 std::ostringstream query;
582 query << coapServerResource;
584 // First POST operation (to create an Light instance)
585 result = InvokeOCDoResource(query, &serverAddr, OC_REST_POST,
586 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
588 if (OC_STACK_OK != result)
590 // Error can happen if for example, network connectivity is down
591 OC_LOG(INFO, TAG, "First POST call did not succeed");
594 // Second 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 OC_LOG(INFO, TAG, "Second POST call did not succeed");
603 // This POST operation will update the original resourced /a/light
604 return (InvokeOCDoResource(query, &serverAddr, OC_REST_POST,
605 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
606 postReqCB, NULL, 0));
609 void* RequestDeleteDeathResourceTask(void* myqos)
611 sleep (30);//long enough to give the server time to finish deleting the resource.
612 std::ostringstream query;
613 query << coapServerResource;
615 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
617 // Second DELETE operation to delete the resource that might have been removed already.
618 OCQualityOfService qos;
628 OCStackResult result = InvokeOCDoResource(query, &serverAddr, OC_REST_DELETE,
630 deleteReqCB, NULL, 0);
632 if (OC_STACK_OK != result)
634 OC_LOG(INFO, TAG, "Second DELETE call did not succeed");
640 int InitDeleteRequest(OCQualityOfService qos)
642 OCStackResult result;
643 std::ostringstream query;
644 query << coapServerResource;
646 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
648 // First DELETE operation
649 result = InvokeOCDoResource(query, &serverAddr, OC_REST_DELETE,
651 deleteReqCB, NULL, 0);
652 if (OC_STACK_OK != result)
654 // Error can happen if for example, network connectivity is down
655 OC_LOG(INFO, TAG, "First DELETE call did not succeed");
659 //Create a thread to delete this resource again
661 pthread_create (&threadId, NULL, RequestDeleteDeathResourceTask, (void*)qos);
664 OC_LOG_V(INFO, TAG, "\n\nExit %s", __func__);
668 int InitGetRequest(OCQualityOfService qos, uint8_t withVendorSpecificHeaderOptions, bool getWithQuery)
671 OCHeaderOption options[MAX_HEADER_OPTIONS];
673 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
674 std::ostringstream query;
675 query << coapServerResource;
677 // ocserver is written to only process "power<X" query.
680 OC_LOG(INFO, TAG, "Using query power<50");
681 query << "?power<50";
684 if (withVendorSpecificHeaderOptions)
686 uint8_t option0[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
687 uint8_t option1[] = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
688 memset(options, 0, sizeof(OCHeaderOption) * MAX_HEADER_OPTIONS);
689 options[0].protocolID = OC_COAP_ID;
690 options[0].optionID = 2048;
691 memcpy(options[0].optionData, option0, sizeof(option0));
692 options[0].optionLength = 10;
693 options[1].protocolID = OC_COAP_ID;
694 options[1].optionID = 3000;
695 memcpy(options[1].optionData, option1, sizeof(option1));
696 options[1].optionLength = 10;
698 if (withVendorSpecificHeaderOptions)
700 return (InvokeOCDoResource(query, &serverAddr, OC_REST_GET,
701 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, options, 2));
705 return (InvokeOCDoResource(query, &serverAddr, OC_REST_GET,
706 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, NULL, 0));
710 int InitPlatformDiscovery(OCQualityOfService qos)
712 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
715 OCCallbackData cbData;
716 char szQueryUri[64] = { 0 };
718 snprintf(szQueryUri, sizeof (szQueryUri) - 1, PLATFORM_DISCOVERY_QUERY, discoveryAddr);
720 cbData.cb = PlatformDiscoveryReqCB;
721 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
724 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
725 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
727 if (ret != OC_STACK_OK)
729 OC_LOG(ERROR, TAG, "OCStack device error");
735 int InitDeviceDiscovery(OCQualityOfService qos)
737 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
740 OCCallbackData cbData;
741 char szQueryUri[100] = { 0 };
743 snprintf(szQueryUri, sizeof (szQueryUri) - 1, DEVICE_DISCOVERY_QUERY, discoveryAddr);
745 cbData.cb = DeviceDiscoveryReqCB;
746 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
749 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
750 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
752 if (ret != OC_STACK_OK)
754 OC_LOG(ERROR, TAG, "OCStack device error");
760 int InitDiscovery(OCQualityOfService qos)
763 OCCallbackData cbData;
764 char szQueryUri[100] = { 0 };
766 snprintf(szQueryUri, sizeof (szQueryUri) - 1, RESOURCE_DISCOVERY_QUERY, discoveryAddr);
768 cbData.cb = discoveryReqCB;
769 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
772 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
773 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
775 if (ret != OC_STACK_OK)
777 OC_LOG(ERROR, TAG, "OCStack resource error");
782 int main(int argc, char* argv[])
786 while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
791 UnicastDiscovery = atoi(optarg);
794 TestCase = atoi(optarg);
797 Connectivity = atoi(optarg);
805 if ((UnicastDiscovery != 0 && UnicastDiscovery != 1) ||
806 (TestCase < TEST_DISCOVER_REQ || TestCase >= MAX_TESTS) ||
807 (Connectivity < CT_ADAPTER_DEFAULT || Connectivity >= MAX_CT))
813 if (OCInit1(OC_CLIENT, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
815 OC_LOG(ERROR, TAG, "OCStack init error");
819 #ifdef ROUTING_GATEWAY
821 * Before invoking Discover resource, we process the gateway requests
822 * and form the routing table.
824 for (int index = 0; index < MAX_NUM_GATEWAY_REQUEST; index++)
826 if (OC_STACK_OK != OCProcess())
828 OC_LOG(ERROR, TAG, "OCStack process error");
831 usleep(SLEEP_DURATION);
834 if (Connectivity == CT_ADAPTER_DEFAULT || Connectivity == CT_IP)
836 ConnType = CT_ADAPTER_IP;
840 OC_LOG(INFO, TAG, "Default Connectivity type selected...");
844 discoveryAddr[0] = '\0';
846 if (UnicastDiscovery)
848 OC_LOG(INFO, TAG, "Enter IP address of server with optional port number");
849 OC_LOG(INFO, TAG, "IPv4: 192.168.0.15:45454\n");
850 OC_LOG(INFO, TAG, "IPv6: [fe80::20c:29ff:fe1b:9c5]:45454\n");
852 if (fgets(discoveryAddr, sizeof (discoveryAddr), stdin))
854 //Strip newline char from ipv4addr
855 StripNewLineChar(discoveryAddr);
859 OC_LOG(ERROR, TAG, "!! Bad input for IP address. !!");
860 return OC_STACK_INVALID_PARAM;
864 if (UnicastDiscovery == 0 && TestCase == TEST_DISCOVER_DEV_REQ)
866 InitDeviceDiscovery(OC_LOW_QOS);
868 else if (UnicastDiscovery == 0 && TestCase == TEST_DISCOVER_PLATFORM_REQ)
870 InitPlatformDiscovery(OC_LOW_QOS);
874 InitDiscovery(OC_LOW_QOS);
877 // Break from loop with Ctrl+C
878 OC_LOG(INFO, TAG, "Entering occlient main loop...");
879 signal(SIGINT, handleSigInt);
883 if (OCProcess() != OC_STACK_OK)
885 OC_LOG(ERROR, TAG, "OCStack process error");
888 #ifndef ROUTING_GATEAWAY
892 OC_LOG(INFO, TAG, "Exiting occlient main loop...");
894 if (OCStop() != OC_STACK_OK)
896 OC_LOG(ERROR, TAG, "OCStack stop error");
902 std::string getConnectivityType (OCConnectivityType connType)
904 switch (connType & CT_MASK_ADAPTER)
915 case CT_ADAPTER_GATT_BTLE:
918 case CT_ADAPTER_RFCOMM_BTEDR:
922 return "Incorrect connectivity";
926 std::string getQueryStrForGetPut(OCClientResponse * /*clientResponse*/)
931 void parseClientResponse(OCClientResponse * clientResponse)
933 coapServerResource = getQueryStrForGetPut(clientResponse);