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