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 = AddServerRequest(&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);
119 result = AddServerRequest(&request, 0, 0, 0, 1, OC_REST_GET,
120 0, resPtr->sequenceNum, qos, resourceObserver->query,
122 &resourceObserver->token, resourceObserver->addr,
123 resourceObserver->resUri, 0);
126 request->observeResult = OC_STACK_OK;
127 if(request && result == OC_STACK_OK)
129 result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) request,
130 request->method, (OCResourceHandle) resPtr, request->query,
131 request->reqJSONPayload, request->numRcvdVendorSpecificHeaderOptions,
132 request->rcvdVendorSpecificHeaderOptions, OC_OBSERVE_NO_OPTION, 0);
133 if(result == OC_STACK_OK)
135 ehResult = resPtr->entityHandler(OC_REQUEST_FLAG, &ehRequest);
136 if(ehResult == OC_EH_ERROR)
138 FindAndDeleteServerRequest(request);
146 OCEntityHandlerResponse ehResponse = {0};
147 unsigned char presenceResBuf[MAX_RESPONSE_LENGTH] = {0};
148 //This is effectively the implementation for the presence entity handler.
149 OC_LOG(DEBUG, TAG, PCF("This notification is for Presence"));
151 result = AddServerRequest(&request, 0, 0, 0, 1, OC_REST_GET,
152 0, resPtr->sequenceNum, qos, resourceObserver->query,
154 &resourceObserver->token, resourceObserver->addr,
155 resourceObserver->resUri, 0,
156 &(resourceObserver->addressInfo), resourceObserver->connectivityType);
159 result = AddServerRequest(&request, 0, 0, 0, 1, OC_REST_GET,
160 0, OC_OBSERVE_NO_OPTION, OC_LOW_QOS,
161 NULL, NULL, NULL, &resourceObserver->token,
162 resourceObserver->addr, resourceObserver->resUri, 0);
164 if(result == OC_STACK_OK)
166 // we create the payload here
167 if(resourceType && resourceType->resourcetypename)
169 snprintf((char *)presenceResBuf, sizeof(presenceResBuf), "%u:%u:%s",
170 resPtr->sequenceNum, maxAge, resourceType->resourcetypename);
174 snprintf((char *)presenceResBuf, sizeof(presenceResBuf), "%u:%u",
175 resPtr->sequenceNum, maxAge);
177 ehResponse.ehResult = OC_EH_OK;
178 ehResponse.payload = presenceResBuf;
179 ehResponse.payloadSize = strlen((const char *)presenceResBuf) + 1;
180 ehResponse.persistentBufferFlag = 0;
181 ehResponse.requestHandle = (OCRequestHandle) request;
182 ehResponse.resourceHandle = (OCResourceHandle) resPtr;
183 strcpy((char *)ehResponse.resourceUri, (const char *)resourceObserver->resUri);
184 result = OCDoResponse(&ehResponse);
189 resourceObserver = resourceObserver->next;
193 OC_LOG(INFO, TAG, PCF("Resource has no observers"));
194 result = OC_STACK_NO_OBSERVERS;
199 OCStackResult SendListObserverNotification (OCResource * resource,
200 OCObservationId *obsIdList, uint8_t numberOfIds,
201 unsigned char *notificationJSONPayload, uint32_t maxAge,
202 OCQualityOfService qos)
204 uint8_t numIds = numberOfIds;
205 ResourceObserver *observation = NULL;
206 uint8_t numSentNotification = 0;
207 OCServerRequest * request = NULL;
208 OCStackResult result = OC_STACK_ERROR;
209 OCEntityHandlerResponse ehResponse = {0};
211 OC_LOG(INFO, TAG, PCF("Entering SendListObserverNotification"));
214 OC_LOG_V(INFO, TAG, "Need to notify observation id %d", *obsIdList);
216 observation = GetObserverUsingId (*obsIdList);
219 // Found observation - verify if it matches the resource handle
220 if (observation->resource == resource)
222 qos = DetermineObserverQoS(OC_REST_GET, observation, qos);
226 result = AddServerRequest(&request, 0, 0, 0, 1, OC_REST_GET,
227 0, resource->sequenceNum, qos, observation->query,
228 NULL, NULL, &observation->token,
229 observation->addr, observation->resUri, 0,
230 &(observation->addressInfo), observation->connectivityType);
232 result = AddServerRequest(&request, 0, 0, 0, 1, OC_REST_GET,
233 0, resource->sequenceNum, qos, observation->query,
234 NULL, NULL, &observation->token,
235 observation->addr, observation->resUri, 0);
238 request->observeResult = OC_STACK_OK;
239 if(request && result == OC_STACK_OK)
241 memset(&ehResponse, 0, sizeof(OCEntityHandlerResponse));
242 ehResponse.ehResult = OC_EH_OK;
243 ehResponse.payload = (unsigned char *) OCMalloc(MAX_RESPONSE_LENGTH);
244 if(!ehResponse.payload)
246 FindAndDeleteServerRequest(request);
249 strcpy((char *)ehResponse.payload, (const char *)notificationJSONPayload);
250 ehResponse.payloadSize = strlen((const char *)ehResponse.payload) + 1;
251 ehResponse.persistentBufferFlag = 0;
252 ehResponse.requestHandle = (OCRequestHandle) request;
253 ehResponse.resourceHandle = (OCResourceHandle) resource;
254 result = OCDoResponse(&ehResponse);
255 if(result == OC_STACK_OK)
257 OCFree(ehResponse.payload);
258 FindAndDeleteServerRequest(request);
263 FindAndDeleteServerRequest(request);
266 numSentNotification++;
272 if(numSentNotification == numberOfIds)
276 else if(numSentNotification == 0)
278 return OC_STACK_NO_OBSERVERS;
282 //TODO: we need to signal that not every one in the
283 // list got an update, should we also indicate who did not receive on?
288 OCStackResult GenerateObserverId (OCObservationId *observationId)
290 ResourceObserver *resObs = NULL;
292 OC_LOG(INFO, TAG, PCF("Entering GenerateObserverId"));
293 VERIFY_NON_NULL (observationId);
297 *observationId = OCGetRandomByte();
298 // Check if observation Id already exists
299 resObs = GetObserverUsingId (*observationId);
300 } while (NULL != resObs);
302 OC_LOG_V(INFO, TAG, "Observation ID is %u", *observationId);
306 return OC_STACK_ERROR;
310 OCStackResult AddObserver (const char *resUri,
312 OCObservationId obsId,
315 OCResource *resHandle,
316 OCQualityOfService qos,
317 CAAddress_t *addressInfo,
318 CAConnectivityType_t connectivityType)
320 OCStackResult AddObserver (const char *resUri,
322 OCObservationId obsId,
325 OCResource *resHandle,
326 OCQualityOfService qos)
329 ResourceObserver *obsNode = NULL;
331 obsNode = (ResourceObserver *) OCCalloc(1, sizeof(ResourceObserver));
334 obsNode->observeId = obsId;
336 obsNode->resUri = (unsigned char *)OCMalloc(strlen(resUri)+1);
337 VERIFY_NON_NULL (obsNode->resUri);
338 memcpy (obsNode->resUri, resUri, strlen(resUri)+1);
343 obsNode->query = (unsigned char *)OCMalloc(strlen(query)+1);
344 VERIFY_NON_NULL (obsNode->query);
345 memcpy (obsNode->query, query, strlen(query)+1);
349 obsNode->token = (CAToken_t)OCMalloc(CA_MAX_TOKEN_LEN+1);
350 VERIFY_NON_NULL (obsNode->token);
351 memset(obsNode->token, 0, CA_MAX_TOKEN_LEN + 1);
352 memcpy(obsNode->token, *token, CA_MAX_TOKEN_LEN);
354 obsNode->token.tokenLength = token->tokenLength;
355 memcpy (obsNode->token.token, token->token, token->tokenLength);
358 obsNode->addr = (OCDevAddr *)OCMalloc(sizeof(OCDevAddr));
359 VERIFY_NON_NULL (obsNode->addr);
360 memcpy (obsNode->addr, addr, sizeof(OCDevAddr));
362 obsNode->addressInfo = *addressInfo;
363 obsNode->connectivityType = connectivityType;
365 obsNode->resource = resHandle;
366 LL_APPEND (serverObsList, obsNode);
373 OCFree(obsNode->resUri);
374 OCFree(obsNode->query);
375 OCFree(obsNode->addr);
378 return OC_STACK_NO_MEMORY;
381 ResourceObserver* GetObserverUsingId (const OCObservationId observeId)
383 ResourceObserver *out = NULL;
387 LL_FOREACH (serverObsList, out)
389 if (out->observeId == observeId)
395 OC_LOG(INFO, TAG, PCF("Observer node not found!!"));
400 ResourceObserver* GetObserverUsingToken (const CAToken_t * token)
402 ResourceObserver* GetObserverUsingToken (const OCCoAPToken * token)
405 ResourceObserver *out = NULL;
409 LL_FOREACH (serverObsList, out)
412 OC_LOG(INFO, TAG,PCF("comparing tokens"));
413 OC_LOG_BUFFER(INFO, TAG, token, CA_MAX_TOKEN_LEN);
414 OC_LOG_BUFFER(INFO, TAG, out->token, CA_MAX_TOKEN_LEN);
415 if((memcmp(out->token, *token, CA_MAX_TOKEN_LEN) == 0))
420 OC_LOG(INFO, TAG,PCF("comparing tokens"));
421 OC_LOG_BUFFER(INFO, TAG, token->token, token->tokenLength);
422 OC_LOG_BUFFER(INFO, TAG, out->token.token, out->token.tokenLength);
423 if((out->token.tokenLength == token->tokenLength) &&
424 (memcmp(out->token.token, token->token, token->tokenLength) == 0))
431 OC_LOG(INFO, TAG, PCF("Observer node not found!!"));
436 OCStackResult DeleteObserverUsingToken (CAToken_t * token)
438 OCStackResult DeleteObserverUsingToken (OCCoAPToken * token)
441 ResourceObserver *obsNode = NULL;
443 obsNode = GetObserverUsingToken (token);
446 OC_LOG_V(INFO, TAG, PCF("deleting tokens"));
448 OC_LOG_BUFFER(INFO, TAG, obsNode->token, CA_MAX_TOKEN_LEN);
450 OC_LOG_BUFFER(INFO, TAG, obsNode->token.token, obsNode->token.tokenLength);
452 LL_DELETE (serverObsList, obsNode);
453 OCFree(obsNode->resUri);
454 OCFree(obsNode->query);
455 OCFree(obsNode->addr);
458 // it is ok if we did not find the observer...
462 void DeleteObserverList()
464 ResourceObserver *out = NULL;
465 ResourceObserver *tmp = NULL;
466 LL_FOREACH_SAFE (serverObsList, out, tmp)
468 DeleteObserverUsingToken (&(out->token));
470 serverObsList = NULL;
475 CreateObserveHeaderOption (CAHeaderOption_t **caHdrOpt,
476 OCHeaderOption *ocHdrOpt,
480 CAHeaderOption_t *tmpHdrOpt = NULL;
482 tmpHdrOpt = (CAHeaderOption_t *) OCMalloc ((numOptions+1)*sizeof(CAHeaderOption_t));
483 if (NULL == tmpHdrOpt)
485 return OC_STACK_NO_MEMORY;
487 tmpHdrOpt[0].protocolID = CA_COAP_ID;
488 // TODO-CA: COAP_OPTION_OBSERVE is defined in CoAP header files which will be abstracted
489 // from resource model. We have to define a new macro for this in the stack layer.
490 tmpHdrOpt[0].optionID = COAP_OPTION_OBSERVE;
491 tmpHdrOpt[0].optionLength = sizeof(uint32_t);
492 tmpHdrOpt[0].optionData[0] = observeFlag;
493 for (uint8_t i = 0; i < numOptions; i++)
495 memcpy (&(tmpHdrOpt[i+1]), &(ocHdrOpt[i]), sizeof(CAHeaderOption_t));
498 *caHdrOpt = tmpHdrOpt;
503 GetObserveHeaderOption (uint32_t * observationOption,
504 CAHeaderOption_t *options,
505 uint8_t * numOptions)
507 *observationOption = OC_OBSERVE_NO_OPTION;
510 for(i = 0; i < *numOptions; i++)
512 if(options[i].protocolID == CA_COAP_ID &&
513 options[i].optionID == COAP_OPTION_OBSERVE)
515 *observationOption = options[i].optionData[0];
516 for(c = i; c < *numOptions-1; c++)
518 options[i].protocolID = options[i+1].protocolID;
519 options[i].optionID = options[i+1].optionID;
520 options[i].optionLength = options[i+1].optionLength;
521 memcpy(options[i].optionData, options[i+1].optionData, options[i+1].optionLength);