mirror of
https://gitlab.freedesktop.org/pulseaudio/pulseaudio.git
synced 2025-10-29 05:40:23 -04:00
bluetooth: Provide (HSP/HFP-received) battery level as device property
Part-of: <https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/merge_requests/482>
This commit is contained in:
parent
d2c97190ef
commit
c667befe9a
4 changed files with 42 additions and 0 deletions
|
|
@ -769,6 +769,7 @@ static void rfcomm_io_callback(pa_mainloop_api *io, pa_io_event *e, int fd, pa_i
|
|||
switch (key) {
|
||||
case 1:
|
||||
pa_log_notice("Battery Level: %d0%%", val + 1);
|
||||
pa_bluetooth_device_report_battery_level(t->device, (val + 1) * 10);
|
||||
break;
|
||||
case 2:
|
||||
pa_log_notice("Dock Status: %s", val ? "docked" : "undocked");
|
||||
|
|
|
|||
|
|
@ -868,6 +868,12 @@ bool pa_bluetooth_device_any_transport_connected(const pa_bluetooth_device *d) {
|
|||
return false;
|
||||
}
|
||||
|
||||
void pa_bluetooth_device_report_battery_level(pa_bluetooth_device *d, uint8_t level) {
|
||||
d->has_battery_level = true;
|
||||
d->battery_level = level;
|
||||
pa_hook_fire(&d->discovery->hooks[PA_BLUETOOTH_HOOK_DEVICE_BATTERY_LEVEL_CHANGED], d);
|
||||
}
|
||||
|
||||
static int transport_state_from_string(const char* value, pa_bluetooth_transport_state_t *state) {
|
||||
pa_assert(value);
|
||||
pa_assert(state);
|
||||
|
|
|
|||
|
|
@ -64,6 +64,7 @@ typedef struct pa_bluetooth_backend pa_bluetooth_backend;
|
|||
typedef enum pa_bluetooth_hook {
|
||||
PA_BLUETOOTH_HOOK_DEVICE_CONNECTION_CHANGED, /* Call data: pa_bluetooth_device */
|
||||
PA_BLUETOOTH_HOOK_DEVICE_UNLINK, /* Call data: pa_bluetooth_device */
|
||||
PA_BLUETOOTH_HOOK_DEVICE_BATTERY_LEVEL_CHANGED, /* Call data: pa_bluetooth_device */
|
||||
PA_BLUETOOTH_HOOK_TRANSPORT_STATE_CHANGED, /* Call data: pa_bluetooth_transport */
|
||||
PA_BLUETOOTH_HOOK_TRANSPORT_SOURCE_VOLUME_CHANGED, /* Call data: pa_bluetooth_transport */
|
||||
PA_BLUETOOTH_HOOK_TRANSPORT_SINK_VOLUME_CHANGED, /* Call data: pa_bluetooth_transport */
|
||||
|
|
@ -150,6 +151,9 @@ struct pa_bluetooth_device {
|
|||
pa_bluetooth_transport *transports[PA_BLUETOOTH_PROFILE_COUNT];
|
||||
|
||||
pa_time_event *wait_for_profiles_timer;
|
||||
|
||||
bool has_battery_level;
|
||||
uint8_t battery_level;
|
||||
};
|
||||
|
||||
struct pa_bluetooth_adapter {
|
||||
|
|
@ -197,6 +201,7 @@ void pa_bluetooth_transport_load_a2dp_sink_volume(pa_bluetooth_transport *t);
|
|||
|
||||
bool pa_bluetooth_device_any_transport_connected(const pa_bluetooth_device *d);
|
||||
bool pa_bluetooth_device_switch_codec(pa_bluetooth_device *device, pa_bluetooth_profile_t profile, pa_hashmap *capabilities_hashmap, const pa_a2dp_endpoint_conf *endpoint_conf, void (*codec_switch_cb)(bool, pa_bluetooth_profile_t profile, void *), void *userdata);
|
||||
void pa_bluetooth_device_report_battery_level(pa_bluetooth_device *d, uint8_t level);
|
||||
|
||||
pa_bluetooth_device* pa_bluetooth_discovery_get_device_by_path(pa_bluetooth_discovery *y, const char *path);
|
||||
pa_bluetooth_device* pa_bluetooth_discovery_get_device_by_address(pa_bluetooth_discovery *y, const char *remote, const char *local);
|
||||
|
|
|
|||
|
|
@ -103,6 +103,7 @@ struct userdata {
|
|||
pa_core *core;
|
||||
|
||||
pa_hook_slot *device_connection_changed_slot;
|
||||
pa_hook_slot *device_battery_level_changed_slot;
|
||||
pa_hook_slot *transport_state_changed_slot;
|
||||
pa_hook_slot *transport_sink_volume_changed_slot;
|
||||
pa_hook_slot *transport_source_volume_changed_slot;
|
||||
|
|
@ -2157,6 +2158,12 @@ static int add_card(struct userdata *u) {
|
|||
data.name = pa_sprintf_malloc("bluez_card.%s", d->address);
|
||||
data.namereg_fail = false;
|
||||
|
||||
if (d->has_battery_level) {
|
||||
// See device_battery_level_changed_cb
|
||||
uint8_t level = d->battery_level;
|
||||
pa_proplist_setf(data.proplist, "bluetooth.battery", "%d%%", level);
|
||||
}
|
||||
|
||||
create_card_ports(u, data.ports);
|
||||
|
||||
PA_HASHMAP_FOREACH(uuid, d->uuids, state) {
|
||||
|
|
@ -2295,6 +2302,22 @@ static pa_hook_result_t device_connection_changed_cb(pa_bluetooth_discovery *y,
|
|||
return PA_HOOK_OK;
|
||||
}
|
||||
|
||||
static pa_hook_result_t device_battery_level_changed_cb(pa_bluetooth_discovery *y, const pa_bluetooth_device *d, struct userdata *u) {
|
||||
uint8_t level;
|
||||
|
||||
pa_assert(d);
|
||||
pa_assert(u);
|
||||
|
||||
if (d != u->device || !d->has_battery_level)
|
||||
return PA_HOOK_OK;
|
||||
|
||||
level = d->battery_level;
|
||||
|
||||
pa_proplist_setf(u->card->proplist, "bluetooth.battery", "%d%%", level);
|
||||
|
||||
return PA_HOOK_OK;
|
||||
}
|
||||
|
||||
/* Run from main thread */
|
||||
static pa_hook_result_t transport_state_changed_cb(pa_bluetooth_discovery *y, pa_bluetooth_transport *t, struct userdata *u) {
|
||||
pa_assert(t);
|
||||
|
|
@ -2691,6 +2714,10 @@ int pa__init(pa_module* m) {
|
|||
pa_hook_connect(pa_bluetooth_discovery_hook(u->discovery, PA_BLUETOOTH_HOOK_DEVICE_CONNECTION_CHANGED),
|
||||
PA_HOOK_NORMAL, (pa_hook_cb_t) device_connection_changed_cb, u);
|
||||
|
||||
u->device_battery_level_changed_slot =
|
||||
pa_hook_connect(pa_bluetooth_discovery_hook(u->discovery, PA_BLUETOOTH_HOOK_DEVICE_BATTERY_LEVEL_CHANGED),
|
||||
PA_HOOK_NORMAL, (pa_hook_cb_t) device_battery_level_changed_cb, u);
|
||||
|
||||
u->transport_state_changed_slot =
|
||||
pa_hook_connect(pa_bluetooth_discovery_hook(u->discovery, PA_BLUETOOTH_HOOK_TRANSPORT_STATE_CHANGED),
|
||||
PA_HOOK_NORMAL, (pa_hook_cb_t) transport_state_changed_cb, u);
|
||||
|
|
@ -2767,6 +2794,9 @@ void pa__done(pa_module *m) {
|
|||
if (u->device_connection_changed_slot)
|
||||
pa_hook_slot_free(u->device_connection_changed_slot);
|
||||
|
||||
if (u->device_battery_level_changed_slot)
|
||||
pa_hook_slot_free(u->device_battery_level_changed_slot);
|
||||
|
||||
if (u->transport_state_changed_slot)
|
||||
pa_hook_slot_free(u->transport_state_changed_slot);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue