@ -58,6 +58,7 @@ public:
class GsmClient : public Client
class GsmClient : public Client
{
{
friend class TinyGsmXBee ;
friend class TinyGsmXBee ;
/ / typedef TinyGsmFifo < uint8_t , TINY_GSM_RX_BUFFER > RxFifo ;
public :
public :
GsmClient ( ) { }
GsmClient ( ) { }
@ -78,45 +79,45 @@ public:
public :
public :
virtual int connect ( const char * host , uint16_t port ) {
virtual int connect ( const char * host , uint16_t port ) {
at - > streamClear ( ) ; / / Empty anything remaining in the buffer ;
bool sock_connected = false ;
at - > streamClear ( ) ; / / Empty anything in the buffer before starting
if ( at - > commandMode ( ) ) { / / Don ' t try if we didn ' t successfully get into command mode
if ( at - > commandMode ( ) ) { / / Don ' t try if we didn ' t successfully get into command mode
sock_connected = at - > modemConnect ( host , port , mux , false ) ;
sock_connected = at - > modemConnect ( host , port , mux , false ) ;
at - > writeChanges ( ) ;
at - > writeChanges ( ) ;
at - > exitCommand ( ) ;
at - > exitCommand ( ) ;
}
}
at - > streamClear ( ) ; / / Empty anything remaining in the buffer ;
else
sock_connected = false ;
return sock_connected ;
return sock_connected ;
}
}
virtual int connect ( IPAddress ip , uint16_t port ) {
virtual int connect ( IPAddress ip , uint16_t port ) {
at - > streamClear ( ) ; / / Empty anything remaining in the buffer ;
bool sock_connected = false ;
at - > streamClear ( ) ; / / Empty anything in the buffer before starting
if ( at - > commandMode ( ) ) { / / Don ' t try if we didn ' t successfully get into command mode
if ( at - > commandMode ( ) ) { / / Don ' t try if we didn ' t successfully get into command mode
sock_connected = at - > modemConnect ( ip , port , mux , false ) ;
sock_connected = at - > modemConnect ( ip , port , mux , false ) ;
at - > writeChanges ( ) ;
at - > writeChanges ( ) ;
at - > exitCommand ( ) ;
at - > exitCommand ( ) ;
}
}
at - > streamClear ( ) ; / / Empty anything remaining in the buffer ;
else
sock_connected = false ;
return sock_connected ;
return sock_connected ;
}
}
/ / This is a hack to shut the socket by setting the timeout to zero and
/ / This is a hack to shut the socket by setting the timeout to zero and
/ / then sending an empty line to the server .
/ / then sending an empty line to the server .
virtual void stop ( ) {
virtual void stop ( ) {
at - > streamClear ( ) ; / / Empty anything remaining in the buffer ;
at - > streamClear ( ) ; / / Empty anything in the buffer
at - > commandMode ( ) ;
at - > commandMode ( ) ;
at - > sendAT ( GF ( " TM0 " ) ) ; / / Set socket timeout to 0 ;
at - > sendAT ( GF ( " TM0 " ) ) ; / / Set socket timeout to 0 ;
at - > waitResponse ( ) ;
at - > waitResponse ( ) ;
at - > writeChanges ( ) ;
at - > writeChanges ( ) ;
at - > exitCommand ( ) ;
at - > exitCommand ( ) ;
at - > modemSend ( " " , 1 , mux ) ;
at - > streamWrite ( " " ) ;
at - > commandMode ( ) ;
at - > commandMode ( ) ;
at - > sendAT ( GF ( " TM64 " ) ) ; / / Set socket timeout back to 10 seconds ;
at - > sendAT ( GF ( " TM64 " ) ) ; / / Set socket timeout back to 10 seconds ;
at - > waitResponse ( ) ;
at - > waitResponse ( ) ;
at - > writeChanges ( ) ;
at - > writeChanges ( ) ;
at - > exitCommand ( ) ;
at - > exitCommand ( ) ;
at - > streamClear ( ) ; / / Empty anything remaining in the buffer ;
at - > streamClear ( ) ; / / Empty anything remaining in the buffer
sock_connected = false ;
sock_connected = false ;
}
}
@ -137,19 +138,45 @@ public:
virtual int available ( ) {
virtual int available ( ) {
TINY_GSM_YIELD ( ) ;
TINY_GSM_YIELD ( ) ;
return at - > stream . available ( ) ;
return at - > stream . available ( ) ;
/ / if ( ! rx . size ( ) | | at - > stream . available ( ) ) {
/ / at - > maintain ( ) ;
/ / }
/ / return at - > stream . available ( ) + rx . size ( ) ;
}
}
virtual int read ( uint8_t * buf , size_t size ) {
virtual int read ( uint8_t * buf , size_t size ) {
TINY_GSM_YIELD ( ) ;
TINY_GSM_YIELD ( ) ;
return at - > stream . readBytes ( ( char * ) buf , size ) ;
return at - > stream . readBytes ( ( char * ) buf , size ) ;
/ / 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 ( ) | | at - > stream . available ( ) ) {
/ / at - > maintain ( ) ;
/ / }
/ / }
/ / return cnt ;
}
}
virtual int read ( ) {
virtual int read ( ) {
TINY_GSM_YIELD ( ) ;
TINY_GSM_YIELD ( ) ;
return at - > stream . read ( ) ;
return at - > stream . read ( ) ;
/ / uint8_t c ;
/ / if ( read ( & c , 1 ) = = 1 ) {
/ / return c ;
/ / }
/ / return - 1 ;
}
}
virtual int peek ( ) { return at - > stream . peek ( ) ; }
virtual int peek ( ) { return at - > stream . peek ( ) ; }
/ / virtual int peek ( ) { return - 1 ; } / / TODO
virtual void flush ( ) { at - > stream . flush ( ) ; }
virtual void flush ( ) { at - > stream . flush ( ) ; }
virtual uint8_t connected ( ) {
virtual uint8_t connected ( ) {
@ -170,6 +197,7 @@ private:
TinyGsmXBee * at ;
TinyGsmXBee * at ;
uint8_t mux ;
uint8_t mux ;
bool sock_connected ;
bool sock_connected ;
/ / RxFifo rx ;
} ;
} ;
@ -178,32 +206,32 @@ class GsmClientSecure : public GsmClient
public :
public :
GsmClientSecure ( ) { }
GsmClientSecure ( ) { }
GsmClientSecure ( TinyGsmXBee & modem , uint8_t mux = 1 )
GsmClientSecure ( TinyGsmXBee & modem , uint8_t mux = 0 )
: GsmClient ( modem , mux )
: GsmClient ( modem , mux )
{ }
{ }
public :
public :
virtual int connect ( const char * host , uint16_t port ) {
virtual int connect ( const char * host , uint16_t port ) {
at - > streamClear ( ) ; / / Empty anything remaining in the buffer ;
bool sock_connected = false ;
at - > streamClear ( ) ; / / Empty anything in the buffer before starting
if ( at - > commandMode ( ) ) { / / Don ' t try if we didn ' t successfully get into command mode
if ( at - > commandMode ( ) ) { / / Don ' t try if we didn ' t successfully get into command mode
sock_connected = at - > modemConnect ( host , port , mux , true ) ;
sock_connected = at - > modemConnect ( host , port , mux , true ) ;
at - > writeChanges ( ) ;
at - > writeChanges ( ) ;
at - > exitCommand ( ) ;
at - > exitCommand ( ) ;
}
}
at - > streamClear ( ) ; / / Empty anything remaining in the buffer ;
else
sock_connected = false ;
return sock_connected ;
return sock_connected ;
}
}
virtual int connect ( IPAddress ip , uint16_t port ) {
virtual int connect ( IPAddress ip , uint16_t port ) {
at - > streamClear ( ) ; / / Empty anything remaining in the buffer ;
bool sock_connected = false ;
at - > streamClear ( ) ; / / Empty anything in the buffer before starting
if ( at - > commandMode ( ) ) { / / Don ' t try if we didn ' t successfully get into command mode
if ( at - > commandMode ( ) ) { / / Don ' t try if we didn ' t successfully get into command mode
sock_connected = at - > modemConnect ( ip , port , mux , true ) ;
sock_connected = at - > modemConnect ( ip , port , mux , true ) ;
at - > writeChanges ( ) ;
at - > writeChanges ( ) ;
at - > exitCommand ( ) ;
at - > exitCommand ( ) ;
}
}
at - > streamClear ( ) ; / / Empty anything remaining in the buffer ;
else
sock_connected = false ;
return sock_connected ;
return sock_connected ;
}
}
} ;
} ;
@ -216,6 +244,7 @@ public:
{
{
beeType = XBEE_UNKNOWN ; / / Start not knowing what kind of bee it is
beeType = XBEE_UNKNOWN ; / / Start not knowing what kind of bee it is
guardTime = TINY_GSM_XBEE_GUARD_TIME ; / / Start with the default guard time of 1 second
guardTime = TINY_GSM_XBEE_GUARD_TIME ; / / Start with the default guard time of 1 second
memset ( sockets , 0 , sizeof ( sockets ) ) ;
}
}
/*
/*
@ -285,7 +314,16 @@ public:
return false ;
return false ;
}
}
void maintain ( ) { }
void maintain ( ) {
/ / this only happens OUTSIDE command mode , so if we ' re getting characters
/ / they should be data received from the TCP connection
/ / TINY_GSM_YIELD ( ) ;
/ / uint32_t _startMillis = millis ( ) ;
/ / while ( stream . available ( ) | | millis ( ) - _startMillis < 10 ) {
/ / char c = stream . read ( ) ;
/ / if ( c > 0 ) sockets [ 0 ] - > rx . put ( c ) ;
/ / }
}
bool factoryDefault ( ) {
bool factoryDefault ( ) {
if ( ! commandMode ( ) ) return false ; / / Return immediately
if ( ! commandMode ( ) ) return false ; / / Return immediately
@ -369,7 +407,7 @@ public:
}
}
if ( beeType ! = XBEE_S6B_WIFI ) {
if ( beeType ! = XBEE_S6B_WIFI ) {
sendAT ( GF ( " AM1 " ) ) ; / / Turn of airplane mode
sendAT ( GF ( " AM0 " ) ) ; / / Turn of f airplane mode
if ( waitResponse ( ) ! = 1 ) return exitAndFail ( ) ;
if ( waitResponse ( ) ! = 1 ) return exitAndFail ( ) ;
if ( ! writeChanges ( ) ) return exitAndFail ( ) ;
if ( ! writeChanges ( ) ) return exitAndFail ( ) ;
}
}
@ -681,7 +719,7 @@ protected:
while ( ! gotIP & & millis ( ) - startMillis < 45000L ) / / the lookup can take a while
while ( ! gotIP & & millis ( ) - startMillis < 45000L ) / / the lookup can take a while
{
{
sendAT ( GF ( " LA " ) , host ) ;
sendAT ( GF ( " LA " ) , host ) ;
while ( stream . available ( ) < 4 ) { } ; / / wait for any response
while ( stream . available ( ) < 4 & & millis ( ) - startMillis < 45000L ) { } ; / / wait for any response
strIP = stream . readStringUntil ( ' \r ' ) ; / / read result
strIP = stream . readStringUntil ( ' \r ' ) ; / / read result
strIP . trim ( ) ;
strIP . trim ( ) ;
if ( ! strIP . endsWith ( GF ( " ERROR " ) ) ) gotIP = true ;
if ( ! strIP . endsWith ( GF ( " ERROR " ) ) ) gotIP = true ;
@ -740,7 +778,7 @@ public:
void streamClear ( void ) {
void streamClear ( void ) {
TINY_GSM_YIELD ( ) ;
TINY_GSM_YIELD ( ) ;
while ( stream . available ( ) ) { stream . read ( ) ; }
while ( stream . available ( ) ) { stream . read ( ) ; }
}
}
template < typename . . . Args >
template < typename . . . Args >