cacommon.h 24.8 KB
Newer Older
1
/* ****************************************************************
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * Copyright 2014 Samsung Electronics All Rights Reserved.
 *
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************/

/**
22
 * @file cacommon.h
23
 * This file contains the common data structures between Resource , CA and adapters
24
 */
25

26 27
#ifndef CA_COMMON_H_
#define CA_COMMON_H_
28

29 30
#include "iotivity_config.h"

31
#include <limits.h>
32 33
#include <stdint.h>
#include <stdlib.h>
34
#include <stdbool.h>
35

36 37 38
#ifdef HAVE_SYS_POLL_H
#include <sys/poll.h>
#endif
39

40
#ifdef HAVE_WINSOCK2_H
41
#include <winsock2.h>
42
#include <mswsock.h>
43 44
#endif

45 46 47 48 49
#ifdef __cplusplus
extern "C"
{
#endif

50 51 52
/**
 * TAG of Analyzer log.
 */
53
#define ANALYZER_TAG "OIC_ANALYZER"
54

55
/**
56
 * IP address Length.
57 58 59
 */
#define CA_IPADDR_SIZE 16

60
/**
61
 * Remote Access jabber ID length.
62 63 64
 */
#define CA_RAJABBERID_SIZE 256

65
/**
66
 * Mac address length for BT port.
67 68 69 70
 */
#define CA_MACADDR_SIZE 18

/**
71
 * Max header options data length.
72
 */
73
#define CA_MAX_HEADER_OPTION_DATA_LENGTH 1024
74

75
/**
76
* Max token length.
77 78 79
*/
#define CA_MAX_TOKEN_LEN (8)

80
/**
81
 * Max URI length.
82
 */
Mats Wichmann's avatar
Mats Wichmann committed
83
#define CA_MAX_URI_LENGTH 512 /* maximum size of URI */
84

85
/**
86
 * Max PDU length supported.
87
 */
Mats Wichmann's avatar
Mats Wichmann committed
88
#define COAP_MAX_PDU_SIZE           1400 /* maximum size of a CoAP PDU */
89

90 91 92 93
#ifdef WITH_BWT
#define CA_DEFAULT_BLOCK_SIZE       CA_BLOCK_SIZE_1024_BYTE
#endif

Sachin Agrawal's avatar
Sachin Agrawal committed
94
/**
95
 *Maximum length of the remoteEndpoint identity.
Sachin Agrawal's avatar
Sachin Agrawal committed
96
 */
97 98 99 100 101 102
#define CA_MAX_ENDPOINT_IDENTITY_LEN  CA_MAX_IDENTITY_SIZE

/**
 * Max identity size.
 */
#define CA_MAX_IDENTITY_SIZE (37)
Sachin Agrawal's avatar
Sachin Agrawal committed
103

104
/**
105
 * option types - the highest option number 63.
106
 */
107 108 109
#define CA_OPTION_IF_MATCH 1
#define CA_OPTION_ETAG 4
#define CA_OPTION_IF_NONE_MATCH 5
110
#define CA_OPTION_OBSERVE 6
111 112 113 114 115 116 117 118
#define CA_OPTION_LOCATION_PATH 8
#define CA_OPTION_URI_PATH 11
#define CA_OPTION_CONTENT_FORMAT 12
#define CA_OPTION_CONTENT_TYPE COAP_OPTION_CONTENT_FORMAT
#define CA_OPTION_MAXAGE 14
#define CA_OPTION_URI_QUERY 15
#define CA_OPTION_ACCEPT 17
#define CA_OPTION_LOCATION_QUERY 20
119

Kevin Kane's avatar
Kevin Kane committed
120 121 122 123 124 125 126 127 128 129 130
/**
 * @def UUID_PREFIX
 * @brief uuid prefix in certificate subject field
 */
#define UUID_PREFIX "uuid:"

/**
 * @def SUBJECT_PREFIX
 * @brief prefix for specifying part of a cert's subject for a particular uuid
 */
#define SUBJECT_PREFIX "CN=" UUID_PREFIX
131

Ziran Sun's avatar
Ziran Sun committed
132 133 134 135 136 137 138
/**
* TODO: Move these COAP defines to CoAP lib once approved.
*/
#define COAP_MEDIATYPE_APPLICATION_VND_OCF_CBOR 10000 // application/vnd.ocf+cbor
#define CA_OPTION_ACCEPT_VERSION 2049
#define CA_OPTION_CONTENT_VERSION 2053

139 140
// The Accept Version and Content-Format Version for OCF 1.0.0 (0b0000 1000 0000 0000).
#define DEFAULT_VERSION_VALUE 2048
Ziran Sun's avatar
Ziran Sun committed
141

142 143 144 145 146 147 148 149 150 151 152 153 154
/**
 * Option numbers for Signaling messages are specific to the message code.
 * They do not share the number space with CoAP options for request/response
 * messages or with Signaling messages using other codes.
 */
#define CA_OPTION_SERVER_NAME_SETTING 1    /**< Capability and Settings messages, code=7.01 */
#define CA_OPTION_MAX_MESSAGE_SIZE 2       /**< Capability and Settings messages, code=7.01 */
#define CA_OPTION_BLOCK_WISE_TRANSFER 4    /**< Capability and Settings messages, code=7.01 */
#define CA_OPTION_CUSTODY 2                /**< Ping and Pong Messages, code=7.02 */
#define CA_OPTION_BAD_SERVER_NAME 2        /**< Release Messages, code=7.04 */
#define CA_OPTION_ALTERNATE_ADDRESS 4      /**< Abort Messages, code=7.05 */
#define CA_OPTION_HOLD_OFF 6               /**< Abort Messages, code=7.05 */

155
/**
156
 * Payload information from resource model.
157
 */
158
typedef uint8_t *CAPayload_t;
159 160

/**
161
 * URI for the OIC base.CA considers relative URI as the URI.
162
 */
163
typedef char *CAURI_t;
164 165

/**
166
 * Token information for mapping the request and responses by resource model.
167
 */
168
typedef char *CAToken_t;
169

170
/*
171
 * Socket types and error definitions.
172 173 174 175
 */
#ifdef HAVE_WINSOCK2_H
# define OC_SOCKET_ERROR      SOCKET_ERROR
# define OC_INVALID_SOCKET    INVALID_SOCKET
Daniel Ferguson's avatar
Daniel Ferguson committed
176
typedef SOCKET CASocketFd_t;
177 178 179 180 181 182
#else // HAVE_WINSOCK2_H
# define OC_SOCKET_ERROR      (-1)
# define OC_INVALID_SOCKET    (-1)
typedef int    CASocketFd_t;
#endif

183 184
/*
 * The following flags are the same as the equivalent OIC values in
185 186 187
 * octypes.h, allowing direct copying with slight fixup.
 * The CA layer should used the OC types when build allows that.
 */
188 189 190
#ifdef RA_ADAPTER
#define MAX_ADDR_STR_SIZE_CA (256)
#else
191
/*
Jaewook Jung's avatar
Jaewook Jung committed
192 193
 * Max Address could be "coaps+tcp://[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:yyy.yyy.yyy.yyy]:xxxxx"
 * Which is 65, +1 for null terminator => 66
194 195 196
 * OCDevAddr (defined in OCTypes.h) must be the same
 * as CAEndpoint_t (defined here)
 */
Jaewook Jung's avatar
Jaewook Jung committed
197
#define MAX_ADDR_STR_SIZE_CA (66)
198
#endif
199 200 201 202 203 204

typedef enum
{
    CA_DEFAULT_ADAPTER = 0,

    // value zero indicates discovery
205 206 207 208
    CA_ADAPTER_IP            = (1 << 0),   // IPv4 and IPv6, including 6LoWPAN
    CA_ADAPTER_GATT_BTLE     = (1 << 1),   // GATT over Bluetooth LE
    CA_ADAPTER_RFCOMM_BTEDR  = (1 << 2),   // RFCOMM over Bluetooth EDR

209
#ifdef RA_ADAPTER
210
    CA_ADAPTER_REMOTE_ACCESS = (1 << 3),   // Remote Access over XMPP.
211
#endif
212

Ashok Channa's avatar
Ashok Channa committed
213 214 215
    CA_ADAPTER_TCP           = (1 << 4),   // CoAP over TCP
    CA_ADAPTER_NFC           = (1 << 5),   // NFC Adapter

216
    CA_ALL_ADAPTERS          = 0xffffffff
217 218
} CATransportAdapter_t;

219 220
typedef enum
{
221 222 223 224 225 226 227
    CA_DEFAULT_FLAGS = 0,

    // Insecure transport is the default (subject to change)
    CA_SECURE          = (1 << 4),   // secure the transport path
    // IPv4 & IPv6 autoselection is the default
    CA_IPV6            = (1 << 5),   // IP adapter only
    CA_IPV4            = (1 << 6),   // IP adapter only
John Light's avatar
John Light committed
228 229
    // Indication that a message was received by multicast.
    CA_MULTICAST       = (1 << 7),
230 231 232 233 234 235 236 237 238 239
    // Link-Local multicast is the default multicast scope for IPv6.
    // These correspond in both value and position to the IPv6 address bits.
    CA_SCOPE_INTERFACE = 0x1, // IPv6 Interface-Local scope
    CA_SCOPE_LINK      = 0x2, // IPv6 Link-Local scope (default)
    CA_SCOPE_REALM     = 0x3, // IPv6 Realm-Local scope
    CA_SCOPE_ADMIN     = 0x4, // IPv6 Admin-Local scope
    CA_SCOPE_SITE      = 0x5, // IPv6 Site-Local scope
    CA_SCOPE_ORG       = 0x8, // IPv6 Organization-Local scope
    CA_SCOPE_GLOBAL    = 0xE, // IPv6 Global scope
} CATransportFlags_t;
240

241 242 243 244
typedef enum
{
    CA_DEFAULT_BT_FLAGS = 0,
    // flags for BLE transport
245 246 247
    CA_LE_ADV_DISABLE   = 0x1,   // disable BLE advertisement.
    CA_LE_ADV_ENABLE    = 0x2,   // enable BLE advertisement.
    CA_LE_SERVER_DISABLE = (1 << 4),   // disable gatt server.
248 249 250 251
    // flags for EDR transport
    CA_EDR_SERVER_DISABLE = (1 << 7)
} CATransportBTFlags_t;

John Light's avatar
John Light committed
252 253 254
#define CA_IPFAMILY_MASK (CA_IPV6|CA_IPV4)
#define CA_SCOPE_MASK 0xf     // mask scope bits above

255
/**
256
 * Information about the network status.
257 258 259
 */
typedef enum
{
260 261
    CA_INTERFACE_DOWN,   /**< Connection is not available */
    CA_INTERFACE_UP    /**< Connection is Available */
262 263
} CANetworkStatus_t;

Sachin Agrawal's avatar
Sachin Agrawal committed
264
/*
265
 * remoteEndpoint identity.
Sachin Agrawal's avatar
Sachin Agrawal committed
266 267 268 269 270
 */
typedef struct
{
    uint16_t id_length;
    unsigned char id[CA_MAX_ENDPOINT_IDENTITY_LEN];
271
} CARemoteId_t;
Sachin Agrawal's avatar
Sachin Agrawal committed
272

273
/**
274
 * Message Type for Base source code.
275 276 277
 */
typedef enum
{
278 279 280
    CA_MSG_CONFIRM = 0,  /**< confirmable message (requires ACK/RST) */
    CA_MSG_NONCONFIRM,   /**< non-confirmable message (one-shot message) */
    CA_MSG_ACKNOWLEDGE,  /**< used to acknowledge confirmable messages */
281 282
    CA_MSG_RESET         /**< used to indicates not-interested or error (lack of context)in
                                                  received messages */
283
} CAMessageType_t;
284 285

/**
286
 * Allowed method to be used by resource model.
287 288 289
 */
typedef enum
{
290 291 292 293
    CA_GET = 1, /**< GET Method  */
    CA_POST,    /**< POST Method */
    CA_PUT,     /**< PUT Method */
    CA_DELETE   /**< DELETE Method */
294 295 296
} CAMethod_t;

/**
297
 * block size.
298 299 300 301 302 303 304 305 306 307 308 309 310 311
 * it depends on defined size in libCoAP.
 */
typedef enum
{
    CA_BLOCK_SIZE_16_BYTE = 0,    /**< 16byte */
    CA_BLOCK_SIZE_32_BYTE = 1,    /**< 32byte */
    CA_BLOCK_SIZE_64_BYTE = 2,    /**< 64byte */
    CA_BLOCK_SIZE_128_BYTE = 3,   /**< 128byte */
    CA_BLOCK_SIZE_256_BYTE = 4,   /**< 256byte */
    CA_BLOCK_SIZE_512_BYTE = 5,   /**< 512byte */
    CA_BLOCK_SIZE_1024_BYTE = 6     /**< 1Kbyte */
} CABlockSize_t;

/**
312
 * Endpoint information for connectivities.
313
 * Must be identical to OCDevAddr.
314 315 316
 */
typedef struct
{
317 318
    CATransportAdapter_t    adapter;    // adapter type
    CATransportFlags_t      flags;      // transport modifiers
319
    uint16_t                port;       // for IP
320
    char                    addr[MAX_ADDR_STR_SIZE_CA]; // address for all
321
    uint32_t                ifindex;    // usually zero for default interface
322
    char                    remoteId[CA_MAX_IDENTITY_SIZE]; // device ID of remote device
323 324 325 326
#if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
    char                    routeData[MAX_ADDR_STR_SIZE_CA]; /**< GatewayId:ClientId of
                                                                    destination. **/
#endif
327
} CAEndpoint_t;
328

ol.beketov's avatar
ol.beketov committed
329
#define CA_SECURE_ENDPOINT_PUBLIC_KEY_MAX_LENGTH    (512)
330

331
/**
332
 * Endpoint information for secure messages.
333 334 335 336
 */
typedef struct
{
    CAEndpoint_t endpoint;      /**< endpoint */
Andrii Shtompel's avatar
Andrii Shtompel committed
337 338 339
    // TODO change name to deviceId
    CARemoteId_t identity;      /**< endpoint device uuid */
    CARemoteId_t userId;        /**< endpoint user uuid */
340
    uint32_t attributes;
341 342
    uint8_t publicKey[CA_SECURE_ENDPOINT_PUBLIC_KEY_MAX_LENGTH]; /**< Peer's DER-encoded public key (if using certificate) */
    size_t publicKeyLength;     /**< Length of publicKey; zero if not using certificate */
343 344
} CASecureEndpoint_t;

345 346 347
/**
 * Endpoint used for security administration - a special type of identity that
 * bypasses Access Control Entry checks for SVR resources, while the device is
348
 * not ready for normal operation yet.
349 350 351
 */
#define CA_SECURE_ENDPOINT_ATTRIBUTE_ADMINISTRATOR  0x1

352
/**
353
 * Enums for CA return values.
354 355 356
 */
typedef enum
{
357
    /* Result code - START HERE */
358 359 360 361
    CA_STATUS_OK = 0,               /**< Success */
    CA_STATUS_INVALID_PARAM,        /**< Invalid Parameter */
    CA_ADAPTER_NOT_ENABLED,         /**< Adapter is not enabled */
    CA_SERVER_STARTED_ALREADY,      /**< Server is started already */
362
    CA_SERVER_NOT_STARTED,          /**< Server is not started */
363 364 365
    CA_DESTINATION_NOT_REACHABLE,   /**< Destination is not reachable */
    CA_SOCKET_OPERATION_FAILED,     /**< Socket operation failed */
    CA_SEND_FAILED,                 /**< Send request failed */
366
    CA_RECEIVE_FAILED,              /**< Receive failed */
367 368 369
    CA_MEMORY_ALLOC_FAILED,         /**< Memory allocation failed */
    CA_REQUEST_TIMEOUT,             /**< Request is Timeout */
    CA_DESTINATION_DISCONNECTED,    /**< Destination is disconnected */
370
    CA_NOT_SUPPORTED,               /**< Not supported */
371
    CA_STATUS_NOT_INITIALIZED,      /**< Not Initialized*/
372
    CA_DTLS_AUTHENTICATION_FAILURE, /**< Decryption error in DTLS */
373 374
    CA_CONTINUE_OPERATION,          /**< Error happens but current operation should continue */
    CA_HANDLE_ERROR_OTHER_MODULE,   /**< Error happens but it should be handled in other module */
375
    CA_STATUS_NOT_FOUND,            /**< Not Found*/
376
    CA_STATUS_FAILED =255           /**< Failure */
377
    /* Result code - END HERE */
378 379 380
} CAResult_t;

/**
381
 * Enums for CA Response values.
382 383 384
 */
typedef enum
{
jihwan.seo's avatar
jihwan.seo committed
385
    /* Response status code - START HERE */
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
    CA_EMPTY = 0,                           /**< Empty */
    CA_CREATED = 201,                       /**< Created */
    CA_DELETED = 202,                       /**< Deleted */
    CA_VALID = 203,                         /**< Valid */
    CA_CHANGED = 204,                       /**< Changed */
    CA_CONTENT = 205,                       /**< Content */
    CA_CONTINUE = 231,                      /**< Continue */
    CA_BAD_REQ = 400,                       /**< Bad Request */
    CA_UNAUTHORIZED_REQ = 401,              /**< Unauthorized Request */
    CA_BAD_OPT = 402,                       /**< Bad Option */
    CA_FORBIDDEN_REQ = 403,                 /**< Forbidden Request */
    CA_NOT_FOUND = 404,                     /**< Not found */
    CA_METHOD_NOT_ALLOWED = 405,            /**< Method Not Allowed */
    CA_NOT_ACCEPTABLE = 406,                /**< Not Acceptable */
    CA_REQUEST_ENTITY_INCOMPLETE = 408,     /**< Request Entity Incomplete */
    CA_REQUEST_ENTITY_TOO_LARGE = 413,      /**< Request Entity Too Large */
    CA_INTERNAL_SERVER_ERROR = 500,         /**< Internal Server Error */
403
    CA_BAD_GATEWAY = 502,
404
    CA_SERVICE_UNAVAILABLE = 503,           /**< Server Unavailable */
405 406
    CA_RETRANSMIT_TIMEOUT = 504,            /**< Retransmit timeout */
    CA_PROXY_NOT_SUPPORTED = 505            /**< Proxy not enabled to service a request */
407
    /* Response status code - END HERE */
408 409
} CAResponseResult_t;

410 411 412 413 414 415 416 417 418 419 420 421 422 423
/**
 * Enums for CA Signaling codes.
 */
typedef enum
{
    /* Signaling code - START HERE */
    CA_CSM = 701,                           /**< Capability and Settings messages */
    CA_PING = 702,                          /**< Ping messages */
    CA_PONG = 703,                          /**< Pong messages */
    CA_RELEASE = 704,                       /**< Release messages */
    CA_ABORT = 705,                         /**< Abort messages */
    /* Signaling code - END HERE */
} CASignalingCode_t;

424
/**
425 426 427 428 429 430 431 432 433
 * Data type whether the data is Request Message or Response Message.
 * if there is some failure before send data on network.
 * Type will be set as error type for error callback.
 */
typedef enum
{
    CA_REQUEST_DATA = 1,
    CA_RESPONSE_DATA,
    CA_ERROR_DATA,
434 435
    CA_RESPONSE_FOR_RES,
    CA_SIGNALING_DATA
436 437 438 439
} CADataType_t;

/**
 * Transport Protocol IDs for additional options.
440 441 442
 */
typedef enum
{
443 444
    CA_INVALID_ID = (1 << 0),   /**< Invalid ID */
    CA_COAP_ID = (1 << 1)       /**< COAP ID */
445 446
} CATransportProtocolID_t;

447
/**
448
 * Adapter State to indicate the network changed notifications.
449 450 451
 */
typedef enum
{
452 453
    CA_ADAPTER_DISABLED,   /**< Adapter is Disabled */
    CA_ADAPTER_ENABLED     /**< Adapter is Enabled */
454 455
} CAAdapterState_t;

456 457 458 459 460
/**
 * Format indicating which encoding has been used on the payload.
 */
typedef enum
{
461 462 463 464 465 466 467 468 469
    CA_FORMAT_UNDEFINED = 0,            /**< Undefined enoding format */
    CA_FORMAT_TEXT_PLAIN,
    CA_FORMAT_APPLICATION_LINK_FORMAT,
    CA_FORMAT_APPLICATION_XML,
    CA_FORMAT_APPLICATION_OCTET_STREAM,
    CA_FORMAT_APPLICATION_RDF_XML,
    CA_FORMAT_APPLICATION_EXI,
    CA_FORMAT_APPLICATION_JSON,
    CA_FORMAT_APPLICATION_CBOR,
Ziran Sun's avatar
Ziran Sun committed
470
    CA_FORMAT_APPLICATION_VND_OCF_CBOR,
471
    CA_FORMAT_UNSUPPORTED
472 473
} CAPayloadFormat_t;

474 475 476 477 478 479 480 481 482
/**
 * Option ID of header option. The values match CoAP option types in pdu.h.
 */
typedef enum
{
    CA_HEADER_OPTION_ID_LOCATION_PATH = 8,
    CA_HEADER_OPTION_ID_LOCATION_QUERY = 20
} CAHeaderOptionId_t;

483
/**
484
 * Header options structure to be filled.
485 486 487 488 489
 *
 * This structure is used to hold header information.
 */
typedef struct
{
490 491 492 493
    CATransportProtocolID_t protocolID;                     /**< Protocol ID of the Option */
    uint16_t optionID;                                      /**< The header option ID which will be
                                                            added to communication packets */
    uint16_t optionLength;                                  /**< Option Length **/
494
    char optionData[CA_MAX_HEADER_OPTION_DATA_LENGTH];      /**< Optional data values**/
495 496 497
} CAHeaderOption_t;

/**
498
 * Base Information received.
499
 *
500
 * This structure is used to hold request & response base information.
501 502 503
 */
typedef struct
{
504
    CAMessageType_t type;       /**< Qos for the request */
505 506 507 508
#ifdef ROUTING_GATEWAY
    bool skipRetransmission;    /**< Will not attempt retransmission even if type is CONFIRM.
                                     Required for packet forwarding */
#endif
509 510 511 512
    uint16_t messageId;         /**< Message id.
                                 * if message id is zero, it will generated by CA inside.
                                 * otherwise, you can use it */
    CAToken_t token;            /**< Token for CA */
513
    uint8_t tokenLength;        /**< token length */
514 515 516
    CAHeaderOption_t *options;  /** Header Options for the request */
    uint8_t numOptions;         /**< Number of Header options */
    CAPayload_t payload;        /**< payload of the request  */
517
    size_t payloadSize;         /**< size in bytes of the payload */
518 519
    CAPayloadFormat_t payloadFormat;    /**< encoding format of the request payload */
    CAPayloadFormat_t acceptFormat;     /**< accept format for the response payload */
Ziran Sun's avatar
Ziran Sun committed
520 521
    uint16_t payloadVersion;    /**< version of the payload */
    uint16_t acceptVersion;     /**< expected version for the response payload */
522
    CAURI_t resourceUri;        /**< Resource URI information **/
523
    CARemoteId_t identity;      /**< endpoint identity */
524
    CADataType_t dataType;      /**< data type */
525 526
} CAInfo_t;

527
/**
528
 * Request Information to be sent.
529
 *
530
 * This structure is used to hold request information.
531
 */
532 533
typedef struct
{
534 535
    CAMethod_t method;  /**< Name of the Method Allowed */
    CAInfo_t info;      /**< Information of the request. */
536
    bool isMulticast;   /**< is multicast request */
537 538 539
} CARequestInfo_t;

/**
540
 * Response information received.
541
 *
542
 * This structure is used to hold response information.
543 544 545
 */
typedef struct
{
546
    CAResponseResult_t result;  /**< Result for response by resource model */
547
    CAInfo_t info;              /**< Information of the response */
Omkar Hegde's avatar
Omkar Hegde committed
548
    bool isMulticast;
549 550
} CAResponseInfo_t;

jnashok's avatar
jnashok committed
551
/**
552
 * Error information from CA
553
 *        contains error code and message information.
jnashok's avatar
jnashok committed
554
 *
555
 * This structure holds error information.
jnashok's avatar
jnashok committed
556 557 558 559 560 561 562 563
 */
typedef struct
{
    CAResult_t result;  /**< CA API request result  */
    CAInfo_t info;      /**< message information such as token and payload data
                             helpful to identify the error */
} CAErrorInfo_t;

564 565 566 567 568 569 570 571 572 573 574
/**
 * Signaling information received.
 *
 * This structure is used to hold signaling information.
 */
typedef struct
{
    CASignalingCode_t code;
    CAInfo_t info;              /**< Information of the signaling */
} CASignalingInfo_t;

575
/**
576
 * Hold global variables for CA layer. (also used by RI layer)
577 578 579
 */
typedef struct
{
580 581
    CASocketFd_t fd;    /**< socket fd */
    uint16_t port;      /**< socket port */
John Light's avatar
John Light committed
582 583
} CASocket_t;

584 585 586 587 588 589
#define HISTORYSIZE (4)

typedef struct
{
    CATransportFlags_t flags;
    uint16_t messageId;
590 591
    char token[CA_MAX_TOKEN_LEN];
    uint8_t tokenLength;
592
    uint32_t ifindex;
593 594 595 596 597 598 599 600
} CAHistoryItem_t;

typedef struct
{
    int nextIndex;
    CAHistoryItem_t items[HISTORYSIZE];
} CAHistory_t;

601
/**
602
 * Hold interface index for keeping track of comings and goings.
603
 */
John Light's avatar
John Light committed
604 605
typedef struct
{
606
    int32_t ifIndex; /**< network interface index */
607 608
} CAIfItem_t;

609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
/**
 * Hold the port number assigned from application.
 * It will be used when creating a socket.
 */
typedef struct
{
    struct udpports
    {
        uint16_t u6;    /**< unicast IPv6 socket port */
        uint16_t u6s;   /**< unicast IPv6 socket secure port */
        uint16_t u4;    /**< unicast IPv4 socket port */
        uint16_t u4s;   /**< unicast IPv4 socket secure port */
    } udp;
#ifdef TCP_ADAPTER
    struct tcpports
    {
        uint16_t u4;    /**< unicast IPv4 socket port */
626
        uint16_t u4s;   /**< unicast IPv6 socket secure port */
627
        uint16_t u6;    /**< unicast IPv6 socket port */
628
        uint16_t u6s;   /**< unicast IPv6 socket secure port */
629 630 631 632
    } tcp;
#endif
} CAPorts_t;

633 634 635 636 637 638
typedef struct
{
    CATransportFlags_t clientFlags; /**< flag for client */
    CATransportFlags_t serverFlags; /**< flag for server */
    bool client; /**< client mode */
    bool server; /**< server mode */
John Light's avatar
John Light committed
639

640 641
    CAPorts_t ports;

John Light's avatar
John Light committed
642 643
    struct sockets
    {
644 645 646 647 648 649 650 651 652
        void *threadpool;           /**< threadpool between Initialize and Start */
        CASocket_t u6;              /**< unicast   IPv6 */
        CASocket_t u6s;             /**< unicast   IPv6 secure */
        CASocket_t u4;              /**< unicast   IPv4 */
        CASocket_t u4s;             /**< unicast   IPv4 secure */
        CASocket_t m6;              /**< multicast IPv6 */
        CASocket_t m6s;             /**< multicast IPv6 secure */
        CASocket_t m4;              /**< multicast IPv4 */
        CASocket_t m4s;             /**< multicast IPv4 secure */
653
#if defined(_WIN32)
654
        WSAEVENT addressChangeEvent;/**< Event used to signal address changes */
655 656
        WSAEVENT shutdownEvent;     /**< Event used to signal threads to stop */
#else
657
        int netlinkFd;              /**< netlink */
658
        int shutdownFds[2];         /**< fds used to signal threads to stop */
659
        CASocketFd_t maxfd;         /**< highest fd (for select) */
660
#endif
661 662 663 664 665 666 667 668 669
        int selectTimeout;          /**< in seconds */
        bool started;               /**< the IP adapter has started */
        bool terminate;             /**< the IP adapter needs to stop */
        bool ipv6enabled;           /**< IPv6 enabled by OCInit flags */
        bool ipv4enabled;           /**< IPv4 enabled by OCInit flags */
        bool dualstack;             /**< IPv6 and IPv4 enabled */
#if defined (_WIN32)
        LPFN_WSARECVMSG wsaRecvMsg; /**< Win32 function pointer to WSARecvMsg() */
#endif
670 671 672

        struct networkmonitors
        {
673 674 675
            CAIfItem_t *ifItems; /**< current network interface index list */
            size_t sizeIfItems;  /**< size of network interface index array */
            size_t numIfItems;   /**< number of valid network interfaces */
676
        } nm;
John Light's avatar
John Light committed
677 678 679 680
    } ip;

    struct calayer
    {
681
        CAHistory_t requestHistory;  /**< filter IP family in requests */
John Light's avatar
John Light committed
682
    } ca;
683 684 685 686 687 688 689 690

#ifdef TCP_ADAPTER
    /**
     * Hold global variables for TCP Adapter.
     */
    struct tcpsockets
    {
        void *threadpool;       /**< threadpool between Initialize and Start */
691
        CASocket_t ipv4;        /**< IPv4 accept socket */
692
        CASocket_t ipv4s;       /**< IPv4 accept socket secure */
693
        CASocket_t ipv6;        /**< IPv6 accept socket */
694
        CASocket_t ipv6s;       /**< IPv6 accept socket secure */
695 696
        int selectTimeout;      /**< in seconds */
        int listenBacklog;      /**< backlog counts*/
Dave Thaler's avatar
Dave Thaler committed
697
#if defined(_WIN32)
698
        WSAEVENT updateEvent;   /**< Event used to signal thread to stop or update the FD list */
Dave Thaler's avatar
Dave Thaler committed
699
#else
700
        int shutdownFds[2];     /**< shutdown pipe */
701
        int connectionFds[2];   /**< connection pipe */
702
        CASocketFd_t maxfd;     /**< highest fd (for select) */
Dave Thaler's avatar
Dave Thaler committed
703
#endif
704
        bool started;           /**< the TCP adapter has started */
705
        volatile bool terminate;/**< the TCP adapter needs to stop */
706
        bool ipv4tcpenabled;    /**< IPv4 TCP enabled by OCInit flags */
707
        bool ipv6tcpenabled;    /**< IPv6 TCP enabled by OCInit flags */
708 709
    } tcp;
#endif
710
    CATransportBTFlags_t bleFlags;   /**< flags related BLE transport */
711 712 713 714
} CAGlobals_t;

extern CAGlobals_t caglobals;

715 716 717 718 719 720
typedef enum
{
    CA_LOG_LEVEL_ALL = 1,             // all logs.
    CA_LOG_LEVEL_INFO,                // debug level is disabled.
} CAUtilLogLevel_t;

721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
/**
 * Callback function type for request delivery.
 * @param[out]   object       Endpoint object from which the request is received.
 *                            It contains endpoint address based on the connectivity type.
 * @param[out]   requestInfo  Info for resource model to understand about the request.
 */
typedef void (*CARequestCallback)(const CAEndpoint_t *object,
                                  const CARequestInfo_t *requestInfo);

/**
 * Callback function type for response delivery.
 * @param[out]   object           Endpoint object from which the response is received.
 * @param[out]   responseInfo     Identifier which needs to be mapped with response.
 */
typedef void (*CAResponseCallback)(const CAEndpoint_t *object,
                                   const CAResponseInfo_t *responseInfo);
/**
 * Callback function type for error.
 * @param[out]   object           remote device information.
 * @param[out]   errorInfo        CA Error information.
 */
typedef void (*CAErrorCallback)(const CAEndpoint_t *object,
                                const CAErrorInfo_t *errorInfo);

745 746 747 748 749 750 751 752
/**
 * Callback function type for error.
 * @param[out]   object           remote device information.
 * @param[out]   result           error information.
 */
typedef CAResult_t (*CAHandshakeErrorCallback)(const CAEndpoint_t *object,
                                               const CAErrorInfo_t *errorInfo);

753 754 755 756 757 758 759 760
/**
 * Callback function type for network status changes delivery from CA common logic.
 * @param[out]   info       Endpoint object from which the network status is changed.
 *                          It contains endpoint address based on the connectivity type.
 * @param[out]   status     Current network status info.
 */
typedef void (*CANetworkMonitorCallback)(const CAEndpoint_t *info, CANetworkStatus_t status);

761 762 763 764
#ifdef __cplusplus
} /* extern "C" */
#endif

765
#endif // CA_COMMON_H_