@@ -62,6 +62,10 @@ nsapi_error_t QUECTEL_BG96_CellularStack::socket_connect(nsapi_socket_t handle,
62
62
handle_open_socket_response (modem_connect_id, err);
63
63
64
64
if ((_at.get_last_error () == NSAPI_ERROR_OK) && err) {
65
+ if (err == BG96_SOCKET_BIND_FAIL) {
66
+ socket->created = false ;
67
+ return NSAPI_ERROR_PARAMETER;
68
+ }
65
69
_at.cmd_start (" AT+QICLOSE=" );
66
70
_at.write_int (modem_connect_id);
67
71
_at.cmd_stop ();
@@ -178,6 +182,10 @@ nsapi_error_t QUECTEL_BG96_CellularStack::create_socket_impl(CellularSocket *soc
178
182
handle_open_socket_response (modem_connect_id, err);
179
183
180
184
if ((_at.get_last_error () == NSAPI_ERROR_OK) && err) {
185
+ if (err == BG96_SOCKET_BIND_FAIL) {
186
+ socket->created = false ;
187
+ return NSAPI_ERROR_PARAMETER;
188
+ }
181
189
_at.cmd_start (" AT+QICLOSE=" );
182
190
_at.write_int (modem_connect_id);
183
191
_at.cmd_stop_read_resp ();
@@ -206,6 +214,10 @@ nsapi_error_t QUECTEL_BG96_CellularStack::create_socket_impl(CellularSocket *soc
206
214
handle_open_socket_response (modem_connect_id, err);
207
215
208
216
if ((_at.get_last_error () == NSAPI_ERROR_OK) && err) {
217
+ if (err == BG96_SOCKET_BIND_FAIL) {
218
+ socket->created = false ;
219
+ return NSAPI_ERROR_PARAMETER;
220
+ }
209
221
_at.cmd_start (" AT+QICLOSE=" );
210
222
_at.write_int (modem_connect_id);
211
223
_at.cmd_stop_read_resp ();
@@ -239,6 +251,14 @@ nsapi_error_t QUECTEL_BG96_CellularStack::create_socket_impl(CellularSocket *soc
239
251
nsapi_size_or_error_t QUECTEL_BG96_CellularStack::socket_sendto_impl (CellularSocket *socket, const SocketAddress &address,
240
252
const void *data, nsapi_size_t size)
241
253
{
254
+ if (size > BG96_MAX_SEND_SIZE) {
255
+ return NSAPI_ERROR_PARAMETER;
256
+ }
257
+
258
+ if (!size && socket->proto == NSAPI_UDP) {
259
+ return NSAPI_ERROR_UNSUPPORTED;
260
+ }
261
+
242
262
int sent_len = 0 ;
243
263
int sent_len_before = 0 ;
244
264
int sent_len_after = 0 ;
@@ -296,18 +316,27 @@ nsapi_size_or_error_t QUECTEL_BG96_CellularStack::socket_recvfrom_impl(CellularS
296
316
297
317
_at.cmd_start (" AT+QIRD=" );
298
318
_at.write_int (socket->id );
319
+ if (socket->proto == NSAPI_TCP) {
320
+ // do not read more than max size
321
+ size = size > BG96_MAX_RECV_SIZE ? BG96_MAX_RECV_SIZE : size;
322
+ _at.write_int (size);
323
+ }
299
324
_at.cmd_stop ();
300
325
301
326
_at.resp_start (" +QIRD:" );
302
327
recv_len = _at.read_int ();
303
328
_at.read_string (ip_address, sizeof (ip_address));
304
329
port = _at.read_int ();
305
330
if (recv_len > 0 ) {
331
+ // do not read more than buffer size
332
+ recv_len = recv_len > size ? size : recv_len;
306
333
_at.read_bytes ((uint8_t *)buffer, recv_len);
307
334
}
308
335
_at.resp_stop ();
309
336
310
- if (!recv_len || (_at.get_last_error () != NSAPI_ERROR_OK)) {
337
+ // We block only if 0 recv length really means no data.
338
+ // If 0 is followed by ip address and port can be an UDP 0 length packet
339
+ if (!recv_len && port < 0 ) {
311
340
return NSAPI_ERROR_WOULD_BLOCK;
312
341
}
313
342
0 commit comments