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;
18 G_MODULE_EXPORT void set_target_cb (GUPnPService *service,
19 GUPnPServiceAction *action,
21 G_MODULE_EXPORT void get_target_cb (GUPnPService *service,
22 GUPnPServiceAction *action,
24 G_MODULE_EXPORT void get_status_cb (GUPnPService *service,
25 GUPnPServiceAction *action,
27 G_MODULE_EXPORT void query_target_cb (GUPnPService *service, char *variable,
28 GValue *value, gpointer user_data);
29 G_MODULE_EXPORT void query_status_cb (GUPnPService *service, char *variable,
30 GValue *value, gpointer user_data);
38 set_target_cb (GUPnPService *service, GUPnPServiceAction *action, gpointer user_data)
42 /* Get the new target value */
43 gupnp_service_action_get (action,
44 "NewTargetValue", G_TYPE_BOOLEAN, &target,
47 /* If the new target doesn't match the current status, change the status and
48 emit a notification that the status has changed. */
49 if (target != status) {
51 gupnp_service_notify (service,
52 "Status", G_TYPE_BOOLEAN, status,
55 g_print ("The light is now %s.\n", status ? "on" : "off");
58 /* Return success to the client */
59 gupnp_service_action_return (action);
64 get_target_cb (GUPnPService *service, GUPnPServiceAction *action, gpointer user_data)
66 gupnp_service_action_set (action,
67 "RetTargetValue", G_TYPE_BOOLEAN, status,
69 gupnp_service_action_return (action);
74 get_status_cb (GUPnPService *service, GUPnPServiceAction *action, gpointer user_data)
76 gupnp_service_action_set (action,
77 "ResultStatus", G_TYPE_BOOLEAN, status,
79 gupnp_service_action_return (action);
83 * State Variable query handlers
88 query_target_cb (GUPnPService *service, char *variable, GValue *value, gpointer user_data)
90 g_value_init (value, G_TYPE_BOOLEAN);
91 g_value_set_boolean (value, status);
96 query_status_cb (GUPnPService *service, char *variable, GValue *value, gpointer user_data)
98 g_value_init (value, G_TYPE_BOOLEAN);
99 g_value_set_boolean (value, status);
104 main (int argc, char **argv)
106 GMainLoop *main_loop;
107 GError *error = NULL;
108 GUPnPContext *context;
109 GUPnPRootDevice *dev;
110 GUPnPServiceInfo *service;
114 /* By default the light is off */
116 g_print ("The light is now %s.\n", status ? "on" : "off");
118 /* Create the UPnP context */
119 context = gupnp_context_new (NULL, NULL, 0, &error);
121 g_printerr ("Error creating the GUPnP context: %s\n",
123 g_error_free (error);
128 /* Create root device */
129 dev = gupnp_root_device_new (context, "BinaryLight1.xml", ".");
130 gupnp_root_device_set_available (dev, TRUE);
132 /* Get the switch service from the root device */
133 service = gupnp_device_info_get_service
134 (GUPNP_DEVICE_INFO (dev), "urn:schemas-upnp-org:service:SwitchPower:1");
136 g_printerr ("Cannot get SwitchPower1 service\n");
141 /* Autoconnect the action and state variable handlers. This connects
142 query_target_cb and query_status_cb to the Target and Status state
143 variables query callbacks, and set_target_cb, get_target_cb and
144 get_status_cb to SetTarget, GetTarget and GetStatus actions
146 gupnp_service_signals_autoconnect (GUPNP_SERVICE (service), NULL, &error);
148 g_printerr ("Failed to autoconnect signals: %s\n", error->message);
149 g_error_free (error);
154 /* Run the main loop */
155 main_loop = g_main_loop_new (NULL, FALSE);
156 g_main_loop_run (main_loop);
159 g_main_loop_unref (main_loop);
160 g_object_unref (service);
161 g_object_unref (dev);
162 g_object_unref (context);