Skip to content

Commit 0ed5e80

Browse files
Mirela ChiricaCruz Monrreal II
authored andcommitted
Cellular: Fix socket's send and receive boundaries for BG96
1 parent 306943a commit 0ed5e80

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularStack.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,9 @@ nsapi_error_t QUECTEL_BG96_CellularStack::create_socket_impl(CellularSocket *soc
251251
nsapi_size_or_error_t QUECTEL_BG96_CellularStack::socket_sendto_impl(CellularSocket *socket, const SocketAddress &address,
252252
const void *data, nsapi_size_t size)
253253
{
254+
if (size > BG96_MAX_SEND_SIZE) {
255+
return NSAPI_ERROR_PARAMETER;
256+
}
254257
int sent_len = 0;
255258
int sent_len_before = 0;
256259
int sent_len_after = 0;
@@ -308,18 +311,27 @@ nsapi_size_or_error_t QUECTEL_BG96_CellularStack::socket_recvfrom_impl(CellularS
308311

309312
_at.cmd_start("AT+QIRD=");
310313
_at.write_int(socket->id);
314+
if (socket->proto == NSAPI_TCP) {
315+
// do not read more than max size
316+
size = size > BG96_MAX_RECV_SIZE ? BG96_MAX_RECV_SIZE : size;
317+
_at.write_int(size);
318+
}
311319
_at.cmd_stop();
312320

313321
_at.resp_start("+QIRD:");
314322
recv_len = _at.read_int();
315323
_at.read_string(ip_address, sizeof(ip_address));
316324
port = _at.read_int();
317325
if (recv_len > 0) {
326+
// do not read more than buffer size
327+
recv_len = recv_len > size ? size : recv_len;
318328
_at.read_bytes((uint8_t *)buffer, recv_len);
319329
}
320330
_at.resp_stop();
321331

322-
if (!recv_len || (_at.get_last_error() != NSAPI_ERROR_OK)) {
332+
// We block only if 0 recv length really means no data.
333+
// If 0 is followed by ip address and port can be an UDP 0 length packet
334+
if (!recv_len && port < 0) {
323335
return NSAPI_ERROR_WOULD_BLOCK;
324336
}
325337

features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularStack.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ namespace mbed {
2525
#define BG96_SOCKET_MAX 12
2626
#define BG96_CREATE_SOCKET_TIMEOUT 150000 //150 seconds
2727
#define BG96_CLOSE_SOCKET_TIMEOUT 20000 // TCP socket max timeout is >10sec
28+
#define BG96_MAX_RECV_SIZE 1500
29+
#define BG96_MAX_SEND_SIZE 1460
2830
#define BG96_SOCKET_BIND_FAIL 556
2931

3032
class QUECTEL_BG96_CellularStack : public AT_CellularStack {

0 commit comments

Comments
 (0)