Skip to content

Commit

Permalink
feat: added optional arqument to MQTT to connect to hidden networks
Browse files Browse the repository at this point in the history
Signed-off-by: James Chapman <james.chapman@pionix.de>
  • Loading branch information
james-ctc committed Jan 22, 2024
1 parent aba1704 commit d5ba4ee
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
22 changes: 21 additions & 1 deletion modules/Setup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,29 @@ To add a wifi network a payload with the following format must be published to t
{
"interface": "wlan0",
"ssid": "Example",
"psk": "a_valid_pre_shared_key"
"psk": "20fcb529dee0aad11b0568f553942850d06e4c4531c0d75b35345d580b300f78"
}
```
The PSK field can represent the passphrase instead using escaped quotes:
```json
{
"interface": "wlan0",
"ssid": "Example",
"psk": "\"A_valid_passphrase\""
}
```
For open WiFi networks the psk must be an empty string `"psk": ""`.

For hidden networks an optional item is needed:
```json
{
"interface": "wlan0",
"ssid": "Example",
"psk": "\"A_valid_passphrase\"",
"hidden": true
}
```
When `hidden` is not supplied then it is assumed to be false.

### everest_api/setup/cmd/enable_network
To enable a wifi network a payload with the following format must be published to this topic:
Expand Down
16 changes: 12 additions & 4 deletions modules/Setup/Setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,19 @@ void to_json(json& j, const NetworkDeviceInfo& k) {
}

void to_json(json& j, const WifiCredentials& k) {
j = json::object({{"interface", k.interface}, {"ssid", k.ssid}, {"psk", k.psk}});
j = json::object({{"interface", k.interface}, {"ssid", k.ssid}, {"psk", k.psk}, {"hidden", k.hidden}});
}

void from_json(const json& j, WifiCredentials& k) {
k.interface = j.at("interface");
k.ssid = j.at("ssid");
k.psk = j.at("psk");
k.hidden = false;
// optional item
auto it = j.find("hidden");
if ((it != j.end() && *it)) {
k.hidden = true;
}
}

void to_json(json& j, const InterfaceAndNetworkId& k) {
Expand Down Expand Up @@ -148,7 +154,8 @@ void Setup::ready() {
this->mqtt.subscribe(add_network_cmd, [this](const std::string& data) {
WifiCredentials wifi_credentials = json::parse(data);
WifiConfigureClass wifi;
this->add_and_enable_network(wifi_credentials.interface, wifi_credentials.ssid, wifi_credentials.psk);
this->add_and_enable_network(wifi_credentials.interface, wifi_credentials.ssid, wifi_credentials.psk,
wifi_credentials.hidden);
wifi.save_config(wifi_credentials.interface);
this->publish_configured_networks();
});
Expand Down Expand Up @@ -521,7 +528,8 @@ void Setup::publish_configured_networks() {
}
}

bool Setup::add_and_enable_network(const std::string& interface, const std::string& ssid, const std::string& psk) {
bool Setup::add_and_enable_network(const std::string& interface, const std::string& ssid, const std::string& psk,
bool hidden) {
WifiConfigureClass wifi;

std::string net_if = interface;
Expand All @@ -538,7 +546,7 @@ bool Setup::add_and_enable_network(const std::string& interface, const std::stri

auto network_id = wifi.add_network(net_if);
bool bResult = network_id != -1;
bResult = bResult && wifi.set_network(net_if, network_id, ssid, psk);
bResult = bResult && wifi.set_network(net_if, network_id, ssid, psk, hidden);
bResult = bResult && wifi.enable_network(net_if, network_id);
return bResult;
}
Expand Down
4 changes: 3 additions & 1 deletion modules/Setup/Setup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct WifiCredentials {
std::string interface;
std::string ssid;
std::string psk;
bool hidden;

operator std::string() {

Expand Down Expand Up @@ -170,7 +171,8 @@ class Setup : public Everest::ModuleBase {
bool rfkill_block(std::string rfkill_id);

void publish_configured_networks();
bool add_and_enable_network(const std::string& interface, const std::string& ssid, const std::string& psk);
bool add_and_enable_network(const std::string& interface, const std::string& ssid, const std::string& psk,
bool hidden = false);
bool remove_all_networks();
bool reboot();
bool is_online();
Expand Down

0 comments on commit d5ba4ee

Please sign in to comment.