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
29 #include "iotivity_config.h"
33 #define HAVE_SYS_POLL_H
41 #ifdef HAVE_SYS_POLL_H
45 #ifdef HAVE_WINSOCK2_H
58 #define CA_IPADDR_SIZE 16
61 * Remote Access jabber ID length.
63 #define CA_RAJABBERID_SIZE 256
66 * Mac address length for BT port.
68 #define CA_MACADDR_SIZE 18
71 * Max header options data length.
74 #define CA_MAX_HEADER_OPTION_DATA_LENGTH 20
76 #define CA_MAX_HEADER_OPTION_DATA_LENGTH 1024
82 #define CA_MAX_TOKEN_LEN (8)
88 #define CA_MAX_URI_LENGTH 128 /* maximum size of URI for embedded platforms*/
90 #define CA_MAX_URI_LENGTH 512 /* maximum size of URI for other platforms*/
94 * Max PDU length supported.
97 #define COAP_MAX_PDU_SIZE 320 /* maximum size of a CoAP PDU for embedded platforms*/
99 #define COAP_MAX_PDU_SIZE 1400 /* maximum size of a CoAP PDU for big platforms*/
103 #define CA_DEFAULT_BLOCK_SIZE CA_BLOCK_SIZE_1024_BYTE
107 *Maximum length of the remoteEndpoint identity.
109 #define CA_MAX_ENDPOINT_IDENTITY_LEN (32)
112 * option types - the highest option number 63.
114 #define CA_OPTION_IF_MATCH 1
115 #define CA_OPTION_ETAG 4
116 #define CA_OPTION_IF_NONE_MATCH 5
117 #define CA_OPTION_OBSERVE 6
118 #define CA_OPTION_LOCATION_PATH 8
119 #define CA_OPTION_URI_PATH 11
120 #define CA_OPTION_CONTENT_FORMAT 12
121 #define CA_OPTION_CONTENT_TYPE COAP_OPTION_CONTENT_FORMAT
122 #define CA_OPTION_MAXAGE 14
123 #define CA_OPTION_URI_QUERY 15
124 #define CA_OPTION_ACCEPT 17
125 #define CA_OPTION_LOCATION_QUERY 20
128 * Payload information from resource model.
130 typedef uint8_t *CAPayload_t;
133 * URI for the OIC base.CA considers relative URI as the URI.
135 typedef char *CAURI_t;
138 * Token information for mapping the request and responses by resource model.
140 typedef char *CAToken_t;
143 * Socket types and error definitions.
145 #ifdef HAVE_WINSOCK2_H
146 # define OC_SOCKET_ERROR SOCKET_ERROR
147 # define OC_INVALID_SOCKET INVALID_SOCKET
148 typedef SOCKET CASocketFd_t;
149 #else // HAVE_WINSOCK2_H
150 # define OC_SOCKET_ERROR (-1)
151 # define OC_INVALID_SOCKET (-1)
152 typedef int CASocketFd_t;
156 * The following flags are the same as the equivalent OIC values in
157 * octypes.h, allowing direct copying with slight fixup.
158 * The CA layer should used the OC types when build allows that.
161 #define MAX_ADDR_STR_SIZE_CA (256)
164 * Max Address could be "coaps+tcp://[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:yyy.yyy.yyy.yyy]:xxxxx"
165 * Which is 65, +1 for null terminator => 66
166 * OCDevAddr (defined in OCTypes.h) must be the same
167 * as CAEndpoint_t (defined here)
169 #define MAX_ADDR_STR_SIZE_CA (66)
174 CA_DEFAULT_ADAPTER = 0,
176 // value zero indicates discovery
177 CA_ADAPTER_IP = (1 << 0), // IPv4 and IPv6, including 6LoWPAN
178 CA_ADAPTER_GATT_BTLE = (1 << 1), // GATT over Bluetooth LE
179 CA_ADAPTER_RFCOMM_BTEDR = (1 << 2), // RFCOMM over Bluetooth EDR
182 CA_ADAPTER_REMOTE_ACCESS = (1 << 3), // Remote Access over XMPP.
185 CA_ADAPTER_TCP = (1 << 4), // CoAP over TCP
186 CA_ADAPTER_NFC = (1 << 5), // NFC Adapter
188 CA_ALL_ADAPTERS = 0xffffffff
189 } CATransportAdapter_t;
193 CA_DEFAULT_FLAGS = 0,
195 // Insecure transport is the default (subject to change)
196 CA_SECURE = (1 << 4), // secure the transport path
197 // IPv4 & IPv6 autoselection is the default
198 CA_IPV6 = (1 << 5), // IP adapter only
199 CA_IPV4 = (1 << 6), // IP adapter only
200 // Indication that a message was received by multicast.
201 CA_MULTICAST = (1 << 7),
202 // Link-Local multicast is the default multicast scope for IPv6.
203 // These correspond in both value and position to the IPv6 address bits.
204 CA_SCOPE_INTERFACE = 0x1, // IPv6 Interface-Local scope
205 CA_SCOPE_LINK = 0x2, // IPv6 Link-Local scope (default)
206 CA_SCOPE_REALM = 0x3, // IPv6 Realm-Local scope
207 CA_SCOPE_ADMIN = 0x4, // IPv6 Admin-Local scope
208 CA_SCOPE_SITE = 0x5, // IPv6 Site-Local scope
209 CA_SCOPE_ORG = 0x8, // IPv6 Organization-Local scope
210 CA_SCOPE_GLOBAL = 0xE, // IPv6 Global scope
211 } CATransportFlags_t;
213 #define CA_IPFAMILY_MASK (CA_IPV6|CA_IPV4)
214 #define CA_SCOPE_MASK 0xf // mask scope bits above
217 * Information about the network status.
221 CA_INTERFACE_DOWN, /**< Connection is not available */
222 CA_INTERFACE_UP /**< Connection is Available */
226 * remoteEndpoint identity.
231 unsigned char id[CA_MAX_ENDPOINT_IDENTITY_LEN];
235 * Message Type for Base source code.
239 CA_MSG_CONFIRM = 0, /**< confirmable message (requires ACK/RST) */
240 CA_MSG_NONCONFIRM, /**< non-confirmable message (one-shot message) */
241 CA_MSG_ACKNOWLEDGE, /**< used to acknowledge confirmable messages */
242 CA_MSG_RESET /**< used to indicates not-interested or error (lack of context)in
247 * Allowed method to be used by resource model.
251 CA_GET = 1, /**< GET Method */
252 CA_POST, /**< POST Method */
253 CA_PUT, /**< PUT Method */
254 CA_DELETE /**< DELETE Method */
259 * it depends on defined size in libCoAP.
263 CA_BLOCK_SIZE_16_BYTE = 0, /**< 16byte */
264 CA_BLOCK_SIZE_32_BYTE = 1, /**< 32byte */
265 CA_BLOCK_SIZE_64_BYTE = 2, /**< 64byte */
266 CA_BLOCK_SIZE_128_BYTE = 3, /**< 128byte */
267 CA_BLOCK_SIZE_256_BYTE = 4, /**< 256byte */
268 CA_BLOCK_SIZE_512_BYTE = 5, /**< 512byte */
269 CA_BLOCK_SIZE_1024_BYTE = 6 /**< 1Kbyte */
273 * Endpoint information for connectivities.
274 * Must be identical to OCDevAddr.
278 CATransportAdapter_t adapter; // adapter type
279 CATransportFlags_t flags; // transport modifiers
280 uint16_t port; // for IP
281 char addr[MAX_ADDR_STR_SIZE_CA]; // address for all
282 uint32_t ifindex; // usually zero for default interface
283 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
284 char routeData[MAX_ADDR_STR_SIZE_CA]; /**< GatewayId:ClientId of
290 * Endpoint information for secure messages.
294 CAEndpoint_t endpoint; /**< endpoint */
295 // TODO change name to deviceId
296 CARemoteId_t identity; /**< endpoint device uuid */
297 CARemoteId_t userId; /**< endpoint user uuid */
298 } CASecureEndpoint_t;
301 * Enums for CA return values.
305 /* Result code - START HERE */
306 CA_STATUS_OK = 0, /**< Success */
307 CA_STATUS_INVALID_PARAM, /**< Invalid Parameter */
308 CA_ADAPTER_NOT_ENABLED, /**< Adapter is not enabled */
309 CA_SERVER_STARTED_ALREADY, /**< Server is started already */
310 CA_SERVER_NOT_STARTED, /**< Server is not started */
311 CA_DESTINATION_NOT_REACHABLE, /**< Destination is not reachable */
312 CA_SOCKET_OPERATION_FAILED, /**< Socket operation failed */
313 CA_SEND_FAILED, /**< Send request failed */
314 CA_RECEIVE_FAILED, /**< Receive failed */
315 CA_MEMORY_ALLOC_FAILED, /**< Memory allocation failed */
316 CA_REQUEST_TIMEOUT, /**< Request is Timeout */
317 CA_DESTINATION_DISCONNECTED, /**< Destination is disconnected */
318 CA_NOT_SUPPORTED, /**< Not supported */
319 CA_STATUS_NOT_INITIALIZED, /**< Not Initialized*/
320 CA_DTLS_AUTHENTICATION_FAILURE, /**< Decryption error in DTLS */
321 CA_STATUS_FAILED =255 /**< Failure */
322 /* Result code - END HERE */
326 * Enums for CA Response values.
330 /* Response status code - START HERE */
331 CA_EMPTY = 0, /**< Empty */
332 CA_CREATED = 201, /**< Created */
333 CA_DELETED = 202, /**< Deleted */
334 CA_VALID = 203, /**< Valid */
335 CA_CHANGED = 204, /**< Changed */
336 CA_CONTENT = 205, /**< Content */
337 CA_CONTINUE = 231, /**< Continue */
338 CA_BAD_REQ = 400, /**< Bad Request */
339 CA_UNAUTHORIZED_REQ = 401, /**< Unauthorized Request */
340 CA_BAD_OPT = 402, /**< Bad Option */
341 CA_FORBIDDEN_REQ = 403, /**< Forbidden Request */
342 CA_NOT_FOUND = 404, /**< Not found */
343 CA_METHOD_NOT_ALLOWED = 405, /**< Method Not Allowed */
344 CA_NOT_ACCEPTABLE = 406, /**< Not Acceptable */
345 CA_REQUEST_ENTITY_INCOMPLETE = 408, /**< Request Entity Incomplete */
346 CA_REQUEST_ENTITY_TOO_LARGE = 413, /**< Request Entity Too Large */
347 CA_INTERNAL_SERVER_ERROR = 500, /**< Internal Server Error */
348 CA_BAD_GATEWAY = 502,
349 CA_RETRANSMIT_TIMEOUT = 504, /**< Retransmit timeout */
350 CA_PROXY_NOT_SUPPORTED = 505 /**< Proxy not enabled to service a request */
351 /* Response status code - END HERE */
352 } CAResponseResult_t;
355 * Data type whether the data is Request Message or Response Message.
356 * if there is some failure before send data on network.
357 * Type will be set as error type for error callback.
368 * Transport Protocol IDs for additional options.
372 CA_INVALID_ID = (1 << 0), /**< Invalid ID */
373 CA_COAP_ID = (1 << 1) /**< COAP ID */
374 } CATransportProtocolID_t;
377 * Adapter State to indicate the network changed notifications.
381 CA_ADAPTER_DISABLED, /**< Adapter is Disabled */
382 CA_ADAPTER_ENABLED /**< Adapter is Enabled */
386 * Format indicating which encoding has been used on the payload.
390 CA_FORMAT_UNDEFINED = 0, /**< Undefined enoding format */
391 CA_FORMAT_TEXT_PLAIN,
392 CA_FORMAT_APPLICATION_LINK_FORMAT,
393 CA_FORMAT_APPLICATION_XML,
394 CA_FORMAT_APPLICATION_OCTET_STREAM,
395 CA_FORMAT_APPLICATION_RDF_XML,
396 CA_FORMAT_APPLICATION_EXI,
397 CA_FORMAT_APPLICATION_JSON,
398 CA_FORMAT_APPLICATION_CBOR,
399 CA_FORMAT_UNSUPPORTED
403 * Header options structure to be filled.
405 * This structure is used to hold header information.
409 CATransportProtocolID_t protocolID; /**< Protocol ID of the Option */
410 uint16_t optionID; /**< The header option ID which will be
411 added to communication packets */
412 uint16_t optionLength; /**< Option Length **/
413 char optionData[CA_MAX_HEADER_OPTION_DATA_LENGTH]; /**< Optional data values**/
417 * Base Information received.
419 * This structure is used to hold request & response base information.
423 CAMessageType_t type; /**< Qos for the request */
424 #ifdef ROUTING_GATEWAY
425 bool skipRetransmission; /**< Will not attempt retransmission even if type is CONFIRM.
426 Required for packet forwarding */
428 uint16_t messageId; /**< Message id.
429 * if message id is zero, it will generated by CA inside.
430 * otherwise, you can use it */
431 CAToken_t token; /**< Token for CA */
432 uint8_t tokenLength; /**< token length */
433 CAHeaderOption_t *options; /** Header Options for the request */
434 uint8_t numOptions; /**< Number of Header options */
435 CAPayload_t payload; /**< payload of the request */
436 size_t payloadSize; /**< size in bytes of the payload */
437 CAPayloadFormat_t payloadFormat; /**< encoding format of the request payload */
438 CAPayloadFormat_t acceptFormat; /**< accept format for the response payload */
439 CAURI_t resourceUri; /**< Resource URI information **/
440 CARemoteId_t identity; /**< endpoint identity */
441 CADataType_t dataType; /**< data type */
445 * Request Information to be sent.
447 * This structure is used to hold request information.
451 CAMethod_t method; /**< Name of the Method Allowed */
452 CAInfo_t info; /**< Information of the request. */
453 bool isMulticast; /**< is multicast request */
457 * Response information received.
459 * This structure is used to hold response information.
463 CAResponseResult_t result; /**< Result for response by resource model */
464 CAInfo_t info; /**< Information of the response */
469 * Error information from CA
470 * contains error code and message information.
472 * This structure holds error information.
476 CAResult_t result; /**< CA API request result */
477 CAInfo_t info; /**< message information such as token and payload data
478 helpful to identify the error */
482 * Hold global variables for CA layer. (also used by RI layer)
486 CASocketFd_t fd; /**< socket fd */
487 uint16_t port; /**< socket port */
490 #define HISTORYSIZE (4)
494 CATransportFlags_t flags;
496 char token[CA_MAX_TOKEN_LEN];
503 CAHistoryItem_t items[HISTORYSIZE];
507 * Hold interface index for keeping track of comings and goings.
511 int32_t ifIndex; /**< network interface index */
515 * Hold the port number assigned from application.
516 * It will be used when creating a socket.
522 uint16_t u6; /**< unicast IPv6 socket port */
523 uint16_t u6s; /**< unicast IPv6 socket secure port */
524 uint16_t u4; /**< unicast IPv4 socket port */
525 uint16_t u4s; /**< unicast IPv4 socket secure port */
530 uint16_t u4; /**< unicast IPv4 socket port */
531 uint16_t u4s; /**< unicast IPv6 socket secure port */
532 uint16_t u6; /**< unicast IPv6 socket port */
533 uint16_t u6s; /**< unicast IPv6 socket secure port */
540 CATransportFlags_t clientFlags; /**< flag for client */
541 CATransportFlags_t serverFlags; /**< flag for server */
542 bool client; /**< client mode */
543 bool server; /**< server mode */
549 void *threadpool; /**< threadpool between Initialize and Start */
550 CASocket_t u6; /**< unicast IPv6 */
551 CASocket_t u6s; /**< unicast IPv6 secure */
552 CASocket_t u4; /**< unicast IPv4 */
553 CASocket_t u4s; /**< unicast IPv4 secure */
554 CASocket_t m6; /**< multicast IPv6 */
555 CASocket_t m6s; /**< multicast IPv6 secure */
556 CASocket_t m4; /**< multicast IPv4 */
557 CASocket_t m4s; /**< multicast IPv4 secure */
558 CASocketFd_t netlinkFd; /**< netlink */
560 WSAEVENT shutdownEvent; /**< Event used to signal threads to stop */
562 int shutdownFds[2]; /**< fds used to signal threads to stop */
564 int selectTimeout; /**< in seconds */
565 int maxfd; /**< highest fd (for select) */
566 bool started; /**< the IP adapter has started */
567 bool terminate; /**< the IP adapter needs to stop */
568 bool ipv6enabled; /**< IPv6 enabled by OCInit flags */
569 bool ipv4enabled; /**< IPv4 enabled by OCInit flags */
570 bool dualstack; /**< IPv6 and IPv4 enabled */
572 LPFN_WSARECVMSG wsaRecvMsg; /**< Win32 function pointer to WSARecvMsg() */
575 struct networkmonitors
577 CAIfItem_t *ifItems; /**< current network interface index list */
578 size_t sizeIfItems; /**< size of network interface index array */
579 size_t numIfItems; /**< number of valid network interfaces */
585 CAHistory_t requestHistory; /**< filter IP family in requests */
590 * Hold global variables for TCP Adapter.
594 void *threadpool; /**< threadpool between Initialize and Start */
595 CASocket_t ipv4; /**< IPv4 accept socket */
596 CASocket_t ipv4s; /**< IPv4 accept socket secure */
597 CASocket_t ipv6; /**< IPv6 accept socket */
598 CASocket_t ipv6s; /**< IPv6 accept socket secure */
599 void *svrlist; /**< unicast IPv4 TCP server information*/
600 int selectTimeout; /**< in seconds */
601 int listenBacklog; /**< backlog counts*/
602 int shutdownFds[2]; /**< shutdown pipe */
603 int connectionFds[2]; /**< connection pipe */
604 int maxfd; /**< highest fd (for select) */
605 bool started; /**< the TCP adapter has started */
606 bool terminate; /**< the TCP adapter needs to stop */
607 bool ipv4tcpenabled; /**< IPv4 TCP enabled by OCInit flags */
608 bool ipv6tcpenabled; /**< IPv6 TCP enabled by OCInit flags */
613 extern CAGlobals_t caglobals;
616 * Callback function type for request delivery.
617 * @param[out] object Endpoint object from which the request is received.
618 * It contains endpoint address based on the connectivity type.
619 * @param[out] requestInfo Info for resource model to understand about the request.
621 typedef void (*CARequestCallback)(const CAEndpoint_t *object,
622 const CARequestInfo_t *requestInfo);
625 * Callback function type for response delivery.
626 * @param[out] object Endpoint object from which the response is received.
627 * @param[out] responseInfo Identifier which needs to be mapped with response.
629 typedef void (*CAResponseCallback)(const CAEndpoint_t *object,
630 const CAResponseInfo_t *responseInfo);
632 * Callback function type for error.
633 * @param[out] object remote device information.
634 * @param[out] errorInfo CA Error information.
636 typedef void (*CAErrorCallback)(const CAEndpoint_t *object,
637 const CAErrorInfo_t *errorInfo);
640 * Callback function type for network status changes delivery from CA common logic.
641 * @param[out] info Endpoint object from which the network status is changed.
642 * It contains endpoint address based on the connectivity type.
643 * @param[out] status Current network status info.
645 typedef void (*CANetworkMonitorCallback)(const CAEndpoint_t *info, CANetworkStatus_t status);
651 #endif // CA_COMMON_H_