Debian Codegraph @

src/event-dumper/event-dumper.c

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 // SPDX-FileCopyrightText: Copyright 2022 Jens Georg <[email protected]>
3
4 #include <config.h>
5
6 #include <glib-unix.h>
7 #include <glib.h>
8 #include <libgupnp/gupnp.h>
9
10 #include <iso646.h>
11
12 void
13 on_variable_notify (GUPnPServiceProxy *proxy,
14 const char *variable,
15 GValue *value,
16 gpointer user_data)
17 {
18 g_autoptr (GDateTime) dt = g_date_time_new_now_local ();
19 g_autofree char *timestr = g_date_time_format_iso8601 (dt);
20
21 g_print ("%s|%s|%s|%s|%s\n",
22 timestr,
23 gupnp_service_info_get_udn (GUPNP_SERVICE_INFO (user_data)),
24 gupnp_service_info_get_id (GUPNP_SERVICE_INFO (user_data)),
25 variable,
26 g_value_get_string (value));
27 }
28
29 void
30 on_introspection (GObject *source, GAsyncResult *res, gpointer user_data)
31 {
32 g_autoptr (GError) error = NULL;
33
34 g_autoptr (GUPnPServiceIntrospection) is =
35 gupnp_service_info_introspect_finish (
36 GUPNP_SERVICE_INFO (source),
37 res,
38 &error);
39
40 g_autofree char *id =
41 gupnp_service_info_get_id (GUPNP_SERVICE_INFO (source));
42
43 if (error != NULL) {
44 g_warning ("Failed to introspect service proxy %s: %s",
45 id,
46 error->message);
47
48 return;
49 } else {
50 g_info ("Got introspection for %s", id);
51 }
52
53 const GList *state_variables =
54 gupnp_service_introspection_list_state_variables (is);
55 for (const GList *it = state_variables; it != NULL;
56 it = g_list_next (it)) {
57 GUPnPServiceStateVariableInfo *info = it->data;
58 if (not info->send_events)
59 continue;
60
61 g_info ("Subscribing to %s (type: %s)",
62 info->name,
63 g_type_name (info->type));
64
65 gupnp_service_proxy_add_notify (GUPNP_SERVICE_PROXY (source),
66 info->name,
67 G_TYPE_STRING,
68 on_variable_notify,
69 source);
70 }
71 }
72
73 void
74 subscribe_to_proxy (GUPnPServiceProxy *sp, gpointer user_data)
75 {
76 g_object_ref (sp);
77 g_autofree char *id =
78 gupnp_service_info_get_id (GUPNP_SERVICE_INFO (sp));
79
80 g_info ("Found new service proxy %s", id);
81 gupnp_service_info_introspect_async (GUPNP_SERVICE_INFO (sp),
82 NULL,
83 on_introspection,
84 NULL);
85 gupnp_service_proxy_set_subscribed (sp, TRUE);
86 }
87
88 void
89 on_proxy_available (GUPnPControlPoint *cp,
90 GUPnPDeviceProxy *dp,
91 gpointer user_data)
92 {
93 g_autofree char *id =
94 gupnp_device_info_get_friendly_name (GUPNP_DEVICE_INFO (dp));
95
96 g_info ("New device %s, type %s at %s",
97 id,
98 gupnp_device_info_get_device_type (GUPNP_DEVICE_INFO (dp)),
99 gupnp_device_info_get_location (GUPNP_DEVICE_INFO (dp)));
100
101 GList *services =
102 gupnp_device_info_list_services (GUPNP_DEVICE_INFO (dp));
103
104 g_list_foreach ((GList *) services, (GFunc) subscribe_to_proxy, NULL);
105
106 // Shallow-free the list, so we keep a reference to each service
107 g_list_free_full (services, (GDestroyNotify) g_object_unref);
108
109 g_object_ref (dp);
110 }
111
112 void
113 on_proxy_unavailable (GUPnPControlPoint *cp,
114 GUPnPDeviceProxy *dp,
115 gpointer user_data)
116 {
117 g_autofree char *id =
118 gupnp_device_info_get_friendly_name (GUPNP_DEVICE_INFO (dp));
119
120 g_info ("Lost service proxy %s", id);
121
122 // Dropping the reference we added in on_proxy_available
123 g_object_unref (dp);
124 }
125
126 void
127 on_context_available (GUPnPContextManager *cm,
128 GUPnPContext *ctx,
129 gpointer user_data)
130 {
131
132 g_info ("New context: %s",
133 gssdp_client_get_host_ip (GSSDP_CLIENT (ctx)));
134 g_autoptr (GUPnPControlPoint) cp =
135 gupnp_control_point_new (ctx, "upnp:rootdevice");
136
137 g_signal_connect (cp,
138 "device-proxy-available",
139 G_CALLBACK (on_proxy_available),
140 NULL);
141
142 g_signal_connect (cp,
143 "device-proxy-unavailable",
144 G_CALLBACK (on_proxy_unavailable),
145 NULL);
146
147 gssdp_resource_browser_set_active (GSSDP_RESOURCE_BROWSER (cp), TRUE);
148
149 gupnp_context_manager_manage_control_point (cm, cp);
150 }
151
152 void
153 on_context_unavailable (GUPnPContextManager *cm,
154 GUPnPContext *ctx,
155 gpointer user_data)
156 {
157 g_info ("Context gone: %s",
158 gssdp_client_get_host_ip (GSSDP_CLIENT (ctx)));
159 }
160
161 int
162 main (int argc, char *argv[])
163 {
164 g_autoptr (GUPnPContextManager) cm = gupnp_context_manager_create (0);
165 g_autoptr (GMainLoop) loop = g_main_loop_new (NULL, FALSE);
166
167 g_signal_connect (cm,
168 "context-available",
169 G_CALLBACK (on_context_available),
170 NULL);
171
172 g_signal_connect (cm,
173 "context-unavailable",
174 G_CALLBACK (on_context_unavailable),
175 NULL);
176
177 g_unix_signal_add (SIGINT, (GSourceFunc) g_main_loop_quit, loop);
178 g_unix_signal_add (SIGTERM, (GSourceFunc) g_main_loop_quit, loop);
179
180 g_main_loop_run (loop);
181
182 return 0;
183 }