Skip to content

Commit 1bbb76d

Browse files
authored
Merge pull request #9514 from mirelachirica/bg96_netsocket_tests
BG96 netsocket tests
2 parents 799476d + b473aca commit 1bbb76d

File tree

4 files changed

+57
-6
lines changed

4 files changed

+57
-6
lines changed

features/cellular/framework/AT/AT_CellularStack.cpp

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,21 @@ nsapi_error_t AT_CellularStack::socket_bind(nsapi_socket_t handle, const SocketA
193193
}
194194

195195
if (addr) {
196-
socket->localAddress.set_addr(addr.get_addr());
197-
}
198-
199-
if (addr.get_port()) {
200-
socket->localAddress.set_port(addr.get_port());
196+
return NSAPI_ERROR_UNSUPPORTED;
201197
}
202198

203199
_at.lock();
204200

201+
uint16_t port = addr.get_port();
202+
if (port != socket->localAddress.get_port()) {
203+
if (port && (get_socket_index_by_port(port) == -1)) {
204+
socket->localAddress.set_port(port);
205+
} else {
206+
_at.unlock();
207+
return NSAPI_ERROR_PARAMETER;
208+
}
209+
}
210+
205211
if (!socket->created) {
206212
create_socket_impl(socket);
207213
}
@@ -335,3 +341,14 @@ void AT_CellularStack::socket_attach(nsapi_socket_t handle, void (*callback)(voi
335341
socket->_cb = callback;
336342
socket->_data = data;
337343
}
344+
345+
int AT_CellularStack::get_socket_index_by_port(uint16_t port)
346+
{
347+
int max_socket_count = get_max_socket_count();
348+
for (int i = 0; i < max_socket_count; i++) {
349+
if (_socket[i]->localAddress.get_port() == port) {
350+
return i;
351+
}
352+
}
353+
return -1;
354+
}

features/cellular/framework/AT/AT_CellularStack.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ class AT_CellularStack : public NetworkStack, public AT_CellularBase {
184184
private:
185185
int find_socket_index(nsapi_socket_t handle);
186186

187+
int get_socket_index_by_port(uint16_t port);
188+
187189
// mutex for write/read to a _socket array, needed when multiple threads may open sockets simultaneously
188190
PlatformMutex _socket_mutex;
189191
};

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ nsapi_error_t QUECTEL_BG96_CellularStack::socket_connect(nsapi_socket_t handle,
6262
handle_open_socket_response(modem_connect_id, err);
6363

6464
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+
}
6569
_at.cmd_start("AT+QICLOSE=");
6670
_at.write_int(modem_connect_id);
6771
_at.cmd_stop();
@@ -178,6 +182,10 @@ nsapi_error_t QUECTEL_BG96_CellularStack::create_socket_impl(CellularSocket *soc
178182
handle_open_socket_response(modem_connect_id, err);
179183

180184
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+
}
181189
_at.cmd_start("AT+QICLOSE=");
182190
_at.write_int(modem_connect_id);
183191
_at.cmd_stop_read_resp();
@@ -206,6 +214,10 @@ nsapi_error_t QUECTEL_BG96_CellularStack::create_socket_impl(CellularSocket *soc
206214
handle_open_socket_response(modem_connect_id, err);
207215

208216
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+
}
209221
_at.cmd_start("AT+QICLOSE=");
210222
_at.write_int(modem_connect_id);
211223
_at.cmd_stop_read_resp();
@@ -239,6 +251,14 @@ nsapi_error_t QUECTEL_BG96_CellularStack::create_socket_impl(CellularSocket *soc
239251
nsapi_size_or_error_t QUECTEL_BG96_CellularStack::socket_sendto_impl(CellularSocket *socket, const SocketAddress &address,
240252
const void *data, nsapi_size_t size)
241253
{
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+
242262
int sent_len = 0;
243263
int sent_len_before = 0;
244264
int sent_len_after = 0;
@@ -296,18 +316,27 @@ nsapi_size_or_error_t QUECTEL_BG96_CellularStack::socket_recvfrom_impl(CellularS
296316

297317
_at.cmd_start("AT+QIRD=");
298318
_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+
}
299324
_at.cmd_stop();
300325

301326
_at.resp_start("+QIRD:");
302327
recv_len = _at.read_int();
303328
_at.read_string(ip_address, sizeof(ip_address));
304329
port = _at.read_int();
305330
if (recv_len > 0) {
331+
// do not read more than buffer size
332+
recv_len = recv_len > size ? size : recv_len;
306333
_at.read_bytes((uint8_t *)buffer, recv_len);
307334
}
308335
_at.resp_stop();
309336

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) {
311340
return NSAPI_ERROR_WOULD_BLOCK;
312341
}
313342

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ 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
30+
#define BG96_SOCKET_BIND_FAIL 556
2831

2932
class QUECTEL_BG96_CellularStack : public AT_CellularStack {
3033
public:

0 commit comments

Comments
 (0)