Skip to content

Commit 306943a

Browse files
Mirela ChiricaCruz Monrreal II
authored andcommitted
Cellular: Fix socket bind tests for BG96
1 parent 3926223 commit 306943a

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

features/cellular/framework/AT/AT_CellularStack.cpp

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

200200
if (addr) {
201-
socket->localAddress.set_addr(addr.get_addr());
202-
}
203-
204-
if (addr.get_port()) {
205-
socket->localAddress.set_port(addr.get_port());
201+
return NSAPI_ERROR_UNSUPPORTED;
206202
}
207203

208204
_at.lock();
209205

206+
uint16_t port = addr.get_port();
207+
if (port != socket->localAddress.get_port()) {
208+
if (port && (get_socket_index_by_port(port) == -1)) {
209+
socket->localAddress.set_port(port);
210+
} else {
211+
_at.unlock();
212+
return NSAPI_ERROR_PARAMETER;
213+
}
214+
}
215+
210216
if (!socket->created) {
211217
create_socket_impl(socket);
212218
}
@@ -340,3 +346,14 @@ void AT_CellularStack::socket_attach(nsapi_socket_t handle, void (*callback)(voi
340346
socket->_cb = callback;
341347
socket->_data = data;
342348
}
349+
350+
int AT_CellularStack::get_socket_index_by_port(uint16_t port)
351+
{
352+
int max_socket_count = get_max_socket_count();
353+
for (int i = 0; i < max_socket_count; i++) {
354+
if (_socket[i]->localAddress.get_port() == port) {
355+
return i;
356+
}
357+
}
358+
return -1;
359+
}

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: 12 additions & 0 deletions
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();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ 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_SOCKET_BIND_FAIL 556
2829

2930
class QUECTEL_BG96_CellularStack : public AT_CellularStack {
3031
public:

0 commit comments

Comments
 (0)