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 * TEST_APP_UNICAST_DISCOVERY_QUERY = "coap://0.0.0.0:5683/oc/core";
35 static const char * TEST_APP_UNICAST_DEVICE_DISCOVERY_QUERY = "coap://0.0.0.0:5683/oc/core/d";
36 static const char * TEST_APP_MULTICAST_DEVICE_DISCOVERY_QUERY = "coap://224.0.1.187:5683/oc/core/d";
37 static std::string putPayload = "{\"state\":\"on\",\"power\":5}";
38 static std::string coapServerIP = "255.255.255.255";
39 static std::string coapServerPort = "5683";
40 static std::string coapServerResource = "/a/light";
42 // The handle for the observe registration
43 OCDoHandle gObserveDoHandle;
45 // The handle for observe registration
46 OCDoHandle gPresenceHandle;
48 // After this crosses a threshold client deregisters for further notifications
49 int gNumObserveNotifies = 0;
52 int gNumPresenceNotifies = 0;
56 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
57 void handleSigInt(int signum) {
58 if (signum == SIGINT) {
63 static void PrintUsage()
65 OC_LOG(INFO, TAG, "Usage : occlient -u <0|1> -t <1|2|3|4|5|6|7>");
66 OC_LOG(INFO, TAG, "-u <0|1> : Perform multicast/unicast discovery of resources");
67 OC_LOG(INFO, TAG, "-t 1 : Discover Resources");
68 OC_LOG(INFO, TAG, "-t 2 : Discover Resources and Initiate Nonconfirmable Get Request");
69 OC_LOG(INFO, TAG, "-t 3 : Discover Resources and Initiate Nonconfirmable Put Requests");
70 OC_LOG(INFO, TAG, "-t 4 : Discover Resources and Initiate Nonconfirmable Post Requests");
71 OC_LOG(INFO, TAG, "-t 5 : Discover Resources and Initiate Nonconfirmable Delete Requests");
72 OC_LOG(INFO, TAG, "-t 6 : Discover Resources and Initiate Nonconfirmable Observe Requests");
73 OC_LOG(INFO, TAG, "-t 7 : Discover Resources and Initiate Nonconfirmable Get Request for a resource which is unavailable");
75 OC_LOG(INFO, TAG, "-t 8 : Discover Resources and Initiate Confirmable Get Request");
76 OC_LOG(INFO, TAG, "-t 9 : Discover Resources and Initiate Confirmable Post Request");
77 OC_LOG(INFO, TAG, "-t 10 : Discover Resources and Initiate Confirmable Delete Requests");
78 OC_LOG(INFO, TAG, "-t 11 : Discover Resources and Initiate Confirmable Observe Requests");
81 OC_LOG(INFO, TAG, "-t 12 : Discover Resources and Initiate Nonconfirmable presence");
82 OC_LOG(INFO, TAG, "-t 13 : Discover Resources and Initiate Nonconfirmable presence with filter");
85 OC_LOG(INFO, TAG, "-t 14 : Discover Resources and Initiate Nonconfirmable Observe Requests then cancel immediately");
86 OC_LOG(INFO, TAG, "-t 15 : Discover Resources and Initiate Nonconfirmable Get Request and add vendor specific header options");
87 OC_LOG(INFO, TAG, "-t 16 : Discover Devices");
90 OCStackResult InvokeOCDoResource(std::ostringstream &query,
91 OCMethod method, OCQualityOfService qos,
92 OCClientResponseHandler cb, OCHeaderOption * options, uint8_t numOptions)
95 OCCallbackData cbData;
99 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
102 ret = OCDoResource(&handle, method, query.str().c_str(), 0,
103 (method == OC_REST_PUT) ? putPayload.c_str() : NULL,
104 qos, &cbData, options, numOptions);
106 if (ret != OC_STACK_OK)
108 OC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method);
110 else if (method == OC_REST_OBSERVE || method == OC_REST_OBSERVE_ALL)
112 gObserveDoHandle = handle;
115 else if (method == OC_REST_PRESENCE)
117 gPresenceHandle = handle;
124 OCStackApplicationResult putReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) {
125 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
127 OC_LOG(INFO, TAG, "Callback Context for PUT recvd successfully");
132 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
133 OC_LOG_V(INFO, TAG, "JSON = %s =============> Put Response", clientResponse->resJSONPayload);
135 return OC_STACK_DELETE_TRANSACTION;
138 OCStackApplicationResult postReqCB(void *ctx, OCDoHandle handle, OCClientResponse *clientResponse)
140 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
142 OC_LOG(INFO, TAG, "Callback Context for POST recvd successfully");
147 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
148 OC_LOG_V(INFO, TAG, "JSON = %s =============> Post Response", clientResponse->resJSONPayload);
150 return OC_STACK_DELETE_TRANSACTION;
153 OCStackApplicationResult deleteReqCB(void *ctx, OCDoHandle handle, OCClientResponse *clientResponse)
155 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
157 OC_LOG(INFO, TAG, "Callback Context for DELETE recvd successfully");
162 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
163 OC_LOG_V(INFO, TAG, "JSON = %s =============> Delete Response", clientResponse->resJSONPayload);
165 return OC_STACK_DELETE_TRANSACTION;
168 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
170 if(clientResponse == NULL)
172 OC_LOG(INFO, TAG, "The clientResponse is NULL");
173 return OC_STACK_DELETE_TRANSACTION;
175 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
177 OC_LOG(INFO, TAG, "Callback Context for GET query recvd successfully");
180 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
181 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
182 OC_LOG_V(INFO, TAG, "JSON = %s =============> Get Response", clientResponse->resJSONPayload);
184 if(clientResponse->rcvdVendorSpecificHeaderOptions &&
185 clientResponse->numRcvdVendorSpecificHeaderOptions)
187 OC_LOG (INFO, TAG, "Received vendor specific options");
189 OCHeaderOption * rcvdOptions = clientResponse->rcvdVendorSpecificHeaderOptions;
190 for( i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
192 if(((OCHeaderOption)rcvdOptions[i]).protocolID == OC_COAP_ID)
194 OC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with",
195 ((OCHeaderOption)rcvdOptions[i]).optionID );
196 OC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData,
197 ((OCHeaderOption)rcvdOptions[i]).optionLength);
201 return OC_STACK_DELETE_TRANSACTION;
204 OCStackApplicationResult obsReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) {
205 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
207 OC_LOG(INFO, TAG, "Callback Context for OBS query recvd successfully");
212 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
213 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
214 OC_LOG_V(INFO, TAG, "Callback Context for OBSERVE notification recvd successfully %d", gNumObserveNotifies);
215 OC_LOG_V(INFO, TAG, "JSON = %s =============> Obs Response", clientResponse->resJSONPayload);
216 gNumObserveNotifies++;
217 if (gNumObserveNotifies == 3) //large number to test observing in DELETE case.
219 if(TEST_CASE == TEST_OBS_REQ_NON || TEST_CASE == TEST_OBS_REQ_CON){
220 if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK){
221 OC_LOG(ERROR, TAG, "Observe cancel error");
223 return OC_STACK_DELETE_TRANSACTION;
224 }else if(TEST_CASE == TEST_OBS_REQ_NON_CANCEL_IMM){
225 if (OCCancel (gObserveDoHandle, OC_HIGH_QOS, NULL, 0) != OC_STACK_OK){
226 OC_LOG(ERROR, TAG, "Observe cancel error");
230 if(clientResponse->sequenceNumber == OC_OBSERVE_REGISTER){
231 OC_LOG(INFO, TAG, "This also serves as a registration confirmation");
232 }else if(clientResponse->sequenceNumber == OC_OBSERVE_DEREGISTER){
233 OC_LOG(INFO, TAG, "This also serves as a deregistration confirmation");
234 return OC_STACK_DELETE_TRANSACTION;
235 }else if(clientResponse->sequenceNumber == OC_OBSERVE_NO_OPTION){
236 OC_LOG(INFO, TAG, "This also tells you that registration/deregistration failed");
237 return OC_STACK_DELETE_TRANSACTION;
240 return OC_STACK_KEEP_TRANSACTION;
243 OCStackApplicationResult presenceCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) {
244 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
246 OC_LOG(INFO, TAG, "Callback Context for Presence recvd successfully");
251 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
252 OC_LOG_V(INFO, TAG, "NONCE NUMBER: %u", clientResponse->sequenceNumber);
253 OC_LOG_V(INFO, TAG, "Callback Context for Presence notification recvd successfully %d", gNumPresenceNotifies);
254 OC_LOG_V(INFO, TAG, "JSON = %s =============> Presence Response", clientResponse->resJSONPayload);
255 gNumPresenceNotifies++;
256 if (gNumPresenceNotifies == 20)
258 if (OCCancel (gPresenceHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK){
259 OC_LOG(ERROR, TAG, "Presence cancel error");
261 return OC_STACK_DELETE_TRANSACTION;
264 return OC_STACK_KEEP_TRANSACTION;
268 // This is a function called back when a device is discovered
269 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
270 OCClientResponse * clientResponse) {
271 uint8_t remoteIpAddr[4];
272 uint16_t remotePortNu;
274 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
276 OC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully");
281 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
283 OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr,
284 remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3);
285 OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu);
288 "Device =============> Discovered %s @ %d.%d.%d.%d:%d",
289 clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1],
290 remoteIpAddr[2], remoteIpAddr[3], remotePortNu);
292 parseClientResponse(clientResponse);
296 case TEST_GET_REQ_NON:
297 InitGetRequest(OC_LOW_QOS, 0);
299 case TEST_PUT_REQ_NON:
302 case TEST_POST_REQ_NON:
303 InitPostRequest(OC_LOW_QOS);
305 case TEST_DELETE_REQ_NON:
306 InitDeleteRequest(OC_LOW_QOS);
308 case TEST_OBS_REQ_NON:
309 case TEST_OBS_REQ_NON_CANCEL_IMM:
310 InitObserveRequest(OC_LOW_QOS);
312 case TEST_GET_UNAVAILABLE_RES_REQ_NON:
313 InitGetRequestToUnavailableResource();
315 case TEST_GET_REQ_CON:
316 InitGetRequest(OC_HIGH_QOS, 0);
318 case TEST_POST_REQ_CON:
319 InitPostRequest(OC_HIGH_QOS);
321 case TEST_DELETE_REQ_CON:
322 InitDeleteRequest(OC_HIGH_QOS);
324 case TEST_OBS_REQ_CON:
325 InitObserveRequest(OC_HIGH_QOS);
328 case TEST_OBS_PRESENCE:
329 case TEST_OBS_PRESENCE_WITH_FILTER:
333 case TEST_GET_REQ_NON_WITH_VENDOR_HEADER_OPTIONS:
334 InitGetRequest(OC_LOW_QOS, 1);
336 case TEST_DISCOVER_DEV_REQ:
337 InitDeviceDiscovery();
345 return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION ;
349 OCStackApplicationResult DeviceDiscoveryReqCB (void* ctx, OCDoHandle handle,
350 OCClientResponse * clientResponse)
352 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
354 OC_LOG(INFO, TAG, "Callback Context for Device DISCOVER query recvd successfully");
359 //OC_LOG truncates the response as it is too long.
360 fprintf(stderr, "Discovery response: \n %s\n", clientResponse->resJSONPayload);
364 return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
370 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
371 std::ostringstream query;
372 query << "coap://" << coapServerIP << ":" << coapServerPort << OC_PRESENCE_URI;
373 if(TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTER)
375 query << "?rt=core.light";
377 return (InvokeOCDoResource(query, OC_REST_PRESENCE, OC_LOW_QOS, presenceCB, NULL, 0));
380 int InitGetRequestToUnavailableResource()
382 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
383 std::ostringstream query;
384 query << "coap://" << coapServerIP << ":" << coapServerPort << "/SomeUnknownResource";
385 return (InvokeOCDoResource(query, OC_REST_GET, OC_LOW_QOS, getReqCB, NULL, 0));
388 int InitObserveRequest(OCQualityOfService qos)
390 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
391 std::ostringstream query;
392 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
393 return (InvokeOCDoResource(query, OC_REST_OBSERVE, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, obsReqCB, NULL, 0));
398 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
399 std::ostringstream query;
400 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
401 return (InvokeOCDoResource(query, OC_REST_PUT, OC_LOW_QOS, putReqCB, NULL, 0));
404 int InitPostRequest(OCQualityOfService qos)
406 OCStackResult result;
407 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
408 std::ostringstream query;
409 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
411 // First POST operation (to create an Light instance)
412 result = InvokeOCDoResource(query, OC_REST_POST,
413 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
415 if (OC_STACK_OK != result)
417 // Error can happen if for example, network connectivity is down
418 OC_LOG(INFO, TAG, "First POST call did not succeed");
421 // Second POST operation (to create an Light instance)
422 result = InvokeOCDoResource(query, OC_REST_POST,
423 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
425 if (OC_STACK_OK != result)
427 OC_LOG(INFO, TAG, "Second POST call did not succeed");
430 // This POST operation will update the original resourced /a/light
431 return (InvokeOCDoResource(query, OC_REST_POST,
432 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
433 postReqCB, NULL, 0));
436 void* RequestDeleteDeathResourceTask(void* myqos)
438 sleep (30); //long enough to give the server time to finish deleting the resource.
439 std::ostringstream query;
440 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
442 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
444 // Second DELETE operation to delete the resource that might have been removed already.
445 OCQualityOfService qos;
451 OCStackResult result = InvokeOCDoResource(query, OC_REST_DELETE,
453 deleteReqCB, NULL, 0);
455 if (OC_STACK_OK != result)
457 OC_LOG(INFO, TAG, "Second DELETE call did not succeed");
463 int InitDeleteRequest(OCQualityOfService qos)
465 OCStackResult result;
466 std::ostringstream query;
467 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
469 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
471 // First DELETE operation
472 result = InvokeOCDoResource(query, OC_REST_DELETE,
474 deleteReqCB, NULL, 0);
475 if (OC_STACK_OK != result)
477 // Error can happen if for example, network connectivity is down
478 OC_LOG(INFO, TAG, "First DELETE call did not succeed");
482 //Create a thread to delete this resource again
484 pthread_create (&threadId, NULL, RequestDeleteDeathResourceTask, (void*)qos);
487 OC_LOG_V(INFO, TAG, "\n\nExit %s", __func__);
491 int InitGetRequest(OCQualityOfService qos, uint8_t withVendorSpecificHeaderOptions)
493 OCHeaderOption options[MAX_HEADER_OPTIONS];
495 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
496 std::ostringstream query;
497 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
499 if(withVendorSpecificHeaderOptions)
501 uint8_t option0[] = {1,2,3,4,5,6,7,8,9,10};
502 uint8_t option1[] = {11,12,13,14,15,16,17,18,19,20};
503 memset(options, 0, sizeof(OCHeaderOption) * MAX_HEADER_OPTIONS);
504 options[0].protocolID = OC_COAP_ID;
505 options[0].optionID = 2048;
506 memcpy(options[0].optionData, option0, sizeof(option0));
507 options[0].optionLength = 10;
508 options[1].protocolID = OC_COAP_ID;
509 options[1].optionID = 3000;
510 memcpy(options[1].optionData, option1, sizeof(option1));
511 options[1].optionLength = 10;
513 if(withVendorSpecificHeaderOptions)
515 return (InvokeOCDoResource(query, OC_REST_GET, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, getReqCB, options, 2));
519 return (InvokeOCDoResource(query, OC_REST_GET, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, getReqCB, NULL, 0));
523 int InitDeviceDiscovery()
526 OCCallbackData cbData;
528 char szQueryUri[64] = { 0 };
530 cbData.cb = DeviceDiscoveryReqCB;
531 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
534 if(UNICAST_DISCOVERY)
536 strncpy(szQueryUri, TEST_APP_UNICAST_DEVICE_DISCOVERY_QUERY,
537 (strlen(TEST_APP_UNICAST_DEVICE_DISCOVERY_QUERY) + 1));
541 strncpy(szQueryUri, TEST_APP_MULTICAST_DEVICE_DISCOVERY_QUERY,
542 (strlen(TEST_APP_MULTICAST_DEVICE_DISCOVERY_QUERY) + 1));
545 ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, OC_LOW_QOS, &cbData, NULL, 0);
547 if (ret != OC_STACK_OK)
549 OC_LOG(ERROR, TAG, "OCStack device error");
558 OCCallbackData cbData;
560 /* Start a discovery query*/
561 char szQueryUri[64] = { 0 };
562 if (UNICAST_DISCOVERY)
564 strcpy(szQueryUri, TEST_APP_UNICAST_DISCOVERY_QUERY);
568 strcpy(szQueryUri, OC_WELL_KNOWN_QUERY);
570 cbData.cb = discoveryReqCB;
571 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
573 ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, OC_LOW_QOS, &cbData, NULL, 0);
574 if (ret != OC_STACK_OK)
576 OC_LOG(ERROR, TAG, "OCStack resource error");
581 int main(int argc, char* argv[]) {
582 uint8_t addr[20] = {0};
583 uint8_t* paddr = NULL;
584 uint16_t port = USE_RANDOM_PORT;
585 uint8_t ifname[] = "eth0";
588 while ((opt = getopt(argc, argv, "u:t:")) != -1)
593 UNICAST_DISCOVERY = atoi(optarg);
596 TEST_CASE = atoi(optarg);
604 if ((UNICAST_DISCOVERY != 0 && UNICAST_DISCOVERY != 1) ||
605 (TEST_CASE < TEST_DISCOVER_REQ || TEST_CASE >= MAX_TESTS) )
612 /*Get Ip address on defined interface and initialize coap on it with random port number
613 * this port number will be used as a source port in all coap communications*/
614 if ( OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr,
615 sizeof(addr)) == ERR_SUCCESS)
617 OC_LOG_V(INFO, TAG, "Starting occlient on address %s",addr);
621 /* Initialize OCStack*/
622 if (OCInit((char *) paddr, port, OC_CLIENT) != OC_STACK_OK) {
623 OC_LOG(ERROR, TAG, "OCStack init error");
629 // Break from loop with Ctrl+C
630 OC_LOG(INFO, TAG, "Entering occlient main loop...");
631 signal(SIGINT, handleSigInt);
634 if (OCProcess() != OC_STACK_OK) {
635 OC_LOG(ERROR, TAG, "OCStack process error");
641 OC_LOG(INFO, TAG, "Exiting occlient main loop...");
643 if (OCStop() != OC_STACK_OK) {
644 OC_LOG(ERROR, TAG, "OCStack stop error");
650 std::string getIPAddrTBServer(OCClientResponse * clientResponse) {
651 if(!clientResponse) return "";
652 if(!clientResponse->addr) return "";
653 uint8_t a, b, c, d = 0;
654 if(0 != OCDevAddrToIPv4Addr(clientResponse->addr, &a, &b, &c, &d) ) return "";
656 char ipaddr[16] = {'\0'};
657 snprintf(ipaddr, sizeof(ipaddr), "%d.%d.%d.%d", a,b,c,d); // ostringstream not working correctly here, hence snprintf
658 return std::string (ipaddr);
661 std::string getPortTBServer(OCClientResponse * clientResponse){
662 if(!clientResponse) return "";
663 if(!clientResponse->addr) return "";
665 if(0 != OCDevAddrToPort(clientResponse->addr, &p) ) return "";
666 std::ostringstream ss;
671 std::string getQueryStrForGetPut(OCClientResponse * clientResponse){
676 void parseClientResponse(OCClientResponse * clientResponse){
677 coapServerIP = getIPAddrTBServer(clientResponse);
678 coapServerPort = getPortTBServer(clientResponse);
679 coapServerResource = getQueryStrForGetPut(clientResponse);