summaryrefslogtreecommitdiff
path: root/lib/bluetooth/device.vala
blob: 8fe086fbd77c14a181b8eb6702d4ee5b38a5c206 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
namespace AstalBluetooth {
[DBus (name = "org.bluez.Device1")]
internal interface IDevice : DBusProxy {
    public abstract void cancel_pairing() throws Error;
    public abstract async void connect() throws Error;
    public abstract void connect_profile(string uuid) throws Error;
    public abstract async void disconnect() throws Error;
    public abstract void disconnect_profile(string uuid) throws Error;
    public abstract void pair() throws Error;

    public abstract string[] uuids { owned get; }
    public abstract bool blocked { get; set; }
    public abstract bool connected { get; }
    public abstract bool legacy_pairing { get; }
    public abstract bool paired { get; }
    public abstract bool trusted { get; set; }
    public abstract int16 rssi { get; }
    public abstract ObjectPath adapter { owned get; }
    public abstract string address { owned get; }
    public abstract string alias { owned get; set; }
    public abstract string icon { owned get; }
    public abstract string modalias { owned get; }
    public abstract string name { owned get; }
    public abstract uint16 appearance { get; }
    public abstract uint32 class { get; }
}

public class Device : Object {
    private IDevice proxy;
    public string object_path { owned get; construct set; }

    internal Device(IDevice proxy) {
        this.proxy = proxy;
        this.object_path = proxy.g_object_path;
        proxy.g_properties_changed.connect((props) => {
            var map = (HashTable<string, Variant>)props;
            foreach (var key in map.get_keys()) {
                var prop = kebab_case(key);
                if (get_class().find_property(prop) != null) {
                    notify_property(prop);
                }
            }
        });
    }

    public string[] uuids { owned get { return proxy.uuids; } }
    public bool connected { get { return proxy.connected; } }
    public bool legacy_pairing { get { return proxy.legacy_pairing; } }
    public bool paired { get { return proxy.paired; } }
    public int16 rssi { get { return proxy.rssi; } }
    public ObjectPath adapter { owned get { return proxy.adapter; } }
    public string address { owned get { return proxy.address; } }
    public string icon { owned get { return proxy.icon; } }
    public string modalias { owned get { return proxy.modalias; } }
    public string name { owned get { return proxy.name; } }
    public uint16 appearance { get { return proxy.appearance; } }
    public uint32 class { get { return proxy.class; } }
    public bool connecting { get; private set; }

    public bool blocked {
        get { return proxy.blocked; }
        set { proxy.blocked = value; }
    }

    public bool trusted {
        get { return proxy.trusted; }
        set { proxy.trusted = value; }
    }

    public string alias {
        owned get { return proxy.alias; }
        set { proxy.alias = value; }
    }

    public void cancel_pairing() {
        try { proxy.cancel_pairing(); } catch (Error err) { critical(err.message); }
    }

    public async void connect_device() {
        try {
            connecting = true;
            yield proxy.connect();
        } catch (Error err) {
            critical(err.message);
        } finally {
            connecting = false;
        }
    }

    public async void disconnect_device() {
        try { yield proxy.disconnect(); } catch (Error err) { critical(err.message); }
    }

    public void connect_profile(string uuid) {
        try { proxy.connect_profile(uuid); } catch (Error err) { critical(err.message); }
    }

    public void disconnect_profile(string uuid) {
        try { proxy.disconnect_profile(uuid); } catch (Error err) { critical(err.message); }
    }

    public void pair() {
        try { proxy.pair(); } catch (Error err) { critical(err.message); }
    }
}
}