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