Condensed client functions into pre-processor macros

This commit is contained in:
Sara Damiano
2019-05-17 12:23:35 -04:00
parent 4c8104dfb6
commit 85d309621d
13 changed files with 258 additions and 897 deletions

View File

@@ -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);
}
TINY_GSM_CLIENT_WRITE()
virtual size_t write(uint8_t c) {
return write(&c, 1);
}
TINY_GSM_CLIENT_AVAILABLE_WITH_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_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;
}
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