Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timeouts to GSMClient #83

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 40 additions & 4 deletions src/GSMClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ GSMClient::GSMClient(int socket, bool synch) :
_host(NULL),
_port(0),
_ssl(false),
_writeSync(true)
_writeSync(true),
_connectTimeout(30000UL),
_socketTimeout(15000UL)
{
MODEM.addUrcHandler(this);
}
Expand Down Expand Up @@ -233,15 +235,27 @@ int GSMClient::connect()
}

if (_synch) {
while (ready() == 0);
unsigned long start = millis();
while (ready() == 0) {
if (_connectTimeout && !((millis() - start) < _connectTimeout)) {
stop();
return 2;
}
}
} else if (ready() == 0) {
return 0;
}

_state = CLIENT_STATE_CREATE_SOCKET;

if (_synch) {
unsigned long start = millis();
while (ready() == 0) {
if (_connectTimeout && !((millis() - start) < _connectTimeout)) {
stop();
return 2;
}

delay(100);
}

Expand Down Expand Up @@ -271,7 +285,13 @@ size_t GSMClient::write(const uint8_t *buf)
size_t GSMClient::write(const uint8_t* buf, size_t size)
{
if (_writeSync) {
while (ready() == 0);
unsigned long start = millis();
while (ready() == 0) {
if (_socketTimeout && !((millis() - start) < _socketTimeout)) {
stop();
return 0;
}
}
} else if (ready() == 0) {
return 0;
}
Expand Down Expand Up @@ -385,7 +405,13 @@ int GSMClient::read()
int GSMClient::available()
{
if (_synch) {
while (ready() == 0);
unsigned long start = millis();
while (ready() == 0) {
if (_socketTimeout && !((millis() - start) < _socketTimeout)) {
stop();
return 0;
}
}
} else if (ready() == 0) {
return 0;
}
Expand Down Expand Up @@ -434,6 +460,16 @@ void GSMClient::stop()
_connected = false;
}

void GSMClient::setSocketTimeout(unsigned long timeout)
{
_socketTimeout = timeout;
}

void GSMClient::setConnectTimeout(unsigned long timeout)
{
_connectTimeout = timeout;
}

void GSMClient::handleUrc(const String& urc)
{
if (urc.startsWith("+UUSORD: ")) {
Expand Down
11 changes: 11 additions & 0 deletions src/GSMClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ class GSMClient : public Client, public ModemUrcHandler {
*/
void stop();

/** Set timeout for connecting
*/
void setConnectTimeout(unsigned long timeout);

/** Set timeout for socket operations
*/
void setSocketTimeout(unsigned long timeout);

virtual void handleUrc(const String& urc);

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

bool _writeSync;
String _response;

unsigned long _connectTimeout;
unsigned long _socketTimeout;
};

#endif