Use DBUS to detect presence of Wi-Fi devices.

This commit is contained in:
Robin E. R. Davies
2024-11-20 14:50:05 -05:00
parent ca87658f90
commit 0dfe7b8080
8 changed files with 316 additions and 313 deletions
+55 -11
View File
@@ -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
@@ -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();
@@ -224,6 +230,7 @@ void HotspotManagerImpl::onInitialize()
}
void HotspotManagerImpl::onDisconnect()
{
SetHasWifi(false);
if (state != State::Initial && state != State::WaitingForNetworkManager)
{
WaitForNetworkManager();
@@ -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)
@@ -291,6 +300,7 @@ void HotspotManagerImpl::ReleaseNetworkManager()
this->onDeviceRemovedHandle = INVALID_DBUS_EVENT_HANDLE;
this->onDeviceAddedHandle = INVALID_DBUS_EVENT_HANDLE;
SetHasWifi(false);
}
void HotspotManagerImpl::onDevicesChanged()
@@ -401,6 +411,8 @@ void HotspotManagerImpl::onStartMonitoring()
OnEthernetStateChanged(ethernetDevice->State());
OnWlanStateChanged(wlanDevice->State());
SetHasWifi(true);
StartScanTimer();
onAccessPointChanged();
}
@@ -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")},
@@ -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};
@@ -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;
}
+5
View File
@@ -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()>;
+75 -51
View File
@@ -99,29 +99,33 @@ PiPedalModel::PiPedalModel()
{
OnNetworkChanging(ethernetConnected, hotspotEnabling);
});
hotspotManager->SetHasWifiListener(
[this](bool hasWifi)
{
this->SetHasWifi(hasWifi);
});
// don't actuall start the hotspotManager until after LV2 is initialized (in order to avoid logging oddities)
}
void PrepareSnapshostsForSave(Pedalboard&pedalboard)
void PrepareSnapshostsForSave(Pedalboard &pedalboard)
{
if (pedalboard.selectedSnapshot() != -1) {
auto&currentSnapshot = pedalboard.snapshots()[pedalboard.selectedSnapshot()];
if (!currentSnapshot || currentSnapshot->isModified_)
if (pedalboard.selectedSnapshot() != -1)
{
auto &currentSnapshot = pedalboard.snapshots()[pedalboard.selectedSnapshot()];
if (!currentSnapshot || currentSnapshot->isModified_)
{
pedalboard.selectedSnapshot(-1);
}
}
for (auto &snapshot: pedalboard.snapshots())
for (auto &snapshot : pedalboard.snapshots())
{
if (snapshot) {
if (snapshot)
{
snapshot->isModified_ = false;
}
}
}
void PiPedalModel::Close()
{
std::unique_ptr<AudioHost> oldAudioHost;
@@ -139,8 +143,8 @@ void PiPedalModel::Close()
}
// take a snapshot incase a client unsusbscribes in the notification handler (in which case the mutex won't protect us)
std::vector<IPiPedalModelSubscriber::ptr> t {subscribers.begin(),subscribers.end()};
for (auto&subscriber: t)
std::vector<IPiPedalModelSubscriber::ptr> t{subscribers.begin(), subscribers.end()};
for (auto &subscriber : t)
{
subscriber->Close();
}
@@ -351,7 +355,8 @@ void PiPedalModel::RemoveNotificationSubsription(std::shared_ptr<IPiPedalModelSu
void PiPedalModel::PreviewControl(int64_t clientId, int64_t pedalItemId, const std::string &symbol, float value)
{
IEffect *effect = lv2Pedalboard->GetEffect(pedalItemId);
if (!effect) {
if (!effect)
{
return;
}
if (effect->IsVst3())
@@ -384,7 +389,8 @@ void PiPedalModel::OnNotifyMaybeLv2StateChanged(uint64_t instanceId)
PedalboardItem *item = pedalboard.GetItem(instanceId);
if (item != nullptr)
{
if (!audioHost) {
if (!audioHost)
{
return;
}
bool changed = this->audioHost->UpdatePluginState(*item);
@@ -395,7 +401,7 @@ void PiPedalModel::OnNotifyMaybeLv2StateChanged(uint64_t instanceId)
Lv2PluginState newState = item->lv2State();
FireLv2StateChanged(instanceId,newState);
FireLv2StateChanged(instanceId, newState);
}
}
}
@@ -481,7 +487,6 @@ void PiPedalModel::FireJackConfigurationChanged(const JackConfiguration &jackCon
{
// noify subscribers.
std::vector<IPiPedalModelSubscriber::ptr> t{subscribers.begin(), subscribers.end()};
for (auto &subscriber : t)
{
@@ -531,15 +536,13 @@ void PiPedalModel::SetPedalboard(int64_t clientId, Pedalboard &pedalboard)
}
}
void PiPedalModel::SetSnapshot(int64_t selectedSnapshot)
{
std::lock_guard<std::recursive_mutex> lock(mutex);
if (this->pedalboard.ApplySnapshot(selectedSnapshot, pluginHost))
{
this->pedalboard.selectedSnapshot(selectedSnapshot);
for (auto snapshot: this->pedalboard.snapshots())
for (auto snapshot : this->pedalboard.snapshots())
{
if (snapshot)
{
@@ -554,7 +557,6 @@ void PiPedalModel::SetSnapshots(std::vector<std::shared_ptr<Snapshot>> &snapshot
{
std::lock_guard<std::recursive_mutex> lock(mutex);
UpdateVst3Settings(pedalboard);
this->pedalboard.snapshots(std::move(snapshots));
@@ -562,7 +564,7 @@ void PiPedalModel::SetSnapshots(std::vector<std::shared_ptr<Snapshot>> &snapshot
if (selectedSnapshot != -1)
{
this->pedalboard.selectedSnapshot(selectedSnapshot);
for (auto &snapshot: pedalboard.snapshots())
for (auto &snapshot : pedalboard.snapshots())
{
if (snapshot)
{
@@ -570,12 +572,11 @@ void PiPedalModel::SetSnapshots(std::vector<std::shared_ptr<Snapshot>> &snapshot
}
}
}
this->FirePedalboardChanged(-1, false); // notify clients (but don't change the running pedalboard, because it's still the same)
// this means that all clients get an up-to-date copy of the snapshots AND the currently selected snapshot if that applies
// (and a fresh copy of the pedalboard settings as well, which is harmless, since they have not changed)
this->SetPresetChanged(-1, true,false);
this->SetPresetChanged(-1, true, false);
}
void PiPedalModel::UpdateCurrentPedalboard(int64_t clientId, Pedalboard &pedalboard)
@@ -638,12 +639,13 @@ void PiPedalModel::SetPresetChanged(int64_t clientId, bool value, bool changeSna
{
if (changeSnapshotSelect && value && this->pedalboard.selectedSnapshot() != -1)
{
auto & snapshot = this->pedalboard.snapshots()[pedalboard.selectedSnapshot()];
if (snapshot) {
auto &snapshot = this->pedalboard.snapshots()[pedalboard.selectedSnapshot()];
if (snapshot)
{
if (!snapshot->isModified_)
{
snapshot->isModified_ = true;
FireSnapshotModified(pedalboard.selectedSnapshot(),true);
snapshot->isModified_ = true;
FireSnapshotModified(pedalboard.selectedSnapshot(), true);
}
}
@@ -664,10 +666,9 @@ void PiPedalModel::FireSnapshotModified(int64_t snapshotIndex, bool modified)
std::vector<IPiPedalModelSubscriber::ptr> t{subscribers.begin(), subscribers.end()};
for (auto &subscriber : t)
{
subscriber->OnSnapshotModified(snapshotIndex,modified);
subscriber->OnSnapshotModified(snapshotIndex, modified);
}
}
}
void PiPedalModel::FireSelectedSnapshotChanged(int64_t selectedSnapshot)
@@ -752,15 +753,15 @@ void PiPedalModel::UpdateVst3Settings(Pedalboard &pedalboard)
#endif
}
void PiPedalModel::FireLv2StateChanged(int64_t instanceId, const Lv2PluginState &lv2State) {
void PiPedalModel::FireLv2StateChanged(int64_t instanceId, const Lv2PluginState &lv2State)
{
std::lock_guard<std::recursive_mutex> guard{mutex};
std::vector<IPiPedalModelSubscriber::ptr> t{subscribers.begin(), subscribers.end()};
for (auto &subscriber : t)
{
subscriber->OnLv2StateChanged(instanceId,lv2State);
subscriber->OnLv2StateChanged(instanceId, lv2State);
}
}
// referesh the plugin state for all plugins.
@@ -774,13 +775,12 @@ bool PiPedalModel::SyncLv2State()
{
if (audioHost->UpdatePluginState(*item))
{
FireLv2StateChanged(item->instanceId(),item->lv2State());
FireLv2StateChanged(item->instanceId(), item->lv2State());
changed = true;
}
}
}
return changed;
}
void PiPedalModel::SaveCurrentPreset(int64_t clientId)
{
@@ -790,7 +790,6 @@ void PiPedalModel::SaveCurrentPreset(int64_t clientId)
SyncLv2State();
PrepareSnapshostsForSave(pedalboard);
storage.SaveCurrentPreset(this->pedalboard);
this->SetPresetChanged(clientId, false);
}
@@ -819,15 +818,14 @@ int64_t PiPedalModel::SavePluginPresetAs(int64_t instanceId, const std::string &
return presetId;
}
int64_t PiPedalModel::SaveCurrentPresetAs(int64_t clientId, const std::string &name, int64_t saveAfterInstanceId)
{
std::lock_guard<std::recursive_mutex> guard{mutex};
SyncLv2State();
auto pedalboard = this->pedalboard.DeepCopy();
PrepareSnapshostsForSave(pedalboard);
PrepareSnapshostsForSave(pedalboard);
UpdateVst3Settings(pedalboard);
pedalboard.name(name);
int64_t result = storage.SaveCurrentPresetAs(pedalboard, name, saveAfterInstanceId);
@@ -873,8 +871,8 @@ void PiPedalModel::NextBank(Direction direction)
}
size_t index = 0;
for (size_t i = 0; i < bankIndex.entries().size(); ++i)
{
auto&entry = bankIndex.entries()[i];
{
auto &entry = bankIndex.entries()[i];
if (entry.instanceId() == bankIndex.selectedBank())
{
index = i;
@@ -888,15 +886,19 @@ void PiPedalModel::NextBank(Direction direction)
{
index = 0;
}
} else {
}
else
{
if (index == 0)
{
index = bankIndex.entries().size()-1;
} else {
index = bankIndex.entries().size() - 1;
}
else
{
--index;
}
}
this->OpenBank(-1,bankIndex.entries()[index].instanceId());
this->OpenBank(-1, bankIndex.entries()[index].instanceId());
}
void PiPedalModel::NextPreset(Direction direction)
{
@@ -966,11 +968,13 @@ void PiPedalModel::OnNotifyNextMidiProgram(const RealtimeNextMidiProgramRequest
}
}
void PiPedalModel::OnNotifyMidiRealtimeSnapshotRequest(int32_t snapshotIndex,int64_t snapshotRequestId)
void PiPedalModel::OnNotifyMidiRealtimeSnapshotRequest(int32_t snapshotIndex, int64_t snapshotRequestId)
{
try {
try
{
SetSnapshot((int64_t)snapshotIndex);
} catch (const std::exception&e)
}
catch (const std::exception &e)
{
Lv2Log::error(SS("SetSnapshot failed. " << e.what()));
}
@@ -1006,7 +1010,6 @@ void PiPedalModel::OnNotifyNextMidiBank(const RealtimeNextMidiProgramRequest &re
}
}
void PiPedalModel::OnNotifyMidiProgramChange(RealtimeMidiProgramRequest &midiProgramRequest)
{
std::lock_guard<std::recursive_mutex> guard{mutex};
@@ -2054,7 +2057,9 @@ void PiPedalModel::OnPatchSetReply(uint64_t instanceId, LV2_URID patchSetPropert
if (properties.contains(propertyUri) && properties[propertyUri] == atomObject)
{
// do noting.
} else {
}
else
{
properties[propertyUri] = std::move(atomObject);
}
@@ -2234,7 +2239,6 @@ std::vector<AlsaDeviceInfo> PiPedalModel::GetAlsaDevices()
result.push_back(MakeDummyDeviceInfo(8));
#endif
return result;
}
const std::filesystem::path &PiPedalModel::GetWebRoot() const
@@ -2617,7 +2621,6 @@ void PiPedalModel::OnNetworkChanged(bool ethernetConnected, bool hotspotConnecte
FireNetworkChanged();
}
void PiPedalModel::OnNotifyMidiRealtimeEvent(RealtimeMidiEventType eventType)
{
try
@@ -2698,3 +2701,24 @@ void PiPedalModel::RequestShutdown(bool restart)
}
}
}
void PiPedalModel::SetHasWifi(bool hasWifi)
{
std::lock_guard<std::recursive_mutex> lock(mutex);
if (this->hasWifi != hasWifi)
{
this->hasWifi = hasWifi;
std::vector<IPiPedalModelSubscriber::ptr> t{subscribers.begin(), subscribers.end()};
for (auto &subscriber : t)
{
subscriber->OnHasWifiChanged(hasWifi);
}
}
}
bool PiPedalModel::GetHasWifi()
{
std::lock_guard<std::recursive_mutex> lock(mutex);
return hasWifi;
}
+7
View File
@@ -92,6 +92,7 @@ namespace pipedal
virtual void OnErrorMessage(const std::string &message) = 0;
virtual void OnLv2PluginsChanging() = 0;
virtual void OnNetworkChanging(bool hotspotConnected) = 0;
virtual void OnHasWifiChanged(bool hasWifi) = 0;
virtual void Close() = 0;
};
@@ -107,6 +108,9 @@ namespace pipedal
using NetworkChangedListener = std::function<void(void)>;
private:
bool hasWifi = false;
void SetHasWifi(bool hasWifi);
std::unique_ptr<HotspotManager> hotspotManager;
std::unique_ptr<Updater> updater;
@@ -257,6 +261,9 @@ namespace pipedal
Increase,
Decrease
};
bool GetHasWifi();
void NextBank(Direction direction = Direction::Increase);
void PreviousBank() { NextBank(Direction::Decrease); }
void NextPreset(Direction direction = Direction::Increase);
+11
View File
@@ -1080,6 +1080,11 @@ public:
UpdateStatus updateStatus = model.GetUpdateStatus();
this->Reply(replyTo, "getUpdateStatus", updateStatus);
}
else if (message == "getHasWifi")
{
bool result = model.GetHasWifi();
this->Reply(replyTo, "getHasWifi",result);
}
else if (message == "updateNow")
{
std::string updateUrl;
@@ -1727,6 +1732,12 @@ private:
Send("onLv2PluginsChanging", true);
Flush();
}
virtual void OnHasWifiChanged(bool hasWifi){
Send("onHasWifiChanged", hasWifi);
Flush();
}
virtual void OnNetworkChanging(bool hotspotConnected) override
{
try