Skip to content

Commit 99df703

Browse files
committed
Prevent blocking whilst waiting for a connection and introduce a read timeout value for buf_read in modem.
Fix bug where timout is reset to the default of 10000ms when calling begin
1 parent f032b82 commit 99df703

File tree

6 files changed

+151
-101
lines changed

6 files changed

+151
-101
lines changed

libraries/WiFiS3/src/Modem.cpp

+13-11
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ void ModemClass::begin(int badurate, int retry){
3636
_serial->begin(badurate);
3737
string res = "";
3838
_serial->flush();
39+
40+
unsigned long modemTimeout = _timeout;
3941
modem.timeout(500);
4042
while(!beginned && retry > 0) {
4143
beginned = modem.write(string(PROMPT(_SOFTRESETWIFI)),res, "%s" , CMD(_SOFTRESETWIFI));
4244
retry -= 1;
4345
}
44-
modem.timeout(MODEM_TIMEOUT);
46+
modem.timeout(modemTimeout);
4547
}
4648
}
4749

@@ -186,7 +188,7 @@ ModemClass::ParseResult ModemClass::buf_read(const string &prompt, string &data_
186188
unsigned long start_time = millis();
187189
while(state != at_parse_state_t::Completed) {
188190

189-
if(millis() - start_time > _timeout) {
191+
if(millis() - start_time > _readTimeout) {
190192
res = Timeout;
191193
break;
192194
}
@@ -293,15 +295,15 @@ ModemClass::ParseResult ModemClass::buf_read(const string &prompt, string &data_
293295
* in data_res contains the length of the next token
294296
*/
295297

296-
if(c == '|') { // sized read, the previous parameter is the length
297-
sized_read_size = atoi(data_res.c_str());
298-
data_res.clear();
299-
if (sized_read_size != 0) {
300-
state = at_parse_state_t::Sized;
301-
} else {
302-
state = at_parse_state_t::Res;
303-
}
304-
} else if(c == '\r') {
298+
if(c == '|') { // sized read, the previous parameter is the length
299+
sized_read_size = atoi(data_res.c_str());
300+
data_res.clear();
301+
if (sized_read_size != 0) {
302+
state = at_parse_state_t::Sized;
303+
} else {
304+
state = at_parse_state_t::Res;
305+
}
306+
} else if(c == '\r') {
305307
state = at_parse_state_t::ResWaitLF;
306308
} else if(c == '\n') {
307309
state = at_parse_state_t::Res;

libraries/WiFiS3/src/Modem.h

+102-80
Original file line numberDiff line numberDiff line change
@@ -17,89 +17,89 @@
1717

1818
/**
1919
* @brief A class that provides methods to interact with a modem.
20-
*
20+
*
2121
* This class is responsible for providing an interface to communicate with
22-
* a modem through serial communication. It includes methods for initialization,
22+
* a modem through serial communication. It includes methods for initialization,
2323
* sending and receiving data, and handling modem configurations.
2424
*/
2525
class ModemClass {
2626

2727
public:
28-
/**
29-
* @brief Constructor for the ModemClass, which initializes the modem with the specified transmit (TX) and receive (RX) pins.
30-
*
31-
* @param Initializes an instance of the ModemClass class with
32-
* specific transmit `tx` and receive `rx` pins for communication.
33-
*/
34-
ModemClass(int tx, int rx);
35-
36-
/**
37-
* @brief Constructor for the ModemClass, which initializes the modem with the specified UART interface.
38-
*
39-
* @param `_serial` is a pointer to the UART object that will be used for communication with the modem.
40-
*/
41-
ModemClass(UART * _serial);
42-
43-
/**
44-
* @brief Destructor for ModemClass.
45-
*/
46-
~ModemClass();
47-
48-
49-
/**
50-
* @brief Initializes the modem communication with a specified baud rate.
51-
*
52-
* @param[in] `badurate` sets the baud rate for the serial connection.
53-
*/
54-
void begin(int badurate = 115200, int retry = 3);
55-
56-
/**
57-
* @brief Ends the modem communication.
58-
*/
59-
void end();
60-
61-
62-
/**
63-
* @brief Sends a formatted command string to a device and stores the response.
64-
*
65-
* This function formats a command string using the provided format and arguments,
66-
* sends it to a device, and waits for a response, which is stored in the `str` string.
67-
*
68-
* @param `cmd` A string representing the command to be sent to the device.
69-
* @param `str` A reference to a string that will hold the device's response.
70-
* @param `fmt` A format string for constructing the command.
71-
*
72-
* @return `true` if the command was successfully sent and a response was received,
73-
* `false` otherwise.
74-
*/
75-
bool write(const std::string &cmd, std::string &str, const char * fmt, ...);
76-
77-
/**
78-
* @brief Used to send a command to the modem without waiting for a response.
79-
*
80-
* @param It takes a command string `cmd`, a string `str` where the response will be stored,
81-
* and a format string `fmt` along with additional arguments.
82-
*/
83-
void write_nowait(const std::string &cmd, std::string &str, const char * fmt, ...);
84-
85-
/**
86-
* @brief Sends binary data directly to the modem without any processing or interpretation.
87-
*
88-
* @param It takes a pointer to the binary `data` and the `size` of the data as arguments.
89-
* Used for sending raw binary commands or data to the modem for operations that
90-
* require direct communication without any additional formatting or parsing.
91-
*/
92-
bool passthrough(const uint8_t *data, size_t size);
93-
94-
/**
95-
* @brief Disables automatic trimming of results for one operation.
96-
*
97-
* This function disables the automatic trimming of results for one operation.
98-
* After it is called, the results will not be trimmed automatically until
99-
* the function is called again.
100-
*/
101-
void avoid_trim_results() {
102-
/* one shot - it works only 1 time the it is necessary to call again this
28+
/**
29+
* @brief Constructor for the ModemClass, which initializes the modem with the specified transmit (TX) and receive (RX) pins.
30+
*
31+
* @param Initializes an instance of the ModemClass class with
32+
* specific transmit `tx` and receive `rx` pins for communication.
33+
*/
34+
ModemClass(int tx, int rx);
35+
36+
/**
37+
* @brief Constructor for the ModemClass, which initializes the modem with the specified UART interface.
38+
*
39+
* @param `_serial` is a pointer to the UART object that will be used for communication with the modem.
40+
*/
41+
ModemClass(UART * _serial);
42+
43+
/**
44+
* @brief Destructor for ModemClass.
45+
*/
46+
~ModemClass();
47+
48+
49+
/**
50+
* @brief Initializes the modem communication with a specified baud rate.
51+
*
52+
* @param[in] `badurate` sets the baud rate for the serial connection.
53+
*/
54+
void begin(int badurate = 115200, int retry = 3);
55+
56+
/**
57+
* @brief Ends the modem communication.
58+
*/
59+
void end();
60+
61+
62+
/**
63+
* @brief Sends a formatted command string to a device and stores the response.
64+
*
65+
* This function formats a command string using the provided format and arguments,
66+
* sends it to a device, and waits for a response, which is stored in the `str` string.
67+
*
68+
* @param `cmd` A string representing the command to be sent to the device.
69+
* @param `str` A reference to a string that will hold the device's response.
70+
* @param `fmt` A format string for constructing the command.
71+
*
72+
* @return `true` if the command was successfully sent and a response was received,
73+
* `false` otherwise.
74+
*/
75+
bool write(const std::string &cmd, std::string &str, const char * fmt, ...);
76+
77+
/**
78+
* @brief Used to send a command to the modem without waiting for a response.
79+
*
80+
* @param It takes a command string `cmd`, a string `str` where the response will be stored,
81+
* and a format string `fmt` along with additional arguments.
82+
*/
83+
void write_nowait(const std::string &cmd, std::string &str, const char * fmt, ...);
84+
85+
/**
86+
* @brief Sends binary data directly to the modem without any processing or interpretation.
87+
*
88+
* @param It takes a pointer to the binary `data` and the `size` of the data as arguments.
89+
* Used for sending raw binary commands or data to the modem for operations that
90+
* require direct communication without any additional formatting or parsing.
91+
*/
92+
bool passthrough(const uint8_t *data, size_t size);
93+
94+
/**
95+
* @brief Disables automatic trimming of results for one operation.
96+
*
97+
* This function disables the automatic trimming of results for one operation.
98+
* After it is called, the results will not be trimmed automatically until
99+
* the function is called again.
100+
*/
101+
void avoid_trim_results() {
102+
/* one shot - it works only 1 time the it is necessary to call again this
103103
funtion */
104104
trim_results = false;
105105
}
@@ -109,9 +109,9 @@ class ModemClass {
109109
* to be read is considered for processing.
110110
*/
111111
void read_using_size() {
112-
// read_by_size = true; // deprecated
112+
// read_by_size = true; // deprecated
113113
}
114-
114+
115115
bool beginned;
116116

117117
/* Calling this function with no argument will enable debug message to be printed
@@ -143,11 +143,32 @@ class ModemClass {
143143

144144
/**
145145
* @brief Sets the timeout value for communication operations.
146-
*
146+
*
147147
* @param Can be called with a specified timeout value in milliseconds.
148148
*/
149149
void timeout(size_t timeout_ms) {_timeout = timeout_ms;}
150150

151+
/**
152+
* @brief Gets the timeout value for communication operations.
153+
*
154+
* @return Can be called to get the specified timeout value in milliseconds.
155+
*/
156+
unsigned long getTimeout() { return _timeout; }
157+
158+
/**
159+
* @brief Sets the timeout value for reading communication operations.
160+
*
161+
* @param Can be called with a specified read timeout value in milliseconds.
162+
*/
163+
void readTimeout(size_t timeout_ms) {_readTimeout = timeout_ms;}
164+
165+
/**
166+
* @brief Gets the timeout value for reading communication operations.
167+
*
168+
* @return Can be called to get the specified read timeout value in milliseconds.
169+
*/
170+
unsigned long getReadTimeout() { return _readTimeout; }
171+
151172
private:
152173
enum ParseResult {
153174
Ok,
@@ -160,6 +181,7 @@ class ModemClass {
160181
bool delete_serial;
161182
UART * _serial;
162183
unsigned long _timeout;
184+
unsigned long _readTimeout = MODEM_TIMEOUT;
163185
uint8_t tx_buff[MAX_BUFF_SIZE];
164186
bool trim_results;
165187
Stream * _serial_debug;

libraries/WiFiS3/src/WiFi.cpp

+16-7
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,22 @@ int CWifi::begin(const char* ssid, const char *passphrase) {
5959
}
6060
}
6161

62-
unsigned long start_time = millis();
63-
while(millis() - start_time < _timeout){
64-
if(status() == WL_CONNECTED) {
65-
return WL_CONNECTED;
66-
}
67-
}
68-
return WL_CONNECT_FAILED;
62+
_start_connection_time = millis();
63+
return WL_CONNECTING;
64+
}
65+
66+
67+
int CWifi::isConnected()
68+
{
69+
if (status() == WL_CONNECTED)
70+
return WL_CONNECTED;
71+
72+
if (millis() - _start_connection_time < _timeout)
73+
{
74+
return WL_CONNECTING;
75+
}
76+
77+
return WL_CONNECT_FAILED;
6978
}
7079

7180
/* passphrase is needed so a default one will be set */

libraries/WiFiS3/src/WiFi.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class CWifi {
6363
void _config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2);
6464
void _sortAPlist(uint8_t num);
6565
unsigned long _timeout;
66+
unsigned long _start_connection_time;
6667
CAccessPoint access_points[WIFI_MAX_SSID_COUNT];
6768
uint8_t _apsFound = 0;
6869
std::string ssid;
@@ -453,7 +454,12 @@ class CWifi {
453454
*/
454455
void setTimeout(unsigned long timeout);
455456

456-
457+
/*
458+
* @brief Retrieves the connected state
459+
*
460+
* @return Current connection state of WL_CONNECT_FAILED, WL_CONNECTED or WL_CONNECTING
461+
*/
462+
int isConnected();
457463
};
458464

459465
/**

libraries/WiFiS3/src/WiFiClient.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,16 @@ class WiFiClient : public Client {
229229
void setConnectionTimeout(int timeout) {
230230
_connectionTimeout = timeout;
231231
}
232-
232+
233+
/**
234+
* @brief Retrieves the connection timeout period
235+
*
236+
* @return Returns the connection timeout in milliseconds
237+
*/
238+
int getConnectionTimeout() {
239+
return _connectionTimeout;
240+
}
241+
233242
/**
234243
* @brief Declares WiFiServer as a friend class.
235244
*

libraries/WiFiS3/src/WiFiTypes.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ typedef enum {
2424
WL_DISCONNECTED,
2525
WL_AP_LISTENING,
2626
WL_AP_CONNECTED,
27-
WL_AP_FAILED
27+
WL_AP_FAILED,
28+
WL_CONNECTING,
29+
WL_DISCONNECTING
2830
} wl_status_t;
2931

3032
/* Encryption modes */

0 commit comments

Comments
 (0)