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 // Tracking user input
35 static int UnicastDiscovery = 0;
36 static int TestCase = 0;
37 static int Connectivity = 0;
39 static const char *DEVICE_DISCOVERY_QUERY = "%s/oic/d";
40 static const char *PLATFORM_DISCOVERY_QUERY = "%s/oic/p";
41 static const char *RESOURCE_DISCOVERY_QUERY = "%s/oic/res";
43 //The following variable determines the interface protocol (IPv4, IPv6, etc)
44 //to be used for sending unicast messages. Default set to IP dual stack.
45 static OCConnectivityType ConnType = CT_ADAPTER_IP;
46 static OCDevAddr serverAddr;
47 static char discoveryAddr[100];
48 static std::string coapServerResource = "/a/light";
50 void StripNewLineChar(char* str);
52 // The handle for the observe registration
53 OCDoHandle gObserveDoHandle;
55 // The handle for observe registration
56 OCDoHandle gPresenceHandle;
58 // After this crosses a threshold client deregisters for further notifications
59 int gNumObserveNotifies = 0;
62 int gNumPresenceNotifies = 0;
66 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
67 void handleSigInt(int signum)
75 OCPayload* putPayload()
77 OCRepPayload* payload = OCRepPayloadCreate();
81 std::cout << "Failed to create put payload object"<<std::endl;
85 OCRepPayloadSetPropInt(payload, "power", 15);
86 OCRepPayloadSetPropBool(payload, "state", true);
88 return (OCPayload*) payload;
91 static void PrintUsage()
93 OC_LOG(INFO, TAG, "Usage : occlient -u <0|1> -t <1..17> -c <0|1>");
94 OC_LOG(INFO, TAG, "-u <0|1> : Perform multicast/unicast discovery of resources");
95 OC_LOG(INFO, TAG, "-c 0 : Use Default connectivity(IP)");
96 OC_LOG(INFO, TAG, "-c 1 : IP Connectivity Type");
97 OC_LOG(INFO, TAG, "-t 1 : Discover Resources");
98 OC_LOG(INFO, TAG, "-t 2 : Discover Resources and Initiate Nonconfirmable Get Request");
99 OC_LOG(INFO, TAG, "-t 3 : Discover Resources and Initiate Nonconfirmable Get Request"
100 " with query filter.");
101 OC_LOG(INFO, TAG, "-t 4 : Discover Resources and Initiate Nonconfirmable Put Requests");
102 OC_LOG(INFO, TAG, "-t 5 : Discover Resources and Initiate Nonconfirmable Post Requests");
103 OC_LOG(INFO, TAG, "-t 6 : Discover Resources and Initiate Nonconfirmable Delete Requests");
104 OC_LOG(INFO, TAG, "-t 7 : Discover Resources and Initiate Nonconfirmable Observe Requests");
105 OC_LOG(INFO, TAG, "-t 8 : Discover Resources and Initiate Nonconfirmable Get Request "\
106 "for a resource which is unavailable");
107 OC_LOG(INFO, TAG, "-t 9 : Discover Resources and Initiate Confirmable Get Request");
108 OC_LOG(INFO, TAG, "-t 10 : Discover Resources and Initiate Confirmable Post Request");
109 OC_LOG(INFO, TAG, "-t 11 : Discover Resources and Initiate Confirmable Delete Requests");
110 OC_LOG(INFO, TAG, "-t 12 : Discover Resources and Initiate Confirmable Observe Requests"\
111 " and cancel with Low QoS");
114 OC_LOG(INFO, TAG, "-t 13 : Discover Resources and Initiate Nonconfirmable presence");
115 OC_LOG(INFO, TAG, "-t 14 : Discover Resources and Initiate Nonconfirmable presence with "\
117 OC_LOG(INFO, TAG, "-t 15 : Discover Resources and Initiate Nonconfirmable presence with "\
119 OC_LOG(INFO, TAG, "-t 16 : Discover Resources and Initiate Nonconfirmable multicast presence.");
122 OC_LOG(INFO, TAG, "-t 17 : Discover Resources and Initiate Nonconfirmable Observe Requests "\
123 "then cancel immediately with High QOS");
124 OC_LOG(INFO, TAG, "-t 18 : Discover Resources and Initiate Nonconfirmable Get Request and "\
125 "add vendor specific header options");
126 OC_LOG(INFO, TAG, "-t 19 : Discover Platform");
127 OC_LOG(INFO, TAG, "-t 20 : Discover Devices");
130 OCStackResult InvokeOCDoResource(std::ostringstream &query,
131 OCDevAddr *remoteAddr,
133 OCQualityOfService qos,
134 OCClientResponseHandler cb,
135 OCHeaderOption * options,
139 OCCallbackData cbData;
143 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
146 ret = OCDoResource(&handle, method, query.str().c_str(), remoteAddr,
147 (method == OC_REST_PUT) ? putPayload() : NULL,
148 (ConnType), qos, &cbData, options, numOptions);
150 if (ret != OC_STACK_OK)
152 OC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method);
154 else if (method == OC_REST_OBSERVE || method == OC_REST_OBSERVE_ALL)
156 gObserveDoHandle = handle;
159 else if (method == OC_REST_PRESENCE)
161 gPresenceHandle = handle;
168 OCStackApplicationResult putReqCB(void* ctx, OCDoHandle /*handle*/,
169 OCClientResponse * clientResponse)
171 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
173 OC_LOG(INFO, TAG, "Callback Context for PUT recvd successfully");
178 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
179 OC_LOG_PAYLOAD(INFO, clientResponse->payload);
180 OC_LOG(INFO, TAG, ("=============> Put Response"));
184 OC_LOG_V(INFO, TAG, "putReqCB received Null clientResponse");
186 return OC_STACK_DELETE_TRANSACTION;
189 OCStackApplicationResult postReqCB(void *ctx, OCDoHandle /*handle*/,
190 OCClientResponse *clientResponse)
192 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
194 OC_LOG(INFO, TAG, "Callback Context for POST recvd successfully");
199 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
200 OC_LOG_PAYLOAD(INFO, clientResponse->payload);
201 OC_LOG(INFO, TAG, ("=============> Post Response"));
205 OC_LOG_V(INFO, TAG, "postReqCB received Null clientResponse");
207 return OC_STACK_DELETE_TRANSACTION;
210 OCStackApplicationResult deleteReqCB(void *ctx,
211 OCDoHandle /*handle*/,
212 OCClientResponse *clientResponse)
214 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
216 OC_LOG(INFO, TAG, "Callback Context for DELETE recvd successfully");
221 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
222 OC_LOG_PAYLOAD(INFO, clientResponse->payload);
223 OC_LOG(INFO, TAG, ("=============> Delete Response"));
227 OC_LOG_V(INFO, TAG, "deleteReqCB received Null clientResponse");
229 return OC_STACK_DELETE_TRANSACTION;
232 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle /*handle*/,
233 OCClientResponse * clientResponse)
235 if (clientResponse == NULL)
237 OC_LOG(INFO, TAG, "getReqCB received NULL clientResponse");
238 return OC_STACK_DELETE_TRANSACTION;
241 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
243 OC_LOG(INFO, TAG, "Callback Context for GET query recvd successfully");
246 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
247 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
248 OC_LOG_PAYLOAD(INFO, clientResponse->payload);
249 OC_LOG(INFO, TAG, ("=============> Get Response"));
251 if (clientResponse->numRcvdVendorSpecificHeaderOptions > 0)
253 OC_LOG (INFO, TAG, "Received vendor specific options");
255 OCHeaderOption * rcvdOptions = clientResponse->rcvdVendorSpecificHeaderOptions;
256 for( i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
258 if (((OCHeaderOption)rcvdOptions[i]).protocolID == OC_COAP_ID)
260 OC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with",
261 ((OCHeaderOption)rcvdOptions[i]).optionID );
263 OC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData,
264 MAX_HEADER_OPTION_DATA_LENGTH);
268 return OC_STACK_DELETE_TRANSACTION;
271 OCStackApplicationResult obsReqCB(void* ctx, OCDoHandle /*handle*/,
272 OCClientResponse * clientResponse)
274 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
276 OC_LOG(INFO, TAG, "Callback Context for OBS query recvd successfully");
281 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
282 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
283 OC_LOG_V(INFO, TAG, "Callback Context for OBSERVE notification recvd successfully %d",
284 gNumObserveNotifies);
285 OC_LOG_PAYLOAD(INFO, clientResponse->payload);
286 OC_LOG(INFO, TAG, ("=============> Obs Response"));
287 gNumObserveNotifies++;
288 if (gNumObserveNotifies == 15) //large number to test observing in DELETE case.
290 if (TestCase == TEST_OBS_REQ_NON || TestCase == TEST_OBS_REQ_CON)
292 if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
294 OC_LOG(ERROR, TAG, "Observe cancel error");
296 return OC_STACK_DELETE_TRANSACTION;
298 else if (TestCase == TEST_OBS_REQ_NON_CANCEL_IMM)
300 if (OCCancel (gObserveDoHandle, OC_HIGH_QOS, NULL, 0) != OC_STACK_OK)
302 OC_LOG(ERROR, TAG, "Observe cancel error");
306 if (clientResponse->sequenceNumber == OC_OBSERVE_REGISTER)
308 OC_LOG(INFO, TAG, "This also serves as a registration confirmation");
310 else if (clientResponse->sequenceNumber == OC_OBSERVE_DEREGISTER)
312 OC_LOG(INFO, TAG, "This also serves as a deregistration confirmation");
313 return OC_STACK_DELETE_TRANSACTION;
315 else if (clientResponse->sequenceNumber == OC_OBSERVE_NO_OPTION)
317 OC_LOG(INFO, TAG, "This also tells you that registration/deregistration failed");
318 return OC_STACK_DELETE_TRANSACTION;
323 OC_LOG_V(INFO, TAG, "obsReqCB received Null clientResponse");
325 return OC_STACK_KEEP_TRANSACTION;
328 OCStackApplicationResult presenceCB(void* ctx, OCDoHandle /*handle*/,
329 OCClientResponse * clientResponse)
331 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
333 OC_LOG(INFO, TAG, "Callback Context for Presence recvd successfully");
338 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
339 OC_LOG_V(INFO, TAG, "Callback Context for Presence notification recvd successfully %d",
340 gNumPresenceNotifies);
341 OC_LOG_PAYLOAD(INFO, clientResponse->payload);
342 OC_LOG(INFO, TAG, ("=============> Presence Response"));
343 gNumPresenceNotifies++;
344 if (gNumPresenceNotifies == 20)
346 if (OCCancel(gPresenceHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
348 OC_LOG(ERROR, TAG, "Presence cancel error");
350 return OC_STACK_DELETE_TRANSACTION;
355 OC_LOG_V(INFO, TAG, "presenceCB received Null clientResponse");
357 return OC_STACK_KEEP_TRANSACTION;
361 // This is a function called back when a device is discovered
362 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle /*handle*/,
363 OCClientResponse * clientResponse)
365 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
367 OC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully");
372 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
374 std::string connectionType = getConnectivityType (clientResponse->connType);
375 OC_LOG_V(INFO, TAG, "Discovered on %s", connectionType.c_str());
377 "Device =============> Discovered @ %s:%d",
378 clientResponse->devAddr.addr,
379 clientResponse->devAddr.port);
380 OC_LOG_PAYLOAD(INFO, clientResponse->payload);
382 ConnType = clientResponse->connType;
383 serverAddr = clientResponse->devAddr;
384 parseClientResponse(clientResponse);
388 case TEST_GET_REQ_NON:
389 InitGetRequest(OC_LOW_QOS, 0, 0);
391 case TEST_GET_REQ_NON_WITH_FILTERS:
392 InitGetRequest(OC_LOW_QOS, 0, 1);
394 case TEST_PUT_REQ_NON:
395 InitPutRequest(OC_LOW_QOS);
397 case TEST_POST_REQ_NON:
398 InitPostRequest(OC_LOW_QOS);
400 case TEST_DELETE_REQ_NON:
401 InitDeleteRequest(OC_LOW_QOS);
403 case TEST_OBS_REQ_NON:
404 case TEST_OBS_REQ_NON_CANCEL_IMM:
405 InitObserveRequest(OC_LOW_QOS);
407 case TEST_GET_UNAVAILABLE_RES_REQ_NON:
408 InitGetRequestToUnavailableResource(OC_LOW_QOS);
410 case TEST_GET_REQ_CON:
411 InitGetRequest(OC_HIGH_QOS, 0, 0);
413 case TEST_POST_REQ_CON:
414 InitPostRequest(OC_HIGH_QOS);
416 case TEST_DELETE_REQ_CON:
417 InitDeleteRequest(OC_HIGH_QOS);
419 case TEST_OBS_REQ_CON:
420 InitObserveRequest(OC_HIGH_QOS);
423 case TEST_OBS_PRESENCE:
424 case TEST_OBS_PRESENCE_WITH_FILTER:
425 case TEST_OBS_PRESENCE_WITH_FILTERS:
426 case TEST_OBS_MULTICAST_PRESENCE:
430 case TEST_GET_REQ_NON_WITH_VENDOR_HEADER_OPTIONS:
431 InitGetRequest(OC_LOW_QOS, 1, 0);
433 case TEST_DISCOVER_PLATFORM_REQ:
434 InitPlatformDiscovery(OC_LOW_QOS);
436 case TEST_DISCOVER_DEV_REQ:
437 InitDeviceDiscovery(OC_LOW_QOS);
446 OC_LOG_V(INFO, TAG, "discoveryReqCB received Null clientResponse");
448 return OC_STACK_KEEP_TRANSACTION;
451 OCStackApplicationResult PlatformDiscoveryReqCB(void* ctx,
452 OCDoHandle /*handle*/,
453 OCClientResponse * clientResponse)
455 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
457 OC_LOG(INFO, TAG, "Callback Context for Platform DISCOVER query recvd successfully");
462 OC_LOG(INFO, TAG, ("Discovery Response:"));
463 OC_LOG_PAYLOAD(INFO, clientResponse->payload);
467 OC_LOG_V(INFO, TAG, "PlatformDiscoveryReqCB received Null clientResponse");
470 return (UnicastDiscovery) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
473 OCStackApplicationResult DeviceDiscoveryReqCB(void* ctx, OCDoHandle /*handle*/,
474 OCClientResponse * clientResponse)
476 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
478 OC_LOG(INFO, TAG, "Callback Context for Device DISCOVER query recvd successfully");
483 OC_LOG(INFO, TAG, ("Discovery Response:"));
484 OC_LOG_PAYLOAD(INFO, clientResponse->payload);
488 OC_LOG_V(INFO, TAG, "PlatformDiscoveryReqCB received Null clientResponse");
491 return (UnicastDiscovery) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
497 OCStackResult result = OC_STACK_OK;
498 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
499 std::ostringstream query;
500 std::ostringstream querySuffix;
501 query << OC_RSRVD_PRESENCE_URI;
502 if (TestCase == TEST_OBS_PRESENCE)
504 result = InvokeOCDoResource(query, &serverAddr, OC_REST_PRESENCE,
505 OC_LOW_QOS, presenceCB, NULL, 0);
507 if (TestCase == TEST_OBS_PRESENCE_WITH_FILTER || TestCase == TEST_OBS_PRESENCE_WITH_FILTERS)
510 querySuffix << query.str() << "?rt=core.led";
511 result = InvokeOCDoResource(querySuffix, &serverAddr, OC_REST_PRESENCE,
512 OC_LOW_QOS, presenceCB, NULL, 0);
514 if (TestCase == TEST_OBS_PRESENCE_WITH_FILTERS)
516 if (result == OC_STACK_OK)
519 querySuffix << query.str() << "?rt=core.fan";
520 result = InvokeOCDoResource(querySuffix, &serverAddr, OC_REST_PRESENCE, OC_LOW_QOS,
521 presenceCB, NULL, 0);
524 if (TestCase == TEST_OBS_MULTICAST_PRESENCE)
526 if (result == OC_STACK_OK)
528 std::ostringstream multicastPresenceQuery;
529 multicastPresenceQuery.str("");
530 multicastPresenceQuery << "coap://" << OC_MULTICAST_PREFIX << OC_RSRVD_PRESENCE_URI;
531 result = InvokeOCDoResource(multicastPresenceQuery, &serverAddr, OC_REST_PRESENCE, OC_LOW_QOS,
532 presenceCB, NULL, 0);
539 int InitGetRequestToUnavailableResource(OCQualityOfService qos)
541 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
542 std::ostringstream query;
543 query << "/SomeUnknownResource";
544 return (InvokeOCDoResource(query, &serverAddr, OC_REST_GET, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
548 int InitObserveRequest(OCQualityOfService qos)
550 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
551 std::ostringstream query;
552 query << coapServerResource;
553 return (InvokeOCDoResource(query, &serverAddr, OC_REST_OBSERVE,
554 (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, obsReqCB, NULL, 0));
557 int InitPutRequest(OCQualityOfService qos)
559 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
560 std::ostringstream query;
561 query << coapServerResource;
562 return (InvokeOCDoResource(query, &serverAddr, OC_REST_PUT, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
566 int InitPostRequest(OCQualityOfService qos)
568 OCStackResult result;
569 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
570 std::ostringstream query;
571 query << coapServerResource;
573 // First POST operation (to create an Light instance)
574 result = InvokeOCDoResource(query, &serverAddr, OC_REST_POST,
575 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
577 if (OC_STACK_OK != result)
579 // Error can happen if for example, network connectivity is down
580 OC_LOG(INFO, TAG, "First POST call did not succeed");
583 // Second POST operation (to create an Light instance)
584 result = InvokeOCDoResource(query, &serverAddr, OC_REST_POST,
585 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
587 if (OC_STACK_OK != result)
589 OC_LOG(INFO, TAG, "Second POST call did not succeed");
592 // This POST operation will update the original resourced /a/light
593 return (InvokeOCDoResource(query, &serverAddr, OC_REST_POST,
594 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
595 postReqCB, NULL, 0));
598 void* RequestDeleteDeathResourceTask(void* myqos)
600 sleep (30);//long enough to give the server time to finish deleting the resource.
601 std::ostringstream query;
602 query << coapServerResource;
604 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
606 // Second DELETE operation to delete the resource that might have been removed already.
607 OCQualityOfService qos;
617 OCStackResult result = InvokeOCDoResource(query, &serverAddr, OC_REST_DELETE,
619 deleteReqCB, NULL, 0);
621 if (OC_STACK_OK != result)
623 OC_LOG(INFO, TAG, "Second DELETE call did not succeed");
629 int InitDeleteRequest(OCQualityOfService qos)
631 OCStackResult result;
632 std::ostringstream query;
633 query << coapServerResource;
635 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
637 // First DELETE operation
638 result = InvokeOCDoResource(query, &serverAddr, OC_REST_DELETE,
640 deleteReqCB, NULL, 0);
641 if (OC_STACK_OK != result)
643 // Error can happen if for example, network connectivity is down
644 OC_LOG(INFO, TAG, "First DELETE call did not succeed");
648 //Create a thread to delete this resource again
650 pthread_create (&threadId, NULL, RequestDeleteDeathResourceTask, (void*)qos);
653 OC_LOG_V(INFO, TAG, "\n\nExit %s", __func__);
657 int InitGetRequest(OCQualityOfService qos, uint8_t withVendorSpecificHeaderOptions, bool getWithQuery)
660 OCHeaderOption options[MAX_HEADER_OPTIONS];
662 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
663 std::ostringstream query;
664 query << coapServerResource;
666 // ocserver is written to only process "power<X" query.
669 OC_LOG(INFO, TAG, "Using query power<50");
670 query << "?power<50";
673 if (withVendorSpecificHeaderOptions)
675 uint8_t option0[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
676 uint8_t option1[] = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
677 memset(options, 0, sizeof(OCHeaderOption) * MAX_HEADER_OPTIONS);
678 options[0].protocolID = OC_COAP_ID;
679 options[0].optionID = 2048;
680 memcpy(options[0].optionData, option0, sizeof(option0));
681 options[0].optionLength = 10;
682 options[1].protocolID = OC_COAP_ID;
683 options[1].optionID = 3000;
684 memcpy(options[1].optionData, option1, sizeof(option1));
685 options[1].optionLength = 10;
687 if (withVendorSpecificHeaderOptions)
689 return (InvokeOCDoResource(query, &serverAddr, OC_REST_GET,
690 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, options, 2));
694 return (InvokeOCDoResource(query, &serverAddr, OC_REST_GET,
695 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, NULL, 0));
699 int InitPlatformDiscovery(OCQualityOfService qos)
701 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
704 OCCallbackData cbData;
705 char szQueryUri[64] = { 0 };
707 snprintf(szQueryUri, sizeof (szQueryUri) - 1, PLATFORM_DISCOVERY_QUERY, discoveryAddr);
709 cbData.cb = PlatformDiscoveryReqCB;
710 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
713 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
714 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
716 if (ret != OC_STACK_OK)
718 OC_LOG(ERROR, TAG, "OCStack device error");
724 int InitDeviceDiscovery(OCQualityOfService qos)
726 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
729 OCCallbackData cbData;
730 char szQueryUri[100] = { 0 };
732 snprintf(szQueryUri, sizeof (szQueryUri) - 1, DEVICE_DISCOVERY_QUERY, discoveryAddr);
734 cbData.cb = DeviceDiscoveryReqCB;
735 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
738 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
739 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
741 if (ret != OC_STACK_OK)
743 OC_LOG(ERROR, TAG, "OCStack device error");
749 int InitDiscovery(OCQualityOfService qos)
752 OCCallbackData cbData;
753 char szQueryUri[100] = { 0 };
755 snprintf(szQueryUri, sizeof (szQueryUri) - 1, RESOURCE_DISCOVERY_QUERY, discoveryAddr);
757 cbData.cb = discoveryReqCB;
758 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
761 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
762 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
764 if (ret != OC_STACK_OK)
766 OC_LOG(ERROR, TAG, "OCStack resource error");
771 int main(int argc, char* argv[])
775 while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
780 UnicastDiscovery = atoi(optarg);
783 TestCase = atoi(optarg);
786 Connectivity = atoi(optarg);
794 if ((UnicastDiscovery != 0 && UnicastDiscovery != 1) ||
795 (TestCase < TEST_DISCOVER_REQ || TestCase >= MAX_TESTS) ||
796 (Connectivity < CT_ADAPTER_DEFAULT || Connectivity >= MAX_CT))
802 if (OCInit1(OC_CLIENT, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
804 OC_LOG(ERROR, TAG, "OCStack init error");
808 if (Connectivity == CT_ADAPTER_DEFAULT || Connectivity == CT_IP)
810 ConnType = CT_ADAPTER_IP;
814 OC_LOG(INFO, TAG, "Default Connectivity type selected...");
818 discoveryAddr[0] = '\0';
820 if (UnicastDiscovery)
822 OC_LOG(INFO, TAG, "Enter IP address of server with optional port number");
823 OC_LOG(INFO, TAG, "IPv4: 192.168.0.15:45454\n");
824 OC_LOG(INFO, TAG, "IPv6: [fe80::20c:29ff:fe1b:9c5]:45454\n");
826 if (fgets(discoveryAddr, sizeof (discoveryAddr), stdin))
828 //Strip newline char from ipv4addr
829 StripNewLineChar(discoveryAddr);
833 OC_LOG(ERROR, TAG, "!! Bad input for IP address. !!");
834 return OC_STACK_INVALID_PARAM;
838 if (UnicastDiscovery == 0 && TestCase == TEST_DISCOVER_DEV_REQ)
840 InitDeviceDiscovery(OC_LOW_QOS);
842 else if (UnicastDiscovery == 0 && TestCase == TEST_DISCOVER_PLATFORM_REQ)
844 InitPlatformDiscovery(OC_LOW_QOS);
848 InitDiscovery(OC_LOW_QOS);
851 // Break from loop with Ctrl+C
852 OC_LOG(INFO, TAG, "Entering occlient main loop...");
853 signal(SIGINT, handleSigInt);
857 if (OCProcess() != OC_STACK_OK)
859 OC_LOG(ERROR, TAG, "OCStack process error");
865 OC_LOG(INFO, TAG, "Exiting occlient main loop...");
867 if (OCStop() != OC_STACK_OK)
869 OC_LOG(ERROR, TAG, "OCStack stop error");
875 std::string getConnectivityType (OCConnectivityType connType)
877 switch (connType & CT_MASK_ADAPTER)
888 case CT_ADAPTER_GATT_BTLE:
891 case CT_ADAPTER_RFCOMM_BTEDR:
895 return "Incorrect connectivity";
899 std::string getQueryStrForGetPut(OCClientResponse * /*clientResponse*/)
904 void parseClientResponse(OCClientResponse * clientResponse)
906 coapServerResource = getQueryStrForGetPut(clientResponse);