aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/lib/bluealsa/hal-bluealsa.c
blob: 502bfc6c27921515165083bec3fd65f43a433b36 (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
/*
 * Copyright (C) 2018 "IoT.bzh"
 * Author : Thierry Bultel <thierry.bultel@iot.bzh>
 *
 * 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.
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>

#include <wrap-json.h>

#include <errno.h>
#include <ctl-config.h>
#include <systemd/sd-event.h>
#include <bluealsa/bluealsa.h>

#include "hal-bluealsa.h"

#define HAL_BLUEALSA_PLUGIN_NAME "hal-bluealsa"

#define SCO_TALK_RATE 44100
#define SCO_TALK_FORMAT "S16_LE"

#define SCO_TALK_ZONE "sco_talk_zone"
#define SCO_CHANNEL_MONO "sco-mono"

/* forward static declarations */
static int halBlueAlsaTransportEventCB(sd_event_source* src, int fd, uint32_t revents, void* userData);
static int halBlueAlsaFetchTransports(bluealsa_watch * plugin);
static int halBlueAlsaRegisterAll(CtlPluginT* plugin);
static int halBlueAlsaRegister(CtlPluginT* plugin, const char * interface);

CTLP_CAPI_REGISTER(HAL_BLUEALSA_PLUGIN_NAME)


// Call at initialization time ('requires' are forbidden at this stage)
CTLP_ONLOAD(plugin, callbacks)
{
	AFB_API_NOTICE(plugin->api, "%s Plugin Registered correctly: uid='%s' 'info='%s'", HAL_BLUEALSA_PLUGIN_NAME, plugin->uid, plugin->info);
	return 0;
}


CTLP_INIT(plugin, callbacks)
{
	json_object *actionsToAdd = NULL;
	CtlConfigT *ctrlConfig;

	wrap_json_pack(&actionsToAdd, "{s:s s:s s:s}",
				      "uid", "init-bluealsa-plugin",
				      "info", "Init Bluez-Alsa hal plugin",
				      "action", "plugin://hal-bluealsa#init");

	if (!(ctrlConfig = (CtlConfigT *) afb_api_get_userdata(plugin->api))) {
		AFB_API_ERROR(plugin->api, "Can't get current hal controller config");
		goto fail;
	}

	int idx = 0;
	while (ctrlConfig->sections[idx].key && strcasecmp(ctrlConfig->sections[idx].key, "onload"))
		idx++;

	if (!ctrlConfig->sections[idx].key) {
		AFB_API_ERROR(plugin->api, "Wasn't able to add '%s' as a new onload, 'onload' section not found", json_object_get_string(actionsToAdd));
		goto fail;
	}

	if(AddActionsToSection(plugin->api, &ctrlConfig->sections[idx], actionsToAdd, 0)) {
		AFB_API_ERROR(plugin->api, "Wasn't able to add '%s' as a new onload to %s", json_object_get_string(actionsToAdd), ctrlConfig->sections[idx].uid);
		goto fail;
	}

	AFB_API_NOTICE(plugin->api, "Plugin initialization of %s plugin correctly done", HAL_BLUEALSA_PLUGIN_NAME);

	return 0;
fail:
	json_object_put(actionsToAdd);
	return -1;
}


// Call at controller onload time
CTLP_CAPI(init, source, argsJ, queryJ)
{
	AFB_API_NOTICE(source->api, "Controller onload event");

	CtlPluginT * plugin = source->plugin;

	hal_bluealsa_plugin_data_t * pluginData  = (hal_bluealsa_plugin_data_t*) calloc(1, sizeof(hal_bluealsa_plugin_data_t));
	int error;

	/*
	 * "params": {
	      "sco": {
	          "mic": "2CH-GENERIC-USB",
	          "zone": "full-stereo",
	          "delayms": 300
	      },
	      "a2dp": {
	          "zone": "full-stereo",
	          "delayms": 1000
	      }
	  }
	 */

	json_object * scoParamsJ = NULL;
	json_object * a2dpParamsJ = NULL;

	error = wrap_json_unpack(plugin->paramsJ, "{s?o,s?o !}",
				"sco", &scoParamsJ,
				"a2dp", &a2dpParamsJ);

	if (error) {
		AFB_API_ERROR(plugin->api, "%s: wrong parameters", __func__);
		goto fail;
	}

	if (scoParamsJ) {
		AFB_API_INFO(plugin->api, "%s: sco parameters: %s", __func__, json_object_get_string(scoParamsJ));
		error = wrap_json_unpack(scoParamsJ, "{s:s,s:s,s?i !}",
				"mic", &pluginData->sco.mic,
				"zone", &pluginData->sco.speaker,
				"delayms", &pluginData->sco.delayms);
		if (error) {
			AFB_API_ERROR(plugin->api, "%s: wrong sco parameters: err %s", __func__, wrap_json_get_error_string(error));
			goto fail;
		}
	}

	if (a2dpParamsJ) {
		AFB_API_INFO(plugin->api, "%s: a2dp parameters: %s", __func__, json_object_get_string(a2dpParamsJ));
		error = wrap_json_unpack(a2dpParamsJ, "{s:s,s?i !}",
				"zone", &pluginData->a2dp.zone,
				"delayms", &pluginData->a2dp.delayms);
		if (error) {
			AFB_API_ERROR(plugin->api, "%s: wrong a2dp parameters: err=%s", __func__, wrap_json_get_error_string(error));
			goto fail;
		}
	}

	halBlueAlsaTransportsInit(&pluginData->transport_list);

	setPluginContext(plugin, pluginData);

	if (halBlueAlsaRegisterAll(plugin) != 0)
		goto fail;

	return 0;
fail:
    return -1;
}


static int halBlueAlsaTransportEventCB(sd_event_source* src, int fd, uint32_t revents, void* userData) {

	bluealsa_watch * watch = (bluealsa_watch *)userData;
	CtlPluginT * plugin = watch->plugin;

	struct ba_msg_event event;
	ssize_t ret;

	AFB_API_DEBUG(plugin->api, "--- %s ----!", __func__);

	if ((revents & EPOLLIN) == 0)
		goto done;

	if (revents & EPOLLHUP) {
		AFB_API_INFO(plugin->api, "Lost connection with bluealsa on interface %s", watch->interface);
		sd_event_source_unref(src);
		close(fd);
		halBlueAlsaRegister(plugin, watch->interface);
		goto done;
	}

	while ((ret = recv(watch->fd, &event, sizeof(event), MSG_DONTWAIT)) == -1 && errno == EINTR)
		continue;

	if (ret != sizeof(event)) {
		AFB_API_ERROR(plugin->api, "Couldn't read event: %s", strerror(ret == -1 ? errno : EBADMSG));
		goto done;
	}

	halBlueAlsaFetchTransports(watch);

done:
	return 0;
}


/*
 *
 * The following function builds that kind of json data and send it to smixer
 *
 * A2DP Case:
 *
 *  {
    "uid":"a2dp_F6:32:15:2A:80:70",
    "captures":
     {
       "uid":"a2dp_listen_capture",
       "pcmplug_params" : "bluealsa:HCI=hci0,DEV=F6:32:15:2A:80:70,PROFILE=a2dp",
       "source" : {
       	   "channels": [
       	       {
                   "uid": "a2dp-right",
                   "port": 0
               },
               {
                   "uid": "a2dp-left",
                   "port": 1
               }
            ]
        }
     },
    "streams":
    {
    	"uid" : "a2dp_listen_stream",
    	"source":"a2dp_listen_capture",
     	"zone": "full-stereo"
    }
   }
 *
 *-----------------------------------------------------------------------------------
 *
 * SCO Case:
 *
 *  {
    "uid":"sco_F6:32:15:2A:80:70",",
    "captures":
     {
       "uid":"sco_listen_capture",
       "pcmplug_params" : "bluealsa:HCI=hci0,DEV=F6:32:15:2A:80:70,PROFILE=sco",
       "source" : {
       	   "channels": [
       	       {
                   "uid": "sco-right",
                   "port": 0
               },
               {
                   "uid": "sco-left",
                   "port": 1
               }
            ]
        }
     },
	"playbacks" :
    {
    	"uid"="sco_talk_playback",
    	"pcmplug_params" : "bluealsa:HCI=hci0,DEV=F6:32:15:2A:80:70,PROFILE=sco",
    	"params": {
            "rate": 44100,
            "format": "S16_LE",
        },
    	"sink": {
    		"channels": [
            	{
            	"uid": "sco-mono"
            	}
            ]
    	}
    },
    "zones" : [
       {
           "uid": "sco_talk_zone",
           "sink": [
                {
                    "target": "sco-mono",
                    "channel": 0,
                    "volume": 0.5
                },
                {
                    "target": "sco-mono",
                    "channel": 1,
                    "volume": 0.5
                }
            ]
        }
    ] ,
    "streams": [
    	{
    		"uid" : "sco_listen_stream",
    		"source":"sco_listen_capture",
     		"zone": "full-stereo",
     		"delayms": 300
    	},
    	{
    		"uid" : "sco_talk_stream",
    		"source": "2CH-GENERIC-USB",
     		"zone": "sco_talk_zone",
     		"delayms": 300
    	}
    ]
   }
 *
 * */

static json_object* halBlueAlsaA2DPTransportChannels(const char * transportTypeS) {
	json_object* channelRightJ;
	json_object* channelLeftJ;
	json_object* channelsJ = json_object_new_array();
	char * channelRightS;
	char * channelLeftS;

	channelRightJ = json_object_new_object();
	if (asprintf(&channelRightS, "%s-right", transportTypeS) == -1)
		goto fail;

	json_object_object_add(channelRightJ, "uid", json_object_new_string(channelRightS));
	json_object_object_add(channelRightJ, "port", json_object_new_int(0));

	channelLeftJ = json_object_new_object();
	if (asprintf(&channelLeftS, "%s-left", transportTypeS) == -1)
		goto fail;

	json_object_object_add(channelLeftJ, "uid", json_object_new_string(channelLeftS));
	json_object_object_add(channelLeftJ, "port", json_object_new_int(1));

	json_object_array_add(channelsJ, channelRightJ);
	json_object_array_add(channelsJ, channelLeftJ);
fail:
	return channelsJ;
}


static json_object* halBlueAlsaSCOTransportChannels(const char * transportTypeS) {
	json_object* channelMonoJ;

	json_object* channelsJ = json_object_new_array();
	char * channelMonoS;

	channelMonoJ = json_object_new_object();
	if (asprintf(&channelMonoS, "%s-mono", transportTypeS) == -1)
		goto fail;

	json_object_object_add(channelMonoJ, "uid", json_object_new_string(channelMonoS));
	json_object_object_add(channelMonoJ, "port", json_object_new_int(0));

	json_object_array_add(channelsJ, channelMonoJ);

fail:
	return channelsJ;
}


static json_object* halBlueAlsaPcmPlugParams(const char * interface, const char * addr, const char * transportTypeS) {
	char * pcmplug_paramsS = NULL;
	if (asprintf(&pcmplug_paramsS, "bluealsa:HCI=%s,DEV=%s,PROFILE=%s", interface, addr, transportTypeS) == -1)
		goto fail;
	return json_object_new_string(pcmplug_paramsS);
fail:
	return NULL;
}


static json_object* halBlueAlsaListenCapture(
	const char * listenCaptureS,
	json_object * pcmplugParamsJ,
	json_object * sourceJ) {

	json_object * captureJ = json_object_new_object();

	json_object_object_add(captureJ, "uid", json_object_new_string(listenCaptureS));
	json_object_object_add(captureJ, "pcmplug_params", pcmplugParamsJ);
	json_object_object_add(captureJ, "source", sourceJ);

	return captureJ;
}

static json_object * halBlueAlsaTalkPlayback(
	const char * talkPlaybackS,
	json_object* pcmplugParamsJ,
	json_object* paramsJ,
	json_object* sinkJ ) {

	json_object * playbackJ = json_object_new_object();

	json_object_object_add(playbackJ, "uid", json_object_new_string(talkPlaybackS));
	json_object_object_add(playbackJ, "pcmplug_params", pcmplugParamsJ);
	json_object_object_add(playbackJ, "params", paramsJ);
	json_object_object_add(playbackJ, "sink", sinkJ);

	return playbackJ;
}

static json_object * halBlueAlsaScoTalkParamsJ() {
	json_object * paramsJ = json_object_new_object();

	json_object_object_add(paramsJ, "rate",   json_object_new_int(SCO_TALK_RATE));
	json_object_object_add(paramsJ, "format", json_object_new_string(SCO_TALK_FORMAT));
	return paramsJ;
}

static json_object * halBlueAlsaScoZone() {
	json_object * zoneJ = json_object_new_object();
	json_object * sinkJ = json_object_new_array();
	json_object_object_add(zoneJ, "uid", json_object_new_string(SCO_TALK_ZONE));

	json_object * channel1J = json_object_new_object();
	json_object_object_add(channel1J, "target" , json_object_new_string(SCO_CHANNEL_MONO));
	json_object_object_add(channel1J, "channel", json_object_new_int(0));
	json_object_object_add(channel1J, "volume", json_object_new_double(0.5));

	json_object * channel2J = json_object_new_object();
	json_object_object_add(channel2J, "target" , json_object_new_string(SCO_CHANNEL_MONO));
	json_object_object_add(channel2J, "channel", json_object_new_int(1));
	json_object_object_add(channel2J, "volume", json_object_new_double(0.5));

	json_object_array_add(sinkJ, channel1J);
	json_object_array_add(sinkJ, channel2J);

	json_object_object_add(zoneJ, "sink", sinkJ);

	return zoneJ;
}

static int halBlueAlsaAttachTransportStreams(bluealsa_transport_t * transport) {
	int ret = -1;
	const char* transportTypeS;

	struct ba_msg_transport * ba_transport = &transport->transport;
	const bluealsa_watch * watch = transport->watch;

	CtlPluginT* plugin = watch->plugin;
	hal_bluealsa_plugin_data_t * pluginData=getPluginContext(plugin);

	json_object* requestJ = NULL;
	json_object* returnJ = NULL;

	json_object* sourceJ = NULL;
	json_object* playbackJ = NULL;

	json_object* pcmplugParamsJ, *pcmplugParamsJ2 = NULL;

	json_object* streamsJ = NULL;
	json_object* streamJ = NULL;

	json_object* zonesJ = NULL;

	char * playbackZoneS;
	uint32_t delayms;

	if (ba_transport->type == BA_PCM_TYPE_SCO) {
		transportTypeS = "sco";
		delayms = pluginData->sco.delayms;
		playbackZoneS = pluginData->sco.speaker;
	}
	else if (ba_transport->type == BA_PCM_TYPE_A2DP) {
		transportTypeS = "a2dp";
		delayms = pluginData->a2dp.delayms;
		playbackZoneS = pluginData->a2dp.zone;
	} else {
		AFB_API_ERROR(plugin->api, "%s: unsupported transport type", __func__ );
		goto fail;
	}

	char * captureS = NULL;
	if (asprintf(&captureS, "%s_listen_capture", transportTypeS) == -1)
		goto fail;

	// for SCO only
	char * playbackS = NULL;
	if (asprintf(&playbackS, "%s_talk_playback", transportTypeS) == -1)
		goto fail;

	char * transactionUidS = NULL;
	char * streamS = NULL;

	char addr[18];
	ba2str(&ba_transport->addr, addr);

	requestJ = json_object_new_object();
	if (!requestJ)
		goto fail;

	if (asprintf(&transactionUidS, "%s_%s", transportTypeS, addr) == -1)
		goto fail;

	json_object_object_add(requestJ, "uid", json_object_new_string(transactionUidS));

	pcmplugParamsJ = halBlueAlsaPcmPlugParams(watch->interface, addr, transportTypeS);

	sourceJ = json_object_new_object();
	if (ba_transport->type == BA_PCM_TYPE_A2DP)
		json_object_object_add(sourceJ, "channels", halBlueAlsaA2DPTransportChannels(transportTypeS));
	else
		json_object_object_add(sourceJ, "channels", halBlueAlsaSCOTransportChannels(transportTypeS));

	json_object_object_add(requestJ, "captures", halBlueAlsaListenCapture(captureS, pcmplugParamsJ, sourceJ));

	if (ba_transport->type == BA_PCM_TYPE_SCO) {
		playbackJ = json_object_new_object();
		// that is a shame that deep copy in not available in the current json-c version
		pcmplugParamsJ2 = halBlueAlsaPcmPlugParams(watch->interface, addr, transportTypeS);
		json_object * paramsJ = halBlueAlsaScoTalkParamsJ();

		json_object_object_add(playbackJ, "channels", halBlueAlsaSCOTransportChannels(transportTypeS));
		json_object_object_add(requestJ, "playbacks", halBlueAlsaTalkPlayback(playbackS, pcmplugParamsJ2, paramsJ, playbackJ));

	}

	/* ZONES */

	if (ba_transport->type == BA_PCM_TYPE_SCO) {
		zonesJ = json_object_new_array();
		json_object_array_add(zonesJ, halBlueAlsaScoZone());
		json_object_object_add(requestJ, "zones", zonesJ);
	}

	/* STREAMS */

	/* Build the array of streams */
	streamsJ = json_object_new_array();

	streamJ = json_object_new_object();
	if (asprintf(&streamS, "%s_listen_stream", transportTypeS) == -1)
		goto fail;

	json_object_object_add(streamJ, "uid", json_object_new_string(streamS));
	json_object_object_add(streamJ, "source", json_object_new_string(captureS));
	json_object_object_add(streamJ, "zone", json_object_new_string(playbackZoneS));
	if (delayms != 0) {
		json_object_object_add(streamJ, "delayms", json_object_new_int(delayms));
	}

	json_object_array_add(streamsJ, streamJ);

	/* In case of SCO, to have full-duplex, we instantiate a stream for talk */
	if (ba_transport->type == BA_PCM_TYPE_SCO ) {

		streamJ = json_object_new_object();
		if (asprintf(&streamS, "%s_talk_stream", transportTypeS) == -1)
			goto fail;

		json_object_object_add(streamJ, "uid", json_object_new_string(streamS));
		json_object_object_add(streamJ, "source", json_object_new_string(pluginData->sco.mic));
		json_object_object_add(streamJ, "zone", json_object_new_string(SCO_TALK_ZONE));
		if (delayms != 0)
			json_object_object_add(streamJ, "delayms", json_object_new_int(delayms));
		json_object_array_add(streamsJ, streamJ);
	}

	json_object_object_add(requestJ, "streams", streamsJ);

	/* In softmixer, this will create a transaction verb (whose name is transactionUidS),
	 * will be used later to destroy all the created objects upon transport removal */

	if (afb_api_call_sync_legacy(plugin->api, SMIXER_API_NAME, "attach", requestJ, &returnJ)) {
		AFB_API_ERROR(plugin->api, "Error calling attach verb of mixer" );
		goto done;
	}

	transport->transactionUidS = transactionUidS;

	if (returnJ)
		json_object_put(returnJ);

	ret = 0;
	goto done;

fail:
	return -1;
done:
	AFB_API_DEBUG(plugin->api, "DONE.");
	return ret;
}


static int halBluezAlsaRemoveTransportStream(bluealsa_transport_t * transport) {

	CtlPluginT * plugin = transport->watch->plugin;
	json_object* requestJ = NULL;
	json_object* returnJ = NULL;

	AFB_API_INFO(plugin->api, "Call transaction detach verb %s", transport->transactionUidS);
	if (transport->transactionUidS == NULL)
		goto fail;

	requestJ = json_object_new_object();
	if (!requestJ)
		goto fail;

	json_object_object_add(requestJ, "action", json_object_new_string("remove"));

	if (afb_api_call_sync_legacy(plugin->api, SMIXER_API_NAME, transport->transactionUidS, requestJ, &returnJ)) {
		AFB_API_ERROR(plugin->api, "Error calling attach verb of mixer" );
		goto fail;
	}

	if (returnJ)
		json_object_put(returnJ);

	return 0;

fail:
	return -1;
}

static int halBlueAlsaFetchTransports(bluealsa_watch * watch) {
	ssize_t nbTransports;
	struct ba_msg_transport *transports;
	CtlPluginT * plugin = watch->plugin;

	hal_bluealsa_plugin_data_t * pluginData = (hal_bluealsa_plugin_data_t*)getPluginContext(plugin);
	bluealsa_transport_t * transport_list = &pluginData->transport_list;
	bluealsa_transport_t * transport = NULL;

	AFB_API_DEBUG(plugin->api, "Fetching available transports of interface %s", watch->interface);

	if ((nbTransports = bluealsa_get_transports(watch->fd, &transports)) == -1) {
		AFB_API_ERROR(plugin->api, "Couldn't get transports: %s", strerror(errno));
		goto done;
	}

	AFB_API_DEBUG(plugin->api, "Got %zu transport(s)", nbTransports);

	for (int ix=0; ix<nbTransports; ix++) {
		char addr[18];
		struct ba_msg_transport * ba_transport = &transports[ix];
		ba2str(&ba_transport->addr, addr);
		const char * typeS;
		if (ba_transport->type == BA_PCM_TYPE_SCO)
			typeS = "sco";
		else if (ba_transport->type == BA_PCM_TYPE_A2DP)
			typeS = "a2dp";
		else
			typeS = "unknown";

		AFB_API_DEBUG(plugin->api, "Transport %d: type %s, dev %s", ix, typeS, addr);

		if (halBlueAlsaTransportFind(watch, transport_list, ba_transport)) {
			AFB_API_DEBUG(plugin->api, "This transport is already streamed");
			continue;
		}

		AFB_API_INFO(plugin->api, "Registering transport type %s, dev %s", typeS, addr);
		transport = halBlueAlsaTransportsAdd(watch, transport_list, ba_transport);
		if (transport == NULL) {
			AFB_API_ERROR(plugin->api, "Failed to register this transport");
			goto done;
		}

		// Do the softmixer stuff
		if (halBlueAlsaAttachTransportStreams(transport) != 0) {
			AFB_API_ERROR(plugin->api, "Failed create transport streams");
			goto done;
		}

		AFB_API_DEBUG(plugin->api, "%s: transaction id %s", __func__, transport->transactionUidS);

	}

	halBlueAlsaTransportUpdate(watch, transport_list, transports, nbTransports , halBluezAlsaRemoveTransportStream);

done:
	free(transports);
	return 0;
}

static int halBlueAlsaRegister(CtlPluginT* plugin, const char * interface) {
	int ret;
    sd_event *sdLoop;
    sd_event_source* evtsrc;

    sdLoop = afb_api_get_event_loop(plugin->api);

    enum ba_event transport_mask = BA_EVENT_TRANSPORT_ADDED |
//    							   BA_EVENT_TRANSPORT_CHANGED |
								   BA_EVENT_TRANSPORT_REMOVED;

    bluealsa_watch * watch = (bluealsa_watch *)malloc(sizeof(bluealsa_watch));
    if (watch == NULL)
    	goto fail;

    watch->interface = interface;
    watch->plugin    = plugin;

    if ((watch->fd = bluealsa_open(interface)) == -1) {
        AFB_API_ERROR(plugin->api, "BlueALSA connection failed: %s", strerror(errno));
    	goto fail;
    }

	halBlueAlsaFetchTransports(watch);

    if (bluealsa_subscribe(watch->fd, transport_mask) == -1) {
        AFB_API_ERROR(plugin->api, "BlueALSA subscription failed: %s", strerror(errno));
    	goto fail;
    }

    // Register sound event to the main loop of the binder
    if ((ret = sd_event_add_io(sdLoop, &evtsrc, watch->fd, EPOLLIN, halBlueAlsaTransportEventCB, watch)) < 0) {
        AFB_API_ERROR(plugin->api,
        		     "%s: Failed to register event fd to io loop",
					 __func__);
        goto fail;
    }
	return 0;

fail:
	if (watch->fd)
		close(watch->fd);
	if (watch)
		free(watch);
	return -1;
}


static int halBlueAlsaRegisterAll(CtlPluginT* plugin) {
    /* TODO add a watch to all the available interfaces */
	halBlueAlsaRegister(plugin, "hci0");
	return 0;
}