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 (wifi, ethernet etc.)
39 //to be used for sending unicast messages. Default set to WIFI.
40 static OCConnectivityType OC_CONNTYPE = OC_WIFI;
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> : Send unicast messages over Ethernet or WIFI");
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 Put Requests");
81 OC_LOG(INFO, TAG, "-t 4 : Discover Resources and Initiate Nonconfirmable Post Requests");
82 OC_LOG(INFO, TAG, "-t 5 : Discover Resources and Initiate Nonconfirmable Delete Requests");
83 OC_LOG(INFO, TAG, "-t 6 : Discover Resources and Initiate Nonconfirmable Observe Requests");
84 OC_LOG(INFO, TAG, "-t 7 : Discover Resources and Initiate Nonconfirmable Get Request "\
85 "for a resource which is unavailable");
86 OC_LOG(INFO, TAG, "-t 8 : Discover Resources and Initiate Confirmable Get Request");
87 OC_LOG(INFO, TAG, "-t 9 : Discover Resources and Initiate Confirmable Post Request");
88 OC_LOG(INFO, TAG, "-t 10 : Discover Resources and Initiate Confirmable Delete Requests");
89 OC_LOG(INFO, TAG, "-t 11 : Discover Resources and Initiate Confirmable Observe Requests");
92 OC_LOG(INFO, TAG, "-t 12 : Discover Resources and Initiate Nonconfirmable presence");
93 OC_LOG(INFO, TAG, "-t 13 : Discover Resources and Initiate Nonconfirmable presence with "\
95 OC_LOG(INFO, TAG, "-t 14 : Discover Resources and Initiate Nonconfirmable presence with "\
99 OC_LOG(INFO, TAG, "-t 15 : Discover Resources and Initiate Nonconfirmable Observe Requests "\
100 "then cancel immediately");
101 OC_LOG(INFO, TAG, "-t 16 : Discover Resources and Initiate Nonconfirmable Get Request and "\
102 "add vendor specific header options");
103 OC_LOG(INFO, TAG, "-t 17 : Discover Devices");
106 OCStackResult InvokeOCDoResource(std::ostringstream &query,
108 OCQualityOfService qos,
109 OCClientResponseHandler cb,
110 OCHeaderOption * options,
114 OCCallbackData cbData;
118 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
121 ret = OCDoResource(&handle, method, query.str().c_str(), 0,
122 (method == OC_REST_PUT) ? putPayload.c_str() : NULL,
123 (OC_CONNTYPE), qos, &cbData, options, numOptions);
125 if (ret != OC_STACK_OK)
127 OC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method);
129 else if (method == OC_REST_OBSERVE || method == OC_REST_OBSERVE_ALL)
131 gObserveDoHandle = handle;
134 else if (method == OC_REST_PRESENCE)
136 gPresenceHandle = handle;
143 OCStackApplicationResult putReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
145 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
147 OC_LOG(INFO, TAG, "Callback Context for PUT recvd successfully");
152 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
153 OC_LOG_V(INFO, TAG, "JSON = %s =============> Put Response",
154 clientResponse->resJSONPayload);
158 OC_LOG_V(INFO, TAG, "putReqCB received Null clientResponse");
160 return OC_STACK_DELETE_TRANSACTION;
163 OCStackApplicationResult postReqCB(void *ctx, OCDoHandle handle, OCClientResponse *clientResponse)
165 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
167 OC_LOG(INFO, TAG, "Callback Context for POST recvd successfully");
172 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
173 OC_LOG_V(INFO, TAG, "JSON = %s =============> Post Response",
174 clientResponse->resJSONPayload);
178 OC_LOG_V(INFO, TAG, "postReqCB received Null clientResponse");
180 return OC_STACK_DELETE_TRANSACTION;
183 OCStackApplicationResult deleteReqCB(void *ctx,
184 OCDoHandle handle, OCClientResponse *clientResponse)
186 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
188 OC_LOG(INFO, TAG, "Callback Context for DELETE recvd successfully");
193 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
194 OC_LOG_V(INFO, TAG, "JSON = %s =============> Delete Response",
195 clientResponse->resJSONPayload);
199 OC_LOG_V(INFO, TAG, "deleteReqCB received Null clientResponse");
201 return OC_STACK_DELETE_TRANSACTION;
204 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
206 if(clientResponse == NULL)
208 OC_LOG(INFO, TAG, "getReqCB received NULL clientResponse");
209 return OC_STACK_DELETE_TRANSACTION;
212 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
214 OC_LOG(INFO, TAG, "Callback Context for GET query recvd successfully");
217 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
218 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
219 OC_LOG_V(INFO, TAG, "JSON = %s =============> Get Response", clientResponse->resJSONPayload);
221 if(clientResponse->rcvdVendorSpecificHeaderOptions &&
222 clientResponse->numRcvdVendorSpecificHeaderOptions)
224 OC_LOG (INFO, TAG, "Received vendor specific options");
226 OCHeaderOption * rcvdOptions = clientResponse->rcvdVendorSpecificHeaderOptions;
227 for( i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
229 if(((OCHeaderOption)rcvdOptions[i]).protocolID == OC_COAP_ID)
231 OC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with",
232 ((OCHeaderOption)rcvdOptions[i]).optionID );
234 OC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData,
235 MAX_HEADER_OPTION_DATA_LENGTH);
239 return OC_STACK_DELETE_TRANSACTION;
242 OCStackApplicationResult obsReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
244 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
246 OC_LOG(INFO, TAG, "Callback Context for OBS query recvd successfully");
251 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
252 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
253 OC_LOG_V(INFO, TAG, "Callback Context for OBSERVE notification recvd successfully %d",
254 gNumObserveNotifies);
255 OC_LOG_V(INFO, TAG, "JSON = %s =============> Obs Response",
256 clientResponse->resJSONPayload);
257 gNumObserveNotifies++;
258 if (gNumObserveNotifies == 3) //large number to test observing in DELETE case.
260 if(TEST_CASE == TEST_OBS_REQ_NON || TEST_CASE == TEST_OBS_REQ_CON)
262 if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
264 OC_LOG(ERROR, TAG, "Observe cancel error");
266 return OC_STACK_DELETE_TRANSACTION;
268 else if(TEST_CASE == TEST_OBS_REQ_NON_CANCEL_IMM)
270 if (OCCancel (gObserveDoHandle, OC_HIGH_QOS, NULL, 0) != OC_STACK_OK)
272 OC_LOG(ERROR, TAG, "Observe cancel error");
276 if(clientResponse->sequenceNumber == OC_OBSERVE_REGISTER)
278 OC_LOG(INFO, TAG, "This also serves as a registration confirmation");
280 else if(clientResponse->sequenceNumber == OC_OBSERVE_DEREGISTER)
282 OC_LOG(INFO, TAG, "This also serves as a deregistration confirmation");
283 return OC_STACK_DELETE_TRANSACTION;
285 else if(clientResponse->sequenceNumber == OC_OBSERVE_NO_OPTION)
287 OC_LOG(INFO, TAG, "This also tells you that registration/deregistration failed");
288 return OC_STACK_DELETE_TRANSACTION;
293 OC_LOG_V(INFO, TAG, "obsReqCB received Null clientResponse");
295 return OC_STACK_KEEP_TRANSACTION;
298 OCStackApplicationResult presenceCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
300 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
302 OC_LOG(INFO, TAG, "Callback Context for Presence recvd successfully");
307 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
308 OC_LOG_V(INFO, TAG, "NONCE NUMBER: %u", clientResponse->sequenceNumber);
309 OC_LOG_V(INFO, TAG, "Callback Context for Presence notification recvd successfully %d",
310 gNumPresenceNotifies);
311 OC_LOG_V(INFO, TAG, "JSON = %s =============> Presence Response",
312 clientResponse->resJSONPayload);
313 gNumPresenceNotifies++;
314 if (gNumPresenceNotifies == 20)
316 if (OCCancel(gPresenceHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
318 OC_LOG(ERROR, TAG, "Presence cancel error");
320 return OC_STACK_DELETE_TRANSACTION;
325 OC_LOG_V(INFO, TAG, "presenceCB received Null clientResponse");
327 return OC_STACK_KEEP_TRANSACTION;
331 // This is a function called back when a device is discovered
332 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
333 OCClientResponse * clientResponse)
335 uint8_t remoteIpAddr[4];
336 uint16_t remotePortNu;
338 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
340 OC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully");
345 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
347 OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr,
348 remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3);
349 OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu);
351 std::string connectionType = getConnectivityType (clientResponse->connType);
352 OC_LOG_V(INFO, TAG, "Discovered on %s", connectionType.c_str());
354 "Device =============> Discovered %s @ %d.%d.%d.%d:%d",
355 clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1],
356 remoteIpAddr[2], remoteIpAddr[3], remotePortNu);
358 parseClientResponse(clientResponse);
362 case TEST_GET_REQ_NON:
363 InitGetRequest(OC_LOW_QOS, 0);
365 case TEST_PUT_REQ_NON:
366 InitPutRequest(OC_LOW_QOS);
368 case TEST_POST_REQ_NON:
369 InitPostRequest(OC_LOW_QOS);
371 case TEST_DELETE_REQ_NON:
372 InitDeleteRequest(OC_LOW_QOS);
374 case TEST_OBS_REQ_NON:
375 case TEST_OBS_REQ_NON_CANCEL_IMM:
376 InitObserveRequest(OC_LOW_QOS);
378 case TEST_GET_UNAVAILABLE_RES_REQ_NON:
379 InitGetRequestToUnavailableResource(OC_LOW_QOS);
381 case TEST_GET_REQ_CON:
382 InitGetRequest(OC_HIGH_QOS, 0);
384 case TEST_POST_REQ_CON:
385 InitPostRequest(OC_HIGH_QOS);
387 case TEST_DELETE_REQ_CON:
388 InitDeleteRequest(OC_HIGH_QOS);
390 case TEST_OBS_REQ_CON:
391 InitObserveRequest(OC_HIGH_QOS);
394 case TEST_OBS_PRESENCE:
395 case TEST_OBS_PRESENCE_WITH_FILTER:
396 case TEST_OBS_PRESENCE_WITH_FILTERS:
400 case TEST_GET_REQ_NON_WITH_VENDOR_HEADER_OPTIONS:
401 InitGetRequest(OC_LOW_QOS, 1);
403 case TEST_DISCOVER_DEV_REQ:
404 InitDeviceDiscovery(OC_LOW_QOS);
413 OC_LOG_V(INFO, TAG, "discoveryReqCB received Null clientResponse");
415 return OC_STACK_KEEP_TRANSACTION;
418 OCStackApplicationResult DeviceDiscoveryReqCB (void* ctx, OCDoHandle handle,
419 OCClientResponse * clientResponse)
421 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
423 OC_LOG(INFO, TAG, "Callback Context for Device DISCOVER query recvd successfully");
428 //OC_LOG truncates the response as it is too long.
429 fprintf(stderr, "Discovery response: \n %s\n", clientResponse->resJSONPayload);
434 OC_LOG_V(INFO, TAG, "DeviceDiscoveryReqCB received Null clientResponse");
437 return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
443 OCStackResult result = OC_STACK_OK;
444 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
445 std::ostringstream query;
446 std::ostringstream querySuffix;
447 query << "coap://" << coapServerIP << ":" << coapServerPort << OC_PRESENCE_URI;
448 if(TEST_CASE == TEST_OBS_PRESENCE)
450 result = InvokeOCDoResource(query, OC_REST_PRESENCE, OC_LOW_QOS,
451 presenceCB, NULL, 0);
453 if(TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTER || TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTERS)
456 querySuffix << query.str() << "?rt=core.led";
457 result = InvokeOCDoResource(querySuffix, OC_REST_PRESENCE, OC_LOW_QOS,
458 presenceCB, NULL, 0);
460 if(TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTERS)
462 if(result == OC_STACK_OK)
465 querySuffix << query.str() << "?rt=core.fan";
466 result = InvokeOCDoResource(querySuffix, OC_REST_PRESENCE, OC_LOW_QOS,
467 presenceCB, NULL, 0);
474 int InitGetRequestToUnavailableResource(OCQualityOfService qos)
476 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
477 std::ostringstream query;
478 query << "coap://" << coapServerIP << ":" << coapServerPort << "/SomeUnknownResource";
479 return (InvokeOCDoResource(query, OC_REST_GET, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
483 int InitObserveRequest(OCQualityOfService qos)
485 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
486 std::ostringstream query;
487 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
488 return (InvokeOCDoResource(query,
489 OC_REST_OBSERVE, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, obsReqCB, NULL, 0));
492 int InitPutRequest(OCQualityOfService qos)
494 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
495 std::ostringstream query;
496 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
497 return (InvokeOCDoResource(query, OC_REST_PUT, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
501 int InitPostRequest(OCQualityOfService qos)
503 OCStackResult result;
504 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
505 std::ostringstream query;
506 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
508 // First POST operation (to create an Light instance)
509 result = InvokeOCDoResource(query, OC_REST_POST,
510 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
512 if (OC_STACK_OK != result)
514 // Error can happen if for example, network connectivity is down
515 OC_LOG(INFO, TAG, "First POST call did not succeed");
518 // Second POST operation (to create an Light instance)
519 result = InvokeOCDoResource(query, OC_REST_POST,
520 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
522 if (OC_STACK_OK != result)
524 OC_LOG(INFO, TAG, "Second POST call did not succeed");
527 // This POST operation will update the original resourced /a/light
528 return (InvokeOCDoResource(query, OC_REST_POST,
529 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
530 postReqCB, NULL, 0));
533 void* RequestDeleteDeathResourceTask(void* myqos)
535 sleep (30);//long enough to give the server time to finish deleting the resource.
536 std::ostringstream query;
537 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
539 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
541 // Second DELETE operation to delete the resource that might have been removed already.
542 OCQualityOfService qos;
552 OCStackResult result = InvokeOCDoResource(query, OC_REST_DELETE,
554 deleteReqCB, NULL, 0);
556 if (OC_STACK_OK != result)
558 OC_LOG(INFO, TAG, "Second DELETE call did not succeed");
564 int InitDeleteRequest(OCQualityOfService qos)
566 OCStackResult result;
567 std::ostringstream query;
568 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
570 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
572 // First DELETE operation
573 result = InvokeOCDoResource(query, OC_REST_DELETE,
575 deleteReqCB, NULL, 0);
576 if (OC_STACK_OK != result)
578 // Error can happen if for example, network connectivity is down
579 OC_LOG(INFO, TAG, "First DELETE call did not succeed");
583 //Create a thread to delete this resource again
585 pthread_create (&threadId, NULL, RequestDeleteDeathResourceTask, (void*)qos);
588 OC_LOG_V(INFO, TAG, "\n\nExit %s", __func__);
592 int InitGetRequest(OCQualityOfService qos, uint8_t withVendorSpecificHeaderOptions)
594 OCHeaderOption options[MAX_HEADER_OPTIONS];
596 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
597 std::ostringstream query;
598 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
600 if (withVendorSpecificHeaderOptions)
602 uint8_t option0[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
603 uint8_t option1[] = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
604 memset(options, 0, sizeof(OCHeaderOption) * MAX_HEADER_OPTIONS);
605 options[0].protocolID = OC_COAP_ID;
606 options[0].optionID = 2048;
607 memcpy(options[0].optionData, option0, sizeof(option0));
608 options[0].optionLength = 10;
609 options[1].protocolID = OC_COAP_ID;
610 options[1].optionID = 3000;
611 memcpy(options[1].optionData, option1, sizeof(option1));
612 options[1].optionLength = 10;
614 if (withVendorSpecificHeaderOptions)
616 return (InvokeOCDoResource(query, OC_REST_GET,
617 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, options, 2));
621 return (InvokeOCDoResource(query, OC_REST_GET,
622 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, NULL, 0));
626 int InitDeviceDiscovery(OCQualityOfService qos)
629 OCCallbackData cbData;
630 char szQueryUri[64] = { 0 };
632 cbData.cb = DeviceDiscoveryReqCB;
633 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
636 if(UNICAST_DISCOVERY)
638 snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_DEVICE_DISCOVERY_QUERY, ipv4addr);
642 strncpy(szQueryUri, MULTICAST_DEVICE_DISCOVERY_QUERY,
643 (strlen(MULTICAST_DEVICE_DISCOVERY_QUERY) + 1));
646 if(UNICAST_DISCOVERY)
648 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, OC_CONNTYPE,
649 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
653 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, (OC_ALL),
654 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
657 if (ret != OC_STACK_OK)
659 OC_LOG(ERROR, TAG, "OCStack device error");
665 int InitDiscovery(OCQualityOfService qos)
668 OCCallbackData cbData;
669 /* Start a discovery query*/
670 char szQueryUri[64] = { 0 };
672 if (UNICAST_DISCOVERY)
674 snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_DISCOVERY_QUERY, ipv4addr);
678 strcpy(szQueryUri, MULTICAST_RESOURCE_DISCOVERY_QUERY);
681 cbData.cb = discoveryReqCB;
682 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
684 if(UNICAST_DISCOVERY)
686 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, OC_CONNTYPE,
687 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
691 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, (OC_ALL),
692 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
694 if (ret != OC_STACK_OK)
696 OC_LOG(ERROR, TAG, "OCStack resource error");
701 int main(int argc, char* argv[])
705 while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
710 UNICAST_DISCOVERY = atoi(optarg);
713 TEST_CASE = atoi(optarg);
716 OC_CONNTYPE = OCConnectivityType(atoi(optarg));
724 if ((UNICAST_DISCOVERY != 0 && UNICAST_DISCOVERY != 1) ||
725 (TEST_CASE < TEST_DISCOVER_REQ || TEST_CASE >= MAX_TESTS) )
731 /* Initialize OCStack*/
732 if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK)
734 OC_LOG(ERROR, TAG, "OCStack init error");
737 if (UNICAST_DISCOVERY)
739 printf("Enter IPv4 address of the Server hosting resource (Ex: 192.168.0.15)\n");
740 if (fgets(ipv4addr, IPV4_ADDR_SIZE, stdin))
742 //Strip newline char from ipv4addr
743 StripNewLineChar(ipv4addr);
747 OC_LOG(ERROR, TAG, "!! Bad input for IPV4 address. !!");
748 return OC_STACK_INVALID_PARAM;
752 if(UNICAST_DISCOVERY == 0 && TEST_CASE == TEST_DISCOVER_DEV_REQ)
754 InitDeviceDiscovery(OC_LOW_QOS);
758 InitDiscovery(OC_LOW_QOS);
761 // Break from loop with Ctrl+C
762 OC_LOG(INFO, TAG, "Entering occlient main loop...");
763 signal(SIGINT, handleSigInt);
767 if (OCProcess() != OC_STACK_OK)
769 OC_LOG(ERROR, TAG, "OCStack process error");
775 OC_LOG(INFO, TAG, "Exiting occlient main loop...");
777 if (OCStop() != OC_STACK_OK)
779 OC_LOG(ERROR, TAG, "OCStack stop error");
785 std::string getIPAddrTBServer(OCClientResponse * clientResponse)
791 if (!clientResponse->addr)
795 uint8_t a, b, c, d = 0;
796 if (0 != OCDevAddrToIPv4Addr(clientResponse->addr, &a, &b, &c, &d))
801 char ipaddr[16] = {'\0'};
802 // ostringstream not working correctly here, hence snprintf
803 snprintf(ipaddr, sizeof(ipaddr), "%d.%d.%d.%d", a,b,c,d);
804 return std::string (ipaddr);
807 std::string getPortTBServer(OCClientResponse * clientResponse)
813 if (!clientResponse->addr)
818 if (0 != OCDevAddrToPort(clientResponse->addr, &p))
822 std::ostringstream ss;
827 std::string getConnectivityType (OCConnectivityType connType)
844 return "Incorrect connectivity";
848 std::string getQueryStrForGetPut(OCClientResponse * clientResponse)
854 void parseClientResponse(OCClientResponse * clientResponse)
856 coapServerIP = getIPAddrTBServer(clientResponse);
857 coapServerPort = getPortTBServer(clientResponse);
858 coapServerResource = getQueryStrForGetPut(clientResponse);