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