blockwise-transfer rebase into master for merge/review
[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 } CATransportAdapter_t;
144
145 typedef enum
146 {
147     CA_DEFAULT_FLAGS = 0,
148
149     // Insecure transport is the default (subject to change)
150     CA_SECURE          = (1 << 4),   // secure the transport path
151     // IPv4 & IPv6 autoselection is the default
152     CA_IPV6            = (1 << 5),   // IP adapter only
153     CA_IPV4            = (1 << 6),   // IP adapter only
154     // Indication that a message was received by multicast.
155     CA_MULTICAST       = (1 << 7),
156     // Link-Local multicast is the default multicast scope for IPv6.
157     // These correspond in both value and position to the IPv6 address bits.
158     CA_SCOPE_INTERFACE = 0x1, // IPv6 Interface-Local scope
159     CA_SCOPE_LINK      = 0x2, // IPv6 Link-Local scope (default)
160     CA_SCOPE_REALM     = 0x3, // IPv6 Realm-Local scope
161     CA_SCOPE_ADMIN     = 0x4, // IPv6 Admin-Local scope
162     CA_SCOPE_SITE      = 0x5, // IPv6 Site-Local scope
163     CA_SCOPE_ORG       = 0x8, // IPv6 Organization-Local scope
164     CA_SCOPE_GLOBAL    = 0xE, // IPv6 Global scope
165 } CATransportFlags_t;
166
167 #define CA_IPFAMILY_MASK (CA_IPV6|CA_IPV4)
168 #define CA_SCOPE_MASK 0xf     // mask scope bits above
169
170 /**
171  * @enum CANetworkStatus_t
172  * Information about the network status.
173  */
174 typedef enum
175 {
176     CA_INTERFACE_DOWN,   /**< Connection is not available */
177     CA_INTERFACE_UP    /**< Connection is Available */
178 } CANetworkStatus_t;
179
180 /*
181  * remoteEndpoint identity
182  */
183 typedef struct
184 {
185     uint16_t id_length;
186     unsigned char id[CA_MAX_ENDPOINT_IDENTITY_LEN];
187 } CARemoteId_t;
188
189 /**
190  * @enum CAMessageType_t
191  * Message Type for Base source code
192  */
193 typedef enum
194 {
195     CA_MSG_CONFIRM = 0,  /**< confirmable message (requires ACK/RST) */
196     CA_MSG_NONCONFIRM,   /**< non-confirmable message (one-shot message) */
197     CA_MSG_ACKNOWLEDGE,  /**< used to acknowledge confirmable messages */
198     CA_MSG_RESET         /**< used to indicates not-interested or error (lack of context)in
199                                                   received messages */
200 } CAMessageType_t;
201
202 /**
203  * @enum CAMethod_t
204  * Allowed method to be used by resource model
205  */
206 typedef enum
207 {
208     CA_GET = 1, /**< GET Method  */
209     CA_POST,    /**< POST Method */
210     CA_PUT,     /**< PUT Method */
211     CA_DELETE   /**< DELETE Method */
212 } CAMethod_t;
213
214 /**
215  * block size
216  * it depends on defined size in libCoAP.
217  */
218 typedef enum
219 {
220     CA_BLOCK_SIZE_16_BYTE = 0,    /**< 16byte */
221     CA_BLOCK_SIZE_32_BYTE = 1,    /**< 32byte */
222     CA_BLOCK_SIZE_64_BYTE = 2,    /**< 64byte */
223     CA_BLOCK_SIZE_128_BYTE = 3,   /**< 128byte */
224     CA_BLOCK_SIZE_256_BYTE = 4,   /**< 256byte */
225     CA_BLOCK_SIZE_512_BYTE = 5,   /**< 512byte */
226     CA_BLOCK_SIZE_1024_BYTE = 6     /**< 1Kbyte */
227 } CABlockSize_t;
228
229 /**
230  * Endpoint information for connectivities
231  * Must be identical to OCDevAddr.
232  */
233 typedef struct
234 {
235     CATransportAdapter_t    adapter;    // adapter type
236     CATransportFlags_t      flags;      // transport modifiers
237     char                    addr[MAX_ADDR_STR_SIZE_CA]; // address for all
238     uint32_t                interface;  // usually zero for default interface
239     uint16_t                port;       // for IP
240     CARemoteId_t identity;              // endpoint identity
241 } CAEndpoint_t;
242
243 /**
244  * @enum CAResult_t
245  * Enums for CA return values
246  */
247 typedef enum
248 {
249     /* Result code - START HERE */
250     CA_STATUS_OK = 0,               /**< Success */
251     CA_STATUS_INVALID_PARAM,        /**< Invalid Parameter */
252     CA_ADAPTER_NOT_ENABLED,         /**< Adapter is not enabled */
253     CA_SERVER_STARTED_ALREADY,      /**< Server is started already */
254     CA_SERVER_NOT_STARTED,          /**< Server is not started*/
255     CA_DESTINATION_NOT_REACHABLE,   /**< Destination is not reachable */
256     CA_SOCKET_OPERATION_FAILED,     /**< Socket operation failed */
257     CA_SEND_FAILED,                 /**< Send request failed */
258     CA_RECEIVE_FAILED,              /**< Receive failed */
259     CA_MEMORY_ALLOC_FAILED,         /**< Memory allocation failed */
260     CA_REQUEST_TIMEOUT,             /**< Request is Timeout */
261     CA_DESTINATION_DISCONNECTED,    /**< Destination is disconnected */
262     CA_NOT_SUPPORTED,               /**< Not supported */
263     CA_STATUS_NOT_INITIALIZED,      /**< Not Initialized*/
264     CA_STATUS_FAILED =255           /**< Failure */
265     /* Result code - END HERE */
266 } CAResult_t;
267
268 /**
269  * @enum CAResponseResult_t
270  * Enums for CA Response values
271  */
272 typedef enum
273 {
274     /* Response status code - START HERE */
275     CA_EMPTY = 0,                    /**< Empty */
276     CA_SUCCESS = 200,                /**< Success */
277     CA_CREATED = 201,                /**< Created */
278     CA_DELETED = 202,                /**< Deleted */
279     CA_VALID = 203,                  /**< Valid */
280     CA_CHANGED = 204,                /**< Changed */
281     CA_CONTENT = 205,                /**< Content */
282     CA_CONTINUE = 231,               /**< Continue */
283     CA_BAD_REQ = 400,                /**< Bad Request */
284     CA_UNAUTHORIZED_REQ = 401,       /**< Unauthorized Request */
285     CA_BAD_OPT = 402,                /**< Bad Option */
286     CA_FORBIDDEN_REQ = 403,          /**< Forbidden Request */
287     CA_NOT_FOUND = 404,              /**< Not found */
288     CA_REQUEST_ENTITY_INCOMPLETE = 408, /**< Request Entity Incomplete */
289     CA_REQUEST_ENTITY_TOO_LARGE = 413,  /**< Request Entity Too Large */
290     CA_INTERNAL_SERVER_ERROR = 500,  /**< Internal Server Error */
291     CA_RETRANSMIT_TIMEOUT = 504      /**< Retransmit timeout */
292     /* Response status code - END HERE */
293 } CAResponseResult_t;
294
295 /**
296  * @enum CATransportProtocolID_t
297  * Transport Protocol IDs for additional options
298  */
299 typedef enum
300 {
301     CA_INVALID_ID = (1 << 0),   /**< Invalid ID */
302     CA_COAP_ID = (1 << 1)       /**< COAP ID */
303 } CATransportProtocolID_t;
304
305 /**
306  * @enum CAAdapterState_t
307  * Adapter State to indicate the network changed notifications.
308  */
309 typedef enum
310 {
311     CA_ADAPTER_DISABLED,   /**< Adapter is Disabled */
312     CA_ADAPTER_ENABLED     /**< Adapter is Enabled */
313 } CAAdapterState_t;
314
315 /**
316  * Header options structure to be filled
317  *
318  * This structure is used to hold header information.
319  */
320 typedef struct
321 {
322     CATransportProtocolID_t protocolID;                     /**< Protocol ID of the Option */
323     uint16_t optionID;                                      /**< The header option ID which will be
324                                                             added to communication packets */
325     uint16_t optionLength;                                  /**< Option Length **/
326     char optionData[CA_MAX_HEADER_OPTION_DATA_LENGTH];      /**< Optional data values**/
327 } CAHeaderOption_t;
328
329 /**
330  * Base Information received
331  *
332  * This structure is used to hold request & response base information
333  */
334 typedef struct
335 {
336
337     CAMessageType_t type;       /**< Qos for the request */
338     uint16_t messageId;         /**< Message id.
339                                  * if message id is zero, it will generated by CA inside.
340                                  * otherwise, you can use it */
341     CAToken_t token;            /**< Token for CA */
342     uint8_t tokenLength;        /**< token length*/
343     CAHeaderOption_t *options;  /** Header Options for the request */
344     uint8_t numOptions;         /**< Number of Header options */
345     CAPayload_t payload;        /**< payload of the request  */
346     size_t payloadSize;         /**< size in bytes of the payload */
347     CAURI_t resourceUri;        /**< Resource URI information **/
348 } CAInfo_t;
349
350 /**
351  * Request Information to be sent
352  *
353  * This structure is used to hold request information
354  */
355 typedef struct
356 {
357     CAMethod_t method;  /**< Name of the Method Allowed */
358     CAInfo_t info;      /**< Information of the request. */
359     bool isMulticast;   /**< is multicast request */
360 } CARequestInfo_t;
361
362 /**
363  * Response information received
364  *
365  * This structure is used to hold response information
366  */
367 typedef struct
368 {
369     CAResponseResult_t result;  /**< Result for response by resource model */
370     CAInfo_t info;              /**< Information of the response */
371 } CAResponseInfo_t;
372
373 /**
374  * Error information from CA
375  *        contains error code and message information
376  *
377  * This structure holds error information
378  */
379 typedef struct
380 {
381     CAResult_t result;  /**< CA API request result  */
382     CAInfo_t info;      /**< message information such as token and payload data
383                              helpful to identify the error */
384 } CAErrorInfo_t;
385
386 /**
387  * CA Remote Access information for XMPP Client
388  *
389  */
390 typedef struct
391 {
392     char *hostname;     /**< XMPP server hostname */
393     uint16_t port;      /**< XMPP server serivce port */
394     char *xmpp_domain;  /**< XMPP login domain */
395     char *username;     /**< login username */
396     char *password;     /**< login password */
397     char *resource;     /**< specific resource for login */
398     char *user_jid;     /**< specific JID for login */
399 } CARAInfo_t;
400
401
402 /**
403  * Hold global variables for CA layer (also used by RI layer)
404  */
405 typedef struct
406 {
407     int fd;
408     uint16_t port;
409 } CASocket_t;
410
411 typedef struct
412 {
413     CATransportFlags_t clientFlags;
414     CATransportFlags_t serverFlags;
415     bool client;
416     bool server;
417
418     struct sockets
419     {
420         void *threadpool;   /**< threadpool between Initialize and Start */
421         CASocket_t u6;      /**< unicast   IPv6 */
422         CASocket_t u6s;     /**< unicast   IPv6 secure */
423         CASocket_t u4;      /**< unicast   IPv4 */
424         CASocket_t u4s;     /**< unicast   IPv4 secure */
425         CASocket_t m6;      /**< multicast IPv6 */
426         CASocket_t m6s;     /**< multicast IPv6 secure */
427         CASocket_t m4;      /**< multicast IPv4 */
428         CASocket_t m4s;     /**< multicast IPv4 secure */
429         int netlinkFd;      /**< netlink */
430         int shutdownFds[2]; /**< shutdown pipe */
431         int selectTimeout;  /**< in seconds */
432         int maxfd;          /**< highest fd (for select) */
433         int numInterfaces;  /**< number of active interfaces */
434         bool started;       /**< the IP adapter has started */
435         bool terminate;     /**< the IP adapter needs to stop */
436         bool ipv6enabled;   /**< IPv6 enabled by OCInit flags */
437         bool ipv4enabled;   /**< IPv4 enabled by OCInit flags */
438     } ip;
439
440     struct calayer
441     {
442         CATransportFlags_t previousRequestFlags; /**< address family filtering */
443         uint16_t previousRequestMessageId;       /**< address family filtering */
444     } ca;
445 } CAGlobals_t;
446
447 extern CAGlobals_t caglobals;
448
449 #ifdef __cplusplus
450 } /* extern "C" */
451 #endif
452
453 #endif // CA_COMMON_H_