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 #include "ocstackinternal.h"
27 #include "ocresourcehandler.h"
28 #include "occlientcb.h"
29 #include "ocobserve.h"
34 #include "ocserverrequest.h"
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 OC_STACK_UNINITIALIZED = 0, OC_STACK_INITIALIZED
45 OC_PRESENCE_UNINITIALIZED = 0, OC_PRESENCE_INITIALIZED
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
52 static OCStackState stackState = OC_STACK_UNINITIALIZED;
54 OCResource *headResource = NULL;
56 static OCPresenceState presenceState = OC_PRESENCE_UNINITIALIZED;
57 static PresenceResource presenceResource;
58 uint8_t PresenceTimeOutSize = 0;
59 uint32_t PresenceTimeOut[] = {50, 75, 85, 95, 100};
63 OCDeviceEntityHandler defaultDeviceHandler;
65 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
68 #define TAG PCF("OCStack")
69 #define VERIFY_NON_NULL(arg, logLevel, retVal) { if (!(arg)) { OC_LOG((logLevel), \
70 TAG, PCF(#arg " is NULL")); return (retVal); } }
72 //TODO: we should allow the server to define this
73 #define MAX_OBSERVE_AGE (0x2FFFFUL)
74 //-----------------------------------------------------------------------------
76 //-----------------------------------------------------------------------------
77 extern void DeinitOCSecurityInfo();
79 //-----------------------------------------------------------------------------
80 // Internal API function
81 //-----------------------------------------------------------------------------
83 // This internal function is called to update the stack with the status of
84 // observers and communication failures
85 OCStackResult OCStackFeedBack(OCCoAPToken * token, uint8_t status)
87 OCStackResult result = OC_STACK_ERROR;
88 ResourceObserver * observer = NULL;
89 OCEntityHandlerRequest ehRequest = {0};
93 case OC_OBSERVER_NOT_INTERESTED:
94 OC_LOG(DEBUG, TAG, PCF("observer is not interested in our notifications anymore"));
95 observer = GetObserverUsingToken (token);
98 result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) NULL,
99 OC_REST_NOMETHOD, (OCResourceHandle) NULL, NULL, NULL, 0,
100 NULL, OC_OBSERVE_DEREGISTER, observer->observeId);
101 if(result != OC_STACK_OK)
105 observer->resource->entityHandler(OC_OBSERVE_FLAG, &ehRequest);
107 //observer is not observing anymore
108 result = DeleteObserverUsingToken (token);
109 if(result == OC_STACK_OK)
111 OC_LOG(DEBUG, TAG, PCF("Removed observer successfully"));
115 result = OC_STACK_OK;
116 OC_LOG(DEBUG, TAG, PCF("Observer Removal failed"));
119 case OC_OBSERVER_STILL_INTERESTED:
120 //observer is still interested
121 OC_LOG(DEBUG, TAG, PCF("observer is interested in our \
122 notifications, reset the failedCount"));
123 observer = GetObserverUsingToken(token);
126 observer->forceHighQos = 0;
127 observer->failedCommCount = 0;
128 result = OC_STACK_OK;
132 result = OC_STACK_OBSERVER_NOT_FOUND;
135 case OC_OBSERVER_FAILED_COMM:
136 //observer is not reachable
137 OC_LOG(DEBUG, TAG, PCF("observer is unreachable"));
138 observer = GetObserverUsingToken(token);
141 if(observer->failedCommCount >= MAX_OBSERVER_FAILED_COMM)
143 result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) NULL,
144 OC_REST_NOMETHOD, (OCResourceHandle) NULL, NULL, NULL, 0,
145 NULL, OC_OBSERVE_DEREGISTER, observer->observeId);
146 if(result != OC_STACK_OK)
148 return OC_STACK_ERROR;
150 observer->resource->entityHandler(OC_OBSERVE_FLAG, &ehRequest);
151 //observer is unreachable
152 result = DeleteObserverUsingToken (token);
153 if(result == OC_STACK_OK)
155 OC_LOG(DEBUG, TAG, PCF("Removed observer successfully"));
159 result = OC_STACK_OK;
160 OC_LOG(DEBUG, TAG, PCF("Observer Removal failed"));
165 observer->failedCommCount++;
166 result = OC_STACK_CONTINUE;
168 observer->forceHighQos = 1;
169 OC_LOG_V(DEBUG, TAG, "Failed count for this observer is %d",observer->failedCommCount);
173 OC_LOG(ERROR, TAG, PCF("Unknown status"));
174 result = OC_STACK_ERROR;
180 //This function will be called back by occoap layer when a request is received
181 OCStackResult HandleStackRequests(OCServerProtocolRequest * protocolRequest)
183 OC_LOG(INFO, TAG, PCF("Entering HandleStackRequests (OCStack Layer)"));
185 OCStackResult result = OC_STACK_ERROR;
186 ResourceHandling resHandling;
187 OCResource *resource;
189 OCServerRequest * request = GetServerRequestUsingToken(protocolRequest->requestToken);
192 OC_LOG(INFO, TAG, PCF("This is a new Server Request"));
193 result = AddServerRequest(&request, protocolRequest->coapID,
194 protocolRequest->delayedResNeeded, protocolRequest->secured, 0,
195 protocolRequest->method, protocolRequest->numRcvdVendorSpecificHeaderOptions,
196 protocolRequest->observationOption, protocolRequest->qos,
197 protocolRequest->query, protocolRequest->rcvdVendorSpecificHeaderOptions,
198 protocolRequest->reqJSONPayload, &protocolRequest->requestToken,
199 &protocolRequest->requesterAddr, protocolRequest->resourceUrl,
200 protocolRequest->reqTotalSize);
201 if (OC_STACK_OK != result)
203 OC_LOG(ERROR, TAG, PCF("Error adding server request"));
206 VERIFY_NON_NULL(request, ERROR, OC_STACK_NO_MEMORY);
208 if(!protocolRequest->reqMorePacket)
210 request->requestComplete = 1;
215 OC_LOG(INFO, TAG, PCF("This is either a repeated Server Request or blocked Server Request"));
218 if(request->requestComplete)
220 OC_LOG(INFO, TAG, PCF("This Server Request is complete"));
221 result = DetermineResourceHandling (request, &resHandling, &resource);
222 if (result == OC_STACK_OK)
224 result = ProcessRequest(resHandling, resource, request);
228 result = OC_STACK_ERROR;
233 OC_LOG(INFO, TAG, PCF("This Server Request is incomplete"));
234 result = OC_STACK_CONTINUE;
239 //This function will be called back by occoap layer when a response is received
240 void HandleStackResponses(OCResponse * response)
242 OCStackApplicationResult result = OC_STACK_DELETE_TRANSACTION;
243 OC_LOG(INFO, TAG, PCF("Entering HandleStackResponses (OCStack Layer)"));
245 if (response->cbNode)
247 OC_LOG(INFO, TAG, PCF("Calling into application address space"));
248 result = response->cbNode->callBack(response->cbNode->context,
249 response->cbNode->handle, response->clientResponse);
250 if (result == OC_STACK_DELETE_TRANSACTION ||
251 response->clientResponse->result == OC_STACK_COMM_ERROR ||
252 response->clientResponse->result == OC_STACK_RESOURCE_DELETED)
254 FindAndDeleteClientCB(response->cbNode);
259 int ParseIPv4Address(unsigned char * ipAddrStr, uint8_t * ipAddr, uint16_t * port)
262 unsigned char *itr, *coap;
263 uint8_t dotCount = 0;
267 /* search for scheme */
269 if (!isdigit((unsigned char) *ipAddrStr))
271 coap = (unsigned char *) OC_COAP_SCHEME;
272 while (*coap && tolower(*itr) == *coap)
281 if (isdigit((unsigned char) *ipAddrStr))
284 ipAddr[index] += *ipAddrStr - '0';
286 else if ((unsigned char) *ipAddrStr == '.')
298 if(*ipAddrStr == ':')
302 if (isdigit((unsigned char) *ipAddrStr))
305 *port += *ipAddrStr - '0';
316 if (ipAddr[0] < 255 && ipAddr[1] < 255 && ipAddr[2] < 255 && ipAddr[3] < 255
327 //-----------------------------------------------------------------------------
328 // Private internal function prototypes
329 //-----------------------------------------------------------------------------
331 static OCDoHandle GenerateInvocationHandle();
332 static OCStackResult initResources();
333 static void insertResource(OCResource *resource);
334 static OCResource *findResource(OCResource *resource);
335 static void insertResourceType(OCResource *resource,
336 OCResourceType *resourceType);
337 static OCResourceType *findResourceTypeAtIndex(OCResourceHandle handle,
339 static void insertResourceInterface(OCResource *resource,
340 OCResourceInterface *resourceInterface);
341 static OCResourceInterface *findResourceInterfaceAtIndex(
342 OCResourceHandle handle, uint8_t index);
343 static void deleteResourceType(OCResourceType *resourceType);
344 static void deleteResourceInterface(OCResourceInterface *resourceInterface);
345 static void deleteResourceElements(OCResource *resource);
346 static int deleteResource(OCResource *resource);
347 static void deleteAllResources();
348 static void incrementSequenceNumber(OCResource * resPtr);
349 static OCStackResult verifyUriQueryLength(const char * inputUri,
351 static uint8_t OCIsPacketTransferRequired(const char *request, const char *response, uint16_t size);
352 OCStackResult getResourceType(const char * uri, unsigned char** resourceType, char ** newURI);
354 //-----------------------------------------------------------------------------
356 //-----------------------------------------------------------------------------
359 * Initialize the OC Stack. Must be called prior to starting the stack.
362 * IP Address of host device
364 * Port of host device
366 * Host device is client, server, or client-server
369 * OC_STACK_OK - no errors
370 * OC_STACK_ERROR - stack init error
372 OCStackResult OCInit(const char *ipAddr, uint16_t port, OCMode mode)
374 OCStackResult result = OC_STACK_ERROR;
375 OC_LOG(INFO, TAG, PCF("Entering OCInit"));
379 OC_LOG_V(INFO, TAG, "IP Address = %s", ipAddr);
385 OC_LOG(INFO, TAG, PCF("Client mode"));
388 OC_LOG(INFO, TAG, PCF("Server mode"));
390 case OC_CLIENT_SERVER:
391 OC_LOG(INFO, TAG, PCF("Client-server mode"));
394 OC_LOG(ERROR, TAG, PCF("Invalid mode"));
395 return OC_STACK_ERROR;
400 defaultDeviceHandler = NULL;
403 PresenceTimeOutSize = sizeof(PresenceTimeOut)/sizeof(PresenceTimeOut[0]) - 1;
404 #endif // WITH_PRESENCE
406 // Make call to OCCoAP layer
407 result = OCInitCoAP(ipAddr, (uint16_t) port, myStackMode);
408 if (result == OC_STACK_OK)
410 stackState = OC_STACK_INITIALIZED;
412 // Initialize resource
413 if(result == OC_STACK_OK && myStackMode != OC_CLIENT)
415 result = initResources();
417 if(result != OC_STACK_OK)
419 OC_LOG(ERROR, TAG, PCF("Stack initialization error"));
425 * Stop the OC stack. Use for a controlled shutdown.
427 * OC_STACK_OK - no errors
428 * OC_STACK_ERROR - stack not initialized
430 OCStackResult OCStop()
432 OCStackResult result = OC_STACK_ERROR;
434 OC_LOG(INFO, TAG, PCF("Entering OCStop"));
436 if (stackState != OC_STACK_INITIALIZED)
438 OC_LOG(ERROR, TAG, PCF("Stack not initialized"));
439 return OC_STACK_ERROR;
443 // Ensure that the TTL associated with ANY and ALL presence notifications originating from
444 // here send with the code "OC_STACK_PRESENCE_STOPPED" result.
445 presenceResource.presenceTTL = 0;
446 #endif // WITH_PRESENCE
448 // Free memory dynamically allocated for resources
449 deleteAllResources();
451 // Make call to OCCoAP layer
452 if (OCStopCoAP() == OC_STACK_OK)
454 // Remove all observers
455 DeleteObserverList();
456 // Remove all the client callbacks
457 DeleteClientCBList();
458 stackState = OC_STACK_UNINITIALIZED;
459 result = OC_STACK_OK;
461 result = OC_STACK_ERROR;
464 // Deinit security blob
465 DeinitOCSecurityInfo();
467 if (result != OC_STACK_OK) {
468 OC_LOG(ERROR, TAG, PCF("Stack stop error"));
475 * Verify the lengths of the URI and the query separately
477 * @param inputUri - Input URI and query.
478 * @param uriLen - The length of the initial URI with query.
480 * Note: The '?' that appears after the URI is not considered as
481 * a part of the query.
483 OCStackResult verifyUriQueryLength(const char *inputUri, uint16_t uriLen)
487 query = strchr (inputUri, '?');
491 if((query - inputUri) > MAX_URI_LENGTH)
493 return OC_STACK_INVALID_URI;
496 if((inputUri + uriLen - 1 - query) > MAX_QUERY_LENGTH)
498 return OC_STACK_INVALID_QUERY;
501 else if(uriLen > MAX_URI_LENGTH)
503 return OC_STACK_INVALID_URI;
509 * Discover or Perform requests on a specified resource (specified by that Resource's respective URI).
511 * @param handle - @ref OCDoHandle to refer to the request sent out on behalf of calling this API.
512 * @param method - @ref OCMethod to perform on the resource
513 * @param requiredUri - URI of the resource to interact with
514 * @param referenceUri - URI of the reference resource
515 * @param request - JSON encoded request
516 * @param qos - quality of service
517 * @param cbData - struct that contains asynchronous callback function that is invoked
518 * by the stack when discovery or resource interaction is complete
519 * @param options - The address of an array containing the vendor specific header
520 * header options to be sent with the request
521 * @param numOptions - Number of vendor specific header options to be included
524 * OC_STACK_OK - no errors
525 * OC_STACK_INVALID_CALLBACK - invalid callback function pointer
526 * OC_STACK_INVALID_METHOD - invalid resource method
527 * OC_STACK_INVALID_URI - invalid required or reference URI
530 OCStackResult OCDoResource(OCDoHandle *handle, OCMethod method, const char *requiredUri,
531 const char *referenceUri, const char *request,
532 OCQualityOfService qos, OCCallbackData *cbData,
533 OCHeaderOption * options, uint8_t numOptions)
535 OCStackResult result = OC_STACK_ERROR;
537 ClientCB *clientCB = NULL;
538 unsigned char * requestUri = NULL;
539 unsigned char * resourceType = NULL;
540 char * newUri = (char *)requiredUri;
543 OC_LOG(INFO, TAG, PCF("Entering OCDoResource"));
545 // Validate input parameters
546 VERIFY_NON_NULL(cbData, FATAL, OC_STACK_INVALID_CALLBACK);
547 VERIFY_NON_NULL(cbData->cb, FATAL, OC_STACK_INVALID_CALLBACK);
549 TODO ("Need to form the final query by concatenating require and reference URI's");
550 VERIFY_NON_NULL(requiredUri, FATAL, OC_STACK_INVALID_URI);
552 uint16_t uriLen = strlen(requiredUri);
554 // ToDo: We should also check if the requiredUri has a mutlicast address, then qos has to be OC_Low_QOS
561 case OC_REST_OBSERVE:
562 case OC_REST_OBSERVE_ALL:
563 case OC_REST_CANCEL_OBSERVE:
566 case OC_REST_PRESENCE:
570 result = OC_STACK_INVALID_METHOD;
574 if((result = verifyUriQueryLength(requiredUri, uriLen)) != OC_STACK_OK)
579 if((request) && (strlen(request) > MAX_REQUEST_LENGTH))
581 result = OC_STACK_INVALID_PARAM;
586 if(method == OC_REST_PRESENCE)
588 result = getResourceType(requiredUri, &resourceType, &newUri);
590 OC_LOG_V(DEBUG, TAG, "Got Resource Type: %s", resourceType);
594 OC_LOG(DEBUG, TAG, "Got Resource Type is NULL.");
596 if(result != OC_STACK_OK)
601 #endif // WITH_PRESENCE
603 requestUri = (unsigned char *) OCMalloc(uriLen + 1);
606 memcpy(requestUri, newUri, (uriLen + 1));
610 result = OC_STACK_NO_MEMORY;
614 *handle = GenerateInvocationHandle();
617 result = OC_STACK_NO_MEMORY;
621 // Generate token which will be used by OCStack to match responses received
623 OCGenerateCoAPToken(&token);
625 if((result = AddClientCB(&clientCB, cbData, &token, handle, method, requestUri, resourceType))
628 result = OC_STACK_NO_MEMORY;
632 // Make call to OCCoAP layer
633 result = OCDoCoAPResource(method, qos, &token, newUri, request, options, numOptions);
636 if(newUri != requiredUri)
640 if (result != OC_STACK_OK)
642 OC_LOG(ERROR, TAG, PCF("OCDoResource error"));
643 FindAndDeleteClientCB(clientCB);
649 * Cancel a request associated with a specific @ref OCDoResource invocation.
651 * @param handle - Used to identify a specific OCDoResource invocation.
652 * @param qos - used to specify Quality of Service (read below for more info)
653 * @param options- used to specify vendor specific header options when sending
654 * explicit observe cancellation
655 * @param numOptions- Number of header options to be included
658 * OC_STACK_OK - No errors; Success
659 * OC_STACK_INVALID_PARAM - The handle provided is invalid.
661 OCStackResult OCCancel(OCDoHandle handle, OCQualityOfService qos, OCHeaderOption * options,
662 uint8_t numOptions) {
664 * This ftn is implemented one of two ways in the case of observation:
666 * 1. qos == OC_NON_CONFIRMABLE. When observe is unobserved..
667 * Remove the callback associated on client side.
668 * When the next notification comes in from server,
669 * reply with RESET message to server.
670 * Keep in mind that the server will react to RESET only
671 * if the last notification was sent ans CON
673 * 2. qos == OC_CONFIRMABLE. When OCCancel is called,
674 * and it is associated with an observe request
675 * (i.e. ClientCB->method == OC_REST_OBSERVE || OC_REST_OBSERVE_ALL),
676 * Send CON Observe request to server with
677 * observe flag = OC_RESOURCE_OBSERVE_DEREGISTER.
678 * Remove the callback associated on client side.
680 OCStackResult ret = OC_STACK_OK;
683 return OC_STACK_INVALID_PARAM;
686 OC_LOG(INFO, TAG, PCF("Entering OCCancel"));
688 ClientCB *clientCB = GetClientCB(NULL, handle, NULL);
691 switch (clientCB->method)
693 case OC_REST_OBSERVE:
694 case OC_REST_OBSERVE_ALL:
695 if(qos == OC_HIGH_QOS)
697 ret = OCDoCoAPResource(OC_REST_CANCEL_OBSERVE, qos,
698 &(clientCB->token), (const char *) clientCB->requestUri, NULL, options,
703 FindAndDeleteClientCB(clientCB);
707 case OC_REST_PRESENCE:
708 FindAndDeleteClientCB(clientCB);
712 return OC_STACK_INVALID_METHOD;
718 OCStackResult OCProcessPresence()
720 OCStackResult result = OC_STACK_OK;
721 uint8_t ipAddr[4] = { 0 };
724 OC_LOG(INFO, TAG, PCF("Entering RequestPresence"));
725 ClientCB* cbNode = NULL;
727 OCClientResponse clientResponse;
728 OCResponse * response = NULL;
730 LL_FOREACH(cbList, cbNode) {
731 if(OC_REST_PRESENCE == cbNode->method)
735 uint32_t now = GetTime(0);
736 OC_LOG_V(DEBUG, TAG, "----------------this TTL level %d", cbNode->presence->TTLlevel);
737 OC_LOG_V(DEBUG, TAG, "----------------current ticks %d", now);
740 if(cbNode->presence->TTLlevel >= (PresenceTimeOutSize + 1))
745 if(cbNode->presence->TTLlevel < PresenceTimeOutSize){
746 OC_LOG_V(DEBUG, TAG, "----------------timeout ticks %d",
747 cbNode->presence->timeOut[cbNode->presence->TTLlevel]);
750 if(cbNode->presence->TTLlevel >= PresenceTimeOutSize)
752 OC_LOG(DEBUG, TAG, PCF("----------------No more timeout ticks"));
753 if (ParseIPv4Address( cbNode->requestUri, ipAddr, &port))
755 OCBuildIPv4Address(ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3], port,
757 result = FormOCClientResponse(&clientResponse, OC_STACK_PRESENCE_TIMEOUT,
758 (OCDevAddr *) &dst, 0, NULL);
759 if(result != OC_STACK_OK)
763 result = FormOCResponse(&response, cbNode, 0, &clientResponse);
764 if(result != OC_STACK_OK)
769 // Increment the TTLLevel (going to a next state), so we don't keep
770 // sending presence notification to client.
771 cbNode->presence->TTLlevel++;
772 OC_LOG_V(DEBUG, TAG, "----------------moving to TTL level %d",
773 cbNode->presence->TTLlevel);
777 result = OC_STACK_INVALID_IP;
780 HandleStackResponses(response);
782 if(now >= cbNode->presence->timeOut[cbNode->presence->TTLlevel])
784 OC_LOG(DEBUG, TAG, PCF("time to test server presence =========="));
786 OCGenerateCoAPToken(&token);
787 result = OCDoCoAPResource(OC_REST_GET, OC_LOW_QOS,
788 &token, (const char *)cbNode->requestUri, NULL, NULL, 0);
789 if(result != OC_STACK_OK)
793 cbNode->presence->TTLlevel++;
794 OC_LOG_V(DEBUG, TAG, "----------------moving to TTL level %d", cbNode->presence->TTLlevel);
800 if (result != OC_STACK_OK)
802 OC_LOG(ERROR, TAG, PCF("OCProcessPresence error"));
809 * Called in main loop of OC client or server. Allows low-level processing of
813 * OC_STACK_OK - no errors
814 * OC_STACK_ERROR - stack process error
816 OCStackResult OCProcess() {
818 OC_LOG(INFO, TAG, PCF("Entering OCProcess"));
829 * When operating in @ref OCServer or @ref OCClientServer mode, this API will start sending out
830 * presence notifications to clients via multicast. Once this API has been called with a success,
831 * clients may query for this server's presence and this server's stack will respond via multicast.
833 * Server can call this function when it comes online for the first time, or when it comes back
834 * online from offline mode, or when it re enters network.
836 * @param ttl - Time To Live in seconds
837 * Note: If ttl is '0', then the default stack value will be used (60 Seconds).
840 * OC_STACK_OK - No errors; Success
842 OCStackResult OCStartPresence(const uint32_t ttl)
844 OCChangeResourceProperty(
845 &(((OCResource *)presenceResource.handle)->resourceProperties),
850 presenceResource.presenceTTL = ttl;
853 if(OC_PRESENCE_UNINITIALIZED == presenceState)
855 OCDevAddr multiCastAddr;
858 presenceState = OC_PRESENCE_INITIALIZED;
859 OCGenerateCoAPToken(&token);
860 OCBuildIPv4Address(224, 0, 1, 187, 5683, &multiCastAddr);
861 //add the presence observer
862 AddObserver(OC_PRESENCE_URI, NULL, 0, &token, &multiCastAddr,
863 (OCResource *)presenceResource.handle, OC_LOW_QOS);
866 // Each time OCStartPresence is called
867 // a different random 32-bit integer number is used
868 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
870 return SendPresenceNotification(NULL);
874 * When operating in @ref OCServer or @ref OCClientServer mode, this API will stop sending out
875 * presence notifications to clients via multicast. Once this API has been called with a success,
876 * this server's stack will not respond to clients querying for this server's presence.
878 * Server can call this function when it is terminating, going offline, or when going
882 * OC_STACK_OK - No errors; Success
884 OCStackResult OCStopPresence()
886 OCStackResult result = OC_STACK_ERROR;
887 //make resource inactive
888 result = OCChangeResourceProperty(
889 &(((OCResource *) presenceResource.handle)->resourceProperties),
891 result = SendPresenceNotification(NULL);
898 OCStackResult OCSetDefaultDeviceEntityHandler(OCDeviceEntityHandler entityHandler)
900 defaultDeviceHandler = entityHandler;
908 * @param handle - pointer to handle to newly created resource. Set by ocstack. Used to refer to resource
909 * @param resourceTypeName - name of resource type. Example: "core.led"
910 * @param resourceInterfaceName - name of resource interface. Example: "core.rw"
911 * @param uri - URI of the resource. Example: "/a/led"
912 * @param entityHandler - entity handler function that is called by ocstack to handle requests, etc
913 * NULL for default entity handler
914 * @param resourceProperties - properties supported by resource. Example: OC_DISCOVERABLE|OC_OBSERVABLE
917 * OC_STACK_OK - no errors
918 * OC_STACK_ERROR - stack process error
920 OCStackResult OCCreateResource(OCResourceHandle *handle,
921 const char *resourceTypeName,
922 const char *resourceInterfaceName,
923 const char *uri, OCEntityHandler entityHandler,
924 uint8_t resourceProperties) {
926 OCResource *pointer = NULL;
929 OCStackResult result = OC_STACK_ERROR;
931 OC_LOG(INFO, TAG, PCF("Entering OCCreateResource"));
933 if(myStackMode == OC_CLIENT)
937 // Validate parameters
938 // Is it presented during resource discovery?
939 if (!handle || !resourceTypeName || !uri) {
940 OC_LOG(ERROR, TAG, PCF("Input parameter is NULL"));
941 return OC_STACK_INVALID_PARAM;
944 if(!resourceInterfaceName || strlen(resourceInterfaceName) == 0) {
945 resourceInterfaceName = OC_RSRVD_INTERFACE_DEFAULT;
948 // Make sure resourceProperties bitmask has allowed properties specified
949 if (resourceProperties
950 > (OC_ACTIVE | OC_DISCOVERABLE | OC_OBSERVABLE | OC_SLOW | OC_SECURE)) {
951 OC_LOG(ERROR, TAG, PCF("Invalid property"));
952 return OC_STACK_INVALID_PARAM;
955 // If the headResource is NULL, then no resources have been created...
956 pointer = headResource;
958 // At least one resources is in the resource list, so we need to search for
959 // repeated URLs, which are not allowed. If a repeat is found, exit with an error
961 if (strcmp(uri, pointer->uri) == 0) {
962 OC_LOG(ERROR, TAG, PCF("URI already in use"));
963 return OC_STACK_INVALID_PARAM;
965 pointer = pointer->next;
968 // Create the pointer and insert it into the resource list
969 pointer = (OCResource *) OCMalloc(sizeof(OCResource));
973 memset(pointer, 0, sizeof(OCResource));
974 pointer->sequenceNum = OC_OFFSET_SEQUENCE_NUMBER;
976 insertResource(pointer);
979 size = strlen(uri) + 1;
980 str = (char *) OCMalloc(size);
984 strncpy(str, uri, size);
987 // Set properties. Set OC_ACTIVE
988 pointer->resourceProperties = (OCResourceProperty) (resourceProperties
991 // Add the resourcetype to the resource
992 result = BindResourceTypeToResource(pointer, resourceTypeName);
993 if (result != OC_STACK_OK) {
994 OC_LOG(ERROR, TAG, PCF("Error adding resourcetype"));
998 // Add the resourceinterface to the resource
999 result = BindResourceInterfaceToResource(pointer, resourceInterfaceName);
1000 if (result != OC_STACK_OK) {
1001 OC_LOG(ERROR, TAG, PCF("Error adding resourceinterface"));
1005 // If an entity handler has been passed, attach it to the newly created
1006 // resource. Otherwise, set the default entity handler.
1009 pointer->entityHandler = entityHandler;
1013 pointer->entityHandler = defaultResourceEHandler;
1017 result = OC_STACK_OK;
1019 #ifdef WITH_PRESENCE
1020 if(presenceResource.handle)
1022 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
1023 SendPresenceNotification(pointer->rsrcType);
1027 if (result != OC_STACK_OK)
1029 // Deep delete of resource and other dynamic elements that it contains
1030 deleteResource(pointer);
1039 * Create a resource. with host ip address for remote resource
1041 * @param handle - pointer to handle to newly created resource. Set by ocstack.
1042 * Used to refer to resource
1043 * @param resourceTypeName - name of resource type. Example: "core.led"
1044 * @param resourceInterfaceName - name of resource interface. Example: "core.rw"
1045 * @param host - HOST address of the remote resource. Example: "coap://xxx.xxx.xxx.xxx:xxxxx"
1046 * @param uri - URI of the resource. Example: "/a/led"
1047 * @param entityHandler - entity handler function that is called by ocstack to handle requests, etc
1048 * NULL for default entity handler
1049 * @param resourceProperties - properties supported by resource.
1050 * Example: OC_DISCOVERABLE|OC_OBSERVABLE
1053 * OC_STACK_OK - no errors
1054 * OC_STACK_ERROR - stack process error
1057 OCStackResult OCCreateResourceWithHost(OCResourceHandle *handle,
1058 const char *resourceTypeName,
1059 const char *resourceInterfaceName,
1062 OCEntityHandler entityHandler,
1063 uint8_t resourceProperties)
1067 OCStackResult result = OC_STACK_ERROR;
1069 result = OCCreateResource(handle, resourceTypeName, resourceInterfaceName,
1070 uri, entityHandler, resourceProperties);
1072 if (result != OC_STACK_ERROR)
1075 size = strlen(host) + 1;
1076 str = (char *) OCMalloc(size);
1079 return OC_STACK_ERROR;
1081 strncpy(str, host, size);
1082 ((OCResource *) *handle)->host = str;
1089 * Add a resource to a collection resource.
1091 * @param collectionHandle - handle to the collection resource
1092 * @param resourceHandle - handle to resource to be added to the collection resource
1095 * OC_STACK_OK - no errors
1096 * OC_STACK_ERROR - stack process error
1097 * OC_STACK_INVALID_PARAM - invalid collectionhandle
1099 OCStackResult OCBindResource(
1100 OCResourceHandle collectionHandle, OCResourceHandle resourceHandle) {
1101 OCResource *resource;
1104 OC_LOG(INFO, TAG, PCF("Entering OCBindResource"));
1106 // Validate parameters
1107 VERIFY_NON_NULL(collectionHandle, ERROR, OC_STACK_ERROR);
1108 VERIFY_NON_NULL(resourceHandle, ERROR, OC_STACK_ERROR);
1109 // Container cannot contain itself
1110 if (collectionHandle == resourceHandle) {
1111 OC_LOG(ERROR, TAG, PCF("Added handle equals collection handle"));
1112 return OC_STACK_INVALID_PARAM;
1115 // Use the handle to find the resource in the resource linked list
1116 resource = findResource((OCResource *) collectionHandle);
1118 OC_LOG(ERROR, TAG, PCF("Collection handle not found"));
1119 return OC_STACK_INVALID_PARAM;
1122 // Look for an open slot to add add the child resource.
1123 // If found, add it and return success
1124 for (i = 0; i < MAX_CONTAINED_RESOURCES; i++) {
1125 if (!resource->rsrcResources[i]) {
1126 resource->rsrcResources[i] = (OCResource *) resourceHandle;
1127 OC_LOG(INFO, TAG, PCF("resource bound"));
1132 #ifdef WITH_PRESENCE
1133 if(presenceResource.handle)
1135 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
1136 SendPresenceNotification(((OCResource *) resourceHandle)->rsrcType);
1140 // Unable to add resourceHandle, so return error
1141 return OC_STACK_ERROR;
1145 * Remove a resource from a collection resource.
1147 * @param collectionHandle - handle to the collection resource
1148 * @param resourceHandle - handle to resource to be added to the collection resource
1151 * OC_STACK_OK - no errors
1152 * OC_STACK_ERROR - stack process error
1153 * OC_STACK_INVALID_PARAM - invalid collectionHandle
1155 OCStackResult OCUnBindResource(
1156 OCResourceHandle collectionHandle, OCResourceHandle resourceHandle) {
1157 OCResource *resource;
1160 OC_LOG(INFO, TAG, PCF("Entering OCUnBindResource"));
1162 // Validate parameters
1163 VERIFY_NON_NULL(collectionHandle, ERROR, OC_STACK_ERROR);
1164 VERIFY_NON_NULL(resourceHandle, ERROR, OC_STACK_ERROR);
1165 // Container cannot contain itself
1166 if (collectionHandle == resourceHandle) {
1167 OC_LOG(ERROR, TAG, PCF("removing handle equals collection handle"));
1168 return OC_STACK_INVALID_PARAM;
1171 // Use the handle to find the resource in the resource linked list
1172 resource = findResource((OCResource *) collectionHandle);
1174 OC_LOG(ERROR, TAG, PCF("Collection handle not found"));
1175 return OC_STACK_INVALID_PARAM;
1178 // Look for an open slot to add add the child resource.
1179 // If found, add it and return success
1180 for (i = 0; i < MAX_CONTAINED_RESOURCES; i++) {
1181 if (resourceHandle == resource->rsrcResources[i]) {
1182 resource->rsrcResources[i] = (OCResource *) NULL;
1183 OC_LOG(INFO, TAG, PCF("resource unbound"));
1188 OC_LOG(INFO, TAG, PCF("resource not found in collection"));
1190 #ifdef WITH_PRESENCE
1191 if(presenceResource.handle)
1193 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
1194 SendPresenceNotification(((OCResource *) resourceHandle)->rsrcType);
1198 // Unable to add resourceHandle, so return error
1199 return OC_STACK_ERROR;
1202 OCStackResult BindResourceTypeToResource(OCResource* resource,
1203 const char *resourceTypeName)
1205 OCResourceType *pointer = NULL;
1208 OCStackResult result = OC_STACK_ERROR;
1210 OC_LOG(INFO, TAG, PCF("Entering BindResourceTypeToResource"));
1212 // Validate parameters
1213 VERIFY_NON_NULL(resourceTypeName, ERROR, OC_STACK_INVALID_PARAM);
1214 // TODO: Does resource attribute resentation really have to be maintained in stack?
1215 // Is it presented during resource discovery?
1217 TODO ("Make sure that the resourcetypename doesn't already exist in the resource");
1219 // Create the resourcetype and insert it into the resource list
1220 pointer = (OCResourceType *) OCMalloc(sizeof(OCResourceType));
1224 memset(pointer, 0, sizeof(OCResourceType));
1226 // Set the resourceTypeName
1227 size = strlen(resourceTypeName) + 1;
1228 str = (char *) OCMalloc(size);
1232 strncpy(str, resourceTypeName, size);
1233 pointer->resourcetypename = str;
1235 insertResourceType(resource, pointer);
1236 result = OC_STACK_OK;
1238 exit: if (result != OC_STACK_OK) {
1246 OCStackResult BindResourceInterfaceToResource(OCResource* resource,
1247 const char *resourceInterfaceName)
1249 OCResourceInterface *pointer = NULL;
1252 OCStackResult result = OC_STACK_ERROR;
1254 OC_LOG(INFO, TAG, PCF("Entering BindResourceInterfaceToResource"));
1256 // Validate parameters
1257 VERIFY_NON_NULL(resourceInterfaceName, ERROR, OC_STACK_INVALID_PARAM);
1259 TODO ("Make sure that the resourceinterface name doesn't already exist in the resource");
1261 // Create the resourceinterface and insert it into the resource list
1262 pointer = (OCResourceInterface *) OCMalloc(sizeof(OCResourceInterface));
1266 memset(pointer, 0, sizeof(OCResourceInterface));
1268 // Set the resourceinterface name
1269 size = strlen(resourceInterfaceName) + 1;
1270 str = (char *) OCMalloc(size);
1274 strncpy(str, resourceInterfaceName, size);
1275 pointer->name = str;
1277 // Bind the resourceinterface to the resource
1278 insertResourceInterface(resource, pointer);
1280 result = OC_STACK_OK;
1282 exit: if (result != OC_STACK_OK) {
1291 * Bind a resourcetype to a resource.
1293 * @param handle - handle to the resource
1294 * @param resourceTypeName - name of resource type. Example: "core.led"
1297 * OC_STACK_OK - no errors
1298 * OC_STACK_ERROR - stack process error
1300 OCStackResult OCBindResourceTypeToResource(OCResourceHandle handle,
1301 const char *resourceTypeName) {
1303 OCStackResult result = OC_STACK_ERROR;
1304 OCResource *resource;
1306 // Make sure resource exists
1307 resource = findResource((OCResource *) handle);
1309 OC_LOG(ERROR, TAG, PCF("Resource not found"));
1310 return OC_STACK_ERROR;
1313 // call internal function
1314 result = BindResourceTypeToResource(resource, resourceTypeName);
1316 #ifdef WITH_PRESENCE
1317 if(presenceResource.handle)
1319 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
1320 SendPresenceNotification(resource->rsrcType);
1328 * Bind a resourceinterface to a resource.
1330 * @param handle - handle to the resource
1331 * @param resourceInterfaceName - name of resource interface. Example: "oc.mi.b"
1334 * OC_STACK_OK - no errors
1335 * OC_STACK_ERROR - stack process error
1338 OCStackResult OCBindResourceInterfaceToResource(OCResourceHandle handle,
1339 const char *resourceInterfaceName) {
1341 OCStackResult result = OC_STACK_ERROR;
1342 OCResource *resource;
1344 // Make sure resource exists
1345 resource = findResource((OCResource *) handle);
1347 OC_LOG(ERROR, TAG, PCF("Resource not found"));
1348 return OC_STACK_ERROR;
1351 // call internal function
1352 result = BindResourceInterfaceToResource(resource, resourceInterfaceName);
1354 #ifdef WITH_PRESENCE
1355 if(presenceResource.handle)
1357 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
1358 SendPresenceNotification(resource->rsrcType);
1366 * Get the number of resources that have been created in the stack.
1368 * @param numResources - pointer to count variable
1371 * OC_STACK_OK - no errors
1372 * OC_STACK_ERROR - stack process error
1375 OCStackResult OCGetNumberOfResources(uint8_t *numResources) {
1376 OCResource *pointer = headResource;
1378 OC_LOG(INFO, TAG, PCF("Entering OCGetNumberOfResources"));
1379 VERIFY_NON_NULL(numResources, ERROR, OC_STACK_INVALID_PARAM);
1382 *numResources = *numResources + 1;
1383 pointer = pointer->next;
1389 * Get a resource handle by index.
1391 * @param index - index of resource, 0 to Count - 1
1394 * Resource handle - if found
1395 * NULL - if not found
1397 OCResourceHandle OCGetResourceHandle(uint8_t index) {
1398 OCResource *pointer = headResource;
1401 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceHandle"));
1403 // Iterate through the list
1404 while ((i < index) && pointer) {
1406 pointer = pointer->next;
1408 return (OCResourceHandle) pointer;
1412 * Delete resource specified by handle. Deletes resource and all resourcetype and resourceinterface
1415 * @param handle - handle of resource to be deleted
1418 * OC_STACK_OK - no errors
1419 * OC_STACK_ERROR - stack process error
1420 * OC_STACK_NO_RESOURCE - resource not found
1421 * OC_STACK_INVALID_PARAM - invalid param
1423 OCStackResult OCDeleteResource(OCResourceHandle handle) {
1424 OC_LOG(INFO, TAG, PCF("Entering OCDeleteResource"));
1427 OC_LOG(ERROR, TAG, PCF("Invalid param"));
1428 return OC_STACK_INVALID_PARAM;
1431 OCResource *resource = findResource((OCResource *) handle);
1432 if (resource == NULL) {
1433 OC_LOG(ERROR, TAG, PCF("Resource not found"));
1434 return OC_STACK_NO_RESOURCE;
1437 if (deleteResource((OCResource *) handle) == 0) {
1438 OC_LOG(ERROR, TAG, PCF("Error deleting resource"));
1439 return OC_STACK_ERROR;
1446 * Get the URI of the resource specified by handle.
1448 * @param handle - handle of resource
1450 * URI string - if resource found
1451 * NULL - resource not found
1453 const char *OCGetResourceUri(OCResourceHandle handle) {
1454 OCResource *resource;
1455 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceUri"));
1457 resource = findResource((OCResource *) handle);
1459 return resource->uri;
1461 return (const char *) NULL;
1465 * Get the properties of the resource specified by handle.
1466 * NOTE: that after a resource is created, the OC_ACTIVE property is set
1467 * for the resource by the stack.
1469 * @param handle - handle of resource
1471 * property bitmap - if resource found
1472 * NULL - resource not found
1474 uint8_t OCGetResourceProperties(OCResourceHandle handle) {
1475 OCResource *resource;
1476 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceProperties"));
1478 resource = findResource((OCResource *) handle);
1480 return resource->resourceProperties;
1486 * Get the number of resource types of the resource.
1488 * @param handle - handle of resource
1489 * @param numResourceTypes - pointer to count variable
1492 * OC_STACK_OK - no errors
1493 * OC_STACK_ERROR - stack process error
1495 OCStackResult OCGetNumberOfResourceTypes(OCResourceHandle handle,
1496 uint8_t *numResourceTypes) {
1497 OCResource *resource;
1498 OCResourceType *pointer;
1500 OC_LOG(INFO, TAG, PCF("Entering OCGetNumberOfResourceTypes"));
1501 VERIFY_NON_NULL(numResourceTypes, ERROR, OC_STACK_INVALID_PARAM);
1502 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
1504 *numResourceTypes = 0;
1506 resource = findResource((OCResource *) handle);
1508 pointer = resource->rsrcType;
1510 *numResourceTypes = *numResourceTypes + 1;
1511 pointer = pointer->next;
1518 * Get name of resource type of the resource.
1520 * @param handle - handle of resource
1521 * @param index - index of resource, 0 to Count - 1
1524 * resource type name - if resource found
1525 * NULL - resource not found
1527 const char *OCGetResourceTypeName(OCResourceHandle handle, uint8_t index) {
1528 OCResourceType *resourceType;
1530 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceTypeName"));
1532 resourceType = findResourceTypeAtIndex(handle, index);
1534 return resourceType->resourcetypename;
1536 return (const char *) NULL;
1542 * Get the number of resource interfaces of the resource.
1544 * @param handle - handle of resource
1545 * @param numResources - pointer to count variable
1548 * OC_STACK_OK - no errors
1549 * OC_STACK_ERROR - stack process error
1551 OCStackResult OCGetNumberOfResourceInterfaces(OCResourceHandle handle,
1552 uint8_t *numResourceInterfaces) {
1553 OCResourceInterface *pointer;
1554 OCResource *resource;
1556 OC_LOG(INFO, TAG, PCF("Entering OCGetNumberOfResourceInterfaces"));
1558 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
1559 VERIFY_NON_NULL(numResourceInterfaces, ERROR, OC_STACK_INVALID_PARAM);
1561 *numResourceInterfaces = 0;
1562 resource = findResource((OCResource *) handle);
1564 pointer = resource->rsrcInterface;
1566 *numResourceInterfaces = *numResourceInterfaces + 1;
1567 pointer = pointer->next;
1574 * Get name of resource interface of the resource.
1576 * @param handle - handle of resource
1577 * @param index - index of resource, 0 to Count - 1
1580 * resource interface name - if resource found
1581 * NULL - resource not found
1583 const char *OCGetResourceInterfaceName(OCResourceHandle handle, uint8_t index) {
1584 OCResourceInterface *resourceInterface;
1586 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceInterfaceName"));
1588 resourceInterface = findResourceInterfaceAtIndex(handle, index);
1589 if (resourceInterface) {
1590 return resourceInterface->name;
1592 return (const char *) NULL;
1596 * Get resource handle from the collection resource by index.
1598 * @param collectionHandle - handle of collection resource
1599 * @param index - index of contained resource, 0 to Count - 1
1602 * handle to resource - if resource found
1603 * NULL - resource not found
1605 OCResourceHandle OCGetResourceHandleFromCollection(OCResourceHandle collectionHandle,
1607 OCResource *resource;
1609 OC_LOG(INFO, TAG, PCF("Entering OCGetContainedResource"));
1611 if (index >= MAX_CONTAINED_RESOURCES) {
1615 resource = findResource((OCResource *) collectionHandle);
1620 return resource->rsrcResources[index];
1624 * Bind an entity handler to the resource.
1626 * @param handle - handle to the resource that the contained resource is to be bound
1627 * @param entityHandler - entity handler function that is called by ocstack to handle requests, etc
1629 * OC_STACK_OK - no errors
1630 * OC_STACK_ERROR - stack process error
1632 OCStackResult OCBindResourceHandler(OCResourceHandle handle,
1633 OCEntityHandler entityHandler) {
1634 OCResource *resource;
1636 OC_LOG(INFO, TAG, PCF("Entering OCBindResourceHandler"));
1638 // Validate parameters
1639 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
1640 //VERIFY_NON_NULL(entityHandler, ERROR, OC_STACK_INVALID_PARAM);
1642 // Use the handle to find the resource in the resource linked list
1643 resource = findResource((OCResource *)handle);
1645 OC_LOG(ERROR, TAG, PCF("Resource not found"));
1646 return OC_STACK_ERROR;
1650 resource->entityHandler = entityHandler;
1652 #ifdef WITH_PRESENCE
1653 if(presenceResource.handle)
1655 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
1656 SendPresenceNotification(resource->rsrcType);
1664 * Get the entity handler for a resource.
1666 * @param handle - handle of resource
1669 * entity handler - if resource found
1670 * NULL - resource not found
1672 OCEntityHandler OCGetResourceHandler(OCResourceHandle handle) {
1673 OCResource *resource;
1675 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceHandler"));
1677 // Use the handle to find the resource in the resource linked list
1678 resource = findResource((OCResource *)handle);
1680 OC_LOG(ERROR, TAG, PCF("Resource not found"));
1685 return resource->entityHandler;
1688 void incrementSequenceNumber(OCResource * resPtr)
1690 // Increment the sequence number
1691 resPtr->sequenceNum += 1;
1692 if (resPtr->sequenceNum == MAX_SEQUENCE_NUMBER)
1694 resPtr->sequenceNum = OC_OFFSET_SEQUENCE_NUMBER+1;
1699 #ifdef WITH_PRESENCE
1701 * Notify Presence subscribers that a resource has been modified
1703 * @param resourceType - Handle to the resourceType linked list of resource
1704 * that was modified.
1705 * @param qos - Quality Of Service
1708 OCStackResult SendPresenceNotification(OCResourceType *resourceType)
1710 OCResource *resPtr = NULL;
1711 OCStackResult result;
1712 OCMethod method = OC_REST_PRESENCE;
1713 uint32_t maxAge = 0;
1714 resPtr = findResource((OCResource *) presenceResource.handle);
1717 return OC_STACK_NO_RESOURCE;
1719 if((((OCResource *) presenceResource.handle)->resourceProperties) & OC_ACTIVE)
1721 maxAge = presenceResource.presenceTTL;
1728 result = SendAllObserverNotification(method, resPtr, maxAge, resourceType, OC_LOW_QOS);
1734 * Notify observers that an observed value has changed.
1736 * @param handle - handle of resource
1739 * OC_STACK_OK - no errors
1740 * OC_STACK_NO_RESOURCE - invalid resource handle
1741 * OC_STACK_NO_OBSERVERS - no more observers intrested in resource
1743 OCStackResult OCNotifyAllObservers(OCResourceHandle handle, OCQualityOfService qos) {
1745 OC_LOG(INFO, TAG, PCF("Entering OCNotifyAllObservers"));
1747 OCResource *resPtr = NULL;
1748 OCStackResult result;
1749 OCMethod method = OC_REST_NOMETHOD;
1750 uint32_t maxAge = 0;
1752 OC_LOG(INFO, TAG, PCF("Entering OCNotifyAllObservers"));
1753 #ifdef WITH_PRESENCE
1754 if(handle == presenceResource.handle)
1758 #endif // WITH_PRESENCE
1759 VERIFY_NON_NULL(handle, ERROR, OC_STACK_ERROR);
1761 // Verify that the resource exists
1762 resPtr = findResource ((OCResource *) handle);
1765 return OC_STACK_NO_RESOURCE;
1769 //only increment in the case of regular observing (not presence)
1770 incrementSequenceNumber(resPtr);
1771 method = OC_REST_OBSERVE;
1772 maxAge = MAX_OBSERVE_AGE;
1773 #ifdef WITH_PRESENCE
1774 result = SendAllObserverNotification (method, resPtr, maxAge, NULL, qos);
1776 result = SendAllObserverNotification (method, resPtr, maxAge, qos);
1783 OCNotifyListOfObservers (OCResourceHandle handle,
1784 OCObservationId *obsIdList,
1785 uint8_t numberOfIds,
1786 unsigned char *notificationJSONPayload,
1787 OCQualityOfService qos)
1789 OC_LOG(INFO, TAG, PCF("Entering OCNotifyListOfObservers"));
1791 OCResource *resPtr = NULL;
1792 //TODO: we should allow the server to define this
1793 uint32_t maxAge = MAX_OBSERVE_AGE;
1795 VERIFY_NON_NULL(handle, ERROR, OC_STACK_ERROR);
1796 VERIFY_NON_NULL(obsIdList, ERROR, OC_STACK_ERROR);
1797 VERIFY_NON_NULL(notificationJSONPayload, ERROR, OC_STACK_ERROR);
1799 // Verify that the resource exists
1800 resPtr = findResource ((OCResource *) handle);
1801 if (NULL == resPtr || myStackMode == OC_CLIENT)
1803 return OC_STACK_NO_RESOURCE;
1807 incrementSequenceNumber(resPtr);
1809 return (SendListObserverNotification(resPtr, obsIdList, numberOfIds,
1810 notificationJSONPayload, maxAge, qos));
1814 * Send a response to a request.
1815 * The response can be a regular, slow, or block (i.e. a response that
1816 * is too large to be sent in a single PDU and must span multiple transmissions)
1818 * @param response - pointer to structure that contains response parameters
1821 * OC_STACK_OK - No errors; Success
1822 * OC_STACK_INVALID_PARAM - Invalid pointer to OCServerResponse
1823 * OC_STACK_INVALID_REQUEST_HANDLE - Request handle not found
1824 * OC_STACK_PERSISTENT_BUFFER_REQUIRED - Block transfer needed for response, so a
1825 * persistent response buffer is necessary
1827 OCStackResult OCDoResponse(OCEntityHandlerResponse *ehResponse)
1829 OCStackResult result = OC_STACK_ERROR;
1830 OCServerRequest *serverRequest = NULL;
1832 OC_LOG(INFO, TAG, PCF("Entering OCDoResponse"));
1834 // Validate input parameters
1835 VERIFY_NON_NULL(ehResponse, ERROR, OC_STACK_INVALID_PARAM);
1836 VERIFY_NON_NULL(ehResponse->requestHandle, ERROR, OC_STACK_INVALID_PARAM);
1838 // TODO: Placeholder for creating a response entry when implementing
1839 // block transfer feature
1841 // If a response payload is present, check if block transfer is required
1842 if (ehResponse->payload && OCIsPacketTransferRequired(NULL,
1843 (const char *)ehResponse->payload, ehResponse->payloadSize))
1845 OC_LOG(INFO, TAG, PCF("Block transfer required"));
1847 // Persistent response buffer is needed for block transfer
1848 if (!ehResponse->persistentBufferFlag)
1850 OC_LOG(WARNING, TAG, PCF("Persistent response buffer required"));
1851 return OC_STACK_PERSISTENT_BUFFER_REQUIRED;
1853 // TODO: Placeholder for block transfer handling
1854 // TODO: Placeholder for setting the the response handle in the OCServerResponse struct
1855 // when implementing the block transfer feature
1861 // Get pointer to request info
1862 serverRequest = GetServerRequestUsingHandle((OCServerRequest *)ehResponse->requestHandle);
1865 result = serverRequest->ehResponseHandler(ehResponse);
1872 * Cancel a response. Applies to a block response
1874 * @param responseHandle - response handle set by stack in OCServerResponse after
1875 * OCDoResponse is called
1878 * OC_STACK_OK - No errors; Success
1879 * OC_STACK_INVALID_PARAM - The handle provided is invalid.
1881 OCStackResult OCCancelResponse(OCResponseHandle responseHandle)
1883 OCStackResult result = OC_STACK_NOTIMPL;
1885 OC_LOG(INFO, TAG, PCF("Entering OCCancelResponse"));
1887 // TODO: validate response handle
1892 //-----------------------------------------------------------------------------
1893 // Private internal function definitions
1894 //-----------------------------------------------------------------------------
1896 * Generate handle of OCDoResource invocation for callback management.
1898 static OCDoHandle GenerateInvocationHandle()
1900 OCDoHandle handle = NULL;
1901 // Generate token here, it will be deleted when the transaction is deleted
1902 handle = (OCDoHandle) OCMalloc(sizeof(uint8_t[MAX_TOKEN_LENGTH]));
1905 OCFillRandomMem((uint8_t*)handle, sizeof(uint8_t[MAX_TOKEN_LENGTH]));
1910 #ifdef WITH_PRESENCE
1911 OCStackResult OCChangeResourceProperty(OCResourceProperty * inputProperty,
1912 OCResourceProperty resourceProperties, uint8_t enable)
1914 if (resourceProperties
1915 > (OC_ACTIVE | OC_DISCOVERABLE | OC_OBSERVABLE | OC_SLOW)) {
1916 OC_LOG(ERROR, TAG, PCF("Invalid property"));
1917 return OC_STACK_INVALID_PARAM;
1921 *inputProperty = (OCResourceProperty) (*inputProperty & ~(resourceProperties));
1925 *inputProperty = (OCResourceProperty) (*inputProperty | resourceProperties);
1932 * Initialize resource data structures, variables, etc.
1934 OCStackResult initResources() {
1935 OCStackResult result = OC_STACK_OK;
1936 // Init application resource vars
1937 headResource = NULL;
1938 // Init Virtual Resources
1939 #ifdef WITH_PRESENCE
1940 presenceResource.presenceTTL = OC_DEFAULT_PRESENCE_TTL;
1941 //presenceResource.token = OCGenerateCoAPToken();
1942 result = OCCreateResource(&presenceResource.handle,
1943 OC_RSRVD_RESOURCE_TYPE_PRESENCE,
1948 //make resource inactive
1949 result = OCChangeResourceProperty(
1950 &(((OCResource *) presenceResource.handle)->resourceProperties),
1957 * Add a resource to the end of the linked list of resources.
1959 * @param resource - resource to be added
1961 void insertResource(OCResource *resource) {
1962 OCResource *pointer;
1964 if (!headResource) {
1965 headResource = resource;
1967 pointer = headResource;
1969 while (pointer->next) {
1970 pointer = pointer->next;
1972 pointer->next = resource;
1974 resource->next = NULL;
1978 * Find a resource in the linked list of resources.
1980 * @param resource - resource to be found
1982 * NULL - resource not found
1983 * pointer to resource - pointer to resource that was found in the linked list
1985 OCResource *findResource(OCResource *resource) {
1986 OCResource *pointer = headResource;
1989 if (pointer == resource) {
1992 pointer = pointer->next;
1997 void deleteAllResources()
1999 OCResource *pointer = headResource;
2004 temp = pointer->next;
2005 #ifdef WITH_PRESENCE
2006 if(pointer != (OCResource *) presenceResource.handle)
2008 #endif // WITH_PRESENCE
2009 deleteResource(pointer);
2010 #ifdef WITH_PRESENCE
2012 #endif // WITH_PRESENCE
2016 #ifdef WITH_PRESENCE
2017 // Ensure that the last resource to be deleted is the presence resource. This allows for all
2018 // presence notification attributed to their deletion to be processed.
2019 deleteResource((OCResource *) presenceResource.handle);
2020 #endif // WITH_PRESENCE
2024 * Delete the resource from the linked list.
2026 * @param resource - resource to be deleted
2031 int deleteResource(OCResource *resource) {
2032 OCResource *prev = NULL;
2035 temp = headResource;
2037 if (temp == resource) {
2038 // Invalidate all Resource Properties.
2039 resource->resourceProperties = (OCResourceProperty) 0;
2040 #ifdef WITH_PRESENCE
2041 if(resource != (OCResource *) presenceResource.handle)
2043 #endif // WITH_PRESENCE
2044 OCNotifyAllObservers((OCResourceHandle)resource, OC_HIGH_QOS);
2045 #ifdef WITH_PRESENCE
2048 if(presenceResource.handle)
2050 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2051 if(resource != (OCResource *) presenceResource.handle)
2053 SendPresenceNotification(resource->rsrcType);
2057 SendPresenceNotification(NULL);
2062 if (temp == headResource) {
2063 headResource = temp->next;
2065 prev->next = temp->next;
2068 deleteResourceElements(temp);
2081 * Delete all of the dynamically allocated elements that were created for the resource.
2083 * @param resource - specified resource
2085 void deleteResourceElements(OCResource *resource) {
2091 OCFree(resource->uri);
2093 // Delete resourcetype linked list
2094 deleteResourceType(resource->rsrcType);
2096 // Delete resourceinterface linked list
2097 deleteResourceInterface(resource->rsrcInterface);
2101 * Delete all of the dynamically allocated elements that were created for the resource type.
2103 * @param resourceType - specified resource type
2105 void deleteResourceType(OCResourceType *resourceType) {
2106 OCResourceType *pointer = resourceType;
2107 OCResourceType *next;
2110 next = pointer->next;
2111 OCFree(pointer->resourcetypename);
2118 * Delete all of the dynamically allocated elements that were created for the resource interface.
2120 * @param resourceInterface - specified resource interface
2122 void deleteResourceInterface(OCResourceInterface *resourceInterface) {
2123 OCResourceInterface *pointer = resourceInterface;
2124 OCResourceInterface *next;
2127 next = pointer->next;
2128 OCFree(pointer->name);
2135 * Insert a resource type into a resource's resource type linked list.
2137 * @param resource - resource where resource type is to be inserted
2138 * @param resourceType - resource type to be inserted
2140 void insertResourceType(OCResource *resource, OCResourceType *resourceType) {
2141 OCResourceType *pointer;
2143 if (resource && !resource->rsrcType) {
2144 resource->rsrcType = resourceType;
2148 pointer = resource->rsrcType;
2152 pointer = resourceType;
2154 while (pointer->next) {
2155 pointer = pointer->next;
2157 pointer->next = resourceType;
2159 resourceType->next = NULL;
2163 * Get a resource type at the specified index within a resource.
2165 * @param handle - handle of resource
2166 * @param index - index of resource type
2169 * resourcetype - if found
2172 OCResourceType *findResourceTypeAtIndex(OCResourceHandle handle, uint8_t index) {
2173 OCResource *resource;
2174 OCResourceType *pointer;
2177 // Find the specified resource
2178 resource = findResource((OCResource *) handle);
2183 // Make sure a resource has a resourcetype
2184 if (!resource->rsrcType) {
2188 // Iterate through the list
2189 pointer = resource->rsrcType;
2191 while ((i < index) && pointer) {
2193 pointer = pointer->next;
2199 * Finds a resource type in an OCResourceType link-list.
2201 * @param resourceTypeList - the link-list to be searched through
2202 * @param resourceTypeName - the key to search for
2205 * resourceType that matches the key (ie. resourceTypeName)
2206 * NULL - either an invalid parameter or this function was unable to find the key.
2208 OCResourceType *findResourceType(OCResourceType * resourceTypeList, const char * resourceTypeName)
2210 if(resourceTypeList && resourceTypeName)
2212 OCResourceType * rtPointer = resourceTypeList;
2213 while(resourceTypeName && rtPointer)
2215 if(rtPointer->resourcetypename &&
2216 strcmp(resourceTypeName, (const char *)
2217 (rtPointer->resourcetypename)) == 0)
2221 rtPointer = rtPointer->next;
2228 * Insert a resource interface into a resource's resource interface linked list.
2230 * @param resource - resource where resource interface is to be inserted
2231 * @param resourceInterface - resource interface to be inserted
2233 void insertResourceInterface(OCResource *resource,
2234 OCResourceInterface *resourceInterface) {
2235 OCResourceInterface *pointer;
2237 if (!resource->rsrcInterface) {
2238 resource->rsrcInterface = resourceInterface;
2240 pointer = resource->rsrcInterface;
2241 while (pointer->next) {
2242 pointer = pointer->next;
2244 pointer->next = resourceInterface;
2246 resourceInterface->next = NULL;
2250 * Get a resource interface at the specified index within a resource.
2252 * @param handle - handle of resource
2253 * @param index - index of resource interface
2256 * resourceinterface - if found
2259 OCResourceInterface *findResourceInterfaceAtIndex(OCResourceHandle handle,
2261 OCResource *resource;
2262 OCResourceInterface *pointer;
2265 // Find the specified resource
2266 resource = findResource((OCResource *) handle);
2271 // Make sure a resource has a resourceinterface
2272 if (!resource->rsrcInterface) {
2276 // Iterate through the list
2277 pointer = resource->rsrcInterface;
2279 while ((i < index) && pointer) {
2281 pointer = pointer->next;
2287 * Determine if a request/response must be sent in a block transfer because it is too large to be
2288 * sent in a single PDU. This function can be used for either a request or a response
2290 * @param request - NULL or pointer to request
2291 * @param response - NULL or pointer to response
2292 * @param size - 0 or size of the request/response. If 0, strlen is used for determining
2293 * the length of the request/response
2296 * 0 - packet transfer NOT required (i.e. normal request/response)
2297 * 1 - packet transfer required (i.e. block transfer needed)
2299 uint8_t OCIsPacketTransferRequired(const char *request, const char *response, uint16_t size)
2303 // Determine if we are checking a request or a response
2306 // If size is greater than 0, use it for the request size value, otherwise
2307 // assume request is null terminated and use strlen for size value
2308 if ((size > MAX_REQUEST_LENGTH) || (strlen(request) > MAX_REQUEST_LENGTH))
2315 // If size is greater than 0, use it for the response size value, otherwise
2316 // assume response is null terminated and use strlen for size value
2317 if ((size > MAX_RESPONSE_LENGTH) || (strlen(response) > MAX_RESPONSE_LENGTH))
2326 * Retrieves a resource type based upon a uri string if the uri string contains only just one
2327 * resource attribute (and that has to be of type "rt").
2329 * @remark This API malloc's memory for the resource type and newURI. Do not malloc resourceType
2330 * or newURI before passing in.
2332 * @param uri - Valid URI for "requiredUri" parameter to OCDoResource API.
2333 * @param resourceType - The resource type to be populated; pass by reference.
2334 * @param newURI - Return URI without resourceType appended to the end of it. This is used to
2335 * ensure that the uri parameter is not modified; pass by reference.
2338 * OC_STACK_INVALID_URI - Returns this if the URI is invalid/NULL.
2339 * OC_STACK_INVALID_PARAM - Returns this if the resourceType parameter is invalid/NULL.
2340 * OC_STACK_OK - Success
2342 OCStackResult getResourceType(const char * uri, unsigned char** resourceType, char ** newURI)
2346 return OC_STACK_INVALID_URI;
2348 if(!resourceType || !newURI)
2350 return OC_STACK_INVALID_PARAM;
2352 char * leftToken = NULL;
2353 char * tempURI = (char *) OCMalloc(strlen(uri) + 1);
2358 strcpy(tempURI, uri);
2359 leftToken = strtok((char *)tempURI, "?");
2361 while(leftToken != NULL)
2363 if(strncmp(leftToken, "rt=", 3) == 0)
2365 *resourceType = (unsigned char *) OCMalloc(strlen(leftToken)-3);
2370 strcpy((char *)*resourceType, ((const char *)&leftToken[3]));
2373 leftToken = strtok(NULL, "?");
2381 return OC_STACK_NO_MEMORY;