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 if (clientResponse->sequenceNumber <= MAX_SEQUENCE_NUMBER)
294 if (clientResponse->sequenceNumber == OC_OBSERVE_REGISTER)
296 cout << "This also serves as a registration confirmation" << endl;
299 cout << "\nStackResult: " << getResult(clientResponse->result);
300 cout << "\nSEQUENCE NUMBER: " << clientResponse->sequenceNumber;
301 cout << "\nCallback Context for OBSERVE notification recvd successfully ";
302 //OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
303 gNumObserveNotifies++;
305 if (gNumObserveNotifies == 15) //large number to test observing in DELETE case.
307 if (g_testCase == TEST_OBS_REQ_NON || g_testCase == TEST_OBS_REQ_CON)
309 if (OCCancel(gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
311 cout << "Observe cancel error" << endl;
313 return OC_STACK_DELETE_TRANSACTION;
315 else if (g_testCase == TEST_OBS_REQ_NON_CANCEL_IMM)
317 if (OCCancel(gObserveDoHandle, OC_HIGH_QOS, NULL, 0) != OC_STACK_OK)
319 cout << "\nObserve cancel error";
326 OIC_LOG(INFO, TAG, "No observe option header is returned in the response.");
327 OIC_LOG(INFO, TAG, "For a registration request, it means the registration failed");
328 return OC_STACK_DELETE_TRANSACTION;
333 cout << "\nobsReqCB received Null clientResponse";
335 return OC_STACK_KEEP_TRANSACTION;
338 OCStackApplicationResult presenceCB(void* ctx, OCDoHandle /*handle*/,
339 OCClientResponse * clientResponse)
341 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
343 cout << "\nCallback Context for Presence recvd successfully";
348 cout << "\nStackResult: " << getResult(clientResponse->result);
349 cout << "\nNONCE NUMBER: " << clientResponse->sequenceNumber;
350 cout << "\nCallback Context for Presence notification recvd successfully ";
351 //OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
352 gNumPresenceNotifies++;
353 if (gNumPresenceNotifies == 20)
355 if (OCCancel(gPresenceHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
357 cout << "\nPresence cancel error";
359 return OC_STACK_DELETE_TRANSACTION;
364 cout << "\npresenceCB received Null clientResponse";
366 return OC_STACK_KEEP_TRANSACTION;
370 // This is a function called back when a device is discovered
371 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle /*handle*/,
372 OCClientResponse * clientResponse)
374 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
376 cout << "\nCallback Context for DISCOVER query recvd successfully";
381 if (NULL == clientResponse->payload)
383 cout << "\nPayload is NULL, No resources found";
384 return OC_STACK_KEEP_TRANSACTION;
387 cout << "\nStackResult: " << getResult(clientResponse->result);
389 std::string connectionType = getConnectivityType (clientResponse->connType);
390 cout << "\nDiscovered on " << connectionType.c_str();
391 cout << "\nDevice ======> Discovered ";
392 cout << clientResponse->devAddr.addr;
393 if (CT_ADAPTER_IP == clientResponse->connType)
395 cout << ":" << clientResponse->devAddr.port;
397 //OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
398 cout << "\nConnectivity type: " << clientResponse->connType;
399 g_connType = clientResponse->connType;
400 g_serverAddr = clientResponse->devAddr;
401 parseClientResponse(clientResponse);
405 case TEST_GET_REQ_NON:
406 InitGetRequest(OC_LOW_QOS, 0, 0);
408 case TEST_GET_REQ_NON_WITH_FILTERS:
409 InitGetRequest(OC_LOW_QOS, 0, 1);
411 case TEST_PUT_REQ_NON:
412 InitPutRequest(OC_LOW_QOS);
414 case TEST_POST_REQ_NON:
415 InitPostRequest(OC_LOW_QOS);
417 case TEST_DELETE_REQ_NON:
418 InitDeleteRequest(OC_LOW_QOS);
420 case TEST_OBS_REQ_NON:
421 case TEST_OBS_REQ_NON_CANCEL_IMM:
422 InitObserveRequest(OC_LOW_QOS);
424 case TEST_GET_UNAVAILABLE_RES_REQ_NON:
425 InitGetRequestToUnavailableResource(OC_LOW_QOS);
427 case TEST_GET_REQ_CON:
428 InitGetRequest(OC_HIGH_QOS, 0, 0);
430 case TEST_POST_REQ_CON:
431 InitPostRequest(OC_HIGH_QOS);
433 case TEST_DELETE_REQ_CON:
434 InitDeleteRequest(OC_HIGH_QOS);
436 case TEST_OBS_REQ_CON:
437 InitObserveRequest(OC_HIGH_QOS);
440 case TEST_OBS_PRESENCE:
441 case TEST_OBS_PRESENCE_WITH_FILTER:
442 case TEST_OBS_PRESENCE_WITH_FILTERS:
443 case TEST_OBS_MULTICAST_PRESENCE:
447 case TEST_GET_REQ_NON_WITH_VENDOR_HEADER_OPTIONS:
448 InitGetRequest(OC_LOW_QOS, 1, 0);
450 case TEST_DISCOVER_PLATFORM_REQ:
451 InitPlatformDiscovery(OC_LOW_QOS);
453 case TEST_DISCOVER_DEV_REQ:
454 InitDeviceDiscovery(OC_LOW_QOS);
463 cout << "\ndiscoveryReqCB received Null clientResponse";
465 return OC_STACK_KEEP_TRANSACTION;
468 OCStackApplicationResult PlatformDiscoveryReqCB(void* ctx,
469 OCDoHandle /*handle*/,
470 OCClientResponse * clientResponse)
472 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
474 cout << "\nCallback Context for Platform DISCOVER query recvd successfully";
479 //OIC_LOG truncates the response as it is too long.
480 //OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
484 cout << "\nPlatformDiscoveryReqCB received Null clientResponse";
487 return (g_unicastDiscovery) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
490 OCStackApplicationResult DeviceDiscoveryReqCB(void* ctx, OCDoHandle /*handle*/,
491 OCClientResponse * clientResponse)
493 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
495 cout << "\nCallback Context for Device DISCOVER query recvd successfully";
500 //OIC_LOG truncates the response as it is too long.
501 cout << "\nDiscovery response: ";
502 cout << clientResponse->payload;
506 cout << "\nPlatformDiscoveryReqCB received Null clientResponse";
509 return (g_unicastDiscovery) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
515 OCStackResult result = OC_STACK_OK;
516 cout << "\nExecuting " << __func__;
517 std::ostringstream query;
518 std::ostringstream querySuffix;
519 query << OC_RSRVD_PRESENCE_URI;
520 if (g_testCase == TEST_OBS_PRESENCE)
522 result = InvokeOCDoResource(query, &g_serverAddr, OC_REST_PRESENCE,
523 OC_LOW_QOS, presenceCB, NULL, 0);
525 if (g_testCase == TEST_OBS_PRESENCE_WITH_FILTER || g_testCase == TEST_OBS_PRESENCE_WITH_FILTERS)
528 querySuffix << query.str() << "?rt=core.led";
529 result = InvokeOCDoResource(querySuffix, &g_serverAddr, OC_REST_PRESENCE,
530 OC_LOW_QOS, presenceCB, NULL, 0);
532 if (g_testCase == TEST_OBS_PRESENCE_WITH_FILTERS)
534 if (result == OC_STACK_OK)
537 querySuffix << query.str() << "?rt=core.fan";
538 result = InvokeOCDoResource(querySuffix, &g_serverAddr, OC_REST_PRESENCE, OC_LOW_QOS,
539 presenceCB, NULL, 0);
542 if (g_testCase == TEST_OBS_MULTICAST_PRESENCE)
544 if (result == OC_STACK_OK)
546 std::ostringstream multicastPresenceQuery;
547 multicastPresenceQuery.str("");
548 multicastPresenceQuery << "coap://" << OC_MULTICAST_PREFIX << OC_RSRVD_PRESENCE_URI;
549 result = InvokeOCDoResource(multicastPresenceQuery, &g_serverAddr, OC_REST_PRESENCE, OC_LOW_QOS,
550 presenceCB, NULL, 0);
557 int InitGetRequestToUnavailableResource(OCQualityOfService qos)
559 cout << "\nExecuting " << __func__;
560 std::ostringstream query;
561 query << "/SomeUnknownResource";
562 return (InvokeOCDoResource(query, &g_serverAddr, OC_REST_GET, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
566 int InitObserveRequest(OCQualityOfService qos)
568 cout << "\nExecuting " << __func__;
569 std::ostringstream query;
570 query << coapServerResource;
571 return (InvokeOCDoResource(query, &g_serverAddr, OC_REST_OBSERVE,
572 (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, obsReqCB, NULL, 0));
575 int InitPutRequest(OCQualityOfService qos)
577 cout << "\nExecuting " << __func__;
578 std::ostringstream query;
579 query << coapServerResource;
580 return (InvokeOCDoResource(query, &g_serverAddr, OC_REST_PUT, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
584 int InitPostRequest(OCQualityOfService qos)
586 OCStackResult result;
587 cout << "\nExecuting " << __func__;
588 std::ostringstream query;
589 query << coapServerResource;
591 // First POST operation (to create an Light instance)
592 result = InvokeOCDoResource(query, &g_serverAddr, OC_REST_POST,
593 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
595 if (OC_STACK_OK != result)
597 // Error can happen if for example, network connectivity is down
598 cout << "\nFirst POST call did not succeed";
601 // Second POST operation (to create an Light instance)
602 result = InvokeOCDoResource(query, &g_serverAddr, OC_REST_POST,
603 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
605 if (OC_STACK_OK != result)
607 cout << "\nSecond POST call did not succeed";
610 // This POST operation will update the original resourced /a/light
611 return (InvokeOCDoResource(query, &g_serverAddr, OC_REST_POST,
612 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
613 postReqCB, NULL, 0));
616 void* RequestDeleteDeathResourceTask(void* myqos)
618 sleep (30);//long enough to give the server time to finish deleting the resource.
619 std::ostringstream query;
620 query << coapServerResource;
622 cout << "\nExecuting " << __func__;
624 // Second DELETE operation to delete the resource that might have been removed already.
625 OCQualityOfService qos;
635 OCStackResult result = InvokeOCDoResource(query, &g_serverAddr, OC_REST_DELETE,
637 deleteReqCB, NULL, 0);
639 if (OC_STACK_OK != result)
641 cout << "\nSecond DELETE call did not succeed";
647 int InitDeleteRequest(OCQualityOfService qos)
649 OCStackResult result;
650 std::ostringstream query;
651 query << coapServerResource;
653 cout << "\nExecuting " << __func__;
655 // First DELETE operation
656 result = InvokeOCDoResource(query, &g_serverAddr, OC_REST_DELETE,
658 deleteReqCB, NULL, 0);
659 if (OC_STACK_OK != result)
661 // Error can happen if for example, network connectivity is down
662 cout << "\nFirst DELETE call did not succeed";
666 //Create a thread to delete this resource again
668 pthread_create (&threadId, NULL, RequestDeleteDeathResourceTask, (void*)qos);
671 cout << "\nExit " << __func__;
675 int InitGetRequest(OCQualityOfService qos, uint8_t withVendorSpecificHeaderOptions, bool getWithQuery)
678 OCHeaderOption options[MAX_HEADER_OPTIONS];
680 cout << "\nExecuting " << __func__;
681 std::ostringstream query;
682 query << coapServerResource;
684 // ocserver is written to only process "power<X" query.
687 cout << "\nUsing query power<30";
688 query << "?power<50";
691 if (withVendorSpecificHeaderOptions)
693 uint8_t option0[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
694 uint8_t option1[] = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
695 memset(options, 0, sizeof(OCHeaderOption) * MAX_HEADER_OPTIONS);
696 options[0].protocolID = OC_COAP_ID;
697 options[0].optionID = 2048;
698 memcpy(options[0].optionData, option0, sizeof(option0));
699 options[0].optionLength = 10;
700 options[1].protocolID = OC_COAP_ID;
701 options[1].optionID = 3000;
702 memcpy(options[1].optionData, option1, sizeof(option1));
703 options[1].optionLength = 10;
705 if (withVendorSpecificHeaderOptions)
707 return (InvokeOCDoResource(query, &g_serverAddr, OC_REST_GET,
708 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, options, 2));
712 return (InvokeOCDoResource(query, &g_serverAddr, OC_REST_GET,
713 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, NULL, 0));
717 int InitPlatformDiscovery(OCQualityOfService qos)
719 cout << "\nExecuting " << __func__;
722 OCCallbackData cbData;
723 char szQueryUri[MAX_QUERY_LENGTH] = { 0 };
725 snprintf(szQueryUri, sizeof (szQueryUri) - 1, PLATFORM_DISCOVERY_QUERY, g_discoveryAddr);
727 cbData.cb = PlatformDiscoveryReqCB;
728 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
731 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
732 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
734 if (ret != OC_STACK_OK)
736 cout << "\nOCStack device error";
742 int InitDeviceDiscovery(OCQualityOfService qos)
744 cout << "\nExecuting " << __func__;
747 OCCallbackData cbData;
748 char szQueryUri[MAX_QUERY_LENGTH] = { 0 };
750 snprintf(szQueryUri, sizeof (szQueryUri) - 1, DEVICE_DISCOVERY_QUERY, g_discoveryAddr);
752 cbData.cb = DeviceDiscoveryReqCB;
753 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
756 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
757 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
759 if (ret != OC_STACK_OK)
761 cout << "\nOCStack device error";
767 int InitDiscovery(OCQualityOfService qos)
770 OCCallbackData cbData;
771 char szQueryUri[MAX_QUERY_LENGTH] = { 0 };
773 snprintf(szQueryUri, sizeof (szQueryUri) - 1, RESOURCE_DISCOVERY_QUERY, g_discoveryAddr);
775 cbData.cb = discoveryReqCB;
776 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
779 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
780 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
782 if (ret != OC_STACK_OK)
784 cout << "\nOCStack resource error";
789 void *GMainLoopThread(void *param)
792 if (g_unicastDiscovery == 0 && g_testCase == TEST_DISCOVER_DEV_REQ)
794 InitDeviceDiscovery(OC_LOW_QOS);
796 else if (g_unicastDiscovery == 0 && g_testCase == TEST_DISCOVER_PLATFORM_REQ)
798 InitPlatformDiscovery(OC_LOW_QOS);
802 InitDiscovery(OC_LOW_QOS);
807 if (OCProcess() != OC_STACK_OK)
809 cout << "\nOCStack process error";
812 #ifndef ROUTING_GATEWAY
819 g_main_loop_quit(g_mainloop);
824 int main(int argc, char* argv[])
828 g_mainloop = g_main_loop_new(NULL, FALSE);
831 printf("g_main_loop_new failed\n");
835 while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
840 g_unicastDiscovery = atoi(optarg);
843 g_testCase = atoi(optarg);
846 g_connectivity = atoi(optarg);
854 if ((g_unicastDiscovery != 0 && g_unicastDiscovery != 1) ||
855 (g_testCase < TEST_DISCOVER_REQ || g_testCase >= MAX_TESTS) ||
856 (g_connectivity < CT_ADAPTER_DEFAULT || g_connectivity >= MAX_CT))
862 cout << "\nEntering occlient main loop...\n";
864 if (OCInit1(OC_CLIENT, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
866 cout << "\nOCStack init error";
870 #ifdef ROUTING_GATEWAY
872 * Before invoking Discover resource, we process the gateway requests
873 * and form the routing table.
875 for (int index = 0; index < MAX_NUM_GATEWAY_REQUEST; index++)
877 if (OCProcess() != OC_STACK_OK)
879 OIC_LOG(ERROR, TAG, "OCStack process error");
882 usleep(SLEEP_DURATION);
886 if (g_connectivity == CT_ADAPTER_DEFAULT || g_connectivity == CT_IP)
888 g_connType = CT_ADAPTER_IP;
890 else if(g_connectivity == CT_EDR)
892 cout << "\nSelected EDR Adapter\n";
893 g_connType = CT_ADAPTER_RFCOMM_BTEDR;
897 cout << "\nDefault Connectivity type selected...";
901 g_discoveryAddr[0] = '\0';
903 if (g_unicastDiscovery)
905 cout << "\nEnter address of Server hosting resource as given below:";
906 cout << "\nIP Adapter: 192.168.0.15:45454(IP:Port)";
907 cout << "\nEDR/BLE Adapter: AB:BC:CD:DE:EF:FG(MAC Address)";
910 if (fgets(g_discoveryAddr, sizeof (g_discoveryAddr), stdin))
912 //Strip newline char from unicastAddr
913 StripNewLineChar(g_discoveryAddr);
917 cout << "\n!! Bad input for IPV4 address. !!";
918 return OC_STACK_INVALID_PARAM;
923 // Break from loop with Ctrl+C
924 OIC_LOG(INFO, TAG, "Entering occlient main loop...");
925 signal(SIGINT, handleSigInt);
927 int result = pthread_create(&g_thread, NULL, GMainLoopThread, (void *)NULL);
930 printf("pthread_create failed in initialize\n");
934 g_main_loop_run(g_mainloop);
936 cout << "\nExiting occlient main loop...\n";
938 if (OCStop() != OC_STACK_OK)
940 cout << "\nOCStack stop error\n";
946 std::string getConnectivityType (OCConnectivityType connType)
948 switch (connType & CT_MASK_ADAPTER)
959 case CT_ADAPTER_GATT_BTLE:
962 case CT_ADAPTER_RFCOMM_BTEDR:
966 return "Incorrect connectivity";
970 std::string getQueryStrForGetPut(OCClientResponse * /*clientResponse*/)
975 void parseClientResponse(OCClientResponse * clientResponse)
977 coapServerResource = getQueryStrForGetPut(clientResponse);