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|2>");
83 OC_LOG(INFO, TAG, "-u <0|1> : Perform multicast/unicast discovery of resources");
84 OC_LOG(INFO, TAG, "-c 0 : Default IPv4 and IPv6 auto-selection");
85 OC_LOG(INFO, TAG, "-c 1 : IPv4 Connectivity Type");
86 OC_LOG(INFO, TAG, "-c 2 : IPv6 Connectivity Type (IPv6 not currently supported)");
87 OC_LOG(INFO, TAG, "-t 1 : Discover Resources");
88 OC_LOG(INFO, TAG, "-t 2 : Discover Resources and Initiate Nonconfirmable Get Request");
89 OC_LOG(INFO, TAG, "-t 3 : Discover Resources and Initiate Nonconfirmable Get Request"
90 " with query filter.");
91 OC_LOG(INFO, TAG, "-t 4 : Discover Resources and Initiate Nonconfirmable Put Requests");
92 OC_LOG(INFO, TAG, "-t 5 : Discover Resources and Initiate Nonconfirmable Post Requests");
93 OC_LOG(INFO, TAG, "-t 6 : Discover Resources and Initiate Nonconfirmable Delete Requests");
94 OC_LOG(INFO, TAG, "-t 7 : Discover Resources and Initiate Nonconfirmable Observe Requests");
95 OC_LOG(INFO, TAG, "-t 8 : Discover Resources and Initiate Nonconfirmable Get Request "\
96 "for a resource which is unavailable");
97 OC_LOG(INFO, TAG, "-t 9 : Discover Resources and Initiate Confirmable Get Request");
98 OC_LOG(INFO, TAG, "-t 10 : Discover Resources and Initiate Confirmable Post Request");
99 OC_LOG(INFO, TAG, "-t 11 : Discover Resources and Initiate Confirmable Delete Requests");
100 OC_LOG(INFO, TAG, "-t 12 : Discover Resources and Initiate Confirmable Observe Requests"\
101 " and cancel with Low QoS");
104 OC_LOG(INFO, TAG, "-t 13 : Discover Resources and Initiate Nonconfirmable presence");
105 OC_LOG(INFO, TAG, "-t 14 : Discover Resources and Initiate Nonconfirmable presence with "\
107 OC_LOG(INFO, TAG, "-t 15 : Discover Resources and Initiate Nonconfirmable presence with "\
109 OC_LOG(INFO, TAG, "-t 16 : Discover Resources and Initiate Nonconfirmable multicast presence.");
112 OC_LOG(INFO, TAG, "-t 17 : Discover Resources and Initiate Nonconfirmable Observe Requests "\
113 "then cancel immediately with High QOS");
114 OC_LOG(INFO, TAG, "-t 18 : Discover Resources and Initiate Nonconfirmable Get Request and "\
115 "add vendor specific header options");
116 OC_LOG(INFO, TAG, "-t 19 : Discover Platform");
117 OC_LOG(INFO, TAG, "-t 20 : Discover Devices");
120 OCStackResult InvokeOCDoResource(std::ostringstream &query,
122 OCQualityOfService qos,
123 OCClientResponseHandler cb,
124 OCHeaderOption * options,
128 OCCallbackData cbData;
132 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
135 ret = OCDoResource(&handle, method, query.str().c_str(), 0,
136 (method == OC_REST_PUT) ? putPayload.c_str() : NULL,
137 (OC_CONNTYPE), qos, &cbData, options, numOptions);
139 if (ret != OC_STACK_OK)
141 OC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method);
143 else if (method == OC_REST_OBSERVE || method == OC_REST_OBSERVE_ALL)
145 gObserveDoHandle = handle;
148 else if (method == OC_REST_PRESENCE)
150 gPresenceHandle = handle;
157 OCStackApplicationResult putReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
159 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
161 OC_LOG(INFO, TAG, "Callback Context for PUT recvd successfully");
166 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
167 OC_LOG_V(INFO, TAG, "JSON = %s =============> Put Response",
168 clientResponse->resJSONPayload);
172 OC_LOG_V(INFO, TAG, "putReqCB received Null clientResponse");
174 return OC_STACK_DELETE_TRANSACTION;
177 OCStackApplicationResult postReqCB(void *ctx, OCDoHandle handle, OCClientResponse *clientResponse)
179 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
181 OC_LOG(INFO, TAG, "Callback Context for POST recvd successfully");
186 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
187 OC_LOG_V(INFO, TAG, "JSON = %s =============> Post Response",
188 clientResponse->resJSONPayload);
192 OC_LOG_V(INFO, TAG, "postReqCB received Null clientResponse");
194 return OC_STACK_DELETE_TRANSACTION;
197 OCStackApplicationResult deleteReqCB(void *ctx,
198 OCDoHandle handle, OCClientResponse *clientResponse)
200 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
202 OC_LOG(INFO, TAG, "Callback Context for DELETE recvd successfully");
207 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
208 OC_LOG_V(INFO, TAG, "JSON = %s =============> Delete Response",
209 clientResponse->resJSONPayload);
213 OC_LOG_V(INFO, TAG, "deleteReqCB received Null clientResponse");
215 return OC_STACK_DELETE_TRANSACTION;
218 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
220 if(clientResponse == NULL)
222 OC_LOG(INFO, TAG, "getReqCB received NULL clientResponse");
223 return OC_STACK_DELETE_TRANSACTION;
226 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
228 OC_LOG(INFO, TAG, "Callback Context for GET query recvd successfully");
231 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
232 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
233 OC_LOG_V(INFO, TAG, "JSON = %s =============> Get Response", clientResponse->resJSONPayload);
235 if(clientResponse->numRcvdVendorSpecificHeaderOptions > 0)
237 OC_LOG (INFO, TAG, "Received vendor specific options");
239 OCHeaderOption * rcvdOptions = clientResponse->rcvdVendorSpecificHeaderOptions;
240 for( i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
242 if(((OCHeaderOption)rcvdOptions[i]).protocolID == OC_COAP_ID)
244 OC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with",
245 ((OCHeaderOption)rcvdOptions[i]).optionID );
247 OC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData,
248 MAX_HEADER_OPTION_DATA_LENGTH);
252 return OC_STACK_DELETE_TRANSACTION;
255 OCStackApplicationResult obsReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
257 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
259 OC_LOG(INFO, TAG, "Callback Context for OBS query recvd successfully");
264 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
265 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
266 OC_LOG_V(INFO, TAG, "Callback Context for OBSERVE notification recvd successfully %d",
267 gNumObserveNotifies);
268 OC_LOG_V(INFO, TAG, "JSON = %s =============> Obs Response",
269 clientResponse->resJSONPayload);
270 gNumObserveNotifies++;
271 if (gNumObserveNotifies == 15) //large number to test observing in DELETE case.
273 if(TEST_CASE == TEST_OBS_REQ_NON || TEST_CASE == TEST_OBS_REQ_CON)
275 if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
277 OC_LOG(ERROR, TAG, "Observe cancel error");
279 return OC_STACK_DELETE_TRANSACTION;
281 else if(TEST_CASE == TEST_OBS_REQ_NON_CANCEL_IMM)
283 if (OCCancel (gObserveDoHandle, OC_HIGH_QOS, NULL, 0) != OC_STACK_OK)
285 OC_LOG(ERROR, TAG, "Observe cancel error");
289 if(clientResponse->sequenceNumber == OC_OBSERVE_REGISTER)
291 OC_LOG(INFO, TAG, "This also serves as a registration confirmation");
293 else if(clientResponse->sequenceNumber == OC_OBSERVE_DEREGISTER)
295 OC_LOG(INFO, TAG, "This also serves as a deregistration confirmation");
296 return OC_STACK_DELETE_TRANSACTION;
298 else if(clientResponse->sequenceNumber == OC_OBSERVE_NO_OPTION)
300 OC_LOG(INFO, TAG, "This also tells you that registration/deregistration failed");
301 return OC_STACK_DELETE_TRANSACTION;
306 OC_LOG_V(INFO, TAG, "obsReqCB received Null clientResponse");
308 return OC_STACK_KEEP_TRANSACTION;
311 OCStackApplicationResult presenceCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
313 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
315 OC_LOG(INFO, TAG, "Callback Context for Presence recvd successfully");
320 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
321 OC_LOG_V(INFO, TAG, "NONCE NUMBER: %u", clientResponse->sequenceNumber);
322 OC_LOG_V(INFO, TAG, "Callback Context for Presence notification recvd successfully %d",
323 gNumPresenceNotifies);
324 OC_LOG_V(INFO, TAG, "JSON = %s =============> Presence Response",
325 clientResponse->resJSONPayload);
326 gNumPresenceNotifies++;
327 if (gNumPresenceNotifies == 20)
329 if (OCCancel(gPresenceHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
331 OC_LOG(ERROR, TAG, "Presence cancel error");
333 return OC_STACK_DELETE_TRANSACTION;
338 OC_LOG_V(INFO, TAG, "presenceCB received Null clientResponse");
340 return OC_STACK_KEEP_TRANSACTION;
344 // This is a function called back when a device is discovered
345 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
346 OCClientResponse * clientResponse)
348 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
350 OC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully");
355 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
357 std::string connectionType = getConnectivityType (clientResponse->connType);
358 OC_LOG_V(INFO, TAG, "Discovered on %s", connectionType.c_str());
360 "Device =============> Discovered %s @ %s:%d",
361 clientResponse->resJSONPayload, clientResponse->devAddr.addr, clientResponse->devAddr.port);
363 parseClientResponse(clientResponse);
367 case TEST_GET_REQ_NON:
368 InitGetRequest(OC_LOW_QOS, 0, 0);
370 case TEST_GET_REQ_NON_WITH_FILTERS:
371 InitGetRequest(OC_LOW_QOS, 0, 1);
373 case TEST_PUT_REQ_NON:
374 InitPutRequest(OC_LOW_QOS);
376 case TEST_POST_REQ_NON:
377 InitPostRequest(OC_LOW_QOS);
379 case TEST_DELETE_REQ_NON:
380 InitDeleteRequest(OC_LOW_QOS);
382 case TEST_OBS_REQ_NON:
383 case TEST_OBS_REQ_NON_CANCEL_IMM:
384 InitObserveRequest(OC_LOW_QOS);
386 case TEST_GET_UNAVAILABLE_RES_REQ_NON:
387 InitGetRequestToUnavailableResource(OC_LOW_QOS);
389 case TEST_GET_REQ_CON:
390 InitGetRequest(OC_HIGH_QOS, 0, 0);
392 case TEST_POST_REQ_CON:
393 InitPostRequest(OC_HIGH_QOS);
395 case TEST_DELETE_REQ_CON:
396 InitDeleteRequest(OC_HIGH_QOS);
398 case TEST_OBS_REQ_CON:
399 InitObserveRequest(OC_HIGH_QOS);
402 case TEST_OBS_PRESENCE:
403 case TEST_OBS_PRESENCE_WITH_FILTER:
404 case TEST_OBS_PRESENCE_WITH_FILTERS:
405 case TEST_OBS_MULTICAST_PRESENCE:
409 case TEST_GET_REQ_NON_WITH_VENDOR_HEADER_OPTIONS:
410 InitGetRequest(OC_LOW_QOS, 1, 0);
412 case TEST_DISCOVER_PLATFORM_REQ:
413 InitPlatformDiscovery(OC_LOW_QOS);
415 case TEST_DISCOVER_DEV_REQ:
416 InitDeviceDiscovery(OC_LOW_QOS);
425 OC_LOG_V(INFO, TAG, "discoveryReqCB received Null clientResponse");
427 return OC_STACK_KEEP_TRANSACTION;
430 OCStackApplicationResult PlatformDiscoveryReqCB (void* ctx, OCDoHandle handle,
431 OCClientResponse * clientResponse)
433 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
435 OC_LOG(INFO, TAG, "Callback Context for Platform DISCOVER query recvd successfully");
440 //OC_LOG truncates the response as it is too long.
441 fprintf(stderr, "Discovery response: \n %s\n", clientResponse->resJSONPayload);
446 OC_LOG_V(INFO, TAG, "PlatformDiscoveryReqCB received Null clientResponse");
449 return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
452 OCStackApplicationResult DeviceDiscoveryReqCB (void* ctx, OCDoHandle handle,
453 OCClientResponse * clientResponse)
455 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
457 OC_LOG(INFO, TAG, "Callback Context for Device DISCOVER query recvd successfully");
462 //OC_LOG truncates the response as it is too long.
463 fprintf(stderr, "Discovery response: \n %s\n", clientResponse->resJSONPayload);
468 OC_LOG_V(INFO, TAG, "PlatformDiscoveryReqCB received Null clientResponse");
471 return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
477 OCStackResult result = OC_STACK_OK;
478 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
479 std::ostringstream query;
480 std::ostringstream querySuffix;
481 query << "coap://" << coapServerIP << ":" << coapServerPort << OC_PRESENCE_URI;
482 if(TEST_CASE == TEST_OBS_PRESENCE)
484 result = InvokeOCDoResource(query, OC_REST_PRESENCE, OC_LOW_QOS,
485 presenceCB, NULL, 0);
487 if(TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTER || TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTERS)
490 querySuffix << query.str() << "?rt=core.led";
491 result = InvokeOCDoResource(querySuffix, OC_REST_PRESENCE, OC_LOW_QOS,
492 presenceCB, NULL, 0);
494 if(TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTERS)
496 if(result == OC_STACK_OK)
499 querySuffix << query.str() << "?rt=core.fan";
500 result = InvokeOCDoResource(querySuffix, OC_REST_PRESENCE, OC_LOW_QOS,
501 presenceCB, NULL, 0);
504 if(TEST_CASE == TEST_OBS_MULTICAST_PRESENCE)
506 if(result == OC_STACK_OK)
508 std::ostringstream multicastPresenceQuery;
509 multicastPresenceQuery.str("");
510 multicastPresenceQuery << "coap://" << OC_MULTICAST_PREFIX << OC_PRESENCE_URI;
511 result = InvokeOCDoResource(multicastPresenceQuery, OC_REST_PRESENCE, OC_LOW_QOS,
512 presenceCB, NULL, 0);
519 int InitGetRequestToUnavailableResource(OCQualityOfService qos)
521 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
522 std::ostringstream query;
523 query << "coap://" << coapServerIP << ":" << coapServerPort << "/SomeUnknownResource";
524 return (InvokeOCDoResource(query, OC_REST_GET, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
528 int InitObserveRequest(OCQualityOfService qos)
530 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
531 std::ostringstream query;
532 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
533 return (InvokeOCDoResource(query,
534 OC_REST_OBSERVE, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, obsReqCB, NULL, 0));
537 int InitPutRequest(OCQualityOfService qos)
539 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
540 std::ostringstream query;
541 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
542 return (InvokeOCDoResource(query, OC_REST_PUT, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
546 int InitPostRequest(OCQualityOfService qos)
548 OCStackResult result;
549 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
550 std::ostringstream query;
551 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
553 // First POST operation (to create an Light instance)
554 result = InvokeOCDoResource(query, OC_REST_POST,
555 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
557 if (OC_STACK_OK != result)
559 // Error can happen if for example, network connectivity is down
560 OC_LOG(INFO, TAG, "First POST call did not succeed");
563 // Second POST operation (to create an Light instance)
564 result = InvokeOCDoResource(query, OC_REST_POST,
565 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
567 if (OC_STACK_OK != result)
569 OC_LOG(INFO, TAG, "Second POST call did not succeed");
572 // This POST operation will update the original resourced /a/light
573 return (InvokeOCDoResource(query, OC_REST_POST,
574 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
575 postReqCB, NULL, 0));
578 void* RequestDeleteDeathResourceTask(void* myqos)
580 sleep (30);//long enough to give the server time to finish deleting the resource.
581 std::ostringstream query;
582 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
584 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
586 // Second DELETE operation to delete the resource that might have been removed already.
587 OCQualityOfService qos;
597 OCStackResult result = InvokeOCDoResource(query, OC_REST_DELETE,
599 deleteReqCB, NULL, 0);
601 if (OC_STACK_OK != result)
603 OC_LOG(INFO, TAG, "Second DELETE call did not succeed");
609 int InitDeleteRequest(OCQualityOfService qos)
611 OCStackResult result;
612 std::ostringstream query;
613 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
615 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
617 // First DELETE operation
618 result = InvokeOCDoResource(query, OC_REST_DELETE,
620 deleteReqCB, NULL, 0);
621 if (OC_STACK_OK != result)
623 // Error can happen if for example, network connectivity is down
624 OC_LOG(INFO, TAG, "First DELETE call did not succeed");
628 //Create a thread to delete this resource again
630 pthread_create (&threadId, NULL, RequestDeleteDeathResourceTask, (void*)qos);
633 OC_LOG_V(INFO, TAG, "\n\nExit %s", __func__);
637 int InitGetRequest(OCQualityOfService qos, uint8_t withVendorSpecificHeaderOptions, bool getWithQuery)
640 OCHeaderOption options[MAX_HEADER_OPTIONS];
642 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
643 std::ostringstream query;
644 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
646 // ocserver is written to only process "power<X" query.
649 OC_LOG(INFO, TAG, "Using query power<30");
650 query << "?power<30";
653 if (withVendorSpecificHeaderOptions)
655 uint8_t option0[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
656 uint8_t option1[] = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
657 memset(options, 0, sizeof(OCHeaderOption) * MAX_HEADER_OPTIONS);
658 options[0].protocolID = OC_COAP_ID;
659 options[0].optionID = 2048;
660 memcpy(options[0].optionData, option0, sizeof(option0));
661 options[0].optionLength = 10;
662 options[1].protocolID = OC_COAP_ID;
663 options[1].optionID = 3000;
664 memcpy(options[1].optionData, option1, sizeof(option1));
665 options[1].optionLength = 10;
667 if (withVendorSpecificHeaderOptions)
669 return (InvokeOCDoResource(query, OC_REST_GET,
670 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, options, 2));
674 return (InvokeOCDoResource(query, OC_REST_GET,
675 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, NULL, 0));
679 int InitPlatformDiscovery(OCQualityOfService qos)
681 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
684 OCCallbackData cbData;
685 char szQueryUri[64] = { 0 };
687 cbData.cb = PlatformDiscoveryReqCB;
688 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
691 if(UNICAST_DISCOVERY)
693 snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_PLATFORM_DISCOVERY_QUERY, ipv4addr);
697 strncpy(szQueryUri, MULTICAST_PLATFORM_DISCOVERY_QUERY, sizeof(szQueryUri) -1 );
699 szQueryUri[sizeof(szQueryUri) -1] = '\0';
701 if(UNICAST_DISCOVERY)
703 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, OC_CONNTYPE,
704 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
709 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, 0, 0, OC_CONNTYPE,
710 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
713 if (ret != OC_STACK_OK)
715 OC_LOG(ERROR, TAG, "OCStack device error");
721 int InitDeviceDiscovery(OCQualityOfService qos)
723 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
726 OCCallbackData cbData;
727 char szQueryUri[64] = { 0 };
729 cbData.cb = DeviceDiscoveryReqCB;
730 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
733 if(UNICAST_DISCOVERY)
735 snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_DEVICE_DISCOVERY_QUERY, ipv4addr);
739 strncpy(szQueryUri, MULTICAST_DEVICE_DISCOVERY_QUERY, sizeof(szQueryUri) -1 );
741 szQueryUri[sizeof(szQueryUri) -1] = '\0';
743 if(UNICAST_DISCOVERY)
745 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, OC_CONNTYPE,
746 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
750 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, 0, 0, OC_CONNTYPE,
751 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
754 if (ret != OC_STACK_OK)
756 OC_LOG(ERROR, TAG, "OCStack device error");
762 int InitDiscovery(OCQualityOfService qos)
765 OCCallbackData cbData;
766 /* Start a discovery query*/
767 char szQueryUri[64] = { 0 };
769 if (UNICAST_DISCOVERY)
771 snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_RESOURCE_DISCOVERY_QUERY, ipv4addr);
775 strcpy(szQueryUri, MULTICAST_RESOURCE_DISCOVERY_QUERY);
778 cbData.cb = discoveryReqCB;
779 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
781 if(UNICAST_DISCOVERY)
783 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, OC_CONNTYPE,
784 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
788 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, 0, 0, OC_CONNTYPE,
789 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
791 if (ret != OC_STACK_OK)
793 OC_LOG(ERROR, TAG, "OCStack resource error");
798 int main(int argc, char* argv[])
802 while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
807 UNICAST_DISCOVERY = atoi(optarg);
810 TEST_CASE = atoi(optarg);
813 CONNECTIVITY = atoi(optarg);
821 if ((UNICAST_DISCOVERY != 0 && UNICAST_DISCOVERY != 1) ||
822 (TEST_CASE < TEST_DISCOVER_REQ || TEST_CASE >= MAX_TESTS) ||
823 (CONNECTIVITY < CT_ADAPTER_DEFAULT || CONNECTIVITY >= MAX_CT))
829 /* Initialize OCStack*/
830 if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK)
832 OC_LOG(ERROR, TAG, "OCStack init error");
836 if(CONNECTIVITY == CT_ADAPTER_DEFAULT || CONNECTIVITY == CT_IPV4)
838 OC_CONNTYPE = CT_ADAPTER_IP;
840 else if(CONNECTIVITY == CT_IPV6)
842 //TODO: Remove when IPv6 is available.
843 OC_LOG(ERROR, TAG, "IPv6 is currently not supported !!!!");
849 OC_LOG(INFO, TAG, "Default Connectivity type selected...");
853 if (UNICAST_DISCOVERY)
855 OC_LOG(INFO, TAG, "Enter IP address with port number of the Server hosting resource");
856 OC_LOG(INFO, TAG, "as follows - eg: 192.168.0.15:45454 (IP:Port) \n");
858 if (fgets(ipv4addr, IPV4_ADDR_SIZE, stdin))
860 //Strip newline char from ipv4addr
861 StripNewLineChar(ipv4addr);
865 OC_LOG(ERROR, TAG, "!! Bad input for IPV4 address. !!");
866 return OC_STACK_INVALID_PARAM;
870 if(UNICAST_DISCOVERY == 0 && TEST_CASE == TEST_DISCOVER_DEV_REQ)
872 InitDeviceDiscovery(OC_LOW_QOS);
874 else if(UNICAST_DISCOVERY == 0 && TEST_CASE == TEST_DISCOVER_PLATFORM_REQ)
876 InitPlatformDiscovery(OC_LOW_QOS);
880 InitDiscovery(OC_LOW_QOS);
883 // Break from loop with Ctrl+C
884 OC_LOG(INFO, TAG, "Entering occlient main loop...");
885 signal(SIGINT, handleSigInt);
889 if (OCProcess() != OC_STACK_OK)
891 OC_LOG(ERROR, TAG, "OCStack process error");
897 OC_LOG(INFO, TAG, "Exiting occlient main loop...");
899 if (OCStop() != OC_STACK_OK)
901 OC_LOG(ERROR, TAG, "OCStack stop error");
907 std::string getIPAddrTBServer(OCClientResponse * clientResponse)
913 if (!clientResponse->addr)
918 return std::string(clientResponse->devAddr.addr);
921 std::string getPortTBServer(OCClientResponse * clientResponse)
927 if (!clientResponse->addr)
931 std::ostringstream ss;
932 ss << clientResponse->devAddr.port;
936 std::string getConnectivityType (OCConnectivityType connType)
938 switch (connType & CT_MASK_ADAPTER)
949 case CT_ADAPTER_GATT_BTLE:
952 case CT_ADAPTER_RFCOMM_BTEDR:
956 return "Incorrect connectivity";
960 std::string getQueryStrForGetPut(OCClientResponse * clientResponse)
966 void parseClientResponse(OCClientResponse * clientResponse)
968 coapServerIP = getIPAddrTBServer(clientResponse);
969 coapServerPort = getPortTBServer(clientResponse);
970 coapServerResource = getQueryStrForGetPut(clientResponse);