tizen 2.3.1 release
[external/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   g_thread_init (NULL);
113   g_type_init ();
114
115   /* By default the light is off */
116   status = FALSE;
117   g_print ("The light is now %s.\n", status ? "on" : "off");
118
119   /* Create the UPnP context */
120   context = gupnp_context_new (NULL, NULL, 0, &error);
121   if (error) {
122     g_printerr ("Error creating the GUPnP context: %s\n",
123                 error->message);
124     g_error_free (error);
125
126     return EXIT_FAILURE;
127   }
128
129   /* Create root device */
130   dev = gupnp_root_device_new (context, "BinaryLight1.xml", ".");
131   gupnp_root_device_set_available (dev, TRUE);
132
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");
136   if (!service) {
137     g_printerr ("Cannot get SwitchPower1 service\n");
138
139     return EXIT_FAILURE;
140   }
141   
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
146      respectively. */
147   gupnp_service_signals_autoconnect (GUPNP_SERVICE (service), NULL, &error);
148   if (error) {
149     g_printerr ("Failed to autoconnect signals: %s\n", error->message);
150     g_error_free (error);
151
152     return EXIT_FAILURE;
153   }
154   
155   /* Run the main loop */
156   main_loop = g_main_loop_new (NULL, FALSE);
157   g_main_loop_run (main_loop);
158
159   /* Cleanup */
160   g_main_loop_unref (main_loop);
161   g_object_unref (service);
162   g_object_unref (dev);
163   g_object_unref (context);
164   
165   return EXIT_SUCCESS;
166 }