Change from switch case to required define

Signed-off-by: Sara Damiano <sdamiano@stroudcenter.org>
This commit is contained in:
Sara Damiano
2020-02-17 13:07:01 -05:00
parent 88620dee7b
commit 2616a3d642
15 changed files with 246 additions and 267 deletions

View File

@@ -19,8 +19,8 @@
#define TINY_GSM_RX_BUFFER 64
#endif
// Because of the ordering of resolution of overrides in templates, hese need to
// be written out every time. This macro is to shorten that.
// Because of the ordering of resolution of overrides in templates, these need
// to be written out every time. This macro is to shorten that.
#define TINY_GSM_CLIENT_CONNECT_OVERRIDES \
virtual int connect(IPAddress ip, uint16_t port, int timeout_s) { \
return connect(TinyGsmStringFromIp(ip).c_str(), port, timeout_s); \
@@ -32,16 +32,16 @@
return connect(ip, port, 75); \
}
enum modemInternalBuffferType {
NO_MODEM_BUFFER =
0, // For modules that do not store incoming data in any sort of buffer
READ_NO_CHECK = 1, // Data is stored in a buffer, but we can only read from
// the buffer, not check how much data is stored in it
READ_AND_CHECK_SIZE = 2, // Data is stored in a buffer and we can both read
// and check the size of the buffer
};
// // For modules that do not store incoming data in any sort of buffer
// #define TINY_GSM_NO_MODEM_BUFFER
// // Data is stored in a buffer, but we can only read from the buffer,
// // not check how much data is stored in it
// #define TINY_GSM_BUFFER_READ_NO_CHECK
// // Data is stored in a buffer and we can both read and check the size
// // of the buffer
// #define TINY_GSM_BUFFER_READ_AND_CHECK_SIZE
template <class modemType, modemInternalBuffferType bufType, uint8_t muxCount>
template <class modemType, uint8_t muxCount>
class TinyGsmTCP {
public:
/*
@@ -68,7 +68,7 @@ class TinyGsmTCP {
public:
class GsmClient : public Client {
// Make all classes created from the modem template friends
friend class TinyGsmTCP<modemType, bufType, muxCount>;
friend class TinyGsmTCP<modemType, muxCount>;
typedef TinyGsmFifo<uint8_t, TINY_GSM_RX_BUFFER> RxFifo;
public:
@@ -77,14 +77,14 @@ class TinyGsmTCP {
// Connect to a IP address given as an IPAddress object by
// converting said IP address to text
// virtual int connect(IPAddress ip, uint16_t port, int timeout_s) {
// virtual int connect(IPAddress ip,uint16_t port, int timeout_s) {
// return connect(TinyGsmStringFromIp(ip).c_str(), port,
// timeout_s);
// }
// int connect(const char* host, uint16_t port) override {
// return connect(host, port, 75);
// }
// int connect(IPAddress ip, uint16_t port) override {
// int connect(IPAddress ip,uint16_t port) override {
// return connect(ip, port, 75);
// }
@@ -124,107 +124,111 @@ class TinyGsmTCP {
int available() override {
TINY_GSM_YIELD();
switch (bufType) {
// Returns the number of characters available in the TinyGSM fifo
case NO_MODEM_BUFFER:
if (!rx.size() && sock_connected) { at->maintain(); }
return rx.size();
#if defined TINY_GSM_NO_MODEM_BUFFER
// Returns the number of characters available in the TinyGSM fifo
if (!rx.size() && sock_connected) { at->maintain(); }
return rx.size();
// Returns the combined number of characters available in the TinyGSM
// fifo and the modem chips internal fifo.
case READ_NO_CHECK:
if (!rx.size()) { at->maintain(); }
return rx.size() + sock_available;
#elif defined TINY_GSM_NO_MODEM_BUFFER
// Returns the combined number of characters available in the TinyGSM
// fifo and the modem chips internal fifo.
if (!rx.size()) { at->maintain(); }
return rx.size() + sock_available;
// Returns the combined number of characters available in the TinyGSM
// fifo and the modem chips internal fifo, doing an extra check-in
// with the modem to see if anything has arrived without a UURC.
case READ_AND_CHECK_SIZE:
if (!rx.size()) {
if (millis() - prev_check > 500) {
got_data = true;
prev_check = millis();
}
at->maintain();
}
return rx.size() + sock_available;
#elif defined TINY_GSM_BUFFER_READ_AND_CHECK_SIZE
// Returns the combined number of characters available in the TinyGSM
// fifo and the modem chips internal fifo, doing an extra check-in
// with the modem to see if anything has arrived without a UURC.
if (!rx.size()) {
if (millis() - prev_check > 500) {
got_data = true;
prev_check = millis();
}
at->maintain();
}
return rx.size() + sock_available;
#else
#error Modem client has been incorrectly created
#endif
}
int read(uint8_t* buf, size_t size) override {
TINY_GSM_YIELD();
size_t cnt = 0;
size_t cnt = 0;
#if defined TINY_GSM_NO_MODEM_BUFFER
// Reads characters out of the TinyGSM fifo, waiting for any URC's
// from the modem for new data if there's nothing in the fifo.
uint32_t _startMillis = millis();
switch (bufType) {
// Reads characters out of the TinyGSM fifo, waiting for any URC's
// from the modem for new data if there's nothing in the fifo.
case NO_MODEM_BUFFER:
while (cnt < size && millis() - _startMillis < _timeout) {
size_t chunk = TinyGsmMin(size - cnt, rx.size());
if (chunk > 0) {
rx.get(buf, chunk);
buf += chunk;
cnt += chunk;
continue;
} /* TODO: Read directly into user buffer? */
if (!rx.size() && sock_connected) { at->maintain(); }
}
return cnt;
// Reads characters out of the TinyGSM fifo, and from the modem chip's
// internal fifo if avaiable.
case READ_NO_CHECK:
at->maintain();
while (cnt < size) {
size_t chunk = TinyGsmMin(size - cnt, rx.size());
if (chunk > 0) {
rx.get(buf, chunk);
buf += chunk;
cnt += chunk;
continue;
} /* TODO: Read directly into user buffer? */
at->maintain();
if (sock_available > 0) {
int n = at->modemRead(
TinyGsmMin((uint16_t)rx.free(), sock_available), mux);
if (n == 0) break;
} else {
break;
}
}
return cnt;
// Reads characters out of the TinyGSM fifo, and from the modem chips
// internal fifo if avaiable, also double checking with the modem if
// data has arrived without issuing a UURC.
case READ_AND_CHECK_SIZE:
at->maintain();
while (cnt < size) {
size_t chunk = TinyGsmMin(size - cnt, rx.size());
if (chunk > 0) {
rx.get(buf, chunk);
buf += chunk;
cnt += chunk;
continue;
}
// Workaround: Some modules "forget" to notify about data arrival
if (millis() - prev_check > 500) {
got_data = true;
prev_check = millis();
}
// TODO(vshymanskyy): Read directly into user buffer?
at->maintain();
if (sock_available > 0) {
int n = at->modemRead(
TinyGsmMin((uint16_t)rx.free(), sock_available), mux);
if (n == 0) break;
} else {
break;
}
}
return cnt;
while (cnt < size && millis() - _startMillis < _timeout) {
size_t chunk = TinyGsmMin(size - cnt, rx.size());
if (chunk > 0) {
rx.get(buf, chunk);
buf += chunk;
cnt += chunk;
continue;
} /* TODO: Read directly into user buffer? */
if (!rx.size() && sock_connected) { at->maintain(); }
}
return cnt;
#elif defined TINY_GSM_BUFFER_READ_NO_CHECK
// Reads characters out of the TinyGSM fifo, and from the modem chip's
// internal fifo if avaiable.
at->maintain();
while (cnt < size) {
size_t chunk = TinyGsmMin(size - cnt, rx.size());
if (chunk > 0) {
rx.get(buf, chunk);
buf += chunk;
cnt += chunk;
continue;
} /* TODO: Read directly into user buffer? */
at->maintain();
if (sock_available > 0) {
int n = at->modemRead(TinyGsmMin((uint16_t)rx.free(), sock_available),
mux);
if (n == 0) break;
} else {
break;
}
}
return cnt;
#elif defined TINY_GSM_BUFFER_READ_AND_CHECK_SIZE
// Reads characters out of the TinyGSM fifo, and from the modem chips
// internal fifo if avaiable, also double checking with the modem if
// data has arrived without issuing a UURC.
at->maintain();
while (cnt < size) {
size_t chunk = TinyGsmMin(size - cnt, rx.size());
if (chunk > 0) {
rx.get(buf, chunk);
buf += chunk;
cnt += chunk;
continue;
}
// Workaround: Some modules "forget" to notify about data arrival
if (millis() - prev_check > 500) {
got_data = true;
prev_check = millis();
}
// TODO(vshymanskyy): Read directly into user buffer?
at->maintain();
if (sock_available > 0) {
int n = at->modemRead(TinyGsmMin((uint16_t)rx.free(), sock_available),
mux);
if (n == 0) break;
} else {
break;
}
}
return cnt;
#else
#error Modem client has been incorrectly created
#endif
}
int read() override {
@@ -264,12 +268,21 @@ class TinyGsmTCP {
// Doing it this way allows the external mcu to find and get all of the
// data that it wants from the socket even if it was closed externally.
inline void dumpModemBuffer(uint32_t maxWaitMs) {
#if defined TINY_GSM_BUFFER_READ_AND_CHECK_SIZE || \
defined TINY_GSM_BUFFER_READ_NO_CHECK
TINY_GSM_YIELD();
uint32_t startMillis = millis();
do {
rx.clear();
at->modemRead(TinyGsmMin((uint16_t)rx.free(), sock_available), mux);
} while (sock_available > 0 && (millis() - startMillis < maxWaitMs));
#elif defined TINY_GSM_NO_MODEM_BUFFER
// Do nothing
#else
#error Modem client has been incorrectly created
#endif
}
modemType* at;
@@ -286,26 +299,27 @@ class TinyGsmTCP {
*/
protected:
void maintainImpl() {
switch (bufType) {
case READ_AND_CHECK_SIZE:
// Keep listening for modem URC's and proactively iterate through
// sockets asking if any data is avaiable
for (int mux = 0; mux < muxCount; mux++) {
GsmClient* sock = thisModem().sockets[mux];
if (sock && sock->got_data) {
sock->got_data = false;
sock->sock_available = thisModem().modemGetAvailable(mux);
}
}
while (thisModem().stream.available()) {
thisModem().waitResponse(15, NULL, NULL);
}
break;
default:
// Just listen for any URC's
thisModem().waitResponse(100, NULL, NULL);
break;
#if defined TINY_GSM_BUFFER_READ_AND_CHECK_SIZE
// Keep listening for modem URC's and proactively iterate through
// sockets asking if any data is avaiable
for (int mux = 0; mux < muxCount; mux++) {
GsmClient* sock = thisModem().sockets[mux];
if (sock && sock->got_data) {
sock->got_data = false;
sock->sock_available = thisModem().modemGetAvailable(mux);
}
}
while (thisModem().stream.available()) {
thisModem().waitResponse(15, NULL, NULL);
}
#elif defined TINY_GSM_NO_MODEM_BUFFER || defined TINY_GSM_BUFFER_READ_NO_CHECK
// Just listen for any URC's
thisModem().waitResponse(100, NULL, NULL);
#else
#error Modem client has been incorrectly created
#endif
}
// Yields up to a time-out period and then reads a character from the stream