Skip to content

Commit 2e53a71

Browse files
Hasnain VirkAri Parkkila
authored andcommitted
Updating M26 to accomodate socket id assignment
This modem is a special case. It uses a given socket ID value rather than providing one. A naive solution here would be to directly map the index of a CellularSocket object in the CellularSocket container. But considering the case where there are multiple sockets being opened (some sockets being already created at the modem and some yet not created), direct mapping to indices will not work. As it can happen that the CellularSocket object is allocated but the socket id is not assigned yet as it is not actually created on the modem. In such a case, we check the container and assign the socket id from the pool if an empty slot was found.
1 parent 3fffa3b commit 2e53a71

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

features/cellular/framework/targets/QUECTEL/M26/QUECTEL_M26_CellularStack.cpp

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ nsapi_error_t QUECTEL_M26_CellularStack::socket_connect(nsapi_socket_t handle, c
302302
{
303303
CellularSocket *socket = (CellularSocket *)handle;
304304

305+
MBED_ASSERT(socket->id != -1);
306+
305307
int modem_connect_id = -1;
306308
int request_connect_id = socket->id;
307309
int err = -1;
@@ -348,7 +350,6 @@ nsapi_error_t QUECTEL_M26_CellularStack::socket_connect(nsapi_socket_t handle, c
348350
_at.unlock();
349351

350352
if ((ret_val == NSAPI_ERROR_OK) && (modem_connect_id == request_connect_id)) {
351-
socket->created = true;
352353
socket->remoteAddress = address;
353354
socket->connected = true;
354355
return NSAPI_ERROR_OK;
@@ -359,7 +360,29 @@ nsapi_error_t QUECTEL_M26_CellularStack::socket_connect(nsapi_socket_t handle, c
359360

360361
nsapi_error_t QUECTEL_M26_CellularStack::create_socket_impl(CellularSocket *socket)
361362
{
362-
int request_connect_id = socket->id;
363+
// This modem is a special case. It takes in the socket ID rather than spitting
364+
// it out. So we will first try to use the index of the socket construct as the id
365+
// but if another opened socket is already opened with that id we will pick the next
366+
// id which is not in use
367+
bool duplicate = false;
368+
int potential_sid = -1;
369+
int index = find_socket_index(socket);
370+
371+
for (int i = 0; i < get_max_socket_count(); i++) {
372+
CellularSocket *sock = _socket[i];
373+
if (sock && sock != socket && sock->id == index) {
374+
duplicate = true;
375+
} else if (duplicate && !sock) {
376+
potential_sid = i;
377+
break;
378+
}
379+
}
380+
381+
if (duplicate) {
382+
index = potential_sid;
383+
}
384+
385+
int request_connect_id = index;
363386
int modem_connect_id = request_connect_id;
364387
int err = -1;
365388
nsapi_error_t ret_val;
@@ -403,13 +426,15 @@ nsapi_error_t QUECTEL_M26_CellularStack::create_socket_impl(CellularSocket *sock
403426
}
404427

405428
ret_val = _at.get_last_error();
406-
socket->created = ((ret_val == NSAPI_ERROR_OK) && (modem_connect_id == request_connect_id));
429+
if ((ret_val == NSAPI_ERROR_OK) && (modem_connect_id == request_connect_id)) {
430+
socket->id = request_connect_id;
431+
}
407432
return ret_val;
408433
} else {
409434
ret_val = NSAPI_ERROR_OK;
410435
}
411436

412-
tr_debug("QUECTEL_M26_CellularStack:%s:%u: END [%d,%d]", __FUNCTION__, __LINE__, socket->created, ret_val);
437+
tr_debug("QUECTEL_M26_CellularStack:%s:%u: END [%d]", __FUNCTION__, __LINE__, ret_val);
413438
return ret_val;
414439
}
415440

@@ -431,11 +456,11 @@ nsapi_size_or_error_t QUECTEL_M26_CellularStack::socket_sendto_impl(CellularSock
431456
return NSAPI_ERROR_PARAMETER;
432457
}
433458

434-
if (!socket->created) {
459+
if (socket->id == -1) {
435460
socket->remoteAddress = address;
436461
socket->connected = true;
437462
nsapi_error_t ret_val = create_socket_impl(socket);
438-
if ((ret_val != NSAPI_ERROR_OK) || (!socket->created)) {
463+
if ((ret_val != NSAPI_ERROR_OK) || (socket->id == -1)) {
439464
tr_error("QUECTEL_M26_CellularStack:%s:%u:[NSAPI_ERROR_NO_SOCKET]", __FUNCTION__, __LINE__);
440465
return NSAPI_ERROR_NO_SOCKET;
441466
}

0 commit comments

Comments
 (0)