1 //******************************************************************
3 // Copyright 2014 Samsung Electronics All Rights Reserved.
4 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
6 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
12 // http://www.apache.org/licenses/LICENSE-2.0
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
20 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
32 #include "ocpayload.h"
33 #include "payload_logging.h"
37 #ifdef ROUTING_GATEWAY
39 * Maximum number of gateway requests to form the routing table.
41 #define MAX_NUM_GATEWAY_REQUEST 20
44 * Sleep duration after every OCProcess().
46 #define SLEEP_DURATION 100000
49 // Tracking user input
50 static int g_unicastDiscovery = 0;
51 static int g_testCase = 0;
52 static int g_connectivity = 0;
54 static const char *DEVICE_DISCOVERY_QUERY = "%s/oic/d";
55 static const char *PLATFORM_DISCOVERY_QUERY = "%s/oic/p";
56 static const char *RESOURCE_DISCOVERY_QUERY = "%s/oic/res";
58 //The following variable determines the interface protocol (IPv4, IPv6, etc)
59 //to be used for sending unicast messages. Default set to IP dual stack.
60 static OCConnectivityType g_connType = CT_ADAPTER_IP;
61 static OCDevAddr g_serverAddr;
62 static char g_discoveryAddr[100];
63 static std::string coapServerResource = "/a/light";
65 void StripNewLineChar(char* str);
67 // The handle for the observe registration
68 OCDoHandle gObserveDoHandle;
70 // The handle for observe registration
71 OCDoHandle gPresenceHandle;
73 // After this crosses a threshold client deregisters for further notifications
74 int gNumObserveNotifies = 0;
77 int gNumPresenceNotifies = 0;
81 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
82 void handleSigInt(int signum)
90 OCPayload* putPayload()
92 OCRepPayload* payload = OCRepPayloadCreate();
96 std::cout << "Failed to create put payload object"<<std::endl;
100 OCRepPayloadSetPropInt(payload, "power", 15);
101 OCRepPayloadSetPropBool(payload, "state", true);
103 return (OCPayload*) payload;
106 static void PrintUsage()
109 cout << "\nUsage : occlient -u <0|1> -t <1..17> -c <0|1|2>";
110 cout << "\n-u <0|1> : Perform multicast/unicast discovery of resources";
111 cout << "\n-c 0 : Default IP selection";
112 cout << "\n-c 1 : IP Connectivity Type";
113 cout << "\n-c 2 : EDR Connectivity Type (IPv6 not currently supported)";
114 cout << "\n-t 1 : Discover Resources";
115 cout << "\n-t 2 : Discover Resources and Initiate Nonconfirmable Get Request";
116 cout << "\n-t 3 : Discover Resources and Initiate Nonconfirmable Get Request with query filter";
117 cout << "\n-t 4 : Discover Resources and Initiate Nonconfirmable Put Requests";
118 cout << "\n-t 5 : Discover Resources and Initiate Nonconfirmable Post Requests";
119 cout << "\n-t 6 : Discover Resources and Initiate Nonconfirmable Delete Requests";
120 cout << "\n-t 7 : Discover Resources and Initiate Nonconfirmable Observe Requests";
121 cout << "\n-t 8 : Discover Resources and Initiate Nonconfirmable Get Request for a resource";
122 cout << "which is unavailable";
123 cout << "\n-t 9 : Discover Resources and Initiate Confirmable Get Request";
124 cout << "\n-t 10 : Discover Resources and Initiate Confirmable Post Request";
125 cout << "\n-t 11 : Discover Resources and Initiate Confirmable Delete Requests";
126 cout << "\n-t 12 : Discover Resources and Initiate Confirmable Observe Requests and";
127 cout << "cancel with Low QoS";
130 cout << "\n-t 13 : Discover Resources and Initiate Nonconfirmable presence";
131 cout << "\n-t 14 : Discover Resources and Initiate Nonconfirmable presence with filter";
132 cout << "\n-t 15 : Discover Resources and Initiate Nonconfirmable presence with 2 filters";
133 cout << "\n-t 16 : Discover Resources and Initiate Nonconfirmable multicast presence.";
136 cout << "\n-t 17 : Discover Resources and Initiate Nonconfirmable Observe Requests";
137 cout << "then cancel immediately with High QOS";
138 cout << "\n-t 18 : Discover Resources and Initiate Nonconfirmable Get Request and add";
139 cout << "vendor specific header options";
140 cout << "\n-t 19 : Discover Platform";
141 cout << "\n-t 20 : Discover Devices";
144 OCStackResult InvokeOCDoResource(std::ostringstream &query,
145 OCDevAddr *remoteAddr,
147 OCQualityOfService qos,
148 OCClientResponseHandler cb,
149 OCHeaderOption * options,
153 OCCallbackData cbData;
157 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
160 ret = OCDoResource(&handle, method, query.str().c_str(), remoteAddr,
161 (method == OC_REST_PUT) ? putPayload() : NULL,
162 (g_connType), qos, &cbData, options, numOptions);
164 if (ret != OC_STACK_OK)
166 cout << "\nOCDoResource returns error "<< ret;
167 cout << " with method " << method;
169 else if (method == OC_REST_OBSERVE || method == OC_REST_OBSERVE_ALL)
171 gObserveDoHandle = handle;
174 else if (method == OC_REST_PRESENCE)
176 gPresenceHandle = handle;
183 OCStackApplicationResult putReqCB(void* ctx, OCDoHandle /*handle*/,
184 OCClientResponse * clientResponse)
186 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
188 cout << "\nCallback Context for PUT recvd successfully";
193 cout << "\nStackResult: " << getResult(clientResponse->result);
194 cout << "\nJSON = " << clientResponse->payload;
198 cout << "\nputReqCB received Null clientResponse";
200 return OC_STACK_DELETE_TRANSACTION;
203 OCStackApplicationResult postReqCB(void *ctx, OCDoHandle /*handle*/,
204 OCClientResponse *clientResponse)
206 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
208 cout << "\nCallback Context for POST recvd successfully";
213 cout << "\nStackResult: " << getResult(clientResponse->result);
214 cout << "\nJSON = " << clientResponse->payload;
218 cout << "\npostReqCB received Null clientResponse";
220 return OC_STACK_DELETE_TRANSACTION;
223 OCStackApplicationResult deleteReqCB(void *ctx,
224 OCDoHandle /*handle*/,
225 OCClientResponse *clientResponse)
227 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
229 cout << "\nCallback Context for DELETE recvd successfully";
234 cout << "\nStackResult: " << getResult(clientResponse->result);
235 //OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
239 cout << "\ndeleteReqCB received Null clientResponse";
241 return OC_STACK_DELETE_TRANSACTION;
244 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle /*handle*/,
245 OCClientResponse * clientResponse)
247 if (clientResponse == NULL)
249 cout << "\ngetReqCB received NULL clientResponse";
250 return OC_STACK_DELETE_TRANSACTION;
253 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
255 cout << "\nCallback Context for GET query recvd successfully";
258 cout << "\nStackResult: " << getResult(clientResponse->result);
259 cout << "\nSEQUENCE NUMBER: " << clientResponse->sequenceNumber;
260 //OIC_LOG_PAYLOAD(INFO, TAG, clientResponse->payload);
262 if (clientResponse->numRcvdVendorSpecificHeaderOptions > 0)
264 cout << "\nReceived vendor specific options";
266 OCHeaderOption * rcvdOptions = clientResponse->rcvdVendorSpecificHeaderOptions;
267 for( i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
269 if (((OCHeaderOption)rcvdOptions[i]).protocolID == OC_COAP_ID)
271 cout << "\nReceived option ID " << ((OCHeaderOption)rcvdOptions[i]).optionID;
275 return OC_STACK_DELETE_TRANSACTION;
278 OCStackApplicationResult obsReqCB(void* ctx, OCDoHandle /*handle*/,
279 OCClientResponse * clientResponse)
281 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
283 cout << "\nCallback Context for OBS query recvd successfully";
288 cout << "\nStackResult: " << getResult(clientResponse->result);
289 cout << "\nSEQUENCE NUMBER: " << clientResponse->sequenceNumber;
290 cout << "\nCallback Context for OBSERVE notification recvd successfully ";
291 //OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
292 gNumObserveNotifies++;
293 if (gNumObserveNotifies == 15) //large number to test observing in DELETE case.
295 if (g_testCase == TEST_OBS_REQ_NON || g_testCase == TEST_OBS_REQ_CON)
297 if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
299 cout << "\nObserve cancel error";
301 return OC_STACK_DELETE_TRANSACTION;
303 else if (g_testCase == TEST_OBS_REQ_NON_CANCEL_IMM)
305 if (OCCancel (gObserveDoHandle, OC_HIGH_QOS, NULL, 0) != OC_STACK_OK)
307 cout << "\nObserve cancel error";
311 if (clientResponse->sequenceNumber == OC_OBSERVE_REGISTER)
313 cout << "\nThis also serves as a registration confirmation";
315 else if (clientResponse->sequenceNumber == OC_OBSERVE_DEREGISTER)
317 cout << "\nThis also serves as a deregistration confirmation";
318 return OC_STACK_DELETE_TRANSACTION;
320 else if (clientResponse->sequenceNumber == OC_OBSERVE_NO_OPTION)
322 cout << "\nThis also tells you that registration/deregistration failed";
323 return OC_STACK_DELETE_TRANSACTION;
328 cout << "\nobsReqCB received Null clientResponse";
330 return OC_STACK_KEEP_TRANSACTION;
333 OCStackApplicationResult presenceCB(void* ctx, OCDoHandle /*handle*/,
334 OCClientResponse * clientResponse)
336 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
338 cout << "\nCallback Context for Presence recvd successfully";
343 cout << "\nStackResult: " << getResult(clientResponse->result);
344 cout << "\nNONCE NUMBER: " << clientResponse->sequenceNumber;
345 cout << "\nCallback Context for Presence notification recvd successfully ";
346 //OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
347 gNumPresenceNotifies++;
348 if (gNumPresenceNotifies == 20)
350 if (OCCancel(gPresenceHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
352 cout << "\nPresence cancel error";
354 return OC_STACK_DELETE_TRANSACTION;
359 cout << "\npresenceCB received Null clientResponse";
361 return OC_STACK_KEEP_TRANSACTION;
365 // This is a function called back when a device is discovered
366 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle /*handle*/,
367 OCClientResponse * clientResponse)
369 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
371 cout << "\nCallback Context for DISCOVER query recvd successfully";
376 if (NULL == clientResponse->payload)
378 cout << "\nPayload is NULL, No resources found";
379 return OC_STACK_KEEP_TRANSACTION;
382 cout << "\nStackResult: " << getResult(clientResponse->result);
384 std::string connectionType = getConnectivityType (clientResponse->connType);
385 cout << "\nDiscovered on " << connectionType.c_str();
386 cout << "\nDevice ======> Discovered ";
387 cout << clientResponse->devAddr.addr;
388 if (CT_ADAPTER_IP == clientResponse->connType)
390 cout << ":" << clientResponse->devAddr.port;
392 //OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
393 cout << "\nConnectivity type: " << clientResponse->connType;
394 g_connType = clientResponse->connType;
395 g_serverAddr = clientResponse->devAddr;
396 parseClientResponse(clientResponse);
400 case TEST_GET_REQ_NON:
401 InitGetRequest(OC_LOW_QOS, 0, 0);
403 case TEST_GET_REQ_NON_WITH_FILTERS:
404 InitGetRequest(OC_LOW_QOS, 0, 1);
406 case TEST_PUT_REQ_NON:
407 InitPutRequest(OC_LOW_QOS);
409 case TEST_POST_REQ_NON:
410 InitPostRequest(OC_LOW_QOS);
412 case TEST_DELETE_REQ_NON:
413 InitDeleteRequest(OC_LOW_QOS);
415 case TEST_OBS_REQ_NON:
416 case TEST_OBS_REQ_NON_CANCEL_IMM:
417 InitObserveRequest(OC_LOW_QOS);
419 case TEST_GET_UNAVAILABLE_RES_REQ_NON:
420 InitGetRequestToUnavailableResource(OC_LOW_QOS);
422 case TEST_GET_REQ_CON:
423 InitGetRequest(OC_HIGH_QOS, 0, 0);
425 case TEST_POST_REQ_CON:
426 InitPostRequest(OC_HIGH_QOS);
428 case TEST_DELETE_REQ_CON:
429 InitDeleteRequest(OC_HIGH_QOS);
431 case TEST_OBS_REQ_CON:
432 InitObserveRequest(OC_HIGH_QOS);
435 case TEST_OBS_PRESENCE:
436 case TEST_OBS_PRESENCE_WITH_FILTER:
437 case TEST_OBS_PRESENCE_WITH_FILTERS:
438 case TEST_OBS_MULTICAST_PRESENCE:
442 case TEST_GET_REQ_NON_WITH_VENDOR_HEADER_OPTIONS:
443 InitGetRequest(OC_LOW_QOS, 1, 0);
445 case TEST_DISCOVER_PLATFORM_REQ:
446 InitPlatformDiscovery(OC_LOW_QOS);
448 case TEST_DISCOVER_DEV_REQ:
449 InitDeviceDiscovery(OC_LOW_QOS);
458 cout << "\ndiscoveryReqCB received Null clientResponse";
460 return OC_STACK_KEEP_TRANSACTION;
463 OCStackApplicationResult PlatformDiscoveryReqCB(void* ctx,
464 OCDoHandle /*handle*/,
465 OCClientResponse * clientResponse)
467 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
469 cout << "\nCallback Context for Platform DISCOVER query recvd successfully";
474 //OIC_LOG truncates the response as it is too long.
475 //OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
479 cout << "\nPlatformDiscoveryReqCB received Null clientResponse";
482 return (g_unicastDiscovery) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
485 OCStackApplicationResult DeviceDiscoveryReqCB(void* ctx, OCDoHandle /*handle*/,
486 OCClientResponse * clientResponse)
488 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
490 cout << "\nCallback Context for Device DISCOVER query recvd successfully";
495 //OIC_LOG truncates the response as it is too long.
496 cout << "\nDiscovery response: ";
497 cout << clientResponse->payload;
501 cout << "\nPlatformDiscoveryReqCB received Null clientResponse";
504 return (g_unicastDiscovery) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
510 OCStackResult result = OC_STACK_OK;
511 cout << "\nExecuting " << __func__;
512 std::ostringstream query;
513 std::ostringstream querySuffix;
514 query << OC_RSRVD_PRESENCE_URI;
515 if (g_testCase == TEST_OBS_PRESENCE)
517 result = InvokeOCDoResource(query, &g_serverAddr, OC_REST_PRESENCE,
518 OC_LOW_QOS, presenceCB, NULL, 0);
520 if (g_testCase == TEST_OBS_PRESENCE_WITH_FILTER || g_testCase == TEST_OBS_PRESENCE_WITH_FILTERS)
523 querySuffix << query.str() << "?rt=core.led";
524 result = InvokeOCDoResource(querySuffix, &g_serverAddr, OC_REST_PRESENCE,
525 OC_LOW_QOS, presenceCB, NULL, 0);
527 if (g_testCase == TEST_OBS_PRESENCE_WITH_FILTERS)
529 if (result == OC_STACK_OK)
532 querySuffix << query.str() << "?rt=core.fan";
533 result = InvokeOCDoResource(querySuffix, &g_serverAddr, OC_REST_PRESENCE, OC_LOW_QOS,
534 presenceCB, NULL, 0);
537 if (g_testCase == TEST_OBS_MULTICAST_PRESENCE)
539 if (result == OC_STACK_OK)
541 std::ostringstream multicastPresenceQuery;
542 multicastPresenceQuery.str("");
543 multicastPresenceQuery << "coap://" << OC_MULTICAST_PREFIX << OC_RSRVD_PRESENCE_URI;
544 result = InvokeOCDoResource(multicastPresenceQuery, &g_serverAddr, OC_REST_PRESENCE, OC_LOW_QOS,
545 presenceCB, NULL, 0);
552 int InitGetRequestToUnavailableResource(OCQualityOfService qos)
554 cout << "\nExecuting " << __func__;
555 std::ostringstream query;
556 query << "/SomeUnknownResource";
557 return (InvokeOCDoResource(query, &g_serverAddr, OC_REST_GET, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
561 int InitObserveRequest(OCQualityOfService qos)
563 cout << "\nExecuting " << __func__;
564 std::ostringstream query;
565 query << coapServerResource;
566 return (InvokeOCDoResource(query, &g_serverAddr, OC_REST_OBSERVE,
567 (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, obsReqCB, NULL, 0));
570 int InitPutRequest(OCQualityOfService qos)
572 cout << "\nExecuting " << __func__;
573 std::ostringstream query;
574 query << coapServerResource;
575 return (InvokeOCDoResource(query, &g_serverAddr, OC_REST_PUT, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
579 int InitPostRequest(OCQualityOfService qos)
581 OCStackResult result;
582 cout << "\nExecuting " << __func__;
583 std::ostringstream query;
584 query << coapServerResource;
586 // First POST operation (to create an Light instance)
587 result = InvokeOCDoResource(query, &g_serverAddr, OC_REST_POST,
588 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
590 if (OC_STACK_OK != result)
592 // Error can happen if for example, network connectivity is down
593 cout << "\nFirst POST call did not succeed";
596 // Second POST operation (to create an Light instance)
597 result = InvokeOCDoResource(query, &g_serverAddr, OC_REST_POST,
598 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
600 if (OC_STACK_OK != result)
602 cout << "\nSecond POST call did not succeed";
605 // This POST operation will update the original resourced /a/light
606 return (InvokeOCDoResource(query, &g_serverAddr, OC_REST_POST,
607 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
608 postReqCB, NULL, 0));
611 void* RequestDeleteDeathResourceTask(void* myqos)
613 sleep (30);//long enough to give the server time to finish deleting the resource.
614 std::ostringstream query;
615 query << coapServerResource;
617 cout << "\nExecuting " << __func__;
619 // Second DELETE operation to delete the resource that might have been removed already.
620 OCQualityOfService qos;
630 OCStackResult result = InvokeOCDoResource(query, &g_serverAddr, OC_REST_DELETE,
632 deleteReqCB, NULL, 0);
634 if (OC_STACK_OK != result)
636 cout << "\nSecond DELETE call did not succeed";
642 int InitDeleteRequest(OCQualityOfService qos)
644 OCStackResult result;
645 std::ostringstream query;
646 query << coapServerResource;
648 cout << "\nExecuting " << __func__;
650 // First DELETE operation
651 result = InvokeOCDoResource(query, &g_serverAddr, OC_REST_DELETE,
653 deleteReqCB, NULL, 0);
654 if (OC_STACK_OK != result)
656 // Error can happen if for example, network connectivity is down
657 cout << "\nFirst DELETE call did not succeed";
661 //Create a thread to delete this resource again
663 pthread_create (&threadId, NULL, RequestDeleteDeathResourceTask, (void*)qos);
666 cout << "\nExit " << __func__;
670 int InitGetRequest(OCQualityOfService qos, uint8_t withVendorSpecificHeaderOptions, bool getWithQuery)
673 OCHeaderOption options[MAX_HEADER_OPTIONS];
675 cout << "\nExecuting " << __func__;
676 std::ostringstream query;
677 query << coapServerResource;
679 // ocserver is written to only process "power<X" query.
682 cout << "\nUsing query power<30";
683 query << "?power<50";
686 if (withVendorSpecificHeaderOptions)
688 uint8_t option0[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
689 uint8_t option1[] = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
690 memset(options, 0, sizeof(OCHeaderOption) * MAX_HEADER_OPTIONS);
691 options[0].protocolID = OC_COAP_ID;
692 options[0].optionID = 2048;
693 memcpy(options[0].optionData, option0, sizeof(option0));
694 options[0].optionLength = 10;
695 options[1].protocolID = OC_COAP_ID;
696 options[1].optionID = 3000;
697 memcpy(options[1].optionData, option1, sizeof(option1));
698 options[1].optionLength = 10;
700 if (withVendorSpecificHeaderOptions)
702 return (InvokeOCDoResource(query, &g_serverAddr, OC_REST_GET,
703 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, options, 2));
707 return (InvokeOCDoResource(query, &g_serverAddr, OC_REST_GET,
708 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, NULL, 0));
712 int InitPlatformDiscovery(OCQualityOfService qos)
714 cout << "\nExecuting " << __func__;
717 OCCallbackData cbData;
718 char szQueryUri[64] = { 0 };
720 snprintf(szQueryUri, sizeof (szQueryUri) - 1, PLATFORM_DISCOVERY_QUERY, g_discoveryAddr);
722 cbData.cb = PlatformDiscoveryReqCB;
723 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
726 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
727 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
729 if (ret != OC_STACK_OK)
731 cout << "\nOCStack device error";
737 int InitDeviceDiscovery(OCQualityOfService qos)
739 cout << "\nExecuting " << __func__;
742 OCCallbackData cbData;
743 char szQueryUri[100] = { 0 };
745 snprintf(szQueryUri, sizeof (szQueryUri) - 1, DEVICE_DISCOVERY_QUERY, g_discoveryAddr);
747 cbData.cb = DeviceDiscoveryReqCB;
748 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
751 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
752 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
754 if (ret != OC_STACK_OK)
756 cout << "\nOCStack device error";
762 int InitDiscovery(OCQualityOfService qos)
765 OCCallbackData cbData;
766 char szQueryUri[100] = { 0 };
768 snprintf(szQueryUri, sizeof (szQueryUri) - 1, RESOURCE_DISCOVERY_QUERY, g_discoveryAddr);
770 cbData.cb = discoveryReqCB;
771 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
774 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
775 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
777 if (ret != OC_STACK_OK)
779 cout << "\nOCStack resource error";
784 int main(int argc, char* argv[])
788 while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
793 g_unicastDiscovery = atoi(optarg);
796 g_testCase = atoi(optarg);
799 g_connectivity = atoi(optarg);
807 if ((g_unicastDiscovery != 0 && g_unicastDiscovery != 1) ||
808 (g_testCase < TEST_DISCOVER_REQ || g_testCase >= MAX_TESTS) ||
809 (g_connectivity < CT_ADAPTER_DEFAULT || g_connectivity >= MAX_CT))
815 cout << "\nEntering occlient main loop...\n";
817 if (OCInit1(OC_CLIENT, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
819 cout << "\nOCStack init error";
823 #ifdef ROUTING_GATEWAY
825 * Before invoking Discover resource, we process the gateway requests
826 * and form the routing table.
828 for (int index = 0; index < MAX_NUM_GATEWAY_REQUEST; index++)
830 if (OCProcess() != OC_STACK_OK)
832 OIC_LOG(ERROR, TAG, "OCStack process error");
835 usleep(SLEEP_DURATION);
839 if (g_connectivity == CT_ADAPTER_DEFAULT || g_connectivity == CT_IP)
841 g_connType = CT_ADAPTER_IP;
843 else if(g_connectivity == CT_EDR)
845 cout << "\nSelected EDR Adapter\n";
846 g_connType = CT_ADAPTER_RFCOMM_BTEDR;
850 cout << "\nDefault Connectivity type selected...";
854 g_discoveryAddr[0] = '\0';
856 if (g_unicastDiscovery)
858 cout << "\nEnter address of Server hosting resource as given below:";
859 cout << "\nIP Adapter: 192.168.0.15:45454(IP:Port)";
860 cout << "\nEDR/BLE Adapter: AB:BC:CD:DE:EF:FG(MAC Address)";
863 if (fgets(g_discoveryAddr, sizeof (g_discoveryAddr), stdin))
865 //Strip newline char from unicastAddr
866 StripNewLineChar(g_discoveryAddr);
870 cout << "\n!! Bad input for IPV4 address. !!";
871 return OC_STACK_INVALID_PARAM;
875 if (g_unicastDiscovery == 0 && g_testCase == TEST_DISCOVER_DEV_REQ)
877 InitDeviceDiscovery(OC_LOW_QOS);
879 else if (g_unicastDiscovery == 0 && g_testCase == TEST_DISCOVER_PLATFORM_REQ)
881 InitPlatformDiscovery(OC_LOW_QOS);
885 InitDiscovery(OC_LOW_QOS);
888 // Break from loop with Ctrl+C
889 OIC_LOG(INFO, TAG, "Entering occlient main loop...");
890 signal(SIGINT, handleSigInt);
894 if (OCProcess() != OC_STACK_OK)
896 cout << "\nOCStack process error\n";
899 #ifndef ROUTING_GATEWAY
904 cout << "\nExiting occlient main loop...\n";
906 if (OCStop() != OC_STACK_OK)
908 cout << "\nOCStack stop error\n";
914 std::string getConnectivityType (OCConnectivityType connType)
916 switch (connType & CT_MASK_ADAPTER)
927 case CT_ADAPTER_GATT_BTLE:
930 case CT_ADAPTER_RFCOMM_BTEDR:
934 return "Incorrect connectivity";
938 std::string getQueryStrForGetPut(OCClientResponse * /*clientResponse*/)
943 void parseClientResponse(OCClientResponse * clientResponse)
945 coapServerResource = getQueryStrForGetPut(clientResponse);