Skip to content

Commit 4c4bab2

Browse files
committed
Merge branch 'bugfix/fix_aws_read_timeout_error' into 'master'
aws: fix AWS read timeout error See merge request sdk/ESP8266_RTOS_SDK!967
2 parents 363db85 + 02cc56a commit 4c4bab2

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

components/aws_iot/port/network_mbedtls_wrapper.c

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,18 +165,32 @@ IoT_Error_t iot_tls_read(Network *pNetwork, unsigned char *pMsg, size_t len, Tim
165165
}
166166

167167
while (len > 0) {
168-
/* Make sure we never block on read for longer than timer has left,
169-
but also that we don't block indefinitely (ie read_timeout > 0) */
170-
read_timeout = MAX(1, MIN(read_timeout, left_ms(timer)));
171-
timeout.tv_sec = read_timeout / 1000;
172-
timeout.tv_usec = (read_timeout % 1000) * 1000;
168+
if (esp_tls_get_bytes_avail(tls) <= 0) {
169+
fd_set read_fs, error_fs;
173170

174-
ret = setsockopt(tls->sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(struct timeval));
175-
ret = esp_tls_conn_read(tls, pMsg, len);
171+
/* Make sure we never block on read for longer than timer has left,
172+
but also that we don't block indefinitely (ie read_timeout > 0) */
173+
read_timeout = MAX(1, MIN(read_timeout, left_ms(timer)));
174+
timeout.tv_sec = read_timeout / 1000;
175+
timeout.tv_usec = (read_timeout % 1000) * 1000;
176+
177+
FD_CLR(tls->sockfd, &read_fs);
178+
FD_CLR(tls->sockfd, &error_fs);
179+
180+
FD_SET(tls->sockfd, &read_fs);
181+
FD_SET(tls->sockfd, &error_fs);
176182

177-
/* Restore the old timeout */
178-
tlsDataParams->timeout = read_timeout;
183+
ret = select(tls->sockfd + 1, &read_fs, NULL, &error_fs, &timeout);
184+
if (!ret)
185+
goto check_time;
186+
else if (ret < 0)
187+
return NETWORK_SSL_READ_ERROR;
179188

189+
if (FD_ISSET(tls->sockfd, &error_fs))
190+
return NETWORK_SSL_READ_ERROR;
191+
}
192+
193+
ret = esp_tls_conn_read(tls, pMsg, len);
180194
if (ret > 0) {
181195
rxLen += ret;
182196
pMsg += ret;
@@ -185,6 +199,7 @@ IoT_Error_t iot_tls_read(Network *pNetwork, unsigned char *pMsg, size_t len, Tim
185199
return NETWORK_SSL_READ_ERROR;
186200
}
187201

202+
check_time:
188203
// Evaluate timeout after the read to make sure read is done at least once
189204
if (has_timer_expired(timer)) {
190205
break;

0 commit comments

Comments
 (0)