diff --git a/src/TinyGsmClientA6.h b/src/TinyGsmClientA6.h index afb0f21..080af74 100644 --- a/src/TinyGsmClientA6.h +++ b/src/TinyGsmClientA6.h @@ -58,10 +58,6 @@ public: this->mux = -1; sock_connected = false; - // at->sockets[mux] = this; - // ^^ TODO: attach the socket here at init? Or later at connect? - // Currently done inconsistently between modems - return true; } @@ -75,23 +71,11 @@ public: if (sock_connected) { mux = newMux; at->sockets[mux] = this; - // ^^ TODO: attach the socket after attempting connection or above at init? - // Currently done inconsistently between modems } return sock_connected; } - virtual int connect(IPAddress ip, uint16_t port) { - String host; host.reserve(16); - host += ip[0]; - host += "."; - host += ip[1]; - host += "."; - host += ip[2]; - host += "."; - host += ip[3]; - return connect(host.c_str(), port); - } +TINY_GSM_CLIENT_CONNECT_TO_IP() virtual void stop() { TINY_GSM_YIELD(); @@ -101,67 +85,13 @@ public: rx.clear(); } - virtual size_t write(const uint8_t *buf, size_t size) { - TINY_GSM_YIELD(); - //at->maintain(); - return at->modemSend(buf, size, mux); - } +TINY_GSM_CLIENT_WRITE() - virtual size_t write(uint8_t c) { - return write(&c, 1); - } +TINY_GSM_CLIENT_AVAILABLE_NO_MODEM_FIFO() - virtual size_t write(const char *str) { - if (str == NULL) return 0; - return write((const uint8_t *)str, strlen(str)); - } +TINY_GSM_CLIENT_READ_NO_MODEM_FIFO() - virtual int available() { - TINY_GSM_YIELD(); - if (!rx.size() && sock_connected) { - at->maintain(); - } - return rx.size(); - } - - virtual int read(uint8_t *buf, size_t size) { - TINY_GSM_YIELD(); - size_t cnt = 0; - uint32_t _startMillis = millis(); - 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; - } - - virtual int read() { - uint8_t c; - if (read(&c, 1) == 1) { - return c; - } - return -1; - } - - virtual int peek() { return -1; } //TODO - virtual void flush() { at->stream.flush(); } - - virtual uint8_t connected() { - if (available()) { - return true; - } - return sock_connected; - } - virtual operator bool() { return connected(); } +TINY_GSM_CLIENT_PEEK_FLUSH_CONNECTED() /* * Extended API diff --git a/src/TinyGsmClientBG96.h b/src/TinyGsmClientBG96.h index f0c987a..3d6c323 100644 --- a/src/TinyGsmClientBG96.h +++ b/src/TinyGsmClientBG96.h @@ -62,8 +62,6 @@ public: got_data = false; at->sockets[mux] = this; - // ^^ TODO: attach the socket here at init? Or later at connect? - // Currently done inconsistently between modems return true; } @@ -74,24 +72,10 @@ public: TINY_GSM_YIELD(); rx.clear(); sock_connected = at->modemConnect(host, port, mux); - // sock_connected = at->modemConnect(host, port, &mux); - // at->sockets[mux] = this; - // ^^ TODO: attach the socket after attempting connection or above at init? - // Currently done inconsistently between modems return sock_connected; } - virtual int connect(IPAddress ip, uint16_t port) { - String host; host.reserve(16); - host += ip[0]; - host += "."; - host += ip[1]; - host += "."; - host += ip[2]; - host += "."; - host += ip[3]; - return connect(host.c_str(), port); - } +TINY_GSM_CLIENT_CONNECT_TO_IP() virtual void stop() { TINY_GSM_YIELD(); @@ -112,70 +96,13 @@ public: at->waitResponse(); } - virtual size_t write(const uint8_t *buf, size_t size) { - TINY_GSM_YIELD(); - at->maintain(); - return at->modemSend(buf, size, mux); - } +TINY_GSM_CLIENT_WRITE() - virtual size_t write(uint8_t c) { - return write(&c, 1); - } - - virtual size_t write(const char *str) { - if (str == NULL) return 0; - return write((const uint8_t *)str, strlen(str)); - } +TINY_GSM_CLIENT_AVAILABLE_NO_BUFFER_CHECK() - virtual int available() { - TINY_GSM_YIELD(); - if (!rx.size()) { - at->maintain(); - } - return rx.size() + sock_available; - } +TINY_GSM_CLIENT_READ_NO_BUFFER_CHECK() - virtual int read(uint8_t *buf, size_t size) { - TINY_GSM_YIELD(); - at->maintain(); - size_t cnt = 0; - 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) { - at->modemRead(TinyGsmMin((uint16_t)rx.free(), sock_available), mux); - } else { - break; - } - } - return cnt; - } - - virtual int read() { - uint8_t c; - if (read(&c, 1) == 1) { - return c; - } - return -1; - } - - virtual int peek() { return -1; } //TODO - virtual void flush() { at->stream.flush(); } - - virtual uint8_t connected() { - if (available()) { - return true; - } - return sock_connected; - } - virtual operator bool() { return connected(); } +TINY_GSM_CLIENT_PEEK_FLUSH_CONNECTED() /* * Extended API diff --git a/src/TinyGsmClientESP8266.h b/src/TinyGsmClientESP8266.h index c9ba8cb..5d14ed6 100644 --- a/src/TinyGsmClientESP8266.h +++ b/src/TinyGsmClientESP8266.h @@ -59,8 +59,6 @@ public: sock_connected = false; at->sockets[mux] = this; - // ^^ TODO: attach the socket here at init? Or later at connect? - // Currently done inconsistently between modems return true; } @@ -71,24 +69,10 @@ public: TINY_GSM_YIELD(); rx.clear(); sock_connected = at->modemConnect(host, port, mux); - // sock_connected = at->modemConnect(host, port, &mux); - // at->sockets[mux] = this; - // ^^ TODO: attach the socket after attempting connection or above at init? - // Currently done inconsistently between modems return sock_connected; } - virtual int connect(IPAddress ip, uint16_t port) { - String host; host.reserve(16); - host += ip[0]; - host += "."; - host += ip[1]; - host += "."; - host += ip[2]; - host += "."; - host += ip[3]; - return connect(host.c_str(), port); - } +TINY_GSM_CLIENT_CONNECT_TO_IP() virtual void stop() { TINY_GSM_YIELD(); @@ -98,67 +82,13 @@ public: rx.clear(); } - virtual size_t write(const uint8_t *buf, size_t size) { - TINY_GSM_YIELD(); - //at->maintain(); - return at->modemSend(buf, size, mux); - } - - virtual size_t write(uint8_t c) { - return write(&c, 1); - } +TINY_GSM_CLIENT_WRITE() - virtual size_t write(const char *str) { - if (str == NULL) return 0; - return write((const uint8_t *)str, strlen(str)); - } +TINY_GSM_CLIENT_AVAILABLE_NO_MODEM_FIFO() - virtual int available() { - TINY_GSM_YIELD(); - if (!rx.size() && sock_connected) { - at->maintain(); - } - return rx.size(); - } +TINY_GSM_CLIENT_READ_NO_MODEM_FIFO() - virtual int read(uint8_t *buf, size_t size) { - TINY_GSM_YIELD(); - size_t cnt = 0; - uint32_t _startMillis = millis(); - 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; - } - - virtual int read() { - uint8_t c; - if (read(&c, 1) == 1) { - return c; - } - return -1; - } - - virtual int peek() { return -1; } //TODO - virtual void flush() { at->stream.flush(); } - - virtual uint8_t connected() { - if (available()) { - return true; - } - return sock_connected; - } - virtual operator bool() { return connected(); } +TINY_GSM_CLIENT_PEEK_FLUSH_CONNECTED() /* * Extended API diff --git a/src/TinyGsmClientM590.h b/src/TinyGsmClientM590.h index fde7f87..52a2507 100644 --- a/src/TinyGsmClientM590.h +++ b/src/TinyGsmClientM590.h @@ -59,8 +59,6 @@ public: sock_connected = false; at->sockets[mux] = this; - // ^^ TODO: attach the socket here at init? Or later at connect? - // Currently done inconsistently between modems return true; } @@ -71,24 +69,11 @@ public: TINY_GSM_YIELD(); rx.clear(); sock_connected = at->modemConnect(host, port, mux); - // sock_connected = at->modemConnect(host, port, &mux); - // at->sockets[mux] = this; - // ^^ TODO: attach the socket after attempting connection or above at init? - // Currently done inconsistently between modems + return sock_connected; } - virtual int connect(IPAddress ip, uint16_t port) { - String host; host.reserve(16); - host += ip[0]; - host += "."; - host += ip[1]; - host += "."; - host += ip[2]; - host += "."; - host += ip[3]; - return connect(host.c_str(), port); - } +TINY_GSM_CLIENT_CONNECT_TO_IP() virtual void stop() { TINY_GSM_YIELD(); @@ -98,67 +83,13 @@ public: rx.clear(); } - virtual size_t write(const uint8_t *buf, size_t size) { - TINY_GSM_YIELD(); - //at->maintain(); - return at->modemSend(buf, size, mux); - } - - virtual size_t write(uint8_t c) { - return write(&c, 1); - } - - virtual size_t write(const char *str) { - if (str == NULL) return 0; - return write((const uint8_t *)str, strlen(str)); - } - - virtual int available() { - TINY_GSM_YIELD(); - if (!rx.size() && sock_connected) { - at->maintain(); - } - return rx.size(); - } - - virtual int read(uint8_t *buf, size_t size) { - TINY_GSM_YIELD(); - size_t cnt = 0; - uint32_t _startMillis = millis(); - 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; - } +TINY_GSM_CLIENT_WRITE() - virtual int read() { - uint8_t c; - if (read(&c, 1) == 1) { - return c; - } - return -1; - } +TINY_GSM_CLIENT_AVAILABLE_NO_MODEM_FIFO() - virtual int peek() { return -1; } //TODO - virtual void flush() { at->stream.flush(); } +TINY_GSM_CLIENT_READ_NO_MODEM_FIFO() - virtual uint8_t connected() { - if (available()) { - return true; - } - return sock_connected; - } - virtual operator bool() { return connected(); } +TINY_GSM_CLIENT_PEEK_FLUSH_CONNECTED() /* * Extended API @@ -167,7 +98,7 @@ public: String remoteIP() TINY_GSM_ATTR_NOT_IMPLEMENTED; private: - TinyGsmM590* at; + TinyGsmM590* at; uint8_t mux; bool sock_connected; RxFifo rx; diff --git a/src/TinyGsmClientM95.h b/src/TinyGsmClientM95.h index 4c51eaa..03225cc 100644 --- a/src/TinyGsmClientM95.h +++ b/src/TinyGsmClientM95.h @@ -62,8 +62,6 @@ public: got_data = false; at->sockets[mux] = this; - // ^^ TODO: attach the socket here at init? Or later at connect? - // Currently done inconsistently between modems return true; } @@ -74,24 +72,10 @@ public: TINY_GSM_YIELD(); rx.clear(); sock_connected = at->modemConnect(host, port, mux); - // sock_connected = at->modemConnect(host, port, &mux); - // at->sockets[mux] = this; - // ^^ TODO: attach the socket after attempting connection or above at init? - // Currently done inconsistently between modems return sock_connected; } - virtual int connect(IPAddress ip, uint16_t port) { - String host; host.reserve(16); - host += ip[0]; - host += "."; - host += ip[1]; - host += "."; - host += ip[2]; - host += "."; - host += ip[3]; - return connect(host.c_str(), port); - } +TINY_GSM_CLIENT_CONNECT_TO_IP() virtual void stop() { TINY_GSM_YIELD(); @@ -112,70 +96,13 @@ public: at->waitResponse(60000L, GF("CLOSED"), GF("CLOSE OK"), GF("ERROR")); } - virtual size_t write(const uint8_t *buf, size_t size) { - TINY_GSM_YIELD(); - at->maintain(); - return at->modemSend(buf, size, mux); - } +TINY_GSM_CLIENT_WRITE() - virtual size_t write(uint8_t c) { - return write(&c, 1); - } +TINY_GSM_CLIENT_AVAILABLE_NO_BUFFER_CHECK() - virtual size_t write(const char *str) { - if (str == NULL) return 0; - return write((const uint8_t *)str, strlen(str)); - } +TINY_GSM_CLIENT_READ_NO_BUFFER_CHECK() - virtual int available() { - TINY_GSM_YIELD(); - if (!rx.size()) { - at->maintain(); - } - return rx.size() + sock_available; - } - - virtual int read(uint8_t *buf, size_t size) { - TINY_GSM_YIELD(); - at->maintain(); - size_t cnt = 0; - 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) { - at->modemRead(TinyGsmMin((uint16_t)rx.free(), sock_available), mux); - } else { - break; - } - } - return cnt; - } - - virtual int read() { - uint8_t c; - if (read(&c, 1) == 1) { - return c; - } - return -1; - } - - virtual int peek() { return -1; } //TODO - virtual void flush() { at->stream.flush(); } - - virtual uint8_t connected() { - if (available()) { - return true; - } - return sock_connected; - } - virtual operator bool() { return connected(); } +TINY_GSM_CLIENT_PEEK_FLUSH_CONNECTED() /* * Extended API diff --git a/src/TinyGsmClientMC60.h b/src/TinyGsmClientMC60.h index 902ec72..b0d3901 100644 --- a/src/TinyGsmClientMC60.h +++ b/src/TinyGsmClientMC60.h @@ -66,8 +66,6 @@ public: got_data = false; at->sockets[mux] = this; - // ^^ TODO: attach the socket here at init? Or later at connect? - // Currently done inconsistently between modems return true; } @@ -78,24 +76,10 @@ public: TINY_GSM_YIELD(); rx.clear(); sock_connected = at->modemConnect(host, port, mux); - // sock_connected = at->modemConnect(host, port, &mux); - // at->sockets[mux] = this; - // ^^ TODO: attach the socket after attempting connection or above at init? - // Currently done inconsistently between modems return sock_connected; } - virtual int connect(IPAddress ip, uint16_t port) { - String host; host.reserve(16); - host += ip[0]; - host += "."; - host += ip[1]; - host += "."; - host += ip[2]; - host += "."; - host += ip[3]; - return connect(host.c_str(), port); - } +TINY_GSM_CLIENT_CONNECT_TO_IP() virtual void stop() { TINY_GSM_YIELD(); @@ -116,70 +100,13 @@ public: at->waitResponse(60000L, GF("CLOSED"), GF("CLOSE OK"), GF("ERROR")); } - virtual size_t write(const uint8_t *buf, size_t size) { - TINY_GSM_YIELD(); - at->maintain(); - return at->modemSend(buf, size, mux); - } +TINY_GSM_CLIENT_WRITE() - virtual size_t write(uint8_t c) { - return write(&c, 1); - } +TINY_GSM_CLIENT_AVAILABLE_NO_BUFFER_CHECK() - virtual size_t write(const char *str) { - if (str == NULL) return 0; - return write((const uint8_t *)str, strlen(str)); - } +TINY_GSM_CLIENT_READ_NO_BUFFER_CHECK() - virtual int available() { - TINY_GSM_YIELD(); - if (!rx.size()) { - at->maintain(); - } - return rx.size() + sock_available; - } - - virtual int read(uint8_t *buf, size_t size) { - TINY_GSM_YIELD(); - at->maintain(); - size_t cnt = 0; - 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) { - at->modemRead(TinyGsmMin((uint16_t)rx.free(), sock_available), mux); - } else { - break; - } - } - return cnt; - } - - virtual int read() { - uint8_t c; - if (read(&c, 1) == 1) { - return c; - } - return -1; - } - - virtual int peek() { return -1; } //TODO - virtual void flush() { at->stream.flush(); } - - virtual uint8_t connected() { - if (available()) { - return true; - } - return sock_connected; - } - virtual operator bool() { return connected(); } +TINY_GSM_CLIENT_PEEK_FLUSH_CONNECTED() /* * Extended API diff --git a/src/TinyGsmClientSIM7000.h b/src/TinyGsmClientSIM7000.h index 53468cf..9b7d543 100644 --- a/src/TinyGsmClientSIM7000.h +++ b/src/TinyGsmClientSIM7000.h @@ -71,8 +71,6 @@ public: got_data = false; at->sockets[mux] = this; - // ^^ TODO: attach the socket here at init? Or later at connect? - // Currently done inconsistently between modems return true; } @@ -83,24 +81,10 @@ public: TINY_GSM_YIELD(); rx.clear(); sock_connected = at->modemConnect(host, port, mux); - // sock_connected = at->modemConnect(host, port, &mux); - // at->sockets[mux] = this; - // ^^ TODO: attach the socket after attempting connection or above at init? - // Currently done inconsistently between modems return sock_connected; } - virtual int connect(IPAddress ip, uint16_t port) { - String host; host.reserve(16); - host += ip[0]; - host += "."; - host += ip[1]; - host += "."; - host += ip[2]; - host += "."; - host += ip[3]; - return connect(host.c_str(), port); - } +TINY_GSM_CLIENT_CONNECT_TO_IP() virtual void stop() { TINY_GSM_YIELD(); @@ -121,86 +105,13 @@ public: at->waitResponse(); } - virtual size_t write(const uint8_t *buf, size_t size) { - TINY_GSM_YIELD(); - at->maintain(); - return at->modemSend(buf, size, mux); - } - - virtual size_t write(uint8_t c) { - return write(&c, 1); - } +TINY_GSM_CLIENT_WRITE() - virtual size_t write(const char *str) { - if (str == NULL) return 0; - return write((const uint8_t *)str, strlen(str)); - } +TINY_GSM_CLIENT_AVAILABLE_WITH_BUFFER_CHECK() - virtual int available() { - TINY_GSM_YIELD(); - if (!rx.size()) { - // TODO: Is this needed for SIM7000? - // Workaround: sometimes SIM7000 forgets to notify about data arrival. - // TODO: Currently we ping the module periodically, - // but maybe there's a better indicator that we need to poll - if (millis() - prev_check > 250) { - got_data = true; - prev_check = millis(); - } - at->maintain(); - } - return rx.size() + sock_available; - } +TINY_GSM_CLIENT_READ_WITH_BUFFER_CHECK() - virtual int read(uint8_t *buf, size_t size) { - TINY_GSM_YIELD(); - at->maintain(); - size_t cnt = 0; - while (cnt < size) { - size_t chunk = TinyGsmMin(size-cnt, rx.size()); - if (chunk > 0) { - rx.get(buf, chunk); - buf += chunk; - cnt += chunk; - continue; - } - // TODO: Is this needed for SIM7000? - // Workaround: sometimes SIM7000 forgets to notify about data arrival. - // TODO: Currently we ping the module periodically, - // but maybe there's a better indicator that we need to poll - if (millis() - prev_check > 250) { - got_data = true; - prev_check = millis(); - } - // TODO: Read directly into user buffer? - at->maintain(); - if (sock_available > 0) { - at->modemRead(TinyGsmMin((uint16_t)rx.free(), sock_available), mux); - } else { - break; - } - } - return cnt; - } - - virtual int read() { - uint8_t c; - if (read(&c, 1) == 1) { - return c; - } - return -1; - } - - virtual int peek() { return -1; } //TODO - virtual void flush() { at->stream.flush(); } - - virtual uint8_t connected() { - if (available()) { - return true; - } - return sock_connected; - } - virtual operator bool() { return connected(); } +TINY_GSM_CLIENT_PEEK_FLUSH_CONNECTED() /* * Extended API diff --git a/src/TinyGsmClientSIM800.h b/src/TinyGsmClientSIM800.h index 20a2b61..5746db8 100644 --- a/src/TinyGsmClientSIM800.h +++ b/src/TinyGsmClientSIM800.h @@ -68,8 +68,6 @@ public: got_data = false; at->sockets[mux] = this; - // ^^ TODO: attach the socket here at init? Or later at connect? - // Currently done inconsistently between modems return true; } @@ -80,24 +78,10 @@ public: TINY_GSM_YIELD(); rx.clear(); sock_connected = at->modemConnect(host, port, mux); - // sock_connected = at->modemConnect(host, port, &mux); - // at->sockets[mux] = this; - // ^^ TODO: attach the socket after attempting connection or above at init? - // Currently done inconsistently between modems return sock_connected; } - virtual int connect(IPAddress ip, uint16_t port) { - String host; host.reserve(16); - host += ip[0]; - host += "."; - host += ip[1]; - host += "."; - host += ip[2]; - host += "."; - host += ip[3]; - return connect(host.c_str(), port); - } +TINY_GSM_CLIENT_CONNECT_TO_IP() virtual void stop() { TINY_GSM_YIELD(); @@ -118,84 +102,13 @@ public: at->waitResponse(); } - virtual size_t write(const uint8_t *buf, size_t size) { - TINY_GSM_YIELD(); - at->maintain(); - return at->modemSend(buf, size, mux); - } - - virtual size_t write(uint8_t c) { - return write(&c, 1); - } - - virtual size_t write(const char *str) { - if (str == NULL) return 0; - return write((const uint8_t *)str, strlen(str)); - } - - virtual int available() { - TINY_GSM_YIELD(); - if (!rx.size()) { - // Workaround: sometimes SIM800 forgets to notify about data arrival. - // TODO: Currently we ping the module periodically, - // but maybe there's a better indicator that we need to poll - if (millis() - prev_check > 250) { - got_data = true; - prev_check = millis(); - } - at->maintain(); - } - return rx.size() + sock_available; - } - - virtual int read(uint8_t *buf, size_t size) { - TINY_GSM_YIELD(); - at->maintain(); - size_t cnt = 0; - while (cnt < size) { - size_t chunk = TinyGsmMin(size-cnt, rx.size()); - if (chunk > 0) { - rx.get(buf, chunk); - buf += chunk; - cnt += chunk; - continue; - } - // Workaround: sometimes SIM800 forgets to notify about data arrival. - // TODO: Currently we ping the module periodically, - // but maybe there's a better indicator that we need to poll - if (millis() - prev_check > 250) { - got_data = true; - prev_check = millis(); - } - // TODO: Read directly into user buffer? - at->maintain(); - if (sock_available > 0) { - at->modemRead(TinyGsmMin((uint16_t)rx.free(), sock_available), mux); - } else { - break; - } - } - return cnt; - } +TINY_GSM_CLIENT_WRITE() - virtual int read() { - uint8_t c; - if (read(&c, 1) == 1) { - return c; - } - return -1; - } +TINY_GSM_CLIENT_AVAILABLE_WITH_BUFFER_CHECK() - virtual int peek() { return -1; } //TODO - virtual void flush() { at->stream.flush(); } +TINY_GSM_CLIENT_READ_WITH_BUFFER_CHECK() - virtual uint8_t connected() { - if (available()) { - return true; - } - return sock_connected; - } - virtual operator bool() { return connected(); } +TINY_GSM_CLIENT_PEEK_FLUSH_CONNECTED() /* * Extended API diff --git a/src/TinyGsmClientSaraR4.h b/src/TinyGsmClientSaraR4.h index b5cdf7a..fd2b797 100644 --- a/src/TinyGsmClientSaraR4.h +++ b/src/TinyGsmClientSaraR4.h @@ -101,17 +101,7 @@ public: return sock_connected; } - virtual int connect(IPAddress ip, uint16_t port) { - String host; host.reserve(16); - host += ip[0]; - host += "."; - host += ip[1]; - host += "."; - host += ip[2]; - host += "."; - host += ip[3]; - return connect(host.c_str(), port); - } +TINY_GSM_CLIENT_CONNECT_TO_IP() virtual void stop() { TINY_GSM_YIELD(); @@ -130,84 +120,13 @@ public: at->modemDisconnect(mux); } - virtual size_t write(const uint8_t *buf, size_t size) { - TINY_GSM_YIELD(); - at->maintain(); - return at->modemSend(buf, size, mux); - } - - virtual size_t write(uint8_t c) { - return write(&c, 1); - } - - virtual size_t write(const char *str) { - if (str == NULL) return 0; - return write((const uint8_t *)str, strlen(str)); - } - - virtual int available() { - TINY_GSM_YIELD(); - if (!rx.size()) { - // Workaround: sometimes SARA R410 forgets to notify about data arrival. - // TODO: Currently we ping the module periodically, - // but maybe there's a better indicator that we need to poll - if (millis() - prev_check > 250) { - got_data = true; - prev_check = millis(); - } - at->maintain(); - } - return rx.size() + sock_available; - } - - virtual int read(uint8_t *buf, size_t size) { - TINY_GSM_YIELD(); - at->maintain(); - size_t cnt = 0; - while (cnt < size) { - size_t chunk = TinyGsmMin(size-cnt, rx.size()); - if (chunk > 0) { - rx.get(buf, chunk); - buf += chunk; - cnt += chunk; - continue; - } - // Workaround: sometimes SARA R410 forgets to notify about data arrival. - // TODO: Currently we ping the module periodically, - // but maybe there's a better indicator that we need to poll - if (millis() - prev_check > 250) { - got_data = true; - prev_check = millis(); - } - // TODO: Read directly into user buffer? - at->maintain(); - if (sock_available > 0) { - at->modemRead(TinyGsmMin((uint16_t)rx.free(), sock_available), mux); - } else { - break; - } - } - return cnt; - } +TINY_GSM_CLIENT_WRITE() - virtual int read() { - uint8_t c; - if (read(&c, 1) == 1) { - return c; - } - return -1; - } +TINY_GSM_CLIENT_AVAILABLE_WITH_BUFFER_CHECK() - virtual int peek() { return -1; } //TODO - virtual void flush() { at->stream.flush(); } +TINY_GSM_CLIENT_READ_WITH_BUFFER_CHECK() - virtual uint8_t connected() { - if (available()) { - return true; - } - return sock_connected; - } - virtual operator bool() { return connected(); } +TINY_GSM_CLIENT_PEEK_FLUSH_CONNECTED() /* * Extended API diff --git a/src/TinyGsmClientSequansMonarch.h b/src/TinyGsmClientSequansMonarch.h index 70d34c3..339ca7b 100644 --- a/src/TinyGsmClientSequansMonarch.h +++ b/src/TinyGsmClientSequansMonarch.h @@ -91,17 +91,7 @@ public: return sock_connected; } - virtual int connect(IPAddress ip, uint16_t port) { - String host; host.reserve(16); - host += ip[0]; - host += "."; - host += ip[1]; - host += "."; - host += ip[2]; - host += "."; - host += ip[3]; - return connect(host.c_str(), port); - } +TINY_GSM_CLIENT_CONNECT_TO_IP() virtual void stop() { TINY_GSM_YIELD(); @@ -122,76 +112,13 @@ public: at->waitResponse(); } - virtual size_t write(const uint8_t *buf, size_t size) { - TINY_GSM_YIELD(); - at->maintain(); - return at->modemSend(buf, size, mux); - } +TINY_GSM_CLIENT_WRITE() - virtual size_t write(uint8_t c) { - return write(&c, 1); - } +TINY_GSM_CLIENT_AVAILABLE_WITH_BUFFER_CHECK() - virtual int available() { - TINY_GSM_YIELD(); - if (!rx.size()) { - // Workaround: sometimes unsolicited SQNSSRING notifications do not arrive. - if (millis() - prev_check > 250) { - got_data = true; - prev_check = millis(); - } - at->maintain(); - } - return rx.size() + sock_available; - } +TINY_GSM_CLIENT_READ_WITH_BUFFER_CHECK() - virtual int read(uint8_t *buf, size_t size) { - TINY_GSM_YIELD(); - at->maintain(); - size_t cnt = 0; - while (cnt < size) { - size_t chunk = TinyGsmMin(size-cnt, rx.size()); - if (chunk > 0) { - rx.get(buf, chunk); - buf += chunk; - cnt += chunk; - continue; - } - // Workaround: sometimes unsolicited SQNSSRING notifications do not arrive. - if (millis() - prev_check > 250) { - got_data = true; - prev_check = millis(); - } - // 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; - } - - virtual int read() { - uint8_t c; - if (read(&c, 1) == 1) { - return c; - } - return -1; - } - - virtual int peek() { return -1; } //TODO - virtual void flush() { at->stream.flush(); } - - virtual uint8_t connected() { - if (available()) { - return true; - } - return sock_connected; - } - virtual operator bool() { return connected(); } +TINY_GSM_CLIENT_PEEK_FLUSH_CONNECTED() /* * Extended API diff --git a/src/TinyGsmClientUBLOX.h b/src/TinyGsmClientUBLOX.h index a450b9b..5feb2d7 100644 --- a/src/TinyGsmClientUBLOX.h +++ b/src/TinyGsmClientUBLOX.h @@ -85,17 +85,7 @@ public: return sock_connected; } - virtual int connect(IPAddress ip, uint16_t port) { - String host; host.reserve(16); - host += ip[0]; - host += "."; - host += ip[1]; - host += "."; - host += ip[2]; - host += "."; - host += ip[3]; - return connect(host.c_str(), port); - } +TINY_GSM_CLIENT_CONNECT_TO_IP() virtual void stop() { TINY_GSM_YIELD(); @@ -114,84 +104,13 @@ public: at->modemDisconnect(mux); } - virtual size_t write(const uint8_t *buf, size_t size) { - TINY_GSM_YIELD(); - at->maintain(); - return at->modemSend(buf, size, mux); - } - - virtual size_t write(uint8_t c) { - return write(&c, 1); - } - - virtual size_t write(const char *str) { - if (str == NULL) return 0; - return write((const uint8_t *)str, strlen(str)); - } - - virtual int available() { - TINY_GSM_YIELD(); - if (!rx.size()) { - // Workaround: sometimes SARA R410 forgets to notify about data arrival. - // TODO: Currently we ping the module periodically, - // but maybe there's a better indicator that we need to poll - if (millis() - prev_check > 250) { - got_data = true; - prev_check = millis(); - } - at->maintain(); - } - return rx.size() + sock_available; - } - - virtual int read(uint8_t *buf, size_t size) { - TINY_GSM_YIELD(); - at->maintain(); - size_t cnt = 0; - while (cnt < size) { - size_t chunk = TinyGsmMin(size-cnt, rx.size()); - if (chunk > 0) { - rx.get(buf, chunk); - buf += chunk; - cnt += chunk; - continue; - } - // Workaround: sometimes SARA R410 forgets to notify about data arrival. - // TODO: Currently we ping the module periodically, - // but maybe there's a better indicator that we need to poll - if (millis() - prev_check > 250) { - got_data = true; - prev_check = millis(); - } - // TODO: Read directly into user buffer? - at->maintain(); - if (sock_available > 0) { - at->modemRead(TinyGsmMin((uint16_t)rx.free(), sock_available), mux); - } else { - break; - } - } - return cnt; - } +TINY_GSM_CLIENT_WRITE() ; - virtual int read() { - uint8_t c; - if (read(&c, 1) == 1) { - return c; - } - return -1; - } +TINY_GSM_CLIENT_AVAILABLE_WITH_BUFFER_CHECK() ; - virtual int peek() { return -1; } //TODO - virtual void flush() { at->stream.flush(); } +TINY_GSM_CLIENT_READ_WITH_BUFFER_CHECK() ; - virtual uint8_t connected() { - if (available()) { - return true; - } - return sock_connected; - } - virtual operator bool() { return connected(); } +TINY_GSM_CLIENT_PEEK_FLUSH_CONNECTED() /* * Extended API diff --git a/src/TinyGsmClientXBee.h b/src/TinyGsmClientXBee.h index 4cd0605..2a536c7 100644 --- a/src/TinyGsmClientXBee.h +++ b/src/TinyGsmClientXBee.h @@ -168,7 +168,7 @@ public: cnt += chunk; continue; } - // TODO: Read directly into user buffer? + /* TODO: Read directly into user buffer? */ if (!rx.size() || at->stream.available()) { at->maintain(); } diff --git a/src/TinyGsmCommon.h b/src/TinyGsmCommon.h index 3247dac..789e976 100644 --- a/src/TinyGsmCommon.h +++ b/src/TinyGsmCommon.h @@ -202,4 +202,204 @@ String TinyGsmDecodeHex16bit(String &instr) { return result; } + +// Connect to a IP address given as an IPAddress object by +// converting said IP address to text +#define TINY_GSM_CLIENT_CONNECT_TO_IP() \ + virtual int connect(IPAddress ip, uint16_t port) { \ + String host; host.reserve(16); \ + host += ip[0]; \ + host += "."; \ + host += ip[1]; \ + host += "."; \ + host += ip[2]; \ + host += "."; \ + host += ip[3]; \ + return connect(host.c_str(), port); \ + } + + +// Writes data out on the client using the modem send functionality +#define TINY_GSM_CLIENT_WRITE() \ + virtual size_t write(const uint8_t *buf, size_t size) { \ + TINY_GSM_YIELD(); \ + at->maintain(); \ + return at->modemSend(buf, size, mux); \ + } \ + \ + virtual size_t write(uint8_t c) {\ + return write(&c, 1); \ + }\ + \ + virtual size_t write(const char *str) { \ + if (str == NULL) return 0; \ + return write((const uint8_t *)str, strlen(str)); \ + } + + +// 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. +#define TINY_GSM_CLIENT_AVAILABLE_WITH_BUFFER_CHECK() \ + virtual int available() { \ + TINY_GSM_YIELD(); \ + if (!rx.size()) { \ + /* Workaround: sometimes module forgets to notify about data arrival. + TODO: Currently we ping the module periodically, + but maybe there's a better indicator that we need to poll */ \ + if (millis() - prev_check > 250) { \ + got_data = true; \ + prev_check = millis(); \ + } \ + at->maintain(); \ + } \ + return rx.size() + sock_available; \ + } + + +// Returns the combined number of characters available in the TinyGSM fifo and +// the modem chips internal fifo. Use this if you don't expect to miss any URC's. +#define TINY_GSM_CLIENT_AVAILABLE_NO_BUFFER_CHECK() \ + virtual int available() { \ + TINY_GSM_YIELD(); \ + if (!rx.size()) { \ + at->maintain(); \ + } \ + return rx.size() + sock_available; \ + } + + +// Returns the number of characters avaialable in the TinyGSM fifo +// Assumes the modem chip has no internal fifo +#define TINY_GSM_CLIENT_AVAILABLE_NO_MODEM_FIFO() \ + virtual int available() { \ + TINY_GSM_YIELD(); \ + if (!rx.size() && sock_connected) { \ + at->maintain(); \ + } \ + return rx.size(); \ + } + + +#define TINY_GSM_CLIENT_READ_OVERLOAD() \ + virtual int read() { \ + uint8_t c; \ + if (read(&c, 1) == 1) { \ + return c; \ + } \ + return -1; \ + } + +// 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. +#define TINY_GSM_CLIENT_READ_WITH_BUFFER_CHECK() \ + virtual int read(uint8_t *buf, size_t size) { \ + TINY_GSM_YIELD(); \ + at->maintain(); \ + size_t cnt = 0; \ + while (cnt < size) { \ + size_t chunk = TinyGsmMin(size-cnt, rx.size()); \ + if (chunk > 0) { \ + rx.get(buf, chunk); \ + buf += chunk; \ + cnt += chunk; \ + continue; \ + } \ + /* Workaround: sometimes module forgets to notify about data arrival. + TODO: Currently we ping the module periodically, + but maybe there's a better indicator that we need to poll */ \ + if (millis() - prev_check > 250) { \ + got_data = true; \ + prev_check = millis(); \ + } \ + /* 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; \ + } \ + TINY_GSM_CLIENT_READ_OVERLOAD() + + +// Reads characters out of the TinyGSM fifo, and from the modem chips internal +// fifo if avaiable. Use this if you don't expect to miss any URC's. +#define TINY_GSM_CLIENT_READ_NO_BUFFER_CHECK() \ + virtual int read(uint8_t *buf, size_t size) { \ + TINY_GSM_YIELD(); \ + at->maintain(); \ + size_t cnt = 0; \ + 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; \ + } \ + TINY_GSM_CLIENT_READ_OVERLOAD() + + +// 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. This assumes the +//modem chip itself has no fifo. +#define TINY_GSM_CLIENT_READ_NO_MODEM_FIFO() \ + virtual int read(uint8_t *buf, size_t size) { \ + TINY_GSM_YIELD(); \ + size_t cnt = 0; \ + uint32_t _startMillis = millis(); \ + 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; \ + } \ + \ + virtual int read() { \ + uint8_t c; \ + if (read(&c, 1) == 1) { \ + return c; \ + } \ + return -1; \ + } + + +#define TINY_GSM_CLIENT_PEEK_FLUSH_CONNECTED() \ + virtual int peek() { return -1; } /* TODO */ \ + \ + virtual void flush() { at->stream.flush(); } \ + \ + virtual uint8_t connected() { \ + if (available()) { \ + return true; \ + } \ + return sock_connected; \ + } \ + virtual operator bool() { return connected(); } + #endif