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)
104 #define CA_MAX_URI_LENGTH 128 /* maximum size of URI for embedded platforms*/
106 #define CA_MAX_URI_LENGTH 512 /* maximum size of URI for other platforms*/
110 * Max PDU length supported.
113 #define COAP_MAX_PDU_SIZE 320 /* maximum size of a CoAP PDU for embedded platforms*/
115 #define COAP_MAX_PDU_SIZE 1400 /* maximum size of a CoAP PDU for big platforms*/
119 #define CA_DEFAULT_BLOCK_SIZE CA_BLOCK_SIZE_1024_BYTE
123 *Maximum length of the remoteEndpoint identity.
125 #define CA_MAX_ENDPOINT_IDENTITY_LEN CA_MAX_IDENTITY_SIZE
130 #define CA_MAX_IDENTITY_SIZE (37)
133 * option types - the highest option number 63.
135 #define CA_OPTION_IF_MATCH 1
136 #define CA_OPTION_ETAG 4
137 #define CA_OPTION_IF_NONE_MATCH 5
138 #define CA_OPTION_OBSERVE 6
139 #define CA_OPTION_LOCATION_PATH 8
140 #define CA_OPTION_URI_PATH 11
141 #define CA_OPTION_CONTENT_FORMAT 12
142 #define CA_OPTION_CONTENT_TYPE COAP_OPTION_CONTENT_FORMAT
143 #define CA_OPTION_MAXAGE 14
144 #define CA_OPTION_URI_QUERY 15
145 #define CA_OPTION_ACCEPT 17
146 #define CA_OPTION_LOCATION_QUERY 20
149 * Payload information from resource model.
151 typedef uint8_t *CAPayload_t;
154 * URI for the OIC base.CA considers relative URI as the URI.
156 typedef char *CAURI_t;
159 * Token information for mapping the request and responses by resource model.
161 typedef char *CAToken_t;
164 * Socket types and error definitions.
166 #ifdef HAVE_WINSOCK2_H
167 # define OC_SOCKET_ERROR SOCKET_ERROR
168 # define OC_INVALID_SOCKET INVALID_SOCKET
169 typedef SOCKET CASocketFd_t;
170 #else // HAVE_WINSOCK2_H
171 # define OC_SOCKET_ERROR (-1)
172 # define OC_INVALID_SOCKET (-1)
173 typedef int CASocketFd_t;
177 * The following flags are the same as the equivalent OIC values in
178 * octypes.h, allowing direct copying with slight fixup.
179 * The CA layer should used the OC types when build allows that.
182 #define MAX_ADDR_STR_SIZE_CA (256)
185 * Max Address could be "coaps+tcp://[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:yyy.yyy.yyy.yyy]:xxxxx"
186 * Which is 65, +1 for null terminator => 66
187 * OCDevAddr (defined in OCTypes.h) must be the same
188 * as CAEndpoint_t (defined here)
190 #define MAX_ADDR_STR_SIZE_CA (66)
195 CA_DEFAULT_ADAPTER = 0,
197 // value zero indicates discovery
198 CA_ADAPTER_IP = (1 << 0), // IPv4 and IPv6, including 6LoWPAN
199 CA_ADAPTER_GATT_BTLE = (1 << 1), // GATT over Bluetooth LE
200 CA_ADAPTER_RFCOMM_BTEDR = (1 << 2), // RFCOMM over Bluetooth EDR
203 CA_ADAPTER_REMOTE_ACCESS = (1 << 3), // Remote Access over XMPP.
206 CA_ADAPTER_TCP = (1 << 4), // CoAP over TCP
207 CA_ADAPTER_NFC = (1 << 5), // NFC Adapter
209 CA_ALL_ADAPTERS = 0xffffffff
210 } CATransportAdapter_t;
214 CA_DEFAULT_FLAGS = 0,
216 // Insecure transport is the default (subject to change)
217 CA_SECURE = (1 << 4), // secure the transport path
218 // IPv4 & IPv6 autoselection is the default
219 CA_IPV6 = (1 << 5), // IP adapter only
220 CA_IPV4 = (1 << 6), // IP adapter only
221 // Indication that a message was received by multicast.
222 CA_MULTICAST = (1 << 7),
223 // Link-Local multicast is the default multicast scope for IPv6.
224 // These correspond in both value and position to the IPv6 address bits.
225 CA_SCOPE_INTERFACE = 0x1, // IPv6 Interface-Local scope
226 CA_SCOPE_LINK = 0x2, // IPv6 Link-Local scope (default)
227 CA_SCOPE_REALM = 0x3, // IPv6 Realm-Local scope
228 CA_SCOPE_ADMIN = 0x4, // IPv6 Admin-Local scope
229 CA_SCOPE_SITE = 0x5, // IPv6 Site-Local scope
230 CA_SCOPE_ORG = 0x8, // IPv6 Organization-Local scope
231 CA_SCOPE_GLOBAL = 0xE, // IPv6 Global scope
232 } CATransportFlags_t;
236 CA_DEFAULT_BT_FLAGS = 0,
237 // flags for BLE transport
238 CA_LE_ADV_DISABLE = 0x1, // disable BLE advertisement.
239 CA_LE_ADV_ENABLE = 0x2, // enable BLE advertisement.
240 CA_LE_SERVER_DISABLE = (1 << 4), // disable gatt server.
241 // flags for EDR transport
242 CA_EDR_SERVER_DISABLE = (1 << 7) // disable rfcomm server.
243 } CATransportBTFlags_t;
245 #define CA_IPFAMILY_MASK (CA_IPV6|CA_IPV4)
246 #define CA_SCOPE_MASK 0xf // mask scope bits above
249 * Information about the network status.
253 CA_INTERFACE_DOWN, /**< Connection is not available */
254 CA_INTERFACE_UP /**< Connection is Available */
258 * remoteEndpoint identity.
263 unsigned char id[CA_MAX_ENDPOINT_IDENTITY_LEN];
267 * Message Type for Base source code.
271 CA_MSG_CONFIRM = 0, /**< confirmable message (requires ACK/RST) */
272 CA_MSG_NONCONFIRM, /**< non-confirmable message (one-shot message) */
273 CA_MSG_ACKNOWLEDGE, /**< used to acknowledge confirmable messages */
274 CA_MSG_RESET /**< used to indicates not-interested or error (lack of context)in
279 * Allowed method to be used by resource model.
283 CA_GET = 1, /**< GET Method */
284 CA_POST, /**< POST Method */
285 CA_PUT, /**< PUT Method */
286 CA_DELETE /**< DELETE Method */
291 * it depends on defined size in libCoAP.
295 CA_BLOCK_SIZE_16_BYTE = 0, /**< 16byte */
296 CA_BLOCK_SIZE_32_BYTE = 1, /**< 32byte */
297 CA_BLOCK_SIZE_64_BYTE = 2, /**< 64byte */
298 CA_BLOCK_SIZE_128_BYTE = 3, /**< 128byte */
299 CA_BLOCK_SIZE_256_BYTE = 4, /**< 256byte */
300 CA_BLOCK_SIZE_512_BYTE = 5, /**< 512byte */
301 CA_BLOCK_SIZE_1024_BYTE = 6 /**< 1Kbyte */
305 * Endpoint information for connectivities.
306 * Must be identical to OCDevAddr.
310 CATransportAdapter_t adapter; // adapter type
311 CATransportFlags_t flags; // transport modifiers
312 uint16_t port; // for IP
313 char addr[MAX_ADDR_STR_SIZE_CA]; // address for all
314 uint32_t ifindex; // usually zero for default interface
315 char remoteId[CA_MAX_IDENTITY_SIZE]; // device ID of remote device
316 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
317 char routeData[MAX_ADDR_STR_SIZE_CA]; /**< GatewayId:ClientId of
323 * Endpoint information for secure messages.
327 CAEndpoint_t endpoint; /**< endpoint */
328 // TODO change name to deviceId
329 CARemoteId_t identity; /**< endpoint device uuid */
330 CARemoteId_t userId; /**< endpoint user uuid */
331 } CASecureEndpoint_t;
334 * Enums for CA return values.
338 /* Result code - START HERE */
339 CA_STATUS_OK = 0, /**< Success */
340 CA_STATUS_INVALID_PARAM, /**< Invalid Parameter */
341 CA_ADAPTER_NOT_ENABLED, /**< Adapter is not enabled */
342 CA_SERVER_STARTED_ALREADY, /**< Server is started already */
343 CA_SERVER_NOT_STARTED, /**< Server is not started */
344 CA_DESTINATION_NOT_REACHABLE, /**< Destination is not reachable */
345 CA_SOCKET_OPERATION_FAILED, /**< Socket operation failed */
346 CA_SEND_FAILED, /**< Send request failed */
347 CA_RECEIVE_FAILED, /**< Receive failed */
348 CA_MEMORY_ALLOC_FAILED, /**< Memory allocation failed */
349 CA_REQUEST_TIMEOUT, /**< Request is Timeout */
350 CA_DESTINATION_DISCONNECTED, /**< Destination is disconnected */
351 CA_NOT_SUPPORTED, /**< Not supported */
352 CA_STATUS_NOT_INITIALIZED, /**< Not Initialized*/
353 CA_DTLS_AUTHENTICATION_FAILURE, /**< Decryption error in DTLS */
354 CA_STATUS_FAILED =255 /**< Failure */
355 /* Result code - END HERE */
359 * Enums for CA Response values.
363 /* Response status code - START HERE */
364 CA_EMPTY = 0, /**< Empty */
365 CA_CREATED = 201, /**< Created */
366 CA_DELETED = 202, /**< Deleted */
367 CA_VALID = 203, /**< Valid */
368 CA_CHANGED = 204, /**< Changed */
369 CA_CONTENT = 205, /**< Content */
370 CA_CONTINUE = 231, /**< Continue */
371 CA_BAD_REQ = 400, /**< Bad Request */
372 CA_UNAUTHORIZED_REQ = 401, /**< Unauthorized Request */
373 CA_BAD_OPT = 402, /**< Bad Option */
374 CA_FORBIDDEN_REQ = 403, /**< Forbidden Request */
375 CA_NOT_FOUND = 404, /**< Not found */
376 CA_METHOD_NOT_ALLOWED = 405, /**< Method Not Allowed */
377 CA_NOT_ACCEPTABLE = 406, /**< Not Acceptable */
378 CA_REQUEST_ENTITY_INCOMPLETE = 408, /**< Request Entity Incomplete */
379 CA_REQUEST_ENTITY_TOO_LARGE = 413, /**< Request Entity Too Large */
380 CA_TOO_MANY_REQUESTS = 429, /**< Too Many Requests */
381 CA_INTERNAL_SERVER_ERROR = 500, /**< Internal Server Error */
382 CA_NOT_IMPLEMENTED = 501, /**< Not Implenented */
383 CA_BAD_GATEWAY = 502, /**< Bad Gateway */
384 CA_SERVICE_UNAVAILABLE = 503, /**< Server Unavailable */
385 CA_RETRANSMIT_TIMEOUT = 504, /**< Retransmit timeout */
386 CA_PROXY_NOT_SUPPORTED = 505 /**< Proxy not enabled to service a request */
387 /* Response status code - END HERE */
388 } CAResponseResult_t;
391 * Data type whether the data is Request Message or Response Message.
392 * if there is some failure before send data on network.
393 * Type will be set as error type for error callback.
412 * Transport Protocol IDs for additional options.
416 CA_INVALID_ID = (1 << 0), /**< Invalid ID */
417 CA_COAP_ID = (1 << 1) /**< COAP ID */
418 } CATransportProtocolID_t;
421 * Adapter State to indicate the network changed notifications.
425 CA_ADAPTER_DISABLED, /**< Adapter is Disabled */
426 CA_ADAPTER_ENABLED /**< Adapter is Enabled */
430 * Format indicating which encoding has been used on the payload.
434 CA_FORMAT_UNDEFINED = 0, /**< Undefined enoding format */
435 CA_FORMAT_TEXT_PLAIN,
436 CA_FORMAT_APPLICATION_LINK_FORMAT,
437 CA_FORMAT_APPLICATION_XML,
438 CA_FORMAT_APPLICATION_OCTET_STREAM,
439 CA_FORMAT_APPLICATION_RDF_XML,
440 CA_FORMAT_APPLICATION_EXI,
441 CA_FORMAT_APPLICATION_JSON,
442 CA_FORMAT_APPLICATION_CBOR,
443 CA_FORMAT_UNSUPPORTED
447 * Header options structure to be filled.
449 * This structure is used to hold header information.
453 CATransportProtocolID_t protocolID; /**< Protocol ID of the Option */
454 uint16_t optionID; /**< The header option ID which will be
455 added to communication packets */
456 uint16_t optionLength; /**< Option Length **/
457 char optionData[CA_MAX_HEADER_OPTION_DATA_LENGTH]; /**< Optional data values**/
461 * Base Information received.
463 * This structure is used to hold request & response base information.
467 CAMessageType_t type; /**< Qos for the request */
468 #ifdef ROUTING_GATEWAY
469 bool skipRetransmission; /**< Will not attempt retransmission even if type is CONFIRM.
470 Required for packet forwarding */
472 uint16_t messageId; /**< Message id.
473 * if message id is zero, it will generated by CA inside.
474 * otherwise, you can use it */
475 CAToken_t token; /**< Token for CA */
476 uint8_t tokenLength; /**< token length */
477 CAHeaderOption_t *options; /** Header Options for the request */
478 uint8_t numOptions; /**< Number of Header options */
479 CAPayload_t payload; /**< payload of the request */
480 size_t payloadSize; /**< size in bytes of the payload */
481 CAPayloadFormat_t payloadFormat; /**< encoding format of the request payload */
482 CAPayloadFormat_t acceptFormat; /**< accept format for the response payload */
483 CAURI_t resourceUri; /**< Resource URI information **/
484 CARemoteId_t identity; /**< endpoint identity */
485 CADataType_t dataType; /**< data type */
486 CAConnectEvent_t event; /**< network request message / event type */
490 * Request Information to be sent.
492 * This structure is used to hold request information.
496 CAMethod_t method; /**< Name of the Method Allowed */
497 CAInfo_t info; /**< Information of the request. */
498 bool isMulticast; /**< is multicast request */
502 * Response information received.
504 * This structure is used to hold response information.
508 CAResponseResult_t result; /**< Result for response by resource model */
509 CAInfo_t info; /**< Information of the response */
514 * Error information from CA
515 * contains error code and message information.
517 * This structure holds error information.
521 CAResult_t result; /**< CA API request result */
522 CAInfo_t info; /**< message information such as token and payload data
523 helpful to identify the error */
527 * Hold global variables for CA layer. (also used by RI layer)
531 CASocketFd_t fd; /**< socket fd */
532 uint16_t port; /**< socket port */
535 #define HISTORYSIZE (4)
539 CATransportFlags_t flags;
541 char token[CA_MAX_TOKEN_LEN];
548 CAHistoryItem_t items[HISTORYSIZE];
552 * Hold interface index for keeping track of comings and goings.
556 int32_t ifIndex; /**< network interface index */
560 * Hold the port number assigned from application.
561 * It will be used when creating a socket.
567 uint16_t u6; /**< unicast IPv6 socket port */
568 uint16_t u6s; /**< unicast IPv6 socket secure port */
569 uint16_t u4; /**< unicast IPv4 socket port */
570 uint16_t u4s; /**< unicast IPv4 socket secure port */
575 uint16_t u4; /**< unicast IPv4 socket port */
576 uint16_t u4s; /**< unicast IPv6 socket secure port */
577 uint16_t u6; /**< unicast IPv6 socket port */
578 uint16_t u6s; /**< unicast IPv6 socket secure port */
585 CATransportFlags_t clientFlags; /**< flag for client */
586 CATransportFlags_t serverFlags; /**< flag for server */
587 bool client; /**< client mode */
588 bool server; /**< server mode */
594 void *threadpool; /**< threadpool between Initialize and Start */
595 CASocket_t u6; /**< unicast IPv6 */
596 CASocket_t u6s; /**< unicast IPv6 secure */
597 CASocket_t u4; /**< unicast IPv4 */
598 CASocket_t u4s; /**< unicast IPv4 secure */
599 CASocket_t m6; /**< multicast IPv6 */
600 CASocket_t m6s; /**< multicast IPv6 secure */
601 CASocket_t m4; /**< multicast IPv4 */
602 CASocket_t m4s; /**< multicast IPv4 secure */
603 CASocketFd_t netlinkFd; /**< netlink */
605 WSAEVENT shutdownEvent; /**< Event used to signal threads to stop */
608 int shutdownFds[2]; /**< fds used to signal threads to stop */
611 int selectTimeout; /**< in seconds */
612 bool started; /**< the IP adapter has started */
613 bool terminate; /**< the IP adapter needs to stop */
614 bool ipv6enabled; /**< IPv6 enabled by OCInit flags */
615 bool ipv4enabled; /**< IPv4 enabled by OCInit flags */
616 bool dualstack; /**< IPv6 and IPv4 enabled */
618 LPFN_WSARECVMSG wsaRecvMsg; /**< Win32 function pointer to WSARecvMsg() */
621 struct networkmonitors
623 CAIfItem_t *ifItems; /**< current network interface index list */
624 size_t sizeIfItems; /**< size of network interface index array */
625 size_t numIfItems; /**< number of valid network interfaces */
631 CAHistory_t requestHistory; /**< filter IP family in requests */
636 * Hold global variables for TCP Adapter.
640 void *threadpool; /**< threadpool between Initialize and Start */
641 CASocket_t ipv4; /**< IPv4 accept socket */
642 CASocket_t ipv4s; /**< IPv4 accept socket secure */
643 CASocket_t ipv6; /**< IPv6 accept socket */
644 CASocket_t ipv6s; /**< IPv6 accept socket secure */
645 void *svrlist; /**< unicast IPv4 TCP server information*/
646 int selectTimeout; /**< in seconds */
647 int listenBacklog; /**< backlog counts*/
649 int shutdownFds[2]; /**< shutdown pipe */
651 int connectionFds[2]; /**< connection pipe */
652 bool started; /**< the TCP adapter has started */
653 bool terminate; /**< the TCP adapter needs to stop */
654 bool ipv4tcpenabled; /**< IPv4 TCP enabled by OCInit flags */
655 bool ipv6tcpenabled; /**< IPv6 TCP enabled by OCInit flags */
658 CATransportBTFlags_t bleFlags;
661 extern CAGlobals_t caglobals;
665 CA_LOG_LEVEL_ALL = 1, // all logs.
666 CA_LOG_LEVEL_INFO, // debug level is disabled
670 * Callback function type for request delivery.
671 * @param[out] object Endpoint object from which the request is received.
672 * It contains endpoint address based on the connectivity type.
673 * @param[out] requestInfo Info for resource model to understand about the request.
675 typedef void (*CARequestCallback)(const CAEndpoint_t *object,
676 const CARequestInfo_t *requestInfo);
679 * Callback function type for response delivery.
680 * @param[out] object Endpoint object from which the response is received.
681 * @param[out] responseInfo Identifier which needs to be mapped with response.
683 typedef void (*CAResponseCallback)(const CAEndpoint_t *object,
684 const CAResponseInfo_t *responseInfo);
686 * Callback function type for error.
687 * @param[out] object remote device information.
688 * @param[out] errorInfo CA Error information.
690 typedef void (*CAErrorCallback)(const CAEndpoint_t *object,
691 const CAErrorInfo_t *errorInfo);
694 * Callback function type for network status changes delivery from CA common logic.
695 * @param[out] info Endpoint object from which the network status is changed.
696 * It contains endpoint address based on the connectivity type.
697 * @param[out] status Current network status info.
699 typedef void (*CANetworkMonitorCallback)(const CAEndpoint_t *info, CANetworkStatus_t status);
702 * Callback function type for editing bluetooth advertisement data before starting bluetooth advertisement.
703 * param[in] max_data_size Maximal size of advertisement data array
704 * param[out] data Advertisement data array
705 * param[out] data_size Actual Data size of advertisement data array
707 typedef void (*CAAdvertisementDataGetterCB)(int max_data_size, char *data, int *data_size);
713 #endif // CA_COMMON_H_