Browse Source

Add stop with timeout and implement where applicable

v_master
Sara Damiano 5 years ago
parent
commit
86356f5323
13 changed files with 76 additions and 156 deletions
  1. +4
    -2
      src/TinyGsmClientA6.h
  2. +4
    -14
      src/TinyGsmClientBG96.h
  3. +4
    -2
      src/TinyGsmClientESP8266.h
  4. +4
    -2
      src/TinyGsmClientM590.h
  5. +5
    -15
      src/TinyGsmClientM95.h
  6. +5
    -15
      src/TinyGsmClientMC60.h
  7. +4
    -14
      src/TinyGsmClientSIM7000.h
  8. +4
    -14
      src/TinyGsmClientSIM800.h
  9. +7
    -30
      src/TinyGsmClientSaraR4.h
  10. +4
    -14
      src/TinyGsmClientSequansMonarch.h
  11. +7
    -30
      src/TinyGsmClientUBLOX.h
  12. +5
    -3
      src/TinyGsmClientXBee.h
  13. +19
    -1
      src/TinyGsmCommon.h

+ 4
- 2
src/TinyGsmClientA6.h View File

@ -81,14 +81,16 @@ public:
TINY_GSM_CLIENT_CONNECT_OVERLOADS() TINY_GSM_CLIENT_CONNECT_OVERLOADS()
virtual void stop() {
virtual void stop(uint32_t maxWaitMs) {
TINY_GSM_YIELD(); TINY_GSM_YIELD();
at->sendAT(GF("+CIPCLOSE="), mux); at->sendAT(GF("+CIPCLOSE="), mux);
sock_connected = false; sock_connected = false;
at->waitResponse();
at->waitResponse(maxWaitMs);
rx.clear(); rx.clear();
} }
virtual void stop() { stop(1000L); }
TINY_GSM_CLIENT_WRITE() TINY_GSM_CLIENT_WRITE()
TINY_GSM_CLIENT_AVAILABLE_NO_MODEM_FIFO() TINY_GSM_CLIENT_AVAILABLE_NO_MODEM_FIFO()


+ 4
- 14
src/TinyGsmClientBG96.h View File

@ -82,25 +82,15 @@ public:
TINY_GSM_CLIENT_CONNECT_OVERLOADS() TINY_GSM_CLIENT_CONNECT_OVERLOADS()
virtual void stop() {
TINY_GSM_YIELD();
// Read and dump anything remaining in the modem's internal buffer.
// The socket will appear open in response to connected() even after it
// closes until all data is read from the buffer.
// 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.
rx.clear();
at->maintain();
while (sock_available > 0) {
at->modemRead(TinyGsmMin((uint16_t)rx.free(), sock_available), mux);
rx.clear();
at->maintain();
}
virtual void stop(uint32_t maxWaitMs) {
TINY_GSM_CLIENT_DUMP_MODEM_BUFFER()
at->sendAT(GF("+QICLOSE="), mux); at->sendAT(GF("+QICLOSE="), mux);
sock_connected = false; sock_connected = false;
at->waitResponse(); at->waitResponse();
} }
virtual void stop() { stop(15000L); }
TINY_GSM_CLIENT_WRITE() TINY_GSM_CLIENT_WRITE()
TINY_GSM_CLIENT_AVAILABLE_WITH_BUFFER_CHECK() TINY_GSM_CLIENT_AVAILABLE_WITH_BUFFER_CHECK()


+ 4
- 2
src/TinyGsmClientESP8266.h View File

@ -78,14 +78,16 @@ public:
TINY_GSM_CLIENT_CONNECT_OVERLOADS() TINY_GSM_CLIENT_CONNECT_OVERLOADS()
virtual void stop() {
virtual void stop(uint32_t maxWaitMs) {
TINY_GSM_YIELD(); TINY_GSM_YIELD();
at->sendAT(GF("+CIPCLOSE="), mux); at->sendAT(GF("+CIPCLOSE="), mux);
sock_connected = false; sock_connected = false;
at->waitResponse(5000L);
at->waitResponse(maxWaitMs);
rx.clear(); rx.clear();
} }
virtual void stop() { stop(5000L); }
TINY_GSM_CLIENT_WRITE() TINY_GSM_CLIENT_WRITE()
TINY_GSM_CLIENT_AVAILABLE_NO_MODEM_FIFO() TINY_GSM_CLIENT_AVAILABLE_NO_MODEM_FIFO()


+ 4
- 2
src/TinyGsmClientM590.h View File

@ -79,14 +79,16 @@ public:
TINY_GSM_CLIENT_CONNECT_OVERLOADS() TINY_GSM_CLIENT_CONNECT_OVERLOADS()
virtual void stop() {
virtual void stop(uint32_t maxWaitMs) {
TINY_GSM_YIELD(); TINY_GSM_YIELD();
at->sendAT(GF("+TCPCLOSE="), mux); at->sendAT(GF("+TCPCLOSE="), mux);
sock_connected = false; sock_connected = false;
at->waitResponse();
at->waitResponse(maxWaitMs);
rx.clear(); rx.clear();
} }
virtual void stop() { stop(1000L); }
TINY_GSM_CLIENT_WRITE() TINY_GSM_CLIENT_WRITE()
TINY_GSM_CLIENT_AVAILABLE_NO_MODEM_FIFO() TINY_GSM_CLIENT_AVAILABLE_NO_MODEM_FIFO()


+ 5
- 15
src/TinyGsmClientM95.h View File

@ -81,25 +81,15 @@ public:
TINY_GSM_CLIENT_CONNECT_OVERLOADS() TINY_GSM_CLIENT_CONNECT_OVERLOADS()
virtual void stop() {
TINY_GSM_YIELD();
// Read and dump anything remaining in the modem's internal buffer.
// The socket will appear open in response to connected() even after it
// closes until all data is read from the buffer.
// 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.
rx.clear();
at->maintain();
while (sock_available > 0) {
at->modemRead(TinyGsmMin((uint16_t)rx.free(), sock_available), mux);
rx.clear();
at->maintain();
}
virtual void stop(uint32_t maxWaitMs) {
TINY_GSM_CLIENT_DUMP_MODEM_BUFFER()
at->sendAT(GF("+QICLOSE="), mux); at->sendAT(GF("+QICLOSE="), mux);
sock_connected = false; sock_connected = false;
at->waitResponse(60000L, GF("CLOSED"), GF("CLOSE OK"), GF("ERROR"));
at->waitResponse((maxWaitMs - (millis() - startMillis)), GF("CLOSED"), GF("CLOSE OK"), GF("ERROR"));
} }
virtual void stop() { stop(75000L); }
TINY_GSM_CLIENT_WRITE() TINY_GSM_CLIENT_WRITE()
TINY_GSM_CLIENT_AVAILABLE_NO_BUFFER_CHECK() TINY_GSM_CLIENT_AVAILABLE_NO_BUFFER_CHECK()


+ 5
- 15
src/TinyGsmClientMC60.h View File

@ -85,25 +85,15 @@ public:
TINY_GSM_CLIENT_CONNECT_OVERLOADS() TINY_GSM_CLIENT_CONNECT_OVERLOADS()
virtual void stop() {
TINY_GSM_YIELD();
// Read and dump anything remaining in the modem's internal buffer.
// The socket will appear open in response to connected() even after it
// closes until all data is read from the buffer.
// 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.
rx.clear();
at->maintain();
while (sock_available > 0) {
at->modemRead(TinyGsmMin((uint16_t)rx.free(), sock_available), mux);
rx.clear();
at->maintain();
}
virtual void stop(uint32_t maxWaitMs) {
TINY_GSM_CLIENT_DUMP_MODEM_BUFFER()
at->sendAT(GF("+QICLOSE="), mux); at->sendAT(GF("+QICLOSE="), mux);
sock_connected = false; sock_connected = false;
at->waitResponse(60000L, GF("CLOSED"), GF("CLOSE OK"), GF("ERROR"));
at->waitResponse((maxWaitMs - (millis() - startMillis)), GF("CLOSED"), GF("CLOSE OK"), GF("ERROR"));
} }
virtual void stop() { stop(75000L); }
TINY_GSM_CLIENT_WRITE() TINY_GSM_CLIENT_WRITE()
TINY_GSM_CLIENT_AVAILABLE_NO_BUFFER_CHECK() TINY_GSM_CLIENT_AVAILABLE_NO_BUFFER_CHECK()


+ 4
- 14
src/TinyGsmClientSIM7000.h View File

@ -86,25 +86,15 @@ public:
TINY_GSM_CLIENT_CONNECT_OVERLOADS() TINY_GSM_CLIENT_CONNECT_OVERLOADS()
virtual void stop() {
TINY_GSM_YIELD();
// Read and dump anything remaining in the modem's internal buffer.
// The socket will appear open in response to connected() even after it
// closes until all data is read from the buffer.
// 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.
rx.clear();
at->maintain();
while (sock_available > 0) {
at->modemRead(TinyGsmMin((uint16_t)rx.free(), sock_available), mux);
rx.clear();
at->maintain();
}
virtual void stop(uint32_t maxWaitMs) {
TINY_GSM_CLIENT_DUMP_MODEM_BUFFER()
at->sendAT(GF("+CIPCLOSE="), mux); at->sendAT(GF("+CIPCLOSE="), mux);
sock_connected = false; sock_connected = false;
at->waitResponse(); at->waitResponse();
} }
virtual void stop() { stop(15000L); }
TINY_GSM_CLIENT_WRITE() TINY_GSM_CLIENT_WRITE()
TINY_GSM_CLIENT_AVAILABLE_WITH_BUFFER_CHECK() TINY_GSM_CLIENT_AVAILABLE_WITH_BUFFER_CHECK()


+ 4
- 14
src/TinyGsmClientSIM800.h View File

@ -87,25 +87,15 @@ public:
TINY_GSM_CLIENT_CONNECT_OVERLOADS() TINY_GSM_CLIENT_CONNECT_OVERLOADS()
virtual void stop() {
TINY_GSM_YIELD();
// Read and dump anything remaining in the modem's internal buffer.
// The socket will appear open in response to connected() even after it
// closes until all data is read from the buffer.
// 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.
rx.clear();
at->maintain();
while (sock_available > 0) {
at->modemRead(TinyGsmMin((uint16_t)rx.free(), sock_available), mux);
rx.clear();
at->maintain();
}
virtual void stop(uint32_t maxWaitMs) {
TINY_GSM_CLIENT_DUMP_MODEM_BUFFER()
at->sendAT(GF("+CIPCLOSE="), mux, GF(",1")); // Quick close at->sendAT(GF("+CIPCLOSE="), mux, GF(",1")); // Quick close
sock_connected = false; sock_connected = false;
at->waitResponse(); at->waitResponse();
} }
virtual void stop() { stop(15000L); }
TINY_GSM_CLIENT_WRITE() TINY_GSM_CLIENT_WRITE()
TINY_GSM_CLIENT_AVAILABLE_WITH_BUFFER_CHECK() TINY_GSM_CLIENT_AVAILABLE_WITH_BUFFER_CHECK()


+ 7
- 30
src/TinyGsmClientSaraR4.h View File

@ -91,23 +91,15 @@ public:
TINY_GSM_CLIENT_CONNECT_OVERLOADS() TINY_GSM_CLIENT_CONNECT_OVERLOADS()
virtual void stop() {
TINY_GSM_YIELD();
// Read and dump anything remaining in the modem's internal buffer.
// The socket will appear open in response to connected() even after it
// closes until all data is read from the buffer.
// 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.
rx.clear();
at->maintain();
while (sock_connected && sock_available > 0) {
at->modemRead(TinyGsmMin((uint16_t)rx.free(), sock_available), mux);
rx.clear();
at->maintain();
}
at->modemDisconnect(mux);
virtual void stop(uint32_t maxWaitMs) {
TINY_GSM_CLIENT_DUMP_MODEM_BUFFER()
at->sendAT(GF("+USOCL="), mux);
at->waitResponse((maxWaitMs - (millis() - startMillis))); // NOTE: can take up to 120s to get a response
sock_connected = false;
} }
virtual void stop() { stop(135000L); }
TINY_GSM_CLIENT_WRITE() TINY_GSM_CLIENT_WRITE()
TINY_GSM_CLIENT_AVAILABLE_WITH_BUFFER_CHECK() TINY_GSM_CLIENT_AVAILABLE_WITH_BUFFER_CHECK()
@ -569,21 +561,6 @@ protected:
return (1 == rsp); return (1 == rsp);
} }
bool modemDisconnect(uint8_t mux) {
TINY_GSM_YIELD();
if (!modemGetConnected(mux)) {
sockets[mux]->sock_connected = false;
return true;
}
bool success;
sendAT(GF("+USOCL="), mux);
success = 1 == waitResponse(120000L); // can take up to 120s to get a response
if (success) {
sockets[mux]->sock_connected = false;
}
return success;
}
int16_t modemSend(const void* buff, size_t len, uint8_t mux) { int16_t modemSend(const void* buff, size_t len, uint8_t mux) {
sendAT(GF("+USOWR="), mux, ',', len); sendAT(GF("+USOWR="), mux, ',', len);
if (waitResponse(GF("@")) != 1) { if (waitResponse(GF("@")) != 1) {


+ 4
- 14
src/TinyGsmClientSequansMonarch.h View File

@ -93,25 +93,15 @@ public:
TINY_GSM_CLIENT_CONNECT_OVERLOADS() TINY_GSM_CLIENT_CONNECT_OVERLOADS()
virtual void stop() {
TINY_GSM_YIELD();
// Read and dump anything remaining in the modem's internal buffer.
// The socket will appear open in response to connected() even after it
// closes until all data is read from the buffer.
// 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.
rx.clear();
at->maintain();
while (sock_available > 0) {
at->modemRead(TinyGsmMin((uint16_t)rx.free(), sock_available), mux);
rx.clear();
at->maintain();
}
virtual void stop(uint32_t maxWaitMs) {
TINY_GSM_CLIENT_DUMP_MODEM_BUFFER()
at->sendAT(GF("+SQNSH="), mux); at->sendAT(GF("+SQNSH="), mux);
sock_connected = false; sock_connected = false;
at->waitResponse(); at->waitResponse();
} }
virtual void stop() { stop(15000L); }
TINY_GSM_CLIENT_WRITE() TINY_GSM_CLIENT_WRITE()
TINY_GSM_CLIENT_AVAILABLE_WITH_BUFFER_CHECK() TINY_GSM_CLIENT_AVAILABLE_WITH_BUFFER_CHECK()


+ 7
- 30
src/TinyGsmClientUBLOX.h View File

@ -91,23 +91,15 @@ public:
TINY_GSM_CLIENT_CONNECT_OVERLOADS() TINY_GSM_CLIENT_CONNECT_OVERLOADS()
virtual void stop() {
TINY_GSM_YIELD();
// Read and dump anything remaining in the modem's internal buffer.
// The socket will appear open in response to connected() even after it
// closes until all data is read from the buffer.
// 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.
rx.clear();
at->maintain();
while (sock_connected && sock_available > 0) {
at->modemRead(TinyGsmMin((uint16_t)rx.free(), sock_available), mux);
rx.clear();
at->maintain();
}
at->modemDisconnect(mux);
virtual void stop(uint32_t maxWaitMs) {
TINY_GSM_CLIENT_DUMP_MODEM_BUFFER()
at->sendAT(GF("+USOCL="), mux);
at->waitResponse(); // should return within 1s
sock_connected = false;
} }
virtual void stop() { stop(15000L); }
TINY_GSM_CLIENT_WRITE() TINY_GSM_CLIENT_WRITE()
TINY_GSM_CLIENT_AVAILABLE_WITH_BUFFER_CHECK() TINY_GSM_CLIENT_AVAILABLE_WITH_BUFFER_CHECK()
@ -580,21 +572,6 @@ protected:
return (1 == rsp); return (1 == rsp);
} }
bool modemDisconnect(uint8_t mux) {
TINY_GSM_YIELD();
if (!modemGetConnected(mux)) {
sockets[mux]->sock_connected = false;
return true;
}
bool success;
sendAT(GF("+USOCL="), mux);
success = 1 == waitResponse(); // should return within 1s
if (success) {
sockets[mux]->sock_connected = false;
}
return success;
}
int16_t modemSend(const void* buff, size_t len, uint8_t mux) { int16_t modemSend(const void* buff, size_t len, uint8_t mux) {
sendAT(GF("+USOWR="), mux, ',', len); sendAT(GF("+USOWR="), mux, ',', len);
if (waitResponse(GF("@")) != 1) { if (waitResponse(GF("@")) != 1) {


+ 5
- 3
src/TinyGsmClientXBee.h View File

@ -116,21 +116,21 @@ public:
return connect(ip, port, 75); return connect(ip, port, 75);
} }
virtual void stop() {
virtual void stop(uint32_t maxWaitMs) {
at->streamClear(); // Empty anything in the buffer at->streamClear(); // Empty anything in the buffer
at->commandMode(); at->commandMode();
// For WiFi models, there's no direct way to close the socket. This is a // For WiFi models, there's no direct way to close the socket. This is a
// hack to shut the socket by setting the timeout to zero. // hack to shut the socket by setting the timeout to zero.
if (at->beeType == XBEE_S6B_WIFI) { if (at->beeType == XBEE_S6B_WIFI) {
at->sendAT(GF("TM0")); // Set socket timeout (using Digi default of 10 seconds) at->sendAT(GF("TM0")); // Set socket timeout (using Digi default of 10 seconds)
at->waitResponse(5000); // This response can be slow
at->waitResponse(maxWaitMs); // This response can be slow
at->writeChanges(); at->writeChanges();
} }
// For cellular models, per documentation: If you change the TM (socket // For cellular models, per documentation: If you change the TM (socket
// timeout) value while in Transparent Mode, the current connection is // timeout) value while in Transparent Mode, the current connection is
// immediately closed. // immediately closed.
at->sendAT(GF("TM64")); // Set socket timeout (using Digi default of 10 seconds) at->sendAT(GF("TM64")); // Set socket timeout (using Digi default of 10 seconds)
at->waitResponse(5000); // This response can be slow
at->waitResponse(maxWaitMs); // This response can be slow
at->writeChanges(); at->writeChanges();
at->exitCommand(); at->exitCommand();
at->streamClear(); // Empty anything remaining in the buffer at->streamClear(); // Empty anything remaining in the buffer
@ -142,6 +142,8 @@ public:
// much more nicely with libraries like PubSubClient. // much more nicely with libraries like PubSubClient.
} }
virtual void stop() { stop(5000L); }
virtual size_t write(const uint8_t *buf, size_t size) { virtual size_t write(const uint8_t *buf, size_t size) {
TINY_GSM_YIELD(); TINY_GSM_YIELD();
return at->modemSend(buf, size, mux); return at->modemSend(buf, size, mux);


+ 19
- 1
src/TinyGsmCommon.h View File

@ -275,7 +275,7 @@ String TinyGsmDecodeHex16bit(String &instr) {
} }
// Returns the number of characters avaialable in the TinyGSM fifo
// Returns the number of characters available in the TinyGSM fifo
// Assumes the modem chip has no internal fifo // Assumes the modem chip has no internal fifo
#define TINY_GSM_CLIENT_AVAILABLE_NO_MODEM_FIFO() \ #define TINY_GSM_CLIENT_AVAILABLE_NO_MODEM_FIFO() \
virtual int available() { \ virtual int available() { \
@ -395,6 +395,24 @@ String TinyGsmDecodeHex16bit(String &instr) {
} }
// Read and dump anything remaining in the modem's internal buffer.
// Using this in the client stop() function.
// The socket will appear open in response to connected() even after it
// closes until all data is read from the buffer.
// 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.
#define TINY_GSM_CLIENT_DUMP_MODEM_BUFFER() \
TINY_GSM_YIELD(); \
rx.clear(); \
at->maintain(); \
unsigned long startMillis = millis(); \
while (sock_available > 0 && (millis() - startMillis < maxWaitMs)) { \
at->modemRead(TinyGsmMin((uint16_t)rx.free(), sock_available), mux); \
rx.clear(); \
at->maintain(); \
}
// The peek, flush, and connected functions // The peek, flush, and connected functions
#define TINY_GSM_CLIENT_PEEK_FLUSH_CONNECTED() \ #define TINY_GSM_CLIENT_PEEK_FLUSH_CONNECTED() \
virtual int peek() { return -1; } /* TODO */ \ virtual int peek() { return -1; } /* TODO */ \


Loading…
Cancel
Save