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>
15 static GMainLoop *main_loop;
24 service_proxy_available_cb (GUPnPControlPoint *cp, GUPnPServiceProxy *proxy)
30 /* We're toggling, so first fetch the current status */
31 if (!gupnp_service_proxy_send_action
32 (proxy, "GetStatus", &error,
34 /* OUT args */ "ResultStatus", G_TYPE_BOOLEAN, &target, NULL)) {
37 /* And then toggle it */
40 /* Mode is a boolean, so the target is the mode thanks to our well chosen
41 enumeration values. */
46 if (!gupnp_service_proxy_send_action (proxy, "SetTarget", &error,
48 "NewTargetValue", G_TYPE_BOOLEAN, target, NULL,
53 g_print ("Set switch to %s.\n", target ? "on" : "off");
57 /* Only manipulate the first light switch that is found */
58 g_main_loop_quit (main_loop);
62 g_printerr ("Cannot set switch: %s\n", error->message);
70 g_printerr ("$ light-client [on|off|toggle]\n");
74 main (int argc, char **argv)
77 GUPnPContext *context;
78 GUPnPControlPoint *cp;
83 /* Check and parse command line arguments */
89 if (g_str_equal (argv[1], "on")) {
91 } else if (g_str_equal (argv[1], "off")) {
93 } else if (g_str_equal (argv[1], "toggle")) {
100 /* Create the UPnP context */
101 context = gupnp_context_new (NULL, NULL, 0, &error);
103 g_printerr ("Error creating the GUPnP context: %s\n",
105 g_error_free (error);
110 /* Create the control point, searching for SwitchPower services */
111 cp = gupnp_control_point_new (context,
112 "urn:schemas-upnp-org:service:SwitchPower:1");
114 /* Connect to the service-found callback */
115 g_signal_connect (cp,
116 "service-proxy-available",
117 G_CALLBACK (service_proxy_available_cb),
120 /* Start searching when the main loop runs */
121 gssdp_resource_browser_set_active (GSSDP_RESOURCE_BROWSER (cp), TRUE);
123 /* Run the main loop */
124 main_loop = g_main_loop_new (NULL, FALSE);
125 g_main_loop_run (main_loop);
128 g_main_loop_unref (main_loop);
130 g_object_unref (context);