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"
45 #include "ocserverrequest.h"
46 #include "secureresourcemanager.h"
47 #include "doxmresource.h"
49 #include "cainterface.h"
50 #include "ocpayload.h"
51 #include "ocpayloadcbor.h"
53 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
54 #include "routingutility.h"
55 #ifdef ROUTING_GATEWAY
56 #include "routingmanager.h"
65 #include "coap_time.h"
70 #include <arpa/inet.h>
74 #define UINT32_MAX (0xFFFFFFFFUL)
77 //-----------------------------------------------------------------------------
79 //-----------------------------------------------------------------------------
82 OC_STACK_UNINITIALIZED = 0,
84 OC_STACK_UNINIT_IN_PROGRESS
90 OC_PRESENCE_UNINITIALIZED = 0,
91 OC_PRESENCE_INITIALIZED
95 //-----------------------------------------------------------------------------
97 //-----------------------------------------------------------------------------
98 static OCStackState stackState = OC_STACK_UNINITIALIZED;
100 OCResource *headResource = NULL;
101 static OCResource *tailResource = NULL;
103 static OCPresenceState presenceState = OC_PRESENCE_UNINITIALIZED;
104 static PresenceResource presenceResource;
105 static uint8_t PresenceTimeOutSize = 0;
106 static uint32_t PresenceTimeOut[] = {50, 75, 85, 95, 100};
109 static OCMode myStackMode;
111 //TODO: revisit this design
112 static bool gRASetInfo = false;
114 OCDeviceEntityHandler defaultDeviceHandler;
115 void* defaultDeviceHandlerCallbackParameter = NULL;
118 static const char COAP_TCP[] = "coap+tcp:";
121 //-----------------------------------------------------------------------------
123 //-----------------------------------------------------------------------------
124 #define TAG "OCStack"
125 #define VERIFY_SUCCESS(op, successCode) { if ((op) != (successCode)) \
126 {OC_LOG_V(FATAL, TAG, "%s failed!!", #op); goto exit;} }
127 #define VERIFY_NON_NULL(arg, logLevel, retVal) { if (!(arg)) { OC_LOG((logLevel), \
128 TAG, #arg " is NULL"); return (retVal); } }
129 #define VERIFY_NON_NULL_NR(arg, logLevel) { if (!(arg)) { OC_LOG((logLevel), \
130 TAG, #arg " is NULL"); return; } }
131 #define VERIFY_NON_NULL_V(arg) { if (!arg) {OC_LOG_V(FATAL, TAG, "%s is NULL", #arg);\
134 //TODO: we should allow the server to define this
135 #define MAX_OBSERVE_AGE (0x2FFFFUL)
137 #define MILLISECONDS_PER_SECOND (1000)
139 //-----------------------------------------------------------------------------
140 // Private internal function prototypes
141 //-----------------------------------------------------------------------------
144 * Generate handle of OCDoResource invocation for callback management.
146 * @return Generated OCDoResource handle.
148 static OCDoHandle GenerateInvocationHandle();
151 * Initialize resource data structures, variables, etc.
153 * @return ::OC_STACK_OK on success, some other value upon failure.
155 static OCStackResult initResources();
158 * Add a resource to the end of the linked list of resources.
160 * @param resource Resource to be added
162 static void insertResource(OCResource *resource);
165 * Find a resource in the linked list of resources.
167 * @param resource Resource to be found.
168 * @return Pointer to resource that was found in the linked list or NULL if the resource was not
171 static OCResource *findResource(OCResource *resource);
174 * Insert a resource type into a resource's resource type linked list.
175 * If resource type already exists, it will not be inserted and the
176 * resourceType will be free'd.
177 * resourceType->next should be null to avoid memory leaks.
178 * Function returns silently for null args.
180 * @param resource Resource where resource type is to be inserted.
181 * @param resourceType Resource type to be inserted.
183 static void insertResourceType(OCResource *resource,
184 OCResourceType *resourceType);
187 * Get a resource type at the specified index within a resource.
189 * @param handle Handle of resource.
190 * @param index Index of resource type.
192 * @return Pointer to resource type if found, NULL otherwise.
194 static OCResourceType *findResourceTypeAtIndex(OCResourceHandle handle,
198 * Insert a resource interface into a resource's resource interface linked list.
199 * If resource interface already exists, it will not be inserted and the
200 * resourceInterface will be free'd.
201 * resourceInterface->next should be null to avoid memory leaks.
203 * @param resource Resource where resource interface is to be inserted.
204 * @param resourceInterface Resource interface to be inserted.
206 static void insertResourceInterface(OCResource *resource,
207 OCResourceInterface *resourceInterface);
210 * Get a resource interface at the specified index within a resource.
212 * @param handle Handle of resource.
213 * @param index Index of resource interface.
215 * @return Pointer to resource interface if found, NULL otherwise.
217 static OCResourceInterface *findResourceInterfaceAtIndex(
218 OCResourceHandle handle, uint8_t index);
221 * Delete all of the dynamically allocated elements that were created for the resource type.
223 * @param resourceType Specified resource type.
225 static void deleteResourceType(OCResourceType *resourceType);
228 * Delete all of the dynamically allocated elements that were created for the resource interface.
230 * @param resourceInterface Specified resource interface.
232 static void deleteResourceInterface(OCResourceInterface *resourceInterface);
235 * Delete all of the dynamically allocated elements that were created for the resource.
237 * @param resource Specified resource.
239 static void deleteResourceElements(OCResource *resource);
242 * Delete resource specified by handle. Deletes resource and all resourcetype and resourceinterface
245 * @param handle Handle of resource to be deleted.
247 * @return ::OC_STACK_OK on success, some other value upon failure.
249 static OCStackResult deleteResource(OCResource *resource);
252 * Delete all of the resources in the resource list.
254 static void deleteAllResources();
257 * Increment resource sequence number. Handles rollover.
259 * @param resPtr Pointer to resource.
261 static void incrementSequenceNumber(OCResource * resPtr);
264 * Verify the lengths of the URI and the query separately.
266 * @param inputUri Input URI and query.
267 * @param uriLen The length of the initial URI with query.
268 * @return ::OC_STACK_OK on success, some other value upon failure.
270 static OCStackResult verifyUriQueryLength(const char * inputUri,
274 * Attempts to initialize every network interface that the CA Layer might have compiled in.
276 * Note: At least one interface must succeed to initialize. If all calls to @ref CASelectNetwork
277 * return something other than @ref CA_STATUS_OK, then this function fails.
279 * @return ::CA_STATUS_OK on success, some other value upon failure.
281 static CAResult_t OCSelectNetwork();
284 * Get the CoAP ticks after the specified number of milli-seconds.
286 * @param afterMilliSeconds Milli-seconds.
290 static uint32_t GetTicks(uint32_t afterMilliSeconds);
293 * Convert CAResponseResult_t to OCStackResult.
295 * @param caCode CAResponseResult_t code.
296 * @return ::OC_STACK_OK on success, some other value upon failure.
298 static OCStackResult CAToOCStackResult(CAResponseResult_t caCode);
301 * Convert OCStackResult to CAResponseResult_t.
303 * @param caCode OCStackResult code.
304 * @param method OCMethod method the return code replies to.
305 * @return ::CA_CONTENT on OK, some other value upon failure.
307 static CAResponseResult_t OCToCAStackResult(OCStackResult ocCode, OCMethod method);
310 * Convert OCTransportFlags_t to CATransportModifiers_t.
312 * @param ocConType OCTransportFlags_t input.
313 * @return CATransportFlags
315 static CATransportFlags_t OCToCATransportFlags(OCTransportFlags ocConType);
318 * Convert CATransportFlags_t to OCTransportModifiers_t.
320 * @param caConType CATransportFlags_t input.
321 * @return OCTransportFlags
323 static OCTransportFlags CAToOCTransportFlags(CATransportFlags_t caConType);
326 * Handle response from presence request.
328 * @param endPoint CA remote endpoint.
329 * @param responseInfo CA response info.
330 * @return ::OC_STACK_OK on success, some other value upon failure.
332 static OCStackResult HandlePresenceResponse(const CAEndpoint_t *endPoint,
333 const CAResponseInfo_t *responseInfo);
336 * This function will be called back by CA layer when a response is received.
338 * @param endPoint CA remote endpoint.
339 * @param responseInfo CA response info.
341 static void HandleCAResponses(const CAEndpoint_t* endPoint,
342 const CAResponseInfo_t* responseInfo);
345 * This function will be called back by CA layer when a request is received.
347 * @param endPoint CA remote endpoint.
348 * @param requestInfo CA request info.
350 static void HandleCARequests(const CAEndpoint_t* endPoint,
351 const CARequestInfo_t* requestInfo);
354 * Extract query from a URI.
356 * @param uri Full URI with query.
357 * @param query Pointer to string that will contain query.
358 * @param newURI Pointer to string that will contain URI.
359 * @return ::OC_STACK_OK on success, some other value upon failure.
361 static OCStackResult getQueryFromUri(const char * uri, char** resourceType, char ** newURI);
364 * Finds a resource type in an OCResourceType link-list.
366 * @param resourceTypeList The link-list to be searched through.
367 * @param resourceTypeName The key to search for.
369 * @return Resource type that matches the key (ie. resourceTypeName) or
370 * NULL if there is either an invalid parameter or this function was unable to find the key.
372 static OCResourceType *findResourceType(OCResourceType * resourceTypeList,
373 const char * resourceTypeName);
376 * Reset presence TTL for a ClientCB struct. ttlLevel will be set to 0.
377 * TTL will be set to maxAge.
379 * @param cbNode Callback Node for which presence ttl is to be reset.
380 * @param maxAge New value of ttl in seconds.
382 * @return ::OC_STACK_OK on success, some other value upon failure.
384 static OCStackResult ResetPresenceTTL(ClientCB *cbNode, uint32_t maxAgeSeconds);
387 * Ensure the accept header option is set appropriatly before sending the requests and routing
388 * header option is updated with destination.
390 * @param object CA remote endpoint.
391 * @param requestInfo CA request info.
393 * @return ::OC_STACK_OK on success, some other value upon failure.
395 static OCStackResult OCSendRequest(const CAEndpoint_t *object, CARequestInfo_t *requestInfo);
397 //-----------------------------------------------------------------------------
398 // Internal functions
399 //-----------------------------------------------------------------------------
401 uint32_t GetTicks(uint32_t afterMilliSeconds)
406 // Guard against overflow of uint32_t
407 if (afterMilliSeconds <= ((UINT32_MAX - (uint32_t)now) * MILLISECONDS_PER_SECOND) /
408 COAP_TICKS_PER_SECOND)
410 return now + (afterMilliSeconds * COAP_TICKS_PER_SECOND)/MILLISECONDS_PER_SECOND;
418 void CopyEndpointToDevAddr(const CAEndpoint_t *in, OCDevAddr *out)
420 VERIFY_NON_NULL_NR(in, FATAL);
421 VERIFY_NON_NULL_NR(out, FATAL);
423 out->adapter = (OCTransportAdapter)in->adapter;
424 out->flags = CAToOCTransportFlags(in->flags);
425 OICStrcpy(out->addr, sizeof(out->addr), in->addr);
426 out->port = in->port;
427 out->interface = in->interface;
428 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
429 memcpy(out->routeData, in->routeData, sizeof(out->routeData));
433 void CopyDevAddrToEndpoint(const OCDevAddr *in, CAEndpoint_t *out)
435 VERIFY_NON_NULL_NR(in, FATAL);
436 VERIFY_NON_NULL_NR(out, FATAL);
438 out->adapter = (CATransportAdapter_t)in->adapter;
439 out->flags = OCToCATransportFlags(in->flags);
440 OICStrcpy(out->addr, sizeof(out->addr), in->addr);
441 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
442 memcpy(out->routeData, in->routeData, sizeof(out->routeData));
444 out->port = in->port;
445 out->interface = in->interface;
448 void FixUpClientResponse(OCClientResponse *cr)
450 VERIFY_NON_NULL_NR(cr, FATAL);
452 cr->addr = &cr->devAddr;
453 cr->connType = (OCConnectivityType)
454 ((cr->devAddr.adapter << CT_ADAPTER_SHIFT) | (cr->devAddr.flags & CT_MASK_FLAGS));
457 static OCStackResult OCSendRequest(const CAEndpoint_t *object, CARequestInfo_t *requestInfo)
459 VERIFY_NON_NULL(object, FATAL, OC_STACK_INVALID_PARAM);
460 VERIFY_NON_NULL(requestInfo, FATAL, OC_STACK_INVALID_PARAM);
462 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
463 OCStackResult rmResult = RMAddInfo(object->routeData, &(requestInfo->info.options),
464 &(requestInfo->info.numOptions));
465 if (OC_STACK_OK != rmResult)
467 OC_LOG(ERROR, TAG, "Add destination option failed");
472 // OC stack prefer CBOR encoded payloads.
473 requestInfo->info.acceptFormat = CA_FORMAT_APPLICATION_CBOR;
474 CAResult_t result = CASendRequest(object, requestInfo);
475 if(CA_STATUS_OK != result)
477 OC_LOG_V(ERROR, TAG, "CASendRequest failed with CA error %u", result);
478 return CAResultToOCResult(result);
482 //-----------------------------------------------------------------------------
483 // Internal API function
484 //-----------------------------------------------------------------------------
486 // This internal function is called to update the stack with the status of
487 // observers and communication failures
488 OCStackResult OCStackFeedBack(CAToken_t token, uint8_t tokenLength, uint8_t status)
490 OCStackResult result = OC_STACK_ERROR;
491 ResourceObserver * observer = NULL;
492 OCEntityHandlerRequest ehRequest = {0};
496 case OC_OBSERVER_NOT_INTERESTED:
497 OC_LOG(DEBUG, TAG, "observer not interested in our notifications");
498 observer = GetObserverUsingToken (token, tokenLength);
501 result = FormOCEntityHandlerRequest(&ehRequest,
502 (OCRequestHandle)NULL,
505 (OCResourceHandle)NULL,
506 NULL, PAYLOAD_TYPE_REPRESENTATION,
508 OC_OBSERVE_DEREGISTER,
509 observer->observeId);
510 if(result != OC_STACK_OK)
514 observer->resource->entityHandler(OC_OBSERVE_FLAG, &ehRequest,
515 observer->resource->entityHandlerCallbackParam);
518 result = DeleteObserverUsingToken (token, tokenLength);
519 if(result == OC_STACK_OK)
521 OC_LOG(DEBUG, TAG, "Removed observer successfully");
525 result = OC_STACK_OK;
526 OC_LOG(DEBUG, TAG, "Observer Removal failed");
530 case OC_OBSERVER_STILL_INTERESTED:
531 OC_LOG(DEBUG, TAG, "observer still interested, reset the failedCount");
532 observer = GetObserverUsingToken (token, tokenLength);
535 observer->forceHighQos = 0;
536 observer->failedCommCount = 0;
537 result = OC_STACK_OK;
541 result = OC_STACK_OBSERVER_NOT_FOUND;
545 case OC_OBSERVER_FAILED_COMM:
546 OC_LOG(DEBUG, TAG, "observer is unreachable");
547 observer = GetObserverUsingToken (token, tokenLength);
550 if(observer->failedCommCount >= MAX_OBSERVER_FAILED_COMM)
552 result = FormOCEntityHandlerRequest(&ehRequest,
553 (OCRequestHandle)NULL,
556 (OCResourceHandle)NULL,
557 NULL, PAYLOAD_TYPE_REPRESENTATION,
559 OC_OBSERVE_DEREGISTER,
560 observer->observeId);
561 if(result != OC_STACK_OK)
563 return OC_STACK_ERROR;
565 observer->resource->entityHandler(OC_OBSERVE_FLAG, &ehRequest,
566 observer->resource->entityHandlerCallbackParam);
568 result = DeleteObserverUsingToken (token, tokenLength);
569 if(result == OC_STACK_OK)
571 OC_LOG(DEBUG, TAG, "Removed observer successfully");
575 result = OC_STACK_OK;
576 OC_LOG(DEBUG, TAG, "Observer Removal failed");
581 observer->failedCommCount++;
582 result = OC_STACK_CONTINUE;
584 observer->forceHighQos = 1;
585 OC_LOG_V(DEBUG, TAG, "Failed count for this observer is %d",observer->failedCommCount);
589 OC_LOG(ERROR, TAG, "Unknown status");
590 result = OC_STACK_ERROR;
595 OCStackResult CAToOCStackResult(CAResponseResult_t caCode)
597 OCStackResult ret = OC_STACK_ERROR;
602 ret = OC_STACK_RESOURCE_CREATED;
605 ret = OC_STACK_RESOURCE_DELETED;
612 ret = OC_STACK_INVALID_QUERY;
614 case CA_UNAUTHORIZED_REQ:
615 ret = OC_STACK_UNAUTHORIZED_REQ;
618 ret = OC_STACK_INVALID_OPTION;
621 ret = OC_STACK_NO_RESOURCE;
623 case CA_RETRANSMIT_TIMEOUT:
624 ret = OC_STACK_COMM_ERROR;
632 CAResponseResult_t OCToCAStackResult(OCStackResult ocCode, OCMethod method)
634 CAResponseResult_t ret = CA_INTERNAL_SERVER_ERROR;
643 // This Response Code is like HTTP 204 "No Content" but only used in
644 // response to POST and PUT requests.
648 // This Response Code is like HTTP 200 "OK" but only used in response to
653 // This should not happen but,
654 // give it a value just in case but output an error
656 OC_LOG_V(ERROR, TAG, "Unexpected OC_STACK_OK return code for method [d].", method);
659 case OC_STACK_RESOURCE_CREATED:
662 case OC_STACK_RESOURCE_DELETED:
665 case OC_STACK_INVALID_QUERY:
668 case OC_STACK_INVALID_OPTION:
671 case OC_STACK_NO_RESOURCE:
674 case OC_STACK_COMM_ERROR:
675 ret = CA_RETRANSMIT_TIMEOUT;
677 case OC_STACK_UNAUTHORIZED_REQ:
678 ret = CA_UNAUTHORIZED_REQ;
686 CATransportFlags_t OCToCATransportFlags(OCTransportFlags ocFlags)
688 CATransportFlags_t caFlags = (CATransportFlags_t)ocFlags;
690 // supply default behavior.
691 if ((caFlags & (CA_IPV6|CA_IPV4)) == 0)
693 caFlags = (CATransportFlags_t)(caFlags|CA_IPV6|CA_IPV4);
695 if ((caFlags & OC_MASK_SCOPE) == 0)
697 caFlags = (CATransportFlags_t)(caFlags|OC_SCOPE_LINK);
702 OCTransportFlags CAToOCTransportFlags(CATransportFlags_t caFlags)
704 return (OCTransportFlags)caFlags;
707 static OCStackResult ResetPresenceTTL(ClientCB *cbNode, uint32_t maxAgeSeconds)
709 uint32_t lowerBound = 0;
710 uint32_t higherBound = 0;
712 if (!cbNode || !cbNode->presence || !cbNode->presence->timeOut)
714 return OC_STACK_INVALID_PARAM;
717 OC_LOG_V(INFO, TAG, "Update presence TTL, time is %u", GetTicks(0));
719 cbNode->presence->TTL = maxAgeSeconds;
721 for (int index = 0; index < PresenceTimeOutSize; index++)
723 // Guard against overflow
724 if (cbNode->presence->TTL < (UINT32_MAX/(MILLISECONDS_PER_SECOND*PresenceTimeOut[index]))
727 lowerBound = GetTicks((PresenceTimeOut[index] *
728 cbNode->presence->TTL *
729 MILLISECONDS_PER_SECOND)/100);
733 lowerBound = GetTicks(UINT32_MAX);
736 if (cbNode->presence->TTL < (UINT32_MAX/(MILLISECONDS_PER_SECOND*PresenceTimeOut[index+1]))
739 higherBound = GetTicks((PresenceTimeOut[index + 1] *
740 cbNode->presence->TTL *
741 MILLISECONDS_PER_SECOND)/100);
745 higherBound = GetTicks(UINT32_MAX);
748 cbNode->presence->timeOut[index] = OCGetRandomRange(lowerBound, higherBound);
750 OC_LOG_V(DEBUG, TAG, "lowerBound timeout %d", lowerBound);
751 OC_LOG_V(DEBUG, TAG, "higherBound timeout %d", higherBound);
752 OC_LOG_V(DEBUG, TAG, "timeOut entry %d", cbNode->presence->timeOut[index]);
755 cbNode->presence->TTLlevel = 0;
757 OC_LOG_V(DEBUG, TAG, "this TTL level %d", cbNode->presence->TTLlevel);
761 const char *convertTriggerEnumToString(OCPresenceTrigger trigger)
763 if (trigger == OC_PRESENCE_TRIGGER_CREATE)
765 return OC_RSRVD_TRIGGER_CREATE;
767 else if (trigger == OC_PRESENCE_TRIGGER_CHANGE)
769 return OC_RSRVD_TRIGGER_CHANGE;
773 return OC_RSRVD_TRIGGER_DELETE;
777 OCPresenceTrigger convertTriggerStringToEnum(const char * triggerStr)
781 return OC_PRESENCE_TRIGGER_CREATE;
783 else if(strcmp(triggerStr, OC_RSRVD_TRIGGER_CREATE) == 0)
785 return OC_PRESENCE_TRIGGER_CREATE;
787 else if(strcmp(triggerStr, OC_RSRVD_TRIGGER_CHANGE) == 0)
789 return OC_PRESENCE_TRIGGER_CHANGE;
793 return OC_PRESENCE_TRIGGER_DELETE;
798 * The cononical presence allows constructed URIs to be string compared.
800 * requestUri must be a char array of size CA_MAX_URI_LENGTH
802 static int FormCanonicalPresenceUri(const CAEndpoint_t *endpoint, char *resourceUri,
805 VERIFY_NON_NULL(endpoint , FATAL, OC_STACK_INVALID_PARAM);
806 VERIFY_NON_NULL(resourceUri, FATAL, OC_STACK_INVALID_PARAM);
807 VERIFY_NON_NULL(presenceUri, FATAL, OC_STACK_INVALID_PARAM);
809 CAEndpoint_t *ep = (CAEndpoint_t *)endpoint;
811 if (ep->adapter == CA_ADAPTER_IP)
813 if ((ep->flags & CA_IPV6) && !(ep->flags & CA_IPV4))
815 if ('\0' == ep->addr[0]) // multicast
817 return snprintf(presenceUri, CA_MAX_URI_LENGTH, OC_RSRVD_PRESENCE_URI);
821 return snprintf(presenceUri, CA_MAX_URI_LENGTH, "coap://[%s]:%u%s",
822 ep->addr, ep->port, OC_RSRVD_PRESENCE_URI);
827 if ('\0' == ep->addr[0]) // multicast
829 OICStrcpy(ep->addr, sizeof(ep->addr), OC_MULTICAST_IP);
830 ep->port = OC_MULTICAST_PORT;
832 return snprintf(presenceUri, CA_MAX_URI_LENGTH, "coap://%s:%u%s",
833 ep->addr, ep->port, OC_RSRVD_PRESENCE_URI);
837 // might work for other adapters (untested, but better than nothing)
838 return snprintf(presenceUri, CA_MAX_URI_LENGTH, "coap://%s%s", ep->addr,
839 OC_RSRVD_PRESENCE_URI);
843 OCStackResult HandlePresenceResponse(const CAEndpoint_t *endpoint,
844 const CAResponseInfo_t *responseInfo)
846 VERIFY_NON_NULL(endpoint, FATAL, OC_STACK_INVALID_PARAM);
847 VERIFY_NON_NULL(responseInfo, FATAL, OC_STACK_INVALID_PARAM);
849 OCStackApplicationResult cbResult = OC_STACK_DELETE_TRANSACTION;
850 ClientCB * cbNode = NULL;
851 char *resourceTypeName = NULL;
852 OCClientResponse response = {.devAddr = {.adapter = OC_DEFAULT_ADAPTER}};
853 OCStackResult result = OC_STACK_ERROR;
856 char presenceUri[CA_MAX_URI_LENGTH];
858 int presenceSubscribe = 0;
859 int multicastPresenceSubscribe = 0;
861 if (responseInfo->result != CA_CONTENT)
863 OC_LOG_V(ERROR, TAG, "HandlePresenceResponse failed %d", responseInfo->result);
864 return OC_STACK_ERROR;
867 // check for unicast presence
868 uriLen = FormCanonicalPresenceUri(endpoint, OC_RSRVD_PRESENCE_URI, presenceUri);
869 if (uriLen < 0 || (size_t)uriLen >= sizeof (presenceUri))
871 return OC_STACK_INVALID_URI;
874 cbNode = GetClientCB(NULL, 0, NULL, presenceUri);
877 presenceSubscribe = 1;
881 // check for multiicast presence
882 CAEndpoint_t ep = { .adapter = endpoint->adapter,
883 .flags = endpoint->flags };
885 uriLen = FormCanonicalPresenceUri(&ep, OC_RSRVD_PRESENCE_URI, presenceUri);
887 cbNode = GetClientCB(NULL, 0, NULL, presenceUri);
890 multicastPresenceSubscribe = 1;
894 if (!presenceSubscribe && !multicastPresenceSubscribe)
896 OC_LOG(ERROR, TAG, "Received a presence notification, but no callback, ignoring");
900 response.payload = NULL;
901 response.result = OC_STACK_OK;
903 CopyEndpointToDevAddr(endpoint, &response.devAddr);
904 FixUpClientResponse(&response);
906 if (responseInfo->info.payload)
908 result = OCParsePayload(&response.payload,
909 PAYLOAD_TYPE_PRESENCE,
910 responseInfo->info.payload,
911 responseInfo->info.payloadSize);
913 if(result != OC_STACK_OK)
915 OC_LOG(ERROR, TAG, "Presence parse failed");
918 if(!response.payload || response.payload->type != PAYLOAD_TYPE_PRESENCE)
920 OC_LOG(ERROR, TAG, "Presence payload was wrong type");
921 result = OC_STACK_ERROR;
924 response.sequenceNumber = ((OCPresencePayload*)response.payload)->sequenceNumber;
925 resourceTypeName = ((OCPresencePayload*)response.payload)->resourceType;
926 maxAge = ((OCPresencePayload*)response.payload)->maxAge;
929 if (presenceSubscribe)
931 if(cbNode->sequenceNumber == response.sequenceNumber)
933 OC_LOG(INFO, TAG, "No presence change");
939 OC_LOG(INFO, TAG, "Stopping presence");
940 response.result = OC_STACK_PRESENCE_STOPPED;
943 OICFree(cbNode->presence->timeOut);
944 OICFree(cbNode->presence);
945 cbNode->presence = NULL;
950 if(!cbNode->presence)
952 cbNode->presence = (OCPresence *)OICMalloc(sizeof (OCPresence));
954 if(!(cbNode->presence))
956 OC_LOG(ERROR, TAG, "Could not allocate memory for cbNode->presence");
957 result = OC_STACK_NO_MEMORY;
961 VERIFY_NON_NULL_V(cbNode->presence);
962 cbNode->presence->timeOut = NULL;
963 cbNode->presence->timeOut = (uint32_t *)
964 OICMalloc(PresenceTimeOutSize * sizeof(uint32_t));
965 if(!(cbNode->presence->timeOut)){
967 "Could not allocate memory for cbNode->presence->timeOut");
968 OICFree(cbNode->presence);
969 result = OC_STACK_NO_MEMORY;
974 ResetPresenceTTL(cbNode, maxAge);
976 cbNode->sequenceNumber = response.sequenceNumber;
978 // Ensure that a filter is actually applied.
979 if( resourceTypeName && cbNode->filterResourceType)
981 if(!findResourceType(cbNode->filterResourceType, resourceTypeName))
990 // This is the multicast case
991 OCMulticastNode* mcNode = NULL;
992 mcNode = GetMCPresenceNode(presenceUri);
996 if(mcNode->nonce == response.sequenceNumber)
998 OC_LOG(INFO, TAG, "No presence change (Multicast)");
1001 mcNode->nonce = response.sequenceNumber;
1005 OC_LOG(INFO, TAG, "Stopping presence");
1006 response.result = OC_STACK_PRESENCE_STOPPED;
1011 char* uri = OICStrdup(presenceUri);
1015 "No Memory for URI to store in the presence node");
1016 result = OC_STACK_NO_MEMORY;
1020 result = AddMCPresenceNode(&mcNode, uri, response.sequenceNumber);
1021 if(result == OC_STACK_NO_MEMORY)
1024 "No Memory for Multicast Presence Node");
1028 // presence node now owns uri
1031 // Ensure that a filter is actually applied.
1032 if(resourceTypeName && cbNode->filterResourceType)
1034 if(!findResourceType(cbNode->filterResourceType, resourceTypeName))
1041 cbResult = cbNode->callBack(cbNode->context, cbNode->handle, &response);
1043 if (cbResult == OC_STACK_DELETE_TRANSACTION)
1045 FindAndDeleteClientCB(cbNode);
1049 OCPayloadDestroy(response.payload);
1053 void HandleCAResponses(const CAEndpoint_t* endPoint, const CAResponseInfo_t* responseInfo)
1055 VERIFY_NON_NULL_NR(endPoint, FATAL);
1056 VERIFY_NON_NULL_NR(responseInfo, FATAL);
1058 OC_LOG(INFO, TAG, "Enter HandleCAResponses");
1060 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
1061 #ifdef ROUTING_GATEWAY
1062 bool needRIHandling = false;
1064 * Routing manager is going to update either of endpoint or response or both.
1065 * This typecasting is done to avoid unnecessary duplication of Endpoint and responseInfo
1066 * RM can update "routeData" option in endPoint so that future RI requests can be sent to proper
1069 OCStackResult ret = RMHandleResponse((CAResponseInfo_t *)responseInfo, (CAEndpoint_t *)endPoint,
1071 if(ret != OC_STACK_OK || !needRIHandling)
1073 OC_LOG_V(INFO, TAG, "Routing status![%d]. Not forwarding to RI", ret);
1079 * Put source in sender endpoint so that the next packet from application can be routed to
1080 * proper destination and remove "RM" coap header option before passing request / response to
1081 * RI as this option will make no sense to either RI or application.
1083 RMUpdateInfo((CAHeaderOption_t **) &(responseInfo->info.options),
1084 (uint8_t *) &(responseInfo->info.numOptions),
1085 (CAEndpoint_t *) endPoint);
1088 if(responseInfo->info.resourceUri &&
1089 strcmp(responseInfo->info.resourceUri, OC_RSRVD_PRESENCE_URI) == 0)
1091 HandlePresenceResponse(endPoint, responseInfo);
1095 ClientCB *cbNode = GetClientCB(responseInfo->info.token,
1096 responseInfo->info.tokenLength, NULL, NULL);
1098 ResourceObserver * observer = GetObserverUsingToken (responseInfo->info.token,
1099 responseInfo->info.tokenLength);
1103 OC_LOG(INFO, TAG, "There is a cbNode associated with the response token");
1104 if(responseInfo->result == CA_EMPTY)
1106 OC_LOG(INFO, TAG, "Receiving A ACK/RESET for this token");
1107 // We do not have a case for the client to receive a RESET
1108 if(responseInfo->info.type == CA_MSG_ACKNOWLEDGE)
1110 //This is the case of receiving an ACK on a request to a slow resource!
1111 OC_LOG(INFO, TAG, "This is a pure ACK");
1112 //TODO: should we inform the client
1113 // app that at least the request was received at the server?
1116 else if(responseInfo->result == CA_RETRANSMIT_TIMEOUT)
1118 OC_LOG(INFO, TAG, "Receiving A Timeout for this token");
1119 OC_LOG(INFO, TAG, "Calling into application address space");
1121 OCClientResponse response =
1122 {.devAddr = {.adapter = OC_DEFAULT_ADAPTER}};
1123 CopyEndpointToDevAddr(endPoint, &response.devAddr);
1124 FixUpClientResponse(&response);
1125 response.resourceUri = responseInfo->info.resourceUri;
1126 memcpy(response.identity.id, responseInfo->info.identity.id,
1127 sizeof (response.identity.id));
1128 response.identity.id_length = responseInfo->info.identity.id_length;
1130 response.result = CAToOCStackResult(responseInfo->result);
1131 cbNode->callBack(cbNode->context,
1132 cbNode->handle, &response);
1133 FindAndDeleteClientCB(cbNode);
1137 OC_LOG(INFO, TAG, "This is a regular response, A client call back is found");
1138 OC_LOG(INFO, TAG, "Calling into application address space");
1140 OCClientResponse response =
1141 {.devAddr = {.adapter = OC_DEFAULT_ADAPTER}};
1142 response.sequenceNumber = OC_OBSERVE_NO_OPTION;
1143 CopyEndpointToDevAddr(endPoint, &response.devAddr);
1144 FixUpClientResponse(&response);
1145 response.resourceUri = responseInfo->info.resourceUri;
1146 memcpy(response.identity.id, responseInfo->info.identity.id,
1147 sizeof (response.identity.id));
1148 response.identity.id_length = responseInfo->info.identity.id_length;
1150 response.result = CAToOCStackResult(responseInfo->result);
1152 if(responseInfo->info.payload &&
1153 responseInfo->info.payloadSize)
1155 OCPayloadType type = PAYLOAD_TYPE_INVALID;
1156 // check the security resource
1157 if (SRMIsSecurityResourceURI(cbNode->requestUri))
1159 type = PAYLOAD_TYPE_SECURITY;
1161 else if (cbNode->method == OC_REST_DISCOVER)
1163 if (strncmp(OC_RSRVD_WELL_KNOWN_URI,cbNode->requestUri,
1164 sizeof(OC_RSRVD_WELL_KNOWN_URI) - 1) == 0)
1166 type = PAYLOAD_TYPE_DISCOVERY;
1168 else if (strcmp(cbNode->requestUri, OC_RSRVD_DEVICE_URI) == 0)
1170 type = PAYLOAD_TYPE_DEVICE;
1172 else if (strcmp(cbNode->requestUri, OC_RSRVD_PLATFORM_URI) == 0)
1174 type = PAYLOAD_TYPE_PLATFORM;
1176 else if (strcmp(cbNode->requestUri, OC_RSRVD_RD_URI) == 0)
1178 type = PAYLOAD_TYPE_RD;
1182 OC_LOG_V(ERROR, TAG, "Unknown Payload type in Discovery: %d %s",
1183 cbNode->method, cbNode->requestUri);
1187 else if (cbNode->method == OC_REST_GET ||
1188 cbNode->method == OC_REST_PUT ||
1189 cbNode->method == OC_REST_POST ||
1190 cbNode->method == OC_REST_OBSERVE ||
1191 cbNode->method == OC_REST_OBSERVE_ALL ||
1192 cbNode->method == OC_REST_DELETE)
1194 char targetUri[MAX_URI_LENGTH];
1195 snprintf(targetUri, MAX_URI_LENGTH, "%s?rt=%s",
1196 OC_RSRVD_RD_URI, OC_RSRVD_RESOURCE_TYPE_RDPUBLISH);
1197 if (strcmp(targetUri, cbNode->requestUri) == 0)
1199 type = PAYLOAD_TYPE_RD;
1201 if (type == PAYLOAD_TYPE_INVALID)
1203 OC_LOG_V(INFO, TAG, "Assuming PAYLOAD_TYPE_REPRESENTATION: %d %s",
1204 cbNode->method, cbNode->requestUri);
1205 type = PAYLOAD_TYPE_REPRESENTATION;
1210 OC_LOG_V(ERROR, TAG, "Unknown Payload type: %d %s",
1211 cbNode->method, cbNode->requestUri);
1215 if(OC_STACK_OK != OCParsePayload(&response.payload,
1217 responseInfo->info.payload,
1218 responseInfo->info.payloadSize))
1220 OC_LOG(ERROR, TAG, "Error converting payload");
1221 OCPayloadDestroy(response.payload);
1226 response.numRcvdVendorSpecificHeaderOptions = 0;
1227 if(responseInfo->info.numOptions > 0)
1230 //First option always with option ID is COAP_OPTION_OBSERVE if it is available.
1231 if(responseInfo->info.options[0].optionID == COAP_OPTION_OBSERVE)
1234 uint32_t observationOption;
1235 uint8_t* optionData = (uint8_t*)responseInfo->info.options[0].optionData;
1236 for (observationOption=0, i=0;
1237 i<sizeof(uint32_t) && i<responseInfo->info.options[0].optionLength;
1241 (observationOption << 8) | optionData[i];
1243 response.sequenceNumber = observationOption;
1245 response.numRcvdVendorSpecificHeaderOptions = responseInfo->info.numOptions - 1;
1250 response.numRcvdVendorSpecificHeaderOptions = responseInfo->info.numOptions;
1253 if(response.numRcvdVendorSpecificHeaderOptions > MAX_HEADER_OPTIONS)
1255 OC_LOG(ERROR, TAG, "#header options are more than MAX_HEADER_OPTIONS");
1256 OCPayloadDestroy(response.payload);
1260 for (uint8_t i = start; i < responseInfo->info.numOptions; i++)
1262 memcpy (&(response.rcvdVendorSpecificHeaderOptions[i-start]),
1263 &(responseInfo->info.options[i]), sizeof(OCHeaderOption));
1267 if (cbNode->method == OC_REST_OBSERVE &&
1268 response.sequenceNumber > OC_OFFSET_SEQUENCE_NUMBER &&
1269 response.sequenceNumber <= cbNode->sequenceNumber)
1271 OC_LOG_V(INFO, TAG, "Received stale notification. Number :%d",
1272 response.sequenceNumber);
1276 OCStackApplicationResult appFeedback = cbNode->callBack(cbNode->context,
1279 cbNode->sequenceNumber = response.sequenceNumber;
1281 if (appFeedback == OC_STACK_DELETE_TRANSACTION)
1283 FindAndDeleteClientCB(cbNode);
1287 // To keep discovery callbacks active.
1288 cbNode->TTL = GetTicks(MAX_CB_TIMEOUT_SECONDS *
1289 MILLISECONDS_PER_SECOND);
1293 //Need to send ACK when the response is CON
1294 if(responseInfo->info.type == CA_MSG_CONFIRM)
1296 SendDirectStackResponse(endPoint, responseInfo->info.messageId, CA_EMPTY,
1297 CA_MSG_ACKNOWLEDGE, 0, NULL, NULL, 0, NULL);
1300 OCPayloadDestroy(response.payload);
1307 OC_LOG(INFO, TAG, "There is an observer associated with the response token");
1308 if(responseInfo->result == CA_EMPTY)
1310 OC_LOG(INFO, TAG, "Receiving A ACK/RESET for this token");
1311 if(responseInfo->info.type == CA_MSG_RESET)
1313 OC_LOG(INFO, TAG, "This is a RESET");
1314 OCStackFeedBack(responseInfo->info.token, responseInfo->info.tokenLength,
1315 OC_OBSERVER_NOT_INTERESTED);
1317 else if(responseInfo->info.type == CA_MSG_ACKNOWLEDGE)
1319 OC_LOG(INFO, TAG, "This is a pure ACK");
1320 OCStackFeedBack(responseInfo->info.token, responseInfo->info.tokenLength,
1321 OC_OBSERVER_STILL_INTERESTED);
1324 else if(responseInfo->result == CA_RETRANSMIT_TIMEOUT)
1326 OC_LOG(INFO, TAG, "Receiving Time Out for an observer");
1327 OCStackFeedBack(responseInfo->info.token, responseInfo->info.tokenLength,
1328 OC_OBSERVER_FAILED_COMM);
1333 if(!cbNode && !observer)
1335 if(myStackMode == OC_CLIENT || myStackMode == OC_CLIENT_SERVER
1336 || myStackMode == OC_GATEWAY)
1338 OC_LOG(INFO, TAG, "This is a client, but no cbNode was found for token");
1339 if(responseInfo->result == CA_EMPTY)
1341 OC_LOG(INFO, TAG, "Receiving CA_EMPTY in the ocstack");
1345 OC_LOG(INFO, TAG, "Received a message without callbacks. Sending RESET");
1346 SendDirectStackResponse(endPoint, responseInfo->info.messageId, CA_EMPTY,
1347 CA_MSG_RESET, 0, NULL, NULL, 0, NULL);
1351 if(myStackMode == OC_SERVER || myStackMode == OC_CLIENT_SERVER
1352 || myStackMode == OC_GATEWAY)
1354 OC_LOG(INFO, TAG, "This is a server, but no observer was found for token");
1355 if (responseInfo->info.type == CA_MSG_ACKNOWLEDGE)
1357 OC_LOG_V(INFO, TAG, "Received ACK at server for messageId : %d",
1358 responseInfo->info.messageId);
1360 if (responseInfo->info.type == CA_MSG_RESET)
1362 OC_LOG_V(INFO, TAG, "Received RESET at server for messageId : %d",
1363 responseInfo->info.messageId);
1370 OC_LOG(INFO, TAG, "Exit HandleCAResponses");
1374 * This function handles error response from CA
1375 * code shall be added to handle the errors
1377 void HandleCAErrorResponse(const CAEndpoint_t *endPoint, const CAErrorInfo_t *errrorInfo)
1379 OC_LOG(INFO, TAG, "Enter HandleCAErrorResponse");
1381 if(NULL == endPoint)
1383 OC_LOG(ERROR, TAG, "endPoint is NULL");
1387 if(NULL == errrorInfo)
1389 OC_LOG(ERROR, TAG, "errrorInfo is NULL");
1392 OC_LOG(INFO, TAG, "Exit HandleCAErrorResponse");
1396 * This function sends out Direct Stack Responses. These are responses that are not coming
1397 * from the application entity handler. These responses have no payload and are usually ACKs,
1398 * RESETs or some error conditions that were caught by the stack.
1400 OCStackResult SendDirectStackResponse(const CAEndpoint_t* endPoint, const uint16_t coapID,
1401 const CAResponseResult_t responseResult, const CAMessageType_t type,
1402 const uint8_t numOptions, const CAHeaderOption_t *options,
1403 CAToken_t token, uint8_t tokenLength, const char *resourceUri)
1405 CAResponseInfo_t respInfo = {
1406 .result = responseResult
1408 respInfo.info.messageId = coapID;
1409 respInfo.info.numOptions = numOptions;
1410 respInfo.info.options = (CAHeaderOption_t*)options;
1411 respInfo.info.payload = NULL;
1412 respInfo.info.token = token;
1413 respInfo.info.tokenLength = tokenLength;
1414 respInfo.info.type = type;
1415 respInfo.info.resourceUri = OICStrdup (resourceUri);
1416 respInfo.info.acceptFormat = CA_FORMAT_UNDEFINED;
1418 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
1419 // Add the destination to route option from the endpoint->routeData.
1420 OCStackResult result = RMAddInfo(endPoint->routeData,
1421 &(respInfo.info.options),
1422 &(respInfo.info.numOptions));
1423 if(OC_STACK_OK != result)
1425 OC_LOG_V(ERROR, TAG, "Add routing option failed [%d]", result);
1430 CAResult_t caResult = CASendResponse(endPoint, &respInfo);
1432 // resourceUri in the info field is cloned in the CA layer and
1433 // thus ownership is still here.
1434 OICFree (respInfo.info.resourceUri);
1436 if(CA_STATUS_OK != caResult)
1438 OC_LOG(ERROR, TAG, "CASendResponse error");
1439 return CAResultToOCResult(caResult);
1444 //This function will be called back by CA layer when a request is received
1445 void HandleCARequests(const CAEndpoint_t* endPoint, const CARequestInfo_t* requestInfo)
1447 OC_LOG(INFO, TAG, "Enter HandleCARequests");
1450 OC_LOG(ERROR, TAG, "endPoint is NULL");
1456 OC_LOG(ERROR, TAG, "requestInfo is NULL");
1460 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
1461 #ifdef ROUTING_GATEWAY
1462 bool needRIHandling = false;
1464 * Routing manager is going to update either of endpoint or request or both.
1465 * This typecasting is done to avoid unnecessary duplication of Endpoint and requestInfo
1466 * RM can update "routeData" option in endPoint so that future RI requests can be sent to proper
1467 * destination. It can also remove "RM" coap header option before passing request / response to
1468 * RI as this option will make no sense to either RI or application.
1470 OCStackResult ret = RMHandleRequest((CARequestInfo_t *)requestInfo, (CAEndpoint_t *)endPoint,
1472 if(OC_STACK_OK != ret || !needRIHandling)
1474 OC_LOG_V(INFO, TAG, "Routing status![%d]. Not forwarding to RI", ret);
1480 * Put source in sender endpoint so that the next packet from application can be routed to
1481 * proper destination and remove RM header option.
1483 RMUpdateInfo((CAHeaderOption_t **) &(requestInfo->info.options),
1484 (uint8_t *) &(requestInfo->info.numOptions),
1485 (CAEndpoint_t *) endPoint);
1488 OCStackResult requestResult = OC_STACK_ERROR;
1490 if(myStackMode == OC_CLIENT)
1492 //TODO: should the client be responding to requests?
1496 OCServerProtocolRequest serverRequest = {0};
1498 OC_LOG_V(INFO, TAG, "Endpoint URI : %s", requestInfo->info.resourceUri);
1500 char * uriWithoutQuery = NULL;
1501 char * query = NULL;
1503 requestResult = getQueryFromUri(requestInfo->info.resourceUri, &query, &uriWithoutQuery);
1505 if (requestResult != OC_STACK_OK || !uriWithoutQuery)
1507 OC_LOG_V(ERROR, TAG, "getQueryFromUri() failed with OC error code %d\n", requestResult);
1510 OC_LOG_V(INFO, TAG, "URI without query: %s", uriWithoutQuery);
1511 OC_LOG_V(INFO, TAG, "Query : %s", query);
1513 if(strlen(uriWithoutQuery) < MAX_URI_LENGTH)
1515 OICStrcpy(serverRequest.resourceUrl, sizeof(serverRequest.resourceUrl), uriWithoutQuery);
1516 OICFree(uriWithoutQuery);
1520 OC_LOG(ERROR, TAG, "URI length exceeds MAX_URI_LENGTH.");
1521 OICFree(uriWithoutQuery);
1528 if(strlen(query) < MAX_QUERY_LENGTH)
1530 OICStrcpy(serverRequest.query, sizeof(serverRequest.query), query);
1535 OC_LOG(ERROR, TAG, "Query length exceeds MAX_QUERY_LENGTH.");
1541 if ((requestInfo->info.payload) && (0 < requestInfo->info.payloadSize))
1543 serverRequest.reqTotalSize = requestInfo->info.payloadSize;
1544 serverRequest.payload = (uint8_t *) OICMalloc(requestInfo->info.payloadSize);
1545 memcpy (serverRequest.payload, requestInfo->info.payload,
1546 requestInfo->info.payloadSize);
1550 serverRequest.reqTotalSize = 0;
1553 switch (requestInfo->method)
1556 serverRequest.method = OC_REST_GET;
1559 serverRequest.method = OC_REST_PUT;
1562 serverRequest.method = OC_REST_POST;
1565 serverRequest.method = OC_REST_DELETE;
1568 OC_LOG_V(ERROR, TAG, "Received CA method %d not supported", requestInfo->method);
1569 SendDirectStackResponse(endPoint, requestInfo->info.messageId, CA_BAD_REQ,
1570 requestInfo->info.type, requestInfo->info.numOptions,
1571 requestInfo->info.options, requestInfo->info.token,
1572 requestInfo->info.tokenLength, requestInfo->info.resourceUri);
1573 OICFree(serverRequest.payload);
1577 OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)requestInfo->info.token,
1578 requestInfo->info.tokenLength);
1579 serverRequest.requestToken = (CAToken_t)OICMalloc(requestInfo->info.tokenLength);
1580 serverRequest.tokenLength = requestInfo->info.tokenLength;
1582 if (!serverRequest.requestToken)
1584 OC_LOG(FATAL, TAG, "Allocation for token failed.");
1585 SendDirectStackResponse(endPoint, requestInfo->info.messageId, CA_INTERNAL_SERVER_ERROR,
1586 requestInfo->info.type, requestInfo->info.numOptions,
1587 requestInfo->info.options, requestInfo->info.token,
1588 requestInfo->info.tokenLength, requestInfo->info.resourceUri);
1589 OICFree(serverRequest.payload);
1592 memcpy(serverRequest.requestToken, requestInfo->info.token, requestInfo->info.tokenLength);
1594 switch (requestInfo->info.acceptFormat)
1596 case CA_FORMAT_APPLICATION_CBOR:
1597 serverRequest.acceptFormat = OC_FORMAT_CBOR;
1599 case CA_FORMAT_UNDEFINED:
1600 serverRequest.acceptFormat = OC_FORMAT_UNDEFINED;
1603 serverRequest.acceptFormat = OC_FORMAT_UNSUPPORTED;
1606 if (requestInfo->info.type == CA_MSG_CONFIRM)
1608 serverRequest.qos = OC_HIGH_QOS;
1612 serverRequest.qos = OC_LOW_QOS;
1614 // CA does not need the following field
1615 // Are we sure CA does not need them? how is it responding to multicast
1616 serverRequest.delayedResNeeded = 0;
1618 serverRequest.coapID = requestInfo->info.messageId;
1620 CopyEndpointToDevAddr(endPoint, &serverRequest.devAddr);
1622 // copy vendor specific header options
1623 uint8_t tempNum = (requestInfo->info.numOptions);
1624 GetObserveHeaderOption(&serverRequest.observationOption, requestInfo->info.options, &tempNum);
1625 if (requestInfo->info.numOptions > MAX_HEADER_OPTIONS)
1628 "The request info numOptions is greater than MAX_HEADER_OPTIONS");
1629 SendDirectStackResponse(endPoint, requestInfo->info.messageId, CA_BAD_OPT,
1630 requestInfo->info.type, requestInfo->info.numOptions,
1631 requestInfo->info.options, requestInfo->info.token,
1632 requestInfo->info.tokenLength, requestInfo->info.resourceUri);
1633 OICFree(serverRequest.payload);
1634 OICFree(serverRequest.requestToken);
1637 serverRequest.numRcvdVendorSpecificHeaderOptions = tempNum;
1638 if (serverRequest.numRcvdVendorSpecificHeaderOptions)
1640 memcpy (&(serverRequest.rcvdVendorSpecificHeaderOptions), requestInfo->info.options,
1641 sizeof(CAHeaderOption_t)*tempNum);
1644 requestResult = HandleStackRequests (&serverRequest);
1646 // Send ACK to client as precursor to slow response
1647 if(requestResult == OC_STACK_SLOW_RESOURCE)
1649 SendDirectStackResponse(endPoint, requestInfo->info.messageId, CA_EMPTY,
1650 CA_MSG_ACKNOWLEDGE,0, NULL, NULL, 0, NULL);
1652 else if(requestResult != OC_STACK_OK)
1654 OC_LOG_V(ERROR, TAG, "HandleStackRequests failed. error: %d", requestResult);
1656 CAResponseResult_t stackResponse =
1657 OCToCAStackResult(requestResult, serverRequest.method);
1659 SendDirectStackResponse(endPoint, requestInfo->info.messageId, stackResponse,
1660 requestInfo->info.type, requestInfo->info.numOptions,
1661 requestInfo->info.options, requestInfo->info.token,
1662 requestInfo->info.tokenLength, requestInfo->info.resourceUri);
1664 // requestToken is fed to HandleStackRequests, which then goes to AddServerRequest.
1665 // The token is copied in there, and is thus still owned by this function.
1666 OICFree(serverRequest.payload);
1667 OICFree(serverRequest.requestToken);
1668 OC_LOG(INFO, TAG, "Exit HandleCARequests");
1671 OCStackResult HandleStackRequests(OCServerProtocolRequest * protocolRequest)
1673 OC_LOG(INFO, TAG, "Entering HandleStackRequests (OCStack Layer)");
1674 OCStackResult result = OC_STACK_ERROR;
1675 ResourceHandling resHandling;
1676 OCResource *resource;
1677 if(!protocolRequest)
1679 OC_LOG(ERROR, TAG, "protocolRequest is NULL");
1680 return OC_STACK_INVALID_PARAM;
1683 OCServerRequest * request = GetServerRequestUsingToken(protocolRequest->requestToken,
1684 protocolRequest->tokenLength);
1687 OC_LOG(INFO, TAG, "This is a new Server Request");
1688 result = AddServerRequest(&request, protocolRequest->coapID,
1689 protocolRequest->delayedResNeeded, 0, protocolRequest->method,
1690 protocolRequest->numRcvdVendorSpecificHeaderOptions,
1691 protocolRequest->observationOption, protocolRequest->qos,
1692 protocolRequest->query, protocolRequest->rcvdVendorSpecificHeaderOptions,
1693 protocolRequest->payload, protocolRequest->requestToken,
1694 protocolRequest->tokenLength, protocolRequest->resourceUrl,
1695 protocolRequest->reqTotalSize, protocolRequest->acceptFormat,
1696 &protocolRequest->devAddr);
1697 if (OC_STACK_OK != result)
1699 OC_LOG(ERROR, TAG, "Error adding server request");
1705 OC_LOG(ERROR, TAG, "Out of Memory");
1706 return OC_STACK_NO_MEMORY;
1709 if(!protocolRequest->reqMorePacket)
1711 request->requestComplete = 1;
1716 OC_LOG(INFO, TAG, "This is either a repeated or blocked Server Request");
1719 if(request->requestComplete)
1721 OC_LOG(INFO, TAG, "This Server Request is complete");
1722 result = DetermineResourceHandling (request, &resHandling, &resource);
1723 if (result == OC_STACK_OK)
1725 result = ProcessRequest(resHandling, resource, request);
1730 OC_LOG(INFO, TAG, "This Server Request is incomplete");
1731 result = OC_STACK_CONTINUE;
1736 bool validatePlatformInfo(OCPlatformInfo info)
1739 if (!info.platformID)
1741 OC_LOG(ERROR, TAG, "No platform ID found.");
1745 if (info.manufacturerName)
1747 size_t lenManufacturerName = strlen(info.manufacturerName);
1749 if(lenManufacturerName == 0 || lenManufacturerName > MAX_MANUFACTURER_NAME_LENGTH)
1751 OC_LOG(ERROR, TAG, "Manufacturer name fails length requirements.");
1757 OC_LOG(ERROR, TAG, "No manufacturer name present");
1761 if (info.manufacturerUrl)
1763 if(strlen(info.manufacturerUrl) > MAX_MANUFACTURER_URL_LENGTH)
1765 OC_LOG(ERROR, TAG, "Manufacturer url fails length requirements.");
1772 //-----------------------------------------------------------------------------
1774 //-----------------------------------------------------------------------------
1776 OCStackResult OCSetRAInfo(const OCRAInfo_t *raInfo)
1779 !raInfo->username ||
1780 !raInfo->hostname ||
1781 !raInfo->xmpp_domain)
1784 return OC_STACK_INVALID_PARAM;
1786 OCStackResult result = CAResultToOCResult(CASetRAInfo((const CARAInfo_t *) raInfo));
1787 gRASetInfo = (result == OC_STACK_OK)? true : false;
1793 OCStackResult OCInit(const char *ipAddr, uint16_t port, OCMode mode)
1797 return OCInit1(mode, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS);
1800 OCStackResult OCInit1(OCMode mode, OCTransportFlags serverFlags, OCTransportFlags clientFlags)
1802 if(stackState == OC_STACK_INITIALIZED)
1804 OC_LOG(INFO, TAG, "Subsequent calls to OCInit() without calling \
1805 OCStop() between them are ignored.");
1809 #ifndef ROUTING_GATEWAY
1810 if (OC_GATEWAY == mode)
1812 OC_LOG(ERROR, TAG, "Routing Manager not supported");
1813 return OC_STACK_INVALID_PARAM;
1820 OC_LOG(ERROR, TAG, "Need to call OCSetRAInfo before calling OCInit");
1821 return OC_STACK_ERROR;
1825 OCStackResult result = OC_STACK_ERROR;
1826 OC_LOG(INFO, TAG, "Entering OCInit");
1829 if (!((mode == OC_CLIENT) || (mode == OC_SERVER) || (mode == OC_CLIENT_SERVER)
1830 || (mode == OC_GATEWAY)))
1832 OC_LOG(ERROR, TAG, "Invalid mode");
1833 return OC_STACK_ERROR;
1837 if (mode == OC_CLIENT || mode == OC_CLIENT_SERVER || mode == OC_GATEWAY)
1839 caglobals.client = true;
1841 if (mode == OC_SERVER || mode == OC_CLIENT_SERVER || mode == OC_GATEWAY)
1843 caglobals.server = true;
1846 caglobals.serverFlags = (CATransportFlags_t)serverFlags;
1847 if (!(caglobals.serverFlags & CA_IPFAMILY_MASK))
1849 caglobals.serverFlags = (CATransportFlags_t)(caglobals.serverFlags|CA_IPV4|CA_IPV6);
1851 caglobals.clientFlags = (CATransportFlags_t)clientFlags;
1852 if (!(caglobals.clientFlags & CA_IPFAMILY_MASK))
1854 caglobals.clientFlags = (CATransportFlags_t)(caglobals.clientFlags|CA_IPV4|CA_IPV6);
1858 if (!(caglobals.serverFlags & CA_IPFAMILY_MASK))
1860 caglobals.serverFlags = (CATransportFlags_t)(caglobals.serverFlags|CA_IPV4);
1862 if (!(caglobals.clientFlags & CA_IPFAMILY_MASK))
1864 caglobals.clientFlags = (CATransportFlags_t)(caglobals.clientFlags|CA_IPV4);
1868 defaultDeviceHandler = NULL;
1869 defaultDeviceHandlerCallbackParameter = NULL;
1872 result = CAResultToOCResult(CAInitialize());
1873 VERIFY_SUCCESS(result, OC_STACK_OK);
1875 result = CAResultToOCResult(OCSelectNetwork());
1876 VERIFY_SUCCESS(result, OC_STACK_OK);
1878 switch (myStackMode)
1881 CARegisterHandler(HandleCARequests, HandleCAResponses, HandleCAErrorResponse);
1882 result = CAResultToOCResult(CAStartDiscoveryServer());
1883 OC_LOG(INFO, TAG, "Client mode: CAStartDiscoveryServer");
1886 SRMRegisterHandler(HandleCARequests, HandleCAResponses, HandleCAErrorResponse);
1887 result = CAResultToOCResult(CAStartListeningServer());
1888 OC_LOG(INFO, TAG, "Server mode: CAStartListeningServer");
1890 case OC_CLIENT_SERVER:
1892 SRMRegisterHandler(HandleCARequests, HandleCAResponses, HandleCAErrorResponse);
1893 result = CAResultToOCResult(CAStartListeningServer());
1894 if(result == OC_STACK_OK)
1896 result = CAResultToOCResult(CAStartDiscoveryServer());
1900 VERIFY_SUCCESS(result, OC_STACK_OK);
1902 #ifdef WITH_PRESENCE
1903 PresenceTimeOutSize = sizeof (PresenceTimeOut) / sizeof (PresenceTimeOut[0]) - 1;
1904 #endif // WITH_PRESENCE
1906 //Update Stack state to initialized
1907 stackState = OC_STACK_INITIALIZED;
1909 // Initialize resource
1910 if(myStackMode != OC_CLIENT)
1912 result = initResources();
1915 // Initialize the SRM Policy Engine
1916 if(result == OC_STACK_OK)
1918 result = SRMInitPolicyEngine();
1919 // TODO after BeachHead delivery: consolidate into single SRMInit()
1922 #ifdef ROUTING_GATEWAY
1923 if (OC_GATEWAY == myStackMode)
1925 result = RMInitialize();
1930 if(result != OC_STACK_OK)
1932 OC_LOG(ERROR, TAG, "Stack initialization error");
1933 deleteAllResources();
1935 stackState = OC_STACK_UNINITIALIZED;
1940 OCStackResult OCStop()
1942 OC_LOG(INFO, TAG, "Entering OCStop");
1944 if (stackState == OC_STACK_UNINIT_IN_PROGRESS)
1946 OC_LOG(DEBUG, TAG, "Stack already stopping, exiting");
1949 else if (stackState != OC_STACK_INITIALIZED)
1951 OC_LOG(ERROR, TAG, "Stack not initialized");
1952 return OC_STACK_ERROR;
1955 stackState = OC_STACK_UNINIT_IN_PROGRESS;
1957 #ifdef WITH_PRESENCE
1958 // Ensure that the TTL associated with ANY and ALL presence notifications originating from
1959 // here send with the code "OC_STACK_PRESENCE_STOPPED" result.
1960 presenceResource.presenceTTL = 0;
1961 #endif // WITH_PRESENCE
1963 #ifdef ROUTING_GATEWAY
1964 if (OC_GATEWAY == myStackMode)
1970 // Free memory dynamically allocated for resources
1971 deleteAllResources();
1973 DeletePlatformInfo();
1975 // Remove all observers
1976 DeleteObserverList();
1977 // Remove all the client callbacks
1978 DeleteClientCBList();
1980 // De-init the SRM Policy Engine
1981 // TODO after BeachHead delivery: consolidate into single SRMDeInit()
1982 SRMDeInitPolicyEngine();
1985 stackState = OC_STACK_UNINITIALIZED;
1989 CAMessageType_t qualityOfServiceToMessageType(OCQualityOfService qos)
1994 return CA_MSG_CONFIRM;
1999 return CA_MSG_NONCONFIRM;
2003 OCStackResult verifyUriQueryLength(const char *inputUri, uint16_t uriLen)
2007 query = strchr (inputUri, '?');
2011 if((query - inputUri) > MAX_URI_LENGTH)
2013 return OC_STACK_INVALID_URI;
2016 if((inputUri + uriLen - 1 - query) > MAX_QUERY_LENGTH)
2018 return OC_STACK_INVALID_QUERY;
2021 else if(uriLen > MAX_URI_LENGTH)
2023 return OC_STACK_INVALID_URI;
2029 * A request uri consists of the following components in order:
2032 * CoAP over UDP prefix "coap://"
2033 * CoAP over TCP prefix "coap+tcp://"
2035 * IPv6 address "[1234::5678]"
2036 * IPv4 address "192.168.1.1"
2037 * optional port ":5683"
2038 * resource uri "/oc/core..."
2040 * for PRESENCE requests, extract resource type.
2042 static OCStackResult ParseRequestUri(const char *fullUri,
2043 OCTransportAdapter adapter,
2044 OCTransportFlags flags,
2045 OCDevAddr **devAddr,
2047 char **resourceType)
2049 VERIFY_NON_NULL(fullUri, FATAL, OC_STACK_INVALID_CALLBACK);
2051 OCStackResult result = OC_STACK_OK;
2052 OCDevAddr *da = NULL;
2056 // provide defaults for all returned values
2063 *resourceUri = NULL;
2067 *resourceType = NULL;
2070 // delimit url prefix, if any
2071 const char *start = fullUri;
2072 char *slash2 = strstr(start, "//");
2077 char *slash = strchr(start, '/');
2080 return OC_STACK_INVALID_URI;
2084 // process url scheme
2085 size_t prefixLen = slash2 - fullUri;
2089 if ((prefixLen == sizeof(COAP_TCP) - 1) && (!strncmp(fullUri, COAP_TCP, prefixLen)))
2096 // TODO: this logic should come in with unit tests exercising the various strings
2097 // processs url prefix, if any
2098 size_t urlLen = slash - start;
2102 if (urlLen && devAddr)
2103 { // construct OCDevAddr
2104 if (start[0] == '[')
2106 char *close = strchr(++start, ']');
2107 if (!close || close > slash)
2109 return OC_STACK_INVALID_URI;
2112 if (close[1] == ':')
2116 adapter = (OCTransportAdapter)(adapter | OC_ADAPTER_IP);
2117 flags = (OCTransportFlags)(flags | OC_IP_USE_V6);
2121 char *dot = strchr(start, '.');
2122 if (dot && dot < slash)
2124 colon = strchr(start, ':');
2125 end = (colon && colon < slash) ? colon : slash;
2129 adapter = (OCTransportAdapter)(adapter | OC_ADAPTER_TCP);
2134 adapter = (OCTransportAdapter)(adapter | OC_ADAPTER_IP);
2135 flags = (OCTransportFlags)(flags | OC_IP_USE_V4);
2144 if (len >= sizeof(da->addr))
2146 return OC_STACK_INVALID_URI;
2148 // collect port, if any
2149 if (colon && colon < slash)
2151 for (colon++; colon < slash; colon++)
2154 if (c < '0' || c > '9')
2156 return OC_STACK_INVALID_URI;
2158 port = 10 * port + c - '0';
2163 if (len >= sizeof(da->addr))
2165 return OC_STACK_INVALID_URI;
2168 da = (OCDevAddr *)OICCalloc(sizeof (OCDevAddr), 1);
2171 return OC_STACK_NO_MEMORY;
2173 OICStrcpyPartial(da->addr, sizeof(da->addr), start, len);
2175 da->adapter = adapter;
2177 if (!strncmp(fullUri, "coaps:", 6))
2179 da->flags = (OCTransportFlags)(da->flags|CA_SECURE);
2184 // process resource uri, if any
2186 { // request uri and query
2187 size_t ulen = strlen(slash); // resource uri length
2188 size_t tlen = 0; // resource type length
2191 static const char strPresence[] = "/oic/ad?rt=";
2192 static const size_t lenPresence = sizeof(strPresence) - 1;
2193 if (!strncmp(slash, strPresence, lenPresence))
2195 type = slash + lenPresence;
2196 tlen = ulen - lenPresence;
2201 *resourceUri = (char *)OICMalloc(ulen + 1);
2204 result = OC_STACK_NO_MEMORY;
2207 strcpy(*resourceUri, slash);
2210 if (type && resourceType)
2212 *resourceType = (char *)OICMalloc(tlen + 1);
2215 result = OC_STACK_NO_MEMORY;
2219 OICStrcpy(*resourceType, (tlen+1), type);
2226 // free all returned values
2233 OICFree(*resourceUri);
2237 OICFree(*resourceType);
2242 static OCStackResult OCPreparePresence(CAEndpoint_t *endpoint,
2243 char *resourceUri, char **requestUri)
2245 char uri[CA_MAX_URI_LENGTH];
2247 FormCanonicalPresenceUri(endpoint, resourceUri, uri);
2249 *requestUri = OICStrdup(uri);
2252 return OC_STACK_NO_MEMORY;
2259 * Discover or Perform requests on a specified resource
2261 OCStackResult OCDoResource(OCDoHandle *handle,
2263 const char *requestUri,
2264 const OCDevAddr *destination,
2266 OCConnectivityType connectivityType,
2267 OCQualityOfService qos,
2268 OCCallbackData *cbData,
2269 OCHeaderOption *options,
2272 OC_LOG(INFO, TAG, "Entering OCDoResource");
2274 // Validate input parameters
2275 VERIFY_NON_NULL(cbData, FATAL, OC_STACK_INVALID_CALLBACK);
2276 VERIFY_NON_NULL(cbData->cb, FATAL, OC_STACK_INVALID_CALLBACK);
2277 VERIFY_NON_NULL(requestUri , FATAL, OC_STACK_INVALID_URI);
2279 OCStackResult result = OC_STACK_ERROR;
2280 CAResult_t caResult;
2281 CAToken_t token = NULL;
2282 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
2283 ClientCB *clientCB = NULL;
2284 OCDoHandle resHandle = NULL;
2285 CAEndpoint_t endpoint = {.adapter = CA_DEFAULT_ADAPTER};
2286 OCDevAddr tmpDevAddr = { OC_DEFAULT_ADAPTER };
2288 OCTransportAdapter adapter;
2289 OCTransportFlags flags;
2290 // the request contents are put here
2291 CARequestInfo_t requestInfo = {.method = CA_GET};
2292 // requestUri will be parsed into the following three variables
2293 OCDevAddr *devAddr = NULL;
2294 char *resourceUri = NULL;
2295 char *resourceType = NULL;
2297 // To track if memory is allocated for additional header options
2298 uint8_t hdrOptionMemAlloc = 0;
2300 // This validation is broken, but doesn't cause harm
2301 size_t uriLen = strlen(requestUri );
2302 if ((result = verifyUriQueryLength(requestUri , uriLen)) != OC_STACK_OK)
2308 * Support original behavior with address on resourceUri argument.
2310 adapter = (OCTransportAdapter)(connectivityType >> CT_ADAPTER_SHIFT);
2311 flags = (OCTransportFlags)(connectivityType & CT_MASK_FLAGS);
2313 result = ParseRequestUri(requestUri, adapter, flags, &devAddr, &resourceUri, &resourceType);
2315 if (result != OC_STACK_OK)
2317 OC_LOG_V(DEBUG, TAG, "Unable to parse uri: %s", requestUri);
2324 case OC_REST_OBSERVE:
2325 case OC_REST_OBSERVE_ALL:
2326 case OC_REST_CANCEL_OBSERVE:
2327 requestInfo.method = CA_GET;
2330 requestInfo.method = CA_PUT;
2333 requestInfo.method = CA_POST;
2335 case OC_REST_DELETE:
2336 requestInfo.method = CA_DELETE;
2338 case OC_REST_DISCOVER:
2340 if (destination || devAddr)
2342 requestInfo.isMulticast = false;
2346 tmpDevAddr.adapter = adapter;
2347 tmpDevAddr.flags = flags;
2348 destination = &tmpDevAddr;
2349 requestInfo.isMulticast = true;
2351 // CA_DISCOVER will become GET and isMulticast
2352 requestInfo.method = CA_GET;
2354 #ifdef WITH_PRESENCE
2355 case OC_REST_PRESENCE:
2356 // Replacing method type with GET because "presence"
2357 // is a stack layer only implementation.
2358 requestInfo.method = CA_GET;
2362 result = OC_STACK_INVALID_METHOD;
2366 if (!devAddr && !destination)
2368 OC_LOG(DEBUG, TAG, "no devAddr and no destination");
2369 result = OC_STACK_INVALID_PARAM;
2373 /* If not original behavior, use destination argument */
2374 if (destination && !devAddr)
2376 devAddr = (OCDevAddr *)OICMalloc(sizeof (OCDevAddr));
2379 result = OC_STACK_NO_MEMORY;
2382 *devAddr = *destination;
2385 resHandle = GenerateInvocationHandle();
2388 result = OC_STACK_NO_MEMORY;
2392 caResult = CAGenerateToken(&token, tokenLength);
2393 if (caResult != CA_STATUS_OK)
2395 OC_LOG(ERROR, TAG, "CAGenerateToken error");
2396 result= OC_STACK_ERROR;
2400 // fill in request data
2401 requestInfo.info.type = qualityOfServiceToMessageType(qos);
2402 requestInfo.info.token = token;
2403 requestInfo.info.tokenLength = tokenLength;
2404 requestInfo.info.resourceUri = resourceUri;
2406 if ((method == OC_REST_OBSERVE) || (method == OC_REST_OBSERVE_ALL))
2408 result = CreateObserveHeaderOption (&(requestInfo.info.options),
2409 options, numOptions, OC_OBSERVE_REGISTER);
2410 if (result != OC_STACK_OK)
2414 hdrOptionMemAlloc = 1;
2415 requestInfo.info.numOptions = numOptions + 1;
2419 requestInfo.info.options = (CAHeaderOption_t*)options;
2420 requestInfo.info.numOptions = numOptions;
2423 CopyDevAddrToEndpoint(devAddr, &endpoint);
2428 OCConvertPayload(payload, &requestInfo.info.payload, &requestInfo.info.payloadSize))
2431 OC_LOG(ERROR, TAG, "Failed to create CBOR Payload");
2434 requestInfo.info.payloadFormat = CA_FORMAT_APPLICATION_CBOR;
2438 requestInfo.info.payload = NULL;
2439 requestInfo.info.payloadSize = 0;
2440 requestInfo.info.payloadFormat = CA_FORMAT_UNDEFINED;
2443 if (result != OC_STACK_OK)
2445 OC_LOG(ERROR, TAG, "CACreateEndpoint error");
2449 // prepare for response
2450 #ifdef WITH_PRESENCE
2451 if (method == OC_REST_PRESENCE)
2453 char *presenceUri = NULL;
2454 result = OCPreparePresence(&endpoint, resourceUri, &presenceUri);
2455 if (OC_STACK_OK != result)
2460 // Assign full presence uri as coap://ip:port/oic/ad to add to callback list.
2461 // Presence notification will form a canonical uri to
2462 // look for callbacks into the application.
2463 resourceUri = presenceUri;
2467 ttl = GetTicks(MAX_CB_TIMEOUT_SECONDS * MILLISECONDS_PER_SECOND);
2468 result = AddClientCB(&clientCB, cbData, token, tokenLength, &resHandle,
2469 method, devAddr, resourceUri, resourceType, ttl);
2470 if (OC_STACK_OK != result)
2475 devAddr = NULL; // Client CB list entry now owns it
2476 resourceUri = NULL; // Client CB list entry now owns it
2477 resourceType = NULL; // Client CB list entry now owns it
2480 result = OCSendRequest(&endpoint, &requestInfo);
2481 if (OC_STACK_OK != result)
2488 *handle = resHandle;
2492 if (result != OC_STACK_OK)
2494 OC_LOG(ERROR, TAG, "OCDoResource error");
2495 FindAndDeleteClientCB(clientCB);
2496 CADestroyToken(token);
2504 // This is the owner of the payload object, so we free it
2505 OCPayloadDestroy(payload);
2506 OICFree(requestInfo.info.payload);
2508 OICFree(resourceUri);
2509 OICFree(resourceType);
2510 if (hdrOptionMemAlloc)
2512 OICFree(requestInfo.info.options);
2517 OCStackResult OCCancel(OCDoHandle handle, OCQualityOfService qos, OCHeaderOption * options,
2521 * This ftn is implemented one of two ways in the case of observation:
2523 * 1. qos == OC_NON_CONFIRMABLE. When observe is unobserved..
2524 * Remove the callback associated on client side.
2525 * When the next notification comes in from server,
2526 * reply with RESET message to server.
2527 * Keep in mind that the server will react to RESET only
2528 * if the last notification was sent as CON
2530 * 2. qos == OC_CONFIRMABLE. When OCCancel is called,
2531 * and it is associated with an observe request
2532 * (i.e. ClientCB->method == OC_REST_OBSERVE || OC_REST_OBSERVE_ALL),
2533 * Send CON Observe request to server with
2534 * observe flag = OC_RESOURCE_OBSERVE_DEREGISTER.
2535 * Remove the callback associated on client side.
2537 OCStackResult ret = OC_STACK_OK;
2538 CAEndpoint_t endpoint = {.adapter = CA_DEFAULT_ADAPTER};
2539 CAInfo_t requestData = {.type = CA_MSG_CONFIRM};
2540 CARequestInfo_t requestInfo = {.method = CA_GET};
2544 return OC_STACK_INVALID_PARAM;
2547 ClientCB *clientCB = GetClientCB(NULL, 0, handle, NULL);
2550 OC_LOG(ERROR, TAG, "Client callback not found. Called OCCancel twice?");
2554 switch (clientCB->method)
2556 case OC_REST_OBSERVE:
2557 case OC_REST_OBSERVE_ALL:
2558 OC_LOG_V(INFO, TAG, "Canceling observation for resource %s",
2559 clientCB->requestUri);
2560 if (qos != OC_HIGH_QOS)
2562 FindAndDeleteClientCB(clientCB);
2567 OC_LOG(INFO, TAG, "Cancelling observation as CONFIRMABLE");
2570 requestData.type = qualityOfServiceToMessageType(qos);
2571 requestData.token = clientCB->token;
2572 requestData.tokenLength = clientCB->tokenLength;
2573 if (CreateObserveHeaderOption (&(requestData.options),
2574 options, numOptions, OC_OBSERVE_DEREGISTER) != OC_STACK_OK)
2576 return OC_STACK_ERROR;
2578 requestData.numOptions = numOptions + 1;
2579 requestData.resourceUri = OICStrdup (clientCB->requestUri);
2581 requestInfo.method = CA_GET;
2582 requestInfo.info = requestData;
2584 CopyDevAddrToEndpoint(clientCB->devAddr, &endpoint);
2587 ret = OCSendRequest(&endpoint, &requestInfo);
2590 #ifdef WITH_PRESENCE
2591 case OC_REST_PRESENCE:
2592 FindAndDeleteClientCB(clientCB);
2597 ret = OC_STACK_INVALID_METHOD;
2602 if (requestData.numOptions > 0)
2604 OICFree(requestData.options);
2606 if (requestData.resourceUri)
2608 OICFree (requestData.resourceUri);
2614 * @brief Register Persistent storage callback.
2615 * @param persistentStorageHandler [IN] Pointers to open, read, write, close & unlink handlers.
2617 * OC_STACK_OK - No errors; Success
2618 * OC_STACK_INVALID_PARAM - Invalid parameter
2620 OCStackResult OCRegisterPersistentStorageHandler(OCPersistentStorage* persistentStorageHandler)
2622 OC_LOG(INFO, TAG, "RegisterPersistentStorageHandler !!");
2623 if(!persistentStorageHandler)
2625 OC_LOG(ERROR, TAG, "The persistent storage handler is invalid");
2626 return OC_STACK_INVALID_PARAM;
2630 if( !persistentStorageHandler->open ||
2631 !persistentStorageHandler->close ||
2632 !persistentStorageHandler->read ||
2633 !persistentStorageHandler->unlink ||
2634 !persistentStorageHandler->write)
2636 OC_LOG(ERROR, TAG, "The persistent storage handler is invalid");
2637 return OC_STACK_INVALID_PARAM;
2640 return SRMRegisterPersistentStorageHandler(persistentStorageHandler);
2643 #ifdef WITH_PRESENCE
2645 OCStackResult OCProcessPresence()
2647 OCStackResult result = OC_STACK_OK;
2649 // the following line floods the log with messages that are irrelevant
2650 // to most purposes. Uncomment as needed.
2651 //OC_LOG(INFO, TAG, "Entering RequestPresence");
2652 ClientCB* cbNode = NULL;
2653 OCClientResponse clientResponse;
2654 OCStackApplicationResult cbResult = OC_STACK_DELETE_TRANSACTION;
2656 LL_FOREACH(cbList, cbNode)
2658 if (OC_REST_PRESENCE != cbNode->method || !cbNode->presence)
2663 uint32_t now = GetTicks(0);
2664 OC_LOG_V(DEBUG, TAG, "this TTL level %d",
2665 cbNode->presence->TTLlevel);
2666 OC_LOG_V(DEBUG, TAG, "current ticks %d", now);
2668 if (cbNode->presence->TTLlevel > PresenceTimeOutSize)
2673 if (cbNode->presence->TTLlevel < PresenceTimeOutSize)
2675 OC_LOG_V(DEBUG, TAG, "timeout ticks %d",
2676 cbNode->presence->timeOut[cbNode->presence->TTLlevel]);
2678 if (cbNode->presence->TTLlevel >= PresenceTimeOutSize)
2680 OC_LOG(DEBUG, TAG, "No more timeout ticks");
2682 clientResponse.sequenceNumber = 0;
2683 clientResponse.result = OC_STACK_PRESENCE_TIMEOUT;
2684 clientResponse.devAddr = *cbNode->devAddr;
2685 FixUpClientResponse(&clientResponse);
2686 clientResponse.payload = NULL;
2688 // Increment the TTLLevel (going to a next state), so we don't keep
2689 // sending presence notification to client.
2690 cbNode->presence->TTLlevel++;
2691 OC_LOG_V(DEBUG, TAG, "moving to TTL level %d",
2692 cbNode->presence->TTLlevel);
2694 cbResult = cbNode->callBack(cbNode->context, cbNode->handle, &clientResponse);
2695 if (cbResult == OC_STACK_DELETE_TRANSACTION)
2697 FindAndDeleteClientCB(cbNode);
2701 if (now < cbNode->presence->timeOut[cbNode->presence->TTLlevel])
2706 CAEndpoint_t endpoint = {.adapter = CA_DEFAULT_ADAPTER};
2707 CAInfo_t requestData = {.type = CA_MSG_CONFIRM};
2708 CARequestInfo_t requestInfo = {.method = CA_GET};
2710 OC_LOG(DEBUG, TAG, "time to test server presence");
2712 CopyDevAddrToEndpoint(cbNode->devAddr, &endpoint);
2714 requestData.type = CA_MSG_NONCONFIRM;
2715 requestData.token = cbNode->token;
2716 requestData.tokenLength = cbNode->tokenLength;
2717 requestData.resourceUri = OC_RSRVD_PRESENCE_URI;
2718 requestInfo.method = CA_GET;
2719 requestInfo.info = requestData;
2721 result = OCSendRequest(&endpoint, &requestInfo);
2722 if (OC_STACK_OK != result)
2727 cbNode->presence->TTLlevel++;
2728 OC_LOG_V(DEBUG, TAG, "moving to TTL level %d", cbNode->presence->TTLlevel);
2731 if (result != OC_STACK_OK)
2733 OC_LOG(ERROR, TAG, "OCProcessPresence error");
2738 #endif // WITH_PRESENCE
2740 OCStackResult OCProcess()
2742 #ifdef WITH_PRESENCE
2743 OCProcessPresence();
2745 CAHandleRequestResponse();
2747 #ifdef ROUTING_GATEWAY
2753 #ifdef WITH_PRESENCE
2754 OCStackResult OCStartPresence(const uint32_t ttl)
2756 uint8_t tokenLength = CA_MAX_TOKEN_LEN;
2757 OCChangeResourceProperty(
2758 &(((OCResource *)presenceResource.handle)->resourceProperties),
2761 if (OC_MAX_PRESENCE_TTL_SECONDS < ttl)
2763 presenceResource.presenceTTL = OC_MAX_PRESENCE_TTL_SECONDS;
2764 OC_LOG(INFO, TAG, "Setting Presence TTL to max value");
2768 presenceResource.presenceTTL = OC_DEFAULT_PRESENCE_TTL_SECONDS;
2769 OC_LOG(INFO, TAG, "Setting Presence TTL to default value");
2773 presenceResource.presenceTTL = ttl;
2775 OC_LOG_V(DEBUG, TAG, "Presence TTL is %lu seconds", presenceResource.presenceTTL);
2777 if (OC_PRESENCE_UNINITIALIZED == presenceState)
2779 presenceState = OC_PRESENCE_INITIALIZED;
2781 OCDevAddr devAddr = { OC_DEFAULT_ADAPTER };
2783 CAToken_t caToken = NULL;
2784 CAResult_t caResult = CAGenerateToken(&caToken, tokenLength);
2785 if (caResult != CA_STATUS_OK)
2787 OC_LOG(ERROR, TAG, "CAGenerateToken error");
2788 CADestroyToken(caToken);
2789 return OC_STACK_ERROR;
2792 AddObserver(OC_RSRVD_PRESENCE_URI, NULL, 0, caToken, tokenLength,
2793 (OCResource *)presenceResource.handle, OC_LOW_QOS, OC_FORMAT_UNDEFINED, &devAddr);
2794 CADestroyToken(caToken);
2797 // Each time OCStartPresence is called
2798 // a different random 32-bit integer number is used
2799 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2801 return SendPresenceNotification(((OCResource *)presenceResource.handle)->rsrcType,
2802 OC_PRESENCE_TRIGGER_CREATE);
2805 OCStackResult OCStopPresence()
2807 OCStackResult result = OC_STACK_ERROR;
2809 if(presenceResource.handle)
2811 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2813 // make resource inactive
2814 result = OCChangeResourceProperty(
2815 &(((OCResource *) presenceResource.handle)->resourceProperties),
2819 if(result != OC_STACK_OK)
2822 "Changing the presence resource properties to ACTIVE not successful");
2826 return SendStopNotification();
2830 OCStackResult OCSetDefaultDeviceEntityHandler(OCDeviceEntityHandler entityHandler,
2831 void* callbackParameter)
2833 defaultDeviceHandler = entityHandler;
2834 defaultDeviceHandlerCallbackParameter = callbackParameter;
2839 OCStackResult OCSetPlatformInfo(OCPlatformInfo platformInfo)
2841 OC_LOG(INFO, TAG, "Entering OCSetPlatformInfo");
2843 if(myStackMode == OC_SERVER || myStackMode == OC_CLIENT_SERVER || myStackMode == OC_GATEWAY)
2845 if (validatePlatformInfo(platformInfo))
2847 return SavePlatformInfo(platformInfo);
2851 return OC_STACK_INVALID_PARAM;
2856 return OC_STACK_ERROR;
2860 OCStackResult OCSetDeviceInfo(OCDeviceInfo deviceInfo)
2862 OC_LOG(INFO, TAG, "Entering OCSetDeviceInfo");
2864 if (!deviceInfo.deviceName || deviceInfo.deviceName[0] == '\0')
2866 OC_LOG(ERROR, TAG, "Null or empty device name.");
2867 return OC_STACK_INVALID_PARAM;
2870 return SaveDeviceInfo(deviceInfo);
2873 OCStackResult OCCreateResource(OCResourceHandle *handle,
2874 const char *resourceTypeName,
2875 const char *resourceInterfaceName,
2876 const char *uri, OCEntityHandler entityHandler,
2877 void* callbackParam,
2878 uint8_t resourceProperties)
2881 OCResource *pointer = NULL;
2883 OCStackResult result = OC_STACK_ERROR;
2885 OC_LOG(INFO, TAG, "Entering OCCreateResource");
2887 if(myStackMode == OC_CLIENT)
2889 return OC_STACK_INVALID_PARAM;
2891 // Validate parameters
2892 if(!uri || uri[0]=='\0' || strlen(uri)>=MAX_URI_LENGTH )
2894 OC_LOG(ERROR, TAG, "URI is empty or too long");
2895 return OC_STACK_INVALID_URI;
2897 // Is it presented during resource discovery?
2898 if (!handle || !resourceTypeName || resourceTypeName[0] == '\0' )
2900 OC_LOG(ERROR, TAG, "Input parameter is NULL");
2901 return OC_STACK_INVALID_PARAM;
2904 if(!resourceInterfaceName || strlen(resourceInterfaceName) == 0)
2906 resourceInterfaceName = OC_RSRVD_INTERFACE_DEFAULT;
2909 // Make sure resourceProperties bitmask has allowed properties specified
2910 if (resourceProperties
2911 > (OC_ACTIVE | OC_DISCOVERABLE | OC_OBSERVABLE | OC_SLOW | OC_SECURE |
2912 OC_EXPLICIT_DISCOVERABLE))
2914 OC_LOG(ERROR, TAG, "Invalid property");
2915 return OC_STACK_INVALID_PARAM;
2918 // If the headResource is NULL, then no resources have been created...
2919 pointer = headResource;
2922 // At least one resources is in the resource list, so we need to search for
2923 // repeated URLs, which are not allowed. If a repeat is found, exit with an error
2926 if (strncmp(uri, pointer->uri, MAX_URI_LENGTH) == 0)
2928 OC_LOG_V(ERROR, TAG, "Resource %s already exists", uri);
2929 return OC_STACK_INVALID_PARAM;
2931 pointer = pointer->next;
2934 // Create the pointer and insert it into the resource list
2935 pointer = (OCResource *) OICCalloc(1, sizeof(OCResource));
2938 result = OC_STACK_NO_MEMORY;
2941 pointer->sequenceNum = OC_OFFSET_SEQUENCE_NUMBER;
2943 insertResource(pointer);
2946 str = OICStrdup(uri);
2949 result = OC_STACK_NO_MEMORY;
2954 // Set properties. Set OC_ACTIVE
2955 pointer->resourceProperties = (OCResourceProperty) (resourceProperties
2958 // Add the resourcetype to the resource
2959 result = BindResourceTypeToResource(pointer, resourceTypeName);
2960 if (result != OC_STACK_OK)
2962 OC_LOG(ERROR, TAG, "Error adding resourcetype");
2966 // Add the resourceinterface to the resource
2967 result = BindResourceInterfaceToResource(pointer, resourceInterfaceName);
2968 if (result != OC_STACK_OK)
2970 OC_LOG(ERROR, TAG, "Error adding resourceinterface");
2974 // If an entity handler has been passed, attach it to the newly created
2975 // resource. Otherwise, set the default entity handler.
2978 pointer->entityHandler = entityHandler;
2979 pointer->entityHandlerCallbackParam = callbackParam;
2983 pointer->entityHandler = defaultResourceEHandler;
2984 pointer->entityHandlerCallbackParam = NULL;
2988 result = OC_STACK_OK;
2990 #ifdef WITH_PRESENCE
2991 if (presenceResource.handle)
2993 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2994 SendPresenceNotification(pointer->rsrcType, OC_PRESENCE_TRIGGER_CREATE);
2998 if (result != OC_STACK_OK)
3000 // Deep delete of resource and other dynamic elements that it contains
3001 deleteResource(pointer);
3008 OCStackResult OCBindResource(
3009 OCResourceHandle collectionHandle, OCResourceHandle resourceHandle)
3011 OCResource *resource = NULL;
3014 OC_LOG(INFO, TAG, "Entering OCBindResource");
3016 // Validate parameters
3017 VERIFY_NON_NULL(collectionHandle, ERROR, OC_STACK_ERROR);
3018 VERIFY_NON_NULL(resourceHandle, ERROR, OC_STACK_ERROR);
3019 // Container cannot contain itself
3020 if (collectionHandle == resourceHandle)
3022 OC_LOG(ERROR, TAG, "Added handle equals collection handle");
3023 return OC_STACK_INVALID_PARAM;
3026 // Use the handle to find the resource in the resource linked list
3027 resource = findResource((OCResource *) collectionHandle);
3030 OC_LOG(ERROR, TAG, "Collection handle not found");
3031 return OC_STACK_INVALID_PARAM;
3034 // Look for an open slot to add add the child resource.
3035 // If found, add it and return success
3036 for (i = 0; i < MAX_CONTAINED_RESOURCES; i++)
3038 if (!resource->rsrcResources[i])
3040 resource->rsrcResources[i] = (OCResource *) resourceHandle;
3041 OC_LOG(INFO, TAG, "resource bound");
3043 #ifdef WITH_PRESENCE
3044 if (presenceResource.handle)
3046 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
3047 SendPresenceNotification(((OCResource *) resourceHandle)->rsrcType,
3048 OC_PRESENCE_TRIGGER_CHANGE);
3056 // Unable to add resourceHandle, so return error
3057 return OC_STACK_ERROR;
3060 OCStackResult OCUnBindResource(
3061 OCResourceHandle collectionHandle, OCResourceHandle resourceHandle)
3063 OCResource *resource = NULL;
3066 OC_LOG(INFO, TAG, "Entering OCUnBindResource");
3068 // Validate parameters
3069 VERIFY_NON_NULL(collectionHandle, ERROR, OC_STACK_ERROR);
3070 VERIFY_NON_NULL(resourceHandle, ERROR, OC_STACK_ERROR);
3071 // Container cannot contain itself
3072 if (collectionHandle == resourceHandle)
3074 OC_LOG(ERROR, TAG, "removing handle equals collection handle");
3075 return OC_STACK_INVALID_PARAM;
3078 // Use the handle to find the resource in the resource linked list
3079 resource = findResource((OCResource *) collectionHandle);
3082 OC_LOG(ERROR, TAG, "Collection handle not found");
3083 return OC_STACK_INVALID_PARAM;
3086 // Look for an open slot to add add the child resource.
3087 // If found, add it and return success
3088 for (i = 0; i < MAX_CONTAINED_RESOURCES; i++)
3090 if (resourceHandle == resource->rsrcResources[i])
3092 resource->rsrcResources[i] = (OCResource *) NULL;
3093 OC_LOG(INFO, TAG, "resource unbound");
3095 // Send notification when resource is unbounded successfully.
3096 #ifdef WITH_PRESENCE
3097 if (presenceResource.handle)
3099 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
3100 SendPresenceNotification(((OCResource *) resourceHandle)->rsrcType,
3101 OC_PRESENCE_TRIGGER_CHANGE);
3108 OC_LOG(INFO, TAG, "resource not found in collection");
3110 // Unable to add resourceHandle, so return error
3111 return OC_STACK_ERROR;
3114 OCStackResult BindResourceTypeToResource(OCResource* resource,
3115 const char *resourceTypeName)
3117 OCResourceType *pointer = NULL;
3119 OCStackResult result = OC_STACK_ERROR;
3121 VERIFY_NON_NULL(resourceTypeName, ERROR, OC_STACK_INVALID_PARAM);
3123 pointer = (OCResourceType *) OICCalloc(1, sizeof(OCResourceType));
3126 result = OC_STACK_NO_MEMORY;
3130 str = OICStrdup(resourceTypeName);
3133 result = OC_STACK_NO_MEMORY;
3136 pointer->resourcetypename = str;
3138 insertResourceType(resource, pointer);
3139 result = OC_STACK_OK;
3142 if (result != OC_STACK_OK)
3151 OCStackResult BindResourceInterfaceToResource(OCResource* resource,
3152 const char *resourceInterfaceName)
3154 OCResourceInterface *pointer = NULL;
3156 OCStackResult result = OC_STACK_ERROR;
3158 VERIFY_NON_NULL(resourceInterfaceName, ERROR, OC_STACK_INVALID_PARAM);
3160 OC_LOG_V(INFO, TAG, "Binding %s interface to %s", resourceInterfaceName, resource->uri);
3162 pointer = (OCResourceInterface *) OICCalloc(1, sizeof(OCResourceInterface));
3165 result = OC_STACK_NO_MEMORY;
3169 str = OICStrdup(resourceInterfaceName);
3172 result = OC_STACK_NO_MEMORY;
3175 pointer->name = str;
3177 // Bind the resourceinterface to the resource
3178 insertResourceInterface(resource, pointer);
3180 result = OC_STACK_OK;
3183 if (result != OC_STACK_OK)
3192 OCStackResult OCBindResourceTypeToResource(OCResourceHandle handle,
3193 const char *resourceTypeName)
3196 OCStackResult result = OC_STACK_ERROR;
3197 OCResource *resource = NULL;
3199 resource = findResource((OCResource *) handle);
3202 OC_LOG(ERROR, TAG, "Resource not found");
3203 return OC_STACK_ERROR;
3206 result = BindResourceTypeToResource(resource, resourceTypeName);
3208 #ifdef WITH_PRESENCE
3209 if(presenceResource.handle)
3211 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
3212 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
3219 OCStackResult OCBindResourceInterfaceToResource(OCResourceHandle handle,
3220 const char *resourceInterfaceName)
3223 OCStackResult result = OC_STACK_ERROR;
3224 OCResource *resource = NULL;
3226 resource = findResource((OCResource *) handle);
3229 OC_LOG(ERROR, TAG, "Resource not found");
3230 return OC_STACK_ERROR;
3233 result = BindResourceInterfaceToResource(resource, resourceInterfaceName);
3235 #ifdef WITH_PRESENCE
3236 if (presenceResource.handle)
3238 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
3239 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
3246 OCStackResult OCGetNumberOfResources(uint8_t *numResources)
3248 OCResource *pointer = headResource;
3250 VERIFY_NON_NULL(numResources, ERROR, OC_STACK_INVALID_PARAM);
3254 *numResources = *numResources + 1;
3255 pointer = pointer->next;
3260 OCResourceHandle OCGetResourceHandle(uint8_t index)
3262 OCResource *pointer = headResource;
3264 for( uint8_t i = 0; i < index && pointer; ++i)
3266 pointer = pointer->next;
3268 return (OCResourceHandle) pointer;
3271 OCStackResult OCDeleteResource(OCResourceHandle handle)
3275 OC_LOG(ERROR, TAG, "Invalid handle for deletion");
3276 return OC_STACK_INVALID_PARAM;
3279 OCResource *resource = findResource((OCResource *) handle);
3280 if (resource == NULL)
3282 OC_LOG(ERROR, TAG, "Resource not found");
3283 return OC_STACK_NO_RESOURCE;
3286 if (deleteResource((OCResource *) handle) != OC_STACK_OK)
3288 OC_LOG(ERROR, TAG, "Error deleting resource");
3289 return OC_STACK_ERROR;
3295 const char *OCGetResourceUri(OCResourceHandle handle)
3297 OCResource *resource = NULL;
3299 resource = findResource((OCResource *) handle);
3302 return resource->uri;
3304 return (const char *) NULL;
3307 OCResourceProperty OCGetResourceProperties(OCResourceHandle handle)
3309 OCResource *resource = NULL;
3311 resource = findResource((OCResource *) handle);
3314 return resource->resourceProperties;
3316 return (OCResourceProperty)-1;
3319 OCStackResult OCGetNumberOfResourceTypes(OCResourceHandle handle,
3320 uint8_t *numResourceTypes)
3322 OCResource *resource = NULL;
3323 OCResourceType *pointer = NULL;
3325 VERIFY_NON_NULL(numResourceTypes, ERROR, OC_STACK_INVALID_PARAM);
3326 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
3328 *numResourceTypes = 0;
3330 resource = findResource((OCResource *) handle);
3333 pointer = resource->rsrcType;
3336 *numResourceTypes = *numResourceTypes + 1;
3337 pointer = pointer->next;
3343 const char *OCGetResourceTypeName(OCResourceHandle handle, uint8_t index)
3345 OCResourceType *resourceType = NULL;
3347 resourceType = findResourceTypeAtIndex(handle, index);
3350 return resourceType->resourcetypename;
3352 return (const char *) NULL;
3355 OCStackResult OCGetNumberOfResourceInterfaces(OCResourceHandle handle,
3356 uint8_t *numResourceInterfaces)
3358 OCResourceInterface *pointer = NULL;
3359 OCResource *resource = NULL;
3361 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
3362 VERIFY_NON_NULL(numResourceInterfaces, ERROR, OC_STACK_INVALID_PARAM);
3364 *numResourceInterfaces = 0;
3365 resource = findResource((OCResource *) handle);
3368 pointer = resource->rsrcInterface;
3371 *numResourceInterfaces = *numResourceInterfaces + 1;
3372 pointer = pointer->next;
3378 const char *OCGetResourceInterfaceName(OCResourceHandle handle, uint8_t index)
3380 OCResourceInterface *resourceInterface = NULL;
3382 resourceInterface = findResourceInterfaceAtIndex(handle, index);
3383 if (resourceInterface)
3385 return resourceInterface->name;
3387 return (const char *) NULL;
3390 OCResourceHandle OCGetResourceHandleFromCollection(OCResourceHandle collectionHandle,
3393 OCResource *resource = NULL;
3395 if (index >= MAX_CONTAINED_RESOURCES)
3400 resource = findResource((OCResource *) collectionHandle);
3406 return resource->rsrcResources[index];
3409 OCStackResult OCBindResourceHandler(OCResourceHandle handle,
3410 OCEntityHandler entityHandler,
3411 void* callbackParam)
3413 OCResource *resource = NULL;
3415 // Validate parameters
3416 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
3418 // Use the handle to find the resource in the resource linked list
3419 resource = findResource((OCResource *)handle);
3422 OC_LOG(ERROR, TAG, "Resource not found");
3423 return OC_STACK_ERROR;
3427 resource->entityHandler = entityHandler;
3428 resource->entityHandlerCallbackParam = callbackParam;
3430 #ifdef WITH_PRESENCE
3431 if (presenceResource.handle)
3433 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
3434 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
3441 OCEntityHandler OCGetResourceHandler(OCResourceHandle handle)
3443 OCResource *resource = NULL;
3445 resource = findResource((OCResource *)handle);
3448 OC_LOG(ERROR, TAG, "Resource not found");
3453 return resource->entityHandler;
3456 void incrementSequenceNumber(OCResource * resPtr)
3458 // Increment the sequence number
3459 resPtr->sequenceNum += 1;
3460 if (resPtr->sequenceNum == MAX_SEQUENCE_NUMBER)
3462 resPtr->sequenceNum = OC_OFFSET_SEQUENCE_NUMBER+1;
3467 #ifdef WITH_PRESENCE
3468 OCStackResult SendPresenceNotification(OCResourceType *resourceType,
3469 OCPresenceTrigger trigger)
3471 OCResource *resPtr = NULL;
3472 OCStackResult result = OC_STACK_ERROR;
3473 OCMethod method = OC_REST_PRESENCE;
3474 uint32_t maxAge = 0;
3475 resPtr = findResource((OCResource *) presenceResource.handle);
3478 return OC_STACK_NO_RESOURCE;
3481 if((((OCResource *) presenceResource.handle)->resourceProperties) & OC_ACTIVE)
3483 maxAge = presenceResource.presenceTTL;
3485 result = SendAllObserverNotification(method, resPtr, maxAge,
3486 trigger, resourceType, OC_LOW_QOS);
3492 OCStackResult SendStopNotification()
3494 OCResource *resPtr = NULL;
3495 OCStackResult result = OC_STACK_ERROR;
3496 OCMethod method = OC_REST_PRESENCE;
3497 resPtr = findResource((OCResource *) presenceResource.handle);
3500 return OC_STACK_NO_RESOURCE;
3503 // maxAge is 0. ResourceType is NULL.
3504 result = SendAllObserverNotification(method, resPtr, 0, OC_PRESENCE_TRIGGER_DELETE,
3510 #endif // WITH_PRESENCE
3511 OCStackResult OCNotifyAllObservers(OCResourceHandle handle, OCQualityOfService qos)
3513 OCResource *resPtr = NULL;
3514 OCStackResult result = OC_STACK_ERROR;
3515 OCMethod method = OC_REST_NOMETHOD;
3516 uint32_t maxAge = 0;
3518 OC_LOG(INFO, TAG, "Notifying all observers");
3519 #ifdef WITH_PRESENCE
3520 if(handle == presenceResource.handle)
3524 #endif // WITH_PRESENCE
3525 VERIFY_NON_NULL(handle, ERROR, OC_STACK_ERROR);
3527 // Verify that the resource exists
3528 resPtr = findResource ((OCResource *) handle);
3531 return OC_STACK_NO_RESOURCE;
3535 //only increment in the case of regular observing (not presence)
3536 incrementSequenceNumber(resPtr);
3537 method = OC_REST_OBSERVE;
3538 maxAge = MAX_OBSERVE_AGE;
3539 #ifdef WITH_PRESENCE
3540 result = SendAllObserverNotification (method, resPtr, maxAge,
3541 OC_PRESENCE_TRIGGER_DELETE, NULL, qos);
3543 result = SendAllObserverNotification (method, resPtr, maxAge, qos);
3550 OCNotifyListOfObservers (OCResourceHandle handle,
3551 OCObservationId *obsIdList,
3552 uint8_t numberOfIds,
3553 const OCRepPayload *payload,
3554 OCQualityOfService qos)
3556 OC_LOG(INFO, TAG, "Entering OCNotifyListOfObservers");
3558 OCResource *resPtr = NULL;
3559 //TODO: we should allow the server to define this
3560 uint32_t maxAge = MAX_OBSERVE_AGE;
3562 VERIFY_NON_NULL(handle, ERROR, OC_STACK_ERROR);
3563 VERIFY_NON_NULL(obsIdList, ERROR, OC_STACK_ERROR);
3564 VERIFY_NON_NULL(payload, ERROR, OC_STACK_ERROR);
3566 resPtr = findResource ((OCResource *) handle);
3567 if (NULL == resPtr || myStackMode == OC_CLIENT)
3569 return OC_STACK_NO_RESOURCE;
3573 incrementSequenceNumber(resPtr);
3575 return (SendListObserverNotification(resPtr, obsIdList, numberOfIds,
3576 payload, maxAge, qos));
3579 OCStackResult OCDoResponse(OCEntityHandlerResponse *ehResponse)
3581 OCStackResult result = OC_STACK_ERROR;
3582 OCServerRequest *serverRequest = NULL;
3584 OC_LOG(INFO, TAG, "Entering OCDoResponse");
3586 // Validate input parameters
3587 VERIFY_NON_NULL(ehResponse, ERROR, OC_STACK_INVALID_PARAM);
3588 VERIFY_NON_NULL(ehResponse->requestHandle, ERROR, OC_STACK_INVALID_PARAM);
3591 // Get pointer to request info
3592 serverRequest = GetServerRequestUsingHandle((OCServerRequest *)ehResponse->requestHandle);
3595 // response handler in ocserverrequest.c. Usually HandleSingleResponse.
3596 result = serverRequest->ehResponseHandler(ehResponse);
3602 //-----------------------------------------------------------------------------
3603 // Private internal function definitions
3604 //-----------------------------------------------------------------------------
3605 static OCDoHandle GenerateInvocationHandle()
3607 OCDoHandle handle = NULL;
3608 // Generate token here, it will be deleted when the transaction is deleted
3609 handle = (OCDoHandle) OICMalloc(sizeof(uint8_t[CA_MAX_TOKEN_LEN]));
3612 OCFillRandomMem((uint8_t*)handle, sizeof(uint8_t[CA_MAX_TOKEN_LEN]));
3618 #ifdef WITH_PRESENCE
3619 OCStackResult OCChangeResourceProperty(OCResourceProperty * inputProperty,
3620 OCResourceProperty resourceProperties, uint8_t enable)
3624 return OC_STACK_INVALID_PARAM;
3626 if (resourceProperties
3627 > (OC_ACTIVE | OC_DISCOVERABLE | OC_OBSERVABLE | OC_SLOW))
3629 OC_LOG(ERROR, TAG, "Invalid property");
3630 return OC_STACK_INVALID_PARAM;
3634 *inputProperty = (OCResourceProperty) (*inputProperty & ~(resourceProperties));
3638 *inputProperty = (OCResourceProperty) (*inputProperty | resourceProperties);
3644 OCStackResult initResources()
3646 OCStackResult result = OC_STACK_OK;
3648 headResource = NULL;
3649 tailResource = NULL;
3650 // Init Virtual Resources
3651 #ifdef WITH_PRESENCE
3652 presenceResource.presenceTTL = OC_DEFAULT_PRESENCE_TTL_SECONDS;
3654 result = OCCreateResource(&presenceResource.handle,
3655 OC_RSRVD_RESOURCE_TYPE_PRESENCE,
3657 OC_RSRVD_PRESENCE_URI,
3661 //make resource inactive
3662 result = OCChangeResourceProperty(
3663 &(((OCResource *) presenceResource.handle)->resourceProperties),
3667 if (result == OC_STACK_OK)
3669 result = SRMInitSecureResources();
3675 void insertResource(OCResource *resource)
3679 headResource = resource;
3680 tailResource = resource;
3684 tailResource->next = resource;
3685 tailResource = resource;
3687 resource->next = NULL;
3690 OCResource *findResource(OCResource *resource)
3692 OCResource *pointer = headResource;
3696 if (pointer == resource)
3700 pointer = pointer->next;
3705 void deleteAllResources()
3707 OCResource *pointer = headResource;
3708 OCResource *temp = NULL;
3712 temp = pointer->next;
3713 #ifdef WITH_PRESENCE
3714 if (pointer != (OCResource *) presenceResource.handle)
3716 #endif // WITH_PRESENCE
3717 deleteResource(pointer);
3718 #ifdef WITH_PRESENCE
3720 #endif // WITH_PRESENCE
3724 SRMDeInitSecureResources();
3726 #ifdef WITH_PRESENCE
3727 // Ensure that the last resource to be deleted is the presence resource. This allows for all
3728 // presence notification attributed to their deletion to be processed.
3729 deleteResource((OCResource *) presenceResource.handle);
3730 #endif // WITH_PRESENCE
3733 OCStackResult deleteResource(OCResource *resource)
3735 OCResource *prev = NULL;
3736 OCResource *temp = NULL;
3739 OC_LOG(DEBUG,TAG,"resource is NULL");
3740 return OC_STACK_INVALID_PARAM;
3743 OC_LOG_V (INFO, TAG, "Deleting resource %s", resource->uri);
3745 temp = headResource;
3748 if (temp == resource)
3750 // Invalidate all Resource Properties.
3751 resource->resourceProperties = (OCResourceProperty) 0;
3752 #ifdef WITH_PRESENCE
3753 if(resource != (OCResource *) presenceResource.handle)
3755 #endif // WITH_PRESENCE
3756 OCNotifyAllObservers((OCResourceHandle)resource, OC_HIGH_QOS);
3757 #ifdef WITH_PRESENCE
3760 if(presenceResource.handle)
3762 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
3763 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_DELETE);
3766 // Only resource in list.
3767 if (temp == headResource && temp == tailResource)
3769 headResource = NULL;
3770 tailResource = NULL;
3773 else if (temp == headResource)
3775 headResource = temp->next;
3778 else if (temp == tailResource)
3780 tailResource = prev;
3781 tailResource->next = NULL;
3785 prev->next = temp->next;
3788 deleteResourceElements(temp);
3799 return OC_STACK_ERROR;
3802 void deleteResourceElements(OCResource *resource)
3809 OICFree(resource->uri);
3810 deleteResourceType(resource->rsrcType);
3811 deleteResourceInterface(resource->rsrcInterface);
3814 void deleteResourceType(OCResourceType *resourceType)
3816 OCResourceType *pointer = resourceType;
3817 OCResourceType *next = NULL;
3821 next = pointer->next;
3822 OICFree(pointer->resourcetypename);
3828 void deleteResourceInterface(OCResourceInterface *resourceInterface)
3830 OCResourceInterface *pointer = resourceInterface;
3831 OCResourceInterface *next = NULL;
3835 next = pointer->next;
3836 OICFree(pointer->name);
3842 void insertResourceType(OCResource *resource, OCResourceType *resourceType)
3844 OCResourceType *pointer = NULL;
3845 OCResourceType *previous = NULL;
3846 if (!resource || !resourceType)
3850 // resource type list is empty.
3851 else if (!resource->rsrcType)
3853 resource->rsrcType = resourceType;
3857 pointer = resource->rsrcType;
3861 if (!strcmp(resourceType->resourcetypename, pointer->resourcetypename))
3863 OC_LOG_V(INFO, TAG, "Type %s already exists", resourceType->resourcetypename);
3864 OICFree(resourceType->resourcetypename);
3865 OICFree(resourceType);
3869 pointer = pointer->next;
3871 previous->next = resourceType;
3873 resourceType->next = NULL;
3875 OC_LOG_V(INFO, TAG, "Added type %s to %s", resourceType->resourcetypename, resource->uri);
3878 OCResourceType *findResourceTypeAtIndex(OCResourceHandle handle, uint8_t index)
3880 OCResource *resource = NULL;
3881 OCResourceType *pointer = NULL;
3883 // Find the specified resource
3884 resource = findResource((OCResource *) handle);
3890 // Make sure a resource has a resourcetype
3891 if (!resource->rsrcType)
3896 // Iterate through the list
3897 pointer = resource->rsrcType;
3898 for(uint8_t i = 0; i< index && pointer; ++i)
3900 pointer = pointer->next;
3905 OCResourceType *findResourceType(OCResourceType * resourceTypeList, const char * resourceTypeName)
3907 if(resourceTypeList && resourceTypeName)
3909 OCResourceType * rtPointer = resourceTypeList;
3910 while(resourceTypeName && rtPointer)
3912 if(rtPointer->resourcetypename &&
3913 strcmp(resourceTypeName, (const char *)
3914 (rtPointer->resourcetypename)) == 0)
3918 rtPointer = rtPointer->next;
3926 * Insert a new interface into interface linked list only if not already present.
3927 * If alredy present, 2nd arg is free'd.
3928 * Default interface will always be first if present.
3930 void insertResourceInterface(OCResource *resource, OCResourceInterface *newInterface)
3932 OCResourceInterface *pointer = NULL;
3933 OCResourceInterface *previous = NULL;
3935 newInterface->next = NULL;
3937 OCResourceInterface **firstInterface = &(resource->rsrcInterface);
3939 if (!*firstInterface)
3941 *firstInterface = newInterface;
3943 else if (strcmp(newInterface->name, OC_RSRVD_INTERFACE_DEFAULT) == 0)
3945 if (strcmp((*firstInterface)->name, OC_RSRVD_INTERFACE_DEFAULT) == 0)
3947 OICFree(newInterface->name);
3948 OICFree(newInterface);
3953 newInterface->next = *firstInterface;
3954 *firstInterface = newInterface;
3959 pointer = *firstInterface;
3962 if (strcmp(newInterface->name, pointer->name) == 0)
3964 OICFree(newInterface->name);
3965 OICFree(newInterface);
3969 pointer = pointer->next;
3971 previous->next = newInterface;
3975 OCResourceInterface *findResourceInterfaceAtIndex(OCResourceHandle handle,
3978 OCResource *resource = NULL;
3979 OCResourceInterface *pointer = NULL;
3981 // Find the specified resource
3982 resource = findResource((OCResource *) handle);
3988 // Make sure a resource has a resourceinterface
3989 if (!resource->rsrcInterface)
3994 // Iterate through the list
3995 pointer = resource->rsrcInterface;
3997 for (uint8_t i = 0; i < index && pointer; ++i)
3999 pointer = pointer->next;
4005 * This function splits the uri using the '?' delimiter.
4006 * "uriWithoutQuery" is the block of characters between the beginning
4007 * till the delimiter or '\0' which ever comes first.
4008 * "query" is whatever is to the right of the delimiter if present.
4009 * No delimiter sets the query to NULL.
4010 * If either are present, they will be malloc'ed into the params 2, 3.
4011 * The first param, *uri is left untouched.
4013 * NOTE: This function does not account for whitespace at the end of the uri NOR
4014 * malformed uri's with '??'. Whitespace at the end will be assumed to be
4015 * part of the query.
4017 OCStackResult getQueryFromUri(const char * uri, char** query, char ** uriWithoutQuery)
4021 return OC_STACK_INVALID_URI;
4023 if(!query || !uriWithoutQuery)
4025 return OC_STACK_INVALID_PARAM;
4029 *uriWithoutQuery = NULL;
4031 size_t uriWithoutQueryLen = 0;
4032 size_t queryLen = 0;
4033 size_t uriLen = strlen(uri);
4035 char *pointerToDelimiter = strstr(uri, "?");
4037 uriWithoutQueryLen = pointerToDelimiter == NULL ? uriLen : (size_t)(pointerToDelimiter - uri);
4038 queryLen = pointerToDelimiter == NULL ? 0 : uriLen - uriWithoutQueryLen - 1;
4040 if (uriWithoutQueryLen)
4042 *uriWithoutQuery = (char *) OICCalloc(uriWithoutQueryLen + 1, 1);
4043 if (!*uriWithoutQuery)
4047 OICStrcpy(*uriWithoutQuery, uriWithoutQueryLen +1, uri);
4051 *query = (char *) OICCalloc(queryLen + 1, 1);
4054 OICFree(*uriWithoutQuery);
4055 *uriWithoutQuery = NULL;
4058 OICStrcpy(*query, queryLen + 1, pointerToDelimiter + 1);
4064 return OC_STACK_NO_MEMORY;
4067 const OicUuid_t* OCGetServerInstanceID(void)
4069 static bool generated = false;
4070 static OicUuid_t sid;
4076 if (GetDoxmDeviceID(&sid) != OC_STACK_OK)
4078 OC_LOG(FATAL, TAG, "Generate UUID for Server Instance failed!");
4085 const char* OCGetServerInstanceIDString(void)
4087 static bool generated = false;
4088 static char sidStr[UUID_STRING_SIZE];
4095 const OicUuid_t* sid = OCGetServerInstanceID();
4097 if(OCConvertUuidToString(sid->id, sidStr) != RAND_UUID_OK)
4099 OC_LOG(FATAL, TAG, "Generate UUID String for Server Instance failed!");
4107 CAResult_t OCSelectNetwork()
4109 CAResult_t retResult = CA_STATUS_FAILED;
4110 CAResult_t caResult = CA_STATUS_OK;
4112 CATransportAdapter_t connTypes[] = {
4114 CA_ADAPTER_RFCOMM_BTEDR,
4115 CA_ADAPTER_GATT_BTLE
4118 ,CA_ADAPTER_REMOTE_ACCESS
4121 int numConnTypes = sizeof(connTypes)/sizeof(connTypes[0]);
4123 for(int i = 0; i<numConnTypes; i++)
4125 // Ignore CA_NOT_SUPPORTED error. The CA Layer may have not compiled in the interface.
4126 if(caResult == CA_STATUS_OK || caResult == CA_NOT_SUPPORTED)
4128 caResult = CASelectNetwork(connTypes[i]);
4129 if(caResult == CA_STATUS_OK)
4131 retResult = CA_STATUS_OK;
4136 if(retResult != CA_STATUS_OK)
4138 return caResult; // Returns error of appropriate transport that failed fatally.
4144 OCStackResult CAResultToOCResult(CAResult_t caResult)
4150 case CA_STATUS_INVALID_PARAM:
4151 return OC_STACK_INVALID_PARAM;
4152 case CA_ADAPTER_NOT_ENABLED:
4153 return OC_STACK_ADAPTER_NOT_ENABLED;
4154 case CA_SERVER_STARTED_ALREADY:
4156 case CA_SERVER_NOT_STARTED:
4157 return OC_STACK_ERROR;
4158 case CA_DESTINATION_NOT_REACHABLE:
4159 return OC_STACK_COMM_ERROR;
4160 case CA_SOCKET_OPERATION_FAILED:
4161 return OC_STACK_COMM_ERROR;
4162 case CA_SEND_FAILED:
4163 return OC_STACK_COMM_ERROR;
4164 case CA_RECEIVE_FAILED:
4165 return OC_STACK_COMM_ERROR;
4166 case CA_MEMORY_ALLOC_FAILED:
4167 return OC_STACK_NO_MEMORY;
4168 case CA_REQUEST_TIMEOUT:
4169 return OC_STACK_TIMEOUT;
4170 case CA_DESTINATION_DISCONNECTED:
4171 return OC_STACK_COMM_ERROR;
4172 case CA_STATUS_FAILED:
4173 return OC_STACK_ERROR;
4174 case CA_NOT_SUPPORTED:
4175 return OC_STACK_NOTIMPL;
4177 return OC_STACK_ERROR;