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"
32 #include "ocserverrequest.h"
35 #define MOD_NAME PCF("ocobserve")
37 #define TAG PCF("OCStackObserve")
39 #define VERIFY_NON_NULL(arg) { if (!arg) {OC_LOG(FATAL, TAG, #arg " is NULL"); goto exit;} }
41 static struct ResourceObserver * serverObsList = NULL;
43 // send notifications based on the qos of the request
44 // The qos passed as a parameter overrides what the client requested
45 // If we want the client preference taking high priority make:
46 // qos = resourceObserver->qos;
47 OCQualityOfService DetermineObserverQoS(OCMethod method, ResourceObserver * resourceObserver,
48 OCQualityOfService appQoS)
50 OCQualityOfService decidedQoS = appQoS;
51 if(appQoS == OC_NA_QOS)
53 decidedQoS = resourceObserver->qos;
56 if(appQoS != OC_HIGH_QOS)
58 OC_LOG_V(INFO, TAG, "Current NON count for this observer is %d",
59 resourceObserver->lowQosCount);
61 if((resourceObserver->forceHighQos \
62 || resourceObserver->lowQosCount >= MAX_OBSERVER_NON_COUNT) \
63 && method != OC_REST_PRESENCE)
65 if(resourceObserver->forceHighQos \
66 || resourceObserver->lowQosCount >= MAX_OBSERVER_NON_COUNT)
69 resourceObserver->lowQosCount = 0;
70 // at some point we have to to send CON to check on the
71 // availability of observer
72 OC_LOG(INFO, TAG, PCF("This time we are sending the notification as High qos"));
73 decidedQoS = OC_HIGH_QOS;
77 (resourceObserver->lowQosCount)++;
84 OCStackResult SendAllObserverNotification (OCMethod method, OCResource *resPtr, uint32_t maxAge,
85 OCResourceType *resourceType, OCQualityOfService qos)
87 OCStackResult SendAllObserverNotification (OCMethod method, OCResource *resPtr, uint32_t maxAge,
88 OCQualityOfService qos)
91 OC_LOG(INFO, TAG, PCF("Entering SendObserverNotification"));
92 OCStackResult result = OC_STACK_ERROR;
93 ResourceObserver * resourceObserver = serverObsList;
95 OCServerRequest * request = NULL;
96 OCEntityHandlerRequest ehRequest = {0};
97 OCEntityHandlerResult ehResult = OC_EH_ERROR;
99 // Find clients that are observing this resource
100 while (resourceObserver)
102 if (resourceObserver->resource == resPtr)
106 if(method != OC_REST_PRESENCE)
109 qos = DetermineObserverQoS(method, resourceObserver, qos);
112 result = AddServerCARequest(&request, 0, 0, 0, 1, OC_REST_GET,
113 0, resPtr->sequenceNum, qos, resourceObserver->query,
115 &resourceObserver->token, resourceObserver->addr,
116 resourceObserver->resUri, 0,
117 &(resourceObserver->addressInfo), resourceObserver->connectivityType,
118 resourceObserver->CAToken);
120 result = AddServerRequest(&request, 0, 0, 0, 1, OC_REST_GET,
121 0, resPtr->sequenceNum, qos, resourceObserver->query,
123 &resourceObserver->token, resourceObserver->addr,
124 resourceObserver->resUri, 0);
127 request->observeResult = OC_STACK_OK;
128 if(request && result == OC_STACK_OK)
130 result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) request,
131 request->method, (OCResourceHandle) resPtr, request->query,
132 request->reqJSONPayload, request->numRcvdVendorSpecificHeaderOptions,
133 request->rcvdVendorSpecificHeaderOptions, OC_OBSERVE_NO_OPTION, 0);
134 if(result == OC_STACK_OK)
136 ehResult = resPtr->entityHandler(OC_REQUEST_FLAG, &ehRequest);
137 if(ehResult == OC_EH_ERROR)
139 FindAndDeleteServerRequest(request);
147 OCEntityHandlerResponse ehResponse = {0};
148 unsigned char presenceResBuf[MAX_RESPONSE_LENGTH] = {0};
149 //This is effectively the implementation for the presence entity handler.
150 OC_LOG(DEBUG, TAG, PCF("This notification is for Presence"));
151 result = AddServerRequest(&request, 0, 0, 0, 1, OC_REST_GET,
152 0, OC_OBSERVE_NO_OPTION, OC_LOW_QOS,
153 NULL, NULL, NULL, &resourceObserver->token,
154 resourceObserver->addr, resourceObserver->resUri, 0);
155 if(result == OC_STACK_OK)
157 // we create the payload here
158 if(resourceType && resourceType->resourcetypename)
160 snprintf((char *)presenceResBuf, sizeof(presenceResBuf), "%u:%u:%s",
161 resPtr->sequenceNum, maxAge, resourceType->resourcetypename);
165 snprintf((char *)presenceResBuf, sizeof(presenceResBuf), "%u:%u",
166 resPtr->sequenceNum, maxAge);
168 ehResponse.ehResult = OC_EH_OK;
169 ehResponse.payload = presenceResBuf;
170 ehResponse.payloadSize = strlen((const char *)presenceResBuf) + 1;
171 ehResponse.persistentBufferFlag = 0;
172 ehResponse.requestHandle = (OCRequestHandle) request;
173 ehResponse.resourceHandle = (OCResourceHandle) resPtr;
174 strcpy((char *)ehResponse.resourceUri, (const char *)resourceObserver->resUri);
175 result = OCDoResponse(&ehResponse);
180 resourceObserver = resourceObserver->next;
184 OC_LOG(INFO, TAG, PCF("Resource has no observers"));
185 result = OC_STACK_NO_OBSERVERS;
190 OCStackResult SendListObserverNotification (OCResource * resource,
191 OCObservationId *obsIdList, uint8_t numberOfIds,
192 unsigned char *notificationJSONPayload, uint32_t maxAge,
193 OCQualityOfService qos)
195 uint8_t numIds = numberOfIds;
196 ResourceObserver *observation = NULL;
197 uint8_t numSentNotification = 0;
198 OCServerRequest * request = NULL;
199 OCStackResult result = OC_STACK_ERROR;
200 OCEntityHandlerResponse ehResponse = {0};
202 OC_LOG(INFO, TAG, PCF("Entering SendListObserverNotification"));
205 OC_LOG_V(INFO, TAG, "Need to notify observation id %d", *obsIdList);
207 observation = GetObserverUsingId (*obsIdList);
210 // Found observation - verify if it matches the resource handle
211 if (observation->resource == resource)
213 qos = DetermineObserverQoS(OC_REST_GET, observation, qos);
217 result = AddServerCARequest(&request, 0, 0, 0, 1, OC_REST_GET,
218 0, resource->sequenceNum, qos, observation->query,
219 NULL, NULL, &observation->token,
220 observation->addr, observation->resUri, 0,
221 &(observation->addressInfo), observation->connectivityType,
222 observation->CAToken);
224 result = AddServerRequest(&request, 0, 0, 0, 1, OC_REST_GET,
225 0, resource->sequenceNum, qos, observation->query,
226 NULL, NULL, &observation->token,
227 observation->addr, observation->resUri, 0);
230 request->observeResult = OC_STACK_OK;
231 if(request && result == OC_STACK_OK)
233 memset(&ehResponse, 0, sizeof(OCEntityHandlerResponse));
234 ehResponse.ehResult = OC_EH_OK;
235 ehResponse.payload = (unsigned char *) OCMalloc(MAX_RESPONSE_LENGTH);
236 if(!ehResponse.payload)
238 FindAndDeleteServerRequest(request);
241 strcpy((char *)ehResponse.payload, (const char *)notificationJSONPayload);
242 ehResponse.payloadSize = strlen((const char *)ehResponse.payload) + 1;
243 ehResponse.persistentBufferFlag = 0;
244 ehResponse.requestHandle = (OCRequestHandle) request;
245 ehResponse.resourceHandle = (OCResourceHandle) resource;
246 result = OCDoResponse(&ehResponse);
247 if(result == OC_STACK_OK)
249 OCFree(ehResponse.payload);
250 FindAndDeleteServerRequest(request);
255 FindAndDeleteServerRequest(request);
258 numSentNotification++;
264 if(numSentNotification == numberOfIds)
268 else if(numSentNotification == 0)
270 return OC_STACK_NO_OBSERVERS;
274 //TODO: we need to signal that not every one in the
275 // list got an update, should we also indicate who did not receive on?
280 OCStackResult GenerateObserverId (OCObservationId *observationId)
282 ResourceObserver *resObs = NULL;
284 OC_LOG(INFO, TAG, PCF("Entering GenerateObserverId"));
285 VERIFY_NON_NULL (observationId);
289 *observationId = OCGetRandomByte();
290 // Check if observation Id already exists
291 resObs = GetObserverUsingId (*observationId);
292 } while (NULL != resObs);
294 OC_LOG_V(INFO, TAG, "Observation ID is %u", *observationId);
298 return OC_STACK_ERROR;
302 OCStackResult AddCAObserver (const char *resUri,
304 OCObservationId obsId,
307 OCResource *resHandle,
308 OCQualityOfService qos,
309 CAAddress_t *addressInfo,
310 CAConnectivityType_t connectivityType,
313 ResourceObserver *obsNode = NULL;
315 obsNode = (ResourceObserver *) OCCalloc(1, sizeof(ResourceObserver));
318 obsNode->observeId = obsId;
320 obsNode->resUri = (unsigned char *)OCMalloc(strlen(resUri)+1);
321 VERIFY_NON_NULL (obsNode->resUri);
322 memcpy (obsNode->resUri, resUri, strlen(resUri)+1);
327 obsNode->query = (unsigned char *)OCMalloc(strlen(query)+1);
328 VERIFY_NON_NULL (obsNode->query);
329 memcpy (obsNode->query, query, strlen(query)+1);
334 obsNode->token.tokenLength = token->tokenLength;
335 memcpy (obsNode->token.token, token->token, token->tokenLength);
340 obsNode->addr = (OCDevAddr *)OCMalloc(sizeof(OCDevAddr));
341 VERIFY_NON_NULL (obsNode->addr);
342 memcpy (obsNode->addr, addr, sizeof(OCDevAddr));
345 obsNode->addressInfo = *addressInfo;
346 obsNode->connectivityType = connectivityType;
349 strncpy(obsNode->CAToken, CAtoken, CA_MAX_TOKEN_LEN);
352 obsNode->resource = resHandle;
354 LL_APPEND (serverObsList, obsNode);
361 OCFree(obsNode->resUri);
362 OCFree(obsNode->query);
363 OCFree(obsNode->addr);
366 return OC_STACK_NO_MEMORY;
370 OCStackResult AddObserver (const char *resUri,
372 OCObservationId obsId,
375 OCResource *resHandle,
376 OCQualityOfService qos)
378 ResourceObserver *obsNode = NULL;
380 obsNode = (ResourceObserver *) OCCalloc(1, sizeof(ResourceObserver));
383 obsNode->observeId = obsId;
385 obsNode->resUri = (unsigned char *)OCMalloc(strlen(resUri)+1);
386 VERIFY_NON_NULL (obsNode->resUri);
387 memcpy (obsNode->resUri, resUri, strlen(resUri)+1);
392 obsNode->query = (unsigned char *)OCMalloc(strlen(query)+1);
393 VERIFY_NON_NULL (obsNode->query);
394 memcpy (obsNode->query, query, strlen(query)+1);
397 obsNode->token.tokenLength = token->tokenLength;
398 memcpy (obsNode->token.token, token->token, token->tokenLength);
400 obsNode->addr = (OCDevAddr *)OCMalloc(sizeof(OCDevAddr));
401 VERIFY_NON_NULL (obsNode->addr);
402 memcpy (obsNode->addr, addr, sizeof(OCDevAddr));
404 obsNode->resource = resHandle;
406 LL_APPEND (serverObsList, obsNode);
413 OCFree(obsNode->resUri);
414 OCFree(obsNode->query);
415 OCFree(obsNode->addr);
418 return OC_STACK_NO_MEMORY;
421 ResourceObserver* GetObserverUsingId (const OCObservationId observeId)
423 ResourceObserver *out = NULL;
427 LL_FOREACH (serverObsList, out)
429 if (out->observeId == observeId)
435 OC_LOG(INFO, TAG, PCF("Observer node not found!!"));
440 ResourceObserver* GetObserverUsingToken (const char * token)
442 ResourceObserver* GetObserverUsingToken (const OCCoAPToken * token)
445 ResourceObserver *out = NULL;
449 LL_FOREACH (serverObsList, out)
452 if((strlen(token) == strlen(out->CAToken)) &&
453 (memcmp(out->CAToken, token, strlen(token)) == 0))
458 OC_LOG(INFO, TAG,PCF("comparing tokens"));
459 OC_LOG_BUFFER(INFO, TAG, token->token, token->tokenLength);
460 OC_LOG_BUFFER(INFO, TAG, out->token.token, out->token.tokenLength);
461 if((out->token.tokenLength == token->tokenLength) &&
462 (memcmp(out->token.token, token->token, token->tokenLength) == 0))
469 OC_LOG(INFO, TAG, PCF("Observer node not found!!"));
474 OCStackResult DeleteObserverUsingToken (char * token)
476 OCStackResult DeleteObserverUsingToken (OCCoAPToken * token)
479 ResourceObserver *obsNode = NULL;
481 obsNode = GetObserverUsingToken (token);
484 OC_LOG_V(INFO, TAG, PCF("deleting tokens"));
485 OC_LOG_BUFFER(INFO, TAG, obsNode->token.token, obsNode->token.tokenLength);
486 LL_DELETE (serverObsList, obsNode);
487 OCFree(obsNode->resUri);
488 OCFree(obsNode->query);
489 OCFree(obsNode->addr);
492 // it is ok if we did not find the observer...
496 void DeleteObserverList()
498 ResourceObserver *out = NULL;
499 ResourceObserver *tmp = NULL;
500 LL_FOREACH_SAFE (serverObsList, out, tmp)
503 DeleteObserverUsingToken (out->CAToken);
505 DeleteObserverUsingToken (&(out->token));
508 serverObsList = NULL;
513 CreateObserveHeaderOption (CAHeaderOption_t **caHdrOpt,
514 OCHeaderOption *ocHdrOpt,
518 CAHeaderOption_t *tmpHdrOpt = NULL;
520 tmpHdrOpt = (CAHeaderOption_t *) OCMalloc ((numOptions+1)*sizeof(CAHeaderOption_t));
521 if (NULL == tmpHdrOpt)
523 return OC_STACK_NO_MEMORY;
525 tmpHdrOpt[0].protocolID = CA_COAP_ID;
526 // TODO-CA: COAP_OPTION_OBSERVE is defined in CoAP header files which will be abstracted
527 // from resource model. We have to define a new macro for this in the stack layer.
528 tmpHdrOpt[0].optionID = COAP_OPTION_OBSERVE;
529 // Length is one byte
530 tmpHdrOpt[0].optionLength = 1;
531 tmpHdrOpt[0].optionData[0] = observeFlag;
532 for (uint8_t i = 0; i < numOptions; i++)
534 memcpy (&(tmpHdrOpt[i+1]), &(ocHdrOpt[i]), sizeof(CAHeaderOption_t));
537 *caHdrOpt = tmpHdrOpt;
542 GetObserveHeaderOption (uint32_t * observationOption,
543 CAHeaderOption_t *options,
544 uint8_t * numOptions)
546 *observationOption = OC_OBSERVE_NO_OPTION;
549 for(i = 0; i < *numOptions; i++)
551 if(options[i].protocolID == CA_COAP_ID &&
552 options[i].optionID == COAP_OPTION_OBSERVE)
554 *observationOption = options[i].optionData[0];
555 for(c = i; c < *numOptions-1; c++)
557 options[i].protocolID = options[i+1].protocolID;
558 options[i].optionID = options[i+1].optionID;
559 options[i].optionLength = options[i+1].optionLength;
560 memcpy(options[i].optionData, options[i+1].optionData, options[i+1].optionLength);