Skip to content

Commit

Permalink
Refactored implementations of modemGetAvaiableI() for Sim7000 and Sim…
Browse files Browse the repository at this point in the history
…7000SSL
  • Loading branch information
Bascy committed Sep 4, 2024
1 parent 422b49a commit 0a92468
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 61 deletions.
16 changes: 7 additions & 9 deletions src/TinyGsmClientSIM7000.h
Original file line number Diff line number Diff line change
Expand Up @@ -449,12 +449,12 @@ class TinyGsmSim7000 : public TinyGsmSim70xx<TinyGsmSim7000>,

size_t modemGetAvailable(uint8_t mux) {

// Reset sock_available on all sockets
for (int muxNo = 0; muxNo < TINY_GSM_MUX_COUNT; muxNo++) {
GsmClientSim7000* isock = sockets[muxNo];
if (isock) {
isock->sock_available = 0;
}
if (sockets[mux]) {
sockets[mux]->sock_connected = modemGetConnected(mux);
}

if (!sockets[mux]->sock_connected) {
return 0;
}

sendAT(GF("+CIPRXGET=4,"), mux);
Expand All @@ -470,9 +470,7 @@ class TinyGsmSim7000 : public TinyGsmSim70xx<TinyGsmSim7000>,
break;
}
// DBG("### Available:", result, "on", mux);
if (!result) {
sockets[mux]->sock_connected = modemGetConnected(mux);
}
sockets[mux]->sock_available = result;
return result;
}

Expand Down
71 changes: 19 additions & 52 deletions src/TinyGsmClientSIM7000SSL.h
Original file line number Diff line number Diff line change
Expand Up @@ -570,68 +570,35 @@ class TinyGsmSim7000SSL
}

size_t modemGetAvailable(uint8_t mux) {
// We need to check if there are any connections open *before* checking for
// available characters. The SIM7000 *will crash* if you ask about data
// when there are no open connections.
if (!modemGetConnected(mux)) {
return 0;
}

// Reset sock_available on all sockets
// Reset all sock_available values to 0
for (int muxNo = 0; muxNo < TINY_GSM_MUX_COUNT; muxNo++) {
GsmClientSim7000SSL* isock = sockets[muxNo];
if (isock) {
isock->sock_available = 0;
GsmClientSim7000SSL* 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) {
int ret_mux = streamGetIntBefore(',');
size_t result = streamGetIntBefore('\n');
GsmClientSim7000SSL* 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; extra_mux++) {
GsmClientSim7000SSL* isock = sockets[extra_mux];
if (isock) {
isock->sock_available = 0;
}
}
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++) {
GsmClientSim7000SSL* isock = sockets[extra_mux];
if (isock) {
isock->sock_available = 0;

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 charsAvailable = streamGetIntBefore('\n');
if (0 <= ret_mux && ret_mux < TINY_GSM_MUX_COUNT) {
GsmClientSim7000SSL* 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 0a92468

Please sign in to comment.