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";
38 static const char * MULTICAST_DEVICE_DISCOVERY_QUERY = "/oc/core/d";
39 static const char * MULTICAST_RESOURCE_DISCOVERY_QUERY = "/oc/core";
40 //The following variable determines the interface (wifi, ethernet etc.)
41 //to be used for sending unicast messages. Default set to WIFI.
42 static OCConnectivityType CA_CONNTYPE = OC_WIFI;
44 static std::string putPayload = "{\"state\":\"on\",\"power\":5}";
45 static std::string coapServerIP = "255.255.255.255";
46 static std::string coapServerPort = "5683";
47 static std::string coapServerResource = "/a/light";
49 // The handle for the observe registration
50 OCDoHandle gObserveDoHandle;
52 // The handle for observe registration
53 OCDoHandle gPresenceHandle;
55 // After this crosses a threshold client deregisters for further notifications
56 int gNumObserveNotifies = 0;
59 int gNumPresenceNotifies = 0;
63 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
64 void handleSigInt(int signum) {
65 if (signum == SIGINT) {
70 static void PrintUsage()
73 OC_LOG(INFO, TAG, "Usage : occlient -u <0|1> -t <1|2|3|4|5|6|7> -c <0|1|2|3>");
75 OC_LOG(INFO, TAG, "Usage : occlient -u <0|1> -t <1|2|3|4|5|6|7>");
77 OC_LOG(INFO, TAG, "-u <0|1> : Perform multicast/unicast discovery of resources");
79 OC_LOG(INFO, TAG, "-c <0|1|2|3> : Send unicast messages over Ethernet, WIFI, EDR or LE");
81 OC_LOG(INFO, TAG, "-t 1 : Discover Resources");
82 OC_LOG(INFO, TAG, "-t 2 : Discover Resources and Initiate Nonconfirmable Get Request");
83 OC_LOG(INFO, TAG, "-t 3 : Discover Resources and Initiate Nonconfirmable Put Requests");
84 OC_LOG(INFO, TAG, "-t 4 : Discover Resources and Initiate Nonconfirmable Post Requests");
85 OC_LOG(INFO, TAG, "-t 5 : Discover Resources and Initiate Nonconfirmable Delete Requests");
86 OC_LOG(INFO, TAG, "-t 6 : Discover Resources and Initiate Nonconfirmable Observe Requests");
87 OC_LOG(INFO, TAG, "-t 7 : Discover Resources and Initiate Nonconfirmable Get Request for a resource which is unavailable");
89 OC_LOG(INFO, TAG, "-t 8 : Discover Resources and Initiate Confirmable Get Request");
90 OC_LOG(INFO, TAG, "-t 9 : Discover Resources and Initiate Confirmable Post Request");
91 OC_LOG(INFO, TAG, "-t 10 : Discover Resources and Initiate Confirmable Delete Requests");
92 OC_LOG(INFO, TAG, "-t 11 : Discover Resources and Initiate Confirmable Observe Requests");
95 OC_LOG(INFO, TAG, "-t 12 : Discover Resources and Initiate Nonconfirmable presence");
96 OC_LOG(INFO, TAG, "-t 13 : Discover Resources and Initiate Nonconfirmable presence with "\
98 OC_LOG(INFO, TAG, "-t 14 : Discover Resources and Initiate Nonconfirmable presence with "\
102 OC_LOG(INFO, TAG, "-t 15 : Discover Resources and Initiate Nonconfirmable Observe Requests "\
103 "then cancel immediately");
104 OC_LOG(INFO, TAG, "-t 16 : Discover Resources and Initiate Nonconfirmable Get Request and "\
105 "add vendor specific header options");
106 OC_LOG(INFO, TAG, "-t 17 : Discover Devices");
109 OCStackResult InvokeOCDoResource(std::ostringstream &query,
110 OCMethod method, OCQualityOfService qos,
111 OCClientResponseHandler cb, OCHeaderOption * options, uint8_t numOptions)
114 OCCallbackData cbData;
118 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
122 ret = OCDoResource(&handle, method, query.str().c_str(), 0,
123 (method == OC_REST_PUT) ? putPayload.c_str() : NULL,
124 (CA_CONNTYPE), qos, &cbData, options, numOptions);
126 ret = OCDoResource(&handle, method, query.str().c_str(), 0,
127 (method == OC_REST_PUT) ? putPayload.c_str() : NULL,
128 qos, &cbData, options, numOptions);
131 if (ret != OC_STACK_OK)
133 OC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method);
135 else if (method == OC_REST_OBSERVE || method == OC_REST_OBSERVE_ALL)
137 gObserveDoHandle = handle;
140 else if (method == OC_REST_PRESENCE)
142 gPresenceHandle = handle;
149 OCStackApplicationResult putReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) {
150 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
152 OC_LOG(INFO, TAG, "Callback Context for PUT recvd successfully");
157 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
158 OC_LOG_V(INFO, TAG, "JSON = %s =============> Put Response", clientResponse->resJSONPayload);
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", clientResponse->resJSONPayload);
175 return OC_STACK_DELETE_TRANSACTION;
178 OCStackApplicationResult deleteReqCB(void *ctx, OCDoHandle handle, OCClientResponse *clientResponse)
180 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
182 OC_LOG(INFO, TAG, "Callback Context for DELETE recvd successfully");
187 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
188 OC_LOG_V(INFO, TAG, "JSON = %s =============> Delete Response", clientResponse->resJSONPayload);
190 return OC_STACK_DELETE_TRANSACTION;
193 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
195 if(clientResponse == NULL)
197 OC_LOG(INFO, TAG, "The clientResponse is NULL");
198 return OC_STACK_DELETE_TRANSACTION;
200 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
202 OC_LOG(INFO, TAG, "Callback Context for GET query recvd successfully");
205 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
206 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
207 OC_LOG_V(INFO, TAG, "JSON = %s =============> Get Response", clientResponse->resJSONPayload);
209 if(clientResponse->rcvdVendorSpecificHeaderOptions &&
210 clientResponse->numRcvdVendorSpecificHeaderOptions)
212 OC_LOG (INFO, TAG, "Received vendor specific options");
214 OCHeaderOption * rcvdOptions = clientResponse->rcvdVendorSpecificHeaderOptions;
215 for( i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
217 if(((OCHeaderOption)rcvdOptions[i]).protocolID == OC_COAP_ID)
219 OC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with",
220 ((OCHeaderOption)rcvdOptions[i]).optionID );
221 OC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData,
222 ((OCHeaderOption)rcvdOptions[i]).optionLength);
226 return OC_STACK_DELETE_TRANSACTION;
229 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", gNumObserveNotifies);
240 OC_LOG_V(INFO, TAG, "JSON = %s =============> Obs Response", clientResponse->resJSONPayload);
241 gNumObserveNotifies++;
242 if (gNumObserveNotifies == 3) //large number to test observing in DELETE case.
244 if(TEST_CASE == TEST_OBS_REQ_NON || TEST_CASE == TEST_OBS_REQ_CON){
245 if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK){
246 OC_LOG(ERROR, TAG, "Observe cancel error");
248 return OC_STACK_DELETE_TRANSACTION;
249 }else if(TEST_CASE == TEST_OBS_REQ_NON_CANCEL_IMM){
250 if (OCCancel (gObserveDoHandle, OC_HIGH_QOS, NULL, 0) != OC_STACK_OK){
251 OC_LOG(ERROR, TAG, "Observe cancel error");
255 if(clientResponse->sequenceNumber == OC_OBSERVE_REGISTER){
256 OC_LOG(INFO, TAG, "This also serves as a registration confirmation");
257 }else if(clientResponse->sequenceNumber == OC_OBSERVE_DEREGISTER){
258 OC_LOG(INFO, TAG, "This also serves as a deregistration confirmation");
259 return OC_STACK_DELETE_TRANSACTION;
260 }else if(clientResponse->sequenceNumber == OC_OBSERVE_NO_OPTION){
261 OC_LOG(INFO, TAG, "This also tells you that registration/deregistration failed");
262 return OC_STACK_DELETE_TRANSACTION;
265 return OC_STACK_KEEP_TRANSACTION;
268 OCStackApplicationResult presenceCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse) {
269 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
271 OC_LOG(INFO, TAG, "Callback Context for Presence recvd successfully");
276 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
277 OC_LOG_V(INFO, TAG, "NONCE NUMBER: %u", clientResponse->sequenceNumber);
278 OC_LOG_V(INFO, TAG, "Callback Context for Presence notification recvd successfully %d", gNumPresenceNotifies);
279 OC_LOG_V(INFO, TAG, "JSON = %s =============> Presence Response", clientResponse->resJSONPayload);
280 gNumPresenceNotifies++;
281 if (gNumPresenceNotifies == 20)
283 if (OCCancel (gPresenceHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK){
284 OC_LOG(ERROR, TAG, "Presence cancel error");
286 return OC_STACK_DELETE_TRANSACTION;
289 return OC_STACK_KEEP_TRANSACTION;
293 // This is a function called back when a device is discovered
294 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
295 OCClientResponse * clientResponse) {
296 uint8_t remoteIpAddr[4];
297 uint16_t remotePortNu;
299 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
301 OC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully");
306 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
308 OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr,
309 remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3);
310 OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu);
313 std::string connectionType = getConnectivityType (clientResponse->connType);
314 OC_LOG_V(INFO, TAG, "Discovered on %s", connectionType.c_str());
317 "Device =============> Discovered %s @ %d.%d.%d.%d:%d",
318 clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1],
319 remoteIpAddr[2], remoteIpAddr[3], remotePortNu);
321 parseClientResponse(clientResponse);
325 case TEST_GET_REQ_NON:
326 InitGetRequest(OC_LOW_QOS, 0);
328 case TEST_PUT_REQ_NON:
331 case TEST_POST_REQ_NON:
332 InitPostRequest(OC_LOW_QOS);
334 case TEST_DELETE_REQ_NON:
335 InitDeleteRequest(OC_LOW_QOS);
337 case TEST_OBS_REQ_NON:
338 case TEST_OBS_REQ_NON_CANCEL_IMM:
339 InitObserveRequest(OC_LOW_QOS);
341 case TEST_GET_UNAVAILABLE_RES_REQ_NON:
342 InitGetRequestToUnavailableResource();
344 case TEST_GET_REQ_CON:
345 InitGetRequest(OC_HIGH_QOS, 0);
347 case TEST_POST_REQ_CON:
348 InitPostRequest(OC_HIGH_QOS);
350 case TEST_DELETE_REQ_CON:
351 InitDeleteRequest(OC_HIGH_QOS);
353 case TEST_OBS_REQ_CON:
354 InitObserveRequest(OC_HIGH_QOS);
357 case TEST_OBS_PRESENCE:
358 case TEST_OBS_PRESENCE_WITH_FILTER:
359 case TEST_OBS_PRESENCE_WITH_FILTERS:
363 case TEST_GET_REQ_NON_WITH_VENDOR_HEADER_OPTIONS:
364 InitGetRequest(OC_LOW_QOS, 1);
366 case TEST_DISCOVER_DEV_REQ:
367 InitDeviceDiscovery();
375 return OC_STACK_KEEP_TRANSACTION;
377 return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION ;
382 OCStackApplicationResult DeviceDiscoveryReqCB (void* ctx, OCDoHandle handle,
383 OCClientResponse * clientResponse)
385 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
387 OC_LOG(INFO, TAG, "Callback Context for Device DISCOVER query recvd successfully");
392 //OC_LOG truncates the response as it is too long.
393 fprintf(stderr, "Discovery response: \n %s\n", clientResponse->resJSONPayload);
397 return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
403 OCStackResult result = OC_STACK_OK;
404 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
405 std::ostringstream query;
406 std::ostringstream querySuffix;
407 query << "coap://" << coapServerIP << ":" << coapServerPort << OC_PRESENCE_URI;
408 if(TEST_CASE == TEST_OBS_PRESENCE)
410 result = InvokeOCDoResource(query, OC_REST_PRESENCE, OC_LOW_QOS,
411 presenceCB, NULL, 0);
413 if(TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTER || TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTERS)
416 querySuffix << query.str() << "?rt=core.light";
417 result = InvokeOCDoResource(querySuffix, OC_REST_PRESENCE, OC_LOW_QOS,
418 presenceCB, NULL, 0);
420 if(TEST_CASE == TEST_OBS_PRESENCE_WITH_FILTERS)
422 if(result == OC_STACK_OK)
425 querySuffix << query.str() << "?rt=core.fan";
426 result = InvokeOCDoResource(querySuffix, OC_REST_PRESENCE, OC_LOW_QOS,
427 presenceCB, NULL, 0);
434 int InitGetRequestToUnavailableResource()
436 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
437 std::ostringstream query;
438 query << "coap://" << coapServerIP << ":" << coapServerPort << "/SomeUnknownResource";
439 return (InvokeOCDoResource(query, OC_REST_GET, OC_LOW_QOS, getReqCB, NULL, 0));
442 int InitObserveRequest(OCQualityOfService qos)
444 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
445 std::ostringstream query;
446 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
447 return (InvokeOCDoResource(query, OC_REST_OBSERVE, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, obsReqCB, NULL, 0));
452 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
453 std::ostringstream query;
454 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
455 return (InvokeOCDoResource(query, OC_REST_PUT, OC_LOW_QOS, putReqCB, NULL, 0));
458 int InitPostRequest(OCQualityOfService qos)
460 OCStackResult result;
461 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
462 std::ostringstream query;
463 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
465 // First POST operation (to create an Light instance)
466 result = InvokeOCDoResource(query, OC_REST_POST,
467 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
469 if (OC_STACK_OK != result)
471 // Error can happen if for example, network connectivity is down
472 OC_LOG(INFO, TAG, "First POST call did not succeed");
475 // Second POST operation (to create an Light instance)
476 result = InvokeOCDoResource(query, OC_REST_POST,
477 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
479 if (OC_STACK_OK != result)
481 OC_LOG(INFO, TAG, "Second POST call did not succeed");
484 // This POST operation will update the original resourced /a/light
485 return (InvokeOCDoResource(query, OC_REST_POST,
486 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
487 postReqCB, NULL, 0));
490 void* RequestDeleteDeathResourceTask(void* myqos)
492 sleep (30); //long enough to give the server time to finish deleting the resource.
493 std::ostringstream query;
494 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
496 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
498 // Second DELETE operation to delete the resource that might have been removed already.
499 OCQualityOfService qos;
505 OCStackResult result = InvokeOCDoResource(query, OC_REST_DELETE,
507 deleteReqCB, NULL, 0);
509 if (OC_STACK_OK != result)
511 OC_LOG(INFO, TAG, "Second DELETE call did not succeed");
517 int InitDeleteRequest(OCQualityOfService qos)
519 OCStackResult result;
520 std::ostringstream query;
521 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
523 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
525 // First DELETE operation
526 result = InvokeOCDoResource(query, OC_REST_DELETE,
528 deleteReqCB, NULL, 0);
529 if (OC_STACK_OK != result)
531 // Error can happen if for example, network connectivity is down
532 OC_LOG(INFO, TAG, "First DELETE call did not succeed");
536 //Create a thread to delete this resource again
538 pthread_create (&threadId, NULL, RequestDeleteDeathResourceTask, (void*)qos);
541 OC_LOG_V(INFO, TAG, "\n\nExit %s", __func__);
545 int InitGetRequest(OCQualityOfService qos, uint8_t withVendorSpecificHeaderOptions)
547 OCHeaderOption options[MAX_HEADER_OPTIONS];
549 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
550 std::ostringstream query;
551 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
553 if(withVendorSpecificHeaderOptions)
555 uint8_t option0[] = {1,2,3,4,5,6,7,8,9,10};
556 uint8_t option1[] = {11,12,13,14,15,16,17,18,19,20};
557 memset(options, 0, sizeof(OCHeaderOption) * MAX_HEADER_OPTIONS);
558 options[0].protocolID = OC_COAP_ID;
559 options[0].optionID = 2048;
560 memcpy(options[0].optionData, option0, sizeof(option0));
561 options[0].optionLength = 10;
562 options[1].protocolID = OC_COAP_ID;
563 options[1].optionID = 3000;
564 memcpy(options[1].optionData, option1, sizeof(option1));
565 options[1].optionLength = 10;
567 if(withVendorSpecificHeaderOptions)
569 return (InvokeOCDoResource(query, OC_REST_GET, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, getReqCB, options, 2));
573 return (InvokeOCDoResource(query, OC_REST_GET, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, getReqCB, NULL, 0));
577 int InitDeviceDiscovery()
580 OCCallbackData cbData;
582 char szQueryUri[64] = { 0 };
584 cbData.cb = DeviceDiscoveryReqCB;
585 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
588 if(UNICAST_DISCOVERY)
590 strncpy(szQueryUri, TEST_APP_UNICAST_DEVICE_DISCOVERY_QUERY,
591 (strlen(TEST_APP_UNICAST_DEVICE_DISCOVERY_QUERY) + 1));
597 strncpy(szQueryUri, MULTICAST_DEVICE_DISCOVERY_QUERY,
598 (strlen(MULTICAST_DEVICE_DISCOVERY_QUERY) + 1));
601 strncpy(szQueryUri, TEST_APP_MULTICAST_DEVICE_DISCOVERY_QUERY,
602 (strlen(TEST_APP_MULTICAST_DEVICE_DISCOVERY_QUERY) + 1));
607 if(UNICAST_DISCOVERY)
609 ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, CA_CONNTYPE,
610 OC_LOW_QOS, &cbData, NULL, 0);
614 ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, (OC_ALL),
615 OC_LOW_QOS, &cbData, NULL, 0);
618 ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, OC_LOW_QOS, &cbData, NULL, 0);
621 if (ret != OC_STACK_OK)
623 OC_LOG(ERROR, TAG, "OCStack device error");
632 OCCallbackData cbData;
634 /* Start a discovery query*/
635 char szQueryUri[64] = { 0 };
637 if (UNICAST_DISCOVERY)
639 strcpy(szQueryUri, TEST_APP_UNICAST_DISCOVERY_QUERY);
644 strcpy(szQueryUri, MULTICAST_RESOURCE_DISCOVERY_QUERY);
646 strcpy(szQueryUri, OC_WELL_KNOWN_QUERY);
650 cbData.cb = discoveryReqCB;
651 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
654 if(UNICAST_DISCOVERY)
656 ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, CA_CONNTYPE,
657 OC_LOW_QOS, &cbData, NULL, 0);
661 ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, (OC_ALL),
662 OC_LOW_QOS, &cbData, NULL, 0);
665 ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, OC_LOW_QOS, &cbData, NULL, 0);
667 if (ret != OC_STACK_OK)
669 OC_LOG(ERROR, TAG, "OCStack resource error");
674 int main(int argc, char* argv[]) {
675 uint8_t addr[20] = {0};
676 uint8_t* paddr = NULL;
677 uint16_t port = USE_RANDOM_PORT;
678 uint8_t ifname[] = "eth0";
682 while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
684 while ((opt = getopt(argc, argv, "u:t:")) != -1)
690 UNICAST_DISCOVERY = atoi(optarg);
693 TEST_CASE = atoi(optarg);
697 CA_CONNTYPE = OCConnectivityType(atoi(optarg));
706 if ((UNICAST_DISCOVERY != 0 && UNICAST_DISCOVERY != 1) ||
707 (TEST_CASE < TEST_DISCOVER_REQ || TEST_CASE >= MAX_TESTS) )
714 /*Get Ip address on defined interface and initialize coap on it with random port number
715 * this port number will be used as a source port in all coap communications*/
716 if ( OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr,
717 sizeof(addr)) == ERR_SUCCESS)
719 OC_LOG_V(INFO, TAG, "Starting occlient on address %s",addr);
723 /* Initialize OCStack*/
724 if (OCInit((char *) paddr, port, OC_CLIENT) != OC_STACK_OK) {
725 OC_LOG(ERROR, TAG, "OCStack init error");
729 if(UNICAST_DISCOVERY == 0 && TEST_CASE == TEST_DISCOVER_DEV_REQ)
731 InitDeviceDiscovery();
738 // Break from loop with Ctrl+C
739 OC_LOG(INFO, TAG, "Entering occlient main loop...");
740 signal(SIGINT, handleSigInt);
743 if (OCProcess() != OC_STACK_OK) {
744 OC_LOG(ERROR, TAG, "OCStack process error");
750 OC_LOG(INFO, TAG, "Exiting occlient main loop...");
752 if (OCStop() != OC_STACK_OK) {
753 OC_LOG(ERROR, TAG, "OCStack stop error");
759 std::string getIPAddrTBServer(OCClientResponse * clientResponse) {
760 if(!clientResponse) return "";
761 if(!clientResponse->addr) return "";
762 uint8_t a, b, c, d = 0;
763 if(0 != OCDevAddrToIPv4Addr(clientResponse->addr, &a, &b, &c, &d) ) return "";
765 char ipaddr[16] = {'\0'};
766 snprintf(ipaddr, sizeof(ipaddr), "%d.%d.%d.%d", a,b,c,d); // ostringstream not working correctly here, hence snprintf
767 return std::string (ipaddr);
770 std::string getPortTBServer(OCClientResponse * clientResponse){
771 if(!clientResponse) return "";
772 if(!clientResponse->addr) return "";
774 if(0 != OCDevAddrToPort(clientResponse->addr, &p) ) return "";
775 std::ostringstream ss;
780 std::string getConnectivityType (OCConnectivityType connType)
797 return "Incorrect connectivity";
801 std::string getQueryStrForGetPut(OCClientResponse * clientResponse){
806 void parseClientResponse(OCClientResponse * clientResponse){
807 coapServerIP = getIPAddrTBServer(clientResponse);
808 coapServerPort = getPortTBServer(clientResponse);
809 coapServerResource = getQueryStrForGetPut(clientResponse);