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
37 #include "ocstackinternal.h"
38 #include "ocresourcehandler.h"
39 #include "occlientcb.h"
40 #include "ocobserve.h"
42 #include "oic_malloc.h"
43 #include "oic_string.h"
44 #include "ocserverrequest.h"
45 #include "secureresourcemanager.h"
46 #include "doxmresource.h"
48 #include "cainterface.h"
49 #include "ocpayload.h"
50 #include "ocpayloadcbor.h"
57 #include "coap_time.h"
62 #include <arpa/inet.h>
66 #define UINT32_MAX (0xFFFFFFFFUL)
69 //-----------------------------------------------------------------------------
71 //-----------------------------------------------------------------------------
74 OC_STACK_UNINITIALIZED = 0,
76 OC_STACK_UNINIT_IN_PROGRESS
82 OC_PRESENCE_UNINITIALIZED = 0,
83 OC_PRESENCE_INITIALIZED
87 //-----------------------------------------------------------------------------
89 //-----------------------------------------------------------------------------
90 static OCStackState stackState = OC_STACK_UNINITIALIZED;
92 OCResource *headResource = NULL;
93 static OCResource *tailResource = NULL;
95 static OCPresenceState presenceState = OC_PRESENCE_UNINITIALIZED;
96 static PresenceResource presenceResource;
97 static uint8_t PresenceTimeOutSize = 0;
98 static uint32_t PresenceTimeOut[] = {50, 75, 85, 95, 100};
101 static OCMode myStackMode;
103 //TODO: revisit this design
104 static bool gRASetInfo = false;
106 OCDeviceEntityHandler defaultDeviceHandler;
107 void* defaultDeviceHandlerCallbackParameter = NULL;
109 //-----------------------------------------------------------------------------
111 //-----------------------------------------------------------------------------
112 #define TAG PCF("OCStack")
113 #define VERIFY_SUCCESS(op, successCode) { if ((op) != (successCode)) \
114 {OC_LOG_V(FATAL, TAG, "%s failed!!", #op); goto exit;} }
115 #define VERIFY_NON_NULL(arg, logLevel, retVal) { if (!(arg)) { OC_LOG((logLevel), \
116 TAG, PCF(#arg " is NULL")); return (retVal); } }
117 #define VERIFY_NON_NULL_NR(arg, logLevel) { if (!(arg)) { OC_LOG((logLevel), \
118 TAG, PCF(#arg " is NULL")); return; } }
119 #define VERIFY_NON_NULL_V(arg) { if (!arg) {OC_LOG_V(FATAL, TAG, "%s is NULL", #arg);\
122 //TODO: we should allow the server to define this
123 #define MAX_OBSERVE_AGE (0x2FFFFUL)
125 #define MILLISECONDS_PER_SECOND (1000)
127 //-----------------------------------------------------------------------------
128 // Private internal function prototypes
129 //-----------------------------------------------------------------------------
132 * Generate handle of OCDoResource invocation for callback management.
134 * @return Generated OCDoResource handle.
136 static OCDoHandle GenerateInvocationHandle();
139 * Initialize resource data structures, variables, etc.
141 * @return ::OC_STACK_OK on success, some other value upon failure.
143 static OCStackResult initResources();
146 * Add a resource to the end of the linked list of resources.
148 * @param resource Resource to be added
150 static void insertResource(OCResource *resource);
153 * Find a resource in the linked list of resources.
155 * @param resource Resource to be found.
156 * @return Pointer to resource that was found in the linked list or NULL if the resource was not
159 static OCResource *findResource(OCResource *resource);
162 * Insert a resource type into a resource's resource type linked list.
163 * If resource type already exists, it will not be inserted and the
164 * resourceType will be free'd.
165 * resourceType->next should be null to avoid memory leaks.
166 * Function returns silently for null args.
168 * @param resource Resource where resource type is to be inserted.
169 * @param resourceType Resource type to be inserted.
171 static void insertResourceType(OCResource *resource,
172 OCResourceType *resourceType);
175 * Get a resource type at the specified index within a resource.
177 * @param handle Handle of resource.
178 * @param index Index of resource type.
180 * @return Pointer to resource type if found, NULL otherwise.
182 static OCResourceType *findResourceTypeAtIndex(OCResourceHandle handle,
186 * Insert a resource interface into a resource's resource interface linked list.
187 * If resource interface already exists, it will not be inserted and the
188 * resourceInterface will be free'd.
189 * resourceInterface->next should be null to avoid memory leaks.
191 * @param resource Resource where resource interface is to be inserted.
192 * @param resourceInterface Resource interface to be inserted.
194 static void insertResourceInterface(OCResource *resource,
195 OCResourceInterface *resourceInterface);
198 * Get a resource interface at the specified index within a resource.
200 * @param handle Handle of resource.
201 * @param index Index of resource interface.
203 * @return Pointer to resource interface if found, NULL otherwise.
205 static OCResourceInterface *findResourceInterfaceAtIndex(
206 OCResourceHandle handle, uint8_t index);
209 * Delete all of the dynamically allocated elements that were created for the resource type.
211 * @param resourceType Specified resource type.
213 static void deleteResourceType(OCResourceType *resourceType);
216 * Delete all of the dynamically allocated elements that were created for the resource interface.
218 * @param resourceInterface Specified resource interface.
220 static void deleteResourceInterface(OCResourceInterface *resourceInterface);
223 * Delete all of the dynamically allocated elements that were created for the resource.
225 * @param resource Specified resource.
227 static void deleteResourceElements(OCResource *resource);
230 * Delete resource specified by handle. Deletes resource and all resourcetype and resourceinterface
233 * @param handle Handle of resource to be deleted.
235 * @return ::OC_STACK_OK on success, some other value upon failure.
237 static OCStackResult deleteResource(OCResource *resource);
240 * Delete all of the resources in the resource list.
242 static void deleteAllResources();
245 * Increment resource sequence number. Handles rollover.
247 * @param resPtr Pointer to resource.
249 static void incrementSequenceNumber(OCResource * resPtr);
252 * Verify the lengths of the URI and the query separately.
254 * @param inputUri Input URI and query.
255 * @param uriLen The length of the initial URI with query.
256 * @return ::OC_STACK_OK on success, some other value upon failure.
258 static OCStackResult verifyUriQueryLength(const char * inputUri,
262 * Attempts to initialize every network interface that the CA Layer might have compiled in.
264 * Note: At least one interface must succeed to initialize. If all calls to @ref CASelectNetwork
265 * return something other than @ref CA_STATUS_OK, then this function fails.
267 * @return ::CA_STATUS_OK on success, some other value upon failure.
269 static CAResult_t OCSelectNetwork();
272 * Get the CoAP ticks after the specified number of milli-seconds.
274 * @param afterMilliSeconds Milli-seconds.
278 static uint32_t GetTicks(uint32_t afterMilliSeconds);
281 * Convert CAResponseResult_t to OCStackResult.
283 * @param caCode CAResponseResult_t code.
284 * @return ::OC_STACK_OK on success, some other value upon failure.
286 static OCStackResult CAToOCStackResult(CAResponseResult_t caCode);
289 * Convert OCStackResult to CAResponseResult_t.
291 * @param caCode OCStackResult code.
292 * @return ::CA_SUCCESS on success, some other value upon failure.
294 static CAResponseResult_t OCToCAStackResult(OCStackResult ocCode);
297 * Convert OCTransportFlags_t to CATransportModifiers_t.
299 * @param ocConType OCTransportFlags_t input.
300 * @return CATransportFlags
302 static CATransportFlags_t OCToCATransportFlags(OCTransportFlags ocConType);
305 * Convert CATransportFlags_t to OCTransportModifiers_t.
307 * @param caConType CATransportFlags_t input.
308 * @return OCTransportFlags
310 static OCTransportFlags CAToOCTransportFlags(CATransportFlags_t caConType);
313 * Handle response from presence request.
315 * @param endPoint CA remote endpoint.
316 * @param responseInfo CA response info.
317 * @return ::OC_STACK_OK on success, some other value upon failure.
319 static OCStackResult HandlePresenceResponse(const CAEndpoint_t *endPoint,
320 const CAResponseInfo_t *responseInfo);
323 * This function will be called back by CA layer when a response is received.
325 * @param endPoint CA remote endpoint.
326 * @param responseInfo CA response info.
328 static void HandleCAResponses(const CAEndpoint_t* endPoint,
329 const CAResponseInfo_t* responseInfo);
332 * This function will be called back by CA layer when a request is received.
334 * @param endPoint CA remote endpoint.
335 * @param requestInfo CA request info.
337 static void HandleCARequests(const CAEndpoint_t* endPoint,
338 const CARequestInfo_t* requestInfo);
341 * Extract query from a URI.
343 * @param uri Full URI with query.
344 * @param query Pointer to string that will contain query.
345 * @param newURI Pointer to string that will contain URI.
346 * @return ::OC_STACK_OK on success, some other value upon failure.
348 static OCStackResult getQueryFromUri(const char * uri, char** resourceType, char ** newURI);
351 * Finds a resource type in an OCResourceType link-list.
353 * @param resourceTypeList The link-list to be searched through.
354 * @param resourceTypeName The key to search for.
356 * @return Resource type that matches the key (ie. resourceTypeName) or
357 * NULL if there is either an invalid parameter or this function was unable to find the key.
359 static OCResourceType *findResourceType(OCResourceType * resourceTypeList,
360 const char * resourceTypeName);
363 * Reset presence TTL for a ClientCB struct. ttlLevel will be set to 0.
364 * TTL will be set to maxAge.
366 * @param cbNode Callback Node for which presence ttl is to be reset.
367 * @param maxAge New value of ttl in seconds.
369 * @return ::OC_STACK_OK on success, some other value upon failure.
371 static OCStackResult ResetPresenceTTL(ClientCB *cbNode, uint32_t maxAgeSeconds);
373 //-----------------------------------------------------------------------------
374 // Internal functions
375 //-----------------------------------------------------------------------------
377 uint32_t GetTicks(uint32_t afterMilliSeconds)
382 // Guard against overflow of uint32_t
383 if (afterMilliSeconds <= ((UINT32_MAX - (uint32_t)now) * MILLISECONDS_PER_SECOND) /
384 COAP_TICKS_PER_SECOND)
386 return now + (afterMilliSeconds * COAP_TICKS_PER_SECOND)/MILLISECONDS_PER_SECOND;
394 void CopyEndpointToDevAddr(const CAEndpoint_t *in, OCDevAddr *out)
396 VERIFY_NON_NULL_NR(in, FATAL);
397 VERIFY_NON_NULL_NR(out, FATAL);
399 out->adapter = (OCTransportAdapter)in->adapter;
400 out->flags = CAToOCTransportFlags(in->flags);
401 OICStrcpy(out->addr, sizeof(out->addr), in->addr);
402 out->port = in->port;
403 out->interface = in->interface;
406 void CopyDevAddrToEndpoint(const OCDevAddr *in, CAEndpoint_t *out)
408 VERIFY_NON_NULL_NR(in, FATAL);
409 VERIFY_NON_NULL_NR(out, FATAL);
411 out->adapter = (CATransportAdapter_t)in->adapter;
412 out->flags = OCToCATransportFlags(in->flags);
413 OICStrcpy(out->addr, sizeof(out->addr), in->addr);
414 out->port = in->port;
415 out->interface = in->interface;
418 void FixUpClientResponse(OCClientResponse *cr)
420 VERIFY_NON_NULL_NR(cr, FATAL);
422 cr->addr = &cr->devAddr;
423 cr->connType = (OCConnectivityType)
424 ((cr->devAddr.adapter << CT_ADAPTER_SHIFT) | (cr->devAddr.flags & CT_MASK_FLAGS));
427 //-----------------------------------------------------------------------------
428 // Internal API function
429 //-----------------------------------------------------------------------------
431 // This internal function is called to update the stack with the status of
432 // observers and communication failures
433 OCStackResult OCStackFeedBack(CAToken_t token, uint8_t tokenLength, uint8_t status)
435 OCStackResult result = OC_STACK_ERROR;
436 ResourceObserver * observer = NULL;
437 OCEntityHandlerRequest ehRequest = {0};
441 case OC_OBSERVER_NOT_INTERESTED:
442 OC_LOG(DEBUG, TAG, PCF("observer not interested in our notifications"));
443 observer = GetObserverUsingToken (token, tokenLength);
446 result = FormOCEntityHandlerRequest(&ehRequest,
447 (OCRequestHandle)NULL,
450 (OCResourceHandle)NULL,
451 NULL, NULL, 0, 0, NULL,
452 OC_OBSERVE_DEREGISTER,
453 observer->observeId);
454 if(result != OC_STACK_OK)
458 observer->resource->entityHandler(OC_OBSERVE_FLAG, &ehRequest,
459 observer->resource->entityHandlerCallbackParam);
462 result = DeleteObserverUsingToken (token, tokenLength);
463 if(result == OC_STACK_OK)
465 OC_LOG(DEBUG, TAG, PCF("Removed observer successfully"));
469 result = OC_STACK_OK;
470 OC_LOG(DEBUG, TAG, PCF("Observer Removal failed"));
474 case OC_OBSERVER_STILL_INTERESTED:
475 OC_LOG(DEBUG, TAG, PCF("observer still interested, reset the failedCount"));
476 observer = GetObserverUsingToken (token, tokenLength);
479 observer->forceHighQos = 0;
480 observer->failedCommCount = 0;
481 result = OC_STACK_OK;
485 result = OC_STACK_OBSERVER_NOT_FOUND;
489 case OC_OBSERVER_FAILED_COMM:
490 OC_LOG(DEBUG, TAG, PCF("observer is unreachable"));
491 observer = GetObserverUsingToken (token, tokenLength);
494 if(observer->failedCommCount >= MAX_OBSERVER_FAILED_COMM)
496 result = FormOCEntityHandlerRequest(&ehRequest,
497 (OCRequestHandle)NULL,
500 (OCResourceHandle)NULL,
501 NULL, NULL, 0, 0, NULL,
502 OC_OBSERVE_DEREGISTER,
503 observer->observeId);
504 if(result != OC_STACK_OK)
506 return OC_STACK_ERROR;
508 observer->resource->entityHandler(OC_OBSERVE_FLAG, &ehRequest,
509 observer->resource->entityHandlerCallbackParam);
511 result = DeleteObserverUsingToken (token, tokenLength);
512 if(result == OC_STACK_OK)
514 OC_LOG(DEBUG, TAG, PCF("Removed observer successfully"));
518 result = OC_STACK_OK;
519 OC_LOG(DEBUG, TAG, PCF("Observer Removal failed"));
524 observer->failedCommCount++;
525 result = OC_STACK_CONTINUE;
527 observer->forceHighQos = 1;
528 OC_LOG_V(DEBUG, TAG, "Failed count for this observer is %d",observer->failedCommCount);
532 OC_LOG(ERROR, TAG, PCF("Unknown status"));
533 result = OC_STACK_ERROR;
538 OCStackResult CAToOCStackResult(CAResponseResult_t caCode)
540 OCStackResult ret = OC_STACK_ERROR;
548 ret = OC_STACK_RESOURCE_CREATED;
551 ret = OC_STACK_RESOURCE_DELETED;
554 ret = OC_STACK_INVALID_QUERY;
556 case CA_UNAUTHORIZED_REQ:
557 ret = OC_STACK_UNAUTHORIZED_REQ;
560 ret = OC_STACK_INVALID_OPTION;
563 ret = OC_STACK_NO_RESOURCE;
565 case CA_RETRANSMIT_TIMEOUT:
566 ret = OC_STACK_COMM_ERROR;
574 CAResponseResult_t OCToCAStackResult(OCStackResult ocCode)
576 CAResponseResult_t ret = CA_INTERNAL_SERVER_ERROR;
583 case OC_STACK_RESOURCE_CREATED:
586 case OC_STACK_RESOURCE_DELETED:
589 case OC_STACK_INVALID_QUERY:
592 case OC_STACK_INVALID_OPTION:
595 case OC_STACK_NO_RESOURCE:
598 case OC_STACK_COMM_ERROR:
599 ret = CA_RETRANSMIT_TIMEOUT;
601 case OC_STACK_UNAUTHORIZED_REQ:
602 ret = CA_UNAUTHORIZED_REQ;
610 CATransportFlags_t OCToCATransportFlags(OCTransportFlags ocFlags)
612 CATransportFlags_t caFlags = (CATransportFlags_t)ocFlags;
614 // supply default behavior.
615 if ((caFlags & (CA_IPV6|CA_IPV4)) == 0)
617 caFlags = (CATransportFlags_t)(caFlags|CA_IPV6|CA_IPV4);
619 if ((caFlags & OC_MASK_SCOPE) == 0)
621 caFlags = (CATransportFlags_t)(caFlags|OC_SCOPE_LINK);
626 OCTransportFlags CAToOCTransportFlags(CATransportFlags_t caFlags)
628 return (OCTransportFlags)caFlags;
631 static OCStackResult ResetPresenceTTL(ClientCB *cbNode, uint32_t maxAgeSeconds)
633 uint32_t lowerBound = 0;
634 uint32_t higherBound = 0;
636 if (!cbNode || !cbNode->presence || !cbNode->presence->timeOut)
638 return OC_STACK_INVALID_PARAM;
641 OC_LOG_V(INFO, TAG, "Update presence TTL, time is %u", GetTicks(0));
643 cbNode->presence->TTL = maxAgeSeconds;
645 for (int index = 0; index < PresenceTimeOutSize; index++)
647 // Guard against overflow
648 if (cbNode->presence->TTL < (UINT32_MAX/(MILLISECONDS_PER_SECOND*PresenceTimeOut[index]))
651 lowerBound = GetTicks((PresenceTimeOut[index] *
652 cbNode->presence->TTL *
653 MILLISECONDS_PER_SECOND)/100);
657 lowerBound = GetTicks(UINT32_MAX);
660 if (cbNode->presence->TTL < (UINT32_MAX/(MILLISECONDS_PER_SECOND*PresenceTimeOut[index+1]))
663 higherBound = GetTicks((PresenceTimeOut[index + 1] *
664 cbNode->presence->TTL *
665 MILLISECONDS_PER_SECOND)/100);
669 higherBound = GetTicks(UINT32_MAX);
672 cbNode->presence->timeOut[index] = OCGetRandomRange(lowerBound, higherBound);
674 OC_LOG_V(DEBUG, TAG, "lowerBound timeout %d", lowerBound);
675 OC_LOG_V(DEBUG, TAG, "higherBound timeout %d", higherBound);
676 OC_LOG_V(DEBUG, TAG, "timeOut entry %d", cbNode->presence->timeOut[index]);
679 cbNode->presence->TTLlevel = 0;
681 OC_LOG_V(DEBUG, TAG, "this TTL level %d", cbNode->presence->TTLlevel);
685 const char *convertTriggerEnumToString(OCPresenceTrigger trigger)
687 if (trigger == OC_PRESENCE_TRIGGER_CREATE)
689 return OC_RSRVD_TRIGGER_CREATE;
691 else if (trigger == OC_PRESENCE_TRIGGER_CHANGE)
693 return OC_RSRVD_TRIGGER_CHANGE;
697 return OC_RSRVD_TRIGGER_DELETE;
701 OCPresenceTrigger convertTriggerStringToEnum(const char * triggerStr)
705 return OC_PRESENCE_TRIGGER_CREATE;
707 else if(strcmp(triggerStr, OC_RSRVD_TRIGGER_CREATE) == 0)
709 return OC_PRESENCE_TRIGGER_CREATE;
711 else if(strcmp(triggerStr, OC_RSRVD_TRIGGER_CHANGE) == 0)
713 return OC_PRESENCE_TRIGGER_CHANGE;
717 return OC_PRESENCE_TRIGGER_DELETE;
722 * The cononical presence allows constructed URIs to be string compared.
724 * requestUri must be a char array of size CA_MAX_URI_LENGTH
726 static int FormCanonicalPresenceUri(const CAEndpoint_t *endpoint, char *resourceUri,
729 VERIFY_NON_NULL(endpoint , FATAL, OC_STACK_INVALID_PARAM);
730 VERIFY_NON_NULL(resourceUri, FATAL, OC_STACK_INVALID_PARAM);
731 VERIFY_NON_NULL(presenceUri, FATAL, OC_STACK_INVALID_PARAM);
733 CAEndpoint_t *ep = (CAEndpoint_t *)endpoint;
735 if (ep->adapter == CA_ADAPTER_IP)
737 if ((ep->flags & CA_IPV6) && !(ep->flags & CA_IPV4))
739 if ('\0' == ep->addr[0]) // multicast
741 return snprintf(presenceUri, CA_MAX_URI_LENGTH, OC_RSRVD_PRESENCE_URI);
745 return snprintf(presenceUri, CA_MAX_URI_LENGTH, "coap://[%s]:%u%s",
746 ep->addr, ep->port, OC_RSRVD_PRESENCE_URI);
751 if ('\0' == ep->addr[0]) // multicast
753 OICStrcpy(ep->addr, sizeof(ep->addr), OC_MULTICAST_IP);
754 ep->port = OC_MULTICAST_PORT;
756 return snprintf(presenceUri, CA_MAX_URI_LENGTH, "coap://%s:%u%s",
757 ep->addr, ep->port, OC_RSRVD_PRESENCE_URI);
761 // might work for other adapters (untested, but better than nothing)
762 return snprintf(presenceUri, CA_MAX_URI_LENGTH, "coap://%s%s", ep->addr,
763 OC_RSRVD_PRESENCE_URI);
767 OCStackResult HandlePresenceResponse(const CAEndpoint_t *endpoint,
768 const CAResponseInfo_t *responseInfo)
770 VERIFY_NON_NULL(endpoint, FATAL, OC_STACK_INVALID_PARAM);
771 VERIFY_NON_NULL(responseInfo, FATAL, OC_STACK_INVALID_PARAM);
773 OCStackApplicationResult cbResult = OC_STACK_DELETE_TRANSACTION;
774 ClientCB * cbNode = NULL;
775 char *resourceTypeName = NULL;
776 OCClientResponse response = {.devAddr = {.adapter = OC_DEFAULT_ADAPTER}};
777 OCStackResult result = OC_STACK_ERROR;
780 char presenceUri[CA_MAX_URI_LENGTH];
782 int presenceSubscribe = 0;
783 int multicastPresenceSubscribe = 0;
785 if (responseInfo->result != CA_SUCCESS)
787 OC_LOG_V(ERROR, TAG, "HandlePresenceResponse failed %d", responseInfo->result);
788 return OC_STACK_ERROR;
791 // check for unicast presence
792 uriLen = FormCanonicalPresenceUri(endpoint, OC_RSRVD_PRESENCE_URI, presenceUri);
793 if (uriLen < 0 || (size_t)uriLen >= sizeof (presenceUri))
795 return OC_STACK_INVALID_URI;
798 cbNode = GetClientCB(NULL, 0, NULL, presenceUri);
801 presenceSubscribe = 1;
805 // check for multiicast presence
806 CAEndpoint_t ep = { .adapter = endpoint->adapter,
807 .flags = endpoint->flags };
809 uriLen = FormCanonicalPresenceUri(&ep, OC_RSRVD_PRESENCE_URI, presenceUri);
811 cbNode = GetClientCB(NULL, 0, NULL, presenceUri);
814 multicastPresenceSubscribe = 1;
818 if (!presenceSubscribe && !multicastPresenceSubscribe)
820 OC_LOG(ERROR, TAG, PCF("Received a presence notification, but no callback, ignoring"));
824 response.payload = NULL;
825 response.result = OC_STACK_OK;
827 CopyEndpointToDevAddr(endpoint, &response.devAddr);
828 FixUpClientResponse(&response);
830 if (responseInfo->info.payload)
832 result = OCParsePayload(&response.payload, responseInfo->info.payload,
833 responseInfo->info.payloadSize);
835 if(result != OC_STACK_OK)
837 OC_LOG(ERROR, TAG, PCF("Presence parse failed"));
840 if(!response.payload || response.payload->type != PAYLOAD_TYPE_PRESENCE)
842 OC_LOG(ERROR, TAG, PCF("Presence payload was wrong type"));
843 result = OC_STACK_ERROR;
846 response.sequenceNumber = ((OCPresencePayload*)response.payload)->sequenceNumber;
847 resourceTypeName = ((OCPresencePayload*)response.payload)->resourceType;
848 maxAge = ((OCPresencePayload*)response.payload)->maxAge;
851 if (presenceSubscribe)
853 if(cbNode->sequenceNumber == response.sequenceNumber)
855 OC_LOG(INFO, TAG, PCF("No presence change"));
861 OC_LOG(INFO, TAG, PCF("Stopping presence"));
862 response.result = OC_STACK_PRESENCE_STOPPED;
865 OICFree(cbNode->presence->timeOut);
866 OICFree(cbNode->presence);
867 cbNode->presence = NULL;
872 if(!cbNode->presence)
874 cbNode->presence = (OCPresence *)OICMalloc(sizeof (OCPresence));
876 if(!(cbNode->presence))
878 OC_LOG(ERROR, TAG, PCF("Could not allocate memory for cbNode->presence"));
879 result = OC_STACK_NO_MEMORY;
883 VERIFY_NON_NULL_V(cbNode->presence);
884 cbNode->presence->timeOut = NULL;
885 cbNode->presence->timeOut = (uint32_t *)
886 OICMalloc(PresenceTimeOutSize * sizeof(uint32_t));
887 if(!(cbNode->presence->timeOut)){
889 PCF("Could not allocate memory for cbNode->presence->timeOut"));
890 OICFree(cbNode->presence);
891 result = OC_STACK_NO_MEMORY;
896 ResetPresenceTTL(cbNode, maxAge);
898 cbNode->sequenceNumber = response.sequenceNumber;
900 // Ensure that a filter is actually applied.
901 if( resourceTypeName && cbNode->filterResourceType)
903 if(!findResourceType(cbNode->filterResourceType, resourceTypeName))
912 // This is the multicast case
913 OCMulticastNode* mcNode = NULL;
914 mcNode = GetMCPresenceNode(presenceUri);
918 if(mcNode->nonce == response.sequenceNumber)
920 OC_LOG(INFO, TAG, PCF("No presence change (Multicast)"));
923 mcNode->nonce = response.sequenceNumber;
927 OC_LOG(INFO, TAG, PCF("Stopping presence"));
928 response.result = OC_STACK_PRESENCE_STOPPED;
933 char* uri = OICStrdup(presenceUri);
937 PCF("No Memory for URI to store in the presence node"));
938 result = OC_STACK_NO_MEMORY;
942 result = AddMCPresenceNode(&mcNode, uri, response.sequenceNumber);
943 if(result == OC_STACK_NO_MEMORY)
946 PCF("No Memory for Multicast Presence Node"));
950 // presence node now owns uri
953 // Ensure that a filter is actually applied.
954 if(resourceTypeName && cbNode->filterResourceType)
956 if(!findResourceType(cbNode->filterResourceType, resourceTypeName))
963 cbResult = cbNode->callBack(cbNode->context, cbNode->handle, &response);
965 if (cbResult == OC_STACK_DELETE_TRANSACTION)
967 FindAndDeleteClientCB(cbNode);
971 OCPayloadDestroy(response.payload);
975 void HandleCAResponses(const CAEndpoint_t* endPoint, const CAResponseInfo_t* responseInfo)
977 VERIFY_NON_NULL_NR(endPoint, FATAL);
978 VERIFY_NON_NULL_NR(responseInfo, FATAL);
980 OC_LOG(INFO, TAG, PCF("Enter HandleCAResponses"));
982 if(responseInfo->info.resourceUri &&
983 strcmp(responseInfo->info.resourceUri, OC_RSRVD_PRESENCE_URI) == 0)
985 HandlePresenceResponse(endPoint, responseInfo);
989 ClientCB *cbNode = GetClientCB(responseInfo->info.token,
990 responseInfo->info.tokenLength, NULL, NULL);
992 ResourceObserver * observer = GetObserverUsingToken (responseInfo->info.token,
993 responseInfo->info.tokenLength);
997 OC_LOG(INFO, TAG, PCF("There is a cbNode associated with the response token"));
998 if(responseInfo->result == CA_EMPTY)
1000 OC_LOG(INFO, TAG, PCF("Receiving A ACK/RESET for this token"));
1001 // We do not have a case for the client to receive a RESET
1002 if(responseInfo->info.type == CA_MSG_ACKNOWLEDGE)
1004 //This is the case of receiving an ACK on a request to a slow resource!
1005 OC_LOG(INFO, TAG, PCF("This is a pure ACK"));
1006 //TODO: should we inform the client
1007 // app that at least the request was received at the server?
1010 else if(responseInfo->result == CA_RETRANSMIT_TIMEOUT)
1012 OC_LOG(INFO, TAG, PCF("Receiving A Timeout for this token"));
1013 OC_LOG(INFO, TAG, PCF("Calling into application address space"));
1015 OCClientResponse response =
1016 {.devAddr = {.adapter = OC_DEFAULT_ADAPTER}};
1017 CopyEndpointToDevAddr(endPoint, &response.devAddr);
1018 FixUpClientResponse(&response);
1019 response.resourceUri = responseInfo->info.resourceUri;
1020 memcpy(response.identity.id, responseInfo->info.identity.id,
1021 sizeof (response.identity.id));
1022 response.identity.id_length = responseInfo->info.identity.id_length;
1024 response.result = CAToOCStackResult(responseInfo->result);
1025 cbNode->callBack(cbNode->context,
1026 cbNode->handle, &response);
1027 FindAndDeleteClientCB(cbNode);
1031 OC_LOG(INFO, TAG, PCF("This is a regular response, A client call back is found"));
1032 OC_LOG(INFO, TAG, PCF("Calling into application address space"));
1034 OCClientResponse response =
1035 {.devAddr = {.adapter = OC_DEFAULT_ADAPTER}};
1036 response.sequenceNumber = OC_OBSERVE_NO_OPTION;
1037 CopyEndpointToDevAddr(endPoint, &response.devAddr);
1038 FixUpClientResponse(&response);
1039 response.resourceUri = responseInfo->info.resourceUri;
1040 memcpy(response.identity.id, responseInfo->info.identity.id,
1041 sizeof (response.identity.id));
1042 response.identity.id_length = responseInfo->info.identity.id_length;
1044 response.result = CAToOCStackResult(responseInfo->result);
1045 if(responseInfo->info.payload &&
1046 responseInfo->info.payloadSize &&
1047 OC_STACK_OK != OCParsePayload(&response.payload, responseInfo->info.payload,
1048 responseInfo->info.payloadSize))
1050 OC_LOG(ERROR, TAG, PCF("Error converting payload"));
1051 OCPayloadDestroy(response.payload);
1055 response.numRcvdVendorSpecificHeaderOptions = 0;
1056 if(responseInfo->info.numOptions > 0)
1059 //First option always with option ID is COAP_OPTION_OBSERVE if it is available.
1060 if(responseInfo->info.options[0].optionID == COAP_OPTION_OBSERVE)
1063 uint32_t observationOption;
1064 uint8_t* optionData = (uint8_t*)responseInfo->info.options[0].optionData;
1065 for (observationOption=0, i=0;
1066 i<sizeof(uint32_t) && i<responseInfo->info.options[0].optionLength;
1070 (observationOption << 8) | optionData[i];
1072 response.sequenceNumber = observationOption;
1074 response.numRcvdVendorSpecificHeaderOptions = responseInfo->info.numOptions - 1;
1079 response.numRcvdVendorSpecificHeaderOptions = responseInfo->info.numOptions;
1082 if(response.numRcvdVendorSpecificHeaderOptions > MAX_HEADER_OPTIONS)
1084 OC_LOG(ERROR, TAG, PCF("#header options are more than MAX_HEADER_OPTIONS"));
1085 OCPayloadDestroy(response.payload);
1089 for (uint8_t i = start; i < responseInfo->info.numOptions; i++)
1091 memcpy (&(response.rcvdVendorSpecificHeaderOptions[i-start]),
1092 &(responseInfo->info.options[i]), sizeof(OCHeaderOption));
1096 if (cbNode->method == OC_REST_OBSERVE &&
1097 response.sequenceNumber > OC_OFFSET_SEQUENCE_NUMBER &&
1098 response.sequenceNumber <= cbNode->sequenceNumber)
1100 OC_LOG_V(INFO, TAG, PCF("Received stale notification. Number :%d"),
1101 response.sequenceNumber);
1105 OCStackApplicationResult appFeedback = cbNode->callBack(cbNode->context,
1108 cbNode->sequenceNumber = response.sequenceNumber;
1110 if (appFeedback == OC_STACK_DELETE_TRANSACTION)
1112 FindAndDeleteClientCB(cbNode);
1116 // To keep discovery callbacks active.
1117 cbNode->TTL = GetTicks(MAX_CB_TIMEOUT_SECONDS *
1118 MILLISECONDS_PER_SECOND);
1122 //Need to send ACK when the response is CON
1123 if(responseInfo->info.type == CA_MSG_CONFIRM)
1125 SendDirectStackResponse(endPoint, responseInfo->info.messageId, CA_EMPTY,
1126 CA_MSG_ACKNOWLEDGE, 0, NULL, NULL, 0);
1129 OCPayloadDestroy(response.payload);
1136 OC_LOG(INFO, TAG, PCF("There is an observer associated with the response token"));
1137 if(responseInfo->result == CA_EMPTY)
1139 OC_LOG(INFO, TAG, PCF("Receiving A ACK/RESET for this token"));
1140 if(responseInfo->info.type == CA_MSG_RESET)
1142 OC_LOG(INFO, TAG, PCF("This is a RESET"));
1143 OCStackFeedBack(responseInfo->info.token, responseInfo->info.tokenLength,
1144 OC_OBSERVER_NOT_INTERESTED);
1146 else if(responseInfo->info.type == CA_MSG_ACKNOWLEDGE)
1148 OC_LOG(INFO, TAG, PCF("This is a pure ACK"));
1149 OCStackFeedBack(responseInfo->info.token, responseInfo->info.tokenLength,
1150 OC_OBSERVER_STILL_INTERESTED);
1153 else if(responseInfo->result == CA_RETRANSMIT_TIMEOUT)
1155 OC_LOG(INFO, TAG, PCF("Receiving Time Out for an observer"));
1156 OCStackFeedBack(responseInfo->info.token, responseInfo->info.tokenLength,
1157 OC_OBSERVER_FAILED_COMM);
1162 if(!cbNode && !observer)
1164 if(myStackMode == OC_CLIENT || myStackMode == OC_CLIENT_SERVER)
1166 OC_LOG(INFO, TAG, PCF("This is a client, but no cbNode was found for token"));
1167 if(responseInfo->result == CA_EMPTY)
1169 OC_LOG(INFO, TAG, PCF("Receiving CA_EMPTY in the ocstack"));
1173 OC_LOG(INFO, TAG, PCF("Received a message without callbacks. Sending RESET"));
1174 SendDirectStackResponse(endPoint, responseInfo->info.messageId, CA_EMPTY,
1175 CA_MSG_RESET, 0, NULL, NULL, 0);
1179 if(myStackMode == OC_SERVER || myStackMode == OC_CLIENT_SERVER)
1181 OC_LOG(INFO, TAG, PCF("This is a server, but no observer was found for token"));
1182 if (responseInfo->info.type == CA_MSG_ACKNOWLEDGE)
1184 OC_LOG_V(INFO, TAG, PCF("Received ACK at server for messageId : %d"),
1185 responseInfo->info.messageId);
1187 if (responseInfo->info.type == CA_MSG_RESET)
1189 OC_LOG_V(INFO, TAG, PCF("Received RESET at server for messageId : %d"),
1190 responseInfo->info.messageId);
1197 OC_LOG(INFO, TAG, PCF("Exit HandleCAResponses"));
1201 * This function handles error response from CA
1202 * code shall be added to handle the errors
1204 void HandleCAErrorResponse(const CAEndpoint_t *endPoint, const CAErrorInfo_t *errrorInfo)
1206 OC_LOG(INFO, TAG, PCF("Enter HandleCAErrorResponse"));
1208 if(NULL == endPoint)
1210 OC_LOG(ERROR, TAG, PCF("endPoint is NULL"));
1214 if(NULL == errrorInfo)
1216 OC_LOG(ERROR, TAG, PCF("errrorInfo is NULL"));
1219 OC_LOG(INFO, TAG, PCF("Exit HandleCAErrorResponse"));
1223 * This function sends out Direct Stack Responses. These are responses that are not coming
1224 * from the application entity handler. These responses have no payload and are usually ACKs,
1225 * RESETs or some error conditions that were caught by the stack.
1227 OCStackResult SendDirectStackResponse(const CAEndpoint_t* endPoint, const uint16_t coapID,
1228 const CAResponseResult_t responseResult, const CAMessageType_t type,
1229 const uint8_t numOptions, const CAHeaderOption_t *options,
1230 CAToken_t token, uint8_t tokenLength)
1232 CAResponseInfo_t respInfo = {
1233 .result = responseResult
1235 respInfo.info.messageId = coapID;
1236 respInfo.info.numOptions = numOptions;
1237 respInfo.info.options = (CAHeaderOption_t*)options;
1238 respInfo.info.payload = NULL;
1239 respInfo.info.token = token;
1240 respInfo.info.tokenLength = tokenLength;
1241 respInfo.info.type = type;
1243 CAResult_t caResult = CASendResponse(endPoint, &respInfo);
1244 if(caResult != CA_STATUS_OK)
1246 OC_LOG(ERROR, TAG, PCF("CASendResponse error"));
1247 return OC_STACK_ERROR;
1252 //This function will be called back by CA layer when a request is received
1253 void HandleCARequests(const CAEndpoint_t* endPoint, const CARequestInfo_t* requestInfo)
1255 OC_LOG(INFO, TAG, PCF("Enter HandleCARequests"));
1258 OC_LOG(ERROR, TAG, PCF("endPoint is NULL"));
1264 OC_LOG(ERROR, TAG, PCF("requestInfo is NULL"));
1268 OCStackResult requestResult = OC_STACK_ERROR;
1270 if(myStackMode == OC_CLIENT)
1272 //TODO: should the client be responding to requests?
1276 OCServerProtocolRequest serverRequest = {0};
1278 OC_LOG_V(INFO, TAG, PCF("Endpoint URI : %s"), requestInfo->info.resourceUri);
1280 char * uriWithoutQuery = NULL;
1281 char * query = NULL;
1283 requestResult = getQueryFromUri(requestInfo->info.resourceUri, &query, &uriWithoutQuery);
1285 if (requestResult != OC_STACK_OK || !uriWithoutQuery)
1287 OC_LOG_V(ERROR, TAG, "getQueryFromUri() failed with OC error code %d\n", requestResult);
1290 OC_LOG_V(INFO, TAG, PCF("URI without query: %s"), uriWithoutQuery);
1291 OC_LOG_V(INFO, TAG, PCF("Query : %s"), query);
1293 if(strlen(uriWithoutQuery) < MAX_URI_LENGTH)
1295 OICStrcpy(serverRequest.resourceUrl, sizeof(serverRequest.resourceUrl), uriWithoutQuery);
1296 OICFree(uriWithoutQuery);
1300 OC_LOG(ERROR, TAG, PCF("URI length exceeds MAX_URI_LENGTH."));
1301 OICFree(uriWithoutQuery);
1308 if(strlen(query) < MAX_QUERY_LENGTH)
1310 OICStrcpy(serverRequest.query, sizeof(serverRequest.query), query);
1315 OC_LOG(ERROR, TAG, PCF("Query length exceeds MAX_QUERY_LENGTH."));
1321 if ((requestInfo->info.payload) && (0 < requestInfo->info.payloadSize))
1323 serverRequest.reqTotalSize = requestInfo->info.payloadSize;
1324 serverRequest.payload = (uint8_t *) OICMalloc(requestInfo->info.payloadSize);
1325 memcpy (serverRequest.payload, requestInfo->info.payload,
1326 requestInfo->info.payloadSize);
1330 serverRequest.reqTotalSize = 0;
1333 switch (requestInfo->method)
1336 serverRequest.method = OC_REST_GET;
1339 serverRequest.method = OC_REST_PUT;
1342 serverRequest.method = OC_REST_POST;
1345 serverRequest.method = OC_REST_DELETE;
1348 OC_LOG_V(ERROR, TAG, "Received CA method %d not supported", requestInfo->method);
1349 SendDirectStackResponse(endPoint, requestInfo->info.messageId, CA_BAD_REQ,
1350 requestInfo->info.type, requestInfo->info.numOptions,
1351 requestInfo->info.options, requestInfo->info.token,
1352 requestInfo->info.tokenLength);
1353 OICFree(serverRequest.payload);
1357 OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)requestInfo->info.token,
1358 requestInfo->info.tokenLength);
1359 serverRequest.requestToken = (CAToken_t)OICMalloc(requestInfo->info.tokenLength);
1360 serverRequest.tokenLength = requestInfo->info.tokenLength;
1362 if (!serverRequest.requestToken)
1364 OC_LOG(FATAL, TAG, "Allocation for token failed.");
1365 SendDirectStackResponse(endPoint, requestInfo->info.messageId, CA_INTERNAL_SERVER_ERROR,
1366 requestInfo->info.type, requestInfo->info.numOptions,
1367 requestInfo->info.options, requestInfo->info.token,
1368 requestInfo->info.tokenLength);
1369 OICFree(serverRequest.payload);
1372 memcpy(serverRequest.requestToken, requestInfo->info.token, requestInfo->info.tokenLength);
1374 if (requestInfo->info.type == CA_MSG_CONFIRM)
1376 serverRequest.qos = OC_HIGH_QOS;
1380 serverRequest.qos = OC_LOW_QOS;
1382 // CA does not need the following field
1383 // Are we sure CA does not need them? how is it responding to multicast
1384 serverRequest.delayedResNeeded = 0;
1386 serverRequest.coapID = requestInfo->info.messageId;
1388 CopyEndpointToDevAddr(endPoint, &serverRequest.devAddr);
1390 // copy vendor specific header options
1391 uint8_t tempNum = (requestInfo->info.numOptions);
1392 GetObserveHeaderOption(&serverRequest.observationOption, requestInfo->info.options, &tempNum);
1393 if (requestInfo->info.numOptions > MAX_HEADER_OPTIONS)
1396 PCF("The request info numOptions is greater than MAX_HEADER_OPTIONS"));
1397 SendDirectStackResponse(endPoint, requestInfo->info.messageId, CA_BAD_OPT,
1398 requestInfo->info.type, requestInfo->info.numOptions,
1399 requestInfo->info.options, requestInfo->info.token,
1400 requestInfo->info.tokenLength);
1401 OICFree(serverRequest.payload);
1402 OICFree(serverRequest.requestToken);
1405 serverRequest.numRcvdVendorSpecificHeaderOptions = tempNum;
1406 if (serverRequest.numRcvdVendorSpecificHeaderOptions)
1408 memcpy (&(serverRequest.rcvdVendorSpecificHeaderOptions), requestInfo->info.options,
1409 sizeof(CAHeaderOption_t)*tempNum);
1412 requestResult = HandleStackRequests (&serverRequest);
1414 // Send ACK to client as precursor to slow response
1415 if(requestResult == OC_STACK_SLOW_RESOURCE)
1417 SendDirectStackResponse(endPoint, requestInfo->info.messageId, CA_EMPTY,
1418 CA_MSG_ACKNOWLEDGE,0, NULL, NULL, 0);
1420 else if(requestResult != OC_STACK_OK)
1422 OC_LOG_V(ERROR, TAG, PCF("HandleStackRequests failed. error: %d"), requestResult);
1424 CAResponseResult_t stackResponse = OCToCAStackResult(requestResult);
1426 SendDirectStackResponse(endPoint, requestInfo->info.messageId, stackResponse,
1427 requestInfo->info.type, requestInfo->info.numOptions,
1428 requestInfo->info.options, requestInfo->info.token,
1429 requestInfo->info.tokenLength);
1431 // requestToken is fed to HandleStackRequests, which then goes to AddServerRequest.
1432 // The token is copied in there, and is thus still owned by this function.
1433 OICFree(serverRequest.payload);
1434 OICFree(serverRequest.requestToken);
1435 OC_LOG(INFO, TAG, PCF("Exit HandleCARequests"));
1438 OCStackResult HandleStackRequests(OCServerProtocolRequest * protocolRequest)
1440 OC_LOG(INFO, TAG, PCF("Entering HandleStackRequests (OCStack Layer)"));
1441 OCStackResult result = OC_STACK_ERROR;
1442 ResourceHandling resHandling;
1443 OCResource *resource;
1444 if(!protocolRequest)
1446 OC_LOG(ERROR, TAG, PCF("protocolRequest is NULL"));
1447 return OC_STACK_INVALID_PARAM;
1450 OCServerRequest * request = GetServerRequestUsingToken(protocolRequest->requestToken,
1451 protocolRequest->tokenLength);
1454 OC_LOG(INFO, TAG, PCF("This is a new Server Request"));
1455 result = AddServerRequest(&request, protocolRequest->coapID,
1456 protocolRequest->delayedResNeeded, 0,
1457 protocolRequest->method, protocolRequest->numRcvdVendorSpecificHeaderOptions,
1458 protocolRequest->observationOption, protocolRequest->qos,
1459 protocolRequest->query, protocolRequest->rcvdVendorSpecificHeaderOptions,
1460 protocolRequest->payload, protocolRequest->requestToken,
1461 protocolRequest->tokenLength,
1462 protocolRequest->resourceUrl, protocolRequest->reqTotalSize,
1463 &protocolRequest->devAddr);
1464 if (OC_STACK_OK != result)
1466 OC_LOG(ERROR, TAG, PCF("Error adding server request"));
1472 OC_LOG(ERROR, TAG, PCF("Out of Memory"));
1473 return OC_STACK_NO_MEMORY;
1476 if(!protocolRequest->reqMorePacket)
1478 request->requestComplete = 1;
1483 OC_LOG(INFO, TAG, PCF("This is either a repeated or blocked Server Request"));
1486 if(request->requestComplete)
1488 OC_LOG(INFO, TAG, PCF("This Server Request is complete"));
1489 result = DetermineResourceHandling (request, &resHandling, &resource);
1490 if (result == OC_STACK_OK)
1492 result = ProcessRequest(resHandling, resource, request);
1497 OC_LOG(INFO, TAG, PCF("This Server Request is incomplete"));
1498 result = OC_STACK_CONTINUE;
1503 bool validatePlatformInfo(OCPlatformInfo info)
1506 if (!info.platformID)
1508 OC_LOG(ERROR, TAG, PCF("No platform ID found."));
1512 if (info.manufacturerName)
1514 size_t lenManufacturerName = strlen(info.manufacturerName);
1516 if(lenManufacturerName == 0 || lenManufacturerName > MAX_MANUFACTURER_NAME_LENGTH)
1518 OC_LOG(ERROR, TAG, PCF("Manufacturer name fails length requirements."));
1524 OC_LOG(ERROR, TAG, PCF("No manufacturer name present"));
1528 if (info.manufacturerUrl)
1530 if(strlen(info.manufacturerUrl) > MAX_MANUFACTURER_URL_LENGTH)
1532 OC_LOG(ERROR, TAG, PCF("Manufacturer url fails length requirements."));
1539 //-----------------------------------------------------------------------------
1541 //-----------------------------------------------------------------------------
1543 OCStackResult OCSetRAInfo(const OCRAInfo_t *raInfo)
1546 !raInfo->username ||
1547 !raInfo->hostname ||
1548 !raInfo->xmpp_domain)
1551 return OC_STACK_INVALID_PARAM;
1553 OCStackResult result = CAResultToOCResult(CASetRAInfo((const CARAInfo_t *) raInfo));
1554 gRASetInfo = (result == OC_STACK_OK)? true : false;
1560 OCStackResult OCInit(const char *ipAddr, uint16_t port, OCMode mode)
1564 return OCInit1(mode, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS);
1567 OCStackResult OCInit1(OCMode mode, OCTransportFlags serverFlags, OCTransportFlags clientFlags)
1569 if(stackState == OC_STACK_INITIALIZED)
1571 OC_LOG(INFO, TAG, PCF("Subsequent calls to OCInit() without calling \
1572 OCStop() between them are ignored."));
1579 OC_LOG(ERROR, TAG, PCF("Need to call OCSetRAInfo before calling OCInit"));
1580 return OC_STACK_ERROR;
1584 OCStackResult result = OC_STACK_ERROR;
1585 OC_LOG(INFO, TAG, PCF("Entering OCInit"));
1588 if (!((mode == OC_CLIENT) || (mode == OC_SERVER) || (mode == OC_CLIENT_SERVER)))
1590 OC_LOG(ERROR, TAG, PCF("Invalid mode"));
1591 return OC_STACK_ERROR;
1595 if (mode == OC_CLIENT || mode == OC_CLIENT_SERVER)
1597 caglobals.client = true;
1599 if (mode == OC_SERVER || mode == OC_CLIENT_SERVER)
1601 caglobals.server = true;
1604 caglobals.serverFlags = (CATransportFlags_t)serverFlags;
1605 if (!(caglobals.serverFlags & CA_IPFAMILY_MASK))
1607 caglobals.serverFlags = (CATransportFlags_t)(caglobals.serverFlags|CA_IPV4|CA_IPV6);
1609 caglobals.clientFlags = (CATransportFlags_t)clientFlags;
1610 if (!(caglobals.clientFlags & CA_IPFAMILY_MASK))
1612 caglobals.clientFlags = (CATransportFlags_t)(caglobals.clientFlags|CA_IPV4|CA_IPV6);
1615 defaultDeviceHandler = NULL;
1616 defaultDeviceHandlerCallbackParameter = NULL;
1619 result = CAResultToOCResult(CAInitialize());
1620 VERIFY_SUCCESS(result, OC_STACK_OK);
1622 result = CAResultToOCResult(OCSelectNetwork());
1623 VERIFY_SUCCESS(result, OC_STACK_OK);
1625 switch (myStackMode)
1628 CARegisterHandler(HandleCARequests, HandleCAResponses, HandleCAErrorResponse);
1629 result = CAResultToOCResult(CAStartDiscoveryServer());
1630 OC_LOG(INFO, TAG, PCF("Client mode: CAStartDiscoveryServer"));
1633 SRMRegisterHandler(HandleCARequests, HandleCAResponses, HandleCAErrorResponse);
1634 result = CAResultToOCResult(CAStartListeningServer());
1635 OC_LOG(INFO, TAG, PCF("Server mode: CAStartListeningServer"));
1637 case OC_CLIENT_SERVER:
1638 SRMRegisterHandler(HandleCARequests, HandleCAResponses, HandleCAErrorResponse);
1639 result = CAResultToOCResult(CAStartListeningServer());
1640 if(result == OC_STACK_OK)
1642 result = CAResultToOCResult(CAStartDiscoveryServer());
1646 VERIFY_SUCCESS(result, OC_STACK_OK);
1648 #ifdef WITH_PRESENCE
1649 PresenceTimeOutSize = sizeof (PresenceTimeOut) / sizeof (PresenceTimeOut[0]) - 1;
1650 #endif // WITH_PRESENCE
1652 //Update Stack state to initialized
1653 stackState = OC_STACK_INITIALIZED;
1655 // Initialize resource
1656 if(myStackMode != OC_CLIENT)
1658 result = initResources();
1661 // Initialize the SRM Policy Engine
1662 if(result == OC_STACK_OK)
1664 result = SRMInitPolicyEngine();
1665 // TODO after BeachHead delivery: consolidate into single SRMInit()
1669 if(result != OC_STACK_OK)
1671 OC_LOG(ERROR, TAG, PCF("Stack initialization error"));
1672 deleteAllResources();
1674 stackState = OC_STACK_UNINITIALIZED;
1679 OCStackResult OCStop()
1681 OC_LOG(INFO, TAG, PCF("Entering OCStop"));
1683 if (stackState == OC_STACK_UNINIT_IN_PROGRESS)
1685 OC_LOG(DEBUG, TAG, PCF("Stack already stopping, exiting"));
1688 else if (stackState != OC_STACK_INITIALIZED)
1690 OC_LOG(ERROR, TAG, PCF("Stack not initialized"));
1691 return OC_STACK_ERROR;
1694 stackState = OC_STACK_UNINIT_IN_PROGRESS;
1696 #ifdef WITH_PRESENCE
1697 // Ensure that the TTL associated with ANY and ALL presence notifications originating from
1698 // here send with the code "OC_STACK_PRESENCE_STOPPED" result.
1699 presenceResource.presenceTTL = 0;
1700 #endif // WITH_PRESENCE
1702 // Free memory dynamically allocated for resources
1703 deleteAllResources();
1705 DeletePlatformInfo();
1707 // Remove all observers
1708 DeleteObserverList();
1709 // Remove all the client callbacks
1710 DeleteClientCBList();
1712 // De-init the SRM Policy Engine
1713 // TODO after BeachHead delivery: consolidate into single SRMDeInit()
1714 SRMDeInitPolicyEngine();
1717 stackState = OC_STACK_UNINITIALIZED;
1721 CAMessageType_t qualityOfServiceToMessageType(OCQualityOfService qos)
1726 return CA_MSG_CONFIRM;
1731 return CA_MSG_NONCONFIRM;
1735 OCStackResult verifyUriQueryLength(const char *inputUri, uint16_t uriLen)
1739 query = strchr (inputUri, '?');
1743 if((query - inputUri) > MAX_URI_LENGTH)
1745 return OC_STACK_INVALID_URI;
1748 if((inputUri + uriLen - 1 - query) > MAX_QUERY_LENGTH)
1750 return OC_STACK_INVALID_QUERY;
1753 else if(uriLen > MAX_URI_LENGTH)
1755 return OC_STACK_INVALID_URI;
1761 * A request uri consists of the following components in order:
1763 * optional prefix "coap://"
1765 * IPv6 address "[1234::5678]"
1766 * IPv4 address "192.168.1.1"
1767 * optional port ":5683"
1768 * resource uri "/oc/core..."
1770 * for PRESENCE requests, extract resource type.
1772 static OCStackResult ParseRequestUri(const char *fullUri,
1773 OCTransportAdapter adapter,
1774 OCTransportFlags flags,
1775 OCDevAddr **devAddr,
1777 char **resourceType)
1779 VERIFY_NON_NULL(fullUri, FATAL, OC_STACK_INVALID_CALLBACK);
1781 OCStackResult result = OC_STACK_OK;
1782 OCDevAddr *da = NULL;
1786 // provide defaults for all returned values
1793 *resourceUri = NULL;
1797 *resourceType = NULL;
1800 // delimit url prefix, if any
1801 const char *start = fullUri;
1802 char *slash2 = strstr(start, "//");
1807 char *slash = strchr(start, '/');
1810 return OC_STACK_INVALID_URI;
1813 // processs url prefix, if any
1814 size_t urlLen = slash - start;
1818 if (urlLen && devAddr)
1819 { // construct OCDevAddr
1820 if (start[0] == '[')
1822 char *close = strchr(++start, ']');
1823 if (!close || close > slash)
1825 return OC_STACK_INVALID_URI;
1828 if (close[1] == ':')
1832 adapter = (OCTransportAdapter)(adapter | OC_ADAPTER_IP);
1833 flags = (OCTransportFlags)(flags | OC_IP_USE_V6);
1837 char *dot = strchr(start, '.');
1838 if (dot && dot < slash)
1840 colon = strchr(start, ':');
1841 end = (colon && colon < slash) ? colon : slash;
1842 adapter = (OCTransportAdapter)(adapter | OC_ADAPTER_IP);
1843 flags = (OCTransportFlags)(flags | OC_IP_USE_V4);
1851 if (len >= sizeof(da->addr))
1853 return OC_STACK_INVALID_URI;
1855 // collect port, if any
1856 if (colon && colon < slash)
1858 for (colon++; colon < slash; colon++)
1861 if (c < '0' || c > '9')
1863 return OC_STACK_INVALID_URI;
1865 port = 10 * port + c - '0';
1869 da = (OCDevAddr *)OICCalloc(1, sizeof (OCDevAddr));
1872 if (len >= sizeof(da->addr))
1874 return OC_STACK_INVALID_URI;
1877 da = (OCDevAddr *)OICCalloc(sizeof (OCDevAddr), 1);
1881 return OC_STACK_NO_MEMORY;
1883 OICStrcpyPartial(da->addr, sizeof(da->addr), start, len);
1885 da->adapter = adapter;
1887 if (!strncmp(fullUri, "coaps:", 6))
1889 da->flags = (OCTransportFlags)(da->flags|CA_SECURE);
1894 // process resource uri, if any
1896 { // request uri and query
1897 size_t ulen = strlen(slash); // resource uri length
1898 size_t tlen = 0; // resource type length
1901 static const char strPresence[] = "/oic/ad?rt=";
1902 static const size_t lenPresence = sizeof(strPresence) - 1;
1903 if (!strncmp(slash, strPresence, lenPresence))
1905 type = slash + lenPresence;
1906 tlen = ulen - lenPresence;
1911 *resourceUri = (char *)OICMalloc(ulen + 1);
1914 result = OC_STACK_NO_MEMORY;
1917 strcpy(*resourceUri, slash);
1920 if (type && resourceType)
1922 *resourceType = (char *)OICMalloc(tlen + 1);
1925 result = OC_STACK_NO_MEMORY;
1929 OICStrcpy(*resourceType, (tlen+1), type);
1936 // free all returned values
1943 OICFree(*resourceUri);
1947 OICFree(*resourceType);
1952 static OCStackResult OCPreparePresence(CAEndpoint_t *endpoint,
1953 char *resourceUri, char **requestUri)
1955 char uri[CA_MAX_URI_LENGTH];
1957 FormCanonicalPresenceUri(endpoint, resourceUri, uri);
1959 *requestUri = OICStrdup(uri);
1962 return OC_STACK_NO_MEMORY;
1969 * Discover or Perform requests on a specified resource
1971 OCStackResult OCDoResource(OCDoHandle *handle,
1973 const char *requestUri,
1974 const OCDevAddr *destination,
1976 OCConnectivityType connectivityType,
1977 OCQualityOfService qos,
1978 OCCallbackData *cbData,
1979 OCHeaderOption *options,
1982 OC_LOG(INFO, TAG, PCF("Entering OCDoResource"));
1984 // Validate input parameters
1985 VERIFY_NON_NULL(cbData, FATAL, OC_STACK_INVALID_CALLBACK);
1986 VERIFY_NON_NULL(cbData->cb, FATAL, OC_STACK_INVALID_CALLBACK);
1987 VERIFY_NON_NULL(requestUri , FATAL, OC_STACK_INVALID_URI);
1989 OCStackResult result = OC_STACK_ERROR;
1990 CAResult_t caResult;
1991 CAToken_t token = NULL;
1992 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
1993 ClientCB *clientCB = NULL;
1994 OCDoHandle resHandle = NULL;
1995 CAEndpoint_t endpoint = {.adapter = CA_DEFAULT_ADAPTER};
1996 OCDevAddr tmpDevAddr = { OC_DEFAULT_ADAPTER };
1998 OCTransportAdapter adapter;
1999 OCTransportFlags flags;
2000 // the request contents are put here
2001 CARequestInfo_t requestInfo = {.method = CA_GET};
2002 // requestUri will be parsed into the following three variables
2003 OCDevAddr *devAddr = NULL;
2004 char *resourceUri = NULL;
2005 char *resourceType = NULL;
2007 // To track if memory is allocated for additional header options
2008 uint8_t hdrOptionMemAlloc = 0;
2010 // This validation is broken, but doesn't cause harm
2011 size_t uriLen = strlen(requestUri );
2012 if ((result = verifyUriQueryLength(requestUri , uriLen)) != OC_STACK_OK)
2018 * Support original behavior with address on resourceUri argument.
2020 adapter = (OCTransportAdapter)(connectivityType >> CT_ADAPTER_SHIFT);
2021 flags = (OCTransportFlags)(connectivityType & CT_MASK_FLAGS);
2023 result = ParseRequestUri(requestUri, adapter, flags, &devAddr, &resourceUri, &resourceType);
2025 if (result != OC_STACK_OK)
2027 OC_LOG_V(DEBUG, TAG, "Unable to parse uri: %s", requestUri);
2034 case OC_REST_OBSERVE:
2035 case OC_REST_OBSERVE_ALL:
2036 case OC_REST_CANCEL_OBSERVE:
2037 requestInfo.method = CA_GET;
2040 requestInfo.method = CA_PUT;
2043 requestInfo.method = CA_POST;
2045 case OC_REST_DELETE:
2046 requestInfo.method = CA_DELETE;
2048 case OC_REST_DISCOVER:
2050 if (destination || devAddr)
2052 requestInfo.isMulticast = false;
2056 tmpDevAddr.adapter = adapter;
2057 tmpDevAddr.flags = flags;
2058 destination = &tmpDevAddr;
2059 requestInfo.isMulticast = true;
2061 // CA_DISCOVER will become GET and isMulticast
2062 requestInfo.method = CA_GET;
2064 #ifdef WITH_PRESENCE
2065 case OC_REST_PRESENCE:
2066 // Replacing method type with GET because "presence"
2067 // is a stack layer only implementation.
2068 requestInfo.method = CA_GET;
2072 result = OC_STACK_INVALID_METHOD;
2076 if (!devAddr && !destination)
2078 OC_LOG_V(DEBUG, TAG, "no devAddr and no destination");
2079 result = OC_STACK_INVALID_PARAM;
2083 /* If not original behavior, use destination argument */
2084 if (destination && !devAddr)
2086 devAddr = (OCDevAddr *)OICMalloc(sizeof (OCDevAddr));
2089 result = OC_STACK_NO_MEMORY;
2092 *devAddr = *destination;
2095 resHandle = GenerateInvocationHandle();
2098 result = OC_STACK_NO_MEMORY;
2102 caResult = CAGenerateToken(&token, tokenLength);
2103 if (caResult != CA_STATUS_OK)
2105 OC_LOG(ERROR, TAG, PCF("CAGenerateToken error"));
2106 result= OC_STACK_ERROR;
2110 // fill in request data
2111 requestInfo.info.type = qualityOfServiceToMessageType(qos);
2112 requestInfo.info.token = token;
2113 requestInfo.info.tokenLength = tokenLength;
2114 requestInfo.info.resourceUri = resourceUri;
2116 if ((method == OC_REST_OBSERVE) || (method == OC_REST_OBSERVE_ALL))
2118 result = CreateObserveHeaderOption (&(requestInfo.info.options),
2119 options, numOptions, OC_OBSERVE_REGISTER);
2120 if (result != OC_STACK_OK)
2124 hdrOptionMemAlloc = 1;
2125 requestInfo.info.numOptions = numOptions + 1;
2129 requestInfo.info.options = (CAHeaderOption_t*)options;
2130 requestInfo.info.numOptions = numOptions;
2133 CopyDevAddrToEndpoint(devAddr, &endpoint);
2138 OCConvertPayload(payload, &requestInfo.info.payload, &requestInfo.info.payloadSize))
2141 OC_LOG(ERROR, TAG, PCF("Failed to create CBOR Payload"));
2147 requestInfo.info.payload = NULL;
2148 requestInfo.info.payloadSize = 0;
2151 if (result != OC_STACK_OK)
2153 OC_LOG(ERROR, TAG, PCF("CACreateEndpoint error"));
2157 // prepare for response
2158 #ifdef WITH_PRESENCE
2159 if (method == OC_REST_PRESENCE)
2161 char *presenceUri = NULL;
2162 result = OCPreparePresence(&endpoint, resourceUri, &presenceUri);
2163 if (OC_STACK_OK != result)
2168 // Assign full presence uri as coap://ip:port/oic/ad to add to callback list.
2169 // Presence notification will form a canonical uri to
2170 // look for callbacks into the application.
2171 resourceUri = presenceUri;
2175 ttl = GetTicks(MAX_CB_TIMEOUT_SECONDS * MILLISECONDS_PER_SECOND);
2176 result = AddClientCB(&clientCB, cbData, token, tokenLength, &resHandle,
2177 method, devAddr, resourceUri, resourceType, ttl);
2178 if (OC_STACK_OK != result)
2183 devAddr = NULL; // Client CB list entry now owns it
2184 resourceUri = NULL; // Client CB list entry now owns it
2185 resourceType = NULL; // Client CB list entry now owns it
2188 caResult = CASendRequest(&endpoint, &requestInfo);
2189 if (caResult != CA_STATUS_OK)
2191 OC_LOG(ERROR, TAG, PCF("CASendRequest"));
2192 result = OC_STACK_COMM_ERROR;
2198 *handle = resHandle;
2202 if (result != OC_STACK_OK)
2204 OC_LOG(ERROR, TAG, PCF("OCDoResource error"));
2205 FindAndDeleteClientCB(clientCB);
2206 CADestroyToken(token);
2214 // This is the owner of the payload object, so we free it
2215 OCPayloadDestroy(payload);
2216 OICFree(requestInfo.info.payload);
2218 OICFree(resourceUri);
2219 OICFree(resourceType);
2220 if (hdrOptionMemAlloc)
2222 OICFree(requestInfo.info.options);
2227 OCStackResult OCCancel(OCDoHandle handle, OCQualityOfService qos, OCHeaderOption * options,
2231 * This ftn is implemented one of two ways in the case of observation:
2233 * 1. qos == OC_NON_CONFIRMABLE. When observe is unobserved..
2234 * Remove the callback associated on client side.
2235 * When the next notification comes in from server,
2236 * reply with RESET message to server.
2237 * Keep in mind that the server will react to RESET only
2238 * if the last notification was sent as CON
2240 * 2. qos == OC_CONFIRMABLE. When OCCancel is called,
2241 * and it is associated with an observe request
2242 * (i.e. ClientCB->method == OC_REST_OBSERVE || OC_REST_OBSERVE_ALL),
2243 * Send CON Observe request to server with
2244 * observe flag = OC_RESOURCE_OBSERVE_DEREGISTER.
2245 * Remove the callback associated on client side.
2247 OCStackResult ret = OC_STACK_OK;
2248 CAEndpoint_t endpoint = {.adapter = CA_DEFAULT_ADAPTER};
2249 CAResult_t caResult;
2250 CAInfo_t requestData = {.type = CA_MSG_CONFIRM};
2251 CARequestInfo_t requestInfo = {.method = CA_GET};
2255 return OC_STACK_INVALID_PARAM;
2258 ClientCB *clientCB = GetClientCB(NULL, 0, handle, NULL);
2261 OC_LOG(ERROR, TAG, PCF("Client callback not found. Called OCCancel twice?"));
2265 switch (clientCB->method)
2267 case OC_REST_OBSERVE:
2268 case OC_REST_OBSERVE_ALL:
2269 OC_LOG_V(INFO, TAG, "Canceling observation for resource %s",
2270 clientCB->requestUri);
2271 if (qos != OC_HIGH_QOS)
2273 FindAndDeleteClientCB(clientCB);
2278 OC_LOG(INFO, TAG, PCF("Cancelling observation as CONFIRMABLE"));
2281 requestData.type = qualityOfServiceToMessageType(qos);
2282 requestData.token = clientCB->token;
2283 requestData.tokenLength = clientCB->tokenLength;
2284 if (CreateObserveHeaderOption (&(requestData.options),
2285 options, numOptions, OC_OBSERVE_DEREGISTER) != OC_STACK_OK)
2287 return OC_STACK_ERROR;
2289 requestData.numOptions = numOptions + 1;
2290 requestData.resourceUri = OICStrdup (clientCB->requestUri);
2292 requestInfo.method = CA_GET;
2293 requestInfo.info = requestData;
2295 CopyDevAddrToEndpoint(clientCB->devAddr, &endpoint);
2298 caResult = CASendRequest(&endpoint, &requestInfo);
2299 if (caResult != CA_STATUS_OK)
2301 OC_LOG(ERROR, TAG, PCF("CASendRequest error"));
2302 ret = OC_STACK_ERROR;
2304 ret = CAResultToOCResult (caResult);
2307 #ifdef WITH_PRESENCE
2308 case OC_REST_PRESENCE:
2309 FindAndDeleteClientCB(clientCB);
2314 ret = OC_STACK_INVALID_METHOD;
2319 if (requestData.numOptions > 0)
2321 OICFree(requestData.options);
2323 if (requestData.resourceUri)
2325 OICFree (requestData.resourceUri);
2331 * @brief Register Persistent storage callback.
2332 * @param persistentStorageHandler [IN] Pointers to open, read, write, close & unlink handlers.
2334 * OC_STACK_OK - No errors; Success
2335 * OC_STACK_INVALID_PARAM - Invalid parameter
2337 OCStackResult OCRegisterPersistentStorageHandler(OCPersistentStorage* persistentStorageHandler)
2339 OC_LOG(INFO, TAG, PCF("RegisterPersistentStorageHandler !!"));
2340 if(!persistentStorageHandler)
2342 OC_LOG(ERROR, TAG, PCF("The persistent storage handler is invalid"));
2343 return OC_STACK_INVALID_PARAM;
2347 if( !persistentStorageHandler->open ||
2348 !persistentStorageHandler->close ||
2349 !persistentStorageHandler->read ||
2350 !persistentStorageHandler->unlink ||
2351 !persistentStorageHandler->write)
2353 OC_LOG(ERROR, TAG, PCF("The persistent storage handler is invalid"));
2354 return OC_STACK_INVALID_PARAM;
2357 return SRMRegisterPersistentStorageHandler(persistentStorageHandler);
2360 #ifdef WITH_PRESENCE
2362 OCStackResult OCProcessPresence()
2364 OCStackResult result = OC_STACK_OK;
2366 // the following line floods the log with messages that are irrelevant
2367 // to most purposes. Uncomment as needed.
2368 //OC_LOG(INFO, TAG, PCF("Entering RequestPresence"));
2369 ClientCB* cbNode = NULL;
2370 OCClientResponse clientResponse;
2371 OCStackApplicationResult cbResult = OC_STACK_DELETE_TRANSACTION;
2373 LL_FOREACH(cbList, cbNode)
2375 if (OC_REST_PRESENCE != cbNode->method || !cbNode->presence)
2380 uint32_t now = GetTicks(0);
2381 OC_LOG_V(DEBUG, TAG, "this TTL level %d",
2382 cbNode->presence->TTLlevel);
2383 OC_LOG_V(DEBUG, TAG, "current ticks %d", now);
2385 if (cbNode->presence->TTLlevel > PresenceTimeOutSize)
2390 if (cbNode->presence->TTLlevel < PresenceTimeOutSize)
2392 OC_LOG_V(DEBUG, TAG, "timeout ticks %d",
2393 cbNode->presence->timeOut[cbNode->presence->TTLlevel]);
2395 if (cbNode->presence->TTLlevel >= PresenceTimeOutSize)
2397 OC_LOG(DEBUG, TAG, PCF("No more timeout ticks"));
2399 clientResponse.sequenceNumber = 0;
2400 clientResponse.result = OC_STACK_PRESENCE_TIMEOUT;
2401 clientResponse.devAddr = *cbNode->devAddr;
2402 FixUpClientResponse(&clientResponse);
2403 clientResponse.payload = NULL;
2405 // Increment the TTLLevel (going to a next state), so we don't keep
2406 // sending presence notification to client.
2407 cbNode->presence->TTLlevel++;
2408 OC_LOG_V(DEBUG, TAG, "moving to TTL level %d",
2409 cbNode->presence->TTLlevel);
2411 cbResult = cbNode->callBack(cbNode->context, cbNode->handle, &clientResponse);
2412 if (cbResult == OC_STACK_DELETE_TRANSACTION)
2414 FindAndDeleteClientCB(cbNode);
2418 if (now < cbNode->presence->timeOut[cbNode->presence->TTLlevel])
2423 CAResult_t caResult = CA_STATUS_OK;
2424 CAEndpoint_t endpoint = {.adapter = CA_DEFAULT_ADAPTER};
2425 CAInfo_t requestData = {.type = CA_MSG_CONFIRM};
2426 CARequestInfo_t requestInfo = {.method = CA_GET};
2428 OC_LOG(DEBUG, TAG, PCF("time to test server presence"));
2430 CopyDevAddrToEndpoint(cbNode->devAddr, &endpoint);
2432 requestData.type = CA_MSG_NONCONFIRM;
2433 requestData.token = cbNode->token;
2434 requestData.tokenLength = cbNode->tokenLength;
2435 requestData.resourceUri = OC_RSRVD_PRESENCE_URI;
2436 requestInfo.method = CA_GET;
2437 requestInfo.info = requestData;
2439 caResult = CASendRequest(&endpoint, &requestInfo);
2441 if (caResult != CA_STATUS_OK)
2443 OC_LOG(ERROR, TAG, PCF("CASendRequest error"));
2447 cbNode->presence->TTLlevel++;
2448 OC_LOG_V(DEBUG, TAG, "moving to TTL level %d", cbNode->presence->TTLlevel);
2451 if (result != OC_STACK_OK)
2453 OC_LOG(ERROR, TAG, PCF("OCProcessPresence error"));
2458 #endif // WITH_PRESENCE
2460 OCStackResult OCProcess()
2462 #ifdef WITH_PRESENCE
2463 OCProcessPresence();
2465 CAHandleRequestResponse();
2470 #ifdef WITH_PRESENCE
2471 OCStackResult OCStartPresence(const uint32_t ttl)
2473 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
2474 OCChangeResourceProperty(
2475 &(((OCResource *)presenceResource.handle)->resourceProperties),
2478 if (OC_MAX_PRESENCE_TTL_SECONDS < ttl)
2480 presenceResource.presenceTTL = OC_MAX_PRESENCE_TTL_SECONDS;
2481 OC_LOG(INFO, TAG, PCF("Setting Presence TTL to max value"));
2485 presenceResource.presenceTTL = OC_DEFAULT_PRESENCE_TTL_SECONDS;
2486 OC_LOG(INFO, TAG, PCF("Setting Presence TTL to default value"));
2490 presenceResource.presenceTTL = ttl;
2492 OC_LOG_V(DEBUG, TAG, "Presence TTL is %lu seconds", presenceResource.presenceTTL);
2494 if (OC_PRESENCE_UNINITIALIZED == presenceState)
2496 presenceState = OC_PRESENCE_INITIALIZED;
2498 OCDevAddr devAddr = { OC_DEFAULT_ADAPTER };
2500 CAToken_t caToken = NULL;
2501 CAResult_t caResult = CAGenerateToken(&caToken, tokenLength);
2502 if (caResult != CA_STATUS_OK)
2504 OC_LOG(ERROR, TAG, PCF("CAGenerateToken error"));
2505 CADestroyToken(caToken);
2506 return OC_STACK_ERROR;
2509 AddObserver(OC_RSRVD_PRESENCE_URI, NULL, 0, caToken, tokenLength,
2510 (OCResource *)presenceResource.handle, OC_LOW_QOS, &devAddr);
2511 CADestroyToken(caToken);
2514 // Each time OCStartPresence is called
2515 // a different random 32-bit integer number is used
2516 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2518 return SendPresenceNotification(((OCResource *)presenceResource.handle)->rsrcType,
2519 OC_PRESENCE_TRIGGER_CREATE);
2522 OCStackResult OCStopPresence()
2524 OCStackResult result = OC_STACK_ERROR;
2526 if(presenceResource.handle)
2528 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2530 // make resource inactive
2531 result = OCChangeResourceProperty(
2532 &(((OCResource *) presenceResource.handle)->resourceProperties),
2536 if(result != OC_STACK_OK)
2539 PCF("Changing the presence resource properties to ACTIVE not successful"));
2543 return SendStopNotification();
2547 OCStackResult OCSetDefaultDeviceEntityHandler(OCDeviceEntityHandler entityHandler,
2548 void* callbackParameter)
2550 defaultDeviceHandler = entityHandler;
2551 defaultDeviceHandlerCallbackParameter = callbackParameter;
2556 OCStackResult OCSetPlatformInfo(OCPlatformInfo platformInfo)
2558 OC_LOG(INFO, TAG, PCF("Entering OCSetPlatformInfo"));
2560 if(myStackMode == OC_SERVER || myStackMode == OC_CLIENT_SERVER)
2562 if (validatePlatformInfo(platformInfo))
2564 return SavePlatformInfo(platformInfo);
2568 return OC_STACK_INVALID_PARAM;
2573 return OC_STACK_ERROR;
2577 OCStackResult OCSetDeviceInfo(OCDeviceInfo deviceInfo)
2579 OC_LOG(INFO, TAG, PCF("Entering OCSetDeviceInfo"));
2581 if (!deviceInfo.deviceName || deviceInfo.deviceName[0] == '\0')
2583 OC_LOG(ERROR, TAG, PCF("Null or empty device name."));
2584 return OC_STACK_INVALID_PARAM;
2587 return SaveDeviceInfo(deviceInfo);
2590 OCStackResult OCCreateResource(OCResourceHandle *handle,
2591 const char *resourceTypeName,
2592 const char *resourceInterfaceName,
2593 const char *uri, OCEntityHandler entityHandler,
2594 void* callbackParam,
2595 uint8_t resourceProperties)
2598 OCResource *pointer = NULL;
2600 OCStackResult result = OC_STACK_ERROR;
2602 OC_LOG(INFO, TAG, PCF("Entering OCCreateResource"));
2604 if(myStackMode == OC_CLIENT)
2606 return OC_STACK_INVALID_PARAM;
2608 // Validate parameters
2609 if(!uri || uri[0]=='\0' || strlen(uri)>=MAX_URI_LENGTH )
2611 OC_LOG(ERROR, TAG, PCF("URI is empty or too long"));
2612 return OC_STACK_INVALID_URI;
2614 // Is it presented during resource discovery?
2615 if (!handle || !resourceTypeName || resourceTypeName[0] == '\0' )
2617 OC_LOG(ERROR, TAG, PCF("Input parameter is NULL"));
2618 return OC_STACK_INVALID_PARAM;
2621 if(!resourceInterfaceName || strlen(resourceInterfaceName) == 0)
2623 resourceInterfaceName = OC_RSRVD_INTERFACE_DEFAULT;
2626 // Make sure resourceProperties bitmask has allowed properties specified
2627 if (resourceProperties
2628 > (OC_ACTIVE | OC_DISCOVERABLE | OC_OBSERVABLE | OC_SLOW | OC_SECURE |
2629 OC_EXPLICIT_DISCOVERABLE))
2631 OC_LOG(ERROR, TAG, PCF("Invalid property"));
2632 return OC_STACK_INVALID_PARAM;
2635 // If the headResource is NULL, then no resources have been created...
2636 pointer = headResource;
2639 // At least one resources is in the resource list, so we need to search for
2640 // repeated URLs, which are not allowed. If a repeat is found, exit with an error
2643 if (strncmp(uri, pointer->uri, MAX_URI_LENGTH) == 0)
2645 OC_LOG_V(ERROR, TAG, "Resource %s already exists", uri);
2646 return OC_STACK_INVALID_PARAM;
2648 pointer = pointer->next;
2651 // Create the pointer and insert it into the resource list
2652 pointer = (OCResource *) OICCalloc(1, sizeof(OCResource));
2655 result = OC_STACK_NO_MEMORY;
2658 pointer->sequenceNum = OC_OFFSET_SEQUENCE_NUMBER;
2660 insertResource(pointer);
2663 str = OICStrdup(uri);
2666 result = OC_STACK_NO_MEMORY;
2671 // Set properties. Set OC_ACTIVE
2672 pointer->resourceProperties = (OCResourceProperty) (resourceProperties
2675 // Add the resourcetype to the resource
2676 result = BindResourceTypeToResource(pointer, resourceTypeName);
2677 if (result != OC_STACK_OK)
2679 OC_LOG(ERROR, TAG, PCF("Error adding resourcetype"));
2683 // Add the resourceinterface to the resource
2684 result = BindResourceInterfaceToResource(pointer, resourceInterfaceName);
2685 if (result != OC_STACK_OK)
2687 OC_LOG(ERROR, TAG, PCF("Error adding resourceinterface"));
2691 // If an entity handler has been passed, attach it to the newly created
2692 // resource. Otherwise, set the default entity handler.
2695 pointer->entityHandler = entityHandler;
2696 pointer->entityHandlerCallbackParam = callbackParam;
2700 pointer->entityHandler = defaultResourceEHandler;
2701 pointer->entityHandlerCallbackParam = NULL;
2705 result = OC_STACK_OK;
2707 #ifdef WITH_PRESENCE
2708 if (presenceResource.handle)
2710 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2711 SendPresenceNotification(pointer->rsrcType, OC_PRESENCE_TRIGGER_CREATE);
2715 if (result != OC_STACK_OK)
2717 // Deep delete of resource and other dynamic elements that it contains
2718 deleteResource(pointer);
2725 OCStackResult OCBindResource(
2726 OCResourceHandle collectionHandle, OCResourceHandle resourceHandle)
2728 OCResource *resource = NULL;
2731 OC_LOG(INFO, TAG, PCF("Entering OCBindResource"));
2733 // Validate parameters
2734 VERIFY_NON_NULL(collectionHandle, ERROR, OC_STACK_ERROR);
2735 VERIFY_NON_NULL(resourceHandle, ERROR, OC_STACK_ERROR);
2736 // Container cannot contain itself
2737 if (collectionHandle == resourceHandle)
2739 OC_LOG(ERROR, TAG, PCF("Added handle equals collection handle"));
2740 return OC_STACK_INVALID_PARAM;
2743 // Use the handle to find the resource in the resource linked list
2744 resource = findResource((OCResource *) collectionHandle);
2747 OC_LOG(ERROR, TAG, PCF("Collection handle not found"));
2748 return OC_STACK_INVALID_PARAM;
2751 // Look for an open slot to add add the child resource.
2752 // If found, add it and return success
2753 for (i = 0; i < MAX_CONTAINED_RESOURCES; i++)
2755 if (!resource->rsrcResources[i])
2757 resource->rsrcResources[i] = (OCResource *) resourceHandle;
2758 OC_LOG(INFO, TAG, PCF("resource bound"));
2760 #ifdef WITH_PRESENCE
2761 if (presenceResource.handle)
2763 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2764 SendPresenceNotification(((OCResource *) resourceHandle)->rsrcType,
2765 OC_PRESENCE_TRIGGER_CHANGE);
2773 // Unable to add resourceHandle, so return error
2774 return OC_STACK_ERROR;
2777 OCStackResult OCUnBindResource(
2778 OCResourceHandle collectionHandle, OCResourceHandle resourceHandle)
2780 OCResource *resource = NULL;
2783 OC_LOG(INFO, TAG, PCF("Entering OCUnBindResource"));
2785 // Validate parameters
2786 VERIFY_NON_NULL(collectionHandle, ERROR, OC_STACK_ERROR);
2787 VERIFY_NON_NULL(resourceHandle, ERROR, OC_STACK_ERROR);
2788 // Container cannot contain itself
2789 if (collectionHandle == resourceHandle)
2791 OC_LOG(ERROR, TAG, PCF("removing handle equals collection handle"));
2792 return OC_STACK_INVALID_PARAM;
2795 // Use the handle to find the resource in the resource linked list
2796 resource = findResource((OCResource *) collectionHandle);
2799 OC_LOG(ERROR, TAG, PCF("Collection handle not found"));
2800 return OC_STACK_INVALID_PARAM;
2803 // Look for an open slot to add add the child resource.
2804 // If found, add it and return success
2805 for (i = 0; i < MAX_CONTAINED_RESOURCES; i++)
2807 if (resourceHandle == resource->rsrcResources[i])
2809 resource->rsrcResources[i] = (OCResource *) NULL;
2810 OC_LOG(INFO, TAG, PCF("resource unbound"));
2812 // Send notification when resource is unbounded successfully.
2813 #ifdef WITH_PRESENCE
2814 if (presenceResource.handle)
2816 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2817 SendPresenceNotification(((OCResource *) resourceHandle)->rsrcType,
2818 OC_PRESENCE_TRIGGER_CHANGE);
2825 OC_LOG(INFO, TAG, PCF("resource not found in collection"));
2827 // Unable to add resourceHandle, so return error
2828 return OC_STACK_ERROR;
2831 OCStackResult BindResourceTypeToResource(OCResource* resource,
2832 const char *resourceTypeName)
2834 OCResourceType *pointer = NULL;
2836 OCStackResult result = OC_STACK_ERROR;
2838 VERIFY_NON_NULL(resourceTypeName, ERROR, OC_STACK_INVALID_PARAM);
2840 pointer = (OCResourceType *) OICCalloc(1, sizeof(OCResourceType));
2843 result = OC_STACK_NO_MEMORY;
2847 str = OICStrdup(resourceTypeName);
2850 result = OC_STACK_NO_MEMORY;
2853 pointer->resourcetypename = str;
2855 insertResourceType(resource, pointer);
2856 result = OC_STACK_OK;
2859 if (result != OC_STACK_OK)
2868 OCStackResult BindResourceInterfaceToResource(OCResource* resource,
2869 const char *resourceInterfaceName)
2871 OCResourceInterface *pointer = NULL;
2873 OCStackResult result = OC_STACK_ERROR;
2875 VERIFY_NON_NULL(resourceInterfaceName, ERROR, OC_STACK_INVALID_PARAM);
2877 OC_LOG_V(INFO, TAG, "Binding %s interface to %s", resourceInterfaceName, resource->uri);
2879 pointer = (OCResourceInterface *) OICCalloc(1, sizeof(OCResourceInterface));
2882 result = OC_STACK_NO_MEMORY;
2886 str = OICStrdup(resourceInterfaceName);
2889 result = OC_STACK_NO_MEMORY;
2892 pointer->name = str;
2894 // Bind the resourceinterface to the resource
2895 insertResourceInterface(resource, pointer);
2897 result = OC_STACK_OK;
2900 if (result != OC_STACK_OK)
2909 OCStackResult OCBindResourceTypeToResource(OCResourceHandle handle,
2910 const char *resourceTypeName)
2913 OCStackResult result = OC_STACK_ERROR;
2914 OCResource *resource = NULL;
2916 resource = findResource((OCResource *) handle);
2919 OC_LOG(ERROR, TAG, PCF("Resource not found"));
2920 return OC_STACK_ERROR;
2923 result = BindResourceTypeToResource(resource, resourceTypeName);
2925 #ifdef WITH_PRESENCE
2926 if(presenceResource.handle)
2928 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2929 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
2936 OCStackResult OCBindResourceInterfaceToResource(OCResourceHandle handle,
2937 const char *resourceInterfaceName)
2940 OCStackResult result = OC_STACK_ERROR;
2941 OCResource *resource = NULL;
2943 resource = findResource((OCResource *) handle);
2946 OC_LOG(ERROR, TAG, PCF("Resource not found"));
2947 return OC_STACK_ERROR;
2950 result = BindResourceInterfaceToResource(resource, resourceInterfaceName);
2952 #ifdef WITH_PRESENCE
2953 if (presenceResource.handle)
2955 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2956 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
2963 OCStackResult OCGetNumberOfResources(uint8_t *numResources)
2965 OCResource *pointer = headResource;
2967 VERIFY_NON_NULL(numResources, ERROR, OC_STACK_INVALID_PARAM);
2971 *numResources = *numResources + 1;
2972 pointer = pointer->next;
2977 OCResourceHandle OCGetResourceHandle(uint8_t index)
2979 OCResource *pointer = headResource;
2981 for( uint8_t i = 0; i < index && pointer; ++i)
2983 pointer = pointer->next;
2985 return (OCResourceHandle) pointer;
2988 OCStackResult OCDeleteResource(OCResourceHandle handle)
2992 OC_LOG(ERROR, TAG, PCF("Invalid handle for deletion"));
2993 return OC_STACK_INVALID_PARAM;
2996 OCResource *resource = findResource((OCResource *) handle);
2997 if (resource == NULL)
2999 OC_LOG(ERROR, TAG, PCF("Resource not found"));
3000 return OC_STACK_NO_RESOURCE;
3003 if (deleteResource((OCResource *) handle) != OC_STACK_OK)
3005 OC_LOG(ERROR, TAG, PCF("Error deleting resource"));
3006 return OC_STACK_ERROR;
3012 const char *OCGetResourceUri(OCResourceHandle handle)
3014 OCResource *resource = NULL;
3016 resource = findResource((OCResource *) handle);
3019 return resource->uri;
3021 return (const char *) NULL;
3024 OCResourceProperty OCGetResourceProperties(OCResourceHandle handle)
3026 OCResource *resource = NULL;
3028 resource = findResource((OCResource *) handle);
3031 return resource->resourceProperties;
3033 return (OCResourceProperty)-1;
3036 OCStackResult OCGetNumberOfResourceTypes(OCResourceHandle handle,
3037 uint8_t *numResourceTypes)
3039 OCResource *resource = NULL;
3040 OCResourceType *pointer = NULL;
3042 VERIFY_NON_NULL(numResourceTypes, ERROR, OC_STACK_INVALID_PARAM);
3043 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
3045 *numResourceTypes = 0;
3047 resource = findResource((OCResource *) handle);
3050 pointer = resource->rsrcType;
3053 *numResourceTypes = *numResourceTypes + 1;
3054 pointer = pointer->next;
3060 const char *OCGetResourceTypeName(OCResourceHandle handle, uint8_t index)
3062 OCResourceType *resourceType = NULL;
3064 resourceType = findResourceTypeAtIndex(handle, index);
3067 return resourceType->resourcetypename;
3069 return (const char *) NULL;
3072 OCStackResult OCGetNumberOfResourceInterfaces(OCResourceHandle handle,
3073 uint8_t *numResourceInterfaces)
3075 OCResourceInterface *pointer = NULL;
3076 OCResource *resource = NULL;
3078 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
3079 VERIFY_NON_NULL(numResourceInterfaces, ERROR, OC_STACK_INVALID_PARAM);
3081 *numResourceInterfaces = 0;
3082 resource = findResource((OCResource *) handle);
3085 pointer = resource->rsrcInterface;
3088 *numResourceInterfaces = *numResourceInterfaces + 1;
3089 pointer = pointer->next;
3095 const char *OCGetResourceInterfaceName(OCResourceHandle handle, uint8_t index)
3097 OCResourceInterface *resourceInterface = NULL;
3099 resourceInterface = findResourceInterfaceAtIndex(handle, index);
3100 if (resourceInterface)
3102 return resourceInterface->name;
3104 return (const char *) NULL;
3107 OCResourceHandle OCGetResourceHandleFromCollection(OCResourceHandle collectionHandle,
3110 OCResource *resource = NULL;
3112 if (index >= MAX_CONTAINED_RESOURCES)
3117 resource = findResource((OCResource *) collectionHandle);
3123 return resource->rsrcResources[index];
3126 OCStackResult OCBindResourceHandler(OCResourceHandle handle,
3127 OCEntityHandler entityHandler,
3128 void* callbackParam)
3130 OCResource *resource = NULL;
3132 // Validate parameters
3133 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
3135 // Use the handle to find the resource in the resource linked list
3136 resource = findResource((OCResource *)handle);
3139 OC_LOG(ERROR, TAG, PCF("Resource not found"));
3140 return OC_STACK_ERROR;
3144 resource->entityHandler = entityHandler;
3145 resource->entityHandlerCallbackParam = callbackParam;
3147 #ifdef WITH_PRESENCE
3148 if (presenceResource.handle)
3150 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
3151 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
3158 OCEntityHandler OCGetResourceHandler(OCResourceHandle handle)
3160 OCResource *resource = NULL;
3162 resource = findResource((OCResource *)handle);
3165 OC_LOG(ERROR, TAG, PCF("Resource not found"));
3170 return resource->entityHandler;
3173 void incrementSequenceNumber(OCResource * resPtr)
3175 // Increment the sequence number
3176 resPtr->sequenceNum += 1;
3177 if (resPtr->sequenceNum == MAX_SEQUENCE_NUMBER)
3179 resPtr->sequenceNum = OC_OFFSET_SEQUENCE_NUMBER+1;
3184 #ifdef WITH_PRESENCE
3185 OCStackResult SendPresenceNotification(OCResourceType *resourceType,
3186 OCPresenceTrigger trigger)
3188 OCResource *resPtr = NULL;
3189 OCStackResult result = OC_STACK_ERROR;
3190 OCMethod method = OC_REST_PRESENCE;
3191 uint32_t maxAge = 0;
3192 resPtr = findResource((OCResource *) presenceResource.handle);
3195 return OC_STACK_NO_RESOURCE;
3198 if((((OCResource *) presenceResource.handle)->resourceProperties) & OC_ACTIVE)
3200 maxAge = presenceResource.presenceTTL;
3202 result = SendAllObserverNotification(method, resPtr, maxAge,
3203 trigger, resourceType, OC_LOW_QOS);
3209 OCStackResult SendStopNotification()
3211 OCResource *resPtr = NULL;
3212 OCStackResult result = OC_STACK_ERROR;
3213 OCMethod method = OC_REST_PRESENCE;
3214 resPtr = findResource((OCResource *) presenceResource.handle);
3217 return OC_STACK_NO_RESOURCE;
3220 // maxAge is 0. ResourceType is NULL.
3221 result = SendAllObserverNotification(method, resPtr, 0, OC_PRESENCE_TRIGGER_DELETE,
3227 #endif // WITH_PRESENCE
3228 OCStackResult OCNotifyAllObservers(OCResourceHandle handle, OCQualityOfService qos)
3230 OCResource *resPtr = NULL;
3231 OCStackResult result = OC_STACK_ERROR;
3232 OCMethod method = OC_REST_NOMETHOD;
3233 uint32_t maxAge = 0;
3235 OC_LOG(INFO, TAG, PCF("Notifying all observers"));
3236 #ifdef WITH_PRESENCE
3237 if (handle == presenceResource.handle)
3241 #endif // WITH_PRESENCE
3242 VERIFY_NON_NULL(handle, ERROR, OC_STACK_ERROR);
3244 // Verify that the resource exists
3245 resPtr = findResource ((OCResource *) handle);
3248 return OC_STACK_NO_RESOURCE;
3252 //only increment in the case of regular observing (not presence)
3253 incrementSequenceNumber(resPtr);
3254 method = OC_REST_OBSERVE;
3255 maxAge = MAX_OBSERVE_AGE;
3256 #ifdef WITH_PRESENCE
3257 result = SendAllObserverNotification (method, resPtr, maxAge,
3258 OC_PRESENCE_TRIGGER_DELETE, NULL, qos);
3260 result = SendAllObserverNotification (method, resPtr, maxAge, qos);
3267 OCNotifyListOfObservers (OCResourceHandle handle,
3268 OCObservationId *obsIdList,
3269 uint8_t numberOfIds,
3270 const OCRepPayload *payload,
3271 OCQualityOfService qos)
3273 OC_LOG(INFO, TAG, PCF("Entering OCNotifyListOfObservers"));
3275 OCResource *resPtr = NULL;
3276 //TODO: we should allow the server to define this
3277 uint32_t maxAge = MAX_OBSERVE_AGE;
3279 VERIFY_NON_NULL(handle, ERROR, OC_STACK_ERROR);
3280 VERIFY_NON_NULL(obsIdList, ERROR, OC_STACK_ERROR);
3281 VERIFY_NON_NULL(payload, ERROR, OC_STACK_ERROR);
3283 resPtr = findResource ((OCResource *) handle);
3284 if (NULL == resPtr || myStackMode == OC_CLIENT)
3286 return OC_STACK_NO_RESOURCE;
3290 incrementSequenceNumber(resPtr);
3292 return (SendListObserverNotification(resPtr, obsIdList, numberOfIds,
3293 payload, maxAge, qos));
3296 OCStackResult OCDoResponse(OCEntityHandlerResponse *ehResponse)
3298 OCStackResult result = OC_STACK_ERROR;
3299 OCServerRequest *serverRequest = NULL;
3301 OC_LOG(INFO, TAG, PCF("Entering OCDoResponse"));
3303 // Validate input parameters
3304 VERIFY_NON_NULL(ehResponse, ERROR, OC_STACK_INVALID_PARAM);
3305 VERIFY_NON_NULL(ehResponse->requestHandle, ERROR, OC_STACK_INVALID_PARAM);
3308 // Get pointer to request info
3309 serverRequest = GetServerRequestUsingHandle((OCServerRequest *)ehResponse->requestHandle);
3312 // response handler in ocserverrequest.c. Usually HandleSingleResponse.
3313 result = serverRequest->ehResponseHandler(ehResponse);
3319 //-----------------------------------------------------------------------------
3320 // Private internal function definitions
3321 //-----------------------------------------------------------------------------
3322 static OCDoHandle GenerateInvocationHandle()
3324 OCDoHandle handle = NULL;
3325 // Generate token here, it will be deleted when the transaction is deleted
3326 handle = (OCDoHandle) OICMalloc(sizeof(uint8_t[CA_MAX_TOKEN_LEN]));
3329 OCFillRandomMem((uint8_t*)handle, sizeof(uint8_t[CA_MAX_TOKEN_LEN]));
3335 #ifdef WITH_PRESENCE
3336 OCStackResult OCChangeResourceProperty(OCResourceProperty * inputProperty,
3337 OCResourceProperty resourceProperties, uint8_t enable)
3341 return OC_STACK_INVALID_PARAM;
3343 if (resourceProperties
3344 > (OC_ACTIVE | OC_DISCOVERABLE | OC_OBSERVABLE | OC_SLOW))
3346 OC_LOG(ERROR, TAG, PCF("Invalid property"));
3347 return OC_STACK_INVALID_PARAM;
3351 *inputProperty = (OCResourceProperty) (*inputProperty & ~(resourceProperties));
3355 *inputProperty = (OCResourceProperty) (*inputProperty | resourceProperties);
3361 OCStackResult initResources()
3363 OCStackResult result = OC_STACK_OK;
3365 headResource = NULL;
3366 tailResource = NULL;
3367 // Init Virtual Resources
3368 #ifdef WITH_PRESENCE
3369 presenceResource.presenceTTL = OC_DEFAULT_PRESENCE_TTL_SECONDS;
3371 result = OCCreateResource(&presenceResource.handle,
3372 OC_RSRVD_RESOURCE_TYPE_PRESENCE,
3374 OC_RSRVD_PRESENCE_URI,
3378 //make resource inactive
3379 result = OCChangeResourceProperty(
3380 &(((OCResource *) presenceResource.handle)->resourceProperties),
3384 if (result == OC_STACK_OK)
3386 result = SRMInitSecureResources();
3392 void insertResource(OCResource *resource)
3396 headResource = resource;
3397 tailResource = resource;
3401 tailResource->next = resource;
3402 tailResource = resource;
3404 resource->next = NULL;
3407 OCResource *findResource(OCResource *resource)
3409 OCResource *pointer = headResource;
3413 if (pointer == resource)
3417 pointer = pointer->next;
3422 void deleteAllResources()
3424 OCResource *pointer = headResource;
3425 OCResource *temp = NULL;
3429 temp = pointer->next;
3430 #ifdef WITH_PRESENCE
3431 if (pointer != (OCResource *) presenceResource.handle)
3433 #endif // WITH_PRESENCE
3434 deleteResource(pointer);
3435 #ifdef WITH_PRESENCE
3437 #endif // WITH_PRESENCE
3441 SRMDeInitSecureResources();
3443 #ifdef WITH_PRESENCE
3444 // Ensure that the last resource to be deleted is the presence resource. This allows for all
3445 // presence notification attributed to their deletion to be processed.
3446 deleteResource((OCResource *) presenceResource.handle);
3447 #endif // WITH_PRESENCE
3450 OCStackResult deleteResource(OCResource *resource)
3452 OCResource *prev = NULL;
3453 OCResource *temp = NULL;
3456 OC_LOG_V(DEBUG,TAG,"resource is NULL");
3457 return OC_STACK_INVALID_PARAM;
3460 OC_LOG_V (INFO, TAG, "Deleting resource %s", resource->uri);
3462 temp = headResource;
3465 if (temp == resource)
3467 // Invalidate all Resource Properties.
3468 resource->resourceProperties = (OCResourceProperty) 0;
3469 #ifdef WITH_PRESENCE
3470 if(resource != (OCResource *) presenceResource.handle)
3472 #endif // WITH_PRESENCE
3473 OCNotifyAllObservers((OCResourceHandle)resource, OC_HIGH_QOS);
3474 #ifdef WITH_PRESENCE
3477 if(presenceResource.handle)
3479 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
3480 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_DELETE);
3483 // Only resource in list.
3484 if (temp == headResource && temp == tailResource)
3486 headResource = NULL;
3487 tailResource = NULL;
3490 else if (temp == headResource)
3492 headResource = temp->next;
3495 else if (temp == tailResource)
3497 tailResource = prev;
3498 tailResource->next = NULL;
3502 prev->next = temp->next;
3505 deleteResourceElements(temp);
3516 return OC_STACK_ERROR;
3519 void deleteResourceElements(OCResource *resource)
3526 OICFree(resource->uri);
3527 deleteResourceType(resource->rsrcType);
3528 deleteResourceInterface(resource->rsrcInterface);
3531 void deleteResourceType(OCResourceType *resourceType)
3533 OCResourceType *pointer = resourceType;
3534 OCResourceType *next = NULL;
3538 next = pointer->next;
3539 OICFree(pointer->resourcetypename);
3545 void deleteResourceInterface(OCResourceInterface *resourceInterface)
3547 OCResourceInterface *pointer = resourceInterface;
3548 OCResourceInterface *next = NULL;
3552 next = pointer->next;
3553 OICFree(pointer->name);
3559 void insertResourceType(OCResource *resource, OCResourceType *resourceType)
3561 OCResourceType *pointer = NULL;
3562 OCResourceType *previous = NULL;
3563 if (!resource || !resourceType)
3567 // resource type list is empty.
3568 else if (!resource->rsrcType)
3570 resource->rsrcType = resourceType;
3574 pointer = resource->rsrcType;
3578 if (!strcmp(resourceType->resourcetypename, pointer->resourcetypename))
3580 OC_LOG_V(INFO, TAG, "Type %s already exists", resourceType->resourcetypename);
3581 OICFree(resourceType->resourcetypename);
3582 OICFree(resourceType);
3586 pointer = pointer->next;
3588 previous->next = resourceType;
3590 resourceType->next = NULL;
3592 OC_LOG_V(INFO, TAG, "Added type %s to %s", resourceType->resourcetypename, resource->uri);
3595 OCResourceType *findResourceTypeAtIndex(OCResourceHandle handle, uint8_t index)
3597 OCResource *resource = NULL;
3598 OCResourceType *pointer = NULL;
3600 // Find the specified resource
3601 resource = findResource((OCResource *) handle);
3607 // Make sure a resource has a resourcetype
3608 if (!resource->rsrcType)
3613 // Iterate through the list
3614 pointer = resource->rsrcType;
3615 for(uint8_t i = 0; i< index && pointer; ++i)
3617 pointer = pointer->next;
3622 OCResourceType *findResourceType(OCResourceType * resourceTypeList, const char * resourceTypeName)
3624 if(resourceTypeList && resourceTypeName)
3626 OCResourceType * rtPointer = resourceTypeList;
3627 while(resourceTypeName && rtPointer)
3629 if(rtPointer->resourcetypename &&
3630 strcmp(resourceTypeName, (const char *)
3631 (rtPointer->resourcetypename)) == 0)
3635 rtPointer = rtPointer->next;
3643 * Insert a new interface into interface linked list only if not already present.
3644 * If alredy present, 2nd arg is free'd.
3645 * Default interface will always be first if present.
3647 void insertResourceInterface(OCResource *resource, OCResourceInterface *newInterface)
3649 OCResourceInterface *pointer = NULL;
3650 OCResourceInterface *previous = NULL;
3652 newInterface->next = NULL;
3654 OCResourceInterface **firstInterface = &(resource->rsrcInterface);
3656 if (!*firstInterface)
3658 *firstInterface = newInterface;
3660 else if (strcmp(newInterface->name, OC_RSRVD_INTERFACE_DEFAULT) == 0)
3662 if (strcmp((*firstInterface)->name, OC_RSRVD_INTERFACE_DEFAULT) == 0)
3664 OICFree(newInterface->name);
3665 OICFree(newInterface);
3670 newInterface->next = *firstInterface;
3671 *firstInterface = newInterface;
3676 pointer = *firstInterface;
3679 if (strcmp(newInterface->name, pointer->name) == 0)
3681 OICFree(newInterface->name);
3682 OICFree(newInterface);
3686 pointer = pointer->next;
3688 previous->next = newInterface;
3692 OCResourceInterface *findResourceInterfaceAtIndex(OCResourceHandle handle,
3695 OCResource *resource = NULL;
3696 OCResourceInterface *pointer = NULL;
3698 // Find the specified resource
3699 resource = findResource((OCResource *) handle);
3705 // Make sure a resource has a resourceinterface
3706 if (!resource->rsrcInterface)
3711 // Iterate through the list
3712 pointer = resource->rsrcInterface;
3714 for (uint8_t i = 0; i < index && pointer; ++i)
3716 pointer = pointer->next;
3722 * This function splits the uri using the '?' delimiter.
3723 * "uriWithoutQuery" is the block of characters between the beginning
3724 * till the delimiter or '\0' which ever comes first.
3725 * "query" is whatever is to the right of the delimiter if present.
3726 * No delimiter sets the query to NULL.
3727 * If either are present, they will be malloc'ed into the params 2, 3.
3728 * The first param, *uri is left untouched.
3730 * NOTE: This function does not account for whitespace at the end of the uri NOR
3731 * malformed uri's with '??'. Whitespace at the end will be assumed to be
3732 * part of the query.
3734 OCStackResult getQueryFromUri(const char * uri, char** query, char ** uriWithoutQuery)
3738 return OC_STACK_INVALID_URI;
3740 if(!query || !uriWithoutQuery)
3742 return OC_STACK_INVALID_PARAM;
3746 *uriWithoutQuery = NULL;
3748 size_t uriWithoutQueryLen = 0;
3749 size_t queryLen = 0;
3750 size_t uriLen = strlen(uri);
3752 char *pointerToDelimiter = strstr(uri, "?");
3754 uriWithoutQueryLen = pointerToDelimiter == NULL ? uriLen : (size_t)(pointerToDelimiter - uri);
3755 queryLen = pointerToDelimiter == NULL ? 0 : uriLen - uriWithoutQueryLen - 1;
3757 if (uriWithoutQueryLen)
3759 *uriWithoutQuery = (char *) OICCalloc(uriWithoutQueryLen + 1, 1);
3760 if (!*uriWithoutQuery)
3764 OICStrcpy(*uriWithoutQuery, uriWithoutQueryLen +1, uri);
3768 *query = (char *) OICCalloc(queryLen + 1, 1);
3771 OICFree(*uriWithoutQuery);
3772 *uriWithoutQuery = NULL;
3775 OICStrcpy(*query, queryLen + 1, pointerToDelimiter + 1);
3781 return OC_STACK_NO_MEMORY;
3784 const OicUuid_t* OCGetServerInstanceID(void)
3786 static bool generated = false;
3787 static OicUuid_t sid;
3793 if (GetDoxmDeviceID(&sid) != OC_STACK_OK)
3795 OC_LOG(FATAL, TAG, PCF("Generate UUID for Server Instance failed!"));
3802 const char* OCGetServerInstanceIDString(void)
3804 static bool generated = false;
3805 static char sidStr[UUID_STRING_SIZE];
3812 const OicUuid_t* sid = OCGetServerInstanceID();
3814 if(OCConvertUuidToString(sid->id, sidStr) != RAND_UUID_OK)
3816 OC_LOG(FATAL, TAG, PCF("Generate UUID String for Server Instance failed!"));
3824 CAResult_t OCSelectNetwork()
3826 CAResult_t retResult = CA_STATUS_FAILED;
3827 CAResult_t caResult = CA_STATUS_OK;
3829 CATransportAdapter_t connTypes[] = {
3831 CA_ADAPTER_RFCOMM_BTEDR,
3832 CA_ADAPTER_GATT_BTLE
3835 ,CA_ADAPTER_REMOTE_ACCESS
3838 int numConnTypes = sizeof(connTypes)/sizeof(connTypes[0]);
3840 for(int i = 0; i<numConnTypes; i++)
3842 // Ignore CA_NOT_SUPPORTED error. The CA Layer may have not compiled in the interface.
3843 if(caResult == CA_STATUS_OK || caResult == CA_NOT_SUPPORTED)
3845 caResult = CASelectNetwork(connTypes[i]);
3846 if(caResult == CA_STATUS_OK)
3848 retResult = CA_STATUS_OK;
3853 if(retResult != CA_STATUS_OK)
3855 return caResult; // Returns error of appropriate transport that failed fatally.
3861 OCStackResult CAResultToOCResult(CAResult_t caResult)
3867 case CA_STATUS_INVALID_PARAM:
3868 return OC_STACK_INVALID_PARAM;
3869 case CA_ADAPTER_NOT_ENABLED:
3870 return OC_STACK_ADAPTER_NOT_ENABLED;
3871 case CA_SERVER_STARTED_ALREADY:
3873 case CA_SERVER_NOT_STARTED:
3874 return OC_STACK_ERROR;
3875 case CA_DESTINATION_NOT_REACHABLE:
3876 return OC_STACK_COMM_ERROR;
3877 case CA_SOCKET_OPERATION_FAILED:
3878 return OC_STACK_COMM_ERROR;
3879 case CA_SEND_FAILED:
3880 return OC_STACK_COMM_ERROR;
3881 case CA_RECEIVE_FAILED:
3882 return OC_STACK_COMM_ERROR;
3883 case CA_MEMORY_ALLOC_FAILED:
3884 return OC_STACK_NO_MEMORY;
3885 case CA_REQUEST_TIMEOUT:
3886 return OC_STACK_TIMEOUT;
3887 case CA_DESTINATION_DISCONNECTED:
3888 return OC_STACK_COMM_ERROR;
3889 case CA_STATUS_FAILED:
3890 return OC_STACK_ERROR;
3891 case CA_NOT_SUPPORTED:
3892 return OC_STACK_NOTIMPL;
3894 return OC_STACK_ERROR;