summaryrefslogtreecommitdiff
path: root/src/librygel-core/rygel-energy-management.vala
blob: fe93f53e851fda771f672108c365929a1f015956 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
 * Copyright (C) 2013 Intel Corporation.
 *
 * Author: Jussi Kukkonen <jussi.kukkonen@intel.com>
 *
 * This file is part of Rygel.
 *
 * Rygel is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Rygel 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

/* EnergyManagement service implementation. The service will be run on
 * plugins that have
 *     energy-management=true
 * set in their configuration. It requires UPower to function properly.
 *
 * Every network interface that supports Wake-On should have a
 * configuration group:
 *     [EnergyManagement-eth0]
 *     mode-on-suspend=IP-down-WakeOn
 *     supported-transport=UDP-Broadcast
 *     password=FEEDDEADBEEF
 * mode-on-suspend is required (without it the mode will always be
 * "Unimplemented"), other configuration items are not.
 */


using Gee;
using GLib;
using GUPnP;

[DBus (name = "org.freedesktop.UPower")]
interface UPower : Object {
    public signal void sleeping ();
    public signal void resuming ();
}

/**
 * Implementation of UPnP EnergyManagement service.
 */
public class Rygel.EnergyManagement : Service {
    public const string UPNP_ID = "urn:upnp-org:serviceId:EnergyManagement";
    public const string UPNP_TYPE = "urn:schemas-upnp-org:service:EnergyManagement:1";
    public const string DESCRIPTION_PATH = "xml/EnergyManagement.xml";

    private const string TEMPLATE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                                    "<NetworkInterfaceInfo xsi:schemaLocation=\"urn:schemas-upnp-org:lp:em-NetworkInterfaceInfo http://www.upnp.org/schemas/lp/em-NetworkInterfaceInfo.xsd\" " +
                                    "                      xmlns=\"urn:schemas-upnp-org:lp:em-NetworkInterfaceInfo\" " +
                                    "                      xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
                                    "%s" +
                                    "</NetworkInterfaceInfo>";

    private Configuration config;
    private bool sleeping;
    private UPower upower;

    public override void constructed () {
        base.constructed ();

        this.config = MetaConfig.get_default ();

        this.sleeping = false;


        try {
            this.upower = Bus.get_proxy_sync
                                        (BusType.SYSTEM,
                                         "org.freedesktop.UPower",
                                         "/org/freedesktop/UPower",
                                         DBusProxyFlags.DO_NOT_LOAD_PROPERTIES);
            this.upower.sleeping.connect (this.upower_sleeping_cb);
            this.upower.resuming.connect (this.upower_resuming_cb);
        } catch (GLib.IOError err) {
            /* this will lead to NetworkInterfaceMode "Unimplemented" */
        }

        this.query_variable["NetworkInterfaceInfo"].connect
                                        (this.query_network_interface_info_cb);
        this.query_variable["ProxiedNetworkInterfaceInfo"].connect
                                        (this.query_proxied_network_interface_info_cb);

        this.action_invoked["GetInterfaceInfo"].connect
                                        (this.get_interface_info_cb);
    }

    private void upower_sleeping_cb () {
        if (this.sleeping == true) {
            return;
        }

        this.sleeping = true;
        this.notify ("NetworkInterfaceInfo",
                     typeof (string),
                     this.create_network_interface_info ());
    }

    private void upower_resuming_cb () {
        if (this.sleeping == false) {
            return;
        }

        this.sleeping = false;
        this.notify ("NetworkInterfaceInfo",
                     typeof (string),
                     this.create_network_interface_info ());
    }

    extern static bool get_mac_and_network_type (string iface,
                                                 out string mac,
                                                 out string type);

    private string create_network_interface_info () {

        string mac_address, type;
        bool success = true;

        var iface = this.root_device.context.interface;
        var config_section ="EnergyManagement-%s".printf (iface);

        success = EnergyManagement.get_mac_and_network_type (iface, out mac_address, out type);

        var mac = mac_address.replace (":", "");

        var wake_pattern = "FFFFFFFFFFFF%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s".printf (
                                        mac, mac, mac, mac, mac, mac, mac, mac,
                                        mac, mac, mac, mac, mac, mac, mac, mac);
        try {
            var password = this.config.get_string (config_section, "password");
            wake_pattern = wake_pattern.concat (password);
        } catch (GLib.Error error) { }

        var ip_addr = new InetAddress.from_string (this.root_device.context.host_ip);
        bool is_ipv6 = (ip_addr != null && ip_addr.family == SocketFamily.IPV6);
        var associated_ips = "<Ipv%d>%s</Ipv%d>".printf
                                        (is_ipv6 ? 6 : 4,
                                         this.root_device.context.host_ip,
                                         is_ipv6 ? 6 : 4);

        string mode;
        if (!success || this.upower == null) {
            mode = "Unimplemented";
        } else {
            try {
                /* Note: we want to check the value exists even when not sleeping */
                var sleep_mode = this.config.get_string (config_section,
                                                         "mode-on-suspend");
                mode = this.sleeping ? sleep_mode : "IP-up";
            } catch (GLib.Error error) {
                mode = "Unimplemented";
            }
        }

        string transport_node;
        try {
            var val = this.config.get_string (config_section,
                                              "supported-transport");
            transport_node = "<WakeSupportedTransport>%s</WakeSupportedTransport>".printf
                                        (val);
        } catch (GLib.Error error) {
            transport_node = "";
        }

        var device_info = ("<DeviceInterface>" +
                           "<DeviceUUID>%s</DeviceUUID>" +
                           "<FriendlyName>%s</FriendlyName>" +
                           "<NetworkInterface>" +
                           "<SystemName>%s</SystemName>" +
                           "<MacAddress>%s</MacAddress>" +
                           "<InterfaceType>%s</InterfaceType>" +
                           "<NetworkInterfaceMode>%s</NetworkInterfaceMode>" +
                           "<AssociatedIpAddresses>%s</AssociatedIpAddresses>" +
                           "<WakeOnPattern>%s</WakeOnPattern>" +
                           "%s" +
                           "</NetworkInterface>" +
                           "</DeviceInterface>").printf
                                        (this.root_device.udn,
                                         this.root_device.get_friendly_name (),
                                         iface,
                                         mac_address,
                                         type,
                                         mode,
                                         associated_ips,
                                         wake_pattern,
                                         transport_node);

        return TEMPLATE.printf (device_info);
    }

    private string create_proxied_network_interface_info () {
        /* No proxy support: Return empty NetworkInterfaceInfo */

        return TEMPLATE.printf ("");
    }

    private void query_network_interface_info_cb (Service   em,
                                                  string    var,
                                                  ref Value val) {
        val.init (typeof (string));
        val.set_string (this.create_network_interface_info ());
    }

    private void query_proxied_network_interface_info_cb (Service   em,
                                                          string    var,
                                                          ref Value val) {
        val.init (typeof (string));
        val.set_string (this.create_proxied_network_interface_info ());
    }

    private void get_interface_info_cb (Service       em,
                                        ServiceAction action) {
        if (action.get_argument_count () != 0) {
            action.return_error (402, _("Invalid argument"));

            return;
        }

        action.set ("NetworkInterfaceInfo",
                    typeof (string),
                    this.create_network_interface_info ());

        action.set ("ProxiedNetworkInterfaceInfo",
                    typeof (string),
                    this.create_proxied_network_interface_info ());

        action.return_success ();
    }
}