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

WiFiClient connect timeout #252

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
66 changes: 66 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2638,6 +2638,72 @@ client.remotePort()
#### Returns
The port of the remote host that the client is connected to

### `client.setConnectionTimeout()`

#### Description

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.


#### Syntax

```
client.setConnectionTimeout(milliseconds)

```

#### Parameters
- milliseconds: the timeout duration for client.connect() (uint16_t)

#### Returns
Nothing

#### Example

```
#include <WiFiNINA.h>

#include "arduino_secrets.h"
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;

IPAddress server(192,168,0,177);
WiFiClient client;

void setup() {

Serial.begin(115200);
while (!Serial) {}

Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
int status = WiFi.begin(ssid, pass);
if ( status != WL_CONNECTED) {
Serial.println("Couldn't get a WiFi connection");
while(true);
}

client.setConnectionTimeout(3000); // 3 seconds

Serial.println("\nStarting connection to server...");
unsigned long startTime = millis();
if (client.connect(server, 80)) {
client.println("Connected");
} else {
client.println("Not Connected");
}

Serial.print("connect time (milliseconds): ");
Serial.println(millis()- startTime);

client.stop();
}

void loop() {
}

```

## Server Class

### `Server()`
Expand Down
40 changes: 5 additions & 35 deletions src/WiFiClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,7 @@ int WiFiClient::connect(IPAddress ip, uint16_t port) {
_sock = ServerDrv::getSocket();
if (_sock != NO_SOCKET_AVAIL)
{
ServerDrv::startClient(uint32_t(ip), port, _sock);

unsigned long start = millis();

// wait 4 second for the connection to close
while (!connected() && millis() - start < 10000)
delay(1);
ServerDrv::startClient(nullptr, 0, uint32_t(ip), port, _sock, TCP_MODE, _connTimeout);

if (!connected())
{
Expand All @@ -88,13 +82,7 @@ int WiFiClient::connectSSL(IPAddress ip, uint16_t port)
_sock = ServerDrv::getSocket();
if (_sock != NO_SOCKET_AVAIL)
{
ServerDrv::startClient(uint32_t(ip), port, _sock, TLS_MODE);

unsigned long start = millis();

// wait 4 second for the connection to close
while (!connected() && millis() - start < 10000)
delay(1);
ServerDrv::startClient(nullptr, 0, uint32_t(ip), port, _sock, TLS_MODE, _connTimeout);

if (!connected())
{
Expand All @@ -117,13 +105,7 @@ int WiFiClient::connectSSL(const char *host, uint16_t port)
_sock = ServerDrv::getSocket();
if (_sock != NO_SOCKET_AVAIL)
{
ServerDrv::startClient(host, strlen(host), uint32_t(0), port, _sock, TLS_MODE);

unsigned long start = millis();

// wait 4 second for the connection to close
while (!connected() && millis() - start < 10000)
delay(1);
ServerDrv::startClient(host, strlen(host), uint32_t(0), port, _sock, TLS_MODE, _connTimeout);

if (!connected())
{
Expand All @@ -146,13 +128,7 @@ int WiFiClient::connectBearSSL(IPAddress ip, uint16_t port)
_sock = ServerDrv::getSocket();
if (_sock != NO_SOCKET_AVAIL)
{
ServerDrv::startClient(uint32_t(ip), port, _sock, TLS_BEARSSL_MODE);

unsigned long start = millis();

// wait 4 second for the connection to close
while (!connected() && millis() - start < 10000)
delay(1);
ServerDrv::startClient(nullptr, 0, uint32_t(ip), port, _sock, TLS_BEARSSL_MODE, _connTimeout);

if (!connected())
{
Expand All @@ -175,13 +151,7 @@ int WiFiClient::connectBearSSL(const char *host, uint16_t port)
_sock = ServerDrv::getSocket();
if (_sock != NO_SOCKET_AVAIL)
{
ServerDrv::startClient(host, strlen(host), uint32_t(0), port, _sock, TLS_BEARSSL_MODE);

unsigned long start = millis();

// wait 4 second for the connection to close
while (!connected() && millis() - start < 10000)
delay(1);
ServerDrv::startClient(host, strlen(host), uint32_t(0), port, _sock, TLS_BEARSSL_MODE, _connTimeout);

if (!connected())
{
Expand Down
6 changes: 4 additions & 2 deletions src/WiFiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class WiFiClient : public Client {
WiFiClient(uint8_t sock);

uint8_t status();
void setConnectionTimeout(uint16_t timeout) {_connTimeout = timeout;}

virtual int connect(IPAddress ip, uint16_t port);
virtual int connect(const char *host, uint16_t port);
virtual int connectSSL(IPAddress ip, uint16_t port);
Expand Down Expand Up @@ -61,8 +63,8 @@ class WiFiClient : public Client {

private:
static uint16_t _srcport;
uint8_t _sock; //not used
uint16_t _socket;
uint8_t _sock;
uint16_t _connTimeout = 0;
bool _retrySend;
};

Expand Down
7 changes: 4 additions & 3 deletions src/utility/server_drv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,17 @@ void ServerDrv::startClient(uint32_t ipAddress, uint16_t port, uint8_t sock, uin
SpiDrv::spiSlaveDeselect();
}

void ServerDrv::startClient(const char* host, uint8_t host_len, uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode)
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)
{
WAIT_FOR_SLAVE_SELECT();
// Send Command
SpiDrv::sendCmd(START_CLIENT_TCP_CMD, PARAM_NUMS_5);
SpiDrv::sendCmd(START_CLIENT_TCP_CMD, PARAM_NUMS_6);
SpiDrv::sendParam((uint8_t*)host, host_len);
SpiDrv::sendParam((uint8_t*)&ipAddress, sizeof(ipAddress));
SpiDrv::sendParam(port);
SpiDrv::sendParam(&sock, 1);
SpiDrv::sendParam(&protMode, 1, LAST_PARAM);
SpiDrv::sendParam(&protMode, 1);
SpiDrv::sendParam(timeout, LAST_PARAM);

// pad to multiple of 4
int commandSize = 17 + host_len;
Expand Down
2 changes: 1 addition & 1 deletion src/utility/server_drv.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ServerDrv

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

static void startClient(const char* host, uint8_t host_len, uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode=TCP_MODE);
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);

static void stopClient(uint8_t sock);

Expand Down