Merge "Merge branch 'master' into resource-manipulation" into resource-manipulation
[platform/upstream/iotivity.git] / resource / csdk / stack / include / ocstack.h
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #ifndef OCSTACK_H_
22 #define OCSTACK_H_
23
24 #include <stdio.h>
25 #include <stdint.h>
26 #include "octypes.h"
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif // __cplusplus
31 #define USE_RANDOM_PORT (0)
32
33 //-----------------------------------------------------------------------------
34 // Function prototypes
35 //-----------------------------------------------------------------------------
36
37 /**
38  * Initialize the OC Stack.  Must be called prior to starting the stack.
39  *
40  * @param mode
41  *     Host device is client, server, or client-server.
42  * @param serverFlags
43  *     Default server transport flags.
44  * @param clientFlags
45  *     Default client transport flags.
46  *
47  * @return ::OC_STACK_OK on success, some other value upon failure.
48  */
49 OCStackResult OCInit1(OCMode mode, OCTransportFlags serverFlags, OCTransportFlags clientFlags);
50
51 /**
52  * Initialize the OC Stack.  Must be called prior to starting the stack.
53  *
54  * @param ipAddr
55  *     IP Address of host device. Deprecated parameter.
56  * @param port
57  *     Port of host device. Deprecated parameter.
58  * @param mode
59  *     Host device is client, server, or client-server.
60  *
61  * @return ::OC_STACK_OK on success, some other value upon failure.
62  */
63 OCStackResult OCInit(const char *ipAddr, uint16_t port, OCMode mode);
64
65 /**
66  * Stop the OC stack.  Use for a controlled shutdown.
67  *
68  * Note: OCStop() performs operations similar to OCStopPresence(), as well as OCDeleteResource() on
69  * all resources this server is hosting. OCDeleteResource() performs operations similar to
70  * OCNotifyAllObservers() to notify all client observers that the respective resource is being
71  * deleted.
72  *
73  * @return ::OC_STACK_OK on success, some other value upon failure.
74  */
75 OCStackResult OCStop();
76
77 /**
78  * Called in main loop of OC client or server.  Allows low-level processing of
79  * stack services.
80  *
81  * @return ::OC_STACK_OK on success, some other value upon failure.
82  */
83 OCStackResult OCProcess();
84
85 /**
86  * Discover or Perform requests on a specified resource (specified by that Resource's respective
87  * URI).
88  *
89  * @param handle             @ref OCDoHandle to refer to the request sent out on behalf of
90  *                           calling this API. This handle can be used to cancel this operation
91  *                           via the OCCancel API.
92  *                           Note: This reference is handled internally, and
93  *                           should not be free'd by the consumer.  A NULL handle is permitted
94  *                           in the event where the caller has no use for the return value.
95  * @param method             @ref OCMethod to perform on the resource.
96  * @param requiredUri        URI of the resource to interact with. (Address prefix
97  *                           is deprecated in favor of destination.)
98  * @param destination        Complete description of destination.
99  * @param request            JSON encoded request.
100  * @param connectivityType   Modifier flags when destination is not given.
101  * @param qos                Quality of service. Note that if this API is called on a uri with
102  *                           the well-known multicast IP address, the qos will be forced to
103  *                           ::OC_LOW_QOS
104  *                           since it is impractical to send other QOS levels on such addresses.
105  * @param cbData             Asynchronous callback function that is invoked
106  *                           by the stack when discovery or resource interaction is complete.
107  * @param options            The address of an array containing the vendor specific
108  *                           header options to be sent with the request.
109  * @param numOptions         Number of header options to be included.
110  *
111  * Note: Presence subscription amendments (ie. adding additional resource type filters by calling
112  * this API again) require the use of the same base URI as the original request to successfully
113  * amend the presence filters.
114  *
115  * @return ::OC_STACK_OK on success, some other value upon failure.
116  */
117 OCStackResult OCDoResource(OCDoHandle *handle,
118                             OCMethod method,
119                             const char *requestUri,
120                             const OCDevAddr *destination,
121                             const char *request,
122                             OCConnectivityType connectivityType,
123                             OCQualityOfService qos,
124                             OCCallbackData *cbData,
125                             OCHeaderOption *options,
126                             uint8_t numOptions);
127 /**
128  * Cancel a request associated with a specific @ref OCDoResource invocation.
129  *
130  * @param handle - Used to identify a specific OCDoResource invocation.
131  * @param qos    - used to specify Quality of Service (read below for more info)
132  * @param options- used to specify vendor specific header options when sending
133  *                 explicit observe cancellation
134  * @param numOptions- Number of header options to be included
135  *
136  * @return ::OC_STACK_OK on success, some other value upon failure.
137  */
138 OCStackResult OCCancel(OCDoHandle handle, OCQualityOfService qos, OCHeaderOption * options,
139         uint8_t numOptions);
140
141 /**
142  * @brief   Register Persistent storage callback.
143  * @param   persistentStorageHandler [IN] Pointers to open, read, write, close & unlink handlers.
144  * @return
145  *     OC_STACK_OK    - No errors; Success
146  *     OC_STACK_INVALID_PARAM - Invalid parameter
147  */
148 OCStackResult OCRegisterPersistentStorageHandler(OCPersistentStorage* persistentStorageHandler);
149
150 #ifdef WITH_PRESENCE
151 /**
152  * When operating in @ref OCServer or @ref OCClientServer mode, this API will start sending out
153  * presence notifications to clients via multicast. Once this API has been called with a success,
154  * clients may query for this server's presence and this server's stack will respond via multicast.
155  *
156  * Server can call this function when it comes online for the first time, or when it comes back
157  * online from offline mode, or when it re enters network.
158  *
159  * @param ttl Time To Live in seconds.
160  * Note: If ttl is '0', then the default stack value will be used (60 Seconds).
161  *
162  *       If ttl is greater than OC_MAX_PRESENCE_TTL_SECONDS, then the ttl will be set to
163  *       OC_MAX_PRESENCE_TTL_SECONDS.
164  *
165  * @return ::OC_STACK_OK on success, some other value upon failure.
166  */
167 OCStackResult OCStartPresence(const uint32_t ttl);
168
169 /**
170  * When operating in @ref OCServer or @ref OCClientServer mode, this API will stop sending out
171  * presence notifications to clients via multicast. Once this API has been called with a success,
172  * this server's stack will not respond to clients querying for this server's presence.
173  *
174  * Server can call this function when it is terminating, going offline, or when going
175  * away from network.
176  *
177  * @return ::OC_STACK_OK on success, some other value upon failure.
178  */
179
180 OCStackResult OCStopPresence();
181 #endif
182
183
184 /**
185  * Set default device entity handler.
186  *
187  * @param entityHandler Entity handler function that is called by ocstack to handle requests for
188  *                      any undefined resources or default actions.
189  *                      If NULL is passed it removes the device default entity handler.
190  * @param callbackParameter paramter passed back when entityHandler is called.
191  *
192  * @return ::OC_STACK_OK on success, some other value upon failure.
193  */
194 OCStackResult OCSetDefaultDeviceEntityHandler(OCDeviceEntityHandler entityHandler, void* callbackParameter);
195
196 /**
197  * Set device information.
198  *
199  * @param deviceInfo - Structure passed by the server application containing
200  *                     the device information.
201  *
202  *
203  * @return
204  *     OC_STACK_OK              - no errors
205  *     OC_STACK_INVALID_PARAM   - invalid paramerter
206  *     OC_STACK_ERROR           - stack process error
207  */
208 OCStackResult OCSetDeviceInfo(OCDeviceInfo deviceInfo);
209
210 /**
211  * Set platform information.
212  *
213  * @param platformInfo - Structure passed by the server application containing
214  *                     the platform information.
215  *
216  *
217  * @return
218  *     OC_STACK_OK              - no errors
219  *     OC_STACK_INVALID_PARAM   - invalid paramerter
220  *     OC_STACK_ERROR           - stack process error
221  */
222 OCStackResult OCSetPlatformInfo(OCPlatformInfo platformInfo);
223
224 /**
225  * Create a resource.
226  *
227  * @param handle Pointer to handle to newly created resource.  Set by ocstack and
228  *               used to refer to resource.
229  * @param resourceTypeName Name of resource type.  Example: "core.led".
230  * @param resourceInterfaceName Name of resource interface.  Example: "core.rw".
231  * @param uri URI of the resource.  Example:  "/a/led".
232  * @param entityHandler Entity handler function that is called by ocstack to handle requests, etc.
233  *                      NULL for default entity handler.
234  * @param callbackParameter paramter passed back when entityHandler is called.
235  * @param resourceProperties Properties supported by resource.
236  *                           Example: ::OC_DISCOVERABLE|::OC_OBSERVABLE.
237  *
238  * @return ::OC_STACK_OK on success, some other value upon failure.
239  */
240 OCStackResult OCCreateResource(OCResourceHandle *handle,
241                                const char *resourceTypeName,
242                                const char *resourceInterfaceName,
243                                const char *uri,
244                                OCEntityHandler entityHandler,
245                                void* callbackParam,
246                                uint8_t resourceProperties);
247
248
249 /**
250  * Add a resource to a collection resource.
251  *
252  * @param collectionHandle Handle to the collection resource.
253  * @param resourceHandle Handle to resource to be added to the collection resource.
254  *
255  * @return ::OC_STACK_OK on success, some other value upon failure.
256  */
257 OCStackResult OCBindResource(OCResourceHandle collectionHandle, OCResourceHandle resourceHandle);
258
259 /**
260  * Remove a resource from a collection resource.
261  *
262  * @param collectionHandle Handle to the collection resource.
263  * @param resourceHandle Handle to resource to be removed from the collection resource.
264  *
265  * @return ::OC_STACK_OK on success, some other value upon failure.
266  */
267 OCStackResult OCUnBindResource(OCResourceHandle collectionHandle, OCResourceHandle resourceHandle);
268
269 /**
270  * Bind a resourcetype to a resource.
271  *
272  * @param handle Handle to the resource.
273  * @param resourceTypeName Name of resource type.  Example: "core.led".
274  *
275  * @return ::OC_STACK_OK on success, some other value upon failure.
276  */
277 OCStackResult OCBindResourceTypeToResource(OCResourceHandle handle,
278                                            const char *resourceTypeName);
279 /**
280  * Bind a resource interface to a resource.
281  *
282  * @param handle Handle to the resource.
283  * @param resourceInterfaceName Name of resource interface.  Example: "core.rw".
284  *
285  * @return ::OC_STACK_OK on success, some other value upon failure.
286  */
287 OCStackResult OCBindResourceInterfaceToResource(OCResourceHandle handle,
288                                                 const char *resourceInterfaceName);
289
290 /**
291  * Bind an entity handler to the resource.
292  *
293  * @param handle Handle to the resource that the contained resource is to be bound.
294  * @param entityHandler Entity handler function that is called by ocstack to handle requests, etc.
295  * @param callbackParameter context paremeter that will be passed to entityHandler
296  * @return ::OC_STACK_OK on success, some other value upon failure.
297  */
298 OCStackResult OCBindResourceHandler(OCResourceHandle handle, OCEntityHandler entityHandler,
299                                         void *callbackParameter);
300
301 /**
302  * Get the number of resources that have been created in the stack.
303  *
304  * @param numResources Pointer to count variable.
305  *
306  * @return ::OC_STACK_OK on success, some other value upon failure.
307  */
308 OCStackResult OCGetNumberOfResources(uint8_t *numResources);
309
310 /**
311  * Get a resource handle by index.
312  *
313  * @param index Index of resource, 0 to Count - 1.
314  *
315  * @return Found resource handle or NULL if not found.
316  */
317 OCResourceHandle OCGetResourceHandle(uint8_t index);
318
319 /**
320  * Delete resource specified by handle.  Deletes resource and all resourcetype and resourceinterface
321  * linked lists.
322  *
323  * Note: OCDeleteResource() performs operations similar to OCNotifyAllObservers() to notify all
324  * client observers that "this" resource is being deleted.
325  *
326  * @param handle Handle of resource to be deleted.
327  *
328  * @return ::OC_STACK_OK on success, some other value upon failure.
329  */
330 OCStackResult OCDeleteResource(OCResourceHandle handle);
331
332 /**
333  * Get the URI of the resource specified by handle.
334  *
335  * @param handle Handle of resource.
336  * @return URI string if resource found or NULL if not found.
337  */
338 const char *OCGetResourceUri(OCResourceHandle handle);
339
340 /**
341  * Get the properties of the resource specified by handle.
342  * NOTE: that after a resource is created, the OC_ACTIVE property is set
343  * for the resource by the stack.
344  *
345  * @param handle Handle of resource.
346  * @return OCResourceProperty Bitmask or -1 if resource is not found.
347  */
348 OCResourceProperty OCGetResourceProperties(OCResourceHandle handle);
349
350 /**
351  * Get the number of resource types of the resource.
352  *
353  * @param handle Handle of resource.
354  * @param numResourceTypes Pointer to count variable.
355  *
356  * @return ::OC_STACK_OK on success, some other value upon failure.
357  */
358 OCStackResult OCGetNumberOfResourceTypes(OCResourceHandle handle, uint8_t *numResourceTypes);
359
360 /**
361  * Get name of resource type of the resource.
362  *
363  * @param handle Handle of resource.
364  * @param index Index of resource, 0 to Count - 1.
365  *
366  * @return Resource type name if resource found or NULL if resource not found.
367  */
368 const char *OCGetResourceTypeName(OCResourceHandle handle, uint8_t index);
369
370 /**
371  * Get the number of resource interfaces of the resource.
372  *
373  * @param handle Handle of resource.
374  * @param numResourceInterfaces Pointer to count variable.
375  *
376  * @return ::OC_STACK_OK on success, some other value upon failure.
377  */
378 OCStackResult OCGetNumberOfResourceInterfaces(OCResourceHandle handle,
379         uint8_t *numResourceInterfaces);
380
381 /**
382  * Get name of resource interface of the resource.
383  *
384  * @param handle Handle of resource.
385  * @param index Index of resource, 0 to Count - 1.
386  *
387  * @return Resource interface name if resource found or NULL if resource not found.
388  */
389 const char *OCGetResourceInterfaceName(OCResourceHandle handle, uint8_t index);
390
391 /**
392  * Get methods of resource interface of the resource.
393  *
394  * @param handle Handle of resource.
395  * @param index Index of resource, 0 to Count - 1.
396  *
397  * @return Allowed methods if resource found or NULL if resource not found.
398  */
399 uint8_t OCGetResourceInterfaceAllowedMethods(OCResourceHandle handle, uint8_t index);
400
401 /**
402  * Get resource handle from the collection resource by index.
403  *
404  * @param collectionHandle Handle of collection resource.
405  * @param index Index of contained resource, 0 to Count - 1.
406  *
407  * @return Handle to contained resource if resource found or NULL if resource not found.
408  */
409 OCResourceHandle OCGetResourceHandleFromCollection(OCResourceHandle collectionHandle,
410         uint8_t index);
411
412 /**
413  * Get the entity handler for a resource.
414  *
415  * @param handle Handle of resource.
416  *
417  * @return Entity handler if resource found or NULL resource not found.
418  */
419 OCEntityHandler OCGetResourceHandler(OCResourceHandle handle);
420
421 /**
422  * Notify all registered observers that the resource representation has
423  * changed. If observation includes a query the client is notified only
424  * if the query is valid after the resource representation has changed.
425  *
426  * @param handle Handle of resource.
427  * @param qos    Desired quality of service for the observation notifications.
428  *
429  * @return ::OC_STACK_OK on success, some other value upon failure.
430  */
431 OCStackResult OCNotifyAllObservers(OCResourceHandle handle, OCQualityOfService qos);
432
433 /**
434  * Notify specific observers with updated value of representation.
435  * Before this API is invoked by entity handler it has finished processing
436  * queries for the associated observers.
437  *
438  * @param handle Handle of resource.
439  * @param obsIdList List of observation ids that need to be notified.
440  * @param numberOfIds Number of observation ids included in obsIdList.
441  * @param notificationJSONPayload JSON encoded payload to send in notification.
442  * @param qos Desired quality of service of the observation notifications.
443  * NOTE: The memory for obsIdList and notificationJSONPayload is managed by the
444  * entity invoking the API. The maximum size of the notification is 1015 bytes
445  * for non-Arduino platforms. For Arduino the maximum size is 247 bytes.
446  *
447  * @return ::OC_STACK_OK on success, some other value upon failure.
448  */
449 OCStackResult
450 OCNotifyListOfObservers (OCResourceHandle handle,
451                             OCObservationId  *obsIdList,
452                             uint8_t          numberOfIds,
453                             const char    *notificationJSONPayload,
454                             OCQualityOfService qos);
455
456
457 /**
458  * Send a response to a request.
459  * The response can be a normal, slow, or block (i.e. a response that
460  * is too large to be sent in a single PDU and must span multiple transmissions).
461  *
462  * @param response Pointer to structure that contains response parameters.
463  *
464  * @return ::OC_STACK_OK on success, some other value upon failure.
465  */
466 OCStackResult OCDoResponse(OCEntityHandlerResponse *response);
467
468 #ifdef __cplusplus
469 }
470 #endif // __cplusplus
471
472 #endif /* OCSTACK_H_ */