1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
26 #include "ocstackinternal.h"
27 #include "ocresourcehandler.h"
28 #include "occlientcb.h"
29 #include "ocobserve.h"
34 #include "ocserverrequest.h"
38 #include "cainterface.h"
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 OC_STACK_UNINITIALIZED = 0, OC_STACK_INITIALIZED
50 OC_PRESENCE_UNINITIALIZED = 0, OC_PRESENCE_INITIALIZED
54 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
57 static OCStackState stackState = OC_STACK_UNINITIALIZED;
59 OCResource *headResource = NULL;
61 static OCPresenceState presenceState = OC_PRESENCE_UNINITIALIZED;
62 static PresenceResource presenceResource;
63 uint8_t PresenceTimeOutSize = 0;
64 uint32_t PresenceTimeOut[] = {50, 75, 85, 95, 100};
68 OCDeviceEntityHandler defaultDeviceHandler;
70 //-----------------------------------------------------------------------------
72 //-----------------------------------------------------------------------------
73 #define TAG PCF("OCStack")
74 #define VERIFY_SUCCESS(op, successCode) { if (op != successCode) \
75 {OC_LOG_V(FATAL, TAG, "%s failed!!", #op); goto exit;} }
76 #define VERIFY_NON_NULL(arg, logLevel, retVal) { if (!(arg)) { OC_LOG((logLevel), \
77 TAG, PCF(#arg " is NULL")); return (retVal); } }
78 #define VERIFY_NON_NULL_V(arg) { if (!arg) {OC_LOG_V(FATAL, TAG, "%s is NULL", #arg);\
81 //TODO: we should allow the server to define this
82 #define MAX_OBSERVE_AGE (0x2FFFFUL)
84 //-----------------------------------------------------------------------------
86 //-----------------------------------------------------------------------------
87 extern void DeinitOCSecurityInfo();
89 //-----------------------------------------------------------------------------
90 // Internal API function
91 //-----------------------------------------------------------------------------
93 // This internal function is called to update the stack with the status of
94 // observers and communication failures
95 OCStackResult OCStackFeedBack(OCCoAPToken * token, uint8_t status)
97 OCStackResult result = OC_STACK_ERROR;
98 ResourceObserver * observer = NULL;
99 OCEntityHandlerRequest ehRequest = {0};
103 case OC_OBSERVER_NOT_INTERESTED:
104 OC_LOG(DEBUG, TAG, PCF("observer is not interested in our notifications anymore"));
106 observer = GetObserverUsingToken (token->token);
108 observer = GetObserverUsingToken (token);
112 result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) NULL,
113 OC_REST_NOMETHOD, (OCResourceHandle) NULL, NULL, NULL, 0,
114 NULL, OC_OBSERVE_DEREGISTER, observer->observeId);
115 if(result != OC_STACK_OK)
119 observer->resource->entityHandler(OC_OBSERVE_FLAG, &ehRequest);
121 //observer is not observing anymore
123 result = DeleteObserverUsingToken (token->token);
125 result = DeleteObserverUsingToken (token);
127 if(result == OC_STACK_OK)
129 OC_LOG(DEBUG, TAG, PCF("Removed observer successfully"));
133 result = OC_STACK_OK;
134 OC_LOG(DEBUG, TAG, PCF("Observer Removal failed"));
137 case OC_OBSERVER_STILL_INTERESTED:
138 //observer is still interested
139 OC_LOG(DEBUG, TAG, PCF("observer is interested in our \
140 notifications, reset the failedCount"));
142 observer = GetObserverUsingToken (token->token);
144 observer = GetObserverUsingToken (token);
148 observer->forceHighQos = 0;
149 observer->failedCommCount = 0;
150 result = OC_STACK_OK;
154 result = OC_STACK_OBSERVER_NOT_FOUND;
157 case OC_OBSERVER_FAILED_COMM:
158 //observer is not reachable
159 OC_LOG(DEBUG, TAG, PCF("observer is unreachable"));
161 observer = GetObserverUsingToken (token->token);
163 observer = GetObserverUsingToken (token);
167 if(observer->failedCommCount >= MAX_OBSERVER_FAILED_COMM)
169 result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) NULL,
170 OC_REST_NOMETHOD, (OCResourceHandle) NULL, NULL, NULL, 0,
171 NULL, OC_OBSERVE_DEREGISTER, observer->observeId);
172 if(result != OC_STACK_OK)
174 return OC_STACK_ERROR;
176 observer->resource->entityHandler(OC_OBSERVE_FLAG, &ehRequest);
177 //observer is unreachable
178 result = DeleteObserverUsingToken (token);
179 if(result == OC_STACK_OK)
181 OC_LOG(DEBUG, TAG, PCF("Removed observer successfully"));
185 result = OC_STACK_OK;
186 OC_LOG(DEBUG, TAG, PCF("Observer Removal failed"));
191 observer->failedCommCount++;
192 result = OC_STACK_CONTINUE;
194 observer->forceHighQos = 1;
195 OC_LOG_V(DEBUG, TAG, "Failed count for this observer is %d",observer->failedCommCount);
199 OC_LOG(ERROR, TAG, PCF("Unknown status"));
200 result = OC_STACK_ERROR;
207 OCStackResult CAToOCStackResult(CAResponseResult_t caCode)
209 OCStackResult ret = OC_STACK_ERROR;
217 ret = OC_STACK_RESOURCE_CREATED;
220 ret = OC_STACK_RESOURCE_DELETED;
223 ret = OC_STACK_INVALID_QUERY;
226 ret = OC_STACK_INVALID_OPTION;
229 ret = OC_STACK_NO_RESOURCE;
237 //This function will be called back by CA layer when a response is received
238 void HandleCAResponses(const CARemoteEndpoint_t* endPoint, const CAResponseInfo_t* responseInfo)
240 OC_LOG(INFO, TAG, PCF("Enter HandleCAResponses"));
241 printf ("Received payload: %s\n", (char *)responseInfo->info.payload);
242 OCStackApplicationResult result = OC_STACK_DELETE_TRANSACTION;
243 ClientCB *cbNode = GetClientCB((CAToken_t *)&responseInfo->info.token, NULL, NULL);
246 OC_LOG(INFO, TAG, PCF("Calling into application address space"));
247 OCClientResponse response;
248 struct sockaddr_in sa;
250 inet_pton(AF_INET, endPoint->addressInfo.IP.ipAddress, &(sa.sin_addr));
251 sa.sin_port = htons(endPoint->addressInfo.IP.port);
252 static OCDevAddr address;
253 memcpy((void*)&address.addr, &(sa), sizeof(sa));
254 response.addr = &address;
255 response.result = CAToOCStackResult(responseInfo->result);
256 response.resJSONPayload = (unsigned char*)responseInfo->info.payload;
257 response.numRcvdVendorSpecificHeaderOptions = 0;
258 if(responseInfo->info.numOptions > 0)
260 //First option alwas with option ID COAP_OPTION_OBSERVE if it is availbale
261 if(responseInfo->info.options[0].optionID == COAP_OPTION_OBSERVE)
263 memcpy (&(response.sequenceNumber),
264 &(responseInfo->info.options[0].optionData), 4);
268 memcpy (&(response.rcvdVendorSpecificHeaderOptions[0]),
269 &(responseInfo->info.options[0]), sizeof(OCHeaderOption));
271 for (uint8_t i = 1; i < responseInfo->info.numOptions; i++)
273 memcpy (&(response.rcvdVendorSpecificHeaderOptions[i]),
274 &(responseInfo->info.options[i]), sizeof(OCHeaderOption));
276 response.numRcvdVendorSpecificHeaderOptions = responseInfo->info.numOptions;
278 result = cbNode->callBack(cbNode->context,
279 cbNode->handle, &response);
280 if (result == OC_STACK_DELETE_TRANSACTION)
282 FindAndDeleteClientCB(cbNode);
285 OC_LOG_V(INFO, TAG, PCF("Received payload: %s\n"), (char*)responseInfo->info.payload);
286 OC_LOG(INFO, TAG, PCF("Exit HandleCAResponses"));
289 //This function will be called back by CA layer when a request is received
290 void HandleCARequests(const CARemoteEndpoint_t* endPoint, const CARequestInfo_t* requestInfo)
292 CAInfo_t responseData;
293 CAResponseInfo_t responseInfo;
294 OCStackResult requestResult = OC_STACK_ERROR;
296 OC_LOG(INFO, TAG, PCF("Enter HandleCARequests"));
299 if(myStackMode == OC_CLIENT)
301 //TODO: should the client be responding to requests?
305 OCServerProtocolRequest serverRequest;
307 memset (&serverRequest, 0, sizeof(OCServerProtocolRequest));
308 // copy URI of resource
309 memcpy (&(serverRequest.resourceUrl), endPoint->resourceUri, strlen(endPoint->resourceUri));
311 // TODO-CA: Is the query part of header option?
312 //copy request payload
313 if (requestInfo->info.payload)
315 serverRequest.reqTotalSize = strlen(requestInfo->info.payload) + 1;
316 memcpy (&(serverRequest.reqJSONPayload), requestInfo->info.payload,
317 strlen(requestInfo->info.payload));
318 serverRequest.reqTotalSize = strlen((const char *)requestInfo->info.payload) + 1;
322 serverRequest.reqTotalSize = 1;
325 switch (requestInfo->method)
329 serverRequest.method = OC_REST_GET;
334 serverRequest.method = OC_REST_PUT;
339 serverRequest.method = OC_REST_POST;
344 serverRequest.method = OC_REST_DELETE;
349 OC_LOG(ERROR, TAG, PCF("Received CA method %d not supported"));
355 OC_LOG_V(INFO, TAG, "HandleCARequests: CA token length = %d", strlen(requestInfo->info.token));
356 OC_LOG_BUFFER(INFO, TAG, requestInfo->info.token, strlen(requestInfo->info.token));
357 // TODO-CA: For CA integration currently copying CAToken to OCCoapToken:
358 // Need to remove OCCoapToken
359 memcpy (&(serverRequest.requestToken.token), requestInfo->info.token,
360 strlen(requestInfo->info.token));
361 serverRequest.requestToken.tokenLength = strlen(requestInfo->info.token);
363 if (requestInfo->info.type == CA_MSG_CONFIRM)
365 serverRequest.qos = OC_HIGH_QOS;
367 else if (requestInfo->info.type == CA_MSG_NONCONFIRM)
369 serverRequest.qos = OC_LOW_QOS;
371 else if (requestInfo->info.type == CA_MSG_ACKNOWLEDGE)
373 // TODO-CA: Need to handle this
375 else if (requestInfo->info.type == CA_MSG_RESET)
377 // TODO-CA: Need to handle this
379 // CA does not need the following 3 fields
380 serverRequest.coapID = 0;
381 serverRequest.delayedResNeeded = 0;
382 serverRequest.secured = endPoint->isSecured;
385 serverRequest.addressInfo = endPoint->addressInfo;
386 serverRequest.connectivityType = endPoint->connectivityType;
387 if (requestInfo->info.token)
389 strncpy(serverRequest.token, requestInfo->info.token, sizeof(serverRequest.token) - 1);
392 // copy vendor specific header options
393 // TODO-CA: CA is including non-vendor header options as well, like observe.
394 // Need to filter those out
395 GetObserveHeaderOption(&serverRequest.observationOption, requestInfo->info.options, &(requestInfo->info.numOptions));
396 if (requestInfo->info.numOptions > MAX_HEADER_OPTIONS)
398 // TODO-CA: Need to send an error indicating the num of options is incorrect
401 serverRequest.numRcvdVendorSpecificHeaderOptions = requestInfo->info.numOptions;
402 if (serverRequest.numRcvdVendorSpecificHeaderOptions)
404 memcpy (&(serverRequest.rcvdVendorSpecificHeaderOptions), requestInfo->info.options,
405 sizeof(CAHeaderOption_t)*requestInfo->info.numOptions);
408 requestResult = HandleStackRequests (&serverRequest);
411 OC_LOG(INFO, TAG, PCF("Exit HandleCARequests"));
416 //This function will be called back by occoap layer when a request is received
417 OCStackResult HandleStackRequests(OCServerProtocolRequest * protocolRequest)
419 OC_LOG(INFO, TAG, PCF("Entering HandleStackRequests (OCStack Layer)"));
421 OCStackResult result = OC_STACK_ERROR;
422 ResourceHandling resHandling;
423 OCResource *resource;
425 OCServerRequest * request = GetServerRequestUsingToken(protocolRequest->requestToken);
428 OC_LOG(INFO, TAG, PCF("This is a new Server Request"));
430 result = AddServerCARequest(&request, protocolRequest->coapID,
431 protocolRequest->delayedResNeeded, protocolRequest->secured, 0,
432 protocolRequest->method, protocolRequest->numRcvdVendorSpecificHeaderOptions,
433 protocolRequest->observationOption, protocolRequest->qos,
434 protocolRequest->query, protocolRequest->rcvdVendorSpecificHeaderOptions,
435 protocolRequest->reqJSONPayload, &protocolRequest->requestToken,
436 &protocolRequest->requesterAddr, protocolRequest->resourceUrl,
437 protocolRequest->reqTotalSize,
438 &protocolRequest->addressInfo, protocolRequest->connectivityType, protocolRequest->token);
440 result = AddServerRequest(&request, protocolRequest->coapID,
441 protocolRequest->delayedResNeeded, protocolRequest->secured, 0,
442 protocolRequest->method, protocolRequest->numRcvdVendorSpecificHeaderOptions,
443 protocolRequest->observationOption, protocolRequest->qos,
444 protocolRequest->query, protocolRequest->rcvdVendorSpecificHeaderOptions,
445 protocolRequest->reqJSONPayload, &protocolRequest->requestToken,
446 &protocolRequest->requesterAddr, protocolRequest->resourceUrl,
447 protocolRequest->reqTotalSize);
449 if (OC_STACK_OK != result)
451 OC_LOG(ERROR, TAG, PCF("Error adding server request"));
454 VERIFY_NON_NULL(request, ERROR, OC_STACK_NO_MEMORY);
456 if(!protocolRequest->reqMorePacket)
458 request->requestComplete = 1;
463 OC_LOG(INFO, TAG, PCF("This is either a repeated Server Request or blocked Server Request"));
466 if(request->requestComplete)
468 OC_LOG(INFO, TAG, PCF("This Server Request is complete"));
469 result = DetermineResourceHandling (request, &resHandling, &resource);
470 if (result == OC_STACK_OK)
472 result = ProcessRequest(resHandling, resource, request);
476 result = OC_STACK_ERROR;
481 OC_LOG(INFO, TAG, PCF("This Server Request is incomplete"));
482 result = OC_STACK_CONTINUE;
487 //This function will be called back by occoap layer when a response is received
488 OCStackResult HandleStackResponses(OCResponse * response)
490 OC_LOG(INFO, TAG, PCF("Entering HandleStackResponses (OCStack Layer)"));
491 OCStackResult result = OC_STACK_OK;
492 OCStackApplicationResult cbResult = OC_STACK_DELETE_TRANSACTION;
493 uint8_t isObserveNotification = 0;
494 ClientCB * cbNode = NULL;
496 uint8_t isPresenceNotification = 0;
497 uint8_t isMulticastPresence = 0;
498 char * resourceTypeName = NULL;
499 uint32_t lowerBound = 0;
500 uint32_t higherBound = 0;
502 unsigned char * bufRes = response->bufRes;
503 #endif // WITH_PRESENCE
505 cbNode = response->cbNode;
508 cbNode = GetClientCB(response->rcvdToken, NULL, NULL);
511 if(response->clientResponse->sequenceNumber >= OC_OFFSET_SEQUENCE_NUMBER)
513 isObserveNotification = 1;
514 OC_LOG(INFO, TAG, PCF("Received an observe notification"));
517 OC_LOG_V(DEBUG, TAG, "The sequenceNumber/NONCE of this response %u",
518 response->clientResponse->sequenceNumber);
519 OC_LOG_V(DEBUG, TAG, "The maxAge/TTL of this response %u", response->maxAge);
520 OC_LOG_V(DEBUG, TAG, "The response received is %s", bufRes);
523 if(!strcmp((char *)response->rcvdUri, (char *)OC_PRESENCE_URI)){
524 isPresenceNotification = 1;
527 result = OC_STACK_INVALID_PARAM;
530 tok = strtok((char *)bufRes, "[:]}");
531 bufRes[strlen((char *)bufRes)] = ':';
532 tok = strtok(NULL, "[:]}");
533 bufRes[strlen((char *)bufRes)] = ':';
534 response->clientResponse->sequenceNumber = (uint32_t )atoi(tok);
535 OC_LOG_V(DEBUG, TAG, "The received NONCE is %u", response->clientResponse->sequenceNumber);
536 tok = strtok(NULL, "[:]}");
537 response->maxAge = (uint32_t )atoi(tok);
538 OC_LOG_V(DEBUG, TAG, "The received TTL is %u", response->maxAge);
539 tok = strtok(NULL, "[:]}");
542 resourceTypeName = (char *)OCMalloc(strlen(tok));
543 if(!resourceTypeName)
547 bufRes[strlen((char *)bufRes)] = ':';
548 strcpy(resourceTypeName, tok);
549 OC_LOG_V(DEBUG, TAG, "----------------resourceTypeName %s",
552 bufRes[strlen((char *)bufRes)] = ']';
555 // Check if the application subcribed for presence
558 cbNode = GetClientCB(NULL, NULL, response->fullUri);
561 // Check if application subscribed for multicast presence
564 snprintf((char *)response->fullUri, MAX_URI_LENGTH, "%s%s",
565 OC_MULTICAST_IP, response->rcvdUri);
566 cbNode = GetClientCB(NULL, NULL, response->fullUri);
569 isMulticastPresence = 1;
570 isPresenceNotification = 0;
574 if(cbNode && isPresenceNotification)
576 OC_LOG(INFO, TAG, PCF("Received a presence notification"));
577 if(!cbNode->presence)
579 cbNode->presence = (OCPresence *) OCMalloc(sizeof(OCPresence));
580 VERIFY_NON_NULL_V(cbNode->presence);
581 cbNode->presence->timeOut = NULL;
582 cbNode->presence->timeOut = (uint32_t *)
583 OCMalloc(PresenceTimeOutSize * sizeof(uint32_t));
584 if(!(cbNode->presence->timeOut)){
585 OCFree(cbNode->presence);
586 result = OC_STACK_NO_MEMORY;
589 if(response->maxAge == 0)
591 OC_LOG(INFO, TAG, PCF("===============Stopping presence"));
592 response->clientResponse->result = OC_STACK_PRESENCE_STOPPED;
595 OCFree(cbNode->presence->timeOut);
596 OCFree(cbNode->presence);
597 cbNode->presence = NULL;
602 OC_LOG_V(INFO, TAG, "===============Update presence TTL, now time is %d", GetTime(0));
603 cbNode->presence->TTL = response->maxAge;
604 for(int index = 0; index < PresenceTimeOutSize; index++)
606 lowerBound = GetTime(((float)(PresenceTimeOut[index])
607 /(float)100)*(float)cbNode->presence->TTL);
608 higherBound = GetTime(((float)(PresenceTimeOut[index + 1])
609 /(float)100)*(float)cbNode->presence->TTL);
610 cbNode->presence->timeOut[index] = OCGetRandomRange(lowerBound, higherBound);
611 OC_LOG_V(DEBUG, TAG, "----------------lowerBound timeout %d", lowerBound);
612 OC_LOG_V(DEBUG, TAG, "----------------higherBound timeout %d", higherBound);
613 OC_LOG_V(DEBUG, TAG, "----------------timeOut entry %d",
614 cbNode->presence->timeOut[index]);
616 cbNode->presence->TTLlevel = 0;
617 OC_LOG_V(DEBUG, TAG, "----------------this TTL level %d", cbNode->presence->TTLlevel);
618 if(cbNode->sequenceNumber == response->clientResponse->sequenceNumber)
620 OC_LOG(INFO, TAG, PCF("===============No presence change"));
623 OC_LOG(INFO, TAG, PCF("===============Presence changed, calling up the stack"));
624 cbNode->sequenceNumber = response->clientResponse->sequenceNumber;;
627 // Ensure that a filter is actually applied.
628 if(resourceTypeName && cbNode->filterResourceType)
630 if(!findResourceType(cbNode->filterResourceType, resourceTypeName))
636 else if(cbNode && isMulticastPresence)
638 // Check if the same nonce for a given host
639 OCMulticastNode* mcNode = NULL;
640 mcNode = GetMCPresenceNode(response->fullUri);
642 if(response->maxAge == 0)
644 OC_LOG(INFO, TAG, PCF("===============Stopping presence"));
645 response->clientResponse->result = OC_STACK_PRESENCE_STOPPED;
648 OCFree(cbNode->presence->timeOut);
649 OCFree(cbNode->presence);
650 cbNode->presence = NULL;
653 else if(mcNode != NULL)
655 if(mcNode->nonce == response->clientResponse->sequenceNumber)
657 OC_LOG(INFO, TAG, PCF("===============No presence change (Multicast)"));
658 result = OC_STACK_NO_MEMORY;
661 mcNode->nonce = response->clientResponse->sequenceNumber;
665 uint32_t uriLen = strlen((char*)response->fullUri);
666 unsigned char* uri = (unsigned char *) OCMalloc(uriLen + 1);
669 memcpy(uri, response->fullUri, (uriLen + 1));
674 PCF("===============No Memory for URI to store in the presence node"));
675 result = OC_STACK_NO_MEMORY;
678 result = AddMCPresenceNode(&mcNode, (unsigned char*) uri,
679 response->clientResponse->sequenceNumber);
680 if(result == OC_STACK_NO_MEMORY)
683 PCF("===============No Memory for Multicast Presence Node"));
684 result = OC_STACK_NO_MEMORY;
689 // Ensure that a filter is actually applied.
690 if(resourceTypeName && cbNode->filterResourceType)
692 if(!findResourceType(cbNode->filterResourceType, resourceTypeName))
699 else if(!cbNode && isPresenceNotification)
701 OC_LOG(INFO, TAG, PCF("Received a presence notification, but I do not have callback \
702 ------------ ignoring"));
704 #endif // WITH_PRESENCE
708 if(isObserveNotification)
710 OC_LOG(INFO, TAG, PCF("Received an observe notification"));
711 //TODO: check the standard for methods to detect wrap around condition
712 if(cbNode->method == OC_REST_OBSERVE &&
713 (response->clientResponse->sequenceNumber <= cbNode->sequenceNumber ||
714 (response->clientResponse->sequenceNumber > cbNode->sequenceNumber &&
715 response->clientResponse->sequenceNumber ==
716 MAX_SEQUENCE_NUMBER)))
718 OC_LOG_V(DEBUG, TAG, "Observe notification came out of order. \
719 Ignoring Incoming:%d Against Current:%d.",
720 response->clientResponse->sequenceNumber, cbNode->sequenceNumber);
723 if(response->clientResponse->sequenceNumber > cbNode->sequenceNumber){
724 cbNode->sequenceNumber = response->clientResponse->sequenceNumber;
728 response->clientResponse->resJSONPayload = bufRes;
730 cbResult = cbNode->callBack(cbNode->context, cbNode->handle, response->clientResponse);
732 if (cbResult == OC_STACK_DELETE_TRANSACTION ||
733 response->clientResponse->result == OC_STACK_COMM_ERROR ||
734 (response->clientResponse->result == OC_STACK_RESOURCE_DELETED &&
735 !isPresenceNotification && !isMulticastPresence))
737 FindAndDeleteClientCB(cbNode);
742 result = OC_STACK_ERROR;
747 OCFree(resourceTypeName);
752 int ParseIPv4Address(unsigned char * ipAddrStr, uint8_t * ipAddr, uint16_t * port)
755 unsigned char *itr, *coap;
756 uint8_t dotCount = 0;
760 /* search for scheme */
762 if (!isdigit((unsigned char) *ipAddrStr))
764 coap = (unsigned char *) OC_COAP_SCHEME;
765 while (*coap && tolower(*itr) == *coap)
774 if (isdigit((unsigned char) *ipAddrStr))
777 ipAddr[index] += *ipAddrStr - '0';
779 else if ((unsigned char) *ipAddrStr == '.')
791 if(*ipAddrStr == ':')
795 if (isdigit((unsigned char) *ipAddrStr))
798 *port += *ipAddrStr - '0';
809 if (ipAddr[0] < 255 && ipAddr[1] < 255 && ipAddr[2] < 255 && ipAddr[3] < 255
820 //-----------------------------------------------------------------------------
821 // Private internal function prototypes
822 //-----------------------------------------------------------------------------
824 static OCDoHandle GenerateInvocationHandle();
825 static OCStackResult initResources();
826 static void insertResource(OCResource *resource);
827 static OCResource *findResource(OCResource *resource);
828 static void insertResourceType(OCResource *resource,
829 OCResourceType *resourceType);
830 static OCResourceType *findResourceTypeAtIndex(OCResourceHandle handle,
832 static void insertResourceInterface(OCResource *resource,
833 OCResourceInterface *resourceInterface);
834 static OCResourceInterface *findResourceInterfaceAtIndex(
835 OCResourceHandle handle, uint8_t index);
836 static void deleteResourceType(OCResourceType *resourceType);
837 static void deleteResourceInterface(OCResourceInterface *resourceInterface);
838 static void deleteResourceElements(OCResource *resource);
839 static int deleteResource(OCResource *resource);
840 static void deleteAllResources();
841 static void incrementSequenceNumber(OCResource * resPtr);
842 static OCStackResult verifyUriQueryLength(const char * inputUri,
844 static uint8_t OCIsPacketTransferRequired(const char *request, const char *response, uint16_t size);
845 OCStackResult getResourceType(const char * uri, unsigned char** resourceType, char ** newURI);
847 //-----------------------------------------------------------------------------
849 //-----------------------------------------------------------------------------
852 * Initialize the OC Stack. Must be called prior to starting the stack.
855 * IP Address of host device
857 * Port of host device
859 * Host device is client, server, or client-server
862 * OC_STACK_OK - no errors
863 * OC_STACK_ERROR - stack init error
865 OCStackResult OCInit(const char *ipAddr, uint16_t port, OCMode mode)
867 OCStackResult result = OC_STACK_ERROR;
868 OC_LOG(INFO, TAG, PCF("Entering OCInit"));
872 OC_LOG_V(INFO, TAG, "IP Address = %s", ipAddr);
876 //It is ok to select network to CA_WIFI for now
877 CAResult_t caResult = CASelectNetwork(CA_WIFI);
878 if(caResult == CA_STATUS_OK)
880 OC_LOG(INFO, TAG, PCF("CASelectNetwork to WIFI"));
881 caResult = CARegisterHandler(HandleCARequests, HandleCAResponses);
882 if(caResult == CA_STATUS_OK)
884 OC_LOG(INFO, TAG, PCF("CARegisterHandler..."));
885 stackState = OC_STACK_INITIALIZED;
886 result = OC_STACK_OK;
890 caResult = CAStartDiscoveryServer();
891 OC_LOG(INFO, TAG, PCF("Client mode: CAStartDiscoveryServer"));
894 caResult = CAStartListeningServer();
895 OC_LOG(INFO, TAG, PCF("Server mode: CAStartListeningServer"));
897 case OC_CLIENT_SERVER:
898 caResult = CAStartListeningServer();
899 if(caResult == CA_STATUS_OK)
901 caResult = CAStartDiscoveryServer();
903 OC_LOG(INFO, TAG, PCF("Client-server mode"));
906 OC_LOG(ERROR, TAG, PCF("Invalid mode"));
907 return OC_STACK_ERROR;
912 if (caResult == CA_STATUS_OK)
914 result = OC_STACK_OK;
918 result = OC_STACK_ERROR;
925 OC_LOG(INFO, TAG, PCF("Client mode"));
928 OC_LOG(INFO, TAG, PCF("Server mode"));
930 case OC_CLIENT_SERVER:
931 OC_LOG(INFO, TAG, PCF("Client-server mode"));
934 OC_LOG(ERROR, TAG, PCF("Invalid mode"));
935 return OC_STACK_ERROR;
939 // Make call to OCCoAP layer
940 result = OCInitCoAP(ipAddr, (uint16_t) port, myStackMode);
944 defaultDeviceHandler = NULL;
947 PresenceTimeOutSize = sizeof(PresenceTimeOut)/sizeof(PresenceTimeOut[0]) - 1;
948 #endif // WITH_PRESENCE
950 if (result == OC_STACK_OK)
952 stackState = OC_STACK_INITIALIZED;
954 // Initialize resource
955 if(result == OC_STACK_OK && myStackMode != OC_CLIENT)
957 result = initResources();
959 if(result != OC_STACK_OK)
961 OC_LOG(ERROR, TAG, PCF("Stack initialization error"));
967 * Stop the OC stack. Use for a controlled shutdown.
969 * OC_STACK_OK - no errors
970 * OC_STACK_ERROR - stack not initialized
972 OCStackResult OCStop()
974 OCStackResult result = OC_STACK_ERROR;
976 OC_LOG(INFO, TAG, PCF("Entering OCStop"));
978 if (stackState != OC_STACK_INITIALIZED)
980 OC_LOG(ERROR, TAG, PCF("Stack not initialized"));
981 return OC_STACK_ERROR;
985 // Ensure that the TTL associated with ANY and ALL presence notifications originating from
986 // here send with the code "OC_STACK_PRESENCE_STOPPED" result.
987 presenceResource.presenceTTL = 0;
988 #endif // WITH_PRESENCE
990 // Free memory dynamically allocated for resources
991 deleteAllResources();
993 // Make call to OCCoAP layer
994 if (OCStopCoAP() == OC_STACK_OK)
996 // Remove all observers
997 DeleteObserverList();
998 // Remove all the client callbacks
999 DeleteClientCBList();
1000 stackState = OC_STACK_UNINITIALIZED;
1001 result = OC_STACK_OK;
1003 result = OC_STACK_ERROR;
1006 // Deinit security blob
1007 DeinitOCSecurityInfo();
1009 if (result != OC_STACK_OK) {
1010 OC_LOG(ERROR, TAG, PCF("Stack stop error"));
1017 * Verify the lengths of the URI and the query separately
1019 * @param inputUri - Input URI and query.
1020 * @param uriLen - The length of the initial URI with query.
1022 * Note: The '?' that appears after the URI is not considered as
1023 * a part of the query.
1025 OCStackResult verifyUriQueryLength(const char *inputUri, uint16_t uriLen)
1029 query = strchr (inputUri, '?');
1033 if((query - inputUri) > MAX_URI_LENGTH)
1035 return OC_STACK_INVALID_URI;
1038 if((inputUri + uriLen - 1 - query) > MAX_QUERY_LENGTH)
1040 return OC_STACK_INVALID_QUERY;
1043 else if(uriLen > MAX_URI_LENGTH)
1045 return OC_STACK_INVALID_URI;
1051 * Discover or Perform requests on a specified resource (specified by that Resource's respective URI).
1053 * @param handle - @ref OCDoHandle to refer to the request sent out on behalf of calling this API.
1054 * @param method - @ref OCMethod to perform on the resource
1055 * @param requiredUri - URI of the resource to interact with
1056 * @param referenceUri - URI of the reference resource
1057 * @param request - JSON encoded request
1058 * @param qos - quality of service
1059 * @param cbData - struct that contains asynchronous callback function that is invoked
1060 * by the stack when discovery or resource interaction is complete
1061 * @param options - The address of an array containing the vendor specific header
1062 * header options to be sent with the request
1063 * @param numOptions - Number of vendor specific header options to be included
1066 * OC_STACK_OK - no errors
1067 * OC_STACK_INVALID_CALLBACK - invalid callback function pointer
1068 * OC_STACK_INVALID_METHOD - invalid resource method
1069 * OC_STACK_INVALID_URI - invalid required or reference URI
1072 OCStackResult OCDoResource(OCDoHandle *handle, OCMethod method, const char *requiredUri,
1073 const char *referenceUri, const char *request,
1074 OCQualityOfService qos, OCCallbackData *cbData,
1075 OCHeaderOption * options, uint8_t numOptions)
1077 OCStackResult result = OC_STACK_ERROR;
1079 ClientCB *clientCB = NULL;
1080 unsigned char * requestUri = NULL;
1081 unsigned char * resourceType = NULL;
1082 char * newUri = (char *)requiredUri;
1083 (void) referenceUri;
1085 CARemoteEndpoint_t* endpoint = NULL;
1086 CAResult_t caResult;
1087 CAToken_t caToken = NULL;
1088 CAInfo_t requestData;
1089 CARequestInfo_t requestInfo;
1090 // To track if memory is allocated for additional header options
1091 uint8_t hdrOptionMemAlloc = 0;
1094 OC_LOG(INFO, TAG, PCF("Entering OCDoResource"));
1096 // Validate input parameters
1097 VERIFY_NON_NULL(cbData, FATAL, OC_STACK_INVALID_CALLBACK);
1098 VERIFY_NON_NULL(cbData->cb, FATAL, OC_STACK_INVALID_CALLBACK);
1100 TODO ("Need to form the final query by concatenating require and reference URI's");
1101 VERIFY_NON_NULL(requiredUri, FATAL, OC_STACK_INVALID_URI);
1103 uint16_t uriLen = strlen(requiredUri);
1105 // ToDo: We should also check if the requiredUri has a mutlicast address, then qos has to be OC_Low_QOS
1111 case OC_REST_DELETE:
1112 case OC_REST_OBSERVE:
1113 case OC_REST_OBSERVE_ALL:
1114 case OC_REST_CANCEL_OBSERVE:
1116 #ifdef WITH_PRESENCE
1117 case OC_REST_PRESENCE:
1121 result = OC_STACK_INVALID_METHOD;
1125 if((result = verifyUriQueryLength(requiredUri, uriLen)) != OC_STACK_OK)
1130 if((request) && (strlen(request) > MAX_REQUEST_LENGTH))
1132 result = OC_STACK_INVALID_PARAM;
1136 #ifdef WITH_PRESENCE
1137 if(method == OC_REST_PRESENCE)
1139 result = getResourceType(requiredUri, &resourceType, &newUri);
1141 OC_LOG_V(DEBUG, TAG, "Got Resource Type: %s", resourceType);
1145 OC_LOG(DEBUG, TAG, PCF("Got Resource Type is NULL."));
1147 if(result != OC_STACK_OK)
1152 #endif // WITH_PRESENCE
1154 requestUri = (unsigned char *) OCMalloc(uriLen + 1);
1157 memcpy(requestUri, newUri, (uriLen + 1));
1161 result = OC_STACK_NO_MEMORY;
1165 *handle = GenerateInvocationHandle();
1168 result = OC_STACK_NO_MEMORY;
1173 memset(&requestData, 0, sizeof(CAInfo_t));
1174 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
1178 case OC_REST_OBSERVE:
1179 case OC_REST_OBSERVE_ALL:
1180 case OC_REST_CANCEL_OBSERVE:
1182 requestInfo.method = CA_GET;
1187 requestInfo.method = CA_PUT;
1192 requestInfo.method = CA_POST;
1195 case OC_REST_DELETE:
1197 requestInfo.method = CA_DELETE;
1200 #ifdef WITH_PRESENCE
1201 case OC_REST_PRESENCE:
1202 //TODO-CA: What should be the CA method?
1206 result = OC_STACK_INVALID_METHOD;
1210 // TODO-CA: Handle multi-cast scenario
1211 // Create remote end point
1212 caResult = CACreateRemoteEndpoint(newUri, CA_WIFI, &endpoint);
1213 // TODO-CA: Connectivity type should be passed to API
1214 endpoint->connectivityType = CA_WIFI;
1215 if (caResult != CA_STATUS_OK)
1217 OC_LOG(ERROR, TAG, PCF("CACreateRemoteEndpoint error"));
1222 caResult = CAGenerateToken(&caToken);
1224 if (caResult != CA_STATUS_OK)
1226 OC_LOG(ERROR, TAG, PCF("CAGenerateToken error"));
1231 // TODO-CA: Map QoS to the right CA msg type
1232 requestData.type = CA_MSG_NONCONFIRM;
1233 requestData.token = caToken;
1234 if ((method == OC_REST_OBSERVE) || (method == OC_REST_OBSERVE_ALL))
1236 result = CreateObserveHeaderOption (&(requestData.options), options,
1237 numOptions, OC_OBSERVE_REGISTER);
1238 if (result != OC_STACK_OK)
1242 hdrOptionMemAlloc = 1;
1243 requestData.numOptions = numOptions + 1;
1247 requestData.options = (CAHeaderOption_t*)options;
1248 requestData.numOptions = numOptions;
1250 requestData.payload = (char *)request;
1252 requestInfo.info = requestData;
1255 caResult = CASendRequest(endpoint, &requestInfo);
1256 if (caResult != CA_STATUS_OK)
1258 OC_LOG(ERROR, TAG, PCF("CASendRequest"));
1262 if((result = AddClientCB(&clientCB, cbData, &caToken, handle, method,
1263 requestUri, resourceType)) != OC_STACK_OK)
1265 result = OC_STACK_NO_MEMORY;
1271 // Generate token which will be used by OCStack to match responses received
1273 OCGenerateCoAPToken(&token);
1275 if((result = AddClientCB(&clientCB, cbData, &token, handle, method, requestUri, resourceType))
1278 result = OC_STACK_NO_MEMORY;
1282 #ifdef WITH_PRESENCE
1283 if(method == OC_REST_PRESENCE)
1285 // Replacing method type with GET because "presence" is a stack layer only implementation.
1286 method = OC_REST_GET;
1290 // Make call to OCCoAP layer
1291 result = OCDoCoAPResource(method, qos, &token, newUri, request, options, numOptions);
1295 if(newUri != requiredUri)
1299 if (result != OC_STACK_OK)
1301 OC_LOG(ERROR, TAG, PCF("OCDoResource error"));
1302 FindAndDeleteClientCB(clientCB);
1305 CADestroyRemoteEndpoint(endpoint);
1306 if (hdrOptionMemAlloc)
1308 OCFree(requestData.options);
1315 * Cancel a request associated with a specific @ref OCDoResource invocation.
1317 * @param handle - Used to identify a specific OCDoResource invocation.
1318 * @param qos - used to specify Quality of Service (read below for more info)
1319 * @param options- used to specify vendor specific header options when sending
1320 * explicit observe cancellation
1321 * @param numOptions- Number of header options to be included
1324 * OC_STACK_OK - No errors; Success
1325 * OC_STACK_INVALID_PARAM - The handle provided is invalid.
1327 OCStackResult OCCancel(OCDoHandle handle, OCQualityOfService qos, OCHeaderOption * options,
1331 * This ftn is implemented one of two ways in the case of observation:
1333 * 1. qos == OC_NON_CONFIRMABLE. When observe is unobserved..
1334 * Remove the callback associated on client side.
1335 * When the next notification comes in from server,
1336 * reply with RESET message to server.
1337 * Keep in mind that the server will react to RESET only
1338 * if the last notification was sent ans CON
1340 * 2. qos == OC_CONFIRMABLE. When OCCancel is called,
1341 * and it is associated with an observe request
1342 * (i.e. ClientCB->method == OC_REST_OBSERVE || OC_REST_OBSERVE_ALL),
1343 * Send CON Observe request to server with
1344 * observe flag = OC_RESOURCE_OBSERVE_DEREGISTER.
1345 * Remove the callback associated on client side.
1347 OCStackResult ret = OC_STACK_OK;
1349 CARemoteEndpoint_t* endpoint = NULL;
1350 CAResult_t caResult;
1351 CAInfo_t requestData;
1352 CARequestInfo_t requestInfo;
1353 // Track if memory is allocated for additional header options
1354 uint8_t hdrOptionMemAlloc = 0;
1358 return OC_STACK_INVALID_PARAM;
1361 OC_LOG(INFO, TAG, PCF("Entering OCCancel"));
1363 ClientCB *clientCB = GetClientCB(NULL, handle, NULL);
1366 switch (clientCB->method)
1368 case OC_REST_OBSERVE:
1369 case OC_REST_OBSERVE_ALL:
1371 caResult = CACreateRemoteEndpoint((char *)clientCB->requestUri, CA_WIFI,
1373 endpoint->connectivityType = CA_WIFI;
1374 if (caResult != CA_STATUS_OK)
1376 OC_LOG(ERROR, TAG, PCF("CACreateRemoteEndpoint error"));
1377 return OC_STACK_ERROR;
1380 memset(&requestData, 0, sizeof(CAInfo_t));
1381 // TODO-CA: Map QoS to the right CA msg type
1382 requestData.type = CA_MSG_NONCONFIRM;
1383 requestData.token = clientCB->token;
1384 if (CreateObserveHeaderOption (&(requestData.options),
1385 options, numOptions, OC_OBSERVE_DEREGISTER) != OC_STACK_OK)
1387 return OC_STACK_ERROR;
1389 hdrOptionMemAlloc = 1;
1390 requestData.numOptions = numOptions + 1;
1391 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
1392 requestInfo.method = CA_GET;
1393 requestInfo.info = requestData;
1395 caResult = CASendRequest(endpoint, &requestInfo);
1396 if (caResult != CA_STATUS_OK)
1398 OC_LOG(ERROR, TAG, PCF("CASendRequest error"));
1400 if(caResult == CA_STATUS_OK)
1405 if(qos == OC_HIGH_QOS)
1407 ret = OCDoCoAPResource(OC_REST_CANCEL_OBSERVE, qos,
1408 &(clientCB->token), (const char *) clientCB->requestUri, NULL, options,
1413 FindAndDeleteClientCB(clientCB);
1417 #ifdef WITH_PRESENCE
1418 case OC_REST_PRESENCE:
1419 FindAndDeleteClientCB(clientCB);
1423 return OC_STACK_INVALID_METHOD;
1427 CADestroyRemoteEndpoint(endpoint);
1428 if (hdrOptionMemAlloc)
1430 OCFree(requestData.options);
1436 #ifdef WITH_PRESENCE
1437 OCStackResult OCProcessPresence()
1439 OCStackResult result = OC_STACK_OK;
1440 uint8_t ipAddr[4] = { 0 };
1443 OC_LOG(INFO, TAG, PCF("Entering RequestPresence"));
1444 ClientCB* cbNode = NULL;
1446 OCClientResponse clientResponse;
1447 OCResponse * response = NULL;
1449 LL_FOREACH(cbList, cbNode) {
1450 if(OC_REST_PRESENCE == cbNode->method)
1452 if(cbNode->presence)
1454 uint32_t now = GetTime(0);
1455 OC_LOG_V(DEBUG, TAG, "----------------this TTL level %d", cbNode->presence->TTLlevel);
1456 OC_LOG_V(DEBUG, TAG, "----------------current ticks %d", now);
1459 if(cbNode->presence->TTLlevel >= (PresenceTimeOutSize + 1))
1464 if(cbNode->presence->TTLlevel < PresenceTimeOutSize){
1465 OC_LOG_V(DEBUG, TAG, "----------------timeout ticks %d",
1466 cbNode->presence->timeOut[cbNode->presence->TTLlevel]);
1469 if(cbNode->presence->TTLlevel >= PresenceTimeOutSize)
1471 OC_LOG(DEBUG, TAG, PCF("----------------No more timeout ticks"));
1472 if (ParseIPv4Address( cbNode->requestUri, ipAddr, &port))
1474 OCBuildIPv4Address(ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3], port,
1476 result = FormOCClientResponse(&clientResponse, OC_STACK_PRESENCE_TIMEOUT,
1477 (OCDevAddr *) &dst, 0, NULL);
1478 if(result != OC_STACK_OK)
1482 result = FormOCResponse(&response, cbNode, 0, NULL, NULL,
1483 &cbNode->token, &clientResponse, NULL);
1484 if(result != OC_STACK_OK)
1489 // Increment the TTLLevel (going to a next state), so we don't keep
1490 // sending presence notification to client.
1491 cbNode->presence->TTLlevel++;
1492 OC_LOG_V(DEBUG, TAG, "----------------moving to TTL level %d",
1493 cbNode->presence->TTLlevel);
1497 result = OC_STACK_INVALID_IP;
1500 HandleStackResponses(response);
1502 if(now >= cbNode->presence->timeOut[cbNode->presence->TTLlevel])
1504 OC_LOG(DEBUG, TAG, PCF("time to test server presence =========="));
1506 OCGenerateCoAPToken(&token);
1507 result = OCDoCoAPResource(OC_REST_GET, OC_LOW_QOS,
1508 &token, (const char *)cbNode->requestUri, NULL, NULL, 0);
1509 if(result != OC_STACK_OK)
1513 cbNode->presence->TTLlevel++;
1514 OC_LOG_V(DEBUG, TAG, "----------------moving to TTL level %d", cbNode->presence->TTLlevel);
1520 if (result != OC_STACK_OK)
1522 OC_LOG(ERROR, TAG, PCF("OCProcessPresence error"));
1529 * Called in main loop of OC client or server. Allows low-level processing of
1533 * OC_STACK_OK - no errors
1534 * OC_STACK_ERROR - stack process error
1536 OCStackResult OCProcess() {
1538 OC_LOG(INFO, TAG, PCF("Entering OCProcess"));
1539 #ifdef WITH_PRESENCE
1540 OCProcessPresence();
1543 CAHandleRequestResponse();
1551 #ifdef WITH_PRESENCE
1553 * When operating in @ref OCServer or @ref OCClientServer mode, this API will start sending out
1554 * presence notifications to clients via multicast. Once this API has been called with a success,
1555 * clients may query for this server's presence and this server's stack will respond via multicast.
1557 * Server can call this function when it comes online for the first time, or when it comes back
1558 * online from offline mode, or when it re enters network.
1560 * @param ttl - Time To Live in seconds
1561 * Note: If ttl is '0', then the default stack value will be used (60 Seconds).
1564 * OC_STACK_OK - No errors; Success
1566 OCStackResult OCStartPresence(const uint32_t ttl)
1568 OCChangeResourceProperty(
1569 &(((OCResource *)presenceResource.handle)->resourceProperties),
1574 presenceResource.presenceTTL = ttl;
1577 if(OC_PRESENCE_UNINITIALIZED == presenceState)
1579 OCDevAddr multiCastAddr;
1582 presenceState = OC_PRESENCE_INITIALIZED;
1583 OCGenerateCoAPToken(&token);
1584 OCBuildIPv4Address(224, 0, 1, 187, 5683, &multiCastAddr);
1585 //add the presence observer
1586 AddObserver(OC_PRESENCE_URI, NULL, 0, &token, &multiCastAddr,
1587 (OCResource *)presenceResource.handle, OC_LOW_QOS);
1590 // Each time OCStartPresence is called
1591 // a different random 32-bit integer number is used
1592 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
1594 return SendPresenceNotification(NULL);
1598 * When operating in @ref OCServer or @ref OCClientServer mode, this API will stop sending out
1599 * presence notifications to clients via multicast. Once this API has been called with a success,
1600 * this server's stack will not respond to clients querying for this server's presence.
1602 * Server can call this function when it is terminating, going offline, or when going
1603 * away from network.
1606 * OC_STACK_OK - No errors; Success
1608 OCStackResult OCStopPresence()
1610 OCStackResult result = OC_STACK_ERROR;
1611 //make resource inactive
1612 result = OCChangeResourceProperty(
1613 &(((OCResource *) presenceResource.handle)->resourceProperties),
1615 result = SendPresenceNotification(NULL);
1622 OCStackResult OCSetDefaultDeviceEntityHandler(OCDeviceEntityHandler entityHandler)
1624 defaultDeviceHandler = entityHandler;
1632 * @param handle - pointer to handle to newly created resource. Set by ocstack. Used to refer to resource
1633 * @param resourceTypeName - name of resource type. Example: "core.led"
1634 * @param resourceInterfaceName - name of resource interface. Example: "core.rw"
1635 * @param uri - URI of the resource. Example: "/a/led"
1636 * @param entityHandler - entity handler function that is called by ocstack to handle requests, etc
1637 * NULL for default entity handler
1638 * @param resourceProperties - properties supported by resource. Example: OC_DISCOVERABLE|OC_OBSERVABLE
1641 * OC_STACK_OK - no errors
1642 * OC_STACK_ERROR - stack process error
1644 OCStackResult OCCreateResource(OCResourceHandle *handle,
1645 const char *resourceTypeName,
1646 const char *resourceInterfaceName,
1647 const char *uri, OCEntityHandler entityHandler,
1648 uint8_t resourceProperties) {
1650 OCResource *pointer = NULL;
1653 OCStackResult result = OC_STACK_ERROR;
1655 OC_LOG(INFO, TAG, PCF("Entering OCCreateResource"));
1657 if(myStackMode == OC_CLIENT)
1661 // Validate parameters
1662 // Is it presented during resource discovery?
1663 if (!handle || !resourceTypeName || !uri) {
1664 OC_LOG(ERROR, TAG, PCF("Input parameter is NULL"));
1665 return OC_STACK_INVALID_PARAM;
1668 if(!resourceInterfaceName || strlen(resourceInterfaceName) == 0) {
1669 resourceInterfaceName = OC_RSRVD_INTERFACE_DEFAULT;
1672 // Make sure resourceProperties bitmask has allowed properties specified
1673 if (resourceProperties
1674 > (OC_ACTIVE | OC_DISCOVERABLE | OC_OBSERVABLE | OC_SLOW | OC_SECURE)) {
1675 OC_LOG(ERROR, TAG, PCF("Invalid property"));
1676 return OC_STACK_INVALID_PARAM;
1679 // If the headResource is NULL, then no resources have been created...
1680 pointer = headResource;
1682 // At least one resources is in the resource list, so we need to search for
1683 // repeated URLs, which are not allowed. If a repeat is found, exit with an error
1685 if (strcmp(uri, pointer->uri) == 0) {
1686 OC_LOG(ERROR, TAG, PCF("URI already in use"));
1687 return OC_STACK_INVALID_PARAM;
1689 pointer = pointer->next;
1692 // Create the pointer and insert it into the resource list
1693 pointer = (OCResource *) OCCalloc(1, sizeof(OCResource));
1697 pointer->sequenceNum = OC_OFFSET_SEQUENCE_NUMBER;
1699 insertResource(pointer);
1702 size = strlen(uri) + 1;
1703 str = (char *) OCMalloc(size);
1707 strncpy(str, uri, size);
1710 // Set properties. Set OC_ACTIVE
1711 pointer->resourceProperties = (OCResourceProperty) (resourceProperties
1714 // Add the resourcetype to the resource
1715 result = BindResourceTypeToResource(pointer, resourceTypeName);
1716 if (result != OC_STACK_OK) {
1717 OC_LOG(ERROR, TAG, PCF("Error adding resourcetype"));
1721 // Add the resourceinterface to the resource
1722 result = BindResourceInterfaceToResource(pointer, resourceInterfaceName);
1723 if (result != OC_STACK_OK) {
1724 OC_LOG(ERROR, TAG, PCF("Error adding resourceinterface"));
1728 // If an entity handler has been passed, attach it to the newly created
1729 // resource. Otherwise, set the default entity handler.
1732 pointer->entityHandler = entityHandler;
1736 pointer->entityHandler = defaultResourceEHandler;
1740 result = OC_STACK_OK;
1742 #ifdef WITH_PRESENCE
1743 if(presenceResource.handle)
1745 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
1746 SendPresenceNotification(pointer->rsrcType);
1750 if (result != OC_STACK_OK)
1752 // Deep delete of resource and other dynamic elements that it contains
1753 deleteResource(pointer);
1762 * Create a resource. with host ip address for remote resource
1764 * @param handle - pointer to handle to newly created resource. Set by ocstack.
1765 * Used to refer to resource
1766 * @param resourceTypeName - name of resource type. Example: "core.led"
1767 * @param resourceInterfaceName - name of resource interface. Example: "core.rw"
1768 * @param host - HOST address of the remote resource. Example: "coap://xxx.xxx.xxx.xxx:xxxxx"
1769 * @param uri - URI of the resource. Example: "/a/led"
1770 * @param entityHandler - entity handler function that is called by ocstack to handle requests, etc
1771 * NULL for default entity handler
1772 * @param resourceProperties - properties supported by resource.
1773 * Example: OC_DISCOVERABLE|OC_OBSERVABLE
1776 * OC_STACK_OK - no errors
1777 * OC_STACK_ERROR - stack process error
1780 OCStackResult OCCreateResourceWithHost(OCResourceHandle *handle,
1781 const char *resourceTypeName,
1782 const char *resourceInterfaceName,
1785 OCEntityHandler entityHandler,
1786 uint8_t resourceProperties)
1790 OCStackResult result = OC_STACK_ERROR;
1792 result = OCCreateResource(handle, resourceTypeName, resourceInterfaceName,
1793 uri, entityHandler, resourceProperties);
1795 if (result != OC_STACK_ERROR)
1798 size = strlen(host) + 1;
1799 str = (char *) OCMalloc(size);
1802 return OC_STACK_ERROR;
1804 strncpy(str, host, size);
1805 ((OCResource *) *handle)->host = str;
1812 * Add a resource to a collection resource.
1814 * @param collectionHandle - handle to the collection resource
1815 * @param resourceHandle - handle to resource to be added to the collection resource
1818 * OC_STACK_OK - no errors
1819 * OC_STACK_ERROR - stack process error
1820 * OC_STACK_INVALID_PARAM - invalid collectionhandle
1822 OCStackResult OCBindResource(
1823 OCResourceHandle collectionHandle, OCResourceHandle resourceHandle) {
1824 OCResource *resource;
1827 OC_LOG(INFO, TAG, PCF("Entering OCBindResource"));
1829 // Validate parameters
1830 VERIFY_NON_NULL(collectionHandle, ERROR, OC_STACK_ERROR);
1831 VERIFY_NON_NULL(resourceHandle, ERROR, OC_STACK_ERROR);
1832 // Container cannot contain itself
1833 if (collectionHandle == resourceHandle) {
1834 OC_LOG(ERROR, TAG, PCF("Added handle equals collection handle"));
1835 return OC_STACK_INVALID_PARAM;
1838 // Use the handle to find the resource in the resource linked list
1839 resource = findResource((OCResource *) collectionHandle);
1841 OC_LOG(ERROR, TAG, PCF("Collection handle not found"));
1842 return OC_STACK_INVALID_PARAM;
1845 // Look for an open slot to add add the child resource.
1846 // If found, add it and return success
1847 for (i = 0; i < MAX_CONTAINED_RESOURCES; i++) {
1848 if (!resource->rsrcResources[i]) {
1849 resource->rsrcResources[i] = (OCResource *) resourceHandle;
1850 OC_LOG(INFO, TAG, PCF("resource bound"));
1855 #ifdef WITH_PRESENCE
1856 if(presenceResource.handle)
1858 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
1859 SendPresenceNotification(((OCResource *) resourceHandle)->rsrcType);
1863 // Unable to add resourceHandle, so return error
1864 return OC_STACK_ERROR;
1868 * Remove a resource from a collection resource.
1870 * @param collectionHandle - handle to the collection resource
1871 * @param resourceHandle - handle to resource to be added to the collection resource
1874 * OC_STACK_OK - no errors
1875 * OC_STACK_ERROR - stack process error
1876 * OC_STACK_INVALID_PARAM - invalid collectionHandle
1878 OCStackResult OCUnBindResource(
1879 OCResourceHandle collectionHandle, OCResourceHandle resourceHandle) {
1880 OCResource *resource;
1883 OC_LOG(INFO, TAG, PCF("Entering OCUnBindResource"));
1885 // Validate parameters
1886 VERIFY_NON_NULL(collectionHandle, ERROR, OC_STACK_ERROR);
1887 VERIFY_NON_NULL(resourceHandle, ERROR, OC_STACK_ERROR);
1888 // Container cannot contain itself
1889 if (collectionHandle == resourceHandle) {
1890 OC_LOG(ERROR, TAG, PCF("removing handle equals collection handle"));
1891 return OC_STACK_INVALID_PARAM;
1894 // Use the handle to find the resource in the resource linked list
1895 resource = findResource((OCResource *) collectionHandle);
1897 OC_LOG(ERROR, TAG, PCF("Collection handle not found"));
1898 return OC_STACK_INVALID_PARAM;
1901 // Look for an open slot to add add the child resource.
1902 // If found, add it and return success
1903 for (i = 0; i < MAX_CONTAINED_RESOURCES; i++) {
1904 if (resourceHandle == resource->rsrcResources[i]) {
1905 resource->rsrcResources[i] = (OCResource *) NULL;
1906 OC_LOG(INFO, TAG, PCF("resource unbound"));
1911 OC_LOG(INFO, TAG, PCF("resource not found in collection"));
1913 #ifdef WITH_PRESENCE
1914 if(presenceResource.handle)
1916 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
1917 SendPresenceNotification(((OCResource *) resourceHandle)->rsrcType);
1921 // Unable to add resourceHandle, so return error
1922 return OC_STACK_ERROR;
1925 OCStackResult BindResourceTypeToResource(OCResource* resource,
1926 const char *resourceTypeName)
1928 OCResourceType *pointer = NULL;
1931 OCStackResult result = OC_STACK_ERROR;
1933 OC_LOG(INFO, TAG, PCF("Entering BindResourceTypeToResource"));
1935 // Validate parameters
1936 VERIFY_NON_NULL(resourceTypeName, ERROR, OC_STACK_INVALID_PARAM);
1937 // TODO: Does resource attribute resentation really have to be maintained in stack?
1938 // Is it presented during resource discovery?
1940 TODO ("Make sure that the resourcetypename doesn't already exist in the resource");
1942 // Create the resourcetype and insert it into the resource list
1943 pointer = (OCResourceType *) OCCalloc(1, sizeof(OCResourceType));
1948 // Set the resourceTypeName
1949 size = strlen(resourceTypeName) + 1;
1950 str = (char *) OCMalloc(size);
1954 strncpy(str, resourceTypeName, size);
1955 pointer->resourcetypename = str;
1957 insertResourceType(resource, pointer);
1958 result = OC_STACK_OK;
1960 exit: if (result != OC_STACK_OK) {
1968 OCStackResult BindResourceInterfaceToResource(OCResource* resource,
1969 const char *resourceInterfaceName)
1971 OCResourceInterface *pointer = NULL;
1974 OCStackResult result = OC_STACK_ERROR;
1976 OC_LOG(INFO, TAG, PCF("Entering BindResourceInterfaceToResource"));
1978 // Validate parameters
1979 VERIFY_NON_NULL(resourceInterfaceName, ERROR, OC_STACK_INVALID_PARAM);
1981 TODO ("Make sure that the resourceinterface name doesn't already exist in the resource");
1983 // Create the resourceinterface and insert it into the resource list
1984 pointer = (OCResourceInterface *) OCCalloc(1, sizeof(OCResourceInterface));
1989 // Set the resourceinterface name
1990 size = strlen(resourceInterfaceName) + 1;
1991 str = (char *) OCMalloc(size);
1995 strncpy(str, resourceInterfaceName, size);
1996 pointer->name = str;
1998 // Bind the resourceinterface to the resource
1999 insertResourceInterface(resource, pointer);
2001 result = OC_STACK_OK;
2003 exit: if (result != OC_STACK_OK) {
2012 * Bind a resourcetype to a resource.
2014 * @param handle - handle to the resource
2015 * @param resourceTypeName - name of resource type. Example: "core.led"
2018 * OC_STACK_OK - no errors
2019 * OC_STACK_ERROR - stack process error
2021 OCStackResult OCBindResourceTypeToResource(OCResourceHandle handle,
2022 const char *resourceTypeName) {
2024 OCStackResult result = OC_STACK_ERROR;
2025 OCResource *resource;
2027 // Make sure resource exists
2028 resource = findResource((OCResource *) handle);
2030 OC_LOG(ERROR, TAG, PCF("Resource not found"));
2031 return OC_STACK_ERROR;
2034 // call internal function
2035 result = BindResourceTypeToResource(resource, resourceTypeName);
2037 #ifdef WITH_PRESENCE
2038 if(presenceResource.handle)
2040 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2041 SendPresenceNotification(resource->rsrcType);
2049 * Bind a resourceinterface to a resource.
2051 * @param handle - handle to the resource
2052 * @param resourceInterfaceName - name of resource interface. Example: "oc.mi.b"
2055 * OC_STACK_OK - no errors
2056 * OC_STACK_ERROR - stack process error
2059 OCStackResult OCBindResourceInterfaceToResource(OCResourceHandle handle,
2060 const char *resourceInterfaceName) {
2062 OCStackResult result = OC_STACK_ERROR;
2063 OCResource *resource;
2065 // Make sure resource exists
2066 resource = findResource((OCResource *) handle);
2068 OC_LOG(ERROR, TAG, PCF("Resource not found"));
2069 return OC_STACK_ERROR;
2072 // call internal function
2073 result = BindResourceInterfaceToResource(resource, resourceInterfaceName);
2075 #ifdef WITH_PRESENCE
2076 if(presenceResource.handle)
2078 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2079 SendPresenceNotification(resource->rsrcType);
2087 * Get the number of resources that have been created in the stack.
2089 * @param numResources - pointer to count variable
2092 * OC_STACK_OK - no errors
2093 * OC_STACK_ERROR - stack process error
2096 OCStackResult OCGetNumberOfResources(uint8_t *numResources) {
2097 OCResource *pointer = headResource;
2099 OC_LOG(INFO, TAG, PCF("Entering OCGetNumberOfResources"));
2100 VERIFY_NON_NULL(numResources, ERROR, OC_STACK_INVALID_PARAM);
2103 *numResources = *numResources + 1;
2104 pointer = pointer->next;
2110 * Get a resource handle by index.
2112 * @param index - index of resource, 0 to Count - 1
2115 * Resource handle - if found
2116 * NULL - if not found
2118 OCResourceHandle OCGetResourceHandle(uint8_t index) {
2119 OCResource *pointer = headResource;
2122 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceHandle"));
2124 // Iterate through the list
2125 while ((i < index) && pointer) {
2127 pointer = pointer->next;
2129 return (OCResourceHandle) pointer;
2133 * Delete resource specified by handle. Deletes resource and all resourcetype and resourceinterface
2136 * @param handle - handle of resource to be deleted
2139 * OC_STACK_OK - no errors
2140 * OC_STACK_ERROR - stack process error
2141 * OC_STACK_NO_RESOURCE - resource not found
2142 * OC_STACK_INVALID_PARAM - invalid param
2144 OCStackResult OCDeleteResource(OCResourceHandle handle) {
2145 OC_LOG(INFO, TAG, PCF("Entering OCDeleteResource"));
2148 OC_LOG(ERROR, TAG, PCF("Invalid param"));
2149 return OC_STACK_INVALID_PARAM;
2152 OCResource *resource = findResource((OCResource *) handle);
2153 if (resource == NULL) {
2154 OC_LOG(ERROR, TAG, PCF("Resource not found"));
2155 return OC_STACK_NO_RESOURCE;
2158 if (deleteResource((OCResource *) handle) == 0) {
2159 OC_LOG(ERROR, TAG, PCF("Error deleting resource"));
2160 return OC_STACK_ERROR;
2167 * Get the URI of the resource specified by handle.
2169 * @param handle - handle of resource
2171 * URI string - if resource found
2172 * NULL - resource not found
2174 const char *OCGetResourceUri(OCResourceHandle handle) {
2175 OCResource *resource;
2176 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceUri"));
2178 resource = findResource((OCResource *) handle);
2180 return resource->uri;
2182 return (const char *) NULL;
2186 * Get the properties of the resource specified by handle.
2187 * NOTE: that after a resource is created, the OC_ACTIVE property is set
2188 * for the resource by the stack.
2190 * @param handle - handle of resource
2192 * property bitmap - if resource found
2193 * NULL - resource not found
2195 uint8_t OCGetResourceProperties(OCResourceHandle handle) {
2196 OCResource *resource;
2197 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceProperties"));
2199 resource = findResource((OCResource *) handle);
2201 return resource->resourceProperties;
2207 * Get the number of resource types of the resource.
2209 * @param handle - handle of resource
2210 * @param numResourceTypes - pointer to count variable
2213 * OC_STACK_OK - no errors
2214 * OC_STACK_ERROR - stack process error
2216 OCStackResult OCGetNumberOfResourceTypes(OCResourceHandle handle,
2217 uint8_t *numResourceTypes) {
2218 OCResource *resource;
2219 OCResourceType *pointer;
2221 OC_LOG(INFO, TAG, PCF("Entering OCGetNumberOfResourceTypes"));
2222 VERIFY_NON_NULL(numResourceTypes, ERROR, OC_STACK_INVALID_PARAM);
2223 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
2225 *numResourceTypes = 0;
2227 resource = findResource((OCResource *) handle);
2229 pointer = resource->rsrcType;
2231 *numResourceTypes = *numResourceTypes + 1;
2232 pointer = pointer->next;
2239 * Get name of resource type of the resource.
2241 * @param handle - handle of resource
2242 * @param index - index of resource, 0 to Count - 1
2245 * resource type name - if resource found
2246 * NULL - resource not found
2248 const char *OCGetResourceTypeName(OCResourceHandle handle, uint8_t index) {
2249 OCResourceType *resourceType;
2251 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceTypeName"));
2253 resourceType = findResourceTypeAtIndex(handle, index);
2255 return resourceType->resourcetypename;
2257 return (const char *) NULL;
2263 * Get the number of resource interfaces of the resource.
2265 * @param handle - handle of resource
2266 * @param numResources - pointer to count variable
2269 * OC_STACK_OK - no errors
2270 * OC_STACK_ERROR - stack process error
2272 OCStackResult OCGetNumberOfResourceInterfaces(OCResourceHandle handle,
2273 uint8_t *numResourceInterfaces) {
2274 OCResourceInterface *pointer;
2275 OCResource *resource;
2277 OC_LOG(INFO, TAG, PCF("Entering OCGetNumberOfResourceInterfaces"));
2279 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
2280 VERIFY_NON_NULL(numResourceInterfaces, ERROR, OC_STACK_INVALID_PARAM);
2282 *numResourceInterfaces = 0;
2283 resource = findResource((OCResource *) handle);
2285 pointer = resource->rsrcInterface;
2287 *numResourceInterfaces = *numResourceInterfaces + 1;
2288 pointer = pointer->next;
2295 * Get name of resource interface of the resource.
2297 * @param handle - handle of resource
2298 * @param index - index of resource, 0 to Count - 1
2301 * resource interface name - if resource found
2302 * NULL - resource not found
2304 const char *OCGetResourceInterfaceName(OCResourceHandle handle, uint8_t index) {
2305 OCResourceInterface *resourceInterface;
2307 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceInterfaceName"));
2309 resourceInterface = findResourceInterfaceAtIndex(handle, index);
2310 if (resourceInterface) {
2311 return resourceInterface->name;
2313 return (const char *) NULL;
2317 * Get resource handle from the collection resource by index.
2319 * @param collectionHandle - handle of collection resource
2320 * @param index - index of contained resource, 0 to Count - 1
2323 * handle to resource - if resource found
2324 * NULL - resource not found
2326 OCResourceHandle OCGetResourceHandleFromCollection(OCResourceHandle collectionHandle,
2328 OCResource *resource;
2330 OC_LOG(INFO, TAG, PCF("Entering OCGetContainedResource"));
2332 if (index >= MAX_CONTAINED_RESOURCES) {
2336 resource = findResource((OCResource *) collectionHandle);
2341 return resource->rsrcResources[index];
2345 * Bind an entity handler to the resource.
2347 * @param handle - handle to the resource that the contained resource is to be bound
2348 * @param entityHandler - entity handler function that is called by ocstack to handle requests, etc
2350 * OC_STACK_OK - no errors
2351 * OC_STACK_ERROR - stack process error
2353 OCStackResult OCBindResourceHandler(OCResourceHandle handle,
2354 OCEntityHandler entityHandler) {
2355 OCResource *resource;
2357 OC_LOG(INFO, TAG, PCF("Entering OCBindResourceHandler"));
2359 // Validate parameters
2360 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
2361 //VERIFY_NON_NULL(entityHandler, ERROR, OC_STACK_INVALID_PARAM);
2363 // Use the handle to find the resource in the resource linked list
2364 resource = findResource((OCResource *)handle);
2366 OC_LOG(ERROR, TAG, PCF("Resource not found"));
2367 return OC_STACK_ERROR;
2371 resource->entityHandler = entityHandler;
2373 #ifdef WITH_PRESENCE
2374 if(presenceResource.handle)
2376 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2377 SendPresenceNotification(resource->rsrcType);
2385 * Get the entity handler for a resource.
2387 * @param handle - handle of resource
2390 * entity handler - if resource found
2391 * NULL - resource not found
2393 OCEntityHandler OCGetResourceHandler(OCResourceHandle handle) {
2394 OCResource *resource;
2396 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceHandler"));
2398 // Use the handle to find the resource in the resource linked list
2399 resource = findResource((OCResource *)handle);
2401 OC_LOG(ERROR, TAG, PCF("Resource not found"));
2406 return resource->entityHandler;
2409 void incrementSequenceNumber(OCResource * resPtr)
2411 // Increment the sequence number
2412 resPtr->sequenceNum += 1;
2413 if (resPtr->sequenceNum == MAX_SEQUENCE_NUMBER)
2415 resPtr->sequenceNum = OC_OFFSET_SEQUENCE_NUMBER+1;
2421 * Notify Presence subscribers that a resource has been modified
2423 * @param resourceType - Handle to the resourceType linked list of resource
2424 * that was modified.
2425 * @param qos - Quality Of Service
2428 #ifdef WITH_PRESENCE
2429 OCStackResult SendPresenceNotification(OCResourceType *resourceType)
2431 OCResource *resPtr = NULL;
2432 OCStackResult result;
2433 OCMethod method = OC_REST_PRESENCE;
2434 uint32_t maxAge = 0;
2435 resPtr = findResource((OCResource *) presenceResource.handle);
2438 return OC_STACK_NO_RESOURCE;
2440 if((((OCResource *) presenceResource.handle)->resourceProperties) & OC_ACTIVE)
2442 maxAge = presenceResource.presenceTTL;
2449 result = SendAllObserverNotification(method, resPtr, maxAge, resourceType, OC_LOW_QOS);
2452 #endif // WITH_PRESENCE
2454 * Notify observers that an observed value has changed.
2456 * @param handle - handle of resource
2459 * OC_STACK_OK - no errors
2460 * OC_STACK_NO_RESOURCE - invalid resource handle
2461 * OC_STACK_NO_OBSERVERS - no more observers intrested in resource
2463 OCStackResult OCNotifyAllObservers(OCResourceHandle handle, OCQualityOfService qos) {
2465 OC_LOG(INFO, TAG, PCF("Entering OCNotifyAllObservers"));
2467 OCResource *resPtr = NULL;
2468 OCStackResult result;
2469 OCMethod method = OC_REST_NOMETHOD;
2470 uint32_t maxAge = 0;
2472 OC_LOG(INFO, TAG, PCF("Entering OCNotifyAllObservers"));
2473 #ifdef WITH_PRESENCE
2474 if(handle == presenceResource.handle)
2478 #endif // WITH_PRESENCE
2479 VERIFY_NON_NULL(handle, ERROR, OC_STACK_ERROR);
2481 // Verify that the resource exists
2482 resPtr = findResource ((OCResource *) handle);
2485 return OC_STACK_NO_RESOURCE;
2489 //only increment in the case of regular observing (not presence)
2490 incrementSequenceNumber(resPtr);
2491 method = OC_REST_OBSERVE;
2492 maxAge = MAX_OBSERVE_AGE;
2493 #ifdef WITH_PRESENCE
2494 result = SendAllObserverNotification (method, resPtr, maxAge, NULL, qos);
2496 result = SendAllObserverNotification (method, resPtr, maxAge, qos);
2503 OCNotifyListOfObservers (OCResourceHandle handle,
2504 OCObservationId *obsIdList,
2505 uint8_t numberOfIds,
2506 unsigned char *notificationJSONPayload,
2507 OCQualityOfService qos)
2509 OC_LOG(INFO, TAG, PCF("Entering OCNotifyListOfObservers"));
2511 OCResource *resPtr = NULL;
2512 //TODO: we should allow the server to define this
2513 uint32_t maxAge = MAX_OBSERVE_AGE;
2515 VERIFY_NON_NULL(handle, ERROR, OC_STACK_ERROR);
2516 VERIFY_NON_NULL(obsIdList, ERROR, OC_STACK_ERROR);
2517 VERIFY_NON_NULL(notificationJSONPayload, ERROR, OC_STACK_ERROR);
2519 // Verify that the resource exists
2520 resPtr = findResource ((OCResource *) handle);
2521 if (NULL == resPtr || myStackMode == OC_CLIENT)
2523 return OC_STACK_NO_RESOURCE;
2527 incrementSequenceNumber(resPtr);
2529 return (SendListObserverNotification(resPtr, obsIdList, numberOfIds,
2530 notificationJSONPayload, maxAge, qos));
2534 * Send a response to a request.
2535 * The response can be a regular, slow, or block (i.e. a response that
2536 * is too large to be sent in a single PDU and must span multiple transmissions)
2538 * @param response - pointer to structure that contains response parameters
2541 * OC_STACK_OK - No errors; Success
2542 * OC_STACK_INVALID_PARAM - Invalid pointer to OCServerResponse
2543 * OC_STACK_INVALID_REQUEST_HANDLE - Request handle not found
2544 * OC_STACK_PERSISTENT_BUFFER_REQUIRED - Block transfer needed for response, so a
2545 * persistent response buffer is necessary
2547 OCStackResult OCDoResponse(OCEntityHandlerResponse *ehResponse)
2549 OCStackResult result = OC_STACK_ERROR;
2550 OCServerRequest *serverRequest = NULL;
2552 OC_LOG(INFO, TAG, PCF("Entering OCDoResponse"));
2554 // Validate input parameters
2555 VERIFY_NON_NULL(ehResponse, ERROR, OC_STACK_INVALID_PARAM);
2556 VERIFY_NON_NULL(ehResponse->requestHandle, ERROR, OC_STACK_INVALID_PARAM);
2558 // TODO: Placeholder for creating a response entry when implementing
2559 // block transfer feature
2561 // If a response payload is present, check if block transfer is required
2562 if (ehResponse->payload && OCIsPacketTransferRequired(NULL,
2563 (const char *)ehResponse->payload, ehResponse->payloadSize))
2565 OC_LOG(INFO, TAG, PCF("Block transfer required"));
2567 // Persistent response buffer is needed for block transfer
2568 if (!ehResponse->persistentBufferFlag)
2570 OC_LOG(WARNING, TAG, PCF("Persistent response buffer required"));
2571 return OC_STACK_PERSISTENT_BUFFER_REQUIRED;
2573 // TODO: Placeholder for block transfer handling
2574 // TODO: Placeholder for setting the the response handle in the OCServerResponse struct
2575 // when implementing the block transfer feature
2581 // Get pointer to request info
2582 serverRequest = GetServerRequestUsingHandle((OCServerRequest *)ehResponse->requestHandle);
2585 result = serverRequest->ehResponseHandler(ehResponse);
2592 * Cancel a response. Applies to a block response
2594 * @param responseHandle - response handle set by stack in OCServerResponse after
2595 * OCDoResponse is called
2598 * OC_STACK_OK - No errors; Success
2599 * OC_STACK_INVALID_PARAM - The handle provided is invalid.
2601 OCStackResult OCCancelResponse(OCResponseHandle responseHandle)
2603 OCStackResult result = OC_STACK_NOTIMPL;
2605 OC_LOG(INFO, TAG, PCF("Entering OCCancelResponse"));
2607 // TODO: validate response handle
2612 //-----------------------------------------------------------------------------
2613 // Private internal function definitions
2614 //-----------------------------------------------------------------------------
2616 * Generate handle of OCDoResource invocation for callback management.
2618 static OCDoHandle GenerateInvocationHandle()
2620 OCDoHandle handle = NULL;
2621 // Generate token here, it will be deleted when the transaction is deleted
2622 handle = (OCDoHandle) OCMalloc(sizeof(uint8_t[MAX_TOKEN_LENGTH]));
2625 OCFillRandomMem((uint8_t*)handle, sizeof(uint8_t[MAX_TOKEN_LENGTH]));
2630 #ifdef WITH_PRESENCE
2631 OCStackResult OCChangeResourceProperty(OCResourceProperty * inputProperty,
2632 OCResourceProperty resourceProperties, uint8_t enable)
2634 if (resourceProperties
2635 > (OC_ACTIVE | OC_DISCOVERABLE | OC_OBSERVABLE | OC_SLOW)) {
2636 OC_LOG(ERROR, TAG, PCF("Invalid property"));
2637 return OC_STACK_INVALID_PARAM;
2641 *inputProperty = (OCResourceProperty) (*inputProperty & ~(resourceProperties));
2645 *inputProperty = (OCResourceProperty) (*inputProperty | resourceProperties);
2652 * Initialize resource data structures, variables, etc.
2654 OCStackResult initResources() {
2655 OCStackResult result = OC_STACK_OK;
2656 // Init application resource vars
2657 headResource = NULL;
2658 // Init Virtual Resources
2659 #ifdef WITH_PRESENCE
2660 presenceResource.presenceTTL = OC_DEFAULT_PRESENCE_TTL;
2661 //presenceResource.token = OCGenerateCoAPToken();
2662 result = OCCreateResource(&presenceResource.handle,
2663 OC_RSRVD_RESOURCE_TYPE_PRESENCE,
2668 //make resource inactive
2669 result = OCChangeResourceProperty(
2670 &(((OCResource *) presenceResource.handle)->resourceProperties),
2677 * Add a resource to the end of the linked list of resources.
2679 * @param resource - resource to be added
2681 void insertResource(OCResource *resource) {
2682 OCResource *pointer;
2684 if (!headResource) {
2685 headResource = resource;
2687 pointer = headResource;
2689 while (pointer->next) {
2690 pointer = pointer->next;
2692 pointer->next = resource;
2694 resource->next = NULL;
2698 * Find a resource in the linked list of resources.
2700 * @param resource - resource to be found
2702 * NULL - resource not found
2703 * pointer to resource - pointer to resource that was found in the linked list
2705 OCResource *findResource(OCResource *resource) {
2706 OCResource *pointer = headResource;
2709 if (pointer == resource) {
2712 pointer = pointer->next;
2717 void deleteAllResources()
2719 OCResource *pointer = headResource;
2724 temp = pointer->next;
2725 #ifdef WITH_PRESENCE
2726 if(pointer != (OCResource *) presenceResource.handle)
2728 #endif // WITH_PRESENCE
2729 deleteResource(pointer);
2730 #ifdef WITH_PRESENCE
2732 #endif // WITH_PRESENCE
2736 #ifdef WITH_PRESENCE
2737 // Ensure that the last resource to be deleted is the presence resource. This allows for all
2738 // presence notification attributed to their deletion to be processed.
2739 deleteResource((OCResource *) presenceResource.handle);
2740 #endif // WITH_PRESENCE
2744 * Delete the resource from the linked list.
2746 * @param resource - resource to be deleted
2751 int deleteResource(OCResource *resource) {
2752 OCResource *prev = NULL;
2755 temp = headResource;
2757 if (temp == resource) {
2758 // Invalidate all Resource Properties.
2759 resource->resourceProperties = (OCResourceProperty) 0;
2760 #ifdef WITH_PRESENCE
2761 if(resource != (OCResource *) presenceResource.handle)
2763 #endif // WITH_PRESENCE
2764 OCNotifyAllObservers((OCResourceHandle)resource, OC_HIGH_QOS);
2765 #ifdef WITH_PRESENCE
2768 if(presenceResource.handle)
2770 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2771 if(resource != (OCResource *) presenceResource.handle)
2773 SendPresenceNotification(resource->rsrcType);
2777 SendPresenceNotification(NULL);
2782 if (temp == headResource) {
2783 headResource = temp->next;
2785 prev->next = temp->next;
2788 deleteResourceElements(temp);
2801 * Delete all of the dynamically allocated elements that were created for the resource.
2803 * @param resource - specified resource
2805 void deleteResourceElements(OCResource *resource) {
2811 OCFree(resource->uri);
2813 // Delete resourcetype linked list
2814 deleteResourceType(resource->rsrcType);
2816 // Delete resourceinterface linked list
2817 deleteResourceInterface(resource->rsrcInterface);
2821 * Delete all of the dynamically allocated elements that were created for the resource type.
2823 * @param resourceType - specified resource type
2825 void deleteResourceType(OCResourceType *resourceType) {
2826 OCResourceType *pointer = resourceType;
2827 OCResourceType *next;
2830 next = pointer->next;
2831 OCFree(pointer->resourcetypename);
2838 * Delete all of the dynamically allocated elements that were created for the resource interface.
2840 * @param resourceInterface - specified resource interface
2842 void deleteResourceInterface(OCResourceInterface *resourceInterface) {
2843 OCResourceInterface *pointer = resourceInterface;
2844 OCResourceInterface *next;
2847 next = pointer->next;
2848 OCFree(pointer->name);
2855 * Insert a resource type into a resource's resource type linked list.
2857 * @param resource - resource where resource type is to be inserted
2858 * @param resourceType - resource type to be inserted
2860 void insertResourceType(OCResource *resource, OCResourceType *resourceType) {
2861 OCResourceType *pointer;
2863 if (resource && !resource->rsrcType) {
2864 resource->rsrcType = resourceType;
2868 pointer = resource->rsrcType;
2872 pointer = resourceType;
2874 while (pointer->next) {
2875 pointer = pointer->next;
2877 pointer->next = resourceType;
2879 resourceType->next = NULL;
2883 * Get a resource type at the specified index within a resource.
2885 * @param handle - handle of resource
2886 * @param index - index of resource type
2889 * resourcetype - if found
2892 OCResourceType *findResourceTypeAtIndex(OCResourceHandle handle, uint8_t index) {
2893 OCResource *resource;
2894 OCResourceType *pointer;
2897 // Find the specified resource
2898 resource = findResource((OCResource *) handle);
2903 // Make sure a resource has a resourcetype
2904 if (!resource->rsrcType) {
2908 // Iterate through the list
2909 pointer = resource->rsrcType;
2911 while ((i < index) && pointer) {
2913 pointer = pointer->next;
2919 * Finds a resource type in an OCResourceType link-list.
2921 * @param resourceTypeList - the link-list to be searched through
2922 * @param resourceTypeName - the key to search for
2925 * resourceType that matches the key (ie. resourceTypeName)
2926 * NULL - either an invalid parameter or this function was unable to find the key.
2928 OCResourceType *findResourceType(OCResourceType * resourceTypeList, const char * resourceTypeName)
2930 if(resourceTypeList && resourceTypeName)
2932 OCResourceType * rtPointer = resourceTypeList;
2933 while(resourceTypeName && rtPointer)
2935 if(rtPointer->resourcetypename &&
2936 strcmp(resourceTypeName, (const char *)
2937 (rtPointer->resourcetypename)) == 0)
2941 rtPointer = rtPointer->next;
2948 * Insert a resource interface into a resource's resource interface linked list.
2950 * @param resource - resource where resource interface is to be inserted
2951 * @param resourceInterface - resource interface to be inserted
2953 void insertResourceInterface(OCResource *resource,
2954 OCResourceInterface *resourceInterface) {
2955 OCResourceInterface *pointer;
2957 if (!resource->rsrcInterface) {
2958 resource->rsrcInterface = resourceInterface;
2960 pointer = resource->rsrcInterface;
2961 while (pointer->next) {
2962 pointer = pointer->next;
2964 pointer->next = resourceInterface;
2966 resourceInterface->next = NULL;
2970 * Get a resource interface at the specified index within a resource.
2972 * @param handle - handle of resource
2973 * @param index - index of resource interface
2976 * resourceinterface - if found
2979 OCResourceInterface *findResourceInterfaceAtIndex(OCResourceHandle handle,
2981 OCResource *resource;
2982 OCResourceInterface *pointer;
2985 // Find the specified resource
2986 resource = findResource((OCResource *) handle);
2991 // Make sure a resource has a resourceinterface
2992 if (!resource->rsrcInterface) {
2996 // Iterate through the list
2997 pointer = resource->rsrcInterface;
2999 while ((i < index) && pointer) {
3001 pointer = pointer->next;
3007 * Determine if a request/response must be sent in a block transfer because it is too large to be
3008 * sent in a single PDU. This function can be used for either a request or a response
3010 * @param request - NULL or pointer to request
3011 * @param response - NULL or pointer to response
3012 * @param size - 0 or size of the request/response. If 0, strlen is used for determining
3013 * the length of the request/response
3016 * 0 - packet transfer NOT required (i.e. normal request/response)
3017 * 1 - packet transfer required (i.e. block transfer needed)
3019 uint8_t OCIsPacketTransferRequired(const char *request, const char *response, uint16_t size)
3023 // Determine if we are checking a request or a response
3026 // If size is greater than 0, use it for the request size value, otherwise
3027 // assume request is null terminated and use strlen for size value
3028 if ((size > MAX_REQUEST_LENGTH) || (strlen(request) > MAX_REQUEST_LENGTH))
3035 // If size is greater than 0, use it for the response size value, otherwise
3036 // assume response is null terminated and use strlen for size value
3037 if ((size > MAX_RESPONSE_LENGTH) || (strlen(response) > MAX_RESPONSE_LENGTH))
3046 * Retrieves a resource type based upon a uri string if the uri string contains only just one
3047 * resource attribute (and that has to be of type "rt").
3049 * @remark This API malloc's memory for the resource type and newURI. Do not malloc resourceType
3050 * or newURI before passing in.
3052 * @param uri - Valid URI for "requiredUri" parameter to OCDoResource API.
3053 * @param resourceType - The resource type to be populated; pass by reference.
3054 * @param newURI - Return URI without resourceType appended to the end of it. This is used to
3055 * ensure that the uri parameter is not modified; pass by reference.
3058 * OC_STACK_INVALID_URI - Returns this if the URI is invalid/NULL.
3059 * OC_STACK_INVALID_PARAM - Returns this if the resourceType parameter is invalid/NULL.
3060 * OC_STACK_OK - Success
3062 OCStackResult getResourceType(const char * uri, unsigned char** resourceType, char ** newURI)
3066 return OC_STACK_INVALID_URI;
3068 if(!resourceType || !newURI)
3070 return OC_STACK_INVALID_PARAM;
3072 char * leftToken = NULL;
3073 char * tempURI = (char *) OCMalloc(strlen(uri) + 1);
3078 strcpy(tempURI, uri);
3079 leftToken = strtok((char *)tempURI, "?");
3081 while(leftToken != NULL)
3083 if(strncmp(leftToken, "rt=", 3) == 0)
3085 *resourceType = (unsigned char *) OCMalloc(strlen(leftToken)-3);
3090 strcpy((char *)*resourceType, ((const char *)&leftToken[3]));
3093 leftToken = strtok(NULL, "?");
3101 return OC_STACK_NO_MEMORY;
3104 const ServerID OCGetServerInstanceID(void)
3106 static bool generated = false;
3107 static ServerID sid;
3114 sid = OCGetRandom();
3119 const char* OCGetServerInstanceIDString(void)
3121 // max printed length of a base 10
3122 // uint32 is 10 characters, so 11 includes null.
3123 // This will change as the representation gets switched
3125 static char buffer[11];
3126 int n = sprintf(buffer, "%u", OCGetServerInstanceID());