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"
30 #ifdef WITH_PROCESS_EVENT
36 #define HAVE_SYS_POLL_H
47 #ifdef HAVE_SYS_POLL_H
52 #ifdef HAVE_WINSOCK2_H
63 * TAG of Analyzer log.
65 #define ANALYZER_TAG "OIC_CA_ANALYZER_102301"
66 #define BLE_CLIENT_TAG "OIC_CA_LE_CLIENT_1023"
67 #define BLE_SERVER_MCD_TAG "OIC_CA_LE_SER_MC_1023"
68 #define IP_SERVER_TAG "OIC_CA_IP_SERVER_1023"
69 #define TCP_SERVER_TAG "OIC_CA_TCP_SERVER_1023"
74 #define CA_IPADDR_SIZE 16
77 * Remote Access jabber ID length.
79 #define CA_RAJABBERID_SIZE 256
82 * Mac address length for BT port.
84 #define CA_MACADDR_SIZE 18
87 * Max header options data length.
89 #if defined(ARDUINO) || defined(__TIZENRT__)
90 #define CA_MAX_HEADER_OPTION_DATA_LENGTH 20
92 #define CA_MAX_HEADER_OPTION_DATA_LENGTH 1024
98 #define CA_MAX_TOKEN_LEN (8)
101 * Max interface name length.
103 #define CA_MAX_INTERFACE_NAME_LEN (16)
109 #define CA_MAX_URI_LENGTH 128 /* maximum size of URI for embedded platforms*/
111 #define CA_MAX_URI_LENGTH 512 /* maximum size of URI for other platforms*/
115 * Max PDU length supported.
118 #define COAP_MAX_PDU_SIZE 320 /* maximum size of a CoAP PDU for embedded platforms*/
120 #define COAP_MAX_PDU_SIZE 1400 /* maximum size of a CoAP PDU for big platforms*/
124 #define CA_DEFAULT_BLOCK_SIZE CA_BLOCK_SIZE_1024_BYTE
128 *Maximum length of the remoteEndpoint identity.
130 #define CA_MAX_ENDPOINT_IDENTITY_LEN CA_MAX_IDENTITY_SIZE
135 #define CA_MAX_IDENTITY_SIZE (37)
138 * option types - the highest option number 63.
140 #define CA_OPTION_IF_MATCH 1
141 #define CA_OPTION_ETAG 4
142 #define CA_OPTION_IF_NONE_MATCH 5
143 #define CA_OPTION_OBSERVE 6
144 #define CA_OPTION_LOCATION_PATH 8
145 #define CA_OPTION_URI_PATH 11
146 #define CA_OPTION_CONTENT_FORMAT 12
147 #define CA_OPTION_CONTENT_TYPE COAP_OPTION_CONTENT_FORMAT
148 #define CA_OPTION_MAXAGE 14
149 #define CA_OPTION_URI_QUERY 15
150 #define CA_OPTION_ACCEPT 17
151 #define CA_OPTION_LOCATION_QUERY 20
154 * Payload information from resource model.
156 typedef uint8_t *CAPayload_t;
159 * URI for the OIC base.CA considers relative URI as the URI.
161 typedef char *CAURI_t;
164 * Token information for mapping the request and responses by resource model.
166 typedef char *CAToken_t;
169 * Socket types and error definitions.
171 #ifdef HAVE_WINSOCK2_H
172 # define OC_SOCKET_ERROR SOCKET_ERROR
173 # define OC_INVALID_SOCKET INVALID_SOCKET
174 typedef SOCKET CASocketFd_t;
175 #else // HAVE_WINSOCK2_H
176 # define OC_SOCKET_ERROR (-1)
177 # define OC_INVALID_SOCKET (-1)
178 typedef int CASocketFd_t;
182 * The following flags are the same as the equivalent OIC values in
183 * octypes.h, allowing direct copying with slight fixup.
184 * The CA layer should used the OC types when build allows that.
187 #define MAX_ADDR_STR_SIZE_CA (256)
190 * Max Address could be "coaps+tcp://[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:yyy.yyy.yyy.yyy]:xxxxx"
191 * Which is 65, +1 for null terminator => 66
192 * OCDevAddr (defined in OCTypes.h) must be the same
193 * as CAEndpoint_t (defined here)
195 #define MAX_ADDR_STR_SIZE_CA (66)
200 CA_DEFAULT_ADAPTER = 0,
202 // value zero indicates discovery
203 CA_ADAPTER_IP = (1 << 0), // IPv4 and IPv6, including 6LoWPAN
204 CA_ADAPTER_GATT_BTLE = (1 << 1), // GATT over Bluetooth LE
205 CA_ADAPTER_RFCOMM_BTEDR = (1 << 2), // RFCOMM over Bluetooth EDR
208 CA_ADAPTER_REMOTE_ACCESS = (1 << 3), // Remote Access over XMPP.
211 CA_ADAPTER_TCP = (1 << 4), // CoAP over TCP
212 CA_ADAPTER_NFC = (1 << 5), // NFC Adapter
214 CA_ALL_ADAPTERS = 0xffffffff
215 } CATransportAdapter_t;
219 CA_DEFAULT_FLAGS = 0,
221 // Insecure transport is the default (subject to change)
222 CA_SECURE = (1 << 4), // secure the transport path
223 // IPv4 & IPv6 autoselection is the default
224 CA_IPV6 = (1 << 5), // IP adapter only
225 CA_IPV4 = (1 << 6), // IP adapter only
226 // Indication that a message was received by multicast.
227 CA_MULTICAST = (1 << 7),
228 // Link-Local multicast is the default multicast scope for IPv6.
229 // These correspond in both value and position to the IPv6 address bits.
230 CA_SCOPE_INTERFACE = 0x1, // IPv6 Interface-Local scope
231 CA_SCOPE_LINK = 0x2, // IPv6 Link-Local scope (default)
232 CA_SCOPE_REALM = 0x3, // IPv6 Realm-Local scope
233 CA_SCOPE_ADMIN = 0x4, // IPv6 Admin-Local scope
234 CA_SCOPE_SITE = 0x5, // IPv6 Site-Local scope
235 CA_SCOPE_ORG = 0x8, // IPv6 Organization-Local scope
236 CA_SCOPE_GLOBAL = 0xE, // IPv6 Global scope
237 } CATransportFlags_t;
241 CA_DEFAULT_BT_FLAGS = 0,
242 // flags for BLE transport
243 CA_LE_ADV_DISABLE = 0x1, // disable BLE advertisement.
244 CA_LE_ADV_ENABLE = 0x2, // enable BLE advertisement.
245 CA_LE_SERVER_DISABLE = (1 << 4), // disable gatt server.
246 // flags for EDR transport
247 CA_EDR_SERVER_DISABLE = (1 << 7) // disable rfcomm server.
248 } CATransportBTFlags_t;
250 #define CA_IPFAMILY_MASK (CA_IPV6|CA_IPV4)
251 #define CA_SCOPE_MASK 0xf // mask scope bits above
254 * Information about the network status.
258 CA_INTERFACE_DOWN, /**< Connection is not available */
259 CA_INTERFACE_UP /**< Connection is Available */
263 * remoteEndpoint identity.
268 unsigned char id[CA_MAX_ENDPOINT_IDENTITY_LEN];
272 * Message Type for Base source code.
276 CA_MSG_CONFIRM = 0, /**< confirmable message (requires ACK/RST) */
277 CA_MSG_NONCONFIRM, /**< non-confirmable message (one-shot message) */
278 CA_MSG_ACKNOWLEDGE, /**< used to acknowledge confirmable messages */
279 CA_MSG_RESET /**< used to indicates not-interested or error (lack of context)in
284 * Allowed method to be used by resource model.
288 CA_GET = 1, /**< GET Method */
289 CA_POST, /**< POST Method */
290 CA_PUT, /**< PUT Method */
291 CA_DELETE /**< DELETE Method */
296 * it depends on defined size in libCoAP.
300 CA_BLOCK_SIZE_16_BYTE = 0, /**< 16byte */
301 CA_BLOCK_SIZE_32_BYTE = 1, /**< 32byte */
302 CA_BLOCK_SIZE_64_BYTE = 2, /**< 64byte */
303 CA_BLOCK_SIZE_128_BYTE = 3, /**< 128byte */
304 CA_BLOCK_SIZE_256_BYTE = 4, /**< 256byte */
305 CA_BLOCK_SIZE_512_BYTE = 5, /**< 512byte */
306 CA_BLOCK_SIZE_1024_BYTE = 6 /**< 1Kbyte */
310 * Endpoint information for connectivities.
311 * Must be identical to OCDevAddr.
315 CATransportAdapter_t adapter; // adapter type
316 CATransportFlags_t flags; // transport modifiers
317 uint16_t port; // for IP
318 char addr[MAX_ADDR_STR_SIZE_CA]; // address for all
319 uint32_t ifindex; // usually zero for default interface
320 char remoteId[CA_MAX_IDENTITY_SIZE]; // device ID of remote device
321 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
322 char routeData[MAX_ADDR_STR_SIZE_CA]; /**< GatewayId:ClientId of
328 * Endpoint information for secure messages.
332 CAEndpoint_t endpoint; /**< endpoint */
333 // TODO change name to deviceId
334 CARemoteId_t identity; /**< endpoint device uuid */
335 CARemoteId_t userId; /**< endpoint user uuid */
336 } CASecureEndpoint_t;
339 * Enums for CA return values.
343 /* Result code - START HERE */
344 CA_STATUS_OK = 0, /**< Success */
345 CA_STATUS_INVALID_PARAM, /**< Invalid Parameter */
346 CA_ADAPTER_NOT_ENABLED, /**< Adapter is not enabled */
347 CA_SERVER_STARTED_ALREADY, /**< Server is started already */
348 CA_SERVER_NOT_STARTED, /**< Server is not started */
349 CA_DESTINATION_NOT_REACHABLE, /**< Destination is not reachable */
350 CA_SOCKET_OPERATION_FAILED, /**< Socket operation failed */
351 CA_SEND_FAILED, /**< Send request failed */
352 CA_RECEIVE_FAILED, /**< Receive failed */
353 CA_MEMORY_ALLOC_FAILED, /**< Memory allocation failed */
354 CA_REQUEST_TIMEOUT, /**< Request is Timeout */
355 CA_DESTINATION_DISCONNECTED, /**< Destination is disconnected */
356 CA_NOT_SUPPORTED, /**< Not supported */
357 CA_STATUS_NOT_INITIALIZED, /**< Not Initialized*/
358 CA_DTLS_AUTHENTICATION_FAILURE, /**< Decryption error in DTLS */
359 CA_STATUS_FAILED =255 /**< Failure */
360 /* Result code - END HERE */
364 * Enums for CA Response values.
368 /* Response status code - START HERE */
369 CA_EMPTY = 0, /**< Empty */
370 CA_CREATED = 201, /**< Created */
371 CA_DELETED = 202, /**< Deleted */
372 CA_VALID = 203, /**< Valid */
373 CA_CHANGED = 204, /**< Changed */
374 CA_CONTENT = 205, /**< Content */
375 CA_CONTINUE = 231, /**< Continue */
376 CA_BAD_REQ = 400, /**< Bad Request */
377 CA_UNAUTHORIZED_REQ = 401, /**< Unauthorized Request */
378 CA_BAD_OPT = 402, /**< Bad Option */
379 CA_FORBIDDEN_REQ = 403, /**< Forbidden Request */
380 CA_NOT_FOUND = 404, /**< Not found */
381 CA_METHOD_NOT_ALLOWED = 405, /**< Method Not Allowed */
382 CA_NOT_ACCEPTABLE = 406, /**< Not Acceptable */
383 CA_REQUEST_ENTITY_INCOMPLETE = 408, /**< Request Entity Incomplete */
384 CA_REQUEST_ENTITY_TOO_LARGE = 413, /**< Request Entity Too Large */
385 CA_TOO_MANY_REQUESTS = 429, /**< Too Many Requests */
386 CA_INTERNAL_SERVER_ERROR = 500, /**< Internal Server Error */
387 CA_NOT_IMPLEMENTED = 501, /**< Not Implenented */
388 CA_BAD_GATEWAY = 502, /**< Bad Gateway */
389 CA_SERVICE_UNAVAILABLE = 503, /**< Server Unavailable */
390 CA_RETRANSMIT_TIMEOUT = 504, /**< Retransmit timeout */
391 CA_PROXY_NOT_SUPPORTED = 505 /**< Proxy not enabled to service a request */
392 /* Response status code - END HERE */
393 } CAResponseResult_t;
396 * Data type whether the data is Request Message or Response Message.
397 * if there is some failure before send data on network.
398 * Type will be set as error type for error callback.
407 CA_REQUEST_DATA_SERVER_INIT,
408 CA_RESPONSE_DATA_CLIENT_INIT
419 * Transport Protocol IDs for additional options.
423 CA_INVALID_ID = (1 << 0), /**< Invalid ID */
424 CA_COAP_ID = (1 << 1) /**< COAP ID */
425 } CATransportProtocolID_t;
428 * Adapter State to indicate the network changed notifications.
432 CA_ADAPTER_DISABLED, /**< Adapter is Disabled */
433 CA_ADAPTER_ENABLED /**< Adapter is Enabled */
437 * Format indicating which encoding has been used on the payload.
441 CA_FORMAT_UNDEFINED = 0, /**< Undefined enoding format */
442 CA_FORMAT_TEXT_PLAIN,
443 CA_FORMAT_APPLICATION_LINK_FORMAT,
444 CA_FORMAT_APPLICATION_XML,
445 CA_FORMAT_APPLICATION_OCTET_STREAM,
446 CA_FORMAT_APPLICATION_RDF_XML,
447 CA_FORMAT_APPLICATION_EXI,
448 CA_FORMAT_APPLICATION_JSON,
449 CA_FORMAT_APPLICATION_CBOR,
450 CA_FORMAT_UNSUPPORTED
454 * Header options structure to be filled.
456 * This structure is used to hold header information.
460 CATransportProtocolID_t protocolID; /**< Protocol ID of the Option */
461 uint16_t optionID; /**< The header option ID which will be
462 added to communication packets */
463 uint16_t optionLength; /**< Option Length **/
464 char optionData[CA_MAX_HEADER_OPTION_DATA_LENGTH]; /**< Optional data values**/
468 * Base Information received.
470 * This structure is used to hold request & response base information.
474 CAMessageType_t type; /**< Qos for the request */
475 #ifdef ROUTING_GATEWAY
476 bool skipRetransmission; /**< Will not attempt retransmission even if type is CONFIRM.
477 Required for packet forwarding */
479 uint16_t messageId; /**< Message id.
480 * if message id is zero, it will generated by CA inside.
481 * otherwise, you can use it */
482 CAToken_t token; /**< Token for CA */
483 uint8_t tokenLength; /**< token length */
484 CAHeaderOption_t *options; /** Header Options for the request */
485 uint8_t numOptions; /**< Number of Header options */
486 CAPayload_t payload; /**< payload of the request */
487 size_t payloadSize; /**< size in bytes of the payload */
488 CAPayloadFormat_t payloadFormat; /**< encoding format of the request payload */
489 CAPayloadFormat_t acceptFormat; /**< accept format for the response payload */
490 CAURI_t resourceUri; /**< Resource URI information **/
491 CARemoteId_t identity; /**< endpoint identity */
492 CADataType_t dataType; /**< data type */
493 CAConnectEvent_t event; /**< network request message / event type */
497 * Request Information to be sent.
499 * This structure is used to hold request information.
503 CAMethod_t method; /**< Name of the Method Allowed */
504 CAInfo_t info; /**< Information of the request. */
505 bool isMulticast; /**< is multicast request */
509 * Response information received.
511 * This structure is used to hold response information.
515 CAResponseResult_t result; /**< Result for response by resource model */
516 CAInfo_t info; /**< Information of the response */
521 * Error information from CA
522 * contains error code and message information.
524 * This structure holds error information.
528 CAResult_t result; /**< CA API request result */
529 CAInfo_t info; /**< message information such as token and payload data
530 helpful to identify the error */
534 * Hold global variables for CA layer. (also used by RI layer)
538 CASocketFd_t fd; /**< socket fd */
539 uint16_t port; /**< socket port */
542 #define HISTORYSIZE (4)
546 CATransportFlags_t flags;
548 char token[CA_MAX_TOKEN_LEN];
555 CAHistoryItem_t items[HISTORYSIZE];
559 * Hold interface index for keeping track of comings and goings.
563 int32_t ifIndex; /**< network interface index */
567 * Hold the port number assigned from application.
568 * It will be used when creating a socket.
574 uint16_t u6; /**< unicast IPv6 socket port */
575 uint16_t u6s; /**< unicast IPv6 socket secure port */
576 uint16_t u4; /**< unicast IPv4 socket port */
577 uint16_t u4s; /**< unicast IPv4 socket secure port */
582 uint16_t u4; /**< unicast IPv4 socket port */
583 uint16_t u4s; /**< unicast IPv6 socket secure port */
584 uint16_t u6; /**< unicast IPv6 socket port */
585 uint16_t u6s; /**< unicast IPv6 socket secure port */
592 CATransportFlags_t clientFlags; /**< flag for client */
593 CATransportFlags_t serverFlags; /**< flag for server */
594 bool client; /**< client mode */
595 bool server; /**< server mode */
601 void *threadpool; /**< threadpool between Initialize and Start */
602 CASocket_t u6; /**< unicast IPv6 */
603 CASocket_t u6s; /**< unicast IPv6 secure */
604 CASocket_t u4; /**< unicast IPv4 */
605 CASocket_t u4s; /**< unicast IPv4 secure */
606 CASocket_t m6; /**< multicast IPv6 */
607 CASocket_t m6s; /**< multicast IPv6 secure */
608 CASocket_t m4; /**< multicast IPv4 */
609 CASocket_t m4s; /**< multicast IPv4 secure */
610 CASocketFd_t netlinkFd; /**< netlink */
612 WSAEVENT shutdownEvent; /**< Event used to signal threads to stop */
615 int shutdownFds[2]; /**< fds used to signal threads to stop */
618 int selectTimeout; /**< in seconds */
619 bool started; /**< the IP adapter has started */
620 bool terminate; /**< the IP adapter needs to stop */
621 bool ipv6enabled; /**< IPv6 enabled by OCInit flags */
622 bool ipv4enabled; /**< IPv4 enabled by OCInit flags */
623 bool dualstack; /**< IPv6 and IPv4 enabled */
625 LPFN_WSARECVMSG wsaRecvMsg; /**< Win32 function pointer to WSARecvMsg() */
628 struct networkmonitors
630 CAIfItem_t *ifItems; /**< current network interface index list */
631 size_t sizeIfItems; /**< size of network interface index array */
632 size_t numIfItems; /**< number of valid network interfaces */
638 CAHistory_t requestHistory; /**< filter IP family in requests */
643 * Hold global variables for TCP Adapter.
647 void *threadpool; /**< threadpool between Initialize and Start */
648 CASocket_t ipv4; /**< IPv4 accept socket */
649 CASocket_t ipv4s; /**< IPv4 accept socket secure */
650 CASocket_t ipv6; /**< IPv6 accept socket */
651 CASocket_t ipv6s; /**< IPv6 accept socket secure */
652 void *svrlist; /**< unicast IPv4 TCP server information*/
653 int selectTimeout; /**< in seconds */
654 int listenBacklog; /**< backlog counts*/
656 int shutdownFds[2]; /**< shutdown pipe */
658 int connectionFds[2]; /**< connection pipe */
659 bool started; /**< the TCP adapter has started */
660 bool terminate; /**< the TCP adapter needs to stop */
661 bool bindenabled; /**< binding of TCP socket enabled */
662 bool ipv4tcpenabled; /**< IPv4 TCP enabled by OCInit flags */
663 bool ipv6tcpenabled; /**< IPv6 TCP enabled by OCInit flags */
666 CATransportBTFlags_t bleFlags;
669 extern CAGlobals_t caglobals;
673 CA_LOG_LEVEL_ALL = 1, // all logs.
674 CA_LOG_LEVEL_INFO, // debug level is disabled
678 * Callback function type for request delivery.
679 * @param[out] object Endpoint object from which the request is received.
680 * It contains endpoint address based on the connectivity type.
681 * @param[out] requestInfo Info for resource model to understand about the request.
683 typedef void (*CARequestCallback)(const CAEndpoint_t *object,
684 const CARequestInfo_t *requestInfo);
687 * Callback function type for response delivery.
688 * @param[out] object Endpoint object from which the response is received.
689 * @param[out] responseInfo Identifier which needs to be mapped with response.
691 typedef void (*CAResponseCallback)(const CAEndpoint_t *object,
692 const CAResponseInfo_t *responseInfo);
694 * Callback function type for error.
695 * @param[out] object remote device information.
696 * @param[out] errorInfo CA Error information.
698 typedef void (*CAErrorCallback)(const CAEndpoint_t *object,
699 const CAErrorInfo_t *errorInfo);
702 * Callback function type for network status changes delivery from CA common logic.
703 * @param[out] info Endpoint object from which the network status is changed.
704 * It contains endpoint address based on the connectivity type.
705 * @param[out] status Current network status info.
707 typedef void (*CANetworkMonitorCallback)(const CAEndpoint_t *info, CANetworkStatus_t status);
710 * Callback function type for editing bluetooth advertisement data before starting bluetooth advertisement.
711 * param[in] max_data_size Maximal size of advertisement data array
712 * param[out] data Advertisement data array
713 * param[out] data_size Actual Data size of advertisement data array
715 typedef void (*CAAdvertisementDataGetterCB)(int max_data_size, char *data, int *data_size);
721 #endif // CA_COMMON_H_