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"
53 #include "ocserverrequest.h"
54 #include "secureresourcemanager.h"
55 #include "psinterface.h"
56 #include "doxmresource.h"
58 #include "cainterface.h"
59 #include "ocpayload.h"
60 #include "ocpayloadcbor.h"
61 #include "cautilinterface.h"
64 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
65 #include "routingutility.h"
66 #ifdef ROUTING_GATEWAY
67 #include "routingmanager.h"
72 #include "oickeepalive.h"
75 //#ifdef DIRECT_PAIRING
76 #include "directpairing.h"
79 #ifdef HAVE_ARDUINO_TIME_H
82 #ifdef HAVE_SYS_TIME_H
85 #include <coap/coap.h>
87 #ifdef HAVE_ARPA_INET_H
88 #include <arpa/inet.h>
92 #define UINT32_MAX (0xFFFFFFFFUL)
95 //-----------------------------------------------------------------------------
97 //-----------------------------------------------------------------------------
100 OC_STACK_UNINITIALIZED = 0,
101 OC_STACK_INITIALIZED,
102 OC_STACK_UNINIT_IN_PROGRESS
108 OC_PRESENCE_UNINITIALIZED = 0,
109 OC_PRESENCE_INITIALIZED
113 //-----------------------------------------------------------------------------
115 //-----------------------------------------------------------------------------
116 static OCStackState stackState = OC_STACK_UNINITIALIZED;
118 OCResource *headResource = NULL;
119 static OCResource *tailResource = NULL;
120 static OCResourceHandle platformResource = {0};
121 static OCResourceHandle deviceResource = {0};
123 static OCResourceHandle brokerResource = {0};
127 static OCPresenceState presenceState = OC_PRESENCE_UNINITIALIZED;
128 static PresenceResource presenceResource = {0};
129 static uint8_t PresenceTimeOutSize = 0;
130 static uint32_t PresenceTimeOut[] = {50, 75, 85, 95, 100};
133 static OCMode myStackMode;
135 //TODO: revisit this design
136 static bool gRASetInfo = false;
138 OCDeviceEntityHandler defaultDeviceHandler;
139 void* defaultDeviceHandlerCallbackParameter = NULL;
140 static const char COAP_TCP_SCHEME[] = "coap+tcp:";
141 static const char COAPS_TCP_SCHEME[] = "coaps+tcp:";
142 static const char CORESPEC[] = "core";
144 CAAdapterStateChangedCB g_adapterHandler = NULL;
145 CAConnectionStateChangedCB g_connectionHandler = NULL;
147 //-----------------------------------------------------------------------------
149 //-----------------------------------------------------------------------------
150 #define TAG "OIC_RI_STACK"
151 #define VERIFY_SUCCESS(op, successCode) { if ((op) != (successCode)) \
152 {OIC_LOG_V(FATAL, TAG, "%s failed!!", #op); goto exit;} }
153 #define VERIFY_NON_NULL(arg, logLevel, retVal) { if (!(arg)) { OIC_LOG((logLevel), \
154 TAG, #arg " is NULL"); return (retVal); } }
155 #define VERIFY_NON_NULL_NR(arg, logLevel) { if (!(arg)) { OIC_LOG((logLevel), \
156 TAG, #arg " is NULL"); return; } }
157 #define VERIFY_NON_NULL_V(arg) { if (!arg) {OIC_LOG(FATAL, TAG, #arg " is NULL");\
160 //TODO: we should allow the server to define this
161 #define MAX_OBSERVE_AGE (0x2FFFFUL)
163 #define MILLISECONDS_PER_SECOND (1000)
165 //-----------------------------------------------------------------------------
166 // Private internal function prototypes
167 //-----------------------------------------------------------------------------
170 * Generate handle of OCDoResource invocation for callback management.
172 * @return Generated OCDoResource handle.
174 static OCDoHandle GenerateInvocationHandle();
177 * Initialize resource data structures, variables, etc.
179 * @return ::OC_STACK_OK on success, some other value upon failure.
181 static OCStackResult initResources();
184 * Add a resource to the end of the linked list of resources.
186 * @param resource Resource to be added
188 static void insertResource(OCResource *resource);
191 * Find a resource in the linked list of resources.
193 * @param resource Resource to be found.
194 * @return Pointer to resource that was found in the linked list or NULL if the resource was not
197 static OCResource *findResource(OCResource *resource);
200 * Insert a resource type into a resource's resource type linked list.
201 * If resource type already exists, it will not be inserted and the
202 * resourceType will be free'd.
203 * resourceType->next should be null to avoid memory leaks.
204 * Function returns silently for null args.
206 * @param resource Resource where resource type is to be inserted.
207 * @param resourceType Resource type to be inserted.
209 static void insertResourceType(OCResource *resource,
210 OCResourceType *resourceType);
213 * Get a resource type at the specified index within a resource.
215 * @param handle Handle of resource.
216 * @param index Index of resource type.
218 * @return Pointer to resource type if found, NULL otherwise.
220 static OCResourceType *findResourceTypeAtIndex(OCResourceHandle handle,
224 * Insert a resource interface into a resource's resource interface linked list.
225 * If resource interface already exists, it will not be inserted and the
226 * resourceInterface will be free'd.
227 * resourceInterface->next should be null to avoid memory leaks.
229 * @param resource Resource where resource interface is to be inserted.
230 * @param resourceInterface Resource interface to be inserted.
232 static void insertResourceInterface(OCResource *resource,
233 OCResourceInterface *resourceInterface);
236 * Get a resource interface at the specified index within a resource.
238 * @param handle Handle of resource.
239 * @param index Index of resource interface.
241 * @return Pointer to resource interface if found, NULL otherwise.
243 static OCResourceInterface *findResourceInterfaceAtIndex(
244 OCResourceHandle handle, uint8_t index);
247 * Delete all of the dynamically allocated elements that were created for the resource type.
249 * @param resourceType Specified resource type.
251 static void deleteResourceType(OCResourceType *resourceType);
254 * Delete all of the dynamically allocated elements that were created for the resource interface.
256 * @param resourceInterface Specified resource interface.
258 static void deleteResourceInterface(OCResourceInterface *resourceInterface);
261 * Delete all of the dynamically allocated elements that were created for the resource.
263 * @param resource Specified resource.
265 static void deleteResourceElements(OCResource *resource);
268 * Delete resource specified by handle. Deletes resource and all resourcetype and resourceinterface
271 * @param handle Handle of resource to be deleted.
273 * @return ::OC_STACK_OK on success, some other value upon failure.
275 static OCStackResult deleteResource(OCResource *resource);
278 * Delete all of the resources in the resource list.
280 static void deleteAllResources();
283 * Increment resource sequence number. Handles rollover.
285 * @param resPtr Pointer to resource.
287 static void incrementSequenceNumber(OCResource * resPtr);
290 * Attempts to initialize every network interface that the CA Layer might have compiled in.
292 * Note: At least one interface must succeed to initialize. If all calls to @ref CASelectNetwork
293 * return something other than @ref CA_STATUS_OK, then this function fails.
295 * @return ::CA_STATUS_OK on success, some other value upon failure.
297 static CAResult_t OCSelectNetwork();
300 * Convert CAResponseResult_t to OCStackResult.
302 * @param caCode CAResponseResult_t code.
303 * @return ::OC_STACK_OK on success, some other value upon failure.
305 static OCStackResult CAResponseToOCStackResult(CAResponseResult_t caCode);
308 * Convert OCTransportFlags_t to CATransportModifiers_t.
310 * @param ocConType OCTransportFlags_t input.
311 * @return CATransportFlags
313 static CATransportFlags_t OCToCATransportFlags(OCTransportFlags ocConType);
316 * Convert CATransportFlags_t to OCTransportModifiers_t.
318 * @param caConType CATransportFlags_t input.
319 * @return OCTransportFlags
321 static OCTransportFlags CAToOCTransportFlags(CATransportFlags_t caConType);
324 * Handle response from presence request.
326 * @param endPoint CA remote endpoint.
327 * @param responseInfo CA response info.
328 * @return ::OC_STACK_OK on success, some other value upon failure.
330 static OCStackResult HandlePresenceResponse(const CAEndpoint_t *endPoint,
331 const CAResponseInfo_t *responseInfo);
334 * This function will be called back by CA layer when a response is received.
336 * @param endPoint CA remote endpoint.
337 * @param responseInfo CA response info.
339 static void HandleCAResponses(const CAEndpoint_t* endPoint,
340 const CAResponseInfo_t* responseInfo);
343 * This function will be called back by CA layer when a request is received.
345 * @param endPoint CA remote endpoint.
346 * @param requestInfo CA request info.
348 static void HandleCARequests(const CAEndpoint_t* endPoint,
349 const CARequestInfo_t* requestInfo);
352 * Extract query from a URI.
354 * @param uri Full URI with query.
355 * @param query Pointer to string that will contain query.
356 * @param newURI Pointer to string that will contain URI.
357 * @return ::OC_STACK_OK on success, some other value upon failure.
359 static OCStackResult getQueryFromUri(const char * uri, char** resourceType, char ** newURI);
362 * Finds a resource type in an OCResourceType link-list.
364 * @param resourceTypeList The link-list to be searched through.
365 * @param resourceTypeName The key to search for.
367 * @return Resource type that matches the key (ie. resourceTypeName) or
368 * NULL if there is either an invalid parameter or this function was unable to find the key.
370 static OCResourceType *findResourceType(OCResourceType * resourceTypeList,
371 const char * resourceTypeName);
374 * Reset presence TTL for a ClientCB struct. ttlLevel will be set to 0.
375 * TTL will be set to maxAge.
377 * @param cbNode Callback Node for which presence ttl is to be reset.
378 * @param maxAge New value of ttl in seconds.
380 * @return ::OC_STACK_OK on success, some other value upon failure.
382 static OCStackResult ResetPresenceTTL(ClientCB *cbNode, uint32_t maxAgeSeconds);
385 * Ensure the accept header option is set appropriatly before sending the requests and routing
386 * header option is updated with destination.
388 * @param object CA remote endpoint.
389 * @param requestInfo CA request info.
391 * @return ::OC_STACK_OK on success, some other value upon failure.
393 static OCStackResult OCSendRequest(const CAEndpoint_t *object, CARequestInfo_t *requestInfo);
396 * default adapter state change callback method
398 * @param adapter CA network adapter type.
399 * @param enabled current adapter state.
401 static void OCDefaultAdapterStateChangedHandler(CATransportAdapter_t adapter, bool enabled);
404 * default connection state change callback method
406 * @param info CAEndpoint which has address, port and etc.
407 * @param isConnected current connection state.
409 static void OCDefaultConnectionStateChangedHandler(const CAEndpoint_t *info, bool isConnected);
412 * Register network monitoring callback.
413 * Network status changes are delivered these callback.
414 * @param adapterHandler Adapter state monitoring callback.
415 * @param connectionHandler Connection state monitoring callback.
417 static void OCSetNetworkMonitorHandler(CAAdapterStateChangedCB adapterHandler,
418 CAConnectionStateChangedCB connectionHandler);
420 //-----------------------------------------------------------------------------
421 // Internal functions
422 //-----------------------------------------------------------------------------
424 bool checkProxyUri(OCHeaderOption *options, uint8_t numOptions)
426 if (!options || 0 == numOptions)
428 OIC_LOG (INFO, TAG, "No options present");
432 for (uint8_t i = 0; i < numOptions; i++)
434 if (options[i].protocolID == OC_COAP_ID && options[i].optionID == OC_RSRVD_PROXY_OPTION_ID)
436 OIC_LOG(DEBUG, TAG, "Proxy URI is present");
443 uint32_t GetTicks(uint32_t milliSeconds)
448 // Guard against overflow of uint32_t
449 if (milliSeconds <= ((UINT32_MAX - (uint32_t)now) * MILLISECONDS_PER_SECOND) /
450 COAP_TICKS_PER_SECOND)
452 return now + (milliSeconds * COAP_TICKS_PER_SECOND)/MILLISECONDS_PER_SECOND;
460 void CopyEndpointToDevAddr(const CAEndpoint_t *in, OCDevAddr *out)
462 VERIFY_NON_NULL_NR(in, FATAL);
463 VERIFY_NON_NULL_NR(out, FATAL);
465 out->adapter = (OCTransportAdapter)in->adapter;
466 out->flags = CAToOCTransportFlags(in->flags);
467 OICStrcpy(out->addr, sizeof(out->addr), in->addr);
468 out->port = in->port;
469 out->ifindex = in->ifindex;
470 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
471 /* This assert is to prevent accidental mismatch between address size macros defined in
472 * RI and CA and cause crash here. */
473 OC_STATIC_ASSERT(MAX_ADDR_STR_SIZE_CA == MAX_ADDR_STR_SIZE,
474 "Address size mismatch between RI and CA");
475 memcpy(out->routeData, in->routeData, sizeof(in->routeData));
479 void CopyDevAddrToEndpoint(const OCDevAddr *in, CAEndpoint_t *out)
481 VERIFY_NON_NULL_NR(in, FATAL);
482 VERIFY_NON_NULL_NR(out, FATAL);
484 out->adapter = (CATransportAdapter_t)in->adapter;
485 out->flags = OCToCATransportFlags(in->flags);
486 OICStrcpy(out->addr, sizeof(out->addr), in->addr);
487 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
488 /* This assert is to prevent accidental mismatch between address size macros defined in
489 * RI and CA and cause crash here. */
490 OC_STATIC_ASSERT(MAX_ADDR_STR_SIZE_CA == MAX_ADDR_STR_SIZE,
491 "Address size mismatch between RI and CA");
492 memcpy(out->routeData, in->routeData, sizeof(in->routeData));
494 out->port = in->port;
495 out->ifindex = in->ifindex;
498 void FixUpClientResponse(OCClientResponse *cr)
500 VERIFY_NON_NULL_NR(cr, FATAL);
502 cr->addr = &cr->devAddr;
503 cr->connType = (OCConnectivityType)
504 ((cr->devAddr.adapter << CT_ADAPTER_SHIFT) | (cr->devAddr.flags & CT_MASK_FLAGS));
507 static OCStackResult OCSendRequest(const CAEndpoint_t *object, CARequestInfo_t *requestInfo)
509 VERIFY_NON_NULL(object, FATAL, OC_STACK_INVALID_PARAM);
510 VERIFY_NON_NULL(requestInfo, FATAL, OC_STACK_INVALID_PARAM);
512 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
513 OCStackResult rmResult = RMAddInfo(object->routeData, requestInfo, true, NULL);
514 if (OC_STACK_OK != rmResult)
516 OIC_LOG(ERROR, TAG, "Add destination option failed");
521 // OC stack prefer CBOR encoded payloads.
522 requestInfo->info.acceptFormat = CA_FORMAT_APPLICATION_CBOR;
523 CAResult_t result = CASendRequest(object, requestInfo);
524 if(CA_STATUS_OK != result)
526 OIC_LOG_V(ERROR, TAG, "CASendRequest failed with CA error %u", result);
527 return CAResultToOCResult(result);
531 //-----------------------------------------------------------------------------
532 // Internal API function
533 //-----------------------------------------------------------------------------
535 // This internal function is called to update the stack with the status of
536 // observers and communication failures
537 OCStackResult OCStackFeedBack(CAToken_t token, uint8_t tokenLength, uint8_t status)
539 OCStackResult result = OC_STACK_ERROR;
540 ResourceObserver * observer = NULL;
541 OCEntityHandlerRequest ehRequest = {0};
545 case OC_OBSERVER_NOT_INTERESTED:
546 OIC_LOG(DEBUG, TAG, "observer not interested in our notifications");
547 observer = GetObserverUsingToken (token, tokenLength);
550 result = FormOCEntityHandlerRequest(&ehRequest,
551 (OCRequestHandle)NULL,
554 (OCResourceHandle)NULL,
555 NULL, PAYLOAD_TYPE_REPRESENTATION,
557 OC_OBSERVE_DEREGISTER,
560 if(result != OC_STACK_OK)
564 observer->resource->entityHandler(OC_OBSERVE_FLAG, &ehRequest,
565 observer->resource->entityHandlerCallbackParam);
568 result = DeleteObserverUsingToken (token, tokenLength);
569 if(result == OC_STACK_OK)
571 OIC_LOG(DEBUG, TAG, "Removed observer successfully");
575 result = OC_STACK_OK;
576 OIC_LOG(DEBUG, TAG, "Observer Removal failed");
580 case OC_OBSERVER_STILL_INTERESTED:
581 OIC_LOG(DEBUG, TAG, "observer still interested, reset the failedCount");
582 observer = GetObserverUsingToken (token, tokenLength);
585 observer->forceHighQos = 0;
586 observer->failedCommCount = 0;
587 result = OC_STACK_OK;
591 result = OC_STACK_OBSERVER_NOT_FOUND;
595 case OC_OBSERVER_FAILED_COMM:
596 OIC_LOG(DEBUG, TAG, "observer is unreachable");
597 observer = GetObserverUsingToken (token, tokenLength);
600 if(observer->failedCommCount >= MAX_OBSERVER_FAILED_COMM)
602 result = FormOCEntityHandlerRequest(&ehRequest,
603 (OCRequestHandle)NULL,
606 (OCResourceHandle)NULL,
607 NULL, PAYLOAD_TYPE_REPRESENTATION,
609 OC_OBSERVE_DEREGISTER,
612 if(result != OC_STACK_OK)
614 return OC_STACK_ERROR;
616 observer->resource->entityHandler(OC_OBSERVE_FLAG, &ehRequest,
617 observer->resource->entityHandlerCallbackParam);
619 result = DeleteObserverUsingToken (token, tokenLength);
620 if(result == OC_STACK_OK)
622 OIC_LOG(DEBUG, TAG, "Removed observer successfully");
626 result = OC_STACK_OK;
627 OIC_LOG(DEBUG, TAG, "Observer Removal failed");
632 observer->failedCommCount++;
633 observer->forceHighQos = 1;
634 OIC_LOG_V(DEBUG, TAG, "Failed count for this observer is %d",
635 observer->failedCommCount);
636 result = OC_STACK_CONTINUE;
641 OIC_LOG(ERROR, TAG, "Unknown status");
642 result = OC_STACK_ERROR;
648 OCStackResult CAResponseToOCStackResult(CAResponseResult_t caCode)
650 OCStackResult ret = OC_STACK_ERROR;
654 ret = OC_STACK_RESOURCE_CREATED;
657 ret = OC_STACK_RESOURCE_DELETED;
660 ret = OC_STACK_RESOURCE_CHANGED;
667 ret = OC_STACK_INVALID_QUERY;
669 case CA_UNAUTHORIZED_REQ:
670 ret = OC_STACK_UNAUTHORIZED_REQ;
673 ret = OC_STACK_INVALID_OPTION;
676 ret = OC_STACK_NO_RESOURCE;
678 case CA_RETRANSMIT_TIMEOUT:
679 ret = OC_STACK_COMM_ERROR;
681 case CA_REQUEST_ENTITY_TOO_LARGE:
682 ret = OC_STACK_TOO_LARGE_REQ;
684 case CA_FORBIDDEN_REQ:
685 ret = OC_STACK_FORBIDDEN_REQ;
687 case CA_INTERNAL_SERVER_ERROR:
688 ret = OC_STACK_INTERNAL_SERVER_ERROR;
696 CAResponseResult_t OCToCAStackResult(OCStackResult ocCode, OCMethod method)
698 CAResponseResult_t ret = CA_INTERNAL_SERVER_ERROR;
707 // This Response Code is like HTTP 204 "No Content" but only used in
708 // response to POST and PUT requests.
712 // This Response Code is like HTTP 200 "OK" but only used in response to
717 // This should not happen but,
718 // give it a value just in case but output an error
720 OIC_LOG_V(ERROR, TAG, "Unexpected OC_STACK_OK return code for method [%d].",
724 case OC_STACK_RESOURCE_CREATED:
727 case OC_STACK_RESOURCE_DELETED:
730 case OC_STACK_RESOURCE_CHANGED:
733 case OC_STACK_INVALID_QUERY:
736 case OC_STACK_INVALID_OPTION:
739 case OC_STACK_NO_RESOURCE:
742 case OC_STACK_COMM_ERROR:
743 ret = CA_RETRANSMIT_TIMEOUT;
745 case OC_STACK_UNAUTHORIZED_REQ:
746 ret = CA_UNAUTHORIZED_REQ;
748 case OC_STACK_FORBIDDEN_REQ:
749 ret = CA_FORBIDDEN_REQ;
751 case OC_STACK_INTERNAL_SERVER_ERROR:
752 ret = CA_INTERNAL_SERVER_ERROR;
760 CATransportFlags_t OCToCATransportFlags(OCTransportFlags ocFlags)
762 CATransportFlags_t caFlags = (CATransportFlags_t)ocFlags;
764 // supply default behavior.
765 if ((caFlags & (CA_IPV6|CA_IPV4)) == 0)
767 caFlags = (CATransportFlags_t)(caFlags|CA_IPV6|CA_IPV4);
769 if ((caFlags & OC_MASK_SCOPE) == 0)
771 caFlags = (CATransportFlags_t)(caFlags|OC_SCOPE_LINK);
776 OCTransportFlags CAToOCTransportFlags(CATransportFlags_t caFlags)
778 return (OCTransportFlags)caFlags;
781 static OCStackResult ResetPresenceTTL(ClientCB *cbNode, uint32_t maxAgeSeconds)
783 uint32_t lowerBound = 0;
784 uint32_t higherBound = 0;
786 if (!cbNode || !cbNode->presence || !cbNode->presence->timeOut)
788 return OC_STACK_INVALID_PARAM;
791 OIC_LOG_V(INFO, TAG, "Update presence TTL, time is %u", GetTicks(0));
793 cbNode->presence->TTL = maxAgeSeconds;
795 for (int index = 0; index < PresenceTimeOutSize; index++)
797 // Guard against overflow
798 if (cbNode->presence->TTL < (UINT32_MAX/(MILLISECONDS_PER_SECOND*PresenceTimeOut[index]))
801 lowerBound = GetTicks((PresenceTimeOut[index] *
802 cbNode->presence->TTL *
803 MILLISECONDS_PER_SECOND)/100);
807 lowerBound = GetTicks(UINT32_MAX);
810 if (cbNode->presence->TTL < (UINT32_MAX/(MILLISECONDS_PER_SECOND*PresenceTimeOut[index+1]))
813 higherBound = GetTicks((PresenceTimeOut[index + 1] *
814 cbNode->presence->TTL *
815 MILLISECONDS_PER_SECOND)/100);
819 higherBound = GetTicks(UINT32_MAX);
822 cbNode->presence->timeOut[index] = OCGetRandomRange(lowerBound, higherBound);
824 OIC_LOG_V(DEBUG, TAG, "lowerBound timeout %d", lowerBound);
825 OIC_LOG_V(DEBUG, TAG, "higherBound timeout %d", higherBound);
826 OIC_LOG_V(DEBUG, TAG, "timeOut entry %d", cbNode->presence->timeOut[index]);
829 cbNode->presence->TTLlevel = 0;
831 OIC_LOG_V(DEBUG, TAG, "this TTL level %d", cbNode->presence->TTLlevel);
835 const char *convertTriggerEnumToString(OCPresenceTrigger trigger)
837 if (trigger == OC_PRESENCE_TRIGGER_CREATE)
839 return OC_RSRVD_TRIGGER_CREATE;
841 else if (trigger == OC_PRESENCE_TRIGGER_CHANGE)
843 return OC_RSRVD_TRIGGER_CHANGE;
847 return OC_RSRVD_TRIGGER_DELETE;
851 OCPresenceTrigger convertTriggerStringToEnum(const char * triggerStr)
855 return OC_PRESENCE_TRIGGER_CREATE;
857 else if(strcmp(triggerStr, OC_RSRVD_TRIGGER_CREATE) == 0)
859 return OC_PRESENCE_TRIGGER_CREATE;
861 else if(strcmp(triggerStr, OC_RSRVD_TRIGGER_CHANGE) == 0)
863 return OC_PRESENCE_TRIGGER_CHANGE;
867 return OC_PRESENCE_TRIGGER_DELETE;
871 OCStackResult OCEncodeAddressForRFC6874(char *outputAddress,
873 const char *inputAddress)
875 VERIFY_NON_NULL(inputAddress, FATAL, OC_STACK_INVALID_PARAM);
876 VERIFY_NON_NULL(outputAddress, FATAL, OC_STACK_INVALID_PARAM);
878 size_t inputLength = strnlen(inputAddress, outputSize);
880 // inputSize includes the null terminator
881 size_t inputSize = inputLength + 1;
883 if (inputSize > outputSize)
885 OIC_LOG_V(ERROR, TAG,
886 "OCEncodeAddressForRFC6874 failed: "
887 "outputSize (%zu) < inputSize (%zu)",
888 outputSize, inputSize);
890 return OC_STACK_ERROR;
893 char* percentChar = strchr(inputAddress, '%');
895 // If there is no '%' character, then no change is required to the string.
896 if (NULL == percentChar)
898 OICStrcpy(outputAddress, outputSize, inputAddress);
902 const char* addressPart = &inputAddress[0];
903 const char* scopeIdPart = percentChar + 1;
905 // Sanity check to make sure this string doesn't have more '%' characters
906 if (NULL != strchr(scopeIdPart, '%'))
908 return OC_STACK_ERROR;
911 // If no string follows the first '%', then the input was invalid.
912 if (scopeIdPart[0] == '\0')
914 OIC_LOG(ERROR, TAG, "OCEncodeAddressForRFC6874 failed: Invalid input string: no scope ID!");
915 return OC_STACK_ERROR;
918 // Check to see if the string is already encoded
919 if ((scopeIdPart[0] == '2') && (scopeIdPart[1] == '5'))
921 OIC_LOG(ERROR, TAG, "OCEncodeAddressForRFC6874 failed: Input string is already encoded");
922 return OC_STACK_ERROR;
925 // Fail if we don't have room for encoded string's two additional chars
926 if (outputSize < (inputSize + 2))
928 OIC_LOG(ERROR, TAG, "OCEncodeAddressForRFC6874 failed: encoded output will not fit!");
929 return OC_STACK_ERROR;
932 // Restore the null terminator with an escaped '%' character, per RFC 6874
933 OICStrcpy(outputAddress, scopeIdPart - addressPart, addressPart);
934 strcat(outputAddress, "%25");
935 strcat(outputAddress, scopeIdPart);
940 OCStackResult OCDecodeAddressForRFC6874(char *outputAddress,
942 const char *inputAddress,
945 VERIFY_NON_NULL(inputAddress, FATAL, OC_STACK_INVALID_PARAM);
946 VERIFY_NON_NULL(outputAddress, FATAL, OC_STACK_INVALID_PARAM);
950 end = inputAddress + strlen(inputAddress);
952 size_t inputLength = end - inputAddress;
954 const char *percent = strchr(inputAddress, '%');
955 if (!percent || (percent > end))
957 OICStrcpyPartial(outputAddress, outputSize, inputAddress, inputLength);
961 if (percent[1] != '2' || percent[2] != '5')
963 return OC_STACK_INVALID_URI;
966 int addrlen = percent - inputAddress + 1;
967 OICStrcpyPartial(outputAddress, outputSize, inputAddress, addrlen);
968 OICStrcpyPartial(outputAddress + addrlen, outputSize - addrlen,
969 percent + 3, end - percent - 3);
976 * The cononical presence allows constructed URIs to be string compared.
978 * requestUri must be a char array of size CA_MAX_URI_LENGTH
980 static int FormCanonicalPresenceUri(const CAEndpoint_t *endpoint,
981 char *presenceUri, bool isMulticast)
983 VERIFY_NON_NULL(endpoint , FATAL, OC_STACK_INVALID_PARAM);
984 VERIFY_NON_NULL(presenceUri, FATAL, OC_STACK_INVALID_PARAM);
988 OIC_LOG(DEBUG, TAG, "Make Multicast Presence URI");
989 return snprintf(presenceUri, CA_MAX_URI_LENGTH, "%s", OC_RSRVD_PRESENCE_URI);
992 CAEndpoint_t *ep = (CAEndpoint_t *)endpoint;
993 if (ep->adapter == CA_ADAPTER_IP)
995 if ((ep->flags & CA_IPV6) && !(ep->flags & CA_IPV4))
997 if ('\0' == ep->addr[0]) // multicast
999 return snprintf(presenceUri, CA_MAX_URI_LENGTH, OC_RSRVD_PRESENCE_URI);
1003 char addressEncoded[CA_MAX_URI_LENGTH] = {0};
1005 OCStackResult result = OCEncodeAddressForRFC6874(addressEncoded,
1006 sizeof(addressEncoded),
1009 if (OC_STACK_OK != result)
1014 return snprintf(presenceUri, CA_MAX_URI_LENGTH, "coap://[%s]:%u%s",
1015 addressEncoded, ep->port, OC_RSRVD_PRESENCE_URI);
1020 if ('\0' == ep->addr[0]) // multicast
1022 OICStrcpy(ep->addr, sizeof(ep->addr), OC_MULTICAST_IP);
1023 ep->port = OC_MULTICAST_PORT;
1025 return snprintf(presenceUri, CA_MAX_URI_LENGTH, "coap://%s:%u%s",
1026 ep->addr, ep->port, OC_RSRVD_PRESENCE_URI);
1030 // might work for other adapters (untested, but better than nothing)
1031 return snprintf(presenceUri, CA_MAX_URI_LENGTH, "coap://%s%s", ep->addr,
1032 OC_RSRVD_PRESENCE_URI);
1036 OCStackResult HandlePresenceResponse(const CAEndpoint_t *endpoint,
1037 const CAResponseInfo_t *responseInfo)
1039 VERIFY_NON_NULL(endpoint, FATAL, OC_STACK_INVALID_PARAM);
1040 VERIFY_NON_NULL(responseInfo, FATAL, OC_STACK_INVALID_PARAM);
1042 OCStackApplicationResult cbResult = OC_STACK_DELETE_TRANSACTION;
1043 ClientCB * cbNode = NULL;
1044 char *resourceTypeName = NULL;
1045 OCClientResponse response = {.devAddr = {.adapter = OC_DEFAULT_ADAPTER}};
1046 OCStackResult result = OC_STACK_ERROR;
1047 uint32_t maxAge = 0;
1049 char presenceUri[CA_MAX_URI_LENGTH];
1051 int presenceSubscribe = 0;
1052 int multicastPresenceSubscribe = 0;
1054 if (responseInfo->result != CA_CONTENT)
1056 OIC_LOG_V(ERROR, TAG, "HandlePresenceResponse failed %d", responseInfo->result);
1057 return OC_STACK_ERROR;
1060 response.payload = NULL;
1061 response.result = OC_STACK_OK;
1063 CopyEndpointToDevAddr(endpoint, &response.devAddr);
1064 FixUpClientResponse(&response);
1066 if (responseInfo->info.payload)
1068 result = OCParsePayload(&response.payload,
1069 PAYLOAD_TYPE_PRESENCE,
1070 responseInfo->info.payload,
1071 responseInfo->info.payloadSize);
1073 if(result != OC_STACK_OK)
1075 OIC_LOG(ERROR, TAG, "Presence parse failed");
1078 if(!response.payload || response.payload->type != PAYLOAD_TYPE_PRESENCE)
1080 OIC_LOG(ERROR, TAG, "Presence payload was wrong type");
1081 result = OC_STACK_ERROR;
1084 response.sequenceNumber = ((OCPresencePayload*)response.payload)->sequenceNumber;
1085 resourceTypeName = ((OCPresencePayload*)response.payload)->resourceType;
1086 maxAge = ((OCPresencePayload*)response.payload)->maxAge;
1089 // check for unicast presence
1090 uriLen = FormCanonicalPresenceUri(endpoint, presenceUri,
1091 responseInfo->isMulticast);
1092 if (uriLen < 0 || (size_t)uriLen >= sizeof (presenceUri))
1094 return OC_STACK_INVALID_URI;
1096 OIC_LOG(ERROR, TAG, "check for unicast presence");
1097 cbNode = GetClientCB(NULL, 0, NULL, presenceUri);
1100 presenceSubscribe = 1;
1104 // check for multicast presence
1105 OIC_LOG(ERROR, TAG, "check for multicast presence");
1106 cbNode = GetClientCB(NULL, 0, NULL, OC_RSRVD_PRESENCE_URI);
1109 multicastPresenceSubscribe = 1;
1113 if (!presenceSubscribe && !multicastPresenceSubscribe)
1115 OIC_LOG(ERROR, TAG, "Received a presence notification, but no callback, ignoring");
1119 if (presenceSubscribe)
1121 if(cbNode->sequenceNumber == response.sequenceNumber)
1123 OIC_LOG(INFO, TAG, "No presence change");
1124 ResetPresenceTTL(cbNode, maxAge);
1125 OIC_LOG_V(INFO, TAG, "ResetPresenceTTL - TTLlevel:%d\n", cbNode->presence->TTLlevel);
1131 OIC_LOG(INFO, TAG, "Stopping presence");
1132 response.result = OC_STACK_PRESENCE_STOPPED;
1133 if(cbNode->presence)
1135 OICFree(cbNode->presence->timeOut);
1136 OICFree(cbNode->presence);
1137 cbNode->presence = NULL;
1142 if(!cbNode->presence)
1144 cbNode->presence = (OCPresence *)OICMalloc(sizeof (OCPresence));
1146 if(!(cbNode->presence))
1148 OIC_LOG(ERROR, TAG, "Could not allocate memory for cbNode->presence");
1149 result = OC_STACK_NO_MEMORY;
1153 VERIFY_NON_NULL_V(cbNode->presence);
1154 cbNode->presence->timeOut = NULL;
1155 cbNode->presence->timeOut = (uint32_t *)
1156 OICMalloc(PresenceTimeOutSize * sizeof(uint32_t));
1157 if(!(cbNode->presence->timeOut)){
1159 "Could not allocate memory for cbNode->presence->timeOut");
1160 OICFree(cbNode->presence);
1161 result = OC_STACK_NO_MEMORY;
1166 ResetPresenceTTL(cbNode, maxAge);
1168 cbNode->sequenceNumber = response.sequenceNumber;
1173 // This is the multicast case
1174 OIC_LOG(INFO, TAG, "this is the multicast presence");
1177 OIC_LOG(INFO, TAG, "Stopping presence");
1178 response.result = OC_STACK_PRESENCE_STOPPED;
1182 // Ensure that a filter is actually applied.
1183 if (resourceTypeName && cbNode->filterResourceType)
1185 OIC_LOG_V(INFO, TAG, "find resource type : %s", resourceTypeName);
1186 if(!findResourceType(cbNode->filterResourceType, resourceTypeName))
1192 OIC_LOG(INFO, TAG, "Callback for presence");
1194 cbResult = cbNode->callBack(cbNode->context, cbNode->handle, &response);
1196 if (cbResult == OC_STACK_DELETE_TRANSACTION)
1198 FindAndDeleteClientCB(cbNode);
1202 OCPayloadDestroy(response.payload);
1206 void OCHandleResponse(const CAEndpoint_t* endPoint, const CAResponseInfo_t* responseInfo)
1208 OIC_LOG(DEBUG, TAG, "Enter OCHandleResponse");
1210 if(responseInfo->info.resourceUri &&
1211 strcmp(responseInfo->info.resourceUri, OC_RSRVD_PRESENCE_URI) == 0)
1213 HandlePresenceResponse(endPoint, responseInfo);
1217 ClientCB *cbNode = GetClientCB(responseInfo->info.token,
1218 responseInfo->info.tokenLength, NULL, NULL);
1220 ResourceObserver * observer = GetObserverUsingToken (responseInfo->info.token,
1221 responseInfo->info.tokenLength);
1225 OIC_LOG(INFO, TAG, "There is a cbNode associated with the response token");
1226 if(responseInfo->result == CA_EMPTY)
1228 OIC_LOG(INFO, TAG, "Receiving A ACK/RESET for this token");
1229 // We do not have a case for the client to receive a RESET
1230 if(responseInfo->info.type == CA_MSG_ACKNOWLEDGE)
1232 //This is the case of receiving an ACK on a request to a slow resource!
1233 OIC_LOG(INFO, TAG, "This is a pure ACK");
1234 //TODO: should we inform the client
1235 // app that at least the request was received at the server?
1238 else if(responseInfo->result == CA_RETRANSMIT_TIMEOUT)
1240 OIC_LOG(INFO, TAG, "Receiving A Timeout for this token");
1241 OIC_LOG(INFO, TAG, "Calling into application address space");
1243 OCClientResponse response =
1244 {.devAddr = {.adapter = OC_DEFAULT_ADAPTER}};
1245 CopyEndpointToDevAddr(endPoint, &response.devAddr);
1246 FixUpClientResponse(&response);
1247 response.resourceUri = responseInfo->info.resourceUri;
1248 memcpy(response.identity.id, responseInfo->info.identity.id,
1249 sizeof (response.identity.id));
1250 response.identity.id_length = responseInfo->info.identity.id_length;
1252 response.result = CAResponseToOCStackResult(responseInfo->result);
1253 cbNode->callBack(cbNode->context,
1254 cbNode->handle, &response);
1255 FindAndDeleteClientCB(cbNode);
1259 OIC_LOG(INFO, TAG, "This is a regular response, A client call back is found");
1260 OIC_LOG(INFO, TAG, "Calling into application address space");
1262 OCClientResponse response =
1263 {.devAddr = {.adapter = OC_DEFAULT_ADAPTER}};
1264 response.sequenceNumber = MAX_SEQUENCE_NUMBER + 1;
1265 CopyEndpointToDevAddr(endPoint, &response.devAddr);
1266 FixUpClientResponse(&response);
1267 response.resourceUri = responseInfo->info.resourceUri;
1268 memcpy(response.identity.id, responseInfo->info.identity.id,
1269 sizeof (response.identity.id));
1270 response.identity.id_length = responseInfo->info.identity.id_length;
1272 response.result = CAResponseToOCStackResult(responseInfo->result);
1274 if(responseInfo->info.payload &&
1275 responseInfo->info.payloadSize)
1277 OCPayloadType type = PAYLOAD_TYPE_INVALID;
1278 // check the security resource
1279 if (SRMIsSecurityResourceURI(cbNode->requestUri))
1281 type = PAYLOAD_TYPE_SECURITY;
1283 else if (cbNode->method == OC_REST_DISCOVER)
1285 if (strncmp(OC_RSRVD_WELL_KNOWN_URI,cbNode->requestUri,
1286 sizeof(OC_RSRVD_WELL_KNOWN_URI) - 1) == 0)
1288 type = PAYLOAD_TYPE_DISCOVERY;
1291 else if (strcmp(cbNode->requestUri, OC_RSRVD_WELL_KNOWN_MQ_URI) == 0)
1293 type = PAYLOAD_TYPE_DISCOVERY;
1296 else if (strcmp(cbNode->requestUri, OC_RSRVD_DEVICE_URI) == 0)
1298 type = PAYLOAD_TYPE_REPRESENTATION;
1300 else if (strcmp(cbNode->requestUri, OC_RSRVD_PLATFORM_URI) == 0)
1302 type = PAYLOAD_TYPE_REPRESENTATION;
1305 #ifdef ROUTING_GATEWAY
1306 else if (strcmp(cbNode->requestUri, OC_RSRVD_GATEWAY_URI) == 0)
1308 type = PAYLOAD_TYPE_REPRESENTATION;
1311 else if (strcmp(cbNode->requestUri, OC_RSRVD_RD_URI) == 0)
1313 type = PAYLOAD_TYPE_REPRESENTATION;
1316 else if (strcmp(cbNode->requestUri, KEEPALIVE_RESOURCE_URI) == 0)
1318 type = PAYLOAD_TYPE_REPRESENTATION;
1323 OIC_LOG_V(ERROR, TAG, "Unknown Payload type in Discovery: %d %s",
1324 cbNode->method, cbNode->requestUri);
1328 else if (cbNode->method == OC_REST_GET ||
1329 cbNode->method == OC_REST_PUT ||
1330 cbNode->method == OC_REST_POST ||
1331 cbNode->method == OC_REST_OBSERVE ||
1332 cbNode->method == OC_REST_OBSERVE_ALL ||
1333 cbNode->method == OC_REST_DELETE)
1335 if (cbNode->requestUri)
1337 if (0 == strcmp(OC_RSRVD_PLATFORM_URI, cbNode->requestUri))
1339 type = PAYLOAD_TYPE_REPRESENTATION;
1341 else if (0 == strcmp(OC_RSRVD_DEVICE_URI, cbNode->requestUri))
1343 type = PAYLOAD_TYPE_REPRESENTATION;
1345 if (type == PAYLOAD_TYPE_INVALID)
1347 OIC_LOG_V(INFO, TAG, "Assuming PAYLOAD_TYPE_REPRESENTATION: %d %s",
1348 cbNode->method, cbNode->requestUri);
1349 type = PAYLOAD_TYPE_REPRESENTATION;
1354 OIC_LOG(INFO, TAG, "No Request URI, PROXY URI");
1355 type = PAYLOAD_TYPE_REPRESENTATION;
1360 OIC_LOG_V(ERROR, TAG, "Unknown Payload type: %d %s",
1361 cbNode->method, cbNode->requestUri);
1365 if(OC_STACK_OK != OCParsePayload(&response.payload,
1367 responseInfo->info.payload,
1368 responseInfo->info.payloadSize))
1370 OIC_LOG(ERROR, TAG, "Error converting payload");
1371 OCPayloadDestroy(response.payload);
1376 response.numRcvdVendorSpecificHeaderOptions = 0;
1377 if(responseInfo->info.numOptions > 0)
1380 //First option always with option ID is COAP_OPTION_OBSERVE if it is available.
1381 if(responseInfo->info.options[0].optionID == COAP_OPTION_OBSERVE)
1384 uint32_t observationOption;
1385 uint8_t* optionData = (uint8_t*)responseInfo->info.options[0].optionData;
1386 for (observationOption=0, i=0;
1387 i<sizeof(uint32_t) && i<responseInfo->info.options[0].optionLength;
1391 (observationOption << 8) | optionData[i];
1393 response.sequenceNumber = observationOption;
1394 response.numRcvdVendorSpecificHeaderOptions = responseInfo->info.numOptions - 1;
1399 response.numRcvdVendorSpecificHeaderOptions = responseInfo->info.numOptions;
1402 if(response.numRcvdVendorSpecificHeaderOptions > MAX_HEADER_OPTIONS)
1404 OIC_LOG(ERROR, TAG, "#header options are more than MAX_HEADER_OPTIONS");
1405 OCPayloadDestroy(response.payload);
1409 for (uint8_t i = start; i < responseInfo->info.numOptions; i++)
1411 memcpy (&(response.rcvdVendorSpecificHeaderOptions[i-start]),
1412 &(responseInfo->info.options[i]), sizeof(OCHeaderOption));
1416 if (cbNode->method == OC_REST_OBSERVE &&
1417 response.sequenceNumber > OC_OFFSET_SEQUENCE_NUMBER &&
1418 cbNode->sequenceNumber <= MAX_SEQUENCE_NUMBER &&
1419 response.sequenceNumber <= cbNode->sequenceNumber)
1421 OIC_LOG_V(INFO, TAG, "Received stale notification. Number :%d",
1422 response.sequenceNumber);
1427 // if request uri is '/oic/rd', update ins value of resource.
1428 char *targetUri = strstr(cbNode->requestUri, OC_RSRVD_RD_URI);
1431 OCUpdateResourceInsWithResponse(cbNode->requestUri, &response);
1434 OCStackApplicationResult appFeedback = cbNode->callBack(cbNode->context,
1437 cbNode->sequenceNumber = response.sequenceNumber;
1439 if (appFeedback == OC_STACK_DELETE_TRANSACTION)
1441 FindAndDeleteClientCB(cbNode);
1445 // To keep discovery callbacks active.
1446 cbNode->TTL = GetTicks(MAX_CB_TIMEOUT_SECONDS *
1447 MILLISECONDS_PER_SECOND);
1451 //Need to send ACK when the response is CON
1452 if(responseInfo->info.type == CA_MSG_CONFIRM)
1454 SendDirectStackResponse(endPoint, responseInfo->info.messageId, CA_EMPTY,
1455 CA_MSG_ACKNOWLEDGE, 0, NULL, NULL, 0, NULL, CA_RESPONSE_FOR_RES);
1458 OCPayloadDestroy(response.payload);
1465 OIC_LOG(INFO, TAG, "There is an observer associated with the response token");
1466 if(responseInfo->result == CA_EMPTY)
1468 OIC_LOG(INFO, TAG, "Receiving A ACK/RESET for this token");
1469 if(responseInfo->info.type == CA_MSG_RESET)
1471 OIC_LOG(INFO, TAG, "This is a RESET");
1472 OCStackFeedBack(responseInfo->info.token, responseInfo->info.tokenLength,
1473 OC_OBSERVER_NOT_INTERESTED);
1475 else if(responseInfo->info.type == CA_MSG_ACKNOWLEDGE)
1477 OIC_LOG(INFO, TAG, "This is a pure ACK");
1478 OCStackFeedBack(responseInfo->info.token, responseInfo->info.tokenLength,
1479 OC_OBSERVER_STILL_INTERESTED);
1482 else if(responseInfo->result == CA_RETRANSMIT_TIMEOUT)
1484 OIC_LOG(INFO, TAG, "Receiving Time Out for an observer");
1485 OCStackFeedBack(responseInfo->info.token, responseInfo->info.tokenLength,
1486 OC_OBSERVER_FAILED_COMM);
1491 if(!cbNode && !observer)
1493 if(myStackMode == OC_CLIENT || myStackMode == OC_CLIENT_SERVER
1494 || myStackMode == OC_GATEWAY)
1496 OIC_LOG(INFO, TAG, "This is a client, but no cbNode was found for token");
1497 if(responseInfo->result == CA_EMPTY)
1499 OIC_LOG(INFO, TAG, "Receiving CA_EMPTY in the ocstack");
1503 OIC_LOG(INFO, TAG, "Received a message without callbacks. Sending RESET");
1504 SendDirectStackResponse(endPoint, responseInfo->info.messageId, CA_EMPTY,
1505 CA_MSG_RESET, 0, NULL, NULL, 0, NULL, CA_RESPONSE_FOR_RES);
1509 if(myStackMode == OC_SERVER || myStackMode == OC_CLIENT_SERVER
1510 || myStackMode == OC_GATEWAY)
1512 OIC_LOG(INFO, TAG, "This is a server, but no observer was found for token");
1513 if (responseInfo->info.type == CA_MSG_ACKNOWLEDGE)
1515 OIC_LOG_V(INFO, TAG, "Received ACK at server for messageId : %d",
1516 responseInfo->info.messageId);
1518 if (responseInfo->info.type == CA_MSG_RESET)
1520 OIC_LOG_V(INFO, TAG, "Received RESET at server for messageId : %d",
1521 responseInfo->info.messageId);
1529 void HandleCAResponses(const CAEndpoint_t* endPoint, const CAResponseInfo_t* responseInfo)
1531 VERIFY_NON_NULL_NR(endPoint, FATAL);
1532 VERIFY_NON_NULL_NR(responseInfo, FATAL);
1534 OIC_LOG(INFO, TAG, "Enter HandleCAResponses");
1536 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
1537 #ifdef ROUTING_GATEWAY
1538 bool needRIHandling = false;
1540 * Routing manager is going to update either of endpoint or response or both.
1541 * This typecasting is done to avoid unnecessary duplication of Endpoint and responseInfo
1542 * RM can update "routeData" option in endPoint so that future RI requests can be sent to proper
1545 OCStackResult ret = RMHandleResponse((CAResponseInfo_t *)responseInfo, (CAEndpoint_t *)endPoint,
1547 if(ret != OC_STACK_OK || !needRIHandling)
1549 OIC_LOG_V(INFO, TAG, "Routing status![%d]. Not forwarding to RI", ret);
1555 * Put source in sender endpoint so that the next packet from application can be routed to
1556 * proper destination and remove "RM" coap header option before passing request / response to
1557 * RI as this option will make no sense to either RI or application.
1559 RMUpdateInfo((CAHeaderOption_t **) &(responseInfo->info.options),
1560 (uint8_t *) &(responseInfo->info.numOptions),
1561 (CAEndpoint_t *) endPoint);
1564 OCHandleResponse(endPoint, responseInfo);
1566 OIC_LOG(INFO, TAG, "Exit HandleCAResponses");
1570 * This function handles error response from CA
1571 * code shall be added to handle the errors
1573 void HandleCAErrorResponse(const CAEndpoint_t *endPoint, const CAErrorInfo_t *errorInfo)
1575 VERIFY_NON_NULL_NR(endPoint, FATAL);
1576 VERIFY_NON_NULL_NR(errorInfo, FATAL);
1578 OIC_LOG(INFO, TAG, "Enter HandleCAErrorResponse");
1580 ClientCB *cbNode = GetClientCB(errorInfo->info.token,
1581 errorInfo->info.tokenLength, NULL, NULL);
1584 OCClientResponse response = { .devAddr = { .adapter = OC_DEFAULT_ADAPTER } };
1585 CopyEndpointToDevAddr(endPoint, &response.devAddr);
1586 FixUpClientResponse(&response);
1587 response.resourceUri = errorInfo->info.resourceUri;
1588 memcpy(response.identity.id, errorInfo->info.identity.id,
1589 sizeof (response.identity.id));
1590 response.identity.id_length = errorInfo->info.identity.id_length;
1591 response.result = CAResultToOCResult(errorInfo->result);
1593 cbNode->callBack(cbNode->context, cbNode->handle, &response);
1596 ResourceObserver *observer = GetObserverUsingToken(errorInfo->info.token,
1597 errorInfo->info.tokenLength);
1600 OIC_LOG(INFO, TAG, "Receiving communication error for an observer");
1601 OCStackResult result = CAResultToOCResult(errorInfo->result);
1602 if (OC_STACK_COMM_ERROR == result)
1604 OCStackFeedBack(errorInfo->info.token, errorInfo->info.tokenLength,
1605 OC_OBSERVER_FAILED_COMM);
1609 OIC_LOG(INFO, TAG, "Exit HandleCAErrorResponse");
1613 * This function sends out Direct Stack Responses. These are responses that are not coming
1614 * from the application entity handler. These responses have no payload and are usually ACKs,
1615 * RESETs or some error conditions that were caught by the stack.
1617 OCStackResult SendDirectStackResponse(const CAEndpoint_t* endPoint, const uint16_t coapID,
1618 const CAResponseResult_t responseResult, const CAMessageType_t type,
1619 const uint8_t numOptions, const CAHeaderOption_t *options,
1620 CAToken_t token, uint8_t tokenLength, const char *resourceUri,
1621 CADataType_t dataType)
1623 OIC_LOG(DEBUG, TAG, "Entering SendDirectStackResponse");
1624 CAResponseInfo_t respInfo = {
1625 .result = responseResult
1627 respInfo.info.messageId = coapID;
1628 respInfo.info.numOptions = numOptions;
1630 if (respInfo.info.numOptions)
1632 respInfo.info.options =
1633 (CAHeaderOption_t *)OICCalloc(respInfo.info.numOptions, sizeof(CAHeaderOption_t));
1634 memcpy (respInfo.info.options, options,
1635 sizeof(CAHeaderOption_t) * respInfo.info.numOptions);
1639 respInfo.info.payload = NULL;
1640 respInfo.info.token = token;
1641 respInfo.info.tokenLength = tokenLength;
1642 respInfo.info.type = type;
1643 respInfo.info.resourceUri = OICStrdup (resourceUri);
1644 respInfo.info.acceptFormat = CA_FORMAT_UNDEFINED;
1645 respInfo.info.dataType = dataType;
1647 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
1648 // Add the destination to route option from the endpoint->routeData.
1649 bool doPost = false;
1650 OCStackResult result = RMAddInfo(endPoint->routeData, &respInfo, false, &doPost);
1651 if(OC_STACK_OK != result)
1653 OIC_LOG_V(ERROR, TAG, "Add routing option failed [%d]", result);
1654 OICFree (respInfo.info.resourceUri);
1655 OICFree (respInfo.info.options);
1660 OIC_LOG(DEBUG, TAG, "Sending a POST message for EMPTY ACK in Client Mode");
1661 CARequestInfo_t reqInfo = {.method = CA_POST };
1662 /* The following initialization is not done in a single initializer block as in
1663 * arduino, .c file is compiled as .cpp and moves it from C99 to C++11. The latter
1664 * does not have designated initalizers. This is a work-around for now.
1666 reqInfo.info.type = CA_MSG_NONCONFIRM;
1667 reqInfo.info.messageId = coapID;
1668 reqInfo.info.tokenLength = tokenLength;
1669 reqInfo.info.token = token;
1670 reqInfo.info.numOptions = respInfo.info.numOptions;
1671 reqInfo.info.payload = NULL;
1672 reqInfo.info.resourceUri = OICStrdup (OC_RSRVD_GATEWAY_URI);
1673 if (reqInfo.info.numOptions)
1675 reqInfo.info.options =
1676 (CAHeaderOption_t *)OICCalloc(reqInfo.info.numOptions, sizeof(CAHeaderOption_t));
1677 if (NULL == reqInfo.info.options)
1679 OIC_LOG(ERROR, TAG, "Calloc failed");
1680 OICFree (reqInfo.info.resourceUri);
1681 OICFree (respInfo.info.resourceUri);
1682 OICFree (respInfo.info.options);
1683 return OC_STACK_NO_MEMORY;
1685 memcpy (reqInfo.info.options, respInfo.info.options,
1686 sizeof(CAHeaderOption_t) * reqInfo.info.numOptions);
1689 CAResult_t caResult = CASendRequest(endPoint, &reqInfo);
1690 OICFree (reqInfo.info.resourceUri);
1691 OICFree (reqInfo.info.options);
1692 OICFree (respInfo.info.resourceUri);
1693 OICFree (respInfo.info.options);
1694 if (CA_STATUS_OK != caResult)
1696 OIC_LOG(ERROR, TAG, "CASendRequest error");
1697 return CAResultToOCResult(caResult);
1703 CAResult_t caResult = CASendResponse(endPoint, &respInfo);
1705 // resourceUri in the info field is cloned in the CA layer and
1706 // thus ownership is still here.
1707 OICFree (respInfo.info.resourceUri);
1708 OICFree (respInfo.info.options);
1709 if(CA_STATUS_OK != caResult)
1711 OIC_LOG(ERROR, TAG, "CASendResponse error");
1712 return CAResultToOCResult(caResult);
1715 OIC_LOG(DEBUG, TAG, "Exit SendDirectStackResponse");
1719 OCStackResult HandleStackRequests(OCServerProtocolRequest * protocolRequest)
1721 OIC_LOG(INFO, TAG, "Entering HandleStackRequests (OCStack Layer)");
1722 OCStackResult result = OC_STACK_ERROR;
1723 if(!protocolRequest)
1725 OIC_LOG(ERROR, TAG, "protocolRequest is NULL");
1726 return OC_STACK_INVALID_PARAM;
1729 OCServerRequest * request = GetServerRequestUsingToken(protocolRequest->requestToken,
1730 protocolRequest->tokenLength);
1733 OIC_LOG(INFO, TAG, "This is a new Server Request");
1734 result = AddServerRequest(&request, protocolRequest->coapID,
1735 protocolRequest->delayedResNeeded, 0, protocolRequest->method,
1736 protocolRequest->numRcvdVendorSpecificHeaderOptions,
1737 protocolRequest->observationOption, protocolRequest->qos,
1738 protocolRequest->query, protocolRequest->rcvdVendorSpecificHeaderOptions,
1739 protocolRequest->payload, protocolRequest->requestToken,
1740 protocolRequest->tokenLength, protocolRequest->resourceUrl,
1741 protocolRequest->reqTotalSize, protocolRequest->acceptFormat,
1742 &protocolRequest->devAddr);
1743 if (OC_STACK_OK != result)
1745 OIC_LOG(ERROR, TAG, "Error adding server request");
1751 OIC_LOG(ERROR, TAG, "Out of Memory");
1752 return OC_STACK_NO_MEMORY;
1755 if(!protocolRequest->reqMorePacket)
1757 request->requestComplete = 1;
1762 OIC_LOG(INFO, TAG, "This is either a repeated or blocked Server Request");
1765 if(request->requestComplete)
1767 OIC_LOG(INFO, TAG, "This Server Request is complete");
1768 ResourceHandling resHandling = OC_RESOURCE_VIRTUAL;
1769 OCResource *resource = NULL;
1770 result = DetermineResourceHandling (request, &resHandling, &resource);
1771 if (result == OC_STACK_OK)
1773 result = ProcessRequest(resHandling, resource, request);
1778 OIC_LOG(INFO, TAG, "This Server Request is incomplete");
1779 result = OC_STACK_CONTINUE;
1784 void OCHandleRequests(const CAEndpoint_t* endPoint, const CARequestInfo_t* requestInfo)
1786 OIC_LOG(DEBUG, TAG, "Enter OCHandleRequests");
1788 OCStackResult requestResult = OC_STACK_ERROR;
1790 if(myStackMode == OC_CLIENT)
1792 //TODO: should the client be responding to requests?
1796 OCServerProtocolRequest serverRequest = {0};
1798 OIC_LOG_V(INFO, TAG, "Endpoint URI : %s", requestInfo->info.resourceUri);
1800 char * uriWithoutQuery = NULL;
1801 char * query = NULL;
1803 requestResult = getQueryFromUri(requestInfo->info.resourceUri, &query, &uriWithoutQuery);
1805 if (requestResult != OC_STACK_OK || !uriWithoutQuery)
1807 OIC_LOG_V(ERROR, TAG, "getQueryFromUri() failed with OC error code %d\n", requestResult);
1810 OIC_LOG_V(INFO, TAG, "URI without query: %s", uriWithoutQuery);
1811 OIC_LOG_V(INFO, TAG, "Query : %s", query);
1813 if(strlen(uriWithoutQuery) < MAX_URI_LENGTH)
1815 OICStrcpy(serverRequest.resourceUrl, sizeof(serverRequest.resourceUrl), uriWithoutQuery);
1816 OICFree(uriWithoutQuery);
1820 OIC_LOG(ERROR, TAG, "URI length exceeds MAX_URI_LENGTH.");
1821 OICFree(uriWithoutQuery);
1828 if(strlen(query) < MAX_QUERY_LENGTH)
1830 OICStrcpy(serverRequest.query, sizeof(serverRequest.query), query);
1835 OIC_LOG(ERROR, TAG, "Query length exceeds MAX_QUERY_LENGTH.");
1841 if ((requestInfo->info.payload) && (0 < requestInfo->info.payloadSize))
1843 serverRequest.reqTotalSize = requestInfo->info.payloadSize;
1844 serverRequest.payload = (uint8_t *) OICMalloc(requestInfo->info.payloadSize);
1845 if (!serverRequest.payload)
1847 OIC_LOG(ERROR, TAG, "Allocation for payload failed.");
1850 memcpy (serverRequest.payload, requestInfo->info.payload,
1851 requestInfo->info.payloadSize);
1855 serverRequest.reqTotalSize = 0;
1858 switch (requestInfo->method)
1861 serverRequest.method = OC_REST_GET;
1864 serverRequest.method = OC_REST_PUT;
1867 serverRequest.method = OC_REST_POST;
1870 serverRequest.method = OC_REST_DELETE;
1873 OIC_LOG_V(ERROR, TAG, "Received CA method %d not supported", requestInfo->method);
1874 SendDirectStackResponse(endPoint, requestInfo->info.messageId, CA_BAD_REQ,
1875 requestInfo->info.type, requestInfo->info.numOptions,
1876 requestInfo->info.options, requestInfo->info.token,
1877 requestInfo->info.tokenLength, requestInfo->info.resourceUri,
1879 OICFree(serverRequest.payload);
1883 OIC_LOG_BUFFER(INFO, TAG, (const uint8_t *)requestInfo->info.token,
1884 requestInfo->info.tokenLength);
1886 serverRequest.tokenLength = requestInfo->info.tokenLength;
1887 if (serverRequest.tokenLength) {
1889 serverRequest.requestToken = (CAToken_t)OICMalloc(requestInfo->info.tokenLength);
1891 if (!serverRequest.requestToken)
1893 OIC_LOG(FATAL, TAG, "Allocation for token failed.");
1894 SendDirectStackResponse(endPoint, requestInfo->info.messageId, CA_INTERNAL_SERVER_ERROR,
1895 requestInfo->info.type, requestInfo->info.numOptions,
1896 requestInfo->info.options, requestInfo->info.token,
1897 requestInfo->info.tokenLength, requestInfo->info.resourceUri,
1899 OICFree(serverRequest.payload);
1902 memcpy(serverRequest.requestToken, requestInfo->info.token, requestInfo->info.tokenLength);
1905 switch (requestInfo->info.acceptFormat)
1907 case CA_FORMAT_APPLICATION_CBOR:
1908 serverRequest.acceptFormat = OC_FORMAT_CBOR;
1910 case CA_FORMAT_UNDEFINED:
1911 serverRequest.acceptFormat = OC_FORMAT_UNDEFINED;
1914 serverRequest.acceptFormat = OC_FORMAT_UNSUPPORTED;
1917 if (requestInfo->info.type == CA_MSG_CONFIRM)
1919 serverRequest.qos = OC_HIGH_QOS;
1923 serverRequest.qos = OC_LOW_QOS;
1925 // CA does not need the following field
1926 // Are we sure CA does not need them? how is it responding to multicast
1927 serverRequest.delayedResNeeded = 0;
1929 serverRequest.coapID = requestInfo->info.messageId;
1931 CopyEndpointToDevAddr(endPoint, &serverRequest.devAddr);
1933 // copy vendor specific header options
1934 uint8_t tempNum = (requestInfo->info.numOptions);
1936 // Assume no observation requested and it is a pure GET.
1937 // If obs registration/de-registration requested it'll be fetched from the
1938 // options in GetObserveHeaderOption()
1939 serverRequest.observationOption = OC_OBSERVE_NO_OPTION;
1941 GetObserveHeaderOption(&serverRequest.observationOption, requestInfo->info.options, &tempNum);
1942 if (requestInfo->info.numOptions > MAX_HEADER_OPTIONS)
1945 "The request info numOptions is greater than MAX_HEADER_OPTIONS");
1946 SendDirectStackResponse(endPoint, requestInfo->info.messageId, CA_BAD_OPT,
1947 requestInfo->info.type, requestInfo->info.numOptions,
1948 requestInfo->info.options, requestInfo->info.token,
1949 requestInfo->info.tokenLength, requestInfo->info.resourceUri,
1951 OICFree(serverRequest.payload);
1952 OICFree(serverRequest.requestToken);
1955 serverRequest.numRcvdVendorSpecificHeaderOptions = tempNum;
1956 if (serverRequest.numRcvdVendorSpecificHeaderOptions)
1958 memcpy (&(serverRequest.rcvdVendorSpecificHeaderOptions), requestInfo->info.options,
1959 sizeof(CAHeaderOption_t)*tempNum);
1962 requestResult = HandleStackRequests (&serverRequest);
1964 // Send ACK to client as precursor to slow response
1965 if (requestResult == OC_STACK_SLOW_RESOURCE)
1967 if (requestInfo->info.type == CA_MSG_CONFIRM)
1969 SendDirectStackResponse(endPoint, requestInfo->info.messageId, CA_EMPTY,
1970 CA_MSG_ACKNOWLEDGE,0, NULL, NULL, 0, NULL,
1974 if (requestResult == OC_STACK_RESOURCE_ERROR
1975 && serverRequest.observationOption == OC_OBSERVE_REGISTER)
1977 OIC_LOG_V(ERROR, TAG, "Observe Registration failed due to resource error");
1979 else if(!OCResultToSuccess(requestResult))
1981 OIC_LOG_V(ERROR, TAG, "HandleStackRequests failed. error: %d", requestResult);
1983 CAResponseResult_t stackResponse =
1984 OCToCAStackResult(requestResult, serverRequest.method);
1986 SendDirectStackResponse(endPoint, requestInfo->info.messageId, stackResponse,
1987 requestInfo->info.type, requestInfo->info.numOptions,
1988 requestInfo->info.options, requestInfo->info.token,
1989 requestInfo->info.tokenLength, requestInfo->info.resourceUri,
1992 // requestToken is fed to HandleStackRequests, which then goes to AddServerRequest.
1993 // The token is copied in there, and is thus still owned by this function.
1994 OICFree(serverRequest.payload);
1995 OICFree(serverRequest.requestToken);
1996 OIC_LOG(INFO, TAG, "Exit OCHandleRequests");
1999 //This function will be called back by CA layer when a request is received
2000 void HandleCARequests(const CAEndpoint_t* endPoint, const CARequestInfo_t* requestInfo)
2002 OIC_LOG(INFO, TAG, "Enter HandleCARequests");
2005 OIC_LOG(ERROR, TAG, "endPoint is NULL");
2011 OIC_LOG(ERROR, TAG, "requestInfo is NULL");
2015 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
2016 #ifdef ROUTING_GATEWAY
2017 bool needRIHandling = false;
2018 bool isEmptyMsg = false;
2020 * Routing manager is going to update either of endpoint or request or both.
2021 * This typecasting is done to avoid unnecessary duplication of Endpoint and requestInfo
2022 * RM can update "routeData" option in endPoint so that future RI requests can be sent to proper
2023 * destination. It can also remove "RM" coap header option before passing request / response to
2024 * RI as this option will make no sense to either RI or application.
2026 OCStackResult ret = RMHandleRequest((CARequestInfo_t *)requestInfo, (CAEndpoint_t *)endPoint,
2027 &needRIHandling, &isEmptyMsg);
2028 if(OC_STACK_OK != ret || !needRIHandling)
2030 OIC_LOG_V(INFO, TAG, "Routing status![%d]. Not forwarding to RI", ret);
2036 * Put source in sender endpoint so that the next packet from application can be routed to
2037 * proper destination and remove RM header option.
2039 RMUpdateInfo((CAHeaderOption_t **) &(requestInfo->info.options),
2040 (uint8_t *) &(requestInfo->info.numOptions),
2041 (CAEndpoint_t *) endPoint);
2043 #ifdef ROUTING_GATEWAY
2047 * In Gateways, the MSGType in route option is used to check if the actual
2048 * response is EMPTY message(4 bytes CoAP Header). In case of Client, the
2049 * EMPTY response is sent in the form of POST request which need to be changed
2050 * to a EMPTY response by RM. This translation is done in this part of the code.
2052 OIC_LOG(INFO, TAG, "This is a Empty response from the Client");
2053 CAResponseInfo_t respInfo = {.result = CA_EMPTY,
2054 .info.messageId = requestInfo->info.messageId,
2055 .info.type = CA_MSG_ACKNOWLEDGE};
2056 OCHandleResponse(endPoint, &respInfo);
2062 // Normal handling of the packet
2063 OCHandleRequests(endPoint, requestInfo);
2065 OIC_LOG(INFO, TAG, "Exit HandleCARequests");
2068 //-----------------------------------------------------------------------------
2070 //-----------------------------------------------------------------------------
2072 OCStackResult OCSetRAInfo(const OCRAInfo_t *raInfo)
2075 !raInfo->username ||
2076 !raInfo->hostname ||
2077 !raInfo->xmpp_domain)
2080 return OC_STACK_INVALID_PARAM;
2082 OCStackResult result = CAResultToOCResult(CASetRAInfo((const CARAInfo_t *) raInfo));
2083 gRASetInfo = (result == OC_STACK_OK)? true : false;
2089 OCStackResult OCInit(const char *ipAddr, uint16_t port, OCMode mode)
2093 return OCInit1(mode, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS);
2096 OCStackResult OCInit1(OCMode mode, OCTransportFlags serverFlags, OCTransportFlags clientFlags)
2098 if(stackState == OC_STACK_INITIALIZED)
2100 OIC_LOG(INFO, TAG, "Subsequent calls to OCInit() without calling \
2101 OCStop() between them are ignored.");
2105 #ifndef ROUTING_GATEWAY
2106 if (OC_GATEWAY == mode)
2108 OIC_LOG(ERROR, TAG, "Routing Manager not supported");
2109 return OC_STACK_INVALID_PARAM;
2116 OIC_LOG(ERROR, TAG, "Need to call OCSetRAInfo before calling OCInit");
2117 return OC_STACK_ERROR;
2121 OCStackResult result = OC_STACK_ERROR;
2122 OIC_LOG(INFO, TAG, "Entering OCInit");
2125 if (!((mode == OC_CLIENT) || (mode == OC_SERVER) || (mode == OC_CLIENT_SERVER)
2126 || (mode == OC_GATEWAY)))
2128 OIC_LOG(ERROR, TAG, "Invalid mode");
2129 return OC_STACK_ERROR;
2133 if (mode == OC_CLIENT || mode == OC_CLIENT_SERVER || mode == OC_GATEWAY)
2135 caglobals.client = true;
2137 if (mode == OC_SERVER || mode == OC_CLIENT_SERVER || mode == OC_GATEWAY)
2139 caglobals.server = true;
2142 caglobals.serverFlags = (CATransportFlags_t)serverFlags;
2143 if (!(caglobals.serverFlags & CA_IPFAMILY_MASK))
2145 caglobals.serverFlags = (CATransportFlags_t)(caglobals.serverFlags|CA_IPV4|CA_IPV6);
2147 caglobals.clientFlags = (CATransportFlags_t)clientFlags;
2148 if (!(caglobals.clientFlags & CA_IPFAMILY_MASK))
2150 caglobals.clientFlags = (CATransportFlags_t)(caglobals.clientFlags|CA_IPV4|CA_IPV6);
2153 defaultDeviceHandler = NULL;
2154 defaultDeviceHandlerCallbackParameter = NULL;
2156 result = InitializeScheduleResourceList();
2157 VERIFY_SUCCESS(result, OC_STACK_OK);
2159 result = CAResultToOCResult(CAInitialize());
2160 VERIFY_SUCCESS(result, OC_STACK_OK);
2162 result = CAResultToOCResult(OCSelectNetwork());
2163 VERIFY_SUCCESS(result, OC_STACK_OK);
2165 result = CAResultToOCResult(CARegisterNetworkMonitorHandler(
2166 OCDefaultAdapterStateChangedHandler, OCDefaultConnectionStateChangedHandler));
2167 VERIFY_SUCCESS(result, OC_STACK_OK);
2169 switch (myStackMode)
2172 CARegisterHandler(HandleCARequests, HandleCAResponses, HandleCAErrorResponse);
2173 result = CAResultToOCResult(CAStartDiscoveryServer());
2174 OIC_LOG(INFO, TAG, "Client mode: CAStartDiscoveryServer");
2177 SRMRegisterHandler(HandleCARequests, HandleCAResponses, HandleCAErrorResponse);
2178 result = CAResultToOCResult(CAStartListeningServer());
2179 OIC_LOG(INFO, TAG, "Server mode: CAStartListeningServer");
2181 case OC_CLIENT_SERVER:
2183 SRMRegisterHandler(HandleCARequests, HandleCAResponses, HandleCAErrorResponse);
2184 result = CAResultToOCResult(CAStartListeningServer());
2185 if(result == OC_STACK_OK)
2187 result = CAResultToOCResult(CAStartDiscoveryServer());
2191 VERIFY_SUCCESS(result, OC_STACK_OK);
2194 CARegisterKeepAliveHandler(HandleKeepAliveConnCB);
2197 #ifdef WITH_PRESENCE
2198 PresenceTimeOutSize = sizeof (PresenceTimeOut) / sizeof (PresenceTimeOut[0]) - 1;
2199 #endif // WITH_PRESENCE
2201 //Update Stack state to initialized
2202 stackState = OC_STACK_INITIALIZED;
2204 // Initialize resource
2205 if(myStackMode != OC_CLIENT)
2207 result = initResources();
2210 // Initialize the SRM Policy Engine
2211 if(result == OC_STACK_OK)
2213 result = SRMInitPolicyEngine();
2214 // TODO after BeachHead delivery: consolidate into single SRMInit()
2216 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
2217 RMSetStackMode(mode);
2218 #ifdef ROUTING_GATEWAY
2219 if (OC_GATEWAY == myStackMode)
2221 result = RMInitialize();
2227 if (result == OC_STACK_OK)
2229 result = InitializeKeepAlive(myStackMode);
2234 if(result != OC_STACK_OK)
2236 OIC_LOG(ERROR, TAG, "Stack initialization error");
2237 deleteAllResources();
2239 TerminateScheduleResourceList();
2240 stackState = OC_STACK_UNINITIALIZED;
2245 OCStackResult OCStop()
2247 OIC_LOG(INFO, TAG, "Entering OCStop");
2249 if (stackState == OC_STACK_UNINIT_IN_PROGRESS)
2251 OIC_LOG(DEBUG, TAG, "Stack already stopping, exiting");
2254 else if (stackState != OC_STACK_INITIALIZED)
2256 OIC_LOG(ERROR, TAG, "Stack not initialized");
2257 return OC_STACK_ERROR;
2260 stackState = OC_STACK_UNINIT_IN_PROGRESS;
2262 #ifdef WITH_PRESENCE
2263 // Ensure that the TTL associated with ANY and ALL presence notifications originating from
2264 // here send with the code "OC_STACK_PRESENCE_STOPPED" result.
2265 presenceResource.presenceTTL = 0;
2266 #endif // WITH_PRESENCE
2268 #ifdef ROUTING_GATEWAY
2269 if (OC_GATEWAY == myStackMode)
2276 TerminateKeepAlive(myStackMode);
2279 // Free memory dynamically allocated for resources
2280 deleteAllResources();
2282 TerminateScheduleResourceList();
2283 // Remove all observers
2284 DeleteObserverList();
2285 // Remove all the client callbacks
2286 DeleteClientCBList();
2288 // De-init the SRM Policy Engine
2289 // TODO after BeachHead delivery: consolidate into single SRMDeInit()
2290 SRMDeInitPolicyEngine();
2293 stackState = OC_STACK_UNINITIALIZED;
2297 OCStackResult OCStartMulticastServer()
2299 if(stackState != OC_STACK_INITIALIZED)
2301 OIC_LOG(ERROR, TAG, "OCStack is not initalized. Cannot start multicast server.");
2302 return OC_STACK_ERROR;
2304 CAResult_t ret = CAStartListeningServer();
2305 if (CA_STATUS_OK != ret)
2307 OIC_LOG_V(ERROR, TAG, "Failed starting listening server: %d", ret);
2308 return OC_STACK_ERROR;
2313 OCStackResult OCStopMulticastServer()
2315 CAResult_t ret = CAStopListeningServer();
2316 if (CA_STATUS_OK != ret)
2318 OIC_LOG_V(ERROR, TAG, "Failed stopping listening server: %d", ret);
2319 return OC_STACK_ERROR;
2324 CAMessageType_t qualityOfServiceToMessageType(OCQualityOfService qos)
2329 return CA_MSG_CONFIRM;
2334 return CA_MSG_NONCONFIRM;
2339 * A request uri consists of the following components in order:
2342 * CoAP over UDP prefix "coap://"
2343 * CoAP over TCP prefix "coap+tcp://"
2344 * CoAP over DTLS prefix "coaps://"
2345 * CoAP over TLS prefix "coaps+tcp://"
2347 * IPv6 address "[1234::5678]"
2348 * IPv4 address "192.168.1.1"
2349 * optional port ":5683"
2350 * resource uri "/oc/core..."
2352 * for PRESENCE requests, extract resource type.
2354 static OCStackResult ParseRequestUri(const char *fullUri,
2355 OCTransportAdapter adapter,
2356 OCTransportFlags flags,
2357 OCDevAddr **devAddr,
2359 char **resourceType)
2361 VERIFY_NON_NULL(fullUri, FATAL, OC_STACK_INVALID_CALLBACK);
2363 OCStackResult result = OC_STACK_OK;
2364 OCDevAddr *da = NULL;
2368 // provide defaults for all returned values
2375 *resourceUri = NULL;
2379 *resourceType = NULL;
2382 // delimit url prefix, if any
2383 const char *start = fullUri;
2384 char *slash2 = strstr(start, "//");
2389 char *slash = strchr(start, '/');
2392 return OC_STACK_INVALID_URI;
2395 // process url scheme
2396 size_t prefixLen = slash2 - fullUri;
2400 if (((prefixLen == sizeof(COAP_TCP_SCHEME) - 1) && (!strncmp(fullUri, COAP_TCP_SCHEME, prefixLen)))
2401 || ((prefixLen == sizeof(COAPS_TCP_SCHEME) - 1) && (!strncmp(fullUri, COAPS_TCP_SCHEME, prefixLen))))
2407 // TODO: this logic should come in with unit tests exercising the various strings
2408 // processs url prefix, if any
2409 size_t urlLen = slash - start;
2413 if (urlLen && devAddr)
2414 { // construct OCDevAddr
2415 if (start[0] == '[')
2417 char *close = strchr(++start, ']');
2418 if (!close || close > slash)
2420 return OC_STACK_INVALID_URI;
2423 if (close[1] == ':')
2430 adapter = (OCTransportAdapter)(adapter | OC_ADAPTER_TCP);
2434 adapter = (OCTransportAdapter)(adapter | OC_ADAPTER_IP);
2436 flags = (OCTransportFlags)(flags | OC_IP_USE_V6);
2440 char *dot = strchr(start, '.');
2441 if (dot && dot < slash)
2443 colon = strchr(start, ':');
2444 end = (colon && colon < slash) ? colon : slash;
2449 adapter = (OCTransportAdapter)(adapter | OC_ADAPTER_TCP);
2453 adapter = (OCTransportAdapter)(adapter | OC_ADAPTER_IP);
2455 flags = (OCTransportFlags)(flags | OC_IP_USE_V4);
2463 if (len >= sizeof(da->addr))
2465 return OC_STACK_INVALID_URI;
2467 // collect port, if any
2468 if (colon && colon < slash)
2470 for (colon++; colon < slash; colon++)
2473 if (c < '0' || c > '9')
2475 return OC_STACK_INVALID_URI;
2477 port = 10 * port + c - '0';
2482 if (len >= sizeof(da->addr))
2484 return OC_STACK_INVALID_URI;
2487 da = (OCDevAddr *)OICCalloc(sizeof (OCDevAddr), 1);
2490 return OC_STACK_NO_MEMORY;
2493 // Decode address per RFC 6874.
2494 result = OCDecodeAddressForRFC6874(da->addr, sizeof(da->addr), start, end);
2495 if (result != OC_STACK_OK)
2502 da->adapter = adapter;
2504 if (!strncmp(fullUri, "coaps", 5))
2506 da->flags = (OCTransportFlags)(da->flags|CA_SECURE);
2511 // process resource uri, if any
2513 { // request uri and query
2514 size_t ulen = strlen(slash); // resource uri length
2515 size_t tlen = 0; // resource type length
2518 static const char strPresence[] = "/oic/ad?rt=";
2519 static const size_t lenPresence = sizeof(strPresence) - 1;
2520 if (!strncmp(slash, strPresence, lenPresence))
2522 type = slash + lenPresence;
2523 tlen = ulen - lenPresence;
2528 *resourceUri = (char *)OICMalloc(ulen + 1);
2531 result = OC_STACK_NO_MEMORY;
2534 strcpy(*resourceUri, slash);
2537 if (type && resourceType)
2539 *resourceType = (char *)OICMalloc(tlen + 1);
2542 result = OC_STACK_NO_MEMORY;
2546 OICStrcpy(*resourceType, (tlen+1), type);
2553 // free all returned values
2560 OICFree(*resourceUri);
2564 OICFree(*resourceType);
2569 static OCStackResult OCPreparePresence(CAEndpoint_t *endpoint,
2573 char uri[CA_MAX_URI_LENGTH];
2575 FormCanonicalPresenceUri(endpoint, uri, isMulticast);
2577 *requestUri = OICStrdup(uri);
2580 return OC_STACK_NO_MEMORY;
2587 * Discover or Perform requests on a specified resource
2589 OCStackResult OCDoResource(OCDoHandle *handle,
2591 const char *requestUri,
2592 const OCDevAddr *destination,
2594 OCConnectivityType connectivityType,
2595 OCQualityOfService qos,
2596 OCCallbackData *cbData,
2597 OCHeaderOption *options,
2600 OIC_LOG(INFO, TAG, "Entering OCDoResource");
2602 // Validate input parameters
2603 VERIFY_NON_NULL(cbData, FATAL, OC_STACK_INVALID_CALLBACK);
2604 VERIFY_NON_NULL(cbData->cb, FATAL, OC_STACK_INVALID_CALLBACK);
2606 OCStackResult result = OC_STACK_ERROR;
2607 CAResult_t caResult;
2608 CAToken_t token = NULL;
2609 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
2610 ClientCB *clientCB = NULL;
2611 OCDoHandle resHandle = NULL;
2612 CAEndpoint_t endpoint = {.adapter = CA_DEFAULT_ADAPTER};
2613 OCDevAddr tmpDevAddr = { OC_DEFAULT_ADAPTER };
2615 OCTransportAdapter adapter;
2616 OCTransportFlags flags;
2617 // the request contents are put here
2618 CARequestInfo_t requestInfo = {.method = CA_GET};
2619 // requestUri will be parsed into the following three variables
2620 OCDevAddr *devAddr = NULL;
2621 char *resourceUri = NULL;
2622 char *resourceType = NULL;
2625 * Support original behavior with address on resourceUri argument.
2627 adapter = (OCTransportAdapter)(connectivityType >> CT_ADAPTER_SHIFT);
2628 flags = (OCTransportFlags)(connectivityType & CT_MASK_FLAGS);
2632 result = ParseRequestUri(requestUri, adapter, flags, &devAddr, &resourceUri, &resourceType);
2633 if (result != OC_STACK_OK)
2635 OIC_LOG_V(DEBUG, TAG, "Unable to parse uri: %s", requestUri);
2639 else if (!checkProxyUri(options, numOptions))
2641 OIC_LOG(ERROR, TAG, "Request doesn't contain RequestURI/Proxy URI");
2648 case OC_REST_OBSERVE:
2649 case OC_REST_OBSERVE_ALL:
2650 requestInfo.method = CA_GET;
2653 requestInfo.method = CA_PUT;
2656 requestInfo.method = CA_POST;
2658 case OC_REST_DELETE:
2659 requestInfo.method = CA_DELETE;
2661 case OC_REST_DISCOVER:
2663 #ifdef WITH_PRESENCE
2664 case OC_REST_PRESENCE:
2666 if (destination || devAddr)
2668 requestInfo.isMulticast = false;
2672 tmpDevAddr.adapter = adapter;
2673 tmpDevAddr.flags = flags;
2674 destination = &tmpDevAddr;
2675 requestInfo.isMulticast = true;
2677 // OC_REST_DISCOVER: CA_DISCOVER will become GET and isMulticast.
2678 // OC_REST_PRESENCE: Since "presence" is a stack layer only implementation.
2679 // replacing method type with GET.
2680 requestInfo.method = CA_GET;
2683 result = OC_STACK_INVALID_METHOD;
2687 if (!devAddr && !destination)
2689 OIC_LOG(DEBUG, TAG, "no devAddr and no destination");
2690 result = OC_STACK_INVALID_PARAM;
2694 /* If not original behavior, use destination argument */
2695 if (destination && !devAddr)
2697 devAddr = (OCDevAddr *)OICMalloc(sizeof (OCDevAddr));
2700 result = OC_STACK_NO_MEMORY;
2703 *devAddr = *destination;
2706 resHandle = GenerateInvocationHandle();
2709 result = OC_STACK_NO_MEMORY;
2713 caResult = CAGenerateToken(&token, tokenLength);
2714 if (caResult != CA_STATUS_OK)
2716 OIC_LOG(ERROR, TAG, "CAGenerateToken error");
2717 result= OC_STACK_ERROR;
2721 // fill in request data
2722 requestInfo.info.type = qualityOfServiceToMessageType(qos);
2723 requestInfo.info.token = token;
2724 requestInfo.info.tokenLength = tokenLength;
2726 if ((method == OC_REST_OBSERVE) || (method == OC_REST_OBSERVE_ALL))
2728 result = CreateObserveHeaderOption (&(requestInfo.info.options),
2729 options, numOptions, OC_OBSERVE_REGISTER);
2730 if (result != OC_STACK_OK)
2734 requestInfo.info.numOptions = numOptions + 1;
2738 requestInfo.info.numOptions = numOptions;
2739 requestInfo.info.options =
2740 (CAHeaderOption_t*) OICCalloc(numOptions, sizeof(CAHeaderOption_t));
2741 memcpy(requestInfo.info.options, (CAHeaderOption_t*)options,
2742 numOptions * sizeof(CAHeaderOption_t));
2745 CopyDevAddrToEndpoint(devAddr, &endpoint);
2750 OCConvertPayload(payload, &requestInfo.info.payload, &requestInfo.info.payloadSize))
2753 OIC_LOG(ERROR, TAG, "Failed to create CBOR Payload");
2756 requestInfo.info.payloadFormat = CA_FORMAT_APPLICATION_CBOR;
2760 requestInfo.info.payload = NULL;
2761 requestInfo.info.payloadSize = 0;
2762 requestInfo.info.payloadFormat = CA_FORMAT_UNDEFINED;
2765 // prepare for response
2766 #ifdef WITH_PRESENCE
2767 if (method == OC_REST_PRESENCE)
2769 char *presenceUri = NULL;
2770 result = OCPreparePresence(&endpoint, &presenceUri,
2771 requestInfo.isMulticast);
2772 if (OC_STACK_OK != result)
2777 // Assign full presence uri as coap://ip:port/oic/ad to add to callback list.
2778 // Presence notification will form a canonical uri to
2779 // look for callbacks into the application.
2782 OICFree(resourceUri);
2784 resourceUri = presenceUri;
2788 // update resourceUri onto requestInfo after check presence uri
2789 requestInfo.info.resourceUri = resourceUri;
2791 ttl = GetTicks(MAX_CB_TIMEOUT_SECONDS * MILLISECONDS_PER_SECOND);
2792 result = AddClientCB(&clientCB, cbData, token, tokenLength, &resHandle,
2793 method, devAddr, resourceUri, resourceType, ttl);
2794 if (OC_STACK_OK != result)
2799 devAddr = NULL; // Client CB list entry now owns it
2800 resourceUri = NULL; // Client CB list entry now owns it
2801 resourceType = NULL; // Client CB list entry now owns it
2803 #ifdef WITH_PRESENCE
2804 if (method == OC_REST_PRESENCE)
2806 OIC_LOG(ERROR, TAG, "AddClientCB for presence done.");
2810 *handle = resHandle;
2818 result = OCSendRequest(&endpoint, &requestInfo);
2819 if (OC_STACK_OK != result)
2826 *handle = resHandle;
2830 if (result != OC_STACK_OK)
2832 OIC_LOG(ERROR, TAG, "OCDoResource error");
2833 FindAndDeleteClientCB(clientCB);
2834 CADestroyToken(token);
2842 // This is the owner of the payload object, so we free it
2843 OCPayloadDestroy(payload);
2844 OICFree(requestInfo.info.payload);
2846 OICFree(resourceUri);
2847 OICFree(resourceType);
2848 OICFree(requestInfo.info.options);
2852 OCStackResult OCCancel(OCDoHandle handle, OCQualityOfService qos, OCHeaderOption * options,
2856 * This ftn is implemented one of two ways in the case of observation:
2858 * 1. qos == OC_NON_CONFIRMABLE. When observe is unobserved..
2859 * Remove the callback associated on client side.
2860 * When the next notification comes in from server,
2861 * reply with RESET message to server.
2862 * Keep in mind that the server will react to RESET only
2863 * if the last notification was sent as CON
2865 * 2. qos == OC_CONFIRMABLE. When OCCancel is called,
2866 * and it is associated with an observe request
2867 * (i.e. ClientCB->method == OC_REST_OBSERVE || OC_REST_OBSERVE_ALL),
2868 * Send CON Observe request to server with
2869 * observe flag = OC_RESOURCE_OBSERVE_DEREGISTER.
2870 * Remove the callback associated on client side.
2872 OCStackResult ret = OC_STACK_OK;
2873 CAEndpoint_t endpoint = {.adapter = CA_DEFAULT_ADAPTER};
2874 CARequestInfo_t requestInfo = {.method = CA_GET};
2878 return OC_STACK_INVALID_PARAM;
2881 ClientCB *clientCB = GetClientCB(NULL, 0, handle, NULL);
2884 OIC_LOG(ERROR, TAG, "Callback not found. Called OCCancel on same resource twice?");
2885 return OC_STACK_ERROR;
2888 switch (clientCB->method)
2890 case OC_REST_OBSERVE:
2891 case OC_REST_OBSERVE_ALL:
2893 OIC_LOG_V(INFO, TAG, "Canceling observation for resource %s", clientCB->requestUri);
2895 CopyDevAddrToEndpoint(clientCB->devAddr, &endpoint);
2897 if ((endpoint.adapter & CA_ADAPTER_IP) && qos != OC_HIGH_QOS)
2899 FindAndDeleteClientCB(clientCB);
2903 OIC_LOG(INFO, TAG, "Cancelling observation as CONFIRMABLE");
2905 requestInfo.info.type = qualityOfServiceToMessageType(qos);
2906 requestInfo.info.token = clientCB->token;
2907 requestInfo.info.tokenLength = clientCB->tokenLength;
2909 if (CreateObserveHeaderOption (&(requestInfo.info.options),
2910 options, numOptions, OC_OBSERVE_DEREGISTER) != OC_STACK_OK)
2912 return OC_STACK_ERROR;
2914 requestInfo.info.numOptions = numOptions + 1;
2915 requestInfo.info.resourceUri = OICStrdup (clientCB->requestUri);
2918 ret = OCSendRequest(&endpoint, &requestInfo);
2920 if (requestInfo.info.options)
2922 OICFree (requestInfo.info.options);
2924 if (requestInfo.info.resourceUri)
2926 OICFree (requestInfo.info.resourceUri);
2931 case OC_REST_DISCOVER:
2932 OIC_LOG_V(INFO, TAG, "Cancelling discovery callback for resource %s",
2933 clientCB->requestUri);
2934 FindAndDeleteClientCB(clientCB);
2937 #ifdef WITH_PRESENCE
2938 case OC_REST_PRESENCE:
2939 FindAndDeleteClientCB(clientCB);
2944 ret = OC_STACK_INVALID_METHOD;
2952 * @brief Register Persistent storage callback.
2953 * @param persistentStorageHandler [IN] Pointers to open, read, write, close & unlink handlers.
2955 * OC_STACK_OK - No errors; Success
2956 * OC_STACK_INVALID_PARAM - Invalid parameter
2958 OCStackResult OCRegisterPersistentStorageHandler(OCPersistentStorage* persistentStorageHandler)
2960 OIC_LOG(INFO, TAG, "RegisterPersistentStorageHandler !!");
2961 if(!persistentStorageHandler)
2963 OIC_LOG(ERROR, TAG, "The persistent storage handler is invalid");
2964 return OC_STACK_INVALID_PARAM;
2968 if( !persistentStorageHandler->open ||
2969 !persistentStorageHandler->close ||
2970 !persistentStorageHandler->read ||
2971 !persistentStorageHandler->unlink ||
2972 !persistentStorageHandler->write)
2974 OIC_LOG(ERROR, TAG, "The persistent storage handler is invalid");
2975 return OC_STACK_INVALID_PARAM;
2978 return SRMRegisterPersistentStorageHandler(persistentStorageHandler);
2981 #ifdef WITH_PRESENCE
2983 OCStackResult OCProcessPresence()
2985 OCStackResult result = OC_STACK_OK;
2987 // the following line floods the log with messages that are irrelevant
2988 // to most purposes. Uncomment as needed.
2989 //OIC_LOG(INFO, TAG, "Entering RequestPresence");
2990 ClientCB* cbNode = NULL;
2991 OCClientResponse clientResponse;
2992 OCStackApplicationResult cbResult = OC_STACK_DELETE_TRANSACTION;
2994 LL_FOREACH(cbList, cbNode)
2996 if (OC_REST_PRESENCE != cbNode->method || !cbNode->presence)
3001 uint32_t now = GetTicks(0);
3002 OIC_LOG_V(DEBUG, TAG, "this TTL level %d",
3003 cbNode->presence->TTLlevel);
3004 OIC_LOG_V(DEBUG, TAG, "current ticks %d", now);
3006 if (cbNode->presence->TTLlevel > PresenceTimeOutSize)
3011 if (cbNode->presence->TTLlevel < PresenceTimeOutSize)
3013 OIC_LOG_V(DEBUG, TAG, "timeout ticks %d",
3014 cbNode->presence->timeOut[cbNode->presence->TTLlevel]);
3016 if (cbNode->presence->TTLlevel >= PresenceTimeOutSize)
3018 OIC_LOG(DEBUG, TAG, "No more timeout ticks");
3020 clientResponse.sequenceNumber = 0;
3021 clientResponse.result = OC_STACK_PRESENCE_TIMEOUT;
3022 clientResponse.devAddr = *cbNode->devAddr;
3023 FixUpClientResponse(&clientResponse);
3024 clientResponse.payload = NULL;
3026 // Increment the TTLLevel (going to a next state), so we don't keep
3027 // sending presence notification to client.
3028 cbNode->presence->TTLlevel++;
3029 OIC_LOG_V(DEBUG, TAG, "moving to TTL level %d",
3030 cbNode->presence->TTLlevel);
3032 cbResult = cbNode->callBack(cbNode->context, cbNode->handle, &clientResponse);
3033 if (cbResult == OC_STACK_DELETE_TRANSACTION)
3035 FindAndDeleteClientCB(cbNode);
3039 if (now < cbNode->presence->timeOut[cbNode->presence->TTLlevel])
3044 CAEndpoint_t endpoint = {.adapter = CA_DEFAULT_ADAPTER};
3045 CAInfo_t requestData = {.type = CA_MSG_CONFIRM};
3046 CARequestInfo_t requestInfo = {.method = CA_GET};
3048 OIC_LOG(DEBUG, TAG, "time to test server presence");
3050 CopyDevAddrToEndpoint(cbNode->devAddr, &endpoint);
3052 requestData.type = CA_MSG_NONCONFIRM;
3053 requestData.token = cbNode->token;
3054 requestData.tokenLength = cbNode->tokenLength;
3055 requestData.resourceUri = OC_RSRVD_PRESENCE_URI;
3056 requestInfo.method = CA_GET;
3057 requestInfo.info = requestData;
3059 result = OCSendRequest(&endpoint, &requestInfo);
3060 if (OC_STACK_OK != result)
3065 cbNode->presence->TTLlevel++;
3066 OIC_LOG_V(DEBUG, TAG, "moving to TTL level %d", cbNode->presence->TTLlevel);
3069 if (result != OC_STACK_OK)
3071 OIC_LOG(ERROR, TAG, "OCProcessPresence error");
3076 #endif // WITH_PRESENCE
3078 OCStackResult OCProcess()
3080 #ifdef WITH_PRESENCE
3081 OCProcessPresence();
3083 CAHandleRequestResponse();
3085 #ifdef ROUTING_GATEWAY
3095 #ifdef WITH_PRESENCE
3096 OCStackResult OCStartPresence(const uint32_t ttl)
3098 OIC_LOG(INFO, TAG, "Entering OCStartPresence");
3099 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
3100 OCChangeResourceProperty(
3101 &(((OCResource *)presenceResource.handle)->resourceProperties),
3104 if (OC_MAX_PRESENCE_TTL_SECONDS < ttl)
3106 presenceResource.presenceTTL = OC_MAX_PRESENCE_TTL_SECONDS;
3107 OIC_LOG(INFO, TAG, "Setting Presence TTL to max value");
3111 presenceResource.presenceTTL = OC_DEFAULT_PRESENCE_TTL_SECONDS;
3112 OIC_LOG(INFO, TAG, "Setting Presence TTL to default value");
3116 presenceResource.presenceTTL = ttl;
3118 OIC_LOG_V(DEBUG, TAG, "Presence TTL is %" PRIu32 " seconds", presenceResource.presenceTTL);
3120 if (OC_PRESENCE_UNINITIALIZED == presenceState)
3122 presenceState = OC_PRESENCE_INITIALIZED;
3124 OCDevAddr devAddr = { OC_DEFAULT_ADAPTER };
3126 CAToken_t caToken = NULL;
3127 CAResult_t caResult = CAGenerateToken(&caToken, tokenLength);
3128 if (caResult != CA_STATUS_OK)
3130 OIC_LOG(ERROR, TAG, "CAGenerateToken error");
3131 CADestroyToken(caToken);
3132 return OC_STACK_ERROR;
3135 AddObserver(OC_RSRVD_PRESENCE_URI, NULL, 0, caToken, tokenLength,
3136 (OCResource *)presenceResource.handle, OC_LOW_QOS, OC_FORMAT_UNDEFINED, &devAddr);
3137 CADestroyToken(caToken);
3140 // Each time OCStartPresence is called
3141 // a different random 32-bit integer number is used
3142 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
3144 return SendPresenceNotification(((OCResource *)presenceResource.handle)->rsrcType,
3145 OC_PRESENCE_TRIGGER_CREATE);
3148 OCStackResult OCStopPresence()
3150 OIC_LOG(INFO, TAG, "Entering OCStopPresence");
3151 OCStackResult result = OC_STACK_ERROR;
3153 if(presenceResource.handle)
3155 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
3157 // make resource inactive
3158 result = OCChangeResourceProperty(
3159 &(((OCResource *) presenceResource.handle)->resourceProperties),
3163 if(result != OC_STACK_OK)
3166 "Changing the presence resource properties to ACTIVE not successful");
3170 return SendStopNotification();
3174 OCStackResult OCSetDefaultDeviceEntityHandler(OCDeviceEntityHandler entityHandler,
3175 void* callbackParameter)
3177 defaultDeviceHandler = entityHandler;
3178 defaultDeviceHandlerCallbackParameter = callbackParameter;
3183 OCStackResult OCCreateResource(OCResourceHandle *handle,
3184 const char *resourceTypeName,
3185 const char *resourceInterfaceName,
3186 const char *uri, OCEntityHandler entityHandler,
3187 void* callbackParam,
3188 uint8_t resourceProperties)
3191 OCResource *pointer = NULL;
3192 OCStackResult result = OC_STACK_ERROR;
3194 OIC_LOG(INFO, TAG, "Entering OCCreateResource");
3196 if(myStackMode == OC_CLIENT)
3198 return OC_STACK_INVALID_PARAM;
3200 // Validate parameters
3201 if(!uri || uri[0]=='\0' || strlen(uri)>=MAX_URI_LENGTH )
3203 OIC_LOG(ERROR, TAG, "URI is empty or too long");
3204 return OC_STACK_INVALID_URI;
3206 // Is it presented during resource discovery?
3207 if (!handle || !resourceTypeName || resourceTypeName[0] == '\0' )
3209 OIC_LOG(ERROR, TAG, "Input parameter is NULL");
3210 return OC_STACK_INVALID_PARAM;
3213 if (!resourceInterfaceName || strlen(resourceInterfaceName) == 0)
3215 resourceInterfaceName = OC_RSRVD_INTERFACE_DEFAULT;
3219 resourceProperties = resourceProperties | OC_MQ_PUBLISHER;
3221 // Make sure resourceProperties bitmask has allowed properties specified
3222 if (resourceProperties
3223 > (OC_ACTIVE | OC_DISCOVERABLE | OC_OBSERVABLE | OC_SLOW | OC_SECURE |
3224 OC_EXPLICIT_DISCOVERABLE
3233 OIC_LOG(ERROR, TAG, "Invalid property");
3234 return OC_STACK_INVALID_PARAM;
3237 // If the headResource is NULL, then no resources have been created...
3238 pointer = headResource;
3241 // At least one resources is in the resource list, so we need to search for
3242 // repeated URLs, which are not allowed. If a repeat is found, exit with an error
3245 if (strncmp(uri, pointer->uri, MAX_URI_LENGTH) == 0)
3247 OIC_LOG_V(ERROR, TAG, "Resource %s already exists", uri);
3248 return OC_STACK_INVALID_PARAM;
3250 pointer = pointer->next;
3253 // Create the pointer and insert it into the resource list
3254 pointer = (OCResource *) OICCalloc(1, sizeof(OCResource));
3257 result = OC_STACK_NO_MEMORY;
3260 pointer->sequenceNum = OC_OFFSET_SEQUENCE_NUMBER;
3262 insertResource(pointer);
3265 pointer->uri = OICStrdup(uri);
3268 result = OC_STACK_NO_MEMORY;
3272 // Set properties. Set OC_ACTIVE
3273 pointer->resourceProperties = (OCResourceProperty) (resourceProperties
3276 // Add the resourcetype to the resource
3277 result = BindResourceTypeToResource(pointer, resourceTypeName);
3278 if (result != OC_STACK_OK)
3280 OIC_LOG(ERROR, TAG, "Error adding resourcetype");
3284 // Add the resourceinterface to the resource
3285 result = BindResourceInterfaceToResource(pointer, resourceInterfaceName);
3286 if (result != OC_STACK_OK)
3288 OIC_LOG(ERROR, TAG, "Error adding resourceinterface");
3292 // If an entity handler has been passed, attach it to the newly created
3293 // resource. Otherwise, set the default entity handler.
3296 pointer->entityHandler = entityHandler;
3297 pointer->entityHandlerCallbackParam = callbackParam;
3301 pointer->entityHandler = defaultResourceEHandler;
3302 pointer->entityHandlerCallbackParam = NULL;
3305 // Initialize a pointer indicating child resources in case of collection
3306 pointer->rsrcChildResourcesHead = NULL;
3309 result = OC_STACK_OK;
3311 #ifdef WITH_PRESENCE
3312 if (presenceResource.handle)
3314 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
3315 SendPresenceNotification(pointer->rsrcType, OC_PRESENCE_TRIGGER_CREATE);
3319 if (result != OC_STACK_OK)
3321 // Deep delete of resource and other dynamic elements that it contains
3322 deleteResource(pointer);
3327 OCStackResult OCBindResource(
3328 OCResourceHandle collectionHandle, OCResourceHandle resourceHandle)
3330 OCResource *resource = NULL;
3331 OCChildResource *tempChildResource = NULL;
3332 OCChildResource *newChildResource = NULL;
3334 OIC_LOG(INFO, TAG, "Entering OCBindResource");
3336 // Validate parameters
3337 VERIFY_NON_NULL(collectionHandle, ERROR, OC_STACK_ERROR);
3338 VERIFY_NON_NULL(resourceHandle, ERROR, OC_STACK_ERROR);
3339 // Container cannot contain itself
3340 if (collectionHandle == resourceHandle)
3342 OIC_LOG(ERROR, TAG, "Added handle equals collection handle");
3343 return OC_STACK_INVALID_PARAM;
3346 // Use the handle to find the resource in the resource linked list
3347 resource = findResource((OCResource *) collectionHandle);
3350 OIC_LOG(ERROR, TAG, "Collection handle not found");
3351 return OC_STACK_INVALID_PARAM;
3354 // Look for an open slot to add add the child resource.
3355 // If found, add it and return success
3357 tempChildResource = resource->rsrcChildResourcesHead;
3359 while(resource->rsrcChildResourcesHead && tempChildResource->next)
3361 // TODO: what if one of child resource was deregistered without unbinding?
3362 tempChildResource = tempChildResource->next;
3365 // Do memory allocation for child resource
3366 newChildResource = (OCChildResource *) OICCalloc(1, sizeof(OCChildResource));
3367 if(!newChildResource)
3369 OIC_LOG(ERROR, TAG, "Adding new child resource is failed due to memory allocation failure");
3370 return OC_STACK_ERROR;
3373 newChildResource->rsrcResource = (OCResource *) resourceHandle;
3374 newChildResource->next = NULL;
3376 if(!resource->rsrcChildResourcesHead)
3378 resource->rsrcChildResourcesHead = newChildResource;
3381 tempChildResource->next = newChildResource;
3384 OIC_LOG(INFO, TAG, "resource bound");
3386 #ifdef WITH_PRESENCE
3387 if (presenceResource.handle)
3389 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
3390 SendPresenceNotification(((OCResource *) resourceHandle)->rsrcType,
3391 OC_PRESENCE_TRIGGER_CHANGE);
3398 OCStackResult OCUnBindResource(
3399 OCResourceHandle collectionHandle, OCResourceHandle resourceHandle)
3401 OCResource *resource = NULL;
3402 OCChildResource *tempChildResource = NULL;
3403 OCChildResource *tempLastChildResource = NULL;
3405 OIC_LOG(INFO, TAG, "Entering OCUnBindResource");
3407 // Validate parameters
3408 VERIFY_NON_NULL(collectionHandle, ERROR, OC_STACK_ERROR);
3409 VERIFY_NON_NULL(resourceHandle, ERROR, OC_STACK_ERROR);
3410 // Container cannot contain itself
3411 if (collectionHandle == resourceHandle)
3413 OIC_LOG(ERROR, TAG, "removing handle equals collection handle");
3414 return OC_STACK_INVALID_PARAM;
3417 // Use the handle to find the resource in the resource linked list
3418 resource = findResource((OCResource *) collectionHandle);
3421 OIC_LOG(ERROR, TAG, "Collection handle not found");
3422 return OC_STACK_INVALID_PARAM;
3425 // Look for an open slot to add add the child resource.
3426 // If found, add it and return success
3427 if(!resource->rsrcChildResourcesHead)
3429 OIC_LOG(INFO, TAG, "resource not found in collection");
3431 // Unable to add resourceHandle, so return error
3432 return OC_STACK_ERROR;
3436 tempChildResource = resource->rsrcChildResourcesHead;
3438 while (tempChildResource)
3440 if(tempChildResource->rsrcResource == resourceHandle)
3442 // if resource going to be unbinded is the head one.
3443 if( tempChildResource == resource->rsrcChildResourcesHead )
3445 OCChildResource *temp = resource->rsrcChildResourcesHead->next;
3446 OICFree(resource->rsrcChildResourcesHead);
3447 resource->rsrcChildResourcesHead = temp;
3452 OCChildResource *temp = tempChildResource->next;
3453 OICFree(tempChildResource);
3454 tempLastChildResource->next = temp;
3458 OIC_LOG(INFO, TAG, "resource unbound");
3460 // Send notification when resource is unbounded successfully.
3461 #ifdef WITH_PRESENCE
3462 if (presenceResource.handle)
3464 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
3465 SendPresenceNotification(((OCResource *) resourceHandle)->rsrcType,
3466 OC_PRESENCE_TRIGGER_CHANGE);
3469 tempChildResource = NULL;
3470 tempLastChildResource = NULL;
3476 tempLastChildResource = tempChildResource;
3477 tempChildResource = tempChildResource->next;
3480 OIC_LOG(INFO, TAG, "resource not found in collection");
3482 tempChildResource = NULL;
3483 tempLastChildResource = NULL;
3485 // Unable to add resourceHandle, so return error
3486 return OC_STACK_ERROR;
3489 static bool ValidateResourceTypeInterface(const char *resourceItemName)
3491 if (!resourceItemName)
3495 // Per RFC 6690 only registered values must follow the first rule below.
3496 // At this point in time the only values registered begin with "core", and
3497 // all other values are specified as opaque strings where multiple values
3498 // are separated by a space.
3499 if (strncmp(resourceItemName, CORESPEC, sizeof(CORESPEC) - 1) == 0)
3501 for(size_t index = sizeof(CORESPEC) - 1; resourceItemName[index]; ++index)
3503 if (resourceItemName[index] != '.'
3504 && resourceItemName[index] != '-'
3505 && (resourceItemName[index] < 'a' || resourceItemName[index] > 'z')
3506 && (resourceItemName[index] < '0' || resourceItemName[index] > '9'))
3514 for (size_t index = 0; resourceItemName[index]; ++index)
3516 if (resourceItemName[index] == ' '
3517 || resourceItemName[index] == '\t'
3518 || resourceItemName[index] == '\r'
3519 || resourceItemName[index] == '\n')
3529 OCStackResult BindResourceTypeToResource(OCResource* resource,
3530 const char *resourceTypeName)
3532 OCResourceType *pointer = NULL;
3534 OCStackResult result = OC_STACK_ERROR;
3536 VERIFY_NON_NULL(resourceTypeName, ERROR, OC_STACK_INVALID_PARAM);
3538 if (!ValidateResourceTypeInterface(resourceTypeName))
3540 OIC_LOG(ERROR, TAG, "resource type illegal (see RFC 6690)");
3541 return OC_STACK_INVALID_PARAM;
3544 pointer = (OCResourceType *) OICCalloc(1, sizeof(OCResourceType));
3547 result = OC_STACK_NO_MEMORY;
3551 str = OICStrdup(resourceTypeName);
3554 result = OC_STACK_NO_MEMORY;
3557 pointer->resourcetypename = str;
3558 pointer->next = NULL;
3560 insertResourceType(resource, pointer);
3561 result = OC_STACK_OK;
3564 if (result != OC_STACK_OK)
3573 OCStackResult BindResourceInterfaceToResource(OCResource* resource,
3574 const char *resourceInterfaceName)
3576 OCResourceInterface *pointer = NULL;
3578 OCStackResult result = OC_STACK_ERROR;
3580 VERIFY_NON_NULL(resourceInterfaceName, ERROR, OC_STACK_INVALID_PARAM);
3582 if (!ValidateResourceTypeInterface(resourceInterfaceName))
3584 OIC_LOG(ERROR, TAG, "resource /interface illegal (see RFC 6690)");
3585 return OC_STACK_INVALID_PARAM;
3588 OIC_LOG_V(INFO, TAG, "Binding %s interface to %s", resourceInterfaceName, resource->uri);
3590 pointer = (OCResourceInterface *) OICCalloc(1, sizeof(OCResourceInterface));
3593 result = OC_STACK_NO_MEMORY;
3597 str = OICStrdup(resourceInterfaceName);
3600 result = OC_STACK_NO_MEMORY;
3603 pointer->name = str;
3605 // Bind the resourceinterface to the resource
3606 insertResourceInterface(resource, pointer);
3608 result = OC_STACK_OK;
3611 if (result != OC_STACK_OK)
3620 OCStackResult OCBindResourceTypeToResource(OCResourceHandle handle,
3621 const char *resourceTypeName)
3624 OCStackResult result = OC_STACK_ERROR;
3625 OCResource *resource = NULL;
3627 resource = findResource((OCResource *) handle);
3630 OIC_LOG(ERROR, TAG, "Resource not found");
3631 return OC_STACK_ERROR;
3634 result = BindResourceTypeToResource(resource, resourceTypeName);
3636 #ifdef WITH_PRESENCE
3637 if(presenceResource.handle)
3639 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
3640 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
3647 OCStackResult OCBindResourceInterfaceToResource(OCResourceHandle handle,
3648 const char *resourceInterfaceName)
3651 OCStackResult result = OC_STACK_ERROR;
3652 OCResource *resource = NULL;
3654 resource = findResource((OCResource *) handle);
3657 OIC_LOG(ERROR, TAG, "Resource not found");
3658 return OC_STACK_ERROR;
3661 result = BindResourceInterfaceToResource(resource, resourceInterfaceName);
3663 #ifdef WITH_PRESENCE
3664 if (presenceResource.handle)
3666 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
3667 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
3674 OCStackResult OCGetNumberOfResources(uint8_t *numResources)
3676 OCResource *pointer = headResource;
3678 VERIFY_NON_NULL(numResources, ERROR, OC_STACK_INVALID_PARAM);
3682 *numResources = *numResources + 1;
3683 pointer = pointer->next;
3688 OCResourceHandle OCGetResourceHandle(uint8_t index)
3690 OCResource *pointer = headResource;
3692 for( uint8_t i = 0; i < index && pointer; ++i)
3694 pointer = pointer->next;
3696 return (OCResourceHandle) pointer;
3699 OCStackResult OCDeleteResource(OCResourceHandle handle)
3703 OIC_LOG(ERROR, TAG, "Invalid handle for deletion");
3704 return OC_STACK_INVALID_PARAM;
3707 OCResource *resource = findResource((OCResource *) handle);
3708 if (resource == NULL)
3710 OIC_LOG(ERROR, TAG, "Resource not found");
3711 return OC_STACK_NO_RESOURCE;
3714 if (deleteResource((OCResource *) handle) != OC_STACK_OK)
3716 OIC_LOG(ERROR, TAG, "Error deleting resource");
3717 return OC_STACK_ERROR;
3723 const char *OCGetResourceUri(OCResourceHandle handle)
3725 OCResource *resource = NULL;
3727 resource = findResource((OCResource *) handle);
3730 return resource->uri;
3732 return (const char *) NULL;
3735 OCResourceProperty OCGetResourceProperties(OCResourceHandle handle)
3737 OCResource *resource = NULL;
3739 resource = findResource((OCResource *) handle);
3742 return resource->resourceProperties;
3744 return (OCResourceProperty)-1;
3747 OCStackResult OCGetNumberOfResourceTypes(OCResourceHandle handle,
3748 uint8_t *numResourceTypes)
3750 OCResource *resource = NULL;
3751 OCResourceType *pointer = NULL;
3753 VERIFY_NON_NULL(numResourceTypes, ERROR, OC_STACK_INVALID_PARAM);
3754 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
3756 *numResourceTypes = 0;
3758 resource = findResource((OCResource *) handle);
3761 pointer = resource->rsrcType;
3764 *numResourceTypes = *numResourceTypes + 1;
3765 pointer = pointer->next;
3771 const char *OCGetResourceTypeName(OCResourceHandle handle, uint8_t index)
3773 OCResourceType *resourceType = NULL;
3775 resourceType = findResourceTypeAtIndex(handle, index);
3778 return resourceType->resourcetypename;
3780 return (const char *) NULL;
3783 OCStackResult OCGetNumberOfResourceInterfaces(OCResourceHandle handle,
3784 uint8_t *numResourceInterfaces)
3786 OCResourceInterface *pointer = NULL;
3787 OCResource *resource = NULL;
3789 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
3790 VERIFY_NON_NULL(numResourceInterfaces, ERROR, OC_STACK_INVALID_PARAM);
3792 *numResourceInterfaces = 0;
3793 resource = findResource((OCResource *) handle);
3796 pointer = resource->rsrcInterface;
3799 *numResourceInterfaces = *numResourceInterfaces + 1;
3800 pointer = pointer->next;
3806 const char *OCGetResourceInterfaceName(OCResourceHandle handle, uint8_t index)
3808 OCResourceInterface *resourceInterface = NULL;
3810 resourceInterface = findResourceInterfaceAtIndex(handle, index);
3811 if (resourceInterface)
3813 return resourceInterface->name;
3815 return (const char *) NULL;
3818 OCResourceHandle OCGetResourceHandleFromCollection(OCResourceHandle collectionHandle,
3821 OCResource *resource = NULL;
3822 OCChildResource *tempChildResource = NULL;
3825 resource = findResource((OCResource *) collectionHandle);
3831 tempChildResource = resource->rsrcChildResourcesHead;
3833 while(tempChildResource)
3837 return tempChildResource->rsrcResource;
3840 tempChildResource = tempChildResource->next;
3843 // In this case, the number of resource handles in the collection exceeds the index
3844 tempChildResource = NULL;
3848 OCStackResult OCBindResourceHandler(OCResourceHandle handle,
3849 OCEntityHandler entityHandler,
3850 void* callbackParam)
3852 OCResource *resource = NULL;
3854 // Validate parameters
3855 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
3857 // Use the handle to find the resource in the resource linked list
3858 resource = findResource((OCResource *)handle);
3861 OIC_LOG(ERROR, TAG, "Resource not found");
3862 return OC_STACK_ERROR;
3866 resource->entityHandler = entityHandler;
3867 resource->entityHandlerCallbackParam = callbackParam;
3869 #ifdef WITH_PRESENCE
3870 if (presenceResource.handle)
3872 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
3873 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
3880 OCEntityHandler OCGetResourceHandler(OCResourceHandle handle)
3882 OCResource *resource = NULL;
3884 resource = findResource((OCResource *)handle);
3887 OIC_LOG(ERROR, TAG, "Resource not found");
3892 return resource->entityHandler;
3895 void incrementSequenceNumber(OCResource * resPtr)
3897 // Increment the sequence number
3898 resPtr->sequenceNum += 1;
3899 if (resPtr->sequenceNum == MAX_SEQUENCE_NUMBER)
3901 resPtr->sequenceNum = OC_OFFSET_SEQUENCE_NUMBER+1;
3906 #ifdef WITH_PRESENCE
3907 OCStackResult SendPresenceNotification(OCResourceType *resourceType,
3908 OCPresenceTrigger trigger)
3910 OIC_LOG(INFO, TAG, "SendPresenceNotification");
3911 OCResource *resPtr = NULL;
3912 OCStackResult result = OC_STACK_ERROR;
3913 OCMethod method = OC_REST_PRESENCE;
3914 uint32_t maxAge = 0;
3915 resPtr = findResource((OCResource *) presenceResource.handle);
3918 return OC_STACK_NO_RESOURCE;
3921 if((((OCResource *) presenceResource.handle)->resourceProperties) & OC_ACTIVE)
3923 maxAge = presenceResource.presenceTTL;
3925 result = SendAllObserverNotification(method, resPtr, maxAge,
3926 trigger, resourceType, OC_LOW_QOS);
3932 OCStackResult SendStopNotification()
3934 OIC_LOG(INFO, TAG, "SendStopNotification");
3935 OCResource *resPtr = NULL;
3936 OCStackResult result = OC_STACK_ERROR;
3937 OCMethod method = OC_REST_PRESENCE;
3938 resPtr = findResource((OCResource *) presenceResource.handle);
3941 return OC_STACK_NO_RESOURCE;
3944 // maxAge is 0. ResourceType is NULL.
3945 result = SendAllObserverNotification(method, resPtr, 0, OC_PRESENCE_TRIGGER_DELETE,
3951 #endif // WITH_PRESENCE
3952 OCStackResult OCNotifyAllObservers(OCResourceHandle handle, OCQualityOfService qos)
3954 OCResource *resPtr = NULL;
3955 OCStackResult result = OC_STACK_ERROR;
3956 OCMethod method = OC_REST_NOMETHOD;
3957 uint32_t maxAge = 0;
3959 OIC_LOG(INFO, TAG, "Notifying all observers");
3960 #ifdef WITH_PRESENCE
3961 if(handle == presenceResource.handle)
3965 #endif // WITH_PRESENCE
3966 VERIFY_NON_NULL(handle, ERROR, OC_STACK_ERROR);
3968 // Verify that the resource exists
3969 resPtr = findResource ((OCResource *) handle);
3972 return OC_STACK_NO_RESOURCE;
3976 //only increment in the case of regular observing (not presence)
3977 incrementSequenceNumber(resPtr);
3978 method = OC_REST_OBSERVE;
3979 maxAge = MAX_OBSERVE_AGE;
3980 #ifdef WITH_PRESENCE
3981 result = SendAllObserverNotification (method, resPtr, maxAge,
3982 OC_PRESENCE_TRIGGER_DELETE, NULL, qos);
3984 result = SendAllObserverNotification (method, resPtr, maxAge, qos);
3991 OCNotifyListOfObservers (OCResourceHandle handle,
3992 OCObservationId *obsIdList,
3993 uint8_t numberOfIds,
3994 const OCRepPayload *payload,
3995 OCQualityOfService qos)
3997 OIC_LOG(INFO, TAG, "Entering OCNotifyListOfObservers");
3999 OCResource *resPtr = NULL;
4000 //TODO: we should allow the server to define this
4001 uint32_t maxAge = MAX_OBSERVE_AGE;
4003 VERIFY_NON_NULL(handle, ERROR, OC_STACK_ERROR);
4004 VERIFY_NON_NULL(obsIdList, ERROR, OC_STACK_ERROR);
4005 VERIFY_NON_NULL(payload, ERROR, OC_STACK_ERROR);
4007 resPtr = findResource ((OCResource *) handle);
4008 if (NULL == resPtr || myStackMode == OC_CLIENT)
4010 return OC_STACK_NO_RESOURCE;
4014 incrementSequenceNumber(resPtr);
4016 return (SendListObserverNotification(resPtr, obsIdList, numberOfIds,
4017 payload, maxAge, qos));
4020 OCStackResult OCDoResponse(OCEntityHandlerResponse *ehResponse)
4022 OCStackResult result = OC_STACK_ERROR;
4023 OCServerRequest *serverRequest = NULL;
4025 OIC_LOG(INFO, TAG, "Entering OCDoResponse");
4027 // Validate input parameters
4028 VERIFY_NON_NULL(ehResponse, ERROR, OC_STACK_INVALID_PARAM);
4029 VERIFY_NON_NULL(ehResponse->requestHandle, ERROR, OC_STACK_INVALID_PARAM);
4032 // Get pointer to request info
4033 serverRequest = GetServerRequestUsingHandle((OCServerRequest *)ehResponse->requestHandle);
4036 // response handler in ocserverrequest.c. Usually HandleSingleResponse.
4037 result = serverRequest->ehResponseHandler(ehResponse);
4043 //#ifdef DIRECT_PAIRING
4044 const OCDPDev_t* OCDiscoverDirectPairingDevices(unsigned short waittime)
4046 OIC_LOG(INFO, TAG, "Start OCDiscoverDirectPairingDevices");
4047 if(OC_STACK_OK != DPDeviceDiscovery(waittime))
4049 OIC_LOG(ERROR, TAG, "Fail to discover Direct-Pairing device");
4053 return (const OCDPDev_t*)DPGetDiscoveredDevices();
4056 const OCDPDev_t* OCGetDirectPairedDevices()
4058 return (const OCDPDev_t*)DPGetPairedDevices();
4061 OCStackResult OCDoDirectPairing(void *ctx, OCDPDev_t* peer, OCPrm_t pmSel, char *pinNumber,
4062 OCDirectPairingCB resultCallback)
4064 OIC_LOG(INFO, TAG, "Start OCDoDirectPairing");
4065 if(NULL == peer || NULL == pinNumber)
4067 OIC_LOG(ERROR, TAG, "Invalid parameters");
4068 return OC_STACK_INVALID_PARAM;
4070 if (NULL == resultCallback)
4072 OIC_LOG(ERROR, TAG, "Invalid callback");
4073 return OC_STACK_INVALID_CALLBACK;
4076 return DPDirectPairing(ctx, (OCDirectPairingDev_t*)peer, (OicSecPrm_t)pmSel,
4077 pinNumber, (OCDirectPairingResultCB)resultCallback);
4079 //#endif // DIRECT_PAIRING
4081 //-----------------------------------------------------------------------------
4082 // Private internal function definitions
4083 //-----------------------------------------------------------------------------
4084 static OCDoHandle GenerateInvocationHandle()
4086 OCDoHandle handle = NULL;
4087 // Generate token here, it will be deleted when the transaction is deleted
4088 handle = (OCDoHandle) OICMalloc(sizeof(uint8_t[CA_MAX_TOKEN_LEN]));
4091 OCFillRandomMem((uint8_t*)handle, sizeof(uint8_t[CA_MAX_TOKEN_LEN]));
4097 #ifdef WITH_PRESENCE
4098 OCStackResult OCChangeResourceProperty(OCResourceProperty * inputProperty,
4099 OCResourceProperty resourceProperties, uint8_t enable)
4103 return OC_STACK_INVALID_PARAM;
4105 if (resourceProperties
4106 > (OC_ACTIVE | OC_DISCOVERABLE | OC_OBSERVABLE | OC_SLOW))
4108 OIC_LOG(ERROR, TAG, "Invalid property");
4109 return OC_STACK_INVALID_PARAM;
4113 *inputProperty = (OCResourceProperty) (*inputProperty & ~(resourceProperties));
4117 *inputProperty = (OCResourceProperty) (*inputProperty | resourceProperties);
4123 OCStackResult initResources()
4125 OCStackResult result = OC_STACK_OK;
4127 headResource = NULL;
4128 tailResource = NULL;
4129 // Init Virtual Resources
4130 #ifdef WITH_PRESENCE
4131 presenceResource.presenceTTL = OC_DEFAULT_PRESENCE_TTL_SECONDS;
4133 result = OCCreateResource(&presenceResource.handle,
4134 OC_RSRVD_RESOURCE_TYPE_PRESENCE,
4136 OC_RSRVD_PRESENCE_URI,
4140 //make resource inactive
4141 result = OCChangeResourceProperty(
4142 &(((OCResource *) presenceResource.handle)->resourceProperties),
4145 #ifndef WITH_ARDUINO
4146 if (result == OC_STACK_OK)
4148 result = SRMInitSecureResources();
4152 if(result == OC_STACK_OK)
4154 CreateResetProfile();
4155 result = OCCreateResource(&deviceResource,
4156 OC_RSRVD_RESOURCE_TYPE_DEVICE,
4157 OC_RSRVD_INTERFACE_DEFAULT,
4158 OC_RSRVD_DEVICE_URI,
4162 if(result == OC_STACK_OK)
4164 result = BindResourceInterfaceToResource((OCResource *)deviceResource,
4165 OC_RSRVD_INTERFACE_READ);
4169 if(result == OC_STACK_OK)
4171 result = OCCreateResource(&platformResource,
4172 OC_RSRVD_RESOURCE_TYPE_PLATFORM,
4173 OC_RSRVD_INTERFACE_DEFAULT,
4174 OC_RSRVD_PLATFORM_URI,
4178 if(result == OC_STACK_OK)
4180 result = BindResourceInterfaceToResource((OCResource *)platformResource,
4181 OC_RSRVD_INTERFACE_READ);
4188 void insertResource(OCResource *resource)
4192 headResource = resource;
4193 tailResource = resource;
4197 tailResource->next = resource;
4198 tailResource = resource;
4200 resource->next = NULL;
4203 OCResource *findResource(OCResource *resource)
4205 OCResource *pointer = headResource;
4209 if (pointer == resource)
4213 pointer = pointer->next;
4218 void deleteAllResources()
4220 OCResource *pointer = headResource;
4221 OCResource *temp = NULL;
4225 temp = pointer->next;
4226 #ifdef WITH_PRESENCE
4227 if (pointer != (OCResource *) presenceResource.handle)
4229 #endif // WITH_PRESENCE
4230 deleteResource(pointer);
4231 #ifdef WITH_PRESENCE
4233 #endif // WITH_PRESENCE
4236 memset(&platformResource, 0, sizeof(platformResource));
4237 memset(&deviceResource, 0, sizeof(deviceResource));
4239 memset(&brokerResource, 0, sizeof(brokerResource));
4242 SRMDeInitSecureResources();
4244 #ifdef WITH_PRESENCE
4245 // Ensure that the last resource to be deleted is the presence resource. This allows for all
4246 // presence notification attributed to their deletion to be processed.
4247 deleteResource((OCResource *) presenceResource.handle);
4248 memset(&presenceResource, 0, sizeof(presenceResource));
4249 #endif // WITH_PRESENCE
4252 OCStackResult deleteResource(OCResource *resource)
4254 OCResource *prev = NULL;
4255 OCResource *temp = NULL;
4258 OIC_LOG(DEBUG,TAG,"resource is NULL");
4259 return OC_STACK_INVALID_PARAM;
4262 OIC_LOG_V (INFO, TAG, "Deleting resource %s", resource->uri);
4264 temp = headResource;
4267 if (temp == resource)
4269 // Invalidate all Resource Properties.
4270 resource->resourceProperties = (OCResourceProperty) 0;
4271 #ifdef WITH_PRESENCE
4272 if(resource != (OCResource *) presenceResource.handle)
4274 #endif // WITH_PRESENCE
4275 OCNotifyAllObservers((OCResourceHandle)resource, OC_HIGH_QOS);
4276 #ifdef WITH_PRESENCE
4279 if(presenceResource.handle)
4281 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
4282 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_DELETE);
4285 // Only resource in list.
4286 if (temp == headResource && temp == tailResource)
4288 headResource = NULL;
4289 tailResource = NULL;
4292 else if (temp == headResource)
4294 headResource = temp->next;
4297 else if (temp == tailResource)
4299 tailResource = prev;
4300 tailResource->next = NULL;
4304 prev->next = temp->next;
4307 deleteResourceElements(temp);
4318 return OC_STACK_ERROR;
4321 void deleteResourceElements(OCResource *resource)
4328 OICFree(resource->uri);
4329 deleteResourceType(resource->rsrcType);
4330 deleteResourceInterface(resource->rsrcInterface);
4331 OCDeleteResourceAttributes(resource->rsrcAttributes);
4334 void deleteResourceType(OCResourceType *resourceType)
4336 OCResourceType *pointer = resourceType;
4337 OCResourceType *next = NULL;
4341 next = pointer->next;
4342 OICFree(pointer->resourcetypename);
4348 void deleteResourceInterface(OCResourceInterface *resourceInterface)
4350 OCResourceInterface *pointer = resourceInterface;
4351 OCResourceInterface *next = NULL;
4355 next = pointer->next;
4356 OICFree(pointer->name);
4362 void OCDeleteResourceAttributes(OCAttribute *rsrcAttributes)
4364 OCAttribute *next = NULL;
4365 for (OCAttribute *pointer = rsrcAttributes; pointer; pointer = next)
4367 next = pointer->next;
4368 if (pointer->attrName && 0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, pointer->attrName))
4370 OCFreeOCStringLL((OCStringLL *)pointer->attrValue);
4374 OICFree(pointer->attrValue);
4376 OICFree(pointer->attrName);
4381 void insertResourceType(OCResource *resource, OCResourceType *resourceType)
4383 OCResourceType *pointer = NULL;
4384 OCResourceType *previous = NULL;
4385 if (!resource || !resourceType)
4389 // resource type list is empty.
4390 else if (!resource->rsrcType)
4392 resource->rsrcType = resourceType;
4396 pointer = resource->rsrcType;
4400 if (!strcmp(resourceType->resourcetypename, pointer->resourcetypename))
4402 OIC_LOG_V(INFO, TAG, "Type %s already exists", resourceType->resourcetypename);
4403 OICFree(resourceType->resourcetypename);
4404 OICFree(resourceType);
4408 pointer = pointer->next;
4413 previous->next = resourceType;
4416 resourceType->next = NULL;
4418 OIC_LOG_V(INFO, TAG, "Added type %s to %s", resourceType->resourcetypename, resource->uri);
4421 OCResourceType *findResourceTypeAtIndex(OCResourceHandle handle, uint8_t index)
4423 OCResource *resource = NULL;
4424 OCResourceType *pointer = NULL;
4426 // Find the specified resource
4427 resource = findResource((OCResource *) handle);
4433 // Make sure a resource has a resourcetype
4434 if (!resource->rsrcType)
4439 // Iterate through the list
4440 pointer = resource->rsrcType;
4441 for(uint8_t i = 0; i< index && pointer; ++i)
4443 pointer = pointer->next;
4448 OCResourceType *findResourceType(OCResourceType * resourceTypeList, const char * resourceTypeName)
4450 if(resourceTypeList && resourceTypeName)
4452 OCResourceType * rtPointer = resourceTypeList;
4453 while(resourceTypeName && rtPointer)
4455 OIC_LOG_V(DEBUG, TAG, "current resourceType : %s", rtPointer->resourcetypename);
4456 if(rtPointer->resourcetypename &&
4457 strcmp(resourceTypeName, (const char *)
4458 (rtPointer->resourcetypename)) == 0)
4462 rtPointer = rtPointer->next;
4470 * Insert a new interface into interface linked list only if not already present.
4471 * If alredy present, 2nd arg is free'd.
4472 * Default interface will always be first if present.
4474 void insertResourceInterface(OCResource *resource, OCResourceInterface *newInterface)
4476 OCResourceInterface *pointer = NULL;
4477 OCResourceInterface *previous = NULL;
4479 newInterface->next = NULL;
4481 OCResourceInterface **firstInterface = &(resource->rsrcInterface);
4483 if (!*firstInterface)
4485 // If first interface is not oic.if.baseline, by default add it as first interface type.
4486 if (0 == strcmp(newInterface->name, OC_RSRVD_INTERFACE_DEFAULT))
4488 *firstInterface = newInterface;
4492 OCStackResult result = BindResourceInterfaceToResource(resource,
4493 OC_RSRVD_INTERFACE_DEFAULT);
4494 if (result != OC_STACK_OK)
4496 OICFree(newInterface->name);
4497 OICFree(newInterface);
4500 if (*firstInterface)
4502 (*firstInterface)->next = newInterface;
4506 // If once add oic.if.baseline, later too below code take care of freeing memory.
4507 else if (strcmp(newInterface->name, OC_RSRVD_INTERFACE_DEFAULT) == 0)
4509 if (strcmp((*firstInterface)->name, OC_RSRVD_INTERFACE_DEFAULT) == 0)
4511 OICFree(newInterface->name);
4512 OICFree(newInterface);
4515 // This code will not hit anymore, keeping
4518 newInterface->next = *firstInterface;
4519 *firstInterface = newInterface;
4524 pointer = *firstInterface;
4527 if (strcmp(newInterface->name, pointer->name) == 0)
4529 OICFree(newInterface->name);
4530 OICFree(newInterface);
4534 pointer = pointer->next;
4539 previous->next = newInterface;
4544 OCResourceInterface *findResourceInterfaceAtIndex(OCResourceHandle handle,
4547 OCResource *resource = NULL;
4548 OCResourceInterface *pointer = NULL;
4550 // Find the specified resource
4551 resource = findResource((OCResource *) handle);
4557 // Make sure a resource has a resourceinterface
4558 if (!resource->rsrcInterface)
4563 // Iterate through the list
4564 pointer = resource->rsrcInterface;
4566 for (uint8_t i = 0; i < index && pointer; ++i)
4568 pointer = pointer->next;
4574 * This function splits the uri using the '?' delimiter.
4575 * "uriWithoutQuery" is the block of characters between the beginning
4576 * till the delimiter or '\0' which ever comes first.
4577 * "query" is whatever is to the right of the delimiter if present.
4578 * No delimiter sets the query to NULL.
4579 * If either are present, they will be malloc'ed into the params 2, 3.
4580 * The first param, *uri is left untouched.
4582 * NOTE: This function does not account for whitespace at the end of the uri NOR
4583 * malformed uri's with '??'. Whitespace at the end will be assumed to be
4584 * part of the query.
4586 OCStackResult getQueryFromUri(const char * uri, char** query, char ** uriWithoutQuery)
4590 return OC_STACK_INVALID_URI;
4592 if(!query || !uriWithoutQuery)
4594 return OC_STACK_INVALID_PARAM;
4598 *uriWithoutQuery = NULL;
4600 size_t uriWithoutQueryLen = 0;
4601 size_t queryLen = 0;
4602 size_t uriLen = strlen(uri);
4604 char *pointerToDelimiter = strstr(uri, "?");
4606 uriWithoutQueryLen = pointerToDelimiter == NULL ? uriLen : (size_t)(pointerToDelimiter - uri);
4607 queryLen = pointerToDelimiter == NULL ? 0 : uriLen - uriWithoutQueryLen - 1;
4609 if (uriWithoutQueryLen)
4611 *uriWithoutQuery = (char *) OICCalloc(uriWithoutQueryLen + 1, 1);
4612 if (!*uriWithoutQuery)
4616 OICStrcpy(*uriWithoutQuery, uriWithoutQueryLen +1, uri);
4620 *query = (char *) OICCalloc(queryLen + 1, 1);
4623 OICFree(*uriWithoutQuery);
4624 *uriWithoutQuery = NULL;
4627 OICStrcpy(*query, queryLen + 1, pointerToDelimiter + 1);
4633 return OC_STACK_NO_MEMORY;
4636 static const OicUuid_t* OCGetServerInstanceID(void)
4638 static bool generated = false;
4639 static OicUuid_t sid;
4645 if (OC_STACK_OK != GetDoxmDeviceID(&sid))
4647 OIC_LOG(FATAL, TAG, "Generate UUID for Server Instance failed!");
4654 const char* OCGetServerInstanceIDString(void)
4656 static bool generated = false;
4657 static char sidStr[UUID_STRING_SIZE];
4664 const OicUuid_t *sid = OCGetServerInstanceID();
4665 if (sid && OCConvertUuidToString(sid->id, sidStr) != RAND_UUID_OK)
4667 OIC_LOG(FATAL, TAG, "Generate UUID String for Server Instance failed!");
4675 CAResult_t OCSelectNetwork()
4677 CAResult_t retResult = CA_STATUS_FAILED;
4678 CAResult_t caResult = CA_STATUS_OK;
4680 CATransportAdapter_t connTypes[] = {
4682 CA_ADAPTER_RFCOMM_BTEDR,
4683 CA_ADAPTER_GATT_BTLE,
4686 ,CA_ADAPTER_REMOTE_ACCESS
4693 int numConnTypes = sizeof(connTypes)/sizeof(connTypes[0]);
4695 for(int i = 0; i < numConnTypes; i++)
4697 // If CA status is not initialized, CASelectNetwork() will not be called.
4698 if (caResult != CA_STATUS_NOT_INITIALIZED)
4700 caResult = CASelectNetwork(connTypes[i]);
4701 if (caResult == CA_STATUS_OK)
4703 retResult = CA_STATUS_OK;
4708 if (retResult != CA_STATUS_OK)
4710 return caResult; // Returns error of appropriate transport that failed fatally.
4716 OCStackResult CAResultToOCResult(CAResult_t caResult)
4722 case CA_STATUS_INVALID_PARAM:
4723 return OC_STACK_INVALID_PARAM;
4724 case CA_ADAPTER_NOT_ENABLED:
4725 return OC_STACK_ADAPTER_NOT_ENABLED;
4726 case CA_SERVER_STARTED_ALREADY:
4728 case CA_SERVER_NOT_STARTED:
4729 return OC_STACK_ERROR;
4730 case CA_DESTINATION_NOT_REACHABLE:
4731 return OC_STACK_COMM_ERROR;
4732 case CA_SOCKET_OPERATION_FAILED:
4733 return OC_STACK_COMM_ERROR;
4734 case CA_SEND_FAILED:
4735 return OC_STACK_COMM_ERROR;
4736 case CA_RECEIVE_FAILED:
4737 return OC_STACK_COMM_ERROR;
4738 case CA_MEMORY_ALLOC_FAILED:
4739 return OC_STACK_NO_MEMORY;
4740 case CA_REQUEST_TIMEOUT:
4741 return OC_STACK_TIMEOUT;
4742 case CA_DESTINATION_DISCONNECTED:
4743 return OC_STACK_COMM_ERROR;
4744 case CA_STATUS_FAILED:
4745 return OC_STACK_ERROR;
4746 case CA_NOT_SUPPORTED:
4747 return OC_STACK_NOTIMPL;
4749 return OC_STACK_ERROR;
4753 bool OCResultToSuccess(OCStackResult ocResult)
4758 case OC_STACK_RESOURCE_CREATED:
4759 case OC_STACK_RESOURCE_DELETED:
4760 case OC_STACK_CONTINUE:
4761 case OC_STACK_RESOURCE_CHANGED:
4762 case OC_STACK_SLOW_RESOURCE:
4770 OCStackResult OCSetProxyURI(const char *uri)
4772 return CAResultToOCResult(CASetProxyUri(uri));
4776 #if defined(RD_CLIENT) || defined(RD_SERVER)
4777 OCStackResult OCBindResourceInsToResource(OCResourceHandle handle, uint8_t ins)
4779 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
4781 OCResource *resource = NULL;
4783 resource = findResource((OCResource *) handle);
4786 OIC_LOG(ERROR, TAG, "Resource not found");
4787 return OC_STACK_ERROR;
4790 resource->ins = ins;
4796 OCStackResult OCUpdateResourceInsWithResponse(const char *requestUri,
4797 const OCClientResponse *response)
4799 // Validate input parameters
4800 VERIFY_NON_NULL(requestUri, ERROR, OC_STACK_INVALID_PARAM);
4801 VERIFY_NON_NULL(response, ERROR, OC_STACK_INVALID_PARAM);
4803 char *targetUri = (char *) OICMalloc(strlen(requestUri) + 1);
4806 return OC_STACK_NO_MEMORY;
4808 strncpy(targetUri, requestUri, strlen(requestUri) + 1);
4810 if (response->result == OC_STACK_RESOURCE_CHANGED) // publish message
4812 OIC_LOG(DEBUG, TAG, "update the ins of published resource");
4814 char rdPubUri[MAX_URI_LENGTH] = { 0 };
4815 snprintf(rdPubUri, MAX_URI_LENGTH, "%s?rt=%s", OC_RSRVD_RD_URI,
4816 OC_RSRVD_RESOURCE_TYPE_RDPUBLISH);
4818 if (strcmp(rdPubUri, targetUri) == 0)
4820 // Update resource unique id in stack.
4823 if (response->payload)
4825 OCRepPayload *rdPayload = (OCRepPayload *) response->payload;
4826 OCRepPayload **links = NULL;
4827 size_t dimensions[MAX_REP_ARRAY_DEPTH] = { 0 };
4828 if (OCRepPayloadGetPropObjectArray(rdPayload, OC_RSRVD_LINKS,
4829 &links, dimensions))
4832 for (; i < dimensions[0]; i++)
4835 if (OCRepPayloadGetPropString(links[i], OC_RSRVD_HREF, &uri))
4837 OCResourceHandle handle = OCGetResourceHandleAtUri(uri);
4839 if (OCRepPayloadGetPropInt(links[i], OC_RSRVD_INS, &ins))
4841 OCBindResourceInsToResource(handle, ins);
4850 else if (response->result == OC_STACK_RESOURCE_DELETED) // delete message
4852 OIC_LOG(DEBUG, TAG, "update the ins of deleted resource with 0");
4854 uint8_t numResources = 0;
4855 OCGetNumberOfResources(&numResources);
4857 char *ins = strstr(targetUri, OC_RSRVD_INS);
4860 for (uint8_t i = 0; i < numResources; i++)
4862 OCResourceHandle resHandle = OCGetResourceHandle(i);
4865 OCBindResourceInsToResource(resHandle, 0);
4871 const char *token = "&";
4872 char *iterTokenPtr = NULL;
4873 char *start = strtok_r(targetUri, token, &iterTokenPtr);
4875 while (start != NULL)
4877 char *query = start;
4878 query = strstr(query, OC_RSRVD_INS);
4881 uint8_t queryIns = atoi(query + 4);
4882 for (uint8_t i = 0; i < numResources; i++)
4884 OCResourceHandle resHandle = OCGetResourceHandle(i);
4888 OCGetResourceIns(resHandle, &resIns);
4889 if (queryIns && queryIns == resIns)
4891 OCBindResourceInsToResource(resHandle, 0);
4897 start = strtok_r(NULL, token, &iterTokenPtr);
4906 OCResourceHandle OCGetResourceHandleAtUri(const char *uri)
4910 OIC_LOG(ERROR, TAG, "Resource uri is NULL");
4914 OCResource *pointer = headResource;
4918 if (strncmp(uri, pointer->uri, MAX_URI_LENGTH) == 0)
4920 OIC_LOG_V(DEBUG, TAG, "Found Resource %s", uri);
4923 pointer = pointer->next;
4928 OCStackResult OCGetResourceIns(OCResourceHandle handle, uint8_t *ins)
4930 OCResource *resource = NULL;
4932 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
4933 VERIFY_NON_NULL(ins, ERROR, OC_STACK_INVALID_PARAM);
4935 resource = findResource((OCResource *) handle);
4938 *ins = resource->ins;
4941 return OC_STACK_ERROR;
4945 OCStackResult OCSetHeaderOption(OCHeaderOption* ocHdrOpt, size_t* numOptions, uint16_t optionID,
4946 void* optionData, size_t optionDataLength)
4950 OIC_LOG (INFO, TAG, "Header options are NULL");
4951 return OC_STACK_INVALID_PARAM;
4956 OIC_LOG (INFO, TAG, "optionData are NULL");
4957 return OC_STACK_INVALID_PARAM;
4962 OIC_LOG (INFO, TAG, "numOptions is NULL");
4963 return OC_STACK_INVALID_PARAM;
4966 if (*numOptions >= MAX_HEADER_OPTIONS)
4968 OIC_LOG (INFO, TAG, "Exceeding MAX_HEADER_OPTIONS");
4969 return OC_STACK_NO_MEMORY;
4972 ocHdrOpt += *numOptions;
4973 ocHdrOpt->protocolID = OC_COAP_ID;
4974 ocHdrOpt->optionID = optionID;
4975 ocHdrOpt->optionLength =
4976 optionDataLength < MAX_HEADER_OPTION_DATA_LENGTH ?
4977 optionDataLength : MAX_HEADER_OPTION_DATA_LENGTH;
4978 memcpy(ocHdrOpt->optionData, (const void*) optionData, ocHdrOpt->optionLength);
4984 OCStackResult OCGetHeaderOption(OCHeaderOption* ocHdrOpt, size_t numOptions, uint16_t optionID,
4985 void* optionData, size_t optionDataLength, uint16_t* receivedDataLength)
4987 if (!ocHdrOpt || !numOptions)
4989 OIC_LOG (INFO, TAG, "No options present");
4995 OIC_LOG (INFO, TAG, "optionData are NULL");
4996 return OC_STACK_INVALID_PARAM;
4999 if (!receivedDataLength)
5001 OIC_LOG (INFO, TAG, "receivedDataLength is NULL");
5002 return OC_STACK_INVALID_PARAM;
5005 for (uint8_t i = 0; i < numOptions; i++)
5007 if (ocHdrOpt[i].optionID == optionID)
5009 if (optionDataLength >= ocHdrOpt->optionLength)
5011 memcpy(optionData, ocHdrOpt->optionData, ocHdrOpt->optionLength);
5012 *receivedDataLength = ocHdrOpt->optionLength;
5017 OIC_LOG (ERROR, TAG, "optionDataLength is less than the length of received data");
5018 return OC_STACK_ERROR;
5025 void OCDefaultAdapterStateChangedHandler(CATransportAdapter_t adapter, bool enabled)
5027 OIC_LOG(DEBUG, TAG, "OCDefaultAdapterStateChangedHandler");
5028 if (g_adapterHandler)
5030 g_adapterHandler(adapter, enabled);
5034 void OCDefaultConnectionStateChangedHandler(const CAEndpoint_t *info, bool isConnected)
5036 OIC_LOG(DEBUG, TAG, "OCDefaultConnectionStateChangedHandler");
5037 if (g_connectionHandler)
5039 g_connectionHandler(info, isConnected);
5043 void OCSetNetworkMonitorHandler(CAAdapterStateChangedCB adapterHandler,
5044 CAConnectionStateChangedCB connectionHandler)
5046 OIC_LOG(DEBUG, TAG, "OCSetNetworkMonitorHandler");
5047 g_adapterHandler = adapterHandler;
5048 g_connectionHandler = connectionHandler;
5051 OCStackResult OCGetDeviceId(OCUUIdentity *deviceId)
5054 OCStackResult ret = OC_STACK_ERROR;
5056 ret = GetDoxmDeviceID(&oicUuid);
5057 if (OC_STACK_OK == ret)
5059 memcpy(deviceId, &oicUuid, UUID_IDENTITY_SIZE);
5063 OIC_LOG(ERROR, TAG, "Device ID Get error");
5068 OCStackResult OCSetDeviceId(const OCUUIdentity *deviceId)
5071 OCStackResult ret = OC_STACK_ERROR;
5073 memcpy(&oicUuid, deviceId, UUID_LENGTH);
5074 for (int i = 0; i < UUID_LENGTH; i++)
5076 OIC_LOG_V(INFO, TAG, "Set Device Id %x", oicUuid.id[i]);
5078 ret = SetDoxmDeviceID(&oicUuid);