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 /** Sequence number is a 24 bit field, per https://tools.ietf.org/html/draft-ietf-core-observe-16.*/
33 #define MAX_SEQUENCE_NUMBER (0xFFFFFF)
35 /** Maximum number of observers to reach */
37 #define MAX_OBSERVER_FAILED_COMM (2)
39 /** Maximum number of observers to reach for resources with low QOS */
40 #define MAX_OBSERVER_NON_COUNT (3)
43 * Data structure to hold informations for each registered observer.
45 typedef struct ResourceObserver
47 /** Observation Identifier for request.*/
48 OCObservationId observeId;
50 /** URI of observed resource.*/
56 /** token for the observe request.*/
59 /** token length for the observe request.*/
62 /** Resource handle.*/
65 /** Remote Endpoint. */
68 /** Quality of service of the request.*/
69 OCQualityOfService qos;
71 /** number of times the server failed to reach the observer.*/
72 uint8_t failedCommCount;
74 /** number of times the server sent NON notifications.*/
77 /** force the qos value to CON.*/
80 /** next node in this list.*/
81 struct ResourceObserver *next;
83 /** requested payload encoding format. */
84 OCPayloadFormat acceptFormat;
90 * Create an observe response and send to all observers in the observe list.
92 * @param method RESTful method.
93 * @param resPtr Observed resource.
94 * @param maxAge Time To Live (in seconds) of observation.
95 * @param resourceType Resource type. Allows resource type name to be added to response.
96 * @param qos Quality of service of resource.
98 * @return ::OC_STACK_OK on success, some other value upon failure.
100 OCStackResult SendAllObserverNotification (OCMethod method, OCResource *resPtr, uint32_t maxAge,
101 OCPresenceTrigger trigger, OCResourceType *resourceType, OCQualityOfService qos);
104 * Create an observe response and send to all observers in the observe list.
106 * @param method RESTful method.
107 * @param resPtr Observed resource.
108 * @param maxAge Time To Live (in seconds) of observation.
109 * @param qos Quality of service of resource.
111 * @return ::OC_STACK_OK on success, some other value upon failure.
113 OCStackResult SendAllObserverNotification (OCMethod method, OCResource *resPtr, uint32_t maxAge,
114 OCQualityOfService qos);
118 * Notify specific observers with updated value of representation.
120 * @param resource Observed resource.
121 * @param obsIdList List of observation ids that need to be notified.
122 * @param numberOfIds Number of observation ids included in obsIdList.
123 * @param notificationJSONPayload JSON encoded payload to send in notification.
124 * @param maxAge Time To Live (in seconds) of observation.
125 * @param qos Desired quality of service of the observation notifications.
127 * @return ::OC_STACK_OK on success, some other value upon failure.
129 OCStackResult SendListObserverNotification (OCResource * resource,
130 OCObservationId *obsIdList, uint8_t numberOfIds,
131 const OCRepPayload *payload, uint32_t maxAge,
132 OCQualityOfService qos);
135 * Delete all observers in the observe list.
137 void DeleteObserverList();
140 * Create a unique observation ID.
142 * @param observationId Pointer to generated ID.
144 * @return ::OC_STACK_OK on success, some other value upon failure.
146 OCStackResult GenerateObserverId (OCObservationId *observationId);
149 * Add observer for a resource.
151 * @param resUri Resource URI string.
152 * @param query Query string.
153 * @param obsId Observation ID.
154 * @param token Request token.
155 * @param tokenLength Length of token.
156 * @param resHandle Resource handle.
157 * @param qos Quality of service of observation.
158 * @param devAddr Device address.
160 * @return ::OC_STACK_OK on success, some other value upon failure.
162 OCStackResult AddObserver (const char *resUri,
164 OCObservationId obsId,
167 OCResource *resHandle,
168 OCQualityOfService qos,
169 OCPayloadFormat acceptFormat,
170 const OCDevAddr *devAddr);
173 * Delete observer with specified token from list of observers.
174 * Free memory that was allocated for the observer in the list.
176 * @param token Token to search for.
177 * @param tokenLength Length of token.
179 * @return ::OC_STACK_OK on success, some other value upon failure.
181 OCStackResult DeleteObserverUsingToken (CAToken_t token, uint8_t tokenLength);
184 * Search the list of observers for the specified token.
186 * @param token Token to search for.
187 * @param tokenLength Length of token.
189 * @return Pointer to found observer.
191 ResourceObserver* GetObserverUsingToken (const CAToken_t token, uint8_t tokenLength);
194 * Search the list of observers for the specified observe ID.
196 * @param observeId Observer ID to search for.
198 * @return Pointer to found observer.
200 ResourceObserver* GetObserverUsingId (const OCObservationId observeId);
203 * Add observe header option to a request.
205 * @param caHdrOpt Target request CA header option.
206 * @param ocHdrOpt Pointer to existing options.
207 * @param numOptions Number of existing options.
208 * @param observeFlag Register/de-register observation. Should be either
209 * ::OC_OBSERVE_REGISTER or ::OC_OBSERVE_DEREGISTER.
211 * @return ::OC_STACK_OK on success, some other value upon failure.
214 CreateObserveHeaderOption (CAHeaderOption_t **caHdrOpt,
215 OCHeaderOption *ocHdrOpt,
217 uint8_t observeFlag);
220 * Copy the observe option from a received request.
222 * @param observationOption Pointer to observe option value. Should be either
223 * ::OC_OBSERVE_REGISTER, ::OC_OBSERVE_DEREGISTER, or
224 * ::OC_OBSERVE_NO_OPTION if observe not found.
226 * @param options Options in received request. Observe option is removed if found.
227 * @param numOptions Number of options in the received request. Decremented if observe
228 * option is extracted.
230 * @return ::OC_STACK_OK on success, some other value upon failure.
233 GetObserveHeaderOption (uint32_t * observationOption,
234 CAHeaderOption_t *options,
235 uint8_t * numOptions);
237 #endif //OC_OBSERVE_H