beginnings of bluez5 compat
authorKevron Rees <kevron.m.rees@intel.com>
Wed, 15 Jan 2014 22:35:47 +0000 (14:35 -0800)
committerKevron Rees <kevron.m.rees@intel.com>
Fri, 17 Jan 2014 21:29:05 +0000 (13:29 -0800)
plugins/common/CMakeLists.txt
plugins/common/bluetooth.hpp
plugins/common/bluetooth5.cpp [new file with mode: 0644]
plugins/common/bluetooth5.h [new file with mode: 0644]

index c9da1de..032379d 100644 (file)
@@ -1,5 +1,5 @@
-set(plugins_common_sources abstractio.hpp serialport.hpp bluetoothadapterproxy.c bluetooth.hpp bluetoothmanagerproxy.c bluetoothserialproxy.c)
-set(plugins_common_headers_install abstractio.hpp serialport.hpp bluetooth.hpp bluetoothadapterproxy.h bluetoothmanagerproxy.h bluetoothserialproxy.h)
+set(plugins_common_sources abstractio.hpp serialport.hpp bluetoothadapterproxy.c bluetooth.hpp bluetoothmanagerproxy.c bluetoothserialproxy.c bluetooth5.cpp)
+set(plugins_common_headers_install abstractio.hpp serialport.hpp bluetooth.hpp bluetoothadapterproxy.h bluetoothmanagerproxy.h bluetoothserialproxy.h bluetooth5.h)
 
 add_library(amb-plugins-common SHARED ${plugins_common_sources})
 
index ddc5f27..8886e3c 100644 (file)
@@ -11,7 +11,7 @@ class BluetoothDevice
 {
 public:
 
-       std::string getDeviceForAddress( std::string address,std::string adapterAddy = "")
+       std::string getDeviceForAddress(std::string address, std::string adapterAddy = "")
        {
                GError* error = NULL;
                OrgBluezManager* manager = org_bluez_manager_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM,
diff --git a/plugins/common/bluetooth5.cpp b/plugins/common/bluetooth5.cpp
new file mode 100644 (file)
index 0000000..fd6a644
--- /dev/null
@@ -0,0 +1,93 @@
+#include "bluetooth5.h"
+#include "debugout.h"
+#include <gio/gio.h>
+#include <string>
+
+std::string findAdapterPath(std::string adapterAddy)
+{
+       std::string adapterObjectPath;
+
+       GError * error = nullptr;
+       GDBusProxy * managerProxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, NULL,
+                                                                                                                         "org.freedesktop.DBus",
+                                                                                                                         "/",
+                                                                                                                         "org.freedesktop.DBus.ObjectManager",
+                                                                                                                         nullptr,&error);
+
+       if(error)
+       {
+               DebugOut(DebugOut::Error)<<"Could not create ObjectManager proxy for Bluez: "<<error->message<<endl;
+               g_error_free(error);
+               return "";
+       }
+
+       GVariant * objectMap = g_dbus_proxy_call_sync(managerProxy, "GetManagedObjects",nullptr, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
+
+       if(error)
+       {
+               DebugOut(DebugOut::Error)<<"Failed call to GetManagedObjects: "<<error->message<<endl;
+               g_object_unref(managerProxy);
+               g_error_free(error);
+               return "";
+       }
+
+       GVariantIter* iter;
+       char* objPath;
+       GVariantIter* level2Dict;
+
+       g_variant_get(objectMap, "a{oa{sa{sv}}}",&iter);
+
+       while(g_variant_iter_next(iter, "oa{sa{sv}}}",&objPath, &level2Dict))
+       {
+               char * interfaceName;
+               GVariantIter* innerDict;
+               while(g_variant_iter_next(level2Dict, "sa{sv}", &interfaceName, &innerDict))
+               {
+                       if(std::string(interfaceName) == "org.bluez.Adapter1")
+                       {
+                               char* propertyName;
+                               GVariant* value;
+
+                               while(g_variant_iter_next(innerDict,"sv", &propertyName, &value))
+                               {
+                                       if(std::string(propertyName) == "Address")
+                                       {
+                                               char* addy;
+                                               g_variant_get(value,"s",&addy);
+
+                                               if(adapterAddy == "" || addy && std::string(addy) == adapterAddy)
+                                               {
+                                                       adapterObjectPath = objPath;
+                                               }
+
+                                               g_free(addy);
+                                       }
+                                       g_free(propertyName);
+                                       g_variant_unref(value);
+                               }
+                       }
+                       g_free(interfaceName);
+                       g_variant_iter_free(innerDict);
+               }
+               g_free(objPath);
+               g_variant_iter_free(level2Dict);
+       }
+       g_variant_iter_free(iter);
+
+       return adapterObjectPath;
+}
+
+std::string Bluetooth5::getDeviceForAddress(std::string address, std::string adapterAddy)
+{
+       std::string result;
+
+       std::string adapterPath = findAdapterPath(adapterAddy);
+
+       if(adapterPath == "")
+       {
+               DebugOut(DebugOut::Error)<<"Adapter not found ("<<adapterAddy<<")"<<endl;
+               return "";
+       }
+
+       return result;
+}
diff --git a/plugins/common/bluetooth5.h b/plugins/common/bluetooth5.h
new file mode 100644 (file)
index 0000000..46c4eef
--- /dev/null
@@ -0,0 +1,13 @@
+#ifndef BLUETOOTH_5_H_
+#define BLUETOOOTH_5_H_
+
+#include <string>
+
+class Bluetooth5
+{
+public:
+       std::string getDeviceForAddress(std::string address, std::string adapterAddy = "");
+       void disconnect(std::string address, std::string adapterAddy = "");
+};
+
+#endif