Skip to content

Commit 3bdb6d1

Browse files
author
Ari Parkkila
committed
Cellular: Fixed ATHandler write poll timeout
1 parent 9b896a1 commit 3bdb6d1

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

features/cellular/framework/AT/ATHandler.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -335,14 +335,8 @@ void ATHandler::rewind_buffer()
335335
}
336336
}
337337

338-
bool ATHandler::fill_buffer(bool wait_for_timeout)
338+
int ATHandler::poll_timeout(bool wait_for_timeout)
339339
{
340-
tr_debug("%s", __func__);
341-
// Reset buffer when full
342-
if (sizeof(_recv_buff) == _recv_len) {
343-
reset_buffer();
344-
}
345-
346340
int timeout;
347341
if (wait_for_timeout) {
348342
uint64_t now = rtos::Kernel::get_ms_count();
@@ -356,11 +350,22 @@ bool ATHandler::fill_buffer(bool wait_for_timeout)
356350
} else {
357351
timeout = 0;
358352
}
353+
return timeout;
354+
}
355+
356+
bool ATHandler::fill_buffer(bool wait_for_timeout)
357+
{
358+
tr_debug("%s", __func__);
359+
// Reset buffer when full
360+
if (sizeof(_recv_buff) == _recv_len) {
361+
reset_buffer();
362+
}
359363

360364
pollfh fhs;
361365
fhs.fh = _fileHandle;
362366
fhs.events = POLLIN;
363-
int count = poll(&fhs, 1, timeout);
367+
int timeout = 0;
368+
int count = poll(&fhs, 1, poll_timeout(wait_for_timeout));
364369
if (count > 0 && (fhs.revents & POLLIN)) {
365370
ssize_t len = _fileHandle->read(_recv_buff + _recv_len, sizeof(_recv_buff) - _recv_len);
366371
if (len > 0) {
@@ -1097,12 +1102,7 @@ size_t ATHandler::write(const void *data, size_t len)
10971102
fhs.events = POLLOUT;
10981103
size_t write_len = 0;
10991104
for (; write_len < len; ) {
1100-
int timeout = (_start_time + _at_timeout) - rtos::Kernel::get_ms_count();
1101-
if (timeout < 0) {
1102-
set_error(NSAPI_ERROR_DEVICE_ERROR);
1103-
return 0;
1104-
}
1105-
int count = poll(&fhs, 1, timeout);
1105+
int count = poll(&fhs, 1, poll_timeout());
11061106
if (count <= 0 || !(fhs.revents & POLLOUT)) {
11071107
set_error(NSAPI_ERROR_DEVICE_ERROR);
11081108
return 0;

features/cellular/framework/AT/ATHandler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,9 @@ class ATHandler
430430
void reset_buffer();
431431
// Reading position set to 0 and buffer's unread content moved to beginning
432432
void rewind_buffer();
433+
// Calculate remaining time for polling based on request start time and AT timeout.
434+
// Returns 0 or time in ms for polling.
435+
int poll_timeout(bool wait_for_timeout = true);
433436
// Reads from serial to receiving buffer.
434437
// Returns true on successful read OR false on timeout.
435438
bool fill_buffer(bool wait_for_timeout = true);

0 commit comments

Comments
 (0)