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

Commit a0ad6713 authored by Jakub Pawlowski's avatar Jakub Pawlowski
Browse files

Hearing Aid Manager implementation

Test: connect to Hearing Aid, play some music, make phone call
Bug: 69623109
Change-Id: I924ce288f1aa28c0d59edffa7a00f90271a808ab
(cherry picked from commit 2c2c78944081b7bedd9fdd06f0dc7e8b4b86bf5e)
parent cd330825
Loading
Loading
Loading
Loading
+1088 −11

File changed.

Preview size limit exceeded, changes collapsed.

+3 −3
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ extern const char* audio_ha_hw_dump_ctrl_event(tHEARING_AID_CTRL_CMD event);
namespace {
int bit_rate = 16;
int sample_rate = 16000;
int data_interval_ms = 20 /* msec */;
int data_interval_ms = 10 /* msec */;
int num_channels = 2;
alarm_t* audio_timer = nullptr;

@@ -113,7 +113,7 @@ void hearing_aid_recv_ctrl_data() {
      break;

    case HEARING_AID_CTRL_CMD_START:
      localAudioReceiver->OnAudioResume();
      if (localAudioReceiver) localAudioReceiver->OnAudioResume();
      // timer is restarted in UIPC_Open
      UIPC_Open(*uipc_hearing_aid, UIPC_CH_ID_AV_AUDIO, hearing_aid_data_cb,
                HEARING_AID_DATA_PATH);
@@ -126,7 +126,7 @@ void hearing_aid_recv_ctrl_data() {

    case HEARING_AID_CTRL_CMD_SUSPEND:
      if (audio_timer) alarm_cancel(audio_timer);
      localAudioReceiver->OnAudioSuspend();
      if (localAudioReceiver) localAudioReceiver->OnAudioSuspend();
      hearing_aid_send_ack(HEARING_AID_CTRL_ACK_SUCCESS);
      break;

+3 −2
Original line number Diff line number Diff line
@@ -43,9 +43,10 @@ class HearingAid {
  static HearingAid* Get();

  static void AddFromStorage(const RawAddress& address, uint16_t psm,
                             uint8_t capabilities, uint8_t codec,
                             uint8_t capabilities, uint16_t codec,
                             uint16_t audioControlPointHandle,
                             uint16_t volumeHandle, uint64_t hiSyncId);
                             uint16_t volumeHandle, uint64_t hiSyncId,
                             uint16_t render_delay, uint16_t preparation_delay);

  virtual void Connect(const RawAddress& address) = 0;
  virtual void Disconnect(const RawAddress& address) = 0;
+1 −0
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ static_library("btif") {
    "src/btif_gatt_server.cc",
    "src/btif_gatt_test.cc",
    "src/btif_gatt_util.cc",
    "src/btif_hearing_aid.cc",
    "src/btif_hf.cc",
    "src/btif_hf_client.cc",
    "src/btif_hh.cc",
+110 −2
Original line number Diff line number Diff line
@@ -1355,11 +1355,119 @@ bt_status_t btif_storage_remove_hid_info(RawAddress* remote_bd_addr) {
  return BT_STATUS_SUCCESS;
}

constexpr char HEARING_AID_PSM[] = "HearingAidPsm";
constexpr char HEARING_AID_CAPABILITIES[] = "HearingAidCapabilities";
constexpr char HEARING_AID_CODECS[] = "HearingAidCodecs";
constexpr char HEARING_AID_AUDIO_CONTROL_POINT[] =
    "HearingAidAudioControlPoint";
constexpr char HEARING_AID_VOLUME_HANDLE[] = "HearingAidVolumeHandle";
constexpr char HEARING_AID_SYNC_ID[] = "HearingAidSyncId";
constexpr char HEARING_AID_RENDER_DELAY[] = "HearingAidRenderDelay";
constexpr char HEARING_AID_PREPARATION_DELAY[] = "HearingAidPreparationDelay";

void btif_storage_add_hearing_aid(const RawAddress& address, uint16_t psm,
                                  uint8_t capabilities, uint16_t codecs,
                                  uint16_t audio_control_point_handle,
                                  uint16_t volume_handle, uint64_t hi_sync_id,
                                  uint16_t render_delay,
                                  uint16_t preparation_delay) {
  do_in_jni_thread(
      FROM_HERE,
      Bind(
          [](const RawAddress& address, uint16_t psm, uint8_t capabilities,
             uint16_t codecs, uint16_t audio_control_point_handle,
             uint16_t volume_handle, uint64_t hi_sync_id, uint16_t render_delay,
             uint16_t preparation_delay) {
            std::string bdstr = address.ToString();
            VLOG(2) << "saving hearing aid device: " << bdstr;
            btif_config_set_int(bdstr, HEARING_AID_PSM, psm);
            btif_config_set_int(bdstr, HEARING_AID_CAPABILITIES, capabilities);
            btif_config_set_int(bdstr, HEARING_AID_CODECS, codecs);
            btif_config_set_int(bdstr, HEARING_AID_AUDIO_CONTROL_POINT,
                                audio_control_point_handle);
            btif_config_set_int(bdstr, HEARING_AID_VOLUME_HANDLE,
                                volume_handle);
            btif_config_set_uint64(bdstr, HEARING_AID_SYNC_ID, hi_sync_id);
            btif_config_set_int(bdstr, HEARING_AID_RENDER_DELAY, render_delay);
            btif_config_set_int(bdstr, HEARING_AID_PREPARATION_DELAY,
                                preparation_delay);
            btif_config_save();
          },
          address, psm, capabilities, codecs, audio_control_point_handle,
          volume_handle, hi_sync_id, render_delay, preparation_delay));
}

/** Loads information about bonded hearing aid devices */
void btif_storage_load_bonded_hearing_aids() {}
void btif_storage_load_bonded_hearing_aids() {
  // TODO: this code is not thread safe, it can corrupt config content.
  // b/67595284
  for (const section_t& section : btif_config_sections()) {
    const std::string& name = section.name;
    if (!RawAddress::IsValidAddress(name)) continue;

    BTIF_TRACE_DEBUG("Remote device:%s", name.c_str());

    int value;
    if (!btif_config_get_int(name, HEARING_AID_PSM, &value)) continue;
    uint16_t psm = value;

    if (btif_in_fetch_bonded_device(name.c_str()) != BT_STATUS_SUCCESS) {
      RawAddress bd_addr;
      RawAddress::FromString(name, bd_addr);
      btif_storage_remove_hearing_aid(bd_addr);
      continue;
    }

    uint8_t capabilities = 0;
    if (btif_config_get_int(name, HEARING_AID_CAPABILITIES, &value))
      capabilities = value;

    uint16_t codecs = 0;
    if (btif_config_get_int(name, HEARING_AID_CODECS, &value)) codecs = value;

    uint16_t audio_control_point_handle = 0;
    if (btif_config_get_int(name, HEARING_AID_AUDIO_CONTROL_POINT, &value))
      audio_control_point_handle = value;

    uint16_t volume_handle = 0;
    if (btif_config_get_int(name, HEARING_AID_VOLUME_HANDLE, &value))
      volume_handle = value;

    uint64_t lvalue;
    uint64_t hi_sync_id = 0;
    if (btif_config_get_uint64(name, HEARING_AID_SYNC_ID, &lvalue))
      hi_sync_id = lvalue;

    uint16_t render_delay = 0;
    if (btif_config_get_int(name, HEARING_AID_RENDER_DELAY, &value))
      render_delay = value;

    uint16_t preparation_delay = 0;
    if (btif_config_get_int(name, HEARING_AID_PREPARATION_DELAY, &value))
      preparation_delay = value;

    RawAddress bd_addr;
    RawAddress::FromString(name, bd_addr);
    // add extracted information to BTA Hearing Aid
    do_in_bta_thread(
        FROM_HERE, Bind(&HearingAid::AddFromStorage, bd_addr, psm, capabilities,
                        codecs, audio_control_point_handle, volume_handle,
                        hi_sync_id, render_delay, preparation_delay));
  }
}

/** Deletes the bonded hearing aid device info from NVRAM */
void btif_storage_remove_hearing_aid(const RawAddress& address) {}
void btif_storage_remove_hearing_aid(const RawAddress& address) {
  std::string addrstr = address.ToString();

  btif_config_remove(addrstr, HEARING_AID_PSM);
  btif_config_remove(addrstr, HEARING_AID_CAPABILITIES);
  btif_config_remove(addrstr, HEARING_AID_CODECS);
  btif_config_remove(addrstr, HEARING_AID_AUDIO_CONTROL_POINT);
  btif_config_remove(addrstr, HEARING_AID_VOLUME_HANDLE);
  btif_config_remove(addrstr, HEARING_AID_SYNC_ID);
  btif_config_save();
}

/*******************************************************************************
 *