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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
32 // Tracking user input
33 static int UNICAST_DISCOVERY = 0;
34 static int TEST_CASE = 0;
35 static int CONNECTIVITY = 0;
37 static const char * UNICAST_DEVICE_DISCOVERY_QUERY = "coap://%s/oic/d";
38 static const char * MULTICAST_DEVICE_DISCOVERY_QUERY = "/oic/d";
39 static const char * UNICAST_PLATFORM_DISCOVERY_QUERY = "coap://%s/oic/p";
40 static const char * MULTICAST_PLATFORM_DISCOVERY_QUERY = "/oic/p";
42 static const char * UNICAST_RESOURCE_DISCOVERY_QUERY = "coap://%s/oic/res";
43 static const char * MULTICAST_RESOURCE_DISCOVERY_QUERY = "/oic/res";
44 //The following variable determines the interface protocol (IPv4, IPv6, etc)
45 //to be used for sending unicast messages. Default set to IPv4.
46 static OCConnectivityType OC_CONNTYPE = CT_ADAPTER_IP;
47 static std::string putPayload = "{\"oic\":[{\"rep\":{\"power\":15,\"state\":true}}]}";
48 static std::string coapServerIP = "255.255.255.255";
49 static std::string coapServerPort = "5683";
50 static std::string coapServerResource = "/a/light";
51 // Size to hold IPV4_ADDRESS:PORT
52 static const int IPV4_ADDR_SIZE = 24;
53 //Use ipv4addr for both InitDiscovery and InitPlatformOrDeviceDiscovery
54 char ipv4addr[IPV4_ADDR_SIZE];
55 void StripNewLineChar(char* str);
57 // The handle for the observe registration
58 OCDoHandle gObserveDoHandle;
60 // The handle for observe registration
61 OCDoHandle gPresenceHandle;
63 // After this crosses a threshold client deregisters for further notifications
64 int gNumObserveNotifies = 0;
67 int gNumPresenceNotifies = 0;
71 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
72 void handleSigInt(int signum)
80 static void PrintUsage()
82 OC_LOG(INFO, TAG, "Usage : occlient -u <0|1> -t <1..17> -c <0|1>");
83 OC_LOG(INFO, TAG, "-u <0|1> : Perform multicast/unicast discovery of resources");
84 OC_LOG(INFO, TAG, "-c 0 : Use Default connectivity(IP)");
85 OC_LOG(INFO, TAG, "-c 1 : IP Connectivity Type");
86 OC_LOG(INFO, TAG, "-t 1 : Discover Resources");
87 OC_LOG(INFO, TAG, "-t 2 : Discover Resources and Initiate Nonconfirmable Get Request");
88 OC_LOG(INFO, TAG, "-t 3 : Discover Resources and Initiate Nonconfirmable Get Request"
89 " with query filter.");
90 OC_LOG(INFO, TAG, "-t 4 : Discover Resources and Initiate Nonconfirmable Put Requests");
91 OC_LOG(INFO, TAG, "-t 5 : Discover Resources and Initiate Nonconfirmable Post Requests");
92 OC_LOG(INFO, TAG, "-t 6 : Discover Resources and Initiate Nonconfirmable Delete Requests");
93 OC_LOG(INFO, TAG, "-t 7 : Discover Resources and Initiate Nonconfirmable Observe Requests");
94 OC_LOG(INFO, TAG, "-t 8 : Discover Resources and Initiate Nonconfirmable Get Request "\
95 "for a resource which is unavailable");
96 OC_LOG(INFO, TAG, "-t 9 : Discover Resources and Initiate Confirmable Get Request");
97 OC_LOG(INFO, TAG, "-t 10 : Discover Resources and Initiate Confirmable Post Request");
98 OC_LOG(INFO, TAG, "-t 11 : Discover Resources and Initiate Confirmable Delete Requests");
99 OC_LOG(INFO, TAG, "-t 12 : Discover Resources and Initiate Confirmable Observe Requests"\
100 " and cancel with Low QoS");
103 OC_LOG(INFO, TAG, "-t 13 : Discover Resources and Initiate Nonconfirmable presence");
104 OC_LOG(INFO, TAG, "-t 14 : Discover Resources and Initiate Nonconfirmable presence with "\
106 OC_LOG(INFO, TAG, "-t 15 : Discover Resources and Initiate Nonconfirmable presence with "\
108 OC_LOG(INFO, TAG, "-t 16 : Discover Resources and Initiate Nonconfirmable multicast presence.");
111 OC_LOG(INFO, TAG, "-t 17 : Discover Resources and Initiate Nonconfirmable Observe Requests "\
112 "then cancel immediately with High QOS");
113 OC_LOG(INFO, TAG, "-t 18 : Discover Resources and Initiate Nonconfirmable Get Request and "\
114 "add vendor specific header options");
115 OC_LOG(INFO, TAG, "-t 19 : Discover Platform");
116 OC_LOG(INFO, TAG, "-t 20 : Discover Devices");
119 OCStackResult InvokeOCDoResource(std::ostringstream &query,
121 OCQualityOfService qos,
122 OCClientResponseHandler cb,
123 OCHeaderOption * options,
127 OCCallbackData cbData;
131 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
134 ret = OCDoResource(&handle, method, query.str().c_str(), 0,
135 (method == OC_REST_PUT) ? putPayload.c_str() : NULL,
136 (OC_CONNTYPE), qos, &cbData, options, numOptions);
138 if (ret != OC_STACK_OK)
140 OC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method);
142 else if (method == OC_REST_OBSERVE || method == OC_REST_OBSERVE_ALL)
144 gObserveDoHandle = handle;
147 else if (method == OC_REST_PRESENCE)
149 gPresenceHandle = handle;
156 OCStackApplicationResult putReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
158 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
160 OC_LOG(INFO, TAG, "Callback Context for PUT recvd successfully");
165 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
166 OC_LOG_V(INFO, TAG, "JSON = %s =============> Put Response",
167 clientResponse->resJSONPayload);
171 OC_LOG_V(INFO, TAG, "putReqCB received Null clientResponse");
173 return OC_STACK_DELETE_TRANSACTION;
176 OCStackApplicationResult postReqCB(void *ctx, OCDoHandle handle, OCClientResponse *clientResponse)
178 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
180 OC_LOG(INFO, TAG, "Callback Context for POST recvd successfully");
185 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
186 OC_LOG_V(INFO, TAG, "JSON = %s =============> Post Response",
187 clientResponse->resJSONPayload);
191 OC_LOG_V(INFO, TAG, "postReqCB received Null clientResponse");
193 return OC_STACK_DELETE_TRANSACTION;
196 OCStackApplicationResult deleteReqCB(void *ctx,
197 OCDoHandle handle, OCClientResponse *clientResponse)
199 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
201 OC_LOG(INFO, TAG, "Callback Context for DELETE recvd successfully");
206 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
207 OC_LOG_V(INFO, TAG, "JSON = %s =============> Delete Response",
208 clientResponse->resJSONPayload);
212 OC_LOG_V(INFO, TAG, "deleteReqCB received Null clientResponse");
214 return OC_STACK_DELETE_TRANSACTION;
217 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
219 if(clientResponse == NULL)
221 OC_LOG(INFO, TAG, "getReqCB received NULL clientResponse");
222 return OC_STACK_DELETE_TRANSACTION;
225 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
227 OC_LOG(INFO, TAG, "Callback Context for GET query recvd successfully");
230 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
231 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
232 OC_LOG_V(INFO, TAG, "JSON = %s =============> Get Response", clientResponse->resJSONPayload);
234 if(clientResponse->numRcvdVendorSpecificHeaderOptions > 0)
236 OC_LOG (INFO, TAG, "Received vendor specific options");
238 OCHeaderOption * rcvdOptions = clientResponse->rcvdVendorSpecificHeaderOptions;
239 for( i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
241 if(((OCHeaderOption)rcvdOptions[i]).protocolID == OC_COAP_ID)
243 OC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with",
244 ((OCHeaderOption)rcvdOptions[i]).optionID );
246 OC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData,
247 MAX_HEADER_OPTION_DATA_LENGTH);
251 return OC_STACK_DELETE_TRANSACTION;
254 OCStackApplicationResult obsReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
256 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
258 OC_LOG(INFO, TAG, "Callback Context for OBS query recvd successfully");
263 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
264 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
265 OC_LOG_V(INFO, TAG, "Callback Context for OBSERVE notification recvd successfully %d",
266 gNumObserveNotifies);
267 OC_LOG_V(INFO, TAG, "JSON = %s =============> Obs Response",
268 clientResponse->resJSONPayload);
269 gNumObserveNotifies++;
270 if (gNumObserveNotifies == 15) //large number to test observing in DELETE case.
272 if(TEST_CASE == TEST_OBS_REQ_NON || TEST_CASE == TEST_OBS_REQ_CON)
274 if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
276 OC_LOG(ERROR, TAG, "Observe cancel error");
278 return OC_STACK_DELETE_TRANSACTION;
280 else if(TEST_CASE == TEST_OBS_REQ_NON_CANCEL_IMM)
282 if (OCCancel (gObserveDoHandle, OC_HIGH_QOS, NULL, 0) != OC_STACK_OK)
284 OC_LOG(ERROR, TAG, "Observe cancel error");
288 if(clientResponse->sequenceNumber == OC_OBSERVE_REGISTER)
290 OC_LOG(INFO, TAG, "This also serves as a registration confirmation");
292 else if(clientResponse->sequenceNumber == OC_OBSERVE_DEREGISTER)
294 OC_LOG(INFO, TAG, "This also serves as a deregistration confirmation");
295 return OC_STACK_DELETE_TRANSACTION;
297 else if(clientResponse->sequenceNumber == OC_OBSERVE_NO_OPTION)
299 OC_LOG(INFO, TAG, "This also tells you that registration/deregistration failed");
300 return OC_STACK_DELETE_TRANSACTION;
305 OC_LOG_V(INFO, TAG, "obsReqCB received Null clientResponse");
307 return OC_STACK_KEEP_TRANSACTION;
310 OCStackApplicationResult presenceCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
312 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
314 OC_LOG(INFO, TAG, "Callback Context for Presence recvd successfully");
319 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
320 OC_LOG_V(INFO, TAG, "NONCE NUMBER: %u", clientResponse->sequenceNumber);
321 OC_LOG_V(INFO, TAG, "Callback Context for Presence notification recvd successfully %d",
322 gNumPresenceNotifies);
323 OC_LOG_V(INFO, TAG, "JSON = %s =============> Presence Response",
324 clientResponse->resJSONPayload);
325 gNumPresenceNotifies++;
326 if (gNumPresenceNotifies == 20)
328 if (OCCancel(gPresenceHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
330 OC_LOG(ERROR, TAG, "Presence cancel error");
332 return OC_STACK_DELETE_TRANSACTION;
337 OC_LOG_V(INFO, TAG, "presenceCB received Null clientResponse");
339 return OC_STACK_KEEP_TRANSACTION;
343 // This is a function called back when a device is discovered
344 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
345 OCClientResponse * clientResponse)
347 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
349 OC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully");
354 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
356 std::string connectionType = getConnectivityType (clientResponse->connType);
357 OC_LOG_V(INFO, TAG, "Discovered on %s", connectionType.c_str());
359 "Device =============> Discovered %s @ %s:%d",
360 clientResponse->resJSONPayload, clientResponse->devAddr.addr, clientResponse->devAddr.port);
362 parseClientResponse(clientResponse);
366 case TEST_GET_REQ_NON:
367 InitGetRequest(OC_LOW_QOS, 0, 0);
369 case TEST_GET_REQ_NON_WITH_FILTERS:
370 InitGetRequest(OC_LOW_QOS, 0, 1);
372 case TEST_PUT_REQ_NON:
373 InitPutRequest(OC_LOW_QOS);
375 case TEST_POST_REQ_NON:
376 InitPostRequest(OC_LOW_QOS);
378 case TEST_DELETE_REQ_NON:
379 InitDeleteRequest(OC_LOW_QOS);
381 case TEST_OBS_REQ_NON:
382 case TEST_OBS_REQ_NON_CANCEL_IMM:
383 InitObserveRequest(OC_LOW_QOS);
385 case TEST_GET_UNAVAILABLE_RES_REQ_NON:
386 InitGetRequestToUnavailableResource(OC_LOW_QOS);
388 case TEST_GET_REQ_CON:
389 InitGetRequest(OC_HIGH_QOS, 0, 0);
391 case TEST_POST_REQ_CON:
392 InitPostRequest(OC_HIGH_QOS);
394 case TEST_DELETE_REQ_CON:
395 InitDeleteRequest(OC_HIGH_QOS);
397 case TEST_OBS_REQ_CON:
398 InitObserveRequest(OC_HIGH_QOS);
401 case TEST_OBS_PRESENCE:
402 case TEST_OBS_PRESENCE_WITH_FILTER:
403 case TEST_OBS_PRESENCE_WITH_FILTERS:
404 case TEST_OBS_MULTICAST_PRESENCE:
408 case TEST_GET_REQ_NON_WITH_VENDOR_HEADER_OPTIONS:
409 InitGetRequest(OC_LOW_QOS, 1, 0);
411 case TEST_DISCOVER_PLATFORM_REQ:
412 InitPlatformDiscovery(OC_LOW_QOS);
414 case TEST_DISCOVER_DEV_REQ:
415 InitDeviceDiscovery(OC_LOW_QOS);
424 OC_LOG_V(INFO, TAG, "discoveryReqCB received Null clientResponse");
426 return OC_STACK_KEEP_TRANSACTION;
429 OCStackApplicationResult PlatformDiscoveryReqCB (void* ctx, OCDoHandle handle,
430 OCClientResponse * clientResponse)
432 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
434 OC_LOG(INFO, TAG, "Callback Context for Platform DISCOVER query recvd successfully");
439 //OC_LOG truncates the response as it is too long.
440 fprintf(stderr, "Discovery response: \n %s\n", clientResponse->resJSONPayload);
445 OC_LOG_V(INFO, TAG, "PlatformDiscoveryReqCB received Null clientResponse");
448 return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
451 OCStackApplicationResult DeviceDiscoveryReqCB (void* ctx, OCDoHandle handle,
452 OCClientResponse * clientResponse)
454 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
456 OC_LOG(INFO, TAG, "Callback Context for Device DISCOVER query recvd successfully");
461 //OC_LOG truncates the response as it is too long.
462 fprintf(stderr, "Discovery response: \n %s\n", clientResponse->resJSONPayload);
467 OC_LOG_V(INFO, TAG, "PlatformDiscoveryReqCB received Null clientResponse");
470 return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
476 OCStackResult result = OC_STACK_OK;
477 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
478 std::ostringstream query;
479 std::ostringstream querySuffix;
480 query << "coap://" << coapServerIP << ":" << coapServerPort << OC_PRESENCE_URI;
481 if(TEST_CASE == TEST_OBS_PRESENCE)
483 result = InvokeOCDoResource(query, OC_REST_PRESENCE, OC_LOW_QOS,
484 presenceCB, NULL, 0);
486 if(TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTER || TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTERS)
489 querySuffix << query.str() << "?rt=core.led";
490 result = InvokeOCDoResource(querySuffix, OC_REST_PRESENCE, OC_LOW_QOS,
491 presenceCB, NULL, 0);
493 if(TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTERS)
495 if(result == OC_STACK_OK)
498 querySuffix << query.str() << "?rt=core.fan";
499 result = InvokeOCDoResource(querySuffix, OC_REST_PRESENCE, OC_LOW_QOS,
500 presenceCB, NULL, 0);
503 if(TEST_CASE == TEST_OBS_MULTICAST_PRESENCE)
505 if(result == OC_STACK_OK)
507 std::ostringstream multicastPresenceQuery;
508 multicastPresenceQuery.str("");
509 multicastPresenceQuery << "coap://" << OC_MULTICAST_PREFIX << OC_PRESENCE_URI;
510 result = InvokeOCDoResource(multicastPresenceQuery, OC_REST_PRESENCE, OC_LOW_QOS,
511 presenceCB, NULL, 0);
518 int InitGetRequestToUnavailableResource(OCQualityOfService qos)
520 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
521 std::ostringstream query;
522 query << "coap://" << coapServerIP << ":" << coapServerPort << "/SomeUnknownResource";
523 return (InvokeOCDoResource(query, OC_REST_GET, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
527 int InitObserveRequest(OCQualityOfService qos)
529 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
530 std::ostringstream query;
531 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
532 return (InvokeOCDoResource(query,
533 OC_REST_OBSERVE, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, obsReqCB, NULL, 0));
536 int InitPutRequest(OCQualityOfService qos)
538 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
539 std::ostringstream query;
540 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
541 return (InvokeOCDoResource(query, OC_REST_PUT, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
545 int InitPostRequest(OCQualityOfService qos)
547 OCStackResult result;
548 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
549 std::ostringstream query;
550 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
552 // First POST operation (to create an Light instance)
553 result = InvokeOCDoResource(query, OC_REST_POST,
554 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
556 if (OC_STACK_OK != result)
558 // Error can happen if for example, network connectivity is down
559 OC_LOG(INFO, TAG, "First POST call did not succeed");
562 // Second POST operation (to create an Light instance)
563 result = InvokeOCDoResource(query, OC_REST_POST,
564 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
566 if (OC_STACK_OK != result)
568 OC_LOG(INFO, TAG, "Second POST call did not succeed");
571 // This POST operation will update the original resourced /a/light
572 return (InvokeOCDoResource(query, OC_REST_POST,
573 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
574 postReqCB, NULL, 0));
577 void* RequestDeleteDeathResourceTask(void* myqos)
579 sleep (30);//long enough to give the server time to finish deleting the resource.
580 std::ostringstream query;
581 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
583 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
585 // Second DELETE operation to delete the resource that might have been removed already.
586 OCQualityOfService qos;
596 OCStackResult result = InvokeOCDoResource(query, OC_REST_DELETE,
598 deleteReqCB, NULL, 0);
600 if (OC_STACK_OK != result)
602 OC_LOG(INFO, TAG, "Second DELETE call did not succeed");
608 int InitDeleteRequest(OCQualityOfService qos)
610 OCStackResult result;
611 std::ostringstream query;
612 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
614 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
616 // First DELETE operation
617 result = InvokeOCDoResource(query, OC_REST_DELETE,
619 deleteReqCB, NULL, 0);
620 if (OC_STACK_OK != result)
622 // Error can happen if for example, network connectivity is down
623 OC_LOG(INFO, TAG, "First DELETE call did not succeed");
627 //Create a thread to delete this resource again
629 pthread_create (&threadId, NULL, RequestDeleteDeathResourceTask, (void*)qos);
632 OC_LOG_V(INFO, TAG, "\n\nExit %s", __func__);
636 int InitGetRequest(OCQualityOfService qos, uint8_t withVendorSpecificHeaderOptions, bool getWithQuery)
639 OCHeaderOption options[MAX_HEADER_OPTIONS];
641 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
642 std::ostringstream query;
643 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
645 // ocserver is written to only process "power<X" query.
648 OC_LOG(INFO, TAG, "Using query power<30");
649 query << "?power<30";
652 if (withVendorSpecificHeaderOptions)
654 uint8_t option0[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
655 uint8_t option1[] = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
656 memset(options, 0, sizeof(OCHeaderOption) * MAX_HEADER_OPTIONS);
657 options[0].protocolID = OC_COAP_ID;
658 options[0].optionID = 2048;
659 memcpy(options[0].optionData, option0, sizeof(option0));
660 options[0].optionLength = 10;
661 options[1].protocolID = OC_COAP_ID;
662 options[1].optionID = 3000;
663 memcpy(options[1].optionData, option1, sizeof(option1));
664 options[1].optionLength = 10;
666 if (withVendorSpecificHeaderOptions)
668 return (InvokeOCDoResource(query, OC_REST_GET,
669 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, options, 2));
673 return (InvokeOCDoResource(query, OC_REST_GET,
674 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, NULL, 0));
678 int InitPlatformDiscovery(OCQualityOfService qos)
680 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
683 OCCallbackData cbData;
684 char szQueryUri[64] = { 0 };
686 cbData.cb = PlatformDiscoveryReqCB;
687 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
690 if(UNICAST_DISCOVERY)
692 snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_PLATFORM_DISCOVERY_QUERY, ipv4addr);
696 strncpy(szQueryUri, MULTICAST_PLATFORM_DISCOVERY_QUERY, sizeof(szQueryUri) -1 );
698 szQueryUri[sizeof(szQueryUri) -1] = '\0';
700 if(UNICAST_DISCOVERY)
702 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, OC_CONNTYPE,
703 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
708 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, 0, 0, OC_CONNTYPE,
709 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
712 if (ret != OC_STACK_OK)
714 OC_LOG(ERROR, TAG, "OCStack device error");
720 int InitDeviceDiscovery(OCQualityOfService qos)
722 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
725 OCCallbackData cbData;
726 char szQueryUri[64] = { 0 };
728 cbData.cb = DeviceDiscoveryReqCB;
729 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
732 if(UNICAST_DISCOVERY)
734 snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_DEVICE_DISCOVERY_QUERY, ipv4addr);
738 strncpy(szQueryUri, MULTICAST_DEVICE_DISCOVERY_QUERY, sizeof(szQueryUri) -1 );
740 szQueryUri[sizeof(szQueryUri) -1] = '\0';
742 if(UNICAST_DISCOVERY)
744 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, OC_CONNTYPE,
745 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
749 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, 0, 0, OC_CONNTYPE,
750 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
753 if (ret != OC_STACK_OK)
755 OC_LOG(ERROR, TAG, "OCStack device error");
761 int InitDiscovery(OCQualityOfService qos)
764 OCCallbackData cbData;
765 /* Start a discovery query*/
766 char szQueryUri[64] = { 0 };
768 if (UNICAST_DISCOVERY)
770 snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_RESOURCE_DISCOVERY_QUERY, ipv4addr);
774 strcpy(szQueryUri, MULTICAST_RESOURCE_DISCOVERY_QUERY);
777 cbData.cb = discoveryReqCB;
778 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
780 if(UNICAST_DISCOVERY)
782 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, OC_CONNTYPE,
783 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
787 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, 0, 0, OC_CONNTYPE,
788 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
790 if (ret != OC_STACK_OK)
792 OC_LOG(ERROR, TAG, "OCStack resource error");
797 int main(int argc, char* argv[])
801 while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
806 UNICAST_DISCOVERY = atoi(optarg);
809 TEST_CASE = atoi(optarg);
812 CONNECTIVITY = atoi(optarg);
820 if ((UNICAST_DISCOVERY != 0 && UNICAST_DISCOVERY != 1) ||
821 (TEST_CASE < TEST_DISCOVER_REQ || TEST_CASE >= MAX_TESTS) ||
822 (CONNECTIVITY < CT_ADAPTER_DEFAULT || CONNECTIVITY >= MAX_CT))
828 /* Initialize OCStack*/
829 if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK)
831 OC_LOG(ERROR, TAG, "OCStack init error");
835 if(CONNECTIVITY == CT_ADAPTER_DEFAULT || CONNECTIVITY == CT_IP)
837 OC_CONNTYPE = CT_ADAPTER_IP;
841 OC_LOG(INFO, TAG, "Default Connectivity type selected...");
845 if (UNICAST_DISCOVERY)
847 OC_LOG(INFO, TAG, "Enter IP address with port number of the Server hosting resource");
848 OC_LOG(INFO, TAG, "as follows - eg: 192.168.0.15:45454 (IP:Port) \n");
850 if (fgets(ipv4addr, IPV4_ADDR_SIZE, stdin))
852 //Strip newline char from ipv4addr
853 StripNewLineChar(ipv4addr);
857 OC_LOG(ERROR, TAG, "!! Bad input for IPV4 address. !!");
858 return OC_STACK_INVALID_PARAM;
862 if(UNICAST_DISCOVERY == 0 && TEST_CASE == TEST_DISCOVER_DEV_REQ)
864 InitDeviceDiscovery(OC_LOW_QOS);
866 else if(UNICAST_DISCOVERY == 0 && TEST_CASE == TEST_DISCOVER_PLATFORM_REQ)
868 InitPlatformDiscovery(OC_LOW_QOS);
872 InitDiscovery(OC_LOW_QOS);
875 // Break from loop with Ctrl+C
876 OC_LOG(INFO, TAG, "Entering occlient main loop...");
877 signal(SIGINT, handleSigInt);
881 if (OCProcess() != OC_STACK_OK)
883 OC_LOG(ERROR, TAG, "OCStack process error");
889 OC_LOG(INFO, TAG, "Exiting occlient main loop...");
891 if (OCStop() != OC_STACK_OK)
893 OC_LOG(ERROR, TAG, "OCStack stop error");
899 std::string getIPAddrTBServer(OCClientResponse * clientResponse)
905 if (!clientResponse->addr)
910 return std::string(clientResponse->devAddr.addr);
913 std::string getPortTBServer(OCClientResponse * clientResponse)
919 if (!clientResponse->addr)
923 std::ostringstream ss;
924 ss << clientResponse->devAddr.port;
928 std::string getConnectivityType (OCConnectivityType connType)
930 switch (connType & CT_MASK_ADAPTER)
941 case CT_ADAPTER_GATT_BTLE:
944 case CT_ADAPTER_RFCOMM_BTEDR:
948 return "Incorrect connectivity";
952 std::string getQueryStrForGetPut(OCClientResponse * clientResponse)
958 void parseClientResponse(OCClientResponse * clientResponse)
960 coapServerIP = getIPAddrTBServer(clientResponse);
961 coapServerPort = getPortTBServer(clientResponse);
962 coapServerResource = getQueryStrForGetPut(clientResponse);