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