2
void device_added()
{
     printf("Added \n");
     g_main_loop_quit (loop);
}

void device_removed()
{
     printf("Removed\n");
     g_main_loop_quit (loop);
}

int
main (int argc, char **argv)
{
  DBusGConnection *connection;
  DBusMessage* message;
  GError *error;
  DBusGProxy *proxy;
  char **name_list;
  char **name_list_ptr;
  gchar *m1;
  gchar *m2;

  g_type_init ();

  error = NULL;
  connection = dbus_g_bus_get(DBUS_BUS_SYSTEM,NULL);

  if (connection == NULL)
    {
      g_printerr ("Failed to open connection to bus: %s\n",
                  error->message);
      g_error_free (error);
      exit (1);
    }


  /* Create a proxy object for the "bus driver" (name "org.freedesktop.DBus") */
     proxy=dbus_g_proxy_new_for_name(connection,"org.freedesktop.UDisks","/org/freedesktop/UDisks","org.freedesktop.UDisks");

     if(proxy == NULL)
     {
        g_printerr ("Failed To Create A proxy...: %s\n", error->message);
        g_error_free (error);
        exit(1);
     }
     else
       printf("Probably got a connection to the correct interface...\n");


     m1=g_cclosure_marshal_VOID__STRING;
     m2=g_cclosure_marshal_VOID__STRING;

     dbus_g_object_register_marshaller(m1,G_TYPE_BOOLEAN,G_TYPE_STRING,G_TYPE_INVALID);

     dbus_g_object_register_marshaller(m2,G_TYPE_BOOLEAN,G_TYPE_STRING,G_TYPE_INVALID);

     dbus_g_proxy_add_signal(proxy,"DeviceAdded",G_TYPE_INVALID);
     dbus_g_proxy_add_signal(proxy,"DeviceRemoved",G_TYPE_INVALID);

     dbus_g_proxy_connect_signal(proxy,"DeviceAdded",(GCallback)device_added,NULL,NULL);

     dbus_g_proxy_connect_signal(proxy,"DeviceRemoved",(GCallback)device_removed,NULL,NULL);

     loop=g_main_loop_new(NULL,FALSE);

     g_main_loop_run (loop);

     return 0;
}

USBイベントを検出したいのですが、org.freedesktop.UDisksインターフェースからDeviceAddedおよびDeviceRemovedシグナルを使用しています。しかし、device_addedまたはdevice_removedが呼び出されていないので、上記のコードの何が問題になっているのか教えてください。

4

2 に答える 2

2
/*
Compile using the command 
 * gcc -o dbus-usb dbus-usb.c `pkg-config --libs --cflags dbus-glib-1`
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include <glib.h>
#include <glib-object.h>

static GMainLoop *loop;

static void
device_added (DBusGProxy *proxy,
         char *ObjectPath[],
         char *word_eol[],
         guint hook_id,
         guint context_id,
         gpointer user_data)
{
    g_debug("\nAdded a New Device:\nObjectPath: %s\nhook_id: %d\ncontext_id: %d\nuser_data: %d\n", (char*)ObjectPath, hook_id, context_id, (int)user_data); 
}


static void
device_removed (DBusGProxy *proxy,
         char *ObjectPath[],
         char *word_eol[],
         guint hook_id,
         guint context_id,
         gpointer user_data)
{
    g_debug("\nRemoved Device\nObjectPath: %s\nhook_id: %d\ncontext_id: %d\nuser_data: %d\n", (char*)ObjectPath, hook_id, context_id, (int)user_data); 
}

int
main (int argc, char **argv)
{
  DBusGConnection *connection;
  DBusMessage* message;
  GError *error;
  DBusGProxy *proxy;
  gchar *m1;
  gchar *m2;
  char *object_path;
  GPtrArray* ret;

  g_type_init ();

  error = NULL;
  connection = dbus_g_bus_get(DBUS_BUS_SYSTEM,NULL);

  if (connection == NULL)
    {
      g_printerr ("Failed to open connection to bus: %s\n",
                  error->message);
      g_error_free (error);
      exit (1);
    }
    else
    {
        printf("Got a connection to DBUS_BUS_SYSTEM\n");
    }


  /* Create a proxy object for the "bus driver" (name "org.freedesktop.DBus") */

     proxy=dbus_g_proxy_new_for_name(connection,"org.freedesktop.UDisks","/org/freedesktop/UDisks","org.freedesktop.UDisks");

     if(proxy == NULL)
     {
        g_printerr ("Failed To Create A proxy...: %s\n", error->message);
        g_error_free (error);
        exit(1);
     }
     else
       printf("Probably got a connection to the correct interface...\n");
//It works for me without marsheller register, add and connect to the signals directly
     m1=g_cclosure_marshal_VOID__STRING;
     m2=g_cclosure_marshal_VOID__STRING;

     dbus_g_object_register_marshaller(m1,G_TYPE_NONE,G_TYPE_STRING,G_TYPE_INVALID);

     dbus_g_object_register_marshaller(m2,G_TYPE_NONE,G_TYPE_STRING,G_TYPE_INVALID);

     dbus_g_proxy_add_signal(proxy,"DeviceAdded",DBUS_TYPE_G_OBJECT_PATH, G_TYPE_INVALID);
     dbus_g_proxy_connect_signal(proxy,"DeviceAdded",(GCallback)device_added,NULL,NULL);

     dbus_g_proxy_add_signal(proxy,"DeviceRemoved",DBUS_TYPE_G_OBJECT_PATH, G_TYPE_INVALID);
     dbus_g_proxy_connect_signal(proxy,"DeviceRemoved",(GCallback)device_removed,NULL,NULL);

     loop=g_main_loop_new(NULL,FALSE);

     g_main_loop_run (loop);

     g_error_free (error);

     return 0;
}

上記のコードは機能しており、Ubuntu PC で USB イベントを検出できます。主な変更点は、DBUS_TYPE_G_OBJECT_PATH パラメータが G_TYPE_STRING ではなく dbus_g_proxy_add_signal 関数に変更されたことです。

しかし、ARM アーキテクチャ用にクロス コンパイルして ARM ボードで同じものを実行すると、同じ問題が再び発生します。

于 2012-06-14T07:01:02.103 に答える
0

両方を起動した後、ARMでも動作するようになりました

dbus-daemon --system

/usr/libexec/udisks-daemon
于 2012-06-14T12:20:40.313 に答える