summaryrefslogtreecommitdiff
path: root/resource/csdk/stack/include/ocstack.h
blob: 0038ec81dba8eb4741160ca13b361be7cc452ee4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
//******************************************************************
//
// Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

#ifndef OCSTACK_H_
#define OCSTACK_H_

#include <stdint.h>
#include "ocstackconfig.h"

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
#define WITH_PRESENCE
//-----------------------------------------------------------------------------
// Defines
//-----------------------------------------------------------------------------

//TODO: May want to refactor this in upcoming sprints.
//Don't want to expose to application layer that lower level stack is using CoAP.

#define OC_WELL_KNOWN_QUERY                  "224.0.1.187:5683/oc/core"
#define OC_EXPLICIT_DEVICE_DISCOVERY_URI     "224.0.1.187:5683/oc/core/d?rt=core.led"
#define OC_MULTICAST_PREFIX                  "224.0.1.187:5683"
#define OC_MULTICAST_IP                      "224.0.1.187"

#define USE_RANDOM_PORT (0)
#ifdef WITH_PRESENCE
#define OC_DEFAULT_PRESENCE_TTL (60)
#define OC_PRESENCE_URI                      "/oc/presence"
extern uint8_t PresenceTimeOutSize; // length of PresenceTimeOut - 1
extern uint32_t PresenceTimeOut[];
#endif

//-----------------------------------------------------------------------------
// Typedefs
//-----------------------------------------------------------------------------

/** This would need to be modified for specific platforms and specific
 *  technologies
 */
#define DEV_ADDR_SIZE_MAX (16)

/**
 * Data structure to encapsulate IPv4/IPv6/Contiki/lwIP device addresses
 *
*/
typedef struct OCDevAddr
{
    uint32_t     size;                    /**< length of the address stored in addr field. */
    uint8_t      addr[DEV_ADDR_SIZE_MAX]; /**< device address. */
}OCDevAddr;

/**
 * OC Virtual resources supported by every OC device
 */
typedef enum
{
    OC_WELL_KNOWN_URI= 0,       // "/oc/core"
    OC_DEVICE_URI,              // "/oc/core/d"
    OC_RESOURCE_TYPES_URI,      // "/oc/core/d/type"
    #ifdef WITH_PRESENCE
    OC_PRESENCE,                // "/oc/presence"
    #endif
    OC_MAX_VIRTUAL_RESOURCES    // Max items in the list
} OCVirtualResources;

/**
 * Standard RESTful HTTP Methods
 */
typedef enum {
    OC_REST_NOMETHOD    = 0,
    OC_REST_GET         = (1 << 0),     // Read
    OC_REST_PUT         = (1 << 1),     // Write
    OC_REST_POST        = (1 << 2),     // Update
    OC_REST_DELETE      = (1 << 3),     // Delete
    // Register observe request for most up date notifications ONLY.
    OC_REST_OBSERVE     = (1 << 4),
    // Register observe request for all notifications, including stale notifications.
    OC_REST_OBSERVE_ALL = (1 << 5),
    // Deregister observation, intended for internal use
    OC_REST_CANCEL_OBSERVE = (1 << 6),
    #ifdef WITH_PRESENCE
    // Subscribe for all presence notifications of a particular resource.
    OC_REST_PRESENCE    = (1 << 7)
    #endif
} OCMethod;

/**
 * Host Mode of Operation
 */
typedef enum {
    OC_CLIENT = 0,
    OC_SERVER,
    OC_CLIENT_SERVER
} OCMode;

extern OCMode myStackMode;
/**
 * Quality of Service
 */
typedef enum {
    OC_LOW_QOS = 0,
    OC_MEDIUM_QOS,
    OC_HIGH_QOS,
    OC_NA_QOS // No Quality is defined, let the stack decide
} OCQualityOfService;

/**
 * Resource Properties
 *
 * OC_ACTIVE       - When this bit is set, the resource is initialized, otherwise the resource
 *                   is 'inactive'. 'inactive' signifies that the resource has been marked for
 *                   deletion or is already deleted.
 * OC_DISCOVERABLE - When this bit is set, the resource is allowed to be discovered by clients.
 * OC_OBSERVABLE   - When this bit is set, the resource is allowed to be observed by clients.
 * OC_SLOW         - When this bit is set, the resource has been marked as 'slow'. 'slow' signifies
 *                   that responses from this resource can expect delays in processing its
 *                   requests from clients.
 * OC_SECURE       - When this bit is set, the resource is a secure resource.
 */
typedef enum {
    OC_ACTIVE       = (1 << 0),
    OC_DISCOVERABLE = (1 << 1),
    OC_OBSERVABLE   = (1 << 2),
    OC_SLOW         = (1 << 3),
    OC_SECURE       = (1 << 4)
} OCResourceProperty;

/**
 * Transport Protocol IDs
 */
typedef enum {
    OC_INVALID_ID   = (1 << 0),
    OC_COAP_ID      = (1 << 1)
} OCTransportProtocolID;

/**
 * Adaptor types
 */
typedef enum {
    OC_ETHERNET = 0,
    OC_WIFI,
    OC_EDR,
    OC_LE,
    OC_ALL //Multicast message: send over all the interfaces.
} OCConnectivityType;

/**
 * Declares Stack Results & Errors
 */
typedef enum {
    /* Success status code - START HERE */
    OC_STACK_OK = 0,
    OC_STACK_RESOURCE_CREATED,
    OC_STACK_RESOURCE_DELETED,
    OC_STACK_CONTINUE,
    /* Success status code - END HERE */
    /* Error status code - START HERE */
    OC_STACK_INVALID_URI = 20,
    OC_STACK_INVALID_QUERY,
    OC_STACK_INVALID_IP,
    OC_STACK_INVALID_PORT,
    OC_STACK_INVALID_CALLBACK,
    OC_STACK_INVALID_METHOD,
    OC_STACK_INVALID_PARAM,
    OC_STACK_INVALID_OBSERVE_PARAM,
    OC_STACK_NO_MEMORY,
    OC_STACK_COMM_ERROR,
    OC_STACK_NOTIMPL,
    OC_STACK_NO_RESOURCE,               /* resource not found */
    OC_STACK_RESOURCE_ERROR,            /* ex: not supported method or interface */
    OC_STACK_SLOW_RESOURCE,
    OC_STACK_NO_OBSERVERS,              /* resource has no registered observers */
    OC_STACK_OBSERVER_NOT_FOUND,
    OC_STACK_VIRTUAL_DO_NOT_HANDLE,
    OC_STACK_INVALID_OPTION,
    OC_STACK_MALFORMED_RESPONSE,        /* the remote reply contained malformed data */
    OC_STACK_PERSISTENT_BUFFER_REQUIRED,
    OC_STACK_INVALID_REQUEST_HANDLE,
    OC_STACK_INVALID_DEVICE_INFO,
    OC_STACK_INVALID_JSON,
    /* NOTE: Insert all new error codes here!*/
    #ifdef WITH_PRESENCE
    OC_STACK_PRESENCE_STOPPED = 128,
    OC_STACK_PRESENCE_TIMEOUT,
    OC_STACK_PRESENCE_DO_NOT_HANDLE,
    #endif
    OC_STACK_ERROR = 255
    /* Error status code - END HERE */
} OCStackResult;

/**
 * Handle to an @ref OCDoResource invocation.
 */
typedef void * OCDoHandle;

/**
 * Handle to an OCResource object owned by the OCStack.
 */
typedef void * OCResourceHandle;

typedef void * OCRequestHandle;
typedef void * OCResponseHandle;

/**
 * Unique identifier for each observation request. Used when observations are
 * registered or deregistering. Used by entity handler to signal specific
 * observers to be notified of resource changes.
 * There can be maximum of 256 observations per server.
 */
typedef uint8_t OCObservationId;

/**
 * Action associated with observation
 */
typedef enum {
    OC_OBSERVE_REGISTER = 0,
    OC_OBSERVE_DEREGISTER = 1,
    OC_OBSERVE_NO_OPTION = 2
} OCObserveAction;

typedef struct {
    // Action associated with observation request
    OCObserveAction action;
    // Identifier for observation being registered/deregistered
    OCObservationId obsId;
} OCObservationInfo;

/**
 * Possible returned values from entity handler
 */
typedef enum {
    OC_EH_OK = 0,
    OC_EH_ERROR,
    OC_EH_RESOURCE_CREATED,
    OC_EH_RESOURCE_DELETED,
    OC_EH_SLOW,
    OC_EH_FORBIDDEN
} OCEntityHandlerResult;

// following structure will be used to define the vendor specific header options to be included
// in communication packets

typedef struct OCHeaderOption {
    // The protocol ID this option applies to
    OCTransportProtocolID protocolID;
    // The header option ID which will be added to communication packets
    uint16_t optionID;
    // its length   191
    uint16_t optionLength;
    // pointer to its data
    uint8_t optionData[MAX_HEADER_OPTION_DATA_LENGTH];
} OCHeaderOption;

/**
 * Incoming requests handled by the server. Requests are passed in as a parameter to the @ref OCEntityHandler callback API.
 * @brief The @ref OCEntityHandler callback API must be implemented in the application in order to receive these requests.
 */
typedef struct {
    // Associated resource
    OCResourceHandle resource;
    OCRequestHandle requestHandle;
    // the REST method retrieved from received request PDU
    OCMethod method;
    // resource query send by client
    unsigned char * query;
    // Information associated with observation - valid only when OCEntityHandler
    // flag includes OC_OBSERVE_FLAG
    OCObservationInfo obsInfo;
    // An array of the received vendor specific header options
    uint8_t numRcvdVendorSpecificHeaderOptions;
    OCHeaderOption * rcvdVendorSpecificHeaderOptions;
    // reqJSON is retrieved from the payload of the received request PDU
    unsigned char * reqJSONPayload;
}OCEntityHandlerRequest;

/**
 * Response from queries to remote servers. Queries are made by calling the @ref OCDoResource API.
 */
typedef struct {
    // Address of remote server
    OCDevAddr * addr;
    // Indicates adaptor type on which the response was received
    OCConnectivityType connType;
    // the is the result of our stack, OCStackResult should contain coap/other error codes;
    OCStackResult result;
    // If associated with observe, this will represent the sequence of notifications from server.
    uint32_t sequenceNumber;
    // resJSONPayload is retrieved from the payload of the received request PDU
    unsigned  const char * resJSONPayload;
    // An array of the received vendor specific header options
    uint8_t numRcvdVendorSpecificHeaderOptions;
    OCHeaderOption rcvdVendorSpecificHeaderOptions[MAX_HEADER_OPTIONS];
}OCClientResponse;

/**
 * Following structure describes the device properties. All non-Null properties will be included
 * in a device discovery request.
 */
typedef struct
{
    char *deviceName;
    char *hostName;
    char *deviceUUID;
    char *contentType;
    char *version;
    char *manufacturerName;
    char *manufacturerUrl;
    char *modelNumber;
    char *dateOfManufacture;
    char *platformVersion;
    char *firmwareVersion;
    char *supportUrl;
} OCDeviceInfo;

typedef struct
{
    // Request handle is passed to server via the entity handler for each incoming request.
    // Stack assigns when request is received, server sets to indicate what request response is for
    OCRequestHandle requestHandle;
    // New handle for tracking block (or slow) response.  Stack assigns, server uses for subsequent calls
    OCResponseHandle  *responseHandle;
    // Resource handle
    OCResourceHandle resourceHandle;
    // Allow the entity handler to pass a result with the response
    OCEntityHandlerResult  ehResult;
    // this is the pointer to server payload data to be transferred
    unsigned char *payload;
    // size of server payload data.  I don't think we should rely on null terminated data for size
    uint16_t payloadSize;
    // An array of the vendor specific header options the entity handler wishes to use in response
    uint8_t numSendVendorSpecificHeaderOptions;
    OCHeaderOption sendVendorSpecificHeaderOptions[MAX_HEADER_OPTIONS];
    // URI of new resource that entity handler might create
    unsigned char resourceUri[MAX_URI_LENGTH];
    // Server sets to true for persistent response buffer, false for non-persistent response buffer
    uint8_t persistentBufferFlag;
} OCEntityHandlerResponse;

typedef enum {
    OC_INIT_FLAG    = (1 << 0),
    OC_REQUEST_FLAG = (1 << 1),
    OC_OBSERVE_FLAG = (1 << 2)
} OCEntityHandlerFlag; //entity_handler_flag_t ;

// possible returned values from client application
typedef enum {
    OC_STACK_DELETE_TRANSACTION = 0,
    OC_STACK_KEEP_TRANSACTION
} OCStackApplicationResult;

//-----------------------------------------------------------------------------
// Callback function definitions
//-----------------------------------------------------------------------------

/**
 * Client applications implement this callback to consume responses received from Servers.
 */
typedef OCStackApplicationResult (* OCClientResponseHandler)(void *context, OCDoHandle handle,
    OCClientResponse * clientResponse);

/**
 * Client applications using a context pointer implement this callback to delete the
 * context upon removal of the callback/context pointer from the internal callback-list
 */
typedef void (* OCClientContextDeleter)(void *context);

/*
 * This info is passed from application to OC Stack when initiating a request to Server
 */
typedef struct {
    void *context;
    OCClientResponseHandler cb;
    OCClientContextDeleter cd;
} OCCallbackData;

/**
 * Application server implementations must implement this callback to consume requests OTA.
 * Entity handler callback needs to fill the resPayload of the entityHandlerRequest.
 */
typedef OCEntityHandlerResult (*OCEntityHandler)
(OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest);

/**
 * Device Entity handler need to use this call back instead of OCEntityHandler
 */
typedef OCEntityHandlerResult (*OCDeviceEntityHandler)
(OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest, char* uri);

//-----------------------------------------------------------------------------
// Function prototypes
//-----------------------------------------------------------------------------

/**
 * Initialize the OC Stack.  Must be called prior to starting the stack.
 *
 * @param ipAddr
 *     IP Address of host device
 * @param port
 *     Port of host device
 * @param mode
 *     Host device is client, server, or client-server
 *
 * @return
 *     OC_STACK_OK    - no errors
 *     OC_STACK_ERROR - stack init error
 */
OCStackResult OCInit(const char *ipAddr, uint16_t port, OCMode mode);

/**
 * Stop the OC stack.  Use for a controlled shutdown.
 *
 * Note: OCStop() performs operations similar to OCStopPresence(), as well as OCDeleteResource() on
 * all resources this server is hosting. OCDeleteResource() performs operations similar to
 * OCNotifyAllObservers() to notify all client observers that the respective resource is being
 * deleted.
 *
 * @return
 *     OC_STACK_OK    - no errors
 *     OC_STACK_ERROR - stack not initialized
 */
OCStackResult OCStop();

/**
 * Called in main loop of OC client or server.  Allows low-level processing of
 * stack services.
 *
 * @return
 *     OC_STACK_OK    - no errors
 *     OC_STACK_ERROR - stack process error
 */
OCStackResult OCProcess();

/**
 * Discover or Perform requests on a specified resource (specified by that Resource's respective
 * URI).
 *
 * @param handle             - @ref OCDoHandle to refer to the request sent out on behalf of
 *                             calling this API.
 * @param method             - @ref OCMethod to perform on the resource
 * @param requiredUri        - URI of the resource to interact with
 * @param referenceUri       - URI of the reference resource
 * @param request            - JSON encoded request
 * @param conType            - @ref OCConnectivityType type of connectivity indicating the
 *                             interface. Example: OC_WIFI, OC_ETHERNET, OC_ALL
 * @param qos                - quality of service. Note that if this API is called on a uri with
 *                             the well-known multicast IP address, the qos will be forced to
 *                             OC_LOW_QOS
 *                             since it is impractical to send other QOS levels on such addresses.
 * @param clientApplicationCB- asynchronous callback function that is invoked
 *                             by the stack when discovery or resource interaction is complete
 * @param options            - The address of an array containing the vendor specific
 *                             header options to be sent with the request
 * @param numOptions         - Number of header options to be included
 *
 * Note: Presence subscription amendments (ie. adding additional resource type filters by calling
 * this API again) require the use of the same base URI as the original request to successfully
 * amend the presence filters.
 *
 * @return
 *     OC_STACK_OK               - no errors
 *     OC_STACK_INVALID_CALLBACK - invalid callback function pointer
 *     OC_STACK_INVALID_METHOD   - invalid resource method
 *     OC_STACK_INVALID_URI      - invalid required or reference URI
 *     OC_STACK_INVALID_QUERY    - number of resource types specified for filtering presence
 *                                 notifications exceeds @ref MAX_PRESENCE_FILTERS.
 */
OCStackResult OCDoResource(OCDoHandle *handle, OCMethod method, const char *requiredUri,
            const char *referenceUri, const char *request, OCConnectivityType conType,
            OCQualityOfService qos, OCCallbackData *cbData,
            OCHeaderOption * options, uint8_t numOptions);

/**
 * Cancel a request associated with a specific @ref OCDoResource invocation.
 *
 * @param handle - Used to identify a specific OCDoResource invocation.
 * @param qos    - used to specify Quality of Service (read below for more info)
 * @param options- used to specify vendor specific header options when sending
 *                 explicit observe cancellation
 * @param numOptions- Number of header options to be included
 *
 * @return
 *     OC_STACK_OK               - No errors; Success
 *     OC_STACK_INVALID_PARAM    - The handle provided is invalid.
 */
OCStackResult OCCancel(OCDoHandle handle, OCQualityOfService qos, OCHeaderOption * options,
        uint8_t numOptions);

#ifdef WITH_PRESENCE
/**
 * When operating in @ref OCServer or @ref OCClientServer mode, this API will start sending out
 * presence notifications to clients via multicast. Once this API has been called with a success,
 * clients may query for this server's presence and this server's stack will respond via multicast.
 *
 * Server can call this function when it comes online for the first time, or when it comes back
 * online from offline mode, or when it re enters network.
 *
 * @param ttl - Time To Live in seconds
 * Note: If ttl is '0', then the default stack value will be used (60 Seconds).
 *
 * @return
 *     OC_STACK_OK      - No errors; Success
 */
OCStackResult OCStartPresence(const uint32_t ttl);

/**
 * When operating in @ref OCServer or @ref OCClientServer mode, this API will stop sending out
 * presence notifications to clients via multicast. Once this API has been called with a success,
 * this server's stack will not respond to clients querying for this server's presence.
 *
 * Server can call this function when it is terminating, going offline, or when going
 * away from network.
 *
 * @return
 *     OC_STACK_OK      - No errors; Success
 */

OCStackResult OCStopPresence();
#endif


/**
 * Set default device entity handler
 *
 * @param entityHandler - entity handler function that is called by ocstack to handle requests for
 *                        any undefined resources or default actions.
 *                        if NULL is passed it removes the device default entity handler.
 *
 * @return
 *     OC_STACK_OK    - no errors
 *     OC_STACK_ERROR - stack process error
 */
OCStackResult OCSetDefaultDeviceEntityHandler(OCDeviceEntityHandler entityHandler);

/**
 * Set device information.
 *
 * @param deviceInfo - Structure passed by the server application containing
 *                     the device information.
 *
 *
 * @return
 *     OC_STACK_OK              - no errors
 *     OC_STACK_INVALID_PARAM   - invalid paramerter
 *     OC_STACK_ERROR           - stack process error
 */
OCStackResult OCSetDeviceInfo(OCDeviceInfo deviceInfo);

/**
 * Create a resource.
 *
 * @param handle - pointer to handle to newly created resource.  Set by ocstack.  Used to refer to resource
 * @param resourceTypeName - name of resource type.  Example: "core.led"
 * @param resourceInterfaceName - name of resource interface.  Example: "core.rw"
 * @param uri - URI of the resource.  Example:  "/a/led"
 * @param entityHandler - entity handler function that is called by ocstack to handle requests, etc
 *                        NULL for default entity handler
 * @param resourceProperties - properties supported by resource.  Example: OC_DISCOVERABLE|OC_OBSERVABLE
 *
 * @return
 *     OC_STACK_OK    - no errors
 *     OC_STACK_ERROR - stack process error
 */
OCStackResult OCCreateResource(OCResourceHandle *handle,
                               const char *resourceTypeName,
                               const char *resourceInterfaceName,
                               const char *uri,
                               OCEntityHandler entityHandler,
                               uint8_t resourceProperties);

/**
 * Create a resource. with host ip address for remote resource
 *
 * @param handle - pointer to handle to newly created resource.  Set by ocstack.
 *                 Used to refer to resource
 * @param resourceTypeName - name of resource type.  Example: "core.led"
 * @param resourceInterfaceName - name of resource interface.  Example: "core.rw"
 * @param host - HOST address of the remote resource.  Example:  "coap://xxx.xxx.xxx.xxx:xxxxx"
 * @param uri - URI of the resource.  Example:  "/a/led"
 * @param entityHandler - entity handler function that is called by ocstack to handle requests, etc
 *                        NULL for default entity handler
 * @param resourceProperties - properties supported by resource.
 *                             Example: OC_DISCOVERABLE|OC_OBSERVABLE
 *
 * @return
 *     OC_STACK_OK    - no errors
 *     OC_STACK_ERROR - stack process error
 */
OCStackResult OCCreateResourceWithHost(OCResourceHandle *handle,
                               const char *resourceTypeName,
                               const char *resourceInterfaceName,
                               const char *host,
                               const char *uri,
                               OCEntityHandler entityHandler,
                               uint8_t resourceProperties);

/**
 * Add a resource to a collection resource.
 *
 * @param collectionHandle - handle to the collection resource
 * @param resourceHandle - handle to resource to be added to the collection resource
 *
 * @return
 *     OC_STACK_OK    - no errors
 *     OC_STACK_ERROR - stack process error
 *     OC_STACK_INVALID_PARAM - invalid collectionhandle
 */
OCStackResult OCBindResource(OCResourceHandle collectionHandle, OCResourceHandle resourceHandle);

/**
 * Remove a resource from a collection resource.
 *
 * @param collectionHandle - handle to the collection resource
 * @param resourceHandle - handle to resource to be removed from the collection resource
 *
 * @return
 *     OC_STACK_OK    - no errors
 *     OC_STACK_ERROR - stack process error
 *     OC_STACK_INVALID_PARAM - invalid collectionhandle
 */
OCStackResult OCUnBindResource(OCResourceHandle collectionHandle, OCResourceHandle resourceHandle);

/**
 * Bind a resourcetype to a resource.
 *
 * @param handle - handle to the resource
 * @param resourceTypeName - name of resource type.  Example: "core.led"
 *
 * @return
 *     OC_STACK_OK    - no errors
 *     OC_STACK_ERROR - stack process error
 */
OCStackResult OCBindResourceTypeToResource(OCResourceHandle handle,
                                           const char *resourceTypeName);
/**
 * Bind a resource interface to a resource.
 *
 * @param handle - handle to the resource
 * @param resourceInterfaceName - name of resource interface.  Example: "core.rw"
 *
 * @return
 *     OC_STACK_OK    - no errors
 *     OC_STACK_ERROR - stack process error
 */
OCStackResult OCBindResourceInterfaceToResource(OCResourceHandle handle,
                                                const char *resourceInterfaceName);

/**
 * Bind an entity handler to the resource.
 *
 * @param handle - handle to the resource that the contained resource is to be bound
 * @param entityHandler - entity handler function that is called by ocstack to handle requests, etc
 * @return
 *     OC_STACK_OK    - no errors
 *     OC_STACK_ERROR - stack process error
 */
OCStackResult OCBindResourceHandler(OCResourceHandle handle, OCEntityHandler entityHandler);

/**
 * Get the number of resources that have been created in the stack.
 *
 * @param numResources - pointer to count variable
 *
 * @return
 *     OC_STACK_OK    - no errors
 *     OC_STACK_ERROR - stack process error

 */
OCStackResult OCGetNumberOfResources(uint8_t *numResources);

/**
 * Get a resource handle by index.
 *
 * @param index - index of resource, 0 to Count - 1
 *
 * @return
 *    Resource handle - if found
 *    NULL - if not found
 */
OCResourceHandle OCGetResourceHandle(uint8_t index);

/**
 * Delete resource specified by handle.  Deletes resource and all resourcetype and resourceinterface
 * linked lists.
 *
 * Note: OCDeleteResource() performs operations similar to OCNotifyAllObservers() to notify all
 * client observers that "this" resource is being deleted.
 *
 * @param handle - handle of resource to be deleted
 *
 * @return
 *     OC_STACK_OK    - no errors
 *     OC_STACK_ERROR - stack process error
 */
OCStackResult OCDeleteResource(OCResourceHandle handle);

/**
 * Get the URI of the resource specified by handle.
 *
 * @param handle - handle of resource
 * @return
 *    URI string - if resource found
 *    NULL - resource not found
 */
const char *OCGetResourceUri(OCResourceHandle handle);

/**
 * Get the properties of the resource specified by handle.
 * NOTE: that after a resource is created, the OC_ACTIVE property is set
 * for the resource by the stack.
 *
 * @param handle - handle of resource
 * @return
 *    property bitmap - if resource found
 *    NULL - resource not found
 */
uint8_t OCGetResourceProperties(OCResourceHandle handle);

/**
 * Get the number of resource types of the resource.
 *
 * @param handle - handle of resource
 * @param numResourceTypes - pointer to count variable
 *
 * @return
 *     OC_STACK_OK    - no errors
 *     OC_STACK_ERROR - stack process error
 */
OCStackResult OCGetNumberOfResourceTypes(OCResourceHandle handle, uint8_t *numResourceTypes);

/**
 * Get name of resource type of the resource.
 *
 * @param handle - handle of resource
 * @param index - index of resource, 0 to Count - 1
 *
 * @return
 *    resource type name - if resource found
 *    NULL - resource not found
 */
const char *OCGetResourceTypeName(OCResourceHandle handle, uint8_t index);

/**
 * Get the number of resource interfaces of the resource.
 *
 * @param handle - handle of resource
 * @param numResources - pointer to count variable
 *
 * @return
 *     OC_STACK_OK    - no errors
 *     OC_STACK_ERROR - stack process error

 */
OCStackResult OCGetNumberOfResourceInterfaces(OCResourceHandle handle, uint8_t *numResourceInterfaces);

/**
 * Get name of resource interface of the resource.
 *
 * @param handle - handle of resource
 * @param index - index of resource, 0 to Count - 1
 *
 * @return
 *    resource interface name - if resource found
 *    NULL - resource not found
 */
const char *OCGetResourceInterfaceName(OCResourceHandle handle, uint8_t index);

/**
 * Get methods of resource interface of the resource.
 *
 * @param handle - handle of resource
 * @param index - index of resource, 0 to Count - 1
 *
 * @return
 *    allowed methods - if resource found
 *    NULL - resource not found
 */
uint8_t OCGetResourceInterfaceAllowedMethods(OCResourceHandle handle, uint8_t index);

/**
 * Get resource handle from the collection resource by index.
 *
 * @param collectionHandle - handle of collection resource
 * @param index - index of contained resource, 0 to Count - 1
 *
 * @return
 *    handle to contained resource - if resource found
 *    NULL - resource not found
 */
OCResourceHandle OCGetResourceHandleFromCollection(OCResourceHandle collectionHandle, uint8_t index);

/**
 * Get the entity handler for a resource.
 *
 * @param handle - handle of resource
 *
 * @return
 *    entity handler - if resource found
 *    NULL - resource not found
 */
OCEntityHandler OCGetResourceHandler(OCResourceHandle handle);

/**
 * Notify all registered observers that the resource representation has
 * changed. If observation includes a query the client is notified only
 * if the query is valid after the resource representation has changed.
 *
 * @param handle - handle of resource
 *
 * @return
 *     OC_STACK_OK    - no errors
 *     OC_STACK_NO_RESOURCE - invalid resource handle
 *     OC_STACK_NO_OBSERVERS - no more observers intrested in resource
 */
OCStackResult OCNotifyAllObservers(OCResourceHandle handle, OCQualityOfService qos);

/**
 * Notify specific observers with updated value of representation.
 * Before this API is invoked by entity handler it has finished processing
 * queries for the associated observers.
 *
 * @param handle - handle of resource
 * @param obsIdList - list of observation ids that need to be notified
 * @param numberOfIds - number of observation ids included in obsIdList
 * @param notificationJSONPayload - JSON encoded payload to send in notification
 * @param qos - desired quality of service of the observation notifications
 * NOTE: The memory for obsIdList and notificationJSONPayload is managed by the
 * entity invoking the API. The maximum size of the notification is 1015 bytes
 * for non-Arduino platforms. For Arduino the maximum size is 247 bytes.
 *
 * @return
 *     OC_STACK_OK    - no errors
 *     OC_STACK_NO_RESOURCE - invalid resource handle
 */
OCStackResult
OCNotifyListOfObservers (OCResourceHandle handle,
                            OCObservationId  *obsIdList,
                            uint8_t          numberOfIds,
                            unsigned char    *notificationJSONPayload,
                            OCQualityOfService qos);


/**
 * Send a response to a request.
 * The response can be a normal, slow, or block (i.e. a response that
 * is too large to be sent in a single PDU and must span multiple transmissions)
 *
 * @param response - pointer to structure that contains response parameters
 *
 * @return
 *     OC_STACK_OK    - no errors
 */
OCStackResult OCDoResponse(OCEntityHandlerResponse *response);

/**
 * Cancel a response.  Applies to a block response
 *
 * @param responseHandle - response handle set by stack in OCServerResponse after
 *                         OCDoResponse is called
 *
 * @return
 *     OC_STACK_OK               - No errors; Success
 *     OC_STACK_INVALID_PARAM    - The handle provided is invalid.
 */
OCStackResult OCCancelResponse(OCResponseHandle responseHandle);

//Utility methods

//-- OCDevAddrToIPv4Addr -------------------------------------------------
/**
 * This method is used to retrieved the IPv4 address from OCDev address
 * data structure.
 *
 * @param[in]  ipAddr
 *              OCDevAddr address.
 * @param[out]  a first byte of IPv4 address.
 * @param[out]  b second byte of IPv4 address.
 * @param[out]  c third byte of IPv4 address.
 * @param[out]  d fourth byte of IPv4 address.
 *
 * @retval 0 for Success, otherwise some error value
 */
//------------------------------------------------------------------------
int32_t OCDevAddrToIPv4Addr(OCDevAddr *ipAddr, uint8_t *a, uint8_t *b,
            uint8_t *c, uint8_t *d );


//-- OCDevAddrToPort -------------------------------------------------
/**
 * This method is used to retrieve the port number from OCDev address
 * data structure.
 *
 * @param[in]  ipAddr
 *              OCDevAddr address.
 * @param[out] port
 *              port number
 *
 * @retval 0 for Success, otherwise some error value
 */
//------------------------------------------------------------------------
int32_t OCDevAddrToPort(OCDevAddr *ipAddr, uint16_t *port);

#ifdef __cplusplus
}
#endif // __cplusplus

#endif /* OCSTACK_H_ */