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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
26 // Defining _POSIX_C_SOURCE macro with 200112L (or greater) as value
27 // causes header files to expose definitions
28 // corresponding to the POSIX.1-2001 base
29 // specification (excluding the XSI extension).
30 // For POSIX.1-2001 base specification,
31 // Refer http://pubs.opengroup.org/onlinepubs/009695399/
32 #define _POSIX_C_SOURCE 200112L
33 #ifndef __STDC_FORMAT_MACROS
34 #define __STDC_FORMAT_MACROS
36 #ifndef __STDC_LIMIT_MACROS
37 #define __STDC_LIMIT_MACROS
39 #include "iotivity_config.h"
45 #include "ocstackinternal.h"
46 #include "ocresourcehandler.h"
47 #include "occlientcb.h"
48 #include "ocobserve.h"
50 #include "oic_malloc.h"
51 #include "oic_string.h"
54 #include "ocserverrequest.h"
55 #include "secureresourcemanager.h"
56 #include "psinterface.h"
58 #include "doxmresource.h"
60 #include "cainterface.h"
61 #include "ocpayload.h"
62 #include "ocpayloadcbor.h"
63 #include "cautilinterface.h"
66 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
67 #include "routingutility.h"
68 #ifdef ROUTING_GATEWAY
69 #include "routingmanager.h"
74 #include "oickeepaliveinternal.h"
77 //#ifdef DIRECT_PAIRING
78 #include "directpairing.h"
81 #ifdef HAVE_ARDUINO_TIME_H
84 #ifdef HAVE_SYS_TIME_H
87 #include <coap/coap.h>
89 #ifdef HAVE_ARPA_INET_H
90 #include <arpa/inet.h>
94 #define UINT32_MAX (0xFFFFFFFFUL)
97 //-----------------------------------------------------------------------------
99 //-----------------------------------------------------------------------------
102 OC_STACK_UNINITIALIZED = 0,
103 OC_STACK_INITIALIZED,
104 OC_STACK_UNINIT_IN_PROGRESS
110 OC_PRESENCE_UNINITIALIZED = 0,
111 OC_PRESENCE_INITIALIZED
115 #if defined(__WITH_DTLS__) || defined (__WITH_TLS__)
118 OCOtmEventHandler cb;
120 } OCOtmEventHandler_t;
123 //-----------------------------------------------------------------------------
125 //-----------------------------------------------------------------------------
126 static OCStackState stackState = OC_STACK_UNINITIALIZED;
128 OCResource *headResource = NULL;
129 static OCResource *tailResource = NULL;
130 static OCResourceHandle platformResource = {0};
131 static OCResourceHandle deviceResource = {0};
133 static OCResourceHandle brokerResource = {0};
137 static OCPresenceState presenceState = OC_PRESENCE_UNINITIALIZED;
138 static PresenceResource presenceResource = {0};
139 static uint8_t PresenceTimeOutSize = 0;
140 static uint32_t PresenceTimeOut[] = {50, 75, 85, 95, 100};
143 static OCMode myStackMode;
145 //TODO: revisit this design
146 static bool gRASetInfo = false;
148 OCDeviceEntityHandler defaultDeviceHandler;
149 void* defaultDeviceHandlerCallbackParameter = NULL;
150 static const char COAP_TCP_SCHEME[] = "coap+tcp:";
151 static const char COAPS_TCP_SCHEME[] = "coaps+tcp:";
152 static const char CORESPEC[] = "core";
154 #if defined(__WITH_DTLS__) || defined (__WITH_TLS__)
155 static OCOtmEventHandler_t g_otmEventHandler = {NULL, NULL};
158 //-----------------------------------------------------------------------------
160 //-----------------------------------------------------------------------------
161 #define TAG "OIC_RI_STACK"
162 #define VERIFY_SUCCESS(op, successCode) { if ((op) != (successCode)) \
163 {OIC_LOG_V(FATAL, TAG, "%s failed!!", #op); goto exit;} }
164 #define VERIFY_NON_NULL(arg, logLevel, retVal) { if (!(arg)) { OIC_LOG((logLevel), \
165 TAG, #arg " is NULL"); return (retVal); } }
166 #define VERIFY_NON_NULL_NR(arg, logLevel) { if (!(arg)) { OIC_LOG((logLevel), \
167 TAG, #arg " is NULL"); return; } }
168 #define VERIFY_NON_NULL_V(arg) { if (!arg) {OIC_LOG(FATAL, TAG, #arg " is NULL");\
171 //TODO: we should allow the server to define this
172 #define MAX_OBSERVE_AGE (0x2FFFFUL)
174 #define MILLISECONDS_PER_SECOND (1000)
176 //-----------------------------------------------------------------------------
177 // Private internal function prototypes
178 //-----------------------------------------------------------------------------
181 * Generate handle of OCDoResource invocation for callback management.
183 * @return Generated OCDoResource handle.
185 static OCDoHandle GenerateInvocationHandle();
188 * Initialize resource data structures, variables, etc.
190 * @return ::OC_STACK_OK on success, some other value upon failure.
192 static OCStackResult initResources();
195 * Add a resource to the end of the linked list of resources.
197 * @param resource Resource to be added
199 static void insertResource(OCResource *resource);
202 * Find a resource in the linked list of resources.
204 * @param resource Resource to be found.
205 * @return Pointer to resource that was found in the linked list or NULL if the resource was not
208 static OCResource *findResource(OCResource *resource);
211 * Insert a resource type into a resource's resource type linked list.
212 * If resource type already exists, it will not be inserted and the
213 * resourceType will be free'd.
214 * resourceType->next should be null to avoid memory leaks.
215 * Function returns silently for null args.
217 * @param resource Resource where resource type is to be inserted.
218 * @param resourceType Resource type to be inserted.
220 static void insertResourceType(OCResource *resource,
221 OCResourceType *resourceType);
224 * Get a resource type at the specified index within a resource.
226 * @param handle Handle of resource.
227 * @param index Index of resource type.
229 * @return Pointer to resource type if found, NULL otherwise.
231 static OCResourceType *findResourceTypeAtIndex(OCResourceHandle handle,
235 * Insert a resource interface into a resource's resource interface linked list.
236 * If resource interface already exists, it will not be inserted and the
237 * resourceInterface will be free'd.
238 * resourceInterface->next should be null to avoid memory leaks.
240 * @param resource Resource where resource interface is to be inserted.
241 * @param resourceInterface Resource interface to be inserted.
243 static void insertResourceInterface(OCResource *resource,
244 OCResourceInterface *resourceInterface);
247 * Get a resource interface at the specified index within a resource.
249 * @param handle Handle of resource.
250 * @param index Index of resource interface.
252 * @return Pointer to resource interface if found, NULL otherwise.
254 static OCResourceInterface *findResourceInterfaceAtIndex(
255 OCResourceHandle handle, uint8_t index);
258 * Delete all of the dynamically allocated elements that were created for the resource type.
260 * @param resourceType Specified resource type.
262 static void deleteResourceType(OCResourceType *resourceType);
265 * Delete all of the dynamically allocated elements that were created for the resource interface.
267 * @param resourceInterface Specified resource interface.
269 static void deleteResourceInterface(OCResourceInterface *resourceInterface);
272 * Unbind all child resources.
274 * @param resourceChild Specified binded resource head is deleted from parent.
276 static void unbindChildResources(OCChildResource *resourceChild);
279 * Delete all of the dynamically allocated elements that were created for the resource.
281 * @param resource Specified resource.
283 static void deleteResourceElements(OCResource *resource);
286 * Delete resource specified by handle. Deletes resource and all resourcetype and resourceinterface
289 * @param handle Handle of resource to be deleted.
291 * @return ::OC_STACK_OK on success, some other value upon failure.
293 static OCStackResult deleteResource(OCResource *resource);
296 * Delete all of the resources in the resource list.
298 static void deleteAllResources();
301 * Increment resource sequence number. Handles rollover.
303 * @param resPtr Pointer to resource.
305 static void incrementSequenceNumber(OCResource * resPtr);
308 * Attempts to initialize every network interface that the CA Layer might have compiled in.
310 * Note: At least one interface must succeed to initialize. If all calls to @ref CASelectNetwork
311 * return something other than @ref CA_STATUS_OK, then this function fails.
312 * @param transportType OCTransportAdapter value to select.
313 * @return ::CA_STATUS_OK on success, some other value upon failure.
315 static CAResult_t OCSelectNetwork(OCTransportAdapter transportType);
318 * Convert CAResponseResult_t to OCStackResult.
320 * @param caCode CAResponseResult_t code.
321 * @return ::OC_STACK_OK on success, some other value upon failure.
323 static OCStackResult CAResponseToOCStackResult(CAResponseResult_t caCode);
326 * Convert OCTransportFlags_t to CATransportModifiers_t.
328 * @param ocConType OCTransportFlags_t input.
329 * @return CATransportFlags
331 static CATransportFlags_t OCToCATransportFlags(OCTransportFlags ocConType);
334 * Convert CATransportFlags_t to OCTransportModifiers_t.
336 * @param caConType CATransportFlags_t input.
337 * @return OCTransportFlags
339 static OCTransportFlags CAToOCTransportFlags(CATransportFlags_t caConType);
342 * Handle response from presence request.
344 * @param endPoint CA remote endpoint.
345 * @param responseInfo CA response info.
346 * @return ::OC_STACK_OK on success, some other value upon failure.
348 static OCStackResult HandlePresenceResponse(const CAEndpoint_t *endPoint,
349 const CAResponseInfo_t *responseInfo);
352 * This function will be called back by CA layer when a response is received.
354 * @param endPoint CA remote endpoint.
355 * @param responseInfo CA response info.
357 static void HandleCAResponses(const CAEndpoint_t* endPoint,
358 const CAResponseInfo_t* responseInfo);
361 * This function will be called back by CA layer when a request is received.
363 * @param endPoint CA remote endpoint.
364 * @param requestInfo CA request info.
366 static void HandleCARequests(const CAEndpoint_t* endPoint,
367 const CARequestInfo_t* requestInfo);
370 * Extract query from a URI.
372 * @param uri Full URI with query.
373 * @param query Pointer to string that will contain query.
374 * @param newURI Pointer to string that will contain URI.
375 * @return ::OC_STACK_OK on success, some other value upon failure.
377 static OCStackResult getQueryFromUri(const char * uri, char** resourceType, char ** newURI);
380 * Finds a resource type in an OCResourceType link-list.
382 * @param resourceTypeList The link-list to be searched through.
383 * @param resourceTypeName The key to search for.
385 * @return Resource type that matches the key (ie. resourceTypeName) or
386 * NULL if there is either an invalid parameter or this function was unable to find the key.
388 static OCResourceType *findResourceType(OCResourceType * resourceTypeList,
389 const char * resourceTypeName);
393 * Reset presence TTL for a ClientCB struct. ttlLevel will be set to 0.
394 * TTL will be set to maxAge.
396 * @param cbNode Callback Node for which presence ttl is to be reset.
397 * @param maxAge New value of ttl in seconds.
399 * @return ::OC_STACK_OK on success, some other value upon failure.
401 static OCStackResult ResetPresenceTTL(ClientCB *cbNode, uint32_t maxAgeSeconds);
405 * Ensure the accept header option is set appropriatly before sending the requests and routing
406 * header option is updated with destination.
408 * @param object CA remote endpoint.
409 * @param requestInfo CA request info.
411 * @return ::OC_STACK_OK on success, some other value upon failure.
413 static OCStackResult OCSendRequest(const CAEndpoint_t *object, CARequestInfo_t *requestInfo);
416 * default adapter state change callback method
418 * @param adapter CA network adapter type.
419 * @param enabled current adapter state.
421 static void OCDefaultAdapterStateChangedHandler(CATransportAdapter_t adapter, bool enabled);
424 * default connection state change callback method
426 * @param info CAEndpoint which has address, port and etc.
427 * @param isConnected current connection state.
429 static void OCDefaultConnectionStateChangedHandler(const CAEndpoint_t *info, bool isConnected);
431 //-----------------------------------------------------------------------------
432 // Internal functions
433 //-----------------------------------------------------------------------------
435 bool checkProxyUri(OCHeaderOption *options, uint8_t numOptions)
437 if (!options || 0 == numOptions)
439 OIC_LOG (INFO, TAG, "No options present");
443 for (uint8_t i = 0; i < numOptions; i++)
445 if (options[i].protocolID == OC_COAP_ID && options[i].optionID == OC_RSRVD_PROXY_OPTION_ID)
447 OIC_LOG(DEBUG, TAG, "Proxy URI is present");
454 uint32_t GetTicks(uint32_t milliSeconds)
459 // Guard against overflow of uint32_t
460 if (milliSeconds <= ((UINT32_MAX - (uint32_t)now) * MILLISECONDS_PER_SECOND) /
461 COAP_TICKS_PER_SECOND)
463 return now + (milliSeconds * COAP_TICKS_PER_SECOND)/MILLISECONDS_PER_SECOND;
471 void CopyEndpointToDevAddr(const CAEndpoint_t *in, OCDevAddr *out)
473 VERIFY_NON_NULL_NR(in, FATAL);
474 VERIFY_NON_NULL_NR(out, FATAL);
476 out->adapter = (OCTransportAdapter)in->adapter;
477 out->flags = CAToOCTransportFlags(in->flags);
478 OICStrcpy(out->addr, sizeof(out->addr), in->addr);
479 out->port = in->port;
480 out->ifindex = in->ifindex;
481 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
482 /* This assert is to prevent accidental mismatch between address size macros defined in
483 * RI and CA and cause crash here. */
484 OC_STATIC_ASSERT(MAX_ADDR_STR_SIZE_CA == MAX_ADDR_STR_SIZE,
485 "Address size mismatch between RI and CA");
486 memcpy(out->routeData, in->routeData, sizeof(in->routeData));
490 void CopyDevAddrToEndpoint(const OCDevAddr *in, CAEndpoint_t *out)
492 VERIFY_NON_NULL_NR(in, FATAL);
493 VERIFY_NON_NULL_NR(out, FATAL);
495 out->adapter = (CATransportAdapter_t)in->adapter;
496 out->flags = OCToCATransportFlags(in->flags);
497 OICStrcpy(out->addr, sizeof(out->addr), in->addr);
498 OICStrcpy(out->remoteId, sizeof(out->remoteId), in->remoteId);
499 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
500 /* This assert is to prevent accidental mismatch between address size macros defined in
501 * RI and CA and cause crash here. */
502 OC_STATIC_ASSERT(MAX_ADDR_STR_SIZE_CA == MAX_ADDR_STR_SIZE,
503 "Address size mismatch between RI and CA");
504 memcpy(out->routeData, in->routeData, sizeof(in->routeData));
506 out->port = in->port;
507 out->ifindex = in->ifindex;
510 void FixUpClientResponse(OCClientResponse *cr)
512 VERIFY_NON_NULL_NR(cr, FATAL);
514 cr->addr = &cr->devAddr;
515 cr->connType = (OCConnectivityType)
516 ((cr->devAddr.adapter << CT_ADAPTER_SHIFT) | (cr->devAddr.flags & CT_MASK_FLAGS));
519 static OCStackResult OCSendRequest(const CAEndpoint_t *object, CARequestInfo_t *requestInfo)
521 VERIFY_NON_NULL(object, FATAL, OC_STACK_INVALID_PARAM);
522 VERIFY_NON_NULL(requestInfo, FATAL, OC_STACK_INVALID_PARAM);
523 OIC_TRACE_BEGIN(%s:OCSendRequest, TAG);
525 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
526 OCStackResult rmResult = RMAddInfo(object->routeData, requestInfo, true, NULL);
527 if (OC_STACK_OK != rmResult)
529 OIC_LOG(ERROR, TAG, "Add destination option failed");
535 // OC stack prefer CBOR encoded payloads.
536 requestInfo->info.acceptFormat = CA_FORMAT_APPLICATION_CBOR;
537 CAResult_t result = CASendRequest(object, requestInfo);
538 if(CA_STATUS_OK != result)
540 OIC_LOG_V(ERROR, TAG, "CASendRequest failed with CA error %u", result);
542 return CAResultToOCResult(result);
548 //-----------------------------------------------------------------------------
549 // Internal API function
550 //-----------------------------------------------------------------------------
552 // This internal function is called to update the stack with the status of
553 // observers and communication failures
554 OCStackResult OCStackFeedBack(CAToken_t token, uint8_t tokenLength, uint8_t status)
556 OCStackResult result = OC_STACK_ERROR;
557 ResourceObserver * observer = NULL;
558 OCEntityHandlerRequest ehRequest = {0};
562 case OC_OBSERVER_NOT_INTERESTED:
563 OIC_LOG(DEBUG, TAG, "observer not interested in our notifications");
564 observer = GetObserverUsingToken(token, tokenLength);
567 result = FormOCEntityHandlerRequest(&ehRequest,
571 (OCResourceHandle)NULL,
572 NULL, PAYLOAD_TYPE_REPRESENTATION,
574 OC_OBSERVE_DEREGISTER,
577 if (result != OC_STACK_OK)
579 FreeObserver(observer);
583 if (observer->resource && observer->resource->entityHandler)
585 observer->resource->entityHandler(OC_OBSERVE_FLAG, &ehRequest,
586 observer->resource->entityHandlerCallbackParam);
589 FreeObserver(observer);
592 result = DeleteObserverUsingToken(token, tokenLength);
593 if (result == OC_STACK_OK)
595 OIC_LOG(DEBUG, TAG, "Removed observer successfully");
599 result = OC_STACK_OK;
600 OIC_LOG(DEBUG, TAG, "Observer Removal failed");
604 case OC_OBSERVER_STILL_INTERESTED:
605 OIC_LOG(DEBUG, TAG, "observer still interested, reset the failedCount");
606 observer = GetObserverUsingToken(token, tokenLength);
609 observer->forceHighQos = 0;
610 observer->failedCommCount = 0;
611 result = OC_STACK_OK;
612 FreeObserver(observer);
616 result = OC_STACK_OBSERVER_NOT_FOUND;
620 case OC_OBSERVER_FAILED_COMM:
621 OIC_LOG(DEBUG, TAG, "observer is unreachable");
622 observer = GetObserverUsingToken (token, tokenLength);
625 if (observer->failedCommCount >= MAX_OBSERVER_FAILED_COMM)
627 result = FormOCEntityHandlerRequest(&ehRequest,
631 (OCResourceHandle)NULL,
632 NULL, PAYLOAD_TYPE_REPRESENTATION,
634 OC_OBSERVE_DEREGISTER,
637 if (result != OC_STACK_OK)
639 FreeObserver(observer);
640 return OC_STACK_ERROR;
643 if (observer->resource && observer->resource->entityHandler)
645 observer->resource->entityHandler(OC_OBSERVE_FLAG, &ehRequest,
646 observer->resource->entityHandlerCallbackParam);
649 result = DeleteObserverUsingToken(token, tokenLength);
650 if (result == OC_STACK_OK)
652 OIC_LOG(DEBUG, TAG, "Removed observer successfully");
656 result = OC_STACK_OK;
657 OIC_LOG(DEBUG, TAG, "Observer Removal failed");
662 observer->failedCommCount++;
663 observer->forceHighQos = 1;
664 OIC_LOG_V(DEBUG, TAG, "Failed count for this observer is %d",
665 observer->failedCommCount);
666 result = OC_STACK_CONTINUE;
669 FreeObserver(observer);
673 OIC_LOG(ERROR, TAG, "Unknown status");
674 result = OC_STACK_ERROR;
680 OCStackResult CAResponseToOCStackResult(CAResponseResult_t caCode)
682 OCStackResult ret = OC_STACK_ERROR;
686 ret = OC_STACK_RESOURCE_CREATED;
689 ret = OC_STACK_RESOURCE_DELETED;
692 ret = OC_STACK_RESOURCE_CHANGED;
699 ret = OC_STACK_INVALID_QUERY;
701 case CA_UNAUTHORIZED_REQ:
702 ret = OC_STACK_UNAUTHORIZED_REQ;
705 ret = OC_STACK_INVALID_OPTION;
708 ret = OC_STACK_NO_RESOURCE;
710 case CA_REQUEST_ENTITY_TOO_LARGE:
711 ret = OC_STACK_TOO_LARGE_REQ;
713 case CA_METHOD_NOT_ALLOWED:
714 ret = OC_STACK_METHOD_NOT_ALLOWED;
716 case CA_NOT_ACCEPTABLE:
717 ret = OC_STACK_NOT_ACCEPTABLE;
719 case CA_FORBIDDEN_REQ:
720 ret = OC_STACK_FORBIDDEN_REQ;
722 case CA_TOO_MANY_REQUESTS:
723 ret = OC_STACK_TOO_MANY_REQUESTS;
725 case CA_INTERNAL_SERVER_ERROR:
726 ret = OC_STACK_INTERNAL_SERVER_ERROR;
728 case CA_NOT_IMPLEMENTED:
729 ret = OC_STACK_NOT_IMPLEMENTED;
732 ret = OC_STACK_BAD_GATEWAY;
734 case CA_SERVICE_UNAVAILABLE:
735 ret = OC_STACK_SERVICE_UNAVAILABLE;
737 case CA_RETRANSMIT_TIMEOUT:
738 ret = OC_STACK_GATEWAY_TIMEOUT;
740 case CA_PROXY_NOT_SUPPORTED:
741 ret = OC_STACK_PROXY_NOT_SUPPORTED;
749 CAResponseResult_t OCToCAStackResult(OCStackResult ocCode, OCMethod method)
751 CAResponseResult_t ret = CA_INTERNAL_SERVER_ERROR;
760 // This Response Code is like HTTP 204 "No Content" but only used in
761 // response to POST and PUT requests.
765 // This Response Code is like HTTP 200 "OK" but only used in response to
770 // This should not happen but,
771 // give it a value just in case but output an error
773 OIC_LOG_V(ERROR, TAG, "Unexpected OC_STACK_OK return code for method [%d].",
777 case OC_STACK_RESOURCE_CREATED:
780 case OC_STACK_RESOURCE_DELETED:
783 case OC_STACK_RESOURCE_CHANGED:
786 case OC_STACK_INVALID_QUERY:
789 case OC_STACK_INVALID_OPTION:
792 case OC_STACK_NO_RESOURCE:
795 case OC_STACK_COMM_ERROR:
796 ret = CA_RETRANSMIT_TIMEOUT;
798 case OC_STACK_METHOD_NOT_ALLOWED:
799 ret = CA_METHOD_NOT_ALLOWED;
801 case OC_STACK_NOT_ACCEPTABLE:
802 ret = CA_NOT_ACCEPTABLE;
804 case OC_STACK_UNAUTHORIZED_REQ:
805 ret = CA_UNAUTHORIZED_REQ;
807 case OC_STACK_FORBIDDEN_REQ:
808 ret = CA_FORBIDDEN_REQ;
810 case OC_STACK_TOO_MANY_REQUESTS:
811 ret = CA_TOO_MANY_REQUESTS;
813 case OC_STACK_INTERNAL_SERVER_ERROR:
814 ret = CA_INTERNAL_SERVER_ERROR;
816 case OC_STACK_NOT_IMPLEMENTED:
817 ret = CA_NOT_IMPLEMENTED;
819 case OC_STACK_BAD_GATEWAY:
820 ret = CA_BAD_GATEWAY;
822 case OC_STACK_SERVICE_UNAVAILABLE:
823 ret = CA_SERVICE_UNAVAILABLE;
825 case OC_STACK_GATEWAY_TIMEOUT:
826 ret = CA_RETRANSMIT_TIMEOUT;
828 case OC_STACK_PROXY_NOT_SUPPORTED:
829 ret = CA_PROXY_NOT_SUPPORTED;
837 CATransportFlags_t OCToCATransportFlags(OCTransportFlags ocFlags)
839 CATransportFlags_t caFlags = (CATransportFlags_t)ocFlags;
841 // supply default behavior.
842 if ((caFlags & (CA_IPV6|CA_IPV4)) == 0)
844 caFlags = (CATransportFlags_t)(caFlags|CA_IPV6|CA_IPV4);
846 if ((caFlags & OC_MASK_SCOPE) == 0)
848 caFlags = (CATransportFlags_t)(caFlags|OC_SCOPE_LINK);
853 OCTransportFlags CAToOCTransportFlags(CATransportFlags_t caFlags)
855 return (OCTransportFlags)caFlags;
859 static OCStackResult ResetPresenceTTL(ClientCB *cbNode, uint32_t maxAgeSeconds)
861 uint32_t lowerBound = 0;
862 uint32_t higherBound = 0;
864 if (!cbNode || !cbNode->presence || !cbNode->presence->timeOut)
866 return OC_STACK_INVALID_PARAM;
869 OIC_LOG_V(INFO, TAG, "Update presence TTL, time is %u", GetTicks(0));
871 cbNode->presence->TTL = maxAgeSeconds;
873 for (int index = 0; index < PresenceTimeOutSize; index++)
875 // Guard against overflow
876 if (cbNode->presence->TTL < (UINT32_MAX/(MILLISECONDS_PER_SECOND*PresenceTimeOut[index]))
879 lowerBound = GetTicks((PresenceTimeOut[index] *
880 cbNode->presence->TTL *
881 MILLISECONDS_PER_SECOND)/100);
885 lowerBound = GetTicks(UINT32_MAX);
888 if (cbNode->presence->TTL < (UINT32_MAX/(MILLISECONDS_PER_SECOND*PresenceTimeOut[index+1]))
891 higherBound = GetTicks((PresenceTimeOut[index + 1] *
892 cbNode->presence->TTL *
893 MILLISECONDS_PER_SECOND)/100);
897 higherBound = GetTicks(UINT32_MAX);
900 cbNode->presence->timeOut[index] = OCGetRandomRange(lowerBound, higherBound);
902 OIC_LOG_V(DEBUG, TAG, "lowerBound timeout %d", lowerBound);
903 OIC_LOG_V(DEBUG, TAG, "higherBound timeout %d", higherBound);
904 OIC_LOG_V(DEBUG, TAG, "timeOut entry %d", cbNode->presence->timeOut[index]);
907 cbNode->presence->TTLlevel = 0;
909 OIC_LOG_V(DEBUG, TAG, "this TTL level %d", cbNode->presence->TTLlevel);
913 const char *convertTriggerEnumToString(OCPresenceTrigger trigger)
915 if (trigger == OC_PRESENCE_TRIGGER_CREATE)
917 return OC_RSRVD_TRIGGER_CREATE;
919 else if (trigger == OC_PRESENCE_TRIGGER_CHANGE)
921 return OC_RSRVD_TRIGGER_CHANGE;
925 return OC_RSRVD_TRIGGER_DELETE;
929 OCPresenceTrigger convertTriggerStringToEnum(const char * triggerStr)
933 return OC_PRESENCE_TRIGGER_CREATE;
935 else if(strcmp(triggerStr, OC_RSRVD_TRIGGER_CREATE) == 0)
937 return OC_PRESENCE_TRIGGER_CREATE;
939 else if(strcmp(triggerStr, OC_RSRVD_TRIGGER_CHANGE) == 0)
941 return OC_PRESENCE_TRIGGER_CHANGE;
945 return OC_PRESENCE_TRIGGER_DELETE;
948 #endif // WITH_PRESENCE
950 OCStackResult OCEncodeAddressForRFC6874(char *outputAddress,
952 const char *inputAddress)
954 VERIFY_NON_NULL(inputAddress, FATAL, OC_STACK_INVALID_PARAM);
955 VERIFY_NON_NULL(outputAddress, FATAL, OC_STACK_INVALID_PARAM);
957 size_t inputLength = strnlen(inputAddress, outputSize);
959 // inputSize includes the null terminator
960 size_t inputSize = inputLength + 1;
962 if (inputSize > outputSize)
964 OIC_LOG_V(ERROR, TAG,
965 "OCEncodeAddressForRFC6874 failed: "
966 "outputSize (%zu) < inputSize (%zu)",
967 outputSize, inputSize);
969 return OC_STACK_ERROR;
972 char* percentChar = strchr(inputAddress, '%');
974 // If there is no '%' character, then no change is required to the string.
975 if (NULL == percentChar)
977 OICStrcpy(outputAddress, outputSize, inputAddress);
981 const char* addressPart = &inputAddress[0];
982 const char* scopeIdPart = percentChar + 1;
984 // Sanity check to make sure this string doesn't have more '%' characters
985 if (NULL != strchr(scopeIdPart, '%'))
987 return OC_STACK_ERROR;
990 // If no string follows the first '%', then the input was invalid.
991 if (scopeIdPart[0] == '\0')
993 OIC_LOG(ERROR, TAG, "OCEncodeAddressForRFC6874 failed: Invalid input string: no scope ID!");
994 return OC_STACK_ERROR;
997 // Check to see if the string is already encoded
998 if ((scopeIdPart[0] == '2') && (scopeIdPart[1] == '5'))
1000 OIC_LOG(ERROR, TAG, "OCEncodeAddressForRFC6874 failed: Input string is already encoded");
1001 return OC_STACK_ERROR;
1004 // Fail if we don't have room for encoded string's two additional chars
1005 if (outputSize < (inputSize + 2))
1007 OIC_LOG(ERROR, TAG, "OCEncodeAddressForRFC6874 failed: encoded output will not fit!");
1008 return OC_STACK_ERROR;
1011 // Restore the null terminator with an escaped '%' character, per RFC 6874
1012 OICStrcpy(outputAddress, scopeIdPart - addressPart, addressPart);
1013 OICStrcat(outputAddress, outputSize, "%25");
1014 OICStrcat(outputAddress, outputSize, scopeIdPart);
1019 OCStackResult OCDecodeAddressForRFC6874(char *outputAddress,
1021 const char *inputAddress,
1024 VERIFY_NON_NULL(inputAddress, FATAL, OC_STACK_INVALID_PARAM);
1025 VERIFY_NON_NULL(outputAddress, FATAL, OC_STACK_INVALID_PARAM);
1029 end = inputAddress + strlen(inputAddress);
1031 size_t inputLength = end - inputAddress;
1033 const char *percent = strchr(inputAddress, '%');
1034 if (!percent || (percent > end))
1036 OICStrcpyPartial(outputAddress, outputSize, inputAddress, inputLength);
1040 if (percent[1] != '2' || percent[2] != '5')
1042 return OC_STACK_INVALID_URI;
1045 int addrlen = percent - inputAddress + 1;
1046 OICStrcpyPartial(outputAddress, outputSize, inputAddress, addrlen);
1047 OICStrcpyPartial(outputAddress + addrlen, outputSize - addrlen,
1048 percent + 3, end - percent - 3);
1054 #ifdef WITH_PRESENCE
1056 * The cononical presence allows constructed URIs to be string compared.
1058 * requestUri must be a char array of size CA_MAX_URI_LENGTH
1060 static int FormCanonicalPresenceUri(const CAEndpoint_t *endpoint,
1061 char *presenceUri, bool isMulticast)
1063 VERIFY_NON_NULL(endpoint , FATAL, OC_STACK_INVALID_PARAM);
1064 VERIFY_NON_NULL(presenceUri, FATAL, OC_STACK_INVALID_PARAM);
1068 OIC_LOG(DEBUG, TAG, "Make Multicast Presence URI");
1069 return snprintf(presenceUri, CA_MAX_URI_LENGTH, "%s", OC_RSRVD_PRESENCE_URI);
1072 CAEndpoint_t *ep = (CAEndpoint_t *)endpoint;
1073 if (ep->adapter == CA_ADAPTER_IP)
1075 if ((ep->flags & CA_IPV6) && !(ep->flags & CA_IPV4))
1077 if ('\0' == ep->addr[0]) // multicast
1079 return snprintf(presenceUri, CA_MAX_URI_LENGTH, OC_RSRVD_PRESENCE_URI);
1083 char addressEncoded[CA_MAX_URI_LENGTH] = {0};
1085 OCStackResult result = OCEncodeAddressForRFC6874(addressEncoded,
1086 sizeof(addressEncoded),
1089 if (OC_STACK_OK != result)
1094 return snprintf(presenceUri, CA_MAX_URI_LENGTH, "coap://[%s]:%u%s",
1095 addressEncoded, ep->port, OC_RSRVD_PRESENCE_URI);
1100 if ('\0' == ep->addr[0]) // multicast
1102 OICStrcpy(ep->addr, sizeof(ep->addr), OC_MULTICAST_IP);
1103 ep->port = OC_MULTICAST_PORT;
1105 return snprintf(presenceUri, CA_MAX_URI_LENGTH, "coap://%s:%u%s",
1106 ep->addr, ep->port, OC_RSRVD_PRESENCE_URI);
1110 // might work for other adapters (untested, but better than nothing)
1111 return snprintf(presenceUri, CA_MAX_URI_LENGTH, "coap://%s%s", ep->addr,
1112 OC_RSRVD_PRESENCE_URI);
1116 OCStackResult HandlePresenceResponse(const CAEndpoint_t *endpoint,
1117 const CAResponseInfo_t *responseInfo)
1119 VERIFY_NON_NULL(endpoint, FATAL, OC_STACK_INVALID_PARAM);
1120 VERIFY_NON_NULL(responseInfo, FATAL, OC_STACK_INVALID_PARAM);
1122 OCStackApplicationResult cbResult = OC_STACK_DELETE_TRANSACTION;
1123 ClientCB * cbNode = NULL;
1124 char *resourceTypeName = NULL;
1125 OCClientResponse response = {.devAddr = {.adapter = OC_DEFAULT_ADAPTER}};
1126 OCStackResult result = OC_STACK_ERROR;
1127 uint32_t maxAge = 0;
1129 char presenceUri[CA_MAX_URI_LENGTH];
1131 int presenceSubscribe = 0;
1132 int multicastPresenceSubscribe = 0;
1134 if (responseInfo->result != CA_CONTENT)
1136 OIC_LOG_V(ERROR, TAG, "HandlePresenceResponse failed %d", responseInfo->result);
1137 return OC_STACK_ERROR;
1140 response.payload = NULL;
1141 response.result = OC_STACK_OK;
1143 CopyEndpointToDevAddr(endpoint, &response.devAddr);
1144 FixUpClientResponse(&response);
1146 if (responseInfo->info.payload)
1148 result = OCParsePayload(&response.payload,
1149 PAYLOAD_TYPE_PRESENCE,
1150 responseInfo->info.payload,
1151 responseInfo->info.payloadSize);
1153 if(result != OC_STACK_OK)
1155 OIC_LOG(ERROR, TAG, "Presence parse failed");
1158 if(!response.payload || response.payload->type != PAYLOAD_TYPE_PRESENCE)
1160 OIC_LOG(ERROR, TAG, "Presence payload was wrong type");
1161 result = OC_STACK_ERROR;
1164 response.sequenceNumber = ((OCPresencePayload*)response.payload)->sequenceNumber;
1165 resourceTypeName = ((OCPresencePayload*)response.payload)->resourceType;
1166 maxAge = ((OCPresencePayload*)response.payload)->maxAge;
1169 // check for unicast presence
1170 uriLen = FormCanonicalPresenceUri(endpoint, presenceUri,
1171 responseInfo->isMulticast);
1172 if (uriLen < 0 || (size_t)uriLen >= sizeof (presenceUri))
1174 return OC_STACK_INVALID_URI;
1176 OIC_LOG(INFO, TAG, "check for unicast presence");
1177 cbNode = GetClientCB(NULL, 0, NULL, presenceUri);
1180 presenceSubscribe = 1;
1184 // check for multicast presence
1185 OIC_LOG(INFO, TAG, "check for multicast presence");
1186 cbNode = GetClientCB(NULL, 0, NULL, OC_RSRVD_PRESENCE_URI);
1189 multicastPresenceSubscribe = 1;
1193 if (!presenceSubscribe && !multicastPresenceSubscribe)
1195 OIC_LOG(INFO, TAG, "Received a presence notification, "
1196 "but need to register presence callback, ignoring");
1200 if (presenceSubscribe)
1202 if(cbNode->sequenceNumber == response.sequenceNumber)
1204 OIC_LOG(INFO, TAG, "No presence change");
1205 ResetPresenceTTL(cbNode, maxAge);
1206 OIC_LOG_V(INFO, TAG, "ResetPresenceTTL - TTLlevel:%d\n", cbNode->presence->TTLlevel);
1212 OIC_LOG(INFO, TAG, "Stopping presence");
1213 response.result = OC_STACK_PRESENCE_STOPPED;
1214 if(cbNode->presence)
1216 OICFree(cbNode->presence->timeOut);
1217 OICFree(cbNode->presence);
1218 cbNode->presence = NULL;
1223 if(!cbNode->presence)
1225 cbNode->presence = (OCPresence *)OICMalloc(sizeof (OCPresence));
1227 if(!(cbNode->presence))
1229 OIC_LOG(ERROR, TAG, "Could not allocate memory for cbNode->presence");
1230 result = OC_STACK_NO_MEMORY;
1234 VERIFY_NON_NULL_V(cbNode->presence);
1235 cbNode->presence->timeOut = NULL;
1236 cbNode->presence->timeOut = (uint32_t *)
1237 OICMalloc(PresenceTimeOutSize * sizeof(uint32_t));
1238 if(!(cbNode->presence->timeOut)){
1240 "Could not allocate memory for cbNode->presence->timeOut");
1241 OICFree(cbNode->presence);
1242 cbNode->presence = NULL;
1243 result = OC_STACK_NO_MEMORY;
1248 ResetPresenceTTL(cbNode, maxAge);
1250 cbNode->sequenceNumber = response.sequenceNumber;
1255 // This is the multicast case
1256 OIC_LOG(INFO, TAG, "this is the multicast presence");
1259 OIC_LOG(INFO, TAG, "Stopping presence");
1260 response.result = OC_STACK_PRESENCE_STOPPED;
1264 // Ensure that a filter is actually applied.
1265 if (resourceTypeName && cbNode->filterResourceType)
1267 OIC_LOG_V(INFO, TAG, "find resource type : %s", resourceTypeName);
1268 if(!findResourceType(cbNode->filterResourceType, resourceTypeName))
1274 OIC_LOG(INFO, TAG, "Before calling into application address space for presence");
1275 cbResult = cbNode->callBack(cbNode->context, cbNode->handle, &response);
1276 OIC_LOG(INFO, TAG, "After calling into application address space for presence");
1278 if (cbResult == OC_STACK_DELETE_TRANSACTION)
1280 FindAndDeleteClientCB(cbNode);
1284 OCPayloadDestroy(response.payload);
1287 #endif // WITH_PRESENCE
1289 OCStackResult HandleBatchResponse(char *requestUri, OCRepPayload **payload)
1291 if (requestUri && *payload)
1293 char *interfaceName = NULL;
1294 char *rtTypeName = NULL;
1295 char *uriQuery = NULL;
1296 char *uriWithoutQuery = NULL;
1297 if (OC_STACK_OK == getQueryFromUri(requestUri, &uriQuery, &uriWithoutQuery))
1299 if (OC_STACK_OK == ExtractFiltersFromQuery(uriQuery, &interfaceName, &rtTypeName))
1301 if (interfaceName && (0 == strcmp(OC_RSRVD_INTERFACE_BATCH, interfaceName)))
1303 char *uri = (*payload)->uri;
1304 if (uri && 0 != strcmp(uriWithoutQuery, uri))
1306 OCRepPayload *newPayload = OCRepPayloadCreate();
1309 OCRepPayloadSetUri(newPayload, uri);
1310 newPayload->next = *payload;
1311 *payload = newPayload;
1318 OICFree(interfaceName);
1319 OICFree(rtTypeName);
1321 OICFree(uriWithoutQuery);
1324 return OC_STACK_INVALID_PARAM;
1327 void OCHandleResponse(const CAEndpoint_t* endPoint, const CAResponseInfo_t* responseInfo)
1329 OIC_TRACE_MARK(%s:OCHandleResponse:%s, TAG, responseInfo->info.resourceUri);
1330 OIC_LOG(DEBUG, TAG, "Enter OCHandleResponse");
1332 #ifdef WITH_PRESENCE
1333 if(responseInfo->info.resourceUri &&
1334 strcmp(responseInfo->info.resourceUri, OC_RSRVD_PRESENCE_URI) == 0)
1336 HandlePresenceResponse(endPoint, responseInfo);
1341 ClientCB *cbNode = GetClientCB(responseInfo->info.token,
1342 responseInfo->info.tokenLength, NULL, NULL);
1346 OIC_LOG(INFO, TAG, "There is a cbNode associated with the response token");
1348 // check obs header option
1349 bool obsHeaderOpt = false;
1350 CAHeaderOption_t *options = responseInfo->info.options;
1351 for (uint8_t i = 0; i< responseInfo->info.numOptions; i++)
1353 if (options && options[i].protocolID == CA_COAP_ID &&
1354 options[i].optionID == COAP_OPTION_OBSERVE)
1356 obsHeaderOpt = true;
1361 if(responseInfo->result == CA_EMPTY)
1363 OIC_LOG(INFO, TAG, "Receiving A ACK/RESET for this token");
1364 // We do not have a case for the client to receive a RESET
1365 if(responseInfo->info.type == CA_MSG_ACKNOWLEDGE)
1367 //This is the case of receiving an ACK on a request to a slow resource!
1368 OIC_LOG(INFO, TAG, "This is a pure ACK");
1369 //TODO: should we inform the client
1370 // app that at least the request was received at the server?
1373 else if(responseInfo->result == CA_RETRANSMIT_TIMEOUT)
1375 OIC_LOG(INFO, TAG, "Receiving A Timeout for this token");
1377 OCClientResponse response =
1378 {.devAddr = {.adapter = OC_DEFAULT_ADAPTER}};
1379 CopyEndpointToDevAddr(endPoint, &response.devAddr);
1380 FixUpClientResponse(&response);
1381 response.resourceUri = OICStrdup(responseInfo->info.resourceUri);
1382 memcpy(response.identity.id, responseInfo->info.identity.id,
1383 sizeof (response.identity.id));
1384 response.identity.id_length = responseInfo->info.identity.id_length;
1386 response.result = CAResponseToOCStackResult(responseInfo->result);
1387 OIC_LOG(INFO, TAG, "Before calling into application address space for reTrans timeout");
1388 cbNode->callBack(cbNode->context,
1389 cbNode->handle, &response);
1390 OIC_LOG(INFO, TAG, "After calling into application address space for reTrans timeout");
1391 FindAndDeleteClientCB(cbNode);
1392 OICFree((void *)response.resourceUri);
1395 else if ((cbNode->method == OC_REST_OBSERVE || cbNode->method == OC_REST_OBSERVE_ALL)
1396 && (responseInfo->result == CA_CONTENT) && !obsHeaderOpt)
1398 OCClientResponse response =
1399 {.devAddr = {.adapter = OC_DEFAULT_ADAPTER}};
1400 CopyEndpointToDevAddr(endPoint, &response.devAddr);
1401 FixUpClientResponse(&response);
1402 response.resourceUri = OICStrdup(responseInfo->info.resourceUri);
1403 memcpy(response.identity.id, responseInfo->info.identity.id,
1404 sizeof (response.identity.id));
1405 response.identity.id_length = responseInfo->info.identity.id_length;
1406 response.result = OC_STACK_UNAUTHORIZED_REQ;
1408 OIC_LOG(INFO, TAG, "Before calling into application address space for observe resp");
1409 cbNode->callBack(cbNode->context,
1412 OIC_LOG(INFO, TAG, "After calling into application address space for observe resp");
1414 FindAndDeleteClientCB(cbNode);
1415 OICFree(response.resourceUri);
1420 OIC_LOG(INFO, TAG, "This is a regular response, A client call back is found");
1422 OCClientResponse response;
1423 memset(&response, 0, sizeof(OCClientResponse));
1424 response.devAddr.adapter = OC_DEFAULT_ADAPTER;
1426 response.sequenceNumber = MAX_SEQUENCE_NUMBER + 1;
1427 CopyEndpointToDevAddr(endPoint, &response.devAddr);
1428 FixUpClientResponse(&response);
1429 response.resourceUri = OICStrdup(responseInfo->info.resourceUri);
1430 memcpy(response.identity.id, responseInfo->info.identity.id,
1431 sizeof (response.identity.id));
1432 response.identity.id_length = responseInfo->info.identity.id_length;
1434 response.result = CAResponseToOCStackResult(responseInfo->result);
1436 if(responseInfo->info.payload &&
1437 responseInfo->info.payloadSize)
1439 OCPayloadType type = PAYLOAD_TYPE_INVALID;
1440 // check the security resource
1441 if (SRMIsSecurityResourceURI(cbNode->requestUri))
1443 type = PAYLOAD_TYPE_SECURITY;
1445 else if (cbNode->method == OC_REST_DISCOVER)
1447 if (strncmp(OC_RSRVD_WELL_KNOWN_URI,cbNode->requestUri,
1448 sizeof(OC_RSRVD_WELL_KNOWN_URI) - 1) == 0)
1450 type = PAYLOAD_TYPE_DISCOVERY;
1453 else if (strcmp(cbNode->requestUri, OC_RSRVD_WELL_KNOWN_MQ_URI) == 0)
1455 type = PAYLOAD_TYPE_DISCOVERY;
1458 else if (strcmp(cbNode->requestUri, OC_RSRVD_DEVICE_URI) == 0)
1460 type = PAYLOAD_TYPE_REPRESENTATION;
1462 else if (strcmp(cbNode->requestUri, OC_RSRVD_PLATFORM_URI) == 0)
1464 type = PAYLOAD_TYPE_REPRESENTATION;
1467 #ifdef ROUTING_GATEWAY
1468 else if (strcmp(cbNode->requestUri, OC_RSRVD_GATEWAY_URI) == 0)
1470 type = PAYLOAD_TYPE_REPRESENTATION;
1473 else if (strcmp(cbNode->requestUri, OC_RSRVD_RD_URI) == 0)
1475 type = PAYLOAD_TYPE_REPRESENTATION;
1478 else if (strcmp(cbNode->requestUri, OC_RSRVD_KEEPALIVE_URI) == 0)
1480 type = PAYLOAD_TYPE_REPRESENTATION;
1485 OIC_LOG_V(ERROR, TAG, "Unknown Payload type in Discovery: %d %s",
1486 cbNode->method, cbNode->requestUri);
1487 OICFree((void *)response.resourceUri);
1491 else if (cbNode->method == OC_REST_GET ||
1492 cbNode->method == OC_REST_PUT ||
1493 cbNode->method == OC_REST_POST ||
1494 cbNode->method == OC_REST_OBSERVE ||
1495 cbNode->method == OC_REST_OBSERVE_ALL ||
1496 cbNode->method == OC_REST_DELETE)
1498 if (cbNode->requestUri)
1500 if (0 == strcmp(OC_RSRVD_PLATFORM_URI, cbNode->requestUri))
1502 type = PAYLOAD_TYPE_REPRESENTATION;
1504 else if (0 == strcmp(OC_RSRVD_DEVICE_URI, cbNode->requestUri))
1506 type = PAYLOAD_TYPE_REPRESENTATION;
1508 if (type == PAYLOAD_TYPE_INVALID)
1510 OIC_LOG_V(INFO, TAG, "Assuming PAYLOAD_TYPE_REPRESENTATION: %d %s",
1511 cbNode->method, cbNode->requestUri);
1512 type = PAYLOAD_TYPE_REPRESENTATION;
1517 OIC_LOG(INFO, TAG, "No Request URI, PROXY URI");
1518 type = PAYLOAD_TYPE_REPRESENTATION;
1523 OIC_LOG_V(ERROR, TAG, "Unknown Payload type: %d %s",
1524 cbNode->method, cbNode->requestUri);
1525 OICFree((void *)response.resourceUri);
1529 // In case of error, still want application to receive the error message.
1530 if (OCResultToSuccess(response.result) || PAYLOAD_TYPE_REPRESENTATION == type)
1532 if(OC_STACK_OK != OCParsePayload(&response.payload,
1534 responseInfo->info.payload,
1535 responseInfo->info.payloadSize))
1537 OIC_LOG(ERROR, TAG, "Error converting payload");
1538 OCPayloadDestroy(response.payload);
1539 OICFree((void *)response.resourceUri);
1545 OICFree((void *)response.resourceUri);
1546 response.resourceUri = OICStrdup(cbNode->requestUri);
1550 response.numRcvdVendorSpecificHeaderOptions = 0;
1551 if(responseInfo->info.numOptions > 0)
1554 //First option always with option ID is COAP_OPTION_OBSERVE if it is available.
1555 if(responseInfo->info.options
1556 && responseInfo->info.options[0].optionID == COAP_OPTION_OBSERVE)
1559 uint32_t observationOption;
1560 uint8_t* optionData = (uint8_t*)responseInfo->info.options[0].optionData;
1561 for (observationOption=0, i=0;
1562 i<sizeof(uint32_t) && i<responseInfo->info.options[0].optionLength;
1566 (observationOption << 8) | optionData[i];
1568 response.sequenceNumber = observationOption;
1569 response.numRcvdVendorSpecificHeaderOptions = responseInfo->info.numOptions - 1;
1574 response.numRcvdVendorSpecificHeaderOptions = responseInfo->info.numOptions;
1577 if(response.numRcvdVendorSpecificHeaderOptions > MAX_HEADER_OPTIONS)
1579 OIC_LOG(ERROR, TAG, "#header options are more than MAX_HEADER_OPTIONS");
1580 OCPayloadDestroy(response.payload);
1581 OICFree((void *)response.resourceUri);
1585 if (response.numRcvdVendorSpecificHeaderOptions > 0)
1587 response.rcvdVendorSpecificHeaderOptions =
1588 (OCHeaderOption *) OICCalloc(response.numRcvdVendorSpecificHeaderOptions, sizeof(OCHeaderOption));
1589 if (NULL == response.rcvdVendorSpecificHeaderOptions)
1591 OIC_LOG(ERROR, TAG, "Failed to allocate memory for vendor header options");
1592 OCPayloadDestroy(response.payload);
1593 OICFree((void *)response.resourceUri);
1597 memcpy(response.rcvdVendorSpecificHeaderOptions, responseInfo->info.options + start,
1598 response.numRcvdVendorSpecificHeaderOptions*sizeof(OCHeaderOption));
1602 if (cbNode->method == OC_REST_OBSERVE &&
1603 response.sequenceNumber > OC_OFFSET_SEQUENCE_NUMBER &&
1604 cbNode->sequenceNumber <= MAX_SEQUENCE_NUMBER &&
1605 response.sequenceNumber <= cbNode->sequenceNumber)
1607 OIC_LOG_V(INFO, TAG, "Received stale notification. Number :%d",
1608 response.sequenceNumber);
1613 if (cbNode->requestUri)
1615 // if request uri is '/oic/rd', update ins value of resource.
1616 char *targetUri = strstr(cbNode->requestUri, OC_RSRVD_RD_URI);
1619 OCUpdateResourceInsWithResponse(cbNode->requestUri, &response);
1623 // set remoteID(device ID) into OCClientResponse callback parameter
1624 if (OC_REST_DISCOVER == cbNode->method)
1626 OIC_LOG(INFO, TAG, "cbNode method is OC_REST_DISCOVER");
1627 OCDiscoveryPayload *payload = (OCDiscoveryPayload*) response.payload;
1628 // Payload can be empty in case of error message.
1629 if (payload && payload->sid)
1631 OICStrcpy(response.devAddr.remoteId, sizeof(response.devAddr.remoteId),
1633 OIC_LOG_V(INFO_PRIVATE, TAG, "Device ID of response : %s",
1634 response.devAddr.remoteId);
1637 if(NULL == response.resourceUri)
1639 response.resourceUri = OICStrdup(cbNode->requestUri);
1643 if (cbNode->requestUri)
1645 OIC_LOG_V(INFO, TAG, "cbNode requestUri is %s", cbNode->requestUri);
1646 if (cbNode->method == OC_REST_POST &&
1647 strcmp(cbNode->requestUri, OC_RSRVD_KEEPALIVE_URI) == 0)
1649 OCHandleKeepAliveResponse(endPoint, response.payload);
1653 if (response.payload && response.payload->type == PAYLOAD_TYPE_REPRESENTATION)
1655 OIC_LOG(INFO, TAG, "Handle batch response");
1656 HandleBatchResponse(cbNode->requestUri, (OCRepPayload **)&response.payload);
1659 OIC_LOG(INFO, TAG, "Before calling into application address space for handleResponse");
1660 OCStackApplicationResult appFeedback = cbNode->callBack(cbNode->context,
1663 OIC_LOG(INFO, TAG, "After calling into application address space for handleResponse");
1665 cbNode->sequenceNumber = response.sequenceNumber;
1667 if (appFeedback == OC_STACK_DELETE_TRANSACTION)
1669 FindAndDeleteClientCB(cbNode);
1673 // To keep discovery callbacks active.
1674 cbNode->TTL = GetTicks(MAX_CB_TIMEOUT_SECONDS *
1675 MILLISECONDS_PER_SECOND);
1679 //Need to send ACK when the response is CON
1680 if(responseInfo->info.type == CA_MSG_CONFIRM)
1682 if (!(endPoint->adapter & CA_ADAPTER_TCP))
1684 OIC_LOG(INFO, TAG, "Received a confirmable message. Sending EMPTY");
1685 SendDirectStackResponse(endPoint, responseInfo->info.messageId, CA_EMPTY,
1686 CA_MSG_ACKNOWLEDGE, 0, NULL, NULL, 0, NULL, CA_RESPONSE_FOR_RES);
1690 OICFree((void *)response.resourceUri);
1691 OCPayloadDestroy(response.payload);
1692 OICFree(response.rcvdVendorSpecificHeaderOptions);
1697 ResourceObserver * observer = GetObserverUsingToken (responseInfo->info.token,
1698 responseInfo->info.tokenLength);
1702 OIC_LOG(INFO, TAG, "There is an observer associated with the response token");
1703 if(responseInfo->result == CA_EMPTY)
1705 OIC_LOG(INFO, TAG, "Receiving A ACK/RESET for this token");
1706 if(responseInfo->info.type == CA_MSG_RESET)
1708 OIC_LOG(INFO, TAG, "This is a RESET");
1709 OCStackFeedBack(responseInfo->info.token, responseInfo->info.tokenLength,
1710 OC_OBSERVER_NOT_INTERESTED);
1712 else if(responseInfo->info.type == CA_MSG_ACKNOWLEDGE)
1714 OIC_LOG(INFO, TAG, "This is a pure ACK");
1715 OCStackFeedBack(responseInfo->info.token, responseInfo->info.tokenLength,
1716 OC_OBSERVER_STILL_INTERESTED);
1719 else if(responseInfo->result == CA_RETRANSMIT_TIMEOUT)
1721 OIC_LOG(INFO, TAG, "Receiving Time Out for an observer");
1722 OCStackFeedBack(responseInfo->info.token, responseInfo->info.tokenLength,
1723 OC_OBSERVER_FAILED_COMM);
1726 FreeObserver(observer);
1730 if(!cbNode && !observer)
1732 if(myStackMode == OC_CLIENT || myStackMode == OC_CLIENT_SERVER
1733 || myStackMode == OC_GATEWAY)
1735 OIC_LOG(INFO, TAG, "This is a client, but no cbNode was found for token");
1736 if(responseInfo->result == CA_EMPTY)
1738 OIC_LOG(INFO, TAG, "Receiving CA_EMPTY in the ocstack");
1742 if (!(endPoint->adapter & CA_ADAPTER_TCP))
1744 OIC_LOG(INFO, TAG, "Received a message without callbacks. Sending RESET");
1745 SendDirectStackResponse(endPoint, responseInfo->info.messageId, CA_EMPTY,
1746 CA_MSG_RESET, 0, NULL, NULL, 0, NULL,
1747 CA_RESPONSE_FOR_RES);
1752 if(myStackMode == OC_SERVER || myStackMode == OC_CLIENT_SERVER
1753 || myStackMode == OC_GATEWAY)
1755 OIC_LOG(INFO, TAG, "This is a server, but no observer was found for token");
1756 if (responseInfo->info.type == CA_MSG_ACKNOWLEDGE)
1758 OIC_LOG_V(INFO, TAG, "Received ACK at server for messageId : %d",
1759 responseInfo->info.messageId);
1761 if (responseInfo->info.type == CA_MSG_RESET)
1763 OIC_LOG_V(INFO, TAG, "Received RESET at server for messageId : %d",
1764 responseInfo->info.messageId);
1772 void HandleCAResponses(const CAEndpoint_t* endPoint, const CAResponseInfo_t* responseInfo)
1774 VERIFY_NON_NULL_NR(endPoint, FATAL);
1775 VERIFY_NON_NULL_NR(responseInfo, FATAL);
1777 OIC_LOG(INFO, TAG, "Enter HandleCAResponses");
1778 OIC_TRACE_BEGIN(%s:HandleCAResponses, TAG);
1780 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
1781 #ifdef ROUTING_GATEWAY
1782 bool needRIHandling = false;
1784 * Routing manager is going to update either of endpoint or response or both.
1785 * This typecasting is done to avoid unnecessary duplication of Endpoint and responseInfo
1786 * RM can update "routeData" option in endPoint so that future RI requests can be sent to proper
1789 OCStackResult ret = RMHandleResponse((CAResponseInfo_t *)responseInfo, (CAEndpoint_t *)endPoint,
1791 if(ret != OC_STACK_OK || !needRIHandling)
1793 OIC_LOG_V(INFO, TAG, "Routing status![%d]. Not forwarding to RI", ret);
1800 * Put source in sender endpoint so that the next packet from application can be routed to
1801 * proper destination and remove "RM" coap header option before passing request / response to
1802 * RI as this option will make no sense to either RI or application.
1804 RMUpdateInfo((CAHeaderOption_t **) &(responseInfo->info.options),
1805 (uint8_t *) &(responseInfo->info.numOptions),
1806 (CAEndpoint_t *) endPoint);
1809 OCHandleResponse(endPoint, responseInfo);
1811 OIC_LOG(INFO, TAG, "Exit HandleCAResponses");
1815 * This function handles error response from CA
1816 * code shall be added to handle the errors
1818 void HandleCAErrorResponse(const CAEndpoint_t *endPoint, const CAErrorInfo_t *errorInfo)
1820 VERIFY_NON_NULL_NR(endPoint, FATAL);
1821 VERIFY_NON_NULL_NR(errorInfo, FATAL);
1823 OIC_LOG(INFO, TAG, "Enter HandleCAErrorResponse");
1824 OIC_TRACE_BEGIN(%s:HandleCAErrorResponse, TAG);
1826 ClientCB *cbNode = GetClientCB(errorInfo->info.token,
1827 errorInfo->info.tokenLength, NULL, NULL);
1830 OCClientResponse response = { .devAddr = { .adapter = OC_DEFAULT_ADAPTER } };
1831 CopyEndpointToDevAddr(endPoint, &response.devAddr);
1832 FixUpClientResponse(&response);
1833 response.resourceUri = errorInfo->info.resourceUri;
1834 memcpy(response.identity.id, errorInfo->info.identity.id,
1835 sizeof (response.identity.id));
1836 response.identity.id_length = errorInfo->info.identity.id_length;
1837 response.result = CAResultToOCResult(errorInfo->result);
1839 OIC_LOG(INFO, TAG, "Before calling into application address space for error response");
1840 cbNode->callBack(cbNode->context, cbNode->handle, &response);
1841 OIC_LOG(INFO, TAG, "After calling into application address space for error response");
1844 ResourceObserver *observer = GetObserverUsingToken(errorInfo->info.token,
1845 errorInfo->info.tokenLength);
1848 OIC_LOG(INFO, TAG, "Receiving communication error for an observer");
1849 OCStackResult result = CAResultToOCResult(errorInfo->result);
1850 if (OC_STACK_COMM_ERROR == result)
1852 OCStackFeedBack(errorInfo->info.token, errorInfo->info.tokenLength,
1853 OC_OBSERVER_FAILED_COMM);
1857 OIC_LOG(INFO, TAG, "Exit HandleCAErrorResponse");
1862 * This function sends out Direct Stack Responses. These are responses that are not coming
1863 * from the application entity handler. These responses have no payload and are usually ACKs,
1864 * RESETs or some error conditions that were caught by the stack.
1866 OCStackResult SendDirectStackResponse(const CAEndpoint_t* endPoint, const uint16_t coapID,
1867 const CAResponseResult_t responseResult, const CAMessageType_t type,
1868 const uint8_t numOptions, const CAHeaderOption_t *options,
1869 CAToken_t token, uint8_t tokenLength, const char *resourceUri,
1870 CADataType_t dataType)
1872 OIC_LOG(DEBUG, TAG, "Entering SendDirectStackResponse");
1874 if (endPoint->flags & CA_MULTICAST)
1876 OIC_LOG(ERROR, TAG, "It is unnecessary to respond to a multicast request");
1880 CAResponseInfo_t respInfo = { .result = responseResult };
1881 respInfo.info.messageId = coapID;
1882 respInfo.info.numOptions = numOptions;
1884 if (respInfo.info.numOptions)
1886 respInfo.info.options =
1887 (CAHeaderOption_t *)OICCalloc(respInfo.info.numOptions, sizeof(CAHeaderOption_t));
1888 memcpy (respInfo.info.options, options,
1889 sizeof(CAHeaderOption_t) * respInfo.info.numOptions);
1893 respInfo.info.payload = NULL;
1894 respInfo.info.token = token;
1895 respInfo.info.tokenLength = tokenLength;
1896 respInfo.info.type = type;
1897 respInfo.info.resourceUri = OICStrdup (resourceUri);
1898 respInfo.info.acceptFormat = CA_FORMAT_UNDEFINED;
1899 respInfo.info.dataType = dataType;
1901 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
1902 // Add the destination to route option from the endpoint->routeData.
1903 bool doPost = false;
1904 OCStackResult result = RMAddInfo(endPoint->routeData, &respInfo, false, &doPost);
1905 if(OC_STACK_OK != result)
1907 OIC_LOG_V(ERROR, TAG, "Add routing option failed [%d]", result);
1908 OICFree (respInfo.info.resourceUri);
1909 OICFree (respInfo.info.options);
1914 OIC_LOG(DEBUG, TAG, "Sending a POST message for EMPTY ACK in Client Mode");
1915 CARequestInfo_t reqInfo = {.method = CA_POST };
1916 /* The following initialization is not done in a single initializer block as in
1917 * arduino, .c file is compiled as .cpp and moves it from C99 to C++11. The latter
1918 * does not have designated initalizers. This is a work-around for now.
1920 reqInfo.info.type = CA_MSG_NONCONFIRM;
1921 reqInfo.info.messageId = coapID;
1922 reqInfo.info.tokenLength = tokenLength;
1923 reqInfo.info.token = token;
1924 reqInfo.info.numOptions = respInfo.info.numOptions;
1925 reqInfo.info.payload = NULL;
1926 reqInfo.info.resourceUri = OICStrdup (OC_RSRVD_GATEWAY_URI);
1927 if (reqInfo.info.numOptions)
1929 reqInfo.info.options =
1930 (CAHeaderOption_t *)OICCalloc(reqInfo.info.numOptions, sizeof(CAHeaderOption_t));
1931 if (NULL == reqInfo.info.options)
1933 OIC_LOG(ERROR, TAG, "Calloc failed");
1934 OICFree (reqInfo.info.resourceUri);
1935 OICFree (respInfo.info.resourceUri);
1936 OICFree (respInfo.info.options);
1937 return OC_STACK_NO_MEMORY;
1939 memcpy (reqInfo.info.options, respInfo.info.options,
1940 sizeof(CAHeaderOption_t) * reqInfo.info.numOptions);
1943 CAResult_t caResult = CASendRequest(endPoint, &reqInfo);
1944 OICFree (reqInfo.info.resourceUri);
1945 OICFree (reqInfo.info.options);
1946 OICFree (respInfo.info.resourceUri);
1947 OICFree (respInfo.info.options);
1948 if (CA_STATUS_OK != caResult)
1950 OIC_LOG(ERROR, TAG, "CASendRequest error");
1951 return CAResultToOCResult(caResult);
1957 CAResult_t caResult = CASendResponse(endPoint, &respInfo);
1959 // resourceUri in the info field is cloned in the CA layer and
1960 // thus ownership is still here.
1961 OICFree (respInfo.info.resourceUri);
1962 OICFree (respInfo.info.options);
1963 if(CA_STATUS_OK != caResult)
1965 OIC_LOG(ERROR, TAG, "CASendResponse error");
1966 return CAResultToOCResult(caResult);
1969 OIC_LOG(DEBUG, TAG, "Exit SendDirectStackResponse");
1973 OCStackResult HandleStackRequests(OCServerProtocolRequest * protocolRequest)
1975 OIC_LOG(INFO, TAG, "Entering HandleStackRequests (OCStack Layer)");
1976 OCStackResult result = OC_STACK_ERROR;
1977 if(!protocolRequest)
1979 OIC_LOG(ERROR, TAG, "protocolRequest is NULL");
1980 return OC_STACK_INVALID_PARAM;
1983 OCServerRequest * request = GetServerRequestUsingToken(protocolRequest->requestToken,
1984 protocolRequest->tokenLength);
1987 OIC_LOG(INFO, TAG, "This is a new Server Request");
1988 result = AddServerRequest(&request, protocolRequest->coapID,
1989 protocolRequest->delayedResNeeded, 0, protocolRequest->method,
1990 protocolRequest->numRcvdVendorSpecificHeaderOptions,
1991 protocolRequest->observationOption, protocolRequest->qos,
1992 protocolRequest->query, protocolRequest->rcvdVendorSpecificHeaderOptions,
1993 protocolRequest->payload, protocolRequest->requestToken,
1994 protocolRequest->tokenLength, protocolRequest->resourceUrl,
1995 protocolRequest->reqTotalSize, protocolRequest->acceptFormat,
1996 &protocolRequest->devAddr);
1997 if (OC_STACK_OK != result)
1999 OIC_LOG(ERROR, TAG, "Error adding server request");
2005 OIC_LOG(ERROR, TAG, "Out of Memory");
2006 return OC_STACK_NO_MEMORY;
2009 if(!protocolRequest->reqMorePacket)
2011 request->requestComplete = 1;
2016 OIC_LOG(INFO, TAG, "This is either a repeated or blocked Server Request");
2019 if(request->requestComplete)
2021 OIC_LOG(INFO, TAG, "This Server Request is complete");
2022 ResourceHandling resHandling = OC_RESOURCE_VIRTUAL;
2023 OCResource *resource = NULL;
2024 result = DetermineResourceHandling (request, &resHandling, &resource);
2025 if (result == OC_STACK_OK)
2027 result = ProcessRequest(resHandling, resource, request);
2032 OIC_LOG(INFO, TAG, "This Server Request is incomplete");
2033 result = OC_STACK_CONTINUE;
2038 void OCHandleRequests(const CAEndpoint_t* endPoint, const CARequestInfo_t* requestInfo)
2040 OIC_TRACE_MARK(%s:OCHandleRequests:%s, TAG, requestInfo->info.resourceUri);
2041 OIC_LOG(DEBUG, TAG, "Enter OCHandleRequests");
2043 OCStackResult requestResult = OC_STACK_ERROR;
2045 if(myStackMode == OC_CLIENT)
2047 //TODO: should the client be responding to requests?
2051 OCServerProtocolRequest serverRequest = {0};
2053 OIC_LOG_V(INFO, TAG, "Endpoint URI : %s", requestInfo->info.resourceUri);
2055 char * uriWithoutQuery = NULL;
2056 char * query = NULL;
2058 requestResult = getQueryFromUri(requestInfo->info.resourceUri, &query, &uriWithoutQuery);
2060 if (requestResult != OC_STACK_OK || !uriWithoutQuery)
2062 OIC_LOG_V(ERROR, TAG, "getQueryFromUri() failed with OC error code %d\n", requestResult);
2065 OIC_LOG_V(INFO, TAG, "URI without query: %s", uriWithoutQuery);
2066 OIC_LOG_V(INFO, TAG, "Query : %s", query);
2068 if(strlen(uriWithoutQuery) < MAX_URI_LENGTH)
2070 OICStrcpy(serverRequest.resourceUrl, sizeof(serverRequest.resourceUrl), uriWithoutQuery);
2071 OICFree(uriWithoutQuery);
2075 OIC_LOG(ERROR, TAG, "URI length exceeds MAX_URI_LENGTH.");
2076 OICFree(uriWithoutQuery);
2083 if(strlen(query) < MAX_QUERY_LENGTH)
2085 OICStrcpy(serverRequest.query, sizeof(serverRequest.query), query);
2090 OIC_LOG(ERROR, TAG, "Query length exceeds MAX_QUERY_LENGTH.");
2096 if ((requestInfo->info.payload) && (0 < requestInfo->info.payloadSize))
2098 serverRequest.reqTotalSize = requestInfo->info.payloadSize;
2099 serverRequest.payload = (uint8_t *) OICMalloc(requestInfo->info.payloadSize);
2100 if (!serverRequest.payload)
2102 OIC_LOG(ERROR, TAG, "Allocation for payload failed.");
2105 memcpy (serverRequest.payload, requestInfo->info.payload,
2106 requestInfo->info.payloadSize);
2110 serverRequest.reqTotalSize = 0;
2113 switch (requestInfo->method)
2116 serverRequest.method = OC_REST_GET;
2119 serverRequest.method = OC_REST_PUT;
2122 serverRequest.method = OC_REST_POST;
2125 serverRequest.method = OC_REST_DELETE;
2128 OIC_LOG_V(ERROR, TAG, "Received CA method %d not supported", requestInfo->method);
2129 SendDirectStackResponse(endPoint, requestInfo->info.messageId, CA_BAD_REQ,
2130 requestInfo->info.type, requestInfo->info.numOptions,
2131 requestInfo->info.options, requestInfo->info.token,
2132 requestInfo->info.tokenLength, requestInfo->info.resourceUri,
2134 OICFree(serverRequest.payload);
2138 OIC_LOG_BUFFER(INFO, TAG, (const uint8_t *)requestInfo->info.token,
2139 requestInfo->info.tokenLength);
2141 serverRequest.tokenLength = requestInfo->info.tokenLength;
2142 if (serverRequest.tokenLength) {
2144 serverRequest.requestToken = (CAToken_t)OICMalloc(requestInfo->info.tokenLength);
2146 if (!serverRequest.requestToken)
2148 OIC_LOG(FATAL, TAG, "Allocation for token failed.");
2149 SendDirectStackResponse(endPoint, requestInfo->info.messageId, CA_INTERNAL_SERVER_ERROR,
2150 requestInfo->info.type, requestInfo->info.numOptions,
2151 requestInfo->info.options, requestInfo->info.token,
2152 requestInfo->info.tokenLength, requestInfo->info.resourceUri,
2154 OICFree(serverRequest.payload);
2157 memcpy(serverRequest.requestToken, requestInfo->info.token, requestInfo->info.tokenLength);
2160 switch (requestInfo->info.acceptFormat)
2162 case CA_FORMAT_APPLICATION_CBOR:
2163 serverRequest.acceptFormat = OC_FORMAT_CBOR;
2165 case CA_FORMAT_UNDEFINED:
2166 serverRequest.acceptFormat = OC_FORMAT_UNDEFINED;
2169 serverRequest.acceptFormat = OC_FORMAT_UNSUPPORTED;
2172 if (requestInfo->info.type == CA_MSG_CONFIRM)
2174 serverRequest.qos = OC_HIGH_QOS;
2178 serverRequest.qos = OC_LOW_QOS;
2180 // CA does not need the following field
2181 // Are we sure CA does not need them? how is it responding to multicast
2182 serverRequest.delayedResNeeded = 0;
2184 serverRequest.coapID = requestInfo->info.messageId;
2186 CopyEndpointToDevAddr(endPoint, &serverRequest.devAddr);
2188 // copy vendor specific header options
2189 uint8_t tempNum = (requestInfo->info.numOptions);
2191 // Assume no observation requested and it is a pure GET.
2192 // If obs registration/de-registration requested it'll be fetched from the
2193 // options in GetObserveHeaderOption()
2194 serverRequest.observationOption = OC_OBSERVE_NO_OPTION;
2196 GetObserveHeaderOption(&serverRequest.observationOption, requestInfo->info.options, &tempNum);
2197 if (requestInfo->info.numOptions > MAX_HEADER_OPTIONS)
2200 "The request info numOptions is greater than MAX_HEADER_OPTIONS");
2201 SendDirectStackResponse(endPoint, requestInfo->info.messageId, CA_BAD_OPT,
2202 requestInfo->info.type, requestInfo->info.numOptions,
2203 requestInfo->info.options, requestInfo->info.token,
2204 requestInfo->info.tokenLength, requestInfo->info.resourceUri,
2206 OICFree(serverRequest.payload);
2207 OICFree(serverRequest.requestToken);
2211 serverRequest.numRcvdVendorSpecificHeaderOptions = tempNum;
2212 if (serverRequest.numRcvdVendorSpecificHeaderOptions && requestInfo->info.options)
2214 serverRequest.rcvdVendorSpecificHeaderOptions = (OCHeaderOption*) OICCalloc(tempNum, sizeof(OCHeaderOption));
2215 if (NULL == serverRequest.rcvdVendorSpecificHeaderOptions)
2217 OIC_LOG(ERROR, TAG, "Failed to allocated memory to vnd header options!");
2218 OICFree(serverRequest.payload);
2219 OICFree(serverRequest.requestToken);
2223 memcpy (serverRequest.rcvdVendorSpecificHeaderOptions, requestInfo->info.options,
2224 sizeof(CAHeaderOption_t)*tempNum);
2227 requestResult = HandleStackRequests (&serverRequest);
2229 // Send ACK to client as precursor to slow response
2230 if (requestResult == OC_STACK_SLOW_RESOURCE)
2232 if (requestInfo->info.type == CA_MSG_CONFIRM)
2234 SendDirectStackResponse(endPoint, requestInfo->info.messageId, CA_EMPTY,
2235 CA_MSG_ACKNOWLEDGE,0, NULL, NULL, 0, NULL,
2240 if (requestResult == OC_STACK_RESOURCE_ERROR
2241 && serverRequest.observationOption == OC_OBSERVE_REGISTER)
2243 OIC_LOG_V(ERROR, TAG, "Observe Registration failed due to resource error");
2246 if (serverRequest.observationOption == OC_OBSERVE_REGISTER)
2248 if (requestResult == OC_STACK_RESOURCE_ERROR)
2250 OIC_LOG_V(ERROR, TAG, "Observe Registration failed due to resource error");
2252 else if (!OCResultToSuccess(requestResult))
2254 DeleteObserverUsingToken(requestInfo->info.token, requestInfo->info.tokenLength);
2258 else if(!OCResultToSuccess(requestResult))
2260 OIC_LOG_V(ERROR, TAG, "HandleStackRequests failed. error: %d", requestResult);
2262 // Delete observer node if it is OBSERVE failure from app
2263 if (serverRequest.observationOption == OC_OBSERVE_REGISTER)
2265 DeleteObserverUsingToken(requestInfo->info.token, requestInfo->info.tokenLength);
2268 CAResponseResult_t stackResponse =
2269 OCToCAStackResult(requestResult, serverRequest.method);
2271 SendDirectStackResponse(endPoint, requestInfo->info.messageId, stackResponse,
2272 requestInfo->info.type, requestInfo->info.numOptions,
2273 requestInfo->info.options, requestInfo->info.token,
2274 requestInfo->info.tokenLength, requestInfo->info.resourceUri,
2277 // requestToken is fed to HandleStackRequests, which then goes to AddServerRequest.
2278 // The token is copied in there, and is thus still owned by this function.
2279 OICFree(serverRequest.payload);
2280 OICFree(serverRequest.requestToken);
2281 OICFree(serverRequest.rcvdVendorSpecificHeaderOptions);
2282 OIC_LOG(INFO, TAG, "Exit OCHandleRequests");
2285 //This function will be called back by CA layer when a request is received
2286 void HandleCARequests(const CAEndpoint_t* endPoint, const CARequestInfo_t* requestInfo)
2288 OIC_LOG(INFO, TAG, "Enter HandleCARequests");
2289 OIC_TRACE_BEGIN(%s:HandleCARequests, TAG);
2292 OIC_LOG(ERROR, TAG, "endPoint is NULL");
2299 OIC_LOG(ERROR, TAG, "requestInfo is NULL");
2304 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
2305 #ifdef ROUTING_GATEWAY
2306 bool needRIHandling = false;
2307 bool isEmptyMsg = false;
2309 * Routing manager is going to update either of endpoint or request or both.
2310 * This typecasting is done to avoid unnecessary duplication of Endpoint and requestInfo
2311 * RM can update "routeData" option in endPoint so that future RI requests can be sent to proper
2312 * destination. It can also remove "RM" coap header option before passing request / response to
2313 * RI as this option will make no sense to either RI or application.
2315 OCStackResult ret = RMHandleRequest((CARequestInfo_t *)requestInfo, (CAEndpoint_t *)endPoint,
2316 &needRIHandling, &isEmptyMsg);
2317 if(OC_STACK_OK != ret || !needRIHandling)
2319 OIC_LOG_V(INFO, TAG, "Routing status![%d]. Not forwarding to RI", ret);
2326 * Put source in sender endpoint so that the next packet from application can be routed to
2327 * proper destination and remove RM header option.
2329 RMUpdateInfo((CAHeaderOption_t **) &(requestInfo->info.options),
2330 (uint8_t *) &(requestInfo->info.numOptions),
2331 (CAEndpoint_t *) endPoint);
2333 #ifdef ROUTING_GATEWAY
2337 * In Gateways, the MSGType in route option is used to check if the actual
2338 * response is EMPTY message(4 bytes CoAP Header). In case of Client, the
2339 * EMPTY response is sent in the form of POST request which need to be changed
2340 * to a EMPTY response by RM. This translation is done in this part of the code.
2342 OIC_LOG(INFO, TAG, "This is a Empty response from the Client");
2343 CAResponseInfo_t respInfo = {.result = CA_EMPTY,
2344 .info.messageId = requestInfo->info.messageId,
2345 .info.type = CA_MSG_ACKNOWLEDGE};
2346 OCHandleResponse(endPoint, &respInfo);
2352 // Normal handling of the packet
2353 OCHandleRequests(endPoint, requestInfo);
2355 OIC_LOG(INFO, TAG, "Exit HandleCARequests");
2359 //-----------------------------------------------------------------------------
2361 //-----------------------------------------------------------------------------
2363 OCStackResult OCSetRAInfo(const OCRAInfo_t *raInfo)
2366 !raInfo->username ||
2367 !raInfo->hostname ||
2368 !raInfo->xmpp_domain)
2371 return OC_STACK_INVALID_PARAM;
2373 OCStackResult result = CAResultToOCResult(CASetRAInfo((const CARAInfo_t *) raInfo));
2374 gRASetInfo = (result == OC_STACK_OK)? true : false;
2380 OCStackResult OCInit(const char *ipAddr, uint16_t port, OCMode mode)
2384 return OCInit1(mode, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS);
2387 OCStackResult OCInit1(OCMode mode, OCTransportFlags serverFlags, OCTransportFlags clientFlags)
2389 OC_UNUSED(serverFlags);
2390 OC_UNUSED(clientFlags);
2392 OIC_LOG(DEBUG, TAG, "call OCInit1");
2393 return OCInit2(mode, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS, OC_DEFAULT_ADAPTER);
2396 OCStackResult OCInit2(OCMode mode, OCTransportFlags serverFlags, OCTransportFlags clientFlags,
2397 OCTransportAdapter transportType)
2399 if(stackState == OC_STACK_INITIALIZED)
2401 OIC_LOG(INFO, TAG, "Subsequent calls to OCInit() without calling \
2402 OCStop() between them are ignored.");
2406 #ifndef ROUTING_GATEWAY
2407 if (OC_GATEWAY == mode)
2409 OIC_LOG(ERROR, TAG, "Routing Manager not supported");
2410 return OC_STACK_INVALID_PARAM;
2417 OIC_LOG(ERROR, TAG, "Need to call OCSetRAInfo before calling OCInit");
2418 return OC_STACK_ERROR;
2422 OCStackResult result = OC_STACK_ERROR;
2423 OIC_LOG(INFO, TAG, "Entering OCInit");
2426 if (!((mode == OC_CLIENT) || (mode == OC_SERVER) || (mode == OC_CLIENT_SERVER)
2427 || (mode == OC_GATEWAY)))
2429 OIC_LOG(ERROR, TAG, "Invalid mode");
2430 return OC_STACK_ERROR;
2434 if (mode == OC_CLIENT || mode == OC_CLIENT_SERVER || mode == OC_GATEWAY)
2436 caglobals.client = true;
2438 if (mode == OC_SERVER || mode == OC_CLIENT_SERVER || mode == OC_GATEWAY)
2440 caglobals.server = true;
2443 caglobals.serverFlags = (CATransportFlags_t)serverFlags;
2444 if (!(caglobals.serverFlags & CA_IPFAMILY_MASK))
2446 caglobals.serverFlags = (CATransportFlags_t)(caglobals.serverFlags|CA_IPV4|CA_IPV6);
2448 caglobals.clientFlags = (CATransportFlags_t)clientFlags;
2449 if (!(caglobals.clientFlags & CA_IPFAMILY_MASK))
2451 caglobals.clientFlags = (CATransportFlags_t)(caglobals.clientFlags|CA_IPV4|CA_IPV6);
2454 defaultDeviceHandler = NULL;
2455 defaultDeviceHandlerCallbackParameter = NULL;
2457 result = InitializeScheduleResourceList();
2458 VERIFY_SUCCESS(result, OC_STACK_OK);
2460 result = CAResultToOCResult(CAInitialize((CATransportAdapter_t)transportType));
2461 VERIFY_SUCCESS(result, OC_STACK_OK);
2463 result = CAResultToOCResult(OCSelectNetwork(transportType));
2464 VERIFY_SUCCESS(result, OC_STACK_OK);
2466 result = CAResultToOCResult(CARegisterNetworkMonitorHandler(
2467 OCDefaultAdapterStateChangedHandler, OCDefaultConnectionStateChangedHandler));
2468 VERIFY_SUCCESS(result, OC_STACK_OK);
2470 switch (myStackMode)
2473 CARegisterHandler(HandleCARequests, HandleCAResponses, HandleCAErrorResponse);
2474 result = CAResultToOCResult(CAStartDiscoveryServer());
2475 OIC_LOG(INFO, TAG, "Client mode: CAStartDiscoveryServer");
2478 SRMRegisterHandler(HandleCARequests, HandleCAResponses, HandleCAErrorResponse);
2479 result = CAResultToOCResult(CAStartListeningServer());
2480 OIC_LOG(INFO, TAG, "Server mode: CAStartListeningServer");
2482 case OC_CLIENT_SERVER:
2484 SRMRegisterHandler(HandleCARequests, HandleCAResponses, HandleCAErrorResponse);
2485 result = CAResultToOCResult(CAStartListeningServer());
2486 if(result == OC_STACK_OK)
2488 result = CAResultToOCResult(CAStartDiscoveryServer());
2492 VERIFY_SUCCESS(result, OC_STACK_OK);
2495 CARegisterKeepAliveHandler(OCHandleKeepAliveConnCB);
2498 #ifdef WITH_PRESENCE
2499 PresenceTimeOutSize = sizeof (PresenceTimeOut) / sizeof (PresenceTimeOut[0]) - 1;
2500 #endif // WITH_PRESENCE
2502 //Update Stack state to initialized
2503 stackState = OC_STACK_INITIALIZED;
2505 // Initialize resource
2506 if(myStackMode != OC_CLIENT)
2508 result = initResources();
2511 // Initialize the SRM Policy Engine
2512 if(result == OC_STACK_OK)
2514 result = SRMInitPolicyEngine();
2515 // TODO after BeachHead delivery: consolidate into single SRMInit()
2517 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
2518 RMSetStackMode(mode);
2519 #ifdef ROUTING_GATEWAY
2520 if (OC_GATEWAY == myStackMode)
2522 result = RMInitialize();
2528 if (result == OC_STACK_OK)
2530 result = OCInitializeKeepAlive(myStackMode);
2534 InitializeObserverList();
2537 if(result != OC_STACK_OK)
2539 OIC_LOG(ERROR, TAG, "Stack initialization error");
2540 TerminateScheduleResourceList();
2541 deleteAllResources();
2543 stackState = OC_STACK_UNINITIALIZED;
2548 OCStackResult OCStop()
2550 OIC_LOG(INFO, TAG, "Entering OCStop");
2552 if (stackState == OC_STACK_UNINIT_IN_PROGRESS)
2554 OIC_LOG(DEBUG, TAG, "Stack already stopping, exiting");
2557 else if (stackState != OC_STACK_INITIALIZED)
2559 OIC_LOG(INFO, TAG, "Stack not initialized");
2560 return OC_STACK_ERROR;
2563 // unset cautil config
2564 CAUtilConfig_t configs = {(CATransportBTFlags_t)CA_DEFAULT_BT_FLAGS};
2565 CAUtilSetBTConfigure(configs);
2567 stackState = OC_STACK_UNINIT_IN_PROGRESS;
2569 CAUnregisterNetworkMonitorHandler(OCDefaultAdapterStateChangedHandler,
2570 OCDefaultConnectionStateChangedHandler);
2572 #ifdef WITH_PRESENCE
2573 // Ensure that the TTL associated with ANY and ALL presence notifications originating from
2574 // here send with the code "OC_STACK_PRESENCE_STOPPED" result.
2575 presenceResource.presenceTTL = 0;
2576 presenceState = OC_PRESENCE_UNINITIALIZED;
2577 #endif // WITH_PRESENCE
2579 #ifdef ROUTING_GATEWAY
2580 if (OC_GATEWAY == myStackMode)
2587 OCTerminateKeepAlive(myStackMode);
2590 TerminateScheduleResourceList();
2591 // Remove all observers
2592 DeleteObserverList();
2593 // Free memory dynamically allocated for resources
2594 deleteAllResources();
2595 // Remove all the client callbacks
2596 DeleteClientCBList();
2597 // Terminate connectivity-abstraction layer.
2600 // De-init the SRM Policy Engine
2601 // TODO after BeachHead delivery: consolidate into single SRMDeInit()
2602 SRMDeInitPolicyEngine();
2604 // Destroy Observer List Mutex
2605 TerminateObserverList();
2607 stackState = OC_STACK_UNINITIALIZED;
2611 OCStackResult OCStartMulticastServer()
2613 if(stackState != OC_STACK_INITIALIZED)
2615 OIC_LOG(ERROR, TAG, "OCStack is not initalized. Cannot start multicast server.");
2616 return OC_STACK_ERROR;
2618 CAResult_t ret = CAStartListeningServer();
2619 if (CA_STATUS_OK != ret)
2621 OIC_LOG_V(ERROR, TAG, "Failed starting listening server: %d", ret);
2622 return OC_STACK_ERROR;
2627 OCStackResult OCStopMulticastServer()
2629 CAResult_t ret = CAStopListeningServer();
2630 if (CA_STATUS_OK != ret)
2632 OIC_LOG_V(ERROR, TAG, "Failed stopping listening server: %d", ret);
2633 return OC_STACK_ERROR;
2638 CAMessageType_t qualityOfServiceToMessageType(OCQualityOfService qos)
2643 return CA_MSG_CONFIRM;
2648 return CA_MSG_NONCONFIRM;
2652 OCStackResult ParseRequestUri(const char *fullUri,
2653 OCTransportAdapter adapter,
2654 OCTransportFlags flags,
2655 OCDevAddr **devAddr,
2657 char **resourceType)
2659 VERIFY_NON_NULL(fullUri, FATAL, OC_STACK_INVALID_CALLBACK);
2661 OCStackResult result = OC_STACK_OK;
2662 OCDevAddr *da = NULL;
2666 // provide defaults for all returned values
2673 *resourceUri = NULL;
2677 *resourceType = NULL;
2680 // delimit url prefix, if any
2681 const char *start = fullUri;
2682 char *slash2 = strstr(start, "//");
2687 char *slash = strchr(start, '/');
2690 return OC_STACK_INVALID_URI;
2693 // process url scheme
2694 size_t prefixLen = slash2 - fullUri;
2698 if (((prefixLen == sizeof(COAP_TCP_SCHEME) - 1) && (!strncmp(fullUri, COAP_TCP_SCHEME, prefixLen)))
2699 || ((prefixLen == sizeof(COAPS_TCP_SCHEME) - 1) && (!strncmp(fullUri, COAPS_TCP_SCHEME, prefixLen))))
2705 // TODO: this logic should come in with unit tests exercising the various strings
2706 // processs url prefix, if any
2707 size_t urlLen = slash - start;
2711 if (urlLen && devAddr)
2712 { // construct OCDevAddr
2713 if (start[0] == '[')
2715 char *close = strchr(++start, ']');
2716 if (!close || close > slash)
2718 return OC_STACK_INVALID_URI;
2721 if (close[1] == ':')
2728 adapter = (OCTransportAdapter)(adapter | OC_ADAPTER_TCP);
2732 adapter = (OCTransportAdapter)(adapter | OC_ADAPTER_IP);
2734 flags = (OCTransportFlags)(flags | OC_IP_USE_V6);
2738 char *dot = strchr(start, '.');
2739 if (dot && dot < slash)
2741 colon = strchr(start, ':');
2742 end = (colon && colon < slash) ? colon : slash;
2747 adapter = (OCTransportAdapter)(adapter | OC_ADAPTER_TCP);
2751 adapter = (OCTransportAdapter)(adapter | OC_ADAPTER_IP);
2753 flags = (OCTransportFlags)(flags | OC_IP_USE_V4);
2761 if (len >= sizeof(da->addr))
2763 return OC_STACK_INVALID_URI;
2765 // collect port, if any
2766 if (colon && colon < slash)
2768 for (colon++; colon < slash; colon++)
2771 if (c < '0' || c > '9')
2773 return OC_STACK_INVALID_URI;
2775 port = 10 * port + c - '0';
2780 if (len >= sizeof(da->addr))
2782 return OC_STACK_INVALID_URI;
2785 da = (OCDevAddr *)OICCalloc(sizeof (OCDevAddr), 1);
2788 return OC_STACK_NO_MEMORY;
2791 // Decode address per RFC 6874.
2792 result = OCDecodeAddressForRFC6874(da->addr, sizeof(da->addr), start, end);
2793 if (result != OC_STACK_OK)
2800 da->adapter = adapter;
2802 if (!strncmp(fullUri, "coaps", 5))
2804 da->flags = (OCTransportFlags)(da->flags|CA_SECURE);
2809 // process resource uri, if any
2811 { // request uri and query
2812 size_t ulen = strlen(slash); // resource uri length
2813 size_t tlen = 0; // resource type length
2816 static const char strPresence[] = "/oic/ad?rt=";
2817 static const size_t lenPresence = sizeof(strPresence) - 1;
2818 if (!strncmp(slash, strPresence, lenPresence))
2820 type = slash + lenPresence;
2821 tlen = ulen - lenPresence;
2826 *resourceUri = (char *)OICMalloc(ulen + 1);
2829 result = OC_STACK_NO_MEMORY;
2832 OICStrcpy(*resourceUri, (ulen + 1), slash);
2835 if (type && resourceType)
2837 *resourceType = (char *)OICMalloc(tlen + 1);
2840 result = OC_STACK_NO_MEMORY;
2844 OICStrcpy(*resourceType, (tlen+1), type);
2851 // free all returned values
2858 OICFree(*resourceUri);
2862 OICFree(*resourceType);
2867 #ifdef WITH_PRESENCE
2868 static OCStackResult OCPreparePresence(CAEndpoint_t *endpoint,
2872 char uri[CA_MAX_URI_LENGTH];
2874 FormCanonicalPresenceUri(endpoint, uri, isMulticast);
2876 *requestUri = OICStrdup(uri);
2879 return OC_STACK_NO_MEMORY;
2884 #endif // WITH_PRESENCE
2887 * Discover or Perform requests on a specified resource
2889 OCStackResult OCDoResource(OCDoHandle *handle,
2891 const char *requestUri,
2892 const OCDevAddr *destination,
2894 OCConnectivityType connectivityType,
2895 OCQualityOfService qos,
2896 OCCallbackData *cbData,
2897 OCHeaderOption *options,
2900 OCStackResult ret = OCDoRequest(handle, method, requestUri,destination, payload,
2901 connectivityType, qos, cbData, options, numOptions);
2903 // This is the owner of the payload object, so we free it
2904 OCPayloadDestroy(payload);
2909 * Discover or Perform requests on a specified resource
2911 OCStackResult OCDoRequest(OCDoHandle *handle,
2913 const char *requestUri,
2914 const OCDevAddr *destination,
2916 OCConnectivityType connectivityType,
2917 OCQualityOfService qos,
2918 OCCallbackData *cbData,
2919 OCHeaderOption *options,
2922 OIC_LOG(INFO, TAG, "Entering OCDoResource");
2923 OIC_TRACE_BEGIN(%s:OCDoRequest, TAG);
2925 // Validate input parameters
2926 VERIFY_NON_NULL(cbData, FATAL, OC_STACK_INVALID_CALLBACK);
2927 VERIFY_NON_NULL(cbData->cb, FATAL, OC_STACK_INVALID_CALLBACK);
2929 OCStackResult result = OC_STACK_ERROR;
2930 CAResult_t caResult;
2931 CAToken_t token = NULL;
2932 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
2933 ClientCB *clientCB = NULL;
2934 OCDoHandle resHandle = NULL;
2935 CAEndpoint_t endpoint = {.adapter = CA_DEFAULT_ADAPTER};
2936 OCDevAddr tmpDevAddr = { OC_DEFAULT_ADAPTER };
2938 OCTransportAdapter adapter;
2939 OCTransportFlags flags;
2940 // the request contents are put here
2941 CARequestInfo_t requestInfo = {.method = CA_GET};
2942 // requestUri will be parsed into the following three variables
2943 OCDevAddr *devAddr = NULL;
2944 char *resourceUri = NULL;
2945 char *resourceType = NULL;
2948 * Support original behavior with address on resourceUri argument.
2950 adapter = (OCTransportAdapter)(connectivityType >> CT_ADAPTER_SHIFT);
2951 flags = (OCTransportFlags)(connectivityType & CT_MASK_FLAGS);
2955 result = ParseRequestUri(requestUri, adapter, flags, &devAddr, &resourceUri, &resourceType);
2956 if (result != OC_STACK_OK)
2958 OIC_LOG_V(DEBUG, TAG, "Unable to parse uri: %s", requestUri);
2962 else if (!checkProxyUri(options, numOptions))
2964 OIC_LOG(ERROR, TAG, "Request doesn't contain RequestURI/Proxy URI");
2971 case OC_REST_OBSERVE:
2972 case OC_REST_OBSERVE_ALL:
2973 requestInfo.method = CA_GET;
2976 requestInfo.method = CA_PUT;
2979 requestInfo.method = CA_POST;
2981 case OC_REST_DELETE:
2982 requestInfo.method = CA_DELETE;
2984 case OC_REST_DISCOVER:
2985 #ifdef WITH_PRESENCE
2986 case OC_REST_PRESENCE:
2988 if (destination || devAddr)
2990 requestInfo.isMulticast = false;
2994 tmpDevAddr.adapter = adapter;
2995 tmpDevAddr.flags = flags;
2996 destination = &tmpDevAddr;
2997 requestInfo.isMulticast = true;
3000 // OC_REST_DISCOVER: CA_DISCOVER will become GET and isMulticast.
3001 // OC_REST_PRESENCE: Since "presence" is a stack layer only implementation.
3002 // replacing method type with GET.
3003 requestInfo.method = CA_GET;
3006 result = OC_STACK_INVALID_METHOD;
3010 if (!devAddr && !destination)
3012 OIC_LOG(DEBUG, TAG, "no devAddr and no destination");
3013 result = OC_STACK_INVALID_PARAM;
3017 /* If not original behavior, use destination argument */
3018 if (destination && !devAddr)
3020 devAddr = (OCDevAddr *)OICMalloc(sizeof (OCDevAddr));
3023 result = OC_STACK_NO_MEMORY;
3026 OIC_LOG(DEBUG, TAG, "devAddr is set as destination");
3027 *devAddr = *destination;
3032 OIC_LOG_V(INFO_PRIVATE, TAG, "remoteId of devAddr : %s", devAddr->remoteId);
3033 if (!requestInfo.isMulticast)
3035 OIC_LOG_V(DEBUG, TAG, "remoteAddr of devAddr : [%s]:[%d]",
3036 devAddr->addr, devAddr->port);
3040 resHandle = GenerateInvocationHandle();
3043 result = OC_STACK_NO_MEMORY;
3047 caResult = CAGenerateToken(&token, tokenLength);
3048 if (caResult != CA_STATUS_OK)
3050 OIC_LOG(ERROR, TAG, "CAGenerateToken error");
3051 result= OC_STACK_ERROR;
3055 // fill in request data
3056 requestInfo.info.type = qualityOfServiceToMessageType(qos);
3057 requestInfo.info.token = token;
3058 requestInfo.info.tokenLength = tokenLength;
3060 if ((method == OC_REST_OBSERVE) || (method == OC_REST_OBSERVE_ALL))
3062 result = CreateObserveHeaderOption (&(requestInfo.info.options),
3063 options, numOptions, OC_OBSERVE_REGISTER);
3064 if (result != OC_STACK_OK)
3068 requestInfo.info.numOptions = numOptions + 1;
3072 requestInfo.info.numOptions = numOptions;
3073 requestInfo.info.options =
3074 (CAHeaderOption_t*) OICCalloc(numOptions, sizeof(CAHeaderOption_t));
3075 memcpy(requestInfo.info.options, (CAHeaderOption_t*)options,
3076 numOptions * sizeof(CAHeaderOption_t));
3079 CopyDevAddrToEndpoint(devAddr, &endpoint);
3084 OCConvertPayload(payload, &requestInfo.info.payload, &requestInfo.info.payloadSize))
3087 OIC_LOG(ERROR, TAG, "Failed to create CBOR Payload");
3090 requestInfo.info.payloadFormat = CA_FORMAT_APPLICATION_CBOR;
3094 requestInfo.info.payload = NULL;
3095 requestInfo.info.payloadSize = 0;
3096 requestInfo.info.payloadFormat = CA_FORMAT_UNDEFINED;
3099 // prepare for response
3100 #ifdef WITH_PRESENCE
3101 if (method == OC_REST_PRESENCE)
3103 char *presenceUri = NULL;
3104 result = OCPreparePresence(&endpoint, &presenceUri,
3105 requestInfo.isMulticast);
3106 if (OC_STACK_OK != result)
3111 // Assign full presence uri as coap://ip:port/oic/ad to add to callback list.
3112 // Presence notification will form a canonical uri to
3113 // look for callbacks into the application.
3116 OICFree(resourceUri);
3118 resourceUri = presenceUri;
3122 // update resourceUri onto requestInfo after check presence uri
3123 requestInfo.info.resourceUri = resourceUri;
3125 ttl = GetTicks(MAX_CB_TIMEOUT_SECONDS * MILLISECONDS_PER_SECOND);
3126 result = AddClientCB(&clientCB, cbData, token, tokenLength, &resHandle,
3127 method, devAddr, resourceUri, resourceType, ttl);
3128 if (OC_STACK_OK != result)
3133 cbData = NULL; // Client CB list entry now owns it
3134 token = NULL; // Client CB list entry now owns it
3135 devAddr = NULL; // Client CB list entry now owns it
3136 resourceUri = NULL; // Client CB list entry now owns it
3137 resourceType = NULL; // Client CB list entry now owns it
3139 #ifdef WITH_PRESENCE
3140 if (method == OC_REST_PRESENCE)
3142 OIC_LOG(ERROR, TAG, "AddClientCB for presence done.");
3146 *handle = resHandle;
3154 result = OCSendRequest(&endpoint, &requestInfo);
3155 if (OC_STACK_OK != result)
3162 *handle = resHandle;
3166 if (result != OC_STACK_OK)
3168 OIC_LOG(ERROR, TAG, "OCDoResource error");
3169 if (NULL != cbData && NULL != cbData->cd)
3171 cbData->cd(cbData->context);
3173 FindAndDeleteClientCB(clientCB);
3174 CADestroyToken(token);
3182 OICFree(requestInfo.info.payload);
3184 OICFree(resourceUri);
3185 OICFree(resourceType);
3186 OICFree(requestInfo.info.options);
3191 OCStackResult OCCancel(OCDoHandle handle, OCQualityOfService qos, OCHeaderOption * options,
3195 * This ftn is implemented one of two ways in the case of observation:
3197 * 1. qos == OC_NON_CONFIRMABLE. When observe is unobserved..
3198 * Remove the callback associated on client side.
3199 * When the next notification comes in from server,
3200 * reply with RESET message to server.
3201 * Keep in mind that the server will react to RESET only
3202 * if the last notification was sent as CON
3204 * 2. qos == OC_CONFIRMABLE. When OCCancel is called,
3205 * and it is associated with an observe request
3206 * (i.e. ClientCB->method == OC_REST_OBSERVE || OC_REST_OBSERVE_ALL),
3207 * Send CON Observe request to server with
3208 * observe flag = OC_RESOURCE_OBSERVE_DEREGISTER.
3209 * Remove the callback associated on client side.
3211 OCStackResult ret = OC_STACK_OK;
3212 CAEndpoint_t endpoint = {.adapter = CA_DEFAULT_ADAPTER};
3213 CARequestInfo_t requestInfo = {.method = CA_GET};
3217 return OC_STACK_INVALID_PARAM;
3220 ClientCB *clientCB = GetClientCB(NULL, 0, handle, NULL);
3223 OIC_LOG(ERROR, TAG, "Callback not found. Called OCCancel on same resource twice?");
3224 return OC_STACK_ERROR;
3227 switch (clientCB->method)
3229 case OC_REST_OBSERVE:
3230 case OC_REST_OBSERVE_ALL:
3232 OIC_LOG_V(INFO, TAG, "Canceling observation for resource %s", clientCB->requestUri);
3234 CopyDevAddrToEndpoint(clientCB->devAddr, &endpoint);
3236 if ((endpoint.adapter & CA_ADAPTER_IP) && qos != OC_HIGH_QOS)
3238 FindAndDeleteClientCB(clientCB);
3242 OIC_LOG(INFO, TAG, "Cancelling observation as CONFIRMABLE");
3244 requestInfo.info.type = qualityOfServiceToMessageType(qos);
3245 requestInfo.info.token = clientCB->token;
3246 requestInfo.info.tokenLength = clientCB->tokenLength;
3248 if (CreateObserveHeaderOption (&(requestInfo.info.options),
3249 options, numOptions, OC_OBSERVE_DEREGISTER) != OC_STACK_OK)
3251 return OC_STACK_ERROR;
3253 requestInfo.info.numOptions = numOptions + 1;
3254 requestInfo.info.resourceUri = OICStrdup (clientCB->requestUri);
3257 ret = OCSendRequest(&endpoint, &requestInfo);
3259 if (requestInfo.info.options)
3261 OICFree (requestInfo.info.options);
3263 if (requestInfo.info.resourceUri)
3265 OICFree (requestInfo.info.resourceUri);
3270 case OC_REST_DISCOVER:
3271 OIC_LOG_V(INFO, TAG, "Cancelling discovery callback for resource %s",
3272 clientCB->requestUri);
3273 FindAndDeleteClientCB(clientCB);
3276 #ifdef WITH_PRESENCE
3277 case OC_REST_PRESENCE:
3278 FindAndDeleteClientCB(clientCB);
3284 case OC_REST_DELETE:
3285 OIC_LOG_V(INFO, TAG, "Cancelling request callback for resource %s",
3286 clientCB->requestUri);
3287 FindAndDeleteClientCB(clientCB);
3291 ret = OC_STACK_INVALID_METHOD;
3299 * @brief Register Persistent storage callback.
3300 * @param persistentStorageHandler [IN] Pointers to open, read, write, close & unlink handlers.
3302 * OC_STACK_OK - No errors; Success
3303 * OC_STACK_INVALID_PARAM - Invalid parameter
3305 OCStackResult OCRegisterPersistentStorageHandler(OCPersistentStorage* persistentStorageHandler)
3307 OIC_LOG(INFO, TAG, "RegisterPersistentStorageHandler !!");
3308 if(!persistentStorageHandler)
3310 OIC_LOG(ERROR, TAG, "The persistent storage handler is invalid");
3311 return OC_STACK_INVALID_PARAM;
3315 if( !persistentStorageHandler->open ||
3316 !persistentStorageHandler->close ||
3317 !persistentStorageHandler->read ||
3318 !persistentStorageHandler->unlink ||
3319 !persistentStorageHandler->write)
3321 OIC_LOG(ERROR, TAG, "The persistent storage handler is invalid");
3322 return OC_STACK_INVALID_PARAM;
3325 return SRMRegisterPersistentStorageHandler(persistentStorageHandler);
3328 #ifdef WITH_PRESENCE
3330 OCStackResult OCProcessPresence()
3332 OCStackResult result = OC_STACK_OK;
3334 // the following line floods the log with messages that are irrelevant
3335 // to most purposes. Uncomment as needed.
3336 //OIC_LOG(INFO, TAG, "Entering RequestPresence");
3337 ClientCB* cbNode = NULL;
3338 ClientCB* tempcbNode = NULL;
3339 OCClientResponse clientResponse;
3340 OCStackApplicationResult cbResult = OC_STACK_DELETE_TRANSACTION;
3342 LL_FOREACH_SAFE(cbList, cbNode, tempcbNode)
3344 if (OC_REST_PRESENCE != cbNode->method || !cbNode->presence)
3349 uint32_t now = GetTicks(0);
3350 OIC_LOG_V(DEBUG, TAG, "this TTL level %d",
3351 cbNode->presence->TTLlevel);
3352 OIC_LOG_V(DEBUG, TAG, "current ticks %d", now);
3354 if (cbNode->presence->TTLlevel > PresenceTimeOutSize)
3359 if (cbNode->presence->TTLlevel < PresenceTimeOutSize)
3361 OIC_LOG_V(DEBUG, TAG, "timeout ticks %d",
3362 cbNode->presence->timeOut[cbNode->presence->TTLlevel]);
3364 if (cbNode->presence->TTLlevel >= PresenceTimeOutSize)
3366 OIC_LOG(DEBUG, TAG, "No more timeout ticks");
3368 clientResponse.sequenceNumber = 0;
3369 clientResponse.result = OC_STACK_PRESENCE_TIMEOUT;
3370 clientResponse.devAddr = *cbNode->devAddr;
3371 FixUpClientResponse(&clientResponse);
3372 clientResponse.payload = NULL;
3374 // Increment the TTLLevel (going to a next state), so we don't keep
3375 // sending presence notification to client.
3376 cbNode->presence->TTLlevel++;
3377 OIC_LOG_V(DEBUG, TAG, "moving to TTL level %d",
3378 cbNode->presence->TTLlevel);
3380 OIC_LOG(INFO, TAG, "Before calling into application address space for presence timeout");
3381 cbResult = cbNode->callBack(cbNode->context, cbNode->handle, &clientResponse);
3382 OIC_LOG(INFO, TAG, "After calling into application address space for presence timeout");
3384 if (cbResult == OC_STACK_DELETE_TRANSACTION)
3386 FindAndDeleteClientCB(cbNode);
3391 if (now < cbNode->presence->timeOut[cbNode->presence->TTLlevel])
3396 CAEndpoint_t endpoint = {.adapter = CA_DEFAULT_ADAPTER};
3397 CAInfo_t requestData = {.type = CA_MSG_CONFIRM};
3398 CARequestInfo_t requestInfo = {.method = CA_GET};
3400 OIC_LOG(DEBUG, TAG, "time to test server presence");
3402 CopyDevAddrToEndpoint(cbNode->devAddr, &endpoint);
3404 requestData.type = CA_MSG_NONCONFIRM;
3405 requestData.token = cbNode->token;
3406 requestData.tokenLength = cbNode->tokenLength;
3407 requestData.resourceUri = OC_RSRVD_PRESENCE_URI;
3408 requestInfo.method = CA_GET;
3409 requestInfo.info = requestData;
3411 result = OCSendRequest(&endpoint, &requestInfo);
3412 if (OC_STACK_OK != result)
3417 cbNode->presence->TTLlevel++;
3418 OIC_LOG_V(DEBUG, TAG, "moving to TTL level %d", cbNode->presence->TTLlevel);
3421 if (result != OC_STACK_OK)
3423 OIC_LOG(ERROR, TAG, "OCProcessPresence error");
3428 #endif // WITH_PRESENCE
3430 OCStackResult OCProcess()
3432 if (stackState == OC_STACK_UNINITIALIZED)
3434 OIC_LOG(ERROR, TAG, "OCProcess has failed. ocstack is not initialized");
3435 return OC_STACK_ERROR;
3437 #ifdef WITH_PRESENCE
3438 OCProcessPresence();
3440 CAHandleRequestResponse();
3442 #ifdef ROUTING_GATEWAY
3447 OCProcessKeepAlive();
3452 #ifdef WITH_PRESENCE
3453 OCStackResult OCStartPresence(const uint32_t ttl)
3455 OIC_LOG(INFO, TAG, "Entering OCStartPresence");
3456 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
3457 OCChangeResourceProperty(
3458 &(((OCResource *)presenceResource.handle)->resourceProperties),
3461 if (OC_MAX_PRESENCE_TTL_SECONDS < ttl)
3463 presenceResource.presenceTTL = OC_MAX_PRESENCE_TTL_SECONDS;
3464 OIC_LOG(INFO, TAG, "Setting Presence TTL to max value");
3468 presenceResource.presenceTTL = OC_DEFAULT_PRESENCE_TTL_SECONDS;
3469 OIC_LOG(INFO, TAG, "Setting Presence TTL to default value");
3473 presenceResource.presenceTTL = ttl;
3476 OIC_LOG_V(DEBUG, TAG, "Presence TTL is %" PRIu32 " seconds", presenceResource.presenceTTL);
3479 if (OC_PRESENCE_UNINITIALIZED == presenceState)
3481 presenceState = OC_PRESENCE_INITIALIZED;
3483 OCDevAddr devAddr = { OC_DEFAULT_ADAPTER };
3485 CAToken_t caToken = NULL;
3486 CAResult_t caResult = CAGenerateToken(&caToken, tokenLength);
3487 if (caResult != CA_STATUS_OK)
3489 OIC_LOG(ERROR, TAG, "CAGenerateToken error");
3490 CADestroyToken(caToken);
3491 return OC_STACK_ERROR;
3494 AddObserver(OC_RSRVD_PRESENCE_URI, NULL, 0, caToken, tokenLength,
3495 (OCResource *)presenceResource.handle, OC_LOW_QOS, OC_FORMAT_UNDEFINED, &devAddr);
3496 CADestroyToken(caToken);
3499 // Each time OCStartPresence is called
3500 // a different random 32-bit integer number is used
3501 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
3503 return SendPresenceNotification(((OCResource *)presenceResource.handle)->rsrcType,
3504 OC_PRESENCE_TRIGGER_CREATE);
3507 OCStackResult OCStopPresence()
3509 OIC_LOG(INFO, TAG, "Entering OCStopPresence");
3510 OCStackResult result = OC_STACK_ERROR;
3512 if(presenceResource.handle)
3514 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
3516 // make resource inactive
3517 result = OCChangeResourceProperty(
3518 &(((OCResource *) presenceResource.handle)->resourceProperties),
3522 if(result != OC_STACK_OK)
3525 "Changing the presence resource properties to ACTIVE not successful");
3529 return SendStopNotification();
3533 OCStackResult OCSetDefaultDeviceEntityHandler(OCDeviceEntityHandler entityHandler,
3534 void* callbackParameter)
3536 defaultDeviceHandler = entityHandler;
3537 defaultDeviceHandlerCallbackParameter = callbackParameter;
3542 OCStackResult OCCreateResource(OCResourceHandle *handle,
3543 const char *resourceTypeName,
3544 const char *resourceInterfaceName,
3545 const char *uri, OCEntityHandler entityHandler,
3546 void* callbackParam,
3547 uint8_t resourceProperties)
3550 OCResource *pointer = NULL;
3551 OCStackResult result = OC_STACK_ERROR;
3553 OIC_LOG(INFO, TAG, "Entering OCCreateResource");
3555 if(myStackMode == OC_CLIENT)
3557 return OC_STACK_INVALID_PARAM;
3559 // Validate parameters
3560 if(!uri || uri[0]=='\0' || strlen(uri)>=MAX_URI_LENGTH )
3562 OIC_LOG(ERROR, TAG, "URI is empty or too long");
3563 return OC_STACK_INVALID_URI;
3565 // Is it presented during resource discovery?
3566 if (!handle || !resourceTypeName || resourceTypeName[0] == '\0' )
3568 OIC_LOG(ERROR, TAG, "Input parameter is NULL");
3569 return OC_STACK_INVALID_PARAM;
3572 if (!resourceInterfaceName || strlen(resourceInterfaceName) == 0)
3574 resourceInterfaceName = OC_RSRVD_INTERFACE_DEFAULT;
3578 resourceProperties = resourceProperties | OC_MQ_PUBLISHER;
3580 // Make sure resourceProperties bitmask has allowed properties specified
3581 if (resourceProperties
3582 > (OC_ACTIVE | OC_DISCOVERABLE | OC_OBSERVABLE | OC_SLOW | OC_SECURE |
3583 OC_EXPLICIT_DISCOVERABLE
3592 OIC_LOG(ERROR, TAG, "Invalid property");
3593 return OC_STACK_INVALID_PARAM;
3596 // If the headResource is NULL, then no resources have been created...
3597 pointer = headResource;
3600 // At least one resources is in the resource list, so we need to search for
3601 // repeated URLs, which are not allowed. If a repeat is found, exit with an error
3604 if (strncmp(uri, pointer->uri, MAX_URI_LENGTH) == 0)
3606 OIC_LOG_V(ERROR, TAG, "Resource %s already exists", uri);
3607 return OC_STACK_INVALID_PARAM;
3609 pointer = pointer->next;
3612 // Create the pointer and insert it into the resource list
3613 pointer = (OCResource *) OICCalloc(1, sizeof(OCResource));
3616 result = OC_STACK_NO_MEMORY;
3619 pointer->sequenceNum = OC_OFFSET_SEQUENCE_NUMBER;
3621 insertResource(pointer);
3624 pointer->uri = OICStrdup(uri);
3627 result = OC_STACK_NO_MEMORY;
3631 // Set properties. Set OC_ACTIVE
3632 pointer->resourceProperties = (OCResourceProperty) (resourceProperties
3635 // Add the resourcetype to the resource
3636 result = BindResourceTypeToResource(pointer, resourceTypeName);
3637 if (result != OC_STACK_OK)
3639 OIC_LOG(ERROR, TAG, "Error adding resourcetype");
3643 // Add the resourceinterface to the resource
3644 result = BindResourceInterfaceToResource(pointer, resourceInterfaceName);
3645 if (result != OC_STACK_OK)
3647 OIC_LOG(ERROR, TAG, "Error adding resourceinterface");
3651 // If an entity handler has been passed, attach it to the newly created
3652 // resource. Otherwise, set the default entity handler.
3655 pointer->entityHandler = entityHandler;
3656 pointer->entityHandlerCallbackParam = callbackParam;
3660 pointer->entityHandler = defaultResourceEHandler;
3661 pointer->entityHandlerCallbackParam = NULL;
3664 // Initialize a pointer indicating child resources in case of collection
3665 pointer->rsrcChildResourcesHead = NULL;
3668 result = OC_STACK_OK;
3670 #ifdef WITH_PRESENCE
3671 if (presenceResource.handle)
3673 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
3674 SendPresenceNotification(pointer->rsrcType, OC_PRESENCE_TRIGGER_CREATE);
3678 if (result != OC_STACK_OK)
3680 // Deep delete of resource and other dynamic elements that it contains
3681 deleteResource(pointer);
3686 OCStackResult OCBindResource(
3687 OCResourceHandle collectionHandle, OCResourceHandle resourceHandle)
3689 OCResource *resource = NULL;
3690 OCChildResource *tempChildResource = NULL;
3691 OCChildResource *newChildResource = NULL;
3693 OIC_LOG(INFO, TAG, "Entering OCBindResource");
3695 // Validate parameters
3696 VERIFY_NON_NULL(collectionHandle, ERROR, OC_STACK_ERROR);
3697 VERIFY_NON_NULL(resourceHandle, ERROR, OC_STACK_ERROR);
3698 // Container cannot contain itself
3699 if (collectionHandle == resourceHandle)
3701 OIC_LOG(ERROR, TAG, "Added handle equals collection handle");
3702 return OC_STACK_INVALID_PARAM;
3705 // Use the handle to find the resource in the resource linked list
3706 resource = findResource((OCResource *) collectionHandle);
3709 OIC_LOG(ERROR, TAG, "Collection handle not found");
3710 return OC_STACK_INVALID_PARAM;
3713 // Look for an open slot to add add the child resource.
3714 // If found, add it and return success
3716 tempChildResource = resource->rsrcChildResourcesHead;
3718 while(resource->rsrcChildResourcesHead && tempChildResource->next)
3720 // TODO: what if one of child resource was deregistered without unbinding?
3721 tempChildResource = tempChildResource->next;
3724 // Do memory allocation for child resource
3725 newChildResource = (OCChildResource *) OICCalloc(1, sizeof(OCChildResource));
3726 if(!newChildResource)
3728 OIC_LOG(ERROR, TAG, "Adding new child resource is failed due to memory allocation failure");
3729 return OC_STACK_ERROR;
3732 newChildResource->rsrcResource = (OCResource *) resourceHandle;
3733 newChildResource->next = NULL;
3735 if(!resource->rsrcChildResourcesHead)
3737 resource->rsrcChildResourcesHead = newChildResource;
3740 tempChildResource->next = newChildResource;
3743 OIC_LOG(INFO, TAG, "resource bound");
3745 #ifdef WITH_PRESENCE
3746 if (presenceResource.handle)
3748 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
3749 SendPresenceNotification(((OCResource *) resourceHandle)->rsrcType,
3750 OC_PRESENCE_TRIGGER_CHANGE);
3757 OCStackResult OCUnBindResource(
3758 OCResourceHandle collectionHandle, OCResourceHandle resourceHandle)
3760 OCResource *resource = NULL;
3761 OCChildResource *tempChildResource = NULL;
3762 OCChildResource *tempLastChildResource = NULL;
3764 OIC_LOG(INFO, TAG, "Entering OCUnBindResource");
3766 // Validate parameters
3767 VERIFY_NON_NULL(collectionHandle, ERROR, OC_STACK_ERROR);
3768 VERIFY_NON_NULL(resourceHandle, ERROR, OC_STACK_ERROR);
3769 // Container cannot contain itself
3770 if (collectionHandle == resourceHandle)
3772 OIC_LOG(ERROR, TAG, "removing handle equals collection handle");
3773 return OC_STACK_INVALID_PARAM;
3776 // Use the handle to find the resource in the resource linked list
3777 resource = findResource((OCResource *) collectionHandle);
3780 OIC_LOG(ERROR, TAG, "Collection handle not found");
3781 return OC_STACK_INVALID_PARAM;
3784 // Look for an open slot to add add the child resource.
3785 // If found, add it and return success
3786 if(!resource->rsrcChildResourcesHead)
3788 OIC_LOG(INFO, TAG, "resource not found in collection");
3790 // Unable to add resourceHandle, so return error
3791 return OC_STACK_ERROR;
3795 tempChildResource = resource->rsrcChildResourcesHead;
3797 while (tempChildResource)
3799 if(tempChildResource->rsrcResource == resourceHandle)
3801 // if resource going to be unbinded is the head one.
3802 if( tempChildResource == resource->rsrcChildResourcesHead )
3804 OCChildResource *temp = resource->rsrcChildResourcesHead->next;
3805 OICFree(resource->rsrcChildResourcesHead);
3806 resource->rsrcChildResourcesHead = temp;
3811 OCChildResource *temp = tempChildResource->next;
3812 OICFree(tempChildResource);
3813 if (tempLastChildResource)
3815 tempLastChildResource->next = temp;
3820 OIC_LOG(INFO, TAG, "resource unbound");
3822 // Send notification when resource is unbounded successfully.
3823 #ifdef WITH_PRESENCE
3824 if (presenceResource.handle)
3826 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
3827 SendPresenceNotification(((OCResource *) resourceHandle)->rsrcType,
3828 OC_PRESENCE_TRIGGER_CHANGE);
3831 tempChildResource = NULL;
3832 tempLastChildResource = NULL;
3838 tempLastChildResource = tempChildResource;
3839 tempChildResource = tempChildResource->next;
3842 OIC_LOG(INFO, TAG, "resource not found in collection");
3844 tempChildResource = NULL;
3845 tempLastChildResource = NULL;
3847 // Unable to add resourceHandle, so return error
3848 return OC_STACK_ERROR;
3851 static bool ValidateResourceTypeInterface(const char *resourceItemName)
3853 if (!resourceItemName)
3857 // Per RFC 6690 only registered values must follow the first rule below.
3858 // At this point in time the only values registered begin with "core", and
3859 // all other values are specified as opaque strings where multiple values
3860 // are separated by a space.
3861 if (strncmp(resourceItemName, CORESPEC, sizeof(CORESPEC) - 1) == 0)
3863 for(size_t index = sizeof(CORESPEC) - 1; resourceItemName[index]; ++index)
3865 if (resourceItemName[index] != '.'
3866 && resourceItemName[index] != '-'
3867 && (resourceItemName[index] < 'a' || resourceItemName[index] > 'z')
3868 && (resourceItemName[index] < '0' || resourceItemName[index] > '9'))
3876 for (size_t index = 0; resourceItemName[index]; ++index)
3878 if (resourceItemName[index] == ' '
3879 || resourceItemName[index] == '\t'
3880 || resourceItemName[index] == '\r'
3881 || resourceItemName[index] == '\n')
3891 OCStackResult BindResourceTypeToResource(OCResource* resource,
3892 const char *resourceTypeName)
3894 OCResourceType *pointer = NULL;
3896 OCStackResult result = OC_STACK_ERROR;
3898 VERIFY_NON_NULL(resourceTypeName, ERROR, OC_STACK_INVALID_PARAM);
3900 if (!ValidateResourceTypeInterface(resourceTypeName))
3902 OIC_LOG(ERROR, TAG, "resource type illegal (see RFC 6690)");
3903 return OC_STACK_INVALID_PARAM;
3906 pointer = (OCResourceType *) OICCalloc(1, sizeof(OCResourceType));
3909 result = OC_STACK_NO_MEMORY;
3913 str = OICStrdup(resourceTypeName);
3916 result = OC_STACK_NO_MEMORY;
3919 pointer->resourcetypename = str;
3920 pointer->next = NULL;
3922 insertResourceType(resource, pointer);
3923 result = OC_STACK_OK;
3926 if (result != OC_STACK_OK)
3935 OCStackResult BindResourceInterfaceToResource(OCResource* resource,
3936 const char *resourceInterfaceName)
3938 OCResourceInterface *pointer = NULL;
3940 OCStackResult result = OC_STACK_ERROR;
3942 VERIFY_NON_NULL(resourceInterfaceName, ERROR, OC_STACK_INVALID_PARAM);
3944 if (!ValidateResourceTypeInterface(resourceInterfaceName))
3946 OIC_LOG(ERROR, TAG, "resource /interface illegal (see RFC 6690)");
3947 return OC_STACK_INVALID_PARAM;
3950 OIC_LOG_V(INFO, TAG, "Binding %s interface to %s", resourceInterfaceName, resource->uri);
3952 pointer = (OCResourceInterface *) OICCalloc(1, sizeof(OCResourceInterface));
3955 result = OC_STACK_NO_MEMORY;
3959 str = OICStrdup(resourceInterfaceName);
3962 result = OC_STACK_NO_MEMORY;
3965 pointer->name = str;
3967 // Bind the resourceinterface to the resource
3968 insertResourceInterface(resource, pointer);
3970 result = OC_STACK_OK;
3973 if (result != OC_STACK_OK)
3982 OCStackResult OCBindResourceTypeToResource(OCResourceHandle handle,
3983 const char *resourceTypeName)
3986 OCStackResult result = OC_STACK_ERROR;
3987 OCResource *resource = NULL;
3989 resource = findResource((OCResource *) handle);
3992 OIC_LOG(ERROR, TAG, "Resource not found");
3993 return OC_STACK_ERROR;
3996 result = BindResourceTypeToResource(resource, resourceTypeName);
3998 #ifdef WITH_PRESENCE
3999 if(presenceResource.handle)
4001 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
4002 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
4009 OCStackResult OCResetResourceTypes(OCResourceHandle handle,
4010 const char *newResourceType)
4012 OCStackResult result = OC_STACK_ERROR;
4013 OCResource *resource = NULL;
4015 resource = findResource((OCResource *) handle);
4018 OIC_LOG(ERROR, TAG, "Resource not found");
4019 return OC_STACK_ERROR;
4022 // Clear all bound resource types
4023 deleteResourceType(resource->rsrcType);
4024 resource->rsrcType = NULL;
4026 // Bind new resource type to resource
4027 result = BindResourceTypeToResource(resource, newResourceType);
4029 #ifdef WITH_PRESENCE
4030 if(presenceResource.handle)
4032 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
4033 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
4040 OCStackResult OCBindResourceInterfaceToResource(OCResourceHandle handle,
4041 const char *resourceInterfaceName)
4044 OCStackResult result = OC_STACK_ERROR;
4045 OCResource *resource = NULL;
4047 resource = findResource((OCResource *) handle);
4050 OIC_LOG(ERROR, TAG, "Resource not found");
4051 return OC_STACK_ERROR;
4054 result = BindResourceInterfaceToResource(resource, resourceInterfaceName);
4056 #ifdef WITH_PRESENCE
4057 if (presenceResource.handle)
4059 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
4060 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
4067 OCStackResult OCResetResourceInterfaces(OCResourceHandle handle,
4068 const char *newResourceInterface)
4070 OCStackResult result = OC_STACK_ERROR;
4071 OCResource *resource = NULL;
4073 resource = findResource((OCResource *) handle);
4076 OIC_LOG(ERROR, TAG, "Resource not found");
4077 return OC_STACK_ERROR;
4080 // Clear all bound interface
4081 deleteResourceInterface(resource->rsrcInterface);
4082 resource->rsrcInterface = NULL;
4084 // Bind new interface to resource
4085 result = BindResourceInterfaceToResource(resource, newResourceInterface);
4087 #ifdef WITH_PRESENCE
4088 if (presenceResource.handle)
4090 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
4091 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
4098 OCStackResult OCGetNumberOfResources(uint8_t *numResources)
4100 OCResource *pointer = headResource;
4102 VERIFY_NON_NULL(numResources, ERROR, OC_STACK_INVALID_PARAM);
4106 *numResources = *numResources + 1;
4107 pointer = pointer->next;
4112 OCResourceHandle OCGetResourceHandle(uint8_t index)
4114 OCResource *pointer = headResource;
4116 for( uint8_t i = 0; i < index && pointer; ++i)
4118 pointer = pointer->next;
4120 return (OCResourceHandle) pointer;
4123 OCStackResult OCDeleteResource(OCResourceHandle handle)
4127 OIC_LOG(ERROR, TAG, "Invalid handle for deletion");
4128 return OC_STACK_INVALID_PARAM;
4131 OCResource *resource = findResource((OCResource *) handle);
4132 if (resource == NULL)
4134 OIC_LOG(ERROR, TAG, "Resource not found");
4135 return OC_STACK_NO_RESOURCE;
4138 if (deleteResource((OCResource *) handle) != OC_STACK_OK)
4140 OIC_LOG(ERROR, TAG, "Error deleting resource");
4141 return OC_STACK_ERROR;
4147 const char *OCGetResourceUri(OCResourceHandle handle)
4149 OCResource *resource = NULL;
4151 resource = findResource((OCResource *) handle);
4154 return resource->uri;
4156 return (const char *) NULL;
4159 OCResourceProperty OCGetResourceProperties(OCResourceHandle handle)
4161 OCResource *resource = NULL;
4163 resource = findResource((OCResource *) handle);
4166 return resource->resourceProperties;
4168 return (OCResourceProperty)-1;
4171 OCStackResult OCGetNumberOfResourceTypes(OCResourceHandle handle,
4172 uint8_t *numResourceTypes)
4174 OCResource *resource = NULL;
4175 OCResourceType *pointer = NULL;
4177 VERIFY_NON_NULL(numResourceTypes, ERROR, OC_STACK_INVALID_PARAM);
4178 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
4180 *numResourceTypes = 0;
4182 resource = findResource((OCResource *) handle);
4185 pointer = resource->rsrcType;
4188 *numResourceTypes = *numResourceTypes + 1;
4189 pointer = pointer->next;
4195 const char *OCGetResourceTypeName(OCResourceHandle handle, uint8_t index)
4197 OCResourceType *resourceType = NULL;
4199 resourceType = findResourceTypeAtIndex(handle, index);
4202 return resourceType->resourcetypename;
4204 return (const char *) NULL;
4207 OCStackResult OCGetNumberOfResourceInterfaces(OCResourceHandle handle,
4208 uint8_t *numResourceInterfaces)
4210 OCResourceInterface *pointer = NULL;
4211 OCResource *resource = NULL;
4213 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
4214 VERIFY_NON_NULL(numResourceInterfaces, ERROR, OC_STACK_INVALID_PARAM);
4216 *numResourceInterfaces = 0;
4217 resource = findResource((OCResource *) handle);
4220 pointer = resource->rsrcInterface;
4223 *numResourceInterfaces = *numResourceInterfaces + 1;
4224 pointer = pointer->next;
4230 const char *OCGetResourceInterfaceName(OCResourceHandle handle, uint8_t index)
4232 OCResourceInterface *resourceInterface = NULL;
4234 resourceInterface = findResourceInterfaceAtIndex(handle, index);
4235 if (resourceInterface)
4237 return resourceInterface->name;
4239 return (const char *) NULL;
4242 OCResourceHandle OCGetResourceHandleFromCollection(OCResourceHandle collectionHandle,
4245 OCResource *resource = NULL;
4246 OCChildResource *tempChildResource = NULL;
4249 resource = findResource((OCResource *) collectionHandle);
4255 tempChildResource = resource->rsrcChildResourcesHead;
4257 while(tempChildResource)
4261 return tempChildResource->rsrcResource;
4264 tempChildResource = tempChildResource->next;
4267 // In this case, the number of resource handles in the collection exceeds the index
4268 tempChildResource = NULL;
4272 OCStackResult OCBindResourceHandler(OCResourceHandle handle,
4273 OCEntityHandler entityHandler,
4274 void* callbackParam)
4276 OCResource *resource = NULL;
4278 // Validate parameters
4279 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
4281 // Use the handle to find the resource in the resource linked list
4282 resource = findResource((OCResource *)handle);
4285 OIC_LOG(ERROR, TAG, "Resource not found");
4286 return OC_STACK_ERROR;
4290 resource->entityHandler = entityHandler;
4291 resource->entityHandlerCallbackParam = callbackParam;
4293 #ifdef WITH_PRESENCE
4294 if (presenceResource.handle)
4296 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
4297 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
4304 OCEntityHandler OCGetResourceHandler(OCResourceHandle handle)
4306 OCResource *resource = NULL;
4308 resource = findResource((OCResource *)handle);
4311 OIC_LOG(ERROR, TAG, "Resource not found");
4316 return resource->entityHandler;
4319 void incrementSequenceNumber(OCResource * resPtr)
4321 // Increment the sequence number
4322 resPtr->sequenceNum += 1;
4323 if (resPtr->sequenceNum == MAX_SEQUENCE_NUMBER)
4325 resPtr->sequenceNum = OC_OFFSET_SEQUENCE_NUMBER+1;
4330 #ifdef WITH_PRESENCE
4331 OCStackResult SendPresenceNotification(OCResourceType *resourceType,
4332 OCPresenceTrigger trigger)
4334 OIC_LOG(INFO, TAG, "SendPresenceNotification");
4335 OCResource *resPtr = NULL;
4336 OCStackResult result = OC_STACK_ERROR;
4337 OCMethod method = OC_REST_PRESENCE;
4338 uint32_t maxAge = 0;
4339 resPtr = findResource((OCResource *) presenceResource.handle);
4342 return OC_STACK_NO_RESOURCE;
4345 if((((OCResource *) presenceResource.handle)->resourceProperties) & OC_ACTIVE)
4347 maxAge = presenceResource.presenceTTL;
4349 result = SendAllObserverNotification(method, resPtr, maxAge,
4350 trigger, resourceType, OC_LOW_QOS);
4356 OCStackResult SendStopNotification()
4358 OIC_LOG(INFO, TAG, "SendStopNotification");
4359 OCResource *resPtr = NULL;
4360 OCStackResult result = OC_STACK_ERROR;
4361 OCMethod method = OC_REST_PRESENCE;
4362 resPtr = findResource((OCResource *) presenceResource.handle);
4365 return OC_STACK_NO_RESOURCE;
4368 // maxAge is 0. ResourceType is NULL.
4369 result = SendAllObserverNotification(method, resPtr, 0, OC_PRESENCE_TRIGGER_DELETE,
4375 #endif // WITH_PRESENCE
4376 OCStackResult OCNotifyAllObservers(OCResourceHandle handle, OCQualityOfService qos)
4378 OCResource *resPtr = NULL;
4379 OCStackResult result = OC_STACK_ERROR;
4380 OCMethod method = OC_REST_NOMETHOD;
4381 uint32_t maxAge = 0;
4383 OIC_LOG(INFO, TAG, "Notifying all observers");
4384 #ifdef WITH_PRESENCE
4385 if(handle == presenceResource.handle)
4389 #endif // WITH_PRESENCE
4390 VERIFY_NON_NULL(handle, ERROR, OC_STACK_ERROR);
4392 // Verify that the resource exists
4393 resPtr = findResource ((OCResource *) handle);
4396 return OC_STACK_NO_RESOURCE;
4400 //only increment in the case of regular observing (not presence)
4401 incrementSequenceNumber(resPtr);
4402 method = OC_REST_OBSERVE;
4403 maxAge = MAX_OBSERVE_AGE;
4404 #ifdef WITH_PRESENCE
4405 result = SendAllObserverNotification (method, resPtr, maxAge,
4406 OC_PRESENCE_TRIGGER_DELETE, NULL, qos);
4408 result = SendAllObserverNotification (method, resPtr, maxAge, qos);
4415 OCNotifyListOfObservers (OCResourceHandle handle,
4416 OCObservationId *obsIdList,
4417 uint8_t numberOfIds,
4418 const OCRepPayload *payload,
4419 OCQualityOfService qos)
4421 OIC_LOG(INFO, TAG, "Entering OCNotifyListOfObservers");
4423 OCResource *resPtr = NULL;
4424 //TODO: we should allow the server to define this
4425 uint32_t maxAge = MAX_OBSERVE_AGE;
4427 VERIFY_NON_NULL(handle, ERROR, OC_STACK_ERROR);
4428 VERIFY_NON_NULL(obsIdList, ERROR, OC_STACK_ERROR);
4429 VERIFY_NON_NULL(payload, ERROR, OC_STACK_ERROR);
4431 resPtr = findResource ((OCResource *) handle);
4432 if (NULL == resPtr || myStackMode == OC_CLIENT)
4434 return OC_STACK_NO_RESOURCE;
4438 incrementSequenceNumber(resPtr);
4440 return (SendListObserverNotification(resPtr, obsIdList, numberOfIds,
4441 payload, maxAge, qos));
4444 OCStackResult OCDoResponse(OCEntityHandlerResponse *ehResponse)
4446 OIC_TRACE_BEGIN(%s:OCDoResponse, TAG);
4447 OCStackResult result = OC_STACK_ERROR;
4448 OCServerRequest *serverRequest = NULL;
4450 OIC_LOG(INFO, TAG, "Entering OCDoResponse");
4452 // Validate input parameters
4453 VERIFY_NON_NULL(ehResponse, ERROR, OC_STACK_INVALID_PARAM);
4454 VERIFY_NON_NULL(ehResponse->requestHandle, ERROR, OC_STACK_INVALID_PARAM);
4457 // Get pointer to request info
4458 serverRequest = GetServerRequestUsingHandle(ehResponse->requestHandle);
4461 // response handler in ocserverrequest.c. Usually HandleSingleResponse.
4462 result = serverRequest->ehResponseHandler(ehResponse);
4469 //#ifdef DIRECT_PAIRING
4470 const OCDPDev_t* OCDiscoverDirectPairingDevices(unsigned short waittime)
4472 OIC_LOG(INFO, TAG, "Start OCDiscoverDirectPairingDevices");
4473 if(OC_STACK_OK != DPDeviceDiscovery(waittime))
4475 OIC_LOG(ERROR, TAG, "Fail to discover Direct-Pairing device");
4479 return (const OCDPDev_t*)DPGetDiscoveredDevices();
4482 const OCDPDev_t* OCGetDirectPairedDevices()
4484 return (const OCDPDev_t*)DPGetPairedDevices();
4487 OCStackResult OCDoDirectPairing(void *ctx, OCDPDev_t* peer, OCPrm_t pmSel, char *pinNumber,
4488 OCDirectPairingCB resultCallback)
4490 OIC_LOG(INFO, TAG, "Start OCDoDirectPairing");
4491 if(NULL == peer || NULL == pinNumber)
4493 OIC_LOG(ERROR, TAG, "Invalid parameters");
4494 return OC_STACK_INVALID_PARAM;
4496 if (NULL == resultCallback)
4498 OIC_LOG(ERROR, TAG, "Invalid callback");
4499 return OC_STACK_INVALID_CALLBACK;
4502 return DPDirectPairing(ctx, (OCDirectPairingDev_t*)peer, (OicSecPrm_t)pmSel,
4503 pinNumber, (OCDirectPairingResultCB)resultCallback);
4505 //#endif // DIRECT_PAIRING
4507 //-----------------------------------------------------------------------------
4508 // Private internal function definitions
4509 //-----------------------------------------------------------------------------
4510 static OCDoHandle GenerateInvocationHandle()
4512 OCDoHandle handle = NULL;
4513 // Generate token here, it will be deleted when the transaction is deleted
4514 handle = (OCDoHandle) OICMalloc(sizeof(uint8_t[CA_MAX_TOKEN_LEN]));
4517 OCFillRandomMem((uint8_t*)handle, sizeof(uint8_t[CA_MAX_TOKEN_LEN]));
4523 #ifdef WITH_PRESENCE
4524 OCStackResult OCChangeResourceProperty(OCResourceProperty * inputProperty,
4525 OCResourceProperty resourceProperties, uint8_t enable)
4529 return OC_STACK_INVALID_PARAM;
4531 if (resourceProperties
4532 > (OC_ACTIVE | OC_DISCOVERABLE | OC_OBSERVABLE | OC_SLOW))
4534 OIC_LOG(ERROR, TAG, "Invalid property");
4535 return OC_STACK_INVALID_PARAM;
4539 *inputProperty = (OCResourceProperty) (*inputProperty & ~(resourceProperties));
4543 *inputProperty = (OCResourceProperty) (*inputProperty | resourceProperties);
4549 OCStackResult initResources()
4551 OCStackResult result = OC_STACK_OK;
4553 headResource = NULL;
4554 tailResource = NULL;
4555 // Init Virtual Resources
4556 #ifdef WITH_PRESENCE
4557 presenceResource.presenceTTL = OC_DEFAULT_PRESENCE_TTL_SECONDS;
4559 result = OCCreateResource(&presenceResource.handle,
4560 OC_RSRVD_RESOURCE_TYPE_PRESENCE,
4562 OC_RSRVD_PRESENCE_URI,
4566 //make resource inactive
4567 result = OCChangeResourceProperty(
4568 &(((OCResource *) presenceResource.handle)->resourceProperties),
4571 #ifndef WITH_ARDUINO
4572 if (result == OC_STACK_OK)
4574 result = SRMInitSecureResources();
4578 if(result == OC_STACK_OK)
4580 CreateResetProfile();
4581 result = OCCreateResource(&deviceResource,
4582 OC_RSRVD_RESOURCE_TYPE_DEVICE,
4583 OC_RSRVD_INTERFACE_DEFAULT,
4584 OC_RSRVD_DEVICE_URI,
4588 if(result == OC_STACK_OK)
4590 result = BindResourceInterfaceToResource((OCResource *)deviceResource,
4591 OC_RSRVD_INTERFACE_READ);
4595 if(result == OC_STACK_OK)
4597 result = OCCreateResource(&platformResource,
4598 OC_RSRVD_RESOURCE_TYPE_PLATFORM,
4599 OC_RSRVD_INTERFACE_DEFAULT,
4600 OC_RSRVD_PLATFORM_URI,
4604 if(result == OC_STACK_OK)
4606 result = BindResourceInterfaceToResource((OCResource *)platformResource,
4607 OC_RSRVD_INTERFACE_READ);
4614 void insertResource(OCResource *resource)
4618 headResource = resource;
4619 tailResource = resource;
4623 tailResource->next = resource;
4624 tailResource = resource;
4626 resource->next = NULL;
4629 OCResource *findResource(OCResource *resource)
4631 OCResource *pointer = headResource;
4635 if (pointer == resource)
4639 pointer = pointer->next;
4644 void deleteAllResources()
4646 OCResource *pointer = headResource;
4647 OCResource *temp = NULL;
4651 temp = pointer->next;
4652 #ifdef WITH_PRESENCE
4653 if (pointer != (OCResource *) presenceResource.handle)
4655 #endif // WITH_PRESENCE
4656 deleteResource(pointer);
4657 #ifdef WITH_PRESENCE
4659 #endif // WITH_PRESENCE
4662 memset(&platformResource, 0, sizeof(platformResource));
4663 memset(&deviceResource, 0, sizeof(deviceResource));
4665 memset(&brokerResource, 0, sizeof(brokerResource));
4668 SRMDeInitSecureResources();
4670 #ifdef WITH_PRESENCE
4671 // Ensure that the last resource to be deleted is the presence resource. This allows for all
4672 // presence notification attributed to their deletion to be processed.
4673 deleteResource((OCResource *) presenceResource.handle);
4674 memset(&presenceResource, 0, sizeof(presenceResource));
4675 #endif // WITH_PRESENCE
4678 OCStackResult deleteResource(OCResource *resource)
4680 OCResource *prev = NULL;
4681 OCResource *temp = NULL;
4684 OIC_LOG(DEBUG,TAG,"resource is NULL");
4685 return OC_STACK_INVALID_PARAM;
4688 OIC_LOG_V (INFO, TAG, "Deleting resource %s", resource->uri);
4690 temp = headResource;
4693 if (temp == resource)
4695 // Invalidate all Resource Properties.
4696 resource->resourceProperties = (OCResourceProperty) 0;
4697 #ifdef WITH_PRESENCE
4698 if(resource != (OCResource *) presenceResource.handle)
4700 #endif // WITH_PRESENCE
4701 OCNotifyAllObservers((OCResourceHandle)resource, OC_HIGH_QOS);
4702 #ifdef WITH_PRESENCE
4705 if(presenceResource.handle)
4707 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
4708 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_DELETE);
4711 // Delete resource's all observers
4712 DeleteObserverUsingResource(resource);
4714 // Only resource in list.
4715 if (temp == headResource && temp == tailResource)
4717 headResource = NULL;
4718 tailResource = NULL;
4721 else if (temp == headResource)
4723 headResource = temp->next;
4726 else if (temp == tailResource && prev)
4728 tailResource = prev;
4729 tailResource->next = NULL;
4733 prev->next = temp->next;
4736 deleteResourceElements(temp);
4747 return OC_STACK_ERROR;
4750 void deleteResourceElements(OCResource *resource)
4759 OICFree(resource->uri);
4760 resource->uri = NULL;
4762 if (resource->rsrcType)
4764 deleteResourceType(resource->rsrcType);
4765 resource->rsrcType = NULL;
4767 if (resource->rsrcInterface)
4769 deleteResourceInterface(resource->rsrcInterface);
4770 resource->rsrcInterface = NULL;
4772 if (resource->rsrcChildResourcesHead)
4774 unbindChildResources(resource->rsrcChildResourcesHead);
4775 resource->rsrcChildResourcesHead = NULL;
4777 if (resource->rsrcAttributes)
4779 OCDeleteResourceAttributes(resource->rsrcAttributes);
4780 resource->rsrcAttributes = NULL;
4783 resource->entityHandler = NULL;
4787 void deleteResourceType(OCResourceType *resourceType)
4789 OCResourceType *next = NULL;
4791 for (OCResourceType *pointer = resourceType; pointer; pointer = next)
4793 next = pointer->next ? pointer->next : NULL;
4794 if (pointer->resourcetypename)
4796 OICFree(pointer->resourcetypename);
4797 pointer->resourcetypename = NULL;
4803 void deleteResourceInterface(OCResourceInterface *resourceInterface)
4805 OCResourceInterface *next = NULL;
4806 for (OCResourceInterface *pointer = resourceInterface; pointer; pointer = next)
4808 next = pointer->next ? pointer->next : NULL;
4811 OICFree(pointer->name);
4812 pointer->name = NULL;
4818 void unbindChildResources(OCChildResource *head)
4820 OCChildResource *next = NULL;
4821 for (OCChildResource *current = head; current; current = next)
4823 next = current->next;
4828 void OCDeleteResourceAttributes(OCAttribute *rsrcAttributes)
4830 OCAttribute *next = NULL;
4831 for (OCAttribute *pointer = rsrcAttributes; pointer; pointer = next)
4833 next = pointer->next ? pointer->next : NULL;
4834 if (pointer->attrName && 0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, pointer->attrName))
4836 OCFreeOCStringLL((OCStringLL *)pointer->attrValue);
4837 pointer->attrValue = NULL;
4839 else if (pointer->attrValue)
4841 OICFree(pointer->attrValue);
4842 pointer->attrValue = NULL;
4844 if (pointer->attrName)
4846 OICFree(pointer->attrName);
4847 pointer->attrName = NULL;
4853 void insertResourceType(OCResource *resource, OCResourceType *resourceType)
4855 OCResourceType *pointer = NULL;
4856 OCResourceType *previous = NULL;
4857 if (!resource || !resourceType)
4861 // resource type list is empty.
4862 else if (!resource->rsrcType)
4864 resource->rsrcType = resourceType;
4868 pointer = resource->rsrcType;
4872 if (!strcmp(resourceType->resourcetypename, pointer->resourcetypename))
4874 OIC_LOG_V(INFO, TAG, "Type %s already exists", resourceType->resourcetypename);
4875 OICFree(resourceType->resourcetypename);
4876 OICFree(resourceType);
4880 pointer = pointer->next;
4885 previous->next = resourceType;
4888 resourceType->next = NULL;
4890 OIC_LOG_V(INFO, TAG, "Added type %s to %s", resourceType->resourcetypename, resource->uri);
4893 OCResourceType *findResourceTypeAtIndex(OCResourceHandle handle, uint8_t index)
4895 OCResource *resource = NULL;
4896 OCResourceType *pointer = NULL;
4898 // Find the specified resource
4899 resource = findResource((OCResource *) handle);
4905 // Make sure a resource has a resourcetype
4906 if (!resource->rsrcType)
4911 // Iterate through the list
4912 pointer = resource->rsrcType;
4913 for(uint8_t i = 0; i< index && pointer; ++i)
4915 pointer = pointer->next;
4920 OCResourceType *findResourceType(OCResourceType * resourceTypeList, const char * resourceTypeName)
4922 if(resourceTypeList && resourceTypeName)
4924 OCResourceType * rtPointer = resourceTypeList;
4925 while(resourceTypeName && rtPointer)
4927 OIC_LOG_V(DEBUG, TAG, "current resourceType : %s", rtPointer->resourcetypename);
4928 if(rtPointer->resourcetypename &&
4929 strcmp(resourceTypeName, (const char *)
4930 (rtPointer->resourcetypename)) == 0)
4934 rtPointer = rtPointer->next;
4942 * Insert a new interface into interface linked list only if not already present.
4943 * If alredy present, 2nd arg is free'd.
4944 * Default interface will always be first if present.
4946 void insertResourceInterface(OCResource *resource, OCResourceInterface *newInterface)
4948 OCResourceInterface *pointer = NULL;
4949 OCResourceInterface *previous = NULL;
4951 newInterface->next = NULL;
4953 OCResourceInterface **firstInterface = &(resource->rsrcInterface);
4955 if (!*firstInterface)
4957 // If first interface is not oic.if.baseline, by default add it as first interface type.
4958 if (0 == strcmp(newInterface->name, OC_RSRVD_INTERFACE_DEFAULT))
4960 *firstInterface = newInterface;
4964 OCStackResult result = BindResourceInterfaceToResource(resource,
4965 OC_RSRVD_INTERFACE_DEFAULT);
4966 if (result != OC_STACK_OK)
4968 OICFree(newInterface->name);
4969 OICFree(newInterface);
4972 if (*firstInterface)
4974 (*firstInterface)->next = newInterface;
4978 // If once add oic.if.baseline, later too below code take care of freeing memory.
4979 else if (strcmp(newInterface->name, OC_RSRVD_INTERFACE_DEFAULT) == 0)
4981 if (strcmp((*firstInterface)->name, OC_RSRVD_INTERFACE_DEFAULT) == 0)
4983 OICFree(newInterface->name);
4984 OICFree(newInterface);
4987 // This code will not hit anymore, keeping
4990 newInterface->next = *firstInterface;
4991 *firstInterface = newInterface;
4996 pointer = *firstInterface;
4999 if (strcmp(newInterface->name, pointer->name) == 0)
5001 OICFree(newInterface->name);
5002 OICFree(newInterface);
5006 pointer = pointer->next;
5011 previous->next = newInterface;
5016 OCResourceInterface *findResourceInterfaceAtIndex(OCResourceHandle handle,
5019 OCResource *resource = NULL;
5020 OCResourceInterface *pointer = NULL;
5022 // Find the specified resource
5023 resource = findResource((OCResource *) handle);
5029 // Make sure a resource has a resourceinterface
5030 if (!resource->rsrcInterface)
5035 // Iterate through the list
5036 pointer = resource->rsrcInterface;
5038 for (uint8_t i = 0; i < index && pointer; ++i)
5040 pointer = pointer->next;
5046 * This function splits the uri using the '?' delimiter.
5047 * "uriWithoutQuery" is the block of characters between the beginning
5048 * till the delimiter or '\0' which ever comes first.
5049 * "query" is whatever is to the right of the delimiter if present.
5050 * No delimiter sets the query to NULL.
5051 * If either are present, they will be malloc'ed into the params 2, 3.
5052 * The first param, *uri is left untouched.
5054 * NOTE: This function does not account for whitespace at the end of the uri NOR
5055 * malformed uri's with '??'. Whitespace at the end will be assumed to be
5056 * part of the query.
5058 OCStackResult getQueryFromUri(const char * uri, char** query, char ** uriWithoutQuery)
5062 return OC_STACK_INVALID_URI;
5064 if(!query || !uriWithoutQuery)
5066 return OC_STACK_INVALID_PARAM;
5070 *uriWithoutQuery = NULL;
5072 size_t uriWithoutQueryLen = 0;
5073 size_t queryLen = 0;
5074 size_t uriLen = strlen(uri);
5076 char *pointerToDelimiter = strstr(uri, "?");
5078 uriWithoutQueryLen = pointerToDelimiter == NULL ? uriLen : (size_t)(pointerToDelimiter - uri);
5079 queryLen = pointerToDelimiter == NULL ? 0 : uriLen - uriWithoutQueryLen - 1;
5081 if (uriWithoutQueryLen)
5083 *uriWithoutQuery = (char *) OICCalloc(uriWithoutQueryLen + 1, 1);
5084 if (!*uriWithoutQuery)
5088 OICStrcpy(*uriWithoutQuery, uriWithoutQueryLen +1, uri);
5092 *query = (char *) OICCalloc(queryLen + 1, 1);
5095 OICFree(*uriWithoutQuery);
5096 *uriWithoutQuery = NULL;
5099 OICStrcpy(*query, queryLen + 1, pointerToDelimiter + 1);
5105 return OC_STACK_NO_MEMORY;
5108 const char* OCGetServerInstanceIDString(void)
5110 static char sidStr[UUID_STRING_SIZE];
5112 if (OC_STACK_OK != GetDoxmDeviceID(&sid))
5114 OIC_LOG(FATAL, TAG, "GetDoxmDeviceID failed!");
5118 if (OCConvertUuidToString(sid.id, sidStr) != RAND_UUID_OK)
5120 OIC_LOG(FATAL, TAG, "Generate UUID String for Server Instance failed!");
5127 CAResult_t OCSelectNetwork(OCTransportAdapter transportType)
5129 OIC_LOG_V(DEBUG, TAG, "OCSelectNetwork [%d]", transportType);
5130 CAResult_t retResult = CA_STATUS_FAILED;
5131 CAResult_t caResult = CA_STATUS_OK;
5133 CATransportAdapter_t connTypes[] = {
5135 CA_ADAPTER_RFCOMM_BTEDR,
5136 CA_ADAPTER_GATT_BTLE,
5139 ,CA_ADAPTER_REMOTE_ACCESS
5146 int numConnTypes = sizeof(connTypes)/sizeof(connTypes[0]);
5148 for(int i = 0; i < numConnTypes; i++)
5150 // If CA status is not initialized, CASelectNetwork() will not be called.
5151 if (caResult != CA_STATUS_NOT_INITIALIZED)
5153 if ((connTypes[i] & transportType) || (OC_DEFAULT_ADAPTER == transportType))
5155 OIC_LOG_V(DEBUG, TAG, "call CASelectNetwork [%d]", connTypes[i]);
5156 caResult = CASelectNetwork(connTypes[i]);
5157 if (caResult == CA_STATUS_OK)
5159 retResult = CA_STATUS_OK;
5164 OIC_LOG_V(DEBUG, TAG, "there is no transport type [%d]", connTypes[i]);
5169 if (retResult != CA_STATUS_OK)
5171 return caResult; // Returns error of appropriate transport that failed fatally.
5177 OCStackResult CAResultToOCResult(CAResult_t caResult)
5183 case CA_STATUS_INVALID_PARAM:
5184 return OC_STACK_INVALID_PARAM;
5185 case CA_ADAPTER_NOT_ENABLED:
5186 return OC_STACK_ADAPTER_NOT_ENABLED;
5187 case CA_SERVER_STARTED_ALREADY:
5189 case CA_SERVER_NOT_STARTED:
5190 return OC_STACK_ERROR;
5191 case CA_DESTINATION_NOT_REACHABLE:
5192 return OC_STACK_COMM_ERROR;
5193 case CA_SOCKET_OPERATION_FAILED:
5194 return OC_STACK_COMM_ERROR;
5195 case CA_SEND_FAILED:
5196 return OC_STACK_COMM_ERROR;
5197 case CA_RECEIVE_FAILED:
5198 return OC_STACK_COMM_ERROR;
5199 case CA_MEMORY_ALLOC_FAILED:
5200 return OC_STACK_NO_MEMORY;
5201 case CA_REQUEST_TIMEOUT:
5202 return OC_STACK_TIMEOUT;
5203 case CA_DESTINATION_DISCONNECTED:
5204 return OC_STACK_COMM_ERROR;
5205 case CA_STATUS_FAILED:
5206 return OC_STACK_ERROR;
5207 case CA_NOT_SUPPORTED:
5208 return OC_STACK_NOTIMPL;
5210 return OC_STACK_ERROR;
5214 bool OCResultToSuccess(OCStackResult ocResult)
5219 case OC_STACK_RESOURCE_CREATED:
5220 case OC_STACK_RESOURCE_DELETED:
5221 case OC_STACK_CONTINUE:
5222 case OC_STACK_RESOURCE_CHANGED:
5223 case OC_STACK_SLOW_RESOURCE:
5231 OCStackResult OCSetProxyURI(const char *uri)
5233 return CAResultToOCResult(CASetProxyUri(uri));
5237 #if defined(RD_CLIENT) || defined(RD_SERVER)
5238 OCStackResult OCBindResourceInsToResource(OCResourceHandle handle, int64_t ins)
5240 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
5242 OCResource *resource = NULL;
5244 resource = findResource((OCResource *) handle);
5247 OIC_LOG(ERROR, TAG, "Resource not found");
5248 return OC_STACK_ERROR;
5251 resource->ins = ins;
5257 OCStackResult OCUpdateResourceInsWithResponse(const char *requestUri,
5258 const OCClientResponse *response)
5260 // Validate input parameters
5261 VERIFY_NON_NULL(requestUri, ERROR, OC_STACK_INVALID_PARAM);
5262 VERIFY_NON_NULL(response, ERROR, OC_STACK_INVALID_PARAM);
5264 char *targetUri = (char *) OICMalloc(strlen(requestUri) + 1);
5267 return OC_STACK_NO_MEMORY;
5269 strncpy(targetUri, requestUri, strlen(requestUri) + 1);
5271 if (response->result == OC_STACK_RESOURCE_CHANGED) // publish message
5273 OIC_LOG(DEBUG, TAG, "update the ins of published resource");
5275 char rdPubUri[MAX_URI_LENGTH] = { 0 };
5276 snprintf(rdPubUri, MAX_URI_LENGTH, "%s?rt=%s", OC_RSRVD_RD_URI,
5277 OC_RSRVD_RESOURCE_TYPE_RDPUBLISH);
5279 if (strcmp(rdPubUri, targetUri) == 0)
5281 // Update resource unique id in stack.
5284 if (response->payload)
5286 OCRepPayload *rdPayload = (OCRepPayload *) response->payload;
5287 OCRepPayload **links = NULL;
5288 size_t dimensions[MAX_REP_ARRAY_DEPTH] = { 0 };
5289 if (OCRepPayloadGetPropObjectArray(rdPayload, OC_RSRVD_LINKS,
5290 &links, dimensions))
5293 for (; i < dimensions[0]; i++)
5296 if (OCRepPayloadGetPropString(links[i], OC_RSRVD_HREF, &uri))
5298 OCResourceHandle handle = OCGetResourceHandleAtUri(uri);
5300 if (OCRepPayloadGetPropInt(links[i], OC_RSRVD_INS, &ins))
5302 OCBindResourceInsToResource(handle, ins);
5311 size_t count = calcDimTotal(dimensions);
5312 for (size_t k = 0; k < count; k++)
5314 OCRepPayloadDestroy(links[k]);
5322 else if (response->result == OC_STACK_RESOURCE_DELETED) // delete message
5324 OIC_LOG(DEBUG, TAG, "update the ins of deleted resource with 0");
5326 uint8_t numResources = 0;
5327 OCGetNumberOfResources(&numResources);
5329 char *ins = strstr(targetUri, OC_RSRVD_INS);
5332 for (uint8_t i = 0; i < numResources; i++)
5334 OCResourceHandle resHandle = OCGetResourceHandle(i);
5337 OCBindResourceInsToResource(resHandle, 0);
5343 const char *token = "&";
5344 char *iterTokenPtr = NULL;
5345 char *start = strtok_r(targetUri, token, &iterTokenPtr);
5347 while (start != NULL)
5349 char *query = start;
5350 query = strstr(query, OC_RSRVD_INS);
5353 int64_t queryIns = atoi(query + 4);
5354 for (uint8_t i = 0; i < numResources; i++)
5356 OCResourceHandle resHandle = OCGetResourceHandle(i);
5360 OCGetResourceIns(resHandle, &resIns);
5361 if (queryIns && queryIns == resIns)
5363 OCBindResourceInsToResource(resHandle, 0);
5369 start = strtok_r(NULL, token, &iterTokenPtr);
5378 OCResourceHandle OCGetResourceHandleAtUri(const char *uri)
5382 OIC_LOG(ERROR, TAG, "Resource uri is NULL");
5386 OCResource *pointer = headResource;
5390 if (strncmp(uri, pointer->uri, MAX_URI_LENGTH) == 0)
5392 OIC_LOG_V(DEBUG, TAG, "Found Resource %s", uri);
5395 pointer = pointer->next;
5400 OCStackResult OCGetResourceIns(OCResourceHandle handle, int64_t *ins)
5402 OCResource *resource = NULL;
5404 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
5405 VERIFY_NON_NULL(ins, ERROR, OC_STACK_INVALID_PARAM);
5407 resource = findResource((OCResource *) handle);
5410 *ins = resource->ins;
5413 return OC_STACK_ERROR;
5417 OCStackResult OCSetHeaderOption(OCHeaderOption* ocHdrOpt, size_t* numOptions, uint16_t optionID,
5418 const void* optionData, size_t optionDataLength)
5422 OIC_LOG (INFO, TAG, "Header options are NULL");
5423 return OC_STACK_INVALID_PARAM;
5428 OIC_LOG (INFO, TAG, "optionData are NULL");
5429 return OC_STACK_INVALID_PARAM;
5434 OIC_LOG (INFO, TAG, "numOptions is NULL");
5435 return OC_STACK_INVALID_PARAM;
5438 if (*numOptions >= MAX_HEADER_OPTIONS)
5440 OIC_LOG (INFO, TAG, "Exceeding MAX_HEADER_OPTIONS");
5441 return OC_STACK_NO_MEMORY;
5444 ocHdrOpt += *numOptions;
5445 ocHdrOpt->protocolID = OC_COAP_ID;
5446 ocHdrOpt->optionID = optionID;
5447 ocHdrOpt->optionLength =
5448 optionDataLength < MAX_HEADER_OPTION_DATA_LENGTH ?
5449 optionDataLength : MAX_HEADER_OPTION_DATA_LENGTH;
5450 memcpy(ocHdrOpt->optionData, (const void*) optionData, ocHdrOpt->optionLength);
5456 OCStackResult OCGetHeaderOption(OCHeaderOption* ocHdrOpt, size_t numOptions, uint16_t optionID,
5457 void* optionData, size_t optionDataLength, uint16_t* receivedDataLength)
5459 if (!ocHdrOpt || !numOptions)
5461 OIC_LOG (INFO, TAG, "No options present");
5467 OIC_LOG (INFO, TAG, "optionData are NULL");
5468 return OC_STACK_INVALID_PARAM;
5471 if (!receivedDataLength)
5473 OIC_LOG (INFO, TAG, "receivedDataLength is NULL");
5474 return OC_STACK_INVALID_PARAM;
5477 for (uint8_t i = 0; i < numOptions; i++)
5479 if (ocHdrOpt[i].optionID == optionID)
5481 if (optionDataLength >= ocHdrOpt->optionLength)
5483 memcpy(optionData, ocHdrOpt->optionData, ocHdrOpt->optionLength);
5484 *receivedDataLength = ocHdrOpt->optionLength;
5489 OIC_LOG (ERROR, TAG, "optionDataLength is less than the length of received data");
5490 return OC_STACK_ERROR;
5497 void OCDefaultAdapterStateChangedHandler(CATransportAdapter_t adapter, bool enabled)
5499 OIC_LOG(DEBUG, TAG, "OCDefaultAdapterStateChangedHandler");
5505 void OCDefaultConnectionStateChangedHandler(const CAEndpoint_t *info, bool isConnected)
5507 OIC_LOG(DEBUG, TAG, "OCDefaultConnectionStateChangedHandler");
5510 * If the client observes one or more resources over a reliable connection,
5511 * then the CoAP server (or intermediary in the role of the CoAP server)
5512 * MUST remove all entries associated with the client endpoint from the lists
5513 * of observers when the connection is either closed or times out.
5517 OCDevAddr devAddr = { OC_DEFAULT_ADAPTER };
5518 CopyEndpointToDevAddr(info, &devAddr);
5520 // remove observer list with remote device address.
5521 DeleteObserverUsingDevAddr(&devAddr);
5525 OCStackResult OCGetDeviceId(OCUUIdentity *deviceId)
5528 OCStackResult ret = OC_STACK_ERROR;
5530 ret = GetDoxmDeviceID(&oicUuid);
5531 if (OC_STACK_OK == ret)
5533 memcpy(deviceId, &oicUuid, UUID_IDENTITY_SIZE);
5537 OIC_LOG(ERROR, TAG, "Device ID Get error");
5542 OCStackResult OCSetDeviceId(const OCUUIdentity *deviceId)
5545 OCStackResult ret = OC_STACK_ERROR;
5547 memcpy(&oicUuid, deviceId, UUID_LENGTH);
5548 for (int i = 0; i < UUID_LENGTH; i++)
5550 OIC_LOG_V(INFO, TAG, "Set Device Id %x", oicUuid.id[i]);
5552 ret = SetDoxmDeviceID(&oicUuid);
5556 OCStackResult OCGetDeviceOwnedState(bool *isOwned)
5558 bool isDeviceOwned = true;
5559 OCStackResult ret = OC_STACK_ERROR;
5561 ret = GetDoxmIsOwned(&isDeviceOwned);
5562 if (OC_STACK_OK == ret)
5564 *isOwned = isDeviceOwned;
5568 OIC_LOG(ERROR, TAG, "Device Owned State Get error");
5573 OCStackResult OCGetDeviceOperationalState(bool* isOp)
5577 *isOp = GetPstatIsop();
5581 return OC_STACK_ERROR;
5584 void OCClearCallBackList()
5586 DeleteClientCBList();
5589 void OCClearObserverlist()
5591 DeleteObserverList();
5594 int OCEncrypt(const unsigned char *pt, size_t pt_len,
5595 unsigned char **ct, size_t *ct_len)
5597 #ifndef __SECURE_PSI__
5598 OIC_LOG_V(DEBUG, TAG, "Not Supported : %s", __func__);
5601 OIC_LOG_V(DEBUG, TAG, "%s", __func__);
5603 return psiEncrypt(pt, pt_len, ct, ct_len);
5604 #endif // __SECURE_PSI__
5607 int OCDecrypt(const unsigned char *ct, size_t ct_len,
5608 unsigned char **pt, size_t *pt_len)
5610 #ifndef __SECURE_PSI__
5611 OIC_LOG_V(DEBUG, TAG, "Not Supported : %s", __func__);
5614 OIC_LOG_V(DEBUG, TAG, "%s", __func__);
5616 return psiDecrypt(ct, ct_len, pt, pt_len);
5617 #endif // __SECURE_PSI__
5620 OCStackResult OCSetKey(const unsigned char* key)
5622 #ifndef __SECURE_PSI__
5623 OIC_LOG_V(DEBUG, TAG, "Not Supported : %s", __func__);
5626 OIC_LOG_V(DEBUG, TAG, "%s", __func__);
5628 return psiSetKey(key);
5629 #endif // __SECURE_PSI__
5632 OCStackResult OCGetKey(unsigned char* key)
5634 #ifndef __SECURE_PSI__
5635 OIC_LOG_V(DEBUG, TAG, "Not Supported : %s", __func__);
5638 OIC_LOG_V(DEBUG, TAG, "%s", __func__);
5640 return psiGetKey(key);
5641 #endif // __SECURE_PSI__
5644 OCStackResult OCSetSecurePSI(const unsigned char *key, const OCPersistentStorage *psPlain,
5645 const OCPersistentStorage *psEnc, const OCPersistentStorage *psRescue)
5647 #ifndef __SECURE_PSI__
5648 OIC_LOG_V(DEBUG, TAG, "Not Supported : %s", __func__);
5651 OIC_LOG_V(DEBUG, TAG, "%s", __func__);
5653 return setSecurePSI(key, psPlain, psEnc, psRescue);
5654 #endif // __SECURE_PSI__
5657 #if defined(__WITH_DTLS__) || defined (__WITH_TLS__)
5658 static void OtmEventHandler(const char *addr, uint16_t port, const char *uuid, int event)
5660 if (g_otmEventHandler.cb)
5662 g_otmEventHandler.cb(g_otmEventHandler.ctx, addr, port, uuid, event);
5667 * It is already declared in srmutility.h.
5668 * We can't include the header file, because of "redefined VERIFY_NON_NULL"
5670 typedef void (*OicSecOtmEventHandler_t)(const char* addr, uint16_t port,
5671 const char* uuid, int event);
5672 void SetOtmEventHandler(OicSecOtmEventHandler_t otmEventHandler);
5675 OCStackResult OCSetOtmEventHandler(void *ctx, OCOtmEventHandler cb)
5677 #if defined(__WITH_DTLS__) || defined (__WITH_TLS__)
5678 OIC_LOG_V(DEBUG, TAG, "%s", __func__);
5680 g_otmEventHandler.cb = cb;
5681 g_otmEventHandler.ctx = ctx;
5683 if (g_otmEventHandler.cb)
5685 OIC_LOG(DEBUG, TAG, "SET OCOtmEventHandler");
5686 SetOtmEventHandler(OtmEventHandler);
5690 OIC_LOG(DEBUG, TAG, "UNSET OCOtmEventHandler");
5691 SetOtmEventHandler(NULL);
5694 OIC_LOG_V(DEBUG, TAG, "Not Supported : %s", __func__);