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;
73 //-----------------------------------------------------------------------------
75 //-----------------------------------------------------------------------------
76 #define TAG PCF("OCStack")
77 #define VERIFY_SUCCESS(op, successCode) { if (op != successCode) \
78 {OC_LOG_V(FATAL, TAG, "%s failed!!", #op); goto exit;} }
79 #define VERIFY_NON_NULL(arg, logLevel, retVal) { if (!(arg)) { OC_LOG((logLevel), \
80 TAG, PCF(#arg " is NULL")); return (retVal); } }
81 #define VERIFY_NON_NULL_V(arg) { if (!arg) {OC_LOG_V(FATAL, TAG, "%s is NULL", #arg);\
84 //TODO: we should allow the server to define this
85 #define MAX_OBSERVE_AGE (0x2FFFFUL)
87 //-----------------------------------------------------------------------------
89 //-----------------------------------------------------------------------------
90 extern void DeinitOCSecurityInfo();
92 //-----------------------------------------------------------------------------
93 // Internal API function
94 //-----------------------------------------------------------------------------
96 // This internal function is called to update the stack with the status of
97 // observers and communication failures
98 OCStackResult OCStackFeedBack(OCCoAPToken * token, uint8_t status)
100 OCStackResult result = OC_STACK_ERROR;
101 ResourceObserver * observer = NULL;
102 OCEntityHandlerRequest ehRequest = {0};
106 case OC_OBSERVER_NOT_INTERESTED:
107 OC_LOG(DEBUG, TAG, PCF("observer is not interested in our notifications anymore"));
109 observer = GetObserverUsingToken (token->token);
111 observer = GetObserverUsingToken (token);
115 result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) NULL,
116 OC_REST_NOMETHOD, (OCResourceHandle) NULL, NULL, NULL, 0,
117 NULL, OC_OBSERVE_DEREGISTER, observer->observeId);
118 if(result != OC_STACK_OK)
122 observer->resource->entityHandler(OC_OBSERVE_FLAG, &ehRequest);
124 //observer is not observing anymore
126 result = DeleteObserverUsingToken (token->token);
128 result = DeleteObserverUsingToken (token);
130 if(result == OC_STACK_OK)
132 OC_LOG(DEBUG, TAG, PCF("Removed observer successfully"));
136 result = OC_STACK_OK;
137 OC_LOG(DEBUG, TAG, PCF("Observer Removal failed"));
140 case OC_OBSERVER_STILL_INTERESTED:
141 //observer is still interested
142 OC_LOG(DEBUG, TAG, PCF("observer is interested in our \
143 notifications, reset the failedCount"));
145 observer = GetObserverUsingToken (token->token);
147 observer = GetObserverUsingToken (token);
151 observer->forceHighQos = 0;
152 observer->failedCommCount = 0;
153 result = OC_STACK_OK;
157 result = OC_STACK_OBSERVER_NOT_FOUND;
160 case OC_OBSERVER_FAILED_COMM:
161 //observer is not reachable
162 OC_LOG(DEBUG, TAG, PCF("observer is unreachable"));
164 observer = GetObserverUsingToken (token->token);
166 observer = GetObserverUsingToken (token);
170 if(observer->failedCommCount >= MAX_OBSERVER_FAILED_COMM)
172 result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) NULL,
173 OC_REST_NOMETHOD, (OCResourceHandle) NULL, NULL, NULL, 0,
174 NULL, OC_OBSERVE_DEREGISTER, observer->observeId);
175 if(result != OC_STACK_OK)
177 return OC_STACK_ERROR;
179 observer->resource->entityHandler(OC_OBSERVE_FLAG, &ehRequest);
180 //observer is unreachable
181 result = DeleteObserverUsingToken (token);
182 if(result == OC_STACK_OK)
184 OC_LOG(DEBUG, TAG, PCF("Removed observer successfully"));
188 result = OC_STACK_OK;
189 OC_LOG(DEBUG, TAG, PCF("Observer Removal failed"));
194 observer->failedCommCount++;
195 result = OC_STACK_CONTINUE;
197 observer->forceHighQos = 1;
198 OC_LOG_V(DEBUG, TAG, "Failed count for this observer is %d",observer->failedCommCount);
202 OC_LOG(ERROR, TAG, PCF("Unknown status"));
203 result = OC_STACK_ERROR;
210 OCStackResult CAToOCStackResult(CAResponseResult_t caCode)
212 OCStackResult ret = OC_STACK_ERROR;
220 ret = OC_STACK_RESOURCE_CREATED;
223 ret = OC_STACK_RESOURCE_DELETED;
226 ret = OC_STACK_INVALID_QUERY;
229 ret = OC_STACK_INVALID_OPTION;
232 ret = OC_STACK_NO_RESOURCE;
240 //This function will be called back by CA layer when a response is received
241 void HandleCAResponses(const CARemoteEndpoint_t* endPoint, const CAResponseInfo_t* responseInfo)
243 OC_LOG(INFO, TAG, PCF("Enter HandleCAResponses"));
244 printf ("Received payload: %s\n", (char *)responseInfo->info.payload);
245 OCStackApplicationResult result = OC_STACK_DELETE_TRANSACTION;
246 ClientCB *cbNode = GetClientCB((CAToken_t *)&responseInfo->info.token, NULL, NULL);
249 OC_LOG(INFO, TAG, PCF("Calling into application address space"));
250 OCClientResponse response;
251 struct sockaddr_in sa;
253 inet_pton(AF_INET, endPoint->addressInfo.IP.ipAddress, &(sa.sin_addr));
254 sa.sin_port = htons(endPoint->addressInfo.IP.port);
255 static OCDevAddr address;
256 memcpy((void*)&address.addr, &(sa), sizeof(sa));
257 response.addr = &address;
258 response.result = CAToOCStackResult(responseInfo->result);
259 response.resJSONPayload = (unsigned char*)responseInfo->info.payload;
260 response.numRcvdVendorSpecificHeaderOptions = 0;
261 if(responseInfo->info.numOptions > 0)
263 //First option alwas with option ID COAP_OPTION_OBSERVE if it is availbale
264 if(responseInfo->info.options[0].optionID == COAP_OPTION_OBSERVE)
266 memcpy (&(response.sequenceNumber),
267 &(responseInfo->info.options[0].optionData), 4);
271 memcpy (&(response.rcvdVendorSpecificHeaderOptions[0]),
272 &(responseInfo->info.options[0]), sizeof(OCHeaderOption));
274 for (uint8_t i = 1; i < responseInfo->info.numOptions; i++)
276 memcpy (&(response.rcvdVendorSpecificHeaderOptions[i]),
277 &(responseInfo->info.options[i]), sizeof(OCHeaderOption));
279 response.numRcvdVendorSpecificHeaderOptions = responseInfo->info.numOptions;
281 result = cbNode->callBack(cbNode->context,
282 cbNode->handle, &response);
283 if (result == OC_STACK_DELETE_TRANSACTION)
285 FindAndDeleteClientCB(cbNode);
288 OC_LOG_V(INFO, TAG, PCF("Received payload: %s\n"), (char*)responseInfo->info.payload);
289 OC_LOG(INFO, TAG, PCF("Exit HandleCAResponses"));
292 //This function will be called back by CA layer when a request is received
293 void HandleCARequests(const CARemoteEndpoint_t* endPoint, const CARequestInfo_t* requestInfo)
295 CAInfo_t responseData;
296 CAResponseInfo_t responseInfo;
297 OCStackResult requestResult = OC_STACK_ERROR;
299 OC_LOG(INFO, TAG, PCF("Enter HandleCARequests"));
302 if(myStackMode == OC_CLIENT)
304 //TODO: should the client be responding to requests?
308 OCServerProtocolRequest serverRequest;
310 memset (&serverRequest, 0, sizeof(OCServerProtocolRequest));
311 // copy URI of resource
312 memcpy (&(serverRequest.resourceUrl), endPoint->resourceUri, strlen(endPoint->resourceUri));
314 // TODO-CA: Is the query part of header option?
315 //copy request payload
316 if (requestInfo->info.payload)
318 serverRequest.reqTotalSize = strlen(requestInfo->info.payload) + 1;
319 memcpy (&(serverRequest.reqJSONPayload), requestInfo->info.payload,
320 strlen(requestInfo->info.payload));
321 serverRequest.reqTotalSize = strlen((const char *)requestInfo->info.payload) + 1;
325 serverRequest.reqTotalSize = 1;
328 switch (requestInfo->method)
332 serverRequest.method = OC_REST_GET;
337 serverRequest.method = OC_REST_PUT;
342 serverRequest.method = OC_REST_POST;
347 serverRequest.method = OC_REST_DELETE;
352 OC_LOG(ERROR, TAG, PCF("Received CA method %d not supported"));
358 OC_LOG_V(INFO, TAG, "HandleCARequests: CA token length = %d", strlen(requestInfo->info.token));
359 OC_LOG_BUFFER(INFO, TAG, requestInfo->info.token, strlen(requestInfo->info.token));
360 // TODO-CA: For CA integration currently copying CAToken to OCCoapToken:
361 // Need to remove OCCoapToken
362 memcpy (&(serverRequest.requestToken.token), requestInfo->info.token,
363 strlen(requestInfo->info.token));
364 serverRequest.requestToken.tokenLength = strlen(requestInfo->info.token);
366 if (requestInfo->info.type == CA_MSG_CONFIRM)
368 serverRequest.qos = OC_HIGH_QOS;
370 else if (requestInfo->info.type == CA_MSG_NONCONFIRM)
372 serverRequest.qos = OC_LOW_QOS;
374 else if (requestInfo->info.type == CA_MSG_ACKNOWLEDGE)
376 // TODO-CA: Need to handle this
378 else if (requestInfo->info.type == CA_MSG_RESET)
380 // TODO-CA: Need to handle this
382 // CA does not need the following 3 fields
383 serverRequest.coapID = 0;
384 serverRequest.delayedResNeeded = 0;
385 serverRequest.secured = endPoint->isSecured;
388 serverRequest.addressInfo = endPoint->addressInfo;
389 serverRequest.connectivityType = endPoint->connectivityType;
390 if (requestInfo->info.token)
392 strncpy(serverRequest.token, requestInfo->info.token, sizeof(serverRequest.token) - 1);
395 struct sockaddr_in sa;
396 inet_pton(AF_INET, endPoint->addressInfo.IP.ipAddress, &(sa.sin_addr));
397 sa.sin_port = htons(endPoint->addressInfo.IP.port);
398 memcpy((void*)&serverRequest.requesterAddr, &(sa), sizeof(sa));
400 // copy vendor specific header options
401 // TODO-CA: CA is including non-vendor header options as well, like observe.
402 // Need to filter those out
403 GetObserveHeaderOption(&serverRequest.observationOption, requestInfo->info.options, &(requestInfo->info.numOptions));
404 if (requestInfo->info.numOptions > MAX_HEADER_OPTIONS)
406 // TODO-CA: Need to send an error indicating the num of options is incorrect
409 serverRequest.numRcvdVendorSpecificHeaderOptions = requestInfo->info.numOptions;
410 if (serverRequest.numRcvdVendorSpecificHeaderOptions)
412 memcpy (&(serverRequest.rcvdVendorSpecificHeaderOptions), requestInfo->info.options,
413 sizeof(CAHeaderOption_t)*requestInfo->info.numOptions);
416 requestResult = HandleStackRequests (&serverRequest);
420 // generate the pdu, if the request was CON, then the response is ACK, otherwire NON
421 memset(&responseData, 0, sizeof(CAInfo_t));
422 responseData.token = (requestInfo != NULL) ? requestInfo->info.token : "";
424 // TODO : I guess we need to allocate memeory?
425 responseData.payload = "{\"oc\":[{\"href\":\"/a/led\",\"sid\":\"\",\"prop\":{\"rt\":[\"core.led\"],\"if\":[\"oc.mi.def\"],\"obs\":1}}]}";
427 //responseInfo = (CAResponseInfo*) malloc(sizeof(CAResponseInfo));
428 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
429 responseInfo.result = 200;
430 responseInfo.info = responseData;
432 // send request (connectivityType from remoteEndpoint of request Info)
433 OC_LOG(INFO, TAG, PCF("CASendResponse in HandleCARequests"));
434 //TODO-CA: CASendResponse returns the result (we need to check if the
436 CAResult_t caResult = CASendResponse(endPoint, &responseInfo);
437 if(caResult != CA_STATUS_OK)
439 OC_LOG(ERROR, TAG, PCF("CASendResponse error"));
443 OC_LOG(INFO, TAG, PCF("Exit HandleCARequests"));
448 //This function will be called back by occoap layer when a request is received
449 OCStackResult HandleStackRequests(OCServerProtocolRequest * protocolRequest)
451 OC_LOG(INFO, TAG, PCF("Entering HandleStackRequests (OCStack Layer)"));
453 OCStackResult result = OC_STACK_ERROR;
454 ResourceHandling resHandling;
455 OCResource *resource;
457 OCServerRequest * request = GetServerRequestUsingToken(protocolRequest->requestToken);
460 OC_LOG(INFO, TAG, PCF("This is a new Server Request"));
462 result = AddServerCARequest(&request, protocolRequest->coapID,
463 protocolRequest->delayedResNeeded, protocolRequest->secured, 0,
464 protocolRequest->method, protocolRequest->numRcvdVendorSpecificHeaderOptions,
465 protocolRequest->observationOption, protocolRequest->qos,
466 protocolRequest->query, protocolRequest->rcvdVendorSpecificHeaderOptions,
467 protocolRequest->reqJSONPayload, &protocolRequest->requestToken,
468 &protocolRequest->requesterAddr, protocolRequest->resourceUrl,
469 protocolRequest->reqTotalSize,
470 &protocolRequest->addressInfo, protocolRequest->connectivityType, protocolRequest->token);
472 result = AddServerRequest(&request, protocolRequest->coapID,
473 protocolRequest->delayedResNeeded, protocolRequest->secured, 0,
474 protocolRequest->method, protocolRequest->numRcvdVendorSpecificHeaderOptions,
475 protocolRequest->observationOption, protocolRequest->qos,
476 protocolRequest->query, protocolRequest->rcvdVendorSpecificHeaderOptions,
477 protocolRequest->reqJSONPayload, &protocolRequest->requestToken,
478 &protocolRequest->requesterAddr, protocolRequest->resourceUrl,
479 protocolRequest->reqTotalSize);
481 if (OC_STACK_OK != result)
483 OC_LOG(ERROR, TAG, PCF("Error adding server request"));
486 VERIFY_NON_NULL(request, ERROR, OC_STACK_NO_MEMORY);
488 if(!protocolRequest->reqMorePacket)
490 request->requestComplete = 1;
495 OC_LOG(INFO, TAG, PCF("This is either a repeated Server Request or blocked Server Request"));
498 if(request->requestComplete)
500 OC_LOG(INFO, TAG, PCF("This Server Request is complete"));
501 result = DetermineResourceHandling (request, &resHandling, &resource);
502 if (result == OC_STACK_OK)
504 result = ProcessRequest(resHandling, resource, request);
508 result = OC_STACK_ERROR;
513 OC_LOG(INFO, TAG, PCF("This Server Request is incomplete"));
514 result = OC_STACK_CONTINUE;
519 //This function will be called back by occoap layer when a response is received
520 OCStackResult HandleStackResponses(OCResponse * response)
522 OC_LOG(INFO, TAG, PCF("Entering HandleStackResponses (OCStack Layer)"));
523 OCStackResult result = OC_STACK_OK;
524 OCStackApplicationResult cbResult = OC_STACK_DELETE_TRANSACTION;
525 uint8_t isObserveNotification = 0;
526 ClientCB * cbNode = NULL;
528 uint8_t isPresenceNotification = 0;
529 uint8_t isMulticastPresence = 0;
530 char * resourceTypeName = NULL;
531 uint32_t lowerBound = 0;
532 uint32_t higherBound = 0;
534 unsigned char * bufRes = response->bufRes;
535 #endif // WITH_PRESENCE
537 cbNode = response->cbNode;
540 cbNode = GetClientCB(response->rcvdToken, NULL, NULL);
543 if(response->clientResponse->sequenceNumber >= OC_OFFSET_SEQUENCE_NUMBER)
545 isObserveNotification = 1;
546 OC_LOG(INFO, TAG, PCF("Received an observe notification"));
549 OC_LOG_V(DEBUG, TAG, "The sequenceNumber/NONCE of this response %u",
550 response->clientResponse->sequenceNumber);
551 OC_LOG_V(DEBUG, TAG, "The maxAge/TTL of this response %u", response->maxAge);
552 OC_LOG_V(DEBUG, TAG, "The response received is %s", bufRes);
555 if(!strcmp((char *)response->rcvdUri, (char *)OC_PRESENCE_URI)){
556 isPresenceNotification = 1;
559 result = OC_STACK_INVALID_PARAM;
562 tok = strtok((char *)bufRes, "[:]}");
563 bufRes[strlen((char *)bufRes)] = ':';
564 tok = strtok(NULL, "[:]}");
565 bufRes[strlen((char *)bufRes)] = ':';
566 response->clientResponse->sequenceNumber = (uint32_t )atoi(tok);
567 OC_LOG_V(DEBUG, TAG, "The received NONCE is %u", response->clientResponse->sequenceNumber);
568 tok = strtok(NULL, "[:]}");
569 response->maxAge = (uint32_t )atoi(tok);
570 OC_LOG_V(DEBUG, TAG, "The received TTL is %u", response->maxAge);
571 tok = strtok(NULL, "[:]}");
574 resourceTypeName = (char *)OCMalloc(strlen(tok));
575 if(!resourceTypeName)
579 bufRes[strlen((char *)bufRes)] = ':';
580 strcpy(resourceTypeName, tok);
581 OC_LOG_V(DEBUG, TAG, "----------------resourceTypeName %s",
584 bufRes[strlen((char *)bufRes)] = ']';
587 // Check if the application subcribed for presence
590 cbNode = GetClientCB(NULL, NULL, response->fullUri);
593 // Check if application subscribed for multicast presence
596 snprintf((char *)response->fullUri, MAX_URI_LENGTH, "%s%s",
597 OC_MULTICAST_IP, response->rcvdUri);
598 cbNode = GetClientCB(NULL, NULL, response->fullUri);
601 isMulticastPresence = 1;
602 isPresenceNotification = 0;
606 if(cbNode && isPresenceNotification)
608 OC_LOG(INFO, TAG, PCF("Received a presence notification"));
609 if(!cbNode->presence)
611 cbNode->presence = (OCPresence *) OCMalloc(sizeof(OCPresence));
612 VERIFY_NON_NULL_V(cbNode->presence);
613 cbNode->presence->timeOut = NULL;
614 cbNode->presence->timeOut = (uint32_t *)
615 OCMalloc(PresenceTimeOutSize * sizeof(uint32_t));
616 if(!(cbNode->presence->timeOut)){
617 OCFree(cbNode->presence);
618 result = OC_STACK_NO_MEMORY;
621 if(response->maxAge == 0)
623 OC_LOG(INFO, TAG, PCF("===============Stopping presence"));
624 response->clientResponse->result = OC_STACK_PRESENCE_STOPPED;
627 OCFree(cbNode->presence->timeOut);
628 OCFree(cbNode->presence);
629 cbNode->presence = NULL;
634 OC_LOG_V(INFO, TAG, "===============Update presence TTL, now time is %d", GetTime(0));
635 cbNode->presence->TTL = response->maxAge;
636 for(int index = 0; index < PresenceTimeOutSize; index++)
638 lowerBound = GetTime(((float)(PresenceTimeOut[index])
639 /(float)100)*(float)cbNode->presence->TTL);
640 higherBound = GetTime(((float)(PresenceTimeOut[index + 1])
641 /(float)100)*(float)cbNode->presence->TTL);
642 cbNode->presence->timeOut[index] = OCGetRandomRange(lowerBound, higherBound);
643 OC_LOG_V(DEBUG, TAG, "----------------lowerBound timeout %d", lowerBound);
644 OC_LOG_V(DEBUG, TAG, "----------------higherBound timeout %d", higherBound);
645 OC_LOG_V(DEBUG, TAG, "----------------timeOut entry %d",
646 cbNode->presence->timeOut[index]);
648 cbNode->presence->TTLlevel = 0;
649 OC_LOG_V(DEBUG, TAG, "----------------this TTL level %d", cbNode->presence->TTLlevel);
650 if(cbNode->sequenceNumber == response->clientResponse->sequenceNumber)
652 OC_LOG(INFO, TAG, PCF("===============No presence change"));
655 OC_LOG(INFO, TAG, PCF("===============Presence changed, calling up the stack"));
656 cbNode->sequenceNumber = response->clientResponse->sequenceNumber;;
659 // Ensure that a filter is actually applied.
660 if(resourceTypeName && cbNode->filterResourceType)
662 if(!findResourceType(cbNode->filterResourceType, resourceTypeName))
668 else if(cbNode && isMulticastPresence)
670 // Check if the same nonce for a given host
671 OCMulticastNode* mcNode = NULL;
672 mcNode = GetMCPresenceNode(response->fullUri);
674 if(response->maxAge == 0)
676 OC_LOG(INFO, TAG, PCF("===============Stopping presence"));
677 response->clientResponse->result = OC_STACK_PRESENCE_STOPPED;
680 OCFree(cbNode->presence->timeOut);
681 OCFree(cbNode->presence);
682 cbNode->presence = NULL;
685 else if(mcNode != NULL)
687 if(mcNode->nonce == response->clientResponse->sequenceNumber)
689 OC_LOG(INFO, TAG, PCF("===============No presence change (Multicast)"));
690 result = OC_STACK_NO_MEMORY;
693 mcNode->nonce = response->clientResponse->sequenceNumber;
697 uint32_t uriLen = strlen((char*)response->fullUri);
698 unsigned char* uri = (unsigned char *) OCMalloc(uriLen + 1);
701 memcpy(uri, response->fullUri, (uriLen + 1));
706 PCF("===============No Memory for URI to store in the presence node"));
707 result = OC_STACK_NO_MEMORY;
710 result = AddMCPresenceNode(&mcNode, (unsigned char*) uri,
711 response->clientResponse->sequenceNumber);
712 if(result == OC_STACK_NO_MEMORY)
715 PCF("===============No Memory for Multicast Presence Node"));
716 result = OC_STACK_NO_MEMORY;
721 // Ensure that a filter is actually applied.
722 if(resourceTypeName && cbNode->filterResourceType)
724 if(!findResourceType(cbNode->filterResourceType, resourceTypeName))
731 else if(!cbNode && isPresenceNotification)
733 OC_LOG(INFO, TAG, PCF("Received a presence notification, but I do not have callback \
734 ------------ ignoring"));
736 #endif // WITH_PRESENCE
740 if(isObserveNotification)
742 OC_LOG(INFO, TAG, PCF("Received an observe notification"));
743 //TODO: check the standard for methods to detect wrap around condition
744 if(cbNode->method == OC_REST_OBSERVE &&
745 (response->clientResponse->sequenceNumber <= cbNode->sequenceNumber ||
746 (response->clientResponse->sequenceNumber > cbNode->sequenceNumber &&
747 response->clientResponse->sequenceNumber ==
748 MAX_SEQUENCE_NUMBER)))
750 OC_LOG_V(DEBUG, TAG, "Observe notification came out of order. \
751 Ignoring Incoming:%d Against Current:%d.",
752 response->clientResponse->sequenceNumber, cbNode->sequenceNumber);
755 if(response->clientResponse->sequenceNumber > cbNode->sequenceNumber){
756 cbNode->sequenceNumber = response->clientResponse->sequenceNumber;
760 response->clientResponse->resJSONPayload = bufRes;
762 cbResult = cbNode->callBack(cbNode->context, cbNode->handle, response->clientResponse);
764 if (cbResult == OC_STACK_DELETE_TRANSACTION ||
765 response->clientResponse->result == OC_STACK_COMM_ERROR ||
766 (response->clientResponse->result == OC_STACK_RESOURCE_DELETED &&
767 !isPresenceNotification && !isMulticastPresence))
769 FindAndDeleteClientCB(cbNode);
774 result = OC_STACK_ERROR;
779 OCFree(resourceTypeName);
784 int ParseIPv4Address(unsigned char * ipAddrStr, uint8_t * ipAddr, uint16_t * port)
787 unsigned char *itr, *coap;
788 uint8_t dotCount = 0;
792 /* search for scheme */
794 if (!isdigit((unsigned char) *ipAddrStr))
796 coap = (unsigned char *) OC_COAP_SCHEME;
797 while (*coap && tolower(*itr) == *coap)
806 if (isdigit((unsigned char) *ipAddrStr))
809 ipAddr[index] += *ipAddrStr - '0';
811 else if ((unsigned char) *ipAddrStr == '.')
823 if(*ipAddrStr == ':')
827 if (isdigit((unsigned char) *ipAddrStr))
830 *port += *ipAddrStr - '0';
841 if (ipAddr[0] < 255 && ipAddr[1] < 255 && ipAddr[2] < 255 && ipAddr[3] < 255
852 //-----------------------------------------------------------------------------
853 // Private internal function prototypes
854 //-----------------------------------------------------------------------------
856 static OCDoHandle GenerateInvocationHandle();
857 static OCStackResult initResources();
858 static void insertResource(OCResource *resource);
859 static OCResource *findResource(OCResource *resource);
860 static void insertResourceType(OCResource *resource,
861 OCResourceType *resourceType);
862 static OCResourceType *findResourceTypeAtIndex(OCResourceHandle handle,
864 static void insertResourceInterface(OCResource *resource,
865 OCResourceInterface *resourceInterface);
866 static OCResourceInterface *findResourceInterfaceAtIndex(
867 OCResourceHandle handle, uint8_t index);
868 static void deleteResourceType(OCResourceType *resourceType);
869 static void deleteResourceInterface(OCResourceInterface *resourceInterface);
870 static void deleteResourceElements(OCResource *resource);
871 static int deleteResource(OCResource *resource);
872 static void deleteAllResources();
873 static void incrementSequenceNumber(OCResource * resPtr);
874 static OCStackResult verifyUriQueryLength(const char * inputUri,
876 static uint8_t OCIsPacketTransferRequired(const char *request, const char *response, uint16_t size);
877 OCStackResult getResourceType(const char * uri, unsigned char** resourceType, char ** newURI);
879 //-----------------------------------------------------------------------------
881 //-----------------------------------------------------------------------------
884 * Initialize the OC Stack. Must be called prior to starting the stack.
887 * IP Address of host device
889 * Port of host device
891 * Host device is client, server, or client-server
894 * OC_STACK_OK - no errors
895 * OC_STACK_ERROR - stack init error
897 OCStackResult OCInit(const char *ipAddr, uint16_t port, OCMode mode)
899 OCStackResult result = OC_STACK_ERROR;
900 OC_LOG(INFO, TAG, PCF("Entering OCInit"));
904 OC_LOG_V(INFO, TAG, "IP Address = %s", ipAddr);
908 //It is ok to select network to CA_WIFI for now
909 CAResult_t caResult = CASelectNetwork(CA_WIFI);
910 if(caResult == CA_STATUS_OK)
912 OC_LOG(INFO, TAG, PCF("CASelectNetwork to WIFI"));
913 caResult = CARegisterHandler(HandleCARequests, HandleCAResponses);
914 if(caResult == CA_STATUS_OK)
916 OC_LOG(INFO, TAG, PCF("CARegisterHandler..."));
917 stackState = OC_STACK_INITIALIZED;
918 result = OC_STACK_OK;
922 caResult = CAStartDiscoveryServer();
923 OC_LOG(INFO, TAG, PCF("Client mode: CAStartDiscoveryServer"));
926 caResult = CAStartListeningServer();
927 OC_LOG(INFO, TAG, PCF("Server mode: CAStartListeningServer"));
929 case OC_CLIENT_SERVER:
930 caResult = CAStartListeningServer();
931 if(caResult == CA_STATUS_OK)
933 caResult = CAStartDiscoveryServer();
935 OC_LOG(INFO, TAG, PCF("Client-server mode"));
938 OC_LOG(ERROR, TAG, PCF("Invalid mode"));
939 return OC_STACK_ERROR;
944 if (caResult == CA_STATUS_OK)
946 result = OC_STACK_OK;
950 result = OC_STACK_ERROR;
957 OC_LOG(INFO, TAG, PCF("Client mode"));
960 OC_LOG(INFO, TAG, PCF("Server mode"));
962 case OC_CLIENT_SERVER:
963 OC_LOG(INFO, TAG, PCF("Client-server mode"));
966 OC_LOG(ERROR, TAG, PCF("Invalid mode"));
967 return OC_STACK_ERROR;
971 // Make call to OCCoAP layer
972 result = OCInitCoAP(ipAddr, (uint16_t) port, myStackMode);
976 defaultDeviceHandler = NULL;
979 PresenceTimeOutSize = sizeof(PresenceTimeOut)/sizeof(PresenceTimeOut[0]) - 1;
980 #endif // WITH_PRESENCE
982 if (result == OC_STACK_OK)
984 stackState = OC_STACK_INITIALIZED;
986 // Initialize resource
987 if(result == OC_STACK_OK && myStackMode != OC_CLIENT)
989 result = initResources();
991 if(result != OC_STACK_OK)
993 OC_LOG(ERROR, TAG, PCF("Stack initialization error"));
999 * Stop the OC stack. Use for a controlled shutdown.
1001 * OC_STACK_OK - no errors
1002 * OC_STACK_ERROR - stack not initialized
1004 OCStackResult OCStop()
1006 OCStackResult result = OC_STACK_ERROR;
1008 OC_LOG(INFO, TAG, PCF("Entering OCStop"));
1010 if (stackState == OC_STACK_UNINIT_IN_PROGRESS)
1012 OC_LOG(DEBUG, TAG, PCF("Stack already stopping, exiting"));
1015 else if (stackState != OC_STACK_INITIALIZED)
1017 OC_LOG(ERROR, TAG, PCF("Stack not initialized"));
1018 return OC_STACK_ERROR;
1021 stackState = OC_STACK_UNINIT_IN_PROGRESS;
1023 #ifdef WITH_PRESENCE
1024 // Ensure that the TTL associated with ANY and ALL presence notifications originating from
1025 // here send with the code "OC_STACK_PRESENCE_STOPPED" result.
1026 presenceResource.presenceTTL = 0;
1027 #endif // WITH_PRESENCE
1029 // Free memory dynamically allocated for resources
1030 deleteAllResources();
1033 // Make call to OCCoAP layer
1034 if (OCStopCoAP() == OC_STACK_OK)
1036 // Remove all observers
1037 DeleteObserverList();
1038 // Remove all the client callbacks
1039 DeleteClientCBList();
1040 stackState = OC_STACK_UNINITIALIZED;
1041 result = OC_STACK_OK;
1043 stackState = OC_STACK_INITIALIZED;
1044 result = OC_STACK_ERROR;
1047 // Deinit security blob
1048 DeinitOCSecurityInfo();
1050 if (result != OC_STACK_OK) {
1051 OC_LOG(ERROR, TAG, PCF("Stack stop error"));
1058 * Verify the lengths of the URI and the query separately
1060 * @param inputUri - Input URI and query.
1061 * @param uriLen - The length of the initial URI with query.
1063 * Note: The '?' that appears after the URI is not considered as
1064 * a part of the query.
1066 OCStackResult verifyUriQueryLength(const char *inputUri, uint16_t uriLen)
1070 query = strchr (inputUri, '?');
1074 if((query - inputUri) > MAX_URI_LENGTH)
1076 return OC_STACK_INVALID_URI;
1079 if((inputUri + uriLen - 1 - query) > MAX_QUERY_LENGTH)
1081 return OC_STACK_INVALID_QUERY;
1084 else if(uriLen > MAX_URI_LENGTH)
1086 return OC_STACK_INVALID_URI;
1092 * Discover or Perform requests on a specified resource (specified by that Resource's respective URI).
1094 * @param handle - @ref OCDoHandle to refer to the request sent out on behalf of calling this API.
1095 * @param method - @ref OCMethod to perform on the resource
1096 * @param requiredUri - URI of the resource to interact with
1097 * @param referenceUri - URI of the reference resource
1098 * @param request - JSON encoded request
1099 * @param qos - quality of service
1100 * @param cbData - struct that contains asynchronous callback function that is invoked
1101 * by the stack when discovery or resource interaction is complete
1102 * @param options - The address of an array containing the vendor specific header
1103 * header options to be sent with the request
1104 * @param numOptions - Number of vendor specific header options to be included
1107 * OC_STACK_OK - no errors
1108 * OC_STACK_INVALID_CALLBACK - invalid callback function pointer
1109 * OC_STACK_INVALID_METHOD - invalid resource method
1110 * OC_STACK_INVALID_URI - invalid required or reference URI
1113 OCStackResult OCDoResource(OCDoHandle *handle, OCMethod method, const char *requiredUri,
1114 const char *referenceUri, const char *request,
1115 OCQualityOfService qos, OCCallbackData *cbData,
1116 OCHeaderOption * options, uint8_t numOptions)
1118 OCStackResult result = OC_STACK_ERROR;
1120 ClientCB *clientCB = NULL;
1121 unsigned char * requestUri = NULL;
1122 unsigned char * resourceType = NULL;
1123 char * newUri = (char *)requiredUri;
1124 (void) referenceUri;
1126 CARemoteEndpoint_t* endpoint = NULL;
1127 CAResult_t caResult;
1128 CAToken_t caToken = NULL;
1129 CAInfo_t requestData;
1130 CARequestInfo_t requestInfo;
1131 // To track if memory is allocated for additional header options
1132 uint8_t hdrOptionMemAlloc = 0;
1135 OC_LOG(INFO, TAG, PCF("Entering OCDoResource"));
1137 // Validate input parameters
1138 VERIFY_NON_NULL(cbData, FATAL, OC_STACK_INVALID_CALLBACK);
1139 VERIFY_NON_NULL(cbData->cb, FATAL, OC_STACK_INVALID_CALLBACK);
1141 TODO ("Need to form the final query by concatenating require and reference URI's");
1142 VERIFY_NON_NULL(requiredUri, FATAL, OC_STACK_INVALID_URI);
1144 uint16_t uriLen = strlen(requiredUri);
1146 // ToDo: We should also check if the requiredUri has a mutlicast address, then qos has to be OC_Low_QOS
1152 case OC_REST_DELETE:
1153 case OC_REST_OBSERVE:
1154 case OC_REST_OBSERVE_ALL:
1155 case OC_REST_CANCEL_OBSERVE:
1157 #ifdef WITH_PRESENCE
1158 case OC_REST_PRESENCE:
1162 result = OC_STACK_INVALID_METHOD;
1166 if((result = verifyUriQueryLength(requiredUri, uriLen)) != OC_STACK_OK)
1171 if((request) && (strlen(request) > MAX_REQUEST_LENGTH))
1173 result = OC_STACK_INVALID_PARAM;
1177 #ifdef WITH_PRESENCE
1178 if(method == OC_REST_PRESENCE)
1180 result = getResourceType(requiredUri, &resourceType, &newUri);
1182 OC_LOG_V(DEBUG, TAG, "Got Resource Type: %s", resourceType);
1186 OC_LOG(DEBUG, TAG, PCF("Got Resource Type is NULL."));
1188 if(result != OC_STACK_OK)
1193 #endif // WITH_PRESENCE
1195 requestUri = (unsigned char *) OCMalloc(uriLen + 1);
1198 memcpy(requestUri, newUri, (uriLen + 1));
1202 result = OC_STACK_NO_MEMORY;
1206 *handle = GenerateInvocationHandle();
1209 result = OC_STACK_NO_MEMORY;
1214 memset(&requestData, 0, sizeof(CAInfo_t));
1215 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
1219 case OC_REST_OBSERVE:
1220 case OC_REST_OBSERVE_ALL:
1221 case OC_REST_CANCEL_OBSERVE:
1223 requestInfo.method = CA_GET;
1228 requestInfo.method = CA_PUT;
1233 requestInfo.method = CA_POST;
1236 case OC_REST_DELETE:
1238 requestInfo.method = CA_DELETE;
1241 #ifdef WITH_PRESENCE
1242 case OC_REST_PRESENCE:
1243 //TODO-CA: What should be the CA method?
1247 result = OC_STACK_INVALID_METHOD;
1251 // TODO-CA: Handle multi-cast scenario
1252 // Create remote end point
1253 caResult = CACreateRemoteEndpoint(newUri, CA_WIFI, &endpoint);
1254 // TODO-CA: Connectivity type should be passed to API
1255 endpoint->connectivityType = CA_WIFI;
1256 if (caResult != CA_STATUS_OK)
1258 OC_LOG(ERROR, TAG, PCF("CACreateRemoteEndpoint error"));
1263 caResult = CAGenerateToken(&caToken);
1265 if (caResult != CA_STATUS_OK)
1267 OC_LOG(ERROR, TAG, PCF("CAGenerateToken error"));
1272 // TODO-CA: Map QoS to the right CA msg type
1273 requestData.type = CA_MSG_NONCONFIRM;
1274 requestData.token = caToken;
1275 if ((method == OC_REST_OBSERVE) || (method == OC_REST_OBSERVE_ALL))
1277 result = CreateObserveHeaderOption (&(requestData.options), options,
1278 numOptions, OC_OBSERVE_REGISTER);
1279 if (result != OC_STACK_OK)
1283 hdrOptionMemAlloc = 1;
1284 requestData.numOptions = numOptions + 1;
1288 requestData.options = (CAHeaderOption_t*)options;
1289 requestData.numOptions = numOptions;
1291 requestData.payload = (char *)request;
1293 requestInfo.info = requestData;
1296 caResult = CASendRequest(endpoint, &requestInfo);
1297 if (caResult != CA_STATUS_OK)
1299 OC_LOG(ERROR, TAG, PCF("CASendRequest"));
1303 if((result = AddClientCB(&clientCB, cbData, &caToken, handle, method,
1304 requestUri, resourceType)) != OC_STACK_OK)
1306 result = OC_STACK_NO_MEMORY;
1312 // Generate token which will be used by OCStack to match responses received
1314 OCGenerateCoAPToken(&token);
1316 if((result = AddClientCB(&clientCB, cbData, &token, handle, method, requestUri, resourceType))
1319 result = OC_STACK_NO_MEMORY;
1323 #ifdef WITH_PRESENCE
1324 if(method == OC_REST_PRESENCE)
1326 // Replacing method type with GET because "presence" is a stack layer only implementation.
1327 method = OC_REST_GET;
1331 // Make call to OCCoAP layer
1332 result = OCDoCoAPResource(method, qos, &token, newUri, request, options, numOptions);
1336 if(newUri != requiredUri)
1340 if (result != OC_STACK_OK)
1342 OC_LOG(ERROR, TAG, PCF("OCDoResource error"));
1343 FindAndDeleteClientCB(clientCB);
1346 CADestroyRemoteEndpoint(endpoint);
1347 if (hdrOptionMemAlloc)
1349 OCFree(requestData.options);
1356 * Cancel a request associated with a specific @ref OCDoResource invocation.
1358 * @param handle - Used to identify a specific OCDoResource invocation.
1359 * @param qos - used to specify Quality of Service (read below for more info)
1360 * @param options- used to specify vendor specific header options when sending
1361 * explicit observe cancellation
1362 * @param numOptions- Number of header options to be included
1365 * OC_STACK_OK - No errors; Success
1366 * OC_STACK_INVALID_PARAM - The handle provided is invalid.
1368 OCStackResult OCCancel(OCDoHandle handle, OCQualityOfService qos, OCHeaderOption * options,
1372 * This ftn is implemented one of two ways in the case of observation:
1374 * 1. qos == OC_NON_CONFIRMABLE. When observe is unobserved..
1375 * Remove the callback associated on client side.
1376 * When the next notification comes in from server,
1377 * reply with RESET message to server.
1378 * Keep in mind that the server will react to RESET only
1379 * if the last notification was sent ans CON
1381 * 2. qos == OC_CONFIRMABLE. When OCCancel is called,
1382 * and it is associated with an observe request
1383 * (i.e. ClientCB->method == OC_REST_OBSERVE || OC_REST_OBSERVE_ALL),
1384 * Send CON Observe request to server with
1385 * observe flag = OC_RESOURCE_OBSERVE_DEREGISTER.
1386 * Remove the callback associated on client side.
1388 OCStackResult ret = OC_STACK_OK;
1390 CARemoteEndpoint_t* endpoint = NULL;
1391 CAResult_t caResult;
1392 CAInfo_t requestData;
1393 CARequestInfo_t requestInfo;
1394 // Track if memory is allocated for additional header options
1395 uint8_t hdrOptionMemAlloc = 0;
1399 return OC_STACK_INVALID_PARAM;
1402 OC_LOG(INFO, TAG, PCF("Entering OCCancel"));
1404 ClientCB *clientCB = GetClientCB(NULL, handle, NULL);
1407 switch (clientCB->method)
1409 case OC_REST_OBSERVE:
1410 case OC_REST_OBSERVE_ALL:
1412 caResult = CACreateRemoteEndpoint((char *)clientCB->requestUri, CA_WIFI,
1414 endpoint->connectivityType = CA_WIFI;
1415 if (caResult != CA_STATUS_OK)
1417 OC_LOG(ERROR, TAG, PCF("CACreateRemoteEndpoint error"));
1418 return OC_STACK_ERROR;
1421 memset(&requestData, 0, sizeof(CAInfo_t));
1422 // TODO-CA: Map QoS to the right CA msg type
1423 requestData.type = CA_MSG_NONCONFIRM;
1424 requestData.token = clientCB->token;
1425 if (CreateObserveHeaderOption (&(requestData.options),
1426 options, numOptions, OC_OBSERVE_DEREGISTER) != OC_STACK_OK)
1428 return OC_STACK_ERROR;
1430 hdrOptionMemAlloc = 1;
1431 requestData.numOptions = numOptions + 1;
1432 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
1433 requestInfo.method = CA_GET;
1434 requestInfo.info = requestData;
1436 caResult = CASendRequest(endpoint, &requestInfo);
1437 if (caResult != CA_STATUS_OK)
1439 OC_LOG(ERROR, TAG, PCF("CASendRequest error"));
1441 if(caResult == CA_STATUS_OK)
1446 if(qos == OC_HIGH_QOS)
1448 ret = OCDoCoAPResource(OC_REST_CANCEL_OBSERVE, qos,
1449 &(clientCB->token), (const char *) clientCB->requestUri, NULL, options,
1454 FindAndDeleteClientCB(clientCB);
1458 #ifdef WITH_PRESENCE
1459 case OC_REST_PRESENCE:
1460 FindAndDeleteClientCB(clientCB);
1464 return OC_STACK_INVALID_METHOD;
1468 CADestroyRemoteEndpoint(endpoint);
1469 if (hdrOptionMemAlloc)
1471 OCFree(requestData.options);
1477 #ifdef WITH_PRESENCE
1478 OCStackResult OCProcessPresence()
1480 OCStackResult result = OC_STACK_OK;
1481 uint8_t ipAddr[4] = { 0 };
1484 OC_LOG(INFO, TAG, PCF("Entering RequestPresence"));
1485 ClientCB* cbNode = NULL;
1487 OCClientResponse clientResponse;
1488 OCResponse * response = NULL;
1490 LL_FOREACH(cbList, cbNode) {
1491 if(OC_REST_PRESENCE == cbNode->method)
1493 if(cbNode->presence)
1495 uint32_t now = GetTime(0);
1496 OC_LOG_V(DEBUG, TAG, "----------------this TTL level %d", cbNode->presence->TTLlevel);
1497 OC_LOG_V(DEBUG, TAG, "----------------current ticks %d", now);
1500 if(cbNode->presence->TTLlevel >= (PresenceTimeOutSize + 1))
1505 if(cbNode->presence->TTLlevel < PresenceTimeOutSize){
1506 OC_LOG_V(DEBUG, TAG, "----------------timeout ticks %d",
1507 cbNode->presence->timeOut[cbNode->presence->TTLlevel]);
1510 if(cbNode->presence->TTLlevel >= PresenceTimeOutSize)
1512 OC_LOG(DEBUG, TAG, PCF("----------------No more timeout ticks"));
1513 if (ParseIPv4Address( cbNode->requestUri, ipAddr, &port))
1515 OCBuildIPv4Address(ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3], port,
1517 result = FormOCClientResponse(&clientResponse, OC_STACK_PRESENCE_TIMEOUT,
1518 (OCDevAddr *) &dst, 0, NULL);
1519 if(result != OC_STACK_OK)
1523 result = FormOCResponse(&response, cbNode, 0, NULL, NULL,
1524 &cbNode->token, &clientResponse, NULL);
1525 if(result != OC_STACK_OK)
1530 // Increment the TTLLevel (going to a next state), so we don't keep
1531 // sending presence notification to client.
1532 cbNode->presence->TTLlevel++;
1533 OC_LOG_V(DEBUG, TAG, "----------------moving to TTL level %d",
1534 cbNode->presence->TTLlevel);
1538 result = OC_STACK_INVALID_IP;
1541 HandleStackResponses(response);
1543 if(now >= cbNode->presence->timeOut[cbNode->presence->TTLlevel])
1545 OC_LOG(DEBUG, TAG, PCF("time to test server presence =========="));
1547 OCGenerateCoAPToken(&token);
1548 result = OCDoCoAPResource(OC_REST_GET, OC_LOW_QOS,
1549 &token, (const char *)cbNode->requestUri, NULL, NULL, 0);
1550 if(result != OC_STACK_OK)
1554 cbNode->presence->TTLlevel++;
1555 OC_LOG_V(DEBUG, TAG, "----------------moving to TTL level %d", cbNode->presence->TTLlevel);
1561 if (result != OC_STACK_OK)
1563 OC_LOG(ERROR, TAG, PCF("OCProcessPresence error"));
1570 * Called in main loop of OC client or server. Allows low-level processing of
1574 * OC_STACK_OK - no errors
1575 * OC_STACK_ERROR - stack process error
1577 OCStackResult OCProcess() {
1579 OC_LOG(INFO, TAG, PCF("Entering OCProcess"));
1580 #ifdef WITH_PRESENCE
1581 OCProcessPresence();
1584 CAHandleRequestResponse();
1592 #ifdef WITH_PRESENCE
1594 * When operating in @ref OCServer or @ref OCClientServer mode, this API will start sending out
1595 * presence notifications to clients via multicast. Once this API has been called with a success,
1596 * clients may query for this server's presence and this server's stack will respond via multicast.
1598 * Server can call this function when it comes online for the first time, or when it comes back
1599 * online from offline mode, or when it re enters network.
1601 * @param ttl - Time To Live in seconds
1602 * Note: If ttl is '0', then the default stack value will be used (60 Seconds).
1605 * OC_STACK_OK - No errors; Success
1607 OCStackResult OCStartPresence(const uint32_t ttl)
1609 OCChangeResourceProperty(
1610 &(((OCResource *)presenceResource.handle)->resourceProperties),
1615 presenceResource.presenceTTL = ttl;
1618 if(OC_PRESENCE_UNINITIALIZED == presenceState)
1620 OCDevAddr multiCastAddr;
1623 presenceState = OC_PRESENCE_INITIALIZED;
1624 OCGenerateCoAPToken(&token);
1625 OCBuildIPv4Address(224, 0, 1, 187, 5683, &multiCastAddr);
1626 //add the presence observer
1627 AddObserver(OC_PRESENCE_URI, NULL, 0, &token, &multiCastAddr,
1628 (OCResource *)presenceResource.handle, OC_LOW_QOS);
1631 // Each time OCStartPresence is called
1632 // a different random 32-bit integer number is used
1633 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
1635 return SendPresenceNotification(NULL);
1639 * When operating in @ref OCServer or @ref OCClientServer mode, this API will stop sending out
1640 * presence notifications to clients via multicast. Once this API has been called with a success,
1641 * this server's stack will not respond to clients querying for this server's presence.
1643 * Server can call this function when it is terminating, going offline, or when going
1644 * away from network.
1647 * OC_STACK_OK - No errors; Success
1649 OCStackResult OCStopPresence()
1651 OCStackResult result = OC_STACK_ERROR;
1652 //make resource inactive
1653 result = OCChangeResourceProperty(
1654 &(((OCResource *) presenceResource.handle)->resourceProperties),
1656 result = SendPresenceNotification(NULL);
1663 OCStackResult OCSetDefaultDeviceEntityHandler(OCDeviceEntityHandler entityHandler)
1665 defaultDeviceHandler = entityHandler;
1670 OCStackResult OCSetDeviceInfo(OCDeviceInfo deviceInfo)
1672 OC_LOG(INFO, TAG, PCF("Entering OCSetDeviceInfo"));
1674 if(myStackMode == OC_CLIENT)
1676 return OC_STACK_ERROR;
1679 return SaveDeviceInfo(deviceInfo);
1685 * @param handle - pointer to handle to newly created resource. Set by ocstack. Used to refer to resource
1686 * @param resourceTypeName - name of resource type. Example: "core.led"
1687 * @param resourceInterfaceName - name of resource interface. Example: "core.rw"
1688 * @param uri - URI of the resource. Example: "/a/led"
1689 * @param entityHandler - entity handler function that is called by ocstack to handle requests, etc
1690 * NULL for default entity handler
1691 * @param resourceProperties - properties supported by resource. Example: OC_DISCOVERABLE|OC_OBSERVABLE
1694 * OC_STACK_OK - no errors
1695 * OC_STACK_ERROR - stack process error
1697 OCStackResult OCCreateResource(OCResourceHandle *handle,
1698 const char *resourceTypeName,
1699 const char *resourceInterfaceName,
1700 const char *uri, OCEntityHandler entityHandler,
1701 uint8_t resourceProperties) {
1703 OCResource *pointer = NULL;
1706 OCStackResult result = OC_STACK_ERROR;
1708 OC_LOG(INFO, TAG, PCF("Entering OCCreateResource"));
1710 if(myStackMode == OC_CLIENT)
1714 // Validate parameters
1715 if(!uri || (strlen(uri) == 0))
1717 OC_LOG(ERROR, TAG, PCF("URI is invalid"));
1718 return OC_STACK_INVALID_URI;
1720 // Is it presented during resource discovery?
1721 if (!handle || !resourceTypeName) {
1722 OC_LOG(ERROR, TAG, PCF("Input parameter is NULL"));
1723 return OC_STACK_INVALID_PARAM;
1726 if(!resourceInterfaceName || strlen(resourceInterfaceName) == 0) {
1727 resourceInterfaceName = OC_RSRVD_INTERFACE_DEFAULT;
1730 // Make sure resourceProperties bitmask has allowed properties specified
1731 if (resourceProperties
1732 > (OC_ACTIVE | OC_DISCOVERABLE | OC_OBSERVABLE | OC_SLOW | OC_SECURE)) {
1733 OC_LOG(ERROR, TAG, PCF("Invalid property"));
1734 return OC_STACK_INVALID_PARAM;
1737 // If the headResource is NULL, then no resources have been created...
1738 pointer = headResource;
1740 // At least one resources is in the resource list, so we need to search for
1741 // repeated URLs, which are not allowed. If a repeat is found, exit with an error
1743 if (strcmp(uri, pointer->uri) == 0) {
1744 OC_LOG(ERROR, TAG, PCF("URI already in use"));
1745 return OC_STACK_INVALID_PARAM;
1747 pointer = pointer->next;
1750 // Create the pointer and insert it into the resource list
1751 pointer = (OCResource *) OCCalloc(1, sizeof(OCResource));
1755 pointer->sequenceNum = OC_OFFSET_SEQUENCE_NUMBER;
1757 insertResource(pointer);
1760 size = strlen(uri) + 1;
1761 str = (char *) OCMalloc(size);
1765 strncpy(str, uri, size);
1768 // Set properties. Set OC_ACTIVE
1769 pointer->resourceProperties = (OCResourceProperty) (resourceProperties
1772 // Add the resourcetype to the resource
1773 result = BindResourceTypeToResource(pointer, resourceTypeName);
1774 if (result != OC_STACK_OK) {
1775 OC_LOG(ERROR, TAG, PCF("Error adding resourcetype"));
1779 // Add the resourceinterface to the resource
1780 result = BindResourceInterfaceToResource(pointer, resourceInterfaceName);
1781 if (result != OC_STACK_OK) {
1782 OC_LOG(ERROR, TAG, PCF("Error adding resourceinterface"));
1786 // If an entity handler has been passed, attach it to the newly created
1787 // resource. Otherwise, set the default entity handler.
1790 pointer->entityHandler = entityHandler;
1794 pointer->entityHandler = defaultResourceEHandler;
1798 result = OC_STACK_OK;
1800 #ifdef WITH_PRESENCE
1801 if(presenceResource.handle)
1803 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
1804 SendPresenceNotification(pointer->rsrcType);
1808 if (result != OC_STACK_OK)
1810 // Deep delete of resource and other dynamic elements that it contains
1811 deleteResource(pointer);
1820 * Create a resource. with host ip address for remote resource
1822 * @param handle - pointer to handle to newly created resource. Set by ocstack.
1823 * Used to refer to resource
1824 * @param resourceTypeName - name of resource type. Example: "core.led"
1825 * @param resourceInterfaceName - name of resource interface. Example: "core.rw"
1826 * @param host - HOST address of the remote resource. Example: "coap://xxx.xxx.xxx.xxx:xxxxx"
1827 * @param uri - URI of the resource. Example: "/a/led"
1828 * @param entityHandler - entity handler function that is called by ocstack to handle requests, etc
1829 * NULL for default entity handler
1830 * @param resourceProperties - properties supported by resource.
1831 * Example: OC_DISCOVERABLE|OC_OBSERVABLE
1834 * OC_STACK_OK - no errors
1835 * OC_STACK_ERROR - stack process error
1838 OCStackResult OCCreateResourceWithHost(OCResourceHandle *handle,
1839 const char *resourceTypeName,
1840 const char *resourceInterfaceName,
1843 OCEntityHandler entityHandler,
1844 uint8_t resourceProperties)
1848 OCStackResult result = OC_STACK_ERROR;
1850 result = OCCreateResource(handle, resourceTypeName, resourceInterfaceName,
1851 uri, entityHandler, resourceProperties);
1853 if (result != OC_STACK_ERROR)
1856 size = strlen(host) + 1;
1857 str = (char *) OCMalloc(size);
1860 return OC_STACK_ERROR;
1862 strncpy(str, host, size);
1863 ((OCResource *) *handle)->host = str;
1870 * Add a resource to a collection resource.
1872 * @param collectionHandle - handle to the collection resource
1873 * @param resourceHandle - handle to resource to be added to the collection resource
1876 * OC_STACK_OK - no errors
1877 * OC_STACK_ERROR - stack process error
1878 * OC_STACK_INVALID_PARAM - invalid collectionhandle
1880 OCStackResult OCBindResource(
1881 OCResourceHandle collectionHandle, OCResourceHandle resourceHandle) {
1882 OCResource *resource;
1885 OC_LOG(INFO, TAG, PCF("Entering OCBindResource"));
1887 // Validate parameters
1888 VERIFY_NON_NULL(collectionHandle, ERROR, OC_STACK_ERROR);
1889 VERIFY_NON_NULL(resourceHandle, ERROR, OC_STACK_ERROR);
1890 // Container cannot contain itself
1891 if (collectionHandle == resourceHandle) {
1892 OC_LOG(ERROR, TAG, PCF("Added handle equals collection handle"));
1893 return OC_STACK_INVALID_PARAM;
1896 // Use the handle to find the resource in the resource linked list
1897 resource = findResource((OCResource *) collectionHandle);
1899 OC_LOG(ERROR, TAG, PCF("Collection handle not found"));
1900 return OC_STACK_INVALID_PARAM;
1903 // Look for an open slot to add add the child resource.
1904 // If found, add it and return success
1905 for (i = 0; i < MAX_CONTAINED_RESOURCES; i++) {
1906 if (!resource->rsrcResources[i]) {
1907 resource->rsrcResources[i] = (OCResource *) resourceHandle;
1908 OC_LOG(INFO, TAG, PCF("resource bound"));
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;
1926 * Remove a resource from a collection resource.
1928 * @param collectionHandle - handle to the collection resource
1929 * @param resourceHandle - handle to resource to be added to the collection resource
1932 * OC_STACK_OK - no errors
1933 * OC_STACK_ERROR - stack process error
1934 * OC_STACK_INVALID_PARAM - invalid collectionHandle
1936 OCStackResult OCUnBindResource(
1937 OCResourceHandle collectionHandle, OCResourceHandle resourceHandle) {
1938 OCResource *resource;
1941 OC_LOG(INFO, TAG, PCF("Entering OCUnBindResource"));
1943 // Validate parameters
1944 VERIFY_NON_NULL(collectionHandle, ERROR, OC_STACK_ERROR);
1945 VERIFY_NON_NULL(resourceHandle, ERROR, OC_STACK_ERROR);
1946 // Container cannot contain itself
1947 if (collectionHandle == resourceHandle) {
1948 OC_LOG(ERROR, TAG, PCF("removing handle equals collection handle"));
1949 return OC_STACK_INVALID_PARAM;
1952 // Use the handle to find the resource in the resource linked list
1953 resource = findResource((OCResource *) collectionHandle);
1955 OC_LOG(ERROR, TAG, PCF("Collection handle not found"));
1956 return OC_STACK_INVALID_PARAM;
1959 // Look for an open slot to add add the child resource.
1960 // If found, add it and return success
1961 for (i = 0; i < MAX_CONTAINED_RESOURCES; i++) {
1962 if (resourceHandle == resource->rsrcResources[i]) {
1963 resource->rsrcResources[i] = (OCResource *) NULL;
1964 OC_LOG(INFO, TAG, PCF("resource unbound"));
1969 OC_LOG(INFO, TAG, PCF("resource not found in collection"));
1971 #ifdef WITH_PRESENCE
1972 if(presenceResource.handle)
1974 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
1975 SendPresenceNotification(((OCResource *) resourceHandle)->rsrcType);
1979 // Unable to add resourceHandle, so return error
1980 return OC_STACK_ERROR;
1983 OCStackResult BindResourceTypeToResource(OCResource* resource,
1984 const char *resourceTypeName)
1986 OCResourceType *pointer = NULL;
1989 OCStackResult result = OC_STACK_ERROR;
1991 OC_LOG(INFO, TAG, PCF("Entering BindResourceTypeToResource"));
1993 // Validate parameters
1994 VERIFY_NON_NULL(resourceTypeName, ERROR, OC_STACK_INVALID_PARAM);
1995 // TODO: Does resource attribute resentation really have to be maintained in stack?
1996 // Is it presented during resource discovery?
1998 TODO ("Make sure that the resourcetypename doesn't already exist in the resource");
2000 // Create the resourcetype and insert it into the resource list
2001 pointer = (OCResourceType *) OCCalloc(1, sizeof(OCResourceType));
2006 // Set the resourceTypeName
2007 size = strlen(resourceTypeName) + 1;
2008 str = (char *) OCMalloc(size);
2012 strncpy(str, resourceTypeName, size);
2013 pointer->resourcetypename = str;
2015 insertResourceType(resource, pointer);
2016 result = OC_STACK_OK;
2018 exit: if (result != OC_STACK_OK) {
2026 OCStackResult BindResourceInterfaceToResource(OCResource* resource,
2027 const char *resourceInterfaceName)
2029 OCResourceInterface *pointer = NULL;
2032 OCStackResult result = OC_STACK_ERROR;
2034 OC_LOG(INFO, TAG, PCF("Entering BindResourceInterfaceToResource"));
2036 // Validate parameters
2037 VERIFY_NON_NULL(resourceInterfaceName, ERROR, OC_STACK_INVALID_PARAM);
2039 TODO ("Make sure that the resourceinterface name doesn't already exist in the resource");
2041 // Create the resourceinterface and insert it into the resource list
2042 pointer = (OCResourceInterface *) OCCalloc(1, sizeof(OCResourceInterface));
2047 // Set the resourceinterface name
2048 size = strlen(resourceInterfaceName) + 1;
2049 str = (char *) OCMalloc(size);
2053 strncpy(str, resourceInterfaceName, size);
2054 pointer->name = str;
2056 // Bind the resourceinterface to the resource
2057 insertResourceInterface(resource, pointer);
2059 result = OC_STACK_OK;
2061 exit: if (result != OC_STACK_OK) {
2070 * Bind a resourcetype to a resource.
2072 * @param handle - handle to the resource
2073 * @param resourceTypeName - name of resource type. Example: "core.led"
2076 * OC_STACK_OK - no errors
2077 * OC_STACK_ERROR - stack process error
2079 OCStackResult OCBindResourceTypeToResource(OCResourceHandle handle,
2080 const char *resourceTypeName) {
2082 OCStackResult result = OC_STACK_ERROR;
2083 OCResource *resource;
2085 // Make sure resource exists
2086 resource = findResource((OCResource *) handle);
2088 OC_LOG(ERROR, TAG, PCF("Resource not found"));
2089 return OC_STACK_ERROR;
2092 // call internal function
2093 result = BindResourceTypeToResource(resource, resourceTypeName);
2095 #ifdef WITH_PRESENCE
2096 if(presenceResource.handle)
2098 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2099 SendPresenceNotification(resource->rsrcType);
2107 * Bind a resourceinterface to a resource.
2109 * @param handle - handle to the resource
2110 * @param resourceInterfaceName - name of resource interface. Example: "oc.mi.b"
2113 * OC_STACK_OK - no errors
2114 * OC_STACK_ERROR - stack process error
2117 OCStackResult OCBindResourceInterfaceToResource(OCResourceHandle handle,
2118 const char *resourceInterfaceName) {
2120 OCStackResult result = OC_STACK_ERROR;
2121 OCResource *resource;
2123 // Make sure resource exists
2124 resource = findResource((OCResource *) handle);
2126 OC_LOG(ERROR, TAG, PCF("Resource not found"));
2127 return OC_STACK_ERROR;
2130 // call internal function
2131 result = BindResourceInterfaceToResource(resource, resourceInterfaceName);
2133 #ifdef WITH_PRESENCE
2134 if(presenceResource.handle)
2136 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2137 SendPresenceNotification(resource->rsrcType);
2145 * Get the number of resources that have been created in the stack.
2147 * @param numResources - pointer to count variable
2150 * OC_STACK_OK - no errors
2151 * OC_STACK_ERROR - stack process error
2154 OCStackResult OCGetNumberOfResources(uint8_t *numResources) {
2155 OCResource *pointer = headResource;
2157 OC_LOG(INFO, TAG, PCF("Entering OCGetNumberOfResources"));
2158 VERIFY_NON_NULL(numResources, ERROR, OC_STACK_INVALID_PARAM);
2161 *numResources = *numResources + 1;
2162 pointer = pointer->next;
2168 * Get a resource handle by index.
2170 * @param index - index of resource, 0 to Count - 1
2173 * Resource handle - if found
2174 * NULL - if not found
2176 OCResourceHandle OCGetResourceHandle(uint8_t index) {
2177 OCResource *pointer = headResource;
2180 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceHandle"));
2182 // Iterate through the list
2183 while ((i < index) && pointer) {
2185 pointer = pointer->next;
2187 return (OCResourceHandle) pointer;
2191 * Delete resource specified by handle. Deletes resource and all resourcetype and resourceinterface
2194 * @param handle - handle of resource to be deleted
2197 * OC_STACK_OK - no errors
2198 * OC_STACK_ERROR - stack process error
2199 * OC_STACK_NO_RESOURCE - resource not found
2200 * OC_STACK_INVALID_PARAM - invalid param
2202 OCStackResult OCDeleteResource(OCResourceHandle handle) {
2203 OC_LOG(INFO, TAG, PCF("Entering OCDeleteResource"));
2206 OC_LOG(ERROR, TAG, PCF("Invalid param"));
2207 return OC_STACK_INVALID_PARAM;
2210 OCResource *resource = findResource((OCResource *) handle);
2211 if (resource == NULL) {
2212 OC_LOG(ERROR, TAG, PCF("Resource not found"));
2213 return OC_STACK_NO_RESOURCE;
2216 if (deleteResource((OCResource *) handle) == 0) {
2217 OC_LOG(ERROR, TAG, PCF("Error deleting resource"));
2218 return OC_STACK_ERROR;
2225 * Get the URI of the resource specified by handle.
2227 * @param handle - handle of resource
2229 * URI string - if resource found
2230 * NULL - resource not found
2232 const char *OCGetResourceUri(OCResourceHandle handle) {
2233 OCResource *resource;
2234 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceUri"));
2236 resource = findResource((OCResource *) handle);
2238 return resource->uri;
2240 return (const char *) NULL;
2244 * Get the properties of the resource specified by handle.
2245 * NOTE: that after a resource is created, the OC_ACTIVE property is set
2246 * for the resource by the stack.
2248 * @param handle - handle of resource
2250 * property bitmap - if resource found
2251 * NULL - resource not found
2253 uint8_t OCGetResourceProperties(OCResourceHandle handle) {
2254 OCResource *resource;
2255 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceProperties"));
2257 resource = findResource((OCResource *) handle);
2259 return resource->resourceProperties;
2265 * Get the number of resource types of the resource.
2267 * @param handle - handle of resource
2268 * @param numResourceTypes - pointer to count variable
2271 * OC_STACK_OK - no errors
2272 * OC_STACK_ERROR - stack process error
2274 OCStackResult OCGetNumberOfResourceTypes(OCResourceHandle handle,
2275 uint8_t *numResourceTypes) {
2276 OCResource *resource;
2277 OCResourceType *pointer;
2279 OC_LOG(INFO, TAG, PCF("Entering OCGetNumberOfResourceTypes"));
2280 VERIFY_NON_NULL(numResourceTypes, ERROR, OC_STACK_INVALID_PARAM);
2281 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
2283 *numResourceTypes = 0;
2285 resource = findResource((OCResource *) handle);
2287 pointer = resource->rsrcType;
2289 *numResourceTypes = *numResourceTypes + 1;
2290 pointer = pointer->next;
2297 * Get name of resource type of the resource.
2299 * @param handle - handle of resource
2300 * @param index - index of resource, 0 to Count - 1
2303 * resource type name - if resource found
2304 * NULL - resource not found
2306 const char *OCGetResourceTypeName(OCResourceHandle handle, uint8_t index) {
2307 OCResourceType *resourceType;
2309 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceTypeName"));
2311 resourceType = findResourceTypeAtIndex(handle, index);
2313 return resourceType->resourcetypename;
2315 return (const char *) NULL;
2321 * Get the number of resource interfaces of the resource.
2323 * @param handle - handle of resource
2324 * @param numResources - pointer to count variable
2327 * OC_STACK_OK - no errors
2328 * OC_STACK_ERROR - stack process error
2330 OCStackResult OCGetNumberOfResourceInterfaces(OCResourceHandle handle,
2331 uint8_t *numResourceInterfaces) {
2332 OCResourceInterface *pointer;
2333 OCResource *resource;
2335 OC_LOG(INFO, TAG, PCF("Entering OCGetNumberOfResourceInterfaces"));
2337 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
2338 VERIFY_NON_NULL(numResourceInterfaces, ERROR, OC_STACK_INVALID_PARAM);
2340 *numResourceInterfaces = 0;
2341 resource = findResource((OCResource *) handle);
2343 pointer = resource->rsrcInterface;
2345 *numResourceInterfaces = *numResourceInterfaces + 1;
2346 pointer = pointer->next;
2353 * Get name of resource interface of the resource.
2355 * @param handle - handle of resource
2356 * @param index - index of resource, 0 to Count - 1
2359 * resource interface name - if resource found
2360 * NULL - resource not found
2362 const char *OCGetResourceInterfaceName(OCResourceHandle handle, uint8_t index) {
2363 OCResourceInterface *resourceInterface;
2365 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceInterfaceName"));
2367 resourceInterface = findResourceInterfaceAtIndex(handle, index);
2368 if (resourceInterface) {
2369 return resourceInterface->name;
2371 return (const char *) NULL;
2375 * Get resource handle from the collection resource by index.
2377 * @param collectionHandle - handle of collection resource
2378 * @param index - index of contained resource, 0 to Count - 1
2381 * handle to resource - if resource found
2382 * NULL - resource not found
2384 OCResourceHandle OCGetResourceHandleFromCollection(OCResourceHandle collectionHandle,
2386 OCResource *resource;
2388 OC_LOG(INFO, TAG, PCF("Entering OCGetContainedResource"));
2390 if (index >= MAX_CONTAINED_RESOURCES) {
2394 resource = findResource((OCResource *) collectionHandle);
2399 return resource->rsrcResources[index];
2403 * Bind an entity handler to the resource.
2405 * @param handle - handle to the resource that the contained resource is to be bound
2406 * @param entityHandler - entity handler function that is called by ocstack to handle requests, etc
2408 * OC_STACK_OK - no errors
2409 * OC_STACK_ERROR - stack process error
2411 OCStackResult OCBindResourceHandler(OCResourceHandle handle,
2412 OCEntityHandler entityHandler) {
2413 OCResource *resource;
2415 OC_LOG(INFO, TAG, PCF("Entering OCBindResourceHandler"));
2417 // Validate parameters
2418 VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
2419 //VERIFY_NON_NULL(entityHandler, ERROR, OC_STACK_INVALID_PARAM);
2421 // Use the handle to find the resource in the resource linked list
2422 resource = findResource((OCResource *)handle);
2424 OC_LOG(ERROR, TAG, PCF("Resource not found"));
2425 return OC_STACK_ERROR;
2429 resource->entityHandler = entityHandler;
2431 #ifdef WITH_PRESENCE
2432 if(presenceResource.handle)
2434 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2435 SendPresenceNotification(resource->rsrcType);
2443 * Get the entity handler for a resource.
2445 * @param handle - handle of resource
2448 * entity handler - if resource found
2449 * NULL - resource not found
2451 OCEntityHandler OCGetResourceHandler(OCResourceHandle handle) {
2452 OCResource *resource;
2454 OC_LOG(INFO, TAG, PCF("Entering OCGetResourceHandler"));
2456 // Use the handle to find the resource in the resource linked list
2457 resource = findResource((OCResource *)handle);
2459 OC_LOG(ERROR, TAG, PCF("Resource not found"));
2464 return resource->entityHandler;
2467 void incrementSequenceNumber(OCResource * resPtr)
2469 // Increment the sequence number
2470 resPtr->sequenceNum += 1;
2471 if (resPtr->sequenceNum == MAX_SEQUENCE_NUMBER)
2473 resPtr->sequenceNum = OC_OFFSET_SEQUENCE_NUMBER+1;
2479 * Notify Presence subscribers that a resource has been modified
2481 * @param resourceType - Handle to the resourceType linked list of resource
2482 * that was modified.
2483 * @param qos - Quality Of Service
2486 #ifdef WITH_PRESENCE
2487 OCStackResult SendPresenceNotification(OCResourceType *resourceType)
2489 OCResource *resPtr = NULL;
2490 OCStackResult result;
2491 OCMethod method = OC_REST_PRESENCE;
2492 uint32_t maxAge = 0;
2493 resPtr = findResource((OCResource *) presenceResource.handle);
2496 return OC_STACK_NO_RESOURCE;
2498 if((((OCResource *) presenceResource.handle)->resourceProperties) & OC_ACTIVE)
2500 maxAge = presenceResource.presenceTTL;
2507 result = SendAllObserverNotification(method, resPtr, maxAge, resourceType, OC_LOW_QOS);
2510 #endif // WITH_PRESENCE
2512 * Notify observers that an observed value has changed.
2514 * @param handle - handle of resource
2517 * OC_STACK_OK - no errors
2518 * OC_STACK_NO_RESOURCE - invalid resource handle
2519 * OC_STACK_NO_OBSERVERS - no more observers intrested in resource
2521 OCStackResult OCNotifyAllObservers(OCResourceHandle handle, OCQualityOfService qos) {
2523 OC_LOG(INFO, TAG, PCF("Entering OCNotifyAllObservers"));
2525 OCResource *resPtr = NULL;
2526 OCStackResult result;
2527 OCMethod method = OC_REST_NOMETHOD;
2528 uint32_t maxAge = 0;
2530 OC_LOG(INFO, TAG, PCF("Entering OCNotifyAllObservers"));
2531 #ifdef WITH_PRESENCE
2532 if(handle == presenceResource.handle)
2536 #endif // WITH_PRESENCE
2537 VERIFY_NON_NULL(handle, ERROR, OC_STACK_ERROR);
2539 // Verify that the resource exists
2540 resPtr = findResource ((OCResource *) handle);
2543 return OC_STACK_NO_RESOURCE;
2547 //only increment in the case of regular observing (not presence)
2548 incrementSequenceNumber(resPtr);
2549 method = OC_REST_OBSERVE;
2550 maxAge = MAX_OBSERVE_AGE;
2551 #ifdef WITH_PRESENCE
2552 result = SendAllObserverNotification (method, resPtr, maxAge, NULL, qos);
2554 result = SendAllObserverNotification (method, resPtr, maxAge, qos);
2561 OCNotifyListOfObservers (OCResourceHandle handle,
2562 OCObservationId *obsIdList,
2563 uint8_t numberOfIds,
2564 unsigned char *notificationJSONPayload,
2565 OCQualityOfService qos)
2567 OC_LOG(INFO, TAG, PCF("Entering OCNotifyListOfObservers"));
2569 OCResource *resPtr = NULL;
2570 //TODO: we should allow the server to define this
2571 uint32_t maxAge = MAX_OBSERVE_AGE;
2573 VERIFY_NON_NULL(handle, ERROR, OC_STACK_ERROR);
2574 VERIFY_NON_NULL(obsIdList, ERROR, OC_STACK_ERROR);
2575 VERIFY_NON_NULL(notificationJSONPayload, ERROR, OC_STACK_ERROR);
2577 // Verify that the resource exists
2578 resPtr = findResource ((OCResource *) handle);
2579 if (NULL == resPtr || myStackMode == OC_CLIENT)
2581 return OC_STACK_NO_RESOURCE;
2585 incrementSequenceNumber(resPtr);
2587 return (SendListObserverNotification(resPtr, obsIdList, numberOfIds,
2588 notificationJSONPayload, maxAge, qos));
2592 * Send a response to a request.
2593 * The response can be a regular, slow, or block (i.e. a response that
2594 * is too large to be sent in a single PDU and must span multiple transmissions)
2596 * @param response - pointer to structure that contains response parameters
2599 * OC_STACK_OK - No errors; Success
2600 * OC_STACK_INVALID_PARAM - Invalid pointer to OCServerResponse
2601 * OC_STACK_INVALID_REQUEST_HANDLE - Request handle not found
2602 * OC_STACK_PERSISTENT_BUFFER_REQUIRED - Block transfer needed for response, so a
2603 * persistent response buffer is necessary
2605 OCStackResult OCDoResponse(OCEntityHandlerResponse *ehResponse)
2607 OCStackResult result = OC_STACK_ERROR;
2608 OCServerRequest *serverRequest = NULL;
2610 OC_LOG(INFO, TAG, PCF("Entering OCDoResponse"));
2612 // Validate input parameters
2613 VERIFY_NON_NULL(ehResponse, ERROR, OC_STACK_INVALID_PARAM);
2614 VERIFY_NON_NULL(ehResponse->requestHandle, ERROR, OC_STACK_INVALID_PARAM);
2616 // TODO: Placeholder for creating a response entry when implementing
2617 // block transfer feature
2619 // If a response payload is present, check if block transfer is required
2620 if (ehResponse->payload && OCIsPacketTransferRequired(NULL,
2621 (const char *)ehResponse->payload, ehResponse->payloadSize))
2623 OC_LOG(INFO, TAG, PCF("Block transfer required"));
2625 // Persistent response buffer is needed for block transfer
2626 if (!ehResponse->persistentBufferFlag)
2628 OC_LOG(WARNING, TAG, PCF("Persistent response buffer required"));
2629 return OC_STACK_PERSISTENT_BUFFER_REQUIRED;
2631 // TODO: Placeholder for block transfer handling
2632 // TODO: Placeholder for setting the the response handle in the OCServerResponse struct
2633 // when implementing the block transfer feature
2639 // Get pointer to request info
2640 serverRequest = GetServerRequestUsingHandle((OCServerRequest *)ehResponse->requestHandle);
2643 result = serverRequest->ehResponseHandler(ehResponse);
2650 * Cancel a response. Applies to a block response
2652 * @param responseHandle - response handle set by stack in OCServerResponse after
2653 * OCDoResponse is called
2656 * OC_STACK_OK - No errors; Success
2657 * OC_STACK_INVALID_PARAM - The handle provided is invalid.
2659 OCStackResult OCCancelResponse(OCResponseHandle responseHandle)
2661 OCStackResult result = OC_STACK_NOTIMPL;
2663 OC_LOG(INFO, TAG, PCF("Entering OCCancelResponse"));
2665 // TODO: validate response handle
2670 //-----------------------------------------------------------------------------
2671 // Private internal function definitions
2672 //-----------------------------------------------------------------------------
2674 * Generate handle of OCDoResource invocation for callback management.
2676 static OCDoHandle GenerateInvocationHandle()
2678 OCDoHandle handle = NULL;
2679 // Generate token here, it will be deleted when the transaction is deleted
2680 handle = (OCDoHandle) OCMalloc(sizeof(uint8_t[MAX_TOKEN_LENGTH]));
2683 OCFillRandomMem((uint8_t*)handle, sizeof(uint8_t[MAX_TOKEN_LENGTH]));
2688 #ifdef WITH_PRESENCE
2689 OCStackResult OCChangeResourceProperty(OCResourceProperty * inputProperty,
2690 OCResourceProperty resourceProperties, uint8_t enable)
2692 if (resourceProperties
2693 > (OC_ACTIVE | OC_DISCOVERABLE | OC_OBSERVABLE | OC_SLOW)) {
2694 OC_LOG(ERROR, TAG, PCF("Invalid property"));
2695 return OC_STACK_INVALID_PARAM;
2699 *inputProperty = (OCResourceProperty) (*inputProperty & ~(resourceProperties));
2703 *inputProperty = (OCResourceProperty) (*inputProperty | resourceProperties);
2710 * Initialize resource data structures, variables, etc.
2712 OCStackResult initResources() {
2713 OCStackResult result = OC_STACK_OK;
2714 // Init application resource vars
2715 headResource = NULL;
2716 // Init Virtual Resources
2717 #ifdef WITH_PRESENCE
2718 presenceResource.presenceTTL = OC_DEFAULT_PRESENCE_TTL;
2719 //presenceResource.token = OCGenerateCoAPToken();
2720 result = OCCreateResource(&presenceResource.handle,
2721 OC_RSRVD_RESOURCE_TYPE_PRESENCE,
2726 //make resource inactive
2727 result = OCChangeResourceProperty(
2728 &(((OCResource *) presenceResource.handle)->resourceProperties),
2735 * Add a resource to the end of the linked list of resources.
2737 * @param resource - resource to be added
2739 void insertResource(OCResource *resource) {
2740 OCResource *pointer;
2742 if (!headResource) {
2743 headResource = resource;
2745 pointer = headResource;
2747 while (pointer->next) {
2748 pointer = pointer->next;
2750 pointer->next = resource;
2752 resource->next = NULL;
2756 * Find a resource in the linked list of resources.
2758 * @param resource - resource to be found
2760 * NULL - resource not found
2761 * pointer to resource - pointer to resource that was found in the linked list
2763 OCResource *findResource(OCResource *resource) {
2764 OCResource *pointer = headResource;
2767 if (pointer == resource) {
2770 pointer = pointer->next;
2775 void deleteAllResources()
2777 OCResource *pointer = headResource;
2782 temp = pointer->next;
2783 #ifdef WITH_PRESENCE
2784 if(pointer != (OCResource *) presenceResource.handle)
2786 #endif // WITH_PRESENCE
2787 deleteResource(pointer);
2788 #ifdef WITH_PRESENCE
2790 #endif // WITH_PRESENCE
2794 #ifdef WITH_PRESENCE
2795 // Ensure that the last resource to be deleted is the presence resource. This allows for all
2796 // presence notification attributed to their deletion to be processed.
2797 deleteResource((OCResource *) presenceResource.handle);
2798 #endif // WITH_PRESENCE
2802 * Delete the resource from the linked list.
2804 * @param resource - resource to be deleted
2809 int deleteResource(OCResource *resource) {
2810 OCResource *prev = NULL;
2813 temp = headResource;
2815 if (temp == resource) {
2816 // Invalidate all Resource Properties.
2817 resource->resourceProperties = (OCResourceProperty) 0;
2818 #ifdef WITH_PRESENCE
2819 if(resource != (OCResource *) presenceResource.handle)
2821 #endif // WITH_PRESENCE
2822 OCNotifyAllObservers((OCResourceHandle)resource, OC_HIGH_QOS);
2823 #ifdef WITH_PRESENCE
2826 if(presenceResource.handle)
2828 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
2829 if(resource != (OCResource *) presenceResource.handle)
2831 SendPresenceNotification(resource->rsrcType);
2835 SendPresenceNotification(NULL);
2840 if (temp == headResource) {
2841 headResource = temp->next;
2843 prev->next = temp->next;
2846 deleteResourceElements(temp);
2859 * Delete all of the dynamically allocated elements that were created for the resource.
2861 * @param resource - specified resource
2863 void deleteResourceElements(OCResource *resource) {
2869 OCFree(resource->uri);
2871 // Delete resourcetype linked list
2872 deleteResourceType(resource->rsrcType);
2874 // Delete resourceinterface linked list
2875 deleteResourceInterface(resource->rsrcInterface);
2879 * Delete all of the dynamically allocated elements that were created for the resource type.
2881 * @param resourceType - specified resource type
2883 void deleteResourceType(OCResourceType *resourceType) {
2884 OCResourceType *pointer = resourceType;
2885 OCResourceType *next;
2888 next = pointer->next;
2889 OCFree(pointer->resourcetypename);
2896 * Delete all of the dynamically allocated elements that were created for the resource interface.
2898 * @param resourceInterface - specified resource interface
2900 void deleteResourceInterface(OCResourceInterface *resourceInterface) {
2901 OCResourceInterface *pointer = resourceInterface;
2902 OCResourceInterface *next;
2905 next = pointer->next;
2906 OCFree(pointer->name);
2913 * Insert a resource type into a resource's resource type linked list.
2915 * @param resource - resource where resource type is to be inserted
2916 * @param resourceType - resource type to be inserted
2918 void insertResourceType(OCResource *resource, OCResourceType *resourceType) {
2919 OCResourceType *pointer;
2921 if (resource && !resource->rsrcType) {
2922 resource->rsrcType = resourceType;
2926 pointer = resource->rsrcType;
2930 pointer = resourceType;
2932 while (pointer->next) {
2933 pointer = pointer->next;
2935 pointer->next = resourceType;
2937 resourceType->next = NULL;
2941 * Get a resource type at the specified index within a resource.
2943 * @param handle - handle of resource
2944 * @param index - index of resource type
2947 * resourcetype - if found
2950 OCResourceType *findResourceTypeAtIndex(OCResourceHandle handle, uint8_t index) {
2951 OCResource *resource;
2952 OCResourceType *pointer;
2955 // Find the specified resource
2956 resource = findResource((OCResource *) handle);
2961 // Make sure a resource has a resourcetype
2962 if (!resource->rsrcType) {
2966 // Iterate through the list
2967 pointer = resource->rsrcType;
2969 while ((i < index) && pointer) {
2971 pointer = pointer->next;
2977 * Finds a resource type in an OCResourceType link-list.
2979 * @param resourceTypeList - the link-list to be searched through
2980 * @param resourceTypeName - the key to search for
2983 * resourceType that matches the key (ie. resourceTypeName)
2984 * NULL - either an invalid parameter or this function was unable to find the key.
2986 OCResourceType *findResourceType(OCResourceType * resourceTypeList, const char * resourceTypeName)
2988 if(resourceTypeList && resourceTypeName)
2990 OCResourceType * rtPointer = resourceTypeList;
2991 while(resourceTypeName && rtPointer)
2993 if(rtPointer->resourcetypename &&
2994 strcmp(resourceTypeName, (const char *)
2995 (rtPointer->resourcetypename)) == 0)
2999 rtPointer = rtPointer->next;
3006 * Insert a resource interface into a resource's resource interface linked list.
3008 * @param resource - resource where resource interface is to be inserted
3009 * @param resourceInterface - resource interface to be inserted
3011 void insertResourceInterface(OCResource *resource,
3012 OCResourceInterface *resourceInterface) {
3013 OCResourceInterface *pointer;
3015 if (!resource->rsrcInterface) {
3016 resource->rsrcInterface = resourceInterface;
3018 pointer = resource->rsrcInterface;
3019 while (pointer->next) {
3020 pointer = pointer->next;
3022 pointer->next = resourceInterface;
3024 resourceInterface->next = NULL;
3028 * Get a resource interface at the specified index within a resource.
3030 * @param handle - handle of resource
3031 * @param index - index of resource interface
3034 * resourceinterface - if found
3037 OCResourceInterface *findResourceInterfaceAtIndex(OCResourceHandle handle,
3039 OCResource *resource;
3040 OCResourceInterface *pointer;
3043 // Find the specified resource
3044 resource = findResource((OCResource *) handle);
3049 // Make sure a resource has a resourceinterface
3050 if (!resource->rsrcInterface) {
3054 // Iterate through the list
3055 pointer = resource->rsrcInterface;
3057 while ((i < index) && pointer) {
3059 pointer = pointer->next;
3065 * Determine if a request/response must be sent in a block transfer because it is too large to be
3066 * sent in a single PDU. This function can be used for either a request or a response
3068 * @param request - NULL or pointer to request
3069 * @param response - NULL or pointer to response
3070 * @param size - 0 or size of the request/response. If 0, strlen is used for determining
3071 * the length of the request/response
3074 * 0 - packet transfer NOT required (i.e. normal request/response)
3075 * 1 - packet transfer required (i.e. block transfer needed)
3077 uint8_t OCIsPacketTransferRequired(const char *request, const char *response, uint16_t size)
3081 // Determine if we are checking a request or a response
3084 // If size is greater than 0, use it for the request size value, otherwise
3085 // assume request is null terminated and use strlen for size value
3086 if ((size > MAX_REQUEST_LENGTH) || (strlen(request) > MAX_REQUEST_LENGTH))
3093 // If size is greater than 0, use it for the response size value, otherwise
3094 // assume response is null terminated and use strlen for size value
3095 if ((size > MAX_RESPONSE_LENGTH) || (strlen(response) > MAX_RESPONSE_LENGTH))
3104 * Retrieves a resource type based upon a uri string if the uri string contains only just one
3105 * resource attribute (and that has to be of type "rt").
3107 * @remark This API malloc's memory for the resource type and newURI. Do not malloc resourceType
3108 * or newURI before passing in.
3110 * @param uri - Valid URI for "requiredUri" parameter to OCDoResource API.
3111 * @param resourceType - The resource type to be populated; pass by reference.
3112 * @param newURI - Return URI without resourceType appended to the end of it. This is used to
3113 * ensure that the uri parameter is not modified; pass by reference.
3116 * OC_STACK_INVALID_URI - Returns this if the URI is invalid/NULL.
3117 * OC_STACK_INVALID_PARAM - Returns this if the resourceType parameter is invalid/NULL.
3118 * OC_STACK_OK - Success
3120 OCStackResult getResourceType(const char * uri, unsigned char** resourceType, char ** newURI)
3124 return OC_STACK_INVALID_URI;
3126 if(!resourceType || !newURI)
3128 return OC_STACK_INVALID_PARAM;
3130 char * leftToken = NULL;
3131 char * tempURI = (char *) OCMalloc(strlen(uri) + 1);
3136 strcpy(tempURI, uri);
3138 leftToken = strtok_r((char *)tempURI, "?", &strTokPtr);
3140 while(leftToken != NULL)
3142 if(strncmp(leftToken, "rt=", 3) == 0)
3144 *resourceType = (unsigned char *) OCMalloc(strlen(leftToken)-3);
3149 strcpy((char *)*resourceType, ((const char *)&leftToken[3]));
3152 leftToken = strtok_r(NULL, "?", &strTokPtr);
3160 return OC_STACK_NO_MEMORY;
3163 const ServerID OCGetServerInstanceID(void)
3165 static bool generated = false;
3166 static ServerID sid;
3173 sid = OCGetRandom();
3178 const char* OCGetServerInstanceIDString(void)
3180 // max printed length of a base 10
3181 // uint32 is 10 characters, so 11 includes null.
3182 // This will change as the representation gets switched
3184 static char buffer[11];
3185 int n = sprintf(buffer, "%u", OCGetServerInstanceID());