Imported Upstream version 1.1.1
[platform/upstream/iotivity.git] / resource / csdk / connectivity / api / cacommon.h
1 /* ****************************************************************
2  *
3  * Copyright 2014 Samsung Electronics 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  * @file cacommon.h
23  * This file contains the common data structures between Resource , CA and adapters
24  */
25
26 #ifndef CA_COMMON_H_
27 #define CA_COMMON_H_
28
29 #ifdef TCP_ADAPTER
30 #define HAVE_SYS_POLL_H
31 #endif
32
33 #include <stdint.h>
34 #include <stdlib.h>
35 #include <stdbool.h>
36
37 #ifdef HAVE_SYS_POLL_H
38 #include <sys/poll.h>
39 #endif
40
41 #ifdef __cplusplus
42 extern "C"
43 {
44 #endif
45
46 /**
47  * IP address Length
48  */
49 #define CA_IPADDR_SIZE 16
50
51 /**
52  * Remote Access jabber ID length.
53  */
54 #define CA_RAJABBERID_SIZE 256
55
56 /**
57  * Mac address length for BT port
58  */
59 #define CA_MACADDR_SIZE 18
60
61 /**
62  * Max header options data length
63  */
64 #define CA_MAX_HEADER_OPTION_DATA_LENGTH 20
65
66 /**
67 * Max token length
68 */
69 #define CA_MAX_TOKEN_LEN (8)
70
71 /**
72  * Max URI length
73  */
74 #ifdef ARDUINO
75 #define CA_MAX_URI_LENGTH 128  /* maximum size of URI for embedded platforms*/
76 #else
77 #define CA_MAX_URI_LENGTH 512 /* maximum size of URI for other platforms*/
78 #endif
79
80 /**
81  * Max PDU length supported
82  */
83 #ifdef ARDUINO
84 #define COAP_MAX_PDU_SIZE           320  /* maximum size of a CoAP PDU for embedded platforms*/
85 #else
86 #define COAP_MAX_PDU_SIZE           1400 /* maximum size of a CoAP PDU for big platforms*/
87 #endif
88
89 #ifdef WITH_BWT
90 #define CA_DEFAULT_BLOCK_SIZE       CA_BLOCK_SIZE_1024_BYTE
91 #endif
92
93 /**
94  *Maximum length of the remoteEndpoint identity
95  */
96 #define CA_MAX_ENDPOINT_IDENTITY_LEN   (32)
97
98 /**
99  * option types - the highest option number 63
100  */
101 #define CA_OPTION_IF_MATCH 1
102 #define CA_OPTION_ETAG 4
103 #define CA_OPTION_IF_NONE_MATCH 5
104 #define CA_OPTION_OBSERVE 6
105 #define CA_OPTION_LOCATION_PATH 8
106 #define CA_OPTION_URI_PATH 11
107 #define CA_OPTION_CONTENT_FORMAT 12
108 #define CA_OPTION_CONTENT_TYPE COAP_OPTION_CONTENT_FORMAT
109 #define CA_OPTION_MAXAGE 14
110 #define CA_OPTION_URI_QUERY 15
111 #define CA_OPTION_ACCEPT 17
112 #define CA_OPTION_LOCATION_QUERY 20
113
114 /**
115  * Payload information from resource model
116  */
117 typedef uint8_t *CAPayload_t;
118
119 /**
120  * URI for the OIC base.CA considers relative URI as the URI.
121  */
122 typedef char *CAURI_t;
123
124 /**
125  * Token information for mapping the request and responses by resource model
126  */
127 typedef char *CAToken_t;
128
129 // The following flags are the same as the equivalent OIC values in
130 // octypes.h, allowing direct copying with slight fixup.
131 // The CA layer should used the OC types when build allows that.
132 #ifdef RA_ADAPTER
133 #define MAX_ADDR_STR_SIZE_CA (256)
134 #else
135 #define MAX_ADDR_STR_SIZE_CA (40)
136 #endif
137
138 typedef enum
139 {
140     CA_DEFAULT_ADAPTER = 0,
141
142     // value zero indicates discovery
143     CA_ADAPTER_IP            = (1 << 0),   // IPv4 and IPv6, including 6LoWPAN
144     CA_ADAPTER_GATT_BTLE     = (1 << 1),   // GATT over Bluetooth LE
145     CA_ADAPTER_RFCOMM_BTEDR  = (1 << 2),   // RFCOMM over Bluetooth EDR
146
147 #ifdef RA_ADAPTER
148     CA_ADAPTER_REMOTE_ACCESS = (1 << 3),   // Remote Access over XMPP.
149 #endif
150
151     CA_ADAPTER_TCP           = (1 << 4),   // CoAP over TCP
152     CA_ADAPTER_NFC           = (1 << 5),   // NFC Adapter
153
154     CA_ALL_ADAPTERS          = 0xffffffff
155 } CATransportAdapter_t;
156
157 typedef enum
158 {
159     CA_DEFAULT_FLAGS = 0,
160
161     // Insecure transport is the default (subject to change)
162     CA_SECURE          = (1 << 4),   // secure the transport path
163     // IPv4 & IPv6 autoselection is the default
164     CA_IPV6            = (1 << 5),   // IP adapter only
165     CA_IPV4            = (1 << 6),   // IP adapter only
166     // Indication that a message was received by multicast.
167     CA_MULTICAST       = (1 << 7),
168     // Link-Local multicast is the default multicast scope for IPv6.
169     // These correspond in both value and position to the IPv6 address bits.
170     CA_SCOPE_INTERFACE = 0x1, // IPv6 Interface-Local scope
171     CA_SCOPE_LINK      = 0x2, // IPv6 Link-Local scope (default)
172     CA_SCOPE_REALM     = 0x3, // IPv6 Realm-Local scope
173     CA_SCOPE_ADMIN     = 0x4, // IPv6 Admin-Local scope
174     CA_SCOPE_SITE      = 0x5, // IPv6 Site-Local scope
175     CA_SCOPE_ORG       = 0x8, // IPv6 Organization-Local scope
176     CA_SCOPE_GLOBAL    = 0xE, // IPv6 Global scope
177 } CATransportFlags_t;
178
179 #define CA_IPFAMILY_MASK (CA_IPV6|CA_IPV4)
180 #define CA_SCOPE_MASK 0xf     // mask scope bits above
181
182 /**
183  * @enum CANetworkStatus_t
184  * Information about the network status.
185  */
186 typedef enum
187 {
188     CA_INTERFACE_DOWN,   /**< Connection is not available */
189     CA_INTERFACE_UP    /**< Connection is Available */
190 } CANetworkStatus_t;
191
192 /*
193  * remoteEndpoint identity
194  */
195 typedef struct
196 {
197     uint16_t id_length;
198     unsigned char id[CA_MAX_ENDPOINT_IDENTITY_LEN];
199 } CARemoteId_t;
200
201 /**
202  * @enum CAMessageType_t
203  * Message Type for Base source code
204  */
205 typedef enum
206 {
207     CA_MSG_CONFIRM = 0,  /**< confirmable message (requires ACK/RST) */
208     CA_MSG_NONCONFIRM,   /**< non-confirmable message (one-shot message) */
209     CA_MSG_ACKNOWLEDGE,  /**< used to acknowledge confirmable messages */
210     CA_MSG_RESET         /**< used to indicates not-interested or error (lack of context)in
211                                                   received messages */
212 } CAMessageType_t;
213
214 /**
215  * @enum CAMethod_t
216  * Allowed method to be used by resource model
217  */
218 typedef enum
219 {
220     CA_GET = 1, /**< GET Method  */
221     CA_POST,    /**< POST Method */
222     CA_PUT,     /**< PUT Method */
223     CA_DELETE   /**< DELETE Method */
224 } CAMethod_t;
225
226 /**
227  * block size
228  * it depends on defined size in libCoAP.
229  */
230 typedef enum
231 {
232     CA_BLOCK_SIZE_16_BYTE = 0,    /**< 16byte */
233     CA_BLOCK_SIZE_32_BYTE = 1,    /**< 32byte */
234     CA_BLOCK_SIZE_64_BYTE = 2,    /**< 64byte */
235     CA_BLOCK_SIZE_128_BYTE = 3,   /**< 128byte */
236     CA_BLOCK_SIZE_256_BYTE = 4,   /**< 256byte */
237     CA_BLOCK_SIZE_512_BYTE = 5,   /**< 512byte */
238     CA_BLOCK_SIZE_1024_BYTE = 6     /**< 1Kbyte */
239 } CABlockSize_t;
240
241 /**
242  * Endpoint information for connectivities
243  * Must be identical to OCDevAddr.
244  */
245 typedef struct
246 {
247     CATransportAdapter_t    adapter;    // adapter type
248     CATransportFlags_t      flags;      // transport modifiers
249     uint16_t                port;       // for IP
250     char                    addr[MAX_ADDR_STR_SIZE_CA]; // address for all
251     uint32_t                interface;  // usually zero for default interface
252 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
253     char                    routeData[MAX_ADDR_STR_SIZE_CA]; /**< GatewayId:ClientId of
254                                                                     destination. **/
255 #endif
256 } CAEndpoint_t;
257
258 /**
259  * Endpoint information for secure messages
260  */
261 typedef struct
262 {
263     CAEndpoint_t endpoint;      /**< endpoint */
264     CARemoteId_t identity;      /**< endpoint identity */
265 } CASecureEndpoint_t;
266
267 /**
268  * @enum CAResult_t
269  * Enums for CA return values
270  */
271 typedef enum
272 {
273     /* Result code - START HERE */
274     CA_STATUS_OK = 0,               /**< Success */
275     CA_STATUS_INVALID_PARAM,        /**< Invalid Parameter */
276     CA_ADAPTER_NOT_ENABLED,         /**< Adapter is not enabled */
277     CA_SERVER_STARTED_ALREADY,      /**< Server is started already */
278     CA_SERVER_NOT_STARTED,          /**< Server is not started*/
279     CA_DESTINATION_NOT_REACHABLE,   /**< Destination is not reachable */
280     CA_SOCKET_OPERATION_FAILED,     /**< Socket operation failed */
281     CA_SEND_FAILED,                 /**< Send request failed */
282     CA_RECEIVE_FAILED,              /**< Receive failed */
283     CA_MEMORY_ALLOC_FAILED,         /**< Memory allocation failed */
284     CA_REQUEST_TIMEOUT,             /**< Request is Timeout */
285     CA_DESTINATION_DISCONNECTED,    /**< Destination is disconnected */
286     CA_NOT_SUPPORTED,               /**< Not supported */
287     CA_STATUS_NOT_INITIALIZED,      /**< Not Initialized*/
288     CA_DTLS_AUTHENTICATION_FAILURE, /**< Decryption error in DTLS */
289     CA_STATUS_FAILED =255           /**< Failure */
290     /* Result code - END HERE */
291 } CAResult_t;
292
293 /**
294  * @enum CAResponseResult_t
295  * Enums for CA Response values
296  */
297 typedef enum
298 {
299     /* Response status code - START HERE */
300     CA_EMPTY = 0,                    /**< Empty */
301     CA_CREATED = 201,                /**< Created */
302     CA_DELETED = 202,                /**< Deleted */
303     CA_VALID = 203,                  /**< Valid */
304     CA_CHANGED = 204,                /**< Changed */
305     CA_CONTENT = 205,                /**< Content */
306     CA_CONTINUE = 231,               /**< Continue */
307     CA_BAD_REQ = 400,                /**< Bad Request */
308     CA_UNAUTHORIZED_REQ = 401,       /**< Unauthorized Request */
309     CA_BAD_OPT = 402,                /**< Bad Option */
310     CA_FORBIDDEN_REQ = 403,          /**< Forbidden Request */
311     CA_NOT_FOUND = 404,              /**< Not found */
312     CA_NOT_ACCEPTABLE = 406,         /**< Not Acceptable */
313     CA_REQUEST_ENTITY_INCOMPLETE = 408, /**< Request Entity Incomplete */
314     CA_REQUEST_ENTITY_TOO_LARGE = 413,  /**< Request Entity Too Large */
315     CA_INTERNAL_SERVER_ERROR = 500,  /**< Internal Server Error */
316     CA_RETRANSMIT_TIMEOUT = 504      /**< Retransmit timeout */
317     /* Response status code - END HERE */
318 } CAResponseResult_t;
319
320 /**
321  * @enum CATransportProtocolID_t
322  * Transport Protocol IDs for additional options
323  */
324 typedef enum
325 {
326     CA_INVALID_ID = (1 << 0),   /**< Invalid ID */
327     CA_COAP_ID = (1 << 1)       /**< COAP ID */
328 } CATransportProtocolID_t;
329
330 /**
331  * @enum CAAdapterState_t
332  * Adapter State to indicate the network changed notifications.
333  */
334 typedef enum
335 {
336     CA_ADAPTER_DISABLED,   /**< Adapter is Disabled */
337     CA_ADAPTER_ENABLED     /**< Adapter is Enabled */
338 } CAAdapterState_t;
339
340 /**
341  * Format indicating which encoding has been used on the payload.
342  */
343 typedef enum
344 {
345     CA_FORMAT_UNDEFINED = 0,            /**< Undefined enoding format */
346     CA_FORMAT_TEXT_PLAIN,
347     CA_FORMAT_APPLICATION_LINK_FORMAT,
348     CA_FORMAT_APPLICATION_XML,
349     CA_FORMAT_APPLICATION_OCTET_STREAM,
350     CA_FORMAT_APPLICATION_RDF_XML,
351     CA_FORMAT_APPLICATION_EXI,
352     CA_FORMAT_APPLICATION_JSON,
353     CA_FORMAT_APPLICATION_CBOR,
354     CA_FORMAT_UNSUPPORTED
355 } CAPayloadFormat_t;
356
357 /**
358  * Header options structure to be filled
359  *
360  * This structure is used to hold header information.
361  */
362 typedef struct
363 {
364     CATransportProtocolID_t protocolID;                     /**< Protocol ID of the Option */
365     uint16_t optionID;                                      /**< The header option ID which will be
366                                                             added to communication packets */
367     uint16_t optionLength;                                  /**< Option Length **/
368     char optionData[CA_MAX_HEADER_OPTION_DATA_LENGTH];      /**< Optional data values**/
369 } CAHeaderOption_t;
370
371 /**
372  * Base Information received
373  *
374  * This structure is used to hold request & response base information
375  */
376 typedef struct
377 {
378
379     CAMessageType_t type;       /**< Qos for the request */
380 #ifdef ROUTING_GATEWAY
381     bool skipRetransmission;    /**< Will not attempt retransmission even if type is CONFIRM.
382                                      Required for packet forwarding */
383 #endif
384     uint16_t messageId;         /**< Message id.
385                                  * if message id is zero, it will generated by CA inside.
386                                  * otherwise, you can use it */
387     CAToken_t token;            /**< Token for CA */
388     uint8_t tokenLength;        /**< token length*/
389     CAHeaderOption_t *options;  /** Header Options for the request */
390     uint8_t numOptions;         /**< Number of Header options */
391     CAPayload_t payload;        /**< payload of the request  */
392     size_t payloadSize;         /**< size in bytes of the payload */
393     CAPayloadFormat_t payloadFormat;    /**< encoding format of the request payload */
394     CAPayloadFormat_t acceptFormat;     /**< accept format for the response payload */
395     CAURI_t resourceUri;        /**< Resource URI information **/
396     CARemoteId_t identity;      /**< endpoint identity */
397 } CAInfo_t;
398
399 /**
400  * Request Information to be sent
401  *
402  * This structure is used to hold request information
403  */
404 typedef struct
405 {
406     CAMethod_t method;  /**< Name of the Method Allowed */
407     CAInfo_t info;      /**< Information of the request. */
408     bool isMulticast;   /**< is multicast request */
409 } CARequestInfo_t;
410
411 /**
412  * Response information received
413  *
414  * This structure is used to hold response information
415  */
416 typedef struct
417 {
418     CAResponseResult_t result;  /**< Result for response by resource model */
419     CAInfo_t info;              /**< Information of the response */
420     bool isMulticast;
421 } CAResponseInfo_t;
422
423 /**
424  * Error information from CA
425  *        contains error code and message information
426  *
427  * This structure holds error information
428  */
429 typedef struct
430 {
431     CAResult_t result;  /**< CA API request result  */
432     CAInfo_t info;      /**< message information such as token and payload data
433                              helpful to identify the error */
434 } CAErrorInfo_t;
435
436 /**
437  * Hold global variables for CA layer (also used by RI layer)
438  */
439 typedef struct
440 {
441     int fd;        /**< socket fd */
442     uint16_t port; /**< socket port */
443 } CASocket_t;
444
445 #define HISTORYSIZE (4)
446
447 typedef struct
448 {
449     CATransportFlags_t flags;
450     uint16_t messageId;
451     char token[CA_MAX_TOKEN_LEN];
452     uint8_t tokenLength;
453 } CAHistoryItem_t;
454
455 typedef struct
456 {
457     int nextIndex;
458     CAHistoryItem_t items[HISTORYSIZE];
459 } CAHistory_t;
460
461 /**
462  * Hold interface index for keeping track of comings and goings
463  */
464 typedef struct
465 {
466     int32_t ifIndex; /**< network interface index */
467 } CAIfItem_t;
468
469 typedef struct
470 {
471     CATransportFlags_t clientFlags; /**< flag for client */
472     CATransportFlags_t serverFlags; /**< flag for server */
473     bool client; /**< client mode */
474     bool server; /**< server mode */
475
476     struct sockets
477     {
478         void *threadpool;   /**< threadpool between Initialize and Start */
479         CASocket_t u6;      /**< unicast   IPv6 */
480         CASocket_t u6s;     /**< unicast   IPv6 secure */
481         CASocket_t u4;      /**< unicast   IPv4 */
482         CASocket_t u4s;     /**< unicast   IPv4 secure */
483         CASocket_t m6;      /**< multicast IPv6 */
484         CASocket_t m6s;     /**< multicast IPv6 secure */
485         CASocket_t m4;      /**< multicast IPv4 */
486         CASocket_t m4s;     /**< multicast IPv4 secure */
487         int netlinkFd;      /**< netlink */
488         int shutdownFds[2]; /**< shutdown pipe */
489         int selectTimeout;  /**< in seconds */
490         int maxfd;          /**< highest fd (for select) */
491         bool started;       /**< the IP adapter has started */
492         bool terminate;     /**< the IP adapter needs to stop */
493         bool ipv6enabled;   /**< IPv6 enabled by OCInit flags */
494         bool ipv4enabled;   /**< IPv4 enabled by OCInit flags */
495         bool dualstack;     /**< IPv6 and IPv4 enabled */
496
497         struct networkmonitors
498         {
499             CAIfItem_t *ifItems; /**< current network interface index list */
500             size_t sizeIfItems;  /**< size of network interface index array */
501             size_t numIfItems;   /**< number of valid network interfaces */
502         } nm;
503     } ip;
504
505     struct calayer
506     {
507         CAHistory_t requestHistory;  /**< filter IP family in requests */
508     } ca;
509
510 #ifdef TCP_ADAPTER
511     /**
512      * Hold global variables for TCP Adapter.
513      */
514     struct tcpsockets
515     {
516         void *threadpool;       /**< threadpool between Initialize and Start */
517         CASocket_t ipv4;        /**< IPv4 accept socket */
518         CASocket_t ipv6;        /**< IPv6 accept socket */
519         void *svrlist;          /**< unicast IPv4 TCP server information*/
520         int selectTimeout;      /**< in seconds */
521         int listenBacklog;      /**< backlog counts*/
522         int shutdownFds[2];     /**< shutdown pipe */
523         int connectionFds[2];   /**< connection pipe */
524         int maxfd;              /**< highest fd (for select) */
525         bool started;           /**< the TCP adapter has started */
526         bool terminate;         /**< the TCP adapter needs to stop */
527         bool ipv4tcpenabled;    /**< IPv4 TCP enabled by OCInit flags */
528         bool ipv6tcpenabled;    /**< IPv6 TCP enabled by OCInit flags */
529     } tcp;
530 #endif
531 } CAGlobals_t;
532
533 extern CAGlobals_t caglobals;
534
535 /**
536  * Callback function type for request delivery.
537  * @param[out]   object       Endpoint object from which the request is received.
538  *                            It contains endpoint address based on the connectivity type.
539  * @param[out]   requestInfo  Info for resource model to understand about the request.
540  */
541 typedef void (*CARequestCallback)(const CAEndpoint_t *object,
542                                   const CARequestInfo_t *requestInfo);
543
544 /**
545  * Callback function type for response delivery.
546  * @param[out]   object           Endpoint object from which the response is received.
547  * @param[out]   responseInfo     Identifier which needs to be mapped with response.
548  */
549 typedef void (*CAResponseCallback)(const CAEndpoint_t *object,
550                                    const CAResponseInfo_t *responseInfo);
551 /**
552  * Callback function type for error.
553  * @param[out]   object           remote device information.
554  * @param[out]   errorInfo        CA Error information.
555  */
556 typedef void (*CAErrorCallback)(const CAEndpoint_t *object,
557                                 const CAErrorInfo_t *errorInfo);
558
559 /**
560  * Callback function type for network status changes delivery from CA common logic.
561  * @param[out]   info       Endpoint object from which the network status is changed.
562  *                          It contains endpoint address based on the connectivity type.
563  * @param[out]   status     Current network status info.
564  */
565 typedef void (*CANetworkMonitorCallback)(const CAEndpoint_t *info, CANetworkStatus_t status);
566
567 #ifdef __cplusplus
568 } /* extern "C" */
569 #endif
570
571 #endif // CA_COMMON_H_