1

我是 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);
        }
}

我可以获取属性更改信号,但如何获取其他信号。

4

1 回答 1

0

查看http://projects.gnome.org/NetworkManager/developers/api/09/spec.html上的 NetworkManager 规范——它为您提供了接口的所有细节。

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

于 2013-04-05T12:25:11.993 回答