Skip to content

Commit 0948021

Browse files
committed
WPA2 Enterprise support: PEAP/MSCHAPv2
1 parent 9cd2d7e commit 0948021

File tree

6 files changed

+81
-0
lines changed

6 files changed

+81
-0
lines changed

keywords.txt

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ remoteIP KEYWORD2
4848
remotePort KEYWORD2
4949

5050
beginAP KEYWORD2
51+
beginEnterprise KEYWORD2
5152
setHostname KEYWORD2
5253
end KEYWORD2
5354
getTime KEYWORD2

src/WiFi.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,37 @@ uint8_t WiFiClass::beginAP(const char *ssid, const char* passphrase, uint8_t cha
156156
return status;
157157
}
158158

159+
uint8_t WiFiClass::beginEnterprise(const char* ssid, const char* username, const char* password)
160+
{
161+
return beginEnterprise(ssid, username, password, "");
162+
}
163+
164+
uint8_t WiFiClass::beginEnterprise(const char* ssid, const char* username, const char* password, const char* identity)
165+
{
166+
return beginEnterprise(ssid, username, password, identity, "");
167+
}
168+
169+
uint8_t WiFiClass::beginEnterprise(const char* ssid, const char* username, const char* password, const char* identity, const char* ca)
170+
{
171+
uint8_t status = WL_IDLE_STATUS;
172+
173+
// set passphrase
174+
if (WiFiDrv::wifiSetEnterprise(0 /*PEAP/MSCHAPv2*/, ssid, strlen(ssid), username, strlen(username), password, strlen(password), identity, strlen(identity), ca, strlen(ca) + 1)!= WL_FAILURE)
175+
{
176+
for (unsigned long start = millis(); (millis() - start) < _timeout;)
177+
{
178+
delay(WL_DELAY_START_CONNECTION);
179+
status = WiFiDrv::getConnectionStatus();
180+
if ((status != WL_IDLE_STATUS) && (status != WL_NO_SSID_AVAIL) && (status != WL_SCAN_COMPLETED)) {
181+
break;
182+
}
183+
}
184+
} else {
185+
status = WL_CONNECT_FAILED;
186+
}
187+
return status;
188+
}
189+
159190
void WiFiClass::config(IPAddress local_ip)
160191
{
161192
WiFiDrv::config(1, (uint32_t)local_ip, 0, 0);

src/WiFi.h

+4
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ class WiFiClass
8080
uint8_t beginAP(const char *ssid, const char* passphrase);
8181
uint8_t beginAP(const char *ssid, const char* passphrase, uint8_t channel);
8282

83+
uint8_t beginEnterprise(const char* ssid, const char* username, const char* password);
84+
uint8_t beginEnterprise(const char* ssid, const char* username, const char* password, const char* identity);
85+
uint8_t beginEnterprise(const char* ssid, const char* username, const char* password, const char* identity, const char* ca);
86+
8387
/* Change Ip configuration settings disabling the dhcp client
8488
*
8589
* param local_ip: Static ip configuration

src/utility/wifi_drv.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,42 @@ int8_t WiFiDrv::wifiSetApPassphrase(const char* ssid, uint8_t ssid_len, const ch
921921
return _data;
922922
}
923923

924+
int8_t WiFiDrv::wifiSetEnterprise(uint8_t eapType, const char* ssid, uint8_t ssid_len, const char *username, const uint8_t username_len, const char *password, const uint8_t password_len, const char *identity, const uint8_t identity_len, const char* ca_cert, uint16_t ca_cert_len)
925+
{
926+
WAIT_FOR_SLAVE_SELECT();
927+
// Send Command
928+
SpiDrv::sendCmd(SET_ENT_CMD, PARAM_NUMS_6);
929+
SpiDrv::sendBuffer(&eapType, sizeof(eapType));
930+
SpiDrv::sendBuffer((uint8_t*)ssid, ssid_len);
931+
SpiDrv::sendBuffer((uint8_t*)username, username_len);
932+
SpiDrv::sendBuffer((uint8_t*)password, password_len);
933+
SpiDrv::sendBuffer((uint8_t*)identity, identity_len);
934+
SpiDrv::sendBuffer((uint8_t*)ca_cert, ca_cert_len, LAST_PARAM);
935+
936+
// pad to multiple of 4
937+
int commandSize = 15 + sizeof(eapType) + ssid_len + username_len + password_len + identity_len + ca_cert_len;
938+
while (commandSize % 4) {
939+
SpiDrv::readChar();
940+
commandSize++;
941+
}
942+
943+
SpiDrv::spiSlaveDeselect();
944+
//Wait the reply elaboration
945+
SpiDrv::waitForSlaveReady();
946+
SpiDrv::spiSlaveSelect();
947+
948+
// Wait for reply
949+
uint8_t _data = 0;
950+
uint8_t _dataLen = 0;
951+
if (!SpiDrv::waitResponseCmd(SET_ENT_CMD, PARAM_NUMS_1, &_data, &_dataLen))
952+
{
953+
WARN("error waitResponse");
954+
_data = WL_FAILURE;
955+
}
956+
SpiDrv::spiSlaveDeselect();
957+
return _data;
958+
}
959+
924960
int16_t WiFiDrv::ping(uint32_t ipAddress, uint8_t ttl)
925961
{
926962
WAIT_FOR_SLAVE_SELECT();

src/utility/wifi_drv.h

+7
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,13 @@ class WiFiDrv
277277

278278
static int8_t wifiSetApNetwork(const char* ssid, uint8_t ssid_len, uint8_t channel);
279279
static int8_t wifiSetApPassphrase(const char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len, uint8_t channel);
280+
static int8_t wifiSetEnterprise(uint8_t eapType,
281+
const char* ssid, uint8_t ssid_len,
282+
const char *username, const uint8_t username_len,
283+
const char *password, const uint8_t password_len,
284+
const char *identity, const uint8_t identity_len,
285+
const char* ca_cert, uint16_t ca_cert_len);
286+
280287

281288
static int16_t ping(uint32_t ipAddress, uint8_t ttl);
282289

src/utility/wifi_spi.h

+2
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ enum {
9292
GET_SOCKET_CMD = 0x3F,
9393

9494
// All command with DATA_FLAG 0x40 send a 16bit Len
95+
SET_ENT_CMD = 0x40,
9596

9697
SEND_DATA_TCP_CMD = 0x44,
9798
GET_DATABUF_TCP_CMD = 0x45,
@@ -126,6 +127,7 @@ enum numParams{
126127
PARAM_NUMS_3,
127128
PARAM_NUMS_4,
128129
PARAM_NUMS_5,
130+
PARAM_NUMS_6,
129131
MAX_PARAM_NUMS
130132
};
131133

0 commit comments

Comments
 (0)