Skip to content

Commit

Permalink
Refactored modemGetAvailable
Browse files Browse the repository at this point in the history
  • Loading branch information
Bascy committed Sep 3, 2024
1 parent 8325c5c commit c057ea6
Showing 1 changed file with 20 additions and 43 deletions.
63 changes: 20 additions & 43 deletions src/TinyGsmClientSIM7080.h
Original file line number Diff line number Diff line change
Expand Up @@ -570,58 +570,35 @@ class TinyGsmSim7080 : public TinyGsmSim70xx<TinyGsmSim7080>,
}

size_t modemGetAvailable(uint8_t mux) {
// If the socket doesn't exist, just return
if (!sockets[mux]) {
return 0;
// Reset all sock_available values to 0
for (int muxNo = 0; muxNo < TINY_GSM_MUX_COUNT; muxNo++) {
GsmClientSim7080* sock = sockets[muxNo];
if (sock) {
sock->sock_available = 0;
}
}
// NOTE: This gets how many characters are available on all connections that
// have data. It does not return all the connections, just those with data.

// Request awaiting data, will return zero or more +CARECV: answers, ending with an OK
sendAT(GF("+CARECV?"));
for (int muxNo = 0; muxNo < TINY_GSM_MUX_COUNT; muxNo++) {
// after the last connection, there's an ok, so we catch it right away
int res = waitResponse(3000, GF("+CARECV:"), GFP(GSM_OK), GFP(GSM_ERROR));
// if we get the +CARECV: response, read the mux number and the number of
// characters available
if (res == 1) {

while (int result = waitResponse(3000, GF("+CARECV:"), GFP(GSM_OK), GFP(GSM_ERROR))) {
if (result == 1) {
// if we get the +CARECV: response, read the mux number and the number of
// characters available
int ret_mux = streamGetIntBefore(',');
size_t result = streamGetIntBefore('\n');
GsmClientSim7080* sock = sockets[ret_mux];
if (sock) {
sock->sock_available = result;
}
// if the first returned mux isn't 0 (or is higher than expected)
// we need to fill in the missing muxes
if (ret_mux > muxNo) {
for (int extra_mux = muxNo; extra_mux < ret_mux + 1; extra_mux++) {
GsmClientSim7080* isock = sockets[extra_mux];
if (isock) {
isock->sock_available = result;
}
}
muxNo = ret_mux;
}
} else if (res == 2) {
// if we get an OK, we've reached the last socket with available data
// so we set any we haven't gotten to yet to 0
for (int extra_mux = muxNo; extra_mux < TINY_GSM_MUX_COUNT;
extra_mux++) {
GsmClientSim7080* isock = sockets[extra_mux];
if (isock) {
isock->sock_available = 0;
size_t charsAvailable = streamGetIntBefore('\n');
if (0 <= ret_mux && ret_mux < TINY_GSM_MUX_COUNT) {
GsmClientSim7080* sock = sockets[ret_mux];
if (sock) {
sock->sock_available = charsAvailable;
}
}
break;
} else {
// if we got an error, give up
// Ok, timeout or Error -> we're done
break;
}
// Should be a final OK at the end.
// If every connection was returned, catch the OK here.
// If only a portion were returned, catch it above.
if (muxNo == TINY_GSM_MUX_COUNT - 1) {
waitResponse();
}
}

modemGetConnected(mux); // check the state of all connections
if (!sockets[mux]) {
return 0;
Expand Down

0 comments on commit c057ea6

Please sign in to comment.