From 65ddd0a42bc546aa879bd94c0ec855ce8aecf52e Mon Sep 17 00:00:00 2001 From: delaosa Date: Mon, 9 May 2022 17:42:15 +0200 Subject: [PATCH] bluez5: Add battery level reporting via AT+XEVENT --- spa/plugins/bluez5/backend-native.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/spa/plugins/bluez5/backend-native.c b/spa/plugins/bluez5/backend-native.c index 8b88b01fb..87adfcd7c 100644 --- a/spa/plugins/bluez5/backend-native.c +++ b/spa/plugins/bluez5/backend-native.c @@ -684,6 +684,18 @@ static bool device_supports_required_mSBC_transport_modes( static int codec_switch_start_timer(struct rfcomm *rfcomm, int timeout_msec); +static void process_xevent_indicator(struct rfcomm *rfcomm, unsigned int batt_p1, unsigned int batt_p2) +{ + struct impl *backend = rfcomm->backend; + + spa_log_debug(backend->log, "batt_p1:%u batt_p2:%u", batt_p1, batt_p2); + + uint8_t level = (batt_p1 * 100) / batt_p2; + spa_log_debug(backend->log, "battery level: %d%%", (int) level); + // TODO: report without Battery Provider (using props) + spa_bt_device_report_battery_level(rfcomm->device, level); +} + static void process_iphoneaccev_indicator(struct rfcomm *rfcomm, unsigned int key, unsigned int value) { struct impl *backend = rfcomm->backend; @@ -760,6 +772,8 @@ static bool rfcomm_hfp_ag(struct rfcomm *rfcomm, char* buf) unsigned int indicator; unsigned int indicator_value; unsigned int value; + unsigned int xevent_batt_p1; + unsigned int xevent_batt_p2; int xapl_vendor; int xapl_product; int xapl_features; @@ -1069,6 +1083,10 @@ next_indicator: rfcomm_send_reply(rfcomm, "+XAPL=iPhone,%u", SPA_BT_HFP_HF_XAPL_FEATURE_BATTERY_REPORTING); } rfcomm_send_reply(rfcomm, "OK"); + } else if (spa_strstartswith(buf, "AT+XEVENT=USER-AGENT")) { + rfcomm_send_reply(rfcomm, "OK"); + } else if (sscanf(buf, "AT+XEVENT=BATTERY,%u,%u,%*u,%*u", &xevent_batt_p1, &xevent_batt_p2) == 2) { + process_xevent_indicator(rfcomm, xevent_batt_p1, xevent_batt_p2); } else if (sscanf(buf, "AT+IPHONEACCEV=%u%n", &count, &r) == 1) { if (count < 1 || count > 100) return false;