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