Update gupnp to 0.20.3 (161969f)
[profile/ivi/GUPnP.git] / examples / light-server.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 #include <gmodule.h>
15
16 static gboolean status;
17
18 G_BEGIN_DECLS
19 G_MODULE_EXPORT void set_target_cb (GUPnPService *service,
20                                     GUPnPServiceAction *action,
21                                     gpointer user_data);
22 G_MODULE_EXPORT void get_target_cb (GUPnPService *service,
23                                     GUPnPServiceAction *action,
24                                     gpointer user_data);
25 G_MODULE_EXPORT void get_status_cb (GUPnPService *service,
26                                     GUPnPServiceAction *action,
27                                     gpointer user_data);
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);
32 G_END_DECLS
33 /*
34  * Action handlers
35  */
36
37 /* SetTarget */
38 G_MODULE_EXPORT void
39 set_target_cb (GUPnPService          *service,
40                GUPnPServiceAction    *action,
41                G_GNUC_UNUSED gpointer user_data)
42 {
43   gboolean target;
44
45   /* Get the new target value */
46   gupnp_service_action_get (action,
47                             "NewTargetValue", G_TYPE_BOOLEAN, &target,
48                             NULL);
49
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) {
53     status = target;
54     gupnp_service_notify (service,
55                           "Status", G_TYPE_BOOLEAN, status,
56                           NULL);
57
58     g_print ("The light is now %s.\n", status ? "on" : "off");
59   }
60
61   /* Return success to the client */
62   gupnp_service_action_return (action);
63 }
64
65 /* GetTarget */
66 G_MODULE_EXPORT void
67 get_target_cb (G_GNUC_UNUSED GUPnPService *service,
68                GUPnPServiceAction         *action,
69                G_GNUC_UNUSED gpointer      user_data)
70 {
71   gupnp_service_action_set (action,
72                             "RetTargetValue", G_TYPE_BOOLEAN, status,
73                             NULL);
74   gupnp_service_action_return (action);
75 }
76
77 /* GetStatus */
78 G_MODULE_EXPORT void
79 get_status_cb (G_GNUC_UNUSED GUPnPService *service,
80                GUPnPServiceAction         *action,
81                G_GNUC_UNUSED gpointer      user_data)
82 {
83   gupnp_service_action_set (action,
84                             "ResultStatus", G_TYPE_BOOLEAN, status,
85                             NULL);
86   gupnp_service_action_return (action);
87 }
88
89 /*
90  * State Variable query handlers
91  */
92
93 /* Target */
94 G_MODULE_EXPORT void
95 query_target_cb (G_GNUC_UNUSED GUPnPService *service,
96                  G_GNUC_UNUSED char         *variable,
97                  GValue                     *value,
98                  G_GNUC_UNUSED gpointer      user_data)
99 {
100   g_value_init (value, G_TYPE_BOOLEAN);
101   g_value_set_boolean (value, status);
102 }
103
104 /* Status */
105 G_MODULE_EXPORT void
106 query_status_cb (G_GNUC_UNUSED GUPnPService *service,
107                  G_GNUC_UNUSED char         *variable,
108                  GValue                     *value,
109                  G_GNUC_UNUSED gpointer      user_data)
110 {
111   g_value_init (value, G_TYPE_BOOLEAN);
112   g_value_set_boolean (value, status);
113 }
114
115
116 int
117 main (G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv)
118 {
119   GMainLoop *main_loop;
120   GError *error = NULL;
121   GUPnPContext *context;
122   GUPnPRootDevice *dev;
123   GUPnPServiceInfo *service;
124   
125 #if !GLIB_CHECK_VERSION(2,35,0)
126   g_type_init ();
127 #endif
128
129   /* By default the light is off */
130   status = FALSE;
131   g_print ("The light is now %s.\n", status ? "on" : "off");
132
133   /* Create the UPnP context */
134   context = gupnp_context_new (NULL, NULL, 0, &error);
135   if (error) {
136     g_printerr ("Error creating the GUPnP context: %s\n",
137                 error->message);
138     g_error_free (error);
139
140     return EXIT_FAILURE;
141   }
142
143   /* Create root device */
144   dev = gupnp_root_device_new (context, "BinaryLight1.xml", ".");
145   gupnp_root_device_set_available (dev, TRUE);
146
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");
150   if (!service) {
151     g_printerr ("Cannot get SwitchPower1 service\n");
152
153     return EXIT_FAILURE;
154   }
155   
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
160      respectively. */
161   gupnp_service_signals_autoconnect (GUPNP_SERVICE (service), NULL, &error);
162   if (error) {
163     g_printerr ("Failed to autoconnect signals: %s\n", error->message);
164     g_error_free (error);
165
166     return EXIT_FAILURE;
167   }
168   
169   /* Run the main loop */
170   main_loop = g_main_loop_new (NULL, FALSE);
171   g_main_loop_run (main_loop);
172
173   /* Cleanup */
174   g_main_loop_unref (main_loop);
175   g_object_unref (service);
176   g_object_unref (dev);
177   g_object_unref (context);
178   
179   return EXIT_SUCCESS;
180 }