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|2|3|4|5|6|7> -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);
156 return OC_STACK_DELETE_TRANSACTION;
159 OCStackApplicationResult postReqCB(void *ctx, OCDoHandle handle, OCClientResponse *clientResponse)
161 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
163 OC_LOG(INFO, TAG, "Callback Context for POST recvd successfully");
168 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
169 OC_LOG_V(INFO, TAG, "JSON = %s =============> Post Response",
170 clientResponse->resJSONPayload);
172 return OC_STACK_DELETE_TRANSACTION;
175 OCStackApplicationResult deleteReqCB(void *ctx,
176 OCDoHandle handle, OCClientResponse *clientResponse)
178 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
180 OC_LOG(INFO, TAG, "Callback Context for DELETE recvd successfully");
185 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
186 OC_LOG_V(INFO, TAG, "JSON = %s =============> Delete Response",
187 clientResponse->resJSONPayload);
189 return OC_STACK_DELETE_TRANSACTION;
192 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
194 if(clientResponse == NULL)
196 OC_LOG(INFO, TAG, "The clientResponse is NULL");
197 return OC_STACK_DELETE_TRANSACTION;
199 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
201 OC_LOG(INFO, TAG, "Callback Context for GET query recvd successfully");
204 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
205 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
206 OC_LOG_V(INFO, TAG, "JSON = %s =============> Get Response", clientResponse->resJSONPayload);
208 if(clientResponse->rcvdVendorSpecificHeaderOptions &&
209 clientResponse->numRcvdVendorSpecificHeaderOptions)
211 OC_LOG (INFO, TAG, "Received vendor specific options");
213 OCHeaderOption * rcvdOptions = clientResponse->rcvdVendorSpecificHeaderOptions;
214 for( i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
216 if(((OCHeaderOption)rcvdOptions[i]).protocolID == OC_COAP_ID)
218 OC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with",
219 ((OCHeaderOption)rcvdOptions[i]).optionID );
220 OC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData,
221 ((OCHeaderOption)rcvdOptions[i]).optionLength);
225 return OC_STACK_DELETE_TRANSACTION;
228 OCStackApplicationResult obsReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
230 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
232 OC_LOG(INFO, TAG, "Callback Context for OBS query recvd successfully");
237 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
238 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
239 OC_LOG_V(INFO, TAG, "Callback Context for OBSERVE notification recvd successfully %d",
240 gNumObserveNotifies);
241 OC_LOG_V(INFO, TAG, "JSON = %s =============> Obs Response",
242 clientResponse->resJSONPayload);
243 gNumObserveNotifies++;
244 if (gNumObserveNotifies == 3) //large number to test observing in DELETE case.
246 if(TEST_CASE == TEST_OBS_REQ_NON || TEST_CASE == TEST_OBS_REQ_CON)
248 if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
250 OC_LOG(ERROR, TAG, "Observe cancel error");
252 return OC_STACK_DELETE_TRANSACTION;
254 else if(TEST_CASE == TEST_OBS_REQ_NON_CANCEL_IMM)
256 if (OCCancel (gObserveDoHandle, OC_HIGH_QOS, NULL, 0) != OC_STACK_OK)
258 OC_LOG(ERROR, TAG, "Observe cancel error");
262 if(clientResponse->sequenceNumber == OC_OBSERVE_REGISTER)
264 OC_LOG(INFO, TAG, "This also serves as a registration confirmation");
266 else if(clientResponse->sequenceNumber == OC_OBSERVE_DEREGISTER)
268 OC_LOG(INFO, TAG, "This also serves as a deregistration confirmation");
269 return OC_STACK_DELETE_TRANSACTION;
271 else if(clientResponse->sequenceNumber == OC_OBSERVE_NO_OPTION)
273 OC_LOG(INFO, TAG, "This also tells you that registration/deregistration failed");
274 return OC_STACK_DELETE_TRANSACTION;
277 return OC_STACK_KEEP_TRANSACTION;
280 OCStackApplicationResult presenceCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
282 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
284 OC_LOG(INFO, TAG, "Callback Context for Presence recvd successfully");
289 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
290 OC_LOG_V(INFO, TAG, "NONCE NUMBER: %u", clientResponse->sequenceNumber);
291 OC_LOG_V(INFO, TAG, "Callback Context for Presence notification recvd successfully %d",
292 gNumPresenceNotifies);
293 OC_LOG_V(INFO, TAG, "JSON = %s =============> Presence Response",
294 clientResponse->resJSONPayload);
295 gNumPresenceNotifies++;
296 if (gNumPresenceNotifies == 20)
298 if (OCCancel(gPresenceHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
300 OC_LOG(ERROR, TAG, "Presence cancel error");
302 return OC_STACK_DELETE_TRANSACTION;
305 return OC_STACK_KEEP_TRANSACTION;
309 // This is a function called back when a device is discovered
310 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
311 OCClientResponse * clientResponse)
313 uint8_t remoteIpAddr[4];
314 uint16_t remotePortNu;
316 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
318 OC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully");
323 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
325 OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr,
326 remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3);
327 OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu);
329 std::string connectionType = getConnectivityType (clientResponse->connType);
330 OC_LOG_V(INFO, TAG, "Discovered on %s", connectionType.c_str());
332 "Device =============> Discovered %s @ %d.%d.%d.%d:%d",
333 clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1],
334 remoteIpAddr[2], remoteIpAddr[3], remotePortNu);
336 parseClientResponse(clientResponse);
340 case TEST_GET_REQ_NON:
341 InitGetRequest(OC_LOW_QOS, 0);
343 case TEST_PUT_REQ_NON:
346 case TEST_POST_REQ_NON:
347 InitPostRequest(OC_LOW_QOS);
349 case TEST_DELETE_REQ_NON:
350 InitDeleteRequest(OC_LOW_QOS);
352 case TEST_OBS_REQ_NON:
353 case TEST_OBS_REQ_NON_CANCEL_IMM:
354 InitObserveRequest(OC_LOW_QOS);
356 case TEST_GET_UNAVAILABLE_RES_REQ_NON:
357 InitGetRequestToUnavailableResource();
359 case TEST_GET_REQ_CON:
360 InitGetRequest(OC_HIGH_QOS, 0);
362 case TEST_POST_REQ_CON:
363 InitPostRequest(OC_HIGH_QOS);
365 case TEST_DELETE_REQ_CON:
366 InitDeleteRequest(OC_HIGH_QOS);
368 case TEST_OBS_REQ_CON:
369 InitObserveRequest(OC_HIGH_QOS);
372 case TEST_OBS_PRESENCE:
373 case TEST_OBS_PRESENCE_WITH_FILTER:
374 case TEST_OBS_PRESENCE_WITH_FILTERS:
378 case TEST_GET_REQ_NON_WITH_VENDOR_HEADER_OPTIONS:
379 InitGetRequest(OC_LOW_QOS, 1);
381 case TEST_DISCOVER_DEV_REQ:
382 InitDeviceDiscovery();
389 return OC_STACK_KEEP_TRANSACTION;
392 OCStackApplicationResult DeviceDiscoveryReqCB (void* ctx, OCDoHandle handle,
393 OCClientResponse * clientResponse)
395 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
397 OC_LOG(INFO, TAG, "Callback Context for Device DISCOVER query recvd successfully");
402 //OC_LOG truncates the response as it is too long.
403 fprintf(stderr, "Discovery response: \n %s\n", clientResponse->resJSONPayload);
407 return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
413 OCStackResult result = OC_STACK_OK;
414 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
415 std::ostringstream query;
416 std::ostringstream querySuffix;
417 query << "coap://" << coapServerIP << ":" << coapServerPort << OC_PRESENCE_URI;
418 if(TEST_CASE == TEST_OBS_PRESENCE)
420 result = InvokeOCDoResource(query, OC_REST_PRESENCE, OC_LOW_QOS,
421 presenceCB, NULL, 0);
423 if(TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTER || TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTERS)
426 querySuffix << query.str() << "?rt=core.light";
427 result = InvokeOCDoResource(querySuffix, OC_REST_PRESENCE, OC_LOW_QOS,
428 presenceCB, NULL, 0);
430 if(TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTERS)
432 if(result == OC_STACK_OK)
435 querySuffix << query.str() << "?rt=core.fan";
436 result = InvokeOCDoResource(querySuffix, OC_REST_PRESENCE, OC_LOW_QOS,
437 presenceCB, NULL, 0);
444 int InitGetRequestToUnavailableResource()
446 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
447 std::ostringstream query;
448 query << "coap://" << coapServerIP << ":" << coapServerPort << "/SomeUnknownResource";
449 return (InvokeOCDoResource(query, OC_REST_GET, OC_LOW_QOS, getReqCB, NULL, 0));
452 int InitObserveRequest(OCQualityOfService qos)
454 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
455 std::ostringstream query;
456 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
457 return (InvokeOCDoResource(query,
458 OC_REST_OBSERVE, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, obsReqCB, NULL, 0));
463 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
464 std::ostringstream query;
465 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
466 return (InvokeOCDoResource(query, OC_REST_PUT, OC_LOW_QOS, putReqCB, NULL, 0));
469 int InitPostRequest(OCQualityOfService qos)
471 OCStackResult result;
472 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
473 std::ostringstream query;
474 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
476 // First POST operation (to create an Light instance)
477 result = InvokeOCDoResource(query, OC_REST_POST,
478 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
480 if (OC_STACK_OK != result)
482 // Error can happen if for example, network connectivity is down
483 OC_LOG(INFO, TAG, "First POST call did not succeed");
486 // Second POST operation (to create an Light instance)
487 result = InvokeOCDoResource(query, OC_REST_POST,
488 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
490 if (OC_STACK_OK != result)
492 OC_LOG(INFO, TAG, "Second POST call did not succeed");
495 // This POST operation will update the original resourced /a/light
496 return (InvokeOCDoResource(query, OC_REST_POST,
497 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
498 postReqCB, NULL, 0));
501 void* RequestDeleteDeathResourceTask(void* myqos)
503 sleep (30);//long enough to give the server time to finish deleting the resource.
504 std::ostringstream query;
505 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
507 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
509 // Second DELETE operation to delete the resource that might have been removed already.
510 OCQualityOfService qos;
520 OCStackResult result = InvokeOCDoResource(query, OC_REST_DELETE,
522 deleteReqCB, NULL, 0);
524 if (OC_STACK_OK != result)
526 OC_LOG(INFO, TAG, "Second DELETE call did not succeed");
532 int InitDeleteRequest(OCQualityOfService qos)
534 OCStackResult result;
535 std::ostringstream query;
536 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
538 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
540 // First DELETE operation
541 result = InvokeOCDoResource(query, OC_REST_DELETE,
543 deleteReqCB, NULL, 0);
544 if (OC_STACK_OK != result)
546 // Error can happen if for example, network connectivity is down
547 OC_LOG(INFO, TAG, "First DELETE call did not succeed");
551 //Create a thread to delete this resource again
553 pthread_create (&threadId, NULL, RequestDeleteDeathResourceTask, (void*)qos);
556 OC_LOG_V(INFO, TAG, "\n\nExit %s", __func__);
560 int InitGetRequest(OCQualityOfService qos, uint8_t withVendorSpecificHeaderOptions)
562 OCHeaderOption options[MAX_HEADER_OPTIONS];
564 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
565 std::ostringstream query;
566 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
568 if (withVendorSpecificHeaderOptions)
570 uint8_t option0[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
571 uint8_t option1[] = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
572 memset(options, 0, sizeof(OCHeaderOption) * MAX_HEADER_OPTIONS);
573 options[0].protocolID = OC_COAP_ID;
574 options[0].optionID = 2048;
575 memcpy(options[0].optionData, option0, sizeof(option0));
576 options[0].optionLength = 10;
577 options[1].protocolID = OC_COAP_ID;
578 options[1].optionID = 3000;
579 memcpy(options[1].optionData, option1, sizeof(option1));
580 options[1].optionLength = 10;
582 if (withVendorSpecificHeaderOptions)
584 return (InvokeOCDoResource(query, OC_REST_GET,
585 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, options, 2));
589 return (InvokeOCDoResource(query, OC_REST_GET,
590 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, NULL, 0));
594 int InitDeviceDiscovery()
597 OCCallbackData cbData;
598 char szQueryUri[64] = { 0 };
600 cbData.cb = DeviceDiscoveryReqCB;
601 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
604 if(UNICAST_DISCOVERY)
606 snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_DEVICE_DISCOVERY_QUERY, ipv4addr);
610 strncpy(szQueryUri, MULTICAST_DEVICE_DISCOVERY_QUERY,
611 (strlen(MULTICAST_DEVICE_DISCOVERY_QUERY) + 1));
614 if(UNICAST_DISCOVERY)
616 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, OC_CONNTYPE,
617 OC_LOW_QOS, &cbData, NULL, 0);
621 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, (OC_ALL),
622 OC_LOW_QOS, &cbData, NULL, 0);
625 if (ret != OC_STACK_OK)
627 OC_LOG(ERROR, TAG, "OCStack device error");
636 OCCallbackData cbData;
637 /* Start a discovery query*/
638 char szQueryUri[64] = { 0 };
640 if (UNICAST_DISCOVERY)
642 snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_DISCOVERY_QUERY, ipv4addr);
646 strcpy(szQueryUri, MULTICAST_RESOURCE_DISCOVERY_QUERY);
649 cbData.cb = discoveryReqCB;
650 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
652 if(UNICAST_DISCOVERY)
654 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, OC_CONNTYPE,
655 OC_LOW_QOS, &cbData, NULL, 0);
659 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, (OC_ALL),
660 OC_LOW_QOS, &cbData, NULL, 0);
662 if (ret != OC_STACK_OK)
664 OC_LOG(ERROR, TAG, "OCStack resource error");
669 int main(int argc, char* argv[])
673 while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
678 UNICAST_DISCOVERY = atoi(optarg);
681 TEST_CASE = atoi(optarg);
684 OC_CONNTYPE = OCConnectivityType(atoi(optarg));
692 if ((UNICAST_DISCOVERY != 0 && UNICAST_DISCOVERY != 1) ||
693 (TEST_CASE < TEST_DISCOVER_REQ || TEST_CASE >= MAX_TESTS) )
699 /* Initialize OCStack*/
700 if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK)
702 OC_LOG(ERROR, TAG, "OCStack init error");
705 if (UNICAST_DISCOVERY)
707 printf("Enter IPv4 address of the Server hosting resource (Ex: 192.168.0.15)\n");
708 if (fgets(ipv4addr, IPV4_ADDR_SIZE, stdin))
710 //Strip newline char from ipv4addr
711 StripNewLineChar(ipv4addr);
715 OC_LOG(ERROR, TAG, "!! Bad input for IPV4 address. !!");
716 return OC_STACK_INVALID_PARAM;
720 if(UNICAST_DISCOVERY == 0 && TEST_CASE == TEST_DISCOVER_DEV_REQ)
722 InitDeviceDiscovery();
729 // Break from loop with Ctrl+C
730 OC_LOG(INFO, TAG, "Entering occlient main loop...");
731 signal(SIGINT, handleSigInt);
735 if (OCProcess() != OC_STACK_OK)
737 OC_LOG(ERROR, TAG, "OCStack process error");
743 OC_LOG(INFO, TAG, "Exiting occlient main loop...");
745 if (OCStop() != OC_STACK_OK)
747 OC_LOG(ERROR, TAG, "OCStack stop error");
753 std::string getIPAddrTBServer(OCClientResponse * clientResponse)
759 if (!clientResponse->addr)
763 uint8_t a, b, c, d = 0;
764 if (0 != OCDevAddrToIPv4Addr(clientResponse->addr, &a, &b, &c, &d))
769 char ipaddr[16] = {'\0'};
770 // ostringstream not working correctly here, hence snprintf
771 snprintf(ipaddr, sizeof(ipaddr), "%d.%d.%d.%d", a,b,c,d);
772 return std::string (ipaddr);
775 std::string getPortTBServer(OCClientResponse * clientResponse)
781 if (!clientResponse->addr)
786 if (0 != OCDevAddrToPort(clientResponse->addr, &p))
790 std::ostringstream ss;
795 std::string getConnectivityType (OCConnectivityType connType)
812 return "Incorrect connectivity";
816 std::string getQueryStrForGetPut(OCClientResponse * clientResponse)
822 void parseClientResponse(OCClientResponse * clientResponse)
824 coapServerIP = getIPAddrTBServer(clientResponse);
825 coapServerPort = getPortTBServer(clientResponse);
826 coapServerResource = getQueryStrForGetPut(clientResponse);