Submit version 0.20.1 of GUPnP (4186015)
[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_MODULE_EXPORT void set_target_cb (GUPnPService *service,
19                                     GUPnPServiceAction *action,
20                                     gpointer user_data);
21 G_MODULE_EXPORT void get_target_cb (GUPnPService *service,
22                                     GUPnPServiceAction *action,
23                                     gpointer user_data);
24 G_MODULE_EXPORT void get_status_cb (GUPnPService *service,
25                                     GUPnPServiceAction *action,
26                                     gpointer user_data);
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);
31
32 /*
33  * Action handlers
34  */
35
36 /* SetTarget */
37 G_MODULE_EXPORT void
38 set_target_cb (GUPnPService *service, GUPnPServiceAction *action, gpointer user_data)
39 {
40   gboolean target;
41
42   /* Get the new target value */
43   gupnp_service_action_get (action,
44                             "NewTargetValue", G_TYPE_BOOLEAN, &target,
45                             NULL);
46
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) {
50     status = target;
51     gupnp_service_notify (service,
52                           "Status", G_TYPE_BOOLEAN, status,
53                           NULL);
54
55     g_print ("The light is now %s.\n", status ? "on" : "off");
56   }
57
58   /* Return success to the client */
59   gupnp_service_action_return (action);
60 }
61
62 /* GetTarget */
63 G_MODULE_EXPORT void
64 get_target_cb (GUPnPService *service, GUPnPServiceAction *action, gpointer user_data)
65 {
66   gupnp_service_action_set (action,
67                             "RetTargetValue", G_TYPE_BOOLEAN, status,
68                             NULL);
69   gupnp_service_action_return (action);
70 }
71
72 /* GetStatus */
73 G_MODULE_EXPORT void
74 get_status_cb (GUPnPService *service, GUPnPServiceAction *action, gpointer user_data)
75 {
76   gupnp_service_action_set (action,
77                             "ResultStatus", G_TYPE_BOOLEAN, status,
78                             NULL);
79   gupnp_service_action_return (action);
80 }
81
82 /*
83  * State Variable query handlers
84  */
85
86 /* Target */
87 G_MODULE_EXPORT void
88 query_target_cb (GUPnPService *service, char *variable, GValue *value, gpointer user_data)
89 {
90   g_value_init (value, G_TYPE_BOOLEAN);
91   g_value_set_boolean (value, status);
92 }
93
94 /* Status */
95 G_MODULE_EXPORT void
96 query_status_cb (GUPnPService *service, char *variable, GValue *value, gpointer user_data)
97 {
98   g_value_init (value, G_TYPE_BOOLEAN);
99   g_value_set_boolean (value, status);
100 }
101
102
103 int
104 main (int argc, char **argv)
105 {
106   GMainLoop *main_loop;
107   GError *error = NULL;
108   GUPnPContext *context;
109   GUPnPRootDevice *dev;
110   GUPnPServiceInfo *service;
111   
112 #if !GLIB_CHECK_VERSION(2,35,0)
113   g_type_init ();
114 #endif
115
116   /* By default the light is off */
117   status = FALSE;
118   g_print ("The light is now %s.\n", status ? "on" : "off");
119
120   /* Create the UPnP context */
121   context = gupnp_context_new (NULL, NULL, 0, &error);
122   if (error) {
123     g_printerr ("Error creating the GUPnP context: %s\n",
124                 error->message);
125     g_error_free (error);
126
127     return EXIT_FAILURE;
128   }
129
130   /* Create root device */
131   dev = gupnp_root_device_new (context, "BinaryLight1.xml", ".");
132   gupnp_root_device_set_available (dev, TRUE);
133
134   /* Get the switch service from the root device */
135   service = gupnp_device_info_get_service
136     (GUPNP_DEVICE_INFO (dev), "urn:schemas-upnp-org:service:SwitchPower:1");
137   if (!service) {
138     g_printerr ("Cannot get SwitchPower1 service\n");
139
140     return EXIT_FAILURE;
141   }
142   
143   /* Autoconnect the action and state variable handlers.  This connects
144      query_target_cb and query_status_cb to the Target and Status state
145      variables query callbacks, and set_target_cb, get_target_cb and
146      get_status_cb to SetTarget, GetTarget and GetStatus actions
147      respectively. */
148   gupnp_service_signals_autoconnect (GUPNP_SERVICE (service), NULL, &error);
149   if (error) {
150     g_printerr ("Failed to autoconnect signals: %s\n", error->message);
151     g_error_free (error);
152
153     return EXIT_FAILURE;
154   }
155   
156   /* Run the main loop */
157   main_loop = g_main_loop_new (NULL, FALSE);
158   g_main_loop_run (main_loop);
159
160   /* Cleanup */
161   g_main_loop_unref (main_loop);
162   g_object_unref (service);
163   g_object_unref (dev);
164   g_object_unref (context);
165   
166   return EXIT_SUCCESS;
167 }