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 //-----------------------------------------------------------------------------
25 #define _POSIX_C_SOURCE 200112L
29 #include "ocstackinternal.h"
30 #include "ocresourcehandler.h"
31 #include "occlientcb.h"
32 #include "ocobserve.h"
37 #include "ocserverrequest.h"
40 #include "cainterface.h"
41 #include <arpa/inet.h>
44 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
48 OC_STACK_UNINITIALIZED = 0, OC_STACK_INITIALIZED, OC_STACK_UNINIT_IN_PROGRESS
52 OC_PRESENCE_UNINITIALIZED = 0, OC_PRESENCE_INITIALIZED
56 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
59 static OCStackState stackState = OC_STACK_UNINITIALIZED;
61 OCResource *headResource = NULL;
63 static OCPresenceState presenceState = OC_PRESENCE_UNINITIALIZED;
64 static PresenceResource presenceResource;
65 uint8_t PresenceTimeOutSize = 0;
66 uint32_t PresenceTimeOut[] = {50, 75, 85, 95, 100};
70 OCDeviceEntityHandler defaultDeviceHandler;
71 OCStackResult getQueryFromUri(const char * uri, unsigned char** resourceType, char ** newURI);
73 //-----------------------------------------------------------------------------
75 //-----------------------------------------------------------------------------
76 #define TAG PCF("OCStack")
77 #define VERIFY_SUCCESS(op, successCode) { if (op != successCode) \
78 {OC_LOG_V(FATAL, TAG, "%s failed!!", #op); goto exit;} }
79 #define VERIFY_NON_NULL(arg, logLevel, retVal) { if (!(arg)) { OC_LOG((logLevel), \
80 TAG, PCF(#arg " is NULL")); return (retVal); } }
81 #define VERIFY_NON_NULL_V(arg) { if (!arg) {OC_LOG_V(FATAL, TAG, "%s is NULL", #arg);\
84 //TODO: we should allow the server to define this
85 #define MAX_OBSERVE_AGE (0x2FFFFUL)
87 //-----------------------------------------------------------------------------
88 // Internal API function
89 //-----------------------------------------------------------------------------
91 // This internal function is called to update the stack with the status of
92 // observers and communication failures
93 OCStackResult OCStackFeedBack(CAToken_t * token, uint8_t status)
95 OCStackResult result = OC_STACK_ERROR;
96 ResourceObserver * observer = NULL;
97 OCEntityHandlerRequest ehRequest = {0};
101 case OC_OBSERVER_NOT_INTERESTED:
102 OC_LOG(DEBUG, TAG, PCF("observer is not interested in our notifications anymore"));
103 observer = GetObserverUsingToken (token);
106 result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) NULL,
107 OC_REST_NOMETHOD, (OCResourceHandle) NULL, NULL, NULL, 0,
108 NULL, OC_OBSERVE_DEREGISTER, observer->observeId);
109 if(result != OC_STACK_OK)
113 observer->resource->entityHandler(OC_OBSERVE_FLAG, &ehRequest);
115 //observer is not observing anymore
116 result = DeleteObserverUsingToken (token);
117 if(result == OC_STACK_OK)
119 OC_LOG(DEBUG, TAG, PCF("Removed observer successfully"));
123 result = OC_STACK_OK;
124 OC_LOG(DEBUG, TAG, PCF("Observer Removal failed"));
127 case OC_OBSERVER_STILL_INTERESTED:
128 //observer is still interested
129 OC_LOG(DEBUG, TAG, PCF("observer is interested in our \
130 notifications, reset the failedCount"));
131 observer = GetObserverUsingToken (token);
134 observer->forceHighQos = 0;
135 observer->failedCommCount = 0;
136 result = OC_STACK_OK;
140 result = OC_STACK_OBSERVER_NOT_FOUND;
143 case OC_OBSERVER_FAILED_COMM:
144 //observer is not reachable
145 OC_LOG(DEBUG, TAG, PCF("observer is unreachable"));
146 observer = GetObserverUsingToken (token);
149 if(observer->failedCommCount >= MAX_OBSERVER_FAILED_COMM)
151 result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) NULL,
152 OC_REST_NOMETHOD, (OCResourceHandle) NULL, NULL, NULL, 0,
153 NULL, OC_OBSERVE_DEREGISTER, observer->observeId);
154 if(result != OC_STACK_OK)
156 return OC_STACK_ERROR;
158 observer->resource->entityHandler(OC_OBSERVE_FLAG, &ehRequest);
159 //observer is unreachable
160 result = DeleteObserverUsingToken (token);
161 if(result == OC_STACK_OK)
163 OC_LOG(DEBUG, TAG, PCF("Removed observer successfully"));
167 result = OC_STACK_OK;
168 OC_LOG(DEBUG, TAG, PCF("Observer Removal failed"));
173 observer->failedCommCount++;
174 result = OC_STACK_CONTINUE;
176 observer->forceHighQos = 1;
177 OC_LOG_V(DEBUG, TAG, "Failed count for this observer is %d",observer->failedCommCount);
181 OC_LOG(ERROR, TAG, PCF("Unknown status"));
182 result = OC_STACK_ERROR;
188 OCStackResult CAToOCStackResult(CAResponseResult_t caCode)
190 OCStackResult ret = OC_STACK_ERROR;
198 ret = OC_STACK_RESOURCE_CREATED;
201 ret = OC_STACK_RESOURCE_DELETED;
204 ret = OC_STACK_INVALID_QUERY;
207 ret = OC_STACK_INVALID_OPTION;
210 ret = OC_STACK_NO_RESOURCE;
218 OCStackResult OCToCAConnectivityType(OCConnectivityType ocConType, CAConnectivityType_t* caConType)
220 OCStackResult ret = OC_STACK_OK;
225 *caConType = CA_ETHERNET;
228 *caConType = CA_WIFI;
237 // Currently OC_ALL represents WIFI and ETHERNET
238 // Add other connectivity types as they are enabled in future
239 *caConType = (CA_WIFI|CA_ETHERNET);
242 ret = OC_STACK_INVALID_PARAM;
248 OCStackResult CAToOCConnectivityType(CAConnectivityType_t caConType, OCConnectivityType *ocConType)
250 OCStackResult ret = OC_STACK_OK;
255 *ocConType = OC_ETHERNET;
258 *ocConType = OC_WIFI;
267 ret = OC_STACK_INVALID_PARAM;
273 // update response.addr appropriately from endPoint.addressInfo
274 OCStackResult UpdateResponseAddr(OCClientResponse *response, const CARemoteEndpoint_t* endPoint)
276 struct sockaddr_in sa;
277 OCStackResult ret = OC_STACK_INVALID_PARAM;
281 OC_LOG(ERROR, TAG, PCF("CA Remote end-point is NULL!"));
284 inet_pton(AF_INET, endPoint->addressInfo.IP.ipAddress, &(sa.sin_addr));
285 sa.sin_port = htons(endPoint->addressInfo.IP.port);
286 static OCDevAddr address;
287 memcpy((void*)&address.addr, &(sa), sizeof(sa));
290 response->addr = &address;
291 ret = CAToOCConnectivityType(endPoint->connectivityType, &(response->connType));
295 OC_LOG(ERROR, TAG, PCF("OCClientResponse is NULL!"));
300 void parsePresencePayload(char* payload, uint32_t* seqNum, uint32_t* maxAge, char** resType)
304 // The format of the payload is {"oc":[%u:%u:%s]}
305 // %u : sequence number,
307 // %s : Resource Type (Optional)
308 tok = strtok(payload, "[:]}");
309 payload[strlen(payload)] = ':';
310 tok = strtok(NULL, "[:]}");
311 payload[strlen((char *)payload)] = ':';
312 *seqNum = (uint32_t) atoi(tok);
313 tok = strtok(NULL, "[:]}");
314 *maxAge = (uint32_t) atoi(tok);
315 tok = strtok(NULL, "[:]}");
319 *resType = (char *)OCMalloc(strlen(tok));
324 payload[strlen((char *)payload)] = ':';
325 strcpy(*resType, tok);
326 OC_LOG_V(DEBUG, TAG, "----------------resourceTypeName %s", *resType);
328 payload[strlen((char *)payload)] = ']';
331 OCStackResult HandlePresenceResponse(const CARemoteEndpoint_t* endPoint,
332 const CAResponseInfo_t* responseInfo)
334 OCStackApplicationResult cbResult = OC_STACK_DELETE_TRANSACTION;
335 ClientCB * cbNode = NULL;
336 char *resourceTypeName = NULL;
337 OCClientResponse response;
338 OCStackResult result = OC_STACK_ERROR;
339 uint32_t lowerBound = 0;
340 uint32_t higherBound = 0;
343 char *fullUri = NULL;
344 char *ipAddress = NULL;
345 int presenceSubscribe = 0;
346 int multicastPresenceSubscribe = 0;
348 if (responseInfo->result != CA_SUCCESS)
350 OC_LOG_V(ERROR, TAG, "HandlePresenceResponse failed %d", responseInfo->result);
351 return OC_STACK_ERROR;
354 fullUri = (char *) OCMalloc(MAX_URI_LENGTH );
358 OC_LOG(ERROR, TAG, PCF("Memory could not be allocated for fullUri"));
359 result = OC_STACK_NO_MEMORY;
363 ipAddress = (char *) OCMalloc(strlen(endPoint->addressInfo.IP.ipAddress) + 1);
365 if(NULL == ipAddress)
367 OC_LOG(ERROR, TAG, PCF("Memory could not be allocated for ipAddress"));
368 result = OC_STACK_NO_MEMORY;
372 strncpy(ipAddress, endPoint->addressInfo.IP.ipAddress,
373 strlen(endPoint->addressInfo.IP.ipAddress));
374 ipAddress[strlen(endPoint->addressInfo.IP.ipAddress)] = '\0';
376 snprintf(fullUri, MAX_URI_LENGTH, "coap://%s:%u%s", ipAddress, endPoint->addressInfo.IP.port,
379 cbNode = GetClientCB(NULL, NULL, (unsigned char *)fullUri);
383 presenceSubscribe = 1;
387 snprintf(fullUri, MAX_URI_LENGTH, "%s%s", OC_MULTICAST_IP, endPoint->resourceUri);
388 cbNode = GetClientCB(NULL, NULL, (unsigned char *)fullUri);
391 multicastPresenceSubscribe = 1;
395 if(!presenceSubscribe && !multicastPresenceSubscribe)
397 OC_LOG(INFO, TAG, PCF("Received a presence notification, but I do not have callback \
398 ------------ ignoring"));
402 // No payload to the application in case of presence
403 response.resJSONPayload = NULL;
404 response.result = OC_STACK_OK;
406 UpdateResponseAddr(&response, endPoint);
408 if(responseInfo->info.payload)
410 parsePresencePayload(responseInfo->info.payload,
411 &(response.sequenceNumber),
418 OC_LOG(INFO, TAG, PCF("===============Stopping presence"));
419 response.result = OC_STACK_PRESENCE_STOPPED;
422 OCFree(cbNode->presence->timeOut);
423 OCFree(cbNode->presence);
424 cbNode->presence = NULL;
427 else if(presenceSubscribe)
429 if(!cbNode->presence)
431 cbNode->presence = (OCPresence *) OCMalloc(sizeof(OCPresence));
432 VERIFY_NON_NULL_V(cbNode->presence);
433 cbNode->presence->timeOut = NULL;
434 cbNode->presence->timeOut = (uint32_t *)
435 OCMalloc(PresenceTimeOutSize * sizeof(uint32_t));
436 if(!(cbNode->presence->timeOut)){
437 OCFree(cbNode->presence);
438 result = OC_STACK_NO_MEMORY;
442 OC_LOG_V(INFO, TAG, "===============Update presence TTL, now time is %u", GetTime(0));
443 cbNode->presence->TTL = maxAge;
444 for(int index = 0; index < PresenceTimeOutSize; index++)
446 lowerBound = GetTime(((float)(PresenceTimeOut[index])
447 /(float)100)*(float)cbNode->presence->TTL);
448 higherBound = GetTime(((float)(PresenceTimeOut[index + 1])
449 /(float)100)*(float)cbNode->presence->TTL);
450 cbNode->presence->timeOut[index] = OCGetRandomRange(lowerBound, higherBound);
451 OC_LOG_V(DEBUG, TAG, "----------------lowerBound timeout %d", lowerBound);
452 OC_LOG_V(DEBUG, TAG, "----------------higherBound timeout %d", higherBound);
453 OC_LOG_V(DEBUG, TAG, "----------------timeOut entry %d",
454 cbNode->presence->timeOut[index]);
456 cbNode->presence->TTLlevel = 0;
457 OC_LOG_V(DEBUG, TAG, "----------------this TTL level %d", cbNode->presence->TTLlevel);
458 if(cbNode->sequenceNumber == response.sequenceNumber)
460 OC_LOG(INFO, TAG, PCF("===============No presence change"));
463 OC_LOG(INFO, TAG, PCF("===============Presence changed, calling up the stack"));
464 cbNode->sequenceNumber = response.sequenceNumber;
466 // Ensure that a filter is actually applied.
467 if(resourceTypeName && cbNode->filterResourceType)
469 if(!findResourceType(cbNode->filterResourceType, resourceTypeName))
477 // This is the multicast case
479 OCMulticastNode* mcNode = NULL;
480 mcNode = GetMCPresenceNode((const unsigned char *)fullUri);
484 if(mcNode->nonce == response.sequenceNumber)
486 OC_LOG(INFO, TAG, PCF("===============No presence change (Multicast)"));
489 mcNode->nonce = response.sequenceNumber;
493 uint32_t uriLen = strlen((char*)fullUri);
494 unsigned char* uri = (unsigned char *) OCMalloc(uriLen + 1);
497 memcpy(uri, fullUri, (uriLen + 1));
502 PCF("===============No Memory for URI to store in the presence node"));
503 result = OC_STACK_NO_MEMORY;
506 result = AddMCPresenceNode(&mcNode, (unsigned char*) uri, response.sequenceNumber);
507 if(result == OC_STACK_NO_MEMORY)
510 PCF("===============No Memory for Multicast Presence Node"));
511 result = OC_STACK_NO_MEMORY;
516 // Ensure that a filter is actually applied.
517 if(resourceTypeName && cbNode->filterResourceType)
519 if(!findResourceType(cbNode->filterResourceType, resourceTypeName))
526 cbResult = cbNode->callBack(cbNode->context, cbNode->handle, &response);
528 if (cbResult == OC_STACK_DELETE_TRANSACTION)
530 FindAndDeleteClientCB(cbNode);
536 OCFree(resourceTypeName);
541 //This function will be called back by CA layer when a response is received
542 void HandleCAResponses(const CARemoteEndpoint_t* endPoint, const CAResponseInfo_t* responseInfo)
544 OC_LOG(INFO, TAG, PCF("Enter HandleCAResponses"));
548 OC_LOG(ERROR, TAG, PCF("endPoint is NULL"));
552 if(NULL == responseInfo)
554 OC_LOG(ERROR, TAG, PCF("responseInfo is NULL"));
558 if(strcmp(endPoint->resourceUri, OC_PRESENCE_URI) == 0)
560 HandlePresenceResponse(endPoint, responseInfo);
564 ClientCB *cbNode = GetClientCB(&(responseInfo->info.token), NULL, NULL);
568 OC_LOG(INFO, TAG, PCF("Calling into application address space"));
569 OCClientResponse response;
571 OCStackResult result = UpdateResponseAddr(&response, endPoint);
572 if(result != OC_STACK_OK)
574 OC_LOG(ERROR, TAG, PCF("Invalid connectivity type in endpoint"));
578 response.result = CAToOCStackResult(responseInfo->result);
579 response.resJSONPayload = (unsigned char*)responseInfo->info.payload;
580 response.numRcvdVendorSpecificHeaderOptions = 0;
581 if(responseInfo->info.numOptions > 0)
584 //First option always with option ID is COAP_OPTION_OBSERVE if it is available.
585 if(responseInfo->info.options[0].optionID == COAP_OPTION_OBSERVE)
587 memcpy (&(response.sequenceNumber),
588 &(responseInfo->info.options[0].optionData), sizeof(uint32_t));
589 response.numRcvdVendorSpecificHeaderOptions = responseInfo->info.numOptions - 1;
594 response.numRcvdVendorSpecificHeaderOptions = responseInfo->info.numOptions;
597 if(response.numRcvdVendorSpecificHeaderOptions > MAX_HEADER_OPTIONS)
599 OC_LOG(ERROR, TAG, PCF("#header options are more than MAX_HEADER_OPTIONS"));
603 for (uint8_t i = start; i < responseInfo->info.numOptions; i++)
605 memcpy (&(response.rcvdVendorSpecificHeaderOptions[i-start]),
606 &(responseInfo->info.options[i]), sizeof(OCHeaderOption));
609 if (cbNode->callBack(cbNode->context,
610 cbNode->handle, &response) == OC_STACK_DELETE_TRANSACTION)
612 FindAndDeleteClientCB(cbNode);
615 OC_LOG_V(INFO, TAG, PCF("Received payload: %s\n"), (char*)responseInfo->info.payload);
616 OC_LOG(INFO, TAG, PCF("Exit HandleCAResponses"));
619 //This function will be called back by CA layer when a request is received
620 void HandleCARequests(const CARemoteEndpoint_t* endPoint, const CARequestInfo_t* requestInfo)
622 OC_LOG(INFO, TAG, PCF("Enter HandleCARequests"));
625 OC_LOG(ERROR, TAG, PCF("endPoint is NULL"));
631 OC_LOG(ERROR, TAG, PCF("requestInfo is NULL"));
635 if(myStackMode == OC_CLIENT)
637 //TODO: should the client be responding to requests?
641 OCServerProtocolRequest serverRequest;
643 memset (&serverRequest, 0, sizeof(OCServerProtocolRequest));
644 OC_LOG_V(INFO, TAG, PCF("***** Endpoint URI ***** : %s\n"), (char*)endPoint->resourceUri);
646 char * newUri = (char *)endPoint->resourceUri;
647 unsigned char * query = NULL;
648 getQueryFromUri(endPoint->resourceUri, &query, &newUri);
649 OC_LOG_V(INFO, TAG, PCF("**********URI without query ****: %s\n"), newUri);
650 OC_LOG_V(INFO, TAG, PCF("**********Query ****: %s\n"), query);
652 memcpy (&(serverRequest.resourceUrl), newUri, strlen(newUri));
656 memcpy (&(serverRequest.query), query, strlen((char*)query));
658 //copy request payload
659 if (requestInfo->info.payload)
661 serverRequest.reqTotalSize = strlen(requestInfo->info.payload) + 1;
662 memcpy (&(serverRequest.reqJSONPayload), requestInfo->info.payload,
663 strlen(requestInfo->info.payload));
664 serverRequest.reqTotalSize = strlen((const char *)requestInfo->info.payload) + 1;
668 serverRequest.reqTotalSize = 1;
671 switch (requestInfo->method)
675 serverRequest.method = OC_REST_GET;
680 serverRequest.method = OC_REST_PUT;
685 serverRequest.method = OC_REST_POST;
690 serverRequest.method = OC_REST_DELETE;
695 OC_LOG(ERROR, TAG, PCF("Received CA method %d not supported"));
700 OC_LOG_V(INFO, TAG, "HandleCARequests: CA token length = %d", CA_MAX_TOKEN_LEN);
701 OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)requestInfo->info.token, CA_MAX_TOKEN_LEN);
703 serverRequest.requestToken = (CAToken_t)OCMalloc(CA_MAX_TOKEN_LEN+1);
705 if (!serverRequest.requestToken)
707 OC_LOG(FATAL, TAG, "Server Request Token is NULL");
710 memset(serverRequest.requestToken, 0, CA_MAX_TOKEN_LEN + 1);
711 memcpy(serverRequest.requestToken, requestInfo->info.token, CA_MAX_TOKEN_LEN);
713 if (requestInfo->info.type == CA_MSG_CONFIRM)
715 serverRequest.qos = OC_HIGH_QOS;
717 else if (requestInfo->info.type == CA_MSG_NONCONFIRM)
719 serverRequest.qos = OC_LOW_QOS;
721 else if (requestInfo->info.type == CA_MSG_ACKNOWLEDGE)
723 // TODO-CA: Need to handle this
725 else if (requestInfo->info.type == CA_MSG_RESET)
727 // TODO-CA: Need to handle this
729 // CA does not need the following 3 fields
730 serverRequest.coapID = 0;
731 serverRequest.delayedResNeeded = 0;
732 serverRequest.secured = endPoint->isSecured;
735 serverRequest.addressInfo = endPoint->addressInfo;
736 serverRequest.connectivityType = endPoint->connectivityType;
738 // copy vendor specific header options
739 // TODO-CA: CA is including non-vendor header options as well, like observe.
740 // Need to filter those out
741 GetObserveHeaderOption(&serverRequest.observationOption,
742 requestInfo->info.options, (uint8_t *)&(requestInfo->info.numOptions));
743 if (requestInfo->info.numOptions > MAX_HEADER_OPTIONS)
746 PCF("The request info numOptions is greater than MAX_HEADER_OPTIONS"));
749 serverRequest.numRcvdVendorSpecificHeaderOptions = requestInfo->info.numOptions;
750 if (serverRequest.numRcvdVendorSpecificHeaderOptions)
752 memcpy (&(serverRequest.rcvdVendorSpecificHeaderOptions), requestInfo->info.options,
753 sizeof(CAHeaderOption_t)*requestInfo->info.numOptions);
757 if(HandleStackRequests (&serverRequest) != OC_STACK_OK)
759 OC_LOG(ERROR, TAG, PCF("HandleStackRequests failed"));
761 OC_LOG(INFO, TAG, PCF("Exit HandleCARequests"));
764 //This function will be called back by occoap layer when a request is received
765 OCStackResult HandleStackRequests(OCServerProtocolRequest * protocolRequest)
767 OC_LOG(INFO, TAG, PCF("Entering HandleStackRequests (OCStack Layer)"));
768 OCStackResult result = OC_STACK_ERROR;
769 ResourceHandling resHandling;
770 OCResource *resource;
773 OC_LOG(ERROR, TAG, PCF("protocolRequest is NULL"));
774 return OC_STACK_INVALID_PARAM;
777 OCServerRequest * request = GetServerRequestUsingToken(protocolRequest->requestToken);
780 OC_LOG(INFO, TAG, PCF("This is a new Server Request"));
781 result = AddServerRequest(&request, protocolRequest->coapID,
782 protocolRequest->delayedResNeeded, protocolRequest->secured, 0,
783 protocolRequest->method, protocolRequest->numRcvdVendorSpecificHeaderOptions,
784 protocolRequest->observationOption, protocolRequest->qos,
785 protocolRequest->query, protocolRequest->rcvdVendorSpecificHeaderOptions,
786 protocolRequest->reqJSONPayload, &protocolRequest->requestToken,
787 &protocolRequest->requesterAddr, protocolRequest->resourceUrl,
788 protocolRequest->reqTotalSize,
789 &protocolRequest->addressInfo, protocolRequest->connectivityType);
790 if (OC_STACK_OK != result)
792 OC_LOG(ERROR, TAG, PCF("Error adding server request"));
795 VERIFY_NON_NULL(request, ERROR, OC_STACK_NO_MEMORY);
797 if(!protocolRequest->reqMorePacket)
799 request->requestComplete = 1;
804 OC_LOG(INFO, TAG, PCF("This is either a repeated Server Request or blocked Server Request"));
807 if(request->requestComplete)
809 OC_LOG(INFO, TAG, PCF("This Server Request is complete"));
810 result = DetermineResourceHandling (request, &resHandling, &resource);
811 if (result == OC_STACK_OK)
813 result = ProcessRequest(resHandling, resource, request);
817 result = OC_STACK_ERROR;
822 OC_LOG(INFO, TAG, PCF("This Server Request is incomplete"));
823 result = OC_STACK_CONTINUE;
828 //This function will be called back by occoap layer when a response is received
829 OCStackResult HandleStackResponses(OCResponse * response)
831 OC_LOG(INFO, TAG, PCF("Entering HandleStackResponses (OCStack Layer)"));
832 OCStackResult result = OC_STACK_OK;
833 OCStackApplicationResult cbResult = OC_STACK_DELETE_TRANSACTION;
834 uint8_t isObserveNotification = 0;
835 ClientCB * cbNode = NULL;
838 OC_LOG(ERROR, TAG, PCF("response is NULL"));
839 return OC_STACK_INVALID_PARAM;
842 uint8_t isPresenceNotification = 0;
843 uint8_t isMulticastPresence = 0;
844 char * resourceTypeName = NULL;
845 uint32_t lowerBound = 0;
846 uint32_t higherBound = 0;
848 unsigned char * bufRes = response->bufRes;
849 #endif // WITH_PRESENCE
851 cbNode = response->cbNode;
854 cbNode = GetClientCB(response->rcvdToken, NULL, NULL);
857 if(response->clientResponse->sequenceNumber >= OC_OFFSET_SEQUENCE_NUMBER)
859 isObserveNotification = 1;
860 OC_LOG(INFO, TAG, PCF("Received an observe notification"));
863 OC_LOG_V(DEBUG, TAG, "The sequenceNumber/NONCE of this response %u",
864 response->clientResponse->sequenceNumber);
865 OC_LOG_V(DEBUG, TAG, "The maxAge/TTL of this response %u", response->maxAge);
866 OC_LOG_V(DEBUG, TAG, "The response received is %s", bufRes);
869 if(!strcmp((char *)response->rcvdUri, (char *)OC_PRESENCE_URI)){
870 isPresenceNotification = 1;
873 result = OC_STACK_INVALID_PARAM;
876 tok = strtok((char *)bufRes, "[:]}");
877 bufRes[strlen((char *)bufRes)] = ':';
878 tok = strtok(NULL, "[:]}");
879 bufRes[strlen((char *)bufRes)] = ':';
880 response->clientResponse->sequenceNumber = (uint32_t )atoi(tok);
881 OC_LOG_V(DEBUG, TAG, "The received NONCE is %u", response->clientResponse->sequenceNumber);
882 tok = strtok(NULL, "[:]}");
883 response->maxAge = (uint32_t )atoi(tok);
884 OC_LOG_V(DEBUG, TAG, "The received TTL is %u", response->maxAge);
885 tok = strtok(NULL, "[:]}");
888 resourceTypeName = (char *)OCMalloc(strlen(tok));
889 if(!resourceTypeName)
893 bufRes[strlen((char *)bufRes)] = ':';
894 strcpy(resourceTypeName, tok);
895 OC_LOG_V(DEBUG, TAG, "----------------resourceTypeName %s",
898 bufRes[strlen((char *)bufRes)] = ']';
901 // Check if the application subcribed for presence
904 cbNode = GetClientCB(NULL, NULL, response->fullUri);
907 // Check if application subscribed for multicast presence
910 snprintf((char *)response->fullUri, MAX_URI_LENGTH, "%s%s",
911 OC_MULTICAST_IP, response->rcvdUri);
912 cbNode = GetClientCB(NULL, NULL, response->fullUri);
915 isMulticastPresence = 1;
916 isPresenceNotification = 0;
920 if(cbNode && isPresenceNotification)
922 OC_LOG(INFO, TAG, PCF("Received a presence notification"));
923 if(!cbNode->presence)
925 cbNode->presence = (OCPresence *) OCMalloc(sizeof(OCPresence));
926 VERIFY_NON_NULL_V(cbNode->presence);
927 cbNode->presence->timeOut = NULL;
928 cbNode->presence->timeOut = (uint32_t *)
929 OCMalloc(PresenceTimeOutSize * sizeof(uint32_t));
930 if(!(cbNode->presence->timeOut)){
931 OCFree(cbNode->presence);
932 result = OC_STACK_NO_MEMORY;
935 if(response->maxAge == 0)
937 OC_LOG(INFO, TAG, PCF("===============Stopping presence"));
938 response->clientResponse->result = OC_STACK_PRESENCE_STOPPED;
941 OCFree(cbNode->presence->timeOut);
942 OCFree(cbNode->presence);
943 cbNode->presence = NULL;
948 OC_LOG_V(INFO, TAG, "===============Update presence TTL, now time is %d", GetTime(0));
949 cbNode->presence->TTL = response->maxAge;
950 for(int index = 0; index < PresenceTimeOutSize; index++)
952 lowerBound = GetTime(((float)(PresenceTimeOut[index])
953 /(float)100)*(float)cbNode->presence->TTL);
954 higherBound = GetTime(((float)(PresenceTimeOut[index + 1])
955 /(float)100)*(float)cbNode->presence->TTL);
956 cbNode->presence->timeOut[index] = OCGetRandomRange(lowerBound, higherBound);
957 OC_LOG_V(DEBUG, TAG, "----------------lowerBound timeout %d", lowerBound);
958 OC_LOG_V(DEBUG, TAG, "----------------higherBound timeout %d", higherBound);
959 OC_LOG_V(DEBUG, TAG, "----------------timeOut entry %d",
960 cbNode->presence->timeOut[index]);
962 cbNode->presence->TTLlevel = 0;
963 OC_LOG_V(DEBUG, TAG, "----------------this TTL level %d", cbNode->presence->TTLlevel);
964 if(cbNode->sequenceNumber == response->clientResponse->sequenceNumber)
966 OC_LOG(INFO, TAG, PCF("===============No presence change"));
969 OC_LOG(INFO, TAG, PCF("===============Presence changed, calling up the stack"));
970 cbNode->sequenceNumber = response->clientResponse->sequenceNumber;;
973 // Ensure that a filter is actually applied.
974 if(resourceTypeName && cbNode->filterResourceType)
976 if(!findResourceType(cbNode->filterResourceType, resourceTypeName))
982 else if(cbNode && isMulticastPresence)
984 // Check if the same nonce for a given host
985 OCMulticastNode* mcNode = NULL;
986 mcNode = GetMCPresenceNode(response->fullUri);
988 if(response->maxAge == 0)
990 OC_LOG(INFO, TAG, PCF("===============Stopping presence"));
991 response->clientResponse->result = OC_STACK_PRESENCE_STOPPED;
994 OCFree(cbNode->presence->timeOut);
995 OCFree(cbNode->presence);
996 cbNode->presence = NULL;
999 else if(mcNode != NULL)
1001 if(mcNode->nonce == response->clientResponse->sequenceNumber)
1003 OC_LOG(INFO, TAG, PCF("===============No presence change (Multicast)"));
1004 result = OC_STACK_NO_MEMORY;
1007 mcNode->nonce = response->clientResponse->sequenceNumber;
1011 uint32_t uriLen = strlen((char*)response->fullUri);
1012 unsigned char* uri = (unsigned char *) OCMalloc(uriLen + 1);
1015 memcpy(uri, response->fullUri, (uriLen + 1));
1020 PCF("===============No Memory for URI to store in the presence node"));
1021 result = OC_STACK_NO_MEMORY;
1024 result = AddMCPresenceNode(&mcNode, (unsigned char*) uri,
1025 response->clientResponse->sequenceNumber);
1026 if(result == OC_STACK_NO_MEMORY)
1029 PCF("===============No Memory for Multicast Presence Node"));
1030 result = OC_STACK_NO_MEMORY;
1035 // Ensure that a filter is actually applied.
1036 if(resourceTypeName && cbNode->filterResourceType)
1038 if(!findResourceType(cbNode->filterResourceType, resourceTypeName))
1045 else if(!cbNode && isPresenceNotification)
1047 OC_LOG(INFO, TAG, PCF("Received a presence notification, but I do not have callback \
1048 ------------ ignoring"));
1050 #endif // WITH_PRESENCE
1054 if(isObserveNotification)
1056 OC_LOG(INFO, TAG, PCF("Received an observe notification"));
1057 //TODO: check the standard for methods to detect wrap around condition
1058 if(cbNode->method == OC_REST_OBSERVE &&
1059 (response->clientResponse->sequenceNumber <= cbNode->sequenceNumber ||
1060 (response->clientResponse->sequenceNumber > cbNode->sequenceNumber &&
1061 response->clientResponse->sequenceNumber ==
1062 MAX_SEQUENCE_NUMBER)))
1064 OC_LOG_V(DEBUG, TAG, "Observe notification came out of order. \
1065 Ignoring Incoming:%d Against Current:%d.",
1066 response->clientResponse->sequenceNumber, cbNode->sequenceNumber);
1069 if(response->clientResponse->sequenceNumber > cbNode->sequenceNumber){
1070 cbNode->sequenceNumber = response->clientResponse->sequenceNumber;
1074 response->clientResponse->resJSONPayload = bufRes;
1076 cbResult = cbNode->callBack(cbNode->context, cbNode->handle, response->clientResponse);
1078 if (cbResult == OC_STACK_DELETE_TRANSACTION ||
1079 response->clientResponse->result == OC_STACK_COMM_ERROR ||
1080 (response->clientResponse->result == OC_STACK_RESOURCE_DELETED &&
1081 !isPresenceNotification && !isMulticastPresence))
1083 FindAndDeleteClientCB(cbNode);
1088 result = OC_STACK_ERROR;
1092 #ifdef WITH_PRESENCE
1093 OCFree(resourceTypeName);
1098 int ParseIPv4Address(unsigned char * ipAddrStr, uint8_t * ipAddr, uint16_t * port)
1101 unsigned char *itr, *coap;
1102 uint8_t dotCount = 0;
1106 /* search for scheme */
1108 if (!isdigit((unsigned char) *ipAddrStr))
1110 coap = (unsigned char *) OC_COAP_SCHEME;
1111 while (*coap && tolower(*itr) == *coap)
1119 while (*ipAddrStr) {
1120 if (isdigit((unsigned char) *ipAddrStr))
1122 ipAddr[index] *= 10;
1123 ipAddr[index] += *ipAddrStr - '0';
1125 else if ((unsigned char) *ipAddrStr == '.')
1137 if(*ipAddrStr == ':')
1141 if (isdigit((unsigned char) *ipAddrStr))
1144 *port += *ipAddrStr - '0';
1155 if (ipAddr[0] < 255 && ipAddr[1] < 255 && ipAddr[2] < 255 && ipAddr[3] < 255
1166 //-----------------------------------------------------------------------------
1167 // Private internal function prototypes
1168 //-----------------------------------------------------------------------------
1170 static OCDoHandle GenerateInvocationHandle();
1171 static OCStackResult initResources();
1172 static void insertResource(OCResource *resource);
1173 static OCResource *findResource(OCResource *resource);
1174 static void insertResourceType(OCResource *resource,
1175 OCResourceType *resourceType);
1176 static OCResourceType *findResourceTypeAtIndex(OCResourceHandle handle,
1178 static void insertResourceInterface(OCResource *resource,
1179 OCResourceInterface *resourceInterface);
1180 static OCResourceInterface *findResourceInterfaceAtIndex(
1181 OCResourceHandle handle, uint8_t index);
1182 static void deleteResourceType(OCResourceType *resourceType);
1183 static void deleteResourceInterface(OCResourceInterface *resourceInterface);
1184 static void deleteResourceElements(OCResource *resource);
1185 static int deleteResource(OCResource *resource);
1186 static void deleteAllResources();
1187 static void incrementSequenceNumber(OCResource * resPtr);
1188 static OCStackResult verifyUriQueryLength(const char * inputUri,
1190 static uint8_t OCIsPacketTransferRequired(const char *request, const char *response, uint16_t size);
1191 OCStackResult getResourceType(const char * query, unsigned char** resourceType);
1193 //-----------------------------------------------------------------------------
1195 //-----------------------------------------------------------------------------
1198 * Initialize the OC Stack. Must be called prior to starting the stack.
1201 * IP Address of host device
1203 * Port of host device
1205 * Host device is client, server, or client-server
1208 * OC_STACK_OK - no errors
1209 * OC_STACK_ERROR - stack init error
1211 OCStackResult OCInit(const char *ipAddr, uint16_t port, OCMode mode)
1213 OCStackResult result = OC_STACK_ERROR;
1214 OC_LOG(INFO, TAG, PCF("Entering OCInit"));
1218 OC_LOG_V(INFO, TAG, "IP Address = %s", ipAddr);
1223 //It is ok to select network to CA_WIFI for now
1224 CAResult_t caResult = CASelectNetwork(CA_WIFI|CA_ETHERNET);
1225 if(caResult == CA_STATUS_OK)
1227 OC_LOG(INFO, TAG, PCF("CASelectNetwork to WIFI"));
1228 CARegisterHandler(HandleCARequests, HandleCAResponses);
1230 OC_LOG(INFO, TAG, PCF("CARegisterHandler..."));
1231 stackState = OC_STACK_INITIALIZED;
1232 result = OC_STACK_OK;
1236 caResult = CAStartDiscoveryServer();
1237 OC_LOG(INFO, TAG, PCF("Client mode: CAStartDiscoveryServer"));
1240 caResult = CAStartListeningServer();
1241 OC_LOG(INFO, TAG, PCF("Server mode: CAStartListeningServer"));
1243 case OC_CLIENT_SERVER:
1244 caResult = CAStartListeningServer();
1245 if(caResult == CA_STATUS_OK)
1247 caResult = CAStartDiscoveryServer();
1249 OC_LOG(INFO, TAG, PCF("Client-server mode"));
1252 OC_LOG(ERROR, TAG, PCF("Invalid mode"));
1253 return OC_STACK_ERROR;
1258 if (caResult == CA_STATUS_OK)
1260 result = OC_STACK_OK;
1264 result = OC_STACK_ERROR;
1269 defaultDeviceHandler = NULL;
1271 #if defined(__WITH_DTLS__)
1272 caResult = CARegisterDTLSCredentialsHandler(GetDtlsPskCredentials);
1273 result = (caResult == CA_STATUS_OK) ? OC_STACK_OK : OC_STACK_ERROR;
1274 #endif // (__WITH_DTLS__)
1276 #ifdef WITH_PRESENCE
1277 PresenceTimeOutSize = sizeof(PresenceTimeOut)/sizeof(PresenceTimeOut[0]) - 1;
1278 #endif // WITH_PRESENCE
1280 if (result == OC_STACK_OK)
1282 stackState = OC_STACK_INITIALIZED;
1284 // Initialize resource
1285 if(result == OC_STACK_OK && myStackMode != OC_CLIENT)
1287 result = initResources();
1289 if(result != OC_STACK_OK)
1291 OC_LOG(ERROR, TAG, PCF("Stack initialization error"));
1297 * Stop the OC stack. Use for a controlled shutdown.
1299 * OC_STACK_OK - no errors
1300 * OC_STACK_ERROR - stack not initialized
1302 OCStackResult OCStop()
1304 OCStackResult result = OC_STACK_ERROR;
1306 OC_LOG(INFO, TAG, PCF("Entering OCStop"));
1308 if (stackState == OC_STACK_UNINIT_IN_PROGRESS)
1310 OC_LOG(DEBUG, TAG, PCF("Stack already stopping, exiting"));
1313 else if (stackState != OC_STACK_INITIALIZED)
1315 OC_LOG(ERROR, TAG, PCF("Stack not initialized"));
1316 return OC_STACK_ERROR;
1319 stackState = OC_STACK_UNINIT_IN_PROGRESS;
1321 #ifdef WITH_PRESENCE
1322 // Ensure that the TTL associated with ANY and ALL presence notifications originating from
1323 // here send with the code "OC_STACK_PRESENCE_STOPPED" result.
1324 presenceResource.presenceTTL = 0;
1325 #endif // WITH_PRESENCE
1327 // Free memory dynamically allocated for resources
1328 deleteAllResources();
1331 //CATerminate does not return any error code. It is OK to assign result to OC_STACK_OK.
1332 result = OC_STACK_OK;
1334 if (result == OC_STACK_OK)
1336 // Remove all observers
1337 DeleteObserverList();
1338 // Remove all the client callbacks
1339 DeleteClientCBList();
1340 stackState = OC_STACK_UNINITIALIZED;
1341 result = OC_STACK_OK;
1343 stackState = OC_STACK_INITIALIZED;
1344 result = OC_STACK_ERROR;
1347 // Deinit security blob
1348 DeinitOCSecurityInfo();
1350 if (result != OC_STACK_OK) {
1351 OC_LOG(ERROR, TAG, PCF("Stack stop error"));
1358 * Map OCQualityOfService to CAMessageType
1360 * @param OCQualityOfService - Input qos.
1362 * Returns CA message type for a given qos.
1364 CAMessageType_t qualityOfServiceToMessageType(OCQualityOfService qos)
1369 return CA_MSG_CONFIRM;
1374 return CA_MSG_NONCONFIRM;
1379 * Verify the lengths of the URI and the query separately
1381 * @param inputUri - Input URI and query.
1382 * @param uriLen - The length of the initial URI with query.
1384 * Note: The '?' that appears after the URI is not considered as
1385 * a part of the query.
1387 OCStackResult verifyUriQueryLength(const char *inputUri, uint16_t uriLen)
1391 query = strchr (inputUri, '?');
1395 if((query - inputUri) > MAX_URI_LENGTH)
1397 return OC_STACK_INVALID_URI;
1400 if((inputUri + uriLen - 1 - query) > MAX_QUERY_LENGTH)
1402 return OC_STACK_INVALID_QUERY;
1405 else if(uriLen > MAX_URI_LENGTH)
1407 return OC_STACK_INVALID_URI;
1413 * Discover or Perform requests on a specified resource (specified by that Resource's respective URI).
1415 * @param handle - @ref OCDoHandle to refer to the request sent out on behalf of calling this API.
1416 * @param method - @ref OCMethod to perform on the resource
1417 * @param requiredUri - URI of the resource to interact with
1418 * @param referenceUri - URI of the reference resource
1419 * @param request - JSON encoded request
1420 * @param qos - quality of service
1421 * @param cbData - struct that contains asynchronous callback function that is invoked
1422 * by the stack when discovery or resource interaction is complete
1423 * @param options - The address of an array containing the vendor specific header
1424 * header options to be sent with the request
1425 * @param numOptions - Number of vendor specific header options to be included
1428 * OC_STACK_OK - no errors
1429 * OC_STACK_INVALID_CALLBACK - invalid callback function pointer
1430 * OC_STACK_INVALID_METHOD - invalid resource method
1431 * OC_STACK_INVALID_URI - invalid required or reference URI
1433 * Note: IN case of CA, when using multicast, the required URI should not contain IP address.
1434 * Instead, it just contains the URI to the resource such as "/oc/core".
1436 OCStackResult OCDoResource(OCDoHandle *handle, OCMethod method, const char *requiredUri,
1437 const char *referenceUri, const char *request, OCConnectivityType conType,
1438 OCQualityOfService qos, OCCallbackData *cbData,
1439 OCHeaderOption * options, uint8_t numOptions)
1441 OCStackResult result = OC_STACK_ERROR;
1442 ClientCB *clientCB = NULL;
1443 unsigned char * requestUri = NULL;
1444 unsigned char * resourceType = NULL;
1445 unsigned char * query = NULL;
1446 char * newUri = (char *)requiredUri;
1447 (void) referenceUri;
1448 CARemoteEndpoint_t* endpoint = NULL;
1449 CAResult_t caResult;
1450 CAToken_t token = NULL;
1451 CAInfo_t requestData;
1452 CARequestInfo_t requestInfo;
1453 CAGroupEndpoint_t grpEnd;
1455 // To track if memory is allocated for additional header options
1456 uint8_t hdrOptionMemAlloc = 0;
1458 OC_LOG(INFO, TAG, PCF("Entering OCDoResource"));
1460 // Validate input parameters
1461 VERIFY_NON_NULL(cbData, FATAL, OC_STACK_INVALID_CALLBACK);
1462 VERIFY_NON_NULL(cbData->cb, FATAL, OC_STACK_INVALID_CALLBACK);
1464 TODO ("Need to form the final query by concatenating require and reference URI's");
1465 VERIFY_NON_NULL(requiredUri, FATAL, OC_STACK_INVALID_URI);
1467 uint16_t uriLen = strlen(requiredUri);
1469 // ToDo: We should also check if the requiredUri has a mutlicast address, then qos has to be OC_Low_QOS
1475 case OC_REST_DELETE:
1476 case OC_REST_OBSERVE:
1477 case OC_REST_OBSERVE_ALL:
1478 case OC_REST_CANCEL_OBSERVE:
1480 #ifdef WITH_PRESENCE
1481 case OC_REST_PRESENCE:
1485 result = OC_STACK_INVALID_METHOD;
1489 if((result = verifyUriQueryLength(requiredUri, uriLen)) != OC_STACK_OK)
1494 if((request) && (strlen(request) > MAX_REQUEST_LENGTH))
1496 result = OC_STACK_INVALID_PARAM;
1500 #ifdef WITH_PRESENCE
1501 if(method == OC_REST_PRESENCE)
1503 result = getQueryFromUri(requiredUri, &query, &newUri);
1506 result = getResourceType((char *) query, &resourceType);
1509 OC_LOG_V(DEBUG, TAG, "Got Resource Type: %s", resourceType);
1513 OC_LOG(DEBUG, TAG, PCF("Resource type is NULL."));
1518 OC_LOG(DEBUG, TAG, PCF("Query string is NULL."));
1520 if(result != OC_STACK_OK)
1525 #endif // WITH_PRESENCE
1527 requestUri = (unsigned char *) OCMalloc(uriLen + 1);
1530 memcpy(requestUri, newUri, (uriLen + 1));
1534 result = OC_STACK_NO_MEMORY;
1538 *handle = GenerateInvocationHandle();
1541 result = OC_STACK_NO_MEMORY;
1545 memset(&requestData, 0, sizeof(CAInfo_t));
1546 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
1547 memset(&grpEnd, 0, sizeof(CAGroupEndpoint_t));
1551 case OC_REST_OBSERVE:
1552 case OC_REST_OBSERVE_ALL:
1553 case OC_REST_CANCEL_OBSERVE:
1555 requestInfo.method = CA_GET;
1560 requestInfo.method = CA_PUT;
1565 requestInfo.method = CA_POST;
1568 case OC_REST_DELETE:
1570 requestInfo.method = CA_DELETE;
1573 #ifdef WITH_PRESENCE
1574 case OC_REST_PRESENCE:
1576 // Replacing method type with GET because "presence"
1577 // is a stack layer only implementation.
1578 requestInfo.method = CA_GET;
1583 result = OC_STACK_INVALID_METHOD;
1587 //High QoS is not supported
1588 if(qos == OC_HIGH_QOS)
1590 result = OC_STACK_INVALID_PARAM;
1596 caResult = CAGenerateToken(&token);
1597 if (caResult != CA_STATUS_OK)
1599 OC_LOG(ERROR, TAG, PCF("CAGenerateToken error"));
1600 CADestroyToken(token);
1604 requestData.type = qualityOfServiceToMessageType(qos);
1605 requestData.token = token;
1606 if ((method == OC_REST_OBSERVE) || (method == OC_REST_OBSERVE_ALL))
1608 result = CreateObserveHeaderOption (&(requestData.options), options,
1609 numOptions, OC_OBSERVE_REGISTER);
1610 if (result != OC_STACK_OK)
1614 hdrOptionMemAlloc = 1;
1615 requestData.numOptions = numOptions + 1;
1619 requestData.options = (CAHeaderOption_t*)options;
1620 requestData.numOptions = numOptions;
1622 requestData.payload = (char *)request;
1624 requestInfo.info = requestData;
1626 CAConnectivityType_t caConType;
1628 result = OCToCAConnectivityType(conType, &caConType);
1629 if (result != OC_STACK_OK)
1631 OC_LOG(ERROR, TAG, PCF("Invalid Connectivity Type"));
1636 if(conType == OC_ALL)
1638 grpEnd.connectivityType = caConType;
1640 grpEnd.resourceUri = (CAURI_t) OCMalloc(uriLen + 1);
1641 strncpy(grpEnd.resourceUri, requiredUri, (uriLen + 1));
1643 caResult = CASendRequestToAll(&grpEnd, &requestInfo);
1647 caResult = CACreateRemoteEndpoint(newUri, caConType, &endpoint);
1649 if (caResult != CA_STATUS_OK)
1651 OC_LOG(ERROR, TAG, PCF("CACreateRemoteEndpoint error"));
1655 caResult = CASendRequest(endpoint, &requestInfo);
1658 if (caResult != CA_STATUS_OK)
1660 OC_LOG(ERROR, TAG, PCF("CASendRequest"));
1664 if((result = AddClientCB(&clientCB, cbData, &token, handle, method,
1665 requestUri, resourceType)) != OC_STACK_OK)
1667 result = OC_STACK_NO_MEMORY;
1672 if(newUri != requiredUri)
1676 if (result != OC_STACK_OK)
1678 OC_LOG(ERROR, TAG, PCF("OCDoResource error"));
1679 FindAndDeleteClientCB(clientCB);
1681 CADestroyRemoteEndpoint(endpoint);
1682 OCFree(grpEnd.resourceUri);
1683 if (hdrOptionMemAlloc)
1685 OCFree(requestData.options);
1691 * Cancel a request associated with a specific @ref OCDoResource invocation.
1693 * @param handle - Used to identify a specific OCDoResource invocation.
1694 * @param qos - used to specify Quality of Service (read below for more info)
1695 * @param options- used to specify vendor specific header options when sending
1696 * explicit observe cancellation
1697 * @param numOptions- Number of header options to be included
1700 * OC_STACK_OK - No errors; Success
1701 * OC_STACK_INVALID_PARAM - The handle provided is invalid.
1703 OCStackResult OCCancel(OCDoHandle handle, OCQualityOfService qos, OCHeaderOption * options,
1707 * This ftn is implemented one of two ways in the case of observation:
1709 * 1. qos == OC_NON_CONFIRMABLE. When observe is unobserved..
1710 * Remove the callback associated on client side.
1711 * When the next notification comes in from server,
1712 * reply with RESET message to server.
1713 * Keep in mind that the server will react to RESET only
1714 * if the last notification was sent ans CON
1716 * 2. qos == OC_CONFIRMABLE. When OCCancel is called,
1717 * and it is associated with an observe request
1718 * (i.e. ClientCB->method == OC_REST_OBSERVE || OC_REST_OBSERVE_ALL),
1719 * Send CON Observe request to server with
1720 * observe flag = OC_RESOURCE_OBSERVE_DEREGISTER.
1721 * Remove the callback associated on client side.
1723 OCStackResult ret = OC_STACK_OK;
1724 CARemoteEndpoint_t* endpoint = NULL;
1725 CAResult_t caResult;
1726 CAInfo_t requestData;
1727 CARequestInfo_t requestInfo;
1728 // Track if memory is allocated for additional header options
1729 uint8_t hdrOptionMemAlloc = 0;
1732 return OC_STACK_INVALID_PARAM;
1735 OC_LOG(INFO, TAG, PCF("Entering OCCancel"));
1737 ClientCB *clientCB = GetClientCB(NULL, handle, NULL);
1740 switch (clientCB->method)
1742 case OC_REST_OBSERVE:
1743 case OC_REST_OBSERVE_ALL:
1744 //TODO-CA : Why CA_WIFI alone?
1745 caResult = CACreateRemoteEndpoint((char *)clientCB->requestUri, CA_WIFI,
1747 if (caResult != CA_STATUS_OK)
1749 OC_LOG(ERROR, TAG, PCF("CACreateRemoteEndpoint error"));
1750 return OC_STACK_ERROR;
1753 memset(&requestData, 0, sizeof(CAInfo_t));
1754 requestData.type = qualityOfServiceToMessageType(qos);
1755 requestData.token = clientCB->token;
1756 if (CreateObserveHeaderOption (&(requestData.options),
1757 options, numOptions, OC_OBSERVE_DEREGISTER) != OC_STACK_OK)
1759 return OC_STACK_ERROR;
1761 hdrOptionMemAlloc = 1;
1762 requestData.numOptions = numOptions + 1;
1763 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
1764 requestInfo.method = CA_GET;
1765 requestInfo.info = requestData;
1767 caResult = CASendRequest(endpoint, &requestInfo);
1768 if (caResult != CA_STATUS_OK)
1770 OC_LOG(ERROR, TAG, PCF("CASendRequest error"));
1772 if(caResult == CA_STATUS_OK)
1776 #ifdef WITH_PRESENCE
1777 case OC_REST_PRESENCE:
1778 FindAndDeleteClientCB(clientCB);
1782 return OC_STACK_INVALID_METHOD;
1785 CADestroyRemoteEndpoint(endpoint);
1786 if (hdrOptionMemAlloc)
1788 OCFree(requestData.options);
1794 #ifdef WITH_PRESENCE
1795 OCStackResult OCProcessPresence()
1797 OCStackResult result = OC_STACK_OK;
1798 uint8_t ipAddr[4] = { 0 };
1801 OC_LOG(INFO, TAG, PCF("Entering RequestPresence"));
1802 ClientCB* cbNode = NULL;
1804 OCClientResponse clientResponse;
1805 OCResponse * response = NULL;
1806 OCStackApplicationResult cbResult = OC_STACK_DELETE_TRANSACTION;
1808 LL_FOREACH(cbList, cbNode) {
1809 if(OC_REST_PRESENCE == cbNode->method)
1811 if(cbNode->presence)
1813 uint32_t now = GetTime(0);
1814 OC_LOG_V(DEBUG, TAG, "----------------this TTL level %d",
1815 cbNode->presence->TTLlevel);
1816 OC_LOG_V(DEBUG, TAG, "----------------current ticks %d", now);
1819 if(cbNode->presence->TTLlevel >= (PresenceTimeOutSize + 1))
1824 if(cbNode->presence->TTLlevel < PresenceTimeOutSize){
1825 OC_LOG_V(DEBUG, TAG, "----------------timeout ticks %d",
1826 cbNode->presence->timeOut[cbNode->presence->TTLlevel]);
1829 if(cbNode->presence->TTLlevel >= PresenceTimeOutSize)
1831 OC_LOG(DEBUG, TAG, PCF("----------------No more timeout ticks"));
1832 if (ParseIPv4Address( cbNode->requestUri, ipAddr, &port))
1834 OCBuildIPv4Address(ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3], port,
1836 result = FormOCClientResponse(&clientResponse, OC_STACK_PRESENCE_TIMEOUT,
1837 (OCDevAddr *) &dst, 0, NULL);
1838 if(result != OC_STACK_OK)
1842 result = FormOCResponse(&response, cbNode, 0, NULL, NULL,
1843 &cbNode->token, &clientResponse, NULL);
1844 if(result != OC_STACK_OK)
1849 // Increment the TTLLevel (going to a next state), so we don't keep
1850 // sending presence notification to client.
1851 cbNode->presence->TTLlevel++;
1852 OC_LOG_V(DEBUG, TAG, "----------------moving to TTL level %d",
1853 cbNode->presence->TTLlevel);
1857 result = OC_STACK_INVALID_IP;
1861 cbResult = cbNode->callBack(cbNode->context, cbNode->handle, &clientResponse);
1862 if (cbResult == OC_STACK_DELETE_TRANSACTION)
1864 FindAndDeleteClientCB(cbNode);
1868 if(now >= cbNode->presence->timeOut[cbNode->presence->TTLlevel])
1870 CAResult_t caResult;
1871 CARemoteEndpoint_t* endpoint = NULL;
1872 CAInfo_t requestData;
1873 CARequestInfo_t requestInfo;
1875 OC_LOG(DEBUG, TAG, PCF("time to test server presence =========="));
1877 //TODO-CA : Why CA_WIFI alone?
1878 caResult = CACreateRemoteEndpoint((char *)cbNode->requestUri, CA_WIFI,
1881 if (caResult != CA_STATUS_OK)
1883 OC_LOG(ERROR, TAG, PCF("CACreateRemoteEndpoint error"));
1887 memset(&requestData, 0, sizeof(CAInfo_t));
1888 requestData.type = CA_MSG_NONCONFIRM;
1889 requestData.token = cbNode->token;
1891 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
1892 requestInfo.method = CA_GET;
1893 requestInfo.info = requestData;
1895 caResult = CASendRequest(endpoint, &requestInfo);
1897 if (caResult != CA_STATUS_OK)
1899 OC_LOG(ERROR, TAG, PCF("CASendRequest error"));
1903 cbNode->presence->TTLlevel++;
1904 OC_LOG_V(DEBUG, TAG, "----------------moving to TTL level %d",
1905 cbNode->presence->TTLlevel);
1911 if (result != OC_STACK_OK)
1913 OC_LOG(ERROR, TAG, PCF("OCProcessPresence error"));
1917 #endif // WITH_PRESENCE
1920 * Called in main loop of OC client or server. Allows low-level processing of
1924 * OC_STACK_OK - no errors
1925 * OC_STACK_ERROR - stack process error
1927 OCStackResult OCProcess()
1929 #ifdef WITH_PRESENCE
1930 OCProcessPresence();
1932 CAHandleRequestResponse();
1937 #ifdef WITH_PRESENCE
1939 * When operating in @ref OCServer or @ref OCClientServer mode, this API will start sending out
1940 * presence notifications to clients via multicast. Once this API has been called with a success,
1941 * clients may query for this server's presence and this server's stack will respond via multicast.
1943 * Server can call this function when it comes online for the first time, or when it comes back
1944 * online from offline mode, or when it re enters network.
1946 * @param ttl - Time To Live in seconds
1947 * Note: If ttl is '0', then the default stack value will be used (60 Seconds).
1950 * OC_STACK_OK - No errors; Success
1952 OCStackResult OCStartPresence(const uint32_t ttl)
1954 OCChangeResourceProperty(
1955 &(((OCResource *)presenceResource.handle)->resourceProperties),
1960 presenceResource.presenceTTL = ttl;
1963 if(OC_PRESENCE_UNINITIALIZED == presenceState)
1965 OCDevAddr multiCastAddr;
1966 presenceState = OC_PRESENCE_INITIALIZED;
1968 OCBuildIPv4Address(224, 0, 1, 187, 5683, &multiCastAddr);
1970 CAAddress_t addressInfo;
1971 strncpy(addressInfo.IP.ipAddress, "224.0.1.187", CA_IPADDR_SIZE);
1972 addressInfo.IP.port = 5683;
1974 //TODO make sure there is no memory leak here since another copy
1975 //of token is being created inside AddObserver
1976 CAToken_t caToken = NULL;
1977 CAResult_t caResult = CAGenerateToken(&caToken);
1978 if (caResult != CA_STATUS_OK)
1980 OC_LOG(ERROR, TAG, PCF("CAGenerateToken error"));
1981 CADestroyToken(caToken);
1982 return OC_STACK_ERROR;
1985 AddObserver(OC_PRESENCE_URI, NULL, 0, &caToken,
1986 &multiCastAddr, (OCResource *)presenceResource.handle, OC_LOW_QOS,
1987 &addressInfo, CA_WIFI);
1990 // Each time OCStartPresence is called
1991 // a different random 32-bit integer number is used
1992 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
1994 return SendPresenceNotification(NULL);
1998 * When operating in @ref OCServer or @ref OCClientServer mode, this API will stop sending out
1999 * presence notifications to clients via multicast. Once this API has been called with a success,
2000 * this server's stack will not respond to clients querying for this server's presence.
2002 * Server can call this function when it is terminating, going offline, or when going
2003 * away from network.
2006 * OC_STACK_OK - No errors; Success
2008 OCStackResult OCStopPresence()
2010 OCStackResult result = OC_STACK_ERROR;
2011 //make resource inactive
2012 result = OCChangeResourceProperty(
2013 &(((OCResource *) presenceResource.handle)->resourceProperties),
2015 result = SendPresenceNotification(NULL);
2022 OCStackResult OCSetDefaultDeviceEntityHandler(OCDeviceEntityHandler entityHandler)
2024 defaultDeviceHandler = entityHandler;
2029 OCStackResult OCSetDeviceInfo(OCDeviceInfo deviceInfo)
2031 OC_LOG(INFO, TAG, PCF("Entering OCSetDeviceInfo"));
2033 if(myStackMode == OC_CLIENT)
2035 return OC_STACK_ERROR;
2038 return SaveDeviceInfo(deviceInfo);
2044 * @param handle - pointer to handle to newly created resource. Set by ocstack. Used to refer to resource
2045 * @param resourceTypeName - name of resource type. Example: "core.led"
2046 * @param resourceInterfaceName - name of resource interface. Example: "core.rw"
2047 * @param uri - URI of the resource. Example: "/a/led"
2048 * @param entityHandler - entity handler function that is called by ocstack to handle requests, etc
2049 * NULL for default entity handler
2050 * @param resourceProperties - properties supported by resource. Example: OC_DISCOVERABLE|OC_OBSERVABLE
2053 * OC_STACK_OK - no errors
2054 * OC_STACK_ERROR - stack process error
2056 OCStackResult OCCreateResource(OCResourceHandle *handle,
2057 const char *resourceTypeName,
2058 const char *resourceInterfaceName,
2059 const char *uri, OCEntityHandler entityHandler,
2060 uint8_t resourceProperties) {
2062 OCResource *pointer = NULL;
2065 OCStackResult result = OC_STACK_ERROR;
2067 OC_LOG(INFO, TAG, PCF("Entering OCCreateResource"));
2069 if(myStackMode == OC_CLIENT)
2073 // Validate parameters
2074 if(!uri || (strlen(uri) == 0))
2076 OC_LOG(ERROR, TAG, PCF("URI is invalid"));
2077 return OC_STACK_INVALID_URI;
2079 // Is it presented during resource discovery?
2080 if (!handle || !resourceTypeName) {
2081 OC_LOG(ERROR, TAG, PCF("Input parameter is NULL"));
2082 return OC_STACK_INVALID_PARAM;
2085 if(!resourceInterfaceName || strlen(resourceInterfaceName) == 0) {
2086 resourceInterfaceName = OC_RSRVD_INTERFACE_DEFAULT;
2089 // Make sure resourceProperties bitmask has allowed properties specified
2090 if (resourceProperties
2091 > (OC_ACTIVE | OC_DISCOVERABLE | OC_OBSERVABLE | OC_SLOW | OC_SECURE)) {
2092 OC_LOG(ERROR, TAG, PCF("Invalid property"));
2093 return OC_STACK_INVALID_PARAM;
2096 // If the headResource is NULL, then no resources have been created...
2097 pointer = headResource;
2099 // At least one resources is in the resource list, so we need to search for
2100 // repeated URLs, which are not allowed. If a repeat is found, exit with an error
2102 if (strcmp(uri, pointer->uri) == 0) {
2103 OC_LOG(ERROR, TAG, PCF("URI already in use"));
2104 return OC_STACK_INVALID_PARAM;
2106 pointer = pointer->next;
2109 // Create the pointer and insert it into the resource list
2110 pointer = (OCResource *) OCCalloc(1, sizeof(OCResource));
2114 pointer->sequenceNum = OC_OFFSET_SEQUENCE_NUMBER;
2116 insertResource(pointer);
2119 size = strlen(uri) + 1;
2120 str = (char *) OCMalloc(size);
2124 strncpy(str, uri, size);
2127 // Set properties. Set OC_ACTIVE
2128 pointer->resourceProperties = (OCResourceProperty) (resourceProperties
2131 // Add the resourcetype to the resource
2132 result = BindResourceTypeToResource(pointer, resourceTypeName);
2133 if (result != OC_STACK_OK) {
2134 OC_LOG(ERROR, TAG, PCF("Error adding resourcetype"));
2138 // Add the resourceinterface to the resource
2139 result = BindResourceInterfaceToResource(pointer, resourceInterfaceName);
2140 if (result != OC_STACK_OK) {
2141 OC_LOG(ERROR, TAG, PCF("Error adding resourceinterface"));
2145 // If an entity handler has been passed, attach it to the newly created
2146 // resource. Otherwise, set the default entity handler.
2149 pointer->entityHandler = entityHandler;
2153 pointer->entityHandler = defaultResourceEHandler;
2157 result = OC_STACK_OK;
2159 #ifdef WITH_PRESENCE
2160 if(presenceResource.handle)
2162 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2163 SendPresenceNotification(pointer->rsrcType);
2167 if (result != OC_STACK_OK)
2169 // Deep delete of resource and other dynamic elements that it contains
2170 deleteResource(pointer);
2179 * Create a resource. with host ip address for remote resource
2181 * @param handle - pointer to handle to newly created resource. Set by ocstack.
2182 * Used to refer to resource
2183 * @param resourceTypeName - name of resource type. Example: "core.led"
2184 * @param resourceInterfaceName - name of resource interface. Example: "core.rw"
2185 * @param host - HOST address of the remote resource. Example: "coap://xxx.xxx.xxx.xxx:xxxxx"
2186 * @param uri - URI of the resource. Example: "/a/led"
2187 * @param entityHandler - entity handler function that is called by ocstack to handle requests, etc
2188 * NULL for default entity handler
2189 * @param resourceProperties - properties supported by resource.
2190 * Example: OC_DISCOVERABLE|OC_OBSERVABLE
2193 * OC_STACK_OK - no errors
2194 * OC_STACK_ERROR - stack process error
2197 OCStackResult OCCreateResourceWithHost(OCResourceHandle *handle,
2198 const char *resourceTypeName,
2199 const char *resourceInterfaceName,
2202 OCEntityHandler entityHandler,
2203 uint8_t resourceProperties)
2207 OCStackResult result = OC_STACK_ERROR;
2209 result = OCCreateResource(handle, resourceTypeName, resourceInterfaceName,
2210 uri, entityHandler, resourceProperties);
2212 if (result != OC_STACK_ERROR)
2215 size = strlen(host) + 1;
2216 str = (char *) OCMalloc(size);
2219 return OC_STACK_ERROR;
2221 strncpy(str, host, size);
2222 ((OCResource *) *handle)->host = str;
2229 * Add a resource to a collection resource.
2231 * @param collectionHandle - handle to the collection resource
2232 * @param resourceHandle - handle to resource to be added to the collection resource
2235 * OC_STACK_OK - no errors
2236 * OC_STACK_ERROR - stack process error
2237 * OC_STACK_INVALID_PARAM - invalid collectionhandle
2239 OCStackResult OCBindResource(
2240 OCResourceHandle collectionHandle, OCResourceHandle resourceHandle) {
2241 OCResource *resource;
2244 OC_LOG(INFO, TAG, PCF("Entering OCBindResource"));
2246 // Validate parameters
2247 VERIFY_NON_NULL(collectionHandle, ERROR, OC_STACK_ERROR);
2248 VERIFY_NON_NULL(resourceHandle, ERROR, OC_STACK_ERROR);
2249 // Container cannot contain itself
2250 if (collectionHandle == resourceHandle) {
2251 OC_LOG(ERROR, TAG, PCF("Added handle equals collection handle"));
2252 return OC_STACK_INVALID_PARAM;
2255 // Use the handle to find the resource in the resource linked list
2256 resource = findResource((OCResource *) collectionHandle);
2258 OC_LOG(ERROR, TAG, PCF("Collection handle not found"));
2259 return OC_STACK_INVALID_PARAM;
2262 // Look for an open slot to add add the child resource.
2263 // If found, add it and return success
2264 for (i = 0; i < MAX_CONTAINED_RESOURCES; i++) {
2265 if (!resource->rsrcResources[i]) {
2266 resource->rsrcResources[i] = (OCResource *) resourceHandle;
2267 OC_LOG(INFO, TAG, PCF("resource bound"));
2272 #ifdef WITH_PRESENCE
2273 if(presenceResource.handle)
2275 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2276 SendPresenceNotification(((OCResource *) resourceHandle)->rsrcType);
2280 // Unable to add resourceHandle, so return error
2281 return OC_STACK_ERROR;
2285 * Remove a resource from a collection resource.
2287 * @param collectionHandle - handle to the collection resource
2288 * @param resourceHandle - handle to resource to be added to the collection resource
2291 * OC_STACK_OK - no errors
2292 * OC_STACK_ERROR - stack process error
2293 * OC_STACK_INVALID_PARAM - invalid collectionHandle
2295 OCStackResult OCUnBindResource(
2296 OCResourceHandle collectionHandle, OCResourceHandle resourceHandle) {
2297 OCResource *resource;
2300 OC_LOG(INFO, TAG, PCF("Entering OCUnBindResource"));
2302 // Validate parameters
2303 VERIFY_NON_NULL(collectionHandle, ERROR, OC_STACK_ERROR);
2304 VERIFY_NON_NULL(resourceHandle, ERROR, OC_STACK_ERROR);
2305 // Container cannot contain itself
2306 if (collectionHandle == resourceHandle) {
2307 OC_LOG(ERROR, TAG, PCF("removing handle equals collection handle"));
2308 return OC_STACK_INVALID_PARAM;
2311 // Use the handle to find the resource in the resource linked list
2312 resource = findResource((OCResource *) collectionHandle);
2314 OC_LOG(ERROR, TAG, PCF("Collection handle not found"));
2315 return OC_STACK_INVALID_PARAM;
2318 // Look for an open slot to add add the child resource.
2319 // If found, add it and return success
2320 for (i = 0; i < MAX_CONTAINED_RESOURCES; i++) {
2321 if (resourceHandle == resource->rsrcResources[i]) {
2322 resource->rsrcResources[i] = (OCResource *) NULL;
2323 OC_LOG(INFO, TAG, PCF("resource unbound"));
2328 OC_LOG(INFO, TAG, PCF("resource not found in collection"));
2330 #ifdef WITH_PRESENCE
2331 if(presenceResource.handle)
2333 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2334 SendPresenceNotification(((OCResource *) resourceHandle)->rsrcType);
2338 // Unable to add resourceHandle, so return error
2339 return OC_STACK_ERROR;
2342 OCStackResult BindResourceTypeToResource(OCResource* resource,
2343 const char *resourceTypeName)
2345 OCResourceType *pointer = NULL;
2348 OCStackResult result = OC_STACK_ERROR;
2350 OC_LOG(INFO, TAG, PCF("Entering BindResourceTypeToResource"));
2352 // Validate parameters
2353 VERIFY_NON_NULL(resourceTypeName, ERROR, OC_STACK_INVALID_PARAM);
2354 // TODO: Does resource attribute resentation really have to be maintained in stack?
2355 // Is it presented during resource discovery?
2357 TODO ("Make sure that the resourcetypename doesn't already exist in the resource");
2359 // Create the resourcetype and insert it into the resource list
2360 pointer = (OCResourceType *) OCCalloc(1, sizeof(OCResourceType));
2365 // Set the resourceTypeName
2366 size = strlen(resourceTypeName) + 1;
2367 str = (char *) OCMalloc(size);
2371 strncpy(str, resourceTypeName, size);
2372 pointer->resourcetypename = str;
2374 insertResourceType(resource, pointer);
2375 result = OC_STACK_OK;
2377 exit: if (result != OC_STACK_OK) {
2385 OCStackResult BindResourceInterfaceToResource(OCResource* resource,
2386 const char *resourceInterfaceName)
2388 OCResourceInterface *pointer = NULL;
2391 OCStackResult result = OC_STACK_ERROR;
2393 OC_LOG(INFO, TAG, PCF("Entering BindResourceInterfaceToResource"));
2395 // Validate parameters
2396 VERIFY_NON_NULL(resourceInterfaceName, ERROR, OC_STACK_INVALID_PARAM);
2398 TODO ("Make sure that the resourceinterface name doesn't already exist in the resource");
2400 // Create the resourceinterface and insert it into the resource list
2401 pointer = (OCResourceInterface *) OCCalloc(1, sizeof(OCResourceInterface));
2406 // Set the resourceinterface name
2407 size = strlen(resourceInterfaceName) + 1;
2408 str = (char *) OCMalloc(size);
2412 strncpy(str, resourceInterfaceName, size);
2413 pointer->name = str;
2415 // Bind the resourceinterface to the resource
2416 insertResourceInterface(resource, pointer);
2418 result = OC_STACK_OK;
2420 exit: if (result != OC_STACK_OK) {
2429 * Bind a resourcetype to a resource.
2431 * @param handle - handle to the resource
2432 * @param resourceTypeName - name of resource type. Example: "core.led"
2435 * OC_STACK_OK - no errors
2436 * OC_STACK_ERROR - stack process error
2438 OCStackResult OCBindResourceTypeToResource(OCResourceHandle handle,
2439 const char *resourceTypeName) {
2441 OCStackResult result = OC_STACK_ERROR;
2442 OCResource *resource;
2444 // Make sure resource exists
2445 resource = findResource((OCResource *) handle);
2447 OC_LOG(ERROR, TAG, PCF("Resource not found"));
2448 return OC_STACK_ERROR;
2451 // call internal function
2452 result = BindResourceTypeToResource(resource, resourceTypeName);
2454 #ifdef WITH_PRESENCE
2455 if(presenceResource.handle)
2457 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2458 SendPresenceNotification(resource->rsrcType);
2466 * Bind a resourceinterface to a resource.
2468 * @param handle - handle to the resource
2469 * @param resourceInterfaceName - name of resource interface. Example: "oc.mi.b"
2472 * OC_STACK_OK - no errors
2473 * OC_STACK_ERROR - stack process error
2476 OCStackResult OCBindResourceInterfaceToResource(OCResourceHandle handle,
2477 const char *resourceInterfaceName) {
2479 OCStackResult result = OC_STACK_ERROR;
2480 OCResource *resource;
2482 // Make sure resource exists
2483 resource = findResource((OCResource *) handle);
2485 OC_LOG(ERROR, TAG, PCF("Resource not found"));
2486 return OC_STACK_ERROR;
2489 // call internal function
2490 result = BindResourceInterfaceToResource(resource, resourceInterfaceName);
2492 #ifdef WITH_PRESENCE
2493 if(presenceResource.handle)
2495 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2496 SendPresenceNotification(resource->rsrcType);
2504 * Get the number of resources that have been created in the stack.
2506 * @param numResources - pointer to count variable
2509 * OC_STACK_OK - no errors
2510 * OC_STACK_ERROR - stack process error
2513 OCStackResult OCGetNumberOfResources(uint8_t *numResources) {
2514 OCResource *pointer = headResource;
2516 OC_LOG(INFO, TAG, PCF("Entering OCGetNumberOfResources"));
2517 VERIFY_NON_NULL(numResources, ERROR, OC_STACK_INVALID_PARAM);
2520 *numResources = *numResources + 1;
2521 pointer = pointer->next;
2527 * Get a resource handle by index.
2529 * @param index - index of resource, 0 to Count - 1
2532 * Resource handle - if found
2533 * NULL - if not found
2535 OCResourceHandle OCGetResourceHandle(uint8_t index) {
2536 OCResource *pointer = headResource;
2539 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceHandle"));
2541 // Iterate through the list
2542 while ((i < index) && pointer) {
2544 pointer = pointer->next;
2546 return (OCResourceHandle) pointer;
2550 * Delete resource specified by handle. Deletes resource and all resourcetype and resourceinterface
2553 * @param handle - handle of resource to be deleted
2556 * OC_STACK_OK - no errors
2557 * OC_STACK_ERROR - stack process error
2558 * OC_STACK_NO_RESOURCE - resource not found
2559 * OC_STACK_INVALID_PARAM - invalid param
2561 OCStackResult OCDeleteResource(OCResourceHandle handle) {
2562 OC_LOG(INFO, TAG, PCF("Entering OCDeleteResource"));
2565 OC_LOG(ERROR, TAG, PCF("Invalid param"));
2566 return OC_STACK_INVALID_PARAM;
2569 OCResource *resource = findResource((OCResource *) handle);
2570 if (resource == NULL) {
2571 OC_LOG(ERROR, TAG, PCF("Resource not found"));
2572 return OC_STACK_NO_RESOURCE;
2575 if (deleteResource((OCResource *) handle) == 0) {
2576 OC_LOG(ERROR, TAG, PCF("Error deleting resource"));
2577 return OC_STACK_ERROR;
2584 * Get the URI of the resource specified by handle.
2586 * @param handle - handle of resource
2588 * URI string - if resource found
2589 * NULL - resource not found
2591 const char *OCGetResourceUri(OCResourceHandle handle) {
2592 OCResource *resource;
2593 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceUri"));
2595 resource = findResource((OCResource *) handle);
2597 return resource->uri;
2599 return (const char *) NULL;
2603 * Get the properties of the resource specified by handle.
2604 * NOTE: that after a resource is created, the OC_ACTIVE property is set
2605 * for the resource by the stack.
2607 * @param handle - handle of resource
2609 * property bitmap - if resource found
2610 * NULL - resource not found
2612 uint8_t OCGetResourceProperties(OCResourceHandle handle) {
2613 OCResource *resource;
2614 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceProperties"));
2616 resource = findResource((OCResource *) handle);
2618 return resource->resourceProperties;
2624 * Get the number of resource types of the resource.
2626 * @param handle - handle of resource
2627 * @param numResourceTypes - pointer to count variable
2630 * OC_STACK_OK - no errors
2631 * OC_STACK_ERROR - stack process error
2633 OCStackResult OCGetNumberOfResourceTypes(OCResourceHandle handle,
2634 uint8_t *numResourceTypes) {
2635 OCResource *resource;
2636 OCResourceType *pointer;
2638 OC_LOG(INFO, TAG, PCF("Entering OCGetNumberOfResourceTypes"));
2639 VERIFY_NON_NULL(numResourceTypes, ERROR, OC_STACK_INVALID_PARAM);
2640 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
2642 *numResourceTypes = 0;
2644 resource = findResource((OCResource *) handle);
2646 pointer = resource->rsrcType;
2648 *numResourceTypes = *numResourceTypes + 1;
2649 pointer = pointer->next;
2656 * Get name of resource type of the resource.
2658 * @param handle - handle of resource
2659 * @param index - index of resource, 0 to Count - 1
2662 * resource type name - if resource found
2663 * NULL - resource not found
2665 const char *OCGetResourceTypeName(OCResourceHandle handle, uint8_t index) {
2666 OCResourceType *resourceType;
2668 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceTypeName"));
2670 resourceType = findResourceTypeAtIndex(handle, index);
2672 return resourceType->resourcetypename;
2674 return (const char *) NULL;
2680 * Get the number of resource interfaces of the resource.
2682 * @param handle - handle of resource
2683 * @param numResources - pointer to count variable
2686 * OC_STACK_OK - no errors
2687 * OC_STACK_ERROR - stack process error
2689 OCStackResult OCGetNumberOfResourceInterfaces(OCResourceHandle handle,
2690 uint8_t *numResourceInterfaces) {
2691 OCResourceInterface *pointer;
2692 OCResource *resource;
2694 OC_LOG(INFO, TAG, PCF("Entering OCGetNumberOfResourceInterfaces"));
2696 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
2697 VERIFY_NON_NULL(numResourceInterfaces, ERROR, OC_STACK_INVALID_PARAM);
2699 *numResourceInterfaces = 0;
2700 resource = findResource((OCResource *) handle);
2702 pointer = resource->rsrcInterface;
2704 *numResourceInterfaces = *numResourceInterfaces + 1;
2705 pointer = pointer->next;
2712 * Get name of resource interface of the resource.
2714 * @param handle - handle of resource
2715 * @param index - index of resource, 0 to Count - 1
2718 * resource interface name - if resource found
2719 * NULL - resource not found
2721 const char *OCGetResourceInterfaceName(OCResourceHandle handle, uint8_t index) {
2722 OCResourceInterface *resourceInterface;
2724 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceInterfaceName"));
2726 resourceInterface = findResourceInterfaceAtIndex(handle, index);
2727 if (resourceInterface) {
2728 return resourceInterface->name;
2730 return (const char *) NULL;
2734 * Get resource handle from the collection resource by index.
2736 * @param collectionHandle - handle of collection resource
2737 * @param index - index of contained resource, 0 to Count - 1
2740 * handle to resource - if resource found
2741 * NULL - resource not found
2743 OCResourceHandle OCGetResourceHandleFromCollection(OCResourceHandle collectionHandle,
2745 OCResource *resource;
2747 OC_LOG(INFO, TAG, PCF("Entering OCGetContainedResource"));
2749 if (index >= MAX_CONTAINED_RESOURCES) {
2753 resource = findResource((OCResource *) collectionHandle);
2758 return resource->rsrcResources[index];
2762 * Bind an entity handler to the resource.
2764 * @param handle - handle to the resource that the contained resource is to be bound
2765 * @param entityHandler - entity handler function that is called by ocstack to handle requests, etc
2767 * OC_STACK_OK - no errors
2768 * OC_STACK_ERROR - stack process error
2770 OCStackResult OCBindResourceHandler(OCResourceHandle handle,
2771 OCEntityHandler entityHandler) {
2772 OCResource *resource;
2774 OC_LOG(INFO, TAG, PCF("Entering OCBindResourceHandler"));
2776 // Validate parameters
2777 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
2778 //VERIFY_NON_NULL(entityHandler, ERROR, OC_STACK_INVALID_PARAM);
2780 // Use the handle to find the resource in the resource linked list
2781 resource = findResource((OCResource *)handle);
2783 OC_LOG(ERROR, TAG, PCF("Resource not found"));
2784 return OC_STACK_ERROR;
2788 resource->entityHandler = entityHandler;
2790 #ifdef WITH_PRESENCE
2791 if(presenceResource.handle)
2793 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2794 SendPresenceNotification(resource->rsrcType);
2802 * Get the entity handler for a resource.
2804 * @param handle - handle of resource
2807 * entity handler - if resource found
2808 * NULL - resource not found
2810 OCEntityHandler OCGetResourceHandler(OCResourceHandle handle) {
2811 OCResource *resource;
2813 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceHandler"));
2815 // Use the handle to find the resource in the resource linked list
2816 resource = findResource((OCResource *)handle);
2818 OC_LOG(ERROR, TAG, PCF("Resource not found"));
2823 return resource->entityHandler;
2826 void incrementSequenceNumber(OCResource * resPtr)
2828 // Increment the sequence number
2829 resPtr->sequenceNum += 1;
2830 if (resPtr->sequenceNum == MAX_SEQUENCE_NUMBER)
2832 resPtr->sequenceNum = OC_OFFSET_SEQUENCE_NUMBER+1;
2838 * Notify Presence subscribers that a resource has been modified
2840 * @param resourceType - Handle to the resourceType linked list of resource
2841 * that was modified.
2842 * @param qos - Quality Of Service
2845 #ifdef WITH_PRESENCE
2846 OCStackResult SendPresenceNotification(OCResourceType *resourceType)
2848 OCResource *resPtr = NULL;
2849 OCStackResult result;
2850 OCMethod method = OC_REST_PRESENCE;
2851 uint32_t maxAge = 0;
2852 resPtr = findResource((OCResource *) presenceResource.handle);
2855 return OC_STACK_NO_RESOURCE;
2857 if((((OCResource *) presenceResource.handle)->resourceProperties) & OC_ACTIVE)
2859 maxAge = presenceResource.presenceTTL;
2866 result = SendAllObserverNotification(method, resPtr, maxAge, resourceType, OC_LOW_QOS);
2870 #endif // WITH_PRESENCE
2872 * Notify observers that an observed value has changed.
2874 * @param handle - handle of resource
2877 * OC_STACK_OK - no errors
2878 * OC_STACK_NO_RESOURCE - invalid resource handle
2879 * OC_STACK_NO_OBSERVERS - no more observers intrested in resource
2881 OCStackResult OCNotifyAllObservers(OCResourceHandle handle, OCQualityOfService qos) {
2883 OC_LOG(INFO, TAG, PCF("Entering OCNotifyAllObservers"));
2885 OCResource *resPtr = NULL;
2886 OCStackResult result;
2887 OCMethod method = OC_REST_NOMETHOD;
2888 uint32_t maxAge = 0;
2890 OC_LOG(INFO, TAG, PCF("Entering OCNotifyAllObservers"));
2891 #ifdef WITH_PRESENCE
2892 if(handle == presenceResource.handle)
2896 #endif // WITH_PRESENCE
2897 VERIFY_NON_NULL(handle, ERROR, OC_STACK_ERROR);
2899 // Verify that the resource exists
2900 resPtr = findResource ((OCResource *) handle);
2903 return OC_STACK_NO_RESOURCE;
2907 //only increment in the case of regular observing (not presence)
2908 incrementSequenceNumber(resPtr);
2909 method = OC_REST_OBSERVE;
2910 maxAge = MAX_OBSERVE_AGE;
2911 #ifdef WITH_PRESENCE
2912 result = SendAllObserverNotification (method, resPtr, maxAge, NULL, qos);
2914 result = SendAllObserverNotification (method, resPtr, maxAge, qos);
2921 OCNotifyListOfObservers (OCResourceHandle handle,
2922 OCObservationId *obsIdList,
2923 uint8_t numberOfIds,
2924 unsigned char *notificationJSONPayload,
2925 OCQualityOfService qos)
2927 OC_LOG(INFO, TAG, PCF("Entering OCNotifyListOfObservers"));
2929 OCResource *resPtr = NULL;
2930 //TODO: we should allow the server to define this
2931 uint32_t maxAge = MAX_OBSERVE_AGE;
2933 VERIFY_NON_NULL(handle, ERROR, OC_STACK_ERROR);
2934 VERIFY_NON_NULL(obsIdList, ERROR, OC_STACK_ERROR);
2935 VERIFY_NON_NULL(notificationJSONPayload, ERROR, OC_STACK_ERROR);
2937 // Verify that the resource exists
2938 resPtr = findResource ((OCResource *) handle);
2939 if (NULL == resPtr || myStackMode == OC_CLIENT)
2941 return OC_STACK_NO_RESOURCE;
2945 incrementSequenceNumber(resPtr);
2947 return (SendListObserverNotification(resPtr, obsIdList, numberOfIds,
2948 notificationJSONPayload, maxAge, qos));
2952 * Send a response to a request.
2953 * The response can be a regular, slow, or block (i.e. a response that
2954 * is too large to be sent in a single PDU and must span multiple transmissions)
2956 * @param response - pointer to structure that contains response parameters
2959 * OC_STACK_OK - No errors; Success
2960 * OC_STACK_INVALID_PARAM - Invalid pointer to OCServerResponse
2961 * OC_STACK_INVALID_REQUEST_HANDLE - Request handle not found
2962 * OC_STACK_PERSISTENT_BUFFER_REQUIRED - Block transfer needed for response, so a
2963 * persistent response buffer is necessary
2965 OCStackResult OCDoResponse(OCEntityHandlerResponse *ehResponse)
2967 OCStackResult result = OC_STACK_ERROR;
2968 OCServerRequest *serverRequest = NULL;
2970 OC_LOG(INFO, TAG, PCF("Entering OCDoResponse"));
2972 // Validate input parameters
2973 VERIFY_NON_NULL(ehResponse, ERROR, OC_STACK_INVALID_PARAM);
2974 VERIFY_NON_NULL(ehResponse->requestHandle, ERROR, OC_STACK_INVALID_PARAM);
2976 // TODO: Placeholder for creating a response entry when implementing
2977 // block transfer feature
2979 // If a response payload is present, check if block transfer is required
2980 if (ehResponse->payload && OCIsPacketTransferRequired(NULL,
2981 (const char *)ehResponse->payload, ehResponse->payloadSize))
2983 OC_LOG(INFO, TAG, PCF("Block transfer required"));
2985 // Persistent response buffer is needed for block transfer
2986 if (!ehResponse->persistentBufferFlag)
2988 OC_LOG(WARNING, TAG, PCF("Persistent response buffer required"));
2989 return OC_STACK_PERSISTENT_BUFFER_REQUIRED;
2991 // TODO: Placeholder for block transfer handling
2992 // TODO: Placeholder for setting the the response handle in the OCServerResponse struct
2993 // when implementing the block transfer feature
2998 // Get pointer to request info
2999 serverRequest = GetServerRequestUsingHandle((OCServerRequest *)ehResponse->requestHandle);
3002 result = serverRequest->ehResponseHandler(ehResponse);
3009 * Cancel a response. Applies to a block response
3011 * @param responseHandle - response handle set by stack in OCServerResponse after
3012 * OCDoResponse is called
3015 * OC_STACK_OK - No errors; Success
3016 * OC_STACK_INVALID_PARAM - The handle provided is invalid.
3018 OCStackResult OCCancelResponse(OCResponseHandle responseHandle)
3020 OCStackResult result = OC_STACK_NOTIMPL;
3022 OC_LOG(INFO, TAG, PCF("Entering OCCancelResponse"));
3024 // TODO: validate response handle
3029 //-----------------------------------------------------------------------------
3030 // Private internal function definitions
3031 //-----------------------------------------------------------------------------
3033 * Generate handle of OCDoResource invocation for callback management.
3035 static OCDoHandle GenerateInvocationHandle()
3037 OCDoHandle handle = NULL;
3038 // Generate token here, it will be deleted when the transaction is deleted
3039 handle = (OCDoHandle) OCMalloc(sizeof(uint8_t[MAX_TOKEN_LENGTH]));
3042 OCFillRandomMem((uint8_t*)handle, sizeof(uint8_t[MAX_TOKEN_LENGTH]));
3047 #ifdef WITH_PRESENCE
3048 OCStackResult OCChangeResourceProperty(OCResourceProperty * inputProperty,
3049 OCResourceProperty resourceProperties, uint8_t enable)
3051 if (resourceProperties
3052 > (OC_ACTIVE | OC_DISCOVERABLE | OC_OBSERVABLE | OC_SLOW)) {
3053 OC_LOG(ERROR, TAG, PCF("Invalid property"));
3054 return OC_STACK_INVALID_PARAM;
3058 *inputProperty = (OCResourceProperty) (*inputProperty & ~(resourceProperties));
3062 *inputProperty = (OCResourceProperty) (*inputProperty | resourceProperties);
3069 * Initialize resource data structures, variables, etc.
3071 OCStackResult initResources() {
3072 OCStackResult result = OC_STACK_OK;
3073 // Init application resource vars
3074 headResource = NULL;
3075 // Init Virtual Resources
3076 #ifdef WITH_PRESENCE
3077 presenceResource.presenceTTL = OC_DEFAULT_PRESENCE_TTL;
3078 //presenceResource.token = OCGenerateCoAPToken();
3079 result = OCCreateResource(&presenceResource.handle,
3080 OC_RSRVD_RESOURCE_TYPE_PRESENCE,
3085 //make resource inactive
3086 result = OCChangeResourceProperty(
3087 &(((OCResource *) presenceResource.handle)->resourceProperties),
3094 * Add a resource to the end of the linked list of resources.
3096 * @param resource - resource to be added
3098 void insertResource(OCResource *resource) {
3099 OCResource *pointer;
3101 if (!headResource) {
3102 headResource = resource;
3104 pointer = headResource;
3106 while (pointer->next) {
3107 pointer = pointer->next;
3109 pointer->next = resource;
3111 resource->next = NULL;
3115 * Find a resource in the linked list of resources.
3117 * @param resource - resource to be found
3119 * NULL - resource not found
3120 * pointer to resource - pointer to resource that was found in the linked list
3122 OCResource *findResource(OCResource *resource) {
3123 OCResource *pointer = headResource;
3126 if (pointer == resource) {
3129 pointer = pointer->next;
3134 void deleteAllResources()
3136 OCResource *pointer = headResource;
3141 temp = pointer->next;
3142 #ifdef WITH_PRESENCE
3143 if(pointer != (OCResource *) presenceResource.handle)
3145 #endif // WITH_PRESENCE
3146 deleteResource(pointer);
3147 #ifdef WITH_PRESENCE
3149 #endif // WITH_PRESENCE
3153 #ifdef WITH_PRESENCE
3154 // Ensure that the last resource to be deleted is the presence resource. This allows for all
3155 // presence notification attributed to their deletion to be processed.
3156 deleteResource((OCResource *) presenceResource.handle);
3157 #endif // WITH_PRESENCE
3161 * Delete the resource from the linked list.
3163 * @param resource - resource to be deleted
3168 int deleteResource(OCResource *resource) {
3169 OCResource *prev = NULL;
3172 temp = headResource;
3174 if (temp == resource) {
3175 // Invalidate all Resource Properties.
3176 resource->resourceProperties = (OCResourceProperty) 0;
3177 #ifdef WITH_PRESENCE
3178 if(resource != (OCResource *) presenceResource.handle)
3180 #endif // WITH_PRESENCE
3181 OCNotifyAllObservers((OCResourceHandle)resource, OC_HIGH_QOS);
3182 #ifdef WITH_PRESENCE
3185 if(presenceResource.handle)
3187 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
3188 if(resource != (OCResource *) presenceResource.handle)
3190 SendPresenceNotification(resource->rsrcType);
3194 SendPresenceNotification(NULL);
3199 if (temp == headResource) {
3200 headResource = temp->next;
3202 prev->next = temp->next;
3205 deleteResourceElements(temp);
3218 * Delete all of the dynamically allocated elements that were created for the resource.
3220 * @param resource - specified resource
3222 void deleteResourceElements(OCResource *resource) {
3228 OCFree(resource->uri);
3230 // Delete resourcetype linked list
3231 deleteResourceType(resource->rsrcType);
3233 // Delete resourceinterface linked list
3234 deleteResourceInterface(resource->rsrcInterface);
3238 * Delete all of the dynamically allocated elements that were created for the resource type.
3240 * @param resourceType - specified resource type
3242 void deleteResourceType(OCResourceType *resourceType) {
3243 OCResourceType *pointer = resourceType;
3244 OCResourceType *next;
3247 next = pointer->next;
3248 OCFree(pointer->resourcetypename);
3255 * Delete all of the dynamically allocated elements that were created for the resource interface.
3257 * @param resourceInterface - specified resource interface
3259 void deleteResourceInterface(OCResourceInterface *resourceInterface) {
3260 OCResourceInterface *pointer = resourceInterface;
3261 OCResourceInterface *next;
3264 next = pointer->next;
3265 OCFree(pointer->name);
3272 * Insert a resource type into a resource's resource type linked list.
3274 * @param resource - resource where resource type is to be inserted
3275 * @param resourceType - resource type to be inserted
3277 void insertResourceType(OCResource *resource, OCResourceType *resourceType) {
3278 OCResourceType *pointer;
3280 if (resource && !resource->rsrcType) {
3281 resource->rsrcType = resourceType;
3285 pointer = resource->rsrcType;
3289 pointer = resourceType;
3291 while (pointer->next) {
3292 pointer = pointer->next;
3294 pointer->next = resourceType;
3296 resourceType->next = NULL;
3300 * Get a resource type at the specified index within a resource.
3302 * @param handle - handle of resource
3303 * @param index - index of resource type
3306 * resourcetype - if found
3309 OCResourceType *findResourceTypeAtIndex(OCResourceHandle handle, uint8_t index) {
3310 OCResource *resource;
3311 OCResourceType *pointer;
3314 // Find the specified resource
3315 resource = findResource((OCResource *) handle);
3320 // Make sure a resource has a resourcetype
3321 if (!resource->rsrcType) {
3325 // Iterate through the list
3326 pointer = resource->rsrcType;
3328 while ((i < index) && pointer) {
3330 pointer = pointer->next;
3336 * Finds a resource type in an OCResourceType link-list.
3338 * @param resourceTypeList - the link-list to be searched through
3339 * @param resourceTypeName - the key to search for
3342 * resourceType that matches the key (ie. resourceTypeName)
3343 * NULL - either an invalid parameter or this function was unable to find the key.
3345 OCResourceType *findResourceType(OCResourceType * resourceTypeList, const char * resourceTypeName)
3347 if(resourceTypeList && resourceTypeName)
3349 OCResourceType * rtPointer = resourceTypeList;
3350 while(resourceTypeName && rtPointer)
3352 if(rtPointer->resourcetypename &&
3353 strcmp(resourceTypeName, (const char *)
3354 (rtPointer->resourcetypename)) == 0)
3358 rtPointer = rtPointer->next;
3365 * Insert a resource interface into a resource's resource interface linked list.
3367 * @param resource - resource where resource interface is to be inserted
3368 * @param resourceInterface - resource interface to be inserted
3370 void insertResourceInterface(OCResource *resource,
3371 OCResourceInterface *resourceInterface) {
3372 OCResourceInterface *pointer;
3374 if (!resource->rsrcInterface) {
3375 resource->rsrcInterface = resourceInterface;
3377 pointer = resource->rsrcInterface;
3378 while (pointer->next) {
3379 pointer = pointer->next;
3381 pointer->next = resourceInterface;
3383 resourceInterface->next = NULL;
3387 * Get a resource interface at the specified index within a resource.
3389 * @param handle - handle of resource
3390 * @param index - index of resource interface
3393 * resourceinterface - if found
3396 OCResourceInterface *findResourceInterfaceAtIndex(OCResourceHandle handle,
3398 OCResource *resource;
3399 OCResourceInterface *pointer;
3402 // Find the specified resource
3403 resource = findResource((OCResource *) handle);
3408 // Make sure a resource has a resourceinterface
3409 if (!resource->rsrcInterface) {
3413 // Iterate through the list
3414 pointer = resource->rsrcInterface;
3416 while ((i < index) && pointer) {
3418 pointer = pointer->next;
3424 * Determine if a request/response must be sent in a block transfer because it is too large to be
3425 * sent in a single PDU. This function can be used for either a request or a response
3427 * @param request - NULL or pointer to request
3428 * @param response - NULL or pointer to response
3429 * @param size - 0 or size of the request/response. If 0, strlen is used for determining
3430 * the length of the request/response
3433 * 0 - packet transfer NOT required (i.e. normal request/response)
3434 * 1 - packet transfer required (i.e. block transfer needed)
3436 uint8_t OCIsPacketTransferRequired(const char *request, const char *response, uint16_t size)
3440 // Determine if we are checking a request or a response
3443 // If size is greater than 0, use it for the request size value, otherwise
3444 // assume request is null terminated and use strlen for size value
3445 if ((size > MAX_REQUEST_LENGTH) || (strlen(request) > MAX_REQUEST_LENGTH))
3452 // If size is greater than 0, use it for the response size value, otherwise
3453 // assume response is null terminated and use strlen for size value
3454 if ((size > MAX_RESPONSE_LENGTH) || (strlen(response) > MAX_RESPONSE_LENGTH))
3463 * Retrieves a resource type based upon a query ontains only just one
3464 * resource attribute (and that has to be of type "rt").
3466 * @remark This API malloc's memory for the resource type. Do not malloc resourceType
3467 * before passing in.
3469 * @param query - The quert part of the URI
3470 * @param resourceType - The resource type to be populated; pass by reference.
3473 * OC_STACK_INVALID_PARAM - Returns this if the resourceType parameter is invalid/NULL.
3474 * OC_STACK_OK - Success
3476 OCStackResult getResourceType(const char * query, unsigned char** resourceType)
3480 return OC_STACK_INVALID_PARAM;
3483 OCStackResult result = OC_STACK_ERROR;
3485 if(strncmp(query, "rt=", 3) == 0)
3487 *resourceType = (unsigned char *) OCMalloc(strlen(query)-3);
3490 result = OC_STACK_NO_MEMORY;
3493 strcpy((char *)*resourceType, ((const char *)&query[3]));
3494 result = OC_STACK_OK;
3500 OCStackResult getQueryFromUri(const char * uri, unsigned char** query, char ** newURI)
3504 return OC_STACK_INVALID_URI;
3506 if(!query || !newURI)
3508 return OC_STACK_INVALID_PARAM;
3510 char * leftToken = NULL;
3511 char * tempURI = (char *) OCMalloc(strlen(uri) + 1);
3516 strcpy(tempURI, uri);
3518 leftToken = strtok_r((char *)tempURI, "?", &strTokPtr);
3520 //TODO-CA: This could be simplified. Clean up required.
3521 while(leftToken != NULL)
3523 if(strncmp(leftToken, "rt=", 3) == 0 || strncmp(leftToken, "if=", 3) == 0)
3525 *query = (unsigned char *) OCMalloc(strlen(leftToken));
3531 strcpy((char *)*query, ((const char *)&leftToken[0]));
3534 leftToken = strtok_r(NULL, "?", &strTokPtr);
3542 return OC_STACK_NO_MEMORY;
3545 const ServerID OCGetServerInstanceID(void)
3547 static bool generated = false;
3548 static ServerID sid;
3555 sid = OCGetRandom();
3560 const char* OCGetServerInstanceIDString(void)
3562 // max printed length of a base 10
3563 // uint32 is 10 characters, so 11 includes null.
3564 // This will change as the representation gets switched
3566 static char buffer[11];
3567 int n = sprintf(buffer, "%u", OCGetServerInstanceID());