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;
86 * Create an observe response and send to all observers in the observe list.
88 * @param method RESTful method.
89 * @param resPtr Observed resource.
90 * @param maxAge Time To Live (in seconds) of observation.
91 * @param resourceType Resource type. Allows resource type name to be added to response.
92 * @param qos Quality of service of resource.
94 * @return ::OC_STACK_OK on success, some other value upon failure.
96 OCStackResult SendAllObserverNotification (OCMethod method, OCResource *resPtr, uint32_t maxAge,
97 OCPresenceTrigger trigger, OCResourceType *resourceType, OCQualityOfService qos);
100 * Create an observe response and send to all observers in the observe list.
102 * @param method RESTful method.
103 * @param resPtr Observed resource.
104 * @param maxAge Time To Live (in seconds) of observation.
105 * @param qos Quality of service of resource.
107 * @return ::OC_STACK_OK on success, some other value upon failure.
109 OCStackResult SendAllObserverNotification (OCMethod method, OCResource *resPtr, uint32_t maxAge,
110 OCQualityOfService qos);
114 * Notify specific observers with updated value of representation.
116 * @param resource Observed resource.
117 * @param obsIdList List of observation ids that need to be notified.
118 * @param numberOfIds Number of observation ids included in obsIdList.
119 * @param notificationJSONPayload JSON encoded payload to send in notification.
120 * @param maxAge Time To Live (in seconds) of observation.
121 * @param qos Desired quality of service of the observation notifications.
123 * @return ::OC_STACK_OK on success, some other value upon failure.
125 OCStackResult SendListObserverNotification (OCResource * resource,
126 OCObservationId *obsIdList, uint8_t numberOfIds,
127 const OCRepPayload *payload, uint32_t maxAge,
128 OCQualityOfService qos);
131 * Delete all observers in the observe list.
133 void DeleteObserverList();
136 * Create a unique observation ID.
138 * @param observationId Pointer to generated ID.
140 * @return ::OC_STACK_OK on success, some other value upon failure.
142 OCStackResult GenerateObserverId (OCObservationId *observationId);
145 * Add observer for a resource.
147 * @param resUri Resource URI string.
148 * @param query Query string.
149 * @param obsId Observation ID.
150 * @param token Request token.
151 * @param tokenLength Length of token.
152 * @param resHandle Resource handle.
153 * @param qos Quality of service of observation.
154 * @param devAddr Device address.
156 * @return ::OC_STACK_OK on success, some other value upon failure.
158 OCStackResult AddObserver (const char *resUri,
160 OCObservationId obsId,
163 OCResource *resHandle,
164 OCQualityOfService qos,
165 const OCDevAddr *devAddr);
168 * Delete observer with specified token from list of observers.
169 * Free memory that was allocated for the observer in the list.
171 * @param token Token to search for.
172 * @param tokenLength Length of token.
174 * @return ::OC_STACK_OK on success, some other value upon failure.
176 OCStackResult DeleteObserverUsingToken (CAToken_t token, uint8_t tokenLength);
179 * Search the list of observers for the specified token.
181 * @param token Token to search for.
182 * @param tokenLength Length of token.
184 * @return Pointer to found observer.
186 ResourceObserver* GetObserverUsingToken (const CAToken_t token, uint8_t tokenLength);
189 * Search the list of observers for the specified observe ID.
191 * @param observeId Observer ID to search for.
193 * @return Pointer to found observer.
195 ResourceObserver* GetObserverUsingId (const OCObservationId observeId);
198 * Add observe header option to a request.
200 * @param caHdrOpt Target request CA header option.
201 * @param ocHdrOpt Pointer to existing options.
202 * @param numOptions Number of existing options.
203 * @param observeFlag Register/de-register observation. Should be either
204 * ::OC_OBSERVE_REGISTER or ::OC_OBSERVE_DEREGISTER.
206 * @return ::OC_STACK_OK on success, some other value upon failure.
209 CreateObserveHeaderOption (CAHeaderOption_t **caHdrOpt,
210 OCHeaderOption *ocHdrOpt,
212 uint8_t observeFlag);
215 * Copy the observe option from a received request.
217 * @param observationOption Pointer to observe option value. Should be either
218 * ::OC_OBSERVE_REGISTER, ::OC_OBSERVE_DEREGISTER, or
219 * ::OC_OBSERVE_NO_OPTION if observe not found.
221 * @param options Options in received request. Observe option is removed if found.
222 * @param numOptions Number of options in the received request. Decremented if observe
223 * option is extracted.
225 * @return ::OC_STACK_OK on success, some other value upon failure.
228 GetObserveHeaderOption (uint32_t * observationOption,
229 CAHeaderOption_t *options,
230 uint8_t * numOptions);
232 #endif //OC_OBSERVE_H