Add wrong PIN defence codes in case of PIN based OxM
[platform/upstream/iotivity.git] / resource / csdk / connectivity / api / cacommon.h
1 /* ****************************************************************
2  *
3  * Copyright 2014 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 /**
22  * @file cacommon.h
23  * This file contains the common data structures between Resource , CA and adapters
24  */
25
26 #ifndef CA_COMMON_H_
27 #define CA_COMMON_H_
28
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <stdbool.h>
32
33
34 #ifdef __cplusplus
35 extern "C"
36 {
37 #endif
38
39 /**
40  * IP address Length
41  */
42 #define CA_IPADDR_SIZE 16
43
44 /**
45  * Remote Access jabber ID length.
46  */
47 #define CA_RAJABBERID_SIZE 256
48
49 /**
50  * Mac address length for BT port
51  */
52 #define CA_MACADDR_SIZE 18
53
54 /**
55  * Max header options data length
56  */
57 #define CA_MAX_HEADER_OPTION_DATA_LENGTH 20
58
59 /**
60 * Max token length
61 */
62 #define CA_MAX_TOKEN_LEN (8)
63
64 /**
65  * Max URI length
66  */
67 #ifdef ARDUINO
68 #define CA_MAX_URI_LENGTH 128  /* maximum size of URI for embedded platforms*/
69 #else
70 #define CA_MAX_URI_LENGTH 512 /* maximum size of URI for other platforms*/
71 #endif
72
73 /**
74  * Max PDU length supported
75  */
76 #ifdef ARDUINO
77 #define COAP_MAX_PDU_SIZE           320  /* maximum size of a CoAP PDU for embedded platforms*/
78 #else
79 #define COAP_MAX_PDU_SIZE           1400 /* maximum size of a CoAP PDU for big platforms*/
80 #endif
81
82 #ifdef WITH_BWT
83 #define CA_DEFAULT_BLOCK_SIZE       CA_BLOCK_SIZE_1024_BYTE
84 #endif
85
86 /**
87  *Maximum length of the remoteEndpoint identity
88  */
89 #define CA_MAX_ENDPOINT_IDENTITY_LEN   (32)
90
91 /**
92  * option types - the highest option number 63
93  */
94 #define CA_OPTION_IF_MATCH 1
95 #define CA_OPTION_ETAG 4
96 #define CA_OPTION_IF_NONE_MATCH 5
97 #define CA_OPTION_OBSERVE 6
98 #define CA_OPTION_LOCATION_PATH 8
99 #define CA_OPTION_URI_PATH 11
100 #define CA_OPTION_CONTENT_FORMAT 12
101 #define CA_OPTION_CONTENT_TYPE COAP_OPTION_CONTENT_FORMAT
102 #define CA_OPTION_MAXAGE 14
103 #define CA_OPTION_URI_QUERY 15
104 #define CA_OPTION_ACCEPT 17
105 #define CA_OPTION_LOCATION_QUERY 20
106
107 /**
108  * Payload information from resource model
109  */
110 typedef uint8_t *CAPayload_t;
111
112 /**
113  * URI for the OIC base.CA considers relative URI as the URI.
114  */
115 typedef char *CAURI_t;
116
117 /**
118  * Token information for mapping the request and responses by resource model
119  */
120 typedef char *CAToken_t;
121
122 // The following flags are the same as the equivalent OIC values in
123 // octypes.h, allowing direct copying with slight fixup.
124 // The CA layer should used the OC types when build allows that.
125 #ifdef RA_ADAPTER
126 #define MAX_ADDR_STR_SIZE_CA (256)
127 #else
128 #define MAX_ADDR_STR_SIZE_CA (40)
129 #endif
130
131 typedef enum
132 {
133     CA_DEFAULT_ADAPTER = 0,
134
135     // value zero indicates discovery
136     CA_ADAPTER_IP            = (1 << 0),   // IPv4 and IPv6, including 6LoWPAN
137     CA_ADAPTER_GATT_BTLE     = (1 << 1),   // GATT over Bluetooth LE
138     CA_ADAPTER_RFCOMM_BTEDR  = (1 << 2),   // RFCOMM over Bluetooth EDR
139
140 #ifdef RA_ADAPTER
141     CA_ADAPTER_REMOTE_ACCESS = (1 << 3),   // Remote Access over XMPP.
142 #endif
143
144     CA_ADAPTER_TCP = (1 << 4),   // CoAP over TCP
145     CA_ALL_ADAPTERS          = 0xffffffff
146 } CATransportAdapter_t;
147
148 typedef enum
149 {
150     CA_DEFAULT_FLAGS = 0,
151
152     // Insecure transport is the default (subject to change)
153     CA_SECURE          = (1 << 4),   // secure the transport path
154     // IPv4 & IPv6 autoselection is the default
155     CA_IPV6            = (1 << 5),   // IP adapter only
156     CA_IPV4            = (1 << 6),   // IP adapter only
157     // Indication that a message was received by multicast.
158     CA_MULTICAST       = (1 << 7),
159     // Link-Local multicast is the default multicast scope for IPv6.
160     // These correspond in both value and position to the IPv6 address bits.
161     CA_SCOPE_INTERFACE = 0x1, // IPv6 Interface-Local scope
162     CA_SCOPE_LINK      = 0x2, // IPv6 Link-Local scope (default)
163     CA_SCOPE_REALM     = 0x3, // IPv6 Realm-Local scope
164     CA_SCOPE_ADMIN     = 0x4, // IPv6 Admin-Local scope
165     CA_SCOPE_SITE      = 0x5, // IPv6 Site-Local scope
166     CA_SCOPE_ORG       = 0x8, // IPv6 Organization-Local scope
167     CA_SCOPE_GLOBAL    = 0xE, // IPv6 Global scope
168 } CATransportFlags_t;
169
170 #define CA_IPFAMILY_MASK (CA_IPV6|CA_IPV4)
171 #define CA_SCOPE_MASK 0xf     // mask scope bits above
172
173 /**
174  * @enum CANetworkStatus_t
175  * Information about the network status.
176  */
177 typedef enum
178 {
179     CA_INTERFACE_DOWN,   /**< Connection is not available */
180     CA_INTERFACE_UP    /**< Connection is Available */
181 } CANetworkStatus_t;
182
183 /*
184  * remoteEndpoint identity
185  */
186 typedef struct
187 {
188     uint16_t id_length;
189     unsigned char id[CA_MAX_ENDPOINT_IDENTITY_LEN];
190 } CARemoteId_t;
191
192 /**
193  * @enum CAMessageType_t
194  * Message Type for Base source code
195  */
196 typedef enum
197 {
198     CA_MSG_CONFIRM = 0,  /**< confirmable message (requires ACK/RST) */
199     CA_MSG_NONCONFIRM,   /**< non-confirmable message (one-shot message) */
200     CA_MSG_ACKNOWLEDGE,  /**< used to acknowledge confirmable messages */
201     CA_MSG_RESET         /**< used to indicates not-interested or error (lack of context)in
202                                                   received messages */
203 } CAMessageType_t;
204
205 /**
206  * @enum CAMethod_t
207  * Allowed method to be used by resource model
208  */
209 typedef enum
210 {
211     CA_GET = 1, /**< GET Method  */
212     CA_POST,    /**< POST Method */
213     CA_PUT,     /**< PUT Method */
214     CA_DELETE   /**< DELETE Method */
215 } CAMethod_t;
216
217 /**
218  * block size
219  * it depends on defined size in libCoAP.
220  */
221 typedef enum
222 {
223     CA_BLOCK_SIZE_16_BYTE = 0,    /**< 16byte */
224     CA_BLOCK_SIZE_32_BYTE = 1,    /**< 32byte */
225     CA_BLOCK_SIZE_64_BYTE = 2,    /**< 64byte */
226     CA_BLOCK_SIZE_128_BYTE = 3,   /**< 128byte */
227     CA_BLOCK_SIZE_256_BYTE = 4,   /**< 256byte */
228     CA_BLOCK_SIZE_512_BYTE = 5,   /**< 512byte */
229     CA_BLOCK_SIZE_1024_BYTE = 6     /**< 1Kbyte */
230 } CABlockSize_t;
231
232 /**
233  * Endpoint information for connectivities
234  * Must be identical to OCDevAddr.
235  */
236 typedef struct
237 {
238     CATransportAdapter_t    adapter;    // adapter type
239     CATransportFlags_t      flags;      // transport modifiers
240     uint16_t                port;       // for IP
241     char                    addr[MAX_ADDR_STR_SIZE_CA]; // address for all
242     uint32_t                interface;  // usually zero for default interface
243 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
244     char                    routeData[MAX_ADDR_STR_SIZE_CA]; /**< GatewayId:ClientId of
245                                                                     destination. **/
246 #endif
247 } CAEndpoint_t;
248
249 /**
250  * Endpoint information for secure messages
251  */
252 typedef struct
253 {
254     CAEndpoint_t endpoint;      /**< endpoint */
255     CARemoteId_t identity;      /**< endpoint identity */
256 } CASecureEndpoint_t;
257
258 /**
259  * @enum CAResult_t
260  * Enums for CA return values
261  */
262 typedef enum
263 {
264     /* Result code - START HERE */
265     CA_STATUS_OK = 0,               /**< Success */
266     CA_STATUS_INVALID_PARAM,        /**< Invalid Parameter */
267     CA_ADAPTER_NOT_ENABLED,         /**< Adapter is not enabled */
268     CA_SERVER_STARTED_ALREADY,      /**< Server is started already */
269     CA_SERVER_NOT_STARTED,          /**< Server is not started*/
270     CA_DESTINATION_NOT_REACHABLE,   /**< Destination is not reachable */
271     CA_SOCKET_OPERATION_FAILED,     /**< Socket operation failed */
272     CA_SEND_FAILED,                 /**< Send request failed */
273     CA_RECEIVE_FAILED,              /**< Receive failed */
274     CA_MEMORY_ALLOC_FAILED,         /**< Memory allocation failed */
275     CA_REQUEST_TIMEOUT,             /**< Request is Timeout */
276     CA_DESTINATION_DISCONNECTED,    /**< Destination is disconnected */
277     CA_NOT_SUPPORTED,               /**< Not supported */
278     CA_STATUS_NOT_INITIALIZED,      /**< Not Initialized*/
279     CA_DTLS_AUTHENTICATION_FAILURE, /**< Decryption error in DTLS */
280     CA_STATUS_FAILED =255           /**< Failure */
281     /* Result code - END HERE */
282 } CAResult_t;
283
284 /**
285  * @enum CAResponseResult_t
286  * Enums for CA Response values
287  */
288 typedef enum
289 {
290     /* Response status code - START HERE */
291     CA_EMPTY = 0,                    /**< Empty */
292     CA_CREATED = 201,                /**< Created */
293     CA_DELETED = 202,                /**< Deleted */
294     CA_VALID = 203,                  /**< Valid */
295     CA_CHANGED = 204,                /**< Changed */
296     CA_CONTENT = 205,                /**< Content */
297     CA_CONTINUE = 231,               /**< Continue */
298     CA_BAD_REQ = 400,                /**< Bad Request */
299     CA_UNAUTHORIZED_REQ = 401,       /**< Unauthorized Request */
300     CA_BAD_OPT = 402,                /**< Bad Option */
301     CA_FORBIDDEN_REQ = 403,          /**< Forbidden Request */
302     CA_NOT_FOUND = 404,              /**< Not found */
303     CA_NOT_ACCEPTABLE = 406,         /**< Not Acceptable */
304     CA_REQUEST_ENTITY_INCOMPLETE = 408, /**< Request Entity Incomplete */
305     CA_REQUEST_ENTITY_TOO_LARGE = 413,  /**< Request Entity Too Large */
306     CA_INTERNAL_SERVER_ERROR = 500,  /**< Internal Server Error */
307     CA_RETRANSMIT_TIMEOUT = 504      /**< Retransmit timeout */
308     /* Response status code - END HERE */
309 } CAResponseResult_t;
310
311 /**
312  * @enum CATransportProtocolID_t
313  * Transport Protocol IDs for additional options
314  */
315 typedef enum
316 {
317     CA_INVALID_ID = (1 << 0),   /**< Invalid ID */
318     CA_COAP_ID = (1 << 1)       /**< COAP ID */
319 } CATransportProtocolID_t;
320
321 /**
322  * @enum CAAdapterState_t
323  * Adapter State to indicate the network changed notifications.
324  */
325 typedef enum
326 {
327     CA_ADAPTER_DISABLED,   /**< Adapter is Disabled */
328     CA_ADAPTER_ENABLED     /**< Adapter is Enabled */
329 } CAAdapterState_t;
330
331 /**
332  * Format indicating which encoding has been used on the payload.
333  */
334 typedef enum
335 {
336     CA_FORMAT_UNDEFINED = 0,            /**< Undefined enoding format */
337     CA_FORMAT_TEXT_PLAIN,
338     CA_FORMAT_APPLICATION_LINK_FORMAT,
339     CA_FORMAT_APPLICATION_XML,
340     CA_FORMAT_APPLICATION_OCTET_STREAM,
341     CA_FORMAT_APPLICATION_RDF_XML,
342     CA_FORMAT_APPLICATION_EXI,
343     CA_FORMAT_APPLICATION_JSON,
344     CA_FORMAT_APPLICATION_CBOR,
345     CA_FORMAT_UNSUPPORTED
346 } CAPayloadFormat_t;
347
348 /**
349  * Header options structure to be filled
350  *
351  * This structure is used to hold header information.
352  */
353 typedef struct
354 {
355     CATransportProtocolID_t protocolID;                     /**< Protocol ID of the Option */
356     uint16_t optionID;                                      /**< The header option ID which will be
357                                                             added to communication packets */
358     uint16_t optionLength;                                  /**< Option Length **/
359     char optionData[CA_MAX_HEADER_OPTION_DATA_LENGTH];      /**< Optional data values**/
360 } CAHeaderOption_t;
361
362 /**
363  * Base Information received
364  *
365  * This structure is used to hold request & response base information
366  */
367 typedef struct
368 {
369
370     CAMessageType_t type;       /**< Qos for the request */
371 #ifdef ROUTING_GATEWAY
372     bool skipRetransmission;    /**< Will not attempt retransmission even if type is CONFIRM.
373                                      Required for packet forwarding */
374 #endif
375     uint16_t messageId;         /**< Message id.
376                                  * if message id is zero, it will generated by CA inside.
377                                  * otherwise, you can use it */
378     CAToken_t token;            /**< Token for CA */
379     uint8_t tokenLength;        /**< token length*/
380     CAHeaderOption_t *options;  /** Header Options for the request */
381     uint8_t numOptions;         /**< Number of Header options */
382     CAPayload_t payload;        /**< payload of the request  */
383     size_t payloadSize;         /**< size in bytes of the payload */
384     CAPayloadFormat_t payloadFormat;    /**< encoding format of the request payload */
385     CAPayloadFormat_t acceptFormat;     /**< accept format for the response payload */
386     CAURI_t resourceUri;        /**< Resource URI information **/
387     CARemoteId_t identity;      /**< endpoint identity */
388 } CAInfo_t;
389
390 /**
391  * Request Information to be sent
392  *
393  * This structure is used to hold request information
394  */
395 typedef struct
396 {
397     CAMethod_t method;  /**< Name of the Method Allowed */
398     CAInfo_t info;      /**< Information of the request. */
399     bool isMulticast;   /**< is multicast request */
400 } CARequestInfo_t;
401
402 /**
403  * Response information received
404  *
405  * This structure is used to hold response information
406  */
407 typedef struct
408 {
409     CAResponseResult_t result;  /**< Result for response by resource model */
410     CAInfo_t info;              /**< Information of the response */
411     bool isMulticast;
412 } CAResponseInfo_t;
413
414 /**
415  * Error information from CA
416  *        contains error code and message information
417  *
418  * This structure holds error information
419  */
420 typedef struct
421 {
422     CAResult_t result;  /**< CA API request result  */
423     CAInfo_t info;      /**< message information such as token and payload data
424                              helpful to identify the error */
425 } CAErrorInfo_t;
426
427 /**
428  * Hold global variables for CA layer (also used by RI layer)
429  */
430 typedef struct
431 {
432     int fd;        /**< socket fd */
433     uint16_t port; /**< socket port */
434 } CASocket_t;
435
436 #define HISTORYSIZE (4)
437
438 typedef struct
439 {
440     CATransportFlags_t flags;
441     uint16_t messageId;
442     char token[CA_MAX_TOKEN_LEN];
443     uint8_t tokenLength;
444 } CAHistoryItem_t;
445
446 typedef struct
447 {
448     int nextIndex;
449     CAHistoryItem_t items[HISTORYSIZE];
450 } CAHistory_t;
451
452 /**
453  * Hold interface index for keeping track of comings and goings
454  */
455 typedef struct
456 {
457     int32_t ifIndex; /**< network interface index */
458 } CAIfItem_t;
459
460 typedef struct
461 {
462     CATransportFlags_t clientFlags; /**< flag for client */
463     CATransportFlags_t serverFlags; /**< flag for server */
464     bool client; /**< client mode */
465     bool server; /**< server mode */
466
467     struct sockets
468     {
469         void *threadpool;   /**< threadpool between Initialize and Start */
470         CASocket_t u6;      /**< unicast   IPv6 */
471         CASocket_t u6s;     /**< unicast   IPv6 secure */
472         CASocket_t u4;      /**< unicast   IPv4 */
473         CASocket_t u4s;     /**< unicast   IPv4 secure */
474         CASocket_t m6;      /**< multicast IPv6 */
475         CASocket_t m6s;     /**< multicast IPv6 secure */
476         CASocket_t m4;      /**< multicast IPv4 */
477         CASocket_t m4s;     /**< multicast IPv4 secure */
478         int netlinkFd;      /**< netlink */
479         int shutdownFds[2]; /**< shutdown pipe */
480         int selectTimeout;  /**< in seconds */
481         int maxfd;          /**< highest fd (for select) */
482         bool started;       /**< the IP adapter has started */
483         bool terminate;     /**< the IP adapter needs to stop */
484         bool ipv6enabled;   /**< IPv6 enabled by OCInit flags */
485         bool ipv4enabled;   /**< IPv4 enabled by OCInit flags */
486         bool dualstack;     /**< IPv6 and IPv4 enabled */
487
488         struct networkmonitors
489         {
490             CAIfItem_t *ifItems; /**< current network interface index list */
491             size_t sizeIfItems;  /**< size of network interface index array */
492             size_t numIfItems;   /**< number of valid network interfaces */
493         } nm;
494     } ip;
495
496     struct calayer
497     {
498         CAHistory_t requestHistory;  /**< filter IP family in requests */
499         CAHistory_t responseHistory; /**< filter IP family in responses */
500         CATransportFlags_t previousRequestFlags;/**< address family filtering */
501         uint16_t previousRequestMessageId;      /**< address family filtering */
502     } ca;
503
504 #ifdef TCP_ADAPTER
505     /**
506      * Hold global variables for TCP Adapter.
507      */
508     struct tcpsockets
509     {
510         void *threadpool;       /**< threadpool between Initialize and Start */
511         void *svrlist;          /**< unicast IPv4 TCP server information*/
512         int selectTimeout;      /**< in seconds */
513         int listenBacklog;      /**< backlog counts*/
514         int maxfd;              /**< highest fd (for select) */
515         bool started;           /**< the TCP adapter has started */
516         bool terminate;         /**< the TCP adapter needs to stop */
517         bool ipv4tcpenabled;    /**< IPv4 TCP enabled by OCInit flags */
518     } tcp;
519 #endif
520 } CAGlobals_t;
521
522 extern CAGlobals_t caglobals;
523
524 /**
525  * Callback function type for request delivery.
526  * @param[out]   object       Endpoint object from which the request is received.
527  *                            It contains endpoint address based on the connectivity type.
528  * @param[out]   requestInfo  Info for resource model to understand about the request.
529  */
530 typedef void (*CARequestCallback)(const CAEndpoint_t *object,
531                                   const CARequestInfo_t *requestInfo);
532
533 /**
534  * Callback function type for response delivery.
535  * @param[out]   object           Endpoint object from which the response is received.
536  * @param[out]   responseInfo     Identifier which needs to be mapped with response.
537  */
538 typedef void (*CAResponseCallback)(const CAEndpoint_t *object,
539                                    const CAResponseInfo_t *responseInfo);
540 /**
541  * Callback function type for error.
542  * @param[out]   object           remote device information.
543  * @param[out]   errorInfo        CA Error information.
544  */
545 typedef void (*CAErrorCallback)(const CAEndpoint_t *object,
546                                 const CAErrorInfo_t *errorInfo);
547
548 #ifdef __cplusplus
549 } /* extern "C" */
550 #endif
551
552 #endif // CA_COMMON_H_