summaryrefslogtreecommitdiff
path: root/dbus/controller_iface_configure.c
blob: a5e67f30f332af73d63aa6585b672e6f2ed47167 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
/* -*- Mode: C ; c-basic-offset: 4 -*- */
/*
    Copyright (C) 2007,2008,2011 Nedko Arnaudov
    Copyright (C) 2007-2008 Juuso Alasuutari

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif

#include <stdint.h>
#include <string.h>
#include <assert.h>
#include <dbus/dbus.h>

#include "jackdbus.h"
#include "controller_internal.h"
#include "xml.h"

unsigned char jack_controller_dbus_types[JACK_PARAM_MAX] =
{
    [JackParamInt] = DBUS_TYPE_INT32,
    [JackParamUInt] = DBUS_TYPE_UINT32,
    [JackParamChar] = DBUS_TYPE_BYTE,
    [JackParamString] = DBUS_TYPE_STRING,
    [JackParamBool] = DBUS_TYPE_BOOLEAN,
};

const char *jack_controller_dbus_type_signatures[JACK_PARAM_MAX] =
{
    [JackParamInt] = DBUS_TYPE_INT32_AS_STRING,
    [JackParamUInt] = DBUS_TYPE_UINT32_AS_STRING,
    [JackParamChar] = DBUS_TYPE_BYTE_AS_STRING,
    [JackParamString] = DBUS_TYPE_STRING_AS_STRING,
    [JackParamBool] = DBUS_TYPE_BOOLEAN_AS_STRING,
};

#define PARAM_TYPE_JACK_TO_DBUS(_) jack_controller_dbus_types[_]
#define PARAM_TYPE_JACK_TO_DBUS_SIGNATURE(_) jack_controller_dbus_type_signatures[_]

static
bool
jack_controller_jack_to_dbus_variant(
    jackctl_param_type_t type,
    const union jackctl_parameter_value *value_ptr,
    message_arg_t  *dbusv_ptr)
{
    switch (type)
    {
    case JackParamInt:
        dbusv_ptr->int32 = (dbus_int32_t)value_ptr->i;
        return true;
    case JackParamUInt:
        dbusv_ptr->uint32 = (dbus_uint32_t)value_ptr->ui;
        return true;
    case JackParamChar:
        dbusv_ptr->byte = value_ptr->c;
        return true;
    case JackParamString:
        dbusv_ptr->string = value_ptr->str;
        return true;
    case JackParamBool:
        dbusv_ptr->boolean = (dbus_bool_t)value_ptr->b;
        return true;
    }

    jack_error("Unknown JACK parameter type %i", (int)type);
    assert(0);
    return false;
}

static
bool
jack_controller_dbus_to_jack_variant(
    int type,
    const message_arg_t *dbusv_ptr,
    union jackctl_parameter_value *value_ptr)
{
    size_t len;

    switch (type)
    {
    case DBUS_TYPE_INT32:
        value_ptr->i = dbusv_ptr->int32;
        return true;
    case DBUS_TYPE_UINT32:
        value_ptr->ui = dbusv_ptr->uint32;
        return true;
    case DBUS_TYPE_BYTE:
        value_ptr->c = dbusv_ptr->byte;
        return true;
    case DBUS_TYPE_STRING:
        len = strlen(dbusv_ptr->string);
        if (len > JACK_PARAM_STRING_MAX)
        {
            jack_error("Parameter string value is too long (%u)", (unsigned int)len);
            return false;
        }
        memcpy(value_ptr->str, dbusv_ptr->string, len + 1);

        return true;
    case DBUS_TYPE_BOOLEAN:
        value_ptr->b = dbusv_ptr->boolean;
        return true;
    }

    jack_error("Unknown D-Bus parameter type %i", (int)type);
    return false;
}

/*
 * Construct a return message for a Get[Driver|Engine]ParameterValue method call.
 *
 * The operation can only fail due to lack of memory, in which case
 * there's no sense in trying to construct an error return. Instead,
 * call->reply will be set to NULL and handled in send_method_return().
 */
static void
jack_dbus_construct_method_return_parameter(
    struct jack_dbus_method_call * call,
    dbus_bool_t is_set,
    int type,
    const char *signature,
    message_arg_t default_value,
    message_arg_t value)
{
    DBusMessageIter iter;

    /* Create a new method return message. */
    call->reply = dbus_message_new_method_return (call->message);
    if (!call->reply)
    {
        goto fail;
    }

    dbus_message_iter_init_append (call->reply, &iter);

    /* Append the is_set argument. */
    if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_BOOLEAN, (const void *) &is_set))
    {
        goto fail_unref;
    }

    /* Append the 'default' and 'value' arguments. */
    if (!jack_dbus_message_append_variant(&iter, type, signature, &default_value))
    {
        goto fail_unref;
    }
    if (!jack_dbus_message_append_variant(&iter, type, signature, &value))
    {
        goto fail_unref;
    }

    return;

fail_unref:
    dbus_message_unref (call->reply);
    call->reply = NULL;

fail:
    jack_error ("Ran out of memory trying to construct method return");
}

static
bool
jack_controller_dbus_get_parameter_address_ex(
    struct jack_dbus_method_call * call,
    DBusMessageIter * iter_ptr,
    const char ** address_array)
{
    const char * signature;
    DBusMessageIter array_iter;
    int type;
    int index;

    if (!dbus_message_iter_init(call->message, iter_ptr))
    {
        jack_dbus_error(
            call,
            JACK_DBUS_ERROR_INVALID_ARGS,
            "Invalid arguments to method '%s'. No input arguments found.",
            call->method_name);
        return false;
    }

    signature = dbus_message_iter_get_signature(iter_ptr);
    if (signature == NULL)
    {
        jack_error("dbus_message_iter_get_signature() failed");
        return false;
    }

    if (strcmp(signature, "as") != 0)
    {
        jack_dbus_error(
            call,
            JACK_DBUS_ERROR_INVALID_ARGS,
            "Invalid arguments to method '%s'. Input arguments signature '%s', must begin with 'as'.",
            call->method_name,
            signature);
        return false;
    }

    dbus_message_iter_recurse(iter_ptr, &array_iter);

    index = 0;
    while ((type = dbus_message_iter_get_arg_type(&array_iter)) != DBUS_TYPE_INVALID)
    {
        if (index == PARAM_ADDRESS_SIZE)
        {
            jack_dbus_error(
                call,
                JACK_DBUS_ERROR_INVALID_ARGS,
                "Invalid arguments to method '%s'. Parameter address array must contain not more than %u elements.",
                (unsigned int)PARAM_ADDRESS_SIZE,
                call->method_name);
            return false;
        }

        ;
        if (type != DBUS_TYPE_STRING)
        {
            jack_dbus_error(
                call,
                JACK_DBUS_ERROR_FATAL,
                "Internal error when parsing parameter address of method '%s'. Address array element type '%c' is not string type.",
                call->method_name,
                type);
            return false;
        }

        dbus_message_iter_get_basic(&array_iter, address_array + index);
        //jack_info("address component: '%s'", address_array[index]);

        dbus_message_iter_next(&array_iter);
        index++;
    }

    while (index < PARAM_ADDRESS_SIZE)
    {
        address_array[index] = NULL;
        index++;
    }

    return true;
}

static
bool
jack_controller_dbus_get_parameter_address(
    struct jack_dbus_method_call * call,
    const char ** address_array)
{
    DBusMessageIter iter;
    bool ret;

    ret = jack_controller_dbus_get_parameter_address_ex(call, &iter, address_array);
    if (ret && dbus_message_iter_has_next(&iter))
    {
        jack_dbus_error(
            call,
            JACK_DBUS_ERROR_INVALID_ARGS,
            "Invalid arguments to method '%s'. Input arguments signature must be 'as'.",
            call->method_name);
        return false;
    }

    return ret;
}

#define controller_ptr ((struct jack_controller *)call->context)

static bool append_node_name(void * context, const char * name)
{
    return dbus_message_iter_append_basic(context, DBUS_TYPE_STRING, &name);
}

static
void
jack_controller_dbus_read_container(
    struct jack_dbus_method_call * call)
{
    const char * address[PARAM_ADDRESS_SIZE];
    dbus_bool_t leaf;
    DBusMessageIter iter;
    DBusMessageIter array_iter;

    //jack_info("jack_controller_dbus_read_container() called");

    if (!jack_controller_dbus_get_parameter_address(call, address))
    {
        /* The method call had invalid arguments meaning that
         * jack_controller_dbus_get_parameter_address() has
         * constructed an error for us. */
        return;
    }

    //jack_info("address is '%s':'%s':'%s'", address[0], address[1], address[2]);

    /* Create a new method return message. */
    call->reply = dbus_message_new_method_return(call->message);
    if (!call->reply)
    {
        goto oom;
    }

    dbus_message_iter_init_append(call->reply, &iter);

    if (!jack_params_check_address(controller_ptr->params, address, false))
    {
        jack_dbus_error(
            call,
            JACK_DBUS_ERROR_INVALID_ARGS,
            "Invalid container address '%s':'%s':'%s' supplied to method '%s'.",
            address[0],
            address[1],
            address[2],
            call->method_name);
        return;
    }

    leaf = jack_params_is_leaf_container(controller_ptr->params, address);
    if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &leaf))
    {
        goto oom_unref;
    }

    if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &array_iter))
    {
        goto oom_unref;
    }


    if (!jack_params_iterate_container(controller_ptr->params, address, append_node_name, &array_iter))
    {
        goto oom_close_unref;
    }

    dbus_message_iter_close_container(&iter, &array_iter);

    return;

oom_close_unref:
    dbus_message_iter_close_container(&iter, &array_iter);

oom_unref:
    dbus_message_unref(call->reply);
    call->reply = NULL;

oom:
    jack_error ("Ran out of memory trying to construct method return");
}

static bool append_parameter(void * context, const struct jack_parameter * param_ptr)
{
    DBusMessageIter struct_iter;
    unsigned char type;

    /* Open the struct. */
    if (!dbus_message_iter_open_container(context, DBUS_TYPE_STRUCT, NULL, &struct_iter))
    {
        goto fail;
    }

    /* Append parameter type. */
    type = PARAM_TYPE_JACK_TO_DBUS(param_ptr->type);
    if (!dbus_message_iter_append_basic(&struct_iter, DBUS_TYPE_BYTE, &type))
    {
        goto fail_close;
    }

    /* Append parameter name. */
    if (!dbus_message_iter_append_basic(&struct_iter, DBUS_TYPE_STRING, &param_ptr->name))
    {
        goto fail_close;
    }

    /* Append parameter short description. */
    if (!dbus_message_iter_append_basic(&struct_iter, DBUS_TYPE_STRING, &param_ptr->short_decr))
    {
        goto fail_close;
    }

    /* Append parameter long description. */
    if (!dbus_message_iter_append_basic(&struct_iter, DBUS_TYPE_STRING, &param_ptr->long_descr))
    {
        goto fail_close;
    }

    /* Close the struct. */
    if (!dbus_message_iter_close_container(context, &struct_iter))
    {
        goto fail;
    }

    return true;

fail_close:
    dbus_message_iter_close_container(context, &struct_iter);

fail:
    return false;
}

static
void
jack_controller_dbus_get_parameters_info(
    struct jack_dbus_method_call * call)
{
    const char * address[PARAM_ADDRESS_SIZE];
    DBusMessageIter iter, array_iter;

    //jack_info("jack_controller_dbus_get_parameters_info() called");

    if (!jack_controller_dbus_get_parameter_address(call, address))
    {
        /* The method call had invalid arguments meaning that
         * jack_controller_dbus_get_parameter_address() has
         * constructed an error for us. */
        return;
    }

    //jack_info("address is '%s':'%s':'%s'", address[0], address[1], address[2]);

    if (!jack_params_check_address(controller_ptr->params, address, true))
    {
        jack_dbus_error(
            call,
            JACK_DBUS_ERROR_INVALID_ARGS,
            "Invalid container address '%s':'%s':'%s' supplied to method '%s'.",
            address[0],
            address[1],
            address[2],
            call->method_name);
        return;
    }

    call->reply = dbus_message_new_method_return (call->message);
    if (!call->reply)
    {
        goto fail;
    }

    dbus_message_iter_init_append (call->reply, &iter);

    /* Open the array. */
    if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY, "(ysss)", &array_iter))
    {
        goto fail_unref;
    }

    if (!jack_params_iterate_params(controller_ptr->params, address, append_parameter, &array_iter))
    {
        goto fail_close_unref;
    }

    /* Close the array. */
    if (!dbus_message_iter_close_container (&iter, &array_iter))
    {
        goto fail_unref;
    }

    return;

fail_close_unref:
    dbus_message_iter_close_container (&iter, &array_iter);

fail_unref:
    dbus_message_unref (call->reply);
    call->reply = NULL;

fail:
    jack_error ("Ran out of memory trying to construct method return");
}

static
void
jack_controller_dbus_get_parameter_info(
    struct jack_dbus_method_call * call)
{
    const char * address[PARAM_ADDRESS_SIZE];
    const struct jack_parameter * param_ptr;
    DBusMessageIter iter;

    //jack_info("jack_controller_dbus_get_parameter_info() called");

    if (!jack_controller_dbus_get_parameter_address(call, address))
    {
        /* The method call had invalid arguments meaning that
         * jack_controller_dbus_get_parameter_address() has
         * constructed an error for us. */
        return;
    }

    //jack_info("address is '%s':'%s':'%s'", address[0], address[1], address[2]);

    param_ptr = jack_params_get_parameter(controller_ptr->params, address);
    if (param_ptr == NULL)
    {
        jack_dbus_error(
            call,
            JACK_DBUS_ERROR_INVALID_ARGS,
            "Invalid container address '%s':'%s':'%s' supplied to method '%s'.",
            address[0],
            address[1],
            address[2],
            call->method_name);
        return;
    }

    call->reply = dbus_message_new_method_return(call->message);
    if (!call->reply)
    {
        goto fail;
    }

    dbus_message_iter_init_append(call->reply, &iter);

    if (!append_parameter(&iter, param_ptr))
    {
        goto fail_unref;
    }

    return;

fail_unref:
    dbus_message_unref(call->reply);
    call->reply = NULL;

fail:
    jack_error("Ran out of memory trying to construct method return");
}

static
void
jack_controller_dbus_get_parameter_constraint(
    struct jack_dbus_method_call * call)
{
    const char * address[PARAM_ADDRESS_SIZE];
    const struct jack_parameter * param_ptr;
    uint32_t index;
    DBusMessageIter iter, array_iter, struct_iter;
    const char * descr;
    message_arg_t value;

    //jack_info("jack_controller_dbus_get_parameter_constraint() called");

    if (!jack_controller_dbus_get_parameter_address(call, address))
    {
        /* The method call had invalid arguments meaning that
         * jack_controller_dbus_get_parameter_address() has
         * constructed an error for us. */
        return;
    }

    //jack_info("address is '%s':'%s':'%s'", address[0], address[1], address[2]);

    param_ptr = jack_params_get_parameter(controller_ptr->params, address);
    if (param_ptr == NULL)
    {
        jack_dbus_error(
            call,
            JACK_DBUS_ERROR_INVALID_ARGS,
            "Invalid container address '%s':'%s':'%s' supplied to method '%s'.",
            address[0],
            address[1],
            address[2],
            call->method_name);
        return;
    }

    call->reply = dbus_message_new_method_return(call->message);
    if (!call->reply)
    {
        goto fail;
    }

    dbus_message_iter_init_append(call->reply, &iter);

    if ((param_ptr->constraint_flags & JACK_CONSTRAINT_FLAG_VALID) != 0)
    {
        value.boolean = param_ptr->constraint_range;
    }
    else
    {
        value.boolean = false;
    }

    if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &value))
    {
        goto fail_unref;
    }

    if ((param_ptr->constraint_flags & JACK_CONSTRAINT_FLAG_VALID) != 0)
    {
        value.boolean = (param_ptr->constraint_flags & JACK_CONSTRAINT_FLAG_STRICT) != 0;
    }

    if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &value))
    {
        goto fail_unref;
    }

    if ((param_ptr->constraint_flags & JACK_CONSTRAINT_FLAG_VALID) != 0)
    {
        value.boolean = (param_ptr->constraint_flags & JACK_CONSTRAINT_FLAG_FAKE_VALUE) != 0;
    }

    if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &value))
    {
        goto fail_unref;
    }

    /* Open the array. */
    if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(vs)", &array_iter))
    {
        goto fail_unref;
    }

    if ((param_ptr->constraint_flags & JACK_CONSTRAINT_FLAG_VALID) == 0)
    {
        goto close;
    }

    if (param_ptr->constraint_range)
    {
        /* Open the struct. */
        if (!dbus_message_iter_open_container(&array_iter, DBUS_TYPE_STRUCT, NULL, &struct_iter))
        {
            goto fail_close_unref;
        }

        jack_controller_jack_to_dbus_variant(param_ptr->type, &param_ptr->constraint.range.min, &value);

        if (!jack_dbus_message_append_variant(
                &struct_iter,
                PARAM_TYPE_JACK_TO_DBUS(param_ptr->type),
                PARAM_TYPE_JACK_TO_DBUS_SIGNATURE(param_ptr->type),
                &value))
        {
            goto fail_close2_unref;
        }

        descr = "min";

        if (!dbus_message_iter_append_basic(&struct_iter, DBUS_TYPE_STRING, &descr))
        {
            goto fail_close2_unref;
        }

        /* Close the struct. */
        if (!dbus_message_iter_close_container(&array_iter, &struct_iter))
        {
            goto fail_close_unref;
        }

        /* Open the struct. */
        if (!dbus_message_iter_open_container(&array_iter, DBUS_TYPE_STRUCT, NULL, &struct_iter))
        {
            goto fail_close_unref;
        }

        jack_controller_jack_to_dbus_variant(param_ptr->type, &param_ptr->constraint.range.max, &value);
 
        if (!jack_dbus_message_append_variant(
                &struct_iter,
                PARAM_TYPE_JACK_TO_DBUS(param_ptr->type),
                PARAM_TYPE_JACK_TO_DBUS_SIGNATURE(param_ptr->type),
                &value))
        {
            goto fail_close2_unref;
        }

        descr = "max";

        if (!dbus_message_iter_append_basic(&struct_iter, DBUS_TYPE_STRING, &descr))
        {
            goto fail_close2_unref;
        }

        /* Close the struct. */
        if (!dbus_message_iter_close_container(&array_iter, &struct_iter))
        {
            goto fail_close_unref;
        }
    }
    else
    {
        /* Append enum values to the array. */
        for (index = 0 ; index < param_ptr->constraint.enumeration.count ; index++)
        {
            descr = param_ptr->constraint.enumeration.possible_values_array[index].short_desc;

            jack_controller_jack_to_dbus_variant(param_ptr->type, &param_ptr->constraint.enumeration.possible_values_array[index].value, &value);

            /* Open the struct. */
            if (!dbus_message_iter_open_container(&array_iter, DBUS_TYPE_STRUCT, NULL, &struct_iter))
            {
                goto fail_close_unref;
            }

            if (!jack_dbus_message_append_variant(
                    &struct_iter,
                    PARAM_TYPE_JACK_TO_DBUS(param_ptr->type),
                    PARAM_TYPE_JACK_TO_DBUS_SIGNATURE(param_ptr->type),
                    &value))
            {
                goto fail_close2_unref;
            }

            if (!dbus_message_iter_append_basic(&struct_iter, DBUS_TYPE_STRING, &descr))
            {
                goto fail_close2_unref;
            }

            /* Close the struct. */
            if (!dbus_message_iter_close_container(&array_iter, &struct_iter))
            {
                goto fail_close_unref;
            }
        }
    }

close:
    /* Close the array. */
    if (!dbus_message_iter_close_container(&iter, &array_iter))
    {
        goto fail_unref;
    }

    return;

fail_close2_unref:
    dbus_message_iter_close_container(&array_iter, &struct_iter);

fail_close_unref:
    dbus_message_iter_close_container(&iter, &array_iter);

fail_unref:
    dbus_message_unref(call->reply);
    call->reply = NULL;

fail:
    jack_error ("Ran out of memory trying to construct method return");
}

static void
jack_controller_dbus_get_parameter_value(
    struct jack_dbus_method_call * call)
{
    const char * address[PARAM_ADDRESS_SIZE];
    const struct jack_parameter * param_ptr;
    union jackctl_parameter_value jackctl_value;
    union jackctl_parameter_value jackctl_default_value;
    message_arg_t value;
    message_arg_t default_value;

    //jack_info("jack_controller_dbus_get_parameter_value() called");

    if (!jack_controller_dbus_get_parameter_address(call, address))
    {
        /* The method call had invalid arguments meaning that
         * jack_controller_dbus_get_parameter_address() has
         * constructed an error for us. */
        return;
    }

    //jack_info("address is '%s':'%s':'%s'", address[0], address[1], address[2]);

    param_ptr = jack_params_get_parameter(controller_ptr->params, address);
    if (param_ptr == NULL)
    {
        jack_dbus_error(
            call,
            JACK_DBUS_ERROR_INVALID_ARGS,
            "Invalid container address '%s':'%s':'%s' supplied to method '%s'.",
            address[0],
            address[1],
            address[2],
            call->method_name);
        return;
    }

    jackctl_default_value = param_ptr->vtable.get_default_value(param_ptr->obj);
    jackctl_value = param_ptr->vtable.get_value(param_ptr->obj);

    jack_controller_jack_to_dbus_variant(param_ptr->type, &jackctl_value, &value);
    jack_controller_jack_to_dbus_variant(param_ptr->type, &jackctl_default_value, &default_value);

    /* Construct the reply. */
    jack_dbus_construct_method_return_parameter(
        call,
        (dbus_bool_t)(param_ptr->vtable.is_set(param_ptr->obj) ? TRUE : FALSE),
        PARAM_TYPE_JACK_TO_DBUS(param_ptr->type),
        PARAM_TYPE_JACK_TO_DBUS_SIGNATURE(param_ptr->type),
        default_value,
        value);
}

static
void
jack_controller_dbus_set_parameter_value(
    struct jack_dbus_method_call * call)
{
    const char * address[PARAM_ADDRESS_SIZE];
    const struct jack_parameter * param_ptr;
    DBusMessageIter iter;
    DBusMessageIter variant_iter;
    message_arg_t arg;
    int arg_type;
    union jackctl_parameter_value value;

    //jack_info("jack_controller_dbus_set_parameter_value() called");

    if (!jack_controller_dbus_get_parameter_address_ex(call, &iter, address))
    {
        /* The method call had invalid arguments meaning that
         * jack_controller_dbus_get_parameter_address() has
         * constructed an error for us. */
        return;
    }

    //jack_info("address is '%s':'%s':'%s'", address[0], address[1], address[2]);

    param_ptr = jack_params_get_parameter(controller_ptr->params, address);
    if (param_ptr == NULL)
    {
        jack_dbus_error(
            call,
            JACK_DBUS_ERROR_INVALID_ARGS,
            "Invalid container address '%s':'%s':'%s' supplied to method '%s'.",
            address[0],
            address[1],
            address[2],
            call->method_name);
        return;
    }

    dbus_message_iter_next(&iter);

    if (dbus_message_iter_has_next(&iter))
    {
        jack_dbus_error(
            call,
            JACK_DBUS_ERROR_INVALID_ARGS,
            "Invalid arguments to method '%s'. Too many arguments.",
            call->method_name);
        return;
    }

    if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
    {
        jack_dbus_error(
            call,
            JACK_DBUS_ERROR_INVALID_ARGS,
            "Invalid arguments to method '%s'. Value to set must be variant.",
            call->method_name);
        return;
    }

    dbus_message_iter_recurse (&iter, &variant_iter);
    dbus_message_iter_get_basic(&variant_iter, &arg);
    arg_type = dbus_message_iter_get_arg_type(&variant_iter);

    //jack_info("argument of type '%c'", arg_type);

    if (PARAM_TYPE_JACK_TO_DBUS(param_ptr->type) != arg_type)
    {
        jack_dbus_error(
            call,
            JACK_DBUS_ERROR_INVALID_ARGS,
            "Parameter value type mismatch: was expecting '%c', got '%c'",
            (char)PARAM_TYPE_JACK_TO_DBUS(param_ptr->type),
            (char)arg_type);
        return;
    }

    if (!jack_controller_dbus_to_jack_variant(
            arg_type,
            &arg,
            &value))
    {
        jack_dbus_error(
            call,
            JACK_DBUS_ERROR_INVALID_ARGS,
            "Cannot convert parameter value");
        return;
    }

    param_ptr->vtable.set_value(param_ptr->obj, &value);

    jack_controller_pending_save(controller_ptr);

    jack_dbus_construct_method_return_empty(call);

}

static
void
jack_controller_dbus_reset_parameter_value(
    struct jack_dbus_method_call * call)
{
    const char * address[PARAM_ADDRESS_SIZE];
    const struct jack_parameter * param_ptr;

    //jack_info("jack_controller_dbus_reset_parameter_value() called");

    if (!jack_controller_dbus_get_parameter_address(call, address))
    {
        /* The method call had invalid arguments meaning that
         * jack_controller_dbus_get_parameter_address() has
         * constructed an error for us. */
        return;
    }

    //jack_info("address is '%s':'%s':'%s'", address[0], address[1], address[2]);

    param_ptr = jack_params_get_parameter(controller_ptr->params, address);
    if (param_ptr == NULL)
    {
        jack_dbus_error(
            call,
            JACK_DBUS_ERROR_INVALID_ARGS,
            "Invalid container address '%s':'%s':'%s' supplied to method '%s'.",
            address[0],
            address[1],
            address[2],
            call->method_name);
        return;
    }

    param_ptr->vtable.reset(param_ptr->obj);

    jack_controller_pending_save(controller_ptr);

    jack_dbus_construct_method_return_empty(call);
}

#undef controller_ptr

JACK_DBUS_METHOD_ARGUMENTS_BEGIN_EX(ReadContainer, "Get names of child parameters or containers")
    JACK_DBUS_METHOD_ARGUMENT_IN("parent", "as", "Address of parent container")
    JACK_DBUS_METHOD_ARGUMENT_OUT("leaf", "b", "Whether children are parameters (true) or containers (false)")
    JACK_DBUS_METHOD_ARGUMENT_OUT("children", "as", "Array of child names")
JACK_DBUS_METHOD_ARGUMENTS_END

JACK_DBUS_METHOD_ARGUMENTS_BEGIN_EX(GetParametersInfo, "Retrieve info about parameters")
    JACK_DBUS_METHOD_ARGUMENT_IN("parent", "as", "Address of parameters parent")
    JACK_DBUS_METHOD_ARGUMENT_OUT("parameter_info_array", "a(ysss)", "Array of parameter info structs. Each info struct contains: type char, name, short and long description")
JACK_DBUS_METHOD_ARGUMENTS_END

JACK_DBUS_METHOD_ARGUMENTS_BEGIN_EX(GetParameterInfo, "Retrieve info about parameter")
    JACK_DBUS_METHOD_ARGUMENT_IN("parameter", "as", "Address of parameter")
    JACK_DBUS_METHOD_ARGUMENT_OUT("parameter_info", "(ysss)", "Parameter info struct that contains: type char, name, short and long description")
JACK_DBUS_METHOD_ARGUMENTS_END

JACK_DBUS_METHOD_ARGUMENTS_BEGIN_EX(GetParameterConstraint, "Get constraint of parameter")
    JACK_DBUS_METHOD_ARGUMENT_IN("parameter", "as", "Address of parameter")
    JACK_DBUS_METHOD_ARGUMENT_OUT("is_range", "b", "Whether constrinat is a range. If so, values parameter will contain two values, min and max")
    JACK_DBUS_METHOD_ARGUMENT_OUT("is_strict", "b", "Whether enum constraint is strict. I.e. value not listed in values array will not work")
    JACK_DBUS_METHOD_ARGUMENT_OUT("is_fake_value", "b", "Whether enum values are fake. I.e. have no user meaningful meaning")
    JACK_DBUS_METHOD_ARGUMENT_OUT("values", "a(vs)", "Values. If there is no constraint, this array will be empty. For range constraint there will be two values, min and max. For enum constraint there will be 2 or more values.")
JACK_DBUS_METHOD_ARGUMENTS_END

JACK_DBUS_METHOD_ARGUMENTS_BEGIN_EX(GetParameterValue, "Get value of parameter")
    JACK_DBUS_METHOD_ARGUMENT_IN("parameter", "as", "Address of parameter")
    JACK_DBUS_METHOD_ARGUMENT_OUT("is_set", "b", "Whether parameter is set or its default value is used")
    JACK_DBUS_METHOD_ARGUMENT_OUT("default", "v", "Default value of parameter")
    JACK_DBUS_METHOD_ARGUMENT_OUT("value", "v", "Actual value of parameter")
JACK_DBUS_METHOD_ARGUMENTS_END

JACK_DBUS_METHOD_ARGUMENTS_BEGIN_EX(SetParameterValue, "Set value of parameter")
    JACK_DBUS_METHOD_ARGUMENT_IN("parameter", "as", "Address of parameter")
    JACK_DBUS_METHOD_ARGUMENT_IN("value", "v", "New value for parameter")
JACK_DBUS_METHOD_ARGUMENTS_END

JACK_DBUS_METHOD_ARGUMENTS_BEGIN_EX(ResetParameterValue, "Reset parameter to default value")
    JACK_DBUS_METHOD_ARGUMENT_IN("parameter", "as", "Address of parameter")
JACK_DBUS_METHOD_ARGUMENTS_END

JACK_DBUS_METHODS_BEGIN
    JACK_DBUS_METHOD_DESCRIBE(ReadContainer, jack_controller_dbus_read_container)
    JACK_DBUS_METHOD_DESCRIBE(GetParametersInfo, jack_controller_dbus_get_parameters_info)
    JACK_DBUS_METHOD_DESCRIBE(GetParameterInfo, jack_controller_dbus_get_parameter_info)
    JACK_DBUS_METHOD_DESCRIBE(GetParameterConstraint, jack_controller_dbus_get_parameter_constraint)
    JACK_DBUS_METHOD_DESCRIBE(GetParameterValue, jack_controller_dbus_get_parameter_value)
    JACK_DBUS_METHOD_DESCRIBE(SetParameterValue, jack_controller_dbus_set_parameter_value)
    JACK_DBUS_METHOD_DESCRIBE(ResetParameterValue, jack_controller_dbus_reset_parameter_value)
JACK_DBUS_METHODS_END

/*
 * Parameter addresses:
 *
 * "engine"
 * "engine", "driver"
 * "engine", "realtime"
 * "engine", ...more engine parameters
 *
 * "driver", "device"
 * "driver", ...more driver parameters
 *
 * "drivers", "alsa", "device"
 * "drivers", "alsa", ...more alsa driver parameters
 *
 * "drivers", ...more drivers
 *
 * "internals", "netmanager", "multicast_ip"
 * "internals", "netmanager", ...more netmanager parameters
 *
 * "internals", ...more internals
 *
 */

JACK_DBUS_IFACE_BEGIN(g_jack_controller_iface_configure, "org.jackaudio.Configure")
    JACK_DBUS_IFACE_EXPOSE_METHODS
JACK_DBUS_IFACE_END