11
11
12
12
#define TAG "network_transport"
13
13
14
+ Timeouts_t timeouts = { .connectionTimeoutMs = 4000 , .sendTimeoutMs = 10000 , .recvTimeoutMs = 2000 };
15
+
16
+ void vTlsSetConnectTimeout ( uint16_t connectionTimeoutMs )
17
+ {
18
+ timeouts .connectionTimeoutMs = connectionTimeoutMs ;
19
+ }
20
+
21
+ void vTlsSetSendTimeout ( uint16_t sendTimeoutMs )
22
+ {
23
+ timeouts .sendTimeoutMs = sendTimeoutMs ;
24
+ }
25
+
26
+ void vTlsSetRecvTimeout ( uint16_t recvTimeoutMs )
27
+ {
28
+ timeouts .recvTimeoutMs = recvTimeoutMs ;
29
+ }
30
+
14
31
TlsTransportStatus_t xTlsConnect ( NetworkContext_t * pxNetworkContext )
15
32
{
16
33
TlsTransportStatus_t xResult = TLS_TRANSPORT_CONNECT_FAILURE ;
@@ -26,11 +43,11 @@ TlsTransportStatus_t xTlsConnect( NetworkContext_t* pxNetworkContext )
26
43
.ds_data = pxNetworkContext -> ds_data ,
27
44
.clientkey_buf = ( const unsigned char * )( pxNetworkContext -> pcClientKey ),
28
45
.clientkey_bytes = pxNetworkContext -> pcClientKeySize ,
29
- .timeout_ms = 2000 ,
46
+ .timeout_ms = timeouts . connectionTimeoutMs ,
30
47
.non_block = false,
31
48
};
32
49
33
- if ( xSemaphoreTake (pxNetworkContext -> xTlsContextSemaphore , portMAX_DELAY ) == pdTRUE )
50
+ if ( xSemaphoreTake ( pxNetworkContext -> xTlsContextSemaphore , portMAX_DELAY ) == pdTRUE )
34
51
{
35
52
int lConnectResult = -1 ;
36
53
esp_tls_t * pxTls = esp_tls_init ();
@@ -46,13 +63,12 @@ TlsTransportStatus_t xTlsConnect( NetworkContext_t* pxNetworkContext )
46
63
47
64
if ( lConnectResult == 1 )
48
65
{
49
-
50
66
int lSockFd = -1 ;
51
- if ( esp_tls_get_conn_sockfd (pxNetworkContext -> pxTls , & lSockFd ) == ESP_OK )
67
+ if ( esp_tls_get_conn_sockfd ( pxNetworkContext -> pxTls , & lSockFd ) == ESP_OK )
52
68
{
53
- int flags = fcntl (lSockFd , F_GETFL );
69
+ int flags = fcntl ( lSockFd , F_GETFL );
54
70
55
- if ( fcntl (lSockFd , F_SETFL , flags | O_NONBLOCK ) != -1 )
71
+ if ( fcntl ( lSockFd , F_SETFL , flags | O_NONBLOCK ) != -1 )
56
72
{
57
73
xResult = TLS_TRANSPORT_SUCCESS ;
58
74
}
@@ -63,6 +79,8 @@ TlsTransportStatus_t xTlsConnect( NetworkContext_t* pxNetworkContext )
63
79
{
64
80
esp_tls_conn_destroy ( pxNetworkContext -> pxTls );
65
81
pxNetworkContext -> pxTls = NULL ;
82
+ } else
83
+ {
66
84
}
67
85
}
68
86
( void ) xSemaphoreGive ( pxNetworkContext -> xTlsContextSemaphore );
@@ -81,7 +99,7 @@ TlsTransportStatus_t xTlsDisconnect( NetworkContext_t* pxNetworkContext )
81
99
{
82
100
xResult = TLS_TRANSPORT_SUCCESS ;
83
101
}
84
- else if (esp_tls_conn_destroy (pxNetworkContext -> pxTls ) == 0 )
102
+ else if ( esp_tls_conn_destroy (pxNetworkContext -> pxTls ) == 0 )
85
103
{
86
104
xResult = TLS_TRANSPORT_SUCCESS ;
87
105
}
@@ -101,7 +119,7 @@ TlsTransportStatus_t xTlsDisconnect( NetworkContext_t* pxNetworkContext )
101
119
}
102
120
103
121
int32_t espTlsTransportSend ( NetworkContext_t * pxNetworkContext ,
104
- const void * pvData , size_t uxDataLen )
122
+ const void * pvData , size_t uxDataLen )
105
123
{
106
124
int32_t lBytesSent = -1 ;
107
125
@@ -111,18 +129,19 @@ int32_t espTlsTransportSend( NetworkContext_t* pxNetworkContext,
111
129
( pxNetworkContext -> pxTls != NULL ) )
112
130
{
113
131
TimeOut_t xTimeout ;
114
- TickType_t xTicksToWait = pdMS_TO_TICKS (10 );
115
-
116
132
vTaskSetTimeOutState ( & xTimeout );
117
133
134
+ struct timeval timeout = { .tv_usec = timeouts .sendTimeoutMs * 1000 , .tv_sec = 0 };
135
+ TickType_t xTicksToWait = pdMS_TO_TICKS ( timeouts .sendTimeoutMs );
136
+ TickType_t start_tick = xTaskGetTickCount ();
137
+
118
138
if ( xSemaphoreTake ( pxNetworkContext -> xTlsContextSemaphore , xTicksToWait ) == pdTRUE )
119
139
{
120
140
int lSockFd = -1 ;
121
141
esp_err_t xError = esp_tls_get_conn_sockfd ( pxNetworkContext -> pxTls , & lSockFd );
122
- if ( xError == ESP_OK )
142
+ if ( xError == ESP_OK )
123
143
{
124
144
unsigned char * pucData = ( unsigned char * ) pvData ;
125
- struct timeval timeout = { .tv_usec = 10000 , .tv_sec = 0 };
126
145
lBytesSent = 0 ;
127
146
do
128
147
{
@@ -132,25 +151,25 @@ int32_t espTlsTransportSend( NetworkContext_t* pxNetworkContext,
132
151
133
152
FD_ZERO ( & write_fds );
134
153
FD_SET ( lSockFd , & write_fds );
135
-
136
154
FD_ZERO ( & error_fds );
137
155
FD_SET ( lSockFd , & error_fds );
138
156
157
+ suseconds_t elapsed_time_usec = ( xTaskGetTickCount () - start_tick ) * portTICK_PERIOD_MS * 1000 ;
158
+ timeout .tv_usec = ( timeout .tv_usec - elapsed_time_usec >= 0 ) ? timeout .tv_usec - elapsed_time_usec : 0 ;
139
159
lSelectResult = select ( lSockFd + 1 , NULL , & write_fds , & error_fds , & timeout );
140
160
141
161
if ( lSelectResult < 0 )
142
162
{
143
163
lBytesSent = -1 ;
144
- ESP_LOGE (TAG , "Error during call to select." );
145
- break ;
164
+ ESP_LOGE ( TAG , "Error during call to select." );
146
165
}
147
166
else if ( ( lSelectResult > 0 ) && ( FD_ISSET ( lSockFd , & write_fds ) != 0 ) )
148
167
{
149
168
ssize_t lResult = esp_tls_conn_write ( pxNetworkContext -> pxTls ,
150
- & (pucData [lBytesSent ]),
169
+ & ( pucData [lBytesSent ] ),
151
170
uxDataLen - lBytesSent );
152
171
153
- if ( lResult > 0 )
172
+ if ( lResult >= 0 )
154
173
{
155
174
lBytesSent += ( int32_t ) lResult ;
156
175
}
@@ -161,22 +180,17 @@ int32_t espTlsTransportSend( NetworkContext_t* pxNetworkContext,
161
180
}
162
181
else
163
182
{
164
- /* Empty when lResult == 0 */
165
- }
166
-
167
- if ( ( lBytesSent < 0 ) ||
168
- ( lBytesSent == uxDataLen ) )
169
- {
170
- break ;
183
+ /* Empty when MBEDTLS_ERR_SSL_WANT_READ || MBEDTLS_ERR_SSL_WANT_WRITE */
171
184
}
172
185
}
173
-
174
186
else
175
187
{
176
188
/* Empty when lSelectResult == 0 */
177
189
}
178
190
}
179
- while ( xTaskCheckForTimeOut ( & xTimeout , & xTicksToWait ) == pdFALSE );
191
+ while ( ( xTaskCheckForTimeOut ( & xTimeout , & xTicksToWait ) == pdFALSE ) &&
192
+ ( lBytesSent < uxDataLen ) &&
193
+ ( lBytesSent >= 0 ) );
180
194
}
181
195
xSemaphoreGive (pxNetworkContext -> xTlsContextSemaphore );
182
196
}
@@ -186,38 +200,74 @@ int32_t espTlsTransportSend( NetworkContext_t* pxNetworkContext,
186
200
}
187
201
188
202
int32_t espTlsTransportRecv ( NetworkContext_t * pxNetworkContext ,
189
- void * pvData , size_t uxDataLen )
203
+ void * pvData , size_t uxDataLen )
190
204
{
191
- int32_t lBytesRead = -1 ;
205
+ int32_t lBytesRead = 0 ;
192
206
193
207
if ( ( pvData != NULL ) &&
194
208
( uxDataLen > 0 ) &&
195
209
( pxNetworkContext != NULL ) &&
196
210
( pxNetworkContext -> pxTls != NULL ) )
197
211
{
198
- if ( xSemaphoreTake ( pxNetworkContext -> xTlsContextSemaphore , portMAX_DELAY ) == pdTRUE )
212
+ TimeOut_t xTimeout ;
213
+ vTaskSetTimeOutState ( & xTimeout );
214
+
215
+ TickType_t xTicksToWait = pdMS_TO_TICKS ( timeouts .recvTimeoutMs );
216
+ struct timeval timeout = {.tv_usec = timeouts .recvTimeoutMs * 1000 , .tv_sec = 0 };
217
+ TickType_t start_tick = xTaskGetTickCount ();
218
+
219
+ if ( xSemaphoreTake ( pxNetworkContext -> xTlsContextSemaphore , xTicksToWait ) == pdTRUE )
199
220
{
200
- ssize_t lResult = esp_tls_conn_read ( pxNetworkContext -> pxTls ,
201
- pvData ,
202
- ( size_t ) uxDataLen ) ;
221
+ int lSockFd = -1 ;
222
+ fd_set read_fds ;
223
+ fd_set error_fds ;
203
224
204
- if ( lResult > 0 )
205
- {
206
- lBytesRead = ( int32_t ) lResult ;
207
- }
208
- else if ( ( lResult != MBEDTLS_ERR_SSL_WANT_WRITE ) &&
209
- ( lResult != MBEDTLS_ERR_SSL_WANT_READ ) )
210
- {
211
- lBytesRead = ( int32_t ) lResult ;
212
- }
213
- else
225
+ esp_tls_get_conn_sockfd ( pxNetworkContext -> pxTls , & lSockFd );
226
+ FD_ZERO ( & read_fds );
227
+ FD_SET ( lSockFd , & read_fds );
228
+ FD_ZERO ( & error_fds );
229
+ FD_SET ( lSockFd , & error_fds );
230
+
231
+ do
214
232
{
215
- lBytesRead = 0 ;
233
+ // Fetch any buffered data
234
+ ssize_t lResult = esp_tls_conn_read ( pxNetworkContext -> pxTls ,
235
+ pvData ,
236
+ ( size_t ) uxDataLen );
237
+
238
+ if ( lResult > 0 )
239
+ {
240
+ lBytesRead = ( int32_t ) lResult ;
241
+ }
242
+ else if ( ( lResult == MBEDTLS_ERR_SSL_WANT_WRITE ) ||
243
+ ( lResult == MBEDTLS_ERR_SSL_WANT_READ ) )
244
+ {
245
+ suseconds_t elapsed_time_usec = ( xTaskGetTickCount () - start_tick ) * portTICK_PERIOD_MS * 1000 ;
246
+ timeout .tv_usec = ( timeout .tv_usec - elapsed_time_usec >= 0 ) ? timeout .tv_usec - elapsed_time_usec : 0 ;
247
+
248
+ int lSelectResult = select ( lSockFd + 1 , & read_fds , NULL , & error_fds , & timeout );
249
+ if ( ( lSelectResult < 0 ) || FD_ISSET ( lSockFd , & error_fds ) ) {
250
+ ESP_LOGE ( TAG , "Error reading the message" );
251
+ lBytesRead = lResult = -1 ;
252
+ }
253
+ }
254
+ else if ( lResult == 0 )
255
+ {
256
+ ESP_LOGE ( TAG , "Connection closed" );
257
+ lBytesRead = -1 ;
258
+ }
259
+ else
260
+ {
261
+ ESP_LOGE ( TAG , "Error reading: %d" , ( int ) lResult );
262
+ lBytesRead = ( int32_t ) lResult ;
263
+ }
216
264
}
265
+ while ( ( xTaskCheckForTimeOut ( & xTimeout , & xTicksToWait ) == pdFALSE ) &&
266
+ ( lBytesRead == 0 ) );
217
267
218
- ( void ) xSemaphoreGive ( pxNetworkContext -> xTlsContextSemaphore );
268
+
269
+ ( void ) xSemaphoreGive ( pxNetworkContext -> xTlsContextSemaphore );
219
270
}
220
271
}
221
-
222
272
return lBytesRead ;
223
273
}
0 commit comments