繁体   English   中英

C ++:遍历DBUS消息以使用迭代器查找BlueZ 5适配器

[英]C++: Traverse DBUS message to find BlueZ 5 adapter using iterator

我正在尝试使用BlueZ 5更新的Dbus API查找可用的蓝牙适配器。

与BlueZ 4完全有一个方法(即org.bluez.Manager.FindAdapter())不同,BlueZ 5将FreeDesktop ObjectManager接口与GetManagedObjects()方法一起使用。 此方法返回大量结果:

array [
      dict entry(
         object path "/org/bluez"
         array [
            dict entry(
               string "org.freedesktop.DBus.Introspectable"
               array [
               ]
            )
            dict entry(
               string "org.bluez.AgentManager1"
               array [
               ]
            )
            dict entry(
               string "org.bluez.ProfileManager1"
               array [
               ]
            )
         ]
      )
      dict entry(
         object path "/org/bluez/hci0"
         array [
            dict entry(
               string "org.freedesktop.DBus.Introspectable"
               array [
               ]
            )
            dict entry(
               string "org.bluez.Adapter1"
               array [
                  dict entry(
                     string "Address"
                     variant                         string "XX:XX:XX:XX:XX:XX"
                  )
                  dict entry(
                     string "Name"
                     variant                         string "My_Adapter"
                  )
                  dict entry(
                     string "Alias"
                     variant                         string "kubuntu-0"
                  )
                  dict entry(
                     string "Class"
                     variant                         uint32 0
                  )
                  dict entry(
                     string "Powered"
                     variant                         boolean false
                  )
                  dict entry(
                     string "Discoverable"
                     variant                         boolean true
                  )
                  dict entry(
                     string "DiscoverableTimeout"
                     variant                         uint32 0
                  )
                  dict entry(
                     string "Pairable"
                     variant                         boolean true
                  )
                  dict entry(
                     string "PairableTimeout"
                     variant                         uint32 0
                  )
                  dict entry(
                     string "Discovering"
                     variant                         boolean false
                  )
                  dict entry(
                     string "UUIDs"
                     variant                         array [
                           string "00001200-0000-1000-8000-00805f9b34fb"
                           string "00001800-0000-1000-8000-00805f9b34fb"
                           string "00001801-0000-1000-8000-00805f9b34fb"
                           string "0000110e-0000-1000-8000-00805f9b34fb"
                           string "0000110c-0000-1000-8000-00805f9b34fb"
                        ]
                  )
                  dict entry(
                     string "Modalias"
                     variant                         string "usb:sdfasdfsadf"
                  )
               ]
            )
            dict entry(
               string "org.freedesktop.DBus.Properties"
               array [
               ]
            )
            dict entry(
               string "org.bluez.Media1"
               array [
               ]
            )
            dict entry(
               string "org.bluez.NetworkServer1"
               array [
               ]
            )
         ]
      )
      .
      .etc.
      . ]

BlueZ 5 API简介和移植指南说:“ 默认适配器的概念总是有点模糊,无法更改其值,因此,如果应用程序需要类似的东西,则可以例如选择它们遇到的第一个适配器。 GetManagedObjects答复。

我已经使用迭代器编写了C ++代码来做到这一点(我没有使用Python的经验),但是对于看起来如此简单的东西却真的很久了:

if (dbus_message_iter_init(reply, &rootIter) && //point iterator to reply message
    DBUS_TYPE_ARRAY == dbus_message_iter_get_arg_type(&rootIter)) //get the type of message that iter points to
{
    debug_print("Type Array.\n");   
    DBusMessageIter arrayElementIter;
    dbus_message_iter_recurse(&rootIter, &arrayElementIter); //assign new iterator to first element of array
    debug_print("-->Descending into Array (recursing).\n");

   while(!adapterFound){
    if (DBUS_TYPE_DICT_ENTRY == dbus_message_iter_get_arg_type(&arrayElementIter))
    {
        debug_print("  Type Dict_Entry.\n");

        DBusMessageIter dictEntryIter;
        dbus_message_iter_recurse(&arrayElementIter,&dictEntryIter ); //assign new iterator to first element of
        debug_print("  -->Descending into Dict_Entry (recursing).\n");
        if (DBUS_TYPE_OBJECT_PATH == dbus_message_iter_get_arg_type(&dictEntryIter))
                {
            debug_print("    Type DBUS_TYPE_OBJECT_PATH.\n");
            dbus_message_iter_get_basic(&dictEntryIter, &adapter_path);  
            if(device && strstr(adapter_path,device)) 
            {   
               adapterFound = TRUE;
               debug_print("    Adapter %s FOUND!\n",device);
            }
        }
        dbus_message_iter_next(&dictEntryIter);
        if (DBUS_TYPE_ARRAY == dbus_message_iter_get_arg_type(&dictEntryIter))
                    {
                            debug_print("    Type DBUS_TYPE_ARRAY.\n");
            DBusMessageIter innerArrayIter;
            dbus_message_iter_recurse(&dictEntryIter, &innerArrayIter);
            dbus_message_iter_next(&innerArrayIter);
            debug_print("    -->Descending into Array (recursing).\n");
            if (DBUS_TYPE_DICT_ENTRY == dbus_message_iter_get_arg_type(&innerArrayIter))
            {
                debug_print("      Type Dict_Entry.\n");
                DBusMessageIter innerDictEntryIter;
                dbus_message_iter_recurse(&innerArrayIter,&innerDictEntryIter ); //assign new iterator to first element of
                debug_print("      -->Descending into Dict_Entry (recursing).\n");
                if (DBUS_TYPE_STRING == dbus_message_iter_get_arg_type(&innerDictEntryIter))
                {
                    debug_print("        Type DBUS_TYPE_STRING.\n");
                    char *dbusObject;
                    dbus_message_iter_get_basic(&innerDictEntryIter, &dbusObject);
                    debug_print("        This is an object of type:%s.\n", dbusObject);
                    if(strncmp(dbusObject,adapterString,strCmpLimit))
                        debug_print("        Not an adapter.\n");
                    else{
                        if(!device)
                        {
                           debug_print("        No adapter given, so using first available: %s\n", adapter_path);
                           adapterFound = TRUE;
                        }
                        debug_print("        Adapter.\n");
                    }
                }
                debug_print("      <--Ascending from Dict_Entry.\n");
            }
            debug_print("    <--Ascending from Array (top layer).\n");
                            //dbus_message_iter_get_basic(&dictEntryIter, &adapter_path);  
                    }

        debug_print("  <--Ascending from Dict_Entry.\n");
    }
    debug_print("<--Ascending from Array (top layer).\n");
    if(!dbus_message_iter_has_next(&arrayElementIter)) break; //check to see if end of array
    else dbus_message_iter_next(&arrayElementIter);

    }//while loop end --used to traverse array
} //end if - outer arrray
else return 0;

换句话说,我必须为树的每个分支定义一个新的迭代器,以便每次都找到位于固定位置的字符串。 (array [x]-> secondfield [2]-> firstfieldstring)我的问题(最后,我知道)是有人可以指出一种更优雅/更快的解决方案吗?

您可以使用应该由BLuez 5 Adapter对象实现的org.freedesktop.DBus.Properties接口来获取适配器属性。

据我所知,您仍然需要遍历Array直到找到适配器对象路径,但是从那里开始,您可以执行以下操作:

DBusMessage *msg = dbus_message_new_method_call(
        "org.bluez",
        adapter_object_path,
        "org.freedesktop.DBus.Properties", "Get");

const char *property = "Powered"; // (e.g.)
dbus_message_append_args(msg,
        DBUS_TYPE_STRING, &property,
        DBUS_TYPE_INVALID);

//...

这样,您可以只在数组中查找适配器对象路径,而忽略其余所有内容。 通过此对象路径,您可以请求所需的属性和/或开始发送方法调用。

我不确定是否可以认为这更“优雅”,并且可能不会更快,因为我认为额外的方法调用将比数组遍历慢。 它应该/可以减少代码量,但使其更具可读性...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM