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
44 #ifdef HAVE_SYS_POLL_H
49 #ifdef HAVE_WINSOCK2_H
60 * TAG of Analyzer log.
62 #define ANALYZER_TAG "OIC_CA_ANALYZER_092601"
63 #define BLE_CLIENT_TAG "OIC_CA_LE_CLIENT_0926"
64 #define BLE_SERVER_MCD_TAG "OIC_CA_LE_SER_MC_0926"
65 #define IP_SERVER_TAG "OIC_CA_IP_SERVER_0926"
66 #define TCP_SERVER_TAG "OIC_CA_TCP_SERVER_0926"
71 #define CA_IPADDR_SIZE 16
74 * Remote Access jabber ID length.
76 #define CA_RAJABBERID_SIZE 256
79 * Mac address length for BT port.
81 #define CA_MACADDR_SIZE 18
84 * Max header options data length.
86 #if defined(ARDUINO) || defined(__TIZENRT__)
87 #define CA_MAX_HEADER_OPTION_DATA_LENGTH 20
89 #define CA_MAX_HEADER_OPTION_DATA_LENGTH 1024
95 #define CA_MAX_TOKEN_LEN (8)
101 #define CA_MAX_URI_LENGTH 128 /* maximum size of URI for embedded platforms*/
103 #define CA_MAX_URI_LENGTH 512 /* maximum size of URI for other platforms*/
107 * Max PDU length supported.
110 #define COAP_MAX_PDU_SIZE 320 /* maximum size of a CoAP PDU for embedded platforms*/
112 #define COAP_MAX_PDU_SIZE 1400 /* maximum size of a CoAP PDU for big platforms*/
116 #define CA_DEFAULT_BLOCK_SIZE CA_BLOCK_SIZE_1024_BYTE
120 *Maximum length of the remoteEndpoint identity.
122 #define CA_MAX_ENDPOINT_IDENTITY_LEN CA_MAX_IDENTITY_SIZE
127 #define CA_MAX_IDENTITY_SIZE (37)
130 * option types - the highest option number 63.
132 #define CA_OPTION_IF_MATCH 1
133 #define CA_OPTION_ETAG 4
134 #define CA_OPTION_IF_NONE_MATCH 5
135 #define CA_OPTION_OBSERVE 6
136 #define CA_OPTION_LOCATION_PATH 8
137 #define CA_OPTION_URI_PATH 11
138 #define CA_OPTION_CONTENT_FORMAT 12
139 #define CA_OPTION_CONTENT_TYPE COAP_OPTION_CONTENT_FORMAT
140 #define CA_OPTION_MAXAGE 14
141 #define CA_OPTION_URI_QUERY 15
142 #define CA_OPTION_ACCEPT 17
143 #define CA_OPTION_LOCATION_QUERY 20
146 * Payload information from resource model.
148 typedef uint8_t *CAPayload_t;
151 * URI for the OIC base.CA considers relative URI as the URI.
153 typedef char *CAURI_t;
156 * Token information for mapping the request and responses by resource model.
158 typedef char *CAToken_t;
161 * Socket types and error definitions.
163 #ifdef HAVE_WINSOCK2_H
164 # define OC_SOCKET_ERROR SOCKET_ERROR
165 # define OC_INVALID_SOCKET INVALID_SOCKET
166 typedef SOCKET CASocketFd_t;
167 #else // HAVE_WINSOCK2_H
168 # define OC_SOCKET_ERROR (-1)
169 # define OC_INVALID_SOCKET (-1)
170 typedef int CASocketFd_t;
174 * The following flags are the same as the equivalent OIC values in
175 * octypes.h, allowing direct copying with slight fixup.
176 * The CA layer should used the OC types when build allows that.
179 #define MAX_ADDR_STR_SIZE_CA (256)
182 * Max Address could be "coaps+tcp://[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:yyy.yyy.yyy.yyy]:xxxxx"
183 * Which is 65, +1 for null terminator => 66
184 * OCDevAddr (defined in OCTypes.h) must be the same
185 * as CAEndpoint_t (defined here)
187 #define MAX_ADDR_STR_SIZE_CA (66)
192 CA_DEFAULT_ADAPTER = 0,
194 // value zero indicates discovery
195 CA_ADAPTER_IP = (1 << 0), // IPv4 and IPv6, including 6LoWPAN
196 CA_ADAPTER_GATT_BTLE = (1 << 1), // GATT over Bluetooth LE
197 CA_ADAPTER_RFCOMM_BTEDR = (1 << 2), // RFCOMM over Bluetooth EDR
200 CA_ADAPTER_REMOTE_ACCESS = (1 << 3), // Remote Access over XMPP.
203 CA_ADAPTER_TCP = (1 << 4), // CoAP over TCP
204 CA_ADAPTER_NFC = (1 << 5), // NFC Adapter
206 CA_ALL_ADAPTERS = 0xffffffff
207 } CATransportAdapter_t;
211 CA_DEFAULT_FLAGS = 0,
213 // Insecure transport is the default (subject to change)
214 CA_SECURE = (1 << 4), // secure the transport path
215 // IPv4 & IPv6 autoselection is the default
216 CA_IPV6 = (1 << 5), // IP adapter only
217 CA_IPV4 = (1 << 6), // IP adapter only
218 // Indication that a message was received by multicast.
219 CA_MULTICAST = (1 << 7),
220 // Link-Local multicast is the default multicast scope for IPv6.
221 // These correspond in both value and position to the IPv6 address bits.
222 CA_SCOPE_INTERFACE = 0x1, // IPv6 Interface-Local scope
223 CA_SCOPE_LINK = 0x2, // IPv6 Link-Local scope (default)
224 CA_SCOPE_REALM = 0x3, // IPv6 Realm-Local scope
225 CA_SCOPE_ADMIN = 0x4, // IPv6 Admin-Local scope
226 CA_SCOPE_SITE = 0x5, // IPv6 Site-Local scope
227 CA_SCOPE_ORG = 0x8, // IPv6 Organization-Local scope
228 CA_SCOPE_GLOBAL = 0xE, // IPv6 Global scope
229 } CATransportFlags_t;
233 CA_DEFAULT_BT_FLAGS = 0,
234 // flags for BLE transport
235 CA_LE_ADV_DISABLE = 0x1, // disable BLE advertisement.
236 CA_LE_ADV_ENABLE = 0x2, // enable BLE advertisement.
237 CA_LE_SERVER_DISABLE = (1 << 4), // disable gatt server.
238 // flags for EDR transport
239 CA_EDR_SERVER_DISABLE = (1 << 7) // disable rfcomm server.
240 } CATransportBTFlags_t;
242 #define CA_IPFAMILY_MASK (CA_IPV6|CA_IPV4)
243 #define CA_SCOPE_MASK 0xf // mask scope bits above
246 * Information about the network status.
250 CA_INTERFACE_DOWN, /**< Connection is not available */
251 CA_INTERFACE_UP /**< Connection is Available */
255 * remoteEndpoint identity.
260 unsigned char id[CA_MAX_ENDPOINT_IDENTITY_LEN];
264 * Message Type for Base source code.
268 CA_MSG_CONFIRM = 0, /**< confirmable message (requires ACK/RST) */
269 CA_MSG_NONCONFIRM, /**< non-confirmable message (one-shot message) */
270 CA_MSG_ACKNOWLEDGE, /**< used to acknowledge confirmable messages */
271 CA_MSG_RESET /**< used to indicates not-interested or error (lack of context)in
276 * Allowed method to be used by resource model.
280 CA_GET = 1, /**< GET Method */
281 CA_POST, /**< POST Method */
282 CA_PUT, /**< PUT Method */
283 CA_DELETE /**< DELETE Method */
288 * it depends on defined size in libCoAP.
292 CA_BLOCK_SIZE_16_BYTE = 0, /**< 16byte */
293 CA_BLOCK_SIZE_32_BYTE = 1, /**< 32byte */
294 CA_BLOCK_SIZE_64_BYTE = 2, /**< 64byte */
295 CA_BLOCK_SIZE_128_BYTE = 3, /**< 128byte */
296 CA_BLOCK_SIZE_256_BYTE = 4, /**< 256byte */
297 CA_BLOCK_SIZE_512_BYTE = 5, /**< 512byte */
298 CA_BLOCK_SIZE_1024_BYTE = 6 /**< 1Kbyte */
302 * Endpoint information for connectivities.
303 * Must be identical to OCDevAddr.
307 CATransportAdapter_t adapter; // adapter type
308 CATransportFlags_t flags; // transport modifiers
309 uint16_t port; // for IP
310 char addr[MAX_ADDR_STR_SIZE_CA]; // address for all
311 uint32_t ifindex; // usually zero for default interface
312 char remoteId[CA_MAX_IDENTITY_SIZE]; // device ID of remote device
313 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
314 char routeData[MAX_ADDR_STR_SIZE_CA]; /**< GatewayId:ClientId of
320 * Endpoint information for secure messages.
324 CAEndpoint_t endpoint; /**< endpoint */
325 // TODO change name to deviceId
326 CARemoteId_t identity; /**< endpoint device uuid */
327 CARemoteId_t userId; /**< endpoint user uuid */
328 } CASecureEndpoint_t;
331 * Enums for CA return values.
335 /* Result code - START HERE */
336 CA_STATUS_OK = 0, /**< Success */
337 CA_STATUS_INVALID_PARAM, /**< Invalid Parameter */
338 CA_ADAPTER_NOT_ENABLED, /**< Adapter is not enabled */
339 CA_SERVER_STARTED_ALREADY, /**< Server is started already */
340 CA_SERVER_NOT_STARTED, /**< Server is not started */
341 CA_DESTINATION_NOT_REACHABLE, /**< Destination is not reachable */
342 CA_SOCKET_OPERATION_FAILED, /**< Socket operation failed */
343 CA_SEND_FAILED, /**< Send request failed */
344 CA_RECEIVE_FAILED, /**< Receive failed */
345 CA_MEMORY_ALLOC_FAILED, /**< Memory allocation failed */
346 CA_REQUEST_TIMEOUT, /**< Request is Timeout */
347 CA_DESTINATION_DISCONNECTED, /**< Destination is disconnected */
348 CA_NOT_SUPPORTED, /**< Not supported */
349 CA_STATUS_NOT_INITIALIZED, /**< Not Initialized*/
350 CA_DTLS_AUTHENTICATION_FAILURE, /**< Decryption error in DTLS */
351 CA_STATUS_FAILED =255 /**< Failure */
352 /* Result code - END HERE */
356 * Enums for CA Response values.
360 /* Response status code - START HERE */
361 CA_EMPTY = 0, /**< Empty */
362 CA_CREATED = 201, /**< Created */
363 CA_DELETED = 202, /**< Deleted */
364 CA_VALID = 203, /**< Valid */
365 CA_CHANGED = 204, /**< Changed */
366 CA_CONTENT = 205, /**< Content */
367 CA_CONTINUE = 231, /**< Continue */
368 CA_BAD_REQ = 400, /**< Bad Request */
369 CA_UNAUTHORIZED_REQ = 401, /**< Unauthorized Request */
370 CA_BAD_OPT = 402, /**< Bad Option */
371 CA_FORBIDDEN_REQ = 403, /**< Forbidden Request */
372 CA_NOT_FOUND = 404, /**< Not found */
373 CA_METHOD_NOT_ALLOWED = 405, /**< Method Not Allowed */
374 CA_NOT_ACCEPTABLE = 406, /**< Not Acceptable */
375 CA_REQUEST_ENTITY_INCOMPLETE = 408, /**< Request Entity Incomplete */
376 CA_REQUEST_ENTITY_TOO_LARGE = 413, /**< Request Entity Too Large */
377 CA_INTERNAL_SERVER_ERROR = 500, /**< Internal Server Error */
378 CA_NOT_IMPLEMENTED = 501, /**< Not Implenented */
379 CA_BAD_GATEWAY = 502, /**< Bad Gateway */
380 CA_SERVICE_UNAVAILABLE = 503, /**< Server Unavailable */
381 CA_RETRANSMIT_TIMEOUT = 504, /**< Retransmit timeout */
382 CA_PROXY_NOT_SUPPORTED = 505 /**< Proxy not enabled to service a request */
383 /* Response status code - END HERE */
384 } CAResponseResult_t;
387 * Data type whether the data is Request Message or Response Message.
388 * if there is some failure before send data on network.
389 * Type will be set as error type for error callback.
408 * Transport Protocol IDs for additional options.
412 CA_INVALID_ID = (1 << 0), /**< Invalid ID */
413 CA_COAP_ID = (1 << 1) /**< COAP ID */
414 } CATransportProtocolID_t;
417 * Adapter State to indicate the network changed notifications.
421 CA_ADAPTER_DISABLED, /**< Adapter is Disabled */
422 CA_ADAPTER_ENABLED /**< Adapter is Enabled */
426 * Format indicating which encoding has been used on the payload.
430 CA_FORMAT_UNDEFINED = 0, /**< Undefined enoding format */
431 CA_FORMAT_TEXT_PLAIN,
432 CA_FORMAT_APPLICATION_LINK_FORMAT,
433 CA_FORMAT_APPLICATION_XML,
434 CA_FORMAT_APPLICATION_OCTET_STREAM,
435 CA_FORMAT_APPLICATION_RDF_XML,
436 CA_FORMAT_APPLICATION_EXI,
437 CA_FORMAT_APPLICATION_JSON,
438 CA_FORMAT_APPLICATION_CBOR,
439 CA_FORMAT_UNSUPPORTED
443 * Header options structure to be filled.
445 * This structure is used to hold header information.
449 CATransportProtocolID_t protocolID; /**< Protocol ID of the Option */
450 uint16_t optionID; /**< The header option ID which will be
451 added to communication packets */
452 uint16_t optionLength; /**< Option Length **/
453 char optionData[CA_MAX_HEADER_OPTION_DATA_LENGTH]; /**< Optional data values**/
457 * Base Information received.
459 * This structure is used to hold request & response base information.
463 CAMessageType_t type; /**< Qos for the request */
464 #ifdef ROUTING_GATEWAY
465 bool skipRetransmission; /**< Will not attempt retransmission even if type is CONFIRM.
466 Required for packet forwarding */
468 uint16_t messageId; /**< Message id.
469 * if message id is zero, it will generated by CA inside.
470 * otherwise, you can use it */
471 CAToken_t token; /**< Token for CA */
472 uint8_t tokenLength; /**< token length */
473 CAHeaderOption_t *options; /** Header Options for the request */
474 uint8_t numOptions; /**< Number of Header options */
475 CAPayload_t payload; /**< payload of the request */
476 size_t payloadSize; /**< size in bytes of the payload */
477 CAPayloadFormat_t payloadFormat; /**< encoding format of the request payload */
478 CAPayloadFormat_t acceptFormat; /**< accept format for the response payload */
479 CAURI_t resourceUri; /**< Resource URI information **/
480 CARemoteId_t identity; /**< endpoint identity */
481 CADataType_t dataType; /**< data type */
482 CAConnectEvent_t event; /**< network request message / event type */
486 * Request Information to be sent.
488 * This structure is used to hold request information.
492 CAMethod_t method; /**< Name of the Method Allowed */
493 CAInfo_t info; /**< Information of the request. */
494 bool isMulticast; /**< is multicast request */
498 * Response information received.
500 * This structure is used to hold response information.
504 CAResponseResult_t result; /**< Result for response by resource model */
505 CAInfo_t info; /**< Information of the response */
510 * Error information from CA
511 * contains error code and message information.
513 * This structure holds error information.
517 CAResult_t result; /**< CA API request result */
518 CAInfo_t info; /**< message information such as token and payload data
519 helpful to identify the error */
523 * Hold global variables for CA layer. (also used by RI layer)
527 CASocketFd_t fd; /**< socket fd */
528 uint16_t port; /**< socket port */
531 #define HISTORYSIZE (4)
535 CATransportFlags_t flags;
537 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 */
599 CASocketFd_t netlinkFd; /**< netlink */
601 WSAEVENT shutdownEvent; /**< Event used to signal threads to stop */
604 int shutdownFds[2]; /**< fds used to signal threads to stop */
607 int selectTimeout; /**< in seconds */
608 int maxfd; /**< highest fd (for select) */
609 bool started; /**< the IP adapter has started */
610 bool terminate; /**< the IP adapter needs to stop */
611 bool ipv6enabled; /**< IPv6 enabled by OCInit flags */
612 bool ipv4enabled; /**< IPv4 enabled by OCInit flags */
613 bool dualstack; /**< IPv6 and IPv4 enabled */
615 LPFN_WSARECVMSG wsaRecvMsg; /**< Win32 function pointer to WSARecvMsg() */
618 struct networkmonitors
620 CAIfItem_t *ifItems; /**< current network interface index list */
621 size_t sizeIfItems; /**< size of network interface index array */
622 size_t numIfItems; /**< number of valid network interfaces */
628 CAHistory_t requestHistory; /**< filter IP family in requests */
633 * Hold global variables for TCP Adapter.
637 void *threadpool; /**< threadpool between Initialize and Start */
638 CASocket_t ipv4; /**< IPv4 accept socket */
639 CASocket_t ipv4s; /**< IPv4 accept socket secure */
640 CASocket_t ipv6; /**< IPv6 accept socket */
641 CASocket_t ipv6s; /**< IPv6 accept socket secure */
642 void *svrlist; /**< unicast IPv4 TCP server information*/
643 int selectTimeout; /**< in seconds */
644 int listenBacklog; /**< backlog counts*/
646 int shutdownFds[2]; /**< shutdown pipe */
648 int connectionFds[2]; /**< connection pipe */
649 int maxfd; /**< highest fd (for select) */
650 bool started; /**< the TCP adapter has started */
651 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;
659 extern CAGlobals_t caglobals;
663 CA_LOG_LEVEL_ALL = 1, // all logs.
664 CA_LOG_LEVEL_INFO, // debug level is disabled
668 * Callback function type for request delivery.
669 * @param[out] object Endpoint object from which the request is received.
670 * It contains endpoint address based on the connectivity type.
671 * @param[out] requestInfo Info for resource model to understand about the request.
673 typedef void (*CARequestCallback)(const CAEndpoint_t *object,
674 const CARequestInfo_t *requestInfo);
677 * Callback function type for response delivery.
678 * @param[out] object Endpoint object from which the response is received.
679 * @param[out] responseInfo Identifier which needs to be mapped with response.
681 typedef void (*CAResponseCallback)(const CAEndpoint_t *object,
682 const CAResponseInfo_t *responseInfo);
684 * Callback function type for error.
685 * @param[out] object remote device information.
686 * @param[out] errorInfo CA Error information.
688 typedef void (*CAErrorCallback)(const CAEndpoint_t *object,
689 const CAErrorInfo_t *errorInfo);
692 * Callback function type for network status changes delivery from CA common logic.
693 * @param[out] info Endpoint object from which the network status is changed.
694 * It contains endpoint address based on the connectivity type.
695 * @param[out] status Current network status info.
697 typedef void (*CANetworkMonitorCallback)(const CAEndpoint_t *info, CANetworkStatus_t status);
703 #endif // CA_COMMON_H_