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