2 * Example UPnP device/service, implementing the BinaryLight device and
3 * SwitchPower services to emulate a light switch.
5 * The user interface is as minimal as possible so that the GUPnP concepts and
6 * best practises are more apparent. For a better implementation of
7 * BinaryLight, see gupnp-tools.
9 * This example code is in the public domain.
12 #include <libgupnp/gupnp.h>
16 static gboolean status;
19 G_MODULE_EXPORT void set_target_cb (GUPnPService *service,
20 GUPnPServiceAction *action,
22 G_MODULE_EXPORT void get_target_cb (GUPnPService *service,
23 GUPnPServiceAction *action,
25 G_MODULE_EXPORT void get_status_cb (GUPnPService *service,
26 GUPnPServiceAction *action,
28 G_MODULE_EXPORT void query_target_cb (GUPnPService *service, char *variable,
29 GValue *value, gpointer user_data);
30 G_MODULE_EXPORT void query_status_cb (GUPnPService *service, char *variable,
31 GValue *value, gpointer user_data);
39 set_target_cb (GUPnPService *service,
40 GUPnPServiceAction *action,
41 G_GNUC_UNUSED gpointer user_data)
45 /* Get the new target value */
46 gupnp_service_action_get (action,
47 "NewTargetValue", G_TYPE_BOOLEAN, &target,
50 /* If the new target doesn't match the current status, change the status and
51 emit a notification that the status has changed. */
52 if (target != status) {
54 gupnp_service_notify (service,
55 "Status", G_TYPE_BOOLEAN, status,
58 g_print ("The light is now %s.\n", status ? "on" : "off");
61 /* Return success to the client */
62 gupnp_service_action_return (action);
67 get_target_cb (G_GNUC_UNUSED GUPnPService *service,
68 GUPnPServiceAction *action,
69 G_GNUC_UNUSED gpointer user_data)
71 gupnp_service_action_set (action,
72 "RetTargetValue", G_TYPE_BOOLEAN, status,
74 gupnp_service_action_return (action);
79 get_status_cb (G_GNUC_UNUSED GUPnPService *service,
80 GUPnPServiceAction *action,
81 G_GNUC_UNUSED gpointer user_data)
83 gupnp_service_action_set (action,
84 "ResultStatus", G_TYPE_BOOLEAN, status,
86 gupnp_service_action_return (action);
90 * State Variable query handlers
95 query_target_cb (G_GNUC_UNUSED GUPnPService *service,
96 G_GNUC_UNUSED char *variable,
98 G_GNUC_UNUSED gpointer user_data)
100 g_value_init (value, G_TYPE_BOOLEAN);
101 g_value_set_boolean (value, status);
106 query_status_cb (G_GNUC_UNUSED GUPnPService *service,
107 G_GNUC_UNUSED char *variable,
109 G_GNUC_UNUSED gpointer user_data)
111 g_value_init (value, G_TYPE_BOOLEAN);
112 g_value_set_boolean (value, status);
117 main (G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv)
119 GMainLoop *main_loop;
120 GError *error = NULL;
121 GUPnPContext *context;
122 GUPnPRootDevice *dev;
123 GUPnPServiceInfo *service;
125 #if !GLIB_CHECK_VERSION(2,35,0)
129 /* By default the light is off */
131 g_print ("The light is now %s.\n", status ? "on" : "off");
133 /* Create the UPnP context */
134 context = gupnp_context_new (NULL, NULL, 0, &error);
136 g_printerr ("Error creating the GUPnP context: %s\n",
138 g_error_free (error);
143 /* Create root device */
144 dev = gupnp_root_device_new (context, "BinaryLight1.xml", ".");
145 gupnp_root_device_set_available (dev, TRUE);
147 /* Get the switch service from the root device */
148 service = gupnp_device_info_get_service
149 (GUPNP_DEVICE_INFO (dev), "urn:schemas-upnp-org:service:SwitchPower:1");
151 g_printerr ("Cannot get SwitchPower1 service\n");
156 /* Autoconnect the action and state variable handlers. This connects
157 query_target_cb and query_status_cb to the Target and Status state
158 variables query callbacks, and set_target_cb, get_target_cb and
159 get_status_cb to SetTarget, GetTarget and GetStatus actions
161 gupnp_service_signals_autoconnect (GUPNP_SERVICE (service), NULL, &error);
163 g_printerr ("Failed to autoconnect signals: %s\n", error->message);
164 g_error_free (error);
169 /* Run the main loop */
170 main_loop = g_main_loop_new (NULL, FALSE);
171 g_main_loop_run (main_loop);
174 g_main_loop_unref (main_loop);
175 g_object_unref (service);
176 g_object_unref (dev);
177 g_object_unref (context);