Skip to content

Commit

Permalink
Add WiFiClass::setTimeout(...) API's to set timeout of begin
Browse files Browse the repository at this point in the history
Added timeout logic check on WiFiClass's begin API

Porting of arduino-libraries/WiFiNINA#46
  • Loading branch information
Rocketct authored and sandeepmistry committed Feb 4, 2019
1 parent 30967e1 commit e85c35d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
1 change: 1 addition & 0 deletions keywords.txt
Expand Up @@ -54,6 +54,7 @@ WiFiMDNSResponder KEYWORD2
lowPowerMode KEYWORD2
maxLowPowerMode KEYWORD2
noLowPowerMode KEYWORD2
setTimeout KEYWORD2

#######################################
# Constants (LITERAL1)
Expand Down
32 changes: 20 additions & 12 deletions src/WiFi.cpp
Expand Up @@ -260,11 +260,12 @@ void WiFiClass::handlePingResponse(uint32 u32IPAddr, uint32 u32RTT, uint8 u8Erro
}
}

WiFiClass::WiFiClass()
WiFiClass::WiFiClass() :
_init(0),
_mode(WL_RESET_MODE),
_status(WL_NO_SHIELD),
_timeout(60000)
{
_mode = WL_RESET_MODE;
_status = WL_NO_SHIELD;
_init = 0;
}

void WiFiClass::setPins(int8_t cs, int8_t irq, int8_t rst, int8_t en)
Expand Down Expand Up @@ -377,11 +378,12 @@ uint8_t WiFiClass::begin()
_mode = WL_STA_MODE;

// Wait for connection or timeout:
unsigned long start = millis();
while (!(_status & WL_CONNECTED) &&
!(_status & WL_DISCONNECTED) &&
millis() - start < 60000) {
for (unsigned long start = millis(); millis() - start < _timeout;)
{
m2m_wifi_handle_events(NULL);
if ((_status & WL_CONNECTED) || (_status & WL_DISCONNECTED)) {
break;
}
}

memset(_ssid, 0, M2M_MAX_SSID_LEN);
Expand Down Expand Up @@ -438,11 +440,12 @@ uint8_t WiFiClass::startConnect(const char *ssid, uint8_t u8SecType, const void
_mode = WL_STA_MODE;

// Wait for connection or timeout:
unsigned long start = millis();
while (!(_status & WL_CONNECTED) &&
!(_status & WL_DISCONNECTED) &&
millis() - start < 60000) {
for (unsigned long start = millis(); millis() - start < _timeout;)
{
m2m_wifi_handle_events(NULL);
if ((_status & WL_CONNECTED) || (_status & WL_DISCONNECTED)) {
break;
}
}
if (!(_status & WL_CONNECTED)) {
_mode = WL_RESET_MODE;
Expand Down Expand Up @@ -1166,4 +1169,9 @@ uint32_t WiFiClass::getTime()
#endif
}

void WiFiClass::setTimeout(unsigned long timeout)
{
_timeout = timeout;
}

WiFiClass WiFi;
2 changes: 2 additions & 0 deletions src/WiFi101.h
Expand Up @@ -165,6 +165,7 @@ class WiFiClass
void handleEvent(uint8_t u8MsgType, void *pvMsg);
void handleResolve(uint8_t * hostName, uint32_t hostIp);
void handlePingResponse(uint32 u32IPAddr, uint32 u32RTT, uint8 u8ErrorCode);
void setTimeout(unsigned long timeout);

private:
int _init;
Expand All @@ -182,6 +183,7 @@ class WiFiClass
uint8_t _scan_auth;
uint8_t _scan_channel;
char _ssid[M2M_MAX_SSID_LEN];
unsigned long _timeout;

uint8_t startConnect(const char *ssid, uint8_t u8SecType, const void *pvAuthInfo);
uint8_t startAP(const char *ssid, uint8_t u8SecType, const void *pvAuthInfo, uint8_t channel);
Expand Down

0 comments on commit e85c35d

Please sign in to comment.