40#define WIFI_LOG(level, ...) DEBUGFDEVICE(m_Device->getDeviceName(), level, __VA_ARGS__)
48 m_LoadConfig = std::move(loadConfig);
49 m_SaveConfig = std::move(saveConfig);
50 const char *dev = m_Device->getDeviceName();
53 HotspotSP[0].fill(
"HOTSPOT_ENABLE",
"Enable",
ISS_OFF);
54 HotspotSP[1].fill(
"HOTSPOT_DISABLE",
"Disable",
ISS_ON);
58 HotspotTP[0].fill(
"HOTSPOT_SSID",
"SSID",
"");
59 HotspotTP[1].fill(
"HOTSPOT_PASSWORD",
"Password",
"");
60 HotspotTP.fill(dev,
"HOTSPOT_CREDENTIALS",
"Hotspot Credentials", tab,
IP_RW, 60,
IPS_IDLE);
63 ChannelNP[0].fill(
"WIFI_CHANNEL_VALUE",
"Channel (1-11)",
"%.0f", 1, 11, 1, 11);
64 ChannelNP.fill(dev,
"WIFI_CHANNEL",
"WiFi Channel", tab,
IP_RW, 60,
IPS_IDLE);
67 ConnectionTP[0].fill(
"CURRENT_WIFI",
"Connection",
"");
68 ConnectionTP.fill(dev,
"CURRENT_WIFI",
"Current WiFi", tab,
IP_RO, 60,
IPS_IDLE);
71 ScanSP[0].fill(
"WIFI_SCAN",
"Scan",
ISS_OFF);
73 ScanTP[0].fill(
"WIFI_SCAN_RESULTS",
"Networks",
"");
74 ScanTP.fill(dev,
"WIFI_SCAN_RESULTS",
"Scan Results", tab,
IP_RO, 60,
IPS_IDLE);
77 NetworkTP[0].fill(
"WIFI_SSID",
"SSID",
"");
78 NetworkTP[1].fill(
"WIFI_PASSWORD",
"Password",
"");
79 NetworkTP.fill(dev,
"WIFI_CONNECT_CREDS",
"WiFi Credentials", tab,
IP_RW, 60,
IPS_IDLE);
80 ConnectSP[0].fill(
"WIFI_CONNECT",
"Connect",
ISS_OFF);
84 PasswordTP[0].fill(
"TCP_PASSWORD_VALUE",
"Password", DEFAULT_PASSWORD);
90 m_Device->defineProperty(PasswordTP);
91 if (!m_PasswordLoaded)
93 m_PasswordLoaded =
true;
94 m_LoadConfig(PasswordTP.getName());
103 m_Device->defineProperty(HotspotSP);
104 m_Device->defineProperty(HotspotTP);
105 m_Device->defineProperty(ChannelNP);
106 m_Device->defineProperty(ConnectionTP);
107 m_Device->defineProperty(ScanSP);
108 m_Device->defineProperty(ScanTP);
109 m_Device->defineProperty(NetworkTP);
110 m_Device->defineProperty(ConnectSP);
113 readHotspotCredentials();
119 m_Device->deleteProperty(HotspotSP);
120 m_Device->deleteProperty(HotspotTP);
121 m_Device->deleteProperty(ChannelNP);
122 m_Device->deleteProperty(ConnectionTP);
123 m_Device->deleteProperty(ScanSP);
124 m_Device->deleteProperty(ScanTP);
125 m_Device->deleteProperty(NetworkTP);
126 m_Device->deleteProperty(ConnectSP);
133 if (dev ==
nullptr || strcmp(dev, m_Device->getDeviceName()) != 0)
136 if (HotspotSP.isNameMatch(name))
139 const bool enable = onSwitch !=
nullptr && HotspotSP[0].isNameMatch(onSwitch);
140 char cmd[LEN] = {0}, res[LEN] = {0};
141 snprintf(
cmd, LEN,
"AE:%d", enable ? 1 : 0);
144 if (sendCommand(
cmd, res, LEN) && strcmp(res,
"0") != 0)
147 if (enable && strcmp(HotspotTP[1].getText(), DEFAULT_PASSWORD) == 0)
149 "The hotspot uses the factory password. Change it, since anyone in range can control the device.");
160 if (ScanSP.isNameMatch(name))
168 if (ConnectSP.isNameMatch(name))
171 ConnectSP.setState(connectToNetwork(NetworkTP[0].getText(), NetworkTP[1].getText()) ?
IPS_OK :
IPS_ALERT);
179bool WiFi::ISNewText(
const char *dev,
const char *name,
char *texts[],
char *names[],
int n)
181 if (dev ==
nullptr || strcmp(dev, m_Device->getDeviceName()) != 0)
184 if (PasswordTP.isNameMatch(name))
186 PasswordTP.update(texts, names,
n);
187 PasswordTP.setState(
IPS_OK);
189 m_SaveConfig(PasswordTP);
193 if (HotspotTP.isNameMatch(name))
195 HotspotTP.update(texts, names,
n);
196 const std::string ssid = HotspotTP[0].getText(), password = HotspotTP[1].getText();
197 const bool set = setHotspotCredentials(ssid.c_str(), password.c_str());
200 if (!readHotspotCredentials() || !set)
204 PasswordTP[0].setText(HotspotTP[1].getText());
206 m_SaveConfig(PasswordTP);
207 if (ssid == HotspotTP[0].getText() && password == HotspotTP[1].getText())
209 "Hotspot credentials set. The password is also the login for TCP and the web dashboard.");
215 "The device did not take the new hotspot credentials. The TCP password is set to the password it reports.");
220 if (NetworkTP.isNameMatch(name))
223 NetworkTP.update(texts, names,
n);
224 NetworkTP.setState(
IPS_OK);
234 if (dev ==
nullptr || strcmp(dev, m_Device->getDeviceName()) != 0)
237 if (ChannelNP.isNameMatch(name))
239 if (!ChannelNP.update(values, names,
n))
246 const int channel =
static_cast<int>(ChannelNP[0].getValue());
247 char cmd[LEN] = {0}, res[LEN] = {0};
248 snprintf(
cmd, LEN,
"AC:%d", channel);
250 if (!sendCommand(
cmd, res, LEN) || strcmp(res,
"0") == 0)
271 char cmd[LEN] = {0}, res[LEN] = {0};
272 snprintf(
cmd, LEN,
"%s\r\n", PasswordTP[0].getText());
277 if (strncmp(res,
"AUTHOK", 6) == 0)
282 if (strncmp(res,
">>", 2) == 0)
285 "The device accepts TCP commands without a password, so anyone on the network can control it.");
291 "TCP login failed. Check the TCP Password on the Connection tab; it is the hotspot password.");
295bool WiFi::sendCommand(
const char *
cmd,
char *res,
int len,
int timeout,
bool secret)
300 char command[LEN] = {0};
301 snprintf(command, LEN,
"%s\r\n",
cmd);
316 while (nbytes > 0 && (res[nbytes - 1] ==
'\n' || res[nbytes - 1] ==
'\r'))
317 res[--nbytes] =
'\0';
326 tcflush(portFD, TCIOFLUSH);
333 FD_SET(portFD, &set);
334 struct timeval timeout = {0, 0};
335 if (select(portFD + 1, &set,
nullptr,
nullptr, &timeout) <= 0 || read(portFD, buffer,
sizeof(buffer)) <= 0)
346bool WiFi::readHotspotStatus()
349 if (!sendCommand(
"A?", res, LEN))
352 const bool on = strcmp(res,
"1") == 0;
360bool WiFi::readHotspotCredentials()
364 const char *separator =
nullptr;
365 if (!sendCommand(
"AL", res, LEN) || (separator = strchr(res,
':')) ==
nullptr)
372 HotspotTP[0].setText(std::string(res, separator - res).c_str());
373 HotspotTP[1].setText(separator + 1);
374 HotspotTP.setState(
IPS_OK);
379bool WiFi::readChannel()
382 const int channel = sendCommand(
"AC", res, LEN) ? atoi(res) : 0;
383 if (channel < 1 || channel > 11)
390 ChannelNP[0].setValue(channel);
391 ChannelNP.setState(
IPS_OK);
396bool WiFi::readConnection()
400 if (!sendCommand(
"WA", res, LEN))
403 m_Connected = strcmp(res,
"0") != 0;
404 std::string text = m_Connected ?
res :
"Not connected";
408 const std::string reply(res);
409 const size_t security = reply.rfind(
',');
410 const size_t signal = security == std::string::npos || security == 0 ? std::string::npos : reply.rfind(
',', security - 1);
411 const size_t address = signal == std::string::npos || signal == 0 ? std::string::npos : reply.rfind(
',', signal - 1);
412 if (address != std::string::npos && address > 0)
413 text = reply.substr(0, address) +
" (" + reply.substr(address + 1, signal - address - 1) +
"), signal " +
414 reply.substr(signal + 1, security - signal - 1) +
"%";
418 if (text != ConnectionTP[0].getText() || ConnectionTP.getState() !=
IPS_OK)
420 ConnectionTP[0].setText(text.c_str());
421 ConnectionTP.setState(
IPS_OK);
422 ConnectionTP.apply();
430 if (HotspotSP[1].getState() ==
ISS_ON && !m_Connected)
433 "The scan needs the WiFi radio. Enable the hotspot or connect to a WiFi network first.");
438 char res[SCAN_LEN] = {0};
440 for (
int i = 0; i < SCAN_RETRIES; i++)
442 if (!sendCommand(
"WS", res, SCAN_LEN, SCAN_TIMEOUT))
444 if (strcmp(res,
"0") != 0)
446 if (i + 1 < SCAN_RETRIES)
447 usleep(SCAN_RETRY_DELAY_US);
450 ScanTP[0].setText(strcmp(res,
"0") == 0 ?
"No networks found" : formatScan(res).c_str());
456std::string WiFi::formatScan(
const char *reply)
459 std::vector<std::pair<std::string, int>> networks;
460 std::string entries(reply);
462 while (start < entries.size())
464 size_t end = entries.find(
':', start);
465 if (end == std::string::npos)
466 end = entries.size();
467 const std::string entry = entries.substr(start, end - start);
471 const size_t security = entry.rfind(
',');
472 const size_t signal = security == std::string::npos || security == 0 ? std::string::npos : entry.rfind(
',', security - 1);
473 if (signal == std::string::npos || signal == 0)
476 const std::string ssid = entry.substr(0, signal);
477 const int quality = atoi(entry.substr(signal + 1, security - signal - 1).c_str());
478 auto known = std::find_if(networks.begin(), networks.end(), [&](
const auto & network)
480 return network.first == ssid;
482 if (known == networks.end())
483 networks.emplace_back(ssid, quality);
485 known->second = std::max(known->second, quality);
488 std::stable_sort(networks.begin(), networks.end(), [](
const auto & a,
const auto & b)
490 return a.second > b.second;
494 for (
const auto &network : networks)
495 text += (text.empty() ?
"" :
", ") + network.first +
" (" + std::to_string(network.second) +
"%)";
496 return text.empty() ? std::string(reply) : text;
499bool WiFi::validText(
const char *text,
size_t maxLength)
502 return text !=
nullptr && text[0] !=
'\0' && strlen(text) <= maxLength && strpbrk(text,
"\r\n") ==
nullptr;
505bool WiFi::setHotspotCredentials(
const char *ssid,
const char *password)
507 if (!validText(ssid, 32) || !validText(password, 63) || strlen(password) < 8)
510 "Hotspot SSID must have 1 to 32 characters and the password 8 to 63 characters.");
516 char cmd[LEN] = {0},
res[LEN] = {0};
517 snprintf(
cmd, LEN,
"AN:%s", ssid);
518 if (!sendCommand(
cmd, res, LEN) || strcmp(res,
"0") == 0)
525 snprintf(
cmd, LEN,
"AP:%s", password);
526 if (!sendCommand(
cmd, res, LEN, 3,
true) || strcmp(res,
"0") == 0)
535bool WiFi::connectToNetwork(
const char *ssid,
const char *password)
538 const bool open = m_AllowOpenNetworks && password !=
nullptr && password[0] ==
'\0';
539 if (!validText(ssid, 32) || (!open && (!validText(password, 63) || strlen(password) < 8)))
542 "Enter the WiFi SSID (1 to 32 characters) and password (8 to 63 characters, empty for an open network) first." :
543 "Enter the WiFi SSID (1 to 32 characters) and password (8 to 63 characters) first.");
547 char cmd[LEN] = {0},
res[LEN] = {0};
548 snprintf(
cmd, LEN,
"WN:%s", ssid);
549 if (!sendCommand(
cmd, res, LEN) || strcmp(res,
"0") == 0)
556 snprintf(
cmd, LEN,
"WP:%s", open ? OPEN_NETWORK_PASSWORD : password);
557 if (!sendCommand(
cmd, res, LEN, 3,
true) || strcmp(res,
"0") == 0)
565 IEAddTimer(CONNECT_CHECK_MS, &WiFi::readConnectionLater,
this);
569void WiFi::readConnectionLater(
void *context)
571 auto wifi =
static_cast<WiFi *
>(context);
572 if (wifi->m_PortFD < 0 || !wifi->readConnection())
575 if (wifi->m_Connected)
577 wifi->ConnectionTP[0].getText());
580 "Not connected to %s yet. Check SSID and password; the device keeps trying every 10 seconds.",
581 wifi->NetworkTP[0].getText());