Not receiving subscribed-to signals #373
-
Hi there. I'm attempting to subscribe to messages from the NetworkManager on the system bus when the wireless properties change. Documentation is here. While searching these discussions, I found #161 which hinted to me to create a connection and enterEventLoop() at the end. By all indications, the following code should work but I am simply not receiving the signal, sudo or not. I have built and using the latest v1.4.0 running on Debian 12 aarch64 GNU/Linux on a Raspberry Pi 4. #include <sdbus-c++/sdbus-c++.h>
#include <vector>
#include <map>
#include <iostream>
const std::string dest = "org.freedesktop.NetworkManager";
const std::string iface = "org.freedesktop.NetworkManager";
const std::string d_iface = iface + ".Device";
int main(int argc, char** argv) {
auto connection = sdbus::createSystemBusConnection();
auto nm_proxy = sdbus::createProxy(*connection, dest, iface);
std::vector<sdbus::ObjectPath> paths = nm_proxy->getProperty("Devices").onInterface(iface);
std::string device_path;
for (auto dp = paths.begin(); dp != paths.end(); ++dp) {
auto d_proxy = sdbus::createProxy(*connection, dest, *dp);
uint32_t dtype = d_proxy->getProperty("DeviceType").onInterface(d_iface);
if (dtype == 2) {
std::cout <<"found wireless device: " << *dp << std::endl;
device_path = *dp;
break;
}
}
if (device_path.empty()) {
std::cerr << "no wireless device found." << std::endl;
return 1;
}
auto d_proxy = sdbus::createProxy(*connection, dest, device_path);
d_proxy->uponSignal("PropertiesChanged").onInterface("org.freedesktop.DBus.Properties")
.call([](std::string key, std::map<std::string, sdbus::Variant> change, std::map<std::string, sdbus::Variant> invalidated) {
std::cout << "Wireless PropertiesChanged" << std::endl;
});
d_proxy->finishRegistration();
connection->enterEventLoop();
} While that code is running, I can see what I think is the subscribe request and signals to which I've subscribed, but am not receiving, using dbus-monitor --system:
I feel like I have distilled the problem to the simplest possible case, yet cannot seem to figure out what I'm doing wrong here. I'm sure it's quite simple and any guidance anyone can provide is greatly appreciated. Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Sorry, I have no idea why the code is not formatting correctly. |
Beta Was this translation helpful? Give feedback.
-
Looking at the code (markdown description of which I modified to fix the formatting problem), I see two problems:
Let me know if this helped. |
Beta Was this translation helpful? Give feedback.
Looking at the code (markdown description of which I modified to fix the formatting problem), I see two problems:
nm_proxy
is created with wrong third argument. It shall be an object path, not an interface. This by itself shall have thrownsdbus::Error
exception already at this point.invalidated
parameter of the "PropertiesChanged" is of typeas
, i.e.std::vector<std::string>
, notstd::map<std::string, sdbus::Variant>
. Wrong argument type will result insdbus::Error
exception being thrown in the event loop thread, which in your case is also your main thread. So you should definitely see that exception.Let me know if this helped.