device: renew dhcp leases on awake for software devices
[NetworkManager.git] / src / nm-sleep-monitor-upower.c
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License along
13  * with this program; if not, write to the Free Software Foundation, Inc.,
14  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15  *
16  * (C) Copyright 2012 Red Hat, Inc.
17  * Author: Matthias Clasen <mclasen@redhat.com>
18  */
19
20 #include "nm-default.h"
21
22 #include <errno.h>
23 #include <string.h>
24 #include <sys/stat.h>
25
26 #include "nm-core-internal.h"
27
28 #include "nm-sleep-monitor.h"
29
30 #define UPOWER_DBUS_SERVICE "org.freedesktop.UPower"
31
32 struct _NMSleepMonitor {
33         GObject parent_instance;
34
35         GDBusProxy *upower_proxy;
36 };
37
38 struct _NMSleepMonitorClass {
39         GObjectClass parent_class;
40
41         void (*sleeping) (NMSleepMonitor *monitor);
42         void (*resuming) (NMSleepMonitor *monitor);
43 };
44
45
46 enum {
47         SLEEPING,
48         RESUMING,
49         LAST_SIGNAL,
50 };
51 static guint signals[LAST_SIGNAL] = {0};
52
53 G_DEFINE_TYPE (NMSleepMonitor, nm_sleep_monitor, G_TYPE_OBJECT);
54
55 /********************************************************************/
56
57 static void
58 upower_sleeping_cb (GDBusProxy *proxy, gpointer user_data)
59 {
60         nm_log_dbg (LOGD_SUSPEND, "Received UPower sleeping signal");
61         g_signal_emit (user_data, signals[SLEEPING], 0);
62 }
63
64 static void
65 upower_resuming_cb (GDBusProxy *proxy, gpointer user_data)
66 {
67         nm_log_dbg (LOGD_SUSPEND, "Received UPower resuming signal");
68         g_signal_emit (user_data, signals[RESUMING], 0);
69 }
70
71 static void
72 nm_sleep_monitor_init (NMSleepMonitor *self)
73 {
74         GError *error = NULL;
75
76         self->upower_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
77                                                             G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START |
78                                                                 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
79                                                             NULL,
80                                                             UPOWER_DBUS_SERVICE,
81                                                             "/org/freedesktop/UPower",
82                                                             "org.freedesktop.UPower",
83                                                             NULL, &error);
84         if (self->upower_proxy) {
85                 _nm_dbus_signal_connect (self->upower_proxy, "Sleeping", NULL,
86                                          G_CALLBACK (upower_sleeping_cb), self);
87                 _nm_dbus_signal_connect (self->upower_proxy, "Resuming", NULL,
88                                          G_CALLBACK (upower_resuming_cb), self);
89         } else {
90                 nm_log_warn (LOGD_SUSPEND, "could not initialize UPower D-Bus proxy: %s", error->message);
91                 g_error_free (error);
92         }
93 }
94
95 static void
96 finalize (GObject *object)
97 {
98         NMSleepMonitor *self = NM_SLEEP_MONITOR (object);
99
100         g_clear_object (&self->upower_proxy);
101
102         G_OBJECT_CLASS (nm_sleep_monitor_parent_class)->finalize (object);
103 }
104
105 static void
106 nm_sleep_monitor_class_init (NMSleepMonitorClass *klass)
107 {
108         GObjectClass *gobject_class;
109
110         gobject_class = G_OBJECT_CLASS (klass);
111
112         gobject_class->finalize = finalize;
113
114         signals[SLEEPING] = g_signal_new (NM_SLEEP_MONITOR_SLEEPING,
115                                           NM_TYPE_SLEEP_MONITOR,
116                                           G_SIGNAL_RUN_LAST,
117                                           G_STRUCT_OFFSET (NMSleepMonitorClass, sleeping),
118                                           NULL,                   /* accumulator      */
119                                           NULL,                   /* accumulator data */
120                                           g_cclosure_marshal_VOID__VOID,
121                                           G_TYPE_NONE, 0);
122         signals[RESUMING] = g_signal_new (NM_SLEEP_MONITOR_RESUMING,
123                                           NM_TYPE_SLEEP_MONITOR,
124                                           G_SIGNAL_RUN_LAST,
125                                           G_STRUCT_OFFSET (NMSleepMonitorClass, resuming),
126                                           NULL,                   /* accumulator      */
127                                           NULL,                   /* accumulator data */
128                                           g_cclosure_marshal_VOID__VOID,
129                                           G_TYPE_NONE, 0);
130 }
131
132 NM_DEFINE_SINGLETON_GETTER (NMSleepMonitor, nm_sleep_monitor_get, NM_TYPE_SLEEP_MONITOR);
133
134 /* ---------------------------------------------------------------------------------------------------- */