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"\
90 " and cancel with Low QoS");
93 OC_LOG(INFO, TAG, "-t 12 : Discover Resources and Initiate Nonconfirmable presence");
94 OC_LOG(INFO, TAG, "-t 13 : Discover Resources and Initiate Nonconfirmable presence with "\
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 multicast presence.");
101 OC_LOG(INFO, TAG, "-t 16 : Discover Resources and Initiate Nonconfirmable Observe Requests "\
102 "then cancel immediately with High QOS");
103 OC_LOG(INFO, TAG, "-t 17 : Discover Resources and Initiate Nonconfirmable Get Request and "\
104 "add vendor specific header options");
105 OC_LOG(INFO, TAG, "-t 18 : Discover Devices");
108 OCStackResult InvokeOCDoResource(std::ostringstream &query,
110 OCQualityOfService qos,
111 OCClientResponseHandler cb,
112 OCHeaderOption * options,
116 OCCallbackData cbData;
120 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
123 ret = OCDoResource(&handle, method, query.str().c_str(), 0,
124 (method == OC_REST_PUT) ? putPayload.c_str() : NULL,
125 (OC_CONNTYPE), qos, &cbData, options, numOptions);
127 if (ret != OC_STACK_OK)
129 OC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method);
131 else if (method == OC_REST_OBSERVE || method == OC_REST_OBSERVE_ALL)
133 gObserveDoHandle = handle;
136 else if (method == OC_REST_PRESENCE)
138 gPresenceHandle = handle;
145 OCStackApplicationResult putReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
147 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
149 OC_LOG(INFO, TAG, "Callback Context for PUT recvd successfully");
154 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
155 OC_LOG_V(INFO, TAG, "JSON = %s =============> Put Response",
156 clientResponse->resJSONPayload);
160 OC_LOG_V(INFO, TAG, "putReqCB received Null clientResponse");
162 return OC_STACK_DELETE_TRANSACTION;
165 OCStackApplicationResult postReqCB(void *ctx, OCDoHandle handle, OCClientResponse *clientResponse)
167 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
169 OC_LOG(INFO, TAG, "Callback Context for POST recvd successfully");
174 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
175 OC_LOG_V(INFO, TAG, "JSON = %s =============> Post Response",
176 clientResponse->resJSONPayload);
180 OC_LOG_V(INFO, TAG, "postReqCB received Null clientResponse");
182 return OC_STACK_DELETE_TRANSACTION;
185 OCStackApplicationResult deleteReqCB(void *ctx,
186 OCDoHandle handle, OCClientResponse *clientResponse)
188 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
190 OC_LOG(INFO, TAG, "Callback Context for DELETE recvd successfully");
195 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
196 OC_LOG_V(INFO, TAG, "JSON = %s =============> Delete Response",
197 clientResponse->resJSONPayload);
201 OC_LOG_V(INFO, TAG, "deleteReqCB received Null clientResponse");
203 return OC_STACK_DELETE_TRANSACTION;
206 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
208 if(clientResponse == NULL)
210 OC_LOG(INFO, TAG, "getReqCB received NULL clientResponse");
211 return OC_STACK_DELETE_TRANSACTION;
214 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
216 OC_LOG(INFO, TAG, "Callback Context for GET query recvd successfully");
219 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
220 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
221 OC_LOG_V(INFO, TAG, "JSON = %s =============> Get Response", clientResponse->resJSONPayload);
223 if(clientResponse->rcvdVendorSpecificHeaderOptions &&
224 clientResponse->numRcvdVendorSpecificHeaderOptions)
226 OC_LOG (INFO, TAG, "Received vendor specific options");
228 OCHeaderOption * rcvdOptions = clientResponse->rcvdVendorSpecificHeaderOptions;
229 for( i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
231 if(((OCHeaderOption)rcvdOptions[i]).protocolID == OC_COAP_ID)
233 OC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with",
234 ((OCHeaderOption)rcvdOptions[i]).optionID );
236 OC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData,
237 MAX_HEADER_OPTION_DATA_LENGTH);
241 return OC_STACK_DELETE_TRANSACTION;
244 OCStackApplicationResult obsReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
246 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
248 OC_LOG(INFO, TAG, "Callback Context for OBS query recvd successfully");
253 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
254 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
255 OC_LOG_V(INFO, TAG, "Callback Context for OBSERVE notification recvd successfully %d",
256 gNumObserveNotifies);
257 OC_LOG_V(INFO, TAG, "JSON = %s =============> Obs Response",
258 clientResponse->resJSONPayload);
259 gNumObserveNotifies++;
260 if (gNumObserveNotifies == 15) //large number to test observing in DELETE case.
262 if(TEST_CASE == TEST_OBS_REQ_NON || TEST_CASE == TEST_OBS_REQ_CON)
264 if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
266 OC_LOG(ERROR, TAG, "Observe cancel error");
268 return OC_STACK_DELETE_TRANSACTION;
270 else if(TEST_CASE == TEST_OBS_REQ_NON_CANCEL_IMM)
272 if (OCCancel (gObserveDoHandle, OC_HIGH_QOS, NULL, 0) != OC_STACK_OK)
274 OC_LOG(ERROR, TAG, "Observe cancel error");
278 if(clientResponse->sequenceNumber == OC_OBSERVE_REGISTER)
280 OC_LOG(INFO, TAG, "This also serves as a registration confirmation");
282 else if(clientResponse->sequenceNumber == OC_OBSERVE_DEREGISTER)
284 OC_LOG(INFO, TAG, "This also serves as a deregistration confirmation");
285 return OC_STACK_DELETE_TRANSACTION;
287 else if(clientResponse->sequenceNumber == OC_OBSERVE_NO_OPTION)
289 OC_LOG(INFO, TAG, "This also tells you that registration/deregistration failed");
290 return OC_STACK_DELETE_TRANSACTION;
295 OC_LOG_V(INFO, TAG, "obsReqCB received Null clientResponse");
297 return OC_STACK_KEEP_TRANSACTION;
300 OCStackApplicationResult presenceCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
302 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
304 OC_LOG(INFO, TAG, "Callback Context for Presence recvd successfully");
309 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
310 OC_LOG_V(INFO, TAG, "NONCE NUMBER: %u", clientResponse->sequenceNumber);
311 OC_LOG_V(INFO, TAG, "Callback Context for Presence notification recvd successfully %d",
312 gNumPresenceNotifies);
313 OC_LOG_V(INFO, TAG, "JSON = %s =============> Presence Response",
314 clientResponse->resJSONPayload);
315 gNumPresenceNotifies++;
316 if (gNumPresenceNotifies == 20)
318 if (OCCancel(gPresenceHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
320 OC_LOG(ERROR, TAG, "Presence cancel error");
322 return OC_STACK_DELETE_TRANSACTION;
327 OC_LOG_V(INFO, TAG, "presenceCB received Null clientResponse");
329 return OC_STACK_KEEP_TRANSACTION;
333 // This is a function called back when a device is discovered
334 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
335 OCClientResponse * clientResponse)
337 uint8_t remoteIpAddr[4];
338 uint16_t remotePortNu;
340 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
342 OC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully");
347 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
349 OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr,
350 remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3);
351 OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu);
353 std::string connectionType = getConnectivityType (clientResponse->connType);
354 OC_LOG_V(INFO, TAG, "Discovered on %s", connectionType.c_str());
356 "Device =============> Discovered %s @ %d.%d.%d.%d:%d",
357 clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1],
358 remoteIpAddr[2], remoteIpAddr[3], remotePortNu);
360 parseClientResponse(clientResponse);
364 case TEST_GET_REQ_NON:
365 InitGetRequest(OC_LOW_QOS, 0);
367 case TEST_PUT_REQ_NON:
368 InitPutRequest(OC_LOW_QOS);
370 case TEST_POST_REQ_NON:
371 InitPostRequest(OC_LOW_QOS);
373 case TEST_DELETE_REQ_NON:
374 InitDeleteRequest(OC_LOW_QOS);
376 case TEST_OBS_REQ_NON:
377 case TEST_OBS_REQ_NON_CANCEL_IMM:
378 InitObserveRequest(OC_LOW_QOS);
380 case TEST_GET_UNAVAILABLE_RES_REQ_NON:
381 InitGetRequestToUnavailableResource(OC_LOW_QOS);
383 case TEST_GET_REQ_CON:
384 InitGetRequest(OC_HIGH_QOS, 0);
386 case TEST_POST_REQ_CON:
387 InitPostRequest(OC_HIGH_QOS);
389 case TEST_DELETE_REQ_CON:
390 InitDeleteRequest(OC_HIGH_QOS);
392 case TEST_OBS_REQ_CON:
393 InitObserveRequest(OC_HIGH_QOS);
396 case TEST_OBS_PRESENCE:
397 case TEST_OBS_PRESENCE_WITH_FILTER:
398 case TEST_OBS_PRESENCE_WITH_FILTERS:
399 case TEST_OBS_MULTICAST_PRESENCE:
403 case TEST_GET_REQ_NON_WITH_VENDOR_HEADER_OPTIONS:
404 InitGetRequest(OC_LOW_QOS, 1);
406 case TEST_DISCOVER_DEV_REQ:
407 InitDeviceDiscovery(OC_LOW_QOS);
416 OC_LOG_V(INFO, TAG, "discoveryReqCB received Null clientResponse");
418 return OC_STACK_KEEP_TRANSACTION;
421 OCStackApplicationResult DeviceDiscoveryReqCB (void* ctx, OCDoHandle handle,
422 OCClientResponse * clientResponse)
424 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
426 OC_LOG(INFO, TAG, "Callback Context for Device DISCOVER query recvd successfully");
431 //OC_LOG truncates the response as it is too long.
432 fprintf(stderr, "Discovery response: \n %s\n", clientResponse->resJSONPayload);
437 OC_LOG_V(INFO, TAG, "DeviceDiscoveryReqCB received Null clientResponse");
440 return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
446 OCStackResult result = OC_STACK_OK;
447 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
448 std::ostringstream query;
449 std::ostringstream querySuffix;
450 query << "coap://" << coapServerIP << ":" << coapServerPort << OC_PRESENCE_URI;
451 if(TEST_CASE == TEST_OBS_PRESENCE)
453 result = InvokeOCDoResource(query, OC_REST_PRESENCE, OC_LOW_QOS,
454 presenceCB, NULL, 0);
456 if(TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTER || TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTERS)
459 querySuffix << query.str() << "?rt=core.led";
460 result = InvokeOCDoResource(querySuffix, OC_REST_PRESENCE, OC_LOW_QOS,
461 presenceCB, NULL, 0);
463 if(TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTERS)
465 if(result == OC_STACK_OK)
468 querySuffix << query.str() << "?rt=core.fan";
469 result = InvokeOCDoResource(querySuffix, OC_REST_PRESENCE, OC_LOW_QOS,
470 presenceCB, NULL, 0);
473 if(TEST_CASE == TEST_OBS_MULTICAST_PRESENCE)
475 if(result == OC_STACK_OK)
477 std::ostringstream multicastPresenceQuery;
478 multicastPresenceQuery.str("");
479 multicastPresenceQuery << "coap://" << OC_MULTICAST_PREFIX << OC_PRESENCE_URI;
480 result = InvokeOCDoResource(multicastPresenceQuery, OC_REST_PRESENCE, OC_LOW_QOS,
481 presenceCB, NULL, 0);
488 int InitGetRequestToUnavailableResource(OCQualityOfService qos)
490 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
491 std::ostringstream query;
492 query << "coap://" << coapServerIP << ":" << coapServerPort << "/SomeUnknownResource";
493 return (InvokeOCDoResource(query, OC_REST_GET, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
497 int InitObserveRequest(OCQualityOfService qos)
499 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
500 std::ostringstream query;
501 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
502 return (InvokeOCDoResource(query,
503 OC_REST_OBSERVE, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, obsReqCB, NULL, 0));
506 int InitPutRequest(OCQualityOfService qos)
508 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
509 std::ostringstream query;
510 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
511 return (InvokeOCDoResource(query, OC_REST_PUT, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
515 int InitPostRequest(OCQualityOfService qos)
517 OCStackResult result;
518 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
519 std::ostringstream query;
520 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
522 // First POST operation (to create an Light instance)
523 result = InvokeOCDoResource(query, OC_REST_POST,
524 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
526 if (OC_STACK_OK != result)
528 // Error can happen if for example, network connectivity is down
529 OC_LOG(INFO, TAG, "First POST call did not succeed");
532 // Second POST operation (to create an Light instance)
533 result = InvokeOCDoResource(query, OC_REST_POST,
534 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
536 if (OC_STACK_OK != result)
538 OC_LOG(INFO, TAG, "Second POST call did not succeed");
541 // This POST operation will update the original resourced /a/light
542 return (InvokeOCDoResource(query, OC_REST_POST,
543 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
544 postReqCB, NULL, 0));
547 void* RequestDeleteDeathResourceTask(void* myqos)
549 sleep (30);//long enough to give the server time to finish deleting the resource.
550 std::ostringstream query;
551 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
553 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
555 // Second DELETE operation to delete the resource that might have been removed already.
556 OCQualityOfService qos;
566 OCStackResult result = InvokeOCDoResource(query, OC_REST_DELETE,
568 deleteReqCB, NULL, 0);
570 if (OC_STACK_OK != result)
572 OC_LOG(INFO, TAG, "Second DELETE call did not succeed");
578 int InitDeleteRequest(OCQualityOfService qos)
580 OCStackResult result;
581 std::ostringstream query;
582 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
584 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
586 // First DELETE operation
587 result = InvokeOCDoResource(query, OC_REST_DELETE,
589 deleteReqCB, NULL, 0);
590 if (OC_STACK_OK != result)
592 // Error can happen if for example, network connectivity is down
593 OC_LOG(INFO, TAG, "First DELETE call did not succeed");
597 //Create a thread to delete this resource again
599 pthread_create (&threadId, NULL, RequestDeleteDeathResourceTask, (void*)qos);
602 OC_LOG_V(INFO, TAG, "\n\nExit %s", __func__);
606 int InitGetRequest(OCQualityOfService qos, uint8_t withVendorSpecificHeaderOptions)
608 OCHeaderOption options[MAX_HEADER_OPTIONS];
610 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
611 std::ostringstream query;
612 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
614 if (withVendorSpecificHeaderOptions)
616 uint8_t option0[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
617 uint8_t option1[] = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
618 memset(options, 0, sizeof(OCHeaderOption) * MAX_HEADER_OPTIONS);
619 options[0].protocolID = OC_COAP_ID;
620 options[0].optionID = 2048;
621 memcpy(options[0].optionData, option0, sizeof(option0));
622 options[0].optionLength = 10;
623 options[1].protocolID = OC_COAP_ID;
624 options[1].optionID = 3000;
625 memcpy(options[1].optionData, option1, sizeof(option1));
626 options[1].optionLength = 10;
628 if (withVendorSpecificHeaderOptions)
630 return (InvokeOCDoResource(query, OC_REST_GET,
631 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, options, 2));
635 return (InvokeOCDoResource(query, OC_REST_GET,
636 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, NULL, 0));
640 int InitDeviceDiscovery(OCQualityOfService qos)
643 OCCallbackData cbData;
644 char szQueryUri[64] = { 0 };
646 cbData.cb = DeviceDiscoveryReqCB;
647 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
650 if(UNICAST_DISCOVERY)
652 snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_DEVICE_DISCOVERY_QUERY, ipv4addr);
656 strncpy(szQueryUri, MULTICAST_DEVICE_DISCOVERY_QUERY,
657 (strlen(MULTICAST_DEVICE_DISCOVERY_QUERY) + 1));
660 if(UNICAST_DISCOVERY)
662 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, OC_CONNTYPE,
663 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
667 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, (OC_ALL),
668 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
671 if (ret != OC_STACK_OK)
673 OC_LOG(ERROR, TAG, "OCStack device error");
679 int InitDiscovery(OCQualityOfService qos)
682 OCCallbackData cbData;
683 /* Start a discovery query*/
684 char szQueryUri[64] = { 0 };
686 if (UNICAST_DISCOVERY)
688 snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_DISCOVERY_QUERY, ipv4addr);
692 strcpy(szQueryUri, MULTICAST_RESOURCE_DISCOVERY_QUERY);
695 cbData.cb = discoveryReqCB;
696 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
698 if(UNICAST_DISCOVERY)
700 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, OC_CONNTYPE,
701 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
705 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, (OC_ALL),
706 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, &cbData, NULL, 0);
708 if (ret != OC_STACK_OK)
710 OC_LOG(ERROR, TAG, "OCStack resource error");
715 int main(int argc, char* argv[])
719 while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
724 UNICAST_DISCOVERY = atoi(optarg);
727 TEST_CASE = atoi(optarg);
730 OC_CONNTYPE = OCConnectivityType(atoi(optarg));
738 if ((UNICAST_DISCOVERY != 0 && UNICAST_DISCOVERY != 1) ||
739 (TEST_CASE < TEST_DISCOVER_REQ || TEST_CASE >= MAX_TESTS) )
745 /* Initialize OCStack*/
746 if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK)
748 OC_LOG(ERROR, TAG, "OCStack init error");
751 if (UNICAST_DISCOVERY)
753 printf("Enter IPv4 address of the Server hosting resource (Ex: 192.168.0.15)\n");
754 if (fgets(ipv4addr, IPV4_ADDR_SIZE, stdin))
756 //Strip newline char from ipv4addr
757 StripNewLineChar(ipv4addr);
761 OC_LOG(ERROR, TAG, "!! Bad input for IPV4 address. !!");
762 return OC_STACK_INVALID_PARAM;
766 if(UNICAST_DISCOVERY == 0 && TEST_CASE == TEST_DISCOVER_DEV_REQ)
768 InitDeviceDiscovery(OC_LOW_QOS);
772 InitDiscovery(OC_LOW_QOS);
775 // Break from loop with Ctrl+C
776 OC_LOG(INFO, TAG, "Entering occlient main loop...");
777 signal(SIGINT, handleSigInt);
781 if (OCProcess() != OC_STACK_OK)
783 OC_LOG(ERROR, TAG, "OCStack process error");
789 OC_LOG(INFO, TAG, "Exiting occlient main loop...");
791 if (OCStop() != OC_STACK_OK)
793 OC_LOG(ERROR, TAG, "OCStack stop error");
799 std::string getIPAddrTBServer(OCClientResponse * clientResponse)
805 if (!clientResponse->addr)
809 uint8_t a, b, c, d = 0;
810 if (0 != OCDevAddrToIPv4Addr(clientResponse->addr, &a, &b, &c, &d))
815 char ipaddr[16] = {'\0'};
816 // ostringstream not working correctly here, hence snprintf
817 snprintf(ipaddr, sizeof(ipaddr), "%d.%d.%d.%d", a,b,c,d);
818 return std::string (ipaddr);
821 std::string getPortTBServer(OCClientResponse * clientResponse)
827 if (!clientResponse->addr)
832 if (0 != OCDevAddrToPort(clientResponse->addr, &p))
836 std::ostringstream ss;
841 std::string getConnectivityType (OCConnectivityType connType)
858 return "Incorrect connectivity";
862 std::string getQueryStrForGetPut(OCClientResponse * clientResponse)
868 void parseClientResponse(OCClientResponse * clientResponse)
870 coapServerIP = getIPAddrTBServer(clientResponse);
871 coapServerPort = getPortTBServer(clientResponse);
872 coapServerResource = getQueryStrForGetPut(clientResponse);