2944c2aaf010ab6fbfb1341a3f3a759d53759441
[profile/ivi/GUPnP.git] / examples / light-client.c
1 /*
2  * Example UPnP device/service, implementing the BinaryLight device and
3  * SwitchPower services to emulate a light switch.
4  *
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.
8  *
9  * This example code is in the public domain.
10  */
11
12 #include <libgupnp/gupnp.h>
13 #include <stdlib.h>
14
15 static GMainLoop *main_loop;
16
17 static enum {
18   OFF = 0,
19   ON = 1,
20   TOGGLE
21 } mode;
22
23 static void
24 service_proxy_available_cb (GUPnPControlPoint *cp, GUPnPServiceProxy *proxy)
25 {
26   GError *error = NULL;
27   gboolean target;
28
29   if (mode == TOGGLE) {
30     /* We're toggling, so first fetch the current status */
31     if (!gupnp_service_proxy_send_action
32         (proxy, "GetStatus", &error,
33          /* IN args */ NULL,
34          /* OUT args */ "ResultStatus", G_TYPE_BOOLEAN, &target, NULL)) {
35       goto error;
36     }
37     /* And then toggle it */
38     target = ! target;
39   } else {
40     /* Mode is a boolean, so the target is the mode thanks to our well chosen
41        enumeration values. */
42     target = mode;
43   }
44
45   /* Set the target */
46   if (!gupnp_service_proxy_send_action (proxy, "SetTarget", &error,
47                                         /* IN args */
48                                         "NewTargetValue", G_TYPE_BOOLEAN, target, NULL,
49                                         /* OUT args */
50                                         NULL)) {
51     goto error;
52   } else {
53     g_print ("Set switch to %s.\n", target ? "on" : "off");
54   }
55   
56  done:
57   /* Only manipulate the first light switch that is found */
58   g_main_loop_quit (main_loop);
59   return;
60
61  error:
62   g_printerr ("Cannot set switch: %s\n", error->message);
63   g_error_free (error);
64   goto done;
65 }
66
67 static void
68 usage (void)
69 {
70     g_printerr ("$ light-client [on|off|toggle]\n");
71 }
72
73 int
74 main (int argc, char **argv)
75 {
76   GError *error = NULL;
77   GUPnPContext *context;
78   GUPnPControlPoint *cp;
79
80   g_type_init ();
81
82   /* Check and parse command line arguments */
83   if (argc != 2) {
84     usage ();
85     return EXIT_FAILURE;
86   }
87   
88   if (g_str_equal (argv[1], "on")) {
89     mode = ON;
90   } else if (g_str_equal (argv[1], "off")) {
91     mode = OFF;
92   } else if (g_str_equal (argv[1], "toggle")) {
93     mode = TOGGLE;
94   } else {
95     usage ();
96     return EXIT_FAILURE;
97   }
98
99   /* Create the UPnP context */
100   context = gupnp_context_new (NULL, NULL, 0, &error);
101   if (error) {
102     g_printerr ("Error creating the GUPnP context: %s\n",
103                 error->message);
104     g_error_free (error);
105
106     return EXIT_FAILURE;
107   }
108
109   /* Create the control point, searching for SwitchPower services */
110   cp = gupnp_control_point_new (context,
111                                 "urn:schemas-upnp-org:service:SwitchPower:1");
112   
113   /* Connect to the service-found callback */
114   g_signal_connect (cp,
115                     "service-proxy-available",
116                     G_CALLBACK (service_proxy_available_cb),
117                     NULL);
118   
119   /* Start searching when the main loop runs */
120   gssdp_resource_browser_set_active (GSSDP_RESOURCE_BROWSER (cp), TRUE);
121
122   /* Run the main loop */
123   main_loop = g_main_loop_new (NULL, FALSE);
124   g_main_loop_run (main_loop);
125
126   /* Cleanup */
127   g_main_loop_unref (main_loop);
128   g_object_unref (cp);
129   g_object_unref (context);
130   
131   return EXIT_SUCCESS;
132 }