Skip to content

Commit 37945d0

Browse files
authored
WiFiClient connect timeout (#252)
1 parent c59ef3b commit 37945d0

File tree

5 files changed

+81
-42
lines changed

5 files changed

+81
-42
lines changed

docs/api.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2703,6 +2703,72 @@ client.remotePort()
27032703
#### Returns
27042704
The port of the remote host that the client is connected to
27052705

2706+
### `client.setConnectionTimeout()`
2707+
2708+
#### Description
2709+
2710+
Set the timeout for client.connect(). With timeout value not set, a connect attempt times out after the time determined by the firmware which is more than 18 seconds. You might prefer to set a lower timeout value to make your program more responsive in the event something goes wrong.
2711+
2712+
2713+
#### Syntax
2714+
2715+
```
2716+
client.setConnectionTimeout(milliseconds)
2717+
2718+
```
2719+
2720+
#### Parameters
2721+
- milliseconds: the timeout duration for client.connect() (uint16_t)
2722+
2723+
#### Returns
2724+
Nothing
2725+
2726+
#### Example
2727+
2728+
```
2729+
#include <WiFiNINA.h>
2730+
2731+
#include "arduino_secrets.h"
2732+
char ssid[] = SECRET_SSID;
2733+
char pass[] = SECRET_PASS;
2734+
2735+
IPAddress server(192,168,0,177);
2736+
WiFiClient client;
2737+
2738+
void setup() {
2739+
2740+
Serial.begin(115200);
2741+
while (!Serial) {}
2742+
2743+
Serial.print("Attempting to connect to SSID: ");
2744+
Serial.println(ssid);
2745+
int status = WiFi.begin(ssid, pass);
2746+
if ( status != WL_CONNECTED) {
2747+
Serial.println("Couldn't get a WiFi connection");
2748+
while(true);
2749+
}
2750+
2751+
client.setConnectionTimeout(3000); // 3 seconds
2752+
2753+
Serial.println("\nStarting connection to server...");
2754+
unsigned long startTime = millis();
2755+
if (client.connect(server, 80)) {
2756+
client.println("Connected");
2757+
} else {
2758+
client.println("Not Connected");
2759+
}
2760+
2761+
Serial.print("connect time (milliseconds): ");
2762+
Serial.println(millis()- startTime);
2763+
2764+
client.stop();
2765+
}
2766+
2767+
void loop() {
2768+
}
2769+
2770+
```
2771+
27062772
## Server Class
27072773

27082774
### `Server()`

src/WiFiClient.cpp

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,7 @@ int WiFiClient::connect(IPAddress ip, uint16_t port) {
5959
_sock = ServerDrv::getSocket();
6060
if (_sock != NO_SOCKET_AVAIL)
6161
{
62-
ServerDrv::startClient(uint32_t(ip), port, _sock);
63-
64-
unsigned long start = millis();
65-
66-
// wait 4 second for the connection to close
67-
while (!connected() && millis() - start < 10000)
68-
delay(1);
62+
ServerDrv::startClient(nullptr, 0, uint32_t(ip), port, _sock, TCP_MODE, _connTimeout);
6963

7064
if (!connected())
7165
{
@@ -88,13 +82,7 @@ int WiFiClient::connectSSL(IPAddress ip, uint16_t port)
8882
_sock = ServerDrv::getSocket();
8983
if (_sock != NO_SOCKET_AVAIL)
9084
{
91-
ServerDrv::startClient(uint32_t(ip), port, _sock, TLS_MODE);
92-
93-
unsigned long start = millis();
94-
95-
// wait 4 second for the connection to close
96-
while (!connected() && millis() - start < 10000)
97-
delay(1);
85+
ServerDrv::startClient(nullptr, 0, uint32_t(ip), port, _sock, TLS_MODE, _connTimeout);
9886

9987
if (!connected())
10088
{
@@ -117,13 +105,7 @@ int WiFiClient::connectSSL(const char *host, uint16_t port)
117105
_sock = ServerDrv::getSocket();
118106
if (_sock != NO_SOCKET_AVAIL)
119107
{
120-
ServerDrv::startClient(host, strlen(host), uint32_t(0), port, _sock, TLS_MODE);
121-
122-
unsigned long start = millis();
123-
124-
// wait 4 second for the connection to close
125-
while (!connected() && millis() - start < 10000)
126-
delay(1);
108+
ServerDrv::startClient(host, strlen(host), uint32_t(0), port, _sock, TLS_MODE, _connTimeout);
127109

128110
if (!connected())
129111
{
@@ -146,13 +128,7 @@ int WiFiClient::connectBearSSL(IPAddress ip, uint16_t port)
146128
_sock = ServerDrv::getSocket();
147129
if (_sock != NO_SOCKET_AVAIL)
148130
{
149-
ServerDrv::startClient(uint32_t(ip), port, _sock, TLS_BEARSSL_MODE);
150-
151-
unsigned long start = millis();
152-
153-
// wait 4 second for the connection to close
154-
while (!connected() && millis() - start < 10000)
155-
delay(1);
131+
ServerDrv::startClient(nullptr, 0, uint32_t(ip), port, _sock, TLS_BEARSSL_MODE, _connTimeout);
156132

157133
if (!connected())
158134
{
@@ -175,13 +151,7 @@ int WiFiClient::connectBearSSL(const char *host, uint16_t port)
175151
_sock = ServerDrv::getSocket();
176152
if (_sock != NO_SOCKET_AVAIL)
177153
{
178-
ServerDrv::startClient(host, strlen(host), uint32_t(0), port, _sock, TLS_BEARSSL_MODE);
179-
180-
unsigned long start = millis();
181-
182-
// wait 4 second for the connection to close
183-
while (!connected() && millis() - start < 10000)
184-
delay(1);
154+
ServerDrv::startClient(host, strlen(host), uint32_t(0), port, _sock, TLS_BEARSSL_MODE, _connTimeout);
185155

186156
if (!connected())
187157
{

src/WiFiClient.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class WiFiClient : public Client {
3232
WiFiClient(uint8_t sock);
3333

3434
uint8_t status();
35+
void setConnectionTimeout(uint16_t timeout) {_connTimeout = timeout;}
36+
3537
virtual int connect(IPAddress ip, uint16_t port);
3638
virtual int connect(const char *host, uint16_t port);
3739
virtual int connectSSL(IPAddress ip, uint16_t port);
@@ -63,8 +65,8 @@ class WiFiClient : public Client {
6365

6466
private:
6567
static uint16_t _srcport;
66-
uint8_t _sock; //not used
67-
uint16_t _socket;
68+
uint8_t _sock;
69+
uint16_t _connTimeout = 0;
6870
bool _retrySend;
6971
};
7072

src/utility/server_drv.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,19 +132,20 @@ void ServerDrv::startClient(uint32_t ipAddress, uint16_t port, uint8_t sock, uin
132132
SpiDrv::spiSlaveDeselect();
133133
}
134134

135-
void ServerDrv::startClient(const char* host, uint8_t host_len, uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode)
135+
void ServerDrv::startClient(const char* host, uint8_t host_len, uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode, uint16_t timeout)
136136
{
137137
WAIT_FOR_SLAVE_SELECT();
138138
// Send Command
139-
SpiDrv::sendCmd(START_CLIENT_TCP_CMD, PARAM_NUMS_5);
139+
SpiDrv::sendCmd(START_CLIENT_TCP_CMD, PARAM_NUMS_6);
140140
SpiDrv::sendParam((uint8_t*)host, host_len);
141141
SpiDrv::sendParam((uint8_t*)&ipAddress, sizeof(ipAddress));
142142
SpiDrv::sendParam(port);
143143
SpiDrv::sendParam(&sock, 1);
144-
SpiDrv::sendParam(&protMode, 1, LAST_PARAM);
144+
SpiDrv::sendParam(&protMode, 1);
145+
SpiDrv::sendParam(timeout, LAST_PARAM);
145146

146147
// pad to multiple of 4
147-
int commandSize = 17 + host_len;
148+
int commandSize = 20 + host_len;
148149
while (commandSize % 4) {
149150
SpiDrv::readChar();
150151
commandSize++;

src/utility/server_drv.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class ServerDrv
3939

4040
static void startClient(uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode=TCP_MODE);
4141

42-
static void startClient(const char* host, uint8_t host_len, uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode=TCP_MODE);
42+
static void startClient(const char* host, uint8_t host_len, uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode=TCP_MODE, uint16_t timeout = 0);
4343

4444
static void stopClient(uint8_t sock);
4545

0 commit comments

Comments
 (0)