Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed modemGetAvailable() and modemGetConnected() when socket[0] is no connected #637

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 53 additions & 63 deletions src/TinyGsmClientSIM7000SSL.h
Original file line number Diff line number Diff line change
Expand Up @@ -485,63 +485,67 @@ class TinyGsmSim7000SSL
}

size_t modemGetAvailable(uint8_t mux) {
// If the socket doesn't exist, just return
if (!sockets[mux]) { return 0; }
// 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; }
// 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.
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; }

// Reset sock_available on all sockets
for (int muxNo = 0; muxNo < TINY_GSM_MUX_COUNT; muxNo++) {
GsmClientSim7000SSL* isock = sockets[muxNo];
if (isock) {
isock->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.
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;
}
muxNo = ret_mux;
} else if (res == 3) {
// if we got an error, give up
break;
}
} 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; }
// 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();
}
break;
} else {
// if we got an error, give up
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(0); // check the state of all connections, discard result of mux 0 (arbitrary)
if (!sockets[mux]) {
return 0;
}
return sockets[mux]->sock_available;
}

modemGetConnected(mux); // check the state of all connections
if (!sockets[mux]) { return 0; }
return sockets[mux]->sock_available;
}

bool modemGetConnected(uint8_t mux) {
bool modemGetConnected(uint8_t mux) {

// Reset sock_connected on all sockets
for (int muxNo = 0; muxNo < TINY_GSM_MUX_COUNT; muxNo++) {
GsmClientSim7000SSL* isock = sockets[muxNo];
if (isock) {
isock->sock_connected = false;
}
}
// NOTE: This gets the state of all connections that have been opened
// since the last connection
sendAT(GF("+CASTATE?"));

for (int muxNo = 0; muxNo < TINY_GSM_MUX_COUNT; muxNo++) {
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("+CASTATE:"), GFP(GSM_OK),
GFP(GSM_ERROR));
Expand All @@ -553,33 +557,19 @@ class TinyGsmSim7000SSL
// 1: Connected to remote server
// 2: Listening (server mode)
GsmClientSim7000SSL* sock = sockets[ret_mux];
if (sock) { sock->sock_connected = (status == 1); }
// 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_connected = false; }
}
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_connected = false; }
if (sock) {
sock->sock_connected = (status == 1);
}
break;
} else {
} else if (res == 3) {
// if we got an error, give up
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(); }
if (muxNo == TINY_GSM_MUX_COUNT - 1) {
waitResponse();
}
}
return sockets[mux]->sock_connected;
}
Expand Down