@@ -251,6 +251,9 @@ nsapi_error_t QUECTEL_BG96_CellularStack::create_socket_impl(CellularSocket *soc
251
251
nsapi_size_or_error_t QUECTEL_BG96_CellularStack::socket_sendto_impl (CellularSocket *socket, const SocketAddress &address,
252
252
const void *data, nsapi_size_t size)
253
253
{
254
+ if (size > BG96_MAX_SEND_SIZE) {
255
+ return NSAPI_ERROR_PARAMETER;
256
+ }
254
257
int sent_len = 0 ;
255
258
int sent_len_before = 0 ;
256
259
int sent_len_after = 0 ;
@@ -308,18 +311,27 @@ nsapi_size_or_error_t QUECTEL_BG96_CellularStack::socket_recvfrom_impl(CellularS
308
311
309
312
_at.cmd_start (" AT+QIRD=" );
310
313
_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
+ }
311
319
_at.cmd_stop ();
312
320
313
321
_at.resp_start (" +QIRD:" );
314
322
recv_len = _at.read_int ();
315
323
_at.read_string (ip_address, sizeof (ip_address));
316
324
port = _at.read_int ();
317
325
if (recv_len > 0 ) {
326
+ // do not read more than buffer size
327
+ recv_len = recv_len > size ? size : recv_len;
318
328
_at.read_bytes ((uint8_t *)buffer, recv_len);
319
329
}
320
330
_at.resp_stop ();
321
331
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 ) {
323
335
return NSAPI_ERROR_WOULD_BLOCK;
324
336
}
325
337
0 commit comments