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"
33 // Tracking user input
34 static int UNICAST_DISCOVERY = 0;
35 static int TEST_CASE = 0;
36 static int CONNECTIVITY = 0;
38 static const char * UNICAST_DEVICE_DISCOVERY_QUERY = "coap://%s/oic/d";
39 static const char * MULTICAST_DEVICE_DISCOVERY_QUERY = "/oic/d";
40 static const char * UNICAST_PLATFORM_DISCOVERY_QUERY = "coap://%s/oic/p";
41 static const char * MULTICAST_PLATFORM_DISCOVERY_QUERY = "/oic/p";
43 static const char * UNICAST_RESOURCE_DISCOVERY_QUERY = "coap://%s/oic/res";
44 static const char * MULTICAST_RESOURCE_DISCOVERY_QUERY = "/oic/res";
45 //The following variable determines the interface protocol (IPv4, IPv6, etc)
46 //to be used for sending unicast messages. Default set to IPv4.
47 static OCConnectivityType OC_CONNTYPE = CT_ADAPTER_IP;
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 OCPayload* putPayload()
82 OCRepPayload* payload = OCRepPayloadCreate();
86 std::cout << "Failed to create put payload object"<<std::endl;
90 OCRepPayloadSetPropInt(payload, "power", 15);
91 OCRepPayloadSetPropBool(payload, "state", true);
93 return (OCPayload*) payload;
96 static void PrintUsage()
98 OC_LOG(INFO, TAG, "Usage : occlient -u <0|1> -t <1..17> -c <0|1>");
99 OC_LOG(INFO, TAG, "-u <0|1> : Perform multicast/unicast discovery of resources");
100 OC_LOG(INFO, TAG, "-c 0 : Use Default connectivity(IP)");
101 OC_LOG(INFO, TAG, "-c 1 : IP Connectivity Type");
102 OC_LOG(INFO, TAG, "-t 1 : Discover Resources");
103 OC_LOG(INFO, TAG, "-t 2 : Discover Resources and Initiate Nonconfirmable Get Request");
104 OC_LOG(INFO, TAG, "-t 3 : Discover Resources and Initiate Nonconfirmable Get Request"
105 " with query filter.");
106 OC_LOG(INFO, TAG, "-t 4 : Discover Resources and Initiate Nonconfirmable Put Requests");
107 OC_LOG(INFO, TAG, "-t 5 : Discover Resources and Initiate Nonconfirmable Post Requests");
108 OC_LOG(INFO, TAG, "-t 6 : Discover Resources and Initiate Nonconfirmable Delete Requests");
109 OC_LOG(INFO, TAG, "-t 7 : Discover Resources and Initiate Nonconfirmable Observe Requests");
110 OC_LOG(INFO, TAG, "-t 8 : Discover Resources and Initiate Nonconfirmable Get Request "\
111 "for a resource which is unavailable");
112 OC_LOG(INFO, TAG, "-t 9 : Discover Resources and Initiate Confirmable Get Request");
113 OC_LOG(INFO, TAG, "-t 10 : Discover Resources and Initiate Confirmable Post Request");
114 OC_LOG(INFO, TAG, "-t 11 : Discover Resources and Initiate Confirmable Delete Requests");
115 OC_LOG(INFO, TAG, "-t 12 : Discover Resources and Initiate Confirmable Observe Requests"\
116 " and cancel with Low QoS");
119 OC_LOG(INFO, TAG, "-t 13 : Discover Resources and Initiate Nonconfirmable presence");
120 OC_LOG(INFO, TAG, "-t 14 : Discover Resources and Initiate Nonconfirmable presence with "\
122 OC_LOG(INFO, TAG, "-t 15 : Discover Resources and Initiate Nonconfirmable presence with "\
124 OC_LOG(INFO, TAG, "-t 16 : Discover Resources and Initiate Nonconfirmable multicast presence.");
127 OC_LOG(INFO, TAG, "-t 17 : Discover Resources and Initiate Nonconfirmable Observe Requests "\
128 "then cancel immediately with High QOS");
129 OC_LOG(INFO, TAG, "-t 18 : Discover Resources and Initiate Nonconfirmable Get Request and "\
130 "add vendor specific header options");
131 OC_LOG(INFO, TAG, "-t 19 : Discover Platform");
132 OC_LOG(INFO, TAG, "-t 20 : Discover Devices");
135 OCStackResult InvokeOCDoResource(std::ostringstream &query,
137 OCQualityOfService qos,
138 OCClientResponseHandler cb,
139 OCHeaderOption * options,
143 OCCallbackData cbData;
147 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
150 ret = OCDoResource(&handle, method, query.str().c_str(), 0,
151 (method == OC_REST_PUT) ? putPayload() : NULL,
152 (OC_CONNTYPE), qos, &cbData, options, numOptions);
154 if (ret != OC_STACK_OK)
156 OC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method);
158 else if (method == OC_REST_OBSERVE || method == OC_REST_OBSERVE_ALL)
160 gObserveDoHandle = handle;
163 else if (method == OC_REST_PRESENCE)
165 gPresenceHandle = handle;
172 OCStackApplicationResult putReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
174 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
176 OC_LOG(INFO, TAG, "Callback Context for PUT recvd successfully");
181 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
182 OC_LOG_PAYLOAD(INFO, TAG, clientResponse->payload);
183 OC_LOG(INFO, TAG, PCF("=============> Put Response"));
187 OC_LOG_V(INFO, TAG, "putReqCB received Null clientResponse");
189 return OC_STACK_DELETE_TRANSACTION;
192 OCStackApplicationResult postReqCB(void *ctx, OCDoHandle handle, OCClientResponse *clientResponse)
194 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
196 OC_LOG(INFO, TAG, "Callback Context for POST recvd successfully");
201 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
202 OC_LOG_PAYLOAD(INFO, TAG, clientResponse->payload);
203 OC_LOG(INFO, TAG, PCF("=============> Post Response"));
207 OC_LOG_V(INFO, TAG, "postReqCB received Null clientResponse");
209 return OC_STACK_DELETE_TRANSACTION;
212 OCStackApplicationResult deleteReqCB(void *ctx,
213 OCDoHandle handle, OCClientResponse *clientResponse)
215 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
217 OC_LOG(INFO, TAG, "Callback Context for DELETE recvd successfully");
222 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
223 OC_LOG_PAYLOAD(INFO, TAG, clientResponse->payload);
224 OC_LOG(INFO, TAG, PCF("=============> Delete Response"));
228 OC_LOG_V(INFO, TAG, "deleteReqCB received Null clientResponse");
230 return OC_STACK_DELETE_TRANSACTION;
233 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, 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, TAG, clientResponse->payload);
249 OC_LOG(INFO, TAG, PCF("=============> 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, OCClientResponse * clientResponse)
273 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
275 OC_LOG(INFO, TAG, "Callback Context for OBS query recvd successfully");
280 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
281 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
282 OC_LOG_V(INFO, TAG, "Callback Context for OBSERVE notification recvd successfully %d",
283 gNumObserveNotifies);
284 OC_LOG_PAYLOAD(INFO, TAG, clientResponse->payload);
285 OC_LOG(INFO, TAG, PCF("=============> Obs Response"));
286 gNumObserveNotifies++;
287 if (gNumObserveNotifies == 15) //large number to test observing in DELETE case.
289 if(TEST_CASE == TEST_OBS_REQ_NON || TEST_CASE == TEST_OBS_REQ_CON)
291 if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
293 OC_LOG(ERROR, TAG, "Observe cancel error");
295 return OC_STACK_DELETE_TRANSACTION;
297 else if(TEST_CASE == TEST_OBS_REQ_NON_CANCEL_IMM)
299 if (OCCancel (gObserveDoHandle, OC_HIGH_QOS, NULL, 0) != OC_STACK_OK)
301 OC_LOG(ERROR, TAG, "Observe cancel error");
305 if(clientResponse->sequenceNumber == OC_OBSERVE_REGISTER)
307 OC_LOG(INFO, TAG, "This also serves as a registration confirmation");
309 else if(clientResponse->sequenceNumber == OC_OBSERVE_DEREGISTER)
311 OC_LOG(INFO, TAG, "This also serves as a deregistration confirmation");
312 return OC_STACK_DELETE_TRANSACTION;
314 else if(clientResponse->sequenceNumber == OC_OBSERVE_NO_OPTION)
316 OC_LOG(INFO, TAG, "This also tells you that registration/deregistration failed");
317 return OC_STACK_DELETE_TRANSACTION;
322 OC_LOG_V(INFO, TAG, "obsReqCB received Null clientResponse");
324 return OC_STACK_KEEP_TRANSACTION;
327 OCStackApplicationResult presenceCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
329 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
331 OC_LOG(INFO, TAG, "Callback Context for Presence recvd successfully");
336 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
337 OC_LOG_V(INFO, TAG, "NONCE NUMBER: %u", clientResponse->sequenceNumber);
338 OC_LOG_V(INFO, TAG, "Callback Context for Presence notification recvd successfully %d",
339 gNumPresenceNotifies);
340 OC_LOG_PAYLOAD(INFO, TAG, clientResponse->payload);
341 OC_LOG(INFO, TAG, PCF("=============> Presence Response"));
342 gNumPresenceNotifies++;
343 if (gNumPresenceNotifies == 20)
345 if (OCCancel(gPresenceHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
347 OC_LOG(ERROR, TAG, "Presence cancel error");
349 return OC_STACK_DELETE_TRANSACTION;
354 OC_LOG_V(INFO, TAG, "presenceCB received Null clientResponse");
356 return OC_STACK_KEEP_TRANSACTION;
360 // This is a function called back when a device is discovered
361 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
362 OCClientResponse * clientResponse)
364 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
366 OC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully");
371 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
373 std::string connectionType = getConnectivityType (clientResponse->connType);
374 OC_LOG_V(INFO, TAG, "Discovered on %s", connectionType.c_str());
376 "Device =============> Discovered @ %s:%d",
377 clientResponse->devAddr.addr,
378 clientResponse->devAddr.port);
379 OC_LOG_PAYLOAD(INFO, TAG, clientResponse->payload);
381 OC_CONNTYPE = clientResponse->connType;
382 parseClientResponse(clientResponse);
386 case TEST_GET_REQ_NON:
387 InitGetRequest(OC_LOW_QOS, 0, 0);
389 case TEST_GET_REQ_NON_WITH_FILTERS:
390 InitGetRequest(OC_LOW_QOS, 0, 1);
392 case TEST_PUT_REQ_NON:
393 InitPutRequest(OC_LOW_QOS);
395 case TEST_POST_REQ_NON:
396 InitPostRequest(OC_LOW_QOS);
398 case TEST_DELETE_REQ_NON:
399 InitDeleteRequest(OC_LOW_QOS);
401 case TEST_OBS_REQ_NON:
402 case TEST_OBS_REQ_NON_CANCEL_IMM:
403 InitObserveRequest(OC_LOW_QOS);
405 case TEST_GET_UNAVAILABLE_RES_REQ_NON:
406 InitGetRequestToUnavailableResource(OC_LOW_QOS);
408 case TEST_GET_REQ_CON:
409 InitGetRequest(OC_HIGH_QOS, 0, 0);
411 case TEST_POST_REQ_CON:
412 InitPostRequest(OC_HIGH_QOS);
414 case TEST_DELETE_REQ_CON:
415 InitDeleteRequest(OC_HIGH_QOS);
417 case TEST_OBS_REQ_CON:
418 InitObserveRequest(OC_HIGH_QOS);
421 case TEST_OBS_PRESENCE:
422 case TEST_OBS_PRESENCE_WITH_FILTER:
423 case TEST_OBS_PRESENCE_WITH_FILTERS:
424 case TEST_OBS_MULTICAST_PRESENCE:
428 case TEST_GET_REQ_NON_WITH_VENDOR_HEADER_OPTIONS:
429 InitGetRequest(OC_LOW_QOS, 1, 0);
431 case TEST_DISCOVER_PLATFORM_REQ:
432 InitPlatformDiscovery(OC_LOW_QOS);
434 case TEST_DISCOVER_DEV_REQ:
435 InitDeviceDiscovery(OC_LOW_QOS);
444 OC_LOG_V(INFO, TAG, "discoveryReqCB received Null clientResponse");
446 return OC_STACK_KEEP_TRANSACTION;
449 OCStackApplicationResult PlatformDiscoveryReqCB (void* ctx, OCDoHandle handle,
450 OCClientResponse * clientResponse)
452 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
454 OC_LOG(INFO, TAG, "Callback Context for Platform DISCOVER query recvd successfully");
459 OC_LOG(INFO, TAG, PCF("Discovery Response:"));
460 OC_LOG_PAYLOAD(INFO, TAG, clientResponse->payload);
464 OC_LOG_V(INFO, TAG, "PlatformDiscoveryReqCB received Null clientResponse");
467 return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
470 OCStackApplicationResult DeviceDiscoveryReqCB (void* ctx, OCDoHandle handle,
471 OCClientResponse * clientResponse)
473 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
475 OC_LOG(INFO, TAG, "Callback Context for Device DISCOVER query recvd successfully");
480 OC_LOG(INFO, TAG, PCF("Discovery Response:"));
481 OC_LOG_PAYLOAD(INFO, TAG, clientResponse->payload);
485 OC_LOG_V(INFO, TAG, "PlatformDiscoveryReqCB received Null clientResponse");
488 return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
494 OCStackResult result = OC_STACK_OK;
495 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
496 std::ostringstream query;
497 std::ostringstream querySuffix;
498 query << "coap://" << coapServerIP << ":" << coapServerPort << OC_RSRVD_PRESENCE_URI;
499 if(TEST_CASE == TEST_OBS_PRESENCE)
501 result = InvokeOCDoResource(query, OC_REST_PRESENCE, OC_LOW_QOS,
502 presenceCB, NULL, 0);
504 if(TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTER || TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTERS)
507 querySuffix << query.str() << "?rt=core.led";
508 result = InvokeOCDoResource(querySuffix, OC_REST_PRESENCE, OC_LOW_QOS,
509 presenceCB, NULL, 0);
511 if(TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTERS)
513 if(result == OC_STACK_OK)
516 querySuffix << query.str() << "?rt=core.fan";
517 result = InvokeOCDoResource(querySuffix, OC_REST_PRESENCE, OC_LOW_QOS,
518 presenceCB, NULL, 0);
521 if(TEST_CASE == TEST_OBS_MULTICAST_PRESENCE)
523 if(result == OC_STACK_OK)
525 std::ostringstream multicastPresenceQuery;
526 multicastPresenceQuery.str("");
527 multicastPresenceQuery << "coap://" << OC_MULTICAST_PREFIX << OC_RSRVD_PRESENCE_URI;
528 result = InvokeOCDoResource(multicastPresenceQuery, OC_REST_PRESENCE, OC_LOW_QOS,
529 presenceCB, NULL, 0);
536 int InitGetRequestToUnavailableResource(OCQualityOfService qos)
538 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
539 std::ostringstream query;
540 query << "coap://" << coapServerIP << ":" << coapServerPort << "/SomeUnknownResource";
541 return (InvokeOCDoResource(query, OC_REST_GET, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
545 int InitObserveRequest(OCQualityOfService qos)
547 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
548 std::ostringstream query;
549 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
550 return (InvokeOCDoResource(query,
551 OC_REST_OBSERVE, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, obsReqCB, NULL, 0));
554 int InitPutRequest(OCQualityOfService qos)
556 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
557 std::ostringstream query;
558 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
559 return (InvokeOCDoResource(query, OC_REST_PUT, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
563 int InitPostRequest(OCQualityOfService qos)
565 OCStackResult result;
566 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
567 std::ostringstream query;
568 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
570 // First POST operation (to create an Light instance)
571 result = InvokeOCDoResource(query, OC_REST_POST,
572 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
574 if (OC_STACK_OK != result)
576 // Error can happen if for example, network connectivity is down
577 OC_LOG(INFO, TAG, "First POST call did not succeed");
580 // Second POST operation (to create an Light instance)
581 result = InvokeOCDoResource(query, OC_REST_POST,
582 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
584 if (OC_STACK_OK != result)
586 OC_LOG(INFO, TAG, "Second POST call did not succeed");
589 // This POST operation will update the original resourced /a/light
590 return (InvokeOCDoResource(query, OC_REST_POST,
591 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
592 postReqCB, NULL, 0));
595 void* RequestDeleteDeathResourceTask(void* myqos)
597 sleep (30);//long enough to give the server time to finish deleting the resource.
598 std::ostringstream query;
599 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
601 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
603 // Second DELETE operation to delete the resource that might have been removed already.
604 OCQualityOfService qos;
614 OCStackResult result = InvokeOCDoResource(query, OC_REST_DELETE,
616 deleteReqCB, NULL, 0);
618 if (OC_STACK_OK != result)
620 OC_LOG(INFO, TAG, "Second DELETE call did not succeed");
626 int InitDeleteRequest(OCQualityOfService qos)
628 OCStackResult result;
629 std::ostringstream query;
630 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
632 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
634 // First DELETE operation
635 result = InvokeOCDoResource(query, OC_REST_DELETE,
637 deleteReqCB, NULL, 0);
638 if (OC_STACK_OK != result)
640 // Error can happen if for example, network connectivity is down
641 OC_LOG(INFO, TAG, "First DELETE call did not succeed");
645 //Create a thread to delete this resource again
647 pthread_create (&threadId, NULL, RequestDeleteDeathResourceTask, (void*)qos);
650 OC_LOG_V(INFO, TAG, "\n\nExit %s", __func__);
654 int InitGetRequest(OCQualityOfService qos, uint8_t withVendorSpecificHeaderOptions, bool getWithQuery)
657 OCHeaderOption options[MAX_HEADER_OPTIONS];
659 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
660 std::ostringstream query;
661 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
663 // ocserver is written to only process "power<X" query.
666 OC_LOG(INFO, TAG, "Using query power<30");
667 query << "?power<30";
670 if (withVendorSpecificHeaderOptions)
672 uint8_t option0[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
673 uint8_t option1[] = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
674 memset(options, 0, sizeof(OCHeaderOption) * MAX_HEADER_OPTIONS);
675 options[0].protocolID = OC_COAP_ID;
676 options[0].optionID = 2048;
677 memcpy(options[0].optionData, option0, sizeof(option0));
678 options[0].optionLength = 10;
679 options[1].protocolID = OC_COAP_ID;
680 options[1].optionID = 3000;
681 memcpy(options[1].optionData, option1, sizeof(option1));
682 options[1].optionLength = 10;
684 if (withVendorSpecificHeaderOptions)
686 return (InvokeOCDoResource(query, OC_REST_GET,
687 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, options, 2));
691 return (InvokeOCDoResource(query, OC_REST_GET,
692 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, NULL, 0));
696 int InitPlatformDiscovery(OCQualityOfService qos)
698 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
701 OCCallbackData cbData;
702 char szQueryUri[64] = { 0 };
704 cbData.cb = PlatformDiscoveryReqCB;
705 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
708 if(UNICAST_DISCOVERY)
710 snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_PLATFORM_DISCOVERY_QUERY, ipv4addr);
714 strncpy(szQueryUri, MULTICAST_PLATFORM_DISCOVERY_QUERY, sizeof(szQueryUri) -1 );
716 szQueryUri[sizeof(szQueryUri) -1] = '\0';
718 if(UNICAST_DISCOVERY)
720 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, OC_CONNTYPE,
721 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
726 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, 0, 0, OC_CONNTYPE,
727 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
730 if (ret != OC_STACK_OK)
732 OC_LOG(ERROR, TAG, "OCStack device error");
738 int InitDeviceDiscovery(OCQualityOfService qos)
740 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
743 OCCallbackData cbData;
744 char szQueryUri[64] = { 0 };
746 cbData.cb = DeviceDiscoveryReqCB;
747 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
750 if(UNICAST_DISCOVERY)
752 snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_DEVICE_DISCOVERY_QUERY, ipv4addr);
756 strncpy(szQueryUri, MULTICAST_DEVICE_DISCOVERY_QUERY, sizeof(szQueryUri) -1 );
758 szQueryUri[sizeof(szQueryUri) -1] = '\0';
760 if(UNICAST_DISCOVERY)
762 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, OC_CONNTYPE,
763 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
767 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, 0, 0, OC_CONNTYPE,
768 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
771 if (ret != OC_STACK_OK)
773 OC_LOG(ERROR, TAG, "OCStack device error");
779 int InitDiscovery(OCQualityOfService qos)
782 OCCallbackData cbData;
783 /* Start a discovery query*/
784 char szQueryUri[64] = { 0 };
786 if (UNICAST_DISCOVERY)
788 snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_RESOURCE_DISCOVERY_QUERY, ipv4addr);
792 strcpy(szQueryUri, MULTICAST_RESOURCE_DISCOVERY_QUERY);
795 cbData.cb = discoveryReqCB;
796 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
798 if(UNICAST_DISCOVERY)
800 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, OC_CONNTYPE,
801 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
805 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, 0, 0, OC_CONNTYPE,
806 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
808 if (ret != OC_STACK_OK)
810 OC_LOG(ERROR, TAG, "OCStack resource error");
815 int main(int argc, char* argv[])
819 while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
824 UNICAST_DISCOVERY = atoi(optarg);
827 TEST_CASE = atoi(optarg);
830 CONNECTIVITY = atoi(optarg);
838 if ((UNICAST_DISCOVERY != 0 && UNICAST_DISCOVERY != 1) ||
839 (TEST_CASE < TEST_DISCOVER_REQ || TEST_CASE >= MAX_TESTS) ||
840 (CONNECTIVITY < CT_ADAPTER_DEFAULT || CONNECTIVITY >= MAX_CT))
846 /* Initialize OCStack*/
847 if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK)
849 OC_LOG(ERROR, TAG, "OCStack init error");
853 if(CONNECTIVITY == CT_ADAPTER_DEFAULT || CONNECTIVITY == CT_IP)
855 OC_CONNTYPE = CT_ADAPTER_IP;
859 OC_LOG(INFO, TAG, "Default Connectivity type selected...");
863 if (UNICAST_DISCOVERY)
865 OC_LOG(INFO, TAG, "Enter IP address with port number of the Server hosting resource");
866 OC_LOG(INFO, TAG, "as follows - eg: 192.168.0.15:45454 (IP:Port) \n");
868 if (fgets(ipv4addr, IPV4_ADDR_SIZE, stdin))
870 //Strip newline char from ipv4addr
871 StripNewLineChar(ipv4addr);
875 OC_LOG(ERROR, TAG, "!! Bad input for IPV4 address. !!");
876 return OC_STACK_INVALID_PARAM;
880 if(UNICAST_DISCOVERY == 0 && TEST_CASE == TEST_DISCOVER_DEV_REQ)
882 InitDeviceDiscovery(OC_LOW_QOS);
884 else if(UNICAST_DISCOVERY == 0 && TEST_CASE == TEST_DISCOVER_PLATFORM_REQ)
886 InitPlatformDiscovery(OC_LOW_QOS);
890 InitDiscovery(OC_LOW_QOS);
893 // Break from loop with Ctrl+C
894 OC_LOG(INFO, TAG, "Entering occlient main loop...");
895 signal(SIGINT, handleSigInt);
899 if (OCProcess() != OC_STACK_OK)
901 OC_LOG(ERROR, TAG, "OCStack process error");
907 OC_LOG(INFO, TAG, "Exiting occlient main loop...");
909 if (OCStop() != OC_STACK_OK)
911 OC_LOG(ERROR, TAG, "OCStack stop error");
917 std::string getIPAddrTBServer(OCClientResponse * clientResponse)
923 if (!clientResponse->addr)
928 return std::string(clientResponse->devAddr.addr);
931 std::string getPortTBServer(OCClientResponse * clientResponse)
937 if (!clientResponse->addr)
941 std::ostringstream ss;
942 ss << clientResponse->devAddr.port;
946 std::string getConnectivityType (OCConnectivityType connType)
948 switch (connType & CT_MASK_ADAPTER)
959 case CT_ADAPTER_GATT_BTLE:
962 case CT_ADAPTER_RFCOMM_BTEDR:
966 return "Incorrect connectivity";
970 std::string getQueryStrForGetPut(OCClientResponse * clientResponse)
976 void parseClientResponse(OCClientResponse * clientResponse)
978 coapServerIP = getIPAddrTBServer(clientResponse);
979 coapServerPort = getPortTBServer(clientResponse);
980 coapServerResource = getQueryStrForGetPut(clientResponse);