Skip to content

Commit

Permalink
Use DBUS to detect presence of Wi-Fi devices.
Browse files Browse the repository at this point in the history
  • Loading branch information
rerdavies committed Nov 20, 2024
1 parent ca87658 commit 0dfe7b8
Show file tree
Hide file tree
Showing 8 changed files with 312 additions and 309 deletions.
394 changes: 148 additions & 246 deletions react/src/PiPedalModel.tsx

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion react/src/SettingsDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,13 @@ const SettingsDialog = withStyles(styles, { withTheme: true })(
this.handleGovernorSettingsChanged = this.handleGovernorSettingsChanged.bind(this);
this.handleConnectionStateChanged = this.handleConnectionStateChanged.bind(this);
this.handleShowStatusMonitorChanged = this.handleShowStatusMonitorChanged.bind(this);
this.handleHasWifiChanged = this.handleHasWifiChanged.bind(this);

}

handleHasWifiChanged(newValue: boolean) {
this.setState({hasWifiDevice: newValue});
}

handleShowStatusMonitorChanged(): void {
this.setState({ showStatusMonitor: this.model.showStatusMonitor.get() });
Expand Down Expand Up @@ -322,6 +326,7 @@ const SettingsDialog = withStyles(styles, { withTheme: true })(
if (active !== this.active) {
this.active = active;
if (active) {
this.model.hasWifiDevice.addOnChangedHandler(this.handleHasWifiChanged);
this.model.state.addOnChangedHandler(this.handleConnectionStateChanged);
this.model.showStatusMonitor.addOnChangedHandler(this.handleShowStatusMonitorChanged);
this.model.jackSettings.addOnChangedHandler(this.handleJackSettingsChanged);
Expand Down Expand Up @@ -350,6 +355,8 @@ const SettingsDialog = withStyles(styles, { withTheme: true })(
this.handleJackServerSettingsChanged();
this.handleWifiConfigSettingsChanged();
this.handleWifiDirectConfigSettingsChanged();
this.setState({hasWifiDevice: this.model.hasWifiDevice.get()});

this.timerHandle = setInterval(() => this.tick(), 1000);
} else {
if (this.timerHandle) {
Expand All @@ -364,7 +371,7 @@ const SettingsDialog = withStyles(styles, { withTheme: true })(
this.model.wifiConfigSettings.removeOnChangedHandler(this.handleWifiConfigSettingsChanged);
this.model.wifiDirectConfigSettings.removeOnChangedHandler(this.handleWifiDirectConfigSettingsChanged);
this.model.governorSettings.removeOnChangedHandler(this.handleGovernorSettingsChanged);

this.model.hasWifiDevice.removeOnChangedHandler(this.handleHasWifiChanged);
}
}

Expand Down
66 changes: 55 additions & 11 deletions src/HotspotManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ namespace pipedal::impl
virtual bool CancelPost(PostHandle handle) override;

virtual void SetNetworkChangingListener(NetworkChangingListener &&listener) override;
virtual void SetHasWifiListener(HasWifiListener &&listener) override;

private:
enum class State
Expand All @@ -75,6 +76,11 @@ namespace pipedal::impl
void SetState(State state);
State state = State::Initial;

bool hasWifi = false;
HasWifiListener hasWifiListener;
void SetHasWifi(bool hasWifi);
virtual bool GetHasWifi() override ;

void onClose();
void onError(const std::string &message);
void onInitialize();
Expand Down Expand Up @@ -224,6 +230,7 @@ void HotspotManagerImpl::onInitialize()
}
void HotspotManagerImpl::onDisconnect()
{
SetHasWifi(false);
if (state != State::Initial && state != State::WaitingForNetworkManager)
{
WaitForNetworkManager();
Expand All @@ -242,6 +249,8 @@ void HotspotManagerImpl::UpdateNetworkManagerStatus()
auto ethernetDevice = GetDevice(NM_DEVICE_TYPE_ETHERNET);
auto wlanDevice = GetDevice(NM_DEVICE_TYPE_WIFI);

SetHasWifi(!!wlanDevice);

if (ethernetDevice && wlanDevice)
{
if (state == State::Initial || state == State::WaitingForNetworkManager)
Expand Down Expand Up @@ -291,6 +300,7 @@ void HotspotManagerImpl::ReleaseNetworkManager()

this->onDeviceRemovedHandle = INVALID_DBUS_EVENT_HANDLE;
this->onDeviceAddedHandle = INVALID_DBUS_EVENT_HANDLE;
SetHasWifi(false);
}

void HotspotManagerImpl::onDevicesChanged()
Expand Down Expand Up @@ -401,6 +411,8 @@ void HotspotManagerImpl::onStartMonitoring()
OnEthernetStateChanged(ethernetDevice->State());
OnWlanStateChanged(wlanDevice->State());

SetHasWifi(true);

StartScanTimer();
onAccessPointChanged();
}
Expand Down Expand Up @@ -863,11 +875,11 @@ void HotspotManagerImpl::StartHotspot()
settings["ipv4"]["method"] = "shared";
settings["ipv6"]["method"] = "shared";
settings["ipv6"]["addr-gen-mode"] = 1; // "stable-privacy";

////////////////////////////////////////////////////////////////

// Create connection
settings["ipv4"]["method"] = sdbus::Variant(std::string("shared"));
////////////////////////////////////////////////////////////////

// Create connection
settings["ipv4"]["method"] = sdbus::Variant(std::string("shared"));
// settings["ipv6"]["method"] = sdbus::Variant(std::string("ignore"));

settings["ipv4"]["address-data"] = sdbus::Variant(std::vector<std::map<std::string, sdbus::Variant>>{{{"address", sdbus::Variant("192.168.60.1")},
Expand Down Expand Up @@ -1014,6 +1026,32 @@ bool HotspotManagerImpl::CancelPost(PostHandle handle)
return dbusDispatcher.CancelPost(handle);
}

void HotspotManagerImpl::SetHasWifiListener(HasWifiListener &&listener)
{
std::lock_guard<std::recursive_mutex> lock{this->networkChangingListenerMutex};
this->hasWifiListener = listener;
if (!this->closed && this->hasWifiListener)
{
this->hasWifiListener(this->hasWifi);
}
}
bool HotspotManagerImpl::GetHasWifi() {
std::lock_guard<std::recursive_mutex> lock{this->networkChangingListenerMutex};
return hasWifi;
}
void HotspotManagerImpl::SetHasWifi(bool hasWifi)
{
std::lock_guard<std::recursive_mutex> lock{this->networkChangingListenerMutex};
if (hasWifi != this->hasWifi)
{
this->hasWifi = hasWifi;
if (this->hasWifiListener)
{
this->hasWifiListener(this->hasWifi);
}
}
}

void HotspotManagerImpl::SetNetworkChangingListener(NetworkChangingListener &&listener)
{
std::lock_guard<std::recursive_mutex> lock{this->networkChangingListenerMutex};
Expand All @@ -1034,23 +1072,29 @@ std::vector<std::string> HotspotManagerImpl::GetKnownWifiNetworks()
return this->knownWifiNetworks;
}


static bool is_wireless_sysfs(const std::string& ifname) {
static bool is_wireless_sysfs(const std::string &ifname)
{
return fs::exists("/sys/class/net/" + ifname + "/wireless");
}

static std::vector<std::string> get_wireless_interfaces_sysfs() {
static std::vector<std::string> get_wireless_interfaces_sysfs()
{
std::vector<std::string> wireless_interfaces;
const std::string net_path = "/sys/class/net";

try {
for (const auto& entry : fs::directory_iterator(net_path)) {
try
{
for (const auto &entry : fs::directory_iterator(net_path))
{
std::string ifname = entry.path().filename();
if (is_wireless_sysfs(ifname)) {
if (is_wireless_sysfs(ifname))
{
wireless_interfaces.push_back(ifname);
}
}
} catch (const fs::filesystem_error& e) {
}
catch (const fs::filesystem_error &e)
{
std::cerr << "Error accessing " << net_path << ": " << e.what() << std::endl;
}

Expand Down
5 changes: 5 additions & 0 deletions src/HotspotManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ namespace pipedal {

virtual void SetNetworkChangingListener(NetworkChangingListener &&listener) = 0;

using HasWifiListener = std::function<void(bool hasWifi)>;
virtual void SetHasWifiListener(HasWifiListener &&listener) = 0;
virtual bool GetHasWifi() = 0;


using PostHandle = uint64_t;
using PostCallback = std::function<void()>;

Expand Down
Loading

0 comments on commit 0dfe7b8

Please sign in to comment.