From 14f051f675b4456f67cab1482779727b938aff70 Mon Sep 17 00:00:00 2001 From: Maciek Borzecki Date: Thu, 1 Jun 2017 18:26:01 +0200 Subject: [PATCH] mconnect: use DBus proxies for devicemanager and device --- Makefile.am | 2 + src/mconnect/application.vala | 16 +++- src/mconnect/device-proxy.vala | 41 ++++++++++ src/mconnect/devicemanager-proxy.vala | 108 ++++++++++++++++++++++++++ src/mconnect/devicemanager.vala | 84 ++++---------------- 5 files changed, 177 insertions(+), 74 deletions(-) create mode 100644 src/mconnect/device-proxy.vala create mode 100644 src/mconnect/devicemanager-proxy.vala diff --git a/Makefile.am b/Makefile.am index b71a7bb..7bfcdb4 100644 --- a/Makefile.am +++ b/Makefile.am @@ -56,7 +56,9 @@ mconnect_SOURCES = \ src/mconnect/discovery.vala \ src/mconnect/packet.vala \ src/mconnect/device.vala \ + src/mconnect/device-proxy.vala \ src/mconnect/devicemanager.vala \ + src/mconnect/devicemanager-proxy.vala \ src/mconnect/devicechannel.vala \ src/mconnect/core.vala \ src/mconnect/packethandlerinterface.vala \ diff --git a/src/mconnect/application.vala b/src/mconnect/application.vala index 4dc9a41..949f75e 100644 --- a/src/mconnect/application.vala +++ b/src/mconnect/application.vala @@ -33,6 +33,7 @@ namespace Mconn { private Discovery discovery = null; private DeviceManager manager = null; + private DeviceManagerDBusProxy bus_manager = null; public Application() { Object(application_id: "org.mconnect"); @@ -60,8 +61,9 @@ namespace Mconn { Notify.init("mconnect"); discovery.device_found.connect((disc, dev) => { - manager.found_device(dev); + manager.handle_new_device(dev); }); + try { discovery.listen(); } catch (Error e) { @@ -76,15 +78,21 @@ namespace Mconn { hold(); } - public override bool dbus_register(DBusConnection conn, string object_path) throws Error { + public override bool dbus_register(DBusConnection conn, + string object_path) throws Error { + + this.bus_manager = new DeviceManagerDBusProxy.with_manager(conn, + this.manager); + this.bus_manager.publish(); base.dbus_register(conn, object_path); debug("dbus register, path %s", object_path); - conn.register_object("/org/mconnect/manager", manager); return true; } - public override void dbus_unregister(DBusConnection conn, string object_path) { + public override void dbus_unregister(DBusConnection conn, + string object_path) { + base.dbus_unregister(conn, object_path); debug("dbus unregister, path %s", object_path); } diff --git a/src/mconnect/device-proxy.vala b/src/mconnect/device-proxy.vala new file mode 100644 index 0000000..4118fa2 --- /dev/null +++ b/src/mconnect/device-proxy.vala @@ -0,0 +1,41 @@ +/* ex:ts=4:sw=4:sts=4:et */ +/* -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ +/** + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * AUTHORS + * Maciek Borzecki + */ + +/** + * General device wrapper. + */ +[DBus (name = "org.mconnect.Device")] +class DeviceDBusProxy : Object { + + public string device_id { get; private set; default = ""; } + public string device_name { get; private set; default = ""; } + public string device_type { get; private set; default = ""; } + public uint protocol_version {get; private set; default = 5; } + public string address { get; private set; default = ""; } + public bool is_paired { get; private set; default = false; } + public bool allowed {get; set; default = false; } + + [DBus (visible = false)] + public Device device {get; private set; default = null; } + + public DeviceDBusProxy.for_device(Device device) { + this.device = device; + } +} \ No newline at end of file diff --git a/src/mconnect/devicemanager-proxy.vala b/src/mconnect/devicemanager-proxy.vala new file mode 100644 index 0000000..546ca7f --- /dev/null +++ b/src/mconnect/devicemanager-proxy.vala @@ -0,0 +1,108 @@ +/* ex:ts=4:sw=4:sts=4:et */ +/* -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ +/** + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * AUTHORS + * Maciek Borzecki + */ +using Gee; + +[DBus (name = "org.mconnect.DeviceManager")] +class DeviceManagerDBusProxy : Object +{ + private DeviceManager manager; + + private const string DBUS_PATH = "/org/mconnect/manager"; + private DBusConnection bus = null; + private HashMap devices; + + private int device_idx = 0; + + public DeviceManagerDBusProxy.with_manager(DBusConnection bus, + DeviceManager manager) { + this.manager = manager; + this.bus = bus; + this.devices = new HashMap(); + + manager.found_device.connect((d) => { + this.add_device(d); + }); + } + + [DBus (visible = false)] + public void publish() throws IOError { + assert(this.bus != null); + + this.bus.register_object(DBUS_PATH, this); + } + + /** + * allow_device: + * @path: device object path + * + * Allow given device + */ + public void allow_device(string path) { + debug("allow device %s", path); + + var dev_proxy = this.devices.@get(path); + + this.manager.allow_device(dev_proxy.device); + } + + /** + * list_devices: + * + * Returns a list of DBus paths of all known devices + */ + public ObjectPath[] list_devices() { + ObjectPath[] devices = {}; + + foreach (var path in this.devices.keys) { + devices += new ObjectPath(path); + } + return devices; + } + + private void add_device(Device dev) { + var device_proxy = new DeviceDBusProxy.for_device(dev); + var path = make_device_path(); + + this.devices.@set(path, device_proxy); + + info("register device %s under path %s", + dev.to_string(), path); + try { + this.bus.register_object(path, device_proxy); + } catch (IOError err) { + warning("failed to register DBus object for device %s under path %s", + dev.to_string(), path); + } + } + + /** + * make_device_path: + * + * return device path string that can be used as ObjectPath + */ + private string make_device_path() { + var path = "/org/mconnect/device/%d".printf(this.device_idx); + + // bump device index + this.device_idx++; + + return path; + } +} \ No newline at end of file diff --git a/src/mconnect/devicemanager.vala b/src/mconnect/devicemanager.vala index 86131c6..1d39a76 100644 --- a/src/mconnect/devicemanager.vala +++ b/src/mconnect/devicemanager.vala @@ -19,32 +19,18 @@ */ using Gee; -[DBus (name = "org.mconnect.DeviceManager")] class DeviceManager : GLib.Object { - public const string DEVICES_CACHE_FILE = "devices"; - - private HashMap devices; - private int device_idx = 0; - - /** - * DBus wrapper for devices - */ - private struct DeviceWrapper { - ObjectPath object_path; - Device device; + public signal void found_device(Device dev); - DeviceWrapper (string path, Device device) { - this.object_path = new ObjectPath(path); - this.device = device; - } - } + public const string DEVICES_CACHE_FILE = "devices"; + private HashMap devices; public DeviceManager() { debug("device manager.."); - this.devices = new HashMap(); + this.devices = new HashMap(); } /** @@ -100,8 +86,7 @@ class DeviceManager : GLib.Object var kf = new KeyFile(); - foreach (var wrapper in devices.values) { - var dev = wrapper.device; + foreach (var dev in devices.values) { dev.to_cache(kf, dev.device_name); } @@ -115,38 +100,24 @@ class DeviceManager : GLib.Object } } - /** - * make_device_path: - * - * return device path string that can be used as ObjectPath - */ - private string make_device_path() { - var path = "/org/mconnect/device/%d".printf(this.device_idx); - - // bump device index - device_idx++; - - return path; - } - [DBus (visible = false)] - public void found_device(Device dev) { - debug("found device: %s", dev.to_string()); + public void handle_new_device(Device new_dev) { + debug("found device: %s", new_dev.to_string()); var is_new = false; - string unique = dev.to_unique_string(); + string unique = new_dev.to_unique_string(); debug("device key: %s", unique); if (this.devices.has_key(unique) == false) { debug("adding new device with key: %s", unique); - this.devices.@set(unique, - DeviceWrapper(make_device_path(), dev)); + + this.devices.@set(unique, new_dev); + is_new = true; - } else { - var wrapper = this.devices.@get(unique); - dev = wrapper.device; } + var dev = this.devices.@get(unique); + if (device_allowed(dev)) { dev.allowed = true; } @@ -212,20 +183,7 @@ class DeviceManager : GLib.Object * * Allow given device */ - public void allow_device(string path) { - debug("allow device %s", path); - - Device dev = null; - foreach (var dw in this.devices.values) { - if (dw.object_path == path) - dev = dw.device; - } - - if (dev == null) { - warning("device with path %s not found", path); - return; - } - + public void allow_device(Device dev) { dev.allowed = true; // update device cache @@ -234,18 +192,4 @@ class DeviceManager : GLib.Object // maybe activate if needed activate_device(dev); } - - /** - * list_devices: - * - * Returns a list of DBus paths of all known devices - */ - public ObjectPath[] list_devices() { - ObjectPath[] devices = {}; - - foreach (var dw in this.devices.values) { - devices += dw.object_path; - } - return devices; - } } \ No newline at end of file