Browse Source

mconnect: use DBus proxies for devicemanager and device

bboozzoo/dbus-support-interfaces
Maciek Borzecki 8 years ago
parent
commit
14f051f675
5 changed files with 177 additions and 74 deletions
  1. +2
    -0
      Makefile.am
  2. +12
    -4
      src/mconnect/application.vala
  3. +41
    -0
      src/mconnect/device-proxy.vala
  4. +108
    -0
      src/mconnect/devicemanager-proxy.vala
  5. +14
    -70
      src/mconnect/devicemanager.vala

+ 2
- 0
Makefile.am View File

@ -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 \


+ 12
- 4
src/mconnect/application.vala View File

@ -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);
}


+ 41
- 0
src/mconnect/device-proxy.vala View File

@ -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 <maciek.borzecki (at] gmail.com>
*/
/**
* 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;
}
}

+ 108
- 0
src/mconnect/devicemanager-proxy.vala View File

@ -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 <maciek.borzecki (at] gmail.com>
*/
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<string, DeviceDBusProxy> devices;
private int device_idx = 0;
public DeviceManagerDBusProxy.with_manager(DBusConnection bus,
DeviceManager manager) {
this.manager = manager;
this.bus = bus;
this.devices = new HashMap<string, DeviceDBusProxy>();
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;
}
}

+ 14
- 70
src/mconnect/devicemanager.vala View File

@ -19,32 +19,18 @@
*/
using Gee;
[DBus (name = "org.mconnect.DeviceManager")]
class DeviceManager : GLib.Object
{
public const string DEVICES_CACHE_FILE = "devices";
private HashMap<string, DeviceWrapper?> 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<string, Device> devices;
public DeviceManager() {
debug("device manager..");
this.devices = new HashMap<string, DeviceWrapper?>();
this.devices = new HashMap<string, Device>();
}
/**
@ -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;
}
}

Loading…
Cancel
Save