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"
35 #ifdef HAVE_SYS_POLL_H
39 #ifdef HAVE_WINSOCK2_H
50 * TAG of Analyzer log.
52 #define ANALYZER_TAG "OIC_ANALYZER"
57 #define CA_IPADDR_SIZE 16
60 * Remote Access jabber ID length.
62 #define CA_RAJABBERID_SIZE 256
65 * Mac address length for BT port.
67 #define CA_MACADDR_SIZE 18
70 * Max header options data length.
73 #define CA_MAX_HEADER_OPTION_DATA_LENGTH 20
75 #define CA_MAX_HEADER_OPTION_DATA_LENGTH 1024
81 #define CA_MAX_TOKEN_LEN (8)
87 #define CA_MAX_URI_LENGTH 128 /* maximum size of URI for embedded platforms*/
89 #define CA_MAX_URI_LENGTH 512 /* maximum size of URI for other platforms*/
93 * Max PDU length supported.
96 #define COAP_MAX_PDU_SIZE 320 /* maximum size of a CoAP PDU for embedded platforms*/
98 #define COAP_MAX_PDU_SIZE 1400 /* maximum size of a CoAP PDU for big platforms*/
102 #define CA_DEFAULT_BLOCK_SIZE CA_BLOCK_SIZE_1024_BYTE
106 *Maximum length of the remoteEndpoint identity.
108 #define CA_MAX_ENDPOINT_IDENTITY_LEN CA_MAX_IDENTITY_SIZE
113 #define CA_MAX_IDENTITY_SIZE (37)
116 * option types - the highest option number 63.
118 #define CA_OPTION_IF_MATCH 1
119 #define CA_OPTION_ETAG 4
120 #define CA_OPTION_IF_NONE_MATCH 5
121 #define CA_OPTION_OBSERVE 6
122 #define CA_OPTION_LOCATION_PATH 8
123 #define CA_OPTION_URI_PATH 11
124 #define CA_OPTION_CONTENT_FORMAT 12
125 #define CA_OPTION_CONTENT_TYPE COAP_OPTION_CONTENT_FORMAT
126 #define CA_OPTION_MAXAGE 14
127 #define CA_OPTION_URI_QUERY 15
128 #define CA_OPTION_ACCEPT 17
129 #define CA_OPTION_LOCATION_QUERY 20
132 * TODO: Move these COAP defines to CoAP lib once approved.
134 #define COAP_OPTION_ACCEPT_VERSION 2049
135 #define COAP_OPTION_CONTENT_VERSION 2053
136 #define COAP_MEDIATYPE_APPLICATION_VND_OCF_CBOR 10000 // application/vnd.ocf+cbor
138 #define CA_OPTION_ACCEPT_VERSION 2049
139 #define CA_OPTION_CONTENT_VERSION 2053
141 #define DEFAULT_ACCEPT_VERSION_VALUE 2048 // OCF version 1.0.0
142 #define DEFAULT_CONTENT_VERSION_VALUE 2048 // OCF version 1.0.0
145 * Payload information from resource model.
147 typedef uint8_t *CAPayload_t;
150 * URI for the OIC base.CA considers relative URI as the URI.
152 typedef char *CAURI_t;
155 * Token information for mapping the request and responses by resource model.
157 typedef char *CAToken_t;
160 * Socket types and error definitions.
162 #ifdef HAVE_WINSOCK2_H
163 # define OC_SOCKET_ERROR SOCKET_ERROR
164 # define OC_INVALID_SOCKET INVALID_SOCKET
165 typedef SOCKET CASocketFd_t;
166 #else // HAVE_WINSOCK2_H
167 # define OC_SOCKET_ERROR (-1)
168 # define OC_INVALID_SOCKET (-1)
169 typedef int CASocketFd_t;
173 * The following flags are the same as the equivalent OIC values in
174 * octypes.h, allowing direct copying with slight fixup.
175 * The CA layer should used the OC types when build allows that.
178 #define MAX_ADDR_STR_SIZE_CA (256)
181 * Max Address could be "coaps+tcp://[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:yyy.yyy.yyy.yyy]:xxxxx"
182 * Which is 65, +1 for null terminator => 66
183 * OCDevAddr (defined in OCTypes.h) must be the same
184 * as CAEndpoint_t (defined here)
186 #define MAX_ADDR_STR_SIZE_CA (66)
191 CA_DEFAULT_ADAPTER = 0,
193 // value zero indicates discovery
194 CA_ADAPTER_IP = (1 << 0), // IPv4 and IPv6, including 6LoWPAN
195 CA_ADAPTER_GATT_BTLE = (1 << 1), // GATT over Bluetooth LE
196 CA_ADAPTER_RFCOMM_BTEDR = (1 << 2), // RFCOMM over Bluetooth EDR
199 CA_ADAPTER_REMOTE_ACCESS = (1 << 3), // Remote Access over XMPP.
202 CA_ADAPTER_TCP = (1 << 4), // CoAP over TCP
203 CA_ADAPTER_NFC = (1 << 5), // NFC Adapter
205 CA_ALL_ADAPTERS = 0xffffffff
206 } CATransportAdapter_t;
210 CA_DEFAULT_FLAGS = 0,
212 // Insecure transport is the default (subject to change)
213 CA_SECURE = (1 << 4), // secure the transport path
214 // IPv4 & IPv6 autoselection is the default
215 CA_IPV6 = (1 << 5), // IP adapter only
216 CA_IPV4 = (1 << 6), // IP adapter only
217 // Indication that a message was received by multicast.
218 CA_MULTICAST = (1 << 7),
219 // Link-Local multicast is the default multicast scope for IPv6.
220 // These correspond in both value and position to the IPv6 address bits.
221 CA_SCOPE_INTERFACE = 0x1, // IPv6 Interface-Local scope
222 CA_SCOPE_LINK = 0x2, // IPv6 Link-Local scope (default)
223 CA_SCOPE_REALM = 0x3, // IPv6 Realm-Local scope
224 CA_SCOPE_ADMIN = 0x4, // IPv6 Admin-Local scope
225 CA_SCOPE_SITE = 0x5, // IPv6 Site-Local scope
226 CA_SCOPE_ORG = 0x8, // IPv6 Organization-Local scope
227 CA_SCOPE_GLOBAL = 0xE, // IPv6 Global scope
228 } CATransportFlags_t;
232 CA_DEFAULT_BT_FLAGS = 0,
233 // flags for BLE transport
234 CA_LE_ADV_DISABLE = 0x1, // disable BLE advertisement.
235 CA_LE_ADV_ENABLE = 0x2, // enable BLE advertisement.
236 CA_LE_SERVER_DISABLE = (1 << 4), // disable gatt server.
237 // flags for EDR transport
238 CA_EDR_SERVER_DISABLE = (1 << 7)
239 } CATransportBTFlags_t;
241 #define CA_IPFAMILY_MASK (CA_IPV6|CA_IPV4)
242 #define CA_SCOPE_MASK 0xf // mask scope bits above
245 * Information about the network status.
249 CA_INTERFACE_DOWN, /**< Connection is not available */
250 CA_INTERFACE_UP /**< Connection is Available */
254 * remoteEndpoint identity.
259 unsigned char id[CA_MAX_ENDPOINT_IDENTITY_LEN];
263 * Message Type for Base source code.
267 CA_MSG_CONFIRM = 0, /**< confirmable message (requires ACK/RST) */
268 CA_MSG_NONCONFIRM, /**< non-confirmable message (one-shot message) */
269 CA_MSG_ACKNOWLEDGE, /**< used to acknowledge confirmable messages */
270 CA_MSG_RESET /**< used to indicates not-interested or error (lack of context)in
275 * Allowed method to be used by resource model.
279 CA_GET = 1, /**< GET Method */
280 CA_POST, /**< POST Method */
281 CA_PUT, /**< PUT Method */
282 CA_DELETE /**< DELETE Method */
287 * it depends on defined size in libCoAP.
291 CA_BLOCK_SIZE_16_BYTE = 0, /**< 16byte */
292 CA_BLOCK_SIZE_32_BYTE = 1, /**< 32byte */
293 CA_BLOCK_SIZE_64_BYTE = 2, /**< 64byte */
294 CA_BLOCK_SIZE_128_BYTE = 3, /**< 128byte */
295 CA_BLOCK_SIZE_256_BYTE = 4, /**< 256byte */
296 CA_BLOCK_SIZE_512_BYTE = 5, /**< 512byte */
297 CA_BLOCK_SIZE_1024_BYTE = 6 /**< 1Kbyte */
301 * Endpoint information for connectivities.
302 * Must be identical to OCDevAddr.
306 CATransportAdapter_t adapter; // adapter type
307 CATransportFlags_t flags; // transport modifiers
308 uint16_t port; // for IP
309 char addr[MAX_ADDR_STR_SIZE_CA]; // address for all
310 uint32_t ifindex; // usually zero for default interface
311 char remoteId[CA_MAX_IDENTITY_SIZE]; // device ID of remote device
312 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
313 char routeData[MAX_ADDR_STR_SIZE_CA]; /**< GatewayId:ClientId of
319 * Endpoint information for secure messages.
323 CAEndpoint_t endpoint; /**< endpoint */
324 // TODO change name to deviceId
325 CARemoteId_t identity; /**< endpoint device uuid */
326 CARemoteId_t userId; /**< endpoint user uuid */
328 } CASecureEndpoint_t;
331 * Endpoint used for security administration - a special type of identity that
332 * bypasses Access Control Entry checks for SVR resources, while the device is
335 #define CA_SECURE_ENDPOINT_ATTRIBUTE_ADMINISTRATOR 0x1
338 * Enums for CA return values.
342 /* Result code - START HERE */
343 CA_STATUS_OK = 0, /**< Success */
344 CA_STATUS_INVALID_PARAM, /**< Invalid Parameter */
345 CA_ADAPTER_NOT_ENABLED, /**< Adapter is not enabled */
346 CA_SERVER_STARTED_ALREADY, /**< Server is started already */
347 CA_SERVER_NOT_STARTED, /**< Server is not started */
348 CA_DESTINATION_NOT_REACHABLE, /**< Destination is not reachable */
349 CA_SOCKET_OPERATION_FAILED, /**< Socket operation failed */
350 CA_SEND_FAILED, /**< Send request failed */
351 CA_RECEIVE_FAILED, /**< Receive failed */
352 CA_MEMORY_ALLOC_FAILED, /**< Memory allocation failed */
353 CA_REQUEST_TIMEOUT, /**< Request is Timeout */
354 CA_DESTINATION_DISCONNECTED, /**< Destination is disconnected */
355 CA_NOT_SUPPORTED, /**< Not supported */
356 CA_STATUS_NOT_INITIALIZED, /**< Not Initialized*/
357 CA_DTLS_AUTHENTICATION_FAILURE, /**< Decryption error in DTLS */
358 CA_STATUS_FAILED =255 /**< Failure */
359 /* Result code - END HERE */
363 * Enums for CA Response values.
367 /* Response status code - START HERE */
368 CA_EMPTY = 0, /**< Empty */
369 CA_CREATED = 201, /**< Created */
370 CA_DELETED = 202, /**< Deleted */
371 CA_VALID = 203, /**< Valid */
372 CA_CHANGED = 204, /**< Changed */
373 CA_CONTENT = 205, /**< Content */
374 CA_CONTINUE = 231, /**< Continue */
375 CA_BAD_REQ = 400, /**< Bad Request */
376 CA_UNAUTHORIZED_REQ = 401, /**< Unauthorized Request */
377 CA_BAD_OPT = 402, /**< Bad Option */
378 CA_FORBIDDEN_REQ = 403, /**< Forbidden Request */
379 CA_NOT_FOUND = 404, /**< Not found */
380 CA_METHOD_NOT_ALLOWED = 405, /**< Method Not Allowed */
381 CA_NOT_ACCEPTABLE = 406, /**< Not Acceptable */
382 CA_REQUEST_ENTITY_INCOMPLETE = 408, /**< Request Entity Incomplete */
383 CA_REQUEST_ENTITY_TOO_LARGE = 413, /**< Request Entity Too Large */
384 CA_INTERNAL_SERVER_ERROR = 500, /**< Internal Server Error */
385 CA_BAD_GATEWAY = 502,
386 CA_RETRANSMIT_TIMEOUT = 504, /**< Retransmit timeout */
387 CA_PROXY_NOT_SUPPORTED = 505 /**< Proxy not enabled to service a request */
388 /* Response status code - END HERE */
389 } CAResponseResult_t;
392 * Data type whether the data is Request Message or Response Message.
393 * if there is some failure before send data on network.
394 * Type will be set as error type for error callback.
405 * Transport Protocol IDs for additional options.
409 CA_INVALID_ID = (1 << 0), /**< Invalid ID */
410 CA_COAP_ID = (1 << 1) /**< COAP ID */
411 } CATransportProtocolID_t;
414 * Adapter State to indicate the network changed notifications.
418 CA_ADAPTER_DISABLED, /**< Adapter is Disabled */
419 CA_ADAPTER_ENABLED /**< Adapter is Enabled */
423 * Format indicating which encoding has been used on the payload.
427 CA_FORMAT_UNDEFINED = 0, /**< Undefined enoding format */
428 CA_FORMAT_TEXT_PLAIN,
429 CA_FORMAT_APPLICATION_LINK_FORMAT,
430 CA_FORMAT_APPLICATION_XML,
431 CA_FORMAT_APPLICATION_OCTET_STREAM,
432 CA_FORMAT_APPLICATION_RDF_XML,
433 CA_FORMAT_APPLICATION_EXI,
434 CA_FORMAT_APPLICATION_JSON,
435 CA_FORMAT_APPLICATION_CBOR,
436 CA_FORMAT_APPLICATION_VND_OCF_CBOR,
437 CA_FORMAT_UNSUPPORTED
441 * Header options structure to be filled.
443 * This structure is used to hold header information.
447 CATransportProtocolID_t protocolID; /**< Protocol ID of the Option */
448 uint16_t optionID; /**< The header option ID which will be
449 added to communication packets */
450 uint16_t optionLength; /**< Option Length **/
451 char optionData[CA_MAX_HEADER_OPTION_DATA_LENGTH]; /**< Optional data values**/
455 * Base Information received.
457 * This structure is used to hold request & response base information.
461 CAMessageType_t type; /**< Qos for the request */
462 #ifdef ROUTING_GATEWAY
463 bool skipRetransmission; /**< Will not attempt retransmission even if type is CONFIRM.
464 Required for packet forwarding */
466 uint16_t messageId; /**< Message id.
467 * if message id is zero, it will generated by CA inside.
468 * otherwise, you can use it */
469 CAToken_t token; /**< Token for CA */
470 uint8_t tokenLength; /**< token length */
471 CAHeaderOption_t *options; /** Header Options for the request */
472 uint8_t numOptions; /**< Number of Header options */
473 CAPayload_t payload; /**< payload of the request */
474 size_t payloadSize; /**< size in bytes of the payload */
475 CAPayloadFormat_t payloadFormat; /**< encoding format of the request payload */
476 CAPayloadFormat_t acceptFormat; /**< accept format for the response payload */
477 uint16_t payloadVersion; /**< version of the payload */
478 uint16_t acceptVersion; /**< expected version for the response payload */
479 CAURI_t resourceUri; /**< Resource URI information **/
480 CARemoteId_t identity; /**< endpoint identity */
481 CADataType_t dataType; /**< data type */
485 * Request Information to be sent.
487 * This structure is used to hold request information.
491 CAMethod_t method; /**< Name of the Method Allowed */
492 CAInfo_t info; /**< Information of the request. */
493 bool isMulticast; /**< is multicast request */
497 * Response information received.
499 * This structure is used to hold response information.
503 CAResponseResult_t result; /**< Result for response by resource model */
504 CAInfo_t info; /**< Information of the response */
509 * Error information from CA
510 * contains error code and message information.
512 * This structure holds error information.
516 CAResult_t result; /**< CA API request result */
517 CAInfo_t info; /**< message information such as token and payload data
518 helpful to identify the error */
522 * Hold global variables for CA layer. (also used by RI layer)
526 CASocketFd_t fd; /**< socket fd */
527 uint16_t port; /**< socket port */
530 #define HISTORYSIZE (4)
534 CATransportFlags_t flags;
536 char token[CA_MAX_TOKEN_LEN];
544 CAHistoryItem_t items[HISTORYSIZE];
548 * Hold interface index for keeping track of comings and goings.
552 int32_t ifIndex; /**< network interface index */
556 * Hold the port number assigned from application.
557 * It will be used when creating a socket.
563 uint16_t u6; /**< unicast IPv6 socket port */
564 uint16_t u6s; /**< unicast IPv6 socket secure port */
565 uint16_t u4; /**< unicast IPv4 socket port */
566 uint16_t u4s; /**< unicast IPv4 socket secure port */
571 uint16_t u4; /**< unicast IPv4 socket port */
572 uint16_t u4s; /**< unicast IPv6 socket secure port */
573 uint16_t u6; /**< unicast IPv6 socket port */
574 uint16_t u6s; /**< unicast IPv6 socket secure port */
581 CATransportFlags_t clientFlags; /**< flag for client */
582 CATransportFlags_t serverFlags; /**< flag for server */
583 bool client; /**< client mode */
584 bool server; /**< server mode */
590 void *threadpool; /**< threadpool between Initialize and Start */
591 CASocket_t u6; /**< unicast IPv6 */
592 CASocket_t u6s; /**< unicast IPv6 secure */
593 CASocket_t u4; /**< unicast IPv4 */
594 CASocket_t u4s; /**< unicast IPv4 secure */
595 CASocket_t m6; /**< multicast IPv6 */
596 CASocket_t m6s; /**< multicast IPv6 secure */
597 CASocket_t m4; /**< multicast IPv4 */
598 CASocket_t m4s; /**< multicast IPv4 secure */
600 WSAEVENT addressChangeEvent;/**< Event used to signal address changes */
601 WSAEVENT shutdownEvent; /**< Event used to signal threads to stop */
603 int netlinkFd; /**< netlink */
604 int shutdownFds[2]; /**< fds used to signal threads to stop */
605 int maxfd; /**< highest fd (for select) */
607 int selectTimeout; /**< in seconds */
608 bool started; /**< the IP adapter has started */
609 bool terminate; /**< the IP adapter needs to stop */
610 bool ipv6enabled; /**< IPv6 enabled by OCInit flags */
611 bool ipv4enabled; /**< IPv4 enabled by OCInit flags */
612 bool dualstack; /**< IPv6 and IPv4 enabled */
614 LPFN_WSARECVMSG wsaRecvMsg; /**< Win32 function pointer to WSARecvMsg() */
617 struct networkmonitors
619 CAIfItem_t *ifItems; /**< current network interface index list */
620 size_t sizeIfItems; /**< size of network interface index array */
621 size_t numIfItems; /**< number of valid network interfaces */
627 CAHistory_t requestHistory; /**< filter IP family in requests */
632 * Hold global variables for TCP Adapter.
636 void *threadpool; /**< threadpool between Initialize and Start */
637 CASocket_t ipv4; /**< IPv4 accept socket */
638 CASocket_t ipv4s; /**< IPv4 accept socket secure */
639 CASocket_t ipv6; /**< IPv6 accept socket */
640 CASocket_t ipv6s; /**< IPv6 accept socket secure */
641 int selectTimeout; /**< in seconds */
642 int listenBacklog; /**< backlog counts*/
644 WSAEVENT updateEvent; /**< Event used to signal thread to stop or update the FD list */
646 int shutdownFds[2]; /**< shutdown pipe */
647 int connectionFds[2]; /**< connection pipe */
648 int maxfd; /**< highest fd (for select) */
650 bool started; /**< the TCP adapter has started */
651 volatile bool terminate;/**< the TCP adapter needs to stop */
652 bool ipv4tcpenabled; /**< IPv4 TCP enabled by OCInit flags */
653 bool ipv6tcpenabled; /**< IPv6 TCP enabled by OCInit flags */
656 CATransportBTFlags_t bleFlags; /**< flags related BLE transport */
659 extern CAGlobals_t caglobals;
662 * Callback function type for request delivery.
663 * @param[out] object Endpoint object from which the request is received.
664 * It contains endpoint address based on the connectivity type.
665 * @param[out] requestInfo Info for resource model to understand about the request.
667 typedef void (*CARequestCallback)(const CAEndpoint_t *object,
668 const CARequestInfo_t *requestInfo);
671 * Callback function type for response delivery.
672 * @param[out] object Endpoint object from which the response is received.
673 * @param[out] responseInfo Identifier which needs to be mapped with response.
675 typedef void (*CAResponseCallback)(const CAEndpoint_t *object,
676 const CAResponseInfo_t *responseInfo);
678 * Callback function type for error.
679 * @param[out] object remote device information.
680 * @param[out] errorInfo CA Error information.
682 typedef void (*CAErrorCallback)(const CAEndpoint_t *object,
683 const CAErrorInfo_t *errorInfo);
686 * Callback function type for network status changes delivery from CA common logic.
687 * @param[out] info Endpoint object from which the network status is changed.
688 * It contains endpoint address based on the connectivity type.
689 * @param[out] status Current network status info.
691 typedef void (*CANetworkMonitorCallback)(const CAEndpoint_t *info, CANetworkStatus_t status);
697 #endif // CA_COMMON_H_