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 * Data structure to hold informations for each registered observer.
42 typedef struct ResourceObserver
44 /** Observation Identifier for request.*/
45 OCObservationId observeId;
47 /** URI of observed resource.*/
53 /** token for the observe request.*/
56 /** token length for the observe request.*/
59 /** Resource handle.*/
62 /** Remote Endpoint. */
65 /** Quality of service of the request.*/
66 OCQualityOfService qos;
68 /** number of times the server failed to reach the observer.*/
69 uint8_t failedCommCount;
71 /** number of times the server sent NON notifications.*/
74 /** force the qos value to CON.*/
77 /** next node in this list.*/
78 struct ResourceObserver *next;
80 /** requested payload encoding format. */
81 OCPayloadFormat acceptFormat;
87 * Create an observe response and send to all observers in the observe list.
89 * @param method RESTful method.
90 * @param resPtr Observed resource.
91 * @param maxAge Time To Live (in seconds) of observation.
92 * @param resourceType Resource type. Allows resource type name to be added to response.
93 * @param qos Quality of service of resource.
95 * @return ::OC_STACK_OK on success, some other value upon failure.
97 OCStackResult SendAllObserverNotification (OCMethod method, OCResource *resPtr, uint32_t maxAge,
98 OCPresenceTrigger trigger, OCResourceType *resourceType, OCQualityOfService qos);
101 * Create an observe response and send to all observers in the observe list.
103 * @param method RESTful method.
104 * @param resPtr Observed resource.
105 * @param maxAge Time To Live (in seconds) of observation.
106 * @param qos Quality of service of resource.
108 * @return ::OC_STACK_OK on success, some other value upon failure.
110 OCStackResult SendAllObserverNotification (OCMethod method, OCResource *resPtr, uint32_t maxAge,
111 OCQualityOfService qos);
115 * Notify specific observers with updated value of representation.
117 * @param resource Observed resource.
118 * @param obsIdList List of observation ids that need to be notified.
119 * @param numberOfIds Number of observation ids included in obsIdList.
120 * @param notificationJSONPayload JSON encoded payload to send in notification.
121 * @param maxAge Time To Live (in seconds) of observation.
122 * @param qos Desired quality of service of the observation notifications.
124 * @return ::OC_STACK_OK on success, some other value upon failure.
126 OCStackResult SendListObserverNotification (OCResource * resource,
127 OCObservationId *obsIdList, uint8_t numberOfIds,
128 const OCRepPayload *payload, uint32_t maxAge,
129 OCQualityOfService qos);
132 * Delete all observers in the observe list.
134 void DeleteObserverList();
137 * Create a unique observation ID.
139 * @param observationId Pointer to generated ID.
141 * @return ::OC_STACK_OK on success, some other value upon failure.
143 OCStackResult GenerateObserverId (OCObservationId *observationId);
146 * Add observer for a resource.
148 * @param resUri Resource URI string.
149 * @param query Query string.
150 * @param obsId Observation ID.
151 * @param token Request token.
152 * @param tokenLength Length of token.
153 * @param resHandle Resource handle.
154 * @param qos Quality of service of observation.
155 * @param devAddr Device address.
157 * @return ::OC_STACK_OK on success, some other value upon failure.
159 OCStackResult AddObserver (const char *resUri,
161 OCObservationId obsId,
164 OCResource *resHandle,
165 OCQualityOfService qos,
166 OCPayloadFormat acceptFormat,
167 const OCDevAddr *devAddr);
170 * Delete observer with specified token from list of observers.
171 * Free memory that was allocated for the observer in the list.
173 * @param token Token to search for.
174 * @param tokenLength Length of token.
176 * @return ::OC_STACK_OK on success, some other value upon failure.
178 OCStackResult DeleteObserverUsingToken (CAToken_t token, uint8_t tokenLength);
181 * Search the list of observers for the specified token.
183 * @param token Token to search for.
184 * @param tokenLength Length of token.
186 * @return Pointer to found observer.
188 ResourceObserver* GetObserverUsingToken (const CAToken_t token, uint8_t tokenLength);
191 * Search the list of observers for the specified observe ID.
193 * @param observeId Observer ID to search for.
195 * @return Pointer to found observer.
197 ResourceObserver* GetObserverUsingId (const OCObservationId observeId);
200 * Add observe header option to a request.
202 * @param caHdrOpt Target request CA header option.
203 * @param ocHdrOpt Pointer to existing options.
204 * @param numOptions Number of existing options.
205 * @param observeFlag Register/de-register observation. Should be either
206 * ::OC_OBSERVE_REGISTER or ::OC_OBSERVE_DEREGISTER.
208 * @return ::OC_STACK_OK on success, some other value upon failure.
211 CreateObserveHeaderOption (CAHeaderOption_t **caHdrOpt,
212 OCHeaderOption *ocHdrOpt,
214 uint8_t observeFlag);
217 * Copy the observe option from a received request.
219 * @param observationOption Pointer to observe option value. Should be either
220 * ::OC_OBSERVE_REGISTER, ::OC_OBSERVE_DEREGISTER, or
221 * ::OC_OBSERVE_NO_OPTION if observe not found.
223 * @param options Options in received request. Observe option is removed if found.
224 * @param numOptions Number of options in the received request. Decremented if observe
225 * option is extracted.
227 * @return ::OC_STACK_OK on success, some other value upon failure.
230 GetObserveHeaderOption (uint32_t * observationOption,
231 CAHeaderOption_t *options,
232 uint8_t * numOptions);
234 #endif //OC_OBSERVE_H