私は Dbus を初めて使用し、ネットワーク ケーブルが差し込まれたとき、または差し込まれたときに生成される信号をキャッチしようとしています。 -d-bus を使用したネットワーク接続"
情報を取得するために dbus_message_is_signal() で使用する信号名。私の概念をクリアできるサンプル コードも提供してください。
私のコードは次のとおりです。
enter code here
#include<stdio.h>
#include<dbus/dbus.h>
#include <gdbus.h>
#include<stdbool.h>
#include<unistd.h>
#include<stdlib.h>
#include<dbus/dbus-glib-bindings.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#define PLATFORM_SERVICE "org.freedesktop.NetworkManager"
#define PLATFORM_PATH "/org/freedesktop/NetworkManager"
#define PLATFORM_CONNECTION_IF "org.freedesktop.NetworkManager"
main()
{
DBusMessage* msg;
DBusConnection* conn;
DBusError err;
printf("Listening for signals\n");
// initialise the errors
dbus_error_init(&err);
//connect to the bus and check for errors
conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
if (dbus_error_is_set(&err))
{
fprintf(stderr, "Connection Error (%s)n", err.message);
dbus_error_free(&err);
}
if (NULL == conn)
{
printf("Error in connection\n");
exit(1);
}
dbus_bus_add_match(conn, "type='signal',interface='org.freedesktop.NetworkManager'", &err);
dbus_connection_flush(conn);
if (dbus_error_is_set(&err))
{
fprintf(stderr, "Match Error (%s)n", err.message);
exit(1);
}
printf("Match rule sent\n");
g_message("Listening to D-BUS signals using a connection filter");
// loop listening for signals being emmitted
while (true)
{
printf("in while \n");
// non blocking read of the next available message
dbus_connection_read_write(conn,0);
msg = dbus_connection_pop_message(conn);
// loop again if we haven't read a message
if (NULL == msg)
{
sleep(1);
continue;
}
if (dbus_message_is_signal(msg, PLATFORM_CONNECTION_IF,"PropertiesChanged"))
printf("Received signal propertyChanged \n");
if (dbus_message_is_signal(msg, PLATFORM_CONNECTION_IF, "DeviceRemoved"))
printf("Received signal %s\n", "Device changed");
// free the message
dbus_message_unref(msg);
}
}
プロパティが変更された信号を取得できますが、他の信号を取得する方法。