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;
112 g_thread_init (NULL);
115 /* By default the light is off */
117 g_print ("The light is now %s.\n", status ? "on" : "off");
119 /* Create the UPnP context */
120 context = gupnp_context_new (NULL, NULL, 0, &error);
122 g_printerr ("Error creating the GUPnP context: %s\n",
124 g_error_free (error);
129 /* Create root device */
130 dev = gupnp_root_device_new (context, "BinaryLight1.xml", ".");
131 gupnp_root_device_set_available (dev, TRUE);
133 /* Get the switch service from the root device */
134 service = gupnp_device_info_get_service
135 (GUPNP_DEVICE_INFO (dev), "urn:schemas-upnp-org:service:SwitchPower:1");
137 g_printerr ("Cannot get SwitchPower1 service\n");
142 /* Autoconnect the action and state variable handlers. This connects
143 query_target_cb and query_status_cb to the Target and Status state
144 variables query callbacks, and set_target_cb, get_target_cb and
145 get_status_cb to SetTarget, GetTarget and GetStatus actions
147 gupnp_service_signals_autoconnect (GUPNP_SERVICE (service), NULL, &error);
149 g_printerr ("Failed to autoconnect signals: %s\n", error->message);
150 g_error_free (error);
155 /* Run the main loop */
156 main_loop = g_main_loop_new (NULL, FALSE);
157 g_main_loop_run (main_loop);
160 g_main_loop_unref (main_loop);
161 g_object_unref (service);
162 g_object_unref (dev);
163 g_object_unref (context);