Debian Codegraph @

libgupnp/gupnp-service-proxy-action.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2006, 2007, 2008 OpenedHand Ltd.
3 *
4 * Author: Jorn Baayen <[email protected]>
5 *
6 * SPDX-License-Identifier: LGPL-2.1-or-later
7 *
8 */
9
10 #include <config.h>
11
12 #include <libxml/parser.h>
13
14 #include "gupnp-error.h"
15 #include "gupnp-service-proxy.h"
16 #include "gvalue-util.h"
17 #include "xml-util.h"
18
19 #include "gupnp-error-private.h"
20 #include "gupnp-service-info-private.h"
21 #include "gupnp-service-proxy-action-private.h"
22
23 struct _GUPnPServiceProxyActionIter {
24 GObject parent;
25
26 GUPnPServiceProxyAction *action;
27 xmlNode *current;
28 GUPnPServiceIntrospection *introspection;
29 gboolean iterating;
30 };
31
32 G_DEFINE_TYPE (GUPnPServiceProxyActionIter,
33 gupnp_service_proxy_action_iter,
34 G_TYPE_OBJECT)
35
36 static void
37 gupnp_service_proxy_action_iter_init (GUPnPServiceProxyActionIter *self)
38 {
39 }
40
41 static void
42 gupnp_service_proxy_action_iter_dispose (GObject *object)
43 {
44 GUPnPServiceProxyActionIter *self =
45 GUPNP_SERVICE_PROXY_ACTION_ITER (object);
46 g_clear_pointer (&self->action, gupnp_service_proxy_action_unref);
47 g_clear_object (&self->introspection);
48
49 G_OBJECT_CLASS (gupnp_service_proxy_action_iter_parent_class)
50 ->dispose (object);
51 }
52
53 static void
54 gupnp_service_proxy_action_iter_class_init (
55 GUPnPServiceProxyActionIterClass *klass)
56 {
57 GObjectClass *object_class = G_OBJECT_CLASS (klass);
58 object_class->dispose = gupnp_service_proxy_action_iter_dispose;
59 }
60
61 /**
62 * gupnp_service_proxy_action_iterate:
63 * @action: A finished GUPnPServiceProxyAction
64 * @error: (out)(nullable): Return location for an optional error
65 *
66 * Iterate over the out arguments of a finished action
67 *
68 * Returns: (transfer full)(nullable): A newly created GUPnPServiceProxyActionIterator, or %NULL on error
69 * Since: 1.6.6
70 */
71 GUPnPServiceProxyActionIter *
72 gupnp_service_proxy_action_iterate (GUPnPServiceProxyAction *action,
73 GError **error)
74 {
75 g_return_val_if_fail (action != NULL, NULL);
76 g_return_val_if_fail (!action->pending, NULL);
77
78 /* Check for saved error from begin_action() */
79 if (action->error) {
80 g_propagate_error (error, g_error_copy (action->error));
81
82 return NULL;
83 }
84
85 /* Check response for errors and do initial parsing */
86 gupnp_service_proxy_action_check_response (action);
87 if (action->error != NULL) {
88 g_propagate_error (error, g_error_copy (action->error));
89
90 return NULL;
91 }
92
93 g_type_ensure (gupnp_service_proxy_action_iter_get_type ());
94
95 GUPnPServiceProxyActionIter *iter =
96 g_object_new (gupnp_service_proxy_action_iter_get_type (),
97 NULL);
98 iter->action = gupnp_service_proxy_action_ref (action);
99
100 if (action->proxy != NULL) {
101 iter->introspection = gupnp_service_info_get_introspection (
102 GUPNP_SERVICE_INFO (action->proxy));
103 if (iter->introspection != NULL) {
104 g_object_ref (iter->introspection);
105 }
106 }
107
108 return iter;
109 }
110
111 /**
112 * gupnp_service_proxy_action_iter_next:
113 * @self: A GUPnP.ServiceProxyActionIter
114 *
115 * Move @self to the next out value of the iterated action
116 *
117 * Returns: %TRUE if the next value was available
118 * Since: 1.6.6
119 */
120 gboolean
121 gupnp_service_proxy_action_iter_next (GUPnPServiceProxyActionIter *self)
122 {
123 if (self->iterating) {
124 self->current = self->current->next;
125 } else {
126 self->current = self->action->params->children;
127 self->iterating = TRUE;
128 }
129
130 return self->current != NULL;
131 }
132
133 /**
134 * gupnp_service_proxy_action_iter_get_name:
135 * @self: A GUPnP.ServiceProxyActionIter
136 *
137 * Get the name of the current out argument
138 *
139 * Returns: Name of the current argument
140 * Since: 1.6.6
141 */
142 const char *
143 gupnp_service_proxy_action_iter_get_name (GUPnPServiceProxyActionIter *self)
144 {
145 g_return_val_if_fail (self->current != NULL, NULL);
146
147 return (const char *) self->current->name;
148 }
149
150 static int
151 find_argument (gconstpointer a, gconstpointer b)
152 {
153 const GUPnPServiceActionArgInfo *arg = a;
154 const char *needle = b;
155
156 return g_strcmp0 (arg->name, needle);
157 }
158
159 /**
160 * gupnp_service_proxy_action_iter_get_value:
161 * @self: A GUPnP.ServiceProxyActionIter
162 * @value: (out): The value
163 *
164 * Get the value of the current parameter.
165 *
166 * If the service proxy had a successful introspection, the type according
167 * to the introspection data will be used, otherwise it will be string.
168 *
169 * Returns: %TRUE if the value could be read successfully
170 * Since: 1.6.6
171 */
172
173 gboolean
174 gupnp_service_proxy_action_iter_get_value (GUPnPServiceProxyActionIter *self,
175 GValue *value)
176 {
177 if (self->introspection != NULL) {
178 const GUPnPServiceActionInfo *action_info =
179 gupnp_service_introspection_get_action (
180 self->introspection,
181 self->action->name);
182 GList *result = g_list_find_custom (action_info->arguments,
183 self->current->name,
184 find_argument);
185 if (result == NULL) {
186 g_debug ("No argument %s\n", self->current->name);
187 return FALSE;
188 }
189
190 const GUPnPServiceActionArgInfo *arg = result->data;
191 const char *var = arg->related_state_variable;
192 const GUPnPServiceStateVariableInfo *info =
193 gupnp_service_introspection_get_state_variable (
194 self->introspection,
195 var);
196
197 if (info == NULL) {
198 g_debug ("No state variable for %s\n",
199 self->current->name);
200 return FALSE;
201 }
202
203 g_value_init (value, info->type);
204 } else {
205 // We know nothing about the type, so just give out the string and let
206 // the user transform the value
207 g_value_init (value, G_TYPE_STRING);
208 }
209
210 gvalue_util_set_value_from_xml_node (value, self->current);
211 return TRUE;
212 }
213
214 /**
215 * gupnp_service_proxy_action_iter_get_value_as:
216 * @self: A GUPnP.ServiceProxyActionIter
217 * @type: The type to convert the value to
218 * @value: (out): The value
219 *
220 * Get the value of the current parameter.
221 *
222 * Converts the value to the given type, similar to the other
223 * finish_action functions.
224 *
225 * Returns: %TRUE if the value could be read successfully
226 * Since: 1.6.8
227 */
228
229 gboolean
230 gupnp_service_proxy_action_iter_get_value_as (GUPnPServiceProxyActionIter *self,
231 GType type,
232 GValue *value)
233 {
234 g_value_init (value, type);
235
236 gvalue_util_set_value_from_xml_node (value, self->current);
237 return TRUE;
238 }
239
240 struct _ActionArgument {
241 char *name;
242 GValue value;
243 };
244 typedef struct _ActionArgument ActionArgument;
245
246 void
247 action_argument_free (ActionArgument* arg)
248 {
249 g_free (arg->name);
250 g_value_unset (&arg->value);
251 g_free (arg);
252 }
253
254 /* Reads a value into the parameter name and initialised GValue pair
255 * from @response */
256 static void
257 read_out_parameter (const char *arg_name,
258 GValue *value,
259 xmlNode *params)
260 {
261 xmlNode *param;
262
263 /* Try to find a matching parameter in the response*/
264 param = xml_util_get_element (params,
265 arg_name,
266 NULL);
267 if (!param) {
268 g_warning ("Could not find variable \"%s\" in response",
269 arg_name);
270
271 return;
272 }
273
274 gvalue_util_set_value_from_xml_node (value, param);
275 }
276
277 /**
278 * gupnp_service_proxy_action_new_plain:
279 * @action: The name of a remote action to call
280 *
281 * Prepares action @action with to be sent off to
282 * a remote service later with gupnp_service_proxy_call_action() or
283 * gupnp_service_proxy_call_action_async() if no arguments required or by adding more
284 * parameters with gupnp_service_proxy_action_add()
285 *
286 * After the action call has finished, the results of the call may be
287 * retrived from the #GUPnPServiceProxyAction by using
288 * gupnp_service_proxy_action_get_result(),
289 * gupnp_service_proxy_action_get_result_list() or
290 * gupnp_service_proxy_action_get_result_hash()
291 *
292 * ```c
293 * GUPnPServiceProxyAction *action =
294 * gupnp_service_proxy_action_new_plain ("GetVolume");
295 * gupnp_service_proxy_action_add (action, "InstanceID", value_instance);
296 * gupnp_service_proxy_action_add (action, "Channel", value_channel);
297 * ````
298 *
299 * Returns: A newly created #GUPnPServiceProxyAction
300 * Since: 1.6.6
301 */
302 GUPnPServiceProxyAction *
303 gupnp_service_proxy_action_new_plain (const char *action)
304 {
305 GUPnPServiceProxyAction *ret;
306
307 g_return_val_if_fail (action != NULL, NULL);
308
309 ret = g_atomic_rc_box_new0 (GUPnPServiceProxyAction);
310 ret->name = g_strdup (action);
311 ret->args = g_ptr_array_new_with_free_func((GDestroyNotify)action_argument_free);
312 ret->arg_map = g_hash_table_new (g_str_hash, g_str_equal);
313
314 return ret;
315 }
316
317 /**
318 * gupnp_service_proxy_action_ref:
319 * @action: an action
320 *
321 * Increases reference count of `action`
322 *
323 * Returns: (nullable): @action with an increased reference count
324 * Since: 1.2.0
325 */
326 GUPnPServiceProxyAction *
327 gupnp_service_proxy_action_ref (GUPnPServiceProxyAction *action)
328 {
329 g_return_val_if_fail (action, NULL);
330
331 return g_atomic_rc_box_acquire (action);
332 }
333
334 void
335 gupnp_service_proxy_action_reset (GUPnPServiceProxyAction *action)
336 {
337 g_clear_weak_pointer (&action->proxy);
338 g_clear_error (&action->error);
339 g_clear_object (&action->msg);
340
341 if (action->msg_str != NULL) {
342 g_string_free (action->msg_str, TRUE);
343 action->msg_str = NULL;
344 }
345 g_clear_pointer (&action->response, g_bytes_unref);
346 action->params = NULL;
347 g_clear_pointer (&action->doc, xmlFreeDoc);
348 }
349
350 static void
351 action_dispose (GUPnPServiceProxyAction *action)
352 {
353 gupnp_service_proxy_action_reset (action);
354 g_hash_table_destroy (action->arg_map);
355 g_ptr_array_unref (action->args);
356
357 g_free (action->name);
358 }
359
360 /**
361 * gupnp_service_proxy_action_unref:
362 * @action: an action
363 *
364 * Decreases reference count of `action`. If reference count drops to 0,
365 * the action and its contents will be freed.
366 *
367 * Since: 1.2.0
368 */
369 void
370 gupnp_service_proxy_action_unref (GUPnPServiceProxyAction *action)
371 {
372 g_return_if_fail (action);
373
374 g_atomic_rc_box_release_full (action, (GDestroyNotify) action_dispose);
375 }
376
377 G_DEFINE_BOXED_TYPE (GUPnPServiceProxyAction,
378 gupnp_service_proxy_action,
379 gupnp_service_proxy_action_ref,
380 gupnp_service_proxy_action_unref)
381
382 /* Writes a parameter name and GValue pair to @msg */
383 static void
384 write_in_parameter (ActionArgument *arg,
385 GString *msg_str)
386 {
387 /* Write parameter pair */
388 xml_util_start_element (msg_str, arg->name);
389 gvalue_util_value_append_to_xml_string (&arg->value, msg_str);
390 xml_util_end_element (msg_str, arg->name);
391 }
392
393 static void
394 write_footer (GUPnPServiceProxyAction *action)
395 {
396 /* Finish message */
397 g_string_append (action->msg_str, "</u:");
398 g_string_append (action->msg_str, action->name);
399 g_string_append_c (action->msg_str, '>');
400
401 g_string_append (action->msg_str,
402 "</s:Body>"
403 "</s:Envelope>");
404 }
405
406 /**
407 * gupnp_service_proxy_action_new:
408 * @action: The name of a remote action to call
409 * @...: tuples of in parameter name, in parameter type, and in parameter
410 * value, terminated with %NULL
411 *
412 * Prepares action @action with parameters @Varargs to be sent off to
413 * a remote service later with gupnp_service_proxy_call_action() or
414 * gupnp_service_proxy_call_action_async().
415 *
416 * After the action call has finished, the results of the call may be
417 * retrived from the #GUPnPServiceProxyAction by using
418 * gupnp_service_proxy_action_get_result(),
419 * gupnp_service_proxy_action_get_result_list() or
420 * gupnp_service_proxy_action_get_result_hash()
421 *
422 * ```c
423 * GUPnPServiceProxyAction *action =
424 * gupnp_service_proxy_action_new ("GetVolume",
425 * // Parameters
426 * "InstanceID", G_TYPE_INT, 0,
427 * "Channel", G_TYPE_STRING, "Master",
428 * NULL);
429 *
430 * GError *error = NULL;
431 * gupnp_service_proxy_call_action (proxy, action, NULL, &error);
432 * if (error != NULL) {
433 * g_warning ("Failed to call GetVolume: %s", error->message);
434 * g_clear_error (&error);
435 *
436 * return;
437 * }
438 *
439 * guint16 volume = 0;
440 * if (!gupnp_service_proxy_action_get_result (action,
441 * &error,
442 * "CurrentVolume", G_TYPE_UINT, &volume,
443 * NULL)) {
444 * g_message ("Current Volume: %u", volume);
445 * }
446 *
447 * gupnp_service_proxy_action_unref (action);
448 * ```
449 *
450 * Returns: A newly created #GUPnPServiceProxyAction
451 */
452 GUPnPServiceProxyAction *
453 gupnp_service_proxy_action_new (const char *action,
454 ...)
455 {
456 GUPnPServiceProxyAction *result = NULL;
457 va_list var_args;
458
459 g_return_val_if_fail (action != NULL, NULL);
460
461 va_start (var_args, action);
462 const char *arg_name = va_arg (var_args, const char *);
463
464 result = gupnp_service_proxy_action_new_plain (action);
465
466 while (arg_name != NULL) {
467 GValue value = G_VALUE_INIT;
468
469 GType type = va_arg (var_args, GType);
470 char *error = NULL;
471
472 G_VALUE_COLLECT_INIT (&value, type, var_args, 0, &error);
473 if (error == NULL) {
474 gupnp_service_proxy_action_add_argument (result,
475 arg_name,
476 &value);
477 g_value_unset (&value);
478 } else {
479 g_warning (
480 "Failed to collect value of type %s for %s: %s",
481 g_type_name (type),
482 arg_name,
483 error);
484 g_free (error);
485 }
486 arg_name = va_arg (var_args, const char *);
487 }
488 va_end (var_args);
489
490 return result;
491 }
492
493 /**
494 * gupnp_service_proxy_action_new_from_list:
495 * @action: An action
496 * @in_names: (element-type utf8) (transfer none): #GList of 'in' parameter
497 * names (as strings)
498 * @in_values: (element-type GValue) (transfer none): #GList of values (as
499 * #GValue) that line up with @in_names
500 *
501 * Prepares action @action with parameters @in_names and @in_values to be
502 * sent off to a remote service later with gupnp_service_proxy_call_action() or
503 * gupnp_service_proxy_call_action_async(). This is mainly useful for language
504 * bindings.
505 *
506 * After the action call has finished, the results of the call may be
507 * retrived from the #GUPnPServiceProxyAction by using
508 * gupnp_service_proxy_action_get_result(),
509 * gupnp_service_proxy_action_get_result_list() or
510 * gupnp_service_proxy_action_get_result_hash()
511 * ```c
512 * GList *in_args = NULL;
513 * in_args = g_list_append (in_args, "InstanceID");
514 * in_args = g_list_append (in_args, "Unit");
515 * in_args = g_list_append (in_args, "Target");
516 *
517 * GValue instance = G_VALUE_INIT;
518 * g_value_set_int (&instance, 0);
519 * GValue unit = G_VALUE_INIT;
520 * g_value_set_static_string (&unit, "ABS_TIME");
521 * GValue target = G_VALUE_INIT;
522 * g_value_set_static_string (&target, "00:00:00.000");
523 *
524 * GList *in_values = NULL;
525 * in_values = g_list_append (in_values, &instance);
526 * in_values = g_list_append (in_values, &unit);
527 * in_values = g_list_append (in_values, &target);
528 *
529 * GUPnPServiceProxyAction *action =
530 * gunp_service_proxy_action_new_from_list ("Seek", in_args, in_values);
531 *
532 * GError *error = NULL;
533 * gupnp_service_proxy_call_action_async (proxy, action, NULL, on_action_finished, NULL);
534 * gupnp_service_proxy_action_unref (action);
535 * ```
536 *
537 * Returns: A newly created #GUPnPServiceProxyAction
538 */
539 GUPnPServiceProxyAction *
540 gupnp_service_proxy_action_new_from_list (const char *action_name,
541 GList *in_names,
542 GList *in_values)
543 {
544 GUPnPServiceProxyAction *action;
545 GList *names = NULL;
546 GList *values = NULL;
547 guint position;
548
549 action = gupnp_service_proxy_action_new_plain (action_name);
550
551 /* Arguments */
552 for (names = in_names, values = in_values, position = 0;
553 names && values;
554 names = names->next, values = values->next, position++) {
555 GValue *val = values->data;
556 gupnp_service_proxy_action_add_argument (
557 action,
558 (const char *) names->data,
559 val);
560 }
561
562 return action;
563 }
564
565 /**
566 * gupnp_service_proxy_action_add_argument:
567 * @action: The action to append to
568 * @name: The name of the argument
569 * @value: The value of the argument
570 *
571 * Append @name to the list of arguments used by @action
572 *
573 * Returns: (transfer none): @action for convenience.
574 * Since: 1.6.6
575 */
576 GUPnPServiceProxyAction *
577 gupnp_service_proxy_action_add_argument (GUPnPServiceProxyAction *action,
578 const char *name,
579 const GValue *value)
580 {
581 g_return_val_if_fail (g_hash_table_lookup_extended (action->arg_map,
582 name,
583 NULL,
584 NULL) == FALSE,
585 NULL);
586
587 ActionArgument *arg = g_new0 (ActionArgument, 1);
588 arg->name = g_strdup (name);
589 g_value_init (&arg->value, G_VALUE_TYPE (value));
590 g_value_copy (value, &arg->value);
591 g_hash_table_insert (action->arg_map,
592 arg->name,
593 GUINT_TO_POINTER (action->args->len));
594 g_ptr_array_add (action->args, arg);
595
596 return action;
597 }
598
599 void
600 gupnp_service_proxy_action_serialize (GUPnPServiceProxyAction *action,
601 const char *service_type)
602 {
603 if (action->msg_str != NULL) {
604 g_string_free (action->msg_str, TRUE);
605 }
606 action->msg_str = xml_util_new_string ();
607
608 g_string_append (action->msg_str,
609 "<?xml version=\"1.0\"?>"
610 "<s:Envelope xmlns:s="
611 "\"http://schemas.xmlsoap.org/soap/envelope/\" "
612 "s:encodingStyle="
613 "\"http://schemas.xmlsoap.org/soap/encoding/\">"
614 "<s:Body>");
615 action->header_pos = action->msg_str->len;
616
617 g_ptr_array_foreach (action->args,
618 (GFunc) write_in_parameter,
619 action->msg_str);
620
621 write_footer (action);
622
623 g_string_insert (action->msg_str, action->header_pos, "<u:");
624 action->header_pos += strlen ("<u:");
625 g_string_insert (action->msg_str, action->header_pos, action->name);
626 action->header_pos += strlen (action->name);
627 g_string_insert (action->msg_str, action->header_pos, " xmlns:u=\"");
628 action->header_pos += strlen (" xmlns:u=\"");
629 g_string_insert (action->msg_str, action->header_pos, service_type);
630 action->header_pos += strlen (service_type);
631 g_string_insert (action->msg_str, action->header_pos, "\">");
632 }
633
634 /* Checks an action response for errors and returns the parsed
635 * xmlDoc object. */
636 void
637 gupnp_service_proxy_action_check_response (GUPnPServiceProxyAction *action)
638 {
639 xmlDoc *response;
640 int code;
641
642 if (action->doc != NULL) {
643 return;
644 }
645
646 if (action->error != NULL) {
647 return;
648 }
649
650 if (action->msg == NULL || action->response == NULL) {
651 g_set_error_literal (&action->error,
652 GUPNP_SERVER_ERROR,
653 GUPNP_SERVER_ERROR_INVALID_RESPONSE,
654 "No message, the action was not sent?");
655
656 return;
657 }
658
659 SoupStatus status = soup_message_get_status (action->msg);
660
661 if (status != SOUP_STATUS_OK &&
662 status != SOUP_STATUS_INTERNAL_SERVER_ERROR) {
663 _gupnp_error_set_server_error (&action->error, action->msg);
664 return;
665 }
666
667 /* Parse response */
668 gconstpointer data;
669 gsize length;
670 data = g_bytes_get_data (action->response, &length);
671 response = xmlReadMemory (data,
672 length,
673 NULL,
674 NULL,
675 XML_PARSE_NONET | XML_PARSE_RECOVER);
676
677 g_clear_pointer (&action->response, g_bytes_unref);
678
679 if (!response) {
680 if (status == SOUP_STATUS_OK) {
681 g_set_error_literal (
682 &action->error,
683 GUPNP_SERVER_ERROR,
684 GUPNP_SERVER_ERROR_INVALID_RESPONSE,
685 "Could not parse SOAP response");
686 } else {
687 g_set_error_literal (
688 &action->error,
689 GUPNP_SERVER_ERROR,
690 GUPNP_SERVER_ERROR_INTERNAL_SERVER_ERROR,
691 soup_message_get_reason_phrase (action->msg));
692 }
693
694 return;
695 }
696
697 xmlNodePtr params = NULL;
698 /* Get parameter list */
699 params = xml_util_get_element ((xmlNode *) response, "Envelope", NULL);
700 if (params != NULL)
701 params = xml_util_real_node (params->children);
702
703 if (params != NULL) {
704 if (strcmp ((const char *) params->name, "Header") == 0)
705 params = xml_util_real_node (params->next);
706
707 if (params != NULL)
708 if (strcmp ((const char *) params->name, "Body") != 0)
709 params = NULL;
710 }
711
712 if (params != NULL)
713 params = xml_util_real_node (params->children);
714
715 if (params == NULL) {
716 g_set_error (&action->error,
717 GUPNP_SERVER_ERROR,
718 GUPNP_SERVER_ERROR_INVALID_RESPONSE,
719 "Invalid Envelope");
720
721 xmlFreeDoc (response);
722
723 return;
724 }
725
726 /* Check whether we have a Fault */
727 if (status == SOUP_STATUS_INTERNAL_SERVER_ERROR) {
728 xmlNode *param;
729 char *desc;
730
731 param = xml_util_get_element (params,
732 "detail",
733 "UPnPError",
734 NULL);
735
736 if (!param) {
737 g_set_error (&action->error,
738 GUPNP_SERVER_ERROR,
739 GUPNP_SERVER_ERROR_INVALID_RESPONSE,
740 "Invalid Fault");
741
742 xmlFreeDoc (response);
743
744 return;
745 }
746
747 /* Code */
748 code = xml_util_get_child_element_content_int (param,
749 "errorCode");
750 if (code == -1) {
751 g_set_error (&action->error,
752 GUPNP_SERVER_ERROR,
753 GUPNP_SERVER_ERROR_INVALID_RESPONSE,
754 "Invalid Fault");
755
756 xmlFreeDoc (response);
757
758 return;
759 }
760
761 /* Description */
762 desc = xml_util_get_child_element_content_glib (
763 param,
764 "errorDescription");
765 if (desc == NULL)
766 desc = g_strdup (
767 soup_message_get_reason_phrase (action->msg));
768
769 g_set_error_literal (&action->error,
770 GUPNP_CONTROL_ERROR,
771 code,
772 desc);
773
774 g_free (desc);
775
776 xmlFreeDoc (response);
777
778 return;
779 }
780
781 action->params = params;
782 action->doc = response;
783 }
784
785 /**
786 * gupnp_service_proxy_action_get_result_list:
787 * @action: A #GUPnPServiceProxyAction handle
788 * @out_names: (element-type utf8) (transfer none): #GList of 'out' parameter
789 * names (as strings)
790 * @out_types: (element-type GType) (transfer none): #GList of types (as #GType)
791 * that line up with @out_names
792 * @out_values: (element-type GValue) (transfer full) (out): #GList of values
793 * (as #GValue) that line up with @out_names and @out_types.
794 * @error:(inout)(optional)(nullable): The location where to store any error, or %NULL
795 *
796 * A variant of gupnp_service_proxy_action_get_result() that takes lists of
797 * out-parameter names, types and place-holders for values.
798 *
799 * The returned list in @out_values must be freed using `g_list_free` and each element
800 * in it using `g_value_unset` and `g_free`.
801 * ```c
802 * void on_action_finished(GObject *object, GAsyncResult *res, gpointer user_data)
803 * {
804 * GUPnPServiceProxyAction *action;
805 * GError *error;
806 *
807 * action = gupnp_service_proxy_call_action_finish (GUPNP_SERVICE_PROXY (object),
808 * res,
809 * &error);
810 *
811 * if (error != NULL) {
812 * g_print ("Call failed: %s", error->message);
813 * g_clear_error (&error);
814 * return;
815 * }
816 *
817 * GList *out_args = NULL;
818 * out_args = g_list_append (out_args, "PlayMode");
819 * out_args = g_list_append (out_args, "RecQualityMode");
820 * GList *out_types = NULL;
821 * out_types = g_list_append (out_types, GSIZE_TO_POINTER (G_TYPE_STRING));
822 * out_types = g_list_append (out_types, GSIZE_TO_POINTER (G_TYPE_STRING));
823 * GList *out_values = NULL;
824 *
825 * if (!gupnp_service_proxy_action_get_result_list (action,
826 * out_args,
827 * out_types,
828 * &out_values,
829 * &error)) {
830 * g_print ("Getting results failed: %s", error->message);
831 * g_clear_error (&error);
832 * return;
833 * }
834 *
835 * GList *iter = out_values;
836 * while (iter != NULL) {
837 * GValue *value = iter->data;
838 * g_print ("Result: %s\n", g_value_get_string (value));
839 * g_value_unset (value);
840 * g_free (value);
841 * iter = g_list_remove_link (iter, iter);
842 * }
843 * g_list_free (out_values);
844 * }
845 *```
846 *
847 * Return value : %TRUE on success.
848 *
849 * Since: 1.2.0
850 *
851 **/
852 gboolean
853 gupnp_service_proxy_action_get_result_list (GUPnPServiceProxyAction *action,
854 GList *out_names,
855 GList *out_types,
856 GList **out_values,
857 GError **error)
858 {
859 GList *names;
860 GList *types;
861 GList *out_values_list;
862
863 out_values_list = NULL;
864
865 g_return_val_if_fail (action, FALSE);
866
867 /* Check for saved error from begin_action() */
868 if (action->error) {
869 g_propagate_error (error, g_error_copy (action->error));
870
871 return FALSE;
872 }
873
874 /* Check response for errors and do initial parsing */
875 gupnp_service_proxy_action_check_response (action);
876 if (action->error != NULL) {
877 g_propagate_error (error, g_error_copy (action->error));
878
879 return FALSE;
880 }
881
882 /* Read arguments */
883 types = out_types;
884 for (names = out_names; names; names=names->next) {
885 GValue *val;
886
887 val = g_new0 (GValue, 1);
888 g_value_init (val, (GType) types->data);
889
890 read_out_parameter (names->data, val, action->params);
891
892 out_values_list = g_list_append (out_values_list, val);
893
894 types = types->next;
895 }
896
897 *out_values = out_values_list;
898
899 return TRUE;
900
901 }
902
903 /**
904 * gupnp_service_proxy_action_get_result_hash:
905 * @action: A #GUPnPServiceProxyAction handle
906 * @out_hash: (element-type utf8 GValue) (inout) (transfer none): A #GHashTable of
907 * out parameter name and initialised #GValue pairs
908 * @error:(inout)(optional)(nullable): The location where to store any error, or %NULL
909 *
910 * See gupnp_service_proxy_action_get_result(); this version takes a #GHashTable for
911 * runtime generated parameter lists.
912 *
913 * The @out_hash needs to be pre-initialized with key value pairs denoting the argument
914 * to retrieve and an empty #GValue initialized to hold the wanted type with g_value_init().
915 *
916 *```c
917 * void on_action_finished(GObject *object, GAsyncResult *res, gpointer user_data)
918 * {
919 * GUPnPServiceProxyAction *action;
920 * GError *error;
921 *
922 * action = gupnp_service_proxy_call_action_finish (GUPNP_SERVICE_PROXY (object),
923 * res,
924 * &error);
925 *
926 * if (error != NULL) {
927 * g_print ("Call failed: %s", error->message);
928 * g_clear_error (&error);
929 * return;
930 * }
931 *
932 * GValue play_mode = G_VALUE_INIT;
933 * g_value_init(&play_mode, G_TYPE_STRING);
934 * GValue rec_quality_mode = G_VALUE_INIT;
935 * g_value_init(&rec_quality_mode, G_TYPE_STRING);
936 *
937 * GHashTable *out_args = g_hash_table_new (g_str_hash, g_str_equal);
938 * g_hash_table_insert(out_args, "PlayMode", &play_mode);
939 * g_hash_table_insert(out_args, "RecQualityMode", &rec_quality_mode);
940 *
941 * if (!gupnp_service_proxy_action_get_result_hash (action,
942 * out_args,
943 * &error)) {
944 * g_print ("Getting results failed: %s", error->message);
945 * g_clear_error (&error);
946 * return;
947 * }
948 *
949 * g_value_unset (&play_mode);
950 * g_value_unset (&rec_quality_mode);
951 *
952 * g_hash_table_unref (out_args);
953 * }
954 * ```
955 *
956 * Return value: %TRUE on success.
957 *
958 * Since: 1.2.0
959 **/
960 gboolean
961 gupnp_service_proxy_action_get_result_hash (GUPnPServiceProxyAction *action,
962 GHashTable *hash,
963 GError **error)
964 {
965 g_return_val_if_fail (action, FALSE);
966
967 /* Check for saved error from begin_action() */
968 if (action->error) {
969 g_propagate_error (error, g_error_copy (action->error));
970
971 return FALSE;
972 }
973
974 /* Check response for errors and do initial parsing */
975 gupnp_service_proxy_action_check_response (action);
976 if (action->error != NULL) {
977 g_propagate_error (error, g_error_copy (action->error));
978
979 return FALSE;
980 }
981
982 /* Read arguments */
983 g_hash_table_foreach (hash,
984 (GHFunc) read_out_parameter,
985 action->params);
986
987 return TRUE;
988
989 }
990
991 /**
992 * gupnp_service_proxy_action_get_result:
993 * @action: A #GUPnPServiceProxyAction handle
994 * @error: (inout)(optional)(nullable): The location where to store any error, or %NULL
995 * @...: tuples of out parameter name, out parameter type, and out parameter
996 * value location, terminated with %NULL. The out parameter values should be
997 * freed after use
998 *
999 * Retrieves the result of @action. The out parameters in @Varargs will be
1000 * filled in, and if an error occurred, @error will be set. In case of
1001 * an UPnP error the error code will be the same in @error.
1002 *
1003 * Return value: %TRUE on success.
1004 **/
1005 gboolean
1006 gupnp_service_proxy_action_get_result (GUPnPServiceProxyAction *action,
1007 GError **error,
1008 ...)
1009 {
1010 g_return_val_if_fail (action, FALSE);
1011
1012 va_list var_args;
1013
1014 va_start (var_args, error);
1015 GHashTable *out_hash;
1016 va_list var_args_copy;
1017 gboolean result;
1018 GError *local_error;
1019
1020
1021 out_hash = g_hash_table_new_full (g_str_hash,
1022 g_str_equal,
1023 g_free,
1024 gvalue_free);
1025 G_VA_COPY (var_args_copy, var_args);
1026 VAR_ARGS_TO_OUT_HASH_TABLE (var_args, out_hash);
1027 local_error = NULL;
1028 result = gupnp_service_proxy_action_get_result_hash (action,
1029 out_hash,
1030 &local_error);
1031
1032 if (local_error == NULL) {
1033 OUT_HASH_TABLE_TO_VAR_ARGS (out_hash, var_args_copy);
1034 } else {
1035 g_propagate_error (error, local_error);
1036 }
1037 va_end (var_args_copy);
1038 g_hash_table_unref (out_hash);
1039 va_end (var_args);
1040
1041 return result;
1042 }
1043
1044 /**
1045 * gupnp_service_proxy_action_set:
1046 * @action: the action to modify
1047 * @key: the name of the value to modify
1048 * @value: the new value of @key
1049 * @error: (nullable): a return location for an #GError
1050 *
1051 * Update the value of @key to @value.
1052 *
1053 * @key needs to already exist in @action.
1054 *
1055 * Returns: true if successfully modified, false otherwise
1056 * Since: 1.4.0
1057 */
1058 gboolean
1059 gupnp_service_proxy_action_set (GUPnPServiceProxyAction *action,
1060 const char *key,
1061 const GValue *value,
1062 GError **error)
1063 {
1064 g_return_val_if_fail (key != NULL, FALSE);
1065 g_return_val_if_fail (value != NULL, FALSE);
1066 g_return_val_if_fail (error != NULL && *error == NULL, FALSE);
1067 gpointer position;
1068
1069 if (!g_hash_table_lookup_extended (action->arg_map,
1070 key,
1071 NULL,
1072 &position)) {
1073 g_propagate_error (error,
1074 g_error_new (GUPNP_SERVER_ERROR,
1075 GUPNP_SERVER_ERROR_OTHER,
1076 "Unknown argument: %s",
1077 key));
1078
1079 return FALSE;
1080 }
1081
1082 ActionArgument *arg =
1083 g_ptr_array_index (action->args, GPOINTER_TO_UINT (position));
1084
1085 if (G_VALUE_TYPE (value) != G_VALUE_TYPE (&arg->value)) {
1086 g_propagate_error (
1087 error,
1088 g_error_new (
1089 GUPNP_SERVER_ERROR,
1090 GUPNP_SERVER_ERROR_OTHER,
1091 "Type mismatch for %s. Expected %s, got %s",
1092 key,
1093 G_VALUE_TYPE_NAME (&arg->value),
1094 G_VALUE_TYPE_NAME (value)));
1095
1096 return FALSE;
1097 }
1098
1099 g_value_copy (value, &arg->value);
1100
1101 return TRUE;
1102 }