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"
41 #include "cainterface.h"
44 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
48 OC_STACK_UNINITIALIZED = 0, OC_STACK_INITIALIZED, OC_STACK_UNINIT_IN_PROGRESS
53 OC_PRESENCE_UNINITIALIZED = 0, OC_PRESENCE_INITIALIZED
57 //-----------------------------------------------------------------------------
59 //-----------------------------------------------------------------------------
60 static OCStackState stackState = OC_STACK_UNINITIALIZED;
62 OCResource *headResource = NULL;
64 static OCPresenceState presenceState = OC_PRESENCE_UNINITIALIZED;
65 static PresenceResource presenceResource;
66 uint8_t PresenceTimeOutSize = 0;
67 uint32_t PresenceTimeOut[] = {50, 75, 85, 95, 100};
71 OCDeviceEntityHandler defaultDeviceHandler;
72 OCStackResult getQueryFromUri(const char * uri, unsigned char** resourceType, char ** newURI);
74 //-----------------------------------------------------------------------------
76 //-----------------------------------------------------------------------------
77 #define TAG PCF("OCStack")
78 #define VERIFY_SUCCESS(op, successCode) { if (op != successCode) \
79 {OC_LOG_V(FATAL, TAG, "%s failed!!", #op); goto exit;} }
80 #define VERIFY_NON_NULL(arg, logLevel, retVal) { if (!(arg)) { OC_LOG((logLevel), \
81 TAG, PCF(#arg " is NULL")); return (retVal); } }
82 #define VERIFY_NON_NULL_V(arg) { if (!arg) {OC_LOG_V(FATAL, TAG, "%s is NULL", #arg);\
85 //TODO: we should allow the server to define this
86 #define MAX_OBSERVE_AGE (0x2FFFFUL)
88 //-----------------------------------------------------------------------------
90 //-----------------------------------------------------------------------------
91 extern void DeinitOCSecurityInfo();
93 //-----------------------------------------------------------------------------
94 // Internal API function
95 //-----------------------------------------------------------------------------
97 // This internal function is called to update the stack with the status of
98 // observers and communication failures
99 OCStackResult OCStackFeedBack(OCCoAPToken * token, uint8_t status)
101 OCStackResult result = OC_STACK_ERROR;
102 ResourceObserver * observer = NULL;
103 OCEntityHandlerRequest ehRequest = {0};
107 case OC_OBSERVER_NOT_INTERESTED:
108 OC_LOG(DEBUG, TAG, PCF("observer is not interested in our notifications anymore"));
110 observer = GetObserverUsingToken (token->token);
112 observer = GetObserverUsingToken (token);
116 result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) NULL,
117 OC_REST_NOMETHOD, (OCResourceHandle) NULL, NULL, NULL, 0,
118 NULL, OC_OBSERVE_DEREGISTER, observer->observeId);
119 if(result != OC_STACK_OK)
123 observer->resource->entityHandler(OC_OBSERVE_FLAG, &ehRequest);
125 //observer is not observing anymore
127 result = DeleteObserverUsingToken (token->token);
129 result = DeleteObserverUsingToken (token);
131 if(result == OC_STACK_OK)
133 OC_LOG(DEBUG, TAG, PCF("Removed observer successfully"));
137 result = OC_STACK_OK;
138 OC_LOG(DEBUG, TAG, PCF("Observer Removal failed"));
141 case OC_OBSERVER_STILL_INTERESTED:
142 //observer is still interested
143 OC_LOG(DEBUG, TAG, PCF("observer is interested in our \
144 notifications, reset the failedCount"));
146 observer = GetObserverUsingToken (token->token);
148 observer = GetObserverUsingToken (token);
152 observer->forceHighQos = 0;
153 observer->failedCommCount = 0;
154 result = OC_STACK_OK;
158 result = OC_STACK_OBSERVER_NOT_FOUND;
161 case OC_OBSERVER_FAILED_COMM:
162 //observer is not reachable
163 OC_LOG(DEBUG, TAG, PCF("observer is unreachable"));
165 observer = GetObserverUsingToken (token->token);
167 observer = GetObserverUsingToken (token);
171 if(observer->failedCommCount >= MAX_OBSERVER_FAILED_COMM)
173 result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) NULL,
174 OC_REST_NOMETHOD, (OCResourceHandle) NULL, NULL, NULL, 0,
175 NULL, OC_OBSERVE_DEREGISTER, observer->observeId);
176 if(result != OC_STACK_OK)
178 return OC_STACK_ERROR;
180 observer->resource->entityHandler(OC_OBSERVE_FLAG, &ehRequest);
181 //observer is unreachable
182 result = DeleteObserverUsingToken (token);
183 if(result == OC_STACK_OK)
185 OC_LOG(DEBUG, TAG, PCF("Removed observer successfully"));
189 result = OC_STACK_OK;
190 OC_LOG(DEBUG, TAG, PCF("Observer Removal failed"));
195 observer->failedCommCount++;
196 result = OC_STACK_CONTINUE;
198 observer->forceHighQos = 1;
199 OC_LOG_V(DEBUG, TAG, "Failed count for this observer is %d",observer->failedCommCount);
203 OC_LOG(ERROR, TAG, PCF("Unknown status"));
204 result = OC_STACK_ERROR;
211 OCStackResult CAToOCStackResult(CAResponseResult_t caCode)
213 OCStackResult ret = OC_STACK_ERROR;
221 ret = OC_STACK_RESOURCE_CREATED;
224 ret = OC_STACK_RESOURCE_DELETED;
227 ret = OC_STACK_INVALID_QUERY;
230 ret = OC_STACK_INVALID_OPTION;
233 ret = OC_STACK_NO_RESOURCE;
241 OCStackResult OCToCAConnectivityType(OCConnectivityType ocConType, CAConnectivityType_t* caConType)
243 OCStackResult ret = OC_STACK_OK;
248 *caConType = CA_ETHERNET;
251 *caConType = CA_WIFI;
260 //TODO-CA Add other connectivity types as they are enabled
261 *caConType = (CA_WIFI|CA_ETHERNET);
264 ret = OC_STACK_INVALID_PARAM;
270 OCStackResult CAToOCConnectivityType(CAConnectivityType_t caConType, OCConnectivityType *ocConType)
272 OCStackResult ret = OC_STACK_OK;
277 *ocConType = OC_ETHERNET;
280 *ocConType = OC_WIFI;
289 ret = OC_STACK_INVALID_PARAM;
295 // update response.addr appropriately from endPoint.addressInfo
296 OCStackResult UpdateResponseAddr(OCClientResponse *response, const CARemoteEndpoint_t* endPoint)
298 struct sockaddr_in sa;
299 OCStackResult ret = OC_STACK_INVALID_PARAM;
300 //TODO-CA Check validity of the endPoint pointer
301 inet_pton(AF_INET, endPoint->addressInfo.IP.ipAddress, &(sa.sin_addr));
302 sa.sin_port = htons(endPoint->addressInfo.IP.port);
303 static OCDevAddr address;
304 memcpy((void*)&address.addr, &(sa), sizeof(sa));
307 response->addr = &address;
308 ret = CAToOCConnectivityType(endPoint->connectivityType, &(response->connType));
313 void parsePresencePayload(char* payload, uint32_t* seqNum, uint32_t* maxAge, char** resType)
317 // The format of the payload is {"oc":[%u:%u:%s]}
318 // %u : sequence number,
320 // %s : Resource Type (Optional)
321 tok = strtok(payload, "[:]}");
322 payload[strlen(payload)] = ':';
323 tok = strtok(NULL, "[:]}");
324 payload[strlen((char *)payload)] = ':';
325 *seqNum = (uint32_t) atoi(tok);
326 tok = strtok(NULL, "[:]}");
327 *maxAge = (uint32_t) atoi(tok);
328 tok = strtok(NULL, "[:]}");
332 *resType = (char *)OCMalloc(strlen(tok));
337 payload[strlen((char *)payload)] = ':';
338 strcpy(*resType, tok);
339 OC_LOG_V(DEBUG, TAG, "----------------resourceTypeName %s", *resType);
341 payload[strlen((char *)payload)] = ']';
344 OCStackResult HandlePresenceResponse(const CARemoteEndpoint_t* endPoint,
345 const CAResponseInfo_t* responseInfo)
347 OCStackApplicationResult cbResult = OC_STACK_DELETE_TRANSACTION;
348 ClientCB * cbNode = NULL;
349 char *resourceTypeName = NULL;
350 OCClientResponse response;
351 OCStackResult result = OC_STACK_ERROR;
352 uint32_t lowerBound = 0;
353 uint32_t higherBound = 0;
356 char *fullUri = NULL;
357 char *ipAddress = NULL;
358 int presenceSubscribe = 0;
359 int multicastPresenceSubscribe = 0;
361 fullUri = (char *) OCMalloc(MAX_URI_LENGTH );
365 OC_LOG(INFO, TAG, PCF("Memory could not be abllocated for fullUri"));
366 result = OC_STACK_NO_MEMORY;
370 ipAddress = (char *) OCMalloc(strlen(endPoint->addressInfo.IP.ipAddress) + 1);
372 if(NULL == ipAddress)
374 OC_LOG(INFO, TAG, PCF("Memory could not be abllocated for ipAddress"));
375 result = OC_STACK_NO_MEMORY;
379 strncpy(ipAddress, endPoint->addressInfo.IP.ipAddress,
380 strlen(endPoint->addressInfo.IP.ipAddress));
381 ipAddress[strlen(endPoint->addressInfo.IP.ipAddress)] = '\0';
383 snprintf(fullUri, MAX_URI_LENGTH, "coap://%s:%u%s", ipAddress, endPoint->addressInfo.IP.port,
386 cbNode = GetClientCB(NULL, NULL, fullUri);
390 presenceSubscribe = 1;
394 snprintf(fullUri, MAX_URI_LENGTH, "%s%s", OC_MULTICAST_IP, endPoint->resourceUri);
395 cbNode = GetClientCB(NULL, NULL, fullUri);
398 multicastPresenceSubscribe = 1;
402 if(!presenceSubscribe && !multicastPresenceSubscribe)
404 OC_LOG(INFO, TAG, PCF("Received a presence notification, but I do not have callback \
405 ------------ ignoring"));
409 // No payload to the application in case of presence
410 response.resJSONPayload = NULL;
411 response.result = OC_STACK_OK;
413 UpdateResponseAddr(&response, endPoint);
415 if(responseInfo->info.payload)
417 parsePresencePayload(responseInfo->info.payload,
418 &(response.sequenceNumber),
425 OC_LOG(INFO, TAG, PCF("===============Stopping presence"));
426 response.result = OC_STACK_PRESENCE_STOPPED;
429 OCFree(cbNode->presence->timeOut);
430 OCFree(cbNode->presence);
431 cbNode->presence = NULL;
434 else if(presenceSubscribe)
436 if(!cbNode->presence)
438 cbNode->presence = (OCPresence *) OCMalloc(sizeof(OCPresence));
439 VERIFY_NON_NULL_V(cbNode->presence);
440 cbNode->presence->timeOut = NULL;
441 cbNode->presence->timeOut = (uint32_t *)
442 OCMalloc(PresenceTimeOutSize * sizeof(uint32_t));
443 if(!(cbNode->presence->timeOut)){
444 OCFree(cbNode->presence);
445 result = OC_STACK_NO_MEMORY;
449 OC_LOG_V(INFO, TAG, "===============Update presence TTL, now time is %u", GetTime(0));
450 cbNode->presence->TTL = maxAge;
451 for(int index = 0; index < PresenceTimeOutSize; index++)
453 lowerBound = GetTime(((float)(PresenceTimeOut[index])
454 /(float)100)*(float)cbNode->presence->TTL);
455 higherBound = GetTime(((float)(PresenceTimeOut[index + 1])
456 /(float)100)*(float)cbNode->presence->TTL);
457 cbNode->presence->timeOut[index] = OCGetRandomRange(lowerBound, higherBound);
458 OC_LOG_V(DEBUG, TAG, "----------------lowerBound timeout %d", lowerBound);
459 OC_LOG_V(DEBUG, TAG, "----------------higherBound timeout %d", higherBound);
460 OC_LOG_V(DEBUG, TAG, "----------------timeOut entry %d",
461 cbNode->presence->timeOut[index]);
463 cbNode->presence->TTLlevel = 0;
464 OC_LOG_V(DEBUG, TAG, "----------------this TTL level %d", cbNode->presence->TTLlevel);
465 if(cbNode->sequenceNumber == response.sequenceNumber)
467 OC_LOG(INFO, TAG, PCF("===============No presence change"));
470 OC_LOG(INFO, TAG, PCF("===============Presence changed, calling up the stack"));
471 cbNode->sequenceNumber = response.sequenceNumber;
473 // Ensure that a filter is actually applied.
474 if(resourceTypeName && cbNode->filterResourceType)
476 if(!findResourceType(cbNode->filterResourceType, resourceTypeName))
484 // This is the multicast case
486 OCMulticastNode* mcNode = NULL;
487 mcNode = GetMCPresenceNode((const unsigned char *)fullUri);
491 if(mcNode->nonce == response.sequenceNumber)
493 OC_LOG(INFO, TAG, PCF("===============No presence change (Multicast)"));
496 mcNode->nonce = response.sequenceNumber;
500 uint32_t uriLen = strlen((char*)fullUri);
501 unsigned char* uri = (unsigned char *) OCMalloc(uriLen + 1);
504 memcpy(uri, fullUri, (uriLen + 1));
509 PCF("===============No Memory for URI to store in the presence node"));
510 result = OC_STACK_NO_MEMORY;
513 result = AddMCPresenceNode(&mcNode, (unsigned char*) uri, response.sequenceNumber);
514 if(result == OC_STACK_NO_MEMORY)
517 PCF("===============No Memory for Multicast Presence Node"));
518 result = OC_STACK_NO_MEMORY;
523 // Ensure that a filter is actually applied.
524 if(resourceTypeName && cbNode->filterResourceType)
526 if(!findResourceType(cbNode->filterResourceType, resourceTypeName))
533 cbResult = cbNode->callBack(cbNode->context, cbNode->handle, &response);
535 if (cbResult == OC_STACK_DELETE_TRANSACTION)
537 FindAndDeleteClientCB(cbNode);
543 OCFree(resourceTypeName);
548 //This function will be called back by CA layer when a response is received
549 void HandleCAResponses(const CARemoteEndpoint_t* endPoint, const CAResponseInfo_t* responseInfo)
551 OC_LOG(INFO, TAG, PCF("Enter HandleCAResponses"));
555 OC_LOG(ERROR, TAG, PCF("endPoint is NULL"));
559 if(NULL == responseInfo)
561 OC_LOG(ERROR, TAG, PCF("responseInfo is NULL"));
565 if(strcmp(endPoint->resourceUri, OC_PRESENCE_URI) == 0)
567 HandlePresenceResponse(endPoint, responseInfo);
571 ClientCB *cbNode = GetClientCB((CAToken_t *)&responseInfo->info.token, NULL, NULL);
575 OC_LOG(INFO, TAG, PCF("Calling into application address space"));
576 OCClientResponse response;
578 OCStackResult result = UpdateResponseAddr(&response, endPoint);
579 if(result != OC_STACK_OK)
581 OC_LOG(ERROR, TAG, PCF("Invalid connectivity type in endpoint"));
585 response.result = CAToOCStackResult(responseInfo->result);
586 response.resJSONPayload = (unsigned char*)responseInfo->info.payload;
587 response.numRcvdVendorSpecificHeaderOptions = 0;
588 if(responseInfo->info.numOptions > 0)
591 //First option alwas with option ID COAP_OPTION_OBSERVE if it is availbale
592 if(responseInfo->info.options[0].optionID == COAP_OPTION_OBSERVE)
594 memcpy (&(response.sequenceNumber),
595 &(responseInfo->info.options[0].optionData), 4);
596 response.numRcvdVendorSpecificHeaderOptions = responseInfo->info.numOptions - 1;
601 response.numRcvdVendorSpecificHeaderOptions = responseInfo->info.numOptions;
604 if(response.numRcvdVendorSpecificHeaderOptions > MAX_HEADER_OPTIONS)
606 OC_LOG(ERROR, TAG, PCF("#header options are more than MAX_HEADER_OPTIONS"));
610 for (uint8_t i = start; i < responseInfo->info.numOptions; i++)
612 memcpy (&(response.rcvdVendorSpecificHeaderOptions[i-start]),
613 &(responseInfo->info.options[i]), sizeof(OCHeaderOption));
616 result = cbNode->callBack(cbNode->context,
617 cbNode->handle, &response);
618 if (result == OC_STACK_DELETE_TRANSACTION)
620 FindAndDeleteClientCB(cbNode);
623 OC_LOG_V(INFO, TAG, PCF("Received payload: %s\n"), (char*)responseInfo->info.payload);
624 OC_LOG(INFO, TAG, PCF("Exit HandleCAResponses"));
627 //This function will be called back by CA layer when a request is received
628 void HandleCARequests(const CARemoteEndpoint_t* endPoint, const CARequestInfo_t* requestInfo)
630 OC_LOG(INFO, TAG, PCF("Enter HandleCARequests"));
633 if(myStackMode == OC_CLIENT)
635 //TODO: should the client be responding to requests?
639 OCServerProtocolRequest serverRequest;
641 memset (&serverRequest, 0, sizeof(OCServerProtocolRequest));
642 OC_LOG_V(INFO, TAG, PCF("***** Endpoint URI ***** : %s\n"), (char*)endPoint->resourceUri);
644 char * newUri = (char *)endPoint->resourceUri;
645 unsigned char * query = NULL;
646 getQueryFromUri(endPoint->resourceUri, &query, &newUri);
647 OC_LOG_V(INFO, TAG, PCF("**********URI without query ****: %s\n"), newUri);
648 OC_LOG_V(INFO, TAG, PCF("**********Query ****: %s\n"), query);
650 memcpy (&(serverRequest.resourceUrl), newUri, strlen(newUri));
654 memcpy (&(serverRequest.query), query, strlen((char*)query));
656 //copy request payload
657 if (requestInfo->info.payload)
659 serverRequest.reqTotalSize = strlen(requestInfo->info.payload) + 1;
660 memcpy (&(serverRequest.reqJSONPayload), requestInfo->info.payload,
661 strlen(requestInfo->info.payload));
662 serverRequest.reqTotalSize = strlen((const char *)requestInfo->info.payload) + 1;
666 serverRequest.reqTotalSize = 1;
669 switch (requestInfo->method)
673 serverRequest.method = OC_REST_GET;
678 serverRequest.method = OC_REST_PUT;
683 serverRequest.method = OC_REST_POST;
688 serverRequest.method = OC_REST_DELETE;
693 OC_LOG(ERROR, TAG, PCF("Received CA method %d not supported"));
699 OC_LOG_V(INFO, TAG, "HandleCARequests: CA token length = %d", strlen(requestInfo->info.token));
700 OC_LOG_BUFFER(INFO, TAG, requestInfo->info.token, strlen(requestInfo->info.token));
701 // TODO-CA: For CA integration currently copying CAToken to OCCoapToken:
702 // Need to remove OCCoapToken
703 memcpy (&(serverRequest.requestToken.token), requestInfo->info.token,
705 serverRequest.requestToken.tokenLength = MAX_TOKEN_LENGTH;
707 if (requestInfo->info.type == CA_MSG_CONFIRM)
709 serverRequest.qos = OC_HIGH_QOS;
711 else if (requestInfo->info.type == CA_MSG_NONCONFIRM)
713 serverRequest.qos = OC_LOW_QOS;
715 else if (requestInfo->info.type == CA_MSG_ACKNOWLEDGE)
717 // TODO-CA: Need to handle this
719 else if (requestInfo->info.type == CA_MSG_RESET)
721 // TODO-CA: Need to handle this
723 // CA does not need the following 3 fields
724 serverRequest.coapID = 0;
725 serverRequest.delayedResNeeded = 0;
726 serverRequest.secured = endPoint->isSecured;
729 serverRequest.addressInfo = endPoint->addressInfo;
730 serverRequest.connectivityType = endPoint->connectivityType;
731 if (requestInfo->info.token)
733 strncpy(serverRequest.token, requestInfo->info.token, sizeof(serverRequest.token) - 1);
736 // copy vendor specific header options
737 // TODO-CA: CA is including non-vendor header options as well, like observe.
738 // Need to filter those out
739 GetObserveHeaderOption(&serverRequest.observationOption, requestInfo->info.options, &(requestInfo->info.numOptions));
740 if (requestInfo->info.numOptions > MAX_HEADER_OPTIONS)
742 // TODO-CA: Need to send an error indicating the num of options is incorrect
745 serverRequest.numRcvdVendorSpecificHeaderOptions = requestInfo->info.numOptions;
746 if (serverRequest.numRcvdVendorSpecificHeaderOptions)
748 memcpy (&(serverRequest.rcvdVendorSpecificHeaderOptions), requestInfo->info.options,
749 sizeof(CAHeaderOption_t)*requestInfo->info.numOptions);
752 if(HandleStackRequests (&serverRequest) != OC_STACK_OK)
754 OC_LOG(INFO, TAG, PCF("HandleStackRequests failed"));
758 OC_LOG(INFO, TAG, PCF("Exit HandleCARequests"));
763 //This function will be called back by occoap layer when a request is received
764 OCStackResult HandleStackRequests(OCServerProtocolRequest * protocolRequest)
766 OC_LOG(INFO, TAG, PCF("Entering HandleStackRequests (OCStack Layer)"));
767 OCStackResult result = OC_STACK_ERROR;
768 ResourceHandling resHandling;
769 OCResource *resource;
771 OCServerRequest * request = GetServerRequestUsingToken(protocolRequest->requestToken);
774 OC_LOG(INFO, TAG, PCF("This is a new Server Request"));
776 result = AddServerCARequest(&request, protocolRequest->coapID,
777 protocolRequest->delayedResNeeded, protocolRequest->secured, 0,
778 protocolRequest->method, protocolRequest->numRcvdVendorSpecificHeaderOptions,
779 protocolRequest->observationOption, protocolRequest->qos,
780 protocolRequest->query, protocolRequest->rcvdVendorSpecificHeaderOptions,
781 protocolRequest->reqJSONPayload, &protocolRequest->requestToken,
782 &protocolRequest->requesterAddr, protocolRequest->resourceUrl,
783 protocolRequest->reqTotalSize,
784 &protocolRequest->addressInfo, protocolRequest->connectivityType, protocolRequest->token);
786 result = AddServerRequest(&request, protocolRequest->coapID,
787 protocolRequest->delayedResNeeded, protocolRequest->secured, 0,
788 protocolRequest->method, protocolRequest->numRcvdVendorSpecificHeaderOptions,
789 protocolRequest->observationOption, protocolRequest->qos,
790 protocolRequest->query, protocolRequest->rcvdVendorSpecificHeaderOptions,
791 protocolRequest->reqJSONPayload, &protocolRequest->requestToken,
792 &protocolRequest->requesterAddr, protocolRequest->resourceUrl,
793 protocolRequest->reqTotalSize);
795 if (OC_STACK_OK != result)
797 OC_LOG(ERROR, TAG, PCF("Error adding server request"));
800 VERIFY_NON_NULL(request, ERROR, OC_STACK_NO_MEMORY);
802 if(!protocolRequest->reqMorePacket)
804 request->requestComplete = 1;
809 OC_LOG(INFO, TAG, PCF("This is either a repeated Server Request or blocked Server Request"));
812 if(request->requestComplete)
814 OC_LOG(INFO, TAG, PCF("This Server Request is complete"));
815 result = DetermineResourceHandling (request, &resHandling, &resource);
816 if (result == OC_STACK_OK)
818 result = ProcessRequest(resHandling, resource, request);
822 result = OC_STACK_ERROR;
827 OC_LOG(INFO, TAG, PCF("This Server Request is incomplete"));
828 result = OC_STACK_CONTINUE;
833 //This function will be called back by occoap layer when a response is received
834 OCStackResult HandleStackResponses(OCResponse * response)
836 OC_LOG(INFO, TAG, PCF("Entering HandleStackResponses (OCStack Layer)"));
837 OCStackResult result = OC_STACK_OK;
838 OCStackApplicationResult cbResult = OC_STACK_DELETE_TRANSACTION;
839 uint8_t isObserveNotification = 0;
840 ClientCB * cbNode = NULL;
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);
1224 //It is ok to select network to CA_WIFI for now
1225 CAResult_t caResult = CASelectNetwork(CA_WIFI|CA_ETHERNET);
1226 if(caResult == CA_STATUS_OK)
1228 OC_LOG(INFO, TAG, PCF("CASelectNetwork to WIFI"));
1229 caResult = CARegisterHandler(HandleCARequests, HandleCAResponses);
1230 if(caResult == CA_STATUS_OK)
1232 OC_LOG(INFO, TAG, PCF("CARegisterHandler..."));
1233 stackState = OC_STACK_INITIALIZED;
1234 result = OC_STACK_OK;
1238 caResult = CAStartDiscoveryServer();
1239 OC_LOG(INFO, TAG, PCF("Client mode: CAStartDiscoveryServer"));
1242 caResult = CAStartListeningServer();
1243 OC_LOG(INFO, TAG, PCF("Server mode: CAStartListeningServer"));
1245 case OC_CLIENT_SERVER:
1246 caResult = CAStartListeningServer();
1247 if(caResult == CA_STATUS_OK)
1249 caResult = CAStartDiscoveryServer();
1251 OC_LOG(INFO, TAG, PCF("Client-server mode"));
1254 OC_LOG(ERROR, TAG, PCF("Invalid mode"));
1255 return OC_STACK_ERROR;
1260 if (caResult == CA_STATUS_OK)
1262 result = OC_STACK_OK;
1266 result = OC_STACK_ERROR;
1273 OC_LOG(INFO, TAG, PCF("Client mode"));
1276 OC_LOG(INFO, TAG, PCF("Server mode"));
1278 case OC_CLIENT_SERVER:
1279 OC_LOG(INFO, TAG, PCF("Client-server mode"));
1282 OC_LOG(ERROR, TAG, PCF("Invalid mode"));
1283 return OC_STACK_ERROR;
1287 // Make call to OCCoAP layer
1288 result = OCInitCoAP(ipAddr, (uint16_t) port, myStackMode);
1292 defaultDeviceHandler = NULL;
1294 #ifdef WITH_PRESENCE
1295 PresenceTimeOutSize = sizeof(PresenceTimeOut)/sizeof(PresenceTimeOut[0]) - 1;
1296 #endif // WITH_PRESENCE
1298 if (result == OC_STACK_OK)
1300 stackState = OC_STACK_INITIALIZED;
1302 // Initialize resource
1303 if(result == OC_STACK_OK && myStackMode != OC_CLIENT)
1305 result = initResources();
1307 if(result != OC_STACK_OK)
1309 OC_LOG(ERROR, TAG, PCF("Stack initialization error"));
1315 * Stop the OC stack. Use for a controlled shutdown.
1317 * OC_STACK_OK - no errors
1318 * OC_STACK_ERROR - stack not initialized
1320 OCStackResult OCStop()
1322 OCStackResult result = OC_STACK_ERROR;
1324 OC_LOG(INFO, TAG, PCF("Entering OCStop"));
1326 if (stackState == OC_STACK_UNINIT_IN_PROGRESS)
1328 OC_LOG(DEBUG, TAG, PCF("Stack already stopping, exiting"));
1331 else if (stackState != OC_STACK_INITIALIZED)
1333 OC_LOG(ERROR, TAG, PCF("Stack not initialized"));
1334 return OC_STACK_ERROR;
1337 stackState = OC_STACK_UNINIT_IN_PROGRESS;
1339 #ifdef WITH_PRESENCE
1340 // Ensure that the TTL associated with ANY and ALL presence notifications originating from
1341 // here send with the code "OC_STACK_PRESENCE_STOPPED" result.
1342 presenceResource.presenceTTL = 0;
1343 #endif // WITH_PRESENCE
1345 // Free memory dynamically allocated for resources
1346 deleteAllResources();
1350 //CATerminate does not return any error code. It is OK to assign result to OC_STACK_OK.
1351 result = OC_STACK_OK;
1353 result = OCStopCoAP();
1356 if (result == OC_STACK_OK)
1358 // Remove all observers
1359 DeleteObserverList();
1360 // Remove all the client callbacks
1361 DeleteClientCBList();
1362 stackState = OC_STACK_UNINITIALIZED;
1363 result = OC_STACK_OK;
1365 stackState = OC_STACK_INITIALIZED;
1366 result = OC_STACK_ERROR;
1369 // Deinit security blob
1370 DeinitOCSecurityInfo();
1372 if (result != OC_STACK_OK) {
1373 OC_LOG(ERROR, TAG, PCF("Stack stop error"));
1380 * Map OCQualityOfService to CAMessageType
1382 * @param OCQualityOfService - Input qos.
1384 * Returns CA message type for a given qos.
1386 CAMessageType_t qualityOfServiceToMessageType(OCQualityOfService qos)
1391 return CA_MSG_CONFIRM;
1396 return CA_MSG_NONCONFIRM;
1401 * Verify the lengths of the URI and the query separately
1403 * @param inputUri - Input URI and query.
1404 * @param uriLen - The length of the initial URI with query.
1406 * Note: The '?' that appears after the URI is not considered as
1407 * a part of the query.
1409 OCStackResult verifyUriQueryLength(const char *inputUri, uint16_t uriLen)
1413 query = strchr (inputUri, '?');
1417 if((query - inputUri) > MAX_URI_LENGTH)
1419 return OC_STACK_INVALID_URI;
1422 if((inputUri + uriLen - 1 - query) > MAX_QUERY_LENGTH)
1424 return OC_STACK_INVALID_QUERY;
1427 else if(uriLen > MAX_URI_LENGTH)
1429 return OC_STACK_INVALID_URI;
1435 * Discover or Perform requests on a specified resource (specified by that Resource's respective URI).
1437 * @param handle - @ref OCDoHandle to refer to the request sent out on behalf of calling this API.
1438 * @param method - @ref OCMethod to perform on the resource
1439 * @param requiredUri - URI of the resource to interact with
1440 * @param referenceUri - URI of the reference resource
1441 * @param request - JSON encoded request
1442 * @param qos - quality of service
1443 * @param cbData - struct that contains asynchronous callback function that is invoked
1444 * by the stack when discovery or resource interaction is complete
1445 * @param options - The address of an array containing the vendor specific header
1446 * header options to be sent with the request
1447 * @param numOptions - Number of vendor specific header options to be included
1450 * OC_STACK_OK - no errors
1451 * OC_STACK_INVALID_CALLBACK - invalid callback function pointer
1452 * OC_STACK_INVALID_METHOD - invalid resource method
1453 * OC_STACK_INVALID_URI - invalid required or reference URI
1455 * Note: IN case of CA, when using multicast, the required URI should not contain IP address.
1456 * Instead, it just contains the URI to the resource such as "/oc/core".
1459 OCStackResult OCDoResource(OCDoHandle *handle, OCMethod method, const char *requiredUri,
1460 const char *referenceUri, const char *request, uint8_t conType,
1461 OCQualityOfService qos, OCCallbackData *cbData,
1462 OCHeaderOption * options, uint8_t numOptions)
1464 OCStackResult OCDoResource(OCDoHandle *handle, OCMethod method, const char *requiredUri,
1465 const char *referenceUri, const char *request,
1466 OCQualityOfService qos, OCCallbackData *cbData,
1467 OCHeaderOption * options, uint8_t numOptions)
1470 OCStackResult result = OC_STACK_ERROR;
1472 ClientCB *clientCB = NULL;
1473 unsigned char * requestUri = NULL;
1474 unsigned char * resourceType = NULL;
1475 unsigned char * query = NULL;
1476 char * newUri = (char *)requiredUri;
1477 (void) referenceUri;
1479 CARemoteEndpoint_t* endpoint = NULL;
1480 CAResult_t caResult;
1481 CAToken_t caToken = NULL;
1482 CAInfo_t requestData;
1483 CARequestInfo_t requestInfo;
1484 CAGroupEndpoint_t grpEnd;
1486 // To track if memory is allocated for additional header options
1487 uint8_t hdrOptionMemAlloc = 0;
1490 OC_LOG(INFO, TAG, PCF("Entering OCDoResource"));
1492 // Validate input parameters
1493 VERIFY_NON_NULL(cbData, FATAL, OC_STACK_INVALID_CALLBACK);
1494 VERIFY_NON_NULL(cbData->cb, FATAL, OC_STACK_INVALID_CALLBACK);
1496 TODO ("Need to form the final query by concatenating require and reference URI's");
1497 VERIFY_NON_NULL(requiredUri, FATAL, OC_STACK_INVALID_URI);
1499 uint16_t uriLen = strlen(requiredUri);
1501 // ToDo: We should also check if the requiredUri has a mutlicast address, then qos has to be OC_Low_QOS
1507 case OC_REST_DELETE:
1508 case OC_REST_OBSERVE:
1509 case OC_REST_OBSERVE_ALL:
1510 case OC_REST_CANCEL_OBSERVE:
1512 #ifdef WITH_PRESENCE
1513 case OC_REST_PRESENCE:
1517 result = OC_STACK_INVALID_METHOD;
1521 if((result = verifyUriQueryLength(requiredUri, uriLen)) != OC_STACK_OK)
1526 if((request) && (strlen(request) > MAX_REQUEST_LENGTH))
1528 result = OC_STACK_INVALID_PARAM;
1532 #ifdef WITH_PRESENCE
1533 if(method == OC_REST_PRESENCE)
1535 result = getQueryFromUri(requiredUri, &query, &newUri);
1538 result = getResourceType((char *) query, &resourceType);
1541 OC_LOG_V(DEBUG, TAG, "Got Resource Type: %s", resourceType);
1545 OC_LOG(DEBUG, TAG, PCF("Resource type is NULL."));
1550 OC_LOG(DEBUG, TAG, PCF("Query string is NULL."));
1552 if(result != OC_STACK_OK)
1557 #endif // WITH_PRESENCE
1559 requestUri = (unsigned char *) OCMalloc(uriLen + 1);
1562 memcpy(requestUri, newUri, (uriLen + 1));
1566 result = OC_STACK_NO_MEMORY;
1570 *handle = GenerateInvocationHandle();
1573 result = OC_STACK_NO_MEMORY;
1578 memset(&requestData, 0, sizeof(CAInfo_t));
1579 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
1580 memset(&grpEnd, 0, sizeof(CAGroupEndpoint_t));
1584 case OC_REST_OBSERVE:
1585 case OC_REST_OBSERVE_ALL:
1586 case OC_REST_CANCEL_OBSERVE:
1588 requestInfo.method = CA_GET;
1593 requestInfo.method = CA_PUT;
1598 requestInfo.method = CA_POST;
1601 case OC_REST_DELETE:
1603 requestInfo.method = CA_DELETE;
1606 #ifdef WITH_PRESENCE
1607 case OC_REST_PRESENCE:
1609 // Replacing method type with GET because "presence"
1610 // is a stack layer only implementation.
1611 requestInfo.method = CA_GET;
1616 result = OC_STACK_INVALID_METHOD;
1620 //High QoS is not supported
1621 if(qos == OC_HIGH_QOS)
1623 result = OC_STACK_INVALID_PARAM;
1628 caResult = CAGenerateToken(&caToken);
1630 //TODO-CA Remove this temporary fix (for some reason same token is being generated)
1631 static int count = 0;
1633 caToken[0] += count;
1635 if (caResult != CA_STATUS_OK)
1637 OC_LOG(ERROR, TAG, PCF("CAGenerateToken error"));
1642 requestData.type = qualityOfServiceToMessageType(qos);
1643 requestData.token = caToken;
1644 if ((method == OC_REST_OBSERVE) || (method == OC_REST_OBSERVE_ALL))
1646 result = CreateObserveHeaderOption (&(requestData.options), options,
1647 numOptions, OC_OBSERVE_REGISTER);
1648 if (result != OC_STACK_OK)
1652 hdrOptionMemAlloc = 1;
1653 requestData.numOptions = numOptions + 1;
1657 requestData.options = (CAHeaderOption_t*)options;
1658 requestData.numOptions = numOptions;
1660 requestData.payload = (char *)request;
1662 requestInfo.info = requestData;
1664 CAConnectivityType_t caConType;
1666 result = OCToCAConnectivityType(conType, &caConType);
1667 if (result != OC_STACK_OK)
1669 OC_LOG(ERROR, TAG, PCF("Invalid Connectivity Type"));
1674 if(conType == OC_ALL)
1676 grpEnd.connectivityType = caConType;
1678 grpEnd.resourceUri = (CAURI_t) OCMalloc(uriLen + 1);
1679 strncpy(grpEnd.resourceUri, requiredUri, (uriLen + 1));
1681 caResult = CASendRequestToAll(&grpEnd, &requestInfo);
1685 caResult = CACreateRemoteEndpoint(newUri, caConType, &endpoint);
1687 if (caResult != CA_STATUS_OK)
1689 OC_LOG(ERROR, TAG, PCF("CACreateRemoteEndpoint error"));
1693 caResult = CASendRequest(endpoint, &requestInfo);
1696 if (caResult != CA_STATUS_OK)
1698 OC_LOG(ERROR, TAG, PCF("CASendRequest"));
1702 if((result = AddClientCB(&clientCB, cbData, &caToken, handle, method,
1703 requestUri, resourceType)) != OC_STACK_OK)
1705 result = OC_STACK_NO_MEMORY;
1711 // Generate token which will be used by OCStack to match responses received
1713 OCGenerateCoAPToken(&token);
1715 if((result = AddClientCB(&clientCB, cbData, &token, handle, method, requestUri, resourceType))
1718 result = OC_STACK_NO_MEMORY;
1722 // Make call to OCCoAP layer
1723 result = OCDoCoAPResource(method, qos, &token, newUri, request, options, numOptions);
1727 if(newUri != requiredUri)
1731 if (result != OC_STACK_OK)
1733 OC_LOG(ERROR, TAG, PCF("OCDoResource error"));
1734 FindAndDeleteClientCB(clientCB);
1737 CADestroyRemoteEndpoint(endpoint);
1738 OCFree(grpEnd.resourceUri);
1739 if (hdrOptionMemAlloc)
1741 OCFree(requestData.options);
1748 * Cancel a request associated with a specific @ref OCDoResource invocation.
1750 * @param handle - Used to identify a specific OCDoResource invocation.
1751 * @param qos - used to specify Quality of Service (read below for more info)
1752 * @param options- used to specify vendor specific header options when sending
1753 * explicit observe cancellation
1754 * @param numOptions- Number of header options to be included
1757 * OC_STACK_OK - No errors; Success
1758 * OC_STACK_INVALID_PARAM - The handle provided is invalid.
1760 OCStackResult OCCancel(OCDoHandle handle, OCQualityOfService qos, OCHeaderOption * options,
1764 * This ftn is implemented one of two ways in the case of observation:
1766 * 1. qos == OC_NON_CONFIRMABLE. When observe is unobserved..
1767 * Remove the callback associated on client side.
1768 * When the next notification comes in from server,
1769 * reply with RESET message to server.
1770 * Keep in mind that the server will react to RESET only
1771 * if the last notification was sent ans CON
1773 * 2. qos == OC_CONFIRMABLE. When OCCancel is called,
1774 * and it is associated with an observe request
1775 * (i.e. ClientCB->method == OC_REST_OBSERVE || OC_REST_OBSERVE_ALL),
1776 * Send CON Observe request to server with
1777 * observe flag = OC_RESOURCE_OBSERVE_DEREGISTER.
1778 * Remove the callback associated on client side.
1780 OCStackResult ret = OC_STACK_OK;
1782 CARemoteEndpoint_t* endpoint = NULL;
1783 CAResult_t caResult;
1784 CAInfo_t requestData;
1785 CARequestInfo_t requestInfo;
1786 // Track if memory is allocated for additional header options
1787 uint8_t hdrOptionMemAlloc = 0;
1791 return OC_STACK_INVALID_PARAM;
1794 OC_LOG(INFO, TAG, PCF("Entering OCCancel"));
1796 ClientCB *clientCB = GetClientCB(NULL, handle, NULL);
1799 switch (clientCB->method)
1801 case OC_REST_OBSERVE:
1802 case OC_REST_OBSERVE_ALL:
1804 //TODO-CA : Why CA_WIFI alone?
1805 caResult = CACreateRemoteEndpoint((char *)clientCB->requestUri, CA_WIFI,
1807 if (caResult != CA_STATUS_OK)
1809 OC_LOG(ERROR, TAG, PCF("CACreateRemoteEndpoint error"));
1810 return OC_STACK_ERROR;
1813 memset(&requestData, 0, sizeof(CAInfo_t));
1814 requestData.type = qualityOfServiceToMessageType(qos);
1815 requestData.token = clientCB->token;
1816 if (CreateObserveHeaderOption (&(requestData.options),
1817 options, numOptions, OC_OBSERVE_DEREGISTER) != OC_STACK_OK)
1819 return OC_STACK_ERROR;
1821 hdrOptionMemAlloc = 1;
1822 requestData.numOptions = numOptions + 1;
1823 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
1824 requestInfo.method = CA_GET;
1825 requestInfo.info = requestData;
1827 caResult = CASendRequest(endpoint, &requestInfo);
1828 if (caResult != CA_STATUS_OK)
1830 OC_LOG(ERROR, TAG, PCF("CASendRequest error"));
1832 if(caResult == CA_STATUS_OK)
1837 if(qos == OC_HIGH_QOS)
1839 ret = OCDoCoAPResource(OC_REST_CANCEL_OBSERVE, qos,
1840 &(clientCB->token), (const char *) clientCB->requestUri, NULL, options,
1845 FindAndDeleteClientCB(clientCB);
1849 #ifdef WITH_PRESENCE
1850 case OC_REST_PRESENCE:
1851 FindAndDeleteClientCB(clientCB);
1855 return OC_STACK_INVALID_METHOD;
1859 CADestroyRemoteEndpoint(endpoint);
1860 if (hdrOptionMemAlloc)
1862 OCFree(requestData.options);
1869 #ifdef WITH_PRESENCE
1871 OCStackResult OCProcessPresence()
1873 OCStackResult result = OC_STACK_OK;
1874 uint8_t ipAddr[4] = { 0 };
1877 OC_LOG(INFO, TAG, PCF("Entering RequestPresence"));
1878 ClientCB* cbNode = NULL;
1880 OCClientResponse clientResponse;
1881 OCResponse * response = NULL;
1882 OCStackApplicationResult cbResult = OC_STACK_DELETE_TRANSACTION;
1884 LL_FOREACH(cbList, cbNode) {
1885 if(OC_REST_PRESENCE == cbNode->method)
1887 if(cbNode->presence)
1889 uint32_t now = GetTime(0);
1890 OC_LOG_V(DEBUG, TAG, "----------------this TTL level %d",
1891 cbNode->presence->TTLlevel);
1892 OC_LOG_V(DEBUG, TAG, "----------------current ticks %d", now);
1895 if(cbNode->presence->TTLlevel >= (PresenceTimeOutSize + 1))
1900 if(cbNode->presence->TTLlevel < PresenceTimeOutSize){
1901 OC_LOG_V(DEBUG, TAG, "----------------timeout ticks %d",
1902 cbNode->presence->timeOut[cbNode->presence->TTLlevel]);
1905 if(cbNode->presence->TTLlevel >= PresenceTimeOutSize)
1907 OC_LOG(DEBUG, TAG, PCF("----------------No more timeout ticks"));
1908 if (ParseIPv4Address( cbNode->requestUri, ipAddr, &port))
1910 OCBuildIPv4Address(ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3], port,
1912 result = FormOCClientResponse(&clientResponse, OC_STACK_PRESENCE_TIMEOUT,
1913 (OCDevAddr *) &dst, 0, NULL);
1914 if(result != OC_STACK_OK)
1918 result = FormOCResponse(&response, cbNode, 0, NULL, NULL,
1919 &cbNode->token, &clientResponse, NULL);
1920 if(result != OC_STACK_OK)
1925 // Increment the TTLLevel (going to a next state), so we don't keep
1926 // sending presence notification to client.
1927 cbNode->presence->TTLlevel++;
1928 OC_LOG_V(DEBUG, TAG, "----------------moving to TTL level %d",
1929 cbNode->presence->TTLlevel);
1933 result = OC_STACK_INVALID_IP;
1937 cbResult = cbNode->callBack(cbNode->context, cbNode->handle, &clientResponse);
1938 if (cbResult == OC_STACK_DELETE_TRANSACTION)
1940 FindAndDeleteClientCB(cbNode);
1944 if(now >= cbNode->presence->timeOut[cbNode->presence->TTLlevel])
1946 CAResult_t caResult;
1947 CARemoteEndpoint_t* endpoint = NULL;
1948 CAInfo_t requestData;
1949 CARequestInfo_t requestInfo;
1951 OC_LOG(DEBUG, TAG, PCF("time to test server presence =========="));
1953 //TODO-CA : Why CA_WIFI alone?
1954 caResult = CACreateRemoteEndpoint((char *)cbNode->requestUri, CA_WIFI,
1957 if (caResult != CA_STATUS_OK)
1959 OC_LOG(ERROR, TAG, PCF("CACreateRemoteEndpoint error"));
1963 memset(&requestData, 0, sizeof(CAInfo_t));
1964 requestData.type = CA_MSG_NONCONFIRM;
1965 requestData.token = cbNode->token;
1967 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
1968 requestInfo.method = CA_GET;
1969 requestInfo.info = requestData;
1971 caResult = CASendRequest(endpoint, &requestInfo);
1973 if (caResult != CA_STATUS_OK)
1975 OC_LOG(ERROR, TAG, PCF("CASendRequest error"));
1979 cbNode->presence->TTLlevel++;
1980 OC_LOG_V(DEBUG, TAG, "----------------moving to TTL level %d",
1981 cbNode->presence->TTLlevel);
1987 if (result != OC_STACK_OK)
1989 OC_LOG(ERROR, TAG, PCF("OCProcessPresence error"));
1994 OCStackResult OCProcessPresence()
1996 OCStackResult result = OC_STACK_OK;
1997 uint8_t ipAddr[4] = { 0 };
2000 OC_LOG(INFO, TAG, PCF("Entering RequestPresence"));
2001 ClientCB* cbNode = NULL;
2003 OCClientResponse clientResponse;
2004 OCResponse * response = NULL;
2006 LL_FOREACH(cbList, cbNode) {
2007 if(OC_REST_PRESENCE == cbNode->method)
2009 if(cbNode->presence)
2011 uint32_t now = GetTime(0);
2012 OC_LOG_V(DEBUG, TAG, "----------------this TTL level %d", cbNode->presence->TTLlevel);
2013 OC_LOG_V(DEBUG, TAG, "----------------current ticks %d", now);
2016 if(cbNode->presence->TTLlevel >= (PresenceTimeOutSize + 1))
2021 if(cbNode->presence->TTLlevel < PresenceTimeOutSize){
2022 OC_LOG_V(DEBUG, TAG, "----------------timeout ticks %d",
2023 cbNode->presence->timeOut[cbNode->presence->TTLlevel]);
2026 if(cbNode->presence->TTLlevel >= PresenceTimeOutSize)
2028 OC_LOG(DEBUG, TAG, PCF("----------------No more timeout ticks"));
2029 if (ParseIPv4Address( cbNode->requestUri, ipAddr, &port))
2031 OCBuildIPv4Address(ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3], port,
2033 result = FormOCClientResponse(&clientResponse, OC_STACK_PRESENCE_TIMEOUT,
2034 (OCDevAddr *) &dst, 0, NULL);
2035 if(result != OC_STACK_OK)
2039 result = FormOCResponse(&response, cbNode, 0, NULL, NULL,
2040 &cbNode->token, &clientResponse, NULL);
2041 if(result != OC_STACK_OK)
2046 // Increment the TTLLevel (going to a next state), so we don't keep
2047 // sending presence notification to client.
2048 cbNode->presence->TTLlevel++;
2049 OC_LOG_V(DEBUG, TAG, "----------------moving to TTL level %d",
2050 cbNode->presence->TTLlevel);
2054 result = OC_STACK_INVALID_IP;
2057 HandleStackResponses(response);
2059 if(now >= cbNode->presence->timeOut[cbNode->presence->TTLlevel])
2061 OC_LOG(DEBUG, TAG, PCF("time to test server presence =========="));
2064 OCGenerateCoAPToken(&token);
2065 result = OCDoCoAPResource(OC_REST_GET, OC_LOW_QOS,
2066 &token, (const char *)cbNode->requestUri, NULL, NULL, 0);
2068 if(result != OC_STACK_OK)
2072 cbNode->presence->TTLlevel++;
2073 OC_LOG_V(DEBUG, TAG, "----------------moving to TTL level %d", cbNode->presence->TTLlevel);
2079 if (result != OC_STACK_OK)
2081 OC_LOG(ERROR, TAG, PCF("OCProcessPresence error"));
2086 #endif // WITH_PRESENCE
2089 * Called in main loop of OC client or server. Allows low-level processing of
2093 * OC_STACK_OK - no errors
2094 * OC_STACK_ERROR - stack process error
2096 OCStackResult OCProcess() {
2098 OC_LOG(INFO, TAG, PCF("Entering OCProcess"));
2099 #ifdef WITH_PRESENCE
2100 OCProcessPresence();
2103 CAHandleRequestResponse();
2111 #ifdef WITH_PRESENCE
2113 * When operating in @ref OCServer or @ref OCClientServer mode, this API will start sending out
2114 * presence notifications to clients via multicast. Once this API has been called with a success,
2115 * clients may query for this server's presence and this server's stack will respond via multicast.
2117 * Server can call this function when it comes online for the first time, or when it comes back
2118 * online from offline mode, or when it re enters network.
2120 * @param ttl - Time To Live in seconds
2121 * Note: If ttl is '0', then the default stack value will be used (60 Seconds).
2124 * OC_STACK_OK - No errors; Success
2126 OCStackResult OCStartPresence(const uint32_t ttl)
2128 OCChangeResourceProperty(
2129 &(((OCResource *)presenceResource.handle)->resourceProperties),
2134 presenceResource.presenceTTL = ttl;
2137 if(OC_PRESENCE_UNINITIALIZED == presenceState)
2139 OCDevAddr multiCastAddr;
2142 presenceState = OC_PRESENCE_INITIALIZED;
2143 OCGenerateCoAPToken(&token);
2144 OCBuildIPv4Address(224, 0, 1, 187, 5683, &multiCastAddr);
2146 CAAddress_t addressInfo;
2147 strncpy(addressInfo.IP.ipAddress, "224.0.1.187", CA_IPADDR_SIZE);
2148 addressInfo.IP.port = 5298;
2150 CAToken_t caToken = NULL;
2151 CAGenerateToken(&caToken);
2153 AddCAObserver(OC_PRESENCE_URI, NULL, 0, &token,
2154 &multiCastAddr, (OCResource *)presenceResource.handle, OC_LOW_QOS,
2155 &addressInfo, CA_WIFI, caToken);
2157 //add the presence observer
2158 AddObserver(OC_PRESENCE_URI, NULL, 0, &token, &multiCastAddr,
2159 (OCResource *)presenceResource.handle, OC_LOW_QOS);
2163 // Each time OCStartPresence is called
2164 // a different random 32-bit integer number is used
2165 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2167 return SendPresenceNotification(NULL);
2171 * When operating in @ref OCServer or @ref OCClientServer mode, this API will stop sending out
2172 * presence notifications to clients via multicast. Once this API has been called with a success,
2173 * this server's stack will not respond to clients querying for this server's presence.
2175 * Server can call this function when it is terminating, going offline, or when going
2176 * away from network.
2179 * OC_STACK_OK - No errors; Success
2181 OCStackResult OCStopPresence()
2183 OCStackResult result = OC_STACK_ERROR;
2184 //make resource inactive
2185 result = OCChangeResourceProperty(
2186 &(((OCResource *) presenceResource.handle)->resourceProperties),
2188 result = SendPresenceNotification(NULL);
2195 OCStackResult OCSetDefaultDeviceEntityHandler(OCDeviceEntityHandler entityHandler)
2197 defaultDeviceHandler = entityHandler;
2202 OCStackResult OCSetDeviceInfo(OCDeviceInfo deviceInfo)
2204 OC_LOG(INFO, TAG, PCF("Entering OCSetDeviceInfo"));
2206 if(myStackMode == OC_CLIENT)
2208 return OC_STACK_ERROR;
2211 return SaveDeviceInfo(deviceInfo);
2217 * @param handle - pointer to handle to newly created resource. Set by ocstack. Used to refer to resource
2218 * @param resourceTypeName - name of resource type. Example: "core.led"
2219 * @param resourceInterfaceName - name of resource interface. Example: "core.rw"
2220 * @param uri - URI of the resource. Example: "/a/led"
2221 * @param entityHandler - entity handler function that is called by ocstack to handle requests, etc
2222 * NULL for default entity handler
2223 * @param resourceProperties - properties supported by resource. Example: OC_DISCOVERABLE|OC_OBSERVABLE
2226 * OC_STACK_OK - no errors
2227 * OC_STACK_ERROR - stack process error
2229 OCStackResult OCCreateResource(OCResourceHandle *handle,
2230 const char *resourceTypeName,
2231 const char *resourceInterfaceName,
2232 const char *uri, OCEntityHandler entityHandler,
2233 uint8_t resourceProperties) {
2235 OCResource *pointer = NULL;
2238 OCStackResult result = OC_STACK_ERROR;
2240 OC_LOG(INFO, TAG, PCF("Entering OCCreateResource"));
2242 if(myStackMode == OC_CLIENT)
2246 // Validate parameters
2247 if(!uri || (strlen(uri) == 0))
2249 OC_LOG(ERROR, TAG, PCF("URI is invalid"));
2250 return OC_STACK_INVALID_URI;
2252 // Is it presented during resource discovery?
2253 if (!handle || !resourceTypeName) {
2254 OC_LOG(ERROR, TAG, PCF("Input parameter is NULL"));
2255 return OC_STACK_INVALID_PARAM;
2258 if(!resourceInterfaceName || strlen(resourceInterfaceName) == 0) {
2259 resourceInterfaceName = OC_RSRVD_INTERFACE_DEFAULT;
2262 // Make sure resourceProperties bitmask has allowed properties specified
2263 if (resourceProperties
2264 > (OC_ACTIVE | OC_DISCOVERABLE | OC_OBSERVABLE | OC_SLOW | OC_SECURE)) {
2265 OC_LOG(ERROR, TAG, PCF("Invalid property"));
2266 return OC_STACK_INVALID_PARAM;
2269 // If the headResource is NULL, then no resources have been created...
2270 pointer = headResource;
2272 // At least one resources is in the resource list, so we need to search for
2273 // repeated URLs, which are not allowed. If a repeat is found, exit with an error
2275 if (strcmp(uri, pointer->uri) == 0) {
2276 OC_LOG(ERROR, TAG, PCF("URI already in use"));
2277 return OC_STACK_INVALID_PARAM;
2279 pointer = pointer->next;
2282 // Create the pointer and insert it into the resource list
2283 pointer = (OCResource *) OCCalloc(1, sizeof(OCResource));
2287 pointer->sequenceNum = OC_OFFSET_SEQUENCE_NUMBER;
2289 insertResource(pointer);
2292 size = strlen(uri) + 1;
2293 str = (char *) OCMalloc(size);
2297 strncpy(str, uri, size);
2300 // Set properties. Set OC_ACTIVE
2301 pointer->resourceProperties = (OCResourceProperty) (resourceProperties
2304 // Add the resourcetype to the resource
2305 result = BindResourceTypeToResource(pointer, resourceTypeName);
2306 if (result != OC_STACK_OK) {
2307 OC_LOG(ERROR, TAG, PCF("Error adding resourcetype"));
2311 // Add the resourceinterface to the resource
2312 result = BindResourceInterfaceToResource(pointer, resourceInterfaceName);
2313 if (result != OC_STACK_OK) {
2314 OC_LOG(ERROR, TAG, PCF("Error adding resourceinterface"));
2318 // If an entity handler has been passed, attach it to the newly created
2319 // resource. Otherwise, set the default entity handler.
2322 pointer->entityHandler = entityHandler;
2326 pointer->entityHandler = defaultResourceEHandler;
2330 result = OC_STACK_OK;
2332 #ifdef WITH_PRESENCE
2333 if(presenceResource.handle)
2335 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2336 SendPresenceNotification(pointer->rsrcType);
2340 if (result != OC_STACK_OK)
2342 // Deep delete of resource and other dynamic elements that it contains
2343 deleteResource(pointer);
2352 * Create a resource. with host ip address for remote resource
2354 * @param handle - pointer to handle to newly created resource. Set by ocstack.
2355 * Used to refer to resource
2356 * @param resourceTypeName - name of resource type. Example: "core.led"
2357 * @param resourceInterfaceName - name of resource interface. Example: "core.rw"
2358 * @param host - HOST address of the remote resource. Example: "coap://xxx.xxx.xxx.xxx:xxxxx"
2359 * @param uri - URI of the resource. Example: "/a/led"
2360 * @param entityHandler - entity handler function that is called by ocstack to handle requests, etc
2361 * NULL for default entity handler
2362 * @param resourceProperties - properties supported by resource.
2363 * Example: OC_DISCOVERABLE|OC_OBSERVABLE
2366 * OC_STACK_OK - no errors
2367 * OC_STACK_ERROR - stack process error
2370 OCStackResult OCCreateResourceWithHost(OCResourceHandle *handle,
2371 const char *resourceTypeName,
2372 const char *resourceInterfaceName,
2375 OCEntityHandler entityHandler,
2376 uint8_t resourceProperties)
2380 OCStackResult result = OC_STACK_ERROR;
2382 result = OCCreateResource(handle, resourceTypeName, resourceInterfaceName,
2383 uri, entityHandler, resourceProperties);
2385 if (result != OC_STACK_ERROR)
2388 size = strlen(host) + 1;
2389 str = (char *) OCMalloc(size);
2392 return OC_STACK_ERROR;
2394 strncpy(str, host, size);
2395 ((OCResource *) *handle)->host = str;
2402 * Add a resource to a collection resource.
2404 * @param collectionHandle - handle to the collection resource
2405 * @param resourceHandle - handle to resource to be added to the collection resource
2408 * OC_STACK_OK - no errors
2409 * OC_STACK_ERROR - stack process error
2410 * OC_STACK_INVALID_PARAM - invalid collectionhandle
2412 OCStackResult OCBindResource(
2413 OCResourceHandle collectionHandle, OCResourceHandle resourceHandle) {
2414 OCResource *resource;
2417 OC_LOG(INFO, TAG, PCF("Entering OCBindResource"));
2419 // Validate parameters
2420 VERIFY_NON_NULL(collectionHandle, ERROR, OC_STACK_ERROR);
2421 VERIFY_NON_NULL(resourceHandle, ERROR, OC_STACK_ERROR);
2422 // Container cannot contain itself
2423 if (collectionHandle == resourceHandle) {
2424 OC_LOG(ERROR, TAG, PCF("Added handle equals collection handle"));
2425 return OC_STACK_INVALID_PARAM;
2428 // Use the handle to find the resource in the resource linked list
2429 resource = findResource((OCResource *) collectionHandle);
2431 OC_LOG(ERROR, TAG, PCF("Collection handle not found"));
2432 return OC_STACK_INVALID_PARAM;
2435 // Look for an open slot to add add the child resource.
2436 // If found, add it and return success
2437 for (i = 0; i < MAX_CONTAINED_RESOURCES; i++) {
2438 if (!resource->rsrcResources[i]) {
2439 resource->rsrcResources[i] = (OCResource *) resourceHandle;
2440 OC_LOG(INFO, TAG, PCF("resource bound"));
2445 #ifdef WITH_PRESENCE
2446 if(presenceResource.handle)
2448 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2449 SendPresenceNotification(((OCResource *) resourceHandle)->rsrcType);
2453 // Unable to add resourceHandle, so return error
2454 return OC_STACK_ERROR;
2458 * Remove a resource from a collection resource.
2460 * @param collectionHandle - handle to the collection resource
2461 * @param resourceHandle - handle to resource to be added to the collection resource
2464 * OC_STACK_OK - no errors
2465 * OC_STACK_ERROR - stack process error
2466 * OC_STACK_INVALID_PARAM - invalid collectionHandle
2468 OCStackResult OCUnBindResource(
2469 OCResourceHandle collectionHandle, OCResourceHandle resourceHandle) {
2470 OCResource *resource;
2473 OC_LOG(INFO, TAG, PCF("Entering OCUnBindResource"));
2475 // Validate parameters
2476 VERIFY_NON_NULL(collectionHandle, ERROR, OC_STACK_ERROR);
2477 VERIFY_NON_NULL(resourceHandle, ERROR, OC_STACK_ERROR);
2478 // Container cannot contain itself
2479 if (collectionHandle == resourceHandle) {
2480 OC_LOG(ERROR, TAG, PCF("removing handle equals collection handle"));
2481 return OC_STACK_INVALID_PARAM;
2484 // Use the handle to find the resource in the resource linked list
2485 resource = findResource((OCResource *) collectionHandle);
2487 OC_LOG(ERROR, TAG, PCF("Collection handle not found"));
2488 return OC_STACK_INVALID_PARAM;
2491 // Look for an open slot to add add the child resource.
2492 // If found, add it and return success
2493 for (i = 0; i < MAX_CONTAINED_RESOURCES; i++) {
2494 if (resourceHandle == resource->rsrcResources[i]) {
2495 resource->rsrcResources[i] = (OCResource *) NULL;
2496 OC_LOG(INFO, TAG, PCF("resource unbound"));
2501 OC_LOG(INFO, TAG, PCF("resource not found in collection"));
2503 #ifdef WITH_PRESENCE
2504 if(presenceResource.handle)
2506 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2507 SendPresenceNotification(((OCResource *) resourceHandle)->rsrcType);
2511 // Unable to add resourceHandle, so return error
2512 return OC_STACK_ERROR;
2515 OCStackResult BindResourceTypeToResource(OCResource* resource,
2516 const char *resourceTypeName)
2518 OCResourceType *pointer = NULL;
2521 OCStackResult result = OC_STACK_ERROR;
2523 OC_LOG(INFO, TAG, PCF("Entering BindResourceTypeToResource"));
2525 // Validate parameters
2526 VERIFY_NON_NULL(resourceTypeName, ERROR, OC_STACK_INVALID_PARAM);
2527 // TODO: Does resource attribute resentation really have to be maintained in stack?
2528 // Is it presented during resource discovery?
2530 TODO ("Make sure that the resourcetypename doesn't already exist in the resource");
2532 // Create the resourcetype and insert it into the resource list
2533 pointer = (OCResourceType *) OCCalloc(1, sizeof(OCResourceType));
2538 // Set the resourceTypeName
2539 size = strlen(resourceTypeName) + 1;
2540 str = (char *) OCMalloc(size);
2544 strncpy(str, resourceTypeName, size);
2545 pointer->resourcetypename = str;
2547 insertResourceType(resource, pointer);
2548 result = OC_STACK_OK;
2550 exit: if (result != OC_STACK_OK) {
2558 OCStackResult BindResourceInterfaceToResource(OCResource* resource,
2559 const char *resourceInterfaceName)
2561 OCResourceInterface *pointer = NULL;
2564 OCStackResult result = OC_STACK_ERROR;
2566 OC_LOG(INFO, TAG, PCF("Entering BindResourceInterfaceToResource"));
2568 // Validate parameters
2569 VERIFY_NON_NULL(resourceInterfaceName, ERROR, OC_STACK_INVALID_PARAM);
2571 TODO ("Make sure that the resourceinterface name doesn't already exist in the resource");
2573 // Create the resourceinterface and insert it into the resource list
2574 pointer = (OCResourceInterface *) OCCalloc(1, sizeof(OCResourceInterface));
2579 // Set the resourceinterface name
2580 size = strlen(resourceInterfaceName) + 1;
2581 str = (char *) OCMalloc(size);
2585 strncpy(str, resourceInterfaceName, size);
2586 pointer->name = str;
2588 // Bind the resourceinterface to the resource
2589 insertResourceInterface(resource, pointer);
2591 result = OC_STACK_OK;
2593 exit: if (result != OC_STACK_OK) {
2602 * Bind a resourcetype to a resource.
2604 * @param handle - handle to the resource
2605 * @param resourceTypeName - name of resource type. Example: "core.led"
2608 * OC_STACK_OK - no errors
2609 * OC_STACK_ERROR - stack process error
2611 OCStackResult OCBindResourceTypeToResource(OCResourceHandle handle,
2612 const char *resourceTypeName) {
2614 OCStackResult result = OC_STACK_ERROR;
2615 OCResource *resource;
2617 // Make sure resource exists
2618 resource = findResource((OCResource *) handle);
2620 OC_LOG(ERROR, TAG, PCF("Resource not found"));
2621 return OC_STACK_ERROR;
2624 // call internal function
2625 result = BindResourceTypeToResource(resource, resourceTypeName);
2627 #ifdef WITH_PRESENCE
2628 if(presenceResource.handle)
2630 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2631 SendPresenceNotification(resource->rsrcType);
2639 * Bind a resourceinterface to a resource.
2641 * @param handle - handle to the resource
2642 * @param resourceInterfaceName - name of resource interface. Example: "oc.mi.b"
2645 * OC_STACK_OK - no errors
2646 * OC_STACK_ERROR - stack process error
2649 OCStackResult OCBindResourceInterfaceToResource(OCResourceHandle handle,
2650 const char *resourceInterfaceName) {
2652 OCStackResult result = OC_STACK_ERROR;
2653 OCResource *resource;
2655 // Make sure resource exists
2656 resource = findResource((OCResource *) handle);
2658 OC_LOG(ERROR, TAG, PCF("Resource not found"));
2659 return OC_STACK_ERROR;
2662 // call internal function
2663 result = BindResourceInterfaceToResource(resource, resourceInterfaceName);
2665 #ifdef WITH_PRESENCE
2666 if(presenceResource.handle)
2668 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2669 SendPresenceNotification(resource->rsrcType);
2677 * Get the number of resources that have been created in the stack.
2679 * @param numResources - pointer to count variable
2682 * OC_STACK_OK - no errors
2683 * OC_STACK_ERROR - stack process error
2686 OCStackResult OCGetNumberOfResources(uint8_t *numResources) {
2687 OCResource *pointer = headResource;
2689 OC_LOG(INFO, TAG, PCF("Entering OCGetNumberOfResources"));
2690 VERIFY_NON_NULL(numResources, ERROR, OC_STACK_INVALID_PARAM);
2693 *numResources = *numResources + 1;
2694 pointer = pointer->next;
2700 * Get a resource handle by index.
2702 * @param index - index of resource, 0 to Count - 1
2705 * Resource handle - if found
2706 * NULL - if not found
2708 OCResourceHandle OCGetResourceHandle(uint8_t index) {
2709 OCResource *pointer = headResource;
2712 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceHandle"));
2714 // Iterate through the list
2715 while ((i < index) && pointer) {
2717 pointer = pointer->next;
2719 return (OCResourceHandle) pointer;
2723 * Delete resource specified by handle. Deletes resource and all resourcetype and resourceinterface
2726 * @param handle - handle of resource to be deleted
2729 * OC_STACK_OK - no errors
2730 * OC_STACK_ERROR - stack process error
2731 * OC_STACK_NO_RESOURCE - resource not found
2732 * OC_STACK_INVALID_PARAM - invalid param
2734 OCStackResult OCDeleteResource(OCResourceHandle handle) {
2735 OC_LOG(INFO, TAG, PCF("Entering OCDeleteResource"));
2738 OC_LOG(ERROR, TAG, PCF("Invalid param"));
2739 return OC_STACK_INVALID_PARAM;
2742 OCResource *resource = findResource((OCResource *) handle);
2743 if (resource == NULL) {
2744 OC_LOG(ERROR, TAG, PCF("Resource not found"));
2745 return OC_STACK_NO_RESOURCE;
2748 if (deleteResource((OCResource *) handle) == 0) {
2749 OC_LOG(ERROR, TAG, PCF("Error deleting resource"));
2750 return OC_STACK_ERROR;
2757 * Get the URI of the resource specified by handle.
2759 * @param handle - handle of resource
2761 * URI string - if resource found
2762 * NULL - resource not found
2764 const char *OCGetResourceUri(OCResourceHandle handle) {
2765 OCResource *resource;
2766 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceUri"));
2768 resource = findResource((OCResource *) handle);
2770 return resource->uri;
2772 return (const char *) NULL;
2776 * Get the properties of the resource specified by handle.
2777 * NOTE: that after a resource is created, the OC_ACTIVE property is set
2778 * for the resource by the stack.
2780 * @param handle - handle of resource
2782 * property bitmap - if resource found
2783 * NULL - resource not found
2785 uint8_t OCGetResourceProperties(OCResourceHandle handle) {
2786 OCResource *resource;
2787 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceProperties"));
2789 resource = findResource((OCResource *) handle);
2791 return resource->resourceProperties;
2797 * Get the number of resource types of the resource.
2799 * @param handle - handle of resource
2800 * @param numResourceTypes - pointer to count variable
2803 * OC_STACK_OK - no errors
2804 * OC_STACK_ERROR - stack process error
2806 OCStackResult OCGetNumberOfResourceTypes(OCResourceHandle handle,
2807 uint8_t *numResourceTypes) {
2808 OCResource *resource;
2809 OCResourceType *pointer;
2811 OC_LOG(INFO, TAG, PCF("Entering OCGetNumberOfResourceTypes"));
2812 VERIFY_NON_NULL(numResourceTypes, ERROR, OC_STACK_INVALID_PARAM);
2813 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
2815 *numResourceTypes = 0;
2817 resource = findResource((OCResource *) handle);
2819 pointer = resource->rsrcType;
2821 *numResourceTypes = *numResourceTypes + 1;
2822 pointer = pointer->next;
2829 * Get name of resource type of the resource.
2831 * @param handle - handle of resource
2832 * @param index - index of resource, 0 to Count - 1
2835 * resource type name - if resource found
2836 * NULL - resource not found
2838 const char *OCGetResourceTypeName(OCResourceHandle handle, uint8_t index) {
2839 OCResourceType *resourceType;
2841 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceTypeName"));
2843 resourceType = findResourceTypeAtIndex(handle, index);
2845 return resourceType->resourcetypename;
2847 return (const char *) NULL;
2853 * Get the number of resource interfaces of the resource.
2855 * @param handle - handle of resource
2856 * @param numResources - pointer to count variable
2859 * OC_STACK_OK - no errors
2860 * OC_STACK_ERROR - stack process error
2862 OCStackResult OCGetNumberOfResourceInterfaces(OCResourceHandle handle,
2863 uint8_t *numResourceInterfaces) {
2864 OCResourceInterface *pointer;
2865 OCResource *resource;
2867 OC_LOG(INFO, TAG, PCF("Entering OCGetNumberOfResourceInterfaces"));
2869 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
2870 VERIFY_NON_NULL(numResourceInterfaces, ERROR, OC_STACK_INVALID_PARAM);
2872 *numResourceInterfaces = 0;
2873 resource = findResource((OCResource *) handle);
2875 pointer = resource->rsrcInterface;
2877 *numResourceInterfaces = *numResourceInterfaces + 1;
2878 pointer = pointer->next;
2885 * Get name of resource interface of the resource.
2887 * @param handle - handle of resource
2888 * @param index - index of resource, 0 to Count - 1
2891 * resource interface name - if resource found
2892 * NULL - resource not found
2894 const char *OCGetResourceInterfaceName(OCResourceHandle handle, uint8_t index) {
2895 OCResourceInterface *resourceInterface;
2897 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceInterfaceName"));
2899 resourceInterface = findResourceInterfaceAtIndex(handle, index);
2900 if (resourceInterface) {
2901 return resourceInterface->name;
2903 return (const char *) NULL;
2907 * Get resource handle from the collection resource by index.
2909 * @param collectionHandle - handle of collection resource
2910 * @param index - index of contained resource, 0 to Count - 1
2913 * handle to resource - if resource found
2914 * NULL - resource not found
2916 OCResourceHandle OCGetResourceHandleFromCollection(OCResourceHandle collectionHandle,
2918 OCResource *resource;
2920 OC_LOG(INFO, TAG, PCF("Entering OCGetContainedResource"));
2922 if (index >= MAX_CONTAINED_RESOURCES) {
2926 resource = findResource((OCResource *) collectionHandle);
2931 return resource->rsrcResources[index];
2935 * Bind an entity handler to the resource.
2937 * @param handle - handle to the resource that the contained resource is to be bound
2938 * @param entityHandler - entity handler function that is called by ocstack to handle requests, etc
2940 * OC_STACK_OK - no errors
2941 * OC_STACK_ERROR - stack process error
2943 OCStackResult OCBindResourceHandler(OCResourceHandle handle,
2944 OCEntityHandler entityHandler) {
2945 OCResource *resource;
2947 OC_LOG(INFO, TAG, PCF("Entering OCBindResourceHandler"));
2949 // Validate parameters
2950 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
2951 //VERIFY_NON_NULL(entityHandler, ERROR, OC_STACK_INVALID_PARAM);
2953 // Use the handle to find the resource in the resource linked list
2954 resource = findResource((OCResource *)handle);
2956 OC_LOG(ERROR, TAG, PCF("Resource not found"));
2957 return OC_STACK_ERROR;
2961 resource->entityHandler = entityHandler;
2963 #ifdef WITH_PRESENCE
2964 if(presenceResource.handle)
2966 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2967 SendPresenceNotification(resource->rsrcType);
2975 * Get the entity handler for a resource.
2977 * @param handle - handle of resource
2980 * entity handler - if resource found
2981 * NULL - resource not found
2983 OCEntityHandler OCGetResourceHandler(OCResourceHandle handle) {
2984 OCResource *resource;
2986 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceHandler"));
2988 // Use the handle to find the resource in the resource linked list
2989 resource = findResource((OCResource *)handle);
2991 OC_LOG(ERROR, TAG, PCF("Resource not found"));
2996 return resource->entityHandler;
2999 void incrementSequenceNumber(OCResource * resPtr)
3001 // Increment the sequence number
3002 resPtr->sequenceNum += 1;
3003 if (resPtr->sequenceNum == MAX_SEQUENCE_NUMBER)
3005 resPtr->sequenceNum = OC_OFFSET_SEQUENCE_NUMBER+1;
3011 * Notify Presence subscribers that a resource has been modified
3013 * @param resourceType - Handle to the resourceType linked list of resource
3014 * that was modified.
3015 * @param qos - Quality Of Service
3018 #ifdef WITH_PRESENCE
3019 OCStackResult SendPresenceNotification(OCResourceType *resourceType)
3021 OCResource *resPtr = NULL;
3022 OCStackResult result;
3023 OCMethod method = OC_REST_PRESENCE;
3024 uint32_t maxAge = 0;
3025 resPtr = findResource((OCResource *) presenceResource.handle);
3028 return OC_STACK_NO_RESOURCE;
3030 if((((OCResource *) presenceResource.handle)->resourceProperties) & OC_ACTIVE)
3032 maxAge = presenceResource.presenceTTL;
3039 result = SendAllObserverNotification(method, resPtr, maxAge, resourceType, OC_LOW_QOS);
3043 #endif // WITH_PRESENCE
3045 * Notify observers that an observed value has changed.
3047 * @param handle - handle of resource
3050 * OC_STACK_OK - no errors
3051 * OC_STACK_NO_RESOURCE - invalid resource handle
3052 * OC_STACK_NO_OBSERVERS - no more observers intrested in resource
3054 OCStackResult OCNotifyAllObservers(OCResourceHandle handle, OCQualityOfService qos) {
3056 OC_LOG(INFO, TAG, PCF("Entering OCNotifyAllObservers"));
3058 OCResource *resPtr = NULL;
3059 OCStackResult result;
3060 OCMethod method = OC_REST_NOMETHOD;
3061 uint32_t maxAge = 0;
3063 OC_LOG(INFO, TAG, PCF("Entering OCNotifyAllObservers"));
3064 #ifdef WITH_PRESENCE
3065 if(handle == presenceResource.handle)
3069 #endif // WITH_PRESENCE
3070 VERIFY_NON_NULL(handle, ERROR, OC_STACK_ERROR);
3072 // Verify that the resource exists
3073 resPtr = findResource ((OCResource *) handle);
3076 return OC_STACK_NO_RESOURCE;
3080 //only increment in the case of regular observing (not presence)
3081 incrementSequenceNumber(resPtr);
3082 method = OC_REST_OBSERVE;
3083 maxAge = MAX_OBSERVE_AGE;
3084 #ifdef WITH_PRESENCE
3085 result = SendAllObserverNotification (method, resPtr, maxAge, NULL, qos);
3087 result = SendAllObserverNotification (method, resPtr, maxAge, qos);
3094 OCNotifyListOfObservers (OCResourceHandle handle,
3095 OCObservationId *obsIdList,
3096 uint8_t numberOfIds,
3097 unsigned char *notificationJSONPayload,
3098 OCQualityOfService qos)
3100 OC_LOG(INFO, TAG, PCF("Entering OCNotifyListOfObservers"));
3102 OCResource *resPtr = NULL;
3103 //TODO: we should allow the server to define this
3104 uint32_t maxAge = MAX_OBSERVE_AGE;
3106 VERIFY_NON_NULL(handle, ERROR, OC_STACK_ERROR);
3107 VERIFY_NON_NULL(obsIdList, ERROR, OC_STACK_ERROR);
3108 VERIFY_NON_NULL(notificationJSONPayload, ERROR, OC_STACK_ERROR);
3110 // Verify that the resource exists
3111 resPtr = findResource ((OCResource *) handle);
3112 if (NULL == resPtr || myStackMode == OC_CLIENT)
3114 return OC_STACK_NO_RESOURCE;
3118 incrementSequenceNumber(resPtr);
3120 return (SendListObserverNotification(resPtr, obsIdList, numberOfIds,
3121 notificationJSONPayload, maxAge, qos));
3125 * Send a response to a request.
3126 * The response can be a regular, slow, or block (i.e. a response that
3127 * is too large to be sent in a single PDU and must span multiple transmissions)
3129 * @param response - pointer to structure that contains response parameters
3132 * OC_STACK_OK - No errors; Success
3133 * OC_STACK_INVALID_PARAM - Invalid pointer to OCServerResponse
3134 * OC_STACK_INVALID_REQUEST_HANDLE - Request handle not found
3135 * OC_STACK_PERSISTENT_BUFFER_REQUIRED - Block transfer needed for response, so a
3136 * persistent response buffer is necessary
3138 OCStackResult OCDoResponse(OCEntityHandlerResponse *ehResponse)
3140 OCStackResult result = OC_STACK_ERROR;
3141 OCServerRequest *serverRequest = NULL;
3143 OC_LOG(INFO, TAG, PCF("Entering OCDoResponse"));
3145 // Validate input parameters
3146 VERIFY_NON_NULL(ehResponse, ERROR, OC_STACK_INVALID_PARAM);
3147 VERIFY_NON_NULL(ehResponse->requestHandle, ERROR, OC_STACK_INVALID_PARAM);
3149 // TODO: Placeholder for creating a response entry when implementing
3150 // block transfer feature
3152 // If a response payload is present, check if block transfer is required
3153 if (ehResponse->payload && OCIsPacketTransferRequired(NULL,
3154 (const char *)ehResponse->payload, ehResponse->payloadSize))
3156 OC_LOG(INFO, TAG, PCF("Block transfer required"));
3158 // Persistent response buffer is needed for block transfer
3159 if (!ehResponse->persistentBufferFlag)
3161 OC_LOG(WARNING, TAG, PCF("Persistent response buffer required"));
3162 return OC_STACK_PERSISTENT_BUFFER_REQUIRED;
3164 // TODO: Placeholder for block transfer handling
3165 // TODO: Placeholder for setting the the response handle in the OCServerResponse struct
3166 // when implementing the block transfer feature
3171 // Get pointer to request info
3172 serverRequest = GetServerRequestUsingHandle((OCServerRequest *)ehResponse->requestHandle);
3175 result = serverRequest->ehResponseHandler(ehResponse);
3182 * Cancel a response. Applies to a block response
3184 * @param responseHandle - response handle set by stack in OCServerResponse after
3185 * OCDoResponse is called
3188 * OC_STACK_OK - No errors; Success
3189 * OC_STACK_INVALID_PARAM - The handle provided is invalid.
3191 OCStackResult OCCancelResponse(OCResponseHandle responseHandle)
3193 OCStackResult result = OC_STACK_NOTIMPL;
3195 OC_LOG(INFO, TAG, PCF("Entering OCCancelResponse"));
3197 // TODO: validate response handle
3202 //-----------------------------------------------------------------------------
3203 // Private internal function definitions
3204 //-----------------------------------------------------------------------------
3206 * Generate handle of OCDoResource invocation for callback management.
3208 static OCDoHandle GenerateInvocationHandle()
3210 OCDoHandle handle = NULL;
3211 // Generate token here, it will be deleted when the transaction is deleted
3212 handle = (OCDoHandle) OCMalloc(sizeof(uint8_t[MAX_TOKEN_LENGTH]));
3215 OCFillRandomMem((uint8_t*)handle, sizeof(uint8_t[MAX_TOKEN_LENGTH]));
3220 #ifdef WITH_PRESENCE
3221 OCStackResult OCChangeResourceProperty(OCResourceProperty * inputProperty,
3222 OCResourceProperty resourceProperties, uint8_t enable)
3224 if (resourceProperties
3225 > (OC_ACTIVE | OC_DISCOVERABLE | OC_OBSERVABLE | OC_SLOW)) {
3226 OC_LOG(ERROR, TAG, PCF("Invalid property"));
3227 return OC_STACK_INVALID_PARAM;
3231 *inputProperty = (OCResourceProperty) (*inputProperty & ~(resourceProperties));
3235 *inputProperty = (OCResourceProperty) (*inputProperty | resourceProperties);
3242 * Initialize resource data structures, variables, etc.
3244 OCStackResult initResources() {
3245 OCStackResult result = OC_STACK_OK;
3246 // Init application resource vars
3247 headResource = NULL;
3248 // Init Virtual Resources
3249 #ifdef WITH_PRESENCE
3250 presenceResource.presenceTTL = OC_DEFAULT_PRESENCE_TTL;
3251 //presenceResource.token = OCGenerateCoAPToken();
3252 result = OCCreateResource(&presenceResource.handle,
3253 OC_RSRVD_RESOURCE_TYPE_PRESENCE,
3258 //make resource inactive
3259 result = OCChangeResourceProperty(
3260 &(((OCResource *) presenceResource.handle)->resourceProperties),
3267 * Add a resource to the end of the linked list of resources.
3269 * @param resource - resource to be added
3271 void insertResource(OCResource *resource) {
3272 OCResource *pointer;
3274 if (!headResource) {
3275 headResource = resource;
3277 pointer = headResource;
3279 while (pointer->next) {
3280 pointer = pointer->next;
3282 pointer->next = resource;
3284 resource->next = NULL;
3288 * Find a resource in the linked list of resources.
3290 * @param resource - resource to be found
3292 * NULL - resource not found
3293 * pointer to resource - pointer to resource that was found in the linked list
3295 OCResource *findResource(OCResource *resource) {
3296 OCResource *pointer = headResource;
3299 if (pointer == resource) {
3302 pointer = pointer->next;
3307 void deleteAllResources()
3309 OCResource *pointer = headResource;
3314 temp = pointer->next;
3315 #ifdef WITH_PRESENCE
3316 if(pointer != (OCResource *) presenceResource.handle)
3318 #endif // WITH_PRESENCE
3319 deleteResource(pointer);
3320 #ifdef WITH_PRESENCE
3322 #endif // WITH_PRESENCE
3326 #ifdef WITH_PRESENCE
3327 // Ensure that the last resource to be deleted is the presence resource. This allows for all
3328 // presence notification attributed to their deletion to be processed.
3329 deleteResource((OCResource *) presenceResource.handle);
3330 #endif // WITH_PRESENCE
3334 * Delete the resource from the linked list.
3336 * @param resource - resource to be deleted
3341 int deleteResource(OCResource *resource) {
3342 OCResource *prev = NULL;
3345 temp = headResource;
3347 if (temp == resource) {
3348 // Invalidate all Resource Properties.
3349 resource->resourceProperties = (OCResourceProperty) 0;
3350 #ifdef WITH_PRESENCE
3351 if(resource != (OCResource *) presenceResource.handle)
3353 #endif // WITH_PRESENCE
3354 OCNotifyAllObservers((OCResourceHandle)resource, OC_HIGH_QOS);
3355 #ifdef WITH_PRESENCE
3358 if(presenceResource.handle)
3360 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
3361 if(resource != (OCResource *) presenceResource.handle)
3363 SendPresenceNotification(resource->rsrcType);
3367 SendPresenceNotification(NULL);
3372 if (temp == headResource) {
3373 headResource = temp->next;
3375 prev->next = temp->next;
3378 deleteResourceElements(temp);
3391 * Delete all of the dynamically allocated elements that were created for the resource.
3393 * @param resource - specified resource
3395 void deleteResourceElements(OCResource *resource) {
3401 OCFree(resource->uri);
3403 // Delete resourcetype linked list
3404 deleteResourceType(resource->rsrcType);
3406 // Delete resourceinterface linked list
3407 deleteResourceInterface(resource->rsrcInterface);
3411 * Delete all of the dynamically allocated elements that were created for the resource type.
3413 * @param resourceType - specified resource type
3415 void deleteResourceType(OCResourceType *resourceType) {
3416 OCResourceType *pointer = resourceType;
3417 OCResourceType *next;
3420 next = pointer->next;
3421 OCFree(pointer->resourcetypename);
3428 * Delete all of the dynamically allocated elements that were created for the resource interface.
3430 * @param resourceInterface - specified resource interface
3432 void deleteResourceInterface(OCResourceInterface *resourceInterface) {
3433 OCResourceInterface *pointer = resourceInterface;
3434 OCResourceInterface *next;
3437 next = pointer->next;
3438 OCFree(pointer->name);
3445 * Insert a resource type into a resource's resource type linked list.
3447 * @param resource - resource where resource type is to be inserted
3448 * @param resourceType - resource type to be inserted
3450 void insertResourceType(OCResource *resource, OCResourceType *resourceType) {
3451 OCResourceType *pointer;
3453 if (resource && !resource->rsrcType) {
3454 resource->rsrcType = resourceType;
3458 pointer = resource->rsrcType;
3462 pointer = resourceType;
3464 while (pointer->next) {
3465 pointer = pointer->next;
3467 pointer->next = resourceType;
3469 resourceType->next = NULL;
3473 * Get a resource type at the specified index within a resource.
3475 * @param handle - handle of resource
3476 * @param index - index of resource type
3479 * resourcetype - if found
3482 OCResourceType *findResourceTypeAtIndex(OCResourceHandle handle, uint8_t index) {
3483 OCResource *resource;
3484 OCResourceType *pointer;
3487 // Find the specified resource
3488 resource = findResource((OCResource *) handle);
3493 // Make sure a resource has a resourcetype
3494 if (!resource->rsrcType) {
3498 // Iterate through the list
3499 pointer = resource->rsrcType;
3501 while ((i < index) && pointer) {
3503 pointer = pointer->next;
3509 * Finds a resource type in an OCResourceType link-list.
3511 * @param resourceTypeList - the link-list to be searched through
3512 * @param resourceTypeName - the key to search for
3515 * resourceType that matches the key (ie. resourceTypeName)
3516 * NULL - either an invalid parameter or this function was unable to find the key.
3518 OCResourceType *findResourceType(OCResourceType * resourceTypeList, const char * resourceTypeName)
3520 if(resourceTypeList && resourceTypeName)
3522 OCResourceType * rtPointer = resourceTypeList;
3523 while(resourceTypeName && rtPointer)
3525 if(rtPointer->resourcetypename &&
3526 strcmp(resourceTypeName, (const char *)
3527 (rtPointer->resourcetypename)) == 0)
3531 rtPointer = rtPointer->next;
3538 * Insert a resource interface into a resource's resource interface linked list.
3540 * @param resource - resource where resource interface is to be inserted
3541 * @param resourceInterface - resource interface to be inserted
3543 void insertResourceInterface(OCResource *resource,
3544 OCResourceInterface *resourceInterface) {
3545 OCResourceInterface *pointer;
3547 if (!resource->rsrcInterface) {
3548 resource->rsrcInterface = resourceInterface;
3550 pointer = resource->rsrcInterface;
3551 while (pointer->next) {
3552 pointer = pointer->next;
3554 pointer->next = resourceInterface;
3556 resourceInterface->next = NULL;
3560 * Get a resource interface at the specified index within a resource.
3562 * @param handle - handle of resource
3563 * @param index - index of resource interface
3566 * resourceinterface - if found
3569 OCResourceInterface *findResourceInterfaceAtIndex(OCResourceHandle handle,
3571 OCResource *resource;
3572 OCResourceInterface *pointer;
3575 // Find the specified resource
3576 resource = findResource((OCResource *) handle);
3581 // Make sure a resource has a resourceinterface
3582 if (!resource->rsrcInterface) {
3586 // Iterate through the list
3587 pointer = resource->rsrcInterface;
3589 while ((i < index) && pointer) {
3591 pointer = pointer->next;
3597 * Determine if a request/response must be sent in a block transfer because it is too large to be
3598 * sent in a single PDU. This function can be used for either a request or a response
3600 * @param request - NULL or pointer to request
3601 * @param response - NULL or pointer to response
3602 * @param size - 0 or size of the request/response. If 0, strlen is used for determining
3603 * the length of the request/response
3606 * 0 - packet transfer NOT required (i.e. normal request/response)
3607 * 1 - packet transfer required (i.e. block transfer needed)
3609 uint8_t OCIsPacketTransferRequired(const char *request, const char *response, uint16_t size)
3613 // Determine if we are checking a request or a response
3616 // If size is greater than 0, use it for the request size value, otherwise
3617 // assume request is null terminated and use strlen for size value
3618 if ((size > MAX_REQUEST_LENGTH) || (strlen(request) > MAX_REQUEST_LENGTH))
3625 // If size is greater than 0, use it for the response size value, otherwise
3626 // assume response is null terminated and use strlen for size value
3627 if ((size > MAX_RESPONSE_LENGTH) || (strlen(response) > MAX_RESPONSE_LENGTH))
3636 * Retrieves a resource type based upon a query ontains only just one
3637 * resource attribute (and that has to be of type "rt").
3639 * @remark This API malloc's memory for the resource type. Do not malloc resourceType
3640 * before passing in.
3642 * @param query - The quert part of the URI
3643 * @param resourceType - The resource type to be populated; pass by reference.
3646 * OC_STACK_INVALID_PARAM - Returns this if the resourceType parameter is invalid/NULL.
3647 * OC_STACK_OK - Success
3649 OCStackResult getResourceType(const char * query, unsigned char** resourceType)
3653 return OC_STACK_INVALID_PARAM;
3656 OCStackResult result = OC_STACK_ERROR;
3658 if(strncmp(query, "rt=", 3) == 0)
3660 *resourceType = (unsigned char *) OCMalloc(strlen(query)-3);
3663 result = OC_STACK_NO_MEMORY;
3666 strcpy((char *)*resourceType, ((const char *)&query[3]));
3667 result = OC_STACK_OK;
3673 OCStackResult getQueryFromUri(const char * uri, unsigned char** query, char ** newURI)
3677 return OC_STACK_INVALID_URI;
3679 if(!query || !newURI)
3681 return OC_STACK_INVALID_PARAM;
3683 char * leftToken = NULL;
3684 char * tempURI = (char *) OCMalloc(strlen(uri) + 1);
3689 strcpy(tempURI, uri);
3691 leftToken = strtok_r((char *)tempURI, "?", &strTokPtr);
3693 //TODO-CA: This could be simplified. Clean up required.
3694 while(leftToken != NULL)
3696 if(strncmp(leftToken, "rt=", 3) == 0 || strncmp(leftToken, "if=", 3) == 0)
3698 *query = (unsigned char *) OCMalloc(strlen(leftToken));
3703 strcpy((char *)*query, ((const char *)&leftToken[0]));
3706 leftToken = strtok_r(NULL, "?", &strTokPtr);
3714 return OC_STACK_NO_MEMORY;
3717 const ServerID OCGetServerInstanceID(void)
3719 static bool generated = false;
3720 static ServerID sid;
3727 sid = OCGetRandom();
3732 const char* OCGetServerInstanceIDString(void)
3734 // max printed length of a base 10
3735 // uint32 is 10 characters, so 11 includes null.
3736 // This will change as the representation gets switched
3738 static char buffer[11];
3739 int n = sprintf(buffer, "%u", OCGetServerInstanceID());