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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
33 #include "ocpayload.h"
34 #include "payload_logging.h"
38 #ifdef ROUTING_GATEWAY
40 * Maximum number of gateway requests to form the routing table.
42 #define MAX_NUM_GATEWAY_REQUEST 20
45 * Sleep duration after every OCProcess().
47 #define SLEEP_DURATION 100000
50 // Tracking user input
51 static int g_unicastDiscovery = 0;
52 static int g_testCase = 0;
53 static int g_connectivity = 0;
55 static GMainLoop *g_mainloop = NULL;
58 static const char *DEVICE_DISCOVERY_QUERY = "%s/oic/d";
59 static const char *PLATFORM_DISCOVERY_QUERY = "%s/oic/p";
60 static const char *RESOURCE_DISCOVERY_QUERY = "%s/oic/res";
62 //The following variable determines the interface protocol (IPv4, IPv6, etc)
63 //to be used for sending unicast messages. Default set to IP dual stack.
64 static OCConnectivityType g_connType = CT_ADAPTER_IP;
65 static OCDevAddr g_serverAddr;
66 static char g_discoveryAddr[100];
67 static std::string coapServerResource = "/a/light";
69 void StripNewLineChar(char* str);
71 // The handle for the observe registration
72 OCDoHandle gObserveDoHandle;
74 // The handle for observe registration
75 OCDoHandle gPresenceHandle;
77 // After this crosses a threshold client deregisters for further notifications
78 int gNumObserveNotifies = 0;
81 int gNumPresenceNotifies = 0;
85 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
86 void handleSigInt(int signum)
94 OCPayload* putPayload()
96 OCRepPayload* payload = OCRepPayloadCreate();
100 std::cout << "Failed to create put payload object"<<std::endl;
104 OCRepPayloadSetPropInt(payload, "power", 15);
105 OCRepPayloadSetPropBool(payload, "state", true);
107 return (OCPayload*) payload;
110 static void PrintUsage()
113 cout << "\nUsage : occlient -u <0|1> -t <1..17> -c <0|1|2>";
114 cout << "\n-u <0|1> : Perform multicast/unicast discovery of resources";
115 cout << "\n-c 0 : Default IP selection";
116 cout << "\n-c 1 : IP Connectivity Type";
117 cout << "\n-c 2 : EDR Connectivity Type (IPv6 not currently supported)";
118 cout << "\n-t 1 : Discover Resources";
119 cout << "\n-t 2 : Discover Resources and Initiate Nonconfirmable Get Request";
120 cout << "\n-t 3 : Discover Resources and Initiate Nonconfirmable Get Request with query filter";
121 cout << "\n-t 4 : Discover Resources and Initiate Nonconfirmable Put Requests";
122 cout << "\n-t 5 : Discover Resources and Initiate Nonconfirmable Post Requests";
123 cout << "\n-t 6 : Discover Resources and Initiate Nonconfirmable Delete Requests";
124 cout << "\n-t 7 : Discover Resources and Initiate Nonconfirmable Observe Requests";
125 cout << "\n-t 8 : Discover Resources and Initiate Nonconfirmable Get Request for a resource";
126 cout << "which is unavailable";
127 cout << "\n-t 9 : Discover Resources and Initiate Confirmable Get Request";
128 cout << "\n-t 10 : Discover Resources and Initiate Confirmable Post Request";
129 cout << "\n-t 11 : Discover Resources and Initiate Confirmable Delete Requests";
130 cout << "\n-t 12 : Discover Resources and Initiate Confirmable Observe Requests and";
131 cout << "cancel with Low QoS";
134 cout << "\n-t 13 : Discover Resources and Initiate Nonconfirmable presence";
135 cout << "\n-t 14 : Discover Resources and Initiate Nonconfirmable presence with filter";
136 cout << "\n-t 15 : Discover Resources and Initiate Nonconfirmable presence with 2 filters";
137 cout << "\n-t 16 : Discover Resources and Initiate Nonconfirmable multicast presence.";
140 cout << "\n-t 17 : Discover Resources and Initiate Nonconfirmable Observe Requests";
141 cout << "then cancel immediately with High QOS";
142 cout << "\n-t 18 : Discover Resources and Initiate Nonconfirmable Get Request and add";
143 cout << "vendor specific header options";
144 cout << "\n-t 19 : Discover Platform";
145 cout << "\n-t 20 : Discover Devices";
148 OCStackResult InvokeOCDoResource(std::ostringstream &query,
149 OCDevAddr *remoteAddr,
151 OCQualityOfService qos,
152 OCClientResponseHandler cb,
153 OCHeaderOption * options,
157 OCCallbackData cbData;
161 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
164 ret = OCDoResource(&handle, method, query.str().c_str(), remoteAddr,
165 (method == OC_REST_PUT) ? putPayload() : NULL,
166 (g_connType), qos, &cbData, options, numOptions);
168 if (ret != OC_STACK_OK)
170 cout << "\nOCDoResource returns error "<< ret;
171 cout << " with method " << method;
173 else if (method == OC_REST_OBSERVE || method == OC_REST_OBSERVE_ALL)
175 gObserveDoHandle = handle;
178 else if (method == OC_REST_PRESENCE)
180 gPresenceHandle = handle;
187 OCStackApplicationResult putReqCB(void* ctx, OCDoHandle /*handle*/,
188 OCClientResponse * clientResponse)
190 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
192 cout << "\nCallback Context for PUT recvd successfully";
197 cout << "\nStackResult: " << getResult(clientResponse->result);
198 cout << "\nJSON = " << clientResponse->payload;
202 cout << "\nputReqCB received Null clientResponse";
204 return OC_STACK_DELETE_TRANSACTION;
207 OCStackApplicationResult postReqCB(void *ctx, OCDoHandle /*handle*/,
208 OCClientResponse *clientResponse)
210 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
212 cout << "\nCallback Context for POST recvd successfully";
217 cout << "\nStackResult: " << getResult(clientResponse->result);
218 cout << "\nJSON = " << clientResponse->payload;
222 cout << "\npostReqCB received Null clientResponse";
224 return OC_STACK_DELETE_TRANSACTION;
227 OCStackApplicationResult deleteReqCB(void *ctx,
228 OCDoHandle /*handle*/,
229 OCClientResponse *clientResponse)
231 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
233 cout << "\nCallback Context for DELETE recvd successfully";
238 cout << "\nStackResult: " << getResult(clientResponse->result);
239 //OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
243 cout << "\ndeleteReqCB received Null clientResponse";
245 return OC_STACK_DELETE_TRANSACTION;
248 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle /*handle*/,
249 OCClientResponse * clientResponse)
251 if (clientResponse == NULL)
253 cout << "\ngetReqCB received NULL clientResponse";
254 return OC_STACK_DELETE_TRANSACTION;
257 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
259 cout << "\nCallback Context for GET query recvd successfully";
262 cout << "\nStackResult: " << getResult(clientResponse->result);
263 cout << "\nSEQUENCE NUMBER: " << clientResponse->sequenceNumber;
264 //OIC_LOG_PAYLOAD(INFO, TAG, clientResponse->payload);
266 if (clientResponse->numRcvdVendorSpecificHeaderOptions > 0)
268 cout << "\nReceived vendor specific options";
270 OCHeaderOption * rcvdOptions = clientResponse->rcvdVendorSpecificHeaderOptions;
271 for( i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
273 if (((OCHeaderOption)rcvdOptions[i]).protocolID == OC_COAP_ID)
275 cout << "\nReceived option ID " << ((OCHeaderOption)rcvdOptions[i]).optionID;
279 return OC_STACK_DELETE_TRANSACTION;
282 OCStackApplicationResult obsReqCB(void* ctx, OCDoHandle /*handle*/,
283 OCClientResponse * clientResponse)
285 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
287 cout << "\nCallback Context for OBS query recvd successfully";
292 cout << "\nStackResult: " << getResult(clientResponse->result);
293 cout << "\nSEQUENCE NUMBER: " << clientResponse->sequenceNumber;
294 cout << "\nCallback Context for OBSERVE notification recvd successfully ";
295 //OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
296 gNumObserveNotifies++;
297 if (gNumObserveNotifies == 15) //large number to test observing in DELETE case.
299 if (g_testCase == TEST_OBS_REQ_NON || g_testCase == TEST_OBS_REQ_CON)
301 if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
303 cout << "\nObserve cancel error";
305 return OC_STACK_DELETE_TRANSACTION;
307 else if (g_testCase == TEST_OBS_REQ_NON_CANCEL_IMM)
309 if (OCCancel (gObserveDoHandle, OC_HIGH_QOS, NULL, 0) != OC_STACK_OK)
311 cout << "\nObserve cancel error";
315 if (clientResponse->sequenceNumber == OC_OBSERVE_REGISTER)
317 cout << "\nThis also serves as a registration confirmation";
319 else if (clientResponse->sequenceNumber == OC_OBSERVE_DEREGISTER)
321 cout << "\nThis also serves as a deregistration confirmation";
322 return OC_STACK_DELETE_TRANSACTION;
324 else if (clientResponse->sequenceNumber == OC_OBSERVE_NO_OPTION)
326 cout << "\nThis also tells you that registration/deregistration failed";
327 return OC_STACK_DELETE_TRANSACTION;
332 cout << "\nobsReqCB received Null clientResponse";
334 return OC_STACK_KEEP_TRANSACTION;
337 OCStackApplicationResult presenceCB(void* ctx, OCDoHandle /*handle*/,
338 OCClientResponse * clientResponse)
340 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
342 cout << "\nCallback Context for Presence recvd successfully";
347 cout << "\nStackResult: " << getResult(clientResponse->result);
348 cout << "\nNONCE NUMBER: " << clientResponse->sequenceNumber;
349 cout << "\nCallback Context for Presence notification recvd successfully ";
350 //OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
351 gNumPresenceNotifies++;
352 if (gNumPresenceNotifies == 20)
354 if (OCCancel(gPresenceHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
356 cout << "\nPresence cancel error";
358 return OC_STACK_DELETE_TRANSACTION;
363 cout << "\npresenceCB received Null clientResponse";
365 return OC_STACK_KEEP_TRANSACTION;
369 // This is a function called back when a device is discovered
370 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle /*handle*/,
371 OCClientResponse * clientResponse)
373 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
375 cout << "\nCallback Context for DISCOVER query recvd successfully";
380 if (NULL == clientResponse->payload)
382 cout << "\nPayload is NULL, No resources found";
383 return OC_STACK_KEEP_TRANSACTION;
386 cout << "\nStackResult: " << getResult(clientResponse->result);
388 std::string connectionType = getConnectivityType (clientResponse->connType);
389 cout << "\nDiscovered on " << connectionType.c_str();
390 cout << "\nDevice ======> Discovered ";
391 cout << clientResponse->devAddr.addr;
392 if (CT_ADAPTER_IP == clientResponse->connType)
394 cout << ":" << clientResponse->devAddr.port;
396 //OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
397 cout << "\nConnectivity type: " << clientResponse->connType;
398 g_connType = clientResponse->connType;
399 g_serverAddr = clientResponse->devAddr;
400 parseClientResponse(clientResponse);
404 case TEST_GET_REQ_NON:
405 InitGetRequest(OC_LOW_QOS, 0, 0);
407 case TEST_GET_REQ_NON_WITH_FILTERS:
408 InitGetRequest(OC_LOW_QOS, 0, 1);
410 case TEST_PUT_REQ_NON:
411 InitPutRequest(OC_LOW_QOS);
413 case TEST_POST_REQ_NON:
414 InitPostRequest(OC_LOW_QOS);
416 case TEST_DELETE_REQ_NON:
417 InitDeleteRequest(OC_LOW_QOS);
419 case TEST_OBS_REQ_NON:
420 case TEST_OBS_REQ_NON_CANCEL_IMM:
421 InitObserveRequest(OC_LOW_QOS);
423 case TEST_GET_UNAVAILABLE_RES_REQ_NON:
424 InitGetRequestToUnavailableResource(OC_LOW_QOS);
426 case TEST_GET_REQ_CON:
427 InitGetRequest(OC_HIGH_QOS, 0, 0);
429 case TEST_POST_REQ_CON:
430 InitPostRequest(OC_HIGH_QOS);
432 case TEST_DELETE_REQ_CON:
433 InitDeleteRequest(OC_HIGH_QOS);
435 case TEST_OBS_REQ_CON:
436 InitObserveRequest(OC_HIGH_QOS);
439 case TEST_OBS_PRESENCE:
440 case TEST_OBS_PRESENCE_WITH_FILTER:
441 case TEST_OBS_PRESENCE_WITH_FILTERS:
442 case TEST_OBS_MULTICAST_PRESENCE:
446 case TEST_GET_REQ_NON_WITH_VENDOR_HEADER_OPTIONS:
447 InitGetRequest(OC_LOW_QOS, 1, 0);
449 case TEST_DISCOVER_PLATFORM_REQ:
450 InitPlatformDiscovery(OC_LOW_QOS);
452 case TEST_DISCOVER_DEV_REQ:
453 InitDeviceDiscovery(OC_LOW_QOS);
462 cout << "\ndiscoveryReqCB received Null clientResponse";
464 return OC_STACK_KEEP_TRANSACTION;
467 OCStackApplicationResult PlatformDiscoveryReqCB(void* ctx,
468 OCDoHandle /*handle*/,
469 OCClientResponse * clientResponse)
471 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
473 cout << "\nCallback Context for Platform DISCOVER query recvd successfully";
478 //OIC_LOG truncates the response as it is too long.
479 //OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
483 cout << "\nPlatformDiscoveryReqCB received Null clientResponse";
486 return (g_unicastDiscovery) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
489 OCStackApplicationResult DeviceDiscoveryReqCB(void* ctx, OCDoHandle /*handle*/,
490 OCClientResponse * clientResponse)
492 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
494 cout << "\nCallback Context for Device DISCOVER query recvd successfully";
499 //OIC_LOG truncates the response as it is too long.
500 cout << "\nDiscovery response: ";
501 cout << clientResponse->payload;
505 cout << "\nPlatformDiscoveryReqCB received Null clientResponse";
508 return (g_unicastDiscovery) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
514 OCStackResult result = OC_STACK_OK;
515 cout << "\nExecuting " << __func__;
516 std::ostringstream query;
517 std::ostringstream querySuffix;
518 query << OC_RSRVD_PRESENCE_URI;
519 if (g_testCase == TEST_OBS_PRESENCE)
521 result = InvokeOCDoResource(query, &g_serverAddr, OC_REST_PRESENCE,
522 OC_LOW_QOS, presenceCB, NULL, 0);
524 if (g_testCase == TEST_OBS_PRESENCE_WITH_FILTER || g_testCase == TEST_OBS_PRESENCE_WITH_FILTERS)
527 querySuffix << query.str() << "?rt=core.led";
528 result = InvokeOCDoResource(querySuffix, &g_serverAddr, OC_REST_PRESENCE,
529 OC_LOW_QOS, presenceCB, NULL, 0);
531 if (g_testCase == TEST_OBS_PRESENCE_WITH_FILTERS)
533 if (result == OC_STACK_OK)
536 querySuffix << query.str() << "?rt=core.fan";
537 result = InvokeOCDoResource(querySuffix, &g_serverAddr, OC_REST_PRESENCE, OC_LOW_QOS,
538 presenceCB, NULL, 0);
541 if (g_testCase == TEST_OBS_MULTICAST_PRESENCE)
543 if (result == OC_STACK_OK)
545 std::ostringstream multicastPresenceQuery;
546 multicastPresenceQuery.str("");
547 multicastPresenceQuery << "coap://" << OC_MULTICAST_PREFIX << OC_RSRVD_PRESENCE_URI;
548 result = InvokeOCDoResource(multicastPresenceQuery, &g_serverAddr, OC_REST_PRESENCE, OC_LOW_QOS,
549 presenceCB, NULL, 0);
556 int InitGetRequestToUnavailableResource(OCQualityOfService qos)
558 cout << "\nExecuting " << __func__;
559 std::ostringstream query;
560 query << "/SomeUnknownResource";
561 return (InvokeOCDoResource(query, &g_serverAddr, OC_REST_GET, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
565 int InitObserveRequest(OCQualityOfService qos)
567 cout << "\nExecuting " << __func__;
568 std::ostringstream query;
569 query << coapServerResource;
570 return (InvokeOCDoResource(query, &g_serverAddr, OC_REST_OBSERVE,
571 (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, obsReqCB, NULL, 0));
574 int InitPutRequest(OCQualityOfService qos)
576 cout << "\nExecuting " << __func__;
577 std::ostringstream query;
578 query << coapServerResource;
579 return (InvokeOCDoResource(query, &g_serverAddr, OC_REST_PUT, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
583 int InitPostRequest(OCQualityOfService qos)
585 OCStackResult result;
586 cout << "\nExecuting " << __func__;
587 std::ostringstream query;
588 query << coapServerResource;
590 // First POST operation (to create an Light instance)
591 result = InvokeOCDoResource(query, &g_serverAddr, OC_REST_POST,
592 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
594 if (OC_STACK_OK != result)
596 // Error can happen if for example, network connectivity is down
597 cout << "\nFirst POST call did not succeed";
600 // Second POST operation (to create an Light instance)
601 result = InvokeOCDoResource(query, &g_serverAddr, OC_REST_POST,
602 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
604 if (OC_STACK_OK != result)
606 cout << "\nSecond POST call did not succeed";
609 // This POST operation will update the original resourced /a/light
610 return (InvokeOCDoResource(query, &g_serverAddr, OC_REST_POST,
611 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
612 postReqCB, NULL, 0));
615 void* RequestDeleteDeathResourceTask(void* myqos)
617 sleep (30);//long enough to give the server time to finish deleting the resource.
618 std::ostringstream query;
619 query << coapServerResource;
621 cout << "\nExecuting " << __func__;
623 // Second DELETE operation to delete the resource that might have been removed already.
624 OCQualityOfService qos;
634 OCStackResult result = InvokeOCDoResource(query, &g_serverAddr, OC_REST_DELETE,
636 deleteReqCB, NULL, 0);
638 if (OC_STACK_OK != result)
640 cout << "\nSecond DELETE call did not succeed";
646 int InitDeleteRequest(OCQualityOfService qos)
648 OCStackResult result;
649 std::ostringstream query;
650 query << coapServerResource;
652 cout << "\nExecuting " << __func__;
654 // First DELETE operation
655 result = InvokeOCDoResource(query, &g_serverAddr, OC_REST_DELETE,
657 deleteReqCB, NULL, 0);
658 if (OC_STACK_OK != result)
660 // Error can happen if for example, network connectivity is down
661 cout << "\nFirst DELETE call did not succeed";
665 //Create a thread to delete this resource again
667 pthread_create (&threadId, NULL, RequestDeleteDeathResourceTask, (void*)qos);
670 cout << "\nExit " << __func__;
674 int InitGetRequest(OCQualityOfService qos, uint8_t withVendorSpecificHeaderOptions, bool getWithQuery)
677 OCHeaderOption options[MAX_HEADER_OPTIONS];
679 cout << "\nExecuting " << __func__;
680 std::ostringstream query;
681 query << coapServerResource;
683 // ocserver is written to only process "power<X" query.
686 cout << "\nUsing query power<30";
687 query << "?power<50";
690 if (withVendorSpecificHeaderOptions)
692 uint8_t option0[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
693 uint8_t option1[] = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
694 memset(options, 0, sizeof(OCHeaderOption) * MAX_HEADER_OPTIONS);
695 options[0].protocolID = OC_COAP_ID;
696 options[0].optionID = 2048;
697 memcpy(options[0].optionData, option0, sizeof(option0));
698 options[0].optionLength = 10;
699 options[1].protocolID = OC_COAP_ID;
700 options[1].optionID = 3000;
701 memcpy(options[1].optionData, option1, sizeof(option1));
702 options[1].optionLength = 10;
704 if (withVendorSpecificHeaderOptions)
706 return (InvokeOCDoResource(query, &g_serverAddr, OC_REST_GET,
707 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, options, 2));
711 return (InvokeOCDoResource(query, &g_serverAddr, OC_REST_GET,
712 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, NULL, 0));
716 int InitPlatformDiscovery(OCQualityOfService qos)
718 cout << "\nExecuting " << __func__;
721 OCCallbackData cbData;
722 char szQueryUri[MAX_QUERY_LENGTH] = { 0 };
724 snprintf(szQueryUri, sizeof (szQueryUri) - 1, PLATFORM_DISCOVERY_QUERY, g_discoveryAddr);
726 cbData.cb = PlatformDiscoveryReqCB;
727 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
730 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
731 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
733 if (ret != OC_STACK_OK)
735 cout << "\nOCStack device error";
741 int InitDeviceDiscovery(OCQualityOfService qos)
743 cout << "\nExecuting " << __func__;
746 OCCallbackData cbData;
747 char szQueryUri[MAX_QUERY_LENGTH] = { 0 };
749 snprintf(szQueryUri, sizeof (szQueryUri) - 1, DEVICE_DISCOVERY_QUERY, g_discoveryAddr);
751 cbData.cb = DeviceDiscoveryReqCB;
752 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
755 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
756 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
758 if (ret != OC_STACK_OK)
760 cout << "\nOCStack device error";
766 int InitDiscovery(OCQualityOfService qos)
769 OCCallbackData cbData;
770 char szQueryUri[MAX_QUERY_LENGTH] = { 0 };
772 snprintf(szQueryUri, sizeof (szQueryUri) - 1, RESOURCE_DISCOVERY_QUERY, g_discoveryAddr);
774 cbData.cb = discoveryReqCB;
775 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
778 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
779 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
781 if (ret != OC_STACK_OK)
783 cout << "\nOCStack resource error";
788 void *GMainLoopThread(void *param)
791 if (g_unicastDiscovery == 0 && g_testCase == TEST_DISCOVER_DEV_REQ)
793 InitDeviceDiscovery(OC_LOW_QOS);
795 else if (g_unicastDiscovery == 0 && g_testCase == TEST_DISCOVER_PLATFORM_REQ)
797 InitPlatformDiscovery(OC_LOW_QOS);
801 InitDiscovery(OC_LOW_QOS);
806 if (OCProcess() != OC_STACK_OK)
808 cout << "\nOCStack process error";
811 #ifndef ROUTING_GATEWAY
818 g_main_loop_quit(g_mainloop);
823 int main(int argc, char* argv[])
827 g_mainloop = g_main_loop_new(NULL, FALSE);
830 printf("g_main_loop_new failed\n");
834 while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
839 g_unicastDiscovery = atoi(optarg);
842 g_testCase = atoi(optarg);
845 g_connectivity = atoi(optarg);
853 if ((g_unicastDiscovery != 0 && g_unicastDiscovery != 1) ||
854 (g_testCase < TEST_DISCOVER_REQ || g_testCase >= MAX_TESTS) ||
855 (g_connectivity < CT_ADAPTER_DEFAULT || g_connectivity >= MAX_CT))
861 cout << "\nEntering occlient main loop...\n";
863 if (OCInit1(OC_CLIENT, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
865 cout << "\nOCStack init error";
869 #ifdef ROUTING_GATEWAY
871 * Before invoking Discover resource, we process the gateway requests
872 * and form the routing table.
874 for (int index = 0; index < MAX_NUM_GATEWAY_REQUEST; index++)
876 if (OCProcess() != OC_STACK_OK)
878 OIC_LOG(ERROR, TAG, "OCStack process error");
881 usleep(SLEEP_DURATION);
885 if (g_connectivity == CT_ADAPTER_DEFAULT || g_connectivity == CT_IP)
887 g_connType = CT_ADAPTER_IP;
889 else if(g_connectivity == CT_EDR)
891 cout << "\nSelected EDR Adapter\n";
892 g_connType = CT_ADAPTER_RFCOMM_BTEDR;
896 cout << "\nDefault Connectivity type selected...";
900 g_discoveryAddr[0] = '\0';
902 if (g_unicastDiscovery)
904 cout << "\nEnter address of Server hosting resource as given below:";
905 cout << "\nIP Adapter: 192.168.0.15:45454(IP:Port)";
906 cout << "\nEDR/BLE Adapter: AB:BC:CD:DE:EF:FG(MAC Address)";
909 if (fgets(g_discoveryAddr, sizeof (g_discoveryAddr), stdin))
911 //Strip newline char from unicastAddr
912 StripNewLineChar(g_discoveryAddr);
916 cout << "\n!! Bad input for IPV4 address. !!";
917 return OC_STACK_INVALID_PARAM;
922 // Break from loop with Ctrl+C
923 OIC_LOG(INFO, TAG, "Entering occlient main loop...");
924 signal(SIGINT, handleSigInt);
926 int result = pthread_create(&g_thread, NULL, GMainLoopThread, (void *)NULL);
929 printf("pthread_create failed in initialize\n");
933 g_main_loop_run(g_mainloop);
935 cout << "\nExiting occlient main loop...\n";
937 if (OCStop() != OC_STACK_OK)
939 cout << "\nOCStack stop error\n";
945 std::string getConnectivityType (OCConnectivityType connType)
947 switch (connType & CT_MASK_ADAPTER)
958 case CT_ADAPTER_GATT_BTLE:
961 case CT_ADAPTER_RFCOMM_BTEDR:
965 return "Incorrect connectivity";
969 std::string getQueryStrForGetPut(OCClientResponse * /*clientResponse*/)
974 void parseClientResponse(OCClientResponse * clientResponse)
976 coapServerResource = getQueryStrForGetPut(clientResponse);