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