1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "iotivity_config.h"
39 #include "ocpayload.h"
40 #include "payload_logging.h"
43 #ifdef ROUTING_GATEWAY
45 * Maximum number of gateway requests to form the routing table.
47 #define MAX_NUM_GATEWAY_REQUEST 20
50 * Sleep duration after every OCProcess().
52 #define SLEEP_DURATION 100000
54 // Tracking user input
55 static int UnicastDiscovery = 0;
56 static int TestCase = 0;
57 static int Connectivity = 0;
58 static int Introspection = 0;
60 static const char *DEVICE_DISCOVERY_QUERY = "%s/oic/d";
61 static const char *PLATFORM_DISCOVERY_QUERY = "%s/oic/p";
62 static const char *RESOURCE_DISCOVERY_QUERY = "%s/oic/res";
63 static const char *INTROSPECTION_DISCOVERY_QUERY = "%s" OC_RSRVD_INTROSPECTION_URI;
65 //The following variable determines the interface protocol (IPv4, IPv6, etc)
66 //to be used for sending unicast messages. Default set to IP dual stack.
67 static OCConnectivityType ConnType = CT_ADAPTER_IP;
68 static OCDevAddr serverAddr;
69 static char discoveryAddr[100];
70 static std::string coapServerResource = "/a/light";
71 static std::string coapIntrospectionResource = OC_RSRVD_INTROSPECTION_URI;
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* createPayload()
96 OCRepPayload* payload = OCRepPayloadCreate();
100 std::cout << "Failed to create payload object"<<std::endl;
104 OCRepPayloadSetPropInt(payload, "power", 15);
105 OCRepPayloadSetPropBool(payload, "state", true);
107 return (OCPayload*) payload;
110 static void PrintUsage()
112 OIC_LOG(INFO, TAG, "Usage : occlient -u <0|1> -t <1..20> -c <0|1> -i<0|1>");
113 OIC_LOG(INFO, TAG, "-u <0|1> : Perform multicast/unicast discovery of resources");
114 OIC_LOG(INFO, TAG, "-c 0 : Use Default connectivity(IP)");
115 OIC_LOG(INFO, TAG, "-c 1 : IP Connectivity Type");
116 OIC_LOG(INFO, TAG, "-t 1 : Discover Resources");
117 OIC_LOG(INFO, TAG, "-t 2 : Discover Resources and Initiate Nonconfirmable Get Request");
118 OIC_LOG(INFO, TAG, "-t 3 : Discover Resources and Initiate Nonconfirmable Get Request"
119 " with query filter.");
120 OIC_LOG(INFO, TAG, "-t 4 : Discover Resources and Initiate Nonconfirmable Put Requests");
121 OIC_LOG(INFO, TAG, "-t 5 : Discover Resources and Initiate Nonconfirmable Post Requests");
122 OIC_LOG(INFO, TAG, "-t 6 : Discover Resources and Initiate Nonconfirmable Delete Requests");
123 OIC_LOG(INFO, TAG, "-t 7 : Discover Resources and Initiate Nonconfirmable Observe Requests");
124 OIC_LOG(INFO, TAG, "-t 8 : Discover Resources and Initiate Nonconfirmable Get Request "\
125 "for a resource which is unavailable");
126 OIC_LOG(INFO, TAG, "-t 9 : Discover Resources and Initiate Confirmable Get Request");
127 OIC_LOG(INFO, TAG, "-t 10 : Discover Resources and Initiate Confirmable Post Request");
128 OIC_LOG(INFO, TAG, "-t 11 : Discover Resources and Initiate Confirmable Delete Requests");
129 OIC_LOG(INFO, TAG, "-t 12 : Discover Resources and Initiate Confirmable Observe Requests"\
130 " and cancel with Low QoS");
133 OIC_LOG(INFO, TAG, "-t 13 : Discover Resources and Initiate Nonconfirmable presence");
134 OIC_LOG(INFO, TAG, "-t 14 : Discover Resources and Initiate Nonconfirmable presence with "\
136 OIC_LOG(INFO, TAG, "-t 15 : Discover Resources and Initiate Nonconfirmable presence with "\
138 OIC_LOG(INFO, TAG, "-t 16 : Discover Resources and Initiate Nonconfirmable multicast presence.");
141 OIC_LOG(INFO, TAG, "-t 17 : Discover Resources and Initiate Nonconfirmable Observe Requests "\
142 "then cancel immediately with High QOS");
143 OIC_LOG(INFO, TAG, "-t 18 : Discover Resources and Initiate Nonconfirmable Get Request and "\
144 "add vendor specific header options");
145 OIC_LOG(INFO, TAG, "-t 19 : Discover Platform");
146 OIC_LOG(INFO, TAG, "-t 20 : Discover Devices");
147 OIC_LOG(INFO, TAG, "-t 21 : Discover Resources and Display endpoints of the server information");
148 OIC_LOG(INFO, TAG, "-t 22 : Discover Resources and Perform Get Requests by IPv4 + COAP + UDP "\
149 "using server's endpoints information");
150 OIC_LOG(INFO, TAG, "-t 23 : Discover Resources and Perform Get Requests by IPv4 + COAP + TCP "\
151 "using server's endpoints information");
152 OIC_LOG(INFO, TAG, "-t 24 : Discover Introspection Resources and Perform Get Request");
155 OCStackResult InvokeOCDoResource(std::ostringstream &query,
156 OCDevAddr *remoteAddr,
158 OCQualityOfService qos,
159 OCClientResponseHandler cb,
160 OCHeaderOption * options,
164 OCCallbackData cbData;
168 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
171 OCPayload* payload = (method == OC_REST_PUT || method == OC_REST_POST) ? createPayload() : NULL;
173 ret = OCDoRequest(&handle, method, query.str().c_str(), remoteAddr,
174 payload, (ConnType), qos, &cbData, options, numOptions);
176 OCPayloadDestroy(payload);
178 if (ret != OC_STACK_OK)
180 OIC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method);
183 else if (method == OC_REST_PRESENCE)
185 gPresenceHandle = handle;
192 OCStackApplicationResult putReqCB(void* ctx, OCDoHandle /*handle*/,
193 OCClientResponse * clientResponse)
195 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
197 OIC_LOG(INFO, TAG, "Callback Context for PUT recvd successfully");
202 OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
203 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
204 OIC_LOG(INFO, TAG, ("=============> Put Response"));
208 OIC_LOG_V(INFO, TAG, "putReqCB 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 OIC_LOG(INFO, TAG, "Callback Context for POST recvd successfully");
223 OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
224 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
225 OIC_LOG(INFO, TAG, ("=============> Post Response"));
229 OIC_LOG_V(INFO, TAG, "postReqCB received Null clientResponse");
231 return OC_STACK_DELETE_TRANSACTION;
234 OCStackApplicationResult deleteReqCB(void *ctx,
235 OCDoHandle /*handle*/,
236 OCClientResponse *clientResponse)
238 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
240 OIC_LOG(INFO, TAG, "Callback Context for DELETE recvd successfully");
245 OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
246 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
247 OIC_LOG(INFO, TAG, ("=============> Delete Response"));
251 OIC_LOG_V(INFO, TAG, "deleteReqCB received Null clientResponse");
253 return OC_STACK_DELETE_TRANSACTION;
256 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle /*handle*/,
257 OCClientResponse * clientResponse)
259 if (clientResponse == NULL)
261 OIC_LOG(INFO, TAG, "getReqCB received NULL clientResponse");
262 return OC_STACK_DELETE_TRANSACTION;
265 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
267 OIC_LOG(INFO, TAG, "Callback Context for GET query recvd successfully");
270 OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
271 OIC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
272 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
273 OIC_LOG(INFO, TAG, ("=============> Get Response"));
275 if (clientResponse->numRcvdVendorSpecificHeaderOptions > 0)
277 OIC_LOG (INFO, TAG, "Received vendor specific options");
279 OCHeaderOption * rcvdOptions = clientResponse->rcvdVendorSpecificHeaderOptions;
280 for( i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
282 if (((OCHeaderOption)rcvdOptions[i]).protocolID == OC_COAP_ID)
284 OIC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with",
285 ((OCHeaderOption)rcvdOptions[i]).optionID );
287 OIC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData,
288 MAX_HEADER_OPTION_DATA_LENGTH);
292 return OC_STACK_DELETE_TRANSACTION;
295 OCStackApplicationResult obsReqCB(void* ctx, OCDoHandle handle,
296 OCClientResponse * clientResponse)
298 if (ctx == (void*)DEFAULT_CONTEXT_VALUE)
300 OIC_LOG(INFO, TAG, "Callback Context for OBS query recvd successfully");
305 if (clientResponse->sequenceNumber <= MAX_SEQUENCE_NUMBER)
307 if (clientResponse->sequenceNumber == OC_OBSERVE_REGISTER)
309 OIC_LOG(INFO, TAG, "This also serves as a registration confirmation.");
312 OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
313 OIC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
315 if (clientResponse->result == OC_STACK_OK)
317 OIC_LOG_V(INFO, TAG, "Callback Context for OBSERVE notification recvd successfully %d",
318 gNumObserveNotifies);
319 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
320 OIC_LOG(INFO, TAG, ("=============> Obs Response"));
321 gNumObserveNotifies++;
323 if (gNumObserveNotifies > 15) //large number to test observing in DELETE case.
325 if (TestCase == TEST_OBS_REQ_NON || TestCase == TEST_OBS_REQ_CON)
327 OIC_LOG(ERROR, TAG, "Cancelling with LOW QOS");
328 if (OCCancel (handle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
330 OIC_LOG(ERROR, TAG, "Observe cancel error");
332 return OC_STACK_DELETE_TRANSACTION;
334 else if (TestCase == TEST_OBS_REQ_NON_CANCEL_IMM)
336 OIC_LOG(ERROR, TAG, "Cancelling with HIGH QOS");
337 if (OCCancel (handle, OC_HIGH_QOS, NULL, 0) != OC_STACK_OK)
339 OIC_LOG(ERROR, TAG, "Observe cancel error");
347 OIC_LOG(INFO, TAG, "No observe option header is returned in the response.");
348 OIC_LOG(INFO, TAG, "For a registration request, it means the registration failed");
349 return OC_STACK_DELETE_TRANSACTION;
354 OIC_LOG_V(INFO, TAG, "obsReqCB received Null clientResponse");
356 return OC_STACK_KEEP_TRANSACTION;
359 OCStackApplicationResult presenceCB(void* ctx, OCDoHandle /*handle*/,
360 OCClientResponse * clientResponse)
362 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
364 OIC_LOG(INFO, TAG, "Callback Context for Presence recvd successfully");
369 OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
370 OIC_LOG_V(INFO, TAG, "Callback Context for Presence notification recvd successfully %d",
371 gNumPresenceNotifies);
372 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
373 OIC_LOG(INFO, TAG, ("=============> Presence Response"));
374 gNumPresenceNotifies++;
375 if (gNumPresenceNotifies == 20)
377 if (OCCancel(gPresenceHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
379 OIC_LOG(ERROR, TAG, "Presence cancel error");
381 return OC_STACK_DELETE_TRANSACTION;
386 OIC_LOG_V(INFO, TAG, "presenceCB received Null clientResponse");
388 return OC_STACK_KEEP_TRANSACTION;
392 // This is a function called back when a device is discovered
393 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle /*handle*/,
394 OCClientResponse * clientResponse)
396 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
398 OIC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully");
403 OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
405 if (OC_STACK_NOT_ACCEPTABLE == clientResponse->result)
407 // Re-initiate discovery with OIC format. This is applicable for the case that
408 // a OCF 1.x client speaks to a OIC 1.1 server.
409 InitDiscovery(OC_LOW_QOS, 1);
410 return OC_STACK_KEEP_TRANSACTION;
413 std::string connectionType = getConnectivityType (clientResponse->connType);
414 OIC_LOG_V(INFO, TAG, "Discovered on %s", connectionType.c_str());
416 "Device =============> Discovered @ %s:%d",
417 clientResponse->devAddr.addr,
418 clientResponse->devAddr.port);
419 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
421 ConnType = clientResponse->connType;
422 serverAddr = clientResponse->devAddr;
424 OCDiscoveryPayload *payload = (OCDiscoveryPayload*) clientResponse->payload;
427 return OC_STACK_DELETE_TRANSACTION;
430 OCResourcePayload *resource = (OCResourcePayload*) payload->resources;
434 if(resource->uri && strcmp(resource->uri, coapServerResource.c_str()) == 0)
439 resource = resource->next;
444 OIC_LOG_V (INFO, TAG, "No %s in payload", coapServerResource.c_str());
445 return OC_STACK_KEEP_TRANSACTION;
450 case TEST_GET_REQ_NON:
451 InitGetRequest(OC_LOW_QOS, 0, 0);
453 case TEST_GET_REQ_NON_WITH_FILTERS:
454 InitGetRequest(OC_LOW_QOS, 0, 1);
456 case TEST_PUT_REQ_NON:
457 InitPutRequest(OC_LOW_QOS);
459 case TEST_POST_REQ_NON:
460 InitPostRequest(OC_LOW_QOS);
462 case TEST_DELETE_REQ_NON:
463 InitDeleteRequest(OC_LOW_QOS);
465 case TEST_OBS_REQ_NON:
466 case TEST_OBS_REQ_NON_CANCEL_IMM:
467 InitObserveRequest(OC_LOW_QOS);
469 case TEST_GET_UNAVAILABLE_RES_REQ_NON:
470 InitGetRequestToUnavailableResource(OC_LOW_QOS);
472 case TEST_GET_REQ_CON:
473 InitGetRequest(OC_HIGH_QOS, 0, 0);
475 case TEST_POST_REQ_CON:
476 InitPostRequest(OC_HIGH_QOS);
478 case TEST_DELETE_REQ_CON:
479 InitDeleteRequest(OC_HIGH_QOS);
481 case TEST_OBS_REQ_CON:
482 InitObserveRequest(OC_HIGH_QOS);
485 case TEST_OBS_PRESENCE:
486 case TEST_OBS_PRESENCE_WITH_FILTER:
487 case TEST_OBS_PRESENCE_WITH_FILTERS:
488 case TEST_OBS_MULTICAST_PRESENCE:
492 case TEST_GET_REQ_NON_WITH_VENDOR_HEADER_OPTIONS:
493 InitGetRequest(OC_LOW_QOS, 1, 0);
495 case TEST_DISCOVER_PLATFORM_REQ:
496 InitPlatformDiscovery(OC_LOW_QOS);
498 case TEST_DISCOVER_DEV_REQ:
499 InitDeviceDiscovery(OC_LOW_QOS);
501 case TEST_DISCOVER_REQ_SHOW_EPS:
502 showEndpointsInfo(resource);
504 case TEST_GET_REQ_UDP:
505 InitGetRequestWithCoap(payload, true);
507 case TEST_GET_REQ_TCP:
508 InitGetRequestWithCoap(payload, false);
510 case TEST_INTROSPECTION:
511 InitIntrospection(payload);
520 OIC_LOG_V(INFO, TAG, "discoveryReqCB received Null clientResponse");
522 return OC_STACK_KEEP_TRANSACTION;
525 OCStackApplicationResult PlatformDiscoveryReqCB(void* ctx,
526 OCDoHandle /*handle*/,
527 OCClientResponse * clientResponse)
529 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
531 OIC_LOG(INFO, TAG, "Callback Context for Platform DISCOVER query recvd successfully");
536 OIC_LOG(INFO, TAG, ("Discovery Response:"));
537 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
541 OIC_LOG_V(INFO, TAG, "PlatformDiscoveryReqCB received Null clientResponse");
544 return (UnicastDiscovery) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
547 OCStackApplicationResult DeviceDiscoveryReqCB(void* ctx, OCDoHandle /*handle*/,
548 OCClientResponse * clientResponse)
550 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
552 OIC_LOG(INFO, TAG, "Callback Context for Device DISCOVER query recvd successfully");
557 OIC_LOG(INFO, TAG, ("Discovery Response:"));
558 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
562 OIC_LOG_V(INFO, TAG, "PlatformDiscoveryReqCB received Null clientResponse");
565 return (UnicastDiscovery) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION;
571 OCStackResult result = OC_STACK_OK;
572 OIC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
573 std::ostringstream query;
574 std::ostringstream querySuffix;
575 query << OC_RSRVD_PRESENCE_URI;
576 if (TestCase == TEST_OBS_PRESENCE)
578 result = InvokeOCDoResource(query, &serverAddr, OC_REST_PRESENCE,
579 OC_LOW_QOS, presenceCB, NULL, 0);
581 if (TestCase == TEST_OBS_PRESENCE_WITH_FILTER || TestCase == TEST_OBS_PRESENCE_WITH_FILTERS)
584 querySuffix << query.str() << "?rt=core.led";
585 result = InvokeOCDoResource(querySuffix, &serverAddr, OC_REST_PRESENCE,
586 OC_LOW_QOS, presenceCB, NULL, 0);
588 if (TestCase == TEST_OBS_PRESENCE_WITH_FILTERS)
590 if (result == OC_STACK_OK)
593 querySuffix << query.str() << "?rt=core.fan";
594 result = InvokeOCDoResource(querySuffix, &serverAddr, OC_REST_PRESENCE, OC_LOW_QOS,
595 presenceCB, NULL, 0);
598 if (TestCase == TEST_OBS_MULTICAST_PRESENCE)
600 if (result == OC_STACK_OK)
602 result = InvokeOCDoResource(query, NULL, OC_REST_PRESENCE, OC_LOW_QOS,
603 presenceCB, NULL, 0);
610 int InitGetRequestToUnavailableResource(OCQualityOfService qos)
612 std::ostringstream query;
613 query << "/SomeUnknownResource";
614 OIC_LOG_V(INFO, TAG, "\nExecuting %s with query %s", __func__, query.str().c_str());
615 return (InvokeOCDoResource(query, &serverAddr, OC_REST_GET, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
619 int InitIntrospection(OCDiscoveryPayload* dis)
622 std::ostringstream query;
623 query << coapIntrospectionResource;
624 OIC_LOG_V(INFO, TAG, "\nExecuting %s with query %s", __func__, query.str().c_str());
625 return (InvokeOCDoResource(query, &serverAddr, OC_REST_GET, OC_LOW_QOS,
629 int InitObserveRequest(OCQualityOfService qos)
631 std::ostringstream query;
632 query << coapServerResource;
633 OIC_LOG_V(INFO, TAG, "\nExecuting %s with query %s", __func__, query.str().c_str());
634 return (InvokeOCDoResource(query, &serverAddr, OC_REST_OBSERVE,
635 (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS, obsReqCB, NULL, 0));
638 int InitPutRequest(OCQualityOfService qos)
640 std::ostringstream query;
641 query << coapServerResource;
642 OIC_LOG_V(INFO, TAG, "\nExecuting %s with query %s", __func__, query.str().c_str());
643 return (InvokeOCDoResource(query, &serverAddr, OC_REST_PUT, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
647 int InitPostRequest(OCQualityOfService qos)
649 OCStackResult result;
651 std::ostringstream query;
652 query << coapServerResource;
654 OIC_LOG_V(INFO, TAG, "\nExecuting %s with query %s", __func__, query.str().c_str());
655 // First POST operation (to create an Light instance)
656 result = InvokeOCDoResource(query, &serverAddr, OC_REST_POST,
657 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
659 if (OC_STACK_OK != result)
661 // Error can happen if for example, network connectivity is down
662 OIC_LOG(INFO, TAG, "First POST call did not succeed");
665 // Second POST operation (to create an Light instance)
666 result = InvokeOCDoResource(query, &serverAddr, OC_REST_POST,
667 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
669 if (OC_STACK_OK != result)
671 OIC_LOG(INFO, TAG, "Second POST call did not succeed");
674 // This POST operation will update the original resourced /a/light
675 return (InvokeOCDoResource(query, &serverAddr, OC_REST_POST,
676 ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
677 postReqCB, NULL, 0));
680 void* RequestDeleteDeathResourceTask(void* myqos)
682 sleep (30);//long enough to give the server time to finish deleting the resource.
683 std::ostringstream query;
684 query << coapServerResource;
686 OIC_LOG_V(INFO, TAG, "\nExecuting %s with query %s", __func__, query.str().c_str());
688 // Second DELETE operation to delete the resource that might have been removed already.
689 OCQualityOfService qos;
699 OCStackResult result = InvokeOCDoResource(query, &serverAddr, OC_REST_DELETE,
701 deleteReqCB, NULL, 0);
703 if (OC_STACK_OK != result)
705 OIC_LOG(INFO, TAG, "Second DELETE call did not succeed");
711 int InitDeleteRequest(OCQualityOfService qos)
713 OCStackResult result;
714 std::ostringstream query;
715 query << coapServerResource;
717 OIC_LOG_V(INFO, TAG, "\nExecuting %s with query %s", __func__, query.str().c_str());
719 // First DELETE operation
720 result = InvokeOCDoResource(query, &serverAddr, OC_REST_DELETE,
722 deleteReqCB, NULL, 0);
723 if (OC_STACK_OK != result)
725 // Error can happen if for example, network connectivity is down
726 OIC_LOG(INFO, TAG, "First DELETE call did not succeed");
730 //Create a thread to delete this resource again
732 pthread_create (&threadId, NULL, RequestDeleteDeathResourceTask, (void*)qos);
735 OIC_LOG_V(INFO, TAG, "\n\nExit %s", __func__);
739 int InitGetRequest(OCQualityOfService qos, uint8_t withVendorSpecificHeaderOptions,
743 OCHeaderOption options[MAX_HEADER_OPTIONS];
745 std::ostringstream query;
746 query << coapServerResource;
748 // ocserver is written to only process "power<X" query.
751 OIC_LOG(INFO, TAG, "Using query power<50");
752 query << "?power<50";
754 OIC_LOG_V(INFO, TAG, "\nExecuting %s with query %s", __func__, query.str().c_str());
756 if (withVendorSpecificHeaderOptions)
758 memset(options, 0, sizeof(OCHeaderOption)* MAX_HEADER_OPTIONS);
759 size_t numOptions = 0;
760 uint8_t option0[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
761 uint16_t optionID = 2048;
762 size_t optionDataSize = sizeof(option0);
763 OCSetHeaderOption(options,
769 uint8_t option1[] = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
771 optionDataSize = sizeof(option1);
772 OCSetHeaderOption(options,
778 if (withVendorSpecificHeaderOptions)
780 return (InvokeOCDoResource(query, &serverAddr, OC_REST_GET,
781 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, options, 2));
785 return (InvokeOCDoResource(query, &serverAddr, OC_REST_GET,
786 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, NULL, 0));
790 int InitPlatformDiscovery(OCQualityOfService qos)
792 OIC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
795 OCCallbackData cbData;
796 char szQueryUri[MAX_QUERY_LENGTH] = { 0 };
798 snprintf(szQueryUri, sizeof (szQueryUri) - 1, PLATFORM_DISCOVERY_QUERY, discoveryAddr);
800 cbData.cb = PlatformDiscoveryReqCB;
801 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
804 ret = OCDoRequest(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
805 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
807 if (ret != OC_STACK_OK)
809 OIC_LOG(ERROR, TAG, "OCStack device error");
815 int InitDeviceDiscovery(OCQualityOfService qos)
817 OIC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
820 OCCallbackData cbData;
821 char szQueryUri[MAX_QUERY_LENGTH] = { 0 };
823 snprintf(szQueryUri, sizeof (szQueryUri) - 1, DEVICE_DISCOVERY_QUERY, discoveryAddr);
825 cbData.cb = DeviceDiscoveryReqCB;
826 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
829 ret = OCDoRequest(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
830 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
832 if (ret != OC_STACK_OK)
834 OIC_LOG(ERROR, TAG, "OCStack device error");
840 int InitIntrospectionDiscovery(OCQualityOfService qos)
842 OIC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
845 OCCallbackData cbData;
846 char szQueryUri[MAX_QUERY_LENGTH] = { 0 };
848 snprintf(szQueryUri, sizeof(szQueryUri) - 1, INTROSPECTION_DISCOVERY_QUERY, discoveryAddr);
850 cbData.cb = DeviceDiscoveryReqCB;
851 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
854 ret = OCDoRequest(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
855 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
857 if (ret != OC_STACK_OK)
859 OIC_LOG(ERROR, TAG, "OCStack device error");
865 int InitDiscovery(OCQualityOfService qos, uint8_t withVendorSpecificHeaderOptions)
868 OCCallbackData cbData;
869 char szQueryUri[MAX_QUERY_LENGTH] = { 0 };
871 snprintf(szQueryUri, sizeof (szQueryUri) - 1, RESOURCE_DISCOVERY_QUERY, discoveryAddr);
873 cbData.cb = discoveryReqCB;
874 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
877 if (withVendorSpecificHeaderOptions)
879 OCHeaderOption options[MAX_HEADER_OPTIONS];
880 memset(options, 0, sizeof(OCHeaderOption) * MAX_HEADER_OPTIONS);
881 size_t numOptions = 0;
883 uint8_t option0[] = {0};
884 uint16_t optionID = 2049;
885 size_t optionDataSize = sizeof(option0);
886 OCSetHeaderOption(options, &numOptions, optionID, option0, optionDataSize);
888 uint8_t option1[] = {0};
890 optionDataSize = sizeof(option1);
891 OCSetHeaderOption(options, &numOptions, optionID, option1, optionDataSize);
893 ret = OCDoRequest(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
894 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
895 &cbData, options, 2);
899 ret = OCDoRequest(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
900 (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
903 if (ret != OC_STACK_OK)
905 OIC_LOG(ERROR, TAG, "OCStack resource error");
910 int InitGetRequestWithCoap(OCDiscoveryPayload* dis, bool isUdp)
914 OIC_LOG(INFO, TAG, "Given payload is NULL!!!");
919 std::ostringstream query;
920 query << coapServerResource;
925 // find endpoint with ipv4, UDP or TCP
926 OCResourcePayload* res = dis->resources;
929 OCEndpointPayload* eps = res->eps;
932 if (strcmp(eps->tps, (isUdp ? COAP_UDP : COAP_TCP)) == 0 &&
933 strlen(eps->addr) < MAX_LENGTH_IPv4_ADDR)
935 OIC_LOG_V(INFO, TAG, "%s found!!!", (isUdp ? COAP_UDP : COAP_TCP));
936 dev.adapter = (isUdp ? OC_ADAPTER_IP : OC_ADAPTER_TCP);
937 dev.flags = OC_IP_USE_V4;
938 dev.port = eps->port;
939 memcpy(dev.addr, eps->addr, sizeof(dev.addr));
946 if (dev.adapter == (isUdp ? OC_ADAPTER_IP : OC_ADAPTER_TCP) && dev.flags == OC_IP_USE_V4)
948 OIC_LOG_V(INFO, TAG, "dev addr is %s", dev.addr);
949 OIC_LOG_V(INFO, TAG, "dev port is %d", dev.port);
950 OIC_LOG_V(INFO, TAG, "dev flags is %d", dev.flags);
951 OIC_LOG_V(INFO, TAG, "dev adapter is %d", dev.adapter);
954 return (InvokeOCDoResource(query, &dev, OC_REST_GET,
955 OC_LOW_QOS, getReqCB, NULL, 0));
959 OIC_LOG(INFO, TAG, "Endpoints infomation not found on given payload!!!");
964 void showEndpointsInfo(OCResourcePayload* res)
968 OIC_LOG(INFO, TAG, "No endpoints information in given payload");
974 OIC_LOG(INFO, TAG, "No endpoints information in given payload");
978 OCEndpointPayload* eps = res->eps;
982 if (eps->family == OC_IP_USE_V6)
984 OIC_LOG_V(INFO, TAG, "Resource [%s] has endpoint [%s://[%s]:%d]",
985 res->uri, eps->tps, eps->addr, eps->port);
987 else if (eps->family == OC_IP_USE_V4)
989 OIC_LOG_V(INFO, TAG, "Resource [%s] has endpoint [%s://%s:%d]",
990 res->uri, eps->tps, eps->addr, eps->port);
994 OIC_LOG_V(INFO, TAG, "Resource [%s] has endpoint [%s://%s]",
995 res->uri, eps->tps, eps->addr);
1002 static FILE* server_fopen(const char* path, const char* mode)
1004 return fopen(path, mode);
1007 int main(int argc, char* argv[])
1010 OCPersistentStorage ps{ server_fopen, fread, fwrite, fclose, unlink };
1012 while ((opt = getopt(argc, argv, "u:t:c:i:")) != -1)
1017 UnicastDiscovery = atoi(optarg);
1020 TestCase = atoi(optarg);
1023 Connectivity = atoi(optarg);
1026 Introspection = atoi(optarg);
1034 if ((UnicastDiscovery != 0 && UnicastDiscovery != 1) ||
1035 (TestCase < TEST_DISCOVER_REQ || TestCase >= MAX_TESTS) ||
1036 (Connectivity < CT_ADAPTER_DEFAULT || Connectivity >= MAX_CT))
1042 if (OCInit1(OC_CLIENT_SERVER, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
1044 OIC_LOG(ERROR, TAG, "OCStack init error");
1048 if (Introspection != 0)
1050 if (OC_STACK_OK != OCRegisterPersistentStorageHandler(&ps))
1052 OIC_LOG(ERROR, TAG, "OCRegisterPersistentStorageHandler");
1056 #ifdef ROUTING_GATEWAY
1058 * Before invoking Discover resource, we process the gateway requests
1059 * and form the routing table.
1061 for (int index = 0; index < MAX_NUM_GATEWAY_REQUEST; index++)
1063 if (OC_STACK_OK != OCProcess())
1065 OIC_LOG(ERROR, TAG, "OCStack process error");
1068 usleep(SLEEP_DURATION);
1071 if (Connectivity == CT_ADAPTER_DEFAULT || Connectivity == CT_IP)
1073 ConnType = CT_ADAPTER_IP;
1077 OIC_LOG(INFO, TAG, "Default Connectivity type selected...");
1081 discoveryAddr[0] = '\0';
1083 if (UnicastDiscovery)
1085 OIC_LOG(INFO, TAG, "Enter IP address of server with optional port number");
1086 OIC_LOG(INFO, TAG, "IPv4: 192.168.0.15:45454\n");
1087 OIC_LOG(INFO, TAG, "IPv6: [fe80::20c:29ff:fe1b:9c5]:45454\n");
1089 if (fgets(discoveryAddr, sizeof (discoveryAddr), stdin))
1091 //Strip newline char from ipv4addr
1092 StripNewLineChar(discoveryAddr);
1096 OIC_LOG(ERROR, TAG, "!! Bad input for IP address. !!");
1097 return OC_STACK_INVALID_PARAM;
1101 if (UnicastDiscovery == 0 && TestCase == TEST_DISCOVER_DEV_REQ)
1103 InitDeviceDiscovery(OC_LOW_QOS);
1105 else if (UnicastDiscovery == 0 && TestCase == TEST_DISCOVER_PLATFORM_REQ)
1107 InitPlatformDiscovery(OC_LOW_QOS);
1111 InitDiscovery(OC_LOW_QOS, 0);
1114 // Break from loop with Ctrl+C
1115 OIC_LOG(INFO, TAG, "Entering occlient main loop...");
1116 signal(SIGINT, handleSigInt);
1120 if (OCProcess() != OC_STACK_OK)
1122 OIC_LOG(ERROR, TAG, "OCStack process error");
1125 #ifndef ROUTING_GATEAWAY
1129 OIC_LOG(INFO, TAG, "Exiting occlient main loop...");
1131 if (OCStop() != OC_STACK_OK)
1133 OIC_LOG(ERROR, TAG, "OCStack stop error");
1139 std::string getConnectivityType (OCConnectivityType connType)
1141 switch (connType & CT_MASK_ADAPTER)
1152 case CT_ADAPTER_GATT_BTLE:
1155 case CT_ADAPTER_RFCOMM_BTEDR:
1159 return "Incorrect connectivity";