接口上生成的信号名称" org.freedesktop.NetworkManager.Device" Linux中的网络管理器

时间:2013-04-05 12:11:40

标签: c linux linux-kernel dbus

我是Dbus的新手,我正在尝试捕获网络电缆插入或拔出时产生的信号。我正在尝试使用“http://software.intel.com/en-us/articles/detecting-network-connectivity-using-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);
        }
}

我可以获得属性改变信号但是如何获得其他信号。

1 个答案:

答案 0 :(得分:0)

查看http://projects.gnome.org/NetworkManager/developers/api/09/spec.html处的NetworkManager规范 - 它为您提供了界面的所有详细信息。

您可能感兴趣的信号将是“DeviceAdded”和“DeviceRemoved”以发现设备何时进出(电缆插入和拔出)。您还可以深入到org.freedesktop.NetworkManager.Device以获取“PropertiesChanged”信号并获取“state”属性等。