1 /* ****************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
23 * This file contains the common data structures between Resource , CA and adapters
30 #define HAVE_SYS_POLL_H
37 #ifdef HAVE_SYS_POLL_H
49 #define CA_IPADDR_SIZE 16
52 * Remote Access jabber ID length.
54 #define CA_RAJABBERID_SIZE 256
57 * Mac address length for BT port
59 #define CA_MACADDR_SIZE 18
62 * Max header options data length
64 #define CA_MAX_HEADER_OPTION_DATA_LENGTH 20
69 #define CA_MAX_TOKEN_LEN (8)
75 #define CA_MAX_URI_LENGTH 128 /* maximum size of URI for embedded platforms*/
77 #define CA_MAX_URI_LENGTH 512 /* maximum size of URI for other platforms*/
81 * Max PDU length supported
84 #define COAP_MAX_PDU_SIZE 320 /* maximum size of a CoAP PDU for embedded platforms*/
86 #define COAP_MAX_PDU_SIZE 1400 /* maximum size of a CoAP PDU for big platforms*/
90 #define CA_DEFAULT_BLOCK_SIZE CA_BLOCK_SIZE_1024_BYTE
94 *Maximum length of the remoteEndpoint identity
96 #define CA_MAX_ENDPOINT_IDENTITY_LEN (32)
99 * option types - the highest option number 63
101 #define CA_OPTION_IF_MATCH 1
102 #define CA_OPTION_ETAG 4
103 #define CA_OPTION_IF_NONE_MATCH 5
104 #define CA_OPTION_OBSERVE 6
105 #define CA_OPTION_LOCATION_PATH 8
106 #define CA_OPTION_URI_PATH 11
107 #define CA_OPTION_CONTENT_FORMAT 12
108 #define CA_OPTION_CONTENT_TYPE COAP_OPTION_CONTENT_FORMAT
109 #define CA_OPTION_MAXAGE 14
110 #define CA_OPTION_URI_QUERY 15
111 #define CA_OPTION_ACCEPT 17
112 #define CA_OPTION_LOCATION_QUERY 20
115 * Payload information from resource model
117 typedef uint8_t *CAPayload_t;
120 * URI for the OIC base.CA considers relative URI as the URI.
122 typedef char *CAURI_t;
125 * Token information for mapping the request and responses by resource model
127 typedef char *CAToken_t;
129 // The following flags are the same as the equivalent OIC values in
130 // octypes.h, allowing direct copying with slight fixup.
131 // The CA layer should used the OC types when build allows that.
133 #define MAX_ADDR_STR_SIZE_CA (256)
135 #define MAX_ADDR_STR_SIZE_CA (40)
140 CA_DEFAULT_ADAPTER = 0,
142 // value zero indicates discovery
143 CA_ADAPTER_IP = (1 << 0), // IPv4 and IPv6, including 6LoWPAN
144 CA_ADAPTER_GATT_BTLE = (1 << 1), // GATT over Bluetooth LE
145 CA_ADAPTER_RFCOMM_BTEDR = (1 << 2), // RFCOMM over Bluetooth EDR
148 CA_ADAPTER_REMOTE_ACCESS = (1 << 3), // Remote Access over XMPP.
151 CA_ADAPTER_TCP = (1 << 4), // CoAP over TCP
152 CA_ADAPTER_NFC = (1 << 5), // NFC Adapter
154 CA_ALL_ADAPTERS = 0xffffffff
155 } CATransportAdapter_t;
159 CA_DEFAULT_FLAGS = 0,
161 // Insecure transport is the default (subject to change)
162 CA_SECURE = (1 << 4), // secure the transport path
163 // IPv4 & IPv6 autoselection is the default
164 CA_IPV6 = (1 << 5), // IP adapter only
165 CA_IPV4 = (1 << 6), // IP adapter only
166 // Indication that a message was received by multicast.
167 CA_MULTICAST = (1 << 7),
168 // Link-Local multicast is the default multicast scope for IPv6.
169 // These correspond in both value and position to the IPv6 address bits.
170 CA_SCOPE_INTERFACE = 0x1, // IPv6 Interface-Local scope
171 CA_SCOPE_LINK = 0x2, // IPv6 Link-Local scope (default)
172 CA_SCOPE_REALM = 0x3, // IPv6 Realm-Local scope
173 CA_SCOPE_ADMIN = 0x4, // IPv6 Admin-Local scope
174 CA_SCOPE_SITE = 0x5, // IPv6 Site-Local scope
175 CA_SCOPE_ORG = 0x8, // IPv6 Organization-Local scope
176 CA_SCOPE_GLOBAL = 0xE, // IPv6 Global scope
177 } CATransportFlags_t;
179 #define CA_IPFAMILY_MASK (CA_IPV6|CA_IPV4)
180 #define CA_SCOPE_MASK 0xf // mask scope bits above
183 * @enum CANetworkStatus_t
184 * Information about the network status.
188 CA_INTERFACE_DOWN, /**< Connection is not available */
189 CA_INTERFACE_UP /**< Connection is Available */
193 * remoteEndpoint identity
198 unsigned char id[CA_MAX_ENDPOINT_IDENTITY_LEN];
202 * @enum CAMessageType_t
203 * Message Type for Base source code
207 CA_MSG_CONFIRM = 0, /**< confirmable message (requires ACK/RST) */
208 CA_MSG_NONCONFIRM, /**< non-confirmable message (one-shot message) */
209 CA_MSG_ACKNOWLEDGE, /**< used to acknowledge confirmable messages */
210 CA_MSG_RESET /**< used to indicates not-interested or error (lack of context)in
216 * Allowed method to be used by resource model
220 CA_GET = 1, /**< GET Method */
221 CA_POST, /**< POST Method */
222 CA_PUT, /**< PUT Method */
223 CA_DELETE /**< DELETE Method */
228 * it depends on defined size in libCoAP.
232 CA_BLOCK_SIZE_16_BYTE = 0, /**< 16byte */
233 CA_BLOCK_SIZE_32_BYTE = 1, /**< 32byte */
234 CA_BLOCK_SIZE_64_BYTE = 2, /**< 64byte */
235 CA_BLOCK_SIZE_128_BYTE = 3, /**< 128byte */
236 CA_BLOCK_SIZE_256_BYTE = 4, /**< 256byte */
237 CA_BLOCK_SIZE_512_BYTE = 5, /**< 512byte */
238 CA_BLOCK_SIZE_1024_BYTE = 6 /**< 1Kbyte */
242 * Endpoint information for connectivities
243 * Must be identical to OCDevAddr.
247 CATransportAdapter_t adapter; // adapter type
248 CATransportFlags_t flags; // transport modifiers
249 uint16_t port; // for IP
250 char addr[MAX_ADDR_STR_SIZE_CA]; // address for all
251 uint32_t interface; // usually zero for default interface
252 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
253 char routeData[MAX_ADDR_STR_SIZE_CA]; /**< GatewayId:ClientId of
259 * Endpoint information for secure messages
263 CAEndpoint_t endpoint; /**< endpoint */
264 CARemoteId_t identity; /**< endpoint identity */
265 } CASecureEndpoint_t;
269 * Enums for CA return values
273 /* Result code - START HERE */
274 CA_STATUS_OK = 0, /**< Success */
275 CA_STATUS_INVALID_PARAM, /**< Invalid Parameter */
276 CA_ADAPTER_NOT_ENABLED, /**< Adapter is not enabled */
277 CA_SERVER_STARTED_ALREADY, /**< Server is started already */
278 CA_SERVER_NOT_STARTED, /**< Server is not started*/
279 CA_DESTINATION_NOT_REACHABLE, /**< Destination is not reachable */
280 CA_SOCKET_OPERATION_FAILED, /**< Socket operation failed */
281 CA_SEND_FAILED, /**< Send request failed */
282 CA_RECEIVE_FAILED, /**< Receive failed */
283 CA_MEMORY_ALLOC_FAILED, /**< Memory allocation failed */
284 CA_REQUEST_TIMEOUT, /**< Request is Timeout */
285 CA_DESTINATION_DISCONNECTED, /**< Destination is disconnected */
286 CA_NOT_SUPPORTED, /**< Not supported */
287 CA_STATUS_NOT_INITIALIZED, /**< Not Initialized*/
288 CA_DTLS_AUTHENTICATION_FAILURE, /**< Decryption error in DTLS */
289 CA_STATUS_FAILED =255 /**< Failure */
290 /* Result code - END HERE */
294 * @enum CAResponseResult_t
295 * Enums for CA Response values
299 /* Response status code - START HERE */
300 CA_EMPTY = 0, /**< Empty */
301 CA_CREATED = 201, /**< Created */
302 CA_DELETED = 202, /**< Deleted */
303 CA_VALID = 203, /**< Valid */
304 CA_CHANGED = 204, /**< Changed */
305 CA_CONTENT = 205, /**< Content */
306 CA_CONTINUE = 231, /**< Continue */
307 CA_BAD_REQ = 400, /**< Bad Request */
308 CA_UNAUTHORIZED_REQ = 401, /**< Unauthorized Request */
309 CA_BAD_OPT = 402, /**< Bad Option */
310 CA_FORBIDDEN_REQ = 403, /**< Forbidden Request */
311 CA_NOT_FOUND = 404, /**< Not found */
312 CA_NOT_ACCEPTABLE = 406, /**< Not Acceptable */
313 CA_REQUEST_ENTITY_INCOMPLETE = 408, /**< Request Entity Incomplete */
314 CA_REQUEST_ENTITY_TOO_LARGE = 413, /**< Request Entity Too Large */
315 CA_INTERNAL_SERVER_ERROR = 500, /**< Internal Server Error */
316 CA_RETRANSMIT_TIMEOUT = 504 /**< Retransmit timeout */
317 /* Response status code - END HERE */
318 } CAResponseResult_t;
321 * @enum CATransportProtocolID_t
322 * Transport Protocol IDs for additional options
326 CA_INVALID_ID = (1 << 0), /**< Invalid ID */
327 CA_COAP_ID = (1 << 1) /**< COAP ID */
328 } CATransportProtocolID_t;
331 * @enum CAAdapterState_t
332 * Adapter State to indicate the network changed notifications.
336 CA_ADAPTER_DISABLED, /**< Adapter is Disabled */
337 CA_ADAPTER_ENABLED /**< Adapter is Enabled */
341 * Format indicating which encoding has been used on the payload.
345 CA_FORMAT_UNDEFINED = 0, /**< Undefined enoding format */
346 CA_FORMAT_TEXT_PLAIN,
347 CA_FORMAT_APPLICATION_LINK_FORMAT,
348 CA_FORMAT_APPLICATION_XML,
349 CA_FORMAT_APPLICATION_OCTET_STREAM,
350 CA_FORMAT_APPLICATION_RDF_XML,
351 CA_FORMAT_APPLICATION_EXI,
352 CA_FORMAT_APPLICATION_JSON,
353 CA_FORMAT_APPLICATION_CBOR,
354 CA_FORMAT_UNSUPPORTED
358 * Header options structure to be filled
360 * This structure is used to hold header information.
364 CATransportProtocolID_t protocolID; /**< Protocol ID of the Option */
365 uint16_t optionID; /**< The header option ID which will be
366 added to communication packets */
367 uint16_t optionLength; /**< Option Length **/
368 char optionData[CA_MAX_HEADER_OPTION_DATA_LENGTH]; /**< Optional data values**/
372 * Base Information received
374 * This structure is used to hold request & response base information
379 CAMessageType_t type; /**< Qos for the request */
380 #ifdef ROUTING_GATEWAY
381 bool skipRetransmission; /**< Will not attempt retransmission even if type is CONFIRM.
382 Required for packet forwarding */
384 uint16_t messageId; /**< Message id.
385 * if message id is zero, it will generated by CA inside.
386 * otherwise, you can use it */
387 CAToken_t token; /**< Token for CA */
388 uint8_t tokenLength; /**< token length*/
389 CAHeaderOption_t *options; /** Header Options for the request */
390 uint8_t numOptions; /**< Number of Header options */
391 CAPayload_t payload; /**< payload of the request */
392 size_t payloadSize; /**< size in bytes of the payload */
393 CAPayloadFormat_t payloadFormat; /**< encoding format of the request payload */
394 CAPayloadFormat_t acceptFormat; /**< accept format for the response payload */
395 CAURI_t resourceUri; /**< Resource URI information **/
396 CARemoteId_t identity; /**< endpoint identity */
400 * Request Information to be sent
402 * This structure is used to hold request information
406 CAMethod_t method; /**< Name of the Method Allowed */
407 CAInfo_t info; /**< Information of the request. */
408 bool isMulticast; /**< is multicast request */
412 * Response information received
414 * This structure is used to hold response information
418 CAResponseResult_t result; /**< Result for response by resource model */
419 CAInfo_t info; /**< Information of the response */
424 * Error information from CA
425 * contains error code and message information
427 * This structure holds error information
431 CAResult_t result; /**< CA API request result */
432 CAInfo_t info; /**< message information such as token and payload data
433 helpful to identify the error */
437 * Hold global variables for CA layer (also used by RI layer)
441 int fd; /**< socket fd */
442 uint16_t port; /**< socket port */
445 #define HISTORYSIZE (4)
449 CATransportFlags_t flags;
451 char token[CA_MAX_TOKEN_LEN];
458 CAHistoryItem_t items[HISTORYSIZE];
462 * Hold interface index for keeping track of comings and goings
466 int32_t ifIndex; /**< network interface index */
471 CATransportFlags_t clientFlags; /**< flag for client */
472 CATransportFlags_t serverFlags; /**< flag for server */
473 bool client; /**< client mode */
474 bool server; /**< server mode */
478 void *threadpool; /**< threadpool between Initialize and Start */
479 CASocket_t u6; /**< unicast IPv6 */
480 CASocket_t u6s; /**< unicast IPv6 secure */
481 CASocket_t u4; /**< unicast IPv4 */
482 CASocket_t u4s; /**< unicast IPv4 secure */
483 CASocket_t m6; /**< multicast IPv6 */
484 CASocket_t m6s; /**< multicast IPv6 secure */
485 CASocket_t m4; /**< multicast IPv4 */
486 CASocket_t m4s; /**< multicast IPv4 secure */
487 int netlinkFd; /**< netlink */
488 int shutdownFds[2]; /**< shutdown pipe */
489 int selectTimeout; /**< in seconds */
490 int maxfd; /**< highest fd (for select) */
491 bool started; /**< the IP adapter has started */
492 bool terminate; /**< the IP adapter needs to stop */
493 bool ipv6enabled; /**< IPv6 enabled by OCInit flags */
494 bool ipv4enabled; /**< IPv4 enabled by OCInit flags */
495 bool dualstack; /**< IPv6 and IPv4 enabled */
497 struct networkmonitors
499 CAIfItem_t *ifItems; /**< current network interface index list */
500 size_t sizeIfItems; /**< size of network interface index array */
501 size_t numIfItems; /**< number of valid network interfaces */
507 CAHistory_t requestHistory; /**< filter IP family in requests */
512 * Hold global variables for TCP Adapter.
516 void *threadpool; /**< threadpool between Initialize and Start */
517 CASocket_t ipv4; /**< IPv4 accept socket */
518 CASocket_t ipv6; /**< IPv6 accept socket */
519 void *svrlist; /**< unicast IPv4 TCP server information*/
520 int selectTimeout; /**< in seconds */
521 int listenBacklog; /**< backlog counts*/
522 int shutdownFds[2]; /**< shutdown pipe */
523 int connectionFds[2]; /**< connection pipe */
524 int maxfd; /**< highest fd (for select) */
525 bool started; /**< the TCP adapter has started */
526 bool terminate; /**< the TCP adapter needs to stop */
527 bool ipv4tcpenabled; /**< IPv4 TCP enabled by OCInit flags */
528 bool ipv6tcpenabled; /**< IPv6 TCP enabled by OCInit flags */
533 extern CAGlobals_t caglobals;
536 * Callback function type for request delivery.
537 * @param[out] object Endpoint object from which the request is received.
538 * It contains endpoint address based on the connectivity type.
539 * @param[out] requestInfo Info for resource model to understand about the request.
541 typedef void (*CARequestCallback)(const CAEndpoint_t *object,
542 const CARequestInfo_t *requestInfo);
545 * Callback function type for response delivery.
546 * @param[out] object Endpoint object from which the response is received.
547 * @param[out] responseInfo Identifier which needs to be mapped with response.
549 typedef void (*CAResponseCallback)(const CAEndpoint_t *object,
550 const CAResponseInfo_t *responseInfo);
552 * Callback function type for error.
553 * @param[out] object remote device information.
554 * @param[out] errorInfo CA Error information.
556 typedef void (*CAErrorCallback)(const CAEndpoint_t *object,
557 const CAErrorInfo_t *errorInfo);
560 * Callback function type for network status changes delivery from CA common logic.
561 * @param[out] info Endpoint object from which the network status is changed.
562 * It contains endpoint address based on the connectivity type.
563 * @param[out] status Current network status info.
565 typedef void (*CANetworkMonitorCallback)(const CAEndpoint_t *info, CANetworkStatus_t status);
571 #endif // CA_COMMON_H_