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., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
24 #include <libgupnp/gupnp-control-point.h>
25 #include <libgupnp/gupnp-service-introspection.h>
33 static gboolean async = FALSE;
34 static GOptionEntry entries[] =
36 { "async", 'a', 0, G_OPTION_ARG_NONE, &async,
37 "Asynchronously create intropection object", NULL },
42 interrupt_signal_handler (G_GNUC_UNUSED int signum)
44 g_main_loop_quit (main_loop);
48 print_action_arguments (GList *argument_list)
52 g_print ("\targuments:\n");
53 for (iter = argument_list; iter; iter = iter->next) {
54 GUPnPServiceActionArgInfo *argument;
56 argument = (GUPnPServiceActionArgInfo *) iter->data;
58 g_print ("\t\tname: %s\n"
60 "\t\trelated state variable: %s\n\n",
62 (argument->direction ==
63 GUPNP_SERVICE_ACTION_ARG_DIRECTION_IN)? "in": "out",
64 argument->related_state_variable);
69 print_actions (GUPnPServiceIntrospection *introspection)
71 const GList *action_list;
73 action_list = gupnp_service_introspection_list_actions (introspection);
77 g_print ("actions:\n");
78 for (iter = action_list; iter; iter = iter->next) {
79 GUPnPServiceActionInfo *action_info;
81 action_info = (GUPnPServiceActionInfo *) iter->data;
83 g_print ("\tname: %s\n", action_info->name);
84 print_action_arguments (action_info->arguments);
91 print_state_variables (GUPnPServiceIntrospection *introspection)
93 const GList *variables;
96 gupnp_service_introspection_list_state_variables (
101 g_print ("state variables:\n");
102 for (iter = variables; iter; iter = iter->next) {
103 GUPnPServiceStateVariableInfo *variable;
104 GValue default_value;
105 const char * default_value_str;
107 variable = (GUPnPServiceStateVariableInfo *) iter->data;
109 g_print ("\tname: %s\n"
111 "\tsend events: %s\n",
113 g_type_name (variable->type),
114 variable->send_events? "yes": "no");
116 memset (&default_value, 0, sizeof (GValue));
117 g_value_init (&default_value, G_TYPE_STRING);
118 g_value_transform (&variable->default_value,
120 default_value_str = g_value_get_string (&default_value);
121 if (default_value_str) {
122 g_print ("\tdefault value: %s\n",
125 g_value_unset (&default_value);
127 if (variable->is_numeric) {
128 GValue min, max, step;
130 memset (&min, 0, sizeof (GValue));
131 memset (&max, 0, sizeof (GValue));
132 memset (&step, 0, sizeof (GValue));
134 g_value_init (&min, G_TYPE_STRING);
135 g_value_init (&max, G_TYPE_STRING);
136 g_value_init (&step, G_TYPE_STRING);
138 g_value_transform (&variable->minimum, &min);
139 g_value_transform (&variable->maximum, &max);
140 g_value_transform (&variable->step, &step);
142 g_print ("\tminimum: %s\n"
145 g_value_get_string (&min),
146 g_value_get_string (&max),
147 g_value_get_string (&step));
149 g_value_unset (&min);
150 g_value_unset (&max);
151 g_value_unset (&step);
154 if (variable->allowed_values) {
157 g_print ("\tallowed values: ");
158 for (value_iter = variable->allowed_values;
160 value_iter = value_iter->next) {
162 (gchar *) value_iter->data);
173 got_introspection (GUPnPServiceInfo *info,
174 GUPnPServiceIntrospection *introspection,
176 G_GNUC_UNUSED gpointer user_data)
179 g_warning ("Failed to create introspection for '%s': %s",
180 gupnp_service_info_get_udn (info),
186 g_print ("service: %s\nlocation: %s\n",
187 gupnp_service_info_get_udn (info),
188 gupnp_service_info_get_location (info));
189 print_actions (introspection);
190 print_state_variables (introspection);
191 g_object_unref (introspection);
195 service_proxy_available_cb (G_GNUC_UNUSED GUPnPControlPoint *cp,
196 GUPnPServiceProxy *proxy)
198 GUPnPServiceInfo *info;
199 GUPnPServiceIntrospection *introspection;
200 GError *error = NULL;
202 info = GUPNP_SERVICE_INFO (proxy);
205 gupnp_service_info_get_introspection_async (info,
210 gupnp_service_info_get_introspection (info, &error);
211 got_introspection (info, introspection, error, NULL);
214 g_error_free (error);
219 service_proxy_unavailable_cb (G_GNUC_UNUSED GUPnPControlPoint *cp,
220 GUPnPServiceProxy *proxy)
222 const char *type, *location;
224 type = gupnp_service_info_get_service_type (GUPNP_SERVICE_INFO (proxy));
225 location = gupnp_service_info_get_location (GUPNP_SERVICE_INFO (proxy));
227 g_print ("Service unavailable:\n");
228 g_print ("\ttype: %s\n", type);
229 g_print ("\tlocation: %s\n", location);
233 main (int argc, char **argv)
235 GError *error = NULL;
236 GUPnPContext *context;
237 GUPnPControlPoint *cp;
238 GOptionContext *option_context;
240 struct sigaction sig_action;
241 #endif /* G_OS_WIN32 */
243 option_context = g_option_context_new ("- test GUPnP introspection");
244 g_option_context_add_main_entries (option_context,
247 g_option_context_parse (option_context,
252 g_printerr ("Error parsing the commandline arguments: %s\n",
254 g_error_free (error);
259 #if !GLIB_CHECK_VERSION(2,35,0)
264 context = g_initable_new (GUPNP_TYPE_CONTEXT, NULL, &error, NULL);
266 g_printerr ("Error creating the GUPnP context: %s\n",
268 g_error_free (error);
273 /* We're interested in everything */
274 cp = gupnp_control_point_new (context, "ssdp:all");
276 g_signal_connect (cp,
277 "service-proxy-available",
278 G_CALLBACK (service_proxy_available_cb),
280 g_signal_connect (cp,
281 "service-proxy-unavailable",
282 G_CALLBACK (service_proxy_unavailable_cb),
285 gssdp_resource_browser_set_active (GSSDP_RESOURCE_BROWSER (cp), TRUE);
287 main_loop = g_main_loop_new (NULL, FALSE);
289 /* Hook the handler for SIGTERM */
291 memset (&sig_action, 0, sizeof (sig_action));
292 sig_action.sa_handler = interrupt_signal_handler;
293 sigaction (SIGINT, &sig_action, NULL);
295 signal(SIGINT,interrupt_signal_handler);
296 #endif /* G_OS_WIN32 */
298 g_main_loop_run (main_loop);
299 g_main_loop_unref (main_loop);
302 g_object_unref (context);