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";
38 static const char * MULTICAST_DEVICE_DISCOVERY_QUERY = "/oc/core/d";
40 static const char * TEST_APP_MULTICAST_DEVICE_DISCOVERY_QUERY = "coap://224.0.1.187:5683/oc/core/d";
44 static const char * MULTICAST_RESOURCE_DISCOVERY_QUERY = "/oc/core";
45 //The following variable determines the interface (wifi, ethernet etc.)
46 //to be used for sending unicast messages. Default set to WIFI.
47 static OCConnectivityType OC_CONNTYPE = OC_WIFI;
49 static std::string putPayload = "{\"state\":\"on\",\"power\":5}";
50 static std::string coapServerIP = "255.255.255.255";
51 static std::string coapServerPort = "5683";
52 static std::string coapServerResource = "/a/light";
54 // The handle for the observe registration
55 OCDoHandle gObserveDoHandle;
57 // The handle for observe registration
58 OCDoHandle gPresenceHandle;
60 // After this crosses a threshold client deregisters for further notifications
61 int gNumObserveNotifies = 0;
64 int gNumPresenceNotifies = 0;
68 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
69 void handleSigInt(int signum) {
70 if (signum == SIGINT) {
75 static void PrintUsage()
78 OC_LOG(INFO, TAG, "Usage : occlient -u <0|1> -t <1|2|3|4|5|6|7> -c <0|1>");
80 OC_LOG(INFO, TAG, "Usage : occlient -u <0|1> -t <1|2|3|4|5|6|7>");
82 OC_LOG(INFO, TAG, "-u <0|1> : Perform multicast/unicast discovery of resources");
84 OC_LOG(INFO, TAG, "-c <0|1> : Send unicast messages over Ethernet or WIFI");
86 OC_LOG(INFO, TAG, "-t 1 : Discover Resources");
87 OC_LOG(INFO, TAG, "-t 2 : Discover Resources and Initiate Nonconfirmable Get Request");
88 OC_LOG(INFO, TAG, "-t 3 : Discover Resources and Initiate Nonconfirmable Put Requests");
89 OC_LOG(INFO, TAG, "-t 4 : Discover Resources and Initiate Nonconfirmable Post Requests");
90 OC_LOG(INFO, TAG, "-t 5 : Discover Resources and Initiate Nonconfirmable Delete Requests");
91 OC_LOG(INFO, TAG, "-t 6 : Discover Resources and Initiate Nonconfirmable Observe Requests");
92 OC_LOG(INFO, TAG, "-t 7 : Discover Resources and Initiate Nonconfirmable Get Request for a resource which is unavailable");
94 OC_LOG(INFO, TAG, "-t 8 : Discover Resources and Initiate Confirmable Get Request");
95 OC_LOG(INFO, TAG, "-t 9 : Discover Resources and Initiate Confirmable Post Request");
96 OC_LOG(INFO, TAG, "-t 10 : Discover Resources and Initiate Confirmable Delete Requests");
97 OC_LOG(INFO, TAG, "-t 11 : Discover Resources and Initiate Confirmable Observe Requests");
100 OC_LOG(INFO, TAG, "-t 12 : Discover Resources and Initiate Nonconfirmable presence");
101 OC_LOG(INFO, TAG, "-t 13 : Discover Resources and Initiate Nonconfirmable presence with "\
103 OC_LOG(INFO, TAG, "-t 14 : Discover Resources and Initiate Nonconfirmable presence with "\
107 OC_LOG(INFO, TAG, "-t 15 : Discover Resources and Initiate Nonconfirmable Observe Requests "\
108 "then cancel immediately");
109 OC_LOG(INFO, TAG, "-t 16 : Discover Resources and Initiate Nonconfirmable Get Request and "\
110 "add vendor specific header options");
111 OC_LOG(INFO, TAG, "-t 17 : Discover Devices");
114 OCStackResult InvokeOCDoResource(std::ostringstream &query,
115 OCMethod method, OCQualityOfService qos,
116 OCClientResponseHandler cb, OCHeaderOption * options, uint8_t numOptions)
119 OCCallbackData cbData;
123 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
127 ret = OCDoResource(&handle, method, query.str().c_str(), 0,
128 (method == OC_REST_PUT) ? putPayload.c_str() : NULL,
129 (OC_CONNTYPE), qos, &cbData, options, numOptions);
131 ret = OCDoResource(&handle, method, query.str().c_str(), 0,
132 (method == OC_REST_PUT) ? putPayload.c_str() : NULL,
133 qos, &cbData, options, numOptions);
136 if (ret != OC_STACK_OK)
138 OC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method);
140 else if (method == OC_REST_OBSERVE || method == OC_REST_OBSERVE_ALL)
142 gObserveDoHandle = handle;
145 else if (method == OC_REST_PRESENCE)
147 gPresenceHandle = handle;
154 OCStackApplicationResult putReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) {
155 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
157 OC_LOG(INFO, TAG, "Callback Context for PUT recvd successfully");
162 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
163 OC_LOG_V(INFO, TAG, "JSON = %s =============> Put Response", clientResponse->resJSONPayload);
165 return OC_STACK_DELETE_TRANSACTION;
168 OCStackApplicationResult postReqCB(void *ctx, OCDoHandle handle, OCClientResponse *clientResponse)
170 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
172 OC_LOG(INFO, TAG, "Callback Context for POST recvd successfully");
177 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
178 OC_LOG_V(INFO, TAG, "JSON = %s =============> Post Response", clientResponse->resJSONPayload);
180 return OC_STACK_DELETE_TRANSACTION;
183 OCStackApplicationResult deleteReqCB(void *ctx, OCDoHandle handle, OCClientResponse *clientResponse)
185 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
187 OC_LOG(INFO, TAG, "Callback Context for DELETE recvd successfully");
192 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
193 OC_LOG_V(INFO, TAG, "JSON = %s =============> Delete Response", clientResponse->resJSONPayload);
195 return OC_STACK_DELETE_TRANSACTION;
198 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
200 if(clientResponse == NULL)
202 OC_LOG(INFO, TAG, "The clientResponse is NULL");
203 return OC_STACK_DELETE_TRANSACTION;
205 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
207 OC_LOG(INFO, TAG, "Callback Context for GET query recvd successfully");
210 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
211 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
212 OC_LOG_V(INFO, TAG, "JSON = %s =============> Get Response", clientResponse->resJSONPayload);
214 if(clientResponse->rcvdVendorSpecificHeaderOptions &&
215 clientResponse->numRcvdVendorSpecificHeaderOptions)
217 OC_LOG (INFO, TAG, "Received vendor specific options");
219 OCHeaderOption * rcvdOptions = clientResponse->rcvdVendorSpecificHeaderOptions;
220 for( i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
222 if(((OCHeaderOption)rcvdOptions[i]).protocolID == OC_COAP_ID)
224 OC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with",
225 ((OCHeaderOption)rcvdOptions[i]).optionID );
226 OC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData,
227 ((OCHeaderOption)rcvdOptions[i]).optionLength);
231 return OC_STACK_DELETE_TRANSACTION;
234 OCStackApplicationResult obsReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) {
235 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
237 OC_LOG(INFO, TAG, "Callback Context for OBS query recvd successfully");
242 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
243 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
244 OC_LOG_V(INFO, TAG, "Callback Context for OBSERVE notification recvd successfully %d", gNumObserveNotifies);
245 OC_LOG_V(INFO, TAG, "JSON = %s =============> Obs Response", clientResponse->resJSONPayload);
246 gNumObserveNotifies++;
247 if (gNumObserveNotifies == 3) //large number to test observing in DELETE case.
249 if(TEST_CASE == TEST_OBS_REQ_NON || TEST_CASE == TEST_OBS_REQ_CON){
250 if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK){
251 OC_LOG(ERROR, TAG, "Observe cancel error");
253 return OC_STACK_DELETE_TRANSACTION;
254 }else if(TEST_CASE == TEST_OBS_REQ_NON_CANCEL_IMM){
255 if (OCCancel (gObserveDoHandle, OC_HIGH_QOS, NULL, 0) != OC_STACK_OK){
256 OC_LOG(ERROR, TAG, "Observe cancel error");
260 if(clientResponse->sequenceNumber == OC_OBSERVE_REGISTER){
261 OC_LOG(INFO, TAG, "This also serves as a registration confirmation");
262 }else if(clientResponse->sequenceNumber == OC_OBSERVE_DEREGISTER){
263 OC_LOG(INFO, TAG, "This also serves as a deregistration confirmation");
264 return OC_STACK_DELETE_TRANSACTION;
265 }else if(clientResponse->sequenceNumber == OC_OBSERVE_NO_OPTION){
266 OC_LOG(INFO, TAG, "This also tells you that registration/deregistration failed");
267 return OC_STACK_DELETE_TRANSACTION;
270 return OC_STACK_KEEP_TRANSACTION;
273 OCStackApplicationResult presenceCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) {
274 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
276 OC_LOG(INFO, TAG, "Callback Context for Presence recvd successfully");
281 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
282 OC_LOG_V(INFO, TAG, "NONCE NUMBER: %u", clientResponse->sequenceNumber);
283 OC_LOG_V(INFO, TAG, "Callback Context for Presence notification recvd successfully %d", gNumPresenceNotifies);
284 OC_LOG_V(INFO, TAG, "JSON = %s =============> Presence Response", clientResponse->resJSONPayload);
285 gNumPresenceNotifies++;
286 if (gNumPresenceNotifies == 20)
288 if (OCCancel (gPresenceHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK){
289 OC_LOG(ERROR, TAG, "Presence cancel error");
291 return OC_STACK_DELETE_TRANSACTION;
294 return OC_STACK_KEEP_TRANSACTION;
298 // This is a function called back when a device is discovered
299 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
300 OCClientResponse * clientResponse) {
301 uint8_t remoteIpAddr[4];
302 uint16_t remotePortNu;
304 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
306 OC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully");
311 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
313 OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr,
314 remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3);
315 OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu);
318 std::string connectionType = getConnectivityType (clientResponse->connType);
319 OC_LOG_V(INFO, TAG, "Discovered on %s", connectionType.c_str());
322 "Device =============> Discovered %s @ %d.%d.%d.%d:%d",
323 clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1],
324 remoteIpAddr[2], remoteIpAddr[3], remotePortNu);
326 parseClientResponse(clientResponse);
330 case TEST_GET_REQ_NON:
331 InitGetRequest(OC_LOW_QOS, 0);
333 case TEST_PUT_REQ_NON:
336 case TEST_POST_REQ_NON:
337 InitPostRequest(OC_LOW_QOS);
339 case TEST_DELETE_REQ_NON:
340 InitDeleteRequest(OC_LOW_QOS);
342 case TEST_OBS_REQ_NON:
343 case TEST_OBS_REQ_NON_CANCEL_IMM:
344 InitObserveRequest(OC_LOW_QOS);
346 case TEST_GET_UNAVAILABLE_RES_REQ_NON:
347 InitGetRequestToUnavailableResource();
349 case TEST_GET_REQ_CON:
350 InitGetRequest(OC_HIGH_QOS, 0);
352 case TEST_POST_REQ_CON:
353 InitPostRequest(OC_HIGH_QOS);
355 case TEST_DELETE_REQ_CON:
356 InitDeleteRequest(OC_HIGH_QOS);
358 case TEST_OBS_REQ_CON:
359 InitObserveRequest(OC_HIGH_QOS);
362 case TEST_OBS_PRESENCE:
363 case TEST_OBS_PRESENCE_WITH_FILTER:
364 case TEST_OBS_PRESENCE_WITH_FILTERS:
368 case TEST_GET_REQ_NON_WITH_VENDOR_HEADER_OPTIONS:
369 InitGetRequest(OC_LOW_QOS, 1);
371 case TEST_DISCOVER_DEV_REQ:
372 InitDeviceDiscovery();
380 return OC_STACK_KEEP_TRANSACTION;
382 return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION ;
387 OCStackApplicationResult DeviceDiscoveryReqCB (void* ctx, OCDoHandle handle,
388 OCClientResponse * clientResponse)
390 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
392 OC_LOG(INFO, TAG, "Callback Context for Device DISCOVER query recvd successfully");
397 //OC_LOG truncates the response as it is too long.
398 fprintf(stderr, "Discovery response: \n %s\n", clientResponse->resJSONPayload);
402 return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
408 OCStackResult result = OC_STACK_OK;
409 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
410 std::ostringstream query;
411 std::ostringstream querySuffix;
412 query << "coap://" << coapServerIP << ":" << coapServerPort << OC_PRESENCE_URI;
413 if(TEST_CASE == TEST_OBS_PRESENCE)
415 result = InvokeOCDoResource(query, OC_REST_PRESENCE, OC_LOW_QOS,
416 presenceCB, NULL, 0);
418 if(TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTER || TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTERS)
421 querySuffix << query.str() << "?rt=core.light";
422 result = InvokeOCDoResource(querySuffix, OC_REST_PRESENCE, OC_LOW_QOS,
423 presenceCB, NULL, 0);
425 if(TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTERS)
427 if(result == OC_STACK_OK)
430 querySuffix << query.str() << "?rt=core.fan";
431 result = InvokeOCDoResource(querySuffix, OC_REST_PRESENCE, OC_LOW_QOS,
432 presenceCB, NULL, 0);
439 int InitGetRequestToUnavailableResource()
441 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
442 std::ostringstream query;
443 query << "coap://" << coapServerIP << ":" << coapServerPort << "/SomeUnknownResource";
444 return (InvokeOCDoResource(query, OC_REST_GET, OC_LOW_QOS, getReqCB, NULL, 0));
447 int InitObserveRequest(OCQualityOfService qos)
449 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
450 std::ostringstream query;
451 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
452 return (InvokeOCDoResource(query, OC_REST_OBSERVE, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, obsReqCB, NULL, 0));
457 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
458 std::ostringstream query;
459 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
460 return (InvokeOCDoResource(query, OC_REST_PUT, OC_LOW_QOS, putReqCB, NULL, 0));
463 int InitPostRequest(OCQualityOfService qos)
465 OCStackResult result;
466 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
467 std::ostringstream query;
468 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
470 // First POST operation (to create an Light instance)
471 result = InvokeOCDoResource(query, OC_REST_POST,
472 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
474 if (OC_STACK_OK != result)
476 // Error can happen if for example, network connectivity is down
477 OC_LOG(INFO, TAG, "First POST call did not succeed");
480 // Second POST operation (to create an Light instance)
481 result = InvokeOCDoResource(query, OC_REST_POST,
482 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
484 if (OC_STACK_OK != result)
486 OC_LOG(INFO, TAG, "Second POST call did not succeed");
489 // This POST operation will update the original resourced /a/light
490 return (InvokeOCDoResource(query, OC_REST_POST,
491 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
492 postReqCB, NULL, 0));
495 void* RequestDeleteDeathResourceTask(void* myqos)
497 sleep (30); //long enough to give the server time to finish deleting the resource.
498 std::ostringstream query;
499 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
501 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
503 // Second DELETE operation to delete the resource that might have been removed already.
504 OCQualityOfService qos;
510 OCStackResult result = InvokeOCDoResource(query, OC_REST_DELETE,
512 deleteReqCB, NULL, 0);
514 if (OC_STACK_OK != result)
516 OC_LOG(INFO, TAG, "Second DELETE call did not succeed");
522 int InitDeleteRequest(OCQualityOfService qos)
524 OCStackResult result;
525 std::ostringstream query;
526 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
528 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
530 // First DELETE operation
531 result = InvokeOCDoResource(query, OC_REST_DELETE,
533 deleteReqCB, NULL, 0);
534 if (OC_STACK_OK != result)
536 // Error can happen if for example, network connectivity is down
537 OC_LOG(INFO, TAG, "First DELETE call did not succeed");
541 //Create a thread to delete this resource again
543 pthread_create (&threadId, NULL, RequestDeleteDeathResourceTask, (void*)qos);
546 OC_LOG_V(INFO, TAG, "\n\nExit %s", __func__);
550 int InitGetRequest(OCQualityOfService qos, uint8_t withVendorSpecificHeaderOptions)
552 OCHeaderOption options[MAX_HEADER_OPTIONS];
554 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
555 std::ostringstream query;
556 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
558 if(withVendorSpecificHeaderOptions)
560 uint8_t option0[] = {1,2,3,4,5,6,7,8,9,10};
561 uint8_t option1[] = {11,12,13,14,15,16,17,18,19,20};
562 memset(options, 0, sizeof(OCHeaderOption) * MAX_HEADER_OPTIONS);
563 options[0].protocolID = OC_COAP_ID;
564 options[0].optionID = 2048;
565 memcpy(options[0].optionData, option0, sizeof(option0));
566 options[0].optionLength = 10;
567 options[1].protocolID = OC_COAP_ID;
568 options[1].optionID = 3000;
569 memcpy(options[1].optionData, option1, sizeof(option1));
570 options[1].optionLength = 10;
572 if(withVendorSpecificHeaderOptions)
574 return (InvokeOCDoResource(query, OC_REST_GET, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, getReqCB, options, 2));
578 return (InvokeOCDoResource(query, OC_REST_GET, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, getReqCB, NULL, 0));
582 int InitDeviceDiscovery()
585 OCCallbackData cbData;
587 char szQueryUri[64] = { 0 };
589 cbData.cb = DeviceDiscoveryReqCB;
590 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
593 if(UNICAST_DISCOVERY)
595 strncpy(szQueryUri, TEST_APP_UNICAST_DEVICE_DISCOVERY_QUERY,
596 (strlen(TEST_APP_UNICAST_DEVICE_DISCOVERY_QUERY) + 1));
602 strncpy(szQueryUri, MULTICAST_DEVICE_DISCOVERY_QUERY,
603 (strlen(MULTICAST_DEVICE_DISCOVERY_QUERY) + 1));
606 strncpy(szQueryUri, TEST_APP_MULTICAST_DEVICE_DISCOVERY_QUERY,
607 (strlen(TEST_APP_MULTICAST_DEVICE_DISCOVERY_QUERY) + 1));
612 if(UNICAST_DISCOVERY)
614 ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, OC_CONNTYPE,
615 OC_LOW_QOS, &cbData, NULL, 0);
619 ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, (OC_ALL),
620 OC_LOW_QOS, &cbData, NULL, 0);
623 ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, OC_LOW_QOS, &cbData, NULL, 0);
626 if (ret != OC_STACK_OK)
628 OC_LOG(ERROR, TAG, "OCStack device error");
637 OCCallbackData cbData;
639 /* Start a discovery query*/
640 char szQueryUri[64] = { 0 };
642 if (UNICAST_DISCOVERY)
644 strcpy(szQueryUri, TEST_APP_UNICAST_DISCOVERY_QUERY);
649 strcpy(szQueryUri, MULTICAST_RESOURCE_DISCOVERY_QUERY);
651 strcpy(szQueryUri, OC_WELL_KNOWN_QUERY);
655 cbData.cb = discoveryReqCB;
656 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
659 if(UNICAST_DISCOVERY)
661 ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, OC_CONNTYPE,
662 OC_LOW_QOS, &cbData, NULL, 0);
666 ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, (OC_ALL),
667 OC_LOW_QOS, &cbData, NULL, 0);
670 ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, OC_LOW_QOS, &cbData, NULL, 0);
672 if (ret != OC_STACK_OK)
674 OC_LOG(ERROR, TAG, "OCStack resource error");
679 int main(int argc, char* argv[]) {
680 uint8_t addr[20] = {0};
681 uint8_t* paddr = NULL;
682 uint16_t port = USE_RANDOM_PORT;
683 uint8_t ifname[] = "eth0";
687 while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
689 while ((opt = getopt(argc, argv, "u:t:")) != -1)
695 UNICAST_DISCOVERY = atoi(optarg);
698 TEST_CASE = atoi(optarg);
702 OC_CONNTYPE = OCConnectivityType(atoi(optarg));
711 if ((UNICAST_DISCOVERY != 0 && UNICAST_DISCOVERY != 1) ||
712 (TEST_CASE < TEST_DISCOVER_REQ || TEST_CASE >= MAX_TESTS) )
719 /*Get Ip address on defined interface and initialize coap on it with random port number
720 * this port number will be used as a source port in all coap communications*/
721 if ( OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr,
722 sizeof(addr)) == ERR_SUCCESS)
724 OC_LOG_V(INFO, TAG, "Starting occlient on address %s",addr);
728 /* Initialize OCStack*/
729 if (OCInit((char *) paddr, port, OC_CLIENT) != OC_STACK_OK) {
730 OC_LOG(ERROR, TAG, "OCStack init error");
734 if(UNICAST_DISCOVERY == 0 && TEST_CASE == TEST_DISCOVER_DEV_REQ)
736 InitDeviceDiscovery();
743 // Break from loop with Ctrl+C
744 OC_LOG(INFO, TAG, "Entering occlient main loop...");
745 signal(SIGINT, handleSigInt);
748 if (OCProcess() != OC_STACK_OK) {
749 OC_LOG(ERROR, TAG, "OCStack process error");
755 OC_LOG(INFO, TAG, "Exiting occlient main loop...");
757 if (OCStop() != OC_STACK_OK) {
758 OC_LOG(ERROR, TAG, "OCStack stop error");
764 std::string getIPAddrTBServer(OCClientResponse * clientResponse) {
765 if(!clientResponse) return "";
766 if(!clientResponse->addr) return "";
767 uint8_t a, b, c, d = 0;
768 if(0 != OCDevAddrToIPv4Addr(clientResponse->addr, &a, &b, &c, &d) ) return "";
770 char ipaddr[16] = {'\0'};
771 snprintf(ipaddr, sizeof(ipaddr), "%d.%d.%d.%d", a,b,c,d); // ostringstream not working correctly here, hence snprintf
772 return std::string (ipaddr);
775 std::string getPortTBServer(OCClientResponse * clientResponse){
776 if(!clientResponse) return "";
777 if(!clientResponse->addr) return "";
779 if(0 != OCDevAddrToPort(clientResponse->addr, &p) ) return "";
780 std::ostringstream ss;
785 std::string getConnectivityType (OCConnectivityType connType)
802 return "Incorrect connectivity";
806 std::string getQueryStrForGetPut(OCClientResponse * clientResponse){
811 void parseClientResponse(OCClientResponse * clientResponse){
812 coapServerIP = getIPAddrTBServer(clientResponse);
813 coapServerPort = getPortTBServer(clientResponse);
814 coapServerResource = getQueryStrForGetPut(clientResponse);