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
31 #define HAVE_SYS_POLL_H
39 #ifdef HAVE_SYS_POLL_H
43 #ifdef HAVE_WINSOCK2_H
56 #define CA_IPADDR_SIZE 16
59 * Remote Access jabber ID length.
61 #define CA_RAJABBERID_SIZE 256
64 * Mac address length for BT port
66 #define CA_MACADDR_SIZE 18
69 * Max header options data length
71 #define CA_MAX_HEADER_OPTION_DATA_LENGTH 20
76 #define CA_MAX_TOKEN_LEN (8)
82 #define CA_MAX_URI_LENGTH 128 /* maximum size of URI for embedded platforms*/
84 #define CA_MAX_URI_LENGTH 512 /* maximum size of URI for other platforms*/
88 * Max PDU length supported
91 #define COAP_MAX_PDU_SIZE 320 /* maximum size of a CoAP PDU for embedded platforms*/
93 #define COAP_MAX_PDU_SIZE 1400 /* maximum size of a CoAP PDU for big platforms*/
97 #define CA_DEFAULT_BLOCK_SIZE CA_BLOCK_SIZE_1024_BYTE
101 *Maximum length of the remoteEndpoint identity
103 #define CA_MAX_ENDPOINT_IDENTITY_LEN (32)
106 * option types - the highest option number 63
108 #define CA_OPTION_IF_MATCH 1
109 #define CA_OPTION_ETAG 4
110 #define CA_OPTION_IF_NONE_MATCH 5
111 #define CA_OPTION_OBSERVE 6
112 #define CA_OPTION_LOCATION_PATH 8
113 #define CA_OPTION_URI_PATH 11
114 #define CA_OPTION_CONTENT_FORMAT 12
115 #define CA_OPTION_CONTENT_TYPE COAP_OPTION_CONTENT_FORMAT
116 #define CA_OPTION_MAXAGE 14
117 #define CA_OPTION_URI_QUERY 15
118 #define CA_OPTION_ACCEPT 17
119 #define CA_OPTION_LOCATION_QUERY 20
122 * Payload information from resource model
124 typedef uint8_t *CAPayload_t;
127 * URI for the OIC base.CA considers relative URI as the URI.
129 typedef char *CAURI_t;
132 * Token information for mapping the request and responses by resource model
134 typedef char *CAToken_t;
137 * Socket types and error definitions
139 #ifdef HAVE_WINSOCK2_H
140 # define OC_SOCKET_ERROR SOCKET_ERROR
141 # define OC_INVALID_SOCKET INVALID_SOCKET
142 typedef HANDLE CASocketFd_t;
143 #else // HAVE_WINSOCK2_H
144 # define OC_SOCKET_ERROR (-1)
145 # define OC_INVALID_SOCKET (-1)
146 typedef int CASocketFd_t;
149 /** The following flags are the same as the equivalent OIC values in
150 * octypes.h, allowing direct copying with slight fixup.
151 * The CA layer should used the OC types when build allows that.
154 #define MAX_ADDR_STR_SIZE_CA (256)
156 /** Max Address could be "coap+tcp://[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:yyy.yyy.yyy.yyy]:xxxxx"
157 * Which is 64, +1 for null terminator => 65
158 * OCDevAddr (defined in OCTypes.h) must be the same
159 * as CAEndpoint_t (defined here)
161 #define MAX_ADDR_STR_SIZE_CA (65)
166 CA_DEFAULT_ADAPTER = 0,
168 // value zero indicates discovery
169 CA_ADAPTER_IP = (1 << 0), // IPv4 and IPv6, including 6LoWPAN
170 CA_ADAPTER_GATT_BTLE = (1 << 1), // GATT over Bluetooth LE
171 CA_ADAPTER_RFCOMM_BTEDR = (1 << 2), // RFCOMM over Bluetooth EDR
174 CA_ADAPTER_REMOTE_ACCESS = (1 << 3), // Remote Access over XMPP.
177 CA_ADAPTER_TCP = (1 << 4), // CoAP over TCP
178 CA_ADAPTER_NFC = (1 << 5), // NFC Adapter
180 CA_ALL_ADAPTERS = 0xffffffff
181 } CATransportAdapter_t;
185 CA_DEFAULT_FLAGS = 0,
187 // Insecure transport is the default (subject to change)
188 CA_SECURE = (1 << 4), // secure the transport path
189 // IPv4 & IPv6 autoselection is the default
190 CA_IPV6 = (1 << 5), // IP adapter only
191 CA_IPV4 = (1 << 6), // IP adapter only
192 // Indication that a message was received by multicast.
193 CA_MULTICAST = (1 << 7),
194 // Link-Local multicast is the default multicast scope for IPv6.
195 // These correspond in both value and position to the IPv6 address bits.
196 CA_SCOPE_INTERFACE = 0x1, // IPv6 Interface-Local scope
197 CA_SCOPE_LINK = 0x2, // IPv6 Link-Local scope (default)
198 CA_SCOPE_REALM = 0x3, // IPv6 Realm-Local scope
199 CA_SCOPE_ADMIN = 0x4, // IPv6 Admin-Local scope
200 CA_SCOPE_SITE = 0x5, // IPv6 Site-Local scope
201 CA_SCOPE_ORG = 0x8, // IPv6 Organization-Local scope
202 CA_SCOPE_GLOBAL = 0xE, // IPv6 Global scope
203 } CATransportFlags_t;
205 #define CA_IPFAMILY_MASK (CA_IPV6|CA_IPV4)
206 #define CA_SCOPE_MASK 0xf // mask scope bits above
209 * @enum CANetworkStatus_t
210 * Information about the network status.
214 CA_INTERFACE_DOWN, /**< Connection is not available */
215 CA_INTERFACE_UP /**< Connection is Available */
219 * remoteEndpoint identity
224 unsigned char id[CA_MAX_ENDPOINT_IDENTITY_LEN];
228 * @enum CAMessageType_t
229 * Message Type for Base source code
233 CA_MSG_CONFIRM = 0, /**< confirmable message (requires ACK/RST) */
234 CA_MSG_NONCONFIRM, /**< non-confirmable message (one-shot message) */
235 CA_MSG_ACKNOWLEDGE, /**< used to acknowledge confirmable messages */
236 CA_MSG_RESET /**< used to indicates not-interested or error (lack of context)in
242 * Allowed method to be used by resource model
246 CA_GET = 1, /**< GET Method */
247 CA_POST, /**< POST Method */
248 CA_PUT, /**< PUT Method */
249 CA_DELETE /**< DELETE Method */
254 * it depends on defined size in libCoAP.
258 CA_BLOCK_SIZE_16_BYTE = 0, /**< 16byte */
259 CA_BLOCK_SIZE_32_BYTE = 1, /**< 32byte */
260 CA_BLOCK_SIZE_64_BYTE = 2, /**< 64byte */
261 CA_BLOCK_SIZE_128_BYTE = 3, /**< 128byte */
262 CA_BLOCK_SIZE_256_BYTE = 4, /**< 256byte */
263 CA_BLOCK_SIZE_512_BYTE = 5, /**< 512byte */
264 CA_BLOCK_SIZE_1024_BYTE = 6 /**< 1Kbyte */
268 * Endpoint information for connectivities
269 * Must be identical to OCDevAddr.
273 CATransportAdapter_t adapter; // adapter type
274 CATransportFlags_t flags; // transport modifiers
275 uint16_t port; // for IP
276 char addr[MAX_ADDR_STR_SIZE_CA]; // address for all
277 uint32_t ifindex; // usually zero for default interface
278 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
279 char routeData[MAX_ADDR_STR_SIZE_CA]; /**< GatewayId:ClientId of
285 * Endpoint information for secure messages
289 CAEndpoint_t endpoint; /**< endpoint */
290 CARemoteId_t identity; /**< endpoint identity */
291 } CASecureEndpoint_t;
295 * Enums for CA return values
299 /* Result code - START HERE */
300 CA_STATUS_OK = 0, /**< Success */
301 CA_STATUS_INVALID_PARAM, /**< Invalid Parameter */
302 CA_ADAPTER_NOT_ENABLED, /**< Adapter is not enabled */
303 CA_SERVER_STARTED_ALREADY, /**< Server is started already */
304 CA_SERVER_NOT_STARTED, /**< Server is not started */
305 CA_DESTINATION_NOT_REACHABLE, /**< Destination is not reachable */
306 CA_SOCKET_OPERATION_FAILED, /**< Socket operation failed */
307 CA_SEND_FAILED, /**< Send request failed */
308 CA_RECEIVE_FAILED, /**< Receive failed */
309 CA_MEMORY_ALLOC_FAILED, /**< Memory allocation failed */
310 CA_REQUEST_TIMEOUT, /**< Request is Timeout */
311 CA_DESTINATION_DISCONNECTED, /**< Destination is disconnected */
312 CA_NOT_SUPPORTED, /**< Not supported */
313 CA_STATUS_NOT_INITIALIZED, /**< Not Initialized*/
314 CA_DTLS_AUTHENTICATION_FAILURE, /**< Decryption error in DTLS */
315 CA_STATUS_FAILED =255 /**< Failure */
316 /* Result code - END HERE */
320 * @enum CAResponseResult_t
321 * Enums for CA Response values
325 /* Response status code - START HERE */
326 CA_EMPTY = 0, /**< Empty */
327 CA_CREATED = 201, /**< Created */
328 CA_DELETED = 202, /**< Deleted */
329 CA_VALID = 203, /**< Valid */
330 CA_CHANGED = 204, /**< Changed */
331 CA_CONTENT = 205, /**< Content */
332 CA_CONTINUE = 231, /**< Continue */
333 CA_BAD_REQ = 400, /**< Bad Request */
334 CA_UNAUTHORIZED_REQ = 401, /**< Unauthorized Request */
335 CA_BAD_OPT = 402, /**< Bad Option */
336 CA_FORBIDDEN_REQ = 403, /**< Forbidden Request */
337 CA_NOT_FOUND = 404, /**< Not found */
338 CA_METHOD_NOT_ALLOWED = 405, /**< Method Not Allowed */
339 CA_NOT_ACCEPTABLE = 406, /**< Not Acceptable */
340 CA_REQUEST_ENTITY_INCOMPLETE = 408, /**< Request Entity Incomplete */
341 CA_REQUEST_ENTITY_TOO_LARGE = 413, /**< Request Entity Too Large */
342 CA_INTERNAL_SERVER_ERROR = 500, /**< Internal Server Error */
343 CA_RETRANSMIT_TIMEOUT = 504 /**< Retransmit timeout */
344 /* Response status code - END HERE */
345 } CAResponseResult_t;
348 * @enum CATransportProtocolID_t
349 * Transport Protocol IDs for additional options
353 CA_INVALID_ID = (1 << 0), /**< Invalid ID */
354 CA_COAP_ID = (1 << 1) /**< COAP ID */
355 } CATransportProtocolID_t;
358 * @enum CAAdapterState_t
359 * Adapter State to indicate the network changed notifications.
363 CA_ADAPTER_DISABLED, /**< Adapter is Disabled */
364 CA_ADAPTER_ENABLED /**< Adapter is Enabled */
368 * Format indicating which encoding has been used on the payload.
372 CA_FORMAT_UNDEFINED = 0, /**< Undefined enoding format */
373 CA_FORMAT_TEXT_PLAIN,
374 CA_FORMAT_APPLICATION_LINK_FORMAT,
375 CA_FORMAT_APPLICATION_XML,
376 CA_FORMAT_APPLICATION_OCTET_STREAM,
377 CA_FORMAT_APPLICATION_RDF_XML,
378 CA_FORMAT_APPLICATION_EXI,
379 CA_FORMAT_APPLICATION_JSON,
380 CA_FORMAT_APPLICATION_CBOR,
381 CA_FORMAT_UNSUPPORTED
385 * Header options structure to be filled
387 * This structure is used to hold header information.
391 CATransportProtocolID_t protocolID; /**< Protocol ID of the Option */
392 uint16_t optionID; /**< The header option ID which will be
393 added to communication packets */
394 uint16_t optionLength; /**< Option Length **/
395 char optionData[CA_MAX_HEADER_OPTION_DATA_LENGTH]; /**< Optional data values**/
399 * Base Information received
401 * This structure is used to hold request & response base information
405 CAMessageType_t type; /**< Qos for the request */
406 #ifdef ROUTING_GATEWAY
407 bool skipRetransmission; /**< Will not attempt retransmission even if type is CONFIRM.
408 Required for packet forwarding */
410 uint16_t messageId; /**< Message id.
411 * if message id is zero, it will generated by CA inside.
412 * otherwise, you can use it */
413 CAToken_t token; /**< Token for CA */
414 uint8_t tokenLength; /**< token length */
415 CAHeaderOption_t *options; /** Header Options for the request */
416 uint8_t numOptions; /**< Number of Header options */
417 CAPayload_t payload; /**< payload of the request */
418 size_t payloadSize; /**< size in bytes of the payload */
419 CAPayloadFormat_t payloadFormat; /**< encoding format of the request payload */
420 CAPayloadFormat_t acceptFormat; /**< accept format for the response payload */
421 CAURI_t resourceUri; /**< Resource URI information **/
422 CARemoteId_t identity; /**< endpoint identity */
426 * Request Information to be sent
428 * This structure is used to hold request information
432 CAMethod_t method; /**< Name of the Method Allowed */
433 CAInfo_t info; /**< Information of the request. */
434 bool isMulticast; /**< is multicast request */
438 * Response information received
440 * This structure is used to hold response information
444 CAResponseResult_t result; /**< Result for response by resource model */
445 CAInfo_t info; /**< Information of the response */
450 * Error information from CA
451 * contains error code and message information
453 * This structure holds error information
457 CAResult_t result; /**< CA API request result */
458 CAInfo_t info; /**< message information such as token and payload data
459 helpful to identify the error */
463 * Hold global variables for CA layer (also used by RI layer)
467 CASocketFd_t fd; /**< socket fd */
468 uint16_t port; /**< socket port */
471 #define HISTORYSIZE (4)
475 CATransportFlags_t flags;
477 char token[CA_MAX_TOKEN_LEN];
484 CAHistoryItem_t items[HISTORYSIZE];
488 * Hold interface index for keeping track of comings and goings
492 int32_t ifIndex; /**< network interface index */
496 * Hold the port number assigned from application.
497 * It will be used when creating a socket.
503 uint16_t u6; /**< unicast IPv6 socket port */
504 uint16_t u6s; /**< unicast IPv6 socket secure port */
505 uint16_t u4; /**< unicast IPv4 socket port */
506 uint16_t u4s; /**< unicast IPv4 socket secure port */
511 uint16_t u4; /**< unicast IPv4 socket port */
512 uint16_t u6; /**< unicast IPv6 socket port */
519 CATransportFlags_t clientFlags; /**< flag for client */
520 CATransportFlags_t serverFlags; /**< flag for server */
521 bool client; /**< client mode */
522 bool server; /**< server mode */
528 void *threadpool; /**< threadpool between Initialize and Start */
529 CASocket_t u6; /**< unicast IPv6 */
530 CASocket_t u6s; /**< unicast IPv6 secure */
531 CASocket_t u4; /**< unicast IPv4 */
532 CASocket_t u4s; /**< unicast IPv4 secure */
533 CASocket_t m6; /**< multicast IPv6 */
534 CASocket_t m6s; /**< multicast IPv6 secure */
535 CASocket_t m4; /**< multicast IPv4 */
536 CASocket_t m4s; /**< multicast IPv4 secure */
537 CASocketFd_t netlinkFd; /**< netlink */
539 WSAEVENT shutdownEvent; /**< Event used to signal threads to stop */
541 int shutdownFds[2]; /**< fds used to signal threads to stop */
543 int selectTimeout; /**< in seconds */
544 int maxfd; /**< highest fd (for select) */
545 bool started; /**< the IP adapter has started */
546 bool terminate; /**< the IP adapter needs to stop */
547 bool ipv6enabled; /**< IPv6 enabled by OCInit flags */
548 bool ipv4enabled; /**< IPv4 enabled by OCInit flags */
549 bool dualstack; /**< IPv6 and IPv4 enabled */
551 LPFN_WSARECVMSG wsaRecvMsg; /**< Win32 function pointer to WSARecvMsg() */
554 struct networkmonitors
556 CAIfItem_t *ifItems; /**< current network interface index list */
557 size_t sizeIfItems; /**< size of network interface index array */
558 size_t numIfItems; /**< number of valid network interfaces */
564 CAHistory_t requestHistory; /**< filter IP family in requests */
569 * Hold global variables for TCP Adapter.
573 void *threadpool; /**< threadpool between Initialize and Start */
574 CASocket_t ipv4; /**< IPv4 accept socket */
575 CASocket_t ipv6; /**< IPv6 accept socket */
576 void *svrlist; /**< unicast IPv4 TCP server information*/
577 int selectTimeout; /**< in seconds */
578 int listenBacklog; /**< backlog counts*/
579 int shutdownFds[2]; /**< shutdown pipe */
580 int connectionFds[2]; /**< connection pipe */
581 int maxfd; /**< highest fd (for select) */
582 bool started; /**< the TCP adapter has started */
583 bool terminate; /**< the TCP adapter needs to stop */
584 bool ipv4tcpenabled; /**< IPv4 TCP enabled by OCInit flags */
585 bool ipv6tcpenabled; /**< IPv6 TCP enabled by OCInit flags */
590 extern CAGlobals_t caglobals;
593 * Callback function type for request delivery.
594 * @param[out] object Endpoint object from which the request is received.
595 * It contains endpoint address based on the connectivity type.
596 * @param[out] requestInfo Info for resource model to understand about the request.
598 typedef void (*CARequestCallback)(const CAEndpoint_t *object,
599 const CARequestInfo_t *requestInfo);
602 * Callback function type for response delivery.
603 * @param[out] object Endpoint object from which the response is received.
604 * @param[out] responseInfo Identifier which needs to be mapped with response.
606 typedef void (*CAResponseCallback)(const CAEndpoint_t *object,
607 const CAResponseInfo_t *responseInfo);
609 * Callback function type for error.
610 * @param[out] object remote device information.
611 * @param[out] errorInfo CA Error information.
613 typedef void (*CAErrorCallback)(const CAEndpoint_t *object,
614 const CAErrorInfo_t *errorInfo);
617 * Callback function type for network status changes delivery from CA common logic.
618 * @param[out] info Endpoint object from which the network status is changed.
619 * It contains endpoint address based on the connectivity type.
620 * @param[out] status Current network status info.
622 typedef void (*CANetworkMonitorCallback)(const CAEndpoint_t *info, CANetworkStatus_t status);
628 #endif // CA_COMMON_H_