Fix bug to prevent continuous presence TTL timeouts.
[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 "ocstackconfig.h"
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif // __cplusplus
30 #define WITH_PRESENCE
31 //-----------------------------------------------------------------------------
32 // Defines
33 //-----------------------------------------------------------------------------
34
35 //TODO: May want to refactor this in upcoming sprints.
36 //Don't want to expose to application layer that lower level stack is using CoAP.
37
38 /// Authority + URI string to prefix well known queries
39 #define OC_WELL_KNOWN_QUERY                  "224.0.1.187:5683/oc/core"
40 #define OC_EXPLICIT_DEVICE_DISCOVERY_URI     "224.0.1.187:5683/oc/core/d?rt=core.led"
41 /// Multicast address and port string to prefix multicast queries
42 #define OC_MULTICAST_PREFIX                  "224.0.1.187:5683"
43 /// IP Multicast address to use for multicast requests
44 #define OC_MULTICAST_IP                      "224.0.1.187"
45 /// IP Multicast port to use for multicast requests
46 #define OC_MULTICAST_PORT                    5683
47
48 #ifdef WITH_PRESENCE
49 #define OC_DEFAULT_PRESENCE_TTL_SECONDS (60)
50 /// OC_MAX_PRESENCE_TTL_SECONDS sets the maximum time to live (TTL) for presence.
51 /// NOTE: Changing the setting to a longer duration may lead to unsupported and untested
52 /// operation.
53 #define OC_MAX_PRESENCE_TTL_SECONDS     (60 * 60 * 24) // 60 sec/min * 60 min/hr * 24 hr/day
54 #define OC_PRESENCE_URI                      "/oc/presence"
55 #endif
56
57 //-----------------------------------------------------------------------------
58 // Typedefs
59 //-----------------------------------------------------------------------------
60
61 /**
62  * Data structure to encapsulate IPv4/IPv6/Contiki/lwIP device addresses.
63  */
64 typedef struct OCDevAddr
65 {
66     uint32_t     size;                    // length of the address stored in addr field.
67     uint8_t      addr[DEV_ADDR_SIZE_MAX]; // device address.
68 } OCDevAddr;
69
70 /**
71  * OC Virtual resources supported by every OC device.
72  */
73 typedef enum
74 {
75     OC_WELL_KNOWN_URI= 0,       // "/oc/core"
76     OC_DEVICE_URI,              // "/oc/core/d"
77     OC_RESOURCE_TYPES_URI,      // "/oc/core/d/type"
78     #ifdef WITH_PRESENCE
79     OC_PRESENCE,                // "/oc/presence"
80     #endif
81     OC_MAX_VIRTUAL_RESOURCES    // Max items in the list
82 } OCVirtualResources;
83
84 /**
85  * Standard RESTful HTTP Methods.
86  */
87 typedef enum
88 {
89     OC_REST_NOMETHOD    = 0,
90     OC_REST_GET         = (1 << 0),     // Read
91     OC_REST_PUT         = (1 << 1),     // Write
92     OC_REST_POST        = (1 << 2),     // Update
93     OC_REST_DELETE      = (1 << 3),     // Delete
94     // Register observe request for most up date notifications ONLY.
95     OC_REST_OBSERVE     = (1 << 4),
96     // Register observe request for all notifications, including stale notifications.
97     OC_REST_OBSERVE_ALL = (1 << 5),
98     // Deregister observation, intended for internal use
99     OC_REST_CANCEL_OBSERVE = (1 << 6),
100     #ifdef WITH_PRESENCE
101     // Subscribe for all presence notifications of a particular resource.
102     OC_REST_PRESENCE    = (1 << 7)
103     #endif
104 } OCMethod;
105
106 /**
107  * Host Mode of Operation.
108  */
109 typedef enum
110 {
111     OC_CLIENT = 0,
112     OC_SERVER,
113     OC_CLIENT_SERVER
114 } OCMode;
115
116 /**
117  * Quality of Service.
118  */
119 typedef enum
120 {
121     OC_LOW_QOS = 0,
122     OC_MEDIUM_QOS,
123     OC_HIGH_QOS,
124     OC_NA_QOS // No Quality is defined, let the stack decide
125 } OCQualityOfService;
126
127 /**
128  * Resource Properties.
129  *
130  * ::OC_ACTIVE       When this bit is set, the resource is initialized, otherwise the resource
131  *                   is 'inactive'. 'inactive' signifies that the resource has been marked for
132  *                   deletion or is already deleted.
133  * ::OC_DISCOVERABLE When this bit is set, the resource is allowed to be discovered by clients.
134  * ::OC_OBSERVABLE   When this bit is set, the resource is allowed to be observed by clients.
135  * ::OC_SLOW         When this bit is set, the resource has been marked as 'slow'. 'slow' signifies
136  *                   that responses from this resource can expect delays in processing its
137  *                   requests from clients.
138  * ::OC_SECURE       When this bit is set, the resource is a secure resource.
139  */
140 typedef enum
141 {
142     OC_ACTIVE       = (1 << 0),
143     OC_DISCOVERABLE = (1 << 1),
144     OC_OBSERVABLE   = (1 << 2),
145     OC_SLOW         = (1 << 3),
146     OC_SECURE       = (1 << 4)
147 } OCResourceProperty;
148
149 /**
150  * Transport Protocol IDs.
151  */
152 typedef enum
153 {
154     OC_INVALID_ID   = (1 << 0),
155     OC_COAP_ID      = (1 << 1)
156 } OCTransportProtocolID;
157
158 /**
159  * Adaptor types.
160  */
161 typedef enum
162 {
163     OC_ETHERNET = 0,
164     OC_WIFI,
165     OC_EDR,
166     OC_LE,
167     OC_ALL // Multicast message: send over all the interfaces.
168 } OCConnectivityType;
169
170 /**
171  * Declares Stack Results & Errors.
172  */
173 typedef enum
174 {
175     /* Success status code - START HERE */
176     OC_STACK_OK = 0,
177     OC_STACK_RESOURCE_CREATED,
178     OC_STACK_RESOURCE_DELETED,
179     OC_STACK_CONTINUE,
180     /* Success status code - END HERE */
181     /* Error status code - START HERE */
182     OC_STACK_INVALID_URI = 20,
183     OC_STACK_INVALID_QUERY,
184     OC_STACK_INVALID_IP,
185     OC_STACK_INVALID_PORT,
186     OC_STACK_INVALID_CALLBACK,
187     OC_STACK_INVALID_METHOD,
188     OC_STACK_INVALID_PARAM,
189     OC_STACK_INVALID_OBSERVE_PARAM,
190     OC_STACK_NO_MEMORY,
191     OC_STACK_COMM_ERROR,
192     OC_STACK_TIMEOUT,
193     OC_STACK_ADAPTER_NOT_ENABLED,
194     OC_STACK_NOTIMPL,
195     OC_STACK_NO_RESOURCE,               /* resource not found */
196     OC_STACK_RESOURCE_ERROR,            /* ex: not supported method or interface */
197     OC_STACK_SLOW_RESOURCE,
198     OC_STACK_DUPLICATE_REQUEST,
199     OC_STACK_NO_OBSERVERS,              /* resource has no registered observers */
200     OC_STACK_OBSERVER_NOT_FOUND,
201     OC_STACK_VIRTUAL_DO_NOT_HANDLE,
202     OC_STACK_INVALID_OPTION,
203     OC_STACK_MALFORMED_RESPONSE,        /* the remote reply contained malformed data */
204     OC_STACK_PERSISTENT_BUFFER_REQUIRED,
205     OC_STACK_INVALID_REQUEST_HANDLE,
206     OC_STACK_INVALID_DEVICE_INFO,
207     OC_STACK_INVALID_JSON,
208     /* NOTE: Insert all new error codes here!*/
209     #ifdef WITH_PRESENCE
210     OC_STACK_PRESENCE_STOPPED = 128,
211     OC_STACK_PRESENCE_TIMEOUT,
212     OC_STACK_PRESENCE_DO_NOT_HANDLE,
213     #endif
214     OC_STACK_ERROR = 255
215     /* Error status code - END HERE */
216 } OCStackResult;
217
218 /**
219  * Handle to an @ref OCDoResource invocation.
220  */
221 typedef void * OCDoHandle;
222
223 /**
224  * Handle to an OCResource object owned by the OCStack.
225  */
226 typedef void * OCResourceHandle;
227
228 typedef void * OCRequestHandle;
229 typedef void * OCResponseHandle;
230
231 /**
232  * Unique identifier for each observation request. Used when observations are
233  * registered or deregistering. Used by entity handler to signal specific
234  * observers to be notified of resource changes.
235  * There can be maximum of 256 observations per server.
236  */
237 typedef uint8_t OCObservationId;
238
239 /**
240  * Action associated with observation.
241  */
242 typedef enum
243 {
244     OC_OBSERVE_REGISTER = 0,
245     OC_OBSERVE_DEREGISTER = 1,
246     OC_OBSERVE_NO_OPTION = 2
247 } OCObserveAction;
248
249 typedef struct
250 {
251     // Action associated with observation request
252     OCObserveAction action;
253     // Identifier for observation being registered/deregistered
254     OCObservationId obsId;
255 } OCObservationInfo;
256
257 /**
258  * Possible returned values from entity handler.
259  */
260 typedef enum
261 {
262     OC_EH_OK = 0,
263     OC_EH_ERROR,
264     OC_EH_RESOURCE_CREATED,
265     OC_EH_RESOURCE_DELETED,
266     OC_EH_SLOW,
267     OC_EH_FORBIDDEN,
268     OC_EH_RESOURCE_NOT_FOUND
269 } OCEntityHandlerResult;
270
271 /**
272  * This structure will be used to define the vendor specific header options to be included
273  * in communication packets.
274  */
275 typedef struct OCHeaderOption
276 {
277     // The protocol ID this option applies to
278     OCTransportProtocolID protocolID;
279     // The header option ID which will be added to communication packets
280     uint16_t optionID;
281     // its length   191
282     uint16_t optionLength;
283     // pointer to its data
284     uint8_t optionData[MAX_HEADER_OPTION_DATA_LENGTH];
285 } OCHeaderOption;
286
287 /**
288  * Incoming requests handled by the server. Requests are passed in as a parameter to the
289  * @ref OCEntityHandler callback API.
290  * @brief The @ref OCEntityHandler callback API must be implemented in the application in order
291  * to receive these requests.
292  */
293 typedef struct
294 {
295     // Associated resource
296     OCResourceHandle resource;
297     OCRequestHandle requestHandle;
298     // the REST method retrieved from received request PDU
299     OCMethod method;
300     // resource query send by client
301     char * query;
302     // Information associated with observation - valid only when OCEntityHandler
303     // flag includes OC_OBSERVE_FLAG
304     OCObservationInfo obsInfo;
305     // An array of the received vendor specific header options
306     uint8_t numRcvdVendorSpecificHeaderOptions;
307     OCHeaderOption * rcvdVendorSpecificHeaderOptions;
308     // reqJSON is retrieved from the payload of the received request PDU
309     char * reqJSONPayload;
310 } OCEntityHandlerRequest;
311
312 /**
313  * Response from queries to remote servers. Queries are made by calling the @ref OCDoResource API.
314  */
315 typedef struct
316 {
317     // Address of remote server
318     OCDevAddr * addr;
319     // Indicates adaptor type on which the response was received
320     OCConnectivityType connType;
321     // the is the result of our stack, OCStackResult should contain coap/other error codes;
322     OCStackResult result;
323     // If associated with observe, this will represent the sequence of notifications from server.
324     uint32_t sequenceNumber;
325     // resJSONPayload is retrieved from the payload of the received request PDU
326     const char * resJSONPayload;
327     // An array of the received vendor specific header options
328     uint8_t numRcvdVendorSpecificHeaderOptions;
329     OCHeaderOption rcvdVendorSpecificHeaderOptions[MAX_HEADER_OPTIONS];
330 } OCClientResponse;
331
332 /**
333  * This structure describes the device properties. All non-Null properties will be included
334  * in a device discovery request.
335  */
336 typedef struct
337 {
338     char *deviceName;
339     char *hostName;
340     char *deviceUUID;
341     char *contentType;
342     char *version;
343     char *manufacturerName;
344     char *manufacturerUrl;
345     char *modelNumber;
346     char *dateOfManufacture;
347     char *platformVersion;
348     char *firmwareVersion;
349     char *supportUrl;
350 } OCDeviceInfo;
351
352 typedef struct
353 {
354     // Request handle is passed to server via the entity handler for each incoming request.
355     // Stack assigns when request is received, server sets to indicate what request response is for
356     OCRequestHandle requestHandle;
357     // New handle for tracking block (or slow) response.  Stack assigns, server uses for subsequent calls
358     OCResponseHandle  *responseHandle;
359     // Resource handle
360     OCResourceHandle resourceHandle;
361     // Allow the entity handler to pass a result with the response
362     OCEntityHandlerResult  ehResult;
363     // this is the pointer to server payload data to be transferred
364     char *payload;
365     // size of server payload data.  I don't think we should rely on null terminated data for size
366     uint16_t payloadSize;
367     // An array of the vendor specific header options the entity handler wishes to use in response
368     uint8_t numSendVendorSpecificHeaderOptions;
369     OCHeaderOption sendVendorSpecificHeaderOptions[MAX_HEADER_OPTIONS];
370     // URI of new resource that entity handler might create
371     char resourceUri[MAX_URI_LENGTH];
372     // Server sets to true for persistent response buffer, false for non-persistent response buffer
373     uint8_t persistentBufferFlag;
374 } OCEntityHandlerResponse;
375
376 typedef enum
377 {
378     OC_INIT_FLAG    = (1 << 0),
379     OC_REQUEST_FLAG = (1 << 1),
380     OC_OBSERVE_FLAG = (1 << 2)
381 } OCEntityHandlerFlag; //entity_handler_flag_t ;
382
383 /**
384  * Possible returned values from client application.
385  */
386 typedef enum
387 {
388     OC_STACK_DELETE_TRANSACTION = 0,//!< OC_STACK_DELETE_TRANSACTION
389     OC_STACK_KEEP_TRANSACTION       //!< OC_STACK_KEEP_TRANSACTION
390 } OCStackApplicationResult;
391
392 //-----------------------------------------------------------------------------
393 // Callback function definitions
394 //-----------------------------------------------------------------------------
395
396 /**
397  * Client applications implement this callback to consume responses received from Servers.
398  */
399 typedef OCStackApplicationResult (* OCClientResponseHandler)(void *context, OCDoHandle handle,
400     OCClientResponse * clientResponse);
401
402 /**
403  * Client applications using a context pointer implement this callback to delete the
404  * context upon removal of the callback/context pointer from the internal callback-list.
405  */
406 typedef void (* OCClientContextDeleter)(void *context);
407
408 /*
409  * This info is passed from application to OC Stack when initiating a request to Server.
410  */
411 typedef struct
412 {
413     void *context;
414     OCClientResponseHandler cb;
415     OCClientContextDeleter cd;
416 } OCCallbackData;
417
418 /**
419  * Application server implementations must implement this callback to consume requests OTA.
420  * Entity handler callback needs to fill the resPayload of the entityHandlerRequest.
421  */
422 typedef OCEntityHandlerResult (*OCEntityHandler)
423 (OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest);
424
425 /**
426  * Device Entity handler need to use this call back instead of OCEntityHandler.
427  */
428 typedef OCEntityHandlerResult (*OCDeviceEntityHandler)
429 (OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest, char* uri);
430
431 //-----------------------------------------------------------------------------
432 // Function prototypes
433 //-----------------------------------------------------------------------------
434
435 /**
436  * Initialize the OC Stack.  Must be called prior to starting the stack.
437  *
438  * @param ipAddr
439  *     IP Address of host device. Deprecated parameter.
440  * @param port
441  *     Port of host device. Deprecated parameter.
442  * @param mode
443  *     Host device is client, server, or client-server.
444  *
445  * @return ::OC_STACK_OK on success, some other value upon failure.
446  */
447 OCStackResult OCInit(const char *ipAddr, uint16_t port, OCMode mode);
448
449 /**
450  * Stop the OC stack.  Use for a controlled shutdown.
451  *
452  * Note: OCStop() performs operations similar to OCStopPresence(), as well as OCDeleteResource() on
453  * all resources this server is hosting. OCDeleteResource() performs operations similar to
454  * OCNotifyAllObservers() to notify all client observers that the respective resource is being
455  * deleted.
456  *
457  * @return ::OC_STACK_OK on success, some other value upon failure.
458  */
459 OCStackResult OCStop();
460
461 /**
462  * Called in main loop of OC client or server.  Allows low-level processing of
463  * stack services.
464  *
465  * @return ::OC_STACK_OK on success, some other value upon failure.
466  */
467 OCStackResult OCProcess();
468
469 /**
470  * Discover or Perform requests on a specified resource (specified by that Resource's respective
471  * URI).
472  *
473  * @param handle             @ref OCDoHandle to refer to the request sent out on behalf of
474  *                           calling this API. This handle can be used to cancel this operation
475  *                           via the OCCancel API.
476  *                           Note: This reference is handled internally, and
477  *                           should not be free'd by the consumer.  A NULL handle is permitted
478  *                           in the event where the caller has no use for the return value.
479  * @param method             @ref OCMethod to perform on the resource.
480  * @param requiredUri        URI of the resource to interact with.
481  * @param referenceUri       URI of the reference resource.
482  * @param request            JSON encoded request.
483  * @param conType            @ref OCConnectivityType type of connectivity indicating the
484  *                           interface. Example: ::OC_WIFI, ::OC_ETHERNET, ::OC_ALL.
485  * @param qos                Quality of service. Note that if this API is called on a uri with
486  *                           the well-known multicast IP address, the qos will be forced to
487  *                           ::OC_LOW_QOS
488  *                           since it is impractical to send other QOS levels on such addresses.
489  * @param cbData             Asynchronous callback function that is invoked
490  *                           by the stack when discovery or resource interaction is complete.
491  * @param options            The address of an array containing the vendor specific
492  *                           header options to be sent with the request.
493  * @param numOptions         Number of header options to be included.
494  *
495  * Note: Presence subscription amendments (ie. adding additional resource type filters by calling
496  * this API again) require the use of the same base URI as the original request to successfully
497  * amend the presence filters.
498  *
499  * @return ::OC_STACK_OK on success, some other value upon failure.
500  */
501 OCStackResult OCDoResource(OCDoHandle *handle, OCMethod method, const char *requiredUri,
502             const char *referenceUri, const char *request, OCConnectivityType conType,
503             OCQualityOfService qos, OCCallbackData *cbData,
504             OCHeaderOption * options, uint8_t numOptions);
505
506 /**
507  * Cancel a request associated with a specific @ref OCDoResource invocation.
508  *
509  * @param handle - Used to identify a specific OCDoResource invocation.
510  * @param qos    - used to specify Quality of Service (read below for more info)
511  * @param options- used to specify vendor specific header options when sending
512  *                 explicit observe cancellation
513  * @param numOptions- Number of header options to be included
514  *
515  * @return ::OC_STACK_OK on success, some other value upon failure.
516  */
517 OCStackResult OCCancel(OCDoHandle handle, OCQualityOfService qos, OCHeaderOption * options,
518         uint8_t numOptions);
519
520 #ifdef WITH_PRESENCE
521 /**
522  * When operating in @ref OCServer or @ref OCClientServer mode, this API will start sending out
523  * presence notifications to clients via multicast. Once this API has been called with a success,
524  * clients may query for this server's presence and this server's stack will respond via multicast.
525  *
526  * Server can call this function when it comes online for the first time, or when it comes back
527  * online from offline mode, or when it re enters network.
528  *
529  * @param ttl Time To Live in seconds.
530  * Note: If ttl is '0', then the default stack value will be used (60 Seconds).
531  *
532  *       If ttl is greater than OC_MAX_PRESENCE_TTL_SECONDS, then the ttl will be set to
533  *       OC_MAX_PRESENCE_TTL_SECONDS.
534  *
535  * @return ::OC_STACK_OK on success, some other value upon failure.
536  */
537 OCStackResult OCStartPresence(const uint32_t ttl);
538
539 /**
540  * When operating in @ref OCServer or @ref OCClientServer mode, this API will stop sending out
541  * presence notifications to clients via multicast. Once this API has been called with a success,
542  * this server's stack will not respond to clients querying for this server's presence.
543  *
544  * Server can call this function when it is terminating, going offline, or when going
545  * away from network.
546  *
547  * @return ::OC_STACK_OK on success, some other value upon failure.
548  */
549
550 OCStackResult OCStopPresence();
551 #endif
552
553
554 /**
555  * Set default device entity handler.
556  *
557  * @param entityHandler Entity handler function that is called by ocstack to handle requests for
558  *                      any undefined resources or default actions.
559  *                      If NULL is passed it removes the device default entity handler.
560  *
561  * @return ::OC_STACK_OK on success, some other value upon failure.
562  */
563 OCStackResult OCSetDefaultDeviceEntityHandler(OCDeviceEntityHandler entityHandler);
564
565 /**
566  * Set device information.
567  *
568  * @param deviceInfo - Structure passed by the server application containing
569  *                     the device information.
570  *
571  *
572  * @return
573  *     OC_STACK_OK              - no errors
574  *     OC_STACK_INVALID_PARAM   - invalid paramerter
575  *     OC_STACK_ERROR           - stack process error
576  */
577 OCStackResult OCSetDeviceInfo(OCDeviceInfo deviceInfo);
578
579 /**
580  * Create a resource.
581  *
582  * @param handle Pointer to handle to newly created resource.  Set by ocstack and
583  *               used to refer to resource.
584  * @param resourceTypeName Name of resource type.  Example: "core.led".
585  * @param resourceInterfaceName Name of resource interface.  Example: "core.rw".
586  * @param uri URI of the resource.  Example:  "/a/led".
587  * @param entityHandler Entity handler function that is called by ocstack to handle requests, etc.
588  *                      NULL for default entity handler.
589  * @param resourceProperties Properties supported by resource.
590  *                           Example: ::OC_DISCOVERABLE|::OC_OBSERVABLE.
591  *
592  * @return ::OC_STACK_OK on success, some other value upon failure.
593  */
594 OCStackResult OCCreateResource(OCResourceHandle *handle,
595                                const char *resourceTypeName,
596                                const char *resourceInterfaceName,
597                                const char *uri,
598                                OCEntityHandler entityHandler,
599                                uint8_t resourceProperties);
600
601 /**
602  * Create a resource. with host ip address for remote resource.
603  *
604  * @param handle Pointer to handle to newly created resource.  Set by ocstack.
605  *               Used to refer to resource.
606  * @param resourceTypeName Name of resource type.  Example: "core.led".
607  * @param resourceInterfaceName Name of resource interface.  Example: "core.rw".
608  * @param host HOST address of the remote resource.  Example:  "coap://xxx.xxx.xxx.xxx:xxxxx".
609  * @param uri URI of the resource.  Example:  "/a/led".
610  * @param entityHandler Entity handler function that is called by ocstack to handle requests, etc.
611  *                      NULL for default entity handler.
612  * @param resourceProperties Properties supported by resource.
613  *                           Example: ::OC_DISCOVERABLE|::OC_OBSERVABLE
614  *
615  * @return ::OC_STACK_OK on success, some other value upon failure.
616  */
617 OCStackResult OCCreateResourceWithHost(OCResourceHandle *handle,
618                                const char *resourceTypeName,
619                                const char *resourceInterfaceName,
620                                const char *host,
621                                const char *uri,
622                                OCEntityHandler entityHandler,
623                                uint8_t resourceProperties);
624
625 /**
626  * Add a resource to a collection resource.
627  *
628  * @param collectionHandle Handle to the collection resource.
629  * @param resourceHandle Handle to resource to be added to the collection resource.
630  *
631  * @return ::OC_STACK_OK on success, some other value upon failure.
632  */
633 OCStackResult OCBindResource(OCResourceHandle collectionHandle, OCResourceHandle resourceHandle);
634
635 /**
636  * Remove a resource from a collection resource.
637  *
638  * @param collectionHandle Handle to the collection resource.
639  * @param resourceHandle Handle to resource to be removed from the collection resource.
640  *
641  * @return ::OC_STACK_OK on success, some other value upon failure.
642  */
643 OCStackResult OCUnBindResource(OCResourceHandle collectionHandle, OCResourceHandle resourceHandle);
644
645 /**
646  * Bind a resourcetype to a resource.
647  *
648  * @param handle Handle to the resource.
649  * @param resourceTypeName Name of resource type.  Example: "core.led".
650  *
651  * @return ::OC_STACK_OK on success, some other value upon failure.
652  */
653 OCStackResult OCBindResourceTypeToResource(OCResourceHandle handle,
654                                            const char *resourceTypeName);
655 /**
656  * Bind a resource interface to a resource.
657  *
658  * @param handle Handle to the resource.
659  * @param resourceInterfaceName Name of resource interface.  Example: "core.rw".
660  *
661  * @return ::OC_STACK_OK on success, some other value upon failure.
662  */
663 OCStackResult OCBindResourceInterfaceToResource(OCResourceHandle handle,
664                                                 const char *resourceInterfaceName);
665
666 /**
667  * Bind an entity handler to the resource.
668  *
669  * @param handle Handle to the resource that the contained resource is to be bound.
670  * @param entityHandler Entity handler function that is called by ocstack to handle requests, etc.
671  * @return ::OC_STACK_OK on success, some other value upon failure.
672  */
673 OCStackResult OCBindResourceHandler(OCResourceHandle handle, OCEntityHandler entityHandler);
674
675 /**
676  * Get the number of resources that have been created in the stack.
677  *
678  * @param numResources Pointer to count variable.
679  *
680  * @return ::OC_STACK_OK on success, some other value upon failure.
681  */
682 OCStackResult OCGetNumberOfResources(uint8_t *numResources);
683
684 /**
685  * Get a resource handle by index.
686  *
687  * @param index Index of resource, 0 to Count - 1.
688  *
689  * @return Found resource handle or NULL if not found.
690  */
691 OCResourceHandle OCGetResourceHandle(uint8_t index);
692
693 /**
694  * Delete resource specified by handle.  Deletes resource and all resourcetype and resourceinterface
695  * linked lists.
696  *
697  * Note: OCDeleteResource() performs operations similar to OCNotifyAllObservers() to notify all
698  * client observers that "this" resource is being deleted.
699  *
700  * @param handle Handle of resource to be deleted.
701  *
702  * @return ::OC_STACK_OK on success, some other value upon failure.
703  */
704 OCStackResult OCDeleteResource(OCResourceHandle handle);
705
706 /**
707  * Get the URI of the resource specified by handle.
708  *
709  * @param handle Handle of resource.
710  * @return URI string if resource found or NULL if not found.
711  */
712 const char *OCGetResourceUri(OCResourceHandle handle);
713
714 /**
715  * Get the properties of the resource specified by handle.
716  * NOTE: that after a resource is created, the OC_ACTIVE property is set
717  * for the resource by the stack.
718  *
719  * @param handle Handle of resource.
720  * @return OCResourceProperty Bitmask or -1 if resource is not found.
721  */
722 OCResourceProperty OCGetResourceProperties(OCResourceHandle handle);
723
724 /**
725  * Get the number of resource types of the resource.
726  *
727  * @param handle Handle of resource.
728  * @param numResourceTypes Pointer to count variable.
729  *
730  * @return ::OC_STACK_OK on success, some other value upon failure.
731  */
732 OCStackResult OCGetNumberOfResourceTypes(OCResourceHandle handle, uint8_t *numResourceTypes);
733
734 /**
735  * Get name of resource type of the resource.
736  *
737  * @param handle Handle of resource.
738  * @param index Index of resource, 0 to Count - 1.
739  *
740  * @return Resource type name if resource found or NULL if resource not found.
741  */
742 const char *OCGetResourceTypeName(OCResourceHandle handle, uint8_t index);
743
744 /**
745  * Get the number of resource interfaces of the resource.
746  *
747  * @param handle Handle of resource.
748  * @param numResourceInterfaces Pointer to count variable.
749  *
750  * @return ::OC_STACK_OK on success, some other value upon failure.
751  */
752 OCStackResult OCGetNumberOfResourceInterfaces(OCResourceHandle handle,
753         uint8_t *numResourceInterfaces);
754
755 /**
756  * Get name of resource interface of the resource.
757  *
758  * @param handle Handle of resource.
759  * @param index Index of resource, 0 to Count - 1.
760  *
761  * @return Resource interface name if resource found or NULL if resource not found.
762  */
763 const char *OCGetResourceInterfaceName(OCResourceHandle handle, uint8_t index);
764
765 /**
766  * Get methods of resource interface of the resource.
767  *
768  * @param handle Handle of resource.
769  * @param index Index of resource, 0 to Count - 1.
770  *
771  * @return Allowed methods if resource found or NULL if resource not found.
772  */
773 uint8_t OCGetResourceInterfaceAllowedMethods(OCResourceHandle handle, uint8_t index);
774
775 /**
776  * Get resource handle from the collection resource by index.
777  *
778  * @param collectionHandle Handle of collection resource.
779  * @param index Index of contained resource, 0 to Count - 1.
780  *
781  * @return Handle to contained resource if resource found or NULL if resource not found.
782  */
783 OCResourceHandle OCGetResourceHandleFromCollection(OCResourceHandle collectionHandle,
784         uint8_t index);
785
786 /**
787  * Get the entity handler for a resource.
788  *
789  * @param handle Handle of resource.
790  *
791  * @return Entity handler if resource found or NULL resource not found.
792  */
793 OCEntityHandler OCGetResourceHandler(OCResourceHandle handle);
794
795 /**
796  * Notify all registered observers that the resource representation has
797  * changed. If observation includes a query the client is notified only
798  * if the query is valid after the resource representation has changed.
799  *
800  * @param handle Handle of resource.
801  * @param qos    Desired quality of service for the observation notifications.
802  *
803  * @return ::OC_STACK_OK on success, some other value upon failure.
804  */
805 OCStackResult OCNotifyAllObservers(OCResourceHandle handle, OCQualityOfService qos);
806
807 /**
808  * Notify specific observers with updated value of representation.
809  * Before this API is invoked by entity handler it has finished processing
810  * queries for the associated observers.
811  *
812  * @param handle Handle of resource.
813  * @param obsIdList List of observation ids that need to be notified.
814  * @param numberOfIds Number of observation ids included in obsIdList.
815  * @param notificationJSONPayload JSON encoded payload to send in notification.
816  * @param qos Desired quality of service of the observation notifications.
817  * NOTE: The memory for obsIdList and notificationJSONPayload is managed by the
818  * entity invoking the API. The maximum size of the notification is 1015 bytes
819  * for non-Arduino platforms. For Arduino the maximum size is 247 bytes.
820  *
821  * @return ::OC_STACK_OK on success, some other value upon failure.
822  */
823 OCStackResult
824 OCNotifyListOfObservers (OCResourceHandle handle,
825                             OCObservationId  *obsIdList,
826                             uint8_t          numberOfIds,
827                             const char    *notificationJSONPayload,
828                             OCQualityOfService qos);
829
830
831 /**
832  * Send a response to a request.
833  * The response can be a normal, slow, or block (i.e. a response that
834  * is too large to be sent in a single PDU and must span multiple transmissions).
835  *
836  * @param response Pointer to structure that contains response parameters.
837  *
838  * @return ::OC_STACK_OK on success, some other value upon failure.
839  */
840 OCStackResult OCDoResponse(OCEntityHandlerResponse *response);
841
842 /**
843  * Cancel a response.  Applies to a block response.
844  *
845  * @param responseHandle Response handle set by stack in OCServerResponse after
846  *                       OCDoResponse is called.
847  *
848  * @return ::OC_STACK_OK on success, some other value upon failure.
849  */
850 OCStackResult OCCancelResponse(OCResponseHandle responseHandle);
851
852 //Utility methods
853
854 /**
855  * This method is used to retrieved the IPv4 address from OCDev address
856  * data structure.
857  *
858  * @param ipAddr OCDevAddr address.
859  * @param a first byte of IPv4 address.
860  * @param b second byte of IPv4 address.
861  * @param c third byte of IPv4 address.
862  * @param d fourth byte of IPv4 address.
863  * @return ::OC_STACK_OK on success, some other value upon failure.
864  */
865 int32_t OCDevAddrToIPv4Addr(OCDevAddr *ipAddr, uint8_t *a, uint8_t *b,
866             uint8_t *c, uint8_t *d );
867
868 /**
869  * This method is used to retrieve the port number from OCDev address
870  * data structure.
871  *
872  * @param ipAddr OCDevAddr address.
873  * @param port Port number.
874  * @return ::OC_STACK_OK on success, some other value upon failure.
875  */
876 int32_t OCDevAddrToPort(OCDevAddr *ipAddr, uint16_t *port);
877
878 #ifdef __cplusplus
879 }
880 #endif // __cplusplus
881
882 #endif /* OCSTACK_H_ */
883
884