Merge branch 'master' into group-manager
[platform/upstream/iotivity.git] / resource / csdk / stack / include / octypes.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 the definition, types and APIs for resource(s) be implemented.
26  */
27
28 #ifndef OCTYPES_H_
29 #define OCTYPES_H_
30
31 #include "platform_features.h"
32 #include "ocstackconfig.h"
33 #include <stdbool.h>
34 #include <stdint.h>
35 #include <stdio.h>
36 #ifdef __cplusplus
37 #include <string.h>
38
39 extern "C" {
40 #endif // __cplusplus
41
42 /** For the feature presence.*/
43 #define WITH_PRESENCE
44
45 #include "ocpresence.h"
46 //-----------------------------------------------------------------------------
47 // Defines
48 //-----------------------------------------------------------------------------
49
50 /**
51  * OIC Virtual resources supported by every OIC device.
52  */
53 /**
54  *  Default discovery mechanism using '/oic/res' is supported by all OIC devices
55  *  That are Discoverable.
56  */
57 #define OC_RSRVD_WELL_KNOWN_URI               "/oic/res"
58
59 /** Device URI.*/
60 #define OC_RSRVD_DEVICE_URI                   "/oic/d"
61
62 /** Platform URI.*/
63 #define OC_RSRVD_PLATFORM_URI                 "/oic/p"
64
65 /** Resource Type.*/
66 #define OC_RSRVD_RESOURCE_TYPES_URI           "/oic/res/types/d"
67 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
68 /** Gateway URI.*/
69 #define OC_RSRVD_GATEWAY_URI                  "/oic/gateway"
70 #endif
71 #ifdef WITH_PRESENCE
72
73 /** Presence URI through which the OIC devices advertise their presence.*/
74 #define OC_RSRVD_PRESENCE_URI                 "/oic/ad"
75
76 /** Sets the default time to live (TTL) for presence.*/
77 #define OC_DEFAULT_PRESENCE_TTL_SECONDS (60)
78
79 /** For multicast Discovery mechanism.*/
80 #define OC_MULTICAST_DISCOVERY_URI           "/oic/res"
81
82 /** Separator for multiple query string.*/
83 #define OC_QUERY_SEPARATOR                "&;"
84
85 /**
86  *  OC_DEFAULT_PRESENCE_TTL_SECONDS sets the default time to live (TTL) for presence.
87  */
88 #define OC_DEFAULT_PRESENCE_TTL_SECONDS (60)
89
90 /**
91  *  OC_MAX_PRESENCE_TTL_SECONDS sets the maximum time to live (TTL) for presence.
92  *  NOTE: Changing the setting to a longer duration may lead to unsupported and untested
93  *  operation.
94  *  60 sec/min * 60 min/hr * 24 hr/day
95  */
96 #define OC_MAX_PRESENCE_TTL_SECONDS     (60 * 60 * 24)
97 #endif
98
99 /**
100  *  Presence "Announcement Triggers".
101  */
102
103 /** To create.*/
104 #define OC_RSRVD_TRIGGER_CREATE         "create"
105
106 /** To change.*/
107 #define OC_RSRVD_TRIGGER_CHANGE         "change"
108
109 /** To delete.*/
110 #define OC_RSRVD_TRIGGER_DELETE         "delete"
111
112 /**
113  *  Attributes used to form a proper OIC conforming JSON message.
114  */
115
116 #define OC_RSRVD_OC                     "oic"
117
118 /** For payload. */
119
120 #define OC_RSRVD_PAYLOAD                "payload"
121
122 /** To represent href */
123 #define OC_RSRVD_HREF                   "href"
124
125 /** To represent property*/
126 #define OC_RSRVD_PROPERTY               "prop"
127
128 /** For representation.*/
129 #define OC_RSRVD_REPRESENTATION         "rep"
130
131 /** To represent content type.*/
132 #define OC_RSRVD_CONTENT_TYPE           "ct"
133
134 /** To represent resource type.*/
135 #define OC_RSRVD_RESOURCE_TYPE          "rt"
136
137 /** To represent resource type with presence.*/
138 #define OC_RSRVD_RESOURCE_TYPE_PRESENCE "oic.wk.ad"
139
140 /** To represent resource type with device.*/
141 #define OC_RSRVD_RESOURCE_TYPE_DEVICE   "oic.wk.d"
142
143 /** To represent resource type with platform.*/
144 #define OC_RSRVD_RESOURCE_TYPE_PLATFORM "oic.wk.p"
145
146 /** To represent interface.*/
147 #define OC_RSRVD_INTERFACE              "if"
148
149 /** To represent time to live.*/
150 #define OC_RSRVD_TTL                    "ttl"
151
152 /** To represent non*/
153 #define OC_RSRVD_NONCE                  "non"
154
155 /** To represent trigger type.*/
156 #define OC_RSRVD_TRIGGER                "trg"
157
158 /** To represent links.*/
159 #define OC_RSRVD_LINKS                  "links"
160
161 /** To represent default interface.*/
162 #define OC_RSRVD_INTERFACE_DEFAULT      "oic.if.baseline"
163
164 /** To represent read-only interface.*/
165 #define OC_RSRVD_INTERFACE_READ         "oic.if.r"
166
167 /** To represent ll interface.*/
168 #define OC_RSRVD_INTERFACE_LL           "oic.if.ll"
169
170 /** To represent batch interface.*/
171 #define OC_RSRVD_INTERFACE_BATCH        "oic.if.b"
172
173 /** To represent interface group.*/
174 #define OC_RSRVD_INTERFACE_GROUP        "oic.mi.grp"
175
176 /** To represent MFG date.*/
177 #define OC_RSRVD_MFG_DATE               "mndt"
178
179 /** To represent FW version.*/
180 #define OC_RSRVD_FW_VERSION             "mnfv"
181
182 /** To represent host name.*/
183 #define OC_RSRVD_HOST_NAME              "hn"
184
185 /** To represent version.*/
186 #define OC_RSRVD_VERSION                "icv"
187
188 /** To represent policy.*/
189 #define OC_RSRVD_POLICY                 "p"
190
191 /** To represent bitmap.*/
192 #define OC_RSRVD_BITMAP                 "bm"
193
194 /** For security.*/
195 #define OC_RSRVD_SECURE                 "sec"
196
197 /** Port. */
198 #define OC_RSRVD_HOSTING_PORT           "port"
199
200 /** For Server instance ID.*/
201 #define OC_RSRVD_SERVER_INSTANCE_ID     "sid"
202
203 /**
204  *  Platform.
205  */
206
207 /** Platform ID. */
208 #define OC_RSRVD_PLATFORM_ID            "pi"
209
210 /** Platform MFG NAME. */
211 #define OC_RSRVD_MFG_NAME               "mnmn"
212
213 /** Platform URL. */
214 #define OC_RSRVD_MFG_URL                "mnml"
215
216 /** Model Number.*/
217 #define OC_RSRVD_MODEL_NUM              "mnmo"
218
219 /** Platform MFG Date.*/
220 #define OC_RSRVD_MFG_DATE               "mndt"
221
222 /** Platform versio.n */
223 #define OC_RSRVD_PLATFORM_VERSION       "mnpv"
224
225 /** Platform Operating system version. */
226 #define OC_RSRVD_OS_VERSION             "mnos"
227
228 /** Platform Hardware version. */
229 #define OC_RSRVD_HARDWARE_VERSION       "mnhw"
230
231 /**Platform Firmware version. */
232 #define OC_RSRVD_FIRMWARE_VERSION       "mnfv"
233
234 /** Support URL for the platform. */
235 #define OC_RSRVD_SUPPORT_URL            "mnsl"
236
237 /** System time for the platform. */
238 #define OC_RSRVD_SYSTEM_TIME             "st"
239
240 /**
241  *  Device.
242  */
243
244 /** Device ID.*/
245 #define OC_RSRVD_DEVICE_ID              "di"
246
247 /** Device Name.*/
248 #define OC_RSRVD_DEVICE_NAME            "n"
249
250 /** Device specification version.*/
251 #define OC_RSRVD_SPEC_VERSION           "lcv"
252
253 /** Device data model.*/
254 #define OC_RSRVD_DATA_MODEL_VERSION     "dmv"
255
256 /** Device specification version.*/
257 #define OC_SPEC_VERSION                "core.1.0.0"
258
259 /** Device Data Model version.*/
260 #define OC_DATA_MODEL_VERSION          "res.1.0.0"
261
262 /**
263  *  These provide backward compatibility - their use is deprecated.
264  */
265 #ifndef GOING_AWAY
266
267 /** Multicast Prefix.*/
268 #define OC_MULTICAST_PREFIX                  "224.0.1.187:5683"
269
270 /** Multicast IP address.*/
271 #define OC_MULTICAST_IP                      "224.0.1.187"
272
273 /** Multicast Port.*/
274 #define OC_MULTICAST_PORT                    5683
275 #endif // GOING_AWAY
276
277 /** Max Device address size. */
278 #ifdef RA_ADAPTER
279 #define MAX_ADDR_STR_SIZE (256)
280 #else
281 #define MAX_ADDR_STR_SIZE (40)
282 #endif
283
284 /** Max identity size. */
285 #define MAX_IDENTITY_SIZE (32)
286
287 /** Resource Directory */
288
289 /** Resource Directory URI used to Discover RD and Publish resources.*/
290 #define OC_RSRVD_RD_URI                  "/oic/rd"
291
292 /** To represent resource type with rd.*/
293 #define OC_RSRVD_RESOURCE_TYPE_RD        "oic.wk.rd"
294
295 /** RD Discovery bias factor type. */
296 #define OC_RSRVD_RD_DISCOVERY_SEL        "sel"
297
298 /** Base URI. */
299 #define OC_RSRVD_BASE_URI                "baseURI"
300
301 /** Unique value per collection/link. */
302 #define OC_RSRVD_INS                     "ins"
303
304 /** Allowable resource types in the links. */
305 #define OC_RSRVD_RTS                     "rts"
306
307 /** Default relationship. */
308 #define OC_RSRVD_DREL                    "drel"
309
310 /** Defines relationship between links. */
311 #define OC_RSRVD_REL                     "rel"
312
313 /** Defines title. */
314 #define OC_RSRVD_TITLE                   "title"
315
316 /** Defines URI. */
317 #define OC_RSRVD_URI                     "uri"
318
319 /** Defines media type. */
320 #define OC_RSRVD_MEDIA_TYPE              "mt"
321
322 /** To represent resource type with Publish RD.*/
323 #define OC_RSRVD_RESOURCE_TYPE_RDPUBLISH "oic.wk.rdPub"
324
325 /**
326  * These enums (OCTransportAdapter and OCTransportFlags) must
327  * be kept synchronized with OCConnectivityType (below) as well as
328  * CATransportAdapter and CATransportFlags (in CACommon.h).
329  */
330 typedef enum
331 {
332     /** value zero indicates discovery.*/
333     OC_DEFAULT_ADAPTER = 0,
334
335     /** IPv4 and IPv6, including 6LoWPAN.*/
336     OC_ADAPTER_IP           = (1 << 0),
337
338     /** GATT over Bluetooth LE.*/
339     OC_ADAPTER_GATT_BTLE    = (1 << 1),
340
341     /** RFCOMM over Bluetooth EDR.*/
342     OC_ADAPTER_RFCOMM_BTEDR = (1 << 2),
343 #ifdef RA_ADAPTER
344     /**Remote Access over XMPP.*/
345     OC_ADAPTER_REMOTE_ACCESS = (1 << 3),
346 #endif
347     /** CoAP over TCP.*/
348     OC_ADAPTER_TCP           = (1 << 4),
349
350     /** NFC Transport for Messaging.*/
351     OC_ADAPTER_NFC           = (1 << 5)
352 } OCTransportAdapter;
353
354 /**
355  *  Enum layout assumes some targets have 16-bit integer (e.g., Arduino).
356  */
357 typedef enum
358 {
359     /** default flag is 0*/
360     OC_DEFAULT_FLAGS = 0,
361
362     /** Insecure transport is the default (subject to change).*/
363     /** secure the transport path*/
364     OC_FLAG_SECURE     = (1 << 4),
365
366     /** IPv4 & IPv6 auto-selection is the default.*/
367     /** IP adapter only.*/
368     OC_IP_USE_V6       = (1 << 5),
369
370     /** IP adapter only.*/
371     OC_IP_USE_V4       = (1 << 6),
372
373     /** internal use only.*/
374     OC_RESERVED1       = (1 << 7),   // internal use only
375
376     /** Link-Local multicast is the default multicast scope for IPv6.
377      *  These are placed here to correspond to the IPv6 multicast address bits.*/
378
379     /** IPv6 Interface-Local scope (loopback).*/
380     OC_SCOPE_INTERFACE = 0x1,
381
382     /** IPv6 Link-Local scope (default).*/
383     OC_SCOPE_LINK      = 0x2,
384
385     /** IPv6 Realm-Local scope. */
386     OC_SCOPE_REALM     = 0x3,
387
388     /** IPv6 Admin-Local scope. */
389     OC_SCOPE_ADMIN     = 0x4,
390
391     /** IPv6 Site-Local scope. */
392     OC_SCOPE_SITE      = 0x5,
393
394     /** IPv6 Organization-Local scope. */
395     OC_SCOPE_ORG       = 0x8,
396
397     /**IPv6 Global scope. */
398     OC_SCOPE_GLOBAL    = 0xE,
399
400 } OCTransportFlags;
401
402 /** Bit mask for scope.*/
403 #define OC_MASK_SCOPE    (0x000F)
404
405 /** Bit mask for Mods.*/
406 #define OC_MASK_MODS     (0x0FF0)
407 #define OC_MASK_FAMS     (OC_IP_USE_V6|OC_IP_USE_V4)
408
409 /**
410  * End point identity.
411  */
412 typedef struct
413 {
414     /** Identity Length */
415     uint16_t id_length;
416
417     /** Array of end point identity.*/
418     unsigned char id[MAX_IDENTITY_SIZE];
419 } OCIdentity;
420
421 /**
422  * Data structure to encapsulate IPv4/IPv6/Contiki/lwIP device addresses.
423  * OCDevAddr must be the same as CAEndpoint (in CACommon.h).
424  */
425 typedef struct
426 {
427     /** adapter type.*/
428     OCTransportAdapter      adapter;
429
430     /** transport modifiers.*/
431     OCTransportFlags        flags;
432
433     /** for IP.*/
434     uint16_t                port;
435
436     /** address for all adapters.*/
437     char                    addr[MAX_ADDR_STR_SIZE];
438
439     /** usually zero for default interface.*/
440     uint32_t                interface;
441 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
442     char                    routeData[MAX_ADDR_STR_SIZE]; //destination GatewayID:ClientId
443 #endif
444 } OCDevAddr;
445
446 /**
447  * This enum type includes elements of both ::OCTransportAdapter and ::OCTransportFlags.
448  * It is defined conditionally because the smaller definition limits expandability on 32/64 bit
449  * integer machines, and the larger definition won't fit into an enum on 16-bit integer machines
450  * like Arduino.
451  *
452  * This structure must directly correspond to ::OCTransportAdapter and ::OCTransportFlags.
453  */
454 typedef enum
455 {
456     /** use when defaults are ok. */
457     CT_DEFAULT = 0,
458
459     /** IPv4 and IPv6, including 6LoWPAN.*/
460     CT_ADAPTER_IP           = (1 << 16),
461
462     /** GATT over Bluetooth LE.*/
463     CT_ADAPTER_GATT_BTLE    = (1 << 17),
464
465     /** RFCOMM over Bluetooth EDR.*/
466     CT_ADAPTER_RFCOMM_BTEDR = (1 << 18),
467
468 #ifdef RA_ADAPTER
469     /** Remote Access over XMPP.*/
470     CT_ADAPTER_REMOTE_ACCESS = (1 << 19),
471 #endif
472     /** CoAP over TCP.*/
473     CT_ADAPTER_TCP     = (1 << 20),
474
475     /** NFC Transport.*/
476     CT_ADAPTER_NFC     = (1 << 21),
477
478     /** Insecure transport is the default (subject to change).*/
479
480     /** secure the transport path.*/
481     CT_FLAG_SECURE     = (1 << 4),
482
483     /** IPv4 & IPv6 autoselection is the default.*/
484
485     /** IP adapter only.*/
486     CT_IP_USE_V6       = (1 << 5),
487
488     /** IP adapter only.*/
489     CT_IP_USE_V4       = (1 << 6),
490
491     /** Link-Local multicast is the default multicast scope for IPv6.
492      * These are placed here to correspond to the IPv6 address bits.*/
493
494     /** IPv6 Interface-Local scope(loopback).*/
495     CT_SCOPE_INTERFACE = 0x1,
496
497     /** IPv6 Link-Local scope (default).*/
498     CT_SCOPE_LINK      = 0x2,
499
500     /** IPv6 Realm-Local scope.*/
501     CT_SCOPE_REALM     = 0x3,
502
503     /** IPv6 Admin-Local scope.*/
504     CT_SCOPE_ADMIN     = 0x4,
505
506     /** IPv6 Site-Local scope.*/
507     CT_SCOPE_SITE      = 0x5,
508
509     /** IPv6 Organization-Local scope.*/
510     CT_SCOPE_ORG       = 0x8,
511
512     /** IPv6 Global scope.*/
513     CT_SCOPE_GLOBAL    = 0xE,
514 } OCConnectivityType;
515
516 /** bit shift required for connectivity adapter.*/
517 #define CT_ADAPTER_SHIFT 16
518
519 /** Mask Flag.*/
520 #define CT_MASK_FLAGS 0xFFFF
521
522 /** Mask Adapter.*/
523 #define CT_MASK_ADAPTER 0xFFFF0000
524
525 /**
526  *  OCDoResource methods to dispatch the request
527  */
528 typedef enum
529 {
530     OC_REST_NOMETHOD       = 0,
531
532     /** Read.*/
533     OC_REST_GET            = (1 << 0),
534
535     /** Write.*/
536     OC_REST_PUT            = (1 << 1),
537
538     /** Update.*/
539     OC_REST_POST           = (1 << 2),
540
541     /** Delete.*/
542     OC_REST_DELETE         = (1 << 3),
543
544     /** Register observe request for most up date notifications ONLY.*/
545     OC_REST_OBSERVE        = (1 << 4),
546
547     /** Register observe request for all notifications, including stale notifications.*/
548     OC_REST_OBSERVE_ALL    = (1 << 5),
549
550     /** De-register observation, intended for internal use.*/
551     OC_REST_CANCEL_OBSERVE = (1 << 6),
552
553     #ifdef WITH_PRESENCE
554     /** Subscribe for all presence notifications of a particular resource.*/
555     OC_REST_PRESENCE       = (1 << 7),
556
557     #endif
558     /** Allows OCDoResource caller to do discovery.*/
559     OC_REST_DISCOVER       = (1 << 8)
560 } OCMethod;
561
562 /**
563  *  Formats for payload encoding.
564  */
565 typedef enum
566 {
567     OC_FORMAT_CBOR,
568     OC_FORMAT_UNDEFINED,
569     OC_FORMAT_UNSUPPORTED,
570 } OCPayloadFormat;
571
572 /**
573  * Host Mode of Operation.
574  */
575 typedef enum
576 {
577     OC_CLIENT = 0,
578     OC_SERVER,
579     OC_CLIENT_SERVER,
580     OC_GATEWAY          /**< Client server mode along with routing capabilities.*/
581 } OCMode;
582
583 /**
584  * Quality of Service attempts to abstract the guarantees provided by the underlying transport
585  * protocol. The precise definitions of each quality of service level depend on the
586  * implementation. In descriptions below are for the current implementation and may changed
587  * over time.
588  */
589 typedef enum
590 {
591     /** Packet delivery is best effort.*/
592     OC_LOW_QOS = 0,
593
594     /** Packet delivery is best effort.*/
595     OC_MEDIUM_QOS,
596
597     /** Acknowledgments are used to confirm delivery.*/
598     OC_HIGH_QOS,
599
600     /** No Quality is defined, let the stack decide.*/
601     OC_NA_QOS
602 } OCQualityOfService;
603
604 /**
605  * Resource Properties.
606  * The value of a policy property is defined as bitmap.
607  * The LSB represents OC_DISCOVERABLE and Second LSB bit represents OC_OBSERVABLE and so on.
608  * Not including the policy property is equivalent to zero.
609  *
610  */
611 typedef enum
612 {
613     /** When none of the bits are set, the resource is non-discoverable &
614      *  non-observable by the client.*/
615     OC_RES_PROP_NONE = (0),
616
617     /** When this bit is set, the resource is allowed to be discovered by clients.*/
618     OC_DISCOVERABLE  = (1 << 0),
619
620     /** When this bit is set, the resource is allowed to be observed by clients.*/
621     OC_OBSERVABLE    = (1 << 1),
622
623     /** When this bit is set, the resource is initialized, otherwise the resource
624      *  is 'inactive'. 'inactive' signifies that the resource has been marked for
625      *  deletion or is already deleted.*/
626     OC_ACTIVE        = (1 << 2),
627
628     /** When this bit is set, the resource has been marked as 'slow'.
629      * 'slow' signifies that responses from this resource can expect delays in
630      *  processing its requests from clients.*/
631     OC_SLOW          = (1 << 3),
632
633     /** When this bit is set, the resource is a secure resource.*/
634     OC_SECURE        = (1 << 4),
635
636     /** When this bit is set, the resource is allowed to be discovered only
637      *  if discovery request contains an explicit querystring.
638      *  Ex: GET /oic/res?rt=oic.sec.acl */
639     OC_EXPLICIT_DISCOVERABLE   = (1 << 5)
640 } OCResourceProperty;
641
642 /**
643  * Transport Protocol IDs.
644  */
645 typedef enum
646 {
647     /** For invalid ID.*/
648     OC_INVALID_ID   = (1 << 0),
649
650     /* For coap ID.*/
651     OC_COAP_ID      = (1 << 1)
652 } OCTransportProtocolID;
653
654 /**
655  * Declares Stack Results & Errors.
656  */
657 typedef enum
658 {
659     /** Success status code - START HERE.*/
660     OC_STACK_OK = 0,
661     OC_STACK_RESOURCE_CREATED,
662     OC_STACK_RESOURCE_DELETED,
663     OC_STACK_CONTINUE,
664     /** Success status code - END HERE.*/
665
666     /** Error status code - START HERE.*/
667     OC_STACK_INVALID_URI = 20,
668     OC_STACK_INVALID_QUERY,
669     OC_STACK_INVALID_IP,
670     OC_STACK_INVALID_PORT,
671     OC_STACK_INVALID_CALLBACK,
672     OC_STACK_INVALID_METHOD,
673
674     /** Invalid parameter.*/
675     OC_STACK_INVALID_PARAM,
676     OC_STACK_INVALID_OBSERVE_PARAM,
677     OC_STACK_NO_MEMORY,
678     OC_STACK_COMM_ERROR,
679     OC_STACK_TIMEOUT,
680     OC_STACK_ADAPTER_NOT_ENABLED,
681     OC_STACK_NOTIMPL,
682
683     /** Resource not found.*/
684     OC_STACK_NO_RESOURCE,
685
686     /** e.g: not supported method or interface.*/
687     OC_STACK_RESOURCE_ERROR,
688     OC_STACK_SLOW_RESOURCE,
689     OC_STACK_DUPLICATE_REQUEST,
690
691     /** Resource has no registered observers.*/
692     OC_STACK_NO_OBSERVERS,
693     OC_STACK_OBSERVER_NOT_FOUND,
694     OC_STACK_VIRTUAL_DO_NOT_HANDLE,
695     OC_STACK_INVALID_OPTION,
696
697     /** The remote reply contained malformed data.*/
698     OC_STACK_MALFORMED_RESPONSE,
699     OC_STACK_PERSISTENT_BUFFER_REQUIRED,
700     OC_STACK_INVALID_REQUEST_HANDLE,
701     OC_STACK_INVALID_DEVICE_INFO,
702     OC_STACK_INVALID_JSON,
703
704     /** Request is not authorized by Resource Server. */
705     OC_STACK_UNAUTHORIZED_REQ,
706
707     /** Error code from PDM */
708     OC_STACK_PDM_IS_NOT_INITIALIZED,
709     OC_STACK_DUPLICATE_UUID,
710     OC_STACK_INCONSISTENT_DB,
711
712     /**
713      * Error code from OTM
714      * This error is plused from DTLS interface when handshake failure happens
715      */
716     OC_STACK_AUTHENTICATION_FAILURE,
717
718     /** Insert all new error codes here!.*/
719     #ifdef WITH_PRESENCE
720     OC_STACK_PRESENCE_STOPPED = 128,
721     OC_STACK_PRESENCE_TIMEOUT,
722     OC_STACK_PRESENCE_DO_NOT_HANDLE,
723     #endif
724     /** ERROR in stack.*/
725     OC_STACK_ERROR = 255
726     /** Error status code - END HERE.*/
727 } OCStackResult;
728
729 /**
730  * Handle to an OCDoResource invocation.
731  */
732 typedef void * OCDoHandle;
733
734 /**
735  * Handle to an OCResource object owned by the OCStack.
736  */
737 typedef void * OCResourceHandle;
738
739 /**
740  * Handle to an OCRequest object owned by the OCStack.
741  */
742 typedef void * OCRequestHandle;
743
744 /**
745  * Unique identifier for each observation request. Used when observations are
746  * registered or de-registered. Used by entity handler to signal specific
747  * observers to be notified of resource changes.
748  * There can be maximum of 256 observations per server.
749  */
750 typedef uint8_t OCObservationId;
751
752 /**
753  * Action associated with observation.
754  */
755 typedef enum
756 {
757     /** To Register. */
758     OC_OBSERVE_REGISTER = 0,
759
760     /** To Deregister. */
761     OC_OBSERVE_DEREGISTER = 1,
762
763     /** Others. */
764     OC_OBSERVE_NO_OPTION = 2
765 } OCObserveAction;
766
767
768 /**
769  * Persistent storage handlers. An APP must provide OCPersistentStorage handler pointers
770  * when it calls OCRegisterPersistentStorageHandler.
771  * Persistent storage open handler points to default file path.
772  * Application can point to appropriate SVR database path for it's IoTivity Server.
773  */
774 typedef struct {
775     /** Persistent storage file path.*/
776     FILE* (* open)(const char *path, const char *mode);
777
778     /** Persistent storage read handler.*/
779     size_t (* read)(void *ptr, size_t size, size_t nmemb, FILE *stream);
780
781     /** Persistent storage write handler.*/
782     size_t (* write)(const void *ptr, size_t size, size_t nmemb, FILE *stream);
783
784     /** Persistent storage close handler.*/
785     int (* close)(FILE *fp);
786
787     /** Persistent storage unlink handler.*/
788     int (* unlink)(const char *path);
789 } OCPersistentStorage;
790
791 /**
792  * Possible returned values from entity handler.
793  */
794 typedef struct
795 {
796     /** Action associated with observation request.*/
797     OCObserveAction action;
798
799     /** Identifier for observation being registered/deregistered.*/
800     OCObservationId obsId;
801 } OCObservationInfo;
802
803 /**
804  * Possible returned values from entity handler.
805  */
806 typedef enum
807 {
808     OC_EH_OK = 0,
809     OC_EH_ERROR,
810     OC_EH_RESOURCE_CREATED,
811     OC_EH_RESOURCE_DELETED,
812     OC_EH_SLOW,
813     OC_EH_FORBIDDEN,
814     OC_EH_RESOURCE_NOT_FOUND
815 } OCEntityHandlerResult;
816
817 /**
818  * This structure will be used to define the vendor specific header options to be included
819  * in communication packets.
820  */
821 typedef struct OCHeaderOption
822 {
823     /** The protocol ID this option applies to.*/
824     OCTransportProtocolID protocolID;
825
826     /** The header option ID which will be added to communication packets.*/
827     uint16_t optionID;
828
829     /** its length 191.*/
830     uint16_t optionLength;
831
832     /** pointer to its data.*/
833     uint8_t optionData[MAX_HEADER_OPTION_DATA_LENGTH];
834
835 #ifdef SUPPORTS_DEFAULT_CTOR
836     OCHeaderOption() = default;
837     OCHeaderOption(OCTransportProtocolID pid,
838                    uint16_t optId,
839                    uint16_t optlen,
840                    const uint8_t* optData)
841         : protocolID(pid),
842           optionID(optId),
843           optionLength(optlen)
844     {
845
846         // parameter includes the null terminator.
847         optionLength = optionLength < MAX_HEADER_OPTION_DATA_LENGTH ?
848                         optionLength : MAX_HEADER_OPTION_DATA_LENGTH;
849         memcpy(optionData, optData, optionLength);
850         optionData[optionLength - 1] = '\0';
851     }
852 #endif
853 } OCHeaderOption;
854
855
856 /**
857  * This structure describes the platform properties. All non-Null properties will be
858  * included in a platform discovery request.
859  */
860 typedef struct
861 {
862     /** Platform ID.*/
863     char *platformID;
864
865     /** Manufacturer name.*/
866     char *manufacturerName;
867
868     /** Manufacturer URL for platform property.*/
869     char *manufacturerUrl;
870
871     /** Model number.*/
872     char *modelNumber;
873
874     /** Manufacturer date.*/
875     char *dateOfManufacture;
876
877     /** Platform version.*/
878     char *platformVersion;
879
880     /** Operating system version.*/
881     char *operatingSystemVersion;
882
883     /** HW version.*/
884     char *hardwareVersion;
885
886     /** FW version.*/
887     char *firmwareVersion;
888
889     /** Platform support URL.*/
890     char *supportUrl;
891
892     /** System time.*/
893     char *systemTime;
894
895 } OCPlatformInfo;
896
897 /**
898  * This structure is expected as input for device properties.
899  * device name is mandatory and expected from the application.
900  * device id of type UUID will be generated by the stack.
901  */
902 typedef struct
903 {
904     /** Pointer to the device name.*/
905     char *deviceName;
906
907 } OCDeviceInfo;
908
909 #ifdef RA_ADAPTER
910 /**
911  * callback for bound JID
912  */
913 typedef void (*jid_bound_cb)(char *jid);
914
915 /**
916  * CA Remote Access information for XMPP Client
917  *
918  */
919 typedef struct
920 {
921     char *hostname;     /**< XMPP server hostname */
922     uint16_t   port;    /**< XMPP server serivce port */
923     char *xmpp_domain;  /**< XMPP login domain */
924     char *username;     /**< login username */
925     char *password;     /**< login password */
926     char *resource;     /**< specific resource for login */
927     char *user_jid;     /**< specific JID for login */
928     jid_bound_cb jidbound;  /**< callback when JID bound */
929 } OCRAInfo_t;
930 #endif  /* RA_ADAPTER */
931
932
933 /** Enum to describe the type of object held by the OCPayload object.*/
934 typedef enum
935 {
936     PAYLOAD_TYPE_INVALID,
937     PAYLOAD_TYPE_DISCOVERY,
938     PAYLOAD_TYPE_DEVICE,
939     PAYLOAD_TYPE_PLATFORM,
940     PAYLOAD_TYPE_REPRESENTATION,
941     PAYLOAD_TYPE_SECURITY,
942     PAYLOAD_TYPE_PRESENCE,
943     PAYLOAD_TYPE_RD
944 } OCPayloadType;
945
946 typedef struct
947 {
948     // The type of message that was received
949     OCPayloadType type;
950 } OCPayload;
951
952 typedef enum
953 {
954     OCREP_PROP_NULL,
955     OCREP_PROP_INT,
956     OCREP_PROP_DOUBLE,
957     OCREP_PROP_BOOL,
958     OCREP_PROP_STRING,
959     OCREP_PROP_BYTE_STRING,
960     OCREP_PROP_OBJECT,
961     OCREP_PROP_ARRAY
962 }OCRepPayloadPropType;
963
964 /** This structure will be used to represent a binary string for CBOR payloads.*/
965 typedef struct
966 {
967     /** pointer to data bytes.*/
968     uint8_t* bytes;
969
970     /** number of data bytes.*/
971     size_t   len;
972 } OCByteString;
973
974 #define MAX_REP_ARRAY_DEPTH 3
975 typedef struct
976 {
977     OCRepPayloadPropType type;
978     size_t dimensions[MAX_REP_ARRAY_DEPTH];
979
980     union
981     {
982         int64_t* iArray;
983         double* dArray;
984         bool* bArray;
985         char** strArray;
986
987         /** pointer to ByteString array.*/
988         OCByteString* ocByteStrArray;
989
990         struct OCRepPayload** objArray;
991     };
992 } OCRepPayloadValueArray;
993
994 typedef struct OCRepPayloadValue
995 {
996     char* name;
997     OCRepPayloadPropType type;
998     union
999     {
1000         int64_t i;
1001         double d;
1002         bool b;
1003         char* str;
1004
1005         /** ByteString object.*/
1006         OCByteString ocByteStr;
1007
1008         struct OCRepPayload* obj;
1009         OCRepPayloadValueArray arr;
1010     };
1011     struct OCRepPayloadValue* next;
1012
1013 } OCRepPayloadValue;
1014
1015 typedef struct OCStringLL
1016 {
1017     struct OCStringLL *next;
1018     char* value;
1019 } OCStringLL;
1020
1021 // used for get/set/put/observe/etc representations
1022 typedef struct OCRepPayload
1023 {
1024     OCPayload base;
1025     char* uri;
1026     OCStringLL* types;
1027     OCStringLL* interfaces;
1028     OCRepPayloadValue* values;
1029     struct OCRepPayload* next;
1030 } OCRepPayload;
1031
1032 // used inside a discovery payload
1033 typedef struct OCResourcePayload
1034 {
1035     char* uri;
1036     OCStringLL* types;
1037     OCStringLL* interfaces;
1038     uint8_t bitmap;
1039     bool secure;
1040     uint16_t port;
1041     struct OCResourcePayload* next;
1042 } OCResourcePayload;
1043
1044 /**
1045  * Structure holding Links Payload. It is a sub-structure used in
1046  * OCResourceCollectionPayload.
1047  */
1048 typedef struct OCLinksPayload
1049 {
1050     /** This is the target relative URI. */
1051     char *href;
1052     /** Resource Type - A standard OIC specified or vendor defined resource
1053      * type of the resource referenced by the target URI. */
1054     OCStringLL *rt;
1055     /** Interface - The interfaces supported by the resource referenced by the target URI. */
1056     OCStringLL *itf;
1057     /** The relation of the target URI referenced by the link to the context URI;
1058      * The default value is null. */
1059     char *rel;
1060     /** Specifies if the resource referenced by the target URIis observable or not. */
1061     bool obs;
1062     /** A title for the link relation. Can be used by the UI to provide a context. */
1063     char *title;
1064     /** This is used to override the context URI e.g. override the URI of the containing collection. */
1065     char *uri;
1066     /** The instance identifier for this web link in an array of web links - used in links. */
1067     union
1068     {
1069         /** An ordinal number that is not repeated - must be unique in the collection context. */
1070         uint8_t ins;
1071         /** Any unique string including a URI. */
1072         char *uniqueStr;
1073         /** Use UUID for universal uniqueness - used in /oic/res to identify the device. */
1074         OCIdentity uniqueUUID;
1075     };
1076     /** A hint of the media type of the representation of the resource referenced by the target URI. */
1077     OCStringLL *mt;
1078     /** Holding address of the next resource. */
1079     struct OCLinksPayload *next;
1080 } OCLinksPayload;
1081
1082 /** Structure holding tags value of the links payload. */
1083 typedef struct
1084 {
1085     /** Name of tags. */
1086     OCDeviceInfo n;
1087     /** Device identifier. */
1088     OCIdentity di;
1089     /** The base URI where the resources are hold. */
1090     char *baseURI;
1091     /** Bitmap holds observable, discoverable, secure option flag.*/
1092     uint8_t bitmap;
1093     /** Port set in case, the secure flag is set above. */
1094     uint16_t port;
1095     /** Id for each set of links i.e. tag. */
1096     union
1097     {
1098         /** An ordinal number that is not repeated - must be unique in the collection context. */
1099         uint8_t ins;
1100         /** Any unique string including a URI. */
1101         char *uniqueStr;
1102         /** Use UUID for universal uniqueness - used in /oic/res to identify the device. */
1103         OCIdentity uniqueUUID;
1104     };
1105     /** Defines the list of allowable resource types (for Target and anchors) in links included
1106      * in the collection; new links being created can only be from this list. */
1107     char *rts;
1108     /** When specified this is the default relationship to use when an OIC Link does not specify
1109      * an explicit relationship with *rel* parameter. */
1110     char *drel;
1111     /** Time to keep holding resource.*/
1112     uint32_t ttl;
1113 } OCTagsPayload;
1114
1115 /** Resource collection payload. */
1116 typedef struct OCResourceCollectionPayload
1117 {
1118     /** Collection tags payload.*/
1119     OCTagsPayload *tags;
1120     /** Array of links payload. */
1121     OCLinksPayload *setLinks;
1122     /** Holding address of the next resource. */
1123     struct OCResourceCollectionPayload *next;
1124 } OCResourceCollectionPayload;
1125
1126 typedef struct
1127 {
1128     OCPayload base;
1129
1130     uint8_t* sid;
1131
1132     /** A special case for handling RD address. */
1133     char* baseURI;
1134
1135     /** This structure holds the old /oic/res response. */
1136     OCResourcePayload *resources;
1137
1138 } OCDiscoveryPayload;
1139
1140 /**
1141  * Structure holding discovery payload.
1142  */
1143 typedef struct
1144 {
1145     /** Device Name. */
1146     OCDeviceInfo n;
1147     /** Device Identity. */
1148     OCIdentity di;
1149     /** Value holding the bias factor of the RD. */
1150     uint8_t sel;
1151 } OCRDDiscoveryPayload;
1152
1153 /**
1154  * RD Payload that will be transmitted over the wire.
1155  */
1156 typedef struct
1157 {
1158     OCPayload base;
1159     /** Pointer to the discovery response payload.*/
1160     OCRDDiscoveryPayload *rdDiscovery;
1161     /** Pointer to the publish payload.*/
1162     OCResourceCollectionPayload *rdPublish;
1163 } OCRDPayload;
1164
1165 typedef struct
1166 {
1167     OCPayload base;
1168     uint8_t* sid;
1169     char* deviceName;
1170     char* specVersion;
1171     char* dataModelVersion;
1172 } OCDevicePayload;
1173
1174 typedef struct
1175 {
1176     OCPayload base;
1177     char* uri;
1178     OCPlatformInfo info;
1179 } OCPlatformPayload;
1180
1181 typedef struct
1182 {
1183     OCPayload base;
1184     char* securityData;
1185 } OCSecurityPayload;
1186 #ifdef WITH_PRESENCE
1187 typedef struct
1188 {
1189     OCPayload base;
1190     uint32_t sequenceNumber;
1191     uint32_t maxAge;
1192     OCPresenceTrigger trigger;
1193     char* resourceType;
1194 } OCPresencePayload;
1195 #endif
1196
1197 /**
1198  * Incoming requests handled by the server. Requests are passed in as a parameter to the
1199  * OCEntityHandler callback API.
1200  * The OCEntityHandler callback API must be implemented in the application in order
1201  * to receive these requests.
1202  */
1203 typedef struct
1204 {
1205     /** Associated resource.*/
1206     OCResourceHandle resource;
1207
1208     /** Associated request handle.*/
1209     OCRequestHandle requestHandle;
1210
1211     /** the REST method retrieved from received request PDU.*/
1212     OCMethod method;
1213
1214     /** description of endpoint that sent the request.*/
1215     OCDevAddr devAddr;
1216
1217     /** resource query send by client.*/
1218     char * query;
1219
1220     /** Information associated with observation - valid only when OCEntityHandler flag includes
1221      * ::OC_OBSERVE_FLAG.*/
1222     OCObservationInfo obsInfo;
1223
1224     /** Number of the received vendor specific header options.*/
1225     uint8_t numRcvdVendorSpecificHeaderOptions;
1226
1227     /** Pointer to the array of the received vendor specific header options.*/
1228     OCHeaderOption * rcvdVendorSpecificHeaderOptions;
1229
1230     /** the payload from the request PDU.*/
1231     OCPayload *payload;
1232
1233 } OCEntityHandlerRequest;
1234
1235
1236 /**
1237  * Response from queries to remote servers. Queries are made by calling the OCDoResource API.
1238  */
1239 typedef struct
1240 {
1241     /** Address of remote server.*/
1242     OCDevAddr devAddr;
1243
1244     /** backward compatibility (points to devAddr).*/
1245     OCDevAddr *addr;
1246
1247     /** backward compatibility.*/
1248     OCConnectivityType connType;
1249
1250     /** the security identity of the remote server.*/
1251     OCIdentity identity;
1252
1253     /** the is the result of our stack, OCStackResult should contain coap/other error codes.*/
1254     OCStackResult result;
1255
1256     /** If associated with observe, this will represent the sequence of notifications from server.*/
1257     uint32_t sequenceNumber;
1258
1259     /** resourceURI.*/
1260     const char * resourceUri;
1261
1262     /** the payload for the response PDU.*/
1263     OCPayload *payload;
1264
1265     /** Number of the received vendor specific header options.*/
1266     uint8_t numRcvdVendorSpecificHeaderOptions;
1267
1268     /** An array of the received vendor specific header options.*/
1269     OCHeaderOption rcvdVendorSpecificHeaderOptions[MAX_HEADER_OPTIONS];
1270 } OCClientResponse;
1271
1272 /**
1273  * Request handle is passed to server via the entity handler for each incoming request.
1274  * Stack assigns when request is received, server sets to indicate what request response is for.
1275  */
1276 typedef struct
1277 {
1278     /** Request handle.*/
1279     OCRequestHandle requestHandle;
1280
1281     /** Resource handle.*/
1282     OCResourceHandle resourceHandle;
1283
1284     /** Allow the entity handler to pass a result with the response.*/
1285     OCEntityHandlerResult  ehResult;
1286
1287     /** This is the pointer to server payload data to be transferred.*/
1288     OCPayload* payload;
1289
1290     /** number of the vendor specific header options .*/
1291     uint8_t numSendVendorSpecificHeaderOptions;
1292
1293     /** An array of the vendor specific header options the entity handler wishes to use in response.*/
1294     OCHeaderOption sendVendorSpecificHeaderOptions[MAX_HEADER_OPTIONS];
1295
1296     /** URI of new resource that entity handler might create.*/
1297     char resourceUri[MAX_URI_LENGTH];
1298
1299     /** Server sets to true for persistent response buffer,false for non-persistent response buffer*/
1300     uint8_t persistentBufferFlag;
1301 } OCEntityHandlerResponse;
1302
1303 /**
1304  * Entity's state
1305  */
1306 typedef enum
1307 {
1308     /** Request state.*/
1309     OC_REQUEST_FLAG = (1 << 1),
1310     /** Observe state.*/
1311     OC_OBSERVE_FLAG = (1 << 2)
1312 } OCEntityHandlerFlag;
1313
1314 /**
1315  * Possible returned values from client application.
1316  */
1317 typedef enum
1318 {
1319     OC_STACK_DELETE_TRANSACTION = 0,
1320     OC_STACK_KEEP_TRANSACTION
1321 } OCStackApplicationResult;
1322
1323
1324 /*
1325  * -------------------------------------------------------------------------------------------
1326  * Callback function definitions
1327  * -------------------------------------------------------------------------------------------
1328  */
1329
1330 /**
1331  * Client applications implement this callback to consume responses received from Servers.
1332  */
1333 typedef OCStackApplicationResult (* OCClientResponseHandler)(void *context, OCDoHandle handle,
1334     OCClientResponse * clientResponse);
1335
1336 /**
1337  * Client applications using a context pointer implement this callback to delete the
1338  * context upon removal of the callback/context pointer from the internal callback-list.
1339  */
1340 typedef void (* OCClientContextDeleter)(void *context);
1341
1342 /**
1343  * This info is passed from application to OC Stack when initiating a request to Server.
1344  */
1345 typedef struct OCCallbackData
1346 {
1347     /** Pointer to the context.*/
1348     void *context;
1349
1350     /** The pointer to a function the stack will call to handle the requests.*/
1351     OCClientResponseHandler cb;
1352
1353     /** A pointer to a function to delete the context when this callback is removed.*/
1354     OCClientContextDeleter cd;
1355
1356 #ifdef SUPPORTS_DEFAULT_CTOR
1357     OCCallbackData() = default;
1358     OCCallbackData(void* ctx, OCClientResponseHandler callback, OCClientContextDeleter deleter)
1359         :context(ctx), cb(callback), cd(deleter){}
1360 #endif
1361 } OCCallbackData;
1362
1363 /**
1364  * Application server implementations must implement this callback to consume requests OTA.
1365  * Entity handler callback needs to fill the resPayload of the entityHandlerRequest.
1366  */
1367 typedef OCEntityHandlerResult (*OCEntityHandler)
1368 (OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest, void* callbackParam);
1369
1370 /**
1371  * Device Entity handler need to use this call back instead of OCEntityHandler.
1372  */
1373 typedef OCEntityHandlerResult (*OCDeviceEntityHandler)
1374 (OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest, char* uri, void* callbackParam);
1375
1376 #ifdef __cplusplus
1377 }
1378 #endif // __cplusplus
1379
1380 #endif /* OCTYPES_H_ */