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_102301"
63 #define BLE_CLIENT_TAG "OIC_CA_LE_CLIENT_1023"
64 #define BLE_SERVER_MCD_TAG "OIC_CA_LE_SER_MC_1023"
65 #define IP_SERVER_TAG "OIC_CA_IP_SERVER_1023"
66 #define TCP_SERVER_TAG "OIC_CA_TCP_SERVER_1023"
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_TOO_MANY_REQUESTS = 429, /**< Too Many Requests */
378 CA_INTERNAL_SERVER_ERROR = 500, /**< Internal Server Error */
379 CA_NOT_IMPLEMENTED = 501, /**< Not Implenented */
380 CA_BAD_GATEWAY = 502, /**< Bad Gateway */
381 CA_SERVICE_UNAVAILABLE = 503, /**< Server Unavailable */
382 CA_RETRANSMIT_TIMEOUT = 504, /**< Retransmit timeout */
383 CA_PROXY_NOT_SUPPORTED = 505 /**< Proxy not enabled to service a request */
384 /* Response status code - END HERE */
385 } CAResponseResult_t;
388 * Data type whether the data is Request Message or Response Message.
389 * if there is some failure before send data on network.
390 * Type will be set as error type for error callback.
409 * Transport Protocol IDs for additional options.
413 CA_INVALID_ID = (1 << 0), /**< Invalid ID */
414 CA_COAP_ID = (1 << 1) /**< COAP ID */
415 } CATransportProtocolID_t;
418 * Adapter State to indicate the network changed notifications.
422 CA_ADAPTER_DISABLED, /**< Adapter is Disabled */
423 CA_ADAPTER_ENABLED /**< Adapter is Enabled */
427 * Format indicating which encoding has been used on the payload.
431 CA_FORMAT_UNDEFINED = 0, /**< Undefined enoding format */
432 CA_FORMAT_TEXT_PLAIN,
433 CA_FORMAT_APPLICATION_LINK_FORMAT,
434 CA_FORMAT_APPLICATION_XML,
435 CA_FORMAT_APPLICATION_OCTET_STREAM,
436 CA_FORMAT_APPLICATION_RDF_XML,
437 CA_FORMAT_APPLICATION_EXI,
438 CA_FORMAT_APPLICATION_JSON,
439 CA_FORMAT_APPLICATION_CBOR,
440 CA_FORMAT_UNSUPPORTED
444 * Header options structure to be filled.
446 * This structure is used to hold header information.
450 CATransportProtocolID_t protocolID; /**< Protocol ID of the Option */
451 uint16_t optionID; /**< The header option ID which will be
452 added to communication packets */
453 uint16_t optionLength; /**< Option Length **/
454 char optionData[CA_MAX_HEADER_OPTION_DATA_LENGTH]; /**< Optional data values**/
458 * Base Information received.
460 * This structure is used to hold request & response base information.
464 CAMessageType_t type; /**< Qos for the request */
465 #ifdef ROUTING_GATEWAY
466 bool skipRetransmission; /**< Will not attempt retransmission even if type is CONFIRM.
467 Required for packet forwarding */
469 uint16_t messageId; /**< Message id.
470 * if message id is zero, it will generated by CA inside.
471 * otherwise, you can use it */
472 CAToken_t token; /**< Token for CA */
473 uint8_t tokenLength; /**< token length */
474 CAHeaderOption_t *options; /** Header Options for the request */
475 uint8_t numOptions; /**< Number of Header options */
476 CAPayload_t payload; /**< payload of the request */
477 size_t payloadSize; /**< size in bytes of the payload */
478 CAPayloadFormat_t payloadFormat; /**< encoding format of the request payload */
479 CAPayloadFormat_t acceptFormat; /**< accept format for the response payload */
480 CAURI_t resourceUri; /**< Resource URI information **/
481 CARemoteId_t identity; /**< endpoint identity */
482 CADataType_t dataType; /**< data type */
483 CAConnectEvent_t event; /**< network request message / event type */
487 * Request Information to be sent.
489 * This structure is used to hold request information.
493 CAMethod_t method; /**< Name of the Method Allowed */
494 CAInfo_t info; /**< Information of the request. */
495 bool isMulticast; /**< is multicast request */
499 * Response information received.
501 * This structure is used to hold response information.
505 CAResponseResult_t result; /**< Result for response by resource model */
506 CAInfo_t info; /**< Information of the response */
511 * Error information from CA
512 * contains error code and message information.
514 * This structure holds error information.
518 CAResult_t result; /**< CA API request result */
519 CAInfo_t info; /**< message information such as token and payload data
520 helpful to identify the error */
524 * Hold global variables for CA layer. (also used by RI layer)
528 CASocketFd_t fd; /**< socket fd */
529 uint16_t port; /**< socket port */
532 #define HISTORYSIZE (4)
536 CATransportFlags_t flags;
538 char token[CA_MAX_TOKEN_LEN];
545 CAHistoryItem_t items[HISTORYSIZE];
549 * Hold interface index for keeping track of comings and goings.
553 int32_t ifIndex; /**< network interface index */
557 * Hold the port number assigned from application.
558 * It will be used when creating a socket.
564 uint16_t u6; /**< unicast IPv6 socket port */
565 uint16_t u6s; /**< unicast IPv6 socket secure port */
566 uint16_t u4; /**< unicast IPv4 socket port */
567 uint16_t u4s; /**< unicast IPv4 socket secure port */
572 uint16_t u4; /**< unicast IPv4 socket port */
573 uint16_t u4s; /**< unicast IPv6 socket secure port */
574 uint16_t u6; /**< unicast IPv6 socket port */
575 uint16_t u6s; /**< unicast IPv6 socket secure port */
582 CATransportFlags_t clientFlags; /**< flag for client */
583 CATransportFlags_t serverFlags; /**< flag for server */
584 bool client; /**< client mode */
585 bool server; /**< server mode */
591 void *threadpool; /**< threadpool between Initialize and Start */
592 CASocket_t u6; /**< unicast IPv6 */
593 CASocket_t u6s; /**< unicast IPv6 secure */
594 CASocket_t u4; /**< unicast IPv4 */
595 CASocket_t u4s; /**< unicast IPv4 secure */
596 CASocket_t m6; /**< multicast IPv6 */
597 CASocket_t m6s; /**< multicast IPv6 secure */
598 CASocket_t m4; /**< multicast IPv4 */
599 CASocket_t m4s; /**< multicast IPv4 secure */
600 CASocketFd_t netlinkFd; /**< netlink */
602 WSAEVENT shutdownEvent; /**< Event used to signal threads to stop */
605 int shutdownFds[2]; /**< fds used to signal threads to stop */
608 int selectTimeout; /**< in seconds */
609 int maxfd; /**< highest fd (for select) */
610 bool started; /**< the IP adapter has started */
611 bool terminate; /**< the IP adapter needs to stop */
612 bool ipv6enabled; /**< IPv6 enabled by OCInit flags */
613 bool ipv4enabled; /**< IPv4 enabled by OCInit flags */
614 bool dualstack; /**< IPv6 and IPv4 enabled */
616 LPFN_WSARECVMSG wsaRecvMsg; /**< Win32 function pointer to WSARecvMsg() */
619 struct networkmonitors
621 CAIfItem_t *ifItems; /**< current network interface index list */
622 size_t sizeIfItems; /**< size of network interface index array */
623 size_t numIfItems; /**< number of valid network interfaces */
629 CAHistory_t requestHistory; /**< filter IP family in requests */
634 * Hold global variables for TCP Adapter.
638 void *threadpool; /**< threadpool between Initialize and Start */
639 CASocket_t ipv4; /**< IPv4 accept socket */
640 CASocket_t ipv4s; /**< IPv4 accept socket secure */
641 CASocket_t ipv6; /**< IPv6 accept socket */
642 CASocket_t ipv6s; /**< IPv6 accept socket secure */
643 void *svrlist; /**< unicast IPv4 TCP server information*/
644 int selectTimeout; /**< in seconds */
645 int listenBacklog; /**< backlog counts*/
647 int shutdownFds[2]; /**< shutdown pipe */
649 int connectionFds[2]; /**< connection pipe */
650 int maxfd; /**< highest fd (for select) */
651 bool started; /**< the TCP adapter has started */
652 bool terminate; /**< the TCP adapter needs to stop */
653 bool ipv4tcpenabled; /**< IPv4 TCP enabled by OCInit flags */
654 bool ipv6tcpenabled; /**< IPv6 TCP enabled by OCInit flags */
657 CATransportBTFlags_t bleFlags;
660 extern CAGlobals_t caglobals;
664 CA_LOG_LEVEL_ALL = 1, // all logs.
665 CA_LOG_LEVEL_INFO, // debug level is disabled
669 * Callback function type for request delivery.
670 * @param[out] object Endpoint object from which the request is received.
671 * It contains endpoint address based on the connectivity type.
672 * @param[out] requestInfo Info for resource model to understand about the request.
674 typedef void (*CARequestCallback)(const CAEndpoint_t *object,
675 const CARequestInfo_t *requestInfo);
678 * Callback function type for response delivery.
679 * @param[out] object Endpoint object from which the response is received.
680 * @param[out] responseInfo Identifier which needs to be mapped with response.
682 typedef void (*CAResponseCallback)(const CAEndpoint_t *object,
683 const CAResponseInfo_t *responseInfo);
685 * Callback function type for error.
686 * @param[out] object remote device information.
687 * @param[out] errorInfo CA Error information.
689 typedef void (*CAErrorCallback)(const CAEndpoint_t *object,
690 const CAErrorInfo_t *errorInfo);
693 * Callback function type for network status changes delivery from CA common logic.
694 * @param[out] info Endpoint object from which the network status is changed.
695 * It contains endpoint address based on the connectivity type.
696 * @param[out] status Current network status info.
698 typedef void (*CANetworkMonitorCallback)(const CAEndpoint_t *info, CANetworkStatus_t status);
704 #endif // CA_COMMON_H_