2 * Copyright (C) 2007 Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
3 * Copyright (C) 2007 OpenedHand Ltd.
5 * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
6 * Jorn Baayen <jorn@openedhand.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
24 #include <libgupnp/gupnp-control-point.h>
25 #include <libgupnp/gupnp-service-introspection.h>
32 static gboolean async = FALSE;
33 static GOptionEntry entries[] =
35 { "async", 'a', 0, G_OPTION_ARG_NONE, &async,
36 "Asynchronously create intropection object", NULL },
41 interrupt_signal_handler (int signum)
43 g_main_loop_quit (main_loop);
47 print_action_arguments (GList *argument_list)
51 g_print ("\targuments:\n");
52 for (iter = argument_list; iter; iter = iter->next) {
53 GUPnPServiceActionArgInfo *argument;
55 argument = (GUPnPServiceActionArgInfo *) iter->data;
57 g_print ("\t\tname: %s\n"
59 "\t\trelated state variable: %s\n\n",
61 (argument->direction ==
62 GUPNP_SERVICE_ACTION_ARG_DIRECTION_IN)? "in": "out",
63 argument->related_state_variable);
68 print_actions (GUPnPServiceIntrospection *introspection)
70 const GList *action_list;
72 action_list = gupnp_service_introspection_list_actions (introspection);
76 g_print ("actions:\n");
77 for (iter = action_list; iter; iter = iter->next) {
78 GUPnPServiceActionInfo *action_info;
80 action_info = (GUPnPServiceActionInfo *) iter->data;
82 g_print ("\tname: %s\n", action_info->name);
83 print_action_arguments (action_info->arguments);
90 print_state_variables (GUPnPServiceIntrospection *introspection)
92 const GList *variables;
95 gupnp_service_introspection_list_state_variables (
100 g_print ("state variables:\n");
101 for (iter = variables; iter; iter = iter->next) {
102 GUPnPServiceStateVariableInfo *variable;
103 GValue default_value;
104 const char * default_value_str;
106 variable = (GUPnPServiceStateVariableInfo *) iter->data;
108 g_print ("\tname: %s\n"
110 "\tsend events: %s\n",
112 g_type_name (variable->type),
113 variable->send_events? "yes": "no");
115 memset (&default_value, 0, sizeof (GValue));
116 g_value_init (&default_value, G_TYPE_STRING);
117 g_value_transform (&variable->default_value,
119 default_value_str = g_value_get_string (&default_value);
120 if (default_value_str) {
121 g_print ("\tdefault value: %s\n",
124 g_value_unset (&default_value);
126 if (variable->is_numeric) {
127 GValue min, max, step;
129 memset (&min, 0, sizeof (GValue));
130 memset (&max, 0, sizeof (GValue));
131 memset (&step, 0, sizeof (GValue));
133 g_value_init (&min, G_TYPE_STRING);
134 g_value_init (&max, G_TYPE_STRING);
135 g_value_init (&step, G_TYPE_STRING);
137 g_value_transform (&variable->minimum, &min);
138 g_value_transform (&variable->maximum, &max);
139 g_value_transform (&variable->step, &step);
141 g_print ("\tminimum: %s\n"
144 g_value_get_string (&min),
145 g_value_get_string (&max),
146 g_value_get_string (&step));
148 g_value_unset (&min);
149 g_value_unset (&max);
150 g_value_unset (&step);
153 if (variable->allowed_values) {
156 g_print ("\tallowed values: ");
157 for (value_iter = variable->allowed_values;
159 value_iter = value_iter->next) {
161 (gchar *) value_iter->data);
172 got_introspection (GUPnPServiceInfo *info,
173 GUPnPServiceIntrospection *introspection,
178 g_warning ("Failed to create introspection for '%s': %s",
179 gupnp_service_info_get_udn (info),
185 g_print ("service: %s\nlocation: %s\n",
186 gupnp_service_info_get_udn (info),
187 gupnp_service_info_get_location (info));
188 print_actions (introspection);
189 print_state_variables (introspection);
190 g_object_unref (introspection);
194 service_proxy_available_cb (GUPnPControlPoint *cp,
195 GUPnPServiceProxy *proxy)
197 GUPnPServiceInfo *info;
198 GUPnPServiceIntrospection *introspection;
199 GError *error = NULL;
201 info = GUPNP_SERVICE_INFO (proxy);
204 gupnp_service_info_get_introspection_async (info,
209 gupnp_service_info_get_introspection (info, &error);
210 got_introspection (info, introspection, error, NULL);
213 g_error_free (error);
218 service_proxy_unavailable_cb (GUPnPControlPoint *cp,
219 GUPnPServiceProxy *proxy)
221 const char *type, *location;
223 type = gupnp_service_info_get_service_type (GUPNP_SERVICE_INFO (proxy));
224 location = gupnp_service_info_get_location (GUPNP_SERVICE_INFO (proxy));
226 g_print ("Service unavailable:\n");
227 g_print ("\ttype: %s\n", type);
228 g_print ("\tlocation: %s\n", location);
232 main (int argc, char **argv)
234 GError *error = NULL;
235 GUPnPContext *context;
236 GUPnPControlPoint *cp;
237 GOptionContext *option_context;
238 struct sigaction sig_action;
240 option_context = g_option_context_new ("- test GUPnP introspection");
241 g_option_context_add_main_entries (option_context,
244 g_option_context_parse (option_context,
249 g_printerr ("Error parsing the commandline arguments: %s\n",
251 g_error_free (error);
256 g_thread_init (NULL);
260 context = gupnp_context_new (NULL, NULL, 0, &error);
262 g_printerr ("Error creating the GUPnP context: %s\n",
264 g_error_free (error);
269 /* We're interested in everything */
270 cp = gupnp_control_point_new (context, "ssdp:all");
272 g_signal_connect (cp,
273 "service-proxy-available",
274 G_CALLBACK (service_proxy_available_cb),
276 g_signal_connect (cp,
277 "service-proxy-unavailable",
278 G_CALLBACK (service_proxy_unavailable_cb),
281 gssdp_resource_browser_set_active (GSSDP_RESOURCE_BROWSER (cp), TRUE);
283 main_loop = g_main_loop_new (NULL, FALSE);
285 /* Hook the handler for SIGTERM */
286 memset (&sig_action, 0, sizeof (sig_action));
287 sig_action.sa_handler = interrupt_signal_handler;
288 sigaction (SIGINT, &sig_action, NULL);
290 g_main_loop_run (main_loop);
291 g_main_loop_unref (main_loop);
294 g_object_unref (context);