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 sprintf((char *)presenceResBuf, "%u:%u:%s",
161 resPtr->sequenceNum, maxAge, resourceType->resourcetypename);
165 sprintf((char *)presenceResBuf, "%u:%u", resPtr->sequenceNum, maxAge);
167 ehResponse.ehResult = OC_EH_OK;
168 ehResponse.payload = presenceResBuf;
169 ehResponse.payloadSize = strlen((const char *)presenceResBuf) + 1;
170 ehResponse.persistentBufferFlag = 0;
171 ehResponse.requestHandle = (OCRequestHandle) request;
172 ehResponse.resourceHandle = (OCResourceHandle) resPtr;
173 strcpy((char *)ehResponse.resourceUri, (const char *)resourceObserver->resUri);
174 result = OCDoResponse(&ehResponse);
179 resourceObserver = resourceObserver->next;
183 OC_LOG(INFO, TAG, PCF("Resource has no observers"));
184 result = OC_STACK_NO_OBSERVERS;
189 OCStackResult SendListObserverNotification (OCResource * resource,
190 OCObservationId *obsIdList, uint8_t numberOfIds,
191 unsigned char *notificationJSONPayload, uint32_t maxAge,
192 OCQualityOfService qos)
194 uint8_t numIds = numberOfIds;
195 ResourceObserver *observation = NULL;
196 uint8_t numSentNotification = 0;
197 OCServerRequest * request = NULL;
198 OCStackResult result = OC_STACK_ERROR;
199 OCEntityHandlerResponse ehResponse = {0};
201 OC_LOG(INFO, TAG, PCF("Entering SendListObserverNotification"));
204 OC_LOG_V(INFO, TAG, "Need to notify observation id %d", *obsIdList);
206 observation = GetObserverUsingId (*obsIdList);
209 // Found observation - verify if it matches the resource handle
210 if (observation->resource == resource)
212 qos = DetermineObserverQoS(OC_REST_GET, observation, qos);
216 result = AddServerCARequest(&request, 0, 0, 0, 1, OC_REST_GET,
217 0, resource->sequenceNum, qos, observation->query,
218 NULL, NULL, &observation->token,
219 observation->addr, observation->resUri, 0,
220 &(observation->addressInfo), observation->connectivityType,
221 observation->CAToken);
223 result = AddServerRequest(&request, 0, 0, 0, 1, OC_REST_GET,
224 0, resource->sequenceNum, qos, observation->query,
225 NULL, NULL, &observation->token,
226 observation->addr, observation->resUri, 0);
229 request->observeResult = OC_STACK_OK;
230 if(request && result == OC_STACK_OK)
232 memset(&ehResponse, 0, sizeof(OCEntityHandlerResponse));
233 ehResponse.ehResult = OC_EH_OK;
234 ehResponse.payload = (unsigned char *) OCMalloc(MAX_RESPONSE_LENGTH);
235 if(!ehResponse.payload)
237 FindAndDeleteServerRequest(request);
240 strcpy((char *)ehResponse.payload, (const char *)notificationJSONPayload);
241 ehResponse.payloadSize = strlen((const char *)ehResponse.payload) + 1;
242 ehResponse.persistentBufferFlag = 0;
243 ehResponse.requestHandle = (OCRequestHandle) request;
244 ehResponse.resourceHandle = (OCResourceHandle) resource;
245 result = OCDoResponse(&ehResponse);
246 if(result == OC_STACK_OK)
248 OCFree(ehResponse.payload);
249 FindAndDeleteServerRequest(request);
254 FindAndDeleteServerRequest(request);
257 numSentNotification++;
263 if(numSentNotification == numberOfIds)
267 else if(numSentNotification == 0)
269 return OC_STACK_NO_OBSERVERS;
273 //TODO: we need to signal that not every one in the
274 // list got an update, should we also indicate who did not receive on?
279 OCStackResult GenerateObserverId (OCObservationId *observationId)
281 ResourceObserver *resObs = NULL;
283 OC_LOG(INFO, TAG, PCF("Entering GenerateObserverId"));
284 VERIFY_NON_NULL (observationId);
288 *observationId = OCGetRandomByte();
289 // Check if observation Id already exists
290 resObs = GetObserverUsingId (*observationId);
291 } while (NULL != resObs);
293 OC_LOG_V(INFO, TAG, "Observation ID is %u", *observationId);
297 return OC_STACK_ERROR;
301 OCStackResult AddCAObserver (const char *resUri,
303 OCObservationId obsId,
306 OCResource *resHandle,
307 OCQualityOfService qos,
308 CAAddress_t *addressInfo,
309 CAConnectivityType_t connectivityType,
312 ResourceObserver *obsNode = NULL;
314 obsNode = (ResourceObserver *) OCCalloc(1, sizeof(ResourceObserver));
317 obsNode->observeId = obsId;
319 obsNode->resUri = (unsigned char *)OCMalloc(strlen(resUri)+1);
320 VERIFY_NON_NULL (obsNode->resUri);
321 memcpy (obsNode->resUri, resUri, strlen(resUri)+1);
326 obsNode->query = (unsigned char *)OCMalloc(strlen(query)+1);
327 VERIFY_NON_NULL (obsNode->query);
328 memcpy (obsNode->query, query, strlen(query)+1);
333 obsNode->token.tokenLength = token->tokenLength;
334 memcpy (obsNode->token.token, token->token, token->tokenLength);
339 obsNode->addr = (OCDevAddr *)OCMalloc(sizeof(OCDevAddr));
340 VERIFY_NON_NULL (obsNode->addr);
341 memcpy (obsNode->addr, addr, sizeof(OCDevAddr));
344 obsNode->addressInfo = *addressInfo;
345 obsNode->connectivityType = connectivityType;
348 strncpy(obsNode->CAToken, CAtoken, sizeof(obsNode->CAToken) - 1);
351 obsNode->resource = resHandle;
353 LL_APPEND (serverObsList, obsNode);
360 OCFree(obsNode->resUri);
361 OCFree(obsNode->query);
362 OCFree(obsNode->addr);
365 return OC_STACK_NO_MEMORY;
369 OCStackResult AddObserver (const char *resUri,
371 OCObservationId obsId,
374 OCResource *resHandle,
375 OCQualityOfService qos)
377 ResourceObserver *obsNode = NULL;
379 obsNode = (ResourceObserver *) OCCalloc(1, sizeof(ResourceObserver));
382 obsNode->observeId = obsId;
384 obsNode->resUri = (unsigned char *)OCMalloc(strlen(resUri)+1);
385 VERIFY_NON_NULL (obsNode->resUri);
386 memcpy (obsNode->resUri, resUri, strlen(resUri)+1);
391 obsNode->query = (unsigned char *)OCMalloc(strlen(query)+1);
392 VERIFY_NON_NULL (obsNode->query);
393 memcpy (obsNode->query, query, strlen(query)+1);
396 obsNode->token.tokenLength = token->tokenLength;
397 memcpy (obsNode->token.token, token->token, token->tokenLength);
399 obsNode->addr = (OCDevAddr *)OCMalloc(sizeof(OCDevAddr));
400 VERIFY_NON_NULL (obsNode->addr);
401 memcpy (obsNode->addr, addr, sizeof(OCDevAddr));
403 obsNode->resource = resHandle;
405 LL_APPEND (serverObsList, obsNode);
412 OCFree(obsNode->resUri);
413 OCFree(obsNode->query);
414 OCFree(obsNode->addr);
417 return OC_STACK_NO_MEMORY;
420 ResourceObserver* GetObserverUsingId (const OCObservationId observeId)
422 ResourceObserver *out = NULL;
426 LL_FOREACH (serverObsList, out)
428 if (out->observeId == observeId)
434 OC_LOG(INFO, TAG, PCF("Observer node not found!!"));
438 ResourceObserver* GetObserverUsingToken (const OCCoAPToken * token)
440 ResourceObserver *out = NULL;
444 LL_FOREACH (serverObsList, out)
446 OC_LOG(INFO, TAG,PCF("comparing tokens"));
447 OC_LOG_BUFFER(INFO, TAG, token->token, token->tokenLength);
448 OC_LOG_BUFFER(INFO, TAG, out->token.token, out->token.tokenLength);
449 if((out->token.tokenLength == token->tokenLength) &&
450 (memcmp(out->token.token, token->token, token->tokenLength) == 0))
456 OC_LOG(INFO, TAG, PCF("Observer node not found!!"));
460 OCStackResult DeleteObserverUsingToken (OCCoAPToken * token)
462 ResourceObserver *obsNode = NULL;
464 obsNode = GetObserverUsingToken (token);
467 OC_LOG_V(INFO, TAG, PCF("deleting tokens"));
468 OC_LOG_BUFFER(INFO, TAG, obsNode->token.token, obsNode->token.tokenLength);
469 LL_DELETE (serverObsList, obsNode);
470 OCFree(obsNode->resUri);
471 OCFree(obsNode->query);
472 OCFree(obsNode->addr);
475 // it is ok if we did not find the observer...
479 void DeleteObserverList()
481 ResourceObserver *out = NULL;
482 ResourceObserver *tmp = NULL;
483 LL_FOREACH_SAFE (serverObsList, out, tmp)
485 DeleteObserverUsingToken (&(out->token));
487 serverObsList = NULL;
492 CreateObserveHeaderOption (CAHeaderOption_t **caHdrOpt,
493 OCHeaderOption *ocHdrOpt,
497 CAHeaderOption_t *tmpHdrOpt = NULL;
499 tmpHdrOpt = (CAHeaderOption_t *) OCMalloc ((numOptions+1)*sizeof(CAHeaderOption_t));
500 if (NULL == tmpHdrOpt)
502 return OC_STACK_NO_MEMORY;
504 tmpHdrOpt[0].protocolID = CA_COAP_ID;
505 // TODO-CA: COAP_OPTION_OBSERVE is defined in CoAP header files which will be abstracted
506 // from resource model. We have to define a new macro for this in the stack layer.
507 tmpHdrOpt[0].optionID = COAP_OPTION_OBSERVE;
508 // Length is one byte
509 tmpHdrOpt[0].optionLength = 1;
510 tmpHdrOpt[0].optionData[0] = observeFlag;
511 for (uint8_t i = 0; i < numOptions; i++)
513 memcpy (&(tmpHdrOpt[i+1]), &(ocHdrOpt[i]), sizeof(CAHeaderOption_t));
516 *caHdrOpt = tmpHdrOpt;
521 GetObserveHeaderOption (uint32_t * observationOption,
522 CAHeaderOption_t *options,
523 uint8_t * numOptions)
525 *observationOption = OC_OBSERVE_NO_OPTION;
528 for(i = 0; i < *numOptions; i++)
530 if(options[i].protocolID == CA_COAP_ID &&
531 options[i].optionID == COAP_OPTION_OBSERVE)
533 *observationOption = options[i].optionData[0];
534 for(c = i; c < *numOptions-1; c++)
536 options[i].protocolID = options[i+1].protocolID;
537 options[i].optionID = options[i+1].optionID;
538 options[i].optionLength = options[i+1].optionLength;
539 memcpy(options[i].optionData, options[i+1].optionData, options[i+1].optionLength);