1 // Copyright 2020 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef _ESP_TRANSPORT_INTERNAL_H_
16 #define _ESP_TRANSPORT_INTERNAL_H_
17 
18 #include "esp_transport.h"
19 #include "sys/queue.h"
20 
21 typedef int (*get_socket_func)(esp_transport_handle_t t);
22 
23 typedef struct esp_foundation_transport {
24     struct esp_transport_error_storage  *error_handle;           /*!< Pointer to the transport error container */
25     struct transport_esp_tls            *transport_esp_tls;      /*!< Pointer to the base transport which uses esp-tls */
26 } esp_foundation_transport_t;
27 
28 /**
29  * Transport layer structure, which will provide functions, basic properties for transport types
30  */
31 struct esp_transport_item_t {
32     int             port;
33     char            *scheme;        /*!< Tag name */
34     void            *data;          /*!< Additional transport data */
35     connect_func    _connect;       /*!< Connect function of this transport */
36     io_read_func    _read;          /*!< Read */
37     io_func         _write;         /*!< Write */
38     trans_func      _close;         /*!< Close */
39     poll_func       _poll_read;     /*!< Poll and read */
40     poll_func       _poll_write;    /*!< Poll and write */
41     trans_func      _destroy;       /*!< Destroy and free transport */
42     connect_async_func _connect_async;      /*!< non-blocking connect function of this transport */
43     payload_transfer_func  _parent_transfer;        /*!< Function returning underlying transport layer */
44     get_socket_func        _get_socket;             /*!< Function returning the transport's socket */
45     esp_transport_keep_alive_t *keep_alive_cfg;     /*!< TCP keep-alive config */
46     struct esp_foundation_transport *base;          /*!< Foundation transport pointer available from each transport */
47 
48     STAILQ_ENTRY(esp_transport_item_t) next;
49 };
50 
51 /**
52  * @brief Internal error types for TCP connection issues not covered in socket's errno
53  */
54 enum tcp_transport_errors {
55     ERR_TCP_TRANSPORT_CONNECTION_TIMEOUT,
56     ERR_TCP_TRANSPORT_CANNOT_RESOLVE_HOSTNAME,
57     ERR_TCP_TRANSPORT_CONNECTION_CLOSED_BY_FIN,
58     ERR_TCP_TRANSPORT_CONNECTION_FAILED,
59     ERR_TCP_TRANSPORT_SETOPT_FAILED,
60     ERR_TCP_TRANSPORT_NO_MEM,
61 };
62 
63 /**
64  * @brief      Captures internal tcp connection error
65  *
66  * This is internally translated to esp-tls return codes of esp_err_t type, since the esp-tls
67  * will be used as TCP transport layer
68  *
69  * @param[in] t The transport handle
70  * @param[in] error Internal tcp-transport's error
71  *
72  */
73 void capture_tcp_transport_error(esp_transport_handle_t t, enum tcp_transport_errors error);
74 
75 /**
76  * @brief Returns underlying socket for the supplied transport handle
77  *
78  * @param t Transport handle
79  *
80  * @return Socket file descriptor in case of success
81  *         -1 in case of error
82  */
83 int esp_transport_get_socket(esp_transport_handle_t t);
84 
85 /**
86  * @brief      Captures the current errno
87  *
88  * @param[in] t The transport handle
89  * @param[in] sock_errno Socket errno to store in internal transport structures
90  *
91  */
92 void esp_transport_capture_errno(esp_transport_handle_t t, int sock_errno);
93 
94 /**
95  * @brief      Creates esp-tls transport used in the foundation transport
96  *
97  * @return     transport esp-tls handle
98  */
99 struct transport_esp_tls* esp_transport_esp_tls_create(void);
100 
101 /**
102  * @brief      Destroys esp-tls transport used in the foundation transport
103  *
104  * @param[in]  transport esp-tls handle
105  */
106 void esp_transport_esp_tls_destroy(struct transport_esp_tls* transport_esp_tls);
107 
108 /**
109  * @brief      Sets error to common transport handle
110  *
111  *             Note: This function copies the supplied error handle object to tcp_transport's internal
112  *             error handle object
113  *
114  * @param[in]  A transport handle
115  *
116  */
117 void esp_transport_set_errors(esp_transport_handle_t t, const esp_tls_error_handle_t error_handle);
118 
119 #endif //_ESP_TRANSPORT_INTERNAL_H_
120