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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
24 * This file contains the data structure and APIs for registered resource as an observer.
32 /** Maximum number of observers to reach */
34 #define MAX_OBSERVER_FAILED_COMM (2)
36 /** Maximum number of observers to reach for resources with low QOS */
37 #define MAX_OBSERVER_NON_COUNT (3)
40 * MAX_OBSERVER_TTL_SECONDS sets the maximum time to live (TTL) for notification.
41 * 60 sec/min * 60 min/hr * 24 hr/day
43 #define MAX_OBSERVER_TTL_SECONDS (60 * 60 * 24)
45 #define MILLISECONDS_PER_SECOND (1000)
48 * Data structure to hold informations for each registered observer.
50 typedef struct ResourceObserver
52 /** Observation Identifier for request.*/
53 OCObservationId observeId;
55 /** URI of observed resource.*/
61 /** token for the observe request.*/
64 /** token length for the observe request.*/
67 /** Resource handle.*/
70 /** Remote Endpoint. */
73 /** Quality of service of the request.*/
74 OCQualityOfService qos;
76 /** number of times the server failed to reach the observer.*/
77 uint8_t failedCommCount;
79 /** number of times the server sent NON notifications.*/
82 /** force the qos value to CON.*/
85 /** The TTL for this callback. TTL is set to 24 hours.
86 * A server send a notification in a confirmable message every 24 hours.
87 * This prevents a client that went away or is no logger interested
88 * from remaining in the list of observers indefinitely.*/
91 /** next node in this list.*/
92 struct ResourceObserver *next;
94 /** requested payload encoding format. */
95 OCPayloadFormat acceptFormat;
100 * Initialize observer list.
102 * @return ::OC_STACK_OK on success, some other value upon failure.
104 OCStackResult InitializeObserverList();
107 * Terminate observer list.
109 void TerminateObserverList();
113 * Create an observe response and send to all observers in the observe list.
115 * @param method RESTful method.
116 * @param resPtr Observed resource.
117 * @param maxAge Time To Live (in seconds) of observation.
118 * @param resourceType Resource type. Allows resource type name to be added to response.
119 * @param qos Quality of service of resource.
121 * @return ::OC_STACK_OK on success, some other value upon failure.
123 OCStackResult SendAllObserverNotification (OCMethod method, OCResource *resPtr, uint32_t maxAge,
124 OCPresenceTrigger trigger, OCResourceType *resourceType, OCQualityOfService qos);
127 * Create an observe response and send to all observers in the observe list.
129 * @param method RESTful method.
130 * @param resPtr Observed resource.
131 * @param maxAge Time To Live (in seconds) of observation.
132 * @param qos Quality of service of resource.
134 * @return ::OC_STACK_OK on success, some other value upon failure.
136 OCStackResult SendAllObserverNotification (OCMethod method, OCResource *resPtr, uint32_t maxAge,
137 OCQualityOfService qos);
141 * Notify specific observers with updated value of representation.
143 * @param resource Observed resource.
144 * @param obsIdList List of observation ids that need to be notified.
145 * @param numberOfIds Number of observation ids included in obsIdList.
146 * @param notificationJSONPayload JSON encoded payload to send in notification.
147 * @param maxAge Time To Live (in seconds) of observation.
148 * @param qos Desired quality of service of the observation notifications.
150 * @return ::OC_STACK_OK on success, some other value upon failure.
152 OCStackResult SendListObserverNotification (OCResource * resource,
153 OCObservationId *obsIdList, uint8_t numberOfIds,
154 const OCRepPayload *payload, uint32_t maxAge,
155 OCQualityOfService qos);
158 * Delete all observers in the observe list.
160 void DeleteObserverList();
163 * Create a unique observation ID.
165 * @param observationId Pointer to generated ID.
167 * @return ::OC_STACK_OK on success, some other value upon failure.
169 OCStackResult GenerateObserverId (OCObservationId *observationId);
172 * Add observer for a resource.
174 * @param resUri Resource URI string.
175 * @param query Query string.
176 * @param obsId Observation ID.
177 * @param token Request token.
178 * @param tokenLength Length of token.
179 * @param resHandle Resource handle.
180 * @param qos Quality of service of observation.
181 * @param devAddr Device address.
183 * @return ::OC_STACK_OK on success, some other value upon failure.
185 OCStackResult AddObserver (const char *resUri,
187 OCObservationId obsId,
190 OCResource *resHandle,
191 OCQualityOfService qos,
192 OCPayloadFormat acceptFormat,
193 const OCDevAddr *devAddr);
196 * Delete observer with specified token from list of observers.
197 * Free memory that was allocated for the observer in the list.
199 * @param token Token to search for.
200 * @param tokenLength Length of token.
202 * @return ::OC_STACK_OK on success, some other value upon failure.
204 OCStackResult DeleteObserverUsingToken (CAToken_t token, uint8_t tokenLength);
207 * Delete observer with device address from list of observers.
208 * Free memory that was allocated for the observer in the list.
210 * @param devAddr Device's address.
212 * @return ::OC_STACK_OK on success, some other value upon failure.
214 OCStackResult DeleteObserverUsingDevAddr(const OCDevAddr *devAddr);
217 * Search the list of observers for the specified token.
219 * @param token Token to search for.
220 * @param tokenLength Length of token.
222 * @return Pointer to found observer.
223 * This is copy of observer, caller must release its memory using FreeObserver().
225 ResourceObserver* GetObserverUsingToken (const CAToken_t token, uint8_t tokenLength);
228 * Search the list of observers for the specified observe ID.
230 * @param observeId Observer ID to search for.
232 * @return Pointer to found observer.
233 * This is copy of observer, caller must release its memory using FreeObserver().
235 ResourceObserver* GetObserverUsingId (const OCObservationId observeId);
238 * Search the list of observers for the specified observe ID.
240 * @param observeId Observer ID to search for.
242 * @return true if observer found matched with observer ID, otherwise false.
244 bool IsObserverAvailable (const OCObservationId observeId);
247 * Add observe header option to a request.
249 * @param caHdrOpt Target request CA header option.
250 * @param ocHdrOpt Pointer to existing options.
251 * @param numOptions Number of existing options.
252 * @param observeFlag Register/de-register observation. Should be either
253 * ::OC_OBSERVE_REGISTER or ::OC_OBSERVE_DEREGISTER.
255 * @return ::OC_STACK_OK on success, some other value upon failure.
258 CreateObserveHeaderOption (CAHeaderOption_t **caHdrOpt,
259 OCHeaderOption *ocHdrOpt,
261 uint8_t observeFlag);
264 * Copy the observe option from a received request.
266 * @param observationOption Pointer to observe option value. Should be either
267 * ::OC_OBSERVE_REGISTER, ::OC_OBSERVE_DEREGISTER, or
268 * ::OC_OBSERVE_NO_OPTION if observe not found.
270 * @param options Options in received request. Observe option is removed if found.
271 * @param numOptions Number of options in the received request. Decremented if observe
272 * option is extracted.
274 * @return ::OC_STACK_OK on success, some other value upon failure.
277 GetObserveHeaderOption (uint32_t * observationOption,
278 CAHeaderOption_t *options,
279 uint8_t * numOptions);
282 * Free memory associated with observer.
284 * @param obsNode Observer to be freed.
286 void FreeObserver (ResourceObserver* obsNode);
288 #endif //OC_OBSERVE_H