Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 645f683a authored by Jayden Kim's avatar Jayden Kim
Browse files

Report Bluetooth system state in energy info

Bug: 322861875
Bug: 323083457
Test: m -j
Test: manual check bta_dm_obtain_system_context in logcat by
running adb shell dumpsys batterystats. The log should be aligned with
system context in terms of inquiry, LE scan, activation of
SCO/A2DP/LeAudio and number of ACL link.

Change-Id: If425c3afb7fbddc25c2e2bff328c06e722e9158e
parent 5f95e354
Loading
Loading
Loading
Loading
+19 −5
Original line number Diff line number Diff line
@@ -7369,7 +7369,7 @@ public class AdapterService extends Service {
        return true;
    }

    void energyInfoCallback(
    void energyInfoCallbackInternal(
            int status,
            int ctrlState,
            long txTime,
@@ -7377,8 +7377,6 @@ public class AdapterService extends Service {
            long idleTime,
            long energyUsed,
            UidTraffic[] data) {
        if (ctrlState >= BluetoothActivityEnergyInfo.BT_STACK_STATE_INVALID
                && ctrlState <= BluetoothActivityEnergyInfo.BT_STACK_STATE_STATE_IDLE) {
            // Energy is product of mA, V and ms. If the chipset doesn't
            // report it, we have to compute it from time
            if (energyUsed == 0) {
@@ -7435,6 +7433,22 @@ public class AdapterService extends Service {
            }
    }

    void energyInfoCallback(
            int status,
            int ctrlState,
            long txTime,
            long rxTime,
            long idleTime,
            long energyUsed,
            UidTraffic[] data) {
        if (Flags.btSystemContextReport()) {
            energyInfoCallbackInternal(
                    status, ctrlState, txTime, rxTime, idleTime, energyUsed, data);
        } else if (ctrlState >= BluetoothActivityEnergyInfo.BT_STACK_STATE_INVALID
                && ctrlState <= BluetoothActivityEnergyInfo.BT_STACK_STATE_STATE_IDLE) {
            energyInfoCallbackInternal(
                    status, ctrlState, txTime, rxTime, idleTime, energyUsed, data);
        }
        verboseLog(
                "energyInfoCallback()"
                        + (" status = " + status)
@@ -7442,7 +7456,7 @@ public class AdapterService extends Service {
                        + (" rxTime = " + rxTime)
                        + (" idleTime = " + idleTime)
                        + (" energyUsed = " + energyUsed)
                        + (" ctrlState = " + ctrlState)
                        + (" ctrlState = " + String.format("0x%08x", ctrlState))
                        + (" traffic = " + Arrays.toString(data)));
    }

+67 −1
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@
#include "bta/dm/bta_dm_int.h"
#include "bta/dm/bta_dm_sec_int.h"
#include "bta/include/bta_api.h"
#include "bta/include/bta_le_audio_api.h"
#include "bta/include/bta_sdp_api.h"
#include "bta/include/bta_sec_api.h"
#include "bta/sys/bta_sys.h"
@@ -1478,6 +1479,67 @@ void bta_dm_ble_set_data_length(const RawAddress& bd_addr) {
  }
}

/** This function returns system context info */
static tBTM_CONTRL_STATE bta_dm_obtain_system_context() {
  uint32_t total_acl_num = bta_dm_cb.device_list.count;
  uint32_t sniff_acl_num = BTM_PM_ReadSniffLinkCount();
  uint32_t le_acl_num = BTM_PM_ReadBleLinkCount();
  uint32_t active_acl_num = total_acl_num - sniff_acl_num - le_acl_num;
  uint32_t le_adv_num =
      bluetooth::shim::BTM_BleGetNumberOfAdvertisingInstancesInUse();
  uint32_t le_scan_duty_cycle = BTM_PM_ReadBleScanDutyCycle();
  bool is_inquiry_active = BTM_PM_DeviceInScanState();
  bool is_le_audio_active = LeAudioClient::IsLeAudioClientInStreaming();
  bool is_av_active = false;
  bool is_sco_active = false;

  for (int i = 0; i < bta_dm_cb.device_list.count; i++) {
    tBTA_DM_PEER_DEVICE* p_dev = &bta_dm_cb.device_list.peer_device[i];
    if (p_dev->conn_state == BTA_DM_CONNECTED && p_dev->is_av_active()) {
      is_av_active = true;
      break;
    }
  }
  for (int j = 0; j < bta_dm_conn_srvcs.count; j++) {
    /* check for SCO connected index */
    if (bta_dm_conn_srvcs.conn_srvc[j].id == BTA_ID_AG ||
        bta_dm_conn_srvcs.conn_srvc[j].id == BTA_ID_HS) {
      if (bta_dm_conn_srvcs.conn_srvc[j].state == BTA_SYS_SCO_OPEN) {
        is_sco_active = true;
        break;
      }
    }
  }

  tBTM_CONTRL_STATE ctrl_state = 0;
  set_num_acl_active_to_ctrl_state(active_acl_num, ctrl_state);
  set_num_acl_sniff_to_ctrl_state(sniff_acl_num, ctrl_state);
  set_num_acl_le_to_ctrl_state(le_acl_num, ctrl_state);
  set_num_le_adv_to_ctrl_state(le_adv_num, ctrl_state);
  set_le_scan_mode_to_ctrl_state(le_scan_duty_cycle, ctrl_state);

  if (is_inquiry_active) {
    ctrl_state |= BTM_CONTRL_INQUIRY;
  }
  if (is_sco_active) {
    ctrl_state |= BTM_CONTRL_SCO;
  }
  if (is_av_active) {
    ctrl_state |= BTM_CONTRL_A2DP;
  }
  if (is_le_audio_active) {
    ctrl_state |= BTM_CONTRL_LE_AUDIO;
  }
  LOG_DEBUG(
      "active_acl_num %d sniff_acl_num %d le_acl_num %d le_adv_num %d "
      "le_scan_duty %d inquiry %d sco %d a2dp %d le_audio %d ctrl_state "
      "0x%" PRIx32,
      active_acl_num, sniff_acl_num, le_acl_num, le_adv_num, le_scan_duty_cycle,
      is_inquiry_active, is_sco_active, is_av_active, is_le_audio_active,
      ctrl_state);
  return ctrl_state;
}

/*******************************************************************************
 *
 * Function         bta_ble_enable_scan_cmpl
@@ -1496,7 +1558,11 @@ static void bta_ble_energy_info_cmpl(tBTM_BLE_TX_TIME_MS tx_time,
  tBTA_STATUS st = (status == HCI_SUCCESS) ? BTA_SUCCESS : BTA_FAILURE;
  tBTM_CONTRL_STATE ctrl_state = BTM_CONTRL_UNKNOWN;

  if (BTA_SUCCESS == st) ctrl_state = bta_dm_pm_obtain_controller_state();
  if (BTA_SUCCESS == st) {
    ctrl_state = IS_FLAG_ENABLED(bt_system_context_report)
                     ? bta_dm_obtain_system_context()
                     : bta_dm_pm_obtain_controller_state();
  }

  if (bta_dm_cb.p_energy_info_cback)
    bta_dm_cb.p_energy_info_cback(tx_time, rx_time, idle_time, energy_used,
+2 −0
Original line number Diff line number Diff line
@@ -67,6 +67,7 @@ class LeAudioClient {
  virtual void SetInVoipCall(bool in_call) = 0;
  virtual void SetUnicastMonitorMode(uint8_t direction, bool enable) = 0;
  virtual bool IsInVoipCall() = 0;
  virtual bool IsInStreaming() = 0;
  virtual void SendAudioProfilePreferences(
      const int group_id, bool is_output_preference_le_audio,
      bool is_duplex_preference_le_audio) = 0;
@@ -91,4 +92,5 @@ class LeAudioClient {
  static bool GetAsesForStorage(const RawAddress& addr,
                                std::vector<uint8_t>& out);
  static bool IsLeAudioClientRunning();
  static bool IsLeAudioClientInStreaming();
};
+12 −0
Original line number Diff line number Diff line
@@ -1044,6 +1044,11 @@ class LeAudioClientImpl : public LeAudioClient {

  bool IsInVoipCall() override { return in_voip_call_; }

  bool IsInStreaming() override {
    return audio_sender_state_ == AudioState::STARTED ||
           audio_receiver_state_ == AudioState::STARTED;
  }

  void SetUnicastMonitorMode(uint8_t direction, bool enable) override {
    if (!IS_FLAG_ENABLED(leaudio_broadcast_audio_handover_policies)) {
      LOG_WARN("Monitor mode is disabled, Set Unicast Monitor mode is ignored");
@@ -6022,6 +6027,13 @@ bool LeAudioClient::GetAsesForStorage(const RawAddress& addr,

bool LeAudioClient::IsLeAudioClientRunning(void) { return instance != nullptr; }

bool LeAudioClient::IsLeAudioClientInStreaming(void) {
  if (!instance) {
    return false;
  }
  return instance->IsInStreaming();
}

LeAudioClient* LeAudioClient::Get() {
  CHECK(instance);
  return instance;
+1 −0
Original line number Diff line number Diff line
@@ -79,3 +79,4 @@ bool LeAudioClient::GetAsesForStorage(const RawAddress& addr,
  return false;
}
bool LeAudioClient::IsLeAudioClientRunning() { return false; }
bool LeAudioClient::IsLeAudioClientInStreaming() { return false; }
Loading