2016-06-01 4 views
0

D-Bus/Gioを通じてbluezにプロファイルを登録しようとしています。私がRegisterProfileを呼び出すと、すべてがOKになります。私は自分のGErrorをチェックし、それはNULLで、戻り値は空の​​GVariantです。私は多くのことを試しましたが、ここに私の最新のコードがあります。私はそれを実行すると "g_dbus_connection_call_sync succeeded"と表示されますが、d-feetを使ってD-Busで新しいプロファイルを見ることができず、テストデバイスを使って新しいプロファイルに接続できません。私は、テストデバイス上のコードを知っている、または少なくとも真っ直ぐなbluezで動作しましたが、私はD-Busでbluezを使用する方法を理解しようとしています。dbus/gioを使用してbluezにプロファイルを登録するにはどうすればよいですか?

#include <stdio.h> 
#include "bt_server.h" 

static const char *serial_service_class_uuid = 
    "00001101-0000-1000-8000-00805F9B34FB"; 
static const char *my_service_uuid = 
    "E62C4DCA-9ABC-11E5-8994-FEFF819CDC9F"; 

static const gchar btp_introspection_xml[] = 
    "<node>" 
    " <interface name='org.bluez.Profile1'>" 
    " <method name='Release' />" 
    " <method name='NewConnection'>" 
    "  <arg type='o' name='device' direction='in' />" 
    "  <arg type='h' name='fd' direction='in' />" 
    "  <arg type='a{sv}' name='fd_properties' direction='in' />" 
    " </method>" 
    " <method name='RequestDisconnection'>" 
    "  <arg type='o' name='device' direction='in' />" 
    " </method>" 
    " </interface>" 
    "</node>"; 

static const GDBusInterfaceVTable btp_interface_vtable = 
{ 
    BTProfile::method_call, 
    BTProfile::get_property, 
    BTProfile::set_property 
}; 

BTProfile::BTProfile() : bus_connection(NULL) 
{ 
} 

// --------------------------------------------------------------------------- 

void BTProfile::init() 
{ 
    GError *error = NULL; 
    GDBusNodeInfo *introspection = g_dbus_node_info_new_for_xml(
     btp_introspection_xml, &error); 
    if (!error) 
    { 
     GDBusInterfaceInfo *interface_info = g_dbus_node_info_lookup_interface(
      introspection, "org.bluez.Profile1"); 
     bus_connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error); 
     if (!error) 
     { 
      g_dbus_connection_register_object(bus_connection, 
       "/org/bluez/myprofile", interface_info, &btp_interface_vtable, 
       this, NULL, &error); 
      if (!error) 
      { 
       GVariantBuilder builder; 
       g_variant_builder_init(&builder, G_VARIANT_TYPE_DICTIONARY); 
       g_variant_builder_add(&builder, "{sv}", "Name", 
        g_variant_new("s", "myprofile")); 
       g_variant_builder_add(&builder, "{sv}", "Service", 
        g_variant_new("s", serial_service_class_uuid)); 
       g_variant_builder_add(&builder, "{sv}", "Channel", 
        g_variant_new("q", 1)); 
       g_variant_builder_add(&builder, "{sv}", "RequireAuthentication", 
        g_variant_new("b", FALSE)); 
       g_variant_builder_add(&builder, "{sv}", "Role", 
        g_variant_new("s", "client")); 
       g_variant_builder_add(&builder, "{sv}", "Version", 
        g_variant_new("q", 1)); 
       g_variant_builder_add(&builder, "{sv}", "AutoConnect", 
        g_variant_new("b", true)); 
       g_dbus_connection_call_sync(bus_connection, "org.bluez", 
        "/org/bluez", "org.bluez.ProfileManager1", 
        "RegisterProfile", g_variant_new("(osa{sv})", 
        "/org/bluez/myprofile", my_service_uuid, &builder), 
        NULL, G_DBUS_CALL_FLAGS_NONE, G_MAXINT, NULL, &error); 
       if (!error) 
       { 
        g_print("g_dbus_connection_call_sync succeeded\n"); 
       } 
       else 
       { 
        g_print("g_dbus_connection_call_sync failed: %s\n", 
         error->message); 
        g_error_free(error); 
       } 
      } 
      else 
      { 
       g_print("g_dbus_connection_register_object failed: %s\n", 
        error->message); 
       g_error_free(error); 
      } 
     } 
     else 
     { 
      g_print("g_bus_get_sync failed: %s\n", error->message); 
      g_error_free(error); 
     } 
    } 
    else 
    { 
     g_print("g_dbus_node_info_new_for_xml failed: %s\n", error->message); 
     g_error_free(error); 
    } 
} 

// --------------------------------------------------------------------------- 

void BTProfile::destroy() 
{ 
    if (bus_connection) 
    { 
     g_object_unref(bus_connection); 
    } 
} 

// --------------------------------------------------------------------------- 

void BTProfile::method_call(GDBusConnection *connection, const gchar *sender, 
    const gchar *object_path, const gchar *interface_name, 
    const gchar *method_name, GVariant *parameters, 
    GDBusMethodInvocation *invocation, gpointer user_data) 
{ 
    g_print("handle_method_call: called\n"); 
    g_dbus_method_invocation_return_value(invocation, NULL); 
} 

// --------------------------------------------------------------------------- 

GVariant *BTProfile::get_property(GDBusConnection *connection, 
    const gchar *sender, const gchar *object_path, const gchar *interface_name, 
    const gchar *property_name, GError **error, gpointer user_data) 
{ 
    g_print("get_property: called\n"); 
    return NULL; 
} 

// --------------------------------------------------------------------------- 

gboolean BTProfile::set_property(GDBusConnection *connection, 
    const gchar *sender, const gchar *object_path, const gchar *interface_name, 
    const gchar *property_name, GVariant *value, GError **error, 
    gpointer userData) 
{ 
    g_print("set_property: called\n"); 
    return false; 
} 

私は少し私は、エラー情報を取得していないかのように見て迷ってしまいました:

#ifndef BT_SERVER_H 
#define BT_SERVER_H 

#include <glib.h> 
#include <gio/gio.h> 

// --------------------------------------------------------------------------- 

class BTProfile 
{ 
public: 
    BTProfile(); 
    void init(); 
    void destroy(); 

    static void method_call(GDBusConnection *connection, 
     const gchar *sender, const gchar *object_path, 
     const gchar *interface_name, const gchar *method_name, 
     GVariant *parameters, GDBusMethodInvocation *invocation, 
     gpointer user_data); 
    static GVariant *get_property(GDBusConnection *connection, 
     const gchar *sender, const gchar *object_path, 
     const gchar *interface_name, const gchar *property_name, 
     GError **error, gpointer user_data); 
    static gboolean set_property(GDBusConnection *connection, 
     const gchar *sender, const gchar *object_path, 
     const gchar *interface_name, const gchar *property_name, 
     GVariant *value, GError **error, gpointer userData); 

protected: 
    GDBusConnection *bus_connection; 
}; 

はここに私の.cppファイルです:

は、ここに私のコードの.hファイルです。 ありがとうございます。

EDIT:

<!-- This configuration file specifies the required security policies 
    for Bluetooth core daemon to work. --> 

<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN" 
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd"> 
<busconfig> 

    <!-- ../system.conf have denied everything, so we just punch some holes --> 

    <policy user="root"> 
    <allow own="org.bluez"/> 
    <allow send_destination="org.bluez"/> 
    <allow send_interface="org.bluez.Agent1"/> 
    <allow send_interface="org.bluez.MediaEndpoint1"/> 
    <allow send_interface="org.bluez.MediaPlayer1"/> 
    <allow send_interface="org.bluez.ThermometerWatcher1"/> 
    <allow send_interface="org.bluez.AlertAgent1"/> 
    <allow send_interface="org.bluez.Profile1"/> 
    <allow send_interface="org.bluez.HeartRateWatcher1"/> 
    <allow send_interface="org.bluez.CyclingSpeedWatcher1"/> 
    <allow send_interface="org.bluez.GattCharacteristic1"/> 
    <allow send_interface="org.bluez.GattDescriptor1"/> 
    <allow send_interface="org.bluez.ProfileManager1"/> 
    <allow send_interface="org.bluez.Device1"/> 
    <allow send_interface="org.freedesktop.DBus.ObjectManager"/> 
    <allow send_interface="org.freedesktop.DBus.Properties"/> 
    </policy> 

    <policy at_console="true"> 
    <allow send_destination="org.bluez"/> 
    </policy> 

    <!-- allow users of lp group (printing subsystem) to 
     communicate with bluetoothd --> 
    <policy group="lp"> 
    <allow send_destination="org.bluez"/> 
    </policy> 

    <policy context="default"> 
    <deny send_destination="org.bluez"/> 
    </policy> 

</busconfig> 

が、私はそれにProfileManager1とデバイス1を追加しましたが、まだ運: 周り掘り後、私はここに私のものです、DBUSのbluetooth.confファイルを見つけました。誰か私がここで行方不明を知っていますか?ストレートブルーズを使ってプロファイルを登録して使うことができるので、bluez/dbusの問題だと分かっています。

答えて

0

私はそれを理解したと信じています。私はとにかく働いている。多くの例を読んだ後、私はこれをやってしまいました:

1:g_bus_own_nameでバス名を所有しています。

2:誰場合RegisterProfileメソッドを呼び出す

私はコードを投稿することができます:私のon_bus_acquiredコールバックで私はGDBusConnectionがg_dbus_connection_register_objectのパラメータ(GDBusConnection自分自身を作成し​​ようとは対照的に)

3とすることを使用します興味があります。

今はすべて機能しているようです。もう一つは、RegisterProfileへのパラメータでした。私はSerialUUIDを使って接続していますが、最初は "server"という役割パラメタも設定していました。それは私のプロファイルが接続を拒否する原因となっていました。私は、このパラメータは異なる種類のプロファイルのためだと思うが、わからない。誰かが確かに知っていますか?

+0

共有しても大丈夫なら、最終結果を見たいと思います。上記の 'GVariantBuilder'を使って' g_variant_builder_init(&builder、G_VARIANT_TYPE_DICTIONARY) 'で初期化することにも興味があります。実際には 'g_variant_builder_init(&builder、G_VARIANT_TYPE_ARRAY)'ではありませんか? –

関連する問題