cacommon.h 12.3 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 23
 * @file cacommon.h
 * @brief 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 <stdint.h>
#include <stdlib.h>
31
#include <stdbool.h>
32 33


34 35 36 37 38 39
#ifdef __cplusplus
extern "C"
{
#endif

/**
40
 * @brief IP address Length
41 42 43 44
 */
#define CA_IPADDR_SIZE 16

/**
45
 * @brief Mac address length for BT port
46 47 48 49
 */
#define CA_MACADDR_SIZE 18

/**
50
 * @brief Max header options data length
51 52 53
 */
#define CA_MAX_HEADER_OPTION_DATA_LENGTH 16

54 55 56 57 58
/**
* @brief Max token length
*/
#define CA_MAX_TOKEN_LEN (8)

59
/**
60
 * @brief Max URI length
61
 */
62 63 64 65 66
#ifdef ARDUINO
#define CA_MAX_URI_LENGTH 128  /* maximum size of URI for embedded platforms*/
#else
#define CA_MAX_URI_LENGTH 512 /* maximum size of URI for other platforms*/
#endif
67

68
/**
69
 * @brief Max PDU length supported
70
 */
71 72 73 74 75
#ifdef ARDUINO
#define COAP_MAX_PDU_SIZE           320  /* maximum size of a CoAP PDU for embedded platforms*/
#else
#define COAP_MAX_PDU_SIZE           1400 /* maximum size of a CoAP PDU for big platforms*/
#endif
76

Sachin Agrawal's avatar
Sachin Agrawal committed
77 78 79 80 81
/**
 *@brief Maximum length of the remoteEndpoint identity
 */
#define CA_MAX_ENDPOINT_IDENTITY_LEN   (32)

82
/**
83 84
 * @brief option types - the highest option number 63
 */
85 86 87
#define CA_OPTION_IF_MATCH 1
#define CA_OPTION_ETAG 4
#define CA_OPTION_IF_NONE_MATCH 5
88
#define CA_OPTION_OBSERVE 6
89 90 91 92 93 94 95 96 97 98
#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

/**
99
 * @brief Payload information from resource model
100
 */
101
typedef uint8_t *CAPayload_t;
102 103

/**
104
 * @brief URI for the OIC base.CA considers relative URI as the URI.
105
 */
106
typedef char *CAURI_t;
107 108

/**
109
 * @brief Token information for mapping the request and responses by resource model
110
 */
111
typedef char *CAToken_t;
112

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
// The following flags are the same as the equivalent OIC values in
// octypes.h, allowing direct copying with slight fixup.
// The CA layer should used the OC types when build allows that.
#define MAX_ADDR_STR_SIZE_CA (40)

typedef enum
{
    CA_DEFAULT_ADAPTER = 0,

    // value zero indicates discovery
    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
} CATransportAdapter_t;

128 129
typedef enum
{
130 131 132 133 134 135 136
    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
137 138
    // Indication that a message was received by multicast.
    CA_MULTICAST       = (1 << 7),
139 140 141 142 143 144 145 146 147 148
    // 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;
149

John Light's avatar
John Light committed
150 151 152
#define CA_IPFAMILY_MASK (CA_IPV6|CA_IPV4)
#define CA_SCOPE_MASK 0xf     // mask scope bits above

153
/**
154 155
 * @enum CANetworkStatus_t
 * @brief Information about the network status.
156 157 158
 */
typedef enum
{
159 160
    CA_INTERFACE_DOWN,   /**< Connection is not available */
    CA_INTERFACE_UP    /**< Connection is Available */
161 162
} CANetworkStatus_t;

Sachin Agrawal's avatar
Sachin Agrawal committed
163 164 165 166 167 168 169
/*
 * @brief remoteEndpoint identity
 */
typedef struct
{
    uint16_t id_length;
    unsigned char id[CA_MAX_ENDPOINT_IDENTITY_LEN];
170
} CARemoteId_t;
Sachin Agrawal's avatar
Sachin Agrawal committed
171

172
/**
173 174
 * @enum CAMessageType_t
 * @brief Message Type for Base source code
175 176 177
 */
typedef enum
{
178 179 180
    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 */
181 182
    CA_MSG_RESET         /**< used to indicates not-interested or error (lack of context)in
                                                  received messages */
183
} CAMessageType_t;
184 185

/**
186 187
 * @enum CAMethod_t
 * @brief Allowed method to be used by resource model
188 189 190
 */
typedef enum
{
191 192 193 194
    CA_GET = 1, /**< GET Method  */
    CA_POST,    /**< POST Method */
    CA_PUT,     /**< PUT Method */
    CA_DELETE   /**< DELETE Method */
195 196 197
} CAMethod_t;

/**
198 199
 * @brief Endpoint information for connectivities
 * Must be identical to OCDevAddr.
200 201 202
 */
typedef struct
{
203 204 205 206 207 208 209
    CATransportAdapter_t    adapter;    // adapter type
    CATransportFlags_t      flags;      // transport modifiers
    char                    addr[MAX_ADDR_STR_SIZE_CA]; // address for all
    uint32_t                interface;  // usually zero for default interface
    uint16_t                port;       // for IP
    CARemoteId_t identity;              // endpoint identity
} CAEndpoint_t;
210 211

/**
212 213
 * @enum CAResult_t
 * @brief Enums for CA return values
214 215 216
 */
typedef enum
{
217
    // Result code - START HERE
218 219 220 221 222 223 224 225
    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 */
    CA_SERVER_NOT_STARTED,          /**< Server is not started*/
    CA_DESTINATION_NOT_REACHABLE,   /**< Destination is not reachable */
    CA_SOCKET_OPERATION_FAILED,     /**< Socket operation failed */
    CA_SEND_FAILED,                 /**< Send request failed */
226
    CA_RECEIVE_FAILED,              /**< Receive failed */
227 228 229
    CA_MEMORY_ALLOC_FAILED,         /**< Memory allocation failed */
    CA_REQUEST_TIMEOUT,             /**< Request is Timeout */
    CA_DESTINATION_DISCONNECTED,    /**< Destination is disconnected */
230
    CA_NOT_SUPPORTED,               /**< Not supported */
231
    CA_STATUS_NOT_INITIALIZED,      /**< CA layer is not initialized */
232
    CA_STATUS_FAILED =255           /**< Failure */
233
    /* Result code - END HERE */
234 235 236
} CAResult_t;

/**
237 238
 * @enum CAResponseResult_t
 * @brief Enums for CA Response values
239 240 241
 */
typedef enum
{
jihwan.seo's avatar
jihwan.seo committed
242 243 244 245 246
    /* Response status code - START HERE */
    CA_EMPTY = 0,                    /**< Empty */
    CA_SUCCESS = 200,                /**< Success */
    CA_CREATED = 201,                /**< Created */
    CA_DELETED = 202,                /**< Deleted */
247 248 249
    CA_VALID = 203,                  /**< Valid */
    CA_CHANGED = 204,                /**< Changed */
    CA_CONTENT = 205,                /**< Content */
jihwan.seo's avatar
jihwan.seo committed
250
    CA_BAD_REQ = 400,                /**< Bad Request */
Sachin Agrawal's avatar
Sachin Agrawal committed
251
    CA_UNAUTHORIZED_REQ = 401,       /**< Unauthorized Request */
jihwan.seo's avatar
jihwan.seo committed
252
    CA_BAD_OPT = 402,                /**< Bad Option */
Sachin Agrawal's avatar
Sachin Agrawal committed
253
    CA_FORBIDDEN_REQ = 403,          /**< Forbidden Request */
jihwan.seo's avatar
jihwan.seo committed
254 255 256
    CA_NOT_FOUND = 404,              /**< Not found */
    CA_INTERNAL_SERVER_ERROR = 500,  /**< Internal Server Error */
    CA_RETRANSMIT_TIMEOUT = 504      /**< Retransmit timeout */
257
    /* Response status code - END HERE */
258 259 260
} CAResponseResult_t;

/**
261 262
 * @enum CATransportProtocolID_t
 * @brief Transport Protocol IDs for additional options
263 264 265
 */
typedef enum
{
266 267
    CA_INVALID_ID = (1 << 0),   /**< Invalid ID */
    CA_COAP_ID = (1 << 1)       /**< COAP ID */
268 269
} CATransportProtocolID_t;

270 271 272 273 274 275
/**
 * @enum CAAdapterState_t
 * @brief Adapter State to indicate the network changed notifications.
 */
typedef enum
{
276 277
    CA_ADAPTER_DISABLED,   /**< Adapter is Disabled */
    CA_ADAPTER_ENABLED     /**< Adapter is Enabled */
278 279
} CAAdapterState_t;

280 281 282 283 284 285 286
/**
 * @brief Header options structure to be filled
 *
 * This structure is used to hold header information.
 */
typedef struct
{
287 288 289 290
    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 **/
291
    char optionData[CA_MAX_HEADER_OPTION_DATA_LENGTH];      /**< Optional data values**/
292 293 294 295 296 297 298 299 300
} CAHeaderOption_t;

/**
 * @brief Base Information received
 *
 * This structure is used to hold request & response base information
 */
typedef struct
{
301 302

    CAMessageType_t type;       /**< Qos for the request */
303 304 305 306
    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 */
307
    uint8_t tokenLength;        /**< token length*/
308 309 310
    CAHeaderOption_t *options;  /** Header Options for the request */
    uint8_t numOptions;         /**< Number of Header options */
    CAPayload_t payload;        /**< payload of the request  */
311
    size_t payloadSize;         /**< size in bytes of the payload */
312
    CAURI_t resourceUri;        /**< Resource URI information **/
313 314
} CAInfo_t;

315 316 317 318 319
/**
 * @brief Request Information to be sent
 *
 * This structure is used to hold request information
 */
320 321
typedef struct
{
322 323
    CAMethod_t method;  /**< Name of the Method Allowed */
    CAInfo_t info;      /**< Information of the request. */
324
    bool isMulticast;   /**< is multicast request */
325 326 327
} CARequestInfo_t;

/**
328
 * @brief Response information received
329 330 331 332 333
 *
 * This structure is used to hold response information
 */
typedef struct
{
334
    CAResponseResult_t result;  /**< Result for response by resource model */
335
    CAInfo_t info;              /**< Information of the response */
336 337
} CAResponseInfo_t;

jnashok's avatar
jnashok committed
338 339 340 341 342 343 344 345 346 347 348 349 350
/**
 * @brief Error information from CA
 *        contains error code and message information
 *
 * This structure holds error information
 */
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;

351 352 353 354 355
/**
 * @brief Hold global variables for CA layer (also used by RI layer)
 */
typedef struct
{
John Light's avatar
John Light committed
356 357 358 359 360 361
    int fd;
    uint16_t port;
} CASocket_t;

typedef struct
{
362
    CATransportFlags_t clientFlags;
John Light's avatar
John Light committed
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
    CATransportFlags_t serverFlags;
    bool client;
    bool server;

    struct sockets
    {
        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
        int netlinkFd;      // netlink
        int shutdownFds[2]; // shutdown pipe
        int selectTimeout;  // in seconds
        int maxfd;          // highest fd (for select)
        int numInterfaces;  // number of active interfaces
        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
    } ip;

    struct calayer
    {
        CATransportFlags_t previousRequestFlags; // address family filtering
        uint16_t previousRequestMessageId;       // address family filtering
    } ca;
394 395 396 397
} CAGlobals_t;

extern CAGlobals_t caglobals;

398 399 400 401
#ifdef __cplusplus
} /* extern "C" */
#endif

402
#endif //#ifndef CA_COMMON_H_
403