-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbluealsa-client.c
640 lines (549 loc) · 21.4 KB
/
bluealsa-client.c
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
/*
* bluealsa-autoconfig - bluealsa-client.c
* Copyright (c) 2023-2024 @borine (https://github.com/borine/)
*
* This project is licensed under the terms of the MIT license.
*
*/
#include "bluealsa-client.h"
#include "bluez-alsa/dbus.h"
#include "bluez-alsa/shared/dbus-client-pcm.h"
#include "bluez-alsa/shared/log.h"
#include <bluetooth/bluetooth.h>
#include <dbus/dbus.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/param.h>
struct bluealsa_client_service {
char well_known_name[32];
char unique_name[16];
};
struct bluealsa_client {
struct ba_dbus_ctx dbus_ctx;
pcm_added_t add_func;
pcm_removed_t remove_func;
pcm_updated_t update_func;
service_stopped_t stopped_func;
void *user_data;
struct bluealsa_client_service *services;
size_t services_count;
};
static const char *bluealsa_client_get_unique_name(DBusConnection *conn, const char *well_known_name) {
DBusError error = DBUS_ERROR_INIT;
DBusMessage *msg;
DBusMessage *rep = NULL;
if ((msg = dbus_message_new_method_call("org.freedesktop.DBus", "/org/freedesktop/DBus",
"org.freedesktop.DBus", "GetNameOwner")) == NULL) {
dbus_set_error(&error, DBUS_ERROR_NO_MEMORY, NULL);
return NULL;
}
DBusMessageIter iter;
dbus_message_iter_init_append(msg, &iter);
if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &well_known_name)) {
dbus_set_error(&error, DBUS_ERROR_NO_MEMORY, NULL);
goto fail;
}
if ((rep = dbus_connection_send_with_reply_and_block(conn,
msg, DBUS_TIMEOUT_USE_DEFAULT, &error)) == NULL)
goto fail;
if (!dbus_message_iter_init(rep, &iter)) {
dbus_set_error(&error, DBUS_ERROR_INVALID_SIGNATURE, "Empty response message");
goto fail;
}
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) {
char *signature = dbus_message_iter_get_signature(&iter);
dbus_set_error(&error, DBUS_ERROR_INVALID_SIGNATURE,
"Incorrect signature: %s != s", signature);
dbus_free(signature);
goto fail;
}
const char *unique_name;
dbus_message_iter_get_basic(&iter, &unique_name);
return unique_name;
fail:
if (rep != NULL)
dbus_message_unref(rep);
if (msg != NULL)
dbus_message_unref(msg);
return NULL;
}
static void bluealsa_client_service_started(bluealsa_client_t client, const char *well_known_name, const char *unique_name) {
for (unsigned int index = 0; index < client->services_count; index++) {
struct bluealsa_client_service *service = &client->services[index];
if (strcmp(service->well_known_name, well_known_name) == 0) {
strncpy(service->unique_name, unique_name, sizeof(service->unique_name) - 1);
return;
}
}
}
static void bluealsa_client_service_stopped(bluealsa_client_t client, const char *well_known_name) {
if (client->stopped_func == NULL)
return;
for (unsigned int index = 0; index < client->services_count; index++) {
struct bluealsa_client_service *service = &client->services[index];
if (strcmp(service->well_known_name, well_known_name) == 0) {
service->unique_name[0] = '\0';
client->stopped_func(service->well_known_name, client->user_data);
return;
}
}
}
static void bluealsa_client_pcm_added(bluealsa_client_t client, struct ba_pcm *pcm, const char *unique_name) {
if (client->add_func == NULL)
return;
for (unsigned int index = 0; index < client->services_count; index++) {
struct bluealsa_client_service *service = &client->services[index];
if (strcmp(service->unique_name, unique_name) == 0) {
client->add_func(pcm, service->well_known_name, client->user_data);
return;
}
}
}
static void bluealsa_client_pcm_removed(bluealsa_client_t client, const char *path, const char *unique_name) {
if (client->remove_func == NULL)
return;
for (unsigned int index = 0; index < client->services_count; index++) {
struct bluealsa_client_service *service = &client->services[index];
if (strcmp(service->unique_name, unique_name) == 0) {
client->remove_func(path, client->user_data);
return;
}
}
}
static void bluealsa_client_pcm_updated(bluealsa_client_t client, const char *path, const char *unique_name, struct bluealsa_pcm_properties *props) {
if (client->update_func == NULL)
return;
for (unsigned int index = 0; index < client->services_count; index++) {
struct bluealsa_client_service *service = &client->services[index];
if (strcmp(service->unique_name, unique_name) == 0) {
client->update_func(path, service->well_known_name, props, client->user_data);
return;
}
}
}
static DBusHandlerResult bluealsa_client_objmgr_signal_handler(bluealsa_client_t client, const char *signal, const char *service, DBusMessageIter *iter) {
if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_OBJECT_PATH)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
const char *path;
dbus_message_iter_get_basic(iter, &path);
if (strcmp(signal, "InterfacesAdded") == 0) {
DBusMessageIter iter_copy = *iter;
if (!dbus_message_iter_next(&iter_copy))
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
DBusMessageIter iter_ifaces;
for (dbus_message_iter_recurse(&iter_copy, &iter_ifaces);
dbus_message_iter_get_arg_type(&iter_ifaces) != DBUS_TYPE_INVALID;
dbus_message_iter_next(&iter_ifaces)) {
DBusMessageIter iter_iface_entry;
if (dbus_message_iter_get_arg_type(&iter_ifaces) != DBUS_TYPE_DICT_ENTRY)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
dbus_message_iter_recurse(&iter_ifaces, &iter_iface_entry);
const char *iface;
if (dbus_message_iter_get_arg_type(&iter_iface_entry) != DBUS_TYPE_STRING)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
dbus_message_iter_get_basic(&iter_iface_entry, &iface);
if (strcmp(iface, BLUEALSA_INTERFACE_PCM) == 0) {
struct ba_pcm pcm;
DBusError err = DBUS_ERROR_INIT;
if (!dbus_message_iter_get_ba_pcm(iter, &err, &pcm)) {
error("Couldn't read PCM properties: %s", err.message);
dbus_error_free(&err);
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
bluealsa_client_pcm_added(client, &pcm, service);
}
}
}
else if (strcmp(signal, "InterfacesRemoved") == 0) {
if (!dbus_message_iter_next(iter))
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
DBusMessageIter iter_ifaces;
for (dbus_message_iter_recurse(iter, &iter_ifaces);
dbus_message_iter_get_arg_type(&iter_ifaces) != DBUS_TYPE_INVALID;
dbus_message_iter_next(&iter_ifaces)) {
const char *iface;
if (dbus_message_iter_get_arg_type(&iter_ifaces) != DBUS_TYPE_STRING)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
dbus_message_iter_get_basic(&iter_ifaces, &iface);
if (strcmp(iface, BLUEALSA_INTERFACE_PCM) != 0)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
bluealsa_client_pcm_removed(client, path, service);
}
}
return DBUS_HANDLER_RESULT_HANDLED;
}
static DBusHandlerResult bluealsa_client_name_signal_handler(bluealsa_client_t client, const char *signal, DBusMessageIter *iter) {
if (strcmp(signal, "NameOwnerChanged") != 0)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
const char *arg0 = NULL, *arg1 = NULL, *arg2 = NULL;
if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_STRING)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
dbus_message_iter_get_basic(iter, &arg0);
if (dbus_message_iter_next(iter) &&
dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING)
dbus_message_iter_get_basic(iter, &arg1);
else
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
if (dbus_message_iter_next(iter) &&
dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING)
dbus_message_iter_get_basic(iter, &arg2);
else
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
if (strncmp(arg0, "org.bluealsa", strlen("org.bluealsa")) != 0)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
if (strlen(arg1) == 0)
bluealsa_client_service_started(client, arg0, arg2);
if (strlen(arg2) == 0)
bluealsa_client_service_stopped(client, arg0);
else
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
return DBUS_HANDLER_RESULT_HANDLED;
}
static DBusHandlerResult bluealsa_client_parse_pcm_property(const char *name, DBusMessageIter *iter, void *data) {
struct bluealsa_pcm_properties *props = data;
if (strcmp(name, "Format") == 0) {
if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_UINT16)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
dbus_message_iter_get_basic(iter, &props->format);
props->mask |= BLUEALSA_PCM_PROPERTY_CHANGED_FORMAT;
}
else if (strcmp(name, "Channels") == 0) {
if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_BYTE)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
dbus_message_iter_get_basic(iter, &props->channels);
props->mask |= BLUEALSA_PCM_PROPERTY_CHANGED_CHANNELS;
}
else if (strcmp(name, "Rate") == 0) {
if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_UINT32)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
dbus_message_iter_get_basic(iter, &props->rate);
props->mask |= BLUEALSA_PCM_PROPERTY_CHANGED_RATE;
}
else if (strcmp(name, "Sampling") == 0) {
if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_UINT32)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
dbus_message_iter_get_basic(iter, &props->rate);
props->mask |= BLUEALSA_PCM_PROPERTY_CHANGED_RATE;
}
else if (strcmp(name, "Codec") == 0) {
if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_STRING)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
const char *codec;
dbus_message_iter_get_basic(iter, &codec);
strncpy(props->codec.name, codec, sizeof(props->codec.name ) - 1);
props->mask |= BLUEALSA_PCM_PROPERTY_CHANGED_CODEC;
}
else if (strcmp(name, "CodecConfiguration") == 0) {
if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
DBusMessageIter iter_config;
uint8_t *config;
int len;
dbus_message_iter_recurse(iter, &iter_config);
dbus_message_iter_get_fixed_array(&iter_config, &config, &len);
props->codec.data_len = MIN((size_t)len, sizeof(props->codec.data));
memcpy(props->codec.data, config, props->codec.data_len);
props->mask |= BLUEALSA_PCM_PROPERTY_CHANGED_CODEC_CONFIG;
}
else if (strcmp(name, "ClientDelay") == 0) {
if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INT16)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
dbus_message_iter_get_basic(iter, &props->client_delay);
props->mask |= BLUEALSA_PCM_PROPERTY_CHANGED_CLIENT_DELAY;
}
else if (strcmp(name, "Delay") == 0) {
if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_UINT16)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
dbus_message_iter_get_basic(iter, &props->delay);
props->mask |= BLUEALSA_PCM_PROPERTY_CHANGED_DELAY;
}
else if (strcmp(name, "SoftVolume") == 0) {
if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_BOOLEAN)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
dbus_message_iter_get_basic(iter, &props->softvolume);
props->mask |= BLUEALSA_PCM_PROPERTY_CHANGED_SOFTVOL;
}
else if (strcmp(name, "Volume") == 0) {
switch (dbus_message_iter_get_arg_type(iter)) {
case DBUS_TYPE_UINT16: {
uint16_t vol;
dbus_message_iter_get_basic(iter, &vol);
memcpy(&props->volume, &vol, 2);
break;
}
case DBUS_TYPE_ARRAY: {
DBusMessageIter iter_vol;
uint8_t *data;
int len;
dbus_message_iter_recurse(iter, &iter_vol);
dbus_message_iter_get_fixed_array(&iter_vol, &data, &len);
memcpy(&props->volume, data, MIN(len, ARRAYSIZE(props->volume)));
break;
}
default:
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
props->mask |= BLUEALSA_PCM_PROPERTY_CHANGED_VOLUME;
}
else if (strcmp(name, "Running") == 0) {
if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_BOOLEAN)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
dbus_message_iter_get_basic(iter, &props->running);
props->mask |= BLUEALSA_PCM_PROPERTY_CHANGED_RUNNING;
}
else if (strcmp(name, "ChannelMap") == 0) {
if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
const char *data[ARRAYSIZE(props->channel_map)];
size_t length = ARRAYSIZE(data);
if (!dbus_message_iter_array_get_strings(iter, NULL, data, &length))
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
for (size_t i = 0; i < length; i++)
strncpy(props->channel_map[i], data[i], sizeof(props->channel_map[i]) - 1);
props->mask |= BLUEALSA_PCM_PROPERTY_CHANGED_CHANNEL_MAP;
}
return DBUS_HANDLER_RESULT_HANDLED;
}
static DBusHandlerResult bluealsa_client_parse_properties(DBusMessageIter *iter, DBusHandlerResult (parse_property)(const char *, DBusMessageIter *, void *), void *data) {
DBusMessageIter iter_properties;
for (dbus_message_iter_recurse(iter, &iter_properties);
dbus_message_iter_get_arg_type(&iter_properties) != DBUS_TYPE_INVALID;
dbus_message_iter_next(&iter_properties)) {
if (dbus_message_iter_get_arg_type(&iter_properties) != DBUS_TYPE_DICT_ENTRY)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
DBusMessageIter iter_property;
dbus_message_iter_recurse(&iter_properties, &iter_property);
if (dbus_message_iter_get_arg_type(&iter_property) != DBUS_TYPE_STRING)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
const char *name = NULL;
dbus_message_iter_get_basic(&iter_property, &name);
if (!dbus_message_iter_next(&iter_property) ||
dbus_message_iter_get_arg_type(&iter_property) != DBUS_TYPE_VARIANT)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
DBusMessageIter iter_val;
dbus_message_iter_recurse(&iter_property, &iter_val);
parse_property(name, &iter_val, data);
}
return DBUS_HANDLER_RESULT_HANDLED;
}
static DBusHandlerResult bluealsa_client_pcm_properties_changed(bluealsa_client_t client, const char *path, const char *service, DBusMessageIter *iter) {
struct bluealsa_pcm_properties props = { 0 };
bluealsa_client_parse_properties(iter, bluealsa_client_parse_pcm_property, &props);
if (props.mask != 0)
bluealsa_client_pcm_updated(client, path, service, &props);
return DBUS_HANDLER_RESULT_HANDLED;
}
static DBusHandlerResult bluealsa_client_properties_signal_handler(bluealsa_client_t client, const char *path, const char *signal, const char *service, DBusMessageIter *iter) {
if (strcmp(signal, "PropertiesChanged") != 0)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_STRING)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
const char *interface = NULL;
dbus_message_iter_get_basic(iter, &interface);
if (!dbus_message_iter_next(iter))
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
if (strcmp(interface, "org.bluealsa.PCM1") == 0)
return bluealsa_client_pcm_properties_changed(client, path, service, iter);
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
static DBusHandlerResult bluealsa_client_dbus_signal_handler(DBusConnection *conn, DBusMessage *message, void *data) {
(void)conn;
bluealsa_client_t client = data;
if (dbus_message_get_type(message) != DBUS_MESSAGE_TYPE_SIGNAL)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
const char *interface = dbus_message_get_interface(message);
const char *signal = dbus_message_get_member(message);
const char *service = dbus_message_get_sender(message);
DBusMessageIter iter;
if (!dbus_message_iter_init(message, &iter))
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
if (strcmp(interface, DBUS_INTERFACE_OBJECT_MANAGER) == 0)
return bluealsa_client_objmgr_signal_handler(client, signal, service, &iter);
else if (strcmp(interface, DBUS_INTERFACE_DBUS) == 0)
return bluealsa_client_name_signal_handler(client, signal, &iter);
else if (strcmp(interface, DBUS_INTERFACE_PROPERTIES) == 0) {
const char *path = dbus_message_get_path(message);
return bluealsa_client_properties_signal_handler(client, path, signal, service, &iter);
}
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
int bluealsa_client_open(bluealsa_client_t *client, struct bluealsa_client_callbacks *callbacks) {
int ret = 0;
bluealsa_client_t new_client = calloc(sizeof(struct bluealsa_client), 1);
if (new_client == NULL)
return -ENOMEM;
DBusError err = DBUS_ERROR_INIT;
dbus_threads_init_default();
if (!ba_dbus_connection_ctx_init(&new_client->dbus_ctx, "", &err)) {
free(new_client);
error("Couldn't initialize D-Bus context: %s", err.message);
ret = EIO;
goto fail;
}
if (callbacks != NULL) {
new_client->add_func = callbacks->add_func;
new_client->remove_func = callbacks->remove_func;
new_client->update_func = callbacks->update_func;
new_client->stopped_func = callbacks->stopped_func;
new_client->user_data = callbacks->data;
if (!dbus_connection_add_filter(new_client->dbus_ctx.conn, bluealsa_client_dbus_signal_handler, new_client, NULL)) {
error("Couldn't add D-Bus filter");
ret = ENOMEM;
goto fail;
}
}
*client = new_client;
return 0;
fail:
ba_dbus_connection_ctx_free(&new_client->dbus_ctx);
free(new_client);
return -ret;
}
int bluealsa_client_close(bluealsa_client_t client) {
ba_dbus_connection_ctx_free(&client->dbus_ctx);
free(client->services);
free(client);
return 0;
}
int bluealsa_client_get_pcms(bluealsa_client_t client, const char *service) {
if (strlen(service) >= sizeof(client->dbus_ctx.ba_service))
return -EINVAL;
DBusError error = DBUS_ERROR_INIT;
struct ba_pcm *pcms = NULL;
size_t count = 0;
strcpy(client->dbus_ctx.ba_service, service);
if (!ba_dbus_pcm_get_all(&client->dbus_ctx, &pcms, &count, &error))
return -ENOENT;
size_t i;
for (i = 0; i < count; i++)
client->add_func(&pcms[i], service, client->user_data);
free(pcms);
return 0;
}
int bluealsa_client_num_services(const bluealsa_client_t client) {
return client->services_count;
}
int bluealsa_client_poll_fds(bluealsa_client_t client, struct pollfd *fds, nfds_t *nfds) {
if (!ba_dbus_connection_poll_fds(&client->dbus_ctx, fds, nfds))
return -1;
return 0;
}
int bluealsa_client_poll_dispatch(bluealsa_client_t client, struct pollfd *pfds, nfds_t nfds) {
if (ba_dbus_connection_poll_dispatch(&client->dbus_ctx, pfds, nfds))
while (dbus_connection_dispatch(client->dbus_ctx.conn) == DBUS_DISPATCH_DATA_REMAINS)
continue;
return 0;
}
int bluealsa_client_watch_service(bluealsa_client_t client, const char *service) {
struct bluealsa_client_service *services = realloc(client->services, (client->services_count + 1) * sizeof(struct bluealsa_client_service));
if (services == NULL)
return -ENOMEM;
client->services = services;
struct bluealsa_client_service *new_service = &client->services[client->services_count++];
strncpy(new_service->well_known_name, service, sizeof(new_service->well_known_name) - 1);
const char *unique_name = bluealsa_client_get_unique_name(client->dbus_ctx.conn, service);
if (unique_name != NULL)
strncpy(new_service->unique_name, unique_name, sizeof(new_service->unique_name) - 1);
if (client->add_func)
ba_dbus_connection_signal_match_add(&client->dbus_ctx,
service, NULL, DBUS_INTERFACE_OBJECT_MANAGER,
"InterfacesAdded", "path_namespace='/org/bluealsa'");
if (client->remove_func)
ba_dbus_connection_signal_match_add(&client->dbus_ctx,
service, NULL, DBUS_INTERFACE_OBJECT_MANAGER,
"InterfacesRemoved", "path_namespace='/org/bluealsa'");
if (client->update_func)
ba_dbus_connection_signal_match_add(&client->dbus_ctx,
service, NULL, DBUS_INTERFACE_PROPERTIES,
"PropertiesChanged", "path_namespace='/org/bluealsa'");
char dbus_args[50];
/* service stopped */
snprintf(dbus_args, sizeof(dbus_args), "arg0='%s',arg2=''", service);
ba_dbus_connection_signal_match_add(&client->dbus_ctx,
DBUS_SERVICE_DBUS, NULL, DBUS_INTERFACE_DBUS,
"NameOwnerChanged", dbus_args);
/* service started */
snprintf(dbus_args, sizeof(dbus_args), "arg0='%s',arg1=''", service);
ba_dbus_connection_signal_match_add(&client->dbus_ctx,
DBUS_SERVICE_DBUS, NULL, DBUS_INTERFACE_DBUS,
"NameOwnerChanged", dbus_args);
return 0;
}
int bluealsa_client_get_device(bluealsa_client_t client, struct bluealsa_client_device *device) {
struct bluez_device dev = { 0 };
if (dbus_bluez_get_device(client->dbus_ctx.conn, device->path, &dev, NULL) < 0)
return -1;
strncpy(device->alias, dev.name, sizeof(device->alias));
device->alias[sizeof(device->alias) - 1] = '\0';
ba2str(&dev.bt_addr, device->hex_addr);
return 0;
}
const char *bluealsa_client_transport_to_role(int transport_code) {
switch (transport_code) {
case BA_PCM_TRANSPORT_A2DP_SOURCE:
return "A2DP-source";
case BA_PCM_TRANSPORT_A2DP_SINK:
return "A2DP-sink";
case BA_PCM_TRANSPORT_HFP_AG:
return "HFP-AG";
case BA_PCM_TRANSPORT_HFP_HF:
return "HFP-HF";
case BA_PCM_TRANSPORT_HSP_AG:
return "HSP-AG";
case BA_PCM_TRANSPORT_HSP_HS:
return "HSP-HS";
default:
return NULL;
}
}
const char *bluealsa_client_transport_to_type(int transport_code) {
if (transport_code & BA_PCM_TRANSPORT_MASK_A2DP)
return "A2DP";
if (transport_code & BA_PCM_TRANSPORT_MASK_SCO)
return "SCO";
return NULL;
}
const char *bluealsa_client_mode_to_string(int pcm_mode) {
switch (pcm_mode) {
case BA_PCM_MODE_SINK:
return "sink";
case BA_PCM_MODE_SOURCE:
return "source";
default:
return NULL;
}
}
const char *bluealsa_client_format_to_string(int pcm_format) {
switch (pcm_format) {
case 0x0108:
return "U8";
case 0x8210:
return "S16_LE";
case 0x8318:
return "S24_3LE";
case 0x8418:
return "S24_LE";
case 0x8420:
return "S32_LE";
default:
return NULL;
}
}
const char *bluealsa_client_transport_to_profile(int transport_code) {
if (transport_code & BA_PCM_TRANSPORT_MASK_A2DP)
return "A2DP";
else if (transport_code & BA_PCM_TRANSPORT_MASK_HFP)
return "HFP";
else if (transport_code & BA_PCM_TRANSPORT_MASK_HSP)
return "HSP";
return NULL;
}
const char *bluealsa_client_codec_blob_to_string(const struct ba_pcm_codec *codec, char buffer[], size_t buflen) {
const size_t data_len = codec->data_len;
size_t n = 0;
if (data_len > 0) {
for (size_t i = 0; i < data_len && n < buflen - 2; i++, n += 2)
sprintf(&buffer[n], "%.2x", codec->data[i]);
}
return buffer;
}