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"
38 #include "ocsecurityinternal.h"
41 #include "cainterface.h"
44 #include <arpa/inet.h>
47 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
51 OC_STACK_UNINITIALIZED = 0, OC_STACK_INITIALIZED, OC_STACK_UNINIT_IN_PROGRESS
55 OC_PRESENCE_UNINITIALIZED = 0, OC_PRESENCE_INITIALIZED
59 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
62 static OCStackState stackState = OC_STACK_UNINITIALIZED;
64 OCResource *headResource = NULL;
66 static OCPresenceState presenceState = OC_PRESENCE_UNINITIALIZED;
67 static PresenceResource presenceResource;
68 uint8_t PresenceTimeOutSize = 0;
69 uint32_t PresenceTimeOut[] = {50, 75, 85, 95, 100};
73 OCDeviceEntityHandler defaultDeviceHandler;
74 OCStackResult getQueryFromUri(const char * uri, unsigned char** resourceType, char ** newURI);
76 //-----------------------------------------------------------------------------
78 //-----------------------------------------------------------------------------
79 #define TAG PCF("OCStack")
80 #define VERIFY_SUCCESS(op, successCode) { if (op != successCode) \
81 {OC_LOG_V(FATAL, TAG, "%s failed!!", #op); goto exit;} }
82 #define VERIFY_NON_NULL(arg, logLevel, retVal) { if (!(arg)) { OC_LOG((logLevel), \
83 TAG, PCF(#arg " is NULL")); return (retVal); } }
84 #define VERIFY_NON_NULL_V(arg) { if (!arg) {OC_LOG_V(FATAL, TAG, "%s is NULL", #arg);\
87 //TODO: we should allow the server to define this
88 #define MAX_OBSERVE_AGE (0x2FFFFUL)
90 //-----------------------------------------------------------------------------
91 // Internal API function
92 //-----------------------------------------------------------------------------
94 // This internal function is called to update the stack with the status of
95 // observers and communication failures
96 OCStackResult OCStackFeedBack(CAToken_t * token, uint8_t status)
98 OCStackResult result = OC_STACK_ERROR;
99 ResourceObserver * observer = NULL;
100 OCEntityHandlerRequest ehRequest = {0};
104 case OC_OBSERVER_NOT_INTERESTED:
105 OC_LOG(DEBUG, TAG, PCF("observer is not interested in our notifications anymore"));
106 observer = GetObserverUsingToken (token);
109 result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) NULL,
110 OC_REST_NOMETHOD, (OCResourceHandle) NULL, NULL, NULL, 0,
111 NULL, OC_OBSERVE_DEREGISTER, observer->observeId);
112 if(result != OC_STACK_OK)
116 observer->resource->entityHandler(OC_OBSERVE_FLAG, &ehRequest);
118 //observer is not observing anymore
119 result = DeleteObserverUsingToken (token);
120 if(result == OC_STACK_OK)
122 OC_LOG(DEBUG, TAG, PCF("Removed observer successfully"));
126 result = OC_STACK_OK;
127 OC_LOG(DEBUG, TAG, PCF("Observer Removal failed"));
130 case OC_OBSERVER_STILL_INTERESTED:
131 //observer is still interested
132 OC_LOG(DEBUG, TAG, PCF("observer is interested in our \
133 notifications, reset the failedCount"));
134 observer = GetObserverUsingToken (token);
137 observer->forceHighQos = 0;
138 observer->failedCommCount = 0;
139 result = OC_STACK_OK;
143 result = OC_STACK_OBSERVER_NOT_FOUND;
146 case OC_OBSERVER_FAILED_COMM:
147 //observer is not reachable
148 OC_LOG(DEBUG, TAG, PCF("observer is unreachable"));
149 observer = GetObserverUsingToken (token);
152 if(observer->failedCommCount >= MAX_OBSERVER_FAILED_COMM)
154 result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) NULL,
155 OC_REST_NOMETHOD, (OCResourceHandle) NULL, NULL, NULL, 0,
156 NULL, OC_OBSERVE_DEREGISTER, observer->observeId);
157 if(result != OC_STACK_OK)
159 return OC_STACK_ERROR;
161 observer->resource->entityHandler(OC_OBSERVE_FLAG, &ehRequest);
162 //observer is unreachable
163 result = DeleteObserverUsingToken (token);
164 if(result == OC_STACK_OK)
166 OC_LOG(DEBUG, TAG, PCF("Removed observer successfully"));
170 result = OC_STACK_OK;
171 OC_LOG(DEBUG, TAG, PCF("Observer Removal failed"));
176 observer->failedCommCount++;
177 result = OC_STACK_CONTINUE;
179 observer->forceHighQos = 1;
180 OC_LOG_V(DEBUG, TAG, "Failed count for this observer is %d",observer->failedCommCount);
184 OC_LOG(ERROR, TAG, PCF("Unknown status"));
185 result = OC_STACK_ERROR;
191 OCStackResult CAToOCStackResult(CAResponseResult_t caCode)
193 OCStackResult ret = OC_STACK_ERROR;
201 ret = OC_STACK_RESOURCE_CREATED;
204 ret = OC_STACK_RESOURCE_DELETED;
207 ret = OC_STACK_INVALID_QUERY;
210 ret = OC_STACK_INVALID_OPTION;
213 ret = OC_STACK_NO_RESOURCE;
221 OCStackResult OCToCAConnectivityType(OCConnectivityType ocConType, CAConnectivityType_t* caConType)
223 OCStackResult ret = OC_STACK_OK;
228 *caConType = CA_ETHERNET;
231 *caConType = CA_WIFI;
240 // Currently OC_ALL represents WIFI and ETHERNET
241 // Add other connectivity types as they are enabled in future
242 *caConType = (CAConnectivityType_t) (CA_WIFI|CA_ETHERNET);
245 ret = OC_STACK_INVALID_PARAM;
251 OCStackResult CAToOCConnectivityType(CAConnectivityType_t caConType, OCConnectivityType *ocConType)
253 OCStackResult ret = OC_STACK_OK;
258 *ocConType = OC_ETHERNET;
261 *ocConType = OC_WIFI;
270 ret = OC_STACK_INVALID_PARAM;
276 // update response.addr appropriately from endPoint.addressInfo
277 OCStackResult UpdateResponseAddr(OCClientResponse *response, const CARemoteEndpoint_t* endPoint)
279 OCStackResult ret = OC_STACK_ERROR;
280 static OCDevAddr address = {0};
282 char * cpAddress = (char *) OCMalloc(strlen(endPoint->addressInfo.IP.ipAddress) + 1);
285 ret = OC_STACK_NO_MEMORY;
288 memcpy(cpAddress, endPoint->addressInfo.IP.ipAddress,
289 strlen(endPoint->addressInfo.IP.ipAddress) + 1);
291 // Grabs the first three numbers from the IPv4 address and replaces dots
292 for(int i=0; i<3; i++)
296 tok = strtok(cpAddress, ".");
300 tok = strtok(NULL, ".");
304 ret = OC_STACK_ERROR;
307 address.addr[i] = atoi(tok);
308 cpAddress[strlen(cpAddress)]='.'; // Replaces the dot here.
311 // Grabs the last number from the IPv4 address - has no dot to replace
312 tok = strtok(NULL, ".");
315 ret = OC_STACK_ERROR;
318 address.addr[3] = atoi(tok);
319 memcpy(&address.addr[4], &endPoint->addressInfo.IP.port, sizeof(uint32_t));
323 response->addr = &address;
324 ret = CAToOCConnectivityType(endPoint->connectivityType, &(response->connType));
328 OC_LOG(ERROR, TAG, PCF("OCClientResponse is NULL!"));
335 void parsePresencePayload(char* payload, uint32_t* seqNum, uint32_t* maxAge, char** resType)
339 // The format of the payload is {"oc":[%u:%u:%s]}
340 // %u : sequence number,
342 // %s : Resource Type (Optional)
343 tok = strtok(payload, "[:]}");
344 payload[strlen(payload)] = ':';
345 tok = strtok(NULL, "[:]}");
346 payload[strlen((char *)payload)] = ':';
347 *seqNum = (uint32_t) atoi(tok);
348 tok = strtok(NULL, "[:]}");
349 *maxAge = (uint32_t) atoi(tok);
350 tok = strtok(NULL, "[:]}");
354 *resType = (char *)OCMalloc(strlen(tok));
359 payload[strlen((char *)payload)] = ':';
360 strcpy(*resType, tok);
361 OC_LOG_V(DEBUG, TAG, "----------------resourceTypeName %s", *resType);
363 payload[strlen((char *)payload)] = ']';
366 OCStackResult HandlePresenceResponse(const CARemoteEndpoint_t* endPoint,
367 const CAResponseInfo_t* responseInfo)
369 OCStackApplicationResult cbResult = OC_STACK_DELETE_TRANSACTION;
370 ClientCB * cbNode = NULL;
371 char *resourceTypeName = NULL;
372 OCClientResponse response;
373 OCStackResult result = OC_STACK_ERROR;
374 uint32_t lowerBound = 0;
375 uint32_t higherBound = 0;
378 char *fullUri = NULL;
379 char *ipAddress = NULL;
380 int presenceSubscribe = 0;
381 int multicastPresenceSubscribe = 0;
383 if (responseInfo->result != CA_SUCCESS)
385 OC_LOG_V(ERROR, TAG, "HandlePresenceResponse failed %d", responseInfo->result);
386 return OC_STACK_ERROR;
389 fullUri = (char *) OCMalloc(MAX_URI_LENGTH );
393 OC_LOG(ERROR, TAG, PCF("Memory could not be allocated for fullUri"));
394 result = OC_STACK_NO_MEMORY;
398 ipAddress = (char *) OCMalloc(strlen(endPoint->addressInfo.IP.ipAddress) + 1);
400 if(NULL == ipAddress)
402 OC_LOG(ERROR, TAG, PCF("Memory could not be allocated for ipAddress"));
403 result = OC_STACK_NO_MEMORY;
407 strncpy(ipAddress, endPoint->addressInfo.IP.ipAddress,
408 strlen(endPoint->addressInfo.IP.ipAddress));
409 ipAddress[strlen(endPoint->addressInfo.IP.ipAddress)] = '\0';
411 snprintf(fullUri, MAX_URI_LENGTH, "coap://%s:%u%s", ipAddress, endPoint->addressInfo.IP.port,
414 cbNode = GetClientCB(NULL, NULL, (unsigned char *) fullUri);
418 presenceSubscribe = 1;
422 snprintf(fullUri, MAX_URI_LENGTH, "%s%s", OC_MULTICAST_IP, endPoint->resourceUri);
423 cbNode = GetClientCB(NULL, NULL, (unsigned char *) fullUri);
426 multicastPresenceSubscribe = 1;
430 if(!presenceSubscribe && !multicastPresenceSubscribe)
432 OC_LOG(INFO, TAG, PCF("Received a presence notification, but I do not have callback \
433 ------------ ignoring"));
437 // No payload to the application in case of presence
438 response.resJSONPayload = NULL;
439 response.result = OC_STACK_OK;
441 result = UpdateResponseAddr(&response, endPoint);
442 if(result != OC_STACK_OK)
447 if(responseInfo->info.payload)
449 parsePresencePayload(responseInfo->info.payload,
450 &(response.sequenceNumber),
457 OC_LOG(INFO, TAG, PCF("===============Stopping presence"));
458 response.result = OC_STACK_PRESENCE_STOPPED;
461 OCFree(cbNode->presence->timeOut);
462 OCFree(cbNode->presence);
463 cbNode->presence = NULL;
466 else if(presenceSubscribe)
468 if(!cbNode->presence)
470 cbNode->presence = (OCPresence *) OCMalloc(sizeof(OCPresence));
471 VERIFY_NON_NULL_V(cbNode->presence);
472 cbNode->presence->timeOut = NULL;
473 cbNode->presence->timeOut = (uint32_t *)
474 OCMalloc(PresenceTimeOutSize * sizeof(uint32_t));
475 if(!(cbNode->presence->timeOut)){
476 OCFree(cbNode->presence);
477 result = OC_STACK_NO_MEMORY;
481 OC_LOG_V(INFO, TAG, "===============Update presence TTL, now time is %u", GetTime(0));
482 cbNode->presence->TTL = maxAge;
483 for(int index = 0; index < PresenceTimeOutSize; index++)
485 lowerBound = GetTime(((float)(PresenceTimeOut[index])
486 /(float)100)*(float)cbNode->presence->TTL);
487 higherBound = GetTime(((float)(PresenceTimeOut[index + 1])
488 /(float)100)*(float)cbNode->presence->TTL);
489 cbNode->presence->timeOut[index] = OCGetRandomRange(lowerBound, higherBound);
490 OC_LOG_V(DEBUG, TAG, "----------------lowerBound timeout %d", lowerBound);
491 OC_LOG_V(DEBUG, TAG, "----------------higherBound timeout %d", higherBound);
492 OC_LOG_V(DEBUG, TAG, "----------------timeOut entry %d",
493 cbNode->presence->timeOut[index]);
495 cbNode->presence->TTLlevel = 0;
496 OC_LOG_V(DEBUG, TAG, "----------------this TTL level %d", cbNode->presence->TTLlevel);
497 if(cbNode->sequenceNumber == response.sequenceNumber)
499 OC_LOG(INFO, TAG, PCF("===============No presence change"));
502 OC_LOG(INFO, TAG, PCF("===============Presence changed, calling up the stack"));
503 cbNode->sequenceNumber = response.sequenceNumber;
505 // Ensure that a filter is actually applied.
506 if(resourceTypeName && cbNode->filterResourceType)
508 if(!findResourceType(cbNode->filterResourceType, resourceTypeName))
516 // This is the multicast case
518 OCMulticastNode* mcNode = NULL;
519 mcNode = GetMCPresenceNode((const unsigned char *)fullUri);
523 if(mcNode->nonce == response.sequenceNumber)
525 OC_LOG(INFO, TAG, PCF("===============No presence change (Multicast)"));
528 mcNode->nonce = response.sequenceNumber;
532 uint32_t uriLen = strlen((char*)fullUri);
533 unsigned char* uri = (unsigned char *) OCMalloc(uriLen + 1);
536 memcpy(uri, fullUri, (uriLen + 1));
541 PCF("===============No Memory for URI to store in the presence node"));
542 result = OC_STACK_NO_MEMORY;
545 result = AddMCPresenceNode(&mcNode, (unsigned char*) uri, response.sequenceNumber);
546 if(result == OC_STACK_NO_MEMORY)
549 PCF("===============No Memory for Multicast Presence Node"));
550 result = OC_STACK_NO_MEMORY;
555 // Ensure that a filter is actually applied.
556 if(resourceTypeName && cbNode->filterResourceType)
558 if(!findResourceType(cbNode->filterResourceType, resourceTypeName))
565 cbResult = cbNode->callBack(cbNode->context, cbNode->handle, &response);
567 if (cbResult == OC_STACK_DELETE_TRANSACTION)
569 FindAndDeleteClientCB(cbNode);
575 OCFree(resourceTypeName);
580 //This function will be called back by CA layer when a response is received
581 void HandleCAResponses(const CARemoteEndpoint_t* endPoint, const CAResponseInfo_t* responseInfo)
583 OC_LOG(INFO, TAG, PCF("Enter HandleCAResponses"));
585 OCStackApplicationResult appResult = OC_STACK_DELETE_TRANSACTION;
589 OC_LOG(ERROR, TAG, PCF("endPoint is NULL"));
593 if(NULL == responseInfo)
595 OC_LOG(ERROR, TAG, PCF("responseInfo is NULL"));
599 if(strcmp(endPoint->resourceUri, OC_PRESENCE_URI) == 0)
601 HandlePresenceResponse(endPoint, responseInfo);
605 ClientCB *cbNode = GetClientCB((CAToken_t *)&(responseInfo->info.token), NULL, NULL);
609 OC_LOG(INFO, TAG, PCF("Calling into application address space"));
610 OCClientResponse response;
612 OCStackResult result = UpdateResponseAddr(&response, endPoint);
613 if(result != OC_STACK_OK)
615 OC_LOG(ERROR, TAG, PCF("Invalid connectivity type in endpoint"));
619 response.result = CAToOCStackResult(responseInfo->result);
620 response.resJSONPayload = (unsigned char*)responseInfo->info.payload;
621 response.numRcvdVendorSpecificHeaderOptions = 0;
622 if(responseInfo->info.numOptions > 0)
625 //First option always with option ID is COAP_OPTION_OBSERVE if it is available.
626 if(responseInfo->info.options[0].optionID == COAP_OPTION_OBSERVE)
628 memcpy (&(response.sequenceNumber),
629 &(responseInfo->info.options[0].optionData), sizeof(uint32_t));
630 response.numRcvdVendorSpecificHeaderOptions = responseInfo->info.numOptions - 1;
635 response.numRcvdVendorSpecificHeaderOptions = responseInfo->info.numOptions;
638 if(response.numRcvdVendorSpecificHeaderOptions > MAX_HEADER_OPTIONS)
640 OC_LOG(ERROR, TAG, PCF("#header options are more than MAX_HEADER_OPTIONS"));
644 for (uint8_t i = start; i < responseInfo->info.numOptions; i++)
646 memcpy (&(response.rcvdVendorSpecificHeaderOptions[i-start]),
647 &(responseInfo->info.options[i]), sizeof(OCHeaderOption));
650 appResult = cbNode->callBack(cbNode->context,
651 cbNode->handle, &response);
652 if (appResult == OC_STACK_DELETE_TRANSACTION)
654 FindAndDeleteClientCB(cbNode);
657 OC_LOG_V(INFO, TAG, PCF("Received payload: %s\n"), (char*)responseInfo->info.payload);
658 OC_LOG(INFO, TAG, PCF("Exit HandleCAResponses"));
661 //This function will be called back by CA layer when a request is received
662 void HandleCARequests(const CARemoteEndpoint_t* endPoint, const CARequestInfo_t* requestInfo)
664 OC_LOG(INFO, TAG, PCF("Enter HandleCARequests"));
667 OC_LOG(ERROR, TAG, PCF("endPoint is NULL"));
673 OC_LOG(ERROR, TAG, PCF("requestInfo is NULL"));
677 OCStackResult requestResult = OC_STACK_ERROR;
679 if(myStackMode == OC_CLIENT)
681 //TODO: should the client be responding to requests?
685 OCServerProtocolRequest serverRequest;
687 memset (&serverRequest, 0, sizeof(OCServerProtocolRequest));
688 OC_LOG_V(INFO, TAG, PCF("***** Endpoint URI ***** : %s\n"), (char*)endPoint->resourceUri);
690 char * newUri = (char *)endPoint->resourceUri;
691 unsigned char * query = NULL;
692 getQueryFromUri(endPoint->resourceUri, &query, &newUri);
693 OC_LOG_V(INFO, TAG, PCF("**********URI without query ****: %s\n"), newUri);
694 OC_LOG_V(INFO, TAG, PCF("**********Query ****: %s\n"), query);
696 memcpy (&(serverRequest.resourceUrl), newUri, strlen(newUri));
700 memcpy (&(serverRequest.query), query, strlen((char*)query));
702 //copy request payload
703 if (requestInfo->info.payload)
705 serverRequest.reqTotalSize = strlen(requestInfo->info.payload) + 1;
706 memcpy (&(serverRequest.reqJSONPayload), requestInfo->info.payload,
707 strlen(requestInfo->info.payload));
708 serverRequest.reqTotalSize = strlen((const char *)requestInfo->info.payload) + 1;
712 serverRequest.reqTotalSize = 1;
715 switch (requestInfo->method)
719 serverRequest.method = OC_REST_GET;
724 serverRequest.method = OC_REST_PUT;
729 serverRequest.method = OC_REST_POST;
734 serverRequest.method = OC_REST_DELETE;
739 OC_LOG(ERROR, TAG, PCF("Received CA method %d not supported"));
744 OC_LOG_V(INFO, TAG, "HandleCARequests: CA token length = %d", CA_MAX_TOKEN_LEN);
745 OC_LOG_BUFFER(INFO, TAG, (const unsigned char *)requestInfo->info.token, CA_MAX_TOKEN_LEN);
747 serverRequest.requestToken = (CAToken_t)OCMalloc(CA_MAX_TOKEN_LEN+1);
749 if (!serverRequest.requestToken)
751 OC_LOG(FATAL, TAG, "Server Request Token is NULL");
754 memset(serverRequest.requestToken, 0, CA_MAX_TOKEN_LEN + 1);
755 memcpy(serverRequest.requestToken, requestInfo->info.token, CA_MAX_TOKEN_LEN);
757 if (requestInfo->info.type == CA_MSG_CONFIRM)
759 serverRequest.qos = OC_HIGH_QOS;
761 else if (requestInfo->info.type == CA_MSG_NONCONFIRM)
763 serverRequest.qos = OC_LOW_QOS;
765 else if (requestInfo->info.type == CA_MSG_ACKNOWLEDGE)
767 // TODO-CA: Need to handle this
769 else if (requestInfo->info.type == CA_MSG_RESET)
771 // TODO-CA: Need to handle this
773 // CA does not need the following 3 fields
774 serverRequest.coapID = 0;
775 serverRequest.delayedResNeeded = 0;
776 serverRequest.secured = endPoint->isSecured;
779 serverRequest.addressInfo = endPoint->addressInfo;
780 serverRequest.connectivityType = endPoint->connectivityType;
782 // copy vendor specific header options
783 // TODO-CA: CA is including non-vendor header options as well, like observe.
784 // Need to filter those out
785 uint8_t tempNum = (requestInfo->info.numOptions);
786 GetObserveHeaderOption(&serverRequest.observationOption, requestInfo->info.options, &tempNum);
787 if (requestInfo->info.numOptions > MAX_HEADER_OPTIONS)
790 PCF("The request info numOptions is greater than MAX_HEADER_OPTIONS"));
793 serverRequest.numRcvdVendorSpecificHeaderOptions = tempNum;
794 if (serverRequest.numRcvdVendorSpecificHeaderOptions)
796 memcpy (&(serverRequest.rcvdVendorSpecificHeaderOptions), requestInfo->info.options,
797 sizeof(CAHeaderOption_t)*tempNum);
800 requestResult = HandleStackRequests (&serverRequest);
801 if(requestResult != OC_STACK_OK)
803 OC_LOG(ERROR, TAG, PCF("HandleStackRequests failed"));
805 OC_LOG(INFO, TAG, PCF("Exit HandleCARequests"));
808 //This function will be called back by occoap layer when a request is received
809 OCStackResult HandleStackRequests(OCServerProtocolRequest * protocolRequest)
811 OC_LOG(INFO, TAG, PCF("Entering HandleStackRequests (OCStack Layer)"));
812 OCStackResult result = OC_STACK_ERROR;
813 ResourceHandling resHandling;
814 OCResource *resource;
817 OC_LOG(ERROR, TAG, PCF("protocolRequest is NULL"));
818 return OC_STACK_INVALID_PARAM;
821 OCServerRequest * request = GetServerRequestUsingToken(protocolRequest->requestToken);
824 OC_LOG(INFO, TAG, PCF("This is a new Server Request"));
825 result = AddServerRequest(&request, protocolRequest->coapID,
826 protocolRequest->delayedResNeeded, protocolRequest->secured, 0,
827 protocolRequest->method, protocolRequest->numRcvdVendorSpecificHeaderOptions,
828 protocolRequest->observationOption, protocolRequest->qos,
829 protocolRequest->query, protocolRequest->rcvdVendorSpecificHeaderOptions,
830 protocolRequest->reqJSONPayload, &protocolRequest->requestToken,
831 &protocolRequest->requesterAddr, protocolRequest->resourceUrl,
832 protocolRequest->reqTotalSize,
833 &protocolRequest->addressInfo, protocolRequest->connectivityType);
834 if (OC_STACK_OK != result)
836 OC_LOG(ERROR, TAG, PCF("Error adding server request"));
839 VERIFY_NON_NULL(request, ERROR, OC_STACK_NO_MEMORY);
841 if(!protocolRequest->reqMorePacket)
843 request->requestComplete = 1;
848 OC_LOG(INFO, TAG, PCF("This is either a repeated Server Request or blocked Server Request"));
851 if(request->requestComplete)
853 OC_LOG(INFO, TAG, PCF("This Server Request is complete"));
854 result = DetermineResourceHandling (request, &resHandling, &resource);
855 if (result == OC_STACK_OK)
857 result = ProcessRequest(resHandling, resource, request);
861 result = OC_STACK_ERROR;
866 OC_LOG(INFO, TAG, PCF("This Server Request is incomplete"));
867 result = OC_STACK_CONTINUE;
872 //This function will be called back by occoap layer when a response is received
873 OCStackResult HandleStackResponses(OCResponse * response)
875 OC_LOG(INFO, TAG, PCF("Entering HandleStackResponses (OCStack Layer)"));
876 OCStackResult result = OC_STACK_OK;
877 OCStackApplicationResult cbResult = OC_STACK_DELETE_TRANSACTION;
878 uint8_t isObserveNotification = 0;
879 ClientCB * cbNode = NULL;
882 OC_LOG(ERROR, TAG, PCF("response is NULL"));
883 return OC_STACK_INVALID_PARAM;
886 uint8_t isPresenceNotification = 0;
887 uint8_t isMulticastPresence = 0;
888 char * resourceTypeName = NULL;
889 uint32_t lowerBound = 0;
890 uint32_t higherBound = 0;
892 unsigned char * bufRes = response->bufRes;
893 #endif // WITH_PRESENCE
895 cbNode = response->cbNode;
898 cbNode = GetClientCB(response->rcvdToken, NULL, NULL);
901 if(response->clientResponse->sequenceNumber >= OC_OFFSET_SEQUENCE_NUMBER)
903 isObserveNotification = 1;
904 OC_LOG(INFO, TAG, PCF("Received an observe notification"));
907 OC_LOG_V(DEBUG, TAG, "The sequenceNumber/NONCE of this response %u",
908 response->clientResponse->sequenceNumber);
909 OC_LOG_V(DEBUG, TAG, "The maxAge/TTL of this response %u", response->maxAge);
910 OC_LOG_V(DEBUG, TAG, "The response received is %s", bufRes);
913 if(!strcmp((char *)response->rcvdUri, (char *)OC_PRESENCE_URI)){
914 isPresenceNotification = 1;
917 result = OC_STACK_INVALID_PARAM;
920 tok = strtok((char *)bufRes, "[:]}");
921 bufRes[strlen((char *)bufRes)] = ':';
922 tok = strtok(NULL, "[:]}");
923 bufRes[strlen((char *)bufRes)] = ':';
924 response->clientResponse->sequenceNumber = (uint32_t )atoi(tok);
925 OC_LOG_V(DEBUG, TAG, "The received NONCE is %u", response->clientResponse->sequenceNumber);
926 tok = strtok(NULL, "[:]}");
927 response->maxAge = (uint32_t )atoi(tok);
928 OC_LOG_V(DEBUG, TAG, "The received TTL is %u", response->maxAge);
929 tok = strtok(NULL, "[:]}");
932 resourceTypeName = (char *)OCMalloc(strlen(tok));
933 if(!resourceTypeName)
937 bufRes[strlen((char *)bufRes)] = ':';
938 strcpy(resourceTypeName, tok);
939 OC_LOG_V(DEBUG, TAG, "----------------resourceTypeName %s",
942 bufRes[strlen((char *)bufRes)] = ']';
945 // Check if the application subcribed for presence
948 cbNode = GetClientCB(NULL, NULL, response->fullUri);
951 // Check if application subscribed for multicast presence
954 snprintf((char *)response->fullUri, MAX_URI_LENGTH, "%s%s",
955 OC_MULTICAST_IP, response->rcvdUri);
956 cbNode = GetClientCB(NULL, NULL, response->fullUri);
959 isMulticastPresence = 1;
960 isPresenceNotification = 0;
964 if(cbNode && isPresenceNotification)
966 OC_LOG(INFO, TAG, PCF("Received a presence notification"));
967 if(!cbNode->presence)
969 cbNode->presence = (OCPresence *) OCMalloc(sizeof(OCPresence));
970 VERIFY_NON_NULL_V(cbNode->presence);
971 cbNode->presence->timeOut = NULL;
972 cbNode->presence->timeOut = (uint32_t *)
973 OCMalloc(PresenceTimeOutSize * sizeof(uint32_t));
974 if(!(cbNode->presence->timeOut)){
975 OCFree(cbNode->presence);
976 result = OC_STACK_NO_MEMORY;
979 if(response->maxAge == 0)
981 OC_LOG(INFO, TAG, PCF("===============Stopping presence"));
982 response->clientResponse->result = OC_STACK_PRESENCE_STOPPED;
985 OCFree(cbNode->presence->timeOut);
986 OCFree(cbNode->presence);
987 cbNode->presence = NULL;
992 OC_LOG_V(INFO, TAG, "===============Update presence TTL, now time is %d", GetTime(0));
993 cbNode->presence->TTL = response->maxAge;
994 for(int index = 0; index < PresenceTimeOutSize; index++)
996 lowerBound = GetTime(((float)(PresenceTimeOut[index])
997 /(float)100)*(float)cbNode->presence->TTL);
998 higherBound = GetTime(((float)(PresenceTimeOut[index + 1])
999 /(float)100)*(float)cbNode->presence->TTL);
1000 cbNode->presence->timeOut[index] = OCGetRandomRange(lowerBound, higherBound);
1001 OC_LOG_V(DEBUG, TAG, "----------------lowerBound timeout %d", lowerBound);
1002 OC_LOG_V(DEBUG, TAG, "----------------higherBound timeout %d", higherBound);
1003 OC_LOG_V(DEBUG, TAG, "----------------timeOut entry %d",
1004 cbNode->presence->timeOut[index]);
1006 cbNode->presence->TTLlevel = 0;
1007 OC_LOG_V(DEBUG, TAG, "----------------this TTL level %d", cbNode->presence->TTLlevel);
1008 if(cbNode->sequenceNumber == response->clientResponse->sequenceNumber)
1010 OC_LOG(INFO, TAG, PCF("===============No presence change"));
1013 OC_LOG(INFO, TAG, PCF("===============Presence changed, calling up the stack"));
1014 cbNode->sequenceNumber = response->clientResponse->sequenceNumber;;
1017 // Ensure that a filter is actually applied.
1018 if(resourceTypeName && cbNode->filterResourceType)
1020 if(!findResourceType(cbNode->filterResourceType, resourceTypeName))
1026 else if(cbNode && isMulticastPresence)
1028 // Check if the same nonce for a given host
1029 OCMulticastNode* mcNode = NULL;
1030 mcNode = GetMCPresenceNode(response->fullUri);
1032 if(response->maxAge == 0)
1034 OC_LOG(INFO, TAG, PCF("===============Stopping presence"));
1035 response->clientResponse->result = OC_STACK_PRESENCE_STOPPED;
1036 if(cbNode->presence)
1038 OCFree(cbNode->presence->timeOut);
1039 OCFree(cbNode->presence);
1040 cbNode->presence = NULL;
1043 else if(mcNode != NULL)
1045 if(mcNode->nonce == response->clientResponse->sequenceNumber)
1047 OC_LOG(INFO, TAG, PCF("===============No presence change (Multicast)"));
1048 result = OC_STACK_NO_MEMORY;
1051 mcNode->nonce = response->clientResponse->sequenceNumber;
1055 uint32_t uriLen = strlen((char*)response->fullUri);
1056 unsigned char* uri = (unsigned char *) OCMalloc(uriLen + 1);
1059 memcpy(uri, response->fullUri, (uriLen + 1));
1064 PCF("===============No Memory for URI to store in the presence node"));
1065 result = OC_STACK_NO_MEMORY;
1068 result = AddMCPresenceNode(&mcNode, (unsigned char*) uri,
1069 response->clientResponse->sequenceNumber);
1070 if(result == OC_STACK_NO_MEMORY)
1073 PCF("===============No Memory for Multicast Presence Node"));
1074 result = OC_STACK_NO_MEMORY;
1079 // Ensure that a filter is actually applied.
1080 if(resourceTypeName && cbNode->filterResourceType)
1082 if(!findResourceType(cbNode->filterResourceType, resourceTypeName))
1089 else if(!cbNode && isPresenceNotification)
1091 OC_LOG(INFO, TAG, PCF("Received a presence notification, but I do not have callback \
1092 ------------ ignoring"));
1094 #endif // WITH_PRESENCE
1098 if(isObserveNotification)
1100 OC_LOG(INFO, TAG, PCF("Received an observe notification"));
1101 //TODO: check the standard for methods to detect wrap around condition
1102 if(cbNode->method == OC_REST_OBSERVE &&
1103 (response->clientResponse->sequenceNumber <= cbNode->sequenceNumber ||
1104 (response->clientResponse->sequenceNumber > cbNode->sequenceNumber &&
1105 response->clientResponse->sequenceNumber ==
1106 MAX_SEQUENCE_NUMBER)))
1108 OC_LOG_V(DEBUG, TAG, "Observe notification came out of order. \
1109 Ignoring Incoming:%d Against Current:%d.",
1110 response->clientResponse->sequenceNumber, cbNode->sequenceNumber);
1113 if(response->clientResponse->sequenceNumber > cbNode->sequenceNumber){
1114 cbNode->sequenceNumber = response->clientResponse->sequenceNumber;
1118 response->clientResponse->resJSONPayload = bufRes;
1120 cbResult = cbNode->callBack(cbNode->context, cbNode->handle, response->clientResponse);
1122 if (cbResult == OC_STACK_DELETE_TRANSACTION ||
1123 response->clientResponse->result == OC_STACK_COMM_ERROR ||
1124 (response->clientResponse->result == OC_STACK_RESOURCE_DELETED &&
1125 !isPresenceNotification && !isMulticastPresence))
1127 FindAndDeleteClientCB(cbNode);
1132 result = OC_STACK_ERROR;
1136 #ifdef WITH_PRESENCE
1137 OCFree(resourceTypeName);
1142 int ParseIPv4Address(unsigned char * ipAddrStr, uint8_t * ipAddr, uint16_t * port)
1145 unsigned char *itr, *coap;
1146 uint8_t dotCount = 0;
1150 /* search for scheme */
1152 if (!isdigit((unsigned char) *ipAddrStr))
1154 coap = (unsigned char *) OC_COAP_SCHEME;
1155 while (*coap && tolower(*itr) == *coap)
1163 while (*ipAddrStr) {
1164 if (isdigit((unsigned char) *ipAddrStr))
1166 ipAddr[index] *= 10;
1167 ipAddr[index] += *ipAddrStr - '0';
1169 else if ((unsigned char) *ipAddrStr == '.')
1181 if(*ipAddrStr == ':')
1185 if (isdigit((unsigned char) *ipAddrStr))
1188 *port += *ipAddrStr - '0';
1199 if (ipAddr[0] < 255 && ipAddr[1] < 255 && ipAddr[2] < 255 && ipAddr[3] < 255
1210 //-----------------------------------------------------------------------------
1211 // Private internal function prototypes
1212 //-----------------------------------------------------------------------------
1214 static OCDoHandle GenerateInvocationHandle();
1215 static OCStackResult initResources();
1216 static void insertResource(OCResource *resource);
1217 static OCResource *findResource(OCResource *resource);
1218 static void insertResourceType(OCResource *resource,
1219 OCResourceType *resourceType);
1220 static OCResourceType *findResourceTypeAtIndex(OCResourceHandle handle,
1222 static void insertResourceInterface(OCResource *resource,
1223 OCResourceInterface *resourceInterface);
1224 static OCResourceInterface *findResourceInterfaceAtIndex(
1225 OCResourceHandle handle, uint8_t index);
1226 static void deleteResourceType(OCResourceType *resourceType);
1227 static void deleteResourceInterface(OCResourceInterface *resourceInterface);
1228 static void deleteResourceElements(OCResource *resource);
1229 static int deleteResource(OCResource *resource);
1230 static void deleteAllResources();
1231 static void incrementSequenceNumber(OCResource * resPtr);
1232 static OCStackResult verifyUriQueryLength(const char * inputUri,
1234 static uint8_t OCIsPacketTransferRequired(const char *request, const char *response, uint16_t size);
1235 OCStackResult getResourceType(const char * query, unsigned char** resourceType);
1237 //-----------------------------------------------------------------------------
1239 //-----------------------------------------------------------------------------
1242 * Initialize the OC Stack. Must be called prior to starting the stack.
1245 * IP Address of host device
1247 * Port of host device
1249 * Host device is client, server, or client-server
1252 * OC_STACK_OK - no errors
1253 * OC_STACK_ERROR - stack init error
1255 OCStackResult OCInit(const char *ipAddr, uint16_t port, OCMode mode)
1257 OCStackResult result = OC_STACK_ERROR;
1258 OC_LOG(INFO, TAG, PCF("Entering OCInit"));
1262 OC_LOG_V(INFO, TAG, "IP Address = %s", ipAddr);
1267 //It is ok to select network to CA_WIFI for now
1269 CAResult_t caResult = CASelectNetwork(CA_ETHERNET);
1271 CAResult_t caResult = CASelectNetwork(CA_WIFI|CA_ETHERNET);
1273 if(caResult == CA_STATUS_OK)
1275 OC_LOG(INFO, TAG, PCF("CASelectNetwork to WIFI"));
1276 CARegisterHandler(HandleCARequests, HandleCAResponses);
1278 OC_LOG(INFO, TAG, PCF("CARegisterHandler..."));
1279 stackState = OC_STACK_INITIALIZED;
1280 result = OC_STACK_OK;
1284 caResult = CAStartDiscoveryServer();
1285 OC_LOG(INFO, TAG, PCF("Client mode: CAStartDiscoveryServer"));
1288 caResult = CAStartListeningServer();
1289 OC_LOG(INFO, TAG, PCF("Server mode: CAStartListeningServer"));
1291 case OC_CLIENT_SERVER:
1292 caResult = CAStartListeningServer();
1293 if(caResult == CA_STATUS_OK)
1295 caResult = CAStartDiscoveryServer();
1297 OC_LOG(INFO, TAG, PCF("Client-server mode"));
1300 OC_LOG(ERROR, TAG, PCF("Invalid mode"));
1301 return OC_STACK_ERROR;
1306 if (caResult == CA_STATUS_OK)
1308 result = OC_STACK_OK;
1312 result = OC_STACK_ERROR;
1317 defaultDeviceHandler = NULL;
1319 #if defined(__WITH_DTLS__)
1320 caResult = CARegisterDTLSCredentialsHandler(GetDtlsPskCredentials);
1321 result = (caResult == CA_STATUS_OK) ? OC_STACK_OK : OC_STACK_ERROR;
1322 #endif // (__WITH_DTLS__)
1324 #ifdef WITH_PRESENCE
1325 PresenceTimeOutSize = sizeof(PresenceTimeOut)/sizeof(PresenceTimeOut[0]) - 1;
1326 #endif // WITH_PRESENCE
1328 if (result == OC_STACK_OK)
1330 stackState = OC_STACK_INITIALIZED;
1332 // Initialize resource
1333 if(result == OC_STACK_OK && myStackMode != OC_CLIENT)
1335 result = initResources();
1337 if(result != OC_STACK_OK)
1339 OC_LOG(ERROR, TAG, PCF("Stack initialization error"));
1345 * Stop the OC stack. Use for a controlled shutdown.
1347 * OC_STACK_OK - no errors
1348 * OC_STACK_ERROR - stack not initialized
1350 OCStackResult OCStop()
1352 OCStackResult result = OC_STACK_ERROR;
1354 OC_LOG(INFO, TAG, PCF("Entering OCStop"));
1356 if (stackState == OC_STACK_UNINIT_IN_PROGRESS)
1358 OC_LOG(DEBUG, TAG, PCF("Stack already stopping, exiting"));
1361 else if (stackState != OC_STACK_INITIALIZED)
1363 OC_LOG(ERROR, TAG, PCF("Stack not initialized"));
1364 return OC_STACK_ERROR;
1367 stackState = OC_STACK_UNINIT_IN_PROGRESS;
1369 #ifdef WITH_PRESENCE
1370 // Ensure that the TTL associated with ANY and ALL presence notifications originating from
1371 // here send with the code "OC_STACK_PRESENCE_STOPPED" result.
1372 presenceResource.presenceTTL = 0;
1373 #endif // WITH_PRESENCE
1375 // Free memory dynamically allocated for resources
1376 deleteAllResources();
1379 //CATerminate does not return any error code. It is OK to assign result to OC_STACK_OK.
1380 result = OC_STACK_OK;
1382 if (result == OC_STACK_OK)
1384 // Remove all observers
1385 DeleteObserverList();
1386 // Remove all the client callbacks
1387 DeleteClientCBList();
1388 stackState = OC_STACK_UNINITIALIZED;
1389 result = OC_STACK_OK;
1391 stackState = OC_STACK_INITIALIZED;
1392 result = OC_STACK_ERROR;
1395 // Deinit security blob
1396 DeinitOCSecurityInfo();
1398 if (result != OC_STACK_OK) {
1399 OC_LOG(ERROR, TAG, PCF("Stack stop error"));
1406 * Map OCQualityOfService to CAMessageType
1408 * @param OCQualityOfService - Input qos.
1410 * Returns CA message type for a given qos.
1412 CAMessageType_t qualityOfServiceToMessageType(OCQualityOfService qos)
1417 return CA_MSG_CONFIRM;
1422 return CA_MSG_NONCONFIRM;
1427 * Verify the lengths of the URI and the query separately
1429 * @param inputUri - Input URI and query.
1430 * @param uriLen - The length of the initial URI with query.
1432 * Note: The '?' that appears after the URI is not considered as
1433 * a part of the query.
1435 OCStackResult verifyUriQueryLength(const char *inputUri, uint16_t uriLen)
1439 query = strchr (inputUri, '?');
1443 if((query - inputUri) > MAX_URI_LENGTH)
1445 return OC_STACK_INVALID_URI;
1448 if((inputUri + uriLen - 1 - query) > MAX_QUERY_LENGTH)
1450 return OC_STACK_INVALID_QUERY;
1453 else if(uriLen > MAX_URI_LENGTH)
1455 return OC_STACK_INVALID_URI;
1461 * Discover or Perform requests on a specified resource (specified by that Resource's respective URI).
1463 * @param handle - @ref OCDoHandle to refer to the request sent out on behalf of calling this API.
1464 * @param method - @ref OCMethod to perform on the resource
1465 * @param requiredUri - URI of the resource to interact with
1466 * @param referenceUri - URI of the reference resource
1467 * @param request - JSON encoded request
1468 * @param qos - quality of service
1469 * @param cbData - struct that contains asynchronous callback function that is invoked
1470 * by the stack when discovery or resource interaction is complete
1471 * @param options - The address of an array containing the vendor specific header
1472 * header options to be sent with the request
1473 * @param numOptions - Number of vendor specific header options to be included
1476 * OC_STACK_OK - no errors
1477 * OC_STACK_INVALID_CALLBACK - invalid callback function pointer
1478 * OC_STACK_INVALID_METHOD - invalid resource method
1479 * OC_STACK_INVALID_URI - invalid required or reference URI
1481 * Note: IN case of CA, when using multicast, the required URI should not contain IP address.
1482 * Instead, it just contains the URI to the resource such as "/oc/core".
1484 OCStackResult OCDoResource(OCDoHandle *handle, OCMethod method, const char *requiredUri,
1485 const char *referenceUri, const char *request, OCConnectivityType conType,
1486 OCQualityOfService qos, OCCallbackData *cbData,
1487 OCHeaderOption * options, uint8_t numOptions)
1489 OCStackResult result = OC_STACK_ERROR;
1490 ClientCB *clientCB = NULL;
1491 unsigned char * requestUri = NULL;
1492 unsigned char * resourceType = NULL;
1493 unsigned char * query = NULL;
1494 char * newUri = (char *)requiredUri;
1495 (void) referenceUri;
1496 CARemoteEndpoint_t* endpoint = NULL;
1497 CAResult_t caResult;
1498 CAToken_t token = NULL;
1499 CAInfo_t requestData;
1500 CARequestInfo_t requestInfo;
1501 CAGroupEndpoint_t grpEnd;
1503 // To track if memory is allocated for additional header options
1504 uint8_t hdrOptionMemAlloc = 0;
1506 OC_LOG(INFO, TAG, PCF("Entering OCDoResource"));
1508 // Validate input parameters
1509 VERIFY_NON_NULL(cbData, FATAL, OC_STACK_INVALID_CALLBACK);
1510 VERIFY_NON_NULL(cbData->cb, FATAL, OC_STACK_INVALID_CALLBACK);
1512 TODO ("Need to form the final query by concatenating require and reference URI's");
1513 VERIFY_NON_NULL(requiredUri, FATAL, OC_STACK_INVALID_URI);
1515 uint16_t uriLen = strlen(requiredUri);
1517 // ToDo: We should also check if the requiredUri has a mutlicast address, then qos has to be OC_Low_QOS
1523 case OC_REST_DELETE:
1524 case OC_REST_OBSERVE:
1525 case OC_REST_OBSERVE_ALL:
1526 case OC_REST_CANCEL_OBSERVE:
1528 #ifdef WITH_PRESENCE
1529 case OC_REST_PRESENCE:
1533 result = OC_STACK_INVALID_METHOD;
1537 if((result = verifyUriQueryLength(requiredUri, uriLen)) != OC_STACK_OK)
1542 if((request) && (strlen(request) > MAX_REQUEST_LENGTH))
1544 result = OC_STACK_INVALID_PARAM;
1548 #ifdef WITH_PRESENCE
1549 if(method == OC_REST_PRESENCE)
1551 result = getQueryFromUri(requiredUri, &query, &newUri);
1554 result = getResourceType((char *) query, &resourceType);
1557 OC_LOG_V(DEBUG, TAG, "Got Resource Type: %s", resourceType);
1561 OC_LOG(DEBUG, TAG, PCF("Resource type is NULL."));
1566 OC_LOG(DEBUG, TAG, PCF("Query string is NULL."));
1568 if(result != OC_STACK_OK)
1573 #endif // WITH_PRESENCE
1575 requestUri = (unsigned char *) OCMalloc(uriLen + 1);
1578 memcpy(requestUri, newUri, (uriLen + 1));
1582 result = OC_STACK_NO_MEMORY;
1586 *handle = GenerateInvocationHandle();
1589 result = OC_STACK_NO_MEMORY;
1593 memset(&requestData, 0, sizeof(CAInfo_t));
1594 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
1595 memset(&grpEnd, 0, sizeof(CAGroupEndpoint_t));
1599 case OC_REST_OBSERVE:
1600 case OC_REST_OBSERVE_ALL:
1601 case OC_REST_CANCEL_OBSERVE:
1603 requestInfo.method = CA_GET;
1608 requestInfo.method = CA_PUT;
1613 requestInfo.method = CA_POST;
1616 case OC_REST_DELETE:
1618 requestInfo.method = CA_DELETE;
1621 #ifdef WITH_PRESENCE
1622 case OC_REST_PRESENCE:
1624 // Replacing method type with GET because "presence"
1625 // is a stack layer only implementation.
1626 requestInfo.method = CA_GET;
1631 result = OC_STACK_INVALID_METHOD;
1635 //High QoS is not supported
1636 if(qos == OC_HIGH_QOS)
1638 result = OC_STACK_INVALID_PARAM;
1643 caResult = CAGenerateToken(&token);
1644 if (caResult != CA_STATUS_OK)
1646 OC_LOG(ERROR, TAG, PCF("CAGenerateToken error"));
1647 CADestroyToken(token);
1651 requestData.type = qualityOfServiceToMessageType(qos);
1652 requestData.token = token;
1653 if ((method == OC_REST_OBSERVE) || (method == OC_REST_OBSERVE_ALL))
1655 result = CreateObserveHeaderOption (&(requestData.options), options,
1656 numOptions, OC_OBSERVE_REGISTER);
1657 if (result != OC_STACK_OK)
1661 hdrOptionMemAlloc = 1;
1662 requestData.numOptions = numOptions + 1;
1666 requestData.options = (CAHeaderOption_t*)options;
1667 requestData.numOptions = numOptions;
1669 requestData.payload = (char *)request;
1671 requestInfo.info = requestData;
1673 CAConnectivityType_t caConType;
1675 result = OCToCAConnectivityType((OCConnectivityType) conType, &caConType);
1676 if (result != OC_STACK_OK)
1678 OC_LOG(ERROR, TAG, PCF("Invalid Connectivity Type"));
1683 if(conType == OC_ALL)
1685 grpEnd.connectivityType = caConType;
1687 grpEnd.resourceUri = (CAURI_t) OCMalloc(uriLen + 1);
1688 if(!grpEnd.resourceUri)
1690 result = OC_STACK_NO_MEMORY;
1693 strncpy(grpEnd.resourceUri, requiredUri, (uriLen + 1));
1695 caResult = CASendRequestToAll(&grpEnd, &requestInfo);
1699 caResult = CACreateRemoteEndpoint(newUri, caConType, &endpoint);
1701 if (caResult != CA_STATUS_OK)
1703 OC_LOG(ERROR, TAG, PCF("CACreateRemoteEndpoint error"));
1707 caResult = CASendRequest(endpoint, &requestInfo);
1710 if (caResult != CA_STATUS_OK)
1712 OC_LOG(ERROR, TAG, PCF("CASendRequest"));
1716 if((result = AddClientCB(&clientCB, cbData, &token, handle, method,
1717 requestUri, resourceType)) != OC_STACK_OK)
1719 result = OC_STACK_NO_MEMORY;
1724 if(newUri != requiredUri)
1728 if (result != OC_STACK_OK)
1730 OC_LOG(ERROR, TAG, PCF("OCDoResource error"));
1731 FindAndDeleteClientCB(clientCB);
1733 CADestroyRemoteEndpoint(endpoint);
1734 OCFree(grpEnd.resourceUri);
1735 if (hdrOptionMemAlloc)
1737 OCFree(requestData.options);
1743 * Cancel a request associated with a specific @ref OCDoResource invocation.
1745 * @param handle - Used to identify a specific OCDoResource invocation.
1746 * @param qos - used to specify Quality of Service (read below for more info)
1747 * @param options- used to specify vendor specific header options when sending
1748 * explicit observe cancellation
1749 * @param numOptions- Number of header options to be included
1752 * OC_STACK_OK - No errors; Success
1753 * OC_STACK_INVALID_PARAM - The handle provided is invalid.
1755 OCStackResult OCCancel(OCDoHandle handle, OCQualityOfService qos, OCHeaderOption * options,
1759 * This ftn is implemented one of two ways in the case of observation:
1761 * 1. qos == OC_NON_CONFIRMABLE. When observe is unobserved..
1762 * Remove the callback associated on client side.
1763 * When the next notification comes in from server,
1764 * reply with RESET message to server.
1765 * Keep in mind that the server will react to RESET only
1766 * if the last notification was sent ans CON
1768 * 2. qos == OC_CONFIRMABLE. When OCCancel is called,
1769 * and it is associated with an observe request
1770 * (i.e. ClientCB->method == OC_REST_OBSERVE || OC_REST_OBSERVE_ALL),
1771 * Send CON Observe request to server with
1772 * observe flag = OC_RESOURCE_OBSERVE_DEREGISTER.
1773 * Remove the callback associated on client side.
1775 OCStackResult ret = OC_STACK_OK;
1776 CARemoteEndpoint_t* endpoint = NULL;
1777 CAResult_t caResult;
1778 CAInfo_t requestData;
1779 CARequestInfo_t requestInfo;
1780 // Track if memory is allocated for additional header options
1781 uint8_t hdrOptionMemAlloc = 0;
1784 return OC_STACK_INVALID_PARAM;
1787 OC_LOG(INFO, TAG, PCF("Entering OCCancel"));
1789 ClientCB *clientCB = GetClientCB(NULL, handle, NULL);
1792 switch (clientCB->method)
1794 case OC_REST_OBSERVE:
1795 case OC_REST_OBSERVE_ALL:
1796 //TODO-CA : Why CA_WIFI alone?
1797 caResult = CACreateRemoteEndpoint((char *)clientCB->requestUri, CA_WIFI,
1799 if (caResult != CA_STATUS_OK)
1801 OC_LOG(ERROR, TAG, PCF("CACreateRemoteEndpoint error"));
1802 return OC_STACK_ERROR;
1805 memset(&requestData, 0, sizeof(CAInfo_t));
1806 requestData.type = qualityOfServiceToMessageType(qos);
1807 requestData.token = clientCB->token;
1808 if (CreateObserveHeaderOption (&(requestData.options),
1809 options, numOptions, OC_OBSERVE_DEREGISTER) != OC_STACK_OK)
1811 return OC_STACK_ERROR;
1813 hdrOptionMemAlloc = 1;
1814 requestData.numOptions = numOptions + 1;
1815 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
1816 requestInfo.method = CA_GET;
1817 requestInfo.info = requestData;
1819 caResult = CASendRequest(endpoint, &requestInfo);
1820 if (caResult != CA_STATUS_OK)
1822 OC_LOG(ERROR, TAG, PCF("CASendRequest error"));
1824 if(caResult == CA_STATUS_OK)
1828 #ifdef WITH_PRESENCE
1829 case OC_REST_PRESENCE:
1830 FindAndDeleteClientCB(clientCB);
1834 return OC_STACK_INVALID_METHOD;
1837 CADestroyRemoteEndpoint(endpoint);
1838 if (hdrOptionMemAlloc)
1840 OCFree(requestData.options);
1846 #ifdef WITH_PRESENCE
1847 OCStackResult OCProcessPresence()
1849 OCStackResult result = OC_STACK_OK;
1850 uint8_t ipAddr[4] = { 0 };
1853 OC_LOG(INFO, TAG, PCF("Entering RequestPresence"));
1854 ClientCB* cbNode = NULL;
1856 OCClientResponse clientResponse;
1857 OCResponse * response = NULL;
1858 OCStackApplicationResult cbResult = OC_STACK_DELETE_TRANSACTION;
1860 LL_FOREACH(cbList, cbNode) {
1861 if(OC_REST_PRESENCE == cbNode->method)
1863 if(cbNode->presence)
1865 uint32_t now = GetTime(0);
1866 OC_LOG_V(DEBUG, TAG, "----------------this TTL level %d",
1867 cbNode->presence->TTLlevel);
1868 OC_LOG_V(DEBUG, TAG, "----------------current ticks %d", now);
1871 if(cbNode->presence->TTLlevel >= (PresenceTimeOutSize + 1))
1876 if(cbNode->presence->TTLlevel < PresenceTimeOutSize){
1877 OC_LOG_V(DEBUG, TAG, "----------------timeout ticks %d",
1878 cbNode->presence->timeOut[cbNode->presence->TTLlevel]);
1881 if(cbNode->presence->TTLlevel >= PresenceTimeOutSize)
1883 OC_LOG(DEBUG, TAG, PCF("----------------No more timeout ticks"));
1884 if (ParseIPv4Address( cbNode->requestUri, ipAddr, &port))
1886 OCBuildIPv4Address(ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3], port,
1888 result = FormOCClientResponse(&clientResponse, OC_STACK_PRESENCE_TIMEOUT,
1889 (OCDevAddr *) &dst, 0, NULL);
1890 if(result != OC_STACK_OK)
1894 result = FormOCResponse(&response, cbNode, 0, NULL, NULL,
1895 &cbNode->token, &clientResponse, NULL);
1896 if(result != OC_STACK_OK)
1901 // Increment the TTLLevel (going to a next state), so we don't keep
1902 // sending presence notification to client.
1903 cbNode->presence->TTLlevel++;
1904 OC_LOG_V(DEBUG, TAG, "----------------moving to TTL level %d",
1905 cbNode->presence->TTLlevel);
1909 result = OC_STACK_INVALID_IP;
1913 cbResult = cbNode->callBack(cbNode->context, cbNode->handle, &clientResponse);
1914 if (cbResult == OC_STACK_DELETE_TRANSACTION)
1916 FindAndDeleteClientCB(cbNode);
1920 if(now >= cbNode->presence->timeOut[cbNode->presence->TTLlevel])
1922 CAResult_t caResult;
1923 CARemoteEndpoint_t* endpoint = NULL;
1924 CAInfo_t requestData;
1925 CARequestInfo_t requestInfo;
1927 OC_LOG(DEBUG, TAG, PCF("time to test server presence =========="));
1929 //TODO-CA : Why CA_WIFI alone?
1930 caResult = CACreateRemoteEndpoint((char *)cbNode->requestUri, CA_WIFI,
1933 if (caResult != CA_STATUS_OK)
1935 OC_LOG(ERROR, TAG, PCF("CACreateRemoteEndpoint error"));
1939 memset(&requestData, 0, sizeof(CAInfo_t));
1940 requestData.type = CA_MSG_NONCONFIRM;
1941 requestData.token = cbNode->token;
1943 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
1944 requestInfo.method = CA_GET;
1945 requestInfo.info = requestData;
1947 caResult = CASendRequest(endpoint, &requestInfo);
1949 if (caResult != CA_STATUS_OK)
1951 OC_LOG(ERROR, TAG, PCF("CASendRequest error"));
1955 cbNode->presence->TTLlevel++;
1956 OC_LOG_V(DEBUG, TAG, "----------------moving to TTL level %d",
1957 cbNode->presence->TTLlevel);
1963 if (result != OC_STACK_OK)
1965 OC_LOG(ERROR, TAG, PCF("OCProcessPresence error"));
1969 #endif // WITH_PRESENCE
1972 * Called in main loop of OC client or server. Allows low-level processing of
1976 * OC_STACK_OK - no errors
1977 * OC_STACK_ERROR - stack process error
1979 OCStackResult OCProcess()
1981 #ifdef WITH_PRESENCE
1982 OCProcessPresence();
1984 CAHandleRequestResponse();
1989 #ifdef WITH_PRESENCE
1991 * When operating in @ref OCServer or @ref OCClientServer mode, this API will start sending out
1992 * presence notifications to clients via multicast. Once this API has been called with a success,
1993 * clients may query for this server's presence and this server's stack will respond via multicast.
1995 * Server can call this function when it comes online for the first time, or when it comes back
1996 * online from offline mode, or when it re enters network.
1998 * @param ttl - Time To Live in seconds
1999 * Note: If ttl is '0', then the default stack value will be used (60 Seconds).
2002 * OC_STACK_OK - No errors; Success
2004 OCStackResult OCStartPresence(const uint32_t ttl)
2006 OCChangeResourceProperty(
2007 &(((OCResource *)presenceResource.handle)->resourceProperties),
2012 presenceResource.presenceTTL = ttl;
2015 if(OC_PRESENCE_UNINITIALIZED == presenceState)
2017 OCDevAddr multiCastAddr;
2018 presenceState = OC_PRESENCE_INITIALIZED;
2020 OCBuildIPv4Address(224, 0, 1, 187, 5683, &multiCastAddr);
2022 CAAddress_t addressInfo;
2023 strncpy(addressInfo.IP.ipAddress, "224.0.1.187", CA_IPADDR_SIZE);
2024 addressInfo.IP.port = 5683;
2026 //TODO make sure there is no memory leak here since another copy
2027 //of token is being created inside AddObserver
2028 CAToken_t caToken = NULL;
2029 CAResult_t caResult = CAGenerateToken(&caToken);
2030 if (caResult != CA_STATUS_OK)
2032 OC_LOG(ERROR, TAG, PCF("CAGenerateToken error"));
2033 CADestroyToken(caToken);
2034 return OC_STACK_ERROR;
2037 AddObserver(OC_PRESENCE_URI, NULL, 0, &caToken,
2038 &multiCastAddr, (OCResource *)presenceResource.handle, OC_LOW_QOS,
2039 &addressInfo, CA_WIFI);
2042 // Each time OCStartPresence is called
2043 // a different random 32-bit integer number is used
2044 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2046 return SendPresenceNotification(NULL);
2050 * When operating in @ref OCServer or @ref OCClientServer mode, this API will stop sending out
2051 * presence notifications to clients via multicast. Once this API has been called with a success,
2052 * this server's stack will not respond to clients querying for this server's presence.
2054 * Server can call this function when it is terminating, going offline, or when going
2055 * away from network.
2058 * OC_STACK_OK - No errors; Success
2060 OCStackResult OCStopPresence()
2062 OCStackResult result = OC_STACK_ERROR;
2063 //make resource inactive
2064 result = OCChangeResourceProperty(
2065 &(((OCResource *) presenceResource.handle)->resourceProperties),
2067 result = SendPresenceNotification(NULL);
2074 OCStackResult OCSetDefaultDeviceEntityHandler(OCDeviceEntityHandler entityHandler)
2076 defaultDeviceHandler = entityHandler;
2081 OCStackResult OCSetDeviceInfo(OCDeviceInfo deviceInfo)
2083 OC_LOG(INFO, TAG, PCF("Entering OCSetDeviceInfo"));
2085 if(myStackMode == OC_CLIENT)
2087 return OC_STACK_ERROR;
2090 return SaveDeviceInfo(deviceInfo);
2096 * @param handle - pointer to handle to newly created resource. Set by ocstack. Used to refer to resource
2097 * @param resourceTypeName - name of resource type. Example: "core.led"
2098 * @param resourceInterfaceName - name of resource interface. Example: "core.rw"
2099 * @param uri - URI of the resource. Example: "/a/led"
2100 * @param entityHandler - entity handler function that is called by ocstack to handle requests, etc
2101 * NULL for default entity handler
2102 * @param resourceProperties - properties supported by resource. Example: OC_DISCOVERABLE|OC_OBSERVABLE
2105 * OC_STACK_OK - no errors
2106 * OC_STACK_ERROR - stack process error
2108 OCStackResult OCCreateResource(OCResourceHandle *handle,
2109 const char *resourceTypeName,
2110 const char *resourceInterfaceName,
2111 const char *uri, OCEntityHandler entityHandler,
2112 uint8_t resourceProperties) {
2114 OCResource *pointer = NULL;
2117 OCStackResult result = OC_STACK_ERROR;
2119 OC_LOG(INFO, TAG, PCF("Entering OCCreateResource"));
2121 if(myStackMode == OC_CLIENT)
2125 // Validate parameters
2126 if(!uri || (strlen(uri) == 0))
2128 OC_LOG(ERROR, TAG, PCF("URI is invalid"));
2129 return OC_STACK_INVALID_URI;
2131 // Is it presented during resource discovery?
2132 if (!handle || !resourceTypeName) {
2133 OC_LOG(ERROR, TAG, PCF("Input parameter is NULL"));
2134 return OC_STACK_INVALID_PARAM;
2137 if(!resourceInterfaceName || strlen(resourceInterfaceName) == 0) {
2138 resourceInterfaceName = OC_RSRVD_INTERFACE_DEFAULT;
2141 // Make sure resourceProperties bitmask has allowed properties specified
2142 if (resourceProperties
2143 > (OC_ACTIVE | OC_DISCOVERABLE | OC_OBSERVABLE | OC_SLOW | OC_SECURE)) {
2144 OC_LOG(ERROR, TAG, PCF("Invalid property"));
2145 return OC_STACK_INVALID_PARAM;
2148 // If the headResource is NULL, then no resources have been created...
2149 pointer = headResource;
2151 // At least one resources is in the resource list, so we need to search for
2152 // repeated URLs, which are not allowed. If a repeat is found, exit with an error
2154 if (strcmp(uri, pointer->uri) == 0) {
2155 OC_LOG(ERROR, TAG, PCF("URI already in use"));
2156 return OC_STACK_INVALID_PARAM;
2158 pointer = pointer->next;
2161 // Create the pointer and insert it into the resource list
2162 pointer = (OCResource *) OCCalloc(1, sizeof(OCResource));
2166 pointer->sequenceNum = OC_OFFSET_SEQUENCE_NUMBER;
2168 insertResource(pointer);
2171 size = strlen(uri) + 1;
2172 str = (char *) OCMalloc(size);
2176 strncpy(str, uri, size);
2179 // Set properties. Set OC_ACTIVE
2180 pointer->resourceProperties = (OCResourceProperty) (resourceProperties
2183 // Add the resourcetype to the resource
2184 result = BindResourceTypeToResource(pointer, resourceTypeName);
2185 if (result != OC_STACK_OK) {
2186 OC_LOG(ERROR, TAG, PCF("Error adding resourcetype"));
2190 // Add the resourceinterface to the resource
2191 result = BindResourceInterfaceToResource(pointer, resourceInterfaceName);
2192 if (result != OC_STACK_OK) {
2193 OC_LOG(ERROR, TAG, PCF("Error adding resourceinterface"));
2197 // If an entity handler has been passed, attach it to the newly created
2198 // resource. Otherwise, set the default entity handler.
2201 pointer->entityHandler = entityHandler;
2205 pointer->entityHandler = defaultResourceEHandler;
2209 result = OC_STACK_OK;
2211 #ifdef WITH_PRESENCE
2212 if(presenceResource.handle)
2214 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2215 SendPresenceNotification(pointer->rsrcType);
2219 if (result != OC_STACK_OK)
2221 // Deep delete of resource and other dynamic elements that it contains
2222 deleteResource(pointer);
2231 * Create a resource. with host ip address for remote resource
2233 * @param handle - pointer to handle to newly created resource. Set by ocstack.
2234 * Used to refer to resource
2235 * @param resourceTypeName - name of resource type. Example: "core.led"
2236 * @param resourceInterfaceName - name of resource interface. Example: "core.rw"
2237 * @param host - HOST address of the remote resource. Example: "coap://xxx.xxx.xxx.xxx:xxxxx"
2238 * @param uri - URI of the resource. Example: "/a/led"
2239 * @param entityHandler - entity handler function that is called by ocstack to handle requests, etc
2240 * NULL for default entity handler
2241 * @param resourceProperties - properties supported by resource.
2242 * Example: OC_DISCOVERABLE|OC_OBSERVABLE
2245 * OC_STACK_OK - no errors
2246 * OC_STACK_ERROR - stack process error
2249 OCStackResult OCCreateResourceWithHost(OCResourceHandle *handle,
2250 const char *resourceTypeName,
2251 const char *resourceInterfaceName,
2254 OCEntityHandler entityHandler,
2255 uint8_t resourceProperties)
2259 OCStackResult result = OC_STACK_ERROR;
2261 result = OCCreateResource(handle, resourceTypeName, resourceInterfaceName,
2262 uri, entityHandler, resourceProperties);
2264 if (result != OC_STACK_ERROR)
2267 size = strlen(host) + 1;
2268 str = (char *) OCMalloc(size);
2271 return OC_STACK_ERROR;
2273 strncpy(str, host, size);
2274 ((OCResource *) *handle)->host = str;
2281 * Add a resource to a collection resource.
2283 * @param collectionHandle - handle to the collection resource
2284 * @param resourceHandle - handle to resource to be added to the collection resource
2287 * OC_STACK_OK - no errors
2288 * OC_STACK_ERROR - stack process error
2289 * OC_STACK_INVALID_PARAM - invalid collectionhandle
2291 OCStackResult OCBindResource(
2292 OCResourceHandle collectionHandle, OCResourceHandle resourceHandle) {
2293 OCResource *resource;
2296 OC_LOG(INFO, TAG, PCF("Entering OCBindResource"));
2298 // Validate parameters
2299 VERIFY_NON_NULL(collectionHandle, ERROR, OC_STACK_ERROR);
2300 VERIFY_NON_NULL(resourceHandle, ERROR, OC_STACK_ERROR);
2301 // Container cannot contain itself
2302 if (collectionHandle == resourceHandle) {
2303 OC_LOG(ERROR, TAG, PCF("Added handle equals collection handle"));
2304 return OC_STACK_INVALID_PARAM;
2307 // Use the handle to find the resource in the resource linked list
2308 resource = findResource((OCResource *) collectionHandle);
2310 OC_LOG(ERROR, TAG, PCF("Collection handle not found"));
2311 return OC_STACK_INVALID_PARAM;
2314 // Look for an open slot to add add the child resource.
2315 // If found, add it and return success
2316 for (i = 0; i < MAX_CONTAINED_RESOURCES; i++) {
2317 if (!resource->rsrcResources[i]) {
2318 resource->rsrcResources[i] = (OCResource *) resourceHandle;
2319 OC_LOG(INFO, TAG, PCF("resource bound"));
2324 #ifdef WITH_PRESENCE
2325 if(presenceResource.handle)
2327 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2328 SendPresenceNotification(((OCResource *) resourceHandle)->rsrcType);
2332 // Unable to add resourceHandle, so return error
2333 return OC_STACK_ERROR;
2337 * Remove a resource from a collection resource.
2339 * @param collectionHandle - handle to the collection resource
2340 * @param resourceHandle - handle to resource to be added to the collection resource
2343 * OC_STACK_OK - no errors
2344 * OC_STACK_ERROR - stack process error
2345 * OC_STACK_INVALID_PARAM - invalid collectionHandle
2347 OCStackResult OCUnBindResource(
2348 OCResourceHandle collectionHandle, OCResourceHandle resourceHandle) {
2349 OCResource *resource;
2352 OC_LOG(INFO, TAG, PCF("Entering OCUnBindResource"));
2354 // Validate parameters
2355 VERIFY_NON_NULL(collectionHandle, ERROR, OC_STACK_ERROR);
2356 VERIFY_NON_NULL(resourceHandle, ERROR, OC_STACK_ERROR);
2357 // Container cannot contain itself
2358 if (collectionHandle == resourceHandle) {
2359 OC_LOG(ERROR, TAG, PCF("removing handle equals collection handle"));
2360 return OC_STACK_INVALID_PARAM;
2363 // Use the handle to find the resource in the resource linked list
2364 resource = findResource((OCResource *) collectionHandle);
2366 OC_LOG(ERROR, TAG, PCF("Collection handle not found"));
2367 return OC_STACK_INVALID_PARAM;
2370 // Look for an open slot to add add the child resource.
2371 // If found, add it and return success
2372 for (i = 0; i < MAX_CONTAINED_RESOURCES; i++) {
2373 if (resourceHandle == resource->rsrcResources[i]) {
2374 resource->rsrcResources[i] = (OCResource *) NULL;
2375 OC_LOG(INFO, TAG, PCF("resource unbound"));
2380 OC_LOG(INFO, TAG, PCF("resource not found in collection"));
2382 #ifdef WITH_PRESENCE
2383 if(presenceResource.handle)
2385 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2386 SendPresenceNotification(((OCResource *) resourceHandle)->rsrcType);
2390 // Unable to add resourceHandle, so return error
2391 return OC_STACK_ERROR;
2394 OCStackResult BindResourceTypeToResource(OCResource* resource,
2395 const char *resourceTypeName)
2397 OCResourceType *pointer = NULL;
2400 OCStackResult result = OC_STACK_ERROR;
2402 OC_LOG(INFO, TAG, PCF("Entering BindResourceTypeToResource"));
2404 // Validate parameters
2405 VERIFY_NON_NULL(resourceTypeName, ERROR, OC_STACK_INVALID_PARAM);
2406 // TODO: Does resource attribute resentation really have to be maintained in stack?
2407 // Is it presented during resource discovery?
2409 TODO ("Make sure that the resourcetypename doesn't already exist in the resource");
2411 // Create the resourcetype and insert it into the resource list
2412 pointer = (OCResourceType *) OCCalloc(1, sizeof(OCResourceType));
2417 // Set the resourceTypeName
2418 size = strlen(resourceTypeName) + 1;
2419 str = (char *) OCMalloc(size);
2423 strncpy(str, resourceTypeName, size);
2424 pointer->resourcetypename = str;
2426 insertResourceType(resource, pointer);
2427 result = OC_STACK_OK;
2429 exit: if (result != OC_STACK_OK) {
2437 OCStackResult BindResourceInterfaceToResource(OCResource* resource,
2438 const char *resourceInterfaceName)
2440 OCResourceInterface *pointer = NULL;
2443 OCStackResult result = OC_STACK_ERROR;
2445 OC_LOG(INFO, TAG, PCF("Entering BindResourceInterfaceToResource"));
2447 // Validate parameters
2448 VERIFY_NON_NULL(resourceInterfaceName, ERROR, OC_STACK_INVALID_PARAM);
2450 TODO ("Make sure that the resourceinterface name doesn't already exist in the resource");
2452 // Create the resourceinterface and insert it into the resource list
2453 pointer = (OCResourceInterface *) OCCalloc(1, sizeof(OCResourceInterface));
2458 // Set the resourceinterface name
2459 size = strlen(resourceInterfaceName) + 1;
2460 str = (char *) OCMalloc(size);
2464 strncpy(str, resourceInterfaceName, size);
2465 pointer->name = str;
2467 // Bind the resourceinterface to the resource
2468 insertResourceInterface(resource, pointer);
2470 result = OC_STACK_OK;
2472 exit: if (result != OC_STACK_OK) {
2481 * Bind a resourcetype to a resource.
2483 * @param handle - handle to the resource
2484 * @param resourceTypeName - name of resource type. Example: "core.led"
2487 * OC_STACK_OK - no errors
2488 * OC_STACK_ERROR - stack process error
2490 OCStackResult OCBindResourceTypeToResource(OCResourceHandle handle,
2491 const char *resourceTypeName) {
2493 OCStackResult result = OC_STACK_ERROR;
2494 OCResource *resource;
2496 // Make sure resource exists
2497 resource = findResource((OCResource *) handle);
2499 OC_LOG(ERROR, TAG, PCF("Resource not found"));
2500 return OC_STACK_ERROR;
2503 // call internal function
2504 result = BindResourceTypeToResource(resource, resourceTypeName);
2506 #ifdef WITH_PRESENCE
2507 if(presenceResource.handle)
2509 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2510 SendPresenceNotification(resource->rsrcType);
2518 * Bind a resourceinterface to a resource.
2520 * @param handle - handle to the resource
2521 * @param resourceInterfaceName - name of resource interface. Example: "oc.mi.b"
2524 * OC_STACK_OK - no errors
2525 * OC_STACK_ERROR - stack process error
2528 OCStackResult OCBindResourceInterfaceToResource(OCResourceHandle handle,
2529 const char *resourceInterfaceName) {
2531 OCStackResult result = OC_STACK_ERROR;
2532 OCResource *resource;
2534 // Make sure resource exists
2535 resource = findResource((OCResource *) handle);
2537 OC_LOG(ERROR, TAG, PCF("Resource not found"));
2538 return OC_STACK_ERROR;
2541 // call internal function
2542 result = BindResourceInterfaceToResource(resource, resourceInterfaceName);
2544 #ifdef WITH_PRESENCE
2545 if(presenceResource.handle)
2547 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2548 SendPresenceNotification(resource->rsrcType);
2556 * Get the number of resources that have been created in the stack.
2558 * @param numResources - pointer to count variable
2561 * OC_STACK_OK - no errors
2562 * OC_STACK_ERROR - stack process error
2565 OCStackResult OCGetNumberOfResources(uint8_t *numResources) {
2566 OCResource *pointer = headResource;
2568 OC_LOG(INFO, TAG, PCF("Entering OCGetNumberOfResources"));
2569 VERIFY_NON_NULL(numResources, ERROR, OC_STACK_INVALID_PARAM);
2572 *numResources = *numResources + 1;
2573 pointer = pointer->next;
2579 * Get a resource handle by index.
2581 * @param index - index of resource, 0 to Count - 1
2584 * Resource handle - if found
2585 * NULL - if not found
2587 OCResourceHandle OCGetResourceHandle(uint8_t index) {
2588 OCResource *pointer = headResource;
2591 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceHandle"));
2593 // Iterate through the list
2594 while ((i < index) && pointer) {
2596 pointer = pointer->next;
2598 return (OCResourceHandle) pointer;
2602 * Delete resource specified by handle. Deletes resource and all resourcetype and resourceinterface
2605 * @param handle - handle of resource to be deleted
2608 * OC_STACK_OK - no errors
2609 * OC_STACK_ERROR - stack process error
2610 * OC_STACK_NO_RESOURCE - resource not found
2611 * OC_STACK_INVALID_PARAM - invalid param
2613 OCStackResult OCDeleteResource(OCResourceHandle handle) {
2614 OC_LOG(INFO, TAG, PCF("Entering OCDeleteResource"));
2617 OC_LOG(ERROR, TAG, PCF("Invalid param"));
2618 return OC_STACK_INVALID_PARAM;
2621 OCResource *resource = findResource((OCResource *) handle);
2622 if (resource == NULL) {
2623 OC_LOG(ERROR, TAG, PCF("Resource not found"));
2624 return OC_STACK_NO_RESOURCE;
2627 if (deleteResource((OCResource *) handle) == 0) {
2628 OC_LOG(ERROR, TAG, PCF("Error deleting resource"));
2629 return OC_STACK_ERROR;
2636 * Get the URI of the resource specified by handle.
2638 * @param handle - handle of resource
2640 * URI string - if resource found
2641 * NULL - resource not found
2643 const char *OCGetResourceUri(OCResourceHandle handle) {
2644 OCResource *resource;
2645 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceUri"));
2647 resource = findResource((OCResource *) handle);
2649 return resource->uri;
2651 return (const char *) NULL;
2655 * Get the properties of the resource specified by handle.
2656 * NOTE: that after a resource is created, the OC_ACTIVE property is set
2657 * for the resource by the stack.
2659 * @param handle - handle of resource
2661 * property bitmap - if resource found
2662 * NULL - resource not found
2664 uint8_t OCGetResourceProperties(OCResourceHandle handle) {
2665 OCResource *resource;
2666 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceProperties"));
2668 resource = findResource((OCResource *) handle);
2670 return resource->resourceProperties;
2676 * Get the number of resource types of the resource.
2678 * @param handle - handle of resource
2679 * @param numResourceTypes - pointer to count variable
2682 * OC_STACK_OK - no errors
2683 * OC_STACK_ERROR - stack process error
2685 OCStackResult OCGetNumberOfResourceTypes(OCResourceHandle handle,
2686 uint8_t *numResourceTypes) {
2687 OCResource *resource;
2688 OCResourceType *pointer;
2690 OC_LOG(INFO, TAG, PCF("Entering OCGetNumberOfResourceTypes"));
2691 VERIFY_NON_NULL(numResourceTypes, ERROR, OC_STACK_INVALID_PARAM);
2692 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
2694 *numResourceTypes = 0;
2696 resource = findResource((OCResource *) handle);
2698 pointer = resource->rsrcType;
2700 *numResourceTypes = *numResourceTypes + 1;
2701 pointer = pointer->next;
2708 * Get name of resource type of the resource.
2710 * @param handle - handle of resource
2711 * @param index - index of resource, 0 to Count - 1
2714 * resource type name - if resource found
2715 * NULL - resource not found
2717 const char *OCGetResourceTypeName(OCResourceHandle handle, uint8_t index) {
2718 OCResourceType *resourceType;
2720 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceTypeName"));
2722 resourceType = findResourceTypeAtIndex(handle, index);
2724 return resourceType->resourcetypename;
2726 return (const char *) NULL;
2732 * Get the number of resource interfaces of the resource.
2734 * @param handle - handle of resource
2735 * @param numResources - pointer to count variable
2738 * OC_STACK_OK - no errors
2739 * OC_STACK_ERROR - stack process error
2741 OCStackResult OCGetNumberOfResourceInterfaces(OCResourceHandle handle,
2742 uint8_t *numResourceInterfaces) {
2743 OCResourceInterface *pointer;
2744 OCResource *resource;
2746 OC_LOG(INFO, TAG, PCF("Entering OCGetNumberOfResourceInterfaces"));
2748 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
2749 VERIFY_NON_NULL(numResourceInterfaces, ERROR, OC_STACK_INVALID_PARAM);
2751 *numResourceInterfaces = 0;
2752 resource = findResource((OCResource *) handle);
2754 pointer = resource->rsrcInterface;
2756 *numResourceInterfaces = *numResourceInterfaces + 1;
2757 pointer = pointer->next;
2764 * Get name of resource interface of the resource.
2766 * @param handle - handle of resource
2767 * @param index - index of resource, 0 to Count - 1
2770 * resource interface name - if resource found
2771 * NULL - resource not found
2773 const char *OCGetResourceInterfaceName(OCResourceHandle handle, uint8_t index) {
2774 OCResourceInterface *resourceInterface;
2776 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceInterfaceName"));
2778 resourceInterface = findResourceInterfaceAtIndex(handle, index);
2779 if (resourceInterface) {
2780 return resourceInterface->name;
2782 return (const char *) NULL;
2786 * Get resource handle from the collection resource by index.
2788 * @param collectionHandle - handle of collection resource
2789 * @param index - index of contained resource, 0 to Count - 1
2792 * handle to resource - if resource found
2793 * NULL - resource not found
2795 OCResourceHandle OCGetResourceHandleFromCollection(OCResourceHandle collectionHandle,
2797 OCResource *resource;
2799 OC_LOG(INFO, TAG, PCF("Entering OCGetContainedResource"));
2801 if (index >= MAX_CONTAINED_RESOURCES) {
2805 resource = findResource((OCResource *) collectionHandle);
2810 return resource->rsrcResources[index];
2814 * Bind an entity handler to the resource.
2816 * @param handle - handle to the resource that the contained resource is to be bound
2817 * @param entityHandler - entity handler function that is called by ocstack to handle requests, etc
2819 * OC_STACK_OK - no errors
2820 * OC_STACK_ERROR - stack process error
2822 OCStackResult OCBindResourceHandler(OCResourceHandle handle,
2823 OCEntityHandler entityHandler) {
2824 OCResource *resource;
2826 OC_LOG(INFO, TAG, PCF("Entering OCBindResourceHandler"));
2828 // Validate parameters
2829 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
2830 //VERIFY_NON_NULL(entityHandler, ERROR, OC_STACK_INVALID_PARAM);
2832 // Use the handle to find the resource in the resource linked list
2833 resource = findResource((OCResource *)handle);
2835 OC_LOG(ERROR, TAG, PCF("Resource not found"));
2836 return OC_STACK_ERROR;
2840 resource->entityHandler = entityHandler;
2842 #ifdef WITH_PRESENCE
2843 if(presenceResource.handle)
2845 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2846 SendPresenceNotification(resource->rsrcType);
2854 * Get the entity handler for a resource.
2856 * @param handle - handle of resource
2859 * entity handler - if resource found
2860 * NULL - resource not found
2862 OCEntityHandler OCGetResourceHandler(OCResourceHandle handle) {
2863 OCResource *resource;
2865 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceHandler"));
2867 // Use the handle to find the resource in the resource linked list
2868 resource = findResource((OCResource *)handle);
2870 OC_LOG(ERROR, TAG, PCF("Resource not found"));
2875 return resource->entityHandler;
2878 void incrementSequenceNumber(OCResource * resPtr)
2880 // Increment the sequence number
2881 resPtr->sequenceNum += 1;
2882 if (resPtr->sequenceNum == MAX_SEQUENCE_NUMBER)
2884 resPtr->sequenceNum = OC_OFFSET_SEQUENCE_NUMBER+1;
2890 * Notify Presence subscribers that a resource has been modified
2892 * @param resourceType - Handle to the resourceType linked list of resource
2893 * that was modified.
2894 * @param qos - Quality Of Service
2897 #ifdef WITH_PRESENCE
2898 OCStackResult SendPresenceNotification(OCResourceType *resourceType)
2900 OCResource *resPtr = NULL;
2901 OCStackResult result;
2902 OCMethod method = OC_REST_PRESENCE;
2903 uint32_t maxAge = 0;
2904 resPtr = findResource((OCResource *) presenceResource.handle);
2907 return OC_STACK_NO_RESOURCE;
2909 if((((OCResource *) presenceResource.handle)->resourceProperties) & OC_ACTIVE)
2911 maxAge = presenceResource.presenceTTL;
2918 result = SendAllObserverNotification(method, resPtr, maxAge, resourceType, OC_LOW_QOS);
2922 #endif // WITH_PRESENCE
2924 * Notify observers that an observed value has changed.
2926 * @param handle - handle of resource
2929 * OC_STACK_OK - no errors
2930 * OC_STACK_NO_RESOURCE - invalid resource handle
2931 * OC_STACK_NO_OBSERVERS - no more observers intrested in resource
2933 OCStackResult OCNotifyAllObservers(OCResourceHandle handle, OCQualityOfService qos) {
2935 OC_LOG(INFO, TAG, PCF("Entering OCNotifyAllObservers"));
2937 OCResource *resPtr = NULL;
2938 OCStackResult result;
2939 OCMethod method = OC_REST_NOMETHOD;
2940 uint32_t maxAge = 0;
2942 OC_LOG(INFO, TAG, PCF("Entering OCNotifyAllObservers"));
2943 #ifdef WITH_PRESENCE
2944 if(handle == presenceResource.handle)
2948 #endif // WITH_PRESENCE
2949 VERIFY_NON_NULL(handle, ERROR, OC_STACK_ERROR);
2951 // Verify that the resource exists
2952 resPtr = findResource ((OCResource *) handle);
2955 return OC_STACK_NO_RESOURCE;
2959 //only increment in the case of regular observing (not presence)
2960 incrementSequenceNumber(resPtr);
2961 method = OC_REST_OBSERVE;
2962 maxAge = MAX_OBSERVE_AGE;
2963 #ifdef WITH_PRESENCE
2964 result = SendAllObserverNotification (method, resPtr, maxAge, NULL, qos);
2966 result = SendAllObserverNotification (method, resPtr, maxAge, qos);
2973 OCNotifyListOfObservers (OCResourceHandle handle,
2974 OCObservationId *obsIdList,
2975 uint8_t numberOfIds,
2976 unsigned char *notificationJSONPayload,
2977 OCQualityOfService qos)
2979 OC_LOG(INFO, TAG, PCF("Entering OCNotifyListOfObservers"));
2981 OCResource *resPtr = NULL;
2982 //TODO: we should allow the server to define this
2983 uint32_t maxAge = MAX_OBSERVE_AGE;
2985 VERIFY_NON_NULL(handle, ERROR, OC_STACK_ERROR);
2986 VERIFY_NON_NULL(obsIdList, ERROR, OC_STACK_ERROR);
2987 VERIFY_NON_NULL(notificationJSONPayload, ERROR, OC_STACK_ERROR);
2989 // Verify that the resource exists
2990 resPtr = findResource ((OCResource *) handle);
2991 if (NULL == resPtr || myStackMode == OC_CLIENT)
2993 return OC_STACK_NO_RESOURCE;
2997 incrementSequenceNumber(resPtr);
2999 return (SendListObserverNotification(resPtr, obsIdList, numberOfIds,
3000 notificationJSONPayload, maxAge, qos));
3004 * Send a response to a request.
3005 * The response can be a regular, slow, or block (i.e. a response that
3006 * is too large to be sent in a single PDU and must span multiple transmissions)
3008 * @param response - pointer to structure that contains response parameters
3011 * OC_STACK_OK - No errors; Success
3012 * OC_STACK_INVALID_PARAM - Invalid pointer to OCServerResponse
3013 * OC_STACK_INVALID_REQUEST_HANDLE - Request handle not found
3014 * OC_STACK_PERSISTENT_BUFFER_REQUIRED - Block transfer needed for response, so a
3015 * persistent response buffer is necessary
3017 OCStackResult OCDoResponse(OCEntityHandlerResponse *ehResponse)
3019 OCStackResult result = OC_STACK_ERROR;
3020 OCServerRequest *serverRequest = NULL;
3022 OC_LOG(INFO, TAG, PCF("Entering OCDoResponse"));
3024 // Validate input parameters
3025 VERIFY_NON_NULL(ehResponse, ERROR, OC_STACK_INVALID_PARAM);
3026 VERIFY_NON_NULL(ehResponse->requestHandle, ERROR, OC_STACK_INVALID_PARAM);
3028 // TODO: Placeholder for creating a response entry when implementing
3029 // block transfer feature
3031 // If a response payload is present, check if block transfer is required
3032 if (ehResponse->payload && OCIsPacketTransferRequired(NULL,
3033 (const char *)ehResponse->payload, ehResponse->payloadSize))
3035 OC_LOG(INFO, TAG, PCF("Block transfer required"));
3037 // Persistent response buffer is needed for block transfer
3038 if (!ehResponse->persistentBufferFlag)
3040 OC_LOG(WARNING, TAG, PCF("Persistent response buffer required"));
3041 return OC_STACK_PERSISTENT_BUFFER_REQUIRED;
3043 // TODO: Placeholder for block transfer handling
3044 // TODO: Placeholder for setting the the response handle in the OCServerResponse struct
3045 // when implementing the block transfer feature
3050 // Get pointer to request info
3051 serverRequest = GetServerRequestUsingHandle((OCServerRequest *)ehResponse->requestHandle);
3054 result = serverRequest->ehResponseHandler(ehResponse);
3061 * Cancel a response. Applies to a block response
3063 * @param responseHandle - response handle set by stack in OCServerResponse after
3064 * OCDoResponse is called
3067 * OC_STACK_OK - No errors; Success
3068 * OC_STACK_INVALID_PARAM - The handle provided is invalid.
3070 OCStackResult OCCancelResponse(OCResponseHandle responseHandle)
3072 OCStackResult result = OC_STACK_NOTIMPL;
3074 OC_LOG(INFO, TAG, PCF("Entering OCCancelResponse"));
3076 // TODO: validate response handle
3081 //-----------------------------------------------------------------------------
3082 // Private internal function definitions
3083 //-----------------------------------------------------------------------------
3085 * Generate handle of OCDoResource invocation for callback management.
3087 static OCDoHandle GenerateInvocationHandle()
3089 OCDoHandle handle = NULL;
3090 // Generate token here, it will be deleted when the transaction is deleted
3091 handle = (OCDoHandle) OCMalloc(sizeof(uint8_t[MAX_TOKEN_LENGTH]));
3094 OCFillRandomMem((uint8_t*)handle, sizeof(uint8_t[MAX_TOKEN_LENGTH]));
3099 #ifdef WITH_PRESENCE
3100 OCStackResult OCChangeResourceProperty(OCResourceProperty * inputProperty,
3101 OCResourceProperty resourceProperties, uint8_t enable)
3103 if (resourceProperties
3104 > (OC_ACTIVE | OC_DISCOVERABLE | OC_OBSERVABLE | OC_SLOW)) {
3105 OC_LOG(ERROR, TAG, PCF("Invalid property"));
3106 return OC_STACK_INVALID_PARAM;
3110 *inputProperty = (OCResourceProperty) (*inputProperty & ~(resourceProperties));
3114 *inputProperty = (OCResourceProperty) (*inputProperty | resourceProperties);
3121 * Initialize resource data structures, variables, etc.
3123 OCStackResult initResources() {
3124 OCStackResult result = OC_STACK_OK;
3125 // Init application resource vars
3126 headResource = NULL;
3127 // Init Virtual Resources
3128 #ifdef WITH_PRESENCE
3129 presenceResource.presenceTTL = OC_DEFAULT_PRESENCE_TTL;
3130 //presenceResource.token = OCGenerateCoAPToken();
3131 result = OCCreateResource(&presenceResource.handle,
3132 OC_RSRVD_RESOURCE_TYPE_PRESENCE,
3137 //make resource inactive
3138 result = OCChangeResourceProperty(
3139 &(((OCResource *) presenceResource.handle)->resourceProperties),
3146 * Add a resource to the end of the linked list of resources.
3148 * @param resource - resource to be added
3150 void insertResource(OCResource *resource) {
3151 OCResource *pointer;
3153 if (!headResource) {
3154 headResource = resource;
3156 pointer = headResource;
3158 while (pointer->next) {
3159 pointer = pointer->next;
3161 pointer->next = resource;
3163 resource->next = NULL;
3167 * Find a resource in the linked list of resources.
3169 * @param resource - resource to be found
3171 * NULL - resource not found
3172 * pointer to resource - pointer to resource that was found in the linked list
3174 OCResource *findResource(OCResource *resource) {
3175 OCResource *pointer = headResource;
3178 if (pointer == resource) {
3181 pointer = pointer->next;
3186 void deleteAllResources()
3188 OCResource *pointer = headResource;
3193 temp = pointer->next;
3194 #ifdef WITH_PRESENCE
3195 if(pointer != (OCResource *) presenceResource.handle)
3197 #endif // WITH_PRESENCE
3198 deleteResource(pointer);
3199 #ifdef WITH_PRESENCE
3201 #endif // WITH_PRESENCE
3205 #ifdef WITH_PRESENCE
3206 // Ensure that the last resource to be deleted is the presence resource. This allows for all
3207 // presence notification attributed to their deletion to be processed.
3208 deleteResource((OCResource *) presenceResource.handle);
3209 #endif // WITH_PRESENCE
3213 * Delete the resource from the linked list.
3215 * @param resource - resource to be deleted
3220 int deleteResource(OCResource *resource) {
3221 OCResource *prev = NULL;
3224 temp = headResource;
3226 if (temp == resource) {
3227 // Invalidate all Resource Properties.
3228 resource->resourceProperties = (OCResourceProperty) 0;
3229 #ifdef WITH_PRESENCE
3230 if(resource != (OCResource *) presenceResource.handle)
3232 #endif // WITH_PRESENCE
3233 OCNotifyAllObservers((OCResourceHandle)resource, OC_HIGH_QOS);
3234 #ifdef WITH_PRESENCE
3237 if(presenceResource.handle)
3239 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
3240 if(resource != (OCResource *) presenceResource.handle)
3242 SendPresenceNotification(resource->rsrcType);
3246 SendPresenceNotification(NULL);
3251 if (temp == headResource) {
3252 headResource = temp->next;
3254 prev->next = temp->next;
3257 deleteResourceElements(temp);
3270 * Delete all of the dynamically allocated elements that were created for the resource.
3272 * @param resource - specified resource
3274 void deleteResourceElements(OCResource *resource) {
3280 OCFree(resource->uri);
3282 // Delete resourcetype linked list
3283 deleteResourceType(resource->rsrcType);
3285 // Delete resourceinterface linked list
3286 deleteResourceInterface(resource->rsrcInterface);
3290 * Delete all of the dynamically allocated elements that were created for the resource type.
3292 * @param resourceType - specified resource type
3294 void deleteResourceType(OCResourceType *resourceType) {
3295 OCResourceType *pointer = resourceType;
3296 OCResourceType *next;
3299 next = pointer->next;
3300 OCFree(pointer->resourcetypename);
3307 * Delete all of the dynamically allocated elements that were created for the resource interface.
3309 * @param resourceInterface - specified resource interface
3311 void deleteResourceInterface(OCResourceInterface *resourceInterface) {
3312 OCResourceInterface *pointer = resourceInterface;
3313 OCResourceInterface *next;
3316 next = pointer->next;
3317 OCFree(pointer->name);
3324 * Insert a resource type into a resource's resource type linked list.
3326 * @param resource - resource where resource type is to be inserted
3327 * @param resourceType - resource type to be inserted
3329 void insertResourceType(OCResource *resource, OCResourceType *resourceType) {
3330 OCResourceType *pointer;
3332 if (resource && !resource->rsrcType) {
3333 resource->rsrcType = resourceType;
3337 pointer = resource->rsrcType;
3341 pointer = resourceType;
3343 while (pointer->next) {
3344 pointer = pointer->next;
3346 pointer->next = resourceType;
3348 resourceType->next = NULL;
3352 * Get a resource type at the specified index within a resource.
3354 * @param handle - handle of resource
3355 * @param index - index of resource type
3358 * resourcetype - if found
3361 OCResourceType *findResourceTypeAtIndex(OCResourceHandle handle, uint8_t index) {
3362 OCResource *resource;
3363 OCResourceType *pointer;
3366 // Find the specified resource
3367 resource = findResource((OCResource *) handle);
3372 // Make sure a resource has a resourcetype
3373 if (!resource->rsrcType) {
3377 // Iterate through the list
3378 pointer = resource->rsrcType;
3380 while ((i < index) && pointer) {
3382 pointer = pointer->next;
3388 * Finds a resource type in an OCResourceType link-list.
3390 * @param resourceTypeList - the link-list to be searched through
3391 * @param resourceTypeName - the key to search for
3394 * resourceType that matches the key (ie. resourceTypeName)
3395 * NULL - either an invalid parameter or this function was unable to find the key.
3397 OCResourceType *findResourceType(OCResourceType * resourceTypeList, const char * resourceTypeName)
3399 if(resourceTypeList && resourceTypeName)
3401 OCResourceType * rtPointer = resourceTypeList;
3402 while(resourceTypeName && rtPointer)
3404 if(rtPointer->resourcetypename &&
3405 strcmp(resourceTypeName, (const char *)
3406 (rtPointer->resourcetypename)) == 0)
3410 rtPointer = rtPointer->next;
3417 * Insert a resource interface into a resource's resource interface linked list.
3419 * @param resource - resource where resource interface is to be inserted
3420 * @param resourceInterface - resource interface to be inserted
3422 void insertResourceInterface(OCResource *resource,
3423 OCResourceInterface *resourceInterface) {
3424 OCResourceInterface *pointer;
3426 if (!resource->rsrcInterface) {
3427 resource->rsrcInterface = resourceInterface;
3429 pointer = resource->rsrcInterface;
3430 while (pointer->next) {
3431 pointer = pointer->next;
3433 pointer->next = resourceInterface;
3435 resourceInterface->next = NULL;
3439 * Get a resource interface at the specified index within a resource.
3441 * @param handle - handle of resource
3442 * @param index - index of resource interface
3445 * resourceinterface - if found
3448 OCResourceInterface *findResourceInterfaceAtIndex(OCResourceHandle handle,
3450 OCResource *resource;
3451 OCResourceInterface *pointer;
3454 // Find the specified resource
3455 resource = findResource((OCResource *) handle);
3460 // Make sure a resource has a resourceinterface
3461 if (!resource->rsrcInterface) {
3465 // Iterate through the list
3466 pointer = resource->rsrcInterface;
3468 while ((i < index) && pointer) {
3470 pointer = pointer->next;
3476 * Determine if a request/response must be sent in a block transfer because it is too large to be
3477 * sent in a single PDU. This function can be used for either a request or a response
3479 * @param request - NULL or pointer to request
3480 * @param response - NULL or pointer to response
3481 * @param size - 0 or size of the request/response. If 0, strlen is used for determining
3482 * the length of the request/response
3485 * 0 - packet transfer NOT required (i.e. normal request/response)
3486 * 1 - packet transfer required (i.e. block transfer needed)
3488 uint8_t OCIsPacketTransferRequired(const char *request, const char *response, uint16_t size)
3492 // Determine if we are checking a request or a response
3495 // If size is greater than 0, use it for the request size value, otherwise
3496 // assume request is null terminated and use strlen for size value
3497 if ((size > MAX_REQUEST_LENGTH) || (strlen(request) > MAX_REQUEST_LENGTH))
3504 // If size is greater than 0, use it for the response size value, otherwise
3505 // assume response is null terminated and use strlen for size value
3506 if ((size > MAX_RESPONSE_LENGTH) || (strlen(response) > MAX_RESPONSE_LENGTH))
3515 * Retrieves a resource type based upon a query ontains only just one
3516 * resource attribute (and that has to be of type "rt").
3518 * @remark This API malloc's memory for the resource type. Do not malloc resourceType
3519 * before passing in.
3521 * @param query - The quert part of the URI
3522 * @param resourceType - The resource type to be populated; pass by reference.
3525 * OC_STACK_INVALID_PARAM - Returns this if the resourceType parameter is invalid/NULL.
3526 * OC_STACK_OK - Success
3528 OCStackResult getResourceType(const char * query, unsigned char** resourceType)
3532 return OC_STACK_INVALID_PARAM;
3535 OCStackResult result = OC_STACK_ERROR;
3537 if(strncmp(query, "rt=", 3) == 0)
3539 *resourceType = (unsigned char *) OCMalloc(strlen(query)-3);
3542 result = OC_STACK_NO_MEMORY;
3545 strcpy((char *)*resourceType, ((const char *)&query[3]));
3546 result = OC_STACK_OK;
3552 OCStackResult getQueryFromUri(const char * uri, unsigned char** query, char ** newURI)
3556 return OC_STACK_INVALID_URI;
3558 if(!query || !newURI)
3560 return OC_STACK_INVALID_PARAM;
3562 char * leftToken = NULL;
3563 char * tempURI = (char *) OCMalloc(strlen(uri) + 1);
3568 strcpy(tempURI, uri);
3570 leftToken = strtok_r((char *)tempURI, "?", &strTokPtr);
3572 //TODO-CA: This could be simplified. Clean up required.
3573 while(leftToken != NULL)
3575 if(strncmp(leftToken, "rt=", 3) == 0 || strncmp(leftToken, "if=", 3) == 0)
3577 *query = (unsigned char *) OCMalloc(strlen(leftToken));
3583 strcpy((char *)*query, ((const char *)&leftToken[0]));
3586 leftToken = strtok_r(NULL, "?", &strTokPtr);
3594 return OC_STACK_NO_MEMORY;
3597 const ServerID OCGetServerInstanceID(void)
3599 static bool generated = false;
3600 static ServerID sid;
3607 sid = OCGetRandom();
3612 const char* OCGetServerInstanceIDString(void)
3614 // max printed length of a base 10
3615 // uint32 is 10 characters, so 11 includes null.
3616 // This will change as the representation gets switched
3618 static char buffer[11];
3619 int n = sprintf(buffer, "%u", OCGetServerInstanceID());