Imported Upstream version 1.1.1
[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  ::OC_STACK_OK on success, some other value upon failure.
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 received. The discovery could be
138  *                          related to filtered/scoped/particular resource. The callback is
139  *                          generated for each response received.
140  * @param options           The address of an array containing the vendor specific header options
141  *                          to be sent with the request.
142  * @param numOptions        Number of header options to be included.
143  *
144  * @note: Presence subscription amendments (i.e. adding additional resource type filters by calling
145  * this API again) require the use of the same base URI as the original request to successfully
146  * amend the presence filters.
147  *
148  * @return ::OC_STACK_OK on success, some other value upon failure.
149  */
150 OCStackResult OCDoResource(OCDoHandle *handle,
151                             OCMethod method,
152                             const char *requestUri,
153                             const OCDevAddr *destination,
154                             OCPayload* payload,
155                             OCConnectivityType connectivityType,
156                             OCQualityOfService qos,
157                             OCCallbackData *cbData,
158                             OCHeaderOption *options,
159                             uint8_t numOptions);
160 /**
161  * This function cancels a request associated with a specific @ref OCDoResource invocation.
162  *
163  * @param handle       Used to identify a specific OCDoResource invocation.
164  * @param qos          Used to specify Quality of Service(read below).
165  * @param options      Used to specify vendor specific header options when sending
166  *                     explicit observe cancellation.
167  * @param numOptions   Number of header options to be included.
168  *
169  * @return ::OC_STACK_OK on success, some other value upon failure.
170  */
171 OCStackResult OCCancel(OCDoHandle handle, OCQualityOfService qos, OCHeaderOption * options,
172         uint8_t numOptions);
173
174 /**
175  * Register Persistent storage callback.
176  * @param   persistentStorageHandler  Pointers to open, read, write, close & unlink handlers.
177  *
178  * @return
179  *     OC_STACK_OK                    No errors; Success.
180  *     OC_STACK_INVALID_PARAM         Invalid parameter.
181  */
182 OCStackResult OCRegisterPersistentStorageHandler(OCPersistentStorage* persistentStorageHandler);
183
184 #ifdef WITH_PRESENCE
185 /**
186  * When operating in  OCServer or  OCClientServer mode,
187  * this API will start sending out presence notifications to clients via multicast.
188  * Once this API has been called with a success, clients may query for this server's presence and
189  * this server's stack will respond via multicast.
190  *
191  * Server can call this function when it comes online for the first time, or when it comes back
192  * online from offline mode, or when it re enters network.
193  *
194  * @param ttl         Time To Live in seconds.
195  *                    @note: If ttl is '0', then the default stack value will be used (60 Seconds).
196  *                    If ttl is greater than ::OC_MAX_PRESENCE_TTL_SECONDS, then the ttl will be
197  *                    set to ::OC_MAX_PRESENCE_TTL_SECONDS.
198  *
199  * @return ::OC_STACK_OK on success, some other value upon failure.
200  */
201 OCStackResult OCStartPresence(const uint32_t ttl);
202
203 /**
204  * When operating in OCServer or OCClientServer mode, this API will stop sending
205  * out presence notifications to clients via multicast.
206  * Once this API has been called with a success this server's stack will not respond to clients
207  * querying for this server's presence.
208  *
209  * Server can call this function when it is terminating, going offline, or when going
210  * away from network.
211  *
212  * @return ::OC_STACK_OK on success, some other value upon failure.
213  */
214
215 OCStackResult OCStopPresence();
216 #endif
217
218
219 /**
220  * This function sets default device entity handler.
221  *
222  * @param entityHandler      Entity handler function that is called by ocstack to handle requests
223  *                           for any undefined resources or default actions.If NULL is passed it
224  *                           removes the device default entity handler.
225  * @param callbackParameter  Parameter passed back when entityHandler is called.
226  *
227  * @return ::OC_STACK_OK on success, some other value upon failure.
228  */
229 OCStackResult OCSetDefaultDeviceEntityHandler(OCDeviceEntityHandler entityHandler, void* callbackParameter);
230
231 /**
232  * This function sets device information.
233  *
234  * Upon call to OCInit, the default device type (i.e. "rt") has already been set to the default
235  * Device Type "oic.wk.d". You do not have to specify "oic.wk.d" in the OCDeviceInfo.types linked
236  * list. The default Device Type is mandatory and always specified by this Device as the first
237  * device type.
238  *
239  * @param deviceInfo   Structure passed by the server application containing the device
240  *                     information.
241  *
242  * @return
243  *     ::OC_STACK_OK               no errors.
244  *     ::OC_STACK_INVALID_PARAM    invalid parameter.
245  *     ::OC_STACK_ERROR            stack process error.
246  */
247 OCStackResult OCSetDeviceInfo(OCDeviceInfo deviceInfo);
248
249 /**
250  * This function sets platform information.
251  *
252  * @param platformInfo   Structure passed by the server application containing
253  *                       the platform information.
254  *
255  *
256  * @return
257  *     ::OC_STACK_OK               no errors.
258  *     ::OC_STACK_INVALID_PARAM    invalid parameter.
259  *     ::OC_STACK_ERROR            stack process error.
260  */
261 OCStackResult OCSetPlatformInfo(OCPlatformInfo platformInfo);
262
263 /**
264  * This function creates a resource.
265  *
266  * @param handle                Pointer to handle to newly created resource. Set by ocstack and
267  *                              used to refer to resource.
268  * @param resourceTypeName      Name of resource type.  Example: "core.led".
269  * @param resourceInterfaceName Name of resource interface.  Example: "core.rw".
270  * @param uri                   URI of the resource.  Example:  "/a/led".
271  * @param entityHandler         Entity handler function that is called by ocstack to handle
272  *                              requests, etc.
273  *                              NULL for default entity handler.
274  * @param callbackParam     parameter passed back when entityHandler is called.
275  * @param resourceProperties    Properties supported by resource.
276  *                              Example: ::OC_DISCOVERABLE|::OC_OBSERVABLE.
277  *
278  * @return ::OC_STACK_OK on success, some other value upon failure.
279  */
280 OCStackResult OCCreateResource(OCResourceHandle *handle,
281                                const char *resourceTypeName,
282                                const char *resourceInterfaceName,
283                                const char *uri,
284                                OCEntityHandler entityHandler,
285                                void* callbackParam,
286                                uint8_t resourceProperties);
287
288
289 /**
290  * This function adds a resource to a collection resource.
291  *
292  * @param collectionHandle    Handle to the collection resource.
293  * @param resourceHandle      Handle to resource to be added to the collection resource.
294  *
295  * @return ::OC_STACK_OK on success, some other value upon failure.
296  */
297 OCStackResult OCBindResource(OCResourceHandle collectionHandle, OCResourceHandle resourceHandle);
298
299 /**
300  * This function removes a resource from a collection resource.
301  *
302  * @param collectionHandle   Handle to the collection resource.
303  * @param resourceHandle     Handle to resource to be removed from the collection resource.
304  *
305  * @return ::OC_STACK_OK on success, some other value upon failure.
306  */
307 OCStackResult OCUnBindResource(OCResourceHandle collectionHandle, OCResourceHandle resourceHandle);
308
309 /**
310  * This function binds a resource type to a resource.
311  *
312  * @param handle            Handle to the resource.
313  * @param resourceTypeName  Name of resource type.  Example: "core.led".
314  *
315  * @return ::OC_STACK_OK on success, some other value upon failure.
316  */
317 OCStackResult OCBindResourceTypeToResource(OCResourceHandle handle,
318                                            const char *resourceTypeName);
319 /**
320  * This function binds a resource interface to a resource.
321  *
322  * @param handle                  Handle to the resource.
323  * @param resourceInterfaceName   Name of resource interface.  Example: "core.rw".
324  *
325  * @return ::OC_STACK_OK on success, some other value upon failure.
326  */
327 OCStackResult OCBindResourceInterfaceToResource(OCResourceHandle handle,
328                                                 const char *resourceInterfaceName);
329
330 /**
331  * This function binds an entity handler to the resource.
332  *
333  * @param handle            Handle to the resource that the contained resource is to be bound.
334  * @param entityHandler     Entity handler function that is called by ocstack to handle requests.
335  * @param callbackParameter Context parameter that will be passed to entityHandler.
336  *
337  * @return ::OC_STACK_OK on success, some other value upon failure.
338  */
339 OCStackResult OCBindResourceHandler(OCResourceHandle handle, OCEntityHandler entityHandler,
340                                         void *callbackParameter);
341
342 /**
343  * This function gets the number of resources that have been created in the stack.
344  *
345  * @param numResources    Pointer to count variable.
346  *
347  * @return ::OC_STACK_OK on success, some other value upon failure.
348  */
349 OCStackResult OCGetNumberOfResources(uint8_t *numResources);
350
351 /**
352  * This function gets a resource handle by index.
353  *
354  * @param index   Index of resource, 0 to Count - 1.
355  *
356  * @return Found  resource handle or NULL if not found.
357  */
358 OCResourceHandle OCGetResourceHandle(uint8_t index);
359
360 /**
361  * This function deletes resource specified by handle.  Deletes resource and all
362  * resource type and resource interface linked lists.
363  *
364  * @note: OCDeleteResource() performs operations similar to OCNotifyAllObservers() to notify all
365  * client observers that "this" resource is being deleted.
366  *
367  * @param handle          Handle of resource to be deleted.
368  *
369  * @return ::OC_STACK_OK on success, some other value upon failure.
370  */
371 OCStackResult OCDeleteResource(OCResourceHandle handle);
372
373 /**
374  * Get a string representation the server instance ID.
375  * The memory is managed internal to this function, so freeing externally will result
376  * in a runtime error.
377  * Note: This will NOT seed the RNG, so it must be called after the RNG is seeded.
378  * This is done automatically during the OCInit process,
379  * so ensure that this call is done after that.
380  *
381  * @return A string representation  the server instance ID.
382  */
383 const char* OCGetServerInstanceIDString(void);
384
385 /**
386  * This function gets the URI of the resource specified by handle.
387  *
388  * @param handle     Handle of resource.
389  *
390  * @return URI string if resource found or NULL if not found.
391  */
392 const char *OCGetResourceUri(OCResourceHandle handle);
393
394 /**
395  * This function gets the properties of the resource specified by handle.
396  *
397  * @param handle                Handle of resource.
398  *
399  * @return OCResourceProperty   Bitmask or -1 if resource is not found.
400  *
401  * @note that after a resource is created, the OC_ACTIVE property is set for the resource by the
402  * stack.
403  */
404 OCResourceProperty OCGetResourceProperties(OCResourceHandle handle);
405
406 /**
407  * This function gets the number of resource types of the resource.
408  *
409  * @param handle            Handle of resource.
410  * @param numResourceTypes  Pointer to count variable.
411  *
412  * @return ::OC_STACK_OK on success, some other value upon failure.
413  */
414 OCStackResult OCGetNumberOfResourceTypes(OCResourceHandle handle, uint8_t *numResourceTypes);
415
416 /**
417  * This function gets name of resource type of the resource.
418  *
419  * @param handle       Handle of resource.
420  * @param index        Index of resource, 0 to Count - 1.
421  *
422  * @return Resource type name if resource found or NULL if resource not found.
423  */
424 const char *OCGetResourceTypeName(OCResourceHandle handle, uint8_t index);
425
426 /**
427  * This function gets the number of resource interfaces of the resource.
428  *
429  * @param handle                 Handle of resource.
430  * @param numResourceInterfaces  Pointer to count variable.
431  *
432  * @return ::OC_STACK_OK on success, some other value upon failure.
433  */
434 OCStackResult OCGetNumberOfResourceInterfaces(OCResourceHandle handle,
435         uint8_t *numResourceInterfaces);
436
437 /**
438  * This function gets name of resource interface of the resource.
439  *
440  * @param handle      Handle of resource.
441  * @param index       Index of resource, 0 to Count - 1.
442  *
443  * @return Resource interface name if resource found or NULL if resource not found.
444  */
445 const char *OCGetResourceInterfaceName(OCResourceHandle handle, uint8_t index);
446
447 /**
448  * This function gets methods of resource interface of the resource.
449  *
450  * @param handle      Handle of resource.
451  * @param index       Index of resource, 0 to Count - 1.
452  *
453  * @return Allowed methods if resource found or NULL if resource not found.
454  */
455 uint8_t OCGetResourceInterfaceAllowedMethods(OCResourceHandle handle, uint8_t index);
456
457 /**
458  * This function gets resource handle from the collection resource by index.
459  *
460  * @param collectionHandle   Handle of collection resource.
461  * @param index              Index of contained resource, 0 to Count - 1.
462  *
463  * @return Handle to contained resource if resource found or NULL if resource not found.
464  */
465 OCResourceHandle OCGetResourceHandleFromCollection(OCResourceHandle collectionHandle,
466         uint8_t index);
467
468 /**
469  * This function gets the entity handler for a resource.
470  *
471  * @param handle            Handle of resource.
472  *
473  * @return Entity handler if resource found or NULL resource not found.
474  */
475 OCEntityHandler OCGetResourceHandler(OCResourceHandle handle);
476
477 /**
478  * This function notify all registered observers that the resource representation has
479  * changed. If observation includes a query the client is notified only if the query is valid after
480  * the resource representation has changed.
481  *
482  * @param handle   Handle of resource.
483  * @param qos      Desired quality of service for the observation notifications.
484  *
485  * @return ::OC_STACK_OK on success, some other value upon failure.
486  */
487 OCStackResult OCNotifyAllObservers(OCResourceHandle handle, OCQualityOfService qos);
488
489 /**
490  * Notify specific observers with updated value of representation.
491  * Before this API is invoked by entity handler it has finished processing
492  * queries for the associated observers.
493  *
494  * @param handle                    Handle of resource.
495  * @param obsIdList                 List of observation IDs that need to be notified.
496  * @param numberOfIds               Number of observation IDs included in obsIdList.
497  * @param payload                   Object representing the notification
498  * @param qos                       Desired quality of service of the observation notifications.
499  *
500  * @note: The memory for obsIdList and payload is managed by the entity invoking the API.
501  * The maximum size of the notification is 1015 bytes for non-Arduino platforms. For Arduino
502  * the maximum size is 247 bytes.
503  *
504  * @return ::OC_STACK_OK on success, some other value upon failure.
505  */
506 OCStackResult
507 OCNotifyListOfObservers (OCResourceHandle handle,
508                             OCObservationId  *obsIdList,
509                             uint8_t          numberOfIds,
510                             const OCRepPayload *payload,
511                             OCQualityOfService qos);
512
513
514 /**
515  * This function sends a response to a request.
516  * The response can be a normal, slow, or block (i.e. a response that
517  * is too large to be sent in a single PDU and must span multiple transmissions).
518  *
519  * @param response   Pointer to structure that contains response parameters.
520  *
521  * @return ::OC_STACK_OK on success, some other value upon failure.
522  */
523 OCStackResult OCDoResponse(OCEntityHandlerResponse *response);
524
525 //#ifdef DIRECT_PAIRING
526 /**
527  * The function is responsible for discovery of direct-pairing device is current subnet. It will list
528  * all the device in subnet which support direct-pairing.
529  * Caller must NOT free returned constant pointer
530  *
531  * @param[in] timeout Timeout in seconds, value till which function will listen to responses from
532  *                    client before returning the list of devices.
533  * @return OCDirectPairingDev_t pointer in case of success and NULL otherwise.
534  */
535 const OCDPDev_t* OCDiscoverDirectPairingDevices(unsigned short waittime);
536
537 /**
538  * The function is responsible for return of paired device list via direct-pairing. It will list
539  * all the device which is previousely paired with client.
540  * Caller must NOT free returned constant pointer
541  *
542  * @return OCDirectPairingDev_t pointer in case of success and NULL otherwise.
543  */
544 const OCDPDev_t* OCGetDirectPairedDevices();
545
546 /**
547  * The function is responsible for establishment of direct-pairing. It will proceed mode negotiation
548  * and connect PIN based dtls session.
549  *
550  * @param[in] peer Target device to establish direct-pairing.
551  * @param[in] pmSel Selected mode of pairing.
552  * @param[in] pinNumber PIN number for authentication, pin lenght is defined DP_PIN_LENGTH(8).
553  * @param[in] resultCallback Callback fucntion to event status of process.
554  * @return OTM_SUCCESS in case of success and other value otherwise.
555  */
556 OCStackResult OCDoDirectPairing(void *ctx, OCDPDev_t* peer, OCPrm_t pmSel, char *pinNumber,
557                                                      OCDirectPairingCB resultCallback);
558 //#endif // DIRECT_PAIRING
559
560 #ifdef __cplusplus
561 }
562 #endif // __cplusplus
563
564 #endif /* OCSTACK_H_ */