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 static int UNICAST_DISCOVERY = 0;
33 static int TEST_CASE = 0;
35 static const char * UNICAST_DEVICE_DISCOVERY_QUERY = "coap://%s:6298/oic/d";
36 static const char * MULTICAST_DEVICE_DISCOVERY_QUERY = "/oic/d";
37 static const char * UNICAST_PLATFORM_DISCOVERY_QUERY = "coap://%s:6298/oic/p";
38 static const char * MULTICAST_PLATFORM_DISCOVERY_QUERY = "/oic/p";
40 static const char * UNICAST_RESOURCE_DISCOVERY_QUERY = "coap://%s:6298/oic/res";
41 static const char * MULTICAST_RESOURCE_DISCOVERY_QUERY = "/oic/res";
42 //The following variable determines the interface protocol (IPv4, IPv6, etc)
43 //to be used for sending unicast messages. Default set to IPv4.
44 static OCConnectivityType OC_CONNTYPE = CT_ADAPTER_IP;
45 static std::string putPayload = "{\"oic\":[{\"rep\":{\"power\":15,\"state\":true}}]}";
46 static std::string coapServerIP = "255.255.255.255";
47 static std::string coapServerPort = "5683";
48 static std::string coapServerResource = "/a/light";
49 static const int IPV4_ADDR_SIZE = 16;
50 //Use ipv4addr for both InitDiscovery and InitPlatformOrDeviceDiscovery
51 char ipv4addr[IPV4_ADDR_SIZE];
52 void StripNewLineChar(char* str);
54 // The handle for the observe registration
55 OCDoHandle gObserveDoHandle;
57 // The handle for observe registration
58 OCDoHandle gPresenceHandle;
60 // After this crosses a threshold client deregisters for further notifications
61 int gNumObserveNotifies = 0;
64 int gNumPresenceNotifies = 0;
68 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
69 void handleSigInt(int signum)
77 static void PrintUsage()
79 OC_LOG(INFO, TAG, "Usage : occlient -u <0|1> -t <1..17> -c <0|1>");
80 OC_LOG(INFO, TAG, "-u <0|1> : Perform multicast/unicast discovery of resources");
81 OC_LOG(INFO, TAG, "-c <0|1> : IPv4/IPv6 (IPv6 not currently supported)");
82 OC_LOG(INFO, TAG, "-t 1 : Discover Resources");
83 OC_LOG(INFO, TAG, "-t 2 : Discover Resources and Initiate Nonconfirmable Get Request");
84 OC_LOG(INFO, TAG, "-t 3 : Discover Resources and Initiate Nonconfirmable Get Request"
85 " with query filter.");
86 OC_LOG(INFO, TAG, "-t 4 : Discover Resources and Initiate Nonconfirmable Put Requests");
87 OC_LOG(INFO, TAG, "-t 5 : Discover Resources and Initiate Nonconfirmable Post Requests");
88 OC_LOG(INFO, TAG, "-t 6 : Discover Resources and Initiate Nonconfirmable Delete Requests");
89 OC_LOG(INFO, TAG, "-t 7 : Discover Resources and Initiate Nonconfirmable Observe Requests");
90 OC_LOG(INFO, TAG, "-t 8 : Discover Resources and Initiate Nonconfirmable Get Request "\
91 "for a resource which is unavailable");
92 OC_LOG(INFO, TAG, "-t 9 : Discover Resources and Initiate Confirmable Get Request");
93 OC_LOG(INFO, TAG, "-t 10 : Discover Resources and Initiate Confirmable Post Request");
94 OC_LOG(INFO, TAG, "-t 11 : Discover Resources and Initiate Confirmable Delete Requests");
95 OC_LOG(INFO, TAG, "-t 12 : Discover Resources and Initiate Confirmable Observe Requests"\
96 " and cancel with Low QoS");
99 OC_LOG(INFO, TAG, "-t 13 : Discover Resources and Initiate Nonconfirmable presence");
100 OC_LOG(INFO, TAG, "-t 14 : Discover Resources and Initiate Nonconfirmable presence with "\
102 OC_LOG(INFO, TAG, "-t 15 : Discover Resources and Initiate Nonconfirmable presence with "\
104 OC_LOG(INFO, TAG, "-t 16 : Discover Resources and Initiate Nonconfirmable multicast presence.");
107 OC_LOG(INFO, TAG, "-t 17 : Discover Resources and Initiate Nonconfirmable Observe Requests "\
108 "then cancel immediately with High QOS");
109 OC_LOG(INFO, TAG, "-t 18 : Discover Resources and Initiate Nonconfirmable Get Request and "\
110 "add vendor specific header options");
111 OC_LOG(INFO, TAG, "-t 19 : Discover Platform");
112 OC_LOG(INFO, TAG, "-t 20 : Discover Devices");
115 OCStackResult InvokeOCDoResource(std::ostringstream &query,
117 OCQualityOfService qos,
118 OCClientResponseHandler cb,
119 OCHeaderOption * options,
123 OCCallbackData cbData;
127 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
130 ret = OCDoResource(&handle, method, query.str().c_str(), 0,
131 (method == OC_REST_PUT) ? putPayload.c_str() : NULL,
132 (OC_CONNTYPE), qos, &cbData, options, numOptions);
134 if (ret != OC_STACK_OK)
136 OC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method);
138 else if (method == OC_REST_OBSERVE || method == OC_REST_OBSERVE_ALL)
140 gObserveDoHandle = handle;
143 else if (method == OC_REST_PRESENCE)
145 gPresenceHandle = handle;
152 OCStackApplicationResult putReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
154 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
156 OC_LOG(INFO, TAG, "Callback Context for PUT recvd successfully");
161 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
162 OC_LOG_V(INFO, TAG, "JSON = %s =============> Put Response",
163 clientResponse->resJSONPayload);
167 OC_LOG_V(INFO, TAG, "putReqCB received Null clientResponse");
169 return OC_STACK_DELETE_TRANSACTION;
172 OCStackApplicationResult postReqCB(void *ctx, OCDoHandle handle, OCClientResponse *clientResponse)
174 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
176 OC_LOG(INFO, TAG, "Callback Context for POST recvd successfully");
181 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
182 OC_LOG_V(INFO, TAG, "JSON = %s =============> Post Response",
183 clientResponse->resJSONPayload);
187 OC_LOG_V(INFO, TAG, "postReqCB received Null clientResponse");
189 return OC_STACK_DELETE_TRANSACTION;
192 OCStackApplicationResult deleteReqCB(void *ctx,
193 OCDoHandle handle, OCClientResponse *clientResponse)
195 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
197 OC_LOG(INFO, TAG, "Callback Context for DELETE recvd successfully");
202 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
203 OC_LOG_V(INFO, TAG, "JSON = %s =============> Delete Response",
204 clientResponse->resJSONPayload);
208 OC_LOG_V(INFO, TAG, "deleteReqCB received Null clientResponse");
210 return OC_STACK_DELETE_TRANSACTION;
213 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
215 if(clientResponse == NULL)
217 OC_LOG(INFO, TAG, "getReqCB received NULL clientResponse");
218 return OC_STACK_DELETE_TRANSACTION;
221 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
223 OC_LOG(INFO, TAG, "Callback Context for GET query recvd successfully");
226 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
227 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
228 OC_LOG_V(INFO, TAG, "JSON = %s =============> Get Response", clientResponse->resJSONPayload);
230 if(clientResponse->rcvdVendorSpecificHeaderOptions &&
231 clientResponse->numRcvdVendorSpecificHeaderOptions)
233 OC_LOG (INFO, TAG, "Received vendor specific options");
235 OCHeaderOption * rcvdOptions = clientResponse->rcvdVendorSpecificHeaderOptions;
236 for( i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
238 if(((OCHeaderOption)rcvdOptions[i]).protocolID == OC_COAP_ID)
240 OC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with",
241 ((OCHeaderOption)rcvdOptions[i]).optionID );
243 OC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData,
244 MAX_HEADER_OPTION_DATA_LENGTH);
248 return OC_STACK_DELETE_TRANSACTION;
251 OCStackApplicationResult obsReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
253 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
255 OC_LOG(INFO, TAG, "Callback Context for OBS query recvd successfully");
260 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
261 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
262 OC_LOG_V(INFO, TAG, "Callback Context for OBSERVE notification recvd successfully %d",
263 gNumObserveNotifies);
264 OC_LOG_V(INFO, TAG, "JSON = %s =============> Obs Response",
265 clientResponse->resJSONPayload);
266 gNumObserveNotifies++;
267 if (gNumObserveNotifies == 15) //large number to test observing in DELETE case.
269 if(TEST_CASE == TEST_OBS_REQ_NON || TEST_CASE == TEST_OBS_REQ_CON)
271 if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
273 OC_LOG(ERROR, TAG, "Observe cancel error");
275 return OC_STACK_DELETE_TRANSACTION;
277 else if(TEST_CASE == TEST_OBS_REQ_NON_CANCEL_IMM)
279 if (OCCancel (gObserveDoHandle, OC_HIGH_QOS, NULL, 0) != OC_STACK_OK)
281 OC_LOG(ERROR, TAG, "Observe cancel error");
285 if(clientResponse->sequenceNumber == OC_OBSERVE_REGISTER)
287 OC_LOG(INFO, TAG, "This also serves as a registration confirmation");
289 else if(clientResponse->sequenceNumber == OC_OBSERVE_DEREGISTER)
291 OC_LOG(INFO, TAG, "This also serves as a deregistration confirmation");
292 return OC_STACK_DELETE_TRANSACTION;
294 else if(clientResponse->sequenceNumber == OC_OBSERVE_NO_OPTION)
296 OC_LOG(INFO, TAG, "This also tells you that registration/deregistration failed");
297 return OC_STACK_DELETE_TRANSACTION;
302 OC_LOG_V(INFO, TAG, "obsReqCB received Null clientResponse");
304 return OC_STACK_KEEP_TRANSACTION;
307 OCStackApplicationResult presenceCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
309 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
311 OC_LOG(INFO, TAG, "Callback Context for Presence recvd successfully");
316 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
317 OC_LOG_V(INFO, TAG, "NONCE NUMBER: %u", clientResponse->sequenceNumber);
318 OC_LOG_V(INFO, TAG, "Callback Context for Presence notification recvd successfully %d",
319 gNumPresenceNotifies);
320 OC_LOG_V(INFO, TAG, "JSON = %s =============> Presence Response",
321 clientResponse->resJSONPayload);
322 gNumPresenceNotifies++;
323 if (gNumPresenceNotifies == 20)
325 if (OCCancel(gPresenceHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
327 OC_LOG(ERROR, TAG, "Presence cancel error");
329 return OC_STACK_DELETE_TRANSACTION;
334 OC_LOG_V(INFO, TAG, "presenceCB received Null clientResponse");
336 return OC_STACK_KEEP_TRANSACTION;
340 // This is a function called back when a device is discovered
341 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
342 OCClientResponse * clientResponse)
344 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
346 OC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully");
351 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
353 std::string connectionType = getConnectivityType (clientResponse->connType);
354 OC_LOG_V(INFO, TAG, "Discovered on %s", connectionType.c_str());
356 "Device =============> Discovered %s @ %s:%d",
357 clientResponse->resJSONPayload, clientResponse->devAddr.addr, clientResponse->devAddr.port);
359 parseClientResponse(clientResponse);
363 case TEST_GET_REQ_NON:
364 InitGetRequest(OC_LOW_QOS, 0, 0);
366 case TEST_GET_REQ_NON_WITH_FILTERS:
367 InitGetRequest(OC_LOW_QOS, 0, 1);
369 case TEST_PUT_REQ_NON:
370 InitPutRequest(OC_LOW_QOS);
372 case TEST_POST_REQ_NON:
373 InitPostRequest(OC_LOW_QOS);
375 case TEST_DELETE_REQ_NON:
376 InitDeleteRequest(OC_LOW_QOS);
378 case TEST_OBS_REQ_NON:
379 case TEST_OBS_REQ_NON_CANCEL_IMM:
380 InitObserveRequest(OC_LOW_QOS);
382 case TEST_GET_UNAVAILABLE_RES_REQ_NON:
383 InitGetRequestToUnavailableResource(OC_LOW_QOS);
385 case TEST_GET_REQ_CON:
386 InitGetRequest(OC_HIGH_QOS, 0, 0);
388 case TEST_POST_REQ_CON:
389 InitPostRequest(OC_HIGH_QOS);
391 case TEST_DELETE_REQ_CON:
392 InitDeleteRequest(OC_HIGH_QOS);
394 case TEST_OBS_REQ_CON:
395 InitObserveRequest(OC_HIGH_QOS);
398 case TEST_OBS_PRESENCE:
399 case TEST_OBS_PRESENCE_WITH_FILTER:
400 case TEST_OBS_PRESENCE_WITH_FILTERS:
401 case TEST_OBS_MULTICAST_PRESENCE:
405 case TEST_GET_REQ_NON_WITH_VENDOR_HEADER_OPTIONS:
406 InitGetRequest(OC_LOW_QOS, 1, 0);
408 case TEST_DISCOVER_PLATFORM_REQ:
409 InitPlatformDiscovery(OC_LOW_QOS);
411 case TEST_DISCOVER_DEV_REQ:
412 InitDeviceDiscovery(OC_LOW_QOS);
421 OC_LOG_V(INFO, TAG, "discoveryReqCB received Null clientResponse");
423 return OC_STACK_KEEP_TRANSACTION;
426 OCStackApplicationResult PlatformDiscoveryReqCB (void* ctx, OCDoHandle handle,
427 OCClientResponse * clientResponse)
429 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
431 OC_LOG(INFO, TAG, "Callback Context for Platform DISCOVER query recvd successfully");
436 //OC_LOG truncates the response as it is too long.
437 fprintf(stderr, "Discovery response: \n %s\n", clientResponse->resJSONPayload);
442 OC_LOG_V(INFO, TAG, "PlatformDiscoveryReqCB received Null clientResponse");
445 return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
448 OCStackApplicationResult DeviceDiscoveryReqCB (void* ctx, OCDoHandle handle,
449 OCClientResponse * clientResponse)
451 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
453 OC_LOG(INFO, TAG, "Callback Context for Device DISCOVER query recvd successfully");
458 //OC_LOG truncates the response as it is too long.
459 fprintf(stderr, "Discovery response: \n %s\n", clientResponse->resJSONPayload);
464 OC_LOG_V(INFO, TAG, "PlatformDiscoveryReqCB received Null clientResponse");
467 return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
473 OCStackResult result = OC_STACK_OK;
474 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
475 std::ostringstream query;
476 std::ostringstream querySuffix;
477 query << "coap://" << coapServerIP << ":" << coapServerPort << OC_PRESENCE_URI;
478 if(TEST_CASE == TEST_OBS_PRESENCE)
480 result = InvokeOCDoResource(query, OC_REST_PRESENCE, OC_LOW_QOS,
481 presenceCB, NULL, 0);
483 if(TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTER || TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTERS)
486 querySuffix << query.str() << "?rt=core.led";
487 result = InvokeOCDoResource(querySuffix, OC_REST_PRESENCE, OC_LOW_QOS,
488 presenceCB, NULL, 0);
490 if(TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTERS)
492 if(result == OC_STACK_OK)
495 querySuffix << query.str() << "?rt=core.fan";
496 result = InvokeOCDoResource(querySuffix, OC_REST_PRESENCE, OC_LOW_QOS,
497 presenceCB, NULL, 0);
500 if(TEST_CASE == TEST_OBS_MULTICAST_PRESENCE)
502 if(result == OC_STACK_OK)
504 std::ostringstream multicastPresenceQuery;
505 multicastPresenceQuery.str("");
506 multicastPresenceQuery << "coap://" << OC_MULTICAST_PREFIX << OC_PRESENCE_URI;
507 result = InvokeOCDoResource(multicastPresenceQuery, OC_REST_PRESENCE, OC_LOW_QOS,
508 presenceCB, NULL, 0);
515 int InitGetRequestToUnavailableResource(OCQualityOfService qos)
517 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
518 std::ostringstream query;
519 query << "coap://" << coapServerIP << ":" << coapServerPort << "/SomeUnknownResource";
520 return (InvokeOCDoResource(query, OC_REST_GET, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
524 int InitObserveRequest(OCQualityOfService qos)
526 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
527 std::ostringstream query;
528 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
529 return (InvokeOCDoResource(query,
530 OC_REST_OBSERVE, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, obsReqCB, NULL, 0));
533 int InitPutRequest(OCQualityOfService qos)
535 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
536 std::ostringstream query;
537 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
538 return (InvokeOCDoResource(query, OC_REST_PUT, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
542 int InitPostRequest(OCQualityOfService qos)
544 OCStackResult result;
545 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
546 std::ostringstream query;
547 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
549 // First POST operation (to create an Light instance)
550 result = InvokeOCDoResource(query, OC_REST_POST,
551 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
553 if (OC_STACK_OK != result)
555 // Error can happen if for example, network connectivity is down
556 OC_LOG(INFO, TAG, "First POST call did not succeed");
559 // Second POST operation (to create an Light instance)
560 result = InvokeOCDoResource(query, OC_REST_POST,
561 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
563 if (OC_STACK_OK != result)
565 OC_LOG(INFO, TAG, "Second POST call did not succeed");
568 // This POST operation will update the original resourced /a/light
569 return (InvokeOCDoResource(query, OC_REST_POST,
570 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
571 postReqCB, NULL, 0));
574 void* RequestDeleteDeathResourceTask(void* myqos)
576 sleep (30);//long enough to give the server time to finish deleting the resource.
577 std::ostringstream query;
578 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
580 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
582 // Second DELETE operation to delete the resource that might have been removed already.
583 OCQualityOfService qos;
593 OCStackResult result = InvokeOCDoResource(query, OC_REST_DELETE,
595 deleteReqCB, NULL, 0);
597 if (OC_STACK_OK != result)
599 OC_LOG(INFO, TAG, "Second DELETE call did not succeed");
605 int InitDeleteRequest(OCQualityOfService qos)
607 OCStackResult result;
608 std::ostringstream query;
609 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
611 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
613 // First DELETE operation
614 result = InvokeOCDoResource(query, OC_REST_DELETE,
616 deleteReqCB, NULL, 0);
617 if (OC_STACK_OK != result)
619 // Error can happen if for example, network connectivity is down
620 OC_LOG(INFO, TAG, "First DELETE call did not succeed");
624 //Create a thread to delete this resource again
626 pthread_create (&threadId, NULL, RequestDeleteDeathResourceTask, (void*)qos);
629 OC_LOG_V(INFO, TAG, "\n\nExit %s", __func__);
633 int InitGetRequest(OCQualityOfService qos, uint8_t withVendorSpecificHeaderOptions, bool getWithQuery)
636 OCHeaderOption options[MAX_HEADER_OPTIONS];
638 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
639 std::ostringstream query;
640 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
642 // ocserver is written to only process "power<X" query.
645 OC_LOG(INFO, TAG, "Using query power<30");
646 query << "?power<30";
649 if (withVendorSpecificHeaderOptions)
651 uint8_t option0[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
652 uint8_t option1[] = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
653 memset(options, 0, sizeof(OCHeaderOption) * MAX_HEADER_OPTIONS);
654 options[0].protocolID = OC_COAP_ID;
655 options[0].optionID = 2048;
656 memcpy(options[0].optionData, option0, sizeof(option0));
657 options[0].optionLength = 10;
658 options[1].protocolID = OC_COAP_ID;
659 options[1].optionID = 3000;
660 memcpy(options[1].optionData, option1, sizeof(option1));
661 options[1].optionLength = 10;
663 if (withVendorSpecificHeaderOptions)
665 return (InvokeOCDoResource(query, OC_REST_GET,
666 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, options, 2));
670 return (InvokeOCDoResource(query, OC_REST_GET,
671 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, NULL, 0));
675 int InitPlatformDiscovery(OCQualityOfService qos)
677 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
680 OCCallbackData cbData;
681 char szQueryUri[64] = { 0 };
683 cbData.cb = PlatformDiscoveryReqCB;
684 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
687 if(UNICAST_DISCOVERY)
689 snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_PLATFORM_DISCOVERY_QUERY, ipv4addr);
693 strncpy(szQueryUri, MULTICAST_PLATFORM_DISCOVERY_QUERY, sizeof(szQueryUri) -1 );
695 szQueryUri[sizeof(szQueryUri) -1] = '\0';
697 if(UNICAST_DISCOVERY)
699 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, OC_CONNTYPE,
700 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
704 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, 0, 0, CT_DEFAULT,
705 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
708 if (ret != OC_STACK_OK)
710 OC_LOG(ERROR, TAG, "OCStack device error");
716 int InitDeviceDiscovery(OCQualityOfService qos)
718 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
721 OCCallbackData cbData;
722 char szQueryUri[64] = { 0 };
724 cbData.cb = DeviceDiscoveryReqCB;
725 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
728 if(UNICAST_DISCOVERY)
730 snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_DEVICE_DISCOVERY_QUERY, ipv4addr);
734 strncpy(szQueryUri, MULTICAST_DEVICE_DISCOVERY_QUERY, sizeof(szQueryUri) -1 );
736 szQueryUri[sizeof(szQueryUri) -1] = '\0';
738 if(UNICAST_DISCOVERY)
740 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, OC_CONNTYPE,
741 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
745 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, 0, 0, CT_DEFAULT,
746 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
749 if (ret != OC_STACK_OK)
751 OC_LOG(ERROR, TAG, "OCStack device error");
757 int InitDiscovery(OCQualityOfService qos)
760 OCCallbackData cbData;
761 /* Start a discovery query*/
762 char szQueryUri[64] = { 0 };
764 if (UNICAST_DISCOVERY)
766 snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_RESOURCE_DISCOVERY_QUERY, ipv4addr);
770 strcpy(szQueryUri, MULTICAST_RESOURCE_DISCOVERY_QUERY);
773 cbData.cb = discoveryReqCB;
774 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
776 if(UNICAST_DISCOVERY)
778 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, OC_CONNTYPE,
779 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
783 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, 0, 0, CT_DEFAULT,
784 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
786 if (ret != OC_STACK_OK)
788 OC_LOG(ERROR, TAG, "OCStack resource error");
793 int main(int argc, char* argv[])
797 while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
802 UNICAST_DISCOVERY = atoi(optarg);
805 TEST_CASE = atoi(optarg);
808 OC_CONNTYPE = CT_ADAPTER_IP;
816 if ((UNICAST_DISCOVERY != 0 && UNICAST_DISCOVERY != 1) ||
817 (TEST_CASE < TEST_DISCOVER_REQ || TEST_CASE >= MAX_TESTS) )
823 /* Initialize OCStack*/
824 if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK)
826 OC_LOG(ERROR, TAG, "OCStack init error");
829 if (UNICAST_DISCOVERY)
831 printf("Enter IPv4 address of the Server hosting resource (Ex: 192.168.0.15)\n");
832 if (fgets(ipv4addr, IPV4_ADDR_SIZE, stdin))
834 //Strip newline char from ipv4addr
835 StripNewLineChar(ipv4addr);
839 OC_LOG(ERROR, TAG, "!! Bad input for IPV4 address. !!");
840 return OC_STACK_INVALID_PARAM;
844 if(UNICAST_DISCOVERY == 0 && TEST_CASE == TEST_DISCOVER_DEV_REQ)
846 InitDeviceDiscovery(OC_LOW_QOS);
848 else if(UNICAST_DISCOVERY == 0 && TEST_CASE == TEST_DISCOVER_PLATFORM_REQ)
850 InitPlatformDiscovery(OC_LOW_QOS);
854 InitDiscovery(OC_LOW_QOS);
857 // Break from loop with Ctrl+C
858 OC_LOG(INFO, TAG, "Entering occlient main loop...");
859 signal(SIGINT, handleSigInt);
863 if (OCProcess() != OC_STACK_OK)
865 OC_LOG(ERROR, TAG, "OCStack process error");
871 OC_LOG(INFO, TAG, "Exiting occlient main loop...");
873 if (OCStop() != OC_STACK_OK)
875 OC_LOG(ERROR, TAG, "OCStack stop error");
881 std::string getIPAddrTBServer(OCClientResponse * clientResponse)
887 if (!clientResponse->addr)
892 return std::string(clientResponse->devAddr.addr);
895 std::string getPortTBServer(OCClientResponse * clientResponse)
901 if (!clientResponse->addr)
905 std::ostringstream ss;
906 ss << clientResponse->devAddr.port;
910 std::string getConnectivityType (OCConnectivityType connType)
912 switch (connType & CT_MASK_ADAPTER)
917 case CT_ADAPTER_GATT_BTLE:
920 case CT_ADAPTER_RFCOMM_BTEDR:
924 return "Incorrect connectivity";
928 std::string getQueryStrForGetPut(OCClientResponse * clientResponse)
934 void parseClientResponse(OCClientResponse * clientResponse)
936 coapServerIP = getIPAddrTBServer(clientResponse);
937 coapServerPort = getPortTBServer(clientResponse);
938 coapServerResource = getQueryStrForGetPut(clientResponse);