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);
111 result = AddServerRequest(&request, 0, 0, 0, 1, OC_REST_GET,
112 0, resPtr->sequenceNum, qos, resourceObserver->query,
114 &resourceObserver->token, resourceObserver->addr,
115 resourceObserver->resUri, 0,
116 &(resourceObserver->addressInfo), resourceObserver->connectivityType);
118 request->observeResult = OC_STACK_OK;
119 if(request && result == OC_STACK_OK)
121 result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) request,
122 request->method, (OCResourceHandle) resPtr, request->query,
123 request->reqJSONPayload, request->numRcvdVendorSpecificHeaderOptions,
124 request->rcvdVendorSpecificHeaderOptions, OC_OBSERVE_NO_OPTION, 0);
125 if(result == OC_STACK_OK)
127 ehResult = resPtr->entityHandler(OC_REQUEST_FLAG, &ehRequest);
128 if(ehResult == OC_EH_ERROR)
130 FindAndDeleteServerRequest(request);
138 OCEntityHandlerResponse ehResponse = {0};
139 unsigned char presenceResBuf[MAX_RESPONSE_LENGTH] = {0};
140 //This is effectively the implementation for the presence entity handler.
141 OC_LOG(DEBUG, TAG, PCF("This notification is for Presence"));
142 result = AddServerRequest(&request, 0, 0, 0, 1, OC_REST_GET,
143 0, resPtr->sequenceNum, qos, resourceObserver->query,
145 &resourceObserver->token, resourceObserver->addr,
146 resourceObserver->resUri, 0,
147 &(resourceObserver->addressInfo), resourceObserver->connectivityType);
149 if(result == OC_STACK_OK)
151 // we create the payload here
152 if(resourceType && resourceType->resourcetypename)
154 snprintf((char *)presenceResBuf, sizeof(presenceResBuf), "%u:%u:%s",
155 resPtr->sequenceNum, maxAge, resourceType->resourcetypename);
159 snprintf((char *)presenceResBuf, sizeof(presenceResBuf), "%u:%u",
160 resPtr->sequenceNum, maxAge);
162 ehResponse.ehResult = OC_EH_OK;
163 ehResponse.payload = presenceResBuf;
164 ehResponse.payloadSize = strlen((const char *)presenceResBuf) + 1;
165 ehResponse.persistentBufferFlag = 0;
166 ehResponse.requestHandle = (OCRequestHandle) request;
167 ehResponse.resourceHandle = (OCResourceHandle) resPtr;
168 strcpy((char *)ehResponse.resourceUri, (const char *)resourceObserver->resUri);
169 result = OCDoResponse(&ehResponse);
174 resourceObserver = resourceObserver->next;
178 OC_LOG(INFO, TAG, PCF("Resource has no observers"));
179 result = OC_STACK_NO_OBSERVERS;
184 OCStackResult SendListObserverNotification (OCResource * resource,
185 OCObservationId *obsIdList, uint8_t numberOfIds,
186 unsigned char *notificationJSONPayload, uint32_t maxAge,
187 OCQualityOfService qos)
189 uint8_t numIds = numberOfIds;
190 ResourceObserver *observation = NULL;
191 uint8_t numSentNotification = 0;
192 OCServerRequest * request = NULL;
193 OCStackResult result = OC_STACK_ERROR;
194 OCEntityHandlerResponse ehResponse = {0};
196 OC_LOG(INFO, TAG, PCF("Entering SendListObserverNotification"));
199 OC_LOG_V(INFO, TAG, "Need to notify observation id %d", *obsIdList);
201 observation = GetObserverUsingId (*obsIdList);
204 // Found observation - verify if it matches the resource handle
205 if (observation->resource == resource)
207 qos = DetermineObserverQoS(OC_REST_GET, observation, qos);
210 result = AddServerRequest(&request, 0, 0, 0, 1, OC_REST_GET,
211 0, resource->sequenceNum, qos, observation->query,
212 NULL, NULL, &observation->token,
213 observation->addr, observation->resUri, 0,
214 &(observation->addressInfo), observation->connectivityType);
216 request->observeResult = OC_STACK_OK;
217 if(request && result == OC_STACK_OK)
219 memset(&ehResponse, 0, sizeof(OCEntityHandlerResponse));
220 ehResponse.ehResult = OC_EH_OK;
221 ehResponse.payload = (unsigned char *) OCMalloc(MAX_RESPONSE_LENGTH);
222 if(!ehResponse.payload)
224 FindAndDeleteServerRequest(request);
227 strcpy((char *)ehResponse.payload, (const char *)notificationJSONPayload);
228 ehResponse.payloadSize = strlen((const char *)ehResponse.payload) + 1;
229 ehResponse.persistentBufferFlag = 0;
230 ehResponse.requestHandle = (OCRequestHandle) request;
231 ehResponse.resourceHandle = (OCResourceHandle) resource;
232 result = OCDoResponse(&ehResponse);
233 if(result == OC_STACK_OK)
235 OCFree(ehResponse.payload);
236 FindAndDeleteServerRequest(request);
241 FindAndDeleteServerRequest(request);
244 numSentNotification++;
250 if(numSentNotification == numberOfIds)
254 else if(numSentNotification == 0)
256 return OC_STACK_NO_OBSERVERS;
260 //TODO: we need to signal that not every one in the
261 // list got an update, should we also indicate who did not receive on?
266 OCStackResult GenerateObserverId (OCObservationId *observationId)
268 ResourceObserver *resObs = NULL;
270 OC_LOG(INFO, TAG, PCF("Entering GenerateObserverId"));
271 VERIFY_NON_NULL (observationId);
275 *observationId = OCGetRandomByte();
276 // Check if observation Id already exists
277 resObs = GetObserverUsingId (*observationId);
278 } while (NULL != resObs);
280 OC_LOG_V(INFO, TAG, "Observation ID is %u", *observationId);
284 return OC_STACK_ERROR;
287 OCStackResult AddObserver (const char *resUri,
289 OCObservationId obsId,
292 OCResource *resHandle,
293 OCQualityOfService qos,
294 CAAddress_t *addressInfo,
295 CAConnectivityType_t connectivityType)
297 ResourceObserver *obsNode = NULL;
299 obsNode = (ResourceObserver *) OCCalloc(1, sizeof(ResourceObserver));
302 obsNode->observeId = obsId;
304 obsNode->resUri = (unsigned char *)OCMalloc(strlen(resUri)+1);
305 VERIFY_NON_NULL (obsNode->resUri);
306 memcpy (obsNode->resUri, resUri, strlen(resUri)+1);
311 obsNode->query = (unsigned char *)OCMalloc(strlen(query)+1);
312 VERIFY_NON_NULL (obsNode->query);
313 memcpy (obsNode->query, query, strlen(query)+1);
316 obsNode->token = (CAToken_t)OCMalloc(CA_MAX_TOKEN_LEN+1);
317 VERIFY_NON_NULL (obsNode->token);
318 memset(obsNode->token, 0, CA_MAX_TOKEN_LEN + 1);
319 memcpy(obsNode->token, *token, CA_MAX_TOKEN_LEN);
321 obsNode->addr = (OCDevAddr *)OCMalloc(sizeof(OCDevAddr));
322 VERIFY_NON_NULL (obsNode->addr);
323 memcpy (obsNode->addr, addr, sizeof(OCDevAddr));
324 obsNode->addressInfo = *addressInfo;
325 obsNode->connectivityType = connectivityType;
326 obsNode->resource = resHandle;
327 LL_APPEND (serverObsList, obsNode);
334 OCFree(obsNode->resUri);
335 OCFree(obsNode->query);
336 OCFree(obsNode->addr);
339 return OC_STACK_NO_MEMORY;
342 ResourceObserver* GetObserverUsingId (const OCObservationId observeId)
344 ResourceObserver *out = NULL;
348 LL_FOREACH (serverObsList, out)
350 if (out->observeId == observeId)
356 OC_LOG(INFO, TAG, PCF("Observer node not found!!"));
360 ResourceObserver* GetObserverUsingToken (const CAToken_t * token)
362 ResourceObserver *out = NULL;
366 LL_FOREACH (serverObsList, out)
368 OC_LOG(INFO, TAG,PCF("comparing tokens"));
369 OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)token, CA_MAX_TOKEN_LEN);
370 OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)out->token, CA_MAX_TOKEN_LEN);
371 if((memcmp(out->token, *token, CA_MAX_TOKEN_LEN) == 0))
377 OC_LOG(INFO, TAG, PCF("Observer node not found!!"));
381 OCStackResult DeleteObserverUsingToken (CAToken_t * token)
383 ResourceObserver *obsNode = NULL;
385 obsNode = GetObserverUsingToken (token);
388 OC_LOG_V(INFO, TAG, PCF("deleting tokens"));
389 OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)obsNode->token, CA_MAX_TOKEN_LEN);
390 LL_DELETE (serverObsList, obsNode);
391 OCFree(obsNode->resUri);
392 OCFree(obsNode->query);
393 OCFree(obsNode->addr);
396 // it is ok if we did not find the observer...
400 void DeleteObserverList()
402 ResourceObserver *out = NULL;
403 ResourceObserver *tmp = NULL;
404 LL_FOREACH_SAFE (serverObsList, out, tmp)
406 DeleteObserverUsingToken (&(out->token));
408 serverObsList = NULL;
412 CreateObserveHeaderOption (CAHeaderOption_t **caHdrOpt,
413 OCHeaderOption *ocHdrOpt,
417 CAHeaderOption_t *tmpHdrOpt = NULL;
419 tmpHdrOpt = (CAHeaderOption_t *) OCMalloc ((numOptions+1)*sizeof(CAHeaderOption_t));
420 if (NULL == tmpHdrOpt)
422 return OC_STACK_NO_MEMORY;
424 tmpHdrOpt[0].protocolID = CA_COAP_ID;
425 // TODO-CA: COAP_OPTION_OBSERVE is defined in CoAP header files which will be abstracted
426 // from resource model. We have to define a new macro for this in the stack layer.
427 tmpHdrOpt[0].optionID = COAP_OPTION_OBSERVE;
428 tmpHdrOpt[0].optionLength = sizeof(uint32_t);
429 tmpHdrOpt[0].optionData[0] = observeFlag;
430 for (uint8_t i = 0; i < numOptions; i++)
432 memcpy (&(tmpHdrOpt[i+1]), &(ocHdrOpt[i]), sizeof(CAHeaderOption_t));
435 *caHdrOpt = tmpHdrOpt;
440 GetObserveHeaderOption (uint32_t * observationOption,
441 CAHeaderOption_t *options,
442 uint8_t * numOptions)
444 *observationOption = OC_OBSERVE_NO_OPTION;
447 for(i = 0; i < *numOptions; i++)
449 if(options[i].protocolID == CA_COAP_ID &&
450 options[i].optionID == COAP_OPTION_OBSERVE)
452 *observationOption = options[i].optionData[0];
453 for(c = i; c < *numOptions-1; c++)
455 options[i].protocolID = options[i+1].protocolID;
456 options[i].optionID = options[i+1].optionID;
457 options[i].optionLength = options[i+1].optionLength;
458 memcpy(options[i].optionData, options[i+1].optionData, options[i+1].optionLength);