summaryrefslogtreecommitdiffstats
path: root/src/audiomanager_proxy.c
blob: d1ace2ced2dfbd6ea4d8a78469a4d39eacb47c90 (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
/*
 * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <glib.h>
#include <assert.h>
#include <pthread.h>
#include "audiomanager_proxy.h"
#include "sm-helper.h"
#include "dbus/audio_manager_interface.h"

static AudiomanagerCommandinterface *am_cmd_bus;
static AudiomanagerRoutinginterface *am_route_bus;
static AudiomanagerRoutingSoundmanager *sm_adapter;
static AudiomanagerRoutingSoundmanagerIface* sm_itf;
static GDBusConnection* system_conn = NULL;

static am_event _am_event = {0};
static am_instruction _am_instruction = {0};
static void *dbus_event_loop_run(void *args);

/*
********** Callback Function invoked by Audio Manager Command Interface**********
*/
static void _on_new_main_connection(AudiomanagerCommandinterface* interface,
    GVariant* mainConnection)
{
    AFB_DEBUG("%s is called",__FUNCTION__);

    guint16 mcid, srcid, sinkid;
    gint16 delay, constate;
    g_variant_get(
        mainConnection,"(qqqnn)", &mcid, &srcid, &sinkid, &delay, &constate);

    _am_event.on_new_main_connection(mcid, srcid, sinkid, delay, constate);
}

static void _on_removed_main_connection(
    AudiomanagerCommandinterface* interface, guint16 mainConnectionID)
{
    AFB_DEBUG("%s is called",__FUNCTION__);

    _am_event.on_removed_main_connection(mainConnectionID);
}

static void _on_main_connection_state_changed(
    AudiomanagerCommandinterface* interface, guint16 connectionID, gint16 connectionState)
{
    AFB_DEBUG("%s is called",__FUNCTION__);
    _am_event.on_main_connection_state_changed(connectionID, connectionState);
}

static void _on_volume_changed(
            AudiomanagerCommandinterface* interface, guint16 sinkID, gint16 volume)
{
    AFB_DEBUG("%s is called",__FUNCTION__);
    _am_event.on_volume_changed(sinkID, volume);
}

static void _on_sink_mute_state_changed(
            AudiomanagerCommandinterface* interface, guint16 sinkID, gint16 mute)
{
    AFB_DEBUG("%s is called",__FUNCTION__);
    _am_event.on_sink_mute_state_changed(sinkID, mute);
}

/*
********** Callback Function invoked by Audio Manager Routing Interface**********
*/
static void _on_set_routing_ready(
            AudiomanagerRoutinginterface* interface)
{
    AFB_DEBUG("%s is called",__FUNCTION__);
    _am_event.on_set_routing_ready();
}

static void _on_set_routing_rundown(
            AudiomanagerRoutinginterface* interface)
{
    AFB_DEBUG("%s is called",__FUNCTION__);
    _am_event.on_set_routing_rundown();
}

static ErrorCode check_send_error(GError *err, guint16 result){
    if(err != NULL){
        return UNABLE_SEND;
    }
    return result;
}

static gboolean _on_async_abort(
    AudiomanagerRoutingSoundmanager *object,
    GDBusMethodInvocation *invocation,
    guint16 arg_handle)
{
    AFB_DEBUG( "%s called", __FUNCTION__);
    _am_instruction.on_async_abort((int)arg_handle);
    return TRUE;
}

static gboolean _on_async_connect(
    AudiomanagerRoutingSoundmanager *object,
    GDBusMethodInvocation *invocation,
    guint16 arg_handle,
    guint16 arg_connectionID,
    guint16 arg_sourceID,
    guint16 arg_sinkID,
    gint arg_connectionFormat)
{
    AFB_DEBUG( "%s called", __FUNCTION__);
    int handle = (int)arg_handle;
    int connection = (int)arg_connectionID;
    int source = (int)arg_sourceID;
    int sink   = (int)arg_sinkID;
    int connection_format = (int)arg_connectionFormat;
    _am_instruction.on_async_connect(handle, connection,
        source, sink, connection_format);
    int ack_ok = 0;
    ErrorCode ec = am_proxy_ack_connect(handle, connection, ack_ok);
    if(ec == UNABLE_SEND)
    {
        AFB_ERROR( "Can't send ackConnect", __FUNCTION__);
        return FALSE;
    }
    return TRUE;
}

static gboolean _on_async_disconnect(
    AudiomanagerRoutingSoundmanager *object,
    GDBusMethodInvocation *invocation,
    guint16 arg_handle,
    guint16 arg_connectionID)
{
    AFB_DEBUG( "%s called", __FUNCTION__);
    int handle = (int)arg_handle;
    int connection = (int)arg_connectionID;
    _am_instruction.on_async_disconnect(handle, connection);
    GError* err = NULL;
    int ack_ok = 0;

    ErrorCode ec = am_proxy_ack_disconnect(handle, connection, ack_ok);
    if(ec == UNABLE_SEND)
    {
        AFB_ERROR( "Can't send ack to sound manager adapter %s", __FUNCTION__);
        return FALSE;
    }
    return TRUE;
}

static gboolean _on_async_set_sink_volume(
    AudiomanagerRoutingSoundmanager *object,
    GDBusMethodInvocation *invocation,
    guint16 arg_handle,
    guint16 arg_sinkID,
    gint16 arg_volume,
    gint16 arg_ramp,
    guint16 arg_time)
{
    AFB_DEBUG( "%s called", __FUNCTION__);
    int handle = (int)arg_handle;
    int sink   = (int)arg_sinkID;
    int volume = (int)arg_volume;
    int ramp = (int)arg_ramp;
    int time = (int)arg_time;
    GError* err = NULL;
    int ack_ok = 0;
    ErrorCode ec = audiomanager_routinginterface_call_ack_set_sink_volume_sync(
        am_route_bus,
        arg_handle,
        arg_volume,
        ack_ok, NULL, &err);
    if(ec == UNABLE_SEND);{
        AFB_ERROR( "Can't send ack to sound manager adapter %s", __FUNCTION__);
        return FALSE;
    }
    return TRUE;
}

static gboolean _on_async_set_source_state(
    AudiomanagerRoutingSoundmanager *object,
    GDBusMethodInvocation *invocation,
    guint16 arg_handle,
    guint16 arg_sourceID,
    gint arg_sourceState)
{
    int handle = (int)arg_handle;
    int source = (int)arg_sourceID;
    int source_state   = (int)arg_sourceState;
    AFB_DEBUG( "%s called", __FUNCTION__);
    _am_instruction.on_async_set_source_state(handle, source, source_state);
    return TRUE;
}

/*
********** API which calls audio manager API **********
*/
ErrorCode am_proxy_connect(int source, int sink, int *main_connection_id){
    if(is_range_over_guint16(source) == OUT_RANGE || is_range_over_guint16(sink) == OUT_RANGE){
        return OUT_RANGE;
    }
    AFB_DEBUG("Call %s: source:%d sink:%d", __FUNCTION__, source, sink);

    guint16 connection_id = 0, ret = 0;
    ErrorCode ec;
    GError *err = NULL;

    assert(am_cmd_bus != NULL);

    audiomanager_commandinterface_call_connect_sync(
        am_cmd_bus,
        (guint16)source, (guint16)sink,
        &ret, &connection_id,
        NULL, &err);
    *main_connection_id = (int)connection_id;
    ec = check_send_error(err, ret);
    return ec;
}

ErrorCode am_proxy_disconnect(int main_connection_id){
    AFB_DEBUG("Call %s: mainConnectionID:%d", __FUNCTION__, main_connection_id);
    if(is_range_over_guint16(main_connection_id) == OUT_RANGE){
        return OUT_RANGE;
    }
    guint16 ret = 0;
    ErrorCode ec;
    GError *err = NULL;

    assert(am_cmd_bus != NULL);
    audiomanager_commandinterface_call_disconnect_sync(
        am_cmd_bus,
        (guint16)main_connection_id,
        &ret,
        NULL, &err);
    ec = check_send_error(err, ret);
    return ec;
}

ErrorCode am_proxy_set_volume(int sink, int volume){
    AFB_DEBUG("Call %s: sink:%d volume:%d", __FUNCTION__, sink, volume);
    if(is_range_over_guint16(sink) == OUT_RANGE && is_range_over_gint16(volume) == OUT_RANGE){
        return OUT_RANGE;
    }
    guint16 ret = 0;
    ErrorCode ec;
    GError *err = NULL;

    assert(am_cmd_bus != NULL);
    audiomanager_commandinterface_call_set_volume_sync(
        am_cmd_bus,
        (guint16)sink,
        (gint16)volume,
        &ret, NULL, &err);
    ec = check_send_error(err, ret);
    return ec;
}

ErrorCode am_proxy_volume_step(int sink, int volume){
    AFB_DEBUG("Call %s: sink:%d volume:%d", __FUNCTION__, sink, volume);
    if(is_range_over_guint16(sink) == OUT_RANGE && is_range_over_gint16(volume) == OUT_RANGE){
        return OUT_RANGE;
    }
    guint16 ret = 0;
    ErrorCode ec;
    GError *err = NULL;

    assert(am_cmd_bus != NULL);
    audiomanager_commandinterface_call_volume_step_sync(
        am_cmd_bus,
        (guint16)sink,
        (gint16)volume,
        &ret, NULL, &err);
    ec = check_send_error(err, ret);
    return ec;
}

ErrorCode am_proxy_set_sink_mute_state(int sink, int mute_state){
    AFB_DEBUG("Call %s: sink:%d mute_state:%d", __FUNCTION__, sink, mute_state);
    if(is_range_over_guint16(sink) == OUT_RANGE || is_range_over_gint16(mute_state) == OUT_RANGE){
        return OUT_RANGE;
    }
    guint16 ret = 0;
    ErrorCode ec;
    GError *err = NULL;

    assert(am_cmd_bus != NULL);
    audiomanager_commandinterface_call_set_sink_mute_state_sync(
        am_cmd_bus,
        (guint16)sink, (gint16)mute_state,
        &ret, NULL, &err);
    ec = check_send_error(err, ret);
    return ec;
}

ErrorCode am_proxy_get_list_main_connections(GVariant** connection_list){
    guint16 ret = 0;
    ErrorCode ec;
    GError *err = NULL;

    AFB_DEBUG("Call %s", __FUNCTION__);
    assert(am_cmd_bus != NULL);
    audiomanager_commandinterface_call_get_list_main_connections_sync(
        am_cmd_bus,
        &ret,
        connection_list,
        NULL,
        &err
    );
    ec = check_send_error(err, ret);
    return ec;
}

ErrorCode am_proxy_get_list_main_sources(GVariant** source_list){
    guint16 ret = 0;
    ErrorCode ec;
    GError *err = NULL;

    AFB_DEBUG("Call %s", __FUNCTION__);
    assert(am_cmd_bus != NULL);
    audiomanager_commandinterface_call_get_list_main_sources_sync(
        am_cmd_bus,
        &ret,
        source_list,
        NULL,
        &err
    );
    ec = check_send_error(err, ret);
    return ec;
}

ErrorCode am_proxy_get_list_main_sinks(GVariant** sink_list){
    guint16 ret = 0;
    ErrorCode ec;
    GError *err = NULL;

    AFB_DEBUG("Call %s", __FUNCTION__);
    assert(am_cmd_bus != NULL);
    audiomanager_commandinterface_call_get_list_main_sinks_sync(
        am_cmd_bus,
        &ret,
        sink_list,
        NULL,
        &err
    );
    ec = check_send_error(err, ret);
    return ec;
}

ErrorCode am_proxy_ack_connect(int handle, int connection_id, int usr_err){
    AFB_DEBUG("Call %s handle:%d connectionID:%d error:%d", __FUNCTION__, handle, connection_id, usr_err);
    if(is_range_over_guint16(handle) == OUT_RANGE ||
       is_range_over_guint16(connection_id) == OUT_RANGE ||
       is_range_over_guint16(usr_err) == OUT_RANGE){
        return OUT_RANGE;
    }

    assert(am_route_bus != NULL);
    GError *err = NULL;

    audiomanager_routinginterface_call_ack_connect_sync(
        am_route_bus,
        (guint16)handle,
        (guint16)connection_id,
        (guint16)usr_err, NULL, &err);
    return OK;
}

ErrorCode am_proxy_ack_disconnect(int handle, int connection_id, int usr_err){
    AFB_DEBUG("Call %s handle:%d connectionID:%d error:%d", __FUNCTION__, handle, connection_id, usr_err);
    if(is_range_over_guint16(handle) == OUT_RANGE ||
       is_range_over_guint16(connection_id) == OUT_RANGE ||
       is_range_over_guint16(usr_err) == OUT_RANGE){
        return OUT_RANGE;
    }
    assert(am_route_bus != NULL);
    GError *err = NULL;
    audiomanager_routinginterface_call_ack_disconnect_sync(
        am_route_bus,
        (guint16)handle,
        (guint16)connection_id,
        (guint16)usr_err,NULL, &err);
    return OK;
}

ErrorCode am_proxy_ack_set_source_state(int handle, int usr_err){
    AFB_DEBUG("Call %s handle:%d error:%d", __FUNCTION__, handle, usr_err);
    if(is_range_over_guint16(handle) == OUT_RANGE ||
       is_range_over_guint16(usr_err) == OUT_RANGE){
        return OUT_RANGE;
    }

    assert(am_route_bus != NULL);
    GError *err = NULL;
    audiomanager_routinginterface_call_ack_set_source_state_sync(
        am_route_bus,
        (guint16)handle,
        (guint16)usr_err,
        NULL, &err);
    return OK;
}

ErrorCode am_proxy_register_source(GVariant *source_data, int *source){
    AFB_DEBUG("Call %s", __FUNCTION__);
    assert(am_route_bus != NULL);
    guint16 ret = 0, g_source = 0;
    ErrorCode ec;
    GError *err = NULL;

    audiomanager_routinginterface_call_register_source_sync(
        am_route_bus,
        source_data,
        &g_source,
        &ret,
        NULL, &err);

    *source = (int)g_source;
    ec = check_send_error(err, ret);
    return ec;
}

ErrorCode am_proxy_deregister_source(int source){
    AFB_DEBUG("Call %s source:%d", __FUNCTION__, source);
    if(is_range_over_guint16(source) == OUT_RANGE){
        return OUT_RANGE;
    }
    guint16 ret = 0;
    ErrorCode ec;
    GError *err = NULL;

    assert(am_route_bus != NULL);
    audiomanager_routinginterface_call_deregister_source_sync(
        am_route_bus,
        (guint16)source,
        &ret,
        NULL, &err
    );
    ec = check_send_error(err, ret);
    return ec;
}

ErrorCode am_proxy_register_domain(GVariant* domain_data, int *domain){
    guint16 ret = 0, g_domain = 0;
    ErrorCode ec;
    GError *err = NULL;

    assert(am_route_bus != NULL);
    assert(domain_data);
    AFB_DEBUG("Call %s", __FUNCTION__);
    audiomanager_routinginterface_call_register_domain_sync(
        am_route_bus,
        domain_data,
        SOUND_MANAGER_BUS_NAME,
        SOUND_MANAGER_PATH,
        SOUND_MANAGER_RETURN_INTERFACE,
        &g_domain, &ret,
        NULL, &err);

    *domain = (int)g_domain;
    ec = check_send_error(err, ret);
    return ec;
}

GVariant* create_domain_data(struct domain_data* domain){
    /*
    *  This function doesn't check the range of guint32 or gint16
    *  Soundmanager must keep the input data is between the size of type.
    *  This function is considered to be only used by soundmanager.
    */
    GVariantBuilder builder;
    g_variant_builder_init (&builder, G_VARIANT_TYPE ("(qsssbbn)"));
    g_variant_builder_add (&builder, "q", (guint16)domain->domainID);
    g_variant_builder_add (&builder, "s", domain->name);
    g_variant_builder_add (&builder, "s", domain->busname);
    g_variant_builder_add (&builder, "s", domain->nodename);
    g_variant_builder_add (&builder, "b", (gboolean)domain->early);
    g_variant_builder_add (&builder, "b", (gboolean)domain->complete);
    g_variant_builder_add (&builder, "n", (gint16)domain->state);
    AFB_DEBUG("create domainData");
    return g_variant_builder_end (&builder);
}

GVariant* create_source_data(int sourceID, int domainID, const char* appname, int sourceClassID,
    int sourceState, int volume, bool visible, struct availability_s availables,
    int interrupt,  struct sound_property_s soundPropertyList, int connectionFormatList,
    struct main_sound_property_s mainPropertyList, struct notification_config_s NConfRouting,
    struct notification_config_s NConfCommand)
{
    GVariantBuilder builder;
    if(is_range_over_guint16(sourceID) == OUT_RANGE ||
        is_range_over_guint16(domainID) == OUT_RANGE ||
        is_range_over_gint16(volume) == OUT_RANGE ||
        is_range_over_guint16(interrupt) == OUT_RANGE ||
        is_range_over_gint16(soundPropertyList.value) == OUT_RANGE ||
        is_range_over_gint16(mainPropertyList.value) == OUT_RANGE ||
        is_range_over_gint16(NConfRouting.parameter) == OUT_RANGE ||
        is_range_over_gint16(NConfCommand.parameter) == OUT_RANGE)
    {
        return NULL;
    }
    g_variant_builder_init (&builder, G_VARIANT_TYPE ("(qqsqinb(ii)qa(in)aia(in)a(iin)a(iin))"));
    g_variant_builder_add (&builder, "q", (guint16)sourceID);
    g_variant_builder_add (&builder, "q", (guint16)domainID);
    g_variant_builder_add (&builder, "s", appname);
    g_variant_builder_add (&builder, "q", (guint16)sourceClassID);
    g_variant_builder_add (&builder, "i", (gint32)sourceState);
    g_variant_builder_add (&builder, "n", (gint16)volume);
    g_variant_builder_add (&builder, "b", (gboolean)visible);
    g_variant_builder_add (&builder, "(ii)", (gint32)availables.availability, (gint32)availables.avalilable_reason);
    g_variant_builder_add (&builder, "q", (guint16)interrupt);

    g_variant_builder_open(&builder, G_VARIANT_TYPE("a(in)"));
    g_variant_builder_open(&builder, G_VARIANT_TYPE("(in)"));
    g_variant_builder_add (&builder, "i", (gint32)soundPropertyList.type);
    g_variant_builder_add (&builder, "n", (gint16)soundPropertyList.value);
    g_variant_builder_close(&builder);
    g_variant_builder_close (&builder);

    g_variant_builder_open(&builder, G_VARIANT_TYPE("ai"));
    g_variant_builder_add (&builder, "i", (gint32)connectionFormatList);
    g_variant_builder_close (&builder);

    g_variant_builder_open(&builder, G_VARIANT_TYPE("a(in)"));
    g_variant_builder_open(&builder, G_VARIANT_TYPE("(in)"));
    g_variant_builder_add (&builder, "i", (gint32)mainPropertyList.type);
    g_variant_builder_add (&builder, "n", (gint16)mainPropertyList.value);
    g_variant_builder_close (&builder);
    g_variant_builder_close(&builder);

    g_variant_builder_open(&builder, G_VARIANT_TYPE("a(iin)"));
    g_variant_builder_open(&builder, G_VARIANT_TYPE("(iin)"));
    g_variant_builder_add (&builder, "i", (gint32)NConfRouting.type);
    g_variant_builder_add (&builder, "i", (gint32)NConfRouting.status);
    g_variant_builder_add (&builder, "n", (gint16)NConfRouting.parameter);
    g_variant_builder_close(&builder);
    g_variant_builder_close (&builder);


    g_variant_builder_open(&builder, G_VARIANT_TYPE("a(iin)"));
    g_variant_builder_open(&builder, G_VARIANT_TYPE("(iin)"));
    g_variant_builder_add (&builder, "i", (gint32)NConfCommand.type);
    g_variant_builder_add (&builder, "i", (gint32)NConfCommand.status);
    g_variant_builder_add (&builder, "n", (gint16)NConfCommand.parameter);
    g_variant_builder_close(&builder);
    g_variant_builder_close (&builder);

    AFB_DEBUG("create sourceData");
    return g_variant_builder_end (&builder);
}

static void *dbus_event_loop_run(void *args)
{
    GMainLoop* loop = g_main_loop_new(NULL, FALSE);
    g_main_loop_run(loop);
}

ErrorCode initialize_proxy(){
    if(am_cmd_bus || am_route_bus)
    {
        return NOT_INITIALIZED;
    }
    am_cmd_bus = audiomanager_commandinterface_proxy_new_for_bus_sync(
        G_BUS_TYPE_SYSTEM,
        G_DBUS_PROXY_FLAGS_NONE,
        AM_NAME,
        AM_CMD_PATH,
        NULL,
        NULL
        );
    if(!am_cmd_bus){
        AFB_ERROR("Failed to create command interface");
    }
    am_route_bus = audiomanager_routinginterface_proxy_new_for_bus_sync(
        G_BUS_TYPE_SYSTEM,
        G_DBUS_PROXY_FLAGS_NONE,
        AM_NAME,
        AM_ROUTE_PATH,
        NULL,
        NULL
        );
    if(!am_route_bus){
        AFB_ERROR("Failed to create routing interface");
    }
    return (am_cmd_bus && am_route_bus) ? OK : UNABLE_SEND;
}

void close_proxy(){
    if(am_cmd_bus) free(am_cmd_bus);
    if(am_route_bus) free(am_route_bus);
}

void set_event_callback(const am_event* callback){
    assert(am_cmd_bus != NULL);
    assert(am_route_bus != NULL);
    /* initialize signal from audio manager command interface */
    _am_event.on_main_connection_state_changed = callback->on_main_connection_state_changed;
    _am_event.on_new_main_connection = callback->on_new_main_connection;
    _am_event.on_removed_main_connection = callback->on_removed_main_connection;
    _am_event.on_set_routing_ready = callback->on_set_routing_ready;
    _am_event.on_set_routing_rundown = callback->on_set_routing_rundown;
    _am_event.on_sink_mute_state_changed = callback->on_sink_mute_state_changed;
    _am_event.on_volume_changed = callback->on_volume_changed;

    g_signal_connect(am_cmd_bus, "volume_changed",
        G_CALLBACK(_on_volume_changed),         NULL);
    g_signal_connect(am_cmd_bus, "new_main_connection",
        G_CALLBACK(_on_new_main_connection),    NULL);
    g_signal_connect(am_cmd_bus, "removed_main_connection",
        G_CALLBACK(_on_removed_main_connection),NULL);
    g_signal_connect(am_cmd_bus, "sink_mute_state_changed",
        G_CALLBACK(_on_sink_mute_state_changed),NULL);
    g_signal_connect(am_cmd_bus, "main_connection_state_changed",
        G_CALLBACK(_on_main_connection_state_changed), NULL);
    g_signal_connect(am_route_bus, "set_routing_ready",
        G_CALLBACK(_on_set_routing_ready),      NULL);
    g_signal_connect(am_route_bus, "set_routing_rundown",
        G_CALLBACK(_on_set_routing_rundown),    NULL);
}

static void on_bus_acquired(GDBusConnection *connection, const gchar *name, gpointer user_data){
    GError* error = NULL;
    AFB_DEBUG("%s is called", __FUNCTION__);

    sm_adapter = audiomanager_routing_soundmanager_skeleton_new();
    sm_itf = AUDIOMANAGER_ROUTING_SOUNDMANAGER_GET_IFACE(sm_adapter);

    /* initialize sound manager adapter */
    sm_itf->handle_async_abort            = _on_async_abort;
    sm_itf->handle_async_connect          = _on_async_connect;
    sm_itf->handle_async_disconnect       = _on_async_disconnect;
    sm_itf->handle_async_set_sink_volume  = _on_async_set_sink_volume;
    sm_itf->handle_async_set_source_state = _on_async_set_source_state;

    int sigret = g_signal_connect(sm_adapter, "handle-async-abort", G_CALLBACK(_on_async_abort),NULL);
    sigret     = g_signal_connect(sm_adapter, "handle-async-connect", G_CALLBACK(_on_async_connect),NULL);
    sigret     = g_signal_connect(sm_adapter, "handle-async-disconnect", G_CALLBACK(_on_async_disconnect),NULL);
    sigret     = g_signal_connect(sm_adapter, "handle-async-set-sink-volume", G_CALLBACK(_on_async_set_sink_volume),NULL);
    sigret     = g_signal_connect(sm_adapter, "handle-async-set-source-state", G_CALLBACK(_on_async_set_source_state),NULL);

    AFB_DEBUG("register %s", AUDIOMANAGER_ROUTING_SOUNDMANAGER_SKELETON(sm_adapter));

    gboolean rc = g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(sm_adapter), connection, SOUND_MANAGER_PATH, &error);
    AFB_DEBUG("export soundmanager path result: %d", rc);
    if (FALSE == rc)
    {
        AFB_ERROR( "failed to export");
        g_error_free(error);
        g_object_unref(connection);
    }
}

static void on_name_acquired(GDBusConnection *connection, const gchar *name, gpointer user_data){
    AFB_DEBUG("%s is called", __FUNCTION__);
}

static void on_name_lost(GDBusConnection *connection, const gchar *name, gpointer user_data){
    AFB_DEBUG("bus name is lost");
    // TODO: soundmanager should register current status?
}

ErrorCode open_soundmanager_interface(const am_instruction *callback){
    _am_instruction.on_async_abort            = callback->on_async_abort;
    _am_instruction.on_async_connect          = callback->on_async_connect;
    _am_instruction.on_async_disconnect       = callback->on_async_disconnect;
    _am_instruction.on_async_set_sink_volume  = callback->on_async_set_sink_volume;
    _am_instruction.on_async_set_source_state = callback->on_async_set_source_state;

    guint owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM, SOUND_MANAGER_BUS_NAME, G_BUS_NAME_OWNER_FLAGS_NONE,
        on_bus_acquired, on_name_acquired, on_name_lost, NULL/* (gpointer)callback */, NULL);
    // see https://developer.gnome.org/gio/stable/gio-Owning-Bus-Names.html#g-bus-own-name

    pthread_t thread_id;
    int ret = pthread_create(&thread_id, NULL, dbus_event_loop_run, NULL);
    if(ret != 0)
    {
        AFB_ERROR("Failed to create thread");
        g_bus_unown_name(owner_id);
        close_soundmanager_inerface();
        close_proxy();
        return NOT_INITIALIZED;
    }
    return OK;
}

void close_soundmanager_inerface(){
    g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(sm_adapter));
}