Skip to content

Commit a8eec8a

Browse files
author
Jindrich Dolezy
committed
Add timeouts to GSMClient
1 parent 2550487 commit a8eec8a

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

src/GSMClient.cpp

+40-4
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ GSMClient::GSMClient(int socket, bool synch) :
5151
_host(NULL),
5252
_port(0),
5353
_ssl(false),
54-
_writeSync(true)
54+
_writeSync(true),
55+
_connectTimeout(30000UL),
56+
_socketTimeout(15000UL)
5557
{
5658
MODEM.addUrcHandler(this);
5759
}
@@ -233,15 +235,27 @@ int GSMClient::connect()
233235
}
234236

235237
if (_synch) {
236-
while (ready() == 0);
238+
unsigned long start = millis();
239+
while (ready() == 0) {
240+
if (_connectTimeout && !((millis() - start) < _connectTimeout)) {
241+
stop();
242+
return 2;
243+
}
244+
}
237245
} else if (ready() == 0) {
238246
return 0;
239247
}
240248

241249
_state = CLIENT_STATE_CREATE_SOCKET;
242250

243251
if (_synch) {
252+
unsigned long start = millis();
244253
while (ready() == 0) {
254+
if (_connectTimeout && !((millis() - start) < _connectTimeout)) {
255+
stop();
256+
return 2;
257+
}
258+
245259
delay(100);
246260
}
247261

@@ -271,7 +285,13 @@ size_t GSMClient::write(const uint8_t *buf)
271285
size_t GSMClient::write(const uint8_t* buf, size_t size)
272286
{
273287
if (_writeSync) {
274-
while (ready() == 0);
288+
unsigned long start = millis();
289+
while (ready() == 0) {
290+
if (_socketTimeout && !((millis() - start) < _socketTimeout)) {
291+
stop();
292+
return 0;
293+
}
294+
}
275295
} else if (ready() == 0) {
276296
return 0;
277297
}
@@ -385,7 +405,13 @@ int GSMClient::read()
385405
int GSMClient::available()
386406
{
387407
if (_synch) {
388-
while (ready() == 0);
408+
unsigned long start = millis();
409+
while (ready() == 0) {
410+
if (_socketTimeout && !((millis() - start) < _socketTimeout)) {
411+
stop();
412+
return 0;
413+
}
414+
}
389415
} else if (ready() == 0) {
390416
return 0;
391417
}
@@ -434,6 +460,16 @@ void GSMClient::stop()
434460
_connected = false;
435461
}
436462

463+
void GSMClient::setSocketTimeout(unsigned long timeout)
464+
{
465+
_socketTimeout = timeout;
466+
}
467+
468+
void GSMClient::setConnectTimeout(unsigned long timeout)
469+
{
470+
_connectTimeout = timeout;
471+
}
472+
437473
void GSMClient::handleUrc(const String& urc)
438474
{
439475
if (urc.startsWith("+UUSORD: ")) {

src/GSMClient.h

+11
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,14 @@ class GSMClient : public Client, public ModemUrcHandler {
128128
*/
129129
void stop();
130130

131+
/** Set timeout for connecting
132+
*/
133+
void setConnectTimeout(unsigned long timeout);
134+
135+
/** Set timeout for socket operations
136+
*/
137+
void setSocketTimeout(unsigned long timeout);
138+
131139
virtual void handleUrc(const String& urc);
132140

133141
private:
@@ -145,6 +153,9 @@ class GSMClient : public Client, public ModemUrcHandler {
145153

146154
bool _writeSync;
147155
String _response;
156+
157+
unsigned long _connectTimeout;
158+
unsigned long _socketTimeout;
148159
};
149160

150161
#endif

0 commit comments

Comments
 (0)