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