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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 #include "ocstackconfig.h"
24 #include "ocstackinternal.h"
25 #include "ocobserve.h"
26 #include "ocresourcehandler.h"
29 #include "ocserverrequest.h"
36 #define MOD_NAME PCF("ocobserve")
38 #define TAG PCF("OCStackObserve")
40 #define VERIFY_NON_NULL(arg) { if (!arg) {OC_LOG(FATAL, TAG, #arg " is NULL"); goto exit;} }
42 static struct ResourceObserver * serverObsList = NULL;
44 static char* GetJSONStringForPresence(uint32_t ttl, uint32_t nonce,
45 OCPresenceTrigger trigger, OCResourceType *resourceType)
47 cJSON *rootObj = cJSON_CreateObject();
53 char *jsonEncodedInfo = NULL;
54 const char * triggerStr = NULL;
55 cJSON_AddItemToObject (rootObj, OC_RSRVD_TTL, cJSON_CreateNumber(ttl));
57 cJSON_AddItemToObject (rootObj, OC_RSRVD_NONCE, cJSON_CreateNumber(nonce));
59 triggerStr = convertTriggerEnumToString(trigger);
60 cJSON_AddItemToObject (rootObj, OC_RSRVD_TRIGGER, cJSON_CreateString(triggerStr));
62 if(resourceType && resourceType->resourcetypename)
64 cJSON_AddItemToObject (rootObj, OC_RSRVD_RESOURCE_TYPE,
65 cJSON_CreateString(resourceType->resourcetypename));
68 jsonEncodedInfo = cJSON_PrintUnformatted (rootObj);
71 cJSON_Delete(rootObj);
73 return jsonEncodedInfo;
77 static OCStackResult BuildPresenceResponse(char *out, uint16_t *remaining,
78 uint32_t ttl, uint32_t nonce, OCPresenceTrigger trigger,
79 OCResourceType *resourceType)
81 if(!out || !remaining)
83 return OC_STACK_INVALID_PARAM;
86 OCStackResult ret = OC_STACK_ERROR;
90 jsonStr = GetJSONStringForPresence(ttl, nonce, trigger, resourceType);
94 jsonLen = strlen(jsonStr);
96 if (jsonLen < *remaining)
98 strncpy(out, jsonStr, (jsonLen + 1));
99 *remaining = *remaining - jsonLen;
104 ret = OC_STACK_ERROR;
111 OC_LOG(ERROR, TAG, PCF("Error encoding presence payload."));
112 ret = OC_STACK_ERROR;
116 #endif // WITH_PRESENCE
118 * Determine observe QOS based on the QOS of the request.
119 * The qos passed as a parameter overrides what the client requested.
120 * If we want the client preference taking high priority make:
121 * qos = resourceObserver->qos;
123 * @param method RESTful method.
124 * @param resourceObserver Observer.
125 * @param appQoS Quality of service.
126 * @return The quality of service of the observer.
128 static OCQualityOfService DetermineObserverQoS(OCMethod method,
129 ResourceObserver * resourceObserver, OCQualityOfService appQoS)
131 if(!resourceObserver)
133 OC_LOG(ERROR, TAG, "DetermineObserverQoS called with invalid resourceObserver");
137 OCQualityOfService decidedQoS = appQoS;
138 if(appQoS == OC_NA_QOS)
140 decidedQoS = resourceObserver->qos;
143 if(appQoS != OC_HIGH_QOS)
145 OC_LOG_V(INFO, TAG, "Current NON count for this observer is %d",
146 resourceObserver->lowQosCount);
148 if((resourceObserver->forceHighQos \
149 || resourceObserver->lowQosCount >= MAX_OBSERVER_NON_COUNT) \
150 && method != OC_REST_PRESENCE)
152 if(resourceObserver->forceHighQos \
153 || resourceObserver->lowQosCount >= MAX_OBSERVER_NON_COUNT)
156 resourceObserver->lowQosCount = 0;
157 // at some point we have to to send CON to check on the
158 // availability of observer
159 OC_LOG(INFO, TAG, PCF("This time we are sending the notification as High qos"));
160 decidedQoS = OC_HIGH_QOS;
164 (resourceObserver->lowQosCount)++;
171 OCStackResult SendAllObserverNotification (OCMethod method, OCResource *resPtr, uint32_t maxAge,
172 OCPresenceTrigger trigger, OCResourceType *resourceType, OCQualityOfService qos)
174 OCStackResult SendAllObserverNotification (OCMethod method, OCResource *resPtr, uint32_t maxAge,
175 OCQualityOfService qos)
178 OC_LOG(INFO, TAG, PCF("Entering SendObserverNotification"));
181 return OC_STACK_INVALID_PARAM;
184 OCStackResult result = OC_STACK_ERROR;
185 ResourceObserver * resourceObserver = serverObsList;
187 OCServerRequest * request = NULL;
188 OCEntityHandlerRequest ehRequest = {};
189 OCEntityHandlerResult ehResult = OC_EH_ERROR;
190 bool observeErrorFlag = false;
192 // Find clients that are observing this resource
193 while (resourceObserver)
195 if (resourceObserver->resource == resPtr)
199 if(method != OC_REST_PRESENCE)
202 qos = DetermineObserverQoS(method, resourceObserver, qos);
204 result = AddServerRequest(&request, 0, 0, 0, 1, OC_REST_GET,
205 0, resPtr->sequenceNum, qos, resourceObserver->query,
207 resourceObserver->token, resourceObserver->tokenLength,
208 resourceObserver->resUri, 0,
209 &(resourceObserver->addressInfo), resourceObserver->connectivityType);
213 request->observeResult = OC_STACK_OK;
214 if(result == OC_STACK_OK)
216 result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) request,
217 request->method, (OCResourceHandle) resPtr, request->query,
218 request->reqJSONPayload,
219 request->numRcvdVendorSpecificHeaderOptions,
220 request->rcvdVendorSpecificHeaderOptions,
221 OC_OBSERVE_NO_OPTION, 0);
222 if(result == OC_STACK_OK)
224 ehResult = resPtr->entityHandler(OC_REQUEST_FLAG, &ehRequest);
225 if(ehResult == OC_EH_ERROR)
227 FindAndDeleteServerRequest(request);
236 OCEntityHandlerResponse ehResponse = {};
237 char presenceResBuf[MAX_RESPONSE_LENGTH] = {};
239 //This is effectively the implementation for the presence entity handler.
240 OC_LOG(DEBUG, TAG, PCF("This notification is for Presence"));
242 result = AddServerRequest(&request, 0, 0, 0, 1, OC_REST_GET,
243 0, resPtr->sequenceNum, qos, resourceObserver->query,
245 resourceObserver->token, resourceObserver->tokenLength,
246 resourceObserver->resUri, 0,
247 &(resourceObserver->addressInfo), resourceObserver->connectivityType);
249 if(result == OC_STACK_OK)
251 uint16_t remaining = MAX_RESPONSE_LENGTH;
252 // create the payload here
253 result = BuildPresenceResponse(presenceResBuf, &remaining,
254 maxAge, resPtr->sequenceNum, trigger,
257 if(result == OC_STACK_OK && remaining < MAX_RESPONSE_LENGTH)
259 ehResponse.ehResult = OC_EH_OK;
260 ehResponse.payload = presenceResBuf;
261 ehResponse.payloadSize = strlen((const char *)presenceResBuf) + 1;
262 ehResponse.persistentBufferFlag = 0;
263 ehResponse.requestHandle = (OCRequestHandle) request;
264 ehResponse.resourceHandle = (OCResourceHandle) resPtr;
265 strcpy((char *)ehResponse.resourceUri,
266 (const char *)resourceObserver->resUri);
267 result = OCDoResponse(&ehResponse);
273 // Since we are in a loop, set an error flag to indicate at least one error occurred.
274 if (result != OC_STACK_OK)
276 observeErrorFlag = true;
279 resourceObserver = resourceObserver->next;
284 OC_LOG(INFO, TAG, PCF("Resource has no observers"));
285 result = OC_STACK_NO_OBSERVERS;
287 else if (observeErrorFlag)
289 OC_LOG(ERROR, TAG, PCF("Observer notification error"));
290 result = OC_STACK_ERROR;
295 OCStackResult SendListObserverNotification (OCResource * resource,
296 OCObservationId *obsIdList, uint8_t numberOfIds,
297 const char *notificationJSONPayload, uint32_t maxAge,
298 OCQualityOfService qos)
300 if(!resource || !obsIdList || !notificationJSONPayload)
302 return OC_STACK_INVALID_PARAM;
305 uint8_t numIds = numberOfIds;
306 ResourceObserver *observer = NULL;
307 uint8_t numSentNotification = 0;
308 OCServerRequest * request = NULL;
309 OCStackResult result = OC_STACK_ERROR;
310 bool observeErrorFlag = false;
312 OC_LOG(INFO, TAG, PCF("Entering SendListObserverNotification"));
315 observer = GetObserverUsingId (*obsIdList);
318 // Found observer - verify if it matches the resource handle
319 if (observer->resource == resource)
321 qos = DetermineObserverQoS(OC_REST_GET, observer, qos);
324 result = AddServerRequest(&request, 0, 0, 0, 1, OC_REST_GET,
325 0, resource->sequenceNum, qos, observer->query,
326 NULL, NULL, observer->token, observer->tokenLength,
328 &(observer->addressInfo), observer->connectivityType);
332 request->observeResult = OC_STACK_OK;
333 if(result == OC_STACK_OK)
335 OCEntityHandlerResponse ehResponse = {};
336 ehResponse.ehResult = OC_EH_OK;
337 ehResponse.payload = (char *) OCMalloc(MAX_RESPONSE_LENGTH + 1);
338 if(!ehResponse.payload)
340 FindAndDeleteServerRequest(request);
343 strncpy(ehResponse.payload, notificationJSONPayload, MAX_RESPONSE_LENGTH-1);
344 ehResponse.payload[MAX_RESPONSE_LENGTH] = '\0';
345 ehResponse.payloadSize = strlen(ehResponse.payload) + 1;
346 ehResponse.persistentBufferFlag = 0;
347 ehResponse.requestHandle = (OCRequestHandle) request;
348 ehResponse.resourceHandle = (OCResourceHandle) resource;
349 result = OCDoResponse(&ehResponse);
350 if(result == OC_STACK_OK)
352 OC_LOG_V(INFO, TAG, "Observer id %d notified.", *obsIdList);
354 // Increment only if OCDoResponse is successful
355 numSentNotification++;
357 OCFree(ehResponse.payload);
358 FindAndDeleteServerRequest(request);
362 OC_LOG_V(INFO, TAG, "Error notifying observer id %d.", *obsIdList);
367 FindAndDeleteServerRequest(request);
370 // Since we are in a loop, set an error flag to indicate
371 // at least one error occurred.
372 if (result != OC_STACK_OK)
374 observeErrorFlag = true;
382 if(numSentNotification == numberOfIds && !observeErrorFlag)
386 else if(numSentNotification == 0)
388 return OC_STACK_NO_OBSERVERS;
392 OC_LOG(ERROR, TAG, PCF("Observer notification error"));
393 return OC_STACK_ERROR;
397 OCStackResult GenerateObserverId (OCObservationId *observationId)
399 ResourceObserver *resObs = NULL;
401 OC_LOG(INFO, TAG, PCF("Entering GenerateObserverId"));
402 VERIFY_NON_NULL (observationId);
406 *observationId = OCGetRandomByte();
407 // Check if observation Id already exists
408 resObs = GetObserverUsingId (*observationId);
409 } while (NULL != resObs);
411 OC_LOG_V(INFO, TAG, "Generated bservation ID is %u", *observationId);
415 return OC_STACK_ERROR;
418 OCStackResult AddObserver (const char *resUri,
420 OCObservationId obsId,
423 OCResource *resHandle,
424 OCQualityOfService qos,
425 const CAAddress_t *addressInfo,
426 CATransportType_t connectivityType)
428 // Check if resource exists and is observable.
431 return OC_STACK_INVALID_PARAM;
433 if (!(resHandle->resourceProperties & OC_OBSERVABLE))
435 return OC_STACK_RESOURCE_ERROR;
437 ResourceObserver *obsNode = NULL;
439 if(!resUri || !token || !*token)
441 return OC_STACK_INVALID_PARAM;
444 obsNode = (ResourceObserver *) OCCalloc(1, sizeof(ResourceObserver));
447 obsNode->observeId = obsId;
449 obsNode->resUri = (char *)OCMalloc(strlen(resUri)+1);
450 VERIFY_NON_NULL (obsNode->resUri);
451 memcpy (obsNode->resUri, resUri, strlen(resUri)+1);
456 obsNode->query = (char *)OCMalloc(strlen(query)+1);
457 VERIFY_NON_NULL (obsNode->query);
458 memcpy (obsNode->query, query, strlen(query)+1);
460 // If tokenLength is zero, the return value depends on the
461 // particular library implementation (it may or may not be a null pointer).
464 obsNode->token = (CAToken_t)OCMalloc(tokenLength);
465 VERIFY_NON_NULL (obsNode->token);
466 memcpy(obsNode->token, token, tokenLength);
468 obsNode->tokenLength = tokenLength;
469 obsNode->addressInfo = *addressInfo;
470 obsNode->connectivityType = connectivityType;
471 obsNode->resource = resHandle;
472 LL_APPEND (serverObsList, obsNode);
479 OCFree(obsNode->resUri);
480 OCFree(obsNode->query);
483 return OC_STACK_NO_MEMORY;
486 ResourceObserver* GetObserverUsingId (const OCObservationId observeId)
488 ResourceObserver *out = NULL;
492 LL_FOREACH (serverObsList, out)
494 if (out->observeId == observeId)
500 OC_LOG(INFO, TAG, PCF("Observer node not found!!"));
504 ResourceObserver* GetObserverUsingToken (const CAToken_t token, uint8_t tokenLength)
506 ResourceObserver *out = NULL;
510 LL_FOREACH (serverObsList, out)
512 OC_LOG(INFO, TAG,PCF("comparing tokens"));
513 OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)token, tokenLength);
514 OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)out->token, tokenLength);
515 if((memcmp(out->token, token, tokenLength) == 0))
521 OC_LOG(INFO, TAG, PCF("Observer node not found!!"));
525 OCStackResult DeleteObserverUsingToken (CAToken_t token, uint8_t tokenLength)
527 if(!token || !*token)
529 return OC_STACK_INVALID_PARAM;
532 ResourceObserver *obsNode = NULL;
534 obsNode = GetObserverUsingToken (token, tokenLength);
537 OC_LOG_V(INFO, TAG, PCF("deleting tokens"));
538 OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)obsNode->token, tokenLength);
539 LL_DELETE (serverObsList, obsNode);
540 OCFree(obsNode->resUri);
541 OCFree(obsNode->query);
542 OCFree(obsNode->token);
545 // it is ok if we did not find the observer...
549 void DeleteObserverList()
551 ResourceObserver *out = NULL;
552 ResourceObserver *tmp = NULL;
553 LL_FOREACH_SAFE (serverObsList, out, tmp)
557 DeleteObserverUsingToken ((out->token), out->tokenLength);
560 serverObsList = NULL;
564 * CA layer expects observe registration/de-reg/notiifcations to be passed as a header
565 * option, which breaks the protocol abstraction requirement between RI & CA, and
566 * has to be fixed in the future. The function below adds the header option for observe.
567 * It should be noted that the observe header option is assumed to be the first option
568 * in the list of user defined header options and hence it is inserted at the front
569 * of the header options list and number of options adjusted accordingly.
572 CreateObserveHeaderOption (CAHeaderOption_t **caHdrOpt,
573 OCHeaderOption *ocHdrOpt,
579 return OC_STACK_INVALID_PARAM;
582 CAHeaderOption_t *tmpHdrOpt = NULL;
584 tmpHdrOpt = (CAHeaderOption_t *) OCCalloc ((numOptions+1), sizeof(CAHeaderOption_t));
585 if (NULL == tmpHdrOpt)
587 return OC_STACK_NO_MEMORY;
589 tmpHdrOpt[0].protocolID = CA_COAP_ID;
590 tmpHdrOpt[0].optionID = COAP_OPTION_OBSERVE;
591 tmpHdrOpt[0].optionLength = sizeof(uint32_t);
592 tmpHdrOpt[0].optionData[0] = observeFlag;
593 for (uint8_t i = 0; i < numOptions; i++)
595 memcpy (&(tmpHdrOpt[i+1]), &(ocHdrOpt[i]), sizeof(CAHeaderOption_t));
598 *caHdrOpt = tmpHdrOpt;
603 * CA layer passes observe information to the RI layer as a header option, which
604 * breaks the protocol abstraction requirement between RI & CA, and has to be fixed
605 * in the future. The function below removes the observe header option and processes it.
606 * It should be noted that the observe header option is always assumed to be the first
607 * option in the list of user defined header options and hence it is deleted from the
608 * front of the header options list and the number of options is adjusted accordingly.
611 GetObserveHeaderOption (uint32_t * observationOption,
612 CAHeaderOption_t *options,
613 uint8_t * numOptions)
615 if(!observationOption)
617 return OC_STACK_INVALID_PARAM;
619 *observationOption = OC_OBSERVE_NO_OPTION;
621 if(!options || !numOptions)
623 return OC_STACK_INVALID_PARAM;
626 for(uint8_t i = 0; i < *numOptions; i++)
628 if(options[i].protocolID == CA_COAP_ID &&
629 options[i].optionID == COAP_OPTION_OBSERVE)
631 *observationOption = options[i].optionData[0];
632 for(uint8_t c = i; c < *numOptions-1; c++)
634 options[i].protocolID = options[i+1].protocolID;
635 options[i].optionID = options[i+1].optionID;
636 options[i].optionLength = options[i+1].optionLength;
637 memcpy(options[i].optionData, options[i+1].optionData, options[i+1].optionLength);