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 static const char *DEFAULT_DB_FILE_PATH = "/opt/usr/etc/oic_svr_db_client.dat";
64 //The following variable determines the interface protocol (IPv4, IPv6, etc)
65 //to be used for sending unicast messages. Default set to IP dual stack.
66 static OCConnectivityType g_connType = CT_ADAPTER_IP;
67 static OCDevAddr g_serverAddr;
68 static char g_discoveryAddr[100];
69 static std::string coapServerResource = "/a/light";
71 void StripNewLineChar(char* str);
73 // The handle for the observe registration
74 OCDoHandle gObserveDoHandle;
76 // The handle for observe registration
77 OCDoHandle gPresenceHandle;
79 // After this crosses a threshold client deregisters for further notifications
80 int gNumObserveNotifies = 0;
83 int gNumPresenceNotifies = 0;
87 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
88 void handleSigInt(int signum)
96 OCPayload* putPayload()
98 OCRepPayload* payload = OCRepPayloadCreate();
102 std::cout << "Failed to create put payload object"<<std::endl;
106 OCRepPayloadSetPropInt(payload, "power", 15);
107 OCRepPayloadSetPropBool(payload, "state", true);
109 return (OCPayload*) payload;
112 static void PrintUsage()
115 cout << "\nUsage : occlient -u <0|1> -t <1..17> -c <0|1|2|3>";
116 cout << "\n-u <0|1> : Perform multicast/unicast discovery of resources";
117 cout << "\n-c 0 : Default IP selection";
118 cout << "\n-c 1 : IP Connectivity Type";
119 cout << "\n-c 2 : EDR Connectivity Type (IPv6 not currently supported)";
120 cout << "\n-c 3 : TCP Connectivity Type";
121 cout << "\n-t 1 : Discover Resources";
122 cout << "\n-t 2 : Discover Resources and Initiate Nonconfirmable Get Request";
123 cout << "\n-t 3 : Discover Resources and Initiate Nonconfirmable Get Request with query filter";
124 cout << "\n-t 4 : Discover Resources and Initiate Nonconfirmable Put Requests";
125 cout << "\n-t 5 : Discover Resources and Initiate Nonconfirmable Post Requests";
126 cout << "\n-t 6 : Discover Resources and Initiate Nonconfirmable Delete Requests";
127 cout << "\n-t 7 : Discover Resources and Initiate Nonconfirmable Observe Requests";
128 cout << "\n-t 8 : Discover Resources and Initiate Nonconfirmable Get Request for a resource";
129 cout << "which is unavailable";
130 cout << "\n-t 9 : Discover Resources and Initiate Confirmable Get Request";
131 cout << "\n-t 10 : Discover Resources and Initiate Confirmable Post Request";
132 cout << "\n-t 11 : Discover Resources and Initiate Confirmable Delete Requests";
133 cout << "\n-t 12 : Discover Resources and Initiate Confirmable Observe Requests and";
134 cout << "cancel with Low QoS";
137 cout << "\n-t 13 : Discover Resources and Initiate Nonconfirmable presence";
138 cout << "\n-t 14 : Discover Resources and Initiate Nonconfirmable presence with filter";
139 cout << "\n-t 15 : Discover Resources and Initiate Nonconfirmable presence with 2 filters";
140 cout << "\n-t 16 : Discover Resources and Initiate Nonconfirmable multicast presence.";
143 cout << "\n-t 17 : Discover Resources and Initiate Nonconfirmable Observe Requests";
144 cout << "then cancel immediately with High QOS";
145 cout << "\n-t 18 : Discover Resources and Initiate Nonconfirmable Get Request and add";
146 cout << "vendor specific header options";
147 cout << "\n-t 19 : Discover Platform";
148 cout << "\n-t 20 : Discover Devices";
151 OCStackResult InvokeOCDoResource(std::ostringstream &query,
152 OCDevAddr *remoteAddr,
154 OCQualityOfService qos,
155 OCClientResponseHandler cb,
156 OCHeaderOption * options,
160 OCCallbackData cbData;
164 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
167 OCPayload* payload = (method == OC_REST_PUT) ? putPayload() : NULL;
169 ret = OCDoRequest(&handle, method, query.str().c_str(), remoteAddr,
170 payload, (g_connType), qos, &cbData, options, numOptions);
172 OCPayloadDestroy(payload);
174 if (ret != OC_STACK_OK)
176 cout << "\nOCDoResource returns error "<< ret;
177 cout << " with method " << method;
179 else if (method == OC_REST_OBSERVE || method == OC_REST_OBSERVE_ALL)
181 gObserveDoHandle = handle;
184 else if (method == OC_REST_PRESENCE)
186 gPresenceHandle = handle;
193 OCStackApplicationResult putReqCB(void* ctx, OCDoHandle /*handle*/,
194 OCClientResponse * clientResponse)
196 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
198 cout << "\nCallback Context for PUT recvd successfully";
203 cout << "\nStackResult: " << getResult(clientResponse->result);
204 cout << "\nJSON = " << clientResponse->payload;
208 cout << "\nputReqCB received Null clientResponse";
210 return OC_STACK_DELETE_TRANSACTION;
213 OCStackApplicationResult postReqCB(void *ctx, OCDoHandle /*handle*/,
214 OCClientResponse *clientResponse)
216 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
218 cout << "\nCallback Context for POST recvd successfully";
223 cout << "\nStackResult: " << getResult(clientResponse->result);
224 cout << "\nJSON = " << clientResponse->payload;
228 cout << "\npostReqCB received Null clientResponse";
230 return OC_STACK_DELETE_TRANSACTION;
233 OCStackApplicationResult deleteReqCB(void *ctx,
234 OCDoHandle /*handle*/,
235 OCClientResponse *clientResponse)
237 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
239 cout << "\nCallback Context for DELETE recvd successfully";
244 cout << "\nStackResult: " << getResult(clientResponse->result);
245 //OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
249 cout << "\ndeleteReqCB received Null clientResponse";
251 return OC_STACK_DELETE_TRANSACTION;
254 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle /*handle*/,
255 OCClientResponse * clientResponse)
257 if (clientResponse == NULL)
259 cout << "\ngetReqCB received NULL clientResponse";
260 return OC_STACK_DELETE_TRANSACTION;
263 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
265 cout << "\nCallback Context for GET query recvd successfully";
268 cout << "\nStackResult: " << getResult(clientResponse->result);
269 cout << "\nSEQUENCE NUMBER: " << clientResponse->sequenceNumber;
270 //OIC_LOG_PAYLOAD(INFO, TAG, clientResponse->payload);
272 if (clientResponse->numRcvdVendorSpecificHeaderOptions > 0)
274 cout << "\nReceived vendor specific options";
276 OCHeaderOption * rcvdOptions = clientResponse->rcvdVendorSpecificHeaderOptions;
277 for( i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
279 if (((OCHeaderOption)rcvdOptions[i]).protocolID == OC_COAP_ID)
281 cout << "\nReceived option ID " << ((OCHeaderOption)rcvdOptions[i]).optionID;
285 return OC_STACK_DELETE_TRANSACTION;
288 OCStackApplicationResult obsReqCB(void* ctx, OCDoHandle /*handle*/,
289 OCClientResponse * clientResponse)
291 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
293 cout << "\nCallback Context for OBS query recvd successfully";
298 if (clientResponse->sequenceNumber <= MAX_SEQUENCE_NUMBER)
300 if (clientResponse->sequenceNumber == OC_OBSERVE_REGISTER)
302 cout << "This also serves as a registration confirmation" << endl;
305 cout << "\nStackResult: " << getResult(clientResponse->result);
306 cout << "\nSEQUENCE NUMBER: " << clientResponse->sequenceNumber;
307 cout << "\nCallback Context for OBSERVE notification recvd successfully ";
308 //OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
309 gNumObserveNotifies++;
311 if (gNumObserveNotifies == 15) //large number to test observing in DELETE case.
313 if (g_testCase == TEST_OBS_REQ_NON || g_testCase == TEST_OBS_REQ_CON)
315 if (OCCancel(gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
317 cout << "Observe cancel error" << endl;
319 return OC_STACK_DELETE_TRANSACTION;
321 else if (g_testCase == TEST_OBS_REQ_NON_CANCEL_IMM)
323 if (OCCancel(gObserveDoHandle, OC_HIGH_QOS, NULL, 0) != OC_STACK_OK)
325 cout << "\nObserve cancel error";
332 OIC_LOG(INFO, TAG, "No observe option header is returned in the response.");
333 OIC_LOG(INFO, TAG, "For a registration request, it means the registration failed");
334 return OC_STACK_DELETE_TRANSACTION;
339 cout << "\nobsReqCB received Null clientResponse";
341 return OC_STACK_KEEP_TRANSACTION;
344 OCStackApplicationResult presenceCB(void* ctx, OCDoHandle /*handle*/,
345 OCClientResponse * clientResponse)
347 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
349 cout << "\nCallback Context for Presence recvd successfully";
354 cout << "\nStackResult: " << getResult(clientResponse->result);
355 cout << "\nNONCE NUMBER: " << clientResponse->sequenceNumber;
356 cout << "\nCallback Context for Presence notification recvd successfully ";
357 //OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
358 gNumPresenceNotifies++;
359 if (gNumPresenceNotifies == 20)
361 if (OCCancel(gPresenceHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
363 cout << "\nPresence cancel error";
365 return OC_STACK_DELETE_TRANSACTION;
370 cout << "\npresenceCB received Null clientResponse";
372 return OC_STACK_KEEP_TRANSACTION;
376 // This is a function called back when a device is discovered
377 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle /*handle*/,
378 OCClientResponse * clientResponse)
380 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
382 cout << "\nCallback Context for DISCOVER query recvd successfully";
387 if (NULL == clientResponse->payload)
389 cout << "\nPayload is NULL, No resources found";
390 return OC_STACK_KEEP_TRANSACTION;
393 cout << "\nStackResult: " << getResult(clientResponse->result);
395 std::string connectionType = getConnectivityType (clientResponse->connType);
396 cout << "\nDiscovered on " << connectionType.c_str();
397 cout << "\nDevice ======> Discovered ";
398 cout << clientResponse->devAddr.addr;
399 if (CT_ADAPTER_IP == (clientResponse->connType & CT_MASK_ADAPTER))
401 cout << ":" << clientResponse->devAddr.port;
403 //OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
404 cout << "\nConnectivity type: " << clientResponse->connType;
405 g_serverAddr = clientResponse->devAddr;
406 parseClientResponse(clientResponse);
408 OCDiscoveryPayload *payload = (OCDiscoveryPayload*) clientResponse->payload;
411 cout << "\nDiscovery payload is empty!";
412 return OC_STACK_KEEP_TRANSACTION;
415 OCResourcePayload *resource = (OCResourcePayload*) payload->resources;
416 int targetResourceFound = 0;
419 cout << "\nFound resource: " << resource->uri;
420 if (resource->uri && strcmp(resource->uri, coapServerResource.c_str()) == 0)
422 cout << "\nLight resource found.";
423 targetResourceFound = 1;
425 if (g_testCase >= TEST_OBS_PRESENCE) break;
427 if (resource->secure && resource->port)
429 cout << "\nLight resource with secure flag";
430 g_serverAddr.flags = (OCTransportFlags) (clientResponse->devAddr.flags | OC_SECURE);
431 g_serverAddr.port = resource->port;
435 if(g_connType == CT_ADAPTER_TCP && resource->tcpPort)
437 cout << "\nRequest will be send with TCP";
438 g_serverAddr.port = resource->tcpPort;
439 g_serverAddr.adapter = OC_ADAPTER_TCP;
445 resource = resource->next;
448 if (!targetResourceFound)
450 OIC_LOG_V(INFO, TAG, "No %s in payload", coapServerResource.c_str());
451 return OC_STACK_KEEP_TRANSACTION;
456 case TEST_GET_REQ_NON:
457 InitGetRequest(OC_LOW_QOS, 0, 0);
459 case TEST_GET_REQ_NON_WITH_FILTERS:
460 InitGetRequest(OC_LOW_QOS, 0, 1);
462 case TEST_PUT_REQ_NON:
463 InitPutRequest(OC_LOW_QOS);
465 case TEST_POST_REQ_NON:
466 InitPostRequest(OC_LOW_QOS);
468 case TEST_DELETE_REQ_NON:
469 InitDeleteRequest(OC_LOW_QOS);
471 case TEST_OBS_REQ_NON:
472 case TEST_OBS_REQ_NON_CANCEL_IMM:
473 InitObserveRequest(OC_LOW_QOS);
475 case TEST_GET_UNAVAILABLE_RES_REQ_NON:
476 InitGetRequestToUnavailableResource(OC_LOW_QOS);
478 case TEST_GET_REQ_CON:
479 InitGetRequest(OC_HIGH_QOS, 0, 0);
481 case TEST_POST_REQ_CON:
482 InitPostRequest(OC_HIGH_QOS);
484 case TEST_DELETE_REQ_CON:
485 InitDeleteRequest(OC_HIGH_QOS);
487 case TEST_OBS_REQ_CON:
488 InitObserveRequest(OC_HIGH_QOS);
491 case TEST_OBS_PRESENCE:
492 case TEST_OBS_PRESENCE_WITH_FILTER:
493 case TEST_OBS_PRESENCE_WITH_FILTERS:
494 case TEST_OBS_MULTICAST_PRESENCE:
498 case TEST_GET_REQ_NON_WITH_VENDOR_HEADER_OPTIONS:
499 InitGetRequest(OC_LOW_QOS, 1, 0);
501 case TEST_DISCOVER_PLATFORM_REQ:
502 InitPlatformDiscovery(OC_LOW_QOS);
504 case TEST_DISCOVER_DEV_REQ:
505 InitDeviceDiscovery(OC_LOW_QOS);
514 cout << "\ndiscoveryReqCB received Null clientResponse";
516 return OC_STACK_KEEP_TRANSACTION;
519 OCStackApplicationResult PlatformDiscoveryReqCB(void* ctx,
520 OCDoHandle /*handle*/,
521 OCClientResponse * clientResponse)
523 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
525 cout << "\nCallback Context for Platform DISCOVER query recvd successfully";
530 //OIC_LOG truncates the response as it is too long.
531 //OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
535 cout << "\nPlatformDiscoveryReqCB received Null clientResponse";
538 return (g_unicastDiscovery) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
541 OCStackApplicationResult DeviceDiscoveryReqCB(void* ctx, OCDoHandle /*handle*/,
542 OCClientResponse * clientResponse)
544 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
546 cout << "\nCallback Context for Device DISCOVER query recvd successfully";
551 //OIC_LOG truncates the response as it is too long.
552 cout << "\nDiscovery response: ";
553 cout << clientResponse->payload;
557 cout << "\nPlatformDiscoveryReqCB received Null clientResponse";
560 return (g_unicastDiscovery) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
566 OCStackResult result = OC_STACK_OK;
567 cout << "\nExecuting " << __func__;
568 std::ostringstream query;
569 std::ostringstream querySuffix;
570 query << OC_RSRVD_PRESENCE_URI;
571 if (g_testCase == TEST_OBS_PRESENCE)
573 result = InvokeOCDoResource(query, &g_serverAddr, OC_REST_PRESENCE,
574 OC_LOW_QOS, presenceCB, NULL, 0);
576 if (g_testCase == TEST_OBS_PRESENCE_WITH_FILTER || g_testCase == TEST_OBS_PRESENCE_WITH_FILTERS)
579 querySuffix << query.str() << "?rt=core.led";
580 result = InvokeOCDoResource(querySuffix, &g_serverAddr, OC_REST_PRESENCE,
581 OC_LOW_QOS, presenceCB, NULL, 0);
583 if (g_testCase == TEST_OBS_PRESENCE_WITH_FILTERS)
585 if (result == OC_STACK_OK)
588 querySuffix << query.str() << "?rt=core.fan";
589 result = InvokeOCDoResource(querySuffix, &g_serverAddr, OC_REST_PRESENCE, OC_LOW_QOS,
590 presenceCB, NULL, 0);
593 if (g_testCase == TEST_OBS_MULTICAST_PRESENCE)
595 if (result == OC_STACK_OK)
597 std::ostringstream multicastPresenceQuery;
598 multicastPresenceQuery.str("");
599 multicastPresenceQuery << "coap://" << OC_MULTICAST_PREFIX << OC_RSRVD_PRESENCE_URI;
600 result = InvokeOCDoResource(multicastPresenceQuery, &g_serverAddr, OC_REST_PRESENCE, OC_LOW_QOS,
601 presenceCB, NULL, 0);
608 int InitGetRequestToUnavailableResource(OCQualityOfService qos)
610 cout << "\nExecuting " << __func__;
611 std::ostringstream query;
612 query << "/SomeUnknownResource";
613 return (InvokeOCDoResource(query, &g_serverAddr, OC_REST_GET, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
617 int InitObserveRequest(OCQualityOfService qos)
619 cout << "\nExecuting " << __func__;
620 std::ostringstream query;
621 query << coapServerResource;
622 return (InvokeOCDoResource(query, &g_serverAddr, OC_REST_OBSERVE,
623 (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, obsReqCB, NULL, 0));
626 int InitPutRequest(OCQualityOfService qos)
628 cout << "\nExecuting " << __func__;
629 std::ostringstream query;
630 query << coapServerResource;
631 return (InvokeOCDoResource(query, &g_serverAddr, OC_REST_PUT, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
635 int InitPostRequest(OCQualityOfService qos)
637 OCStackResult result;
638 cout << "\nExecuting " << __func__;
639 std::ostringstream query;
640 query << coapServerResource;
642 // First POST operation (to create an Light instance)
643 result = InvokeOCDoResource(query, &g_serverAddr, OC_REST_POST,
644 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
646 if (OC_STACK_OK != result)
648 // Error can happen if for example, network connectivity is down
649 cout << "\nFirst POST call did not succeed";
652 // Second POST operation (to create an Light instance)
653 result = InvokeOCDoResource(query, &g_serverAddr, OC_REST_POST,
654 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
656 if (OC_STACK_OK != result)
658 cout << "\nSecond POST call did not succeed";
661 // This POST operation will update the original resourced /a/light
662 return (InvokeOCDoResource(query, &g_serverAddr, OC_REST_POST,
663 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
664 postReqCB, NULL, 0));
667 void* RequestDeleteDeathResourceTask(void* myqos)
669 sleep (30);//long enough to give the server time to finish deleting the resource.
670 std::ostringstream query;
671 query << coapServerResource;
673 cout << "\nExecuting " << __func__;
675 // Second DELETE operation to delete the resource that might have been removed already.
676 OCQualityOfService qos;
686 OCStackResult result = InvokeOCDoResource(query, &g_serverAddr, OC_REST_DELETE,
688 deleteReqCB, NULL, 0);
690 if (OC_STACK_OK != result)
692 cout << "\nSecond DELETE call did not succeed";
698 int InitDeleteRequest(OCQualityOfService qos)
700 OCStackResult result;
701 std::ostringstream query;
702 query << coapServerResource;
704 cout << "\nExecuting " << __func__;
706 // First DELETE operation
707 result = InvokeOCDoResource(query, &g_serverAddr, OC_REST_DELETE,
709 deleteReqCB, NULL, 0);
710 if (OC_STACK_OK != result)
712 // Error can happen if for example, network connectivity is down
713 cout << "\nFirst DELETE call did not succeed";
717 //Create a thread to delete this resource again
719 pthread_create (&threadId, NULL, RequestDeleteDeathResourceTask, (void*)qos);
722 cout << "\nExit " << __func__;
726 int InitGetRequest(OCQualityOfService qos, uint8_t withVendorSpecificHeaderOptions, bool getWithQuery)
729 OCHeaderOption options[MAX_HEADER_OPTIONS];
731 cout << "\nExecuting " << __func__;
732 std::ostringstream query;
733 query << coapServerResource;
735 // ocserver is written to only process "power<X" query.
738 cout << "\nUsing query power<30";
739 query << "?power<50";
742 if (withVendorSpecificHeaderOptions)
744 uint8_t option0[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
745 uint8_t option1[] = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
746 memset(options, 0, sizeof(OCHeaderOption) * MAX_HEADER_OPTIONS);
747 options[0].protocolID = OC_COAP_ID;
748 options[0].optionID = 2048;
749 memcpy(options[0].optionData, option0, sizeof(option0));
750 options[0].optionLength = 10;
751 options[1].protocolID = OC_COAP_ID;
752 options[1].optionID = 3000;
753 memcpy(options[1].optionData, option1, sizeof(option1));
754 options[1].optionLength = 10;
756 if (withVendorSpecificHeaderOptions)
758 return (InvokeOCDoResource(query, &g_serverAddr, OC_REST_GET,
759 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, options, 2));
763 return (InvokeOCDoResource(query, &g_serverAddr, OC_REST_GET,
764 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, NULL, 0));
768 int InitPlatformDiscovery(OCQualityOfService qos)
770 cout << "\nExecuting " << __func__;
773 OCCallbackData cbData;
774 char szQueryUri[MAX_QUERY_LENGTH] = { 0 };
776 snprintf(szQueryUri, sizeof (szQueryUri) - 1, PLATFORM_DISCOVERY_QUERY, g_discoveryAddr);
778 cbData.cb = PlatformDiscoveryReqCB;
779 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
782 ret = OCDoRequest(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
783 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
785 if (ret != OC_STACK_OK)
787 cout << "\nOCStack device error";
793 int InitDeviceDiscovery(OCQualityOfService qos)
795 cout << "\nExecuting " << __func__;
798 OCCallbackData cbData;
799 char szQueryUri[MAX_QUERY_LENGTH] = { 0 };
801 snprintf(szQueryUri, sizeof (szQueryUri) - 1, DEVICE_DISCOVERY_QUERY, g_discoveryAddr);
803 cbData.cb = DeviceDiscoveryReqCB;
804 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
807 ret = OCDoRequest(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
808 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
810 if (ret != OC_STACK_OK)
812 cout << "\nOCStack device error";
818 int InitDiscovery(OCQualityOfService qos)
821 OCCallbackData cbData;
822 char szQueryUri[MAX_QUERY_LENGTH] = { 0 };
824 snprintf(szQueryUri, sizeof (szQueryUri) - 1, RESOURCE_DISCOVERY_QUERY, g_discoveryAddr);
826 cbData.cb = discoveryReqCB;
827 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
830 ret = OCDoRequest(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
831 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
833 if (ret != OC_STACK_OK)
835 cout << "\nOCStack resource error";
840 void *GMainLoopThread(void *param)
843 if (g_unicastDiscovery == 0 && g_testCase == TEST_DISCOVER_DEV_REQ)
845 InitDeviceDiscovery(OC_LOW_QOS);
847 else if (g_unicastDiscovery == 0 && g_testCase == TEST_DISCOVER_PLATFORM_REQ)
849 InitPlatformDiscovery(OC_LOW_QOS);
853 InitDiscovery(OC_LOW_QOS);
858 if (OCProcess() != OC_STACK_OK)
860 cout << "\nOCStack process error";
863 #ifndef ROUTING_GATEWAY
870 g_main_loop_quit(g_mainloop);
875 static FILE *client_fopen(const char */*path*/, const char *mode)
877 return fopen(DEFAULT_DB_FILE_PATH, mode);
880 int main(int argc, char* argv[])
884 g_mainloop = g_main_loop_new(NULL, FALSE);
887 printf("g_main_loop_new failed\n");
891 while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
896 g_unicastDiscovery = atoi(optarg);
899 g_testCase = atoi(optarg);
902 g_connectivity = atoi(optarg);
910 if ((g_unicastDiscovery != 0 && g_unicastDiscovery != 1) ||
911 (g_testCase < TEST_DISCOVER_REQ || g_testCase >= MAX_TESTS) ||
912 (g_connectivity < CT_ADAPTER_DEFAULT || g_connectivity >= MAX_CT))
918 cout << "\nEntering occlient main loop...\n";
920 OCPersistentStorage ps{ client_fopen, fread, fwrite, fclose, unlink };
921 OCRegisterPersistentStorageHandler(&ps);
923 if (OCInit1(OC_CLIENT_SERVER, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
925 cout << "\nOCStack init error";
929 #ifdef ROUTING_GATEWAY
931 * Before invoking Discover resource, we process the gateway requests
932 * and form the routing table.
934 for (int index = 0; index < MAX_NUM_GATEWAY_REQUEST; index++)
936 if (OCProcess() != OC_STACK_OK)
938 OIC_LOG(ERROR, TAG, "OCStack process error");
941 usleep(SLEEP_DURATION);
945 if (g_connectivity == CT_ADAPTER_DEFAULT || g_connectivity == CT_IP)
947 g_connType = CT_ADAPTER_IP;
949 else if(g_connectivity == CT_EDR)
951 cout << "\nSelected EDR Adapter\n";
952 g_connType = CT_ADAPTER_RFCOMM_BTEDR;
954 else if(g_connectivity == CT_TCP)
956 cout << "\nSelected TCP Adapter\n";
957 g_connType = CT_ADAPTER_TCP;
961 cout << "\nDefault Connectivity type selected...";
965 g_discoveryAddr[0] = '\0';
967 if (g_unicastDiscovery)
969 cout << "\nEnter address of Server hosting resource as given below:";
970 cout << "\nIP Adapter: 192.168.0.15:45454(IP:Port)";
971 cout << "\nEDR/BLE Adapter: AB:BC:CD:DE:EF:FG(MAC Address)";
974 if (fgets(g_discoveryAddr, sizeof (g_discoveryAddr), stdin))
976 //Strip newline char from unicastAddr
977 StripNewLineChar(g_discoveryAddr);
981 cout << "\n!! Bad input for IPV4 address. !!";
982 return OC_STACK_INVALID_PARAM;
987 // Break from loop with Ctrl+C
988 OIC_LOG(INFO, TAG, "Entering occlient main loop...");
989 signal(SIGINT, handleSigInt);
991 int result = pthread_create(&g_thread, NULL, GMainLoopThread, (void *)NULL);
994 printf("pthread_create failed in initialize\n");
998 g_main_loop_run(g_mainloop);
1000 cout << "\nExiting occlient main loop...\n";
1002 if (OCStop() != OC_STACK_OK)
1004 cout << "\nOCStack stop error\n";
1010 std::string getConnectivityType (OCConnectivityType connType)
1012 switch (connType & CT_MASK_ADAPTER)
1023 case CT_ADAPTER_GATT_BTLE:
1026 case CT_ADAPTER_RFCOMM_BTEDR:
1030 return "Incorrect connectivity";
1034 std::string getQueryStrForGetPut(OCClientResponse * /*clientResponse*/)
1039 void parseClientResponse(OCClientResponse * clientResponse)
1041 coapServerResource = getQueryStrForGetPut(clientResponse);