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;
34 static const char * UNICAST_DISCOVERY_QUERY = "coap://%s:6298/oc/core";
35 static const char * UNICAST_DEVICE_DISCOVERY_QUERY = "coap://%s:6298/oc/core/d";
36 static const char * MULTICAST_DEVICE_DISCOVERY_QUERY = "/oc/core/d";
37 static const char * MULTICAST_RESOURCE_DISCOVERY_QUERY = "/oc/core";
38 //The following variable determines the interface protocol (IPv4, IPv6, etc)
39 //to be used for sending unicast messages. Default set to IPv4.
40 static OCConnectivityType OC_CONNTYPE = OC_IPV4;
41 static std::string putPayload = "{\"oc\":[{\"rep\":{\"power\":15,\"state\":true}}]}";
42 static std::string coapServerIP = "255.255.255.255";
43 static std::string coapServerPort = "5683";
44 static std::string coapServerResource = "/a/light";
45 static const int IPV4_ADDR_SIZE = 16;
46 //Use ipv4addr for both InitDiscovery and InitDeviceDiscovery
47 char ipv4addr[IPV4_ADDR_SIZE];
48 void StripNewLineChar(char* str);
50 // The handle for the observe registration
51 OCDoHandle gObserveDoHandle;
53 // The handle for observe registration
54 OCDoHandle gPresenceHandle;
56 // After this crosses a threshold client deregisters for further notifications
57 int gNumObserveNotifies = 0;
60 int gNumPresenceNotifies = 0;
64 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
65 void handleSigInt(int signum)
73 static void PrintUsage()
75 OC_LOG(INFO, TAG, "Usage : occlient -u <0|1> -t <1..17> -c <0|1>");
76 OC_LOG(INFO, TAG, "-u <0|1> : Perform multicast/unicast discovery of resources");
77 OC_LOG(INFO, TAG, "-c <0|1> : IPv4/IPv6 (IPv6 not currently supported)");
78 OC_LOG(INFO, TAG, "-t 1 : Discover Resources");
79 OC_LOG(INFO, TAG, "-t 2 : Discover Resources and Initiate Nonconfirmable Get Request");
80 OC_LOG(INFO, TAG, "-t 3 : Discover Resources and Initiate Nonconfirmable Get Request"
81 " with query filter.");
82 OC_LOG(INFO, TAG, "-t 4 : Discover Resources and Initiate Nonconfirmable Put Requests");
83 OC_LOG(INFO, TAG, "-t 5 : Discover Resources and Initiate Nonconfirmable Post Requests");
84 OC_LOG(INFO, TAG, "-t 6 : Discover Resources and Initiate Nonconfirmable Delete Requests");
85 OC_LOG(INFO, TAG, "-t 7 : Discover Resources and Initiate Nonconfirmable Observe Requests");
86 OC_LOG(INFO, TAG, "-t 8 : Discover Resources and Initiate Nonconfirmable Get Request "\
87 "for a resource which is unavailable");
88 OC_LOG(INFO, TAG, "-t 9 : Discover Resources and Initiate Confirmable Get Request");
89 OC_LOG(INFO, TAG, "-t 10 : Discover Resources and Initiate Confirmable Post Request");
90 OC_LOG(INFO, TAG, "-t 11 : Discover Resources and Initiate Confirmable Delete Requests");
91 OC_LOG(INFO, TAG, "-t 12 : Discover Resources and Initiate Confirmable Observe Requests"\
92 " and cancel with Low QoS");
95 OC_LOG(INFO, TAG, "-t 13 : Discover Resources and Initiate Nonconfirmable presence");
96 OC_LOG(INFO, TAG, "-t 14 : Discover Resources and Initiate Nonconfirmable presence with "\
98 OC_LOG(INFO, TAG, "-t 15 : Discover Resources and Initiate Nonconfirmable presence with "\
100 OC_LOG(INFO, TAG, "-t 16 : Discover Resources and Initiate Nonconfirmable multicast presence.");
103 OC_LOG(INFO, TAG, "-t 17 : Discover Resources and Initiate Nonconfirmable Observe Requests "\
104 "then cancel immediately with High QOS");
105 OC_LOG(INFO, TAG, "-t 18 : Discover Resources and Initiate Nonconfirmable Get Request and "\
106 "add vendor specific header options");
107 OC_LOG(INFO, TAG, "-t 19 : Discover Devices");
110 OCStackResult InvokeOCDoResource(std::ostringstream &query,
112 OCQualityOfService qos,
113 OCClientResponseHandler cb,
114 OCHeaderOption * options,
118 OCCallbackData cbData;
122 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
125 ret = OCDoResource(&handle, method, query.str().c_str(), 0,
126 (method == OC_REST_PUT) ? putPayload.c_str() : NULL,
127 (OC_CONNTYPE), qos, &cbData, options, numOptions);
129 if (ret != OC_STACK_OK)
131 OC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method);
133 else if (method == OC_REST_OBSERVE || method == OC_REST_OBSERVE_ALL)
135 gObserveDoHandle = handle;
138 else if (method == OC_REST_PRESENCE)
140 gPresenceHandle = handle;
147 OCStackApplicationResult putReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
149 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
151 OC_LOG(INFO, TAG, "Callback Context for PUT recvd successfully");
156 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
157 OC_LOG_V(INFO, TAG, "JSON = %s =============> Put Response",
158 clientResponse->resJSONPayload);
162 OC_LOG_V(INFO, TAG, "putReqCB received Null clientResponse");
164 return OC_STACK_DELETE_TRANSACTION;
167 OCStackApplicationResult postReqCB(void *ctx, OCDoHandle handle, OCClientResponse *clientResponse)
169 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
171 OC_LOG(INFO, TAG, "Callback Context for POST recvd successfully");
176 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
177 OC_LOG_V(INFO, TAG, "JSON = %s =============> Post Response",
178 clientResponse->resJSONPayload);
182 OC_LOG_V(INFO, TAG, "postReqCB received Null clientResponse");
184 return OC_STACK_DELETE_TRANSACTION;
187 OCStackApplicationResult deleteReqCB(void *ctx,
188 OCDoHandle handle, OCClientResponse *clientResponse)
190 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
192 OC_LOG(INFO, TAG, "Callback Context for DELETE recvd successfully");
197 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
198 OC_LOG_V(INFO, TAG, "JSON = %s =============> Delete Response",
199 clientResponse->resJSONPayload);
203 OC_LOG_V(INFO, TAG, "deleteReqCB received Null clientResponse");
205 return OC_STACK_DELETE_TRANSACTION;
208 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
210 if(clientResponse == NULL)
212 OC_LOG(INFO, TAG, "getReqCB received NULL clientResponse");
213 return OC_STACK_DELETE_TRANSACTION;
216 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
218 OC_LOG(INFO, TAG, "Callback Context for GET query recvd successfully");
221 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
222 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
223 OC_LOG_V(INFO, TAG, "JSON = %s =============> Get Response", clientResponse->resJSONPayload);
225 if(clientResponse->rcvdVendorSpecificHeaderOptions &&
226 clientResponse->numRcvdVendorSpecificHeaderOptions)
228 OC_LOG (INFO, TAG, "Received vendor specific options");
230 OCHeaderOption * rcvdOptions = clientResponse->rcvdVendorSpecificHeaderOptions;
231 for( i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
233 if(((OCHeaderOption)rcvdOptions[i]).protocolID == OC_COAP_ID)
235 OC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with",
236 ((OCHeaderOption)rcvdOptions[i]).optionID );
238 OC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData,
239 MAX_HEADER_OPTION_DATA_LENGTH);
243 return OC_STACK_DELETE_TRANSACTION;
246 OCStackApplicationResult obsReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
248 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
250 OC_LOG(INFO, TAG, "Callback Context for OBS query recvd successfully");
255 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
256 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
257 OC_LOG_V(INFO, TAG, "Callback Context for OBSERVE notification recvd successfully %d",
258 gNumObserveNotifies);
259 OC_LOG_V(INFO, TAG, "JSON = %s =============> Obs Response",
260 clientResponse->resJSONPayload);
261 gNumObserveNotifies++;
262 if (gNumObserveNotifies == 15) //large number to test observing in DELETE case.
264 if(TEST_CASE == TEST_OBS_REQ_NON || TEST_CASE == TEST_OBS_REQ_CON)
266 if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
268 OC_LOG(ERROR, TAG, "Observe cancel error");
270 return OC_STACK_DELETE_TRANSACTION;
272 else if(TEST_CASE == TEST_OBS_REQ_NON_CANCEL_IMM)
274 if (OCCancel (gObserveDoHandle, OC_HIGH_QOS, NULL, 0) != OC_STACK_OK)
276 OC_LOG(ERROR, TAG, "Observe cancel error");
280 if(clientResponse->sequenceNumber == OC_OBSERVE_REGISTER)
282 OC_LOG(INFO, TAG, "This also serves as a registration confirmation");
284 else if(clientResponse->sequenceNumber == OC_OBSERVE_DEREGISTER)
286 OC_LOG(INFO, TAG, "This also serves as a deregistration confirmation");
287 return OC_STACK_DELETE_TRANSACTION;
289 else if(clientResponse->sequenceNumber == OC_OBSERVE_NO_OPTION)
291 OC_LOG(INFO, TAG, "This also tells you that registration/deregistration failed");
292 return OC_STACK_DELETE_TRANSACTION;
297 OC_LOG_V(INFO, TAG, "obsReqCB received Null clientResponse");
299 return OC_STACK_KEEP_TRANSACTION;
302 OCStackApplicationResult presenceCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
304 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
306 OC_LOG(INFO, TAG, "Callback Context for Presence recvd successfully");
311 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
312 OC_LOG_V(INFO, TAG, "NONCE NUMBER: %u", clientResponse->sequenceNumber);
313 OC_LOG_V(INFO, TAG, "Callback Context for Presence notification recvd successfully %d",
314 gNumPresenceNotifies);
315 OC_LOG_V(INFO, TAG, "JSON = %s =============> Presence Response",
316 clientResponse->resJSONPayload);
317 gNumPresenceNotifies++;
318 if (gNumPresenceNotifies == 20)
320 if (OCCancel(gPresenceHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
322 OC_LOG(ERROR, TAG, "Presence cancel error");
324 return OC_STACK_DELETE_TRANSACTION;
329 OC_LOG_V(INFO, TAG, "presenceCB received Null clientResponse");
331 return OC_STACK_KEEP_TRANSACTION;
335 // This is a function called back when a device is discovered
336 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
337 OCClientResponse * clientResponse)
339 uint8_t remoteIpAddr[4];
340 uint16_t remotePortNu;
342 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
344 OC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully");
349 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
351 OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr,
352 remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3);
353 OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu);
355 std::string connectionType = getConnectivityType (clientResponse->connType);
356 OC_LOG_V(INFO, TAG, "Discovered on %s", connectionType.c_str());
358 "Device =============> Discovered %s @ %d.%d.%d.%d:%d",
359 clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1],
360 remoteIpAddr[2], remoteIpAddr[3], remotePortNu);
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_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 DeviceDiscoveryReqCB (void* ctx, OCDoHandle handle,
427 OCClientResponse * clientResponse)
429 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
431 OC_LOG(INFO, TAG, "Callback Context for Device 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, "DeviceDiscoveryReqCB received Null clientResponse");
445 return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
451 OCStackResult result = OC_STACK_OK;
452 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
453 std::ostringstream query;
454 std::ostringstream querySuffix;
455 query << "coap://" << coapServerIP << ":" << coapServerPort << OC_PRESENCE_URI;
456 if(TEST_CASE == TEST_OBS_PRESENCE)
458 result = InvokeOCDoResource(query, OC_REST_PRESENCE, OC_LOW_QOS,
459 presenceCB, NULL, 0);
461 if(TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTER || TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTERS)
464 querySuffix << query.str() << "?rt=core.led";
465 result = InvokeOCDoResource(querySuffix, OC_REST_PRESENCE, OC_LOW_QOS,
466 presenceCB, NULL, 0);
468 if(TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTERS)
470 if(result == OC_STACK_OK)
473 querySuffix << query.str() << "?rt=core.fan";
474 result = InvokeOCDoResource(querySuffix, OC_REST_PRESENCE, OC_LOW_QOS,
475 presenceCB, NULL, 0);
478 if(TEST_CASE == TEST_OBS_MULTICAST_PRESENCE)
480 if(result == OC_STACK_OK)
482 std::ostringstream multicastPresenceQuery;
483 multicastPresenceQuery.str("");
484 multicastPresenceQuery << "coap://" << OC_MULTICAST_PREFIX << OC_PRESENCE_URI;
485 result = InvokeOCDoResource(multicastPresenceQuery, OC_REST_PRESENCE, OC_LOW_QOS,
486 presenceCB, NULL, 0);
493 int InitGetRequestToUnavailableResource(OCQualityOfService qos)
495 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
496 std::ostringstream query;
497 query << "coap://" << coapServerIP << ":" << coapServerPort << "/SomeUnknownResource";
498 return (InvokeOCDoResource(query, OC_REST_GET, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
502 int InitObserveRequest(OCQualityOfService qos)
504 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
505 std::ostringstream query;
506 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
507 return (InvokeOCDoResource(query,
508 OC_REST_OBSERVE, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, obsReqCB, NULL, 0));
511 int InitPutRequest(OCQualityOfService qos)
513 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
514 std::ostringstream query;
515 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
516 return (InvokeOCDoResource(query, OC_REST_PUT, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
520 int InitPostRequest(OCQualityOfService qos)
522 OCStackResult result;
523 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
524 std::ostringstream query;
525 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
527 // First POST operation (to create an Light instance)
528 result = InvokeOCDoResource(query, OC_REST_POST,
529 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
531 if (OC_STACK_OK != result)
533 // Error can happen if for example, network connectivity is down
534 OC_LOG(INFO, TAG, "First POST call did not succeed");
537 // Second POST operation (to create an Light instance)
538 result = InvokeOCDoResource(query, OC_REST_POST,
539 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
541 if (OC_STACK_OK != result)
543 OC_LOG(INFO, TAG, "Second POST call did not succeed");
546 // This POST operation will update the original resourced /a/light
547 return (InvokeOCDoResource(query, OC_REST_POST,
548 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
549 postReqCB, NULL, 0));
552 void* RequestDeleteDeathResourceTask(void* myqos)
554 sleep (30);//long enough to give the server time to finish deleting the resource.
555 std::ostringstream query;
556 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
558 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
560 // Second DELETE operation to delete the resource that might have been removed already.
561 OCQualityOfService qos;
571 OCStackResult result = InvokeOCDoResource(query, OC_REST_DELETE,
573 deleteReqCB, NULL, 0);
575 if (OC_STACK_OK != result)
577 OC_LOG(INFO, TAG, "Second DELETE call did not succeed");
583 int InitDeleteRequest(OCQualityOfService qos)
585 OCStackResult result;
586 std::ostringstream query;
587 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
589 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
591 // First DELETE operation
592 result = InvokeOCDoResource(query, OC_REST_DELETE,
594 deleteReqCB, NULL, 0);
595 if (OC_STACK_OK != result)
597 // Error can happen if for example, network connectivity is down
598 OC_LOG(INFO, TAG, "First DELETE call did not succeed");
602 //Create a thread to delete this resource again
604 pthread_create (&threadId, NULL, RequestDeleteDeathResourceTask, (void*)qos);
607 OC_LOG_V(INFO, TAG, "\n\nExit %s", __func__);
611 int InitGetRequest(OCQualityOfService qos, uint8_t withVendorSpecificHeaderOptions, bool getWithQuery)
614 OCHeaderOption options[MAX_HEADER_OPTIONS];
616 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
617 std::ostringstream query;
618 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
620 // ocserver is written to only process "power<X" query.
623 OC_LOG(INFO, TAG, "Using query power<30");
624 query << "?power<30";
627 if (withVendorSpecificHeaderOptions)
629 uint8_t option0[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
630 uint8_t option1[] = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
631 memset(options, 0, sizeof(OCHeaderOption) * MAX_HEADER_OPTIONS);
632 options[0].protocolID = OC_COAP_ID;
633 options[0].optionID = 2048;
634 memcpy(options[0].optionData, option0, sizeof(option0));
635 options[0].optionLength = 10;
636 options[1].protocolID = OC_COAP_ID;
637 options[1].optionID = 3000;
638 memcpy(options[1].optionData, option1, sizeof(option1));
639 options[1].optionLength = 10;
641 if (withVendorSpecificHeaderOptions)
643 return (InvokeOCDoResource(query, OC_REST_GET,
644 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, options, 2));
648 return (InvokeOCDoResource(query, OC_REST_GET,
649 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, NULL, 0));
653 int InitDeviceDiscovery(OCQualityOfService qos)
656 OCCallbackData cbData;
657 char szQueryUri[64] = { 0 };
659 cbData.cb = DeviceDiscoveryReqCB;
660 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
663 if(UNICAST_DISCOVERY)
665 snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_DEVICE_DISCOVERY_QUERY, ipv4addr);
669 strncpy(szQueryUri, MULTICAST_DEVICE_DISCOVERY_QUERY,
670 sizeof(szQueryUri) -1 );
671 szQueryUri[sizeof(szQueryUri) -1] = '\0';
674 if(UNICAST_DISCOVERY)
676 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, OC_CONNTYPE,
677 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
681 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, (OC_ALL),
682 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
685 if (ret != OC_STACK_OK)
687 OC_LOG(ERROR, TAG, "OCStack device error");
693 int InitDiscovery(OCQualityOfService qos)
696 OCCallbackData cbData;
697 /* Start a discovery query*/
698 char szQueryUri[64] = { 0 };
700 if (UNICAST_DISCOVERY)
702 snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_DISCOVERY_QUERY, ipv4addr);
706 strcpy(szQueryUri, MULTICAST_RESOURCE_DISCOVERY_QUERY);
709 cbData.cb = discoveryReqCB;
710 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
712 if(UNICAST_DISCOVERY)
714 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, OC_CONNTYPE,
715 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
719 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, (OC_ALL),
720 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
722 if (ret != OC_STACK_OK)
724 OC_LOG(ERROR, TAG, "OCStack resource error");
729 int main(int argc, char* argv[])
733 while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
738 UNICAST_DISCOVERY = atoi(optarg);
741 TEST_CASE = atoi(optarg);
744 // TODO: re-enable IPv4/IPv6 command line selection when IPv6 is supported
745 // OC_CONNTYPE = OCConnectivityType(atoi(optarg));
746 OC_CONNTYPE = OC_IPV4;
747 OC_LOG(INFO, TAG, "IPv6 not currently supported, using IPv4.");
755 if ((UNICAST_DISCOVERY != 0 && UNICAST_DISCOVERY != 1) ||
756 (TEST_CASE < TEST_DISCOVER_REQ || TEST_CASE >= MAX_TESTS) )
762 /* Initialize OCStack*/
763 if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK)
765 OC_LOG(ERROR, TAG, "OCStack init error");
768 if (UNICAST_DISCOVERY)
770 printf("Enter IPv4 address of the Server hosting resource (Ex: 192.168.0.15)\n");
771 if (fgets(ipv4addr, IPV4_ADDR_SIZE, stdin))
773 //Strip newline char from ipv4addr
774 StripNewLineChar(ipv4addr);
778 OC_LOG(ERROR, TAG, "!! Bad input for IPV4 address. !!");
779 return OC_STACK_INVALID_PARAM;
783 if(UNICAST_DISCOVERY == 0 && TEST_CASE == TEST_DISCOVER_DEV_REQ)
785 InitDeviceDiscovery(OC_LOW_QOS);
789 InitDiscovery(OC_LOW_QOS);
792 // Break from loop with Ctrl+C
793 OC_LOG(INFO, TAG, "Entering occlient main loop...");
794 signal(SIGINT, handleSigInt);
798 if (OCProcess() != OC_STACK_OK)
800 OC_LOG(ERROR, TAG, "OCStack process error");
806 OC_LOG(INFO, TAG, "Exiting occlient main loop...");
808 if (OCStop() != OC_STACK_OK)
810 OC_LOG(ERROR, TAG, "OCStack stop error");
816 std::string getIPAddrTBServer(OCClientResponse * clientResponse)
822 if (!clientResponse->addr)
826 uint8_t a, b, c, d = 0;
827 if (0 != OCDevAddrToIPv4Addr(clientResponse->addr, &a, &b, &c, &d))
832 char ipaddr[16] = {'\0'};
833 // ostringstream not working correctly here, hence snprintf
834 snprintf(ipaddr, sizeof(ipaddr), "%d.%d.%d.%d", a,b,c,d);
835 return std::string (ipaddr);
838 std::string getPortTBServer(OCClientResponse * clientResponse)
844 if (!clientResponse->addr)
849 if (0 != OCDevAddrToPort(clientResponse->addr, &p))
853 std::ostringstream ss;
858 std::string getConnectivityType (OCConnectivityType connType)
875 return "Incorrect connectivity";
879 std::string getQueryStrForGetPut(OCClientResponse * clientResponse)
885 void parseClientResponse(OCClientResponse * clientResponse)
887 coapServerIP = getIPAddrTBServer(clientResponse);
888 coapServerPort = getPortTBServer(clientResponse);
889 coapServerResource = getQueryStrForGetPut(clientResponse);