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 void HandlePresenceResponse(const CARemoteEndpoint_t* endPoint, const CAResponseInfo_t* responseInfo)
243 OCStackApplicationResult cbResult = OC_STACK_DELETE_TRANSACTION;
244 ClientCB * cbNode = NULL;
245 char *resourceTypeName = NULL;
247 char * bufRes = responseInfo->info.payload;
248 OCClientResponse *response = (OCClientResponse *) OCMalloc(sizeof(OCClientResponse));
255 tok = strtok(bufRes, "[:]}");
256 bufRes[strlen(bufRes)] = ':';
257 tok = strtok(NULL, "[:]}");
258 bufRes[strlen((char *)bufRes)] = ':';
259 response->sequenceNumber = (uint32_t )atoi(tok);
260 tok = strtok(NULL, "[:]}");
261 tok = strtok(NULL, "[:]}");
264 resourceTypeName = (char *)OCMalloc(strlen(tok));
265 if(!resourceTypeName)
269 bufRes[strlen((char *)bufRes)] = ':';
270 strcpy(resourceTypeName, tok);
271 OC_LOG_V(DEBUG, TAG, "----------------resourceTypeName %s",
274 bufRes[strlen((char *)bufRes)] = ']';
276 response->resJSONPayload = responseInfo->info.payload;
277 response->result = OC_STACK_OK;
279 char *fullUri = (char *) OCMalloc(MAX_URI_LENGTH );
280 char *ipAddress = (char *) OCMalloc(strlen(endPoint->addressInfo.IP.ipAddress) + 1);
282 strncpy(ipAddress, endPoint->addressInfo.IP.ipAddress, strlen(endPoint->addressInfo.IP.ipAddress));
283 ipAddress[strlen(endPoint->addressInfo.IP.ipAddress)] = '\0';
285 snprintf(fullUri, MAX_URI_LENGTH, "coap://%s:%u%s", ipAddress, endPoint->addressInfo.IP.port,
288 cbNode = GetClientCB(NULL, NULL, fullUri);
292 cbResult = cbNode->callBack(cbNode->context, cbNode->handle, response);
295 if (cbResult == OC_STACK_DELETE_TRANSACTION)
297 FindAndDeleteClientCB(cbNode);
304 OCFree(resourceTypeName);
308 //This function will be called back by CA layer when a response is received
309 void HandleCAResponses(const CARemoteEndpoint_t* endPoint, const CAResponseInfo_t* responseInfo)
311 OC_LOG(INFO, TAG, PCF("Enter HandleCAResponses"));
312 OCStackApplicationResult result = OC_STACK_DELETE_TRANSACTION;
313 if(strcmp(endPoint->resourceUri, OC_PRESENCE_URI) == 0)
315 HandlePresenceResponse(endPoint, responseInfo);
318 ClientCB *cbNode = GetClientCB((CAToken_t *)&responseInfo->info.token, NULL, NULL);
322 OC_LOG(INFO, TAG, PCF("Calling into application address space"));
323 OCClientResponse response;
324 struct sockaddr_in sa;
326 inet_pton(AF_INET, endPoint->addressInfo.IP.ipAddress, &(sa.sin_addr));
327 sa.sin_port = htons(endPoint->addressInfo.IP.port);
328 static OCDevAddr address;
329 memcpy((void*)&address.addr, &(sa), sizeof(sa));
330 response.addr = &address;
332 response.connType = endPoint->connectivityType;
334 response.result = CAToOCStackResult(responseInfo->result);
335 response.resJSONPayload = (unsigned char*)responseInfo->info.payload;
336 response.numRcvdVendorSpecificHeaderOptions = 0;
337 if(responseInfo->info.numOptions > 0)
339 //First option alwas with option ID COAP_OPTION_OBSERVE if it is availbale
340 if(responseInfo->info.options[0].optionID == COAP_OPTION_OBSERVE)
342 memcpy (&(response.sequenceNumber),
343 &(responseInfo->info.options[0].optionData), 4);
347 memcpy (&(response.rcvdVendorSpecificHeaderOptions[0]),
348 &(responseInfo->info.options[0]), sizeof(OCHeaderOption));
350 for (uint8_t i = 1; i < responseInfo->info.numOptions; i++)
352 memcpy (&(response.rcvdVendorSpecificHeaderOptions[i]),
353 &(responseInfo->info.options[i]), sizeof(OCHeaderOption));
355 response.numRcvdVendorSpecificHeaderOptions = responseInfo->info.numOptions;
357 result = cbNode->callBack(cbNode->context,
358 cbNode->handle, &response);
359 if (result == OC_STACK_DELETE_TRANSACTION)
361 FindAndDeleteClientCB(cbNode);
364 OC_LOG_V(INFO, TAG, PCF("Received payload: %s\n"), (char*)responseInfo->info.payload);
365 OC_LOG(INFO, TAG, PCF("Exit HandleCAResponses"));
368 //This function will be called back by CA layer when a request is received
369 void HandleCARequests(const CARemoteEndpoint_t* endPoint, const CARequestInfo_t* requestInfo)
371 CAInfo_t responseData;
372 CAResponseInfo_t responseInfo;
373 OCStackResult requestResult = OC_STACK_ERROR;
375 OC_LOG(INFO, TAG, PCF("Enter HandleCARequests"));
378 if(myStackMode == OC_CLIENT)
380 //TODO: should the client be responding to requests?
384 OCServerProtocolRequest serverRequest;
386 memset (&serverRequest, 0, sizeof(OCServerProtocolRequest));
387 OC_LOG_V(INFO, TAG, PCF("***** Endpoint URI ***** : %s\n"), (char*)endPoint->resourceUri);
389 char * newUri = (char *)endPoint->resourceUri;
390 unsigned char * query = NULL;
391 unsigned char * resourceType = NULL;
392 getQueryFromUri(endPoint->resourceUri, &query, &newUri);
393 OC_LOG_V(INFO, TAG, PCF("**********URI without query ****: %s\n"), newUri);
394 OC_LOG_V(INFO, TAG, PCF("**********Query ****: %s\n"), query);
396 memcpy (&(serverRequest.resourceUrl), newUri, strlen(newUri));
400 memcpy (&(serverRequest.query), query, strlen(query));
402 //copy request payload
403 if (requestInfo->info.payload)
405 serverRequest.reqTotalSize = strlen(requestInfo->info.payload) + 1;
406 memcpy (&(serverRequest.reqJSONPayload), requestInfo->info.payload,
407 strlen(requestInfo->info.payload));
408 serverRequest.reqTotalSize = strlen((const char *)requestInfo->info.payload) + 1;
412 serverRequest.reqTotalSize = 1;
415 switch (requestInfo->method)
419 serverRequest.method = OC_REST_GET;
424 serverRequest.method = OC_REST_PUT;
429 serverRequest.method = OC_REST_POST;
434 serverRequest.method = OC_REST_DELETE;
439 OC_LOG(ERROR, TAG, PCF("Received CA method %d not supported"));
445 OC_LOG_V(INFO, TAG, "HandleCARequests: CA token length = %d", strlen(requestInfo->info.token));
446 OC_LOG_BUFFER(INFO, TAG, requestInfo->info.token, strlen(requestInfo->info.token));
447 // TODO-CA: For CA integration currently copying CAToken to OCCoapToken:
448 // Need to remove OCCoapToken
449 memcpy (&(serverRequest.requestToken.token), requestInfo->info.token,
450 strlen(requestInfo->info.token));
451 serverRequest.requestToken.tokenLength = strlen(requestInfo->info.token);
453 if (requestInfo->info.type == CA_MSG_CONFIRM)
455 serverRequest.qos = OC_HIGH_QOS;
457 else if (requestInfo->info.type == CA_MSG_NONCONFIRM)
459 serverRequest.qos = OC_LOW_QOS;
461 else if (requestInfo->info.type == CA_MSG_ACKNOWLEDGE)
463 // TODO-CA: Need to handle this
465 else if (requestInfo->info.type == CA_MSG_RESET)
467 // TODO-CA: Need to handle this
469 // CA does not need the following 3 fields
470 serverRequest.coapID = 0;
471 serverRequest.delayedResNeeded = 0;
472 serverRequest.secured = endPoint->isSecured;
475 serverRequest.addressInfo = endPoint->addressInfo;
476 serverRequest.connectivityType = endPoint->connectivityType;
477 if (requestInfo->info.token)
479 strncpy(serverRequest.token, requestInfo->info.token, sizeof(serverRequest.token) - 1);
482 // copy vendor specific header options
483 // TODO-CA: CA is including non-vendor header options as well, like observe.
484 // Need to filter those out
485 GetObserveHeaderOption(&serverRequest.observationOption, requestInfo->info.options, &(requestInfo->info.numOptions));
486 if (requestInfo->info.numOptions > MAX_HEADER_OPTIONS)
488 // TODO-CA: Need to send an error indicating the num of options is incorrect
491 serverRequest.numRcvdVendorSpecificHeaderOptions = requestInfo->info.numOptions;
492 if (serverRequest.numRcvdVendorSpecificHeaderOptions)
494 memcpy (&(serverRequest.rcvdVendorSpecificHeaderOptions), requestInfo->info.options,
495 sizeof(CAHeaderOption_t)*requestInfo->info.numOptions);
498 requestResult = HandleStackRequests (&serverRequest);
501 OC_LOG(INFO, TAG, PCF("Exit HandleCARequests"));
506 //This function will be called back by occoap layer when a request is received
507 OCStackResult HandleStackRequests(OCServerProtocolRequest * protocolRequest)
509 OC_LOG(INFO, TAG, PCF("Entering HandleStackRequests (OCStack Layer)"));
510 OCStackResult result = OC_STACK_ERROR;
511 ResourceHandling resHandling;
512 OCResource *resource;
514 OCServerRequest * request = GetServerRequestUsingToken(protocolRequest->requestToken);
517 OC_LOG(INFO, TAG, PCF("This is a new Server Request"));
519 result = AddServerCARequest(&request, protocolRequest->coapID,
520 protocolRequest->delayedResNeeded, protocolRequest->secured, 0,
521 protocolRequest->method, protocolRequest->numRcvdVendorSpecificHeaderOptions,
522 protocolRequest->observationOption, protocolRequest->qos,
523 protocolRequest->query, protocolRequest->rcvdVendorSpecificHeaderOptions,
524 protocolRequest->reqJSONPayload, &protocolRequest->requestToken,
525 &protocolRequest->requesterAddr, protocolRequest->resourceUrl,
526 protocolRequest->reqTotalSize,
527 &protocolRequest->addressInfo, protocolRequest->connectivityType, protocolRequest->token);
529 result = AddServerRequest(&request, protocolRequest->coapID,
530 protocolRequest->delayedResNeeded, protocolRequest->secured, 0,
531 protocolRequest->method, protocolRequest->numRcvdVendorSpecificHeaderOptions,
532 protocolRequest->observationOption, protocolRequest->qos,
533 protocolRequest->query, protocolRequest->rcvdVendorSpecificHeaderOptions,
534 protocolRequest->reqJSONPayload, &protocolRequest->requestToken,
535 &protocolRequest->requesterAddr, protocolRequest->resourceUrl,
536 protocolRequest->reqTotalSize);
538 if (OC_STACK_OK != result)
540 OC_LOG(ERROR, TAG, PCF("Error adding server request"));
543 VERIFY_NON_NULL(request, ERROR, OC_STACK_NO_MEMORY);
545 if(!protocolRequest->reqMorePacket)
547 request->requestComplete = 1;
552 OC_LOG(INFO, TAG, PCF("This is either a repeated Server Request or blocked Server Request"));
555 if(request->requestComplete)
557 OC_LOG(INFO, TAG, PCF("This Server Request is complete"));
558 result = DetermineResourceHandling (request, &resHandling, &resource);
559 if (result == OC_STACK_OK)
561 result = ProcessRequest(resHandling, resource, request);
565 result = OC_STACK_ERROR;
570 OC_LOG(INFO, TAG, PCF("This Server Request is incomplete"));
571 result = OC_STACK_CONTINUE;
576 //This function will be called back by occoap layer when a response is received
577 OCStackResult HandleStackResponses(OCResponse * response)
579 OC_LOG(INFO, TAG, PCF("Entering HandleStackResponses (OCStack Layer)"));
580 OCStackResult result = OC_STACK_OK;
581 OCStackApplicationResult cbResult = OC_STACK_DELETE_TRANSACTION;
582 uint8_t isObserveNotification = 0;
583 ClientCB * cbNode = NULL;
585 uint8_t isPresenceNotification = 0;
586 uint8_t isMulticastPresence = 0;
587 char * resourceTypeName = NULL;
588 uint32_t lowerBound = 0;
589 uint32_t higherBound = 0;
591 unsigned char * bufRes = response->bufRes;
592 #endif // WITH_PRESENCE
594 cbNode = response->cbNode;
597 cbNode = GetClientCB(response->rcvdToken, NULL, NULL);
600 if(response->clientResponse->sequenceNumber >= OC_OFFSET_SEQUENCE_NUMBER)
602 isObserveNotification = 1;
603 OC_LOG(INFO, TAG, PCF("Received an observe notification"));
606 OC_LOG_V(DEBUG, TAG, "The sequenceNumber/NONCE of this response %u",
607 response->clientResponse->sequenceNumber);
608 OC_LOG_V(DEBUG, TAG, "The maxAge/TTL of this response %u", response->maxAge);
609 OC_LOG_V(DEBUG, TAG, "The response received is %s", bufRes);
612 if(!strcmp((char *)response->rcvdUri, (char *)OC_PRESENCE_URI)){
613 isPresenceNotification = 1;
616 result = OC_STACK_INVALID_PARAM;
619 tok = strtok((char *)bufRes, "[:]}");
620 bufRes[strlen((char *)bufRes)] = ':';
621 tok = strtok(NULL, "[:]}");
622 bufRes[strlen((char *)bufRes)] = ':';
623 response->clientResponse->sequenceNumber = (uint32_t )atoi(tok);
624 OC_LOG_V(DEBUG, TAG, "The received NONCE is %u", response->clientResponse->sequenceNumber);
625 tok = strtok(NULL, "[:]}");
626 response->maxAge = (uint32_t )atoi(tok);
627 OC_LOG_V(DEBUG, TAG, "The received TTL is %u", response->maxAge);
628 tok = strtok(NULL, "[:]}");
631 resourceTypeName = (char *)OCMalloc(strlen(tok));
632 if(!resourceTypeName)
636 bufRes[strlen((char *)bufRes)] = ':';
637 strcpy(resourceTypeName, tok);
638 OC_LOG_V(DEBUG, TAG, "----------------resourceTypeName %s",
641 bufRes[strlen((char *)bufRes)] = ']';
644 // Check if the application subcribed for presence
647 cbNode = GetClientCB(NULL, NULL, response->fullUri);
650 // Check if application subscribed for multicast presence
653 snprintf((char *)response->fullUri, MAX_URI_LENGTH, "%s%s",
654 OC_MULTICAST_IP, response->rcvdUri);
655 cbNode = GetClientCB(NULL, NULL, response->fullUri);
658 isMulticastPresence = 1;
659 isPresenceNotification = 0;
663 if(cbNode && isPresenceNotification)
665 OC_LOG(INFO, TAG, PCF("Received a presence notification"));
666 if(!cbNode->presence)
668 cbNode->presence = (OCPresence *) OCMalloc(sizeof(OCPresence));
669 VERIFY_NON_NULL_V(cbNode->presence);
670 cbNode->presence->timeOut = NULL;
671 cbNode->presence->timeOut = (uint32_t *)
672 OCMalloc(PresenceTimeOutSize * sizeof(uint32_t));
673 if(!(cbNode->presence->timeOut)){
674 OCFree(cbNode->presence);
675 result = OC_STACK_NO_MEMORY;
678 if(response->maxAge == 0)
680 OC_LOG(INFO, TAG, PCF("===============Stopping presence"));
681 response->clientResponse->result = OC_STACK_PRESENCE_STOPPED;
684 OCFree(cbNode->presence->timeOut);
685 OCFree(cbNode->presence);
686 cbNode->presence = NULL;
691 OC_LOG_V(INFO, TAG, "===============Update presence TTL, now time is %d", GetTime(0));
692 cbNode->presence->TTL = response->maxAge;
693 for(int index = 0; index < PresenceTimeOutSize; index++)
695 lowerBound = GetTime(((float)(PresenceTimeOut[index])
696 /(float)100)*(float)cbNode->presence->TTL);
697 higherBound = GetTime(((float)(PresenceTimeOut[index + 1])
698 /(float)100)*(float)cbNode->presence->TTL);
699 cbNode->presence->timeOut[index] = OCGetRandomRange(lowerBound, higherBound);
700 OC_LOG_V(DEBUG, TAG, "----------------lowerBound timeout %d", lowerBound);
701 OC_LOG_V(DEBUG, TAG, "----------------higherBound timeout %d", higherBound);
702 OC_LOG_V(DEBUG, TAG, "----------------timeOut entry %d",
703 cbNode->presence->timeOut[index]);
705 cbNode->presence->TTLlevel = 0;
706 OC_LOG_V(DEBUG, TAG, "----------------this TTL level %d", cbNode->presence->TTLlevel);
707 if(cbNode->sequenceNumber == response->clientResponse->sequenceNumber)
709 OC_LOG(INFO, TAG, PCF("===============No presence change"));
712 OC_LOG(INFO, TAG, PCF("===============Presence changed, calling up the stack"));
713 cbNode->sequenceNumber = response->clientResponse->sequenceNumber;;
716 // Ensure that a filter is actually applied.
717 if(resourceTypeName && cbNode->filterResourceType)
719 if(!findResourceType(cbNode->filterResourceType, resourceTypeName))
725 else if(cbNode && isMulticastPresence)
727 // Check if the same nonce for a given host
728 OCMulticastNode* mcNode = NULL;
729 mcNode = GetMCPresenceNode(response->fullUri);
731 if(response->maxAge == 0)
733 OC_LOG(INFO, TAG, PCF("===============Stopping presence"));
734 response->clientResponse->result = OC_STACK_PRESENCE_STOPPED;
737 OCFree(cbNode->presence->timeOut);
738 OCFree(cbNode->presence);
739 cbNode->presence = NULL;
742 else if(mcNode != NULL)
744 if(mcNode->nonce == response->clientResponse->sequenceNumber)
746 OC_LOG(INFO, TAG, PCF("===============No presence change (Multicast)"));
747 result = OC_STACK_NO_MEMORY;
750 mcNode->nonce = response->clientResponse->sequenceNumber;
754 uint32_t uriLen = strlen((char*)response->fullUri);
755 unsigned char* uri = (unsigned char *) OCMalloc(uriLen + 1);
758 memcpy(uri, response->fullUri, (uriLen + 1));
763 PCF("===============No Memory for URI to store in the presence node"));
764 result = OC_STACK_NO_MEMORY;
767 result = AddMCPresenceNode(&mcNode, (unsigned char*) uri,
768 response->clientResponse->sequenceNumber);
769 if(result == OC_STACK_NO_MEMORY)
772 PCF("===============No Memory for Multicast Presence Node"));
773 result = OC_STACK_NO_MEMORY;
778 // Ensure that a filter is actually applied.
779 if(resourceTypeName && cbNode->filterResourceType)
781 if(!findResourceType(cbNode->filterResourceType, resourceTypeName))
788 else if(!cbNode && isPresenceNotification)
790 OC_LOG(INFO, TAG, PCF("Received a presence notification, but I do not have callback \
791 ------------ ignoring"));
793 #endif // WITH_PRESENCE
797 if(isObserveNotification)
799 OC_LOG(INFO, TAG, PCF("Received an observe notification"));
800 //TODO: check the standard for methods to detect wrap around condition
801 if(cbNode->method == OC_REST_OBSERVE &&
802 (response->clientResponse->sequenceNumber <= cbNode->sequenceNumber ||
803 (response->clientResponse->sequenceNumber > cbNode->sequenceNumber &&
804 response->clientResponse->sequenceNumber ==
805 MAX_SEQUENCE_NUMBER)))
807 OC_LOG_V(DEBUG, TAG, "Observe notification came out of order. \
808 Ignoring Incoming:%d Against Current:%d.",
809 response->clientResponse->sequenceNumber, cbNode->sequenceNumber);
812 if(response->clientResponse->sequenceNumber > cbNode->sequenceNumber){
813 cbNode->sequenceNumber = response->clientResponse->sequenceNumber;
817 response->clientResponse->resJSONPayload = bufRes;
819 cbResult = cbNode->callBack(cbNode->context, cbNode->handle, response->clientResponse);
821 if (cbResult == OC_STACK_DELETE_TRANSACTION ||
822 response->clientResponse->result == OC_STACK_COMM_ERROR ||
823 (response->clientResponse->result == OC_STACK_RESOURCE_DELETED &&
824 !isPresenceNotification && !isMulticastPresence))
826 FindAndDeleteClientCB(cbNode);
831 result = OC_STACK_ERROR;
836 OCFree(resourceTypeName);
841 int ParseIPv4Address(unsigned char * ipAddrStr, uint8_t * ipAddr, uint16_t * port)
844 unsigned char *itr, *coap;
845 uint8_t dotCount = 0;
849 /* search for scheme */
851 if (!isdigit((unsigned char) *ipAddrStr))
853 coap = (unsigned char *) OC_COAP_SCHEME;
854 while (*coap && tolower(*itr) == *coap)
863 if (isdigit((unsigned char) *ipAddrStr))
866 ipAddr[index] += *ipAddrStr - '0';
868 else if ((unsigned char) *ipAddrStr == '.')
880 if(*ipAddrStr == ':')
884 if (isdigit((unsigned char) *ipAddrStr))
887 *port += *ipAddrStr - '0';
898 if (ipAddr[0] < 255 && ipAddr[1] < 255 && ipAddr[2] < 255 && ipAddr[3] < 255
909 //-----------------------------------------------------------------------------
910 // Private internal function prototypes
911 //-----------------------------------------------------------------------------
913 static OCDoHandle GenerateInvocationHandle();
914 static OCStackResult initResources();
915 static void insertResource(OCResource *resource);
916 static OCResource *findResource(OCResource *resource);
917 static void insertResourceType(OCResource *resource,
918 OCResourceType *resourceType);
919 static OCResourceType *findResourceTypeAtIndex(OCResourceHandle handle,
921 static void insertResourceInterface(OCResource *resource,
922 OCResourceInterface *resourceInterface);
923 static OCResourceInterface *findResourceInterfaceAtIndex(
924 OCResourceHandle handle, uint8_t index);
925 static void deleteResourceType(OCResourceType *resourceType);
926 static void deleteResourceInterface(OCResourceInterface *resourceInterface);
927 static void deleteResourceElements(OCResource *resource);
928 static int deleteResource(OCResource *resource);
929 static void deleteAllResources();
930 static void incrementSequenceNumber(OCResource * resPtr);
931 static OCStackResult verifyUriQueryLength(const char * inputUri,
933 static uint8_t OCIsPacketTransferRequired(const char *request, const char *response, uint16_t size);
934 OCStackResult getResourceType(const char * query, unsigned char** resourceType);
936 //-----------------------------------------------------------------------------
938 //-----------------------------------------------------------------------------
941 * Initialize the OC Stack. Must be called prior to starting the stack.
944 * IP Address of host device
946 * Port of host device
948 * Host device is client, server, or client-server
951 * OC_STACK_OK - no errors
952 * OC_STACK_ERROR - stack init error
954 OCStackResult OCInit(const char *ipAddr, uint16_t port, OCMode mode)
956 OCStackResult result = OC_STACK_ERROR;
957 OC_LOG(INFO, TAG, PCF("Entering OCInit"));
961 OC_LOG_V(INFO, TAG, "IP Address = %s", ipAddr);
967 //It is ok to select network to CA_WIFI for now
968 CAResult_t caResult = CASelectNetwork(CA_WIFI|CA_ETHERNET);
969 if(caResult == CA_STATUS_OK)
971 OC_LOG(INFO, TAG, PCF("CASelectNetwork to WIFI"));
972 caResult = CARegisterHandler(HandleCARequests, HandleCAResponses);
973 if(caResult == CA_STATUS_OK)
975 OC_LOG(INFO, TAG, PCF("CARegisterHandler..."));
976 stackState = OC_STACK_INITIALIZED;
977 result = OC_STACK_OK;
981 caResult = CAStartDiscoveryServer();
982 OC_LOG(INFO, TAG, PCF("Client mode: CAStartDiscoveryServer"));
985 caResult = CAStartListeningServer();
986 OC_LOG(INFO, TAG, PCF("Server mode: CAStartListeningServer"));
988 case OC_CLIENT_SERVER:
989 caResult = CAStartListeningServer();
990 if(caResult == CA_STATUS_OK)
992 caResult = CAStartDiscoveryServer();
994 OC_LOG(INFO, TAG, PCF("Client-server mode"));
997 OC_LOG(ERROR, TAG, PCF("Invalid mode"));
998 return OC_STACK_ERROR;
1003 if (caResult == CA_STATUS_OK)
1005 result = OC_STACK_OK;
1009 result = OC_STACK_ERROR;
1016 OC_LOG(INFO, TAG, PCF("Client mode"));
1019 OC_LOG(INFO, TAG, PCF("Server mode"));
1021 case OC_CLIENT_SERVER:
1022 OC_LOG(INFO, TAG, PCF("Client-server mode"));
1025 OC_LOG(ERROR, TAG, PCF("Invalid mode"));
1026 return OC_STACK_ERROR;
1030 // Make call to OCCoAP layer
1031 result = OCInitCoAP(ipAddr, (uint16_t) port, myStackMode);
1035 defaultDeviceHandler = NULL;
1037 #ifdef WITH_PRESENCE
1038 PresenceTimeOutSize = sizeof(PresenceTimeOut)/sizeof(PresenceTimeOut[0]) - 1;
1039 #endif // WITH_PRESENCE
1041 if (result == OC_STACK_OK)
1043 stackState = OC_STACK_INITIALIZED;
1045 // Initialize resource
1046 if(result == OC_STACK_OK && myStackMode != OC_CLIENT)
1048 result = initResources();
1050 if(result != OC_STACK_OK)
1052 OC_LOG(ERROR, TAG, PCF("Stack initialization error"));
1058 * Stop the OC stack. Use for a controlled shutdown.
1060 * OC_STACK_OK - no errors
1061 * OC_STACK_ERROR - stack not initialized
1063 OCStackResult OCStop()
1065 OCStackResult result = OC_STACK_ERROR;
1067 OC_LOG(INFO, TAG, PCF("Entering OCStop"));
1069 if (stackState == OC_STACK_UNINIT_IN_PROGRESS)
1071 OC_LOG(DEBUG, TAG, PCF("Stack already stopping, exiting"));
1074 else if (stackState != OC_STACK_INITIALIZED)
1076 OC_LOG(ERROR, TAG, PCF("Stack not initialized"));
1077 return OC_STACK_ERROR;
1080 stackState = OC_STACK_UNINIT_IN_PROGRESS;
1082 #ifdef WITH_PRESENCE
1083 // Ensure that the TTL associated with ANY and ALL presence notifications originating from
1084 // here send with the code "OC_STACK_PRESENCE_STOPPED" result.
1085 presenceResource.presenceTTL = 0;
1086 #endif // WITH_PRESENCE
1088 // Free memory dynamically allocated for resources
1089 deleteAllResources();
1092 // Make call to OCCoAP layer
1093 if (OCStopCoAP() == OC_STACK_OK)
1095 // Remove all observers
1096 DeleteObserverList();
1097 // Remove all the client callbacks
1098 DeleteClientCBList();
1099 stackState = OC_STACK_UNINITIALIZED;
1100 result = OC_STACK_OK;
1102 stackState = OC_STACK_INITIALIZED;
1103 result = OC_STACK_ERROR;
1106 // Deinit security blob
1107 DeinitOCSecurityInfo();
1109 if (result != OC_STACK_OK) {
1110 OC_LOG(ERROR, TAG, PCF("Stack stop error"));
1117 * Verify the lengths of the URI and the query separately
1119 * @param inputUri - Input URI and query.
1120 * @param uriLen - The length of the initial URI with query.
1122 * Note: The '?' that appears after the URI is not considered as
1123 * a part of the query.
1125 OCStackResult verifyUriQueryLength(const char *inputUri, uint16_t uriLen)
1129 query = strchr (inputUri, '?');
1133 if((query - inputUri) > MAX_URI_LENGTH)
1135 return OC_STACK_INVALID_URI;
1138 if((inputUri + uriLen - 1 - query) > MAX_QUERY_LENGTH)
1140 return OC_STACK_INVALID_QUERY;
1143 else if(uriLen > MAX_URI_LENGTH)
1145 return OC_STACK_INVALID_URI;
1151 * Discover or Perform requests on a specified resource (specified by that Resource's respective URI).
1153 * @param handle - @ref OCDoHandle to refer to the request sent out on behalf of calling this API.
1154 * @param method - @ref OCMethod to perform on the resource
1155 * @param requiredUri - URI of the resource to interact with
1156 * @param referenceUri - URI of the reference resource
1157 * @param request - JSON encoded request
1158 * @param qos - quality of service
1159 * @param cbData - struct that contains asynchronous callback function that is invoked
1160 * by the stack when discovery or resource interaction is complete
1161 * @param options - The address of an array containing the vendor specific header
1162 * header options to be sent with the request
1163 * @param numOptions - Number of vendor specific header options to be included
1166 * OC_STACK_OK - no errors
1167 * OC_STACK_INVALID_CALLBACK - invalid callback function pointer
1168 * OC_STACK_INVALID_METHOD - invalid resource method
1169 * OC_STACK_INVALID_URI - invalid required or reference URI
1172 OCStackResult OCDoResource(OCDoHandle *handle, OCMethod method, const char *requiredUri,
1173 const char *referenceUri, const char *request, uint8_t conType,
1174 OCQualityOfService qos, OCCallbackData *cbData,
1175 OCHeaderOption * options, uint8_t numOptions)
1177 OCStackResult OCDoResource(OCDoHandle *handle, OCMethod method, const char *requiredUri,
1178 const char *referenceUri, const char *request,
1179 OCQualityOfService qos, OCCallbackData *cbData,
1180 OCHeaderOption * options, uint8_t numOptions)
1183 OCStackResult result = OC_STACK_ERROR;
1185 ClientCB *clientCB = NULL;
1186 unsigned char * requestUri = NULL;
1187 unsigned char * resourceType = NULL;
1188 unsigned char * query = NULL;
1189 char * newUri = (char *)requiredUri;
1190 (void) referenceUri;
1192 CARemoteEndpoint_t* endpoint = NULL;
1193 CAResult_t caResult;
1194 CAToken_t caToken = NULL;
1195 CAInfo_t requestData;
1196 CARequestInfo_t requestInfo;
1197 CAGroupEndpoint_t grpEnd;
1199 // To track if memory is allocated for additional header options
1200 uint8_t hdrOptionMemAlloc = 0;
1203 OC_LOG(INFO, TAG, PCF("Entering OCDoResource"));
1205 // Validate input parameters
1206 VERIFY_NON_NULL(cbData, FATAL, OC_STACK_INVALID_CALLBACK);
1207 VERIFY_NON_NULL(cbData->cb, FATAL, OC_STACK_INVALID_CALLBACK);
1209 TODO ("Need to form the final query by concatenating require and reference URI's");
1210 VERIFY_NON_NULL(requiredUri, FATAL, OC_STACK_INVALID_URI);
1212 uint16_t uriLen = strlen(requiredUri);
1214 // ToDo: We should also check if the requiredUri has a mutlicast address, then qos has to be OC_Low_QOS
1220 case OC_REST_DELETE:
1221 case OC_REST_OBSERVE:
1222 case OC_REST_OBSERVE_ALL:
1223 case OC_REST_CANCEL_OBSERVE:
1225 #ifdef WITH_PRESENCE
1226 case OC_REST_PRESENCE:
1230 result = OC_STACK_INVALID_METHOD;
1234 if((result = verifyUriQueryLength(requiredUri, uriLen)) != OC_STACK_OK)
1239 if((request) && (strlen(request) > MAX_REQUEST_LENGTH))
1241 result = OC_STACK_INVALID_PARAM;
1245 #ifdef WITH_PRESENCE
1246 if(method == OC_REST_PRESENCE)
1248 // Replacing method type with GET because "presence" is a stack layer only implementation.
1249 method = OC_REST_GET;
1250 result = getQueryFromUri(requiredUri, &query, &newUri);
1253 result = getResourceType((char *) query, &resourceType);
1256 OC_LOG_V(DEBUG, TAG, "Got Resource Type: %s", resourceType);
1260 OC_LOG(DEBUG, TAG, PCF("Resource type is NULL."));
1265 OC_LOG(DEBUG, TAG, PCF("Query string is NULL."));
1267 if(result != OC_STACK_OK)
1272 #endif // WITH_PRESENCE
1274 requestUri = (unsigned char *) OCMalloc(uriLen + 1);
1277 memcpy(requestUri, newUri, (uriLen + 1));
1281 result = OC_STACK_NO_MEMORY;
1285 *handle = GenerateInvocationHandle();
1288 result = OC_STACK_NO_MEMORY;
1293 memset(&requestData, 0, sizeof(CAInfo_t));
1294 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
1295 memset(&grpEnd, 0, sizeof(CAGroupEndpoint_t));
1299 case OC_REST_OBSERVE:
1300 case OC_REST_OBSERVE_ALL:
1301 case OC_REST_CANCEL_OBSERVE:
1303 requestInfo.method = CA_GET;
1308 requestInfo.method = CA_PUT;
1313 requestInfo.method = CA_POST;
1316 case OC_REST_DELETE:
1318 requestInfo.method = CA_DELETE;
1321 #ifdef WITH_PRESENCE
1322 case OC_REST_PRESENCE:
1323 //TODO-CA: What should be the CA method?
1327 result = OC_STACK_INVALID_METHOD;
1331 //High QoS is not supported
1332 if(qos == OC_HIGH_QOS)
1334 result = OC_STACK_INVALID_PARAM;
1337 // TODO-CA: Handle multi-cast scenario
1338 // Create remote end point
1339 caResult = CACreateRemoteEndpoint(newUri, conType, &endpoint);
1340 // TODO-CA: Connectivity type should be passed to API
1341 endpoint->connectivityType = conType;
1343 if (caResult != CA_STATUS_OK)
1345 OC_LOG(ERROR, TAG, PCF("CACreateRemoteEndpoint error"));
1350 caResult = CAGenerateToken(&caToken);
1352 //TODO-CA Remove this temporary fix (for some reason same token is being generated)
1355 caToken[0] += count;
1357 if (caResult != CA_STATUS_OK)
1359 OC_LOG(ERROR, TAG, PCF("CAGenerateToken error"));
1364 // TODO-CA: Map QoS to the right CA msg type
1365 requestData.type = CA_MSG_NONCONFIRM;
1366 requestData.token = caToken;
1367 if ((method == OC_REST_OBSERVE) || (method == OC_REST_OBSERVE_ALL))
1369 result = CreateObserveHeaderOption (&(requestData.options), options,
1370 numOptions, OC_OBSERVE_REGISTER);
1371 if (result != OC_STACK_OK)
1375 hdrOptionMemAlloc = 1;
1376 requestData.numOptions = numOptions + 1;
1380 requestData.options = (CAHeaderOption_t*)options;
1381 requestData.numOptions = numOptions;
1383 requestData.payload = (char *)request;
1385 requestInfo.info = requestData;
1388 //TODO-CA Change This logic. Currently hardcoded to WIFI & ETHERNET
1389 //Need to support other adapter types.
1390 if(conType == (CA_WIFI | CA_ETHERNET))
1392 //TODO-CA remove hardcoded resource uri. Instead, extract it from newUri
1393 grpEnd.connectivityType = conType;
1394 grpEnd.resourceUri = "/oc/core";
1396 caResult = CASendRequestToAll(&grpEnd, &requestInfo);
1400 caResult = CASendRequest(endpoint, &requestInfo);
1402 if (caResult != CA_STATUS_OK)
1404 OC_LOG(ERROR, TAG, PCF("CASendRequest"));
1408 if((result = AddClientCB(&clientCB, cbData, &caToken, handle, method,
1409 requestUri, resourceType)) != OC_STACK_OK)
1411 result = OC_STACK_NO_MEMORY;
1417 // Generate token which will be used by OCStack to match responses received
1419 OCGenerateCoAPToken(&token);
1421 if((result = AddClientCB(&clientCB, cbData, &token, handle, method, requestUri, resourceType))
1424 result = OC_STACK_NO_MEMORY;
1428 // Make call to OCCoAP layer
1429 result = OCDoCoAPResource(method, qos, &token, newUri, request, options, numOptions);
1433 if(newUri != requiredUri)
1437 if (result != OC_STACK_OK)
1439 OC_LOG(ERROR, TAG, PCF("OCDoResource error"));
1440 FindAndDeleteClientCB(clientCB);
1443 CADestroyRemoteEndpoint(endpoint);
1444 if (hdrOptionMemAlloc)
1446 OCFree(requestData.options);
1453 * Cancel a request associated with a specific @ref OCDoResource invocation.
1455 * @param handle - Used to identify a specific OCDoResource invocation.
1456 * @param qos - used to specify Quality of Service (read below for more info)
1457 * @param options- used to specify vendor specific header options when sending
1458 * explicit observe cancellation
1459 * @param numOptions- Number of header options to be included
1462 * OC_STACK_OK - No errors; Success
1463 * OC_STACK_INVALID_PARAM - The handle provided is invalid.
1465 OCStackResult OCCancel(OCDoHandle handle, OCQualityOfService qos, OCHeaderOption * options,
1469 * This ftn is implemented one of two ways in the case of observation:
1471 * 1. qos == OC_NON_CONFIRMABLE. When observe is unobserved..
1472 * Remove the callback associated on client side.
1473 * When the next notification comes in from server,
1474 * reply with RESET message to server.
1475 * Keep in mind that the server will react to RESET only
1476 * if the last notification was sent ans CON
1478 * 2. qos == OC_CONFIRMABLE. When OCCancel is called,
1479 * and it is associated with an observe request
1480 * (i.e. ClientCB->method == OC_REST_OBSERVE || OC_REST_OBSERVE_ALL),
1481 * Send CON Observe request to server with
1482 * observe flag = OC_RESOURCE_OBSERVE_DEREGISTER.
1483 * Remove the callback associated on client side.
1485 OCStackResult ret = OC_STACK_OK;
1487 CARemoteEndpoint_t* endpoint = NULL;
1488 CAResult_t caResult;
1489 CAInfo_t requestData;
1490 CARequestInfo_t requestInfo;
1491 // Track if memory is allocated for additional header options
1492 uint8_t hdrOptionMemAlloc = 0;
1496 return OC_STACK_INVALID_PARAM;
1499 OC_LOG(INFO, TAG, PCF("Entering OCCancel"));
1501 ClientCB *clientCB = GetClientCB(NULL, handle, NULL);
1504 switch (clientCB->method)
1506 case OC_REST_OBSERVE:
1507 case OC_REST_OBSERVE_ALL:
1509 caResult = CACreateRemoteEndpoint((char *)clientCB->requestUri, CA_WIFI,
1511 endpoint->connectivityType = CA_WIFI;
1512 if (caResult != CA_STATUS_OK)
1514 OC_LOG(ERROR, TAG, PCF("CACreateRemoteEndpoint error"));
1515 return OC_STACK_ERROR;
1518 memset(&requestData, 0, sizeof(CAInfo_t));
1519 // TODO-CA: Map QoS to the right CA msg type
1520 requestData.type = CA_MSG_NONCONFIRM;
1521 requestData.token = clientCB->token;
1522 if (CreateObserveHeaderOption (&(requestData.options),
1523 options, numOptions, OC_OBSERVE_DEREGISTER) != OC_STACK_OK)
1525 return OC_STACK_ERROR;
1527 hdrOptionMemAlloc = 1;
1528 requestData.numOptions = numOptions + 1;
1529 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
1530 requestInfo.method = CA_GET;
1531 requestInfo.info = requestData;
1533 caResult = CASendRequest(endpoint, &requestInfo);
1534 if (caResult != CA_STATUS_OK)
1536 OC_LOG(ERROR, TAG, PCF("CASendRequest error"));
1538 if(caResult == CA_STATUS_OK)
1543 if(qos == OC_HIGH_QOS)
1545 ret = OCDoCoAPResource(OC_REST_CANCEL_OBSERVE, qos,
1546 &(clientCB->token), (const char *) clientCB->requestUri, NULL, options,
1551 FindAndDeleteClientCB(clientCB);
1555 #ifdef WITH_PRESENCE
1556 case OC_REST_PRESENCE:
1557 FindAndDeleteClientCB(clientCB);
1561 return OC_STACK_INVALID_METHOD;
1565 CADestroyRemoteEndpoint(endpoint);
1566 if (hdrOptionMemAlloc)
1568 OCFree(requestData.options);
1574 #ifdef WITH_PRESENCE
1575 OCStackResult OCProcessPresence()
1577 OCStackResult result = OC_STACK_OK;
1578 uint8_t ipAddr[4] = { 0 };
1581 OC_LOG(INFO, TAG, PCF("Entering RequestPresence"));
1582 ClientCB* cbNode = NULL;
1584 OCClientResponse clientResponse;
1585 OCResponse * response = NULL;
1587 LL_FOREACH(cbList, cbNode) {
1588 if(OC_REST_PRESENCE == cbNode->method)
1590 if(cbNode->presence)
1592 uint32_t now = GetTime(0);
1593 OC_LOG_V(DEBUG, TAG, "----------------this TTL level %d", cbNode->presence->TTLlevel);
1594 OC_LOG_V(DEBUG, TAG, "----------------current ticks %d", now);
1597 if(cbNode->presence->TTLlevel >= (PresenceTimeOutSize + 1))
1602 if(cbNode->presence->TTLlevel < PresenceTimeOutSize){
1603 OC_LOG_V(DEBUG, TAG, "----------------timeout ticks %d",
1604 cbNode->presence->timeOut[cbNode->presence->TTLlevel]);
1607 if(cbNode->presence->TTLlevel >= PresenceTimeOutSize)
1609 OC_LOG(DEBUG, TAG, PCF("----------------No more timeout ticks"));
1610 if (ParseIPv4Address( cbNode->requestUri, ipAddr, &port))
1612 OCBuildIPv4Address(ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3], port,
1614 result = FormOCClientResponse(&clientResponse, OC_STACK_PRESENCE_TIMEOUT,
1615 (OCDevAddr *) &dst, 0, NULL);
1616 if(result != OC_STACK_OK)
1620 result = FormOCResponse(&response, cbNode, 0, NULL, NULL,
1621 &cbNode->token, &clientResponse, NULL);
1622 if(result != OC_STACK_OK)
1627 // Increment the TTLLevel (going to a next state), so we don't keep
1628 // sending presence notification to client.
1629 cbNode->presence->TTLlevel++;
1630 OC_LOG_V(DEBUG, TAG, "----------------moving to TTL level %d",
1631 cbNode->presence->TTLlevel);
1635 result = OC_STACK_INVALID_IP;
1638 HandleStackResponses(response);
1640 if(now >= cbNode->presence->timeOut[cbNode->presence->TTLlevel])
1642 OC_LOG(DEBUG, TAG, PCF("time to test server presence =========="));
1644 OCGenerateCoAPToken(&token);
1645 result = OCDoCoAPResource(OC_REST_GET, OC_LOW_QOS,
1646 &token, (const char *)cbNode->requestUri, NULL, NULL, 0);
1647 if(result != OC_STACK_OK)
1651 cbNode->presence->TTLlevel++;
1652 OC_LOG_V(DEBUG, TAG, "----------------moving to TTL level %d", cbNode->presence->TTLlevel);
1658 if (result != OC_STACK_OK)
1660 OC_LOG(ERROR, TAG, PCF("OCProcessPresence error"));
1667 * Called in main loop of OC client or server. Allows low-level processing of
1671 * OC_STACK_OK - no errors
1672 * OC_STACK_ERROR - stack process error
1674 OCStackResult OCProcess() {
1676 OC_LOG(INFO, TAG, PCF("Entering OCProcess"));
1677 #ifdef WITH_PRESENCE
1678 OCProcessPresence();
1681 CAHandleRequestResponse();
1689 #ifdef WITH_PRESENCE
1691 * When operating in @ref OCServer or @ref OCClientServer mode, this API will start sending out
1692 * presence notifications to clients via multicast. Once this API has been called with a success,
1693 * clients may query for this server's presence and this server's stack will respond via multicast.
1695 * Server can call this function when it comes online for the first time, or when it comes back
1696 * online from offline mode, or when it re enters network.
1698 * @param ttl - Time To Live in seconds
1699 * Note: If ttl is '0', then the default stack value will be used (60 Seconds).
1702 * OC_STACK_OK - No errors; Success
1704 OCStackResult OCStartPresence(const uint32_t ttl)
1706 OCChangeResourceProperty(
1707 &(((OCResource *)presenceResource.handle)->resourceProperties),
1712 presenceResource.presenceTTL = ttl;
1715 if(OC_PRESENCE_UNINITIALIZED == presenceState)
1717 OCDevAddr multiCastAddr;
1720 presenceState = OC_PRESENCE_INITIALIZED;
1721 OCGenerateCoAPToken(&token);
1722 OCBuildIPv4Address(224, 0, 1, 187, 5683, &multiCastAddr);
1724 CAAddress_t addressInfo;
1725 strncpy(addressInfo.IP.ipAddress, "224.0.1.187", CA_IPADDR_SIZE);
1726 addressInfo.IP.port = 5298;
1728 CAToken_t caToken = NULL;
1729 CAGenerateToken(&caToken);
1731 AddCAObserver(OC_PRESENCE_URI, NULL, 0, &token,
1732 &multiCastAddr, (OCResource *)presenceResource.handle, OC_LOW_QOS,
1733 &addressInfo, CA_WIFI, caToken);
1735 //add the presence observer
1736 AddObserver(OC_PRESENCE_URI, NULL, 0, &token, &multiCastAddr,
1737 (OCResource *)presenceResource.handle, OC_LOW_QOS);
1741 // Each time OCStartPresence is called
1742 // a different random 32-bit integer number is used
1743 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
1745 return SendPresenceNotification(NULL);
1749 * When operating in @ref OCServer or @ref OCClientServer mode, this API will stop sending out
1750 * presence notifications to clients via multicast. Once this API has been called with a success,
1751 * this server's stack will not respond to clients querying for this server's presence.
1753 * Server can call this function when it is terminating, going offline, or when going
1754 * away from network.
1757 * OC_STACK_OK - No errors; Success
1759 OCStackResult OCStopPresence()
1761 OCStackResult result = OC_STACK_ERROR;
1762 //make resource inactive
1763 result = OCChangeResourceProperty(
1764 &(((OCResource *) presenceResource.handle)->resourceProperties),
1766 result = SendPresenceNotification(NULL);
1773 OCStackResult OCSetDefaultDeviceEntityHandler(OCDeviceEntityHandler entityHandler)
1775 defaultDeviceHandler = entityHandler;
1780 OCStackResult OCSetDeviceInfo(OCDeviceInfo deviceInfo)
1782 OC_LOG(INFO, TAG, PCF("Entering OCSetDeviceInfo"));
1784 if(myStackMode == OC_CLIENT)
1786 return OC_STACK_ERROR;
1789 return SaveDeviceInfo(deviceInfo);
1795 * @param handle - pointer to handle to newly created resource. Set by ocstack. Used to refer to resource
1796 * @param resourceTypeName - name of resource type. Example: "core.led"
1797 * @param resourceInterfaceName - name of resource interface. Example: "core.rw"
1798 * @param uri - URI of the resource. Example: "/a/led"
1799 * @param entityHandler - entity handler function that is called by ocstack to handle requests, etc
1800 * NULL for default entity handler
1801 * @param resourceProperties - properties supported by resource. Example: OC_DISCOVERABLE|OC_OBSERVABLE
1804 * OC_STACK_OK - no errors
1805 * OC_STACK_ERROR - stack process error
1807 OCStackResult OCCreateResource(OCResourceHandle *handle,
1808 const char *resourceTypeName,
1809 const char *resourceInterfaceName,
1810 const char *uri, OCEntityHandler entityHandler,
1811 uint8_t resourceProperties) {
1813 OCResource *pointer = NULL;
1816 OCStackResult result = OC_STACK_ERROR;
1818 OC_LOG(INFO, TAG, PCF("Entering OCCreateResource"));
1820 if(myStackMode == OC_CLIENT)
1824 // Validate parameters
1825 if(!uri || (strlen(uri) == 0))
1827 OC_LOG(ERROR, TAG, PCF("URI is invalid"));
1828 return OC_STACK_INVALID_URI;
1830 // Is it presented during resource discovery?
1831 if (!handle || !resourceTypeName) {
1832 OC_LOG(ERROR, TAG, PCF("Input parameter is NULL"));
1833 return OC_STACK_INVALID_PARAM;
1836 if(!resourceInterfaceName || strlen(resourceInterfaceName) == 0) {
1837 resourceInterfaceName = OC_RSRVD_INTERFACE_DEFAULT;
1840 // Make sure resourceProperties bitmask has allowed properties specified
1841 if (resourceProperties
1842 > (OC_ACTIVE | OC_DISCOVERABLE | OC_OBSERVABLE | OC_SLOW | OC_SECURE)) {
1843 OC_LOG(ERROR, TAG, PCF("Invalid property"));
1844 return OC_STACK_INVALID_PARAM;
1847 // If the headResource is NULL, then no resources have been created...
1848 pointer = headResource;
1850 // At least one resources is in the resource list, so we need to search for
1851 // repeated URLs, which are not allowed. If a repeat is found, exit with an error
1853 if (strcmp(uri, pointer->uri) == 0) {
1854 OC_LOG(ERROR, TAG, PCF("URI already in use"));
1855 return OC_STACK_INVALID_PARAM;
1857 pointer = pointer->next;
1860 // Create the pointer and insert it into the resource list
1861 pointer = (OCResource *) OCCalloc(1, sizeof(OCResource));
1865 pointer->sequenceNum = OC_OFFSET_SEQUENCE_NUMBER;
1867 insertResource(pointer);
1870 size = strlen(uri) + 1;
1871 str = (char *) OCMalloc(size);
1875 strncpy(str, uri, size);
1878 // Set properties. Set OC_ACTIVE
1879 pointer->resourceProperties = (OCResourceProperty) (resourceProperties
1882 // Add the resourcetype to the resource
1883 result = BindResourceTypeToResource(pointer, resourceTypeName);
1884 if (result != OC_STACK_OK) {
1885 OC_LOG(ERROR, TAG, PCF("Error adding resourcetype"));
1889 // Add the resourceinterface to the resource
1890 result = BindResourceInterfaceToResource(pointer, resourceInterfaceName);
1891 if (result != OC_STACK_OK) {
1892 OC_LOG(ERROR, TAG, PCF("Error adding resourceinterface"));
1896 // If an entity handler has been passed, attach it to the newly created
1897 // resource. Otherwise, set the default entity handler.
1900 pointer->entityHandler = entityHandler;
1904 pointer->entityHandler = defaultResourceEHandler;
1908 result = OC_STACK_OK;
1910 #ifdef WITH_PRESENCE
1911 if(presenceResource.handle)
1913 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
1914 SendPresenceNotification(pointer->rsrcType);
1918 if (result != OC_STACK_OK)
1920 // Deep delete of resource and other dynamic elements that it contains
1921 deleteResource(pointer);
1930 * Create a resource. with host ip address for remote resource
1932 * @param handle - pointer to handle to newly created resource. Set by ocstack.
1933 * Used to refer to resource
1934 * @param resourceTypeName - name of resource type. Example: "core.led"
1935 * @param resourceInterfaceName - name of resource interface. Example: "core.rw"
1936 * @param host - HOST address of the remote resource. Example: "coap://xxx.xxx.xxx.xxx:xxxxx"
1937 * @param uri - URI of the resource. Example: "/a/led"
1938 * @param entityHandler - entity handler function that is called by ocstack to handle requests, etc
1939 * NULL for default entity handler
1940 * @param resourceProperties - properties supported by resource.
1941 * Example: OC_DISCOVERABLE|OC_OBSERVABLE
1944 * OC_STACK_OK - no errors
1945 * OC_STACK_ERROR - stack process error
1948 OCStackResult OCCreateResourceWithHost(OCResourceHandle *handle,
1949 const char *resourceTypeName,
1950 const char *resourceInterfaceName,
1953 OCEntityHandler entityHandler,
1954 uint8_t resourceProperties)
1958 OCStackResult result = OC_STACK_ERROR;
1960 result = OCCreateResource(handle, resourceTypeName, resourceInterfaceName,
1961 uri, entityHandler, resourceProperties);
1963 if (result != OC_STACK_ERROR)
1966 size = strlen(host) + 1;
1967 str = (char *) OCMalloc(size);
1970 return OC_STACK_ERROR;
1972 strncpy(str, host, size);
1973 ((OCResource *) *handle)->host = str;
1980 * Add a resource to a collection resource.
1982 * @param collectionHandle - handle to the collection resource
1983 * @param resourceHandle - handle to resource to be added to the collection resource
1986 * OC_STACK_OK - no errors
1987 * OC_STACK_ERROR - stack process error
1988 * OC_STACK_INVALID_PARAM - invalid collectionhandle
1990 OCStackResult OCBindResource(
1991 OCResourceHandle collectionHandle, OCResourceHandle resourceHandle) {
1992 OCResource *resource;
1995 OC_LOG(INFO, TAG, PCF("Entering OCBindResource"));
1997 // Validate parameters
1998 VERIFY_NON_NULL(collectionHandle, ERROR, OC_STACK_ERROR);
1999 VERIFY_NON_NULL(resourceHandle, ERROR, OC_STACK_ERROR);
2000 // Container cannot contain itself
2001 if (collectionHandle == resourceHandle) {
2002 OC_LOG(ERROR, TAG, PCF("Added handle equals collection handle"));
2003 return OC_STACK_INVALID_PARAM;
2006 // Use the handle to find the resource in the resource linked list
2007 resource = findResource((OCResource *) collectionHandle);
2009 OC_LOG(ERROR, TAG, PCF("Collection handle not found"));
2010 return OC_STACK_INVALID_PARAM;
2013 // Look for an open slot to add add the child resource.
2014 // If found, add it and return success
2015 for (i = 0; i < MAX_CONTAINED_RESOURCES; i++) {
2016 if (!resource->rsrcResources[i]) {
2017 resource->rsrcResources[i] = (OCResource *) resourceHandle;
2018 OC_LOG(INFO, TAG, PCF("resource bound"));
2023 #ifdef WITH_PRESENCE
2024 if(presenceResource.handle)
2026 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2027 SendPresenceNotification(((OCResource *) resourceHandle)->rsrcType);
2031 // Unable to add resourceHandle, so return error
2032 return OC_STACK_ERROR;
2036 * Remove a resource from a collection resource.
2038 * @param collectionHandle - handle to the collection resource
2039 * @param resourceHandle - handle to resource to be added to the collection resource
2042 * OC_STACK_OK - no errors
2043 * OC_STACK_ERROR - stack process error
2044 * OC_STACK_INVALID_PARAM - invalid collectionHandle
2046 OCStackResult OCUnBindResource(
2047 OCResourceHandle collectionHandle, OCResourceHandle resourceHandle) {
2048 OCResource *resource;
2051 OC_LOG(INFO, TAG, PCF("Entering OCUnBindResource"));
2053 // Validate parameters
2054 VERIFY_NON_NULL(collectionHandle, ERROR, OC_STACK_ERROR);
2055 VERIFY_NON_NULL(resourceHandle, ERROR, OC_STACK_ERROR);
2056 // Container cannot contain itself
2057 if (collectionHandle == resourceHandle) {
2058 OC_LOG(ERROR, TAG, PCF("removing handle equals collection handle"));
2059 return OC_STACK_INVALID_PARAM;
2062 // Use the handle to find the resource in the resource linked list
2063 resource = findResource((OCResource *) collectionHandle);
2065 OC_LOG(ERROR, TAG, PCF("Collection handle not found"));
2066 return OC_STACK_INVALID_PARAM;
2069 // Look for an open slot to add add the child resource.
2070 // If found, add it and return success
2071 for (i = 0; i < MAX_CONTAINED_RESOURCES; i++) {
2072 if (resourceHandle == resource->rsrcResources[i]) {
2073 resource->rsrcResources[i] = (OCResource *) NULL;
2074 OC_LOG(INFO, TAG, PCF("resource unbound"));
2079 OC_LOG(INFO, TAG, PCF("resource not found in collection"));
2081 #ifdef WITH_PRESENCE
2082 if(presenceResource.handle)
2084 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2085 SendPresenceNotification(((OCResource *) resourceHandle)->rsrcType);
2089 // Unable to add resourceHandle, so return error
2090 return OC_STACK_ERROR;
2093 OCStackResult BindResourceTypeToResource(OCResource* resource,
2094 const char *resourceTypeName)
2096 OCResourceType *pointer = NULL;
2099 OCStackResult result = OC_STACK_ERROR;
2101 OC_LOG(INFO, TAG, PCF("Entering BindResourceTypeToResource"));
2103 // Validate parameters
2104 VERIFY_NON_NULL(resourceTypeName, ERROR, OC_STACK_INVALID_PARAM);
2105 // TODO: Does resource attribute resentation really have to be maintained in stack?
2106 // Is it presented during resource discovery?
2108 TODO ("Make sure that the resourcetypename doesn't already exist in the resource");
2110 // Create the resourcetype and insert it into the resource list
2111 pointer = (OCResourceType *) OCCalloc(1, sizeof(OCResourceType));
2116 // Set the resourceTypeName
2117 size = strlen(resourceTypeName) + 1;
2118 str = (char *) OCMalloc(size);
2122 strncpy(str, resourceTypeName, size);
2123 pointer->resourcetypename = str;
2125 insertResourceType(resource, pointer);
2126 result = OC_STACK_OK;
2128 exit: if (result != OC_STACK_OK) {
2136 OCStackResult BindResourceInterfaceToResource(OCResource* resource,
2137 const char *resourceInterfaceName)
2139 OCResourceInterface *pointer = NULL;
2142 OCStackResult result = OC_STACK_ERROR;
2144 OC_LOG(INFO, TAG, PCF("Entering BindResourceInterfaceToResource"));
2146 // Validate parameters
2147 VERIFY_NON_NULL(resourceInterfaceName, ERROR, OC_STACK_INVALID_PARAM);
2149 TODO ("Make sure that the resourceinterface name doesn't already exist in the resource");
2151 // Create the resourceinterface and insert it into the resource list
2152 pointer = (OCResourceInterface *) OCCalloc(1, sizeof(OCResourceInterface));
2157 // Set the resourceinterface name
2158 size = strlen(resourceInterfaceName) + 1;
2159 str = (char *) OCMalloc(size);
2163 strncpy(str, resourceInterfaceName, size);
2164 pointer->name = str;
2166 // Bind the resourceinterface to the resource
2167 insertResourceInterface(resource, pointer);
2169 result = OC_STACK_OK;
2171 exit: if (result != OC_STACK_OK) {
2180 * Bind a resourcetype to a resource.
2182 * @param handle - handle to the resource
2183 * @param resourceTypeName - name of resource type. Example: "core.led"
2186 * OC_STACK_OK - no errors
2187 * OC_STACK_ERROR - stack process error
2189 OCStackResult OCBindResourceTypeToResource(OCResourceHandle handle,
2190 const char *resourceTypeName) {
2192 OCStackResult result = OC_STACK_ERROR;
2193 OCResource *resource;
2195 // Make sure resource exists
2196 resource = findResource((OCResource *) handle);
2198 OC_LOG(ERROR, TAG, PCF("Resource not found"));
2199 return OC_STACK_ERROR;
2202 // call internal function
2203 result = BindResourceTypeToResource(resource, resourceTypeName);
2205 #ifdef WITH_PRESENCE
2206 if(presenceResource.handle)
2208 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2209 SendPresenceNotification(resource->rsrcType);
2217 * Bind a resourceinterface to a resource.
2219 * @param handle - handle to the resource
2220 * @param resourceInterfaceName - name of resource interface. Example: "oc.mi.b"
2223 * OC_STACK_OK - no errors
2224 * OC_STACK_ERROR - stack process error
2227 OCStackResult OCBindResourceInterfaceToResource(OCResourceHandle handle,
2228 const char *resourceInterfaceName) {
2230 OCStackResult result = OC_STACK_ERROR;
2231 OCResource *resource;
2233 // Make sure resource exists
2234 resource = findResource((OCResource *) handle);
2236 OC_LOG(ERROR, TAG, PCF("Resource not found"));
2237 return OC_STACK_ERROR;
2240 // call internal function
2241 result = BindResourceInterfaceToResource(resource, resourceInterfaceName);
2243 #ifdef WITH_PRESENCE
2244 if(presenceResource.handle)
2246 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2247 SendPresenceNotification(resource->rsrcType);
2255 * Get the number of resources that have been created in the stack.
2257 * @param numResources - pointer to count variable
2260 * OC_STACK_OK - no errors
2261 * OC_STACK_ERROR - stack process error
2264 OCStackResult OCGetNumberOfResources(uint8_t *numResources) {
2265 OCResource *pointer = headResource;
2267 OC_LOG(INFO, TAG, PCF("Entering OCGetNumberOfResources"));
2268 VERIFY_NON_NULL(numResources, ERROR, OC_STACK_INVALID_PARAM);
2271 *numResources = *numResources + 1;
2272 pointer = pointer->next;
2278 * Get a resource handle by index.
2280 * @param index - index of resource, 0 to Count - 1
2283 * Resource handle - if found
2284 * NULL - if not found
2286 OCResourceHandle OCGetResourceHandle(uint8_t index) {
2287 OCResource *pointer = headResource;
2290 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceHandle"));
2292 // Iterate through the list
2293 while ((i < index) && pointer) {
2295 pointer = pointer->next;
2297 return (OCResourceHandle) pointer;
2301 * Delete resource specified by handle. Deletes resource and all resourcetype and resourceinterface
2304 * @param handle - handle of resource to be deleted
2307 * OC_STACK_OK - no errors
2308 * OC_STACK_ERROR - stack process error
2309 * OC_STACK_NO_RESOURCE - resource not found
2310 * OC_STACK_INVALID_PARAM - invalid param
2312 OCStackResult OCDeleteResource(OCResourceHandle handle) {
2313 OC_LOG(INFO, TAG, PCF("Entering OCDeleteResource"));
2316 OC_LOG(ERROR, TAG, PCF("Invalid param"));
2317 return OC_STACK_INVALID_PARAM;
2320 OCResource *resource = findResource((OCResource *) handle);
2321 if (resource == NULL) {
2322 OC_LOG(ERROR, TAG, PCF("Resource not found"));
2323 return OC_STACK_NO_RESOURCE;
2326 if (deleteResource((OCResource *) handle) == 0) {
2327 OC_LOG(ERROR, TAG, PCF("Error deleting resource"));
2328 return OC_STACK_ERROR;
2335 * Get the URI of the resource specified by handle.
2337 * @param handle - handle of resource
2339 * URI string - if resource found
2340 * NULL - resource not found
2342 const char *OCGetResourceUri(OCResourceHandle handle) {
2343 OCResource *resource;
2344 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceUri"));
2346 resource = findResource((OCResource *) handle);
2348 return resource->uri;
2350 return (const char *) NULL;
2354 * Get the properties of the resource specified by handle.
2355 * NOTE: that after a resource is created, the OC_ACTIVE property is set
2356 * for the resource by the stack.
2358 * @param handle - handle of resource
2360 * property bitmap - if resource found
2361 * NULL - resource not found
2363 uint8_t OCGetResourceProperties(OCResourceHandle handle) {
2364 OCResource *resource;
2365 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceProperties"));
2367 resource = findResource((OCResource *) handle);
2369 return resource->resourceProperties;
2375 * Get the number of resource types of the resource.
2377 * @param handle - handle of resource
2378 * @param numResourceTypes - pointer to count variable
2381 * OC_STACK_OK - no errors
2382 * OC_STACK_ERROR - stack process error
2384 OCStackResult OCGetNumberOfResourceTypes(OCResourceHandle handle,
2385 uint8_t *numResourceTypes) {
2386 OCResource *resource;
2387 OCResourceType *pointer;
2389 OC_LOG(INFO, TAG, PCF("Entering OCGetNumberOfResourceTypes"));
2390 VERIFY_NON_NULL(numResourceTypes, ERROR, OC_STACK_INVALID_PARAM);
2391 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
2393 *numResourceTypes = 0;
2395 resource = findResource((OCResource *) handle);
2397 pointer = resource->rsrcType;
2399 *numResourceTypes = *numResourceTypes + 1;
2400 pointer = pointer->next;
2407 * Get name of resource type of the resource.
2409 * @param handle - handle of resource
2410 * @param index - index of resource, 0 to Count - 1
2413 * resource type name - if resource found
2414 * NULL - resource not found
2416 const char *OCGetResourceTypeName(OCResourceHandle handle, uint8_t index) {
2417 OCResourceType *resourceType;
2419 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceTypeName"));
2421 resourceType = findResourceTypeAtIndex(handle, index);
2423 return resourceType->resourcetypename;
2425 return (const char *) NULL;
2431 * Get the number of resource interfaces of the resource.
2433 * @param handle - handle of resource
2434 * @param numResources - pointer to count variable
2437 * OC_STACK_OK - no errors
2438 * OC_STACK_ERROR - stack process error
2440 OCStackResult OCGetNumberOfResourceInterfaces(OCResourceHandle handle,
2441 uint8_t *numResourceInterfaces) {
2442 OCResourceInterface *pointer;
2443 OCResource *resource;
2445 OC_LOG(INFO, TAG, PCF("Entering OCGetNumberOfResourceInterfaces"));
2447 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
2448 VERIFY_NON_NULL(numResourceInterfaces, ERROR, OC_STACK_INVALID_PARAM);
2450 *numResourceInterfaces = 0;
2451 resource = findResource((OCResource *) handle);
2453 pointer = resource->rsrcInterface;
2455 *numResourceInterfaces = *numResourceInterfaces + 1;
2456 pointer = pointer->next;
2463 * Get name of resource interface of the resource.
2465 * @param handle - handle of resource
2466 * @param index - index of resource, 0 to Count - 1
2469 * resource interface name - if resource found
2470 * NULL - resource not found
2472 const char *OCGetResourceInterfaceName(OCResourceHandle handle, uint8_t index) {
2473 OCResourceInterface *resourceInterface;
2475 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceInterfaceName"));
2477 resourceInterface = findResourceInterfaceAtIndex(handle, index);
2478 if (resourceInterface) {
2479 return resourceInterface->name;
2481 return (const char *) NULL;
2485 * Get resource handle from the collection resource by index.
2487 * @param collectionHandle - handle of collection resource
2488 * @param index - index of contained resource, 0 to Count - 1
2491 * handle to resource - if resource found
2492 * NULL - resource not found
2494 OCResourceHandle OCGetResourceHandleFromCollection(OCResourceHandle collectionHandle,
2496 OCResource *resource;
2498 OC_LOG(INFO, TAG, PCF("Entering OCGetContainedResource"));
2500 if (index >= MAX_CONTAINED_RESOURCES) {
2504 resource = findResource((OCResource *) collectionHandle);
2509 return resource->rsrcResources[index];
2513 * Bind an entity handler to the resource.
2515 * @param handle - handle to the resource that the contained resource is to be bound
2516 * @param entityHandler - entity handler function that is called by ocstack to handle requests, etc
2518 * OC_STACK_OK - no errors
2519 * OC_STACK_ERROR - stack process error
2521 OCStackResult OCBindResourceHandler(OCResourceHandle handle,
2522 OCEntityHandler entityHandler) {
2523 OCResource *resource;
2525 OC_LOG(INFO, TAG, PCF("Entering OCBindResourceHandler"));
2527 // Validate parameters
2528 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
2529 //VERIFY_NON_NULL(entityHandler, ERROR, OC_STACK_INVALID_PARAM);
2531 // Use the handle to find the resource in the resource linked list
2532 resource = findResource((OCResource *)handle);
2534 OC_LOG(ERROR, TAG, PCF("Resource not found"));
2535 return OC_STACK_ERROR;
2539 resource->entityHandler = entityHandler;
2541 #ifdef WITH_PRESENCE
2542 if(presenceResource.handle)
2544 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2545 SendPresenceNotification(resource->rsrcType);
2553 * Get the entity handler for a resource.
2555 * @param handle - handle of resource
2558 * entity handler - if resource found
2559 * NULL - resource not found
2561 OCEntityHandler OCGetResourceHandler(OCResourceHandle handle) {
2562 OCResource *resource;
2564 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceHandler"));
2566 // Use the handle to find the resource in the resource linked list
2567 resource = findResource((OCResource *)handle);
2569 OC_LOG(ERROR, TAG, PCF("Resource not found"));
2574 return resource->entityHandler;
2577 void incrementSequenceNumber(OCResource * resPtr)
2579 // Increment the sequence number
2580 resPtr->sequenceNum += 1;
2581 if (resPtr->sequenceNum == MAX_SEQUENCE_NUMBER)
2583 resPtr->sequenceNum = OC_OFFSET_SEQUENCE_NUMBER+1;
2589 * Notify Presence subscribers that a resource has been modified
2591 * @param resourceType - Handle to the resourceType linked list of resource
2592 * that was modified.
2593 * @param qos - Quality Of Service
2596 #ifdef WITH_PRESENCE
2597 OCStackResult SendPresenceNotification(OCResourceType *resourceType)
2599 OCResource *resPtr = NULL;
2600 OCStackResult result;
2601 OCMethod method = OC_REST_PRESENCE;
2602 uint32_t maxAge = 0;
2603 resPtr = findResource((OCResource *) presenceResource.handle);
2606 return OC_STACK_NO_RESOURCE;
2608 if((((OCResource *) presenceResource.handle)->resourceProperties) & OC_ACTIVE)
2610 maxAge = presenceResource.presenceTTL;
2617 result = SendAllObserverNotification(method, resPtr, maxAge, resourceType, OC_LOW_QOS);
2620 #endif // WITH_PRESENCE
2622 * Notify observers that an observed value has changed.
2624 * @param handle - handle of resource
2627 * OC_STACK_OK - no errors
2628 * OC_STACK_NO_RESOURCE - invalid resource handle
2629 * OC_STACK_NO_OBSERVERS - no more observers intrested in resource
2631 OCStackResult OCNotifyAllObservers(OCResourceHandle handle, OCQualityOfService qos) {
2633 OC_LOG(INFO, TAG, PCF("Entering OCNotifyAllObservers"));
2635 OCResource *resPtr = NULL;
2636 OCStackResult result;
2637 OCMethod method = OC_REST_NOMETHOD;
2638 uint32_t maxAge = 0;
2640 OC_LOG(INFO, TAG, PCF("Entering OCNotifyAllObservers"));
2641 #ifdef WITH_PRESENCE
2642 if(handle == presenceResource.handle)
2646 #endif // WITH_PRESENCE
2647 VERIFY_NON_NULL(handle, ERROR, OC_STACK_ERROR);
2649 // Verify that the resource exists
2650 resPtr = findResource ((OCResource *) handle);
2653 return OC_STACK_NO_RESOURCE;
2657 //only increment in the case of regular observing (not presence)
2658 incrementSequenceNumber(resPtr);
2659 method = OC_REST_OBSERVE;
2660 maxAge = MAX_OBSERVE_AGE;
2661 #ifdef WITH_PRESENCE
2662 result = SendAllObserverNotification (method, resPtr, maxAge, NULL, qos);
2664 result = SendAllObserverNotification (method, resPtr, maxAge, qos);
2671 OCNotifyListOfObservers (OCResourceHandle handle,
2672 OCObservationId *obsIdList,
2673 uint8_t numberOfIds,
2674 unsigned char *notificationJSONPayload,
2675 OCQualityOfService qos)
2677 OC_LOG(INFO, TAG, PCF("Entering OCNotifyListOfObservers"));
2679 OCResource *resPtr = NULL;
2680 //TODO: we should allow the server to define this
2681 uint32_t maxAge = MAX_OBSERVE_AGE;
2683 VERIFY_NON_NULL(handle, ERROR, OC_STACK_ERROR);
2684 VERIFY_NON_NULL(obsIdList, ERROR, OC_STACK_ERROR);
2685 VERIFY_NON_NULL(notificationJSONPayload, ERROR, OC_STACK_ERROR);
2687 // Verify that the resource exists
2688 resPtr = findResource ((OCResource *) handle);
2689 if (NULL == resPtr || myStackMode == OC_CLIENT)
2691 return OC_STACK_NO_RESOURCE;
2695 incrementSequenceNumber(resPtr);
2697 return (SendListObserverNotification(resPtr, obsIdList, numberOfIds,
2698 notificationJSONPayload, maxAge, qos));
2702 * Send a response to a request.
2703 * The response can be a regular, slow, or block (i.e. a response that
2704 * is too large to be sent in a single PDU and must span multiple transmissions)
2706 * @param response - pointer to structure that contains response parameters
2709 * OC_STACK_OK - No errors; Success
2710 * OC_STACK_INVALID_PARAM - Invalid pointer to OCServerResponse
2711 * OC_STACK_INVALID_REQUEST_HANDLE - Request handle not found
2712 * OC_STACK_PERSISTENT_BUFFER_REQUIRED - Block transfer needed for response, so a
2713 * persistent response buffer is necessary
2715 OCStackResult OCDoResponse(OCEntityHandlerResponse *ehResponse)
2717 OCStackResult result = OC_STACK_ERROR;
2718 OCServerRequest *serverRequest = NULL;
2720 OC_LOG(INFO, TAG, PCF("Entering OCDoResponse"));
2722 // Validate input parameters
2723 VERIFY_NON_NULL(ehResponse, ERROR, OC_STACK_INVALID_PARAM);
2724 VERIFY_NON_NULL(ehResponse->requestHandle, ERROR, OC_STACK_INVALID_PARAM);
2726 // TODO: Placeholder for creating a response entry when implementing
2727 // block transfer feature
2729 // If a response payload is present, check if block transfer is required
2730 if (ehResponse->payload && OCIsPacketTransferRequired(NULL,
2731 (const char *)ehResponse->payload, ehResponse->payloadSize))
2733 OC_LOG(INFO, TAG, PCF("Block transfer required"));
2735 // Persistent response buffer is needed for block transfer
2736 if (!ehResponse->persistentBufferFlag)
2738 OC_LOG(WARNING, TAG, PCF("Persistent response buffer required"));
2739 return OC_STACK_PERSISTENT_BUFFER_REQUIRED;
2741 // TODO: Placeholder for block transfer handling
2742 // TODO: Placeholder for setting the the response handle in the OCServerResponse struct
2743 // when implementing the block transfer feature
2748 // Get pointer to request info
2749 serverRequest = GetServerRequestUsingHandle((OCServerRequest *)ehResponse->requestHandle);
2752 result = serverRequest->ehResponseHandler(ehResponse);
2759 * Cancel a response. Applies to a block response
2761 * @param responseHandle - response handle set by stack in OCServerResponse after
2762 * OCDoResponse is called
2765 * OC_STACK_OK - No errors; Success
2766 * OC_STACK_INVALID_PARAM - The handle provided is invalid.
2768 OCStackResult OCCancelResponse(OCResponseHandle responseHandle)
2770 OCStackResult result = OC_STACK_NOTIMPL;
2772 OC_LOG(INFO, TAG, PCF("Entering OCCancelResponse"));
2774 // TODO: validate response handle
2779 //-----------------------------------------------------------------------------
2780 // Private internal function definitions
2781 //-----------------------------------------------------------------------------
2783 * Generate handle of OCDoResource invocation for callback management.
2785 static OCDoHandle GenerateInvocationHandle()
2787 OCDoHandle handle = NULL;
2788 // Generate token here, it will be deleted when the transaction is deleted
2789 handle = (OCDoHandle) OCMalloc(sizeof(uint8_t[MAX_TOKEN_LENGTH]));
2792 OCFillRandomMem((uint8_t*)handle, sizeof(uint8_t[MAX_TOKEN_LENGTH]));
2797 #ifdef WITH_PRESENCE
2798 OCStackResult OCChangeResourceProperty(OCResourceProperty * inputProperty,
2799 OCResourceProperty resourceProperties, uint8_t enable)
2801 if (resourceProperties
2802 > (OC_ACTIVE | OC_DISCOVERABLE | OC_OBSERVABLE | OC_SLOW)) {
2803 OC_LOG(ERROR, TAG, PCF("Invalid property"));
2804 return OC_STACK_INVALID_PARAM;
2808 *inputProperty = (OCResourceProperty) (*inputProperty & ~(resourceProperties));
2812 *inputProperty = (OCResourceProperty) (*inputProperty | resourceProperties);
2819 * Initialize resource data structures, variables, etc.
2821 OCStackResult initResources() {
2822 OCStackResult result = OC_STACK_OK;
2823 // Init application resource vars
2824 headResource = NULL;
2825 // Init Virtual Resources
2826 #ifdef WITH_PRESENCE
2827 presenceResource.presenceTTL = OC_DEFAULT_PRESENCE_TTL;
2828 //presenceResource.token = OCGenerateCoAPToken();
2829 result = OCCreateResource(&presenceResource.handle,
2830 OC_RSRVD_RESOURCE_TYPE_PRESENCE,
2835 //make resource inactive
2836 result = OCChangeResourceProperty(
2837 &(((OCResource *) presenceResource.handle)->resourceProperties),
2844 * Add a resource to the end of the linked list of resources.
2846 * @param resource - resource to be added
2848 void insertResource(OCResource *resource) {
2849 OCResource *pointer;
2851 if (!headResource) {
2852 headResource = resource;
2854 pointer = headResource;
2856 while (pointer->next) {
2857 pointer = pointer->next;
2859 pointer->next = resource;
2861 resource->next = NULL;
2865 * Find a resource in the linked list of resources.
2867 * @param resource - resource to be found
2869 * NULL - resource not found
2870 * pointer to resource - pointer to resource that was found in the linked list
2872 OCResource *findResource(OCResource *resource) {
2873 OCResource *pointer = headResource;
2876 if (pointer == resource) {
2879 pointer = pointer->next;
2884 void deleteAllResources()
2886 OCResource *pointer = headResource;
2891 temp = pointer->next;
2892 #ifdef WITH_PRESENCE
2893 if(pointer != (OCResource *) presenceResource.handle)
2895 #endif // WITH_PRESENCE
2896 deleteResource(pointer);
2897 #ifdef WITH_PRESENCE
2899 #endif // WITH_PRESENCE
2903 #ifdef WITH_PRESENCE
2904 // Ensure that the last resource to be deleted is the presence resource. This allows for all
2905 // presence notification attributed to their deletion to be processed.
2906 deleteResource((OCResource *) presenceResource.handle);
2907 #endif // WITH_PRESENCE
2911 * Delete the resource from the linked list.
2913 * @param resource - resource to be deleted
2918 int deleteResource(OCResource *resource) {
2919 OCResource *prev = NULL;
2922 temp = headResource;
2924 if (temp == resource) {
2925 // Invalidate all Resource Properties.
2926 resource->resourceProperties = (OCResourceProperty) 0;
2927 #ifdef WITH_PRESENCE
2928 if(resource != (OCResource *) presenceResource.handle)
2930 #endif // WITH_PRESENCE
2931 OCNotifyAllObservers((OCResourceHandle)resource, OC_HIGH_QOS);
2932 #ifdef WITH_PRESENCE
2935 if(presenceResource.handle)
2937 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2938 if(resource != (OCResource *) presenceResource.handle)
2940 SendPresenceNotification(resource->rsrcType);
2944 SendPresenceNotification(NULL);
2949 if (temp == headResource) {
2950 headResource = temp->next;
2952 prev->next = temp->next;
2955 deleteResourceElements(temp);
2968 * Delete all of the dynamically allocated elements that were created for the resource.
2970 * @param resource - specified resource
2972 void deleteResourceElements(OCResource *resource) {
2978 OCFree(resource->uri);
2980 // Delete resourcetype linked list
2981 deleteResourceType(resource->rsrcType);
2983 // Delete resourceinterface linked list
2984 deleteResourceInterface(resource->rsrcInterface);
2988 * Delete all of the dynamically allocated elements that were created for the resource type.
2990 * @param resourceType - specified resource type
2992 void deleteResourceType(OCResourceType *resourceType) {
2993 OCResourceType *pointer = resourceType;
2994 OCResourceType *next;
2997 next = pointer->next;
2998 OCFree(pointer->resourcetypename);
3005 * Delete all of the dynamically allocated elements that were created for the resource interface.
3007 * @param resourceInterface - specified resource interface
3009 void deleteResourceInterface(OCResourceInterface *resourceInterface) {
3010 OCResourceInterface *pointer = resourceInterface;
3011 OCResourceInterface *next;
3014 next = pointer->next;
3015 OCFree(pointer->name);
3022 * Insert a resource type into a resource's resource type linked list.
3024 * @param resource - resource where resource type is to be inserted
3025 * @param resourceType - resource type to be inserted
3027 void insertResourceType(OCResource *resource, OCResourceType *resourceType) {
3028 OCResourceType *pointer;
3030 if (resource && !resource->rsrcType) {
3031 resource->rsrcType = resourceType;
3035 pointer = resource->rsrcType;
3039 pointer = resourceType;
3041 while (pointer->next) {
3042 pointer = pointer->next;
3044 pointer->next = resourceType;
3046 resourceType->next = NULL;
3050 * Get a resource type at the specified index within a resource.
3052 * @param handle - handle of resource
3053 * @param index - index of resource type
3056 * resourcetype - if found
3059 OCResourceType *findResourceTypeAtIndex(OCResourceHandle handle, uint8_t index) {
3060 OCResource *resource;
3061 OCResourceType *pointer;
3064 // Find the specified resource
3065 resource = findResource((OCResource *) handle);
3070 // Make sure a resource has a resourcetype
3071 if (!resource->rsrcType) {
3075 // Iterate through the list
3076 pointer = resource->rsrcType;
3078 while ((i < index) && pointer) {
3080 pointer = pointer->next;
3086 * Finds a resource type in an OCResourceType link-list.
3088 * @param resourceTypeList - the link-list to be searched through
3089 * @param resourceTypeName - the key to search for
3092 * resourceType that matches the key (ie. resourceTypeName)
3093 * NULL - either an invalid parameter or this function was unable to find the key.
3095 OCResourceType *findResourceType(OCResourceType * resourceTypeList, const char * resourceTypeName)
3097 if(resourceTypeList && resourceTypeName)
3099 OCResourceType * rtPointer = resourceTypeList;
3100 while(resourceTypeName && rtPointer)
3102 if(rtPointer->resourcetypename &&
3103 strcmp(resourceTypeName, (const char *)
3104 (rtPointer->resourcetypename)) == 0)
3108 rtPointer = rtPointer->next;
3115 * Insert a resource interface into a resource's resource interface linked list.
3117 * @param resource - resource where resource interface is to be inserted
3118 * @param resourceInterface - resource interface to be inserted
3120 void insertResourceInterface(OCResource *resource,
3121 OCResourceInterface *resourceInterface) {
3122 OCResourceInterface *pointer;
3124 if (!resource->rsrcInterface) {
3125 resource->rsrcInterface = resourceInterface;
3127 pointer = resource->rsrcInterface;
3128 while (pointer->next) {
3129 pointer = pointer->next;
3131 pointer->next = resourceInterface;
3133 resourceInterface->next = NULL;
3137 * Get a resource interface at the specified index within a resource.
3139 * @param handle - handle of resource
3140 * @param index - index of resource interface
3143 * resourceinterface - if found
3146 OCResourceInterface *findResourceInterfaceAtIndex(OCResourceHandle handle,
3148 OCResource *resource;
3149 OCResourceInterface *pointer;
3152 // Find the specified resource
3153 resource = findResource((OCResource *) handle);
3158 // Make sure a resource has a resourceinterface
3159 if (!resource->rsrcInterface) {
3163 // Iterate through the list
3164 pointer = resource->rsrcInterface;
3166 while ((i < index) && pointer) {
3168 pointer = pointer->next;
3174 * Determine if a request/response must be sent in a block transfer because it is too large to be
3175 * sent in a single PDU. This function can be used for either a request or a response
3177 * @param request - NULL or pointer to request
3178 * @param response - NULL or pointer to response
3179 * @param size - 0 or size of the request/response. If 0, strlen is used for determining
3180 * the length of the request/response
3183 * 0 - packet transfer NOT required (i.e. normal request/response)
3184 * 1 - packet transfer required (i.e. block transfer needed)
3186 uint8_t OCIsPacketTransferRequired(const char *request, const char *response, uint16_t size)
3190 // Determine if we are checking a request or a response
3193 // If size is greater than 0, use it for the request size value, otherwise
3194 // assume request is null terminated and use strlen for size value
3195 if ((size > MAX_REQUEST_LENGTH) || (strlen(request) > MAX_REQUEST_LENGTH))
3202 // If size is greater than 0, use it for the response size value, otherwise
3203 // assume response is null terminated and use strlen for size value
3204 if ((size > MAX_RESPONSE_LENGTH) || (strlen(response) > MAX_RESPONSE_LENGTH))
3213 * Retrieves a resource type based upon a query ontains only just one
3214 * resource attribute (and that has to be of type "rt").
3216 * @remark This API malloc's memory for the resource type. Do not malloc resourceType
3217 * before passing in.
3219 * @param query - The quert part of the URI
3220 * @param resourceType - The resource type to be populated; pass by reference.
3223 * OC_STACK_INVALID_PARAM - Returns this if the resourceType parameter is invalid/NULL.
3224 * OC_STACK_OK - Success
3226 OCStackResult getResourceType(const char * query, unsigned char** resourceType)
3230 return OC_STACK_INVALID_PARAM;
3233 OCStackResult result = OC_STACK_ERROR;
3235 if(strncmp(query, "rt=", 3) == 0)
3237 *resourceType = (unsigned char *) OCMalloc(strlen(query)-3);
3240 result = OC_STACK_NO_MEMORY;
3243 strcpy((char *)*resourceType, ((const char *)&query[3]));
3244 result = OC_STACK_OK;
3250 OCStackResult getQueryFromUri(const char * uri, unsigned char** query, char ** newURI)
3254 return OC_STACK_INVALID_URI;
3256 if(!query || !newURI)
3258 return OC_STACK_INVALID_PARAM;
3260 char * leftToken = NULL;
3261 char * tempURI = (char *) OCMalloc(strlen(uri) + 1);
3266 strcpy(tempURI, uri);
3268 leftToken = strtok_r((char *)tempURI, "?", &strTokPtr);
3270 //TODO-CA: This could be simplified. Clean up required.
3271 while(leftToken != NULL)
3273 if(strncmp(leftToken, "rt=", 3) == 0 || strncmp(leftToken, "if=", 3) == 0)
3275 *query = (unsigned char *) OCMalloc(strlen(leftToken));
3280 strcpy((char *)*query, ((const char *)&leftToken[0]));
3283 leftToken = strtok_r(NULL, "?", &strTokPtr);
3291 return OC_STACK_NO_MEMORY;
3294 const ServerID OCGetServerInstanceID(void)
3296 static bool generated = false;
3297 static ServerID sid;
3304 sid = OCGetRandom();
3309 const char* OCGetServerInstanceIDString(void)
3311 // max printed length of a base 10
3312 // uint32 is 10 characters, so 11 includes null.
3313 // This will change as the representation gets switched
3315 static char buffer[11];
3316 int n = sprintf(buffer, "%u", OCGetServerInstanceID());