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

Commit e8ef42b7 authored by Chris Manton's avatar Chris Manton
Browse files

Remove channel connection timeouts

Defer timeout to lower layer.
Remove hex values for psms
Formalize cid name change to abstract away actual l2cap channel
identifiers from clients.
  (cid) channel_id ==>  connection interface descriptor

Bug: 141758481
Test: Compiles
Change-Id: I3af9a79d7fd721d0d58b131e15cdd7c0204b90cf
parent 046dd002
Loading
Loading
Loading
Loading
+14 −16
Original line number Diff line number Diff line
@@ -30,7 +30,7 @@ uint16_t bluetooth::shim::L2CA_Register(uint16_t client_psm,
                                        tL2CAP_APPL_INFO* callbacks,
                                        bool enable_snoop) {
  if (L2C_INVALID_PSM(client_psm)) {
    LOG_ERROR(LOG_TAG, "%s Invalid classic psm:0x%04x", __func__, client_psm);
    LOG_ERROR(LOG_TAG, "%s Invalid classic psm:%hd", __func__, client_psm);
    return 0;
  }

@@ -38,7 +38,7 @@ uint16_t bluetooth::shim::L2CA_Register(uint16_t client_psm,
      (callbacks->pL2CA_ConfigInd_Cb == nullptr) ||
      (callbacks->pL2CA_DataInd_Cb == nullptr) ||
      (callbacks->pL2CA_DisconnectInd_Cb == nullptr)) {
    LOG_ERROR(LOG_TAG, "%s Invalid classic callbacks psm:0x%04x", __func__,
    LOG_ERROR(LOG_TAG, "%s Invalid classic callbacks psm:%hd", __func__,
              client_psm);
    return 0;
  }
@@ -51,37 +51,36 @@ uint16_t bluetooth::shim::L2CA_Register(uint16_t client_psm,
                                                   is_outgoing_connection_only);

  if (shim_l2cap.Classic().IsPsmRegistered(psm)) {
    LOG_ERROR(LOG_TAG,
              "%s Already registered classic client_psm:0x%04x psm:0x%04x",
    LOG_ERROR(LOG_TAG, "%s Already registered classic client_psm:%hd psm:%hd",
              __func__, client_psm, psm);
    return 0;
  }
  shim_l2cap.Classic().RegisterPsm(psm, callbacks);

  LOG_INFO(LOG_TAG, "%s classic client_psm:0x%04x psm:0x%04x", __func__,
           client_psm, psm);
  LOG_INFO(LOG_TAG, "%s classic client_psm:%hd psm:%hd", __func__, client_psm,
           psm);

  shim_l2cap.Register(psm, callbacks, enable_snoop);

  return psm;
  return client_psm;
}

void bluetooth::shim::L2CA_Deregister(uint16_t client_psm) {
  if (L2C_INVALID_PSM(client_psm)) {
    LOG_ERROR(LOG_TAG, "%s Invalid classic psm:0x%04x", __func__, client_psm);
    LOG_ERROR(LOG_TAG, "%s Invalid classic client_psm:%hd", __func__,
              client_psm);
    return;
  }
  uint16_t psm = shim_l2cap.ConvertClientToRealPsm(client_psm);

  if (!shim_l2cap.Classic().IsPsmRegistered(psm)) {
    LOG_ERROR(
        LOG_TAG,
        "%s Not previously registered classic client_psm:0x%04x psm:0x%04x",
    LOG_ERROR(LOG_TAG,
              "%s Not previously registered classic client_psm:%hd psm:%hd",
              __func__, client_psm, psm);
    return;
  }
  shim_l2cap.Classic().UnregisterPsm(psm);
  shim_l2cap.RemoveClientPsm(client_psm);
  shim_l2cap.RemoveClientPsm(psm);
}

uint16_t bluetooth::shim::L2CA_AllocatePSM(void) {
@@ -98,12 +97,11 @@ uint16_t bluetooth::shim::L2CA_AllocateLePSM(void) {

void bluetooth::shim::L2CA_FreeLePSM(uint16_t psm) {
  if (!shim_l2cap.Le().IsPsmAllocated(psm)) {
    LOG_ERROR(LOG_TAG, "%s Not previously allocated le psm:0x%04x", __func__,
              psm);
    LOG_ERROR(LOG_TAG, "%s Not previously allocated le psm:%hd", __func__, psm);
    return;
  }
  if (!shim_l2cap.Le().IsPsmRegistered(psm)) {
    LOG_ERROR(LOG_TAG, "%s Must deregister psm before deallocation psm:0x%04x",
    LOG_ERROR(LOG_TAG, "%s Must deregister psm before deallocation psm:%hd",
              __func__, psm);
    return;
  }
+18 −20
Original line number Diff line number Diff line
@@ -125,19 +125,14 @@ uint16_t bluetooth::shim::L2cap::GetNextDynamicClassicPsm() {

void bluetooth::shim::L2cap::Register(uint16_t psm, tL2CAP_APPL_INFO* p_cb_info,
                                      bool enable_snoop) {
  LOG_DEBUG(LOG_TAG, "Registering service psm:%hd", psm);

  std::chrono::system_clock::time_point two_seconds =
      std::chrono::system_clock::now() + std::chrono::seconds(2);
  LOG_DEBUG(LOG_TAG, "Registering service on psm:%hd", psm);

  std::promise<void> register_completed;
  auto completed = register_completed.get_future();
  bluetooth::shim::GetL2cap()->RegisterService(psm, enable_snoop,
                                               std::move(register_completed));
  completed.wait();
  if (std::future_status::ready != completed.wait_until(two_seconds)) {
    LOG_WARN(LOG_TAG, "Timed out registering service to psm:%hd", psm);
  }
  LOG_DEBUG(LOG_TAG, "Successfully registered service on psm:%hd", psm);
}

uint16_t bluetooth::shim::L2cap::Connect(uint16_t psm,
@@ -145,19 +140,16 @@ uint16_t bluetooth::shim::L2cap::Connect(uint16_t psm,
  LOG_DEBUG(LOG_TAG, "Requesting connection to psm:%hd address:%s", psm,
            raw_address.ToString().c_str());

  std::chrono::system_clock::time_point two_seconds =
      std::chrono::system_clock::now() + std::chrono::seconds(2);

  std::promise<uint16_t> connect_completed;
  auto completed = connect_completed.get_future();
  bluetooth::shim::GetL2cap()->Connect(psm, raw_address.ToString(),
                                       std::move(connect_completed));
  if (std::future_status::ready == completed.wait_until(two_seconds)) {
    return completed.get();
  }
  LOG_WARN(LOG_TAG, "Timed out connecting to psm:%hd address:%s", psm,
           raw_address.ToString().c_str());
  return 0;
  uint16_t cid = completed.get();
  LOG_INFO(LOG_TAG,
           "Successfully connected using connection_interface_descriptor:%hd "
           "psm:%hd address:%s",
           cid, psm, raw_address.ToString().c_str());
  return cid;
}

bool bluetooth::shim::L2cap::IsCongested(uint16_t cid) const {
@@ -186,11 +178,15 @@ bool bluetooth::shim::L2cap::WriteNonFlushable(uint16_t cid, BT_HDR* bt_hdr) {
bool bluetooth::shim::L2cap::SetCallbacks(uint16_t cid,
                                          const tL2CAP_APPL_INFO* callbacks) {
  LOG_ASSERT(cid_to_callback_map_.find(cid) != cid_to_callback_map_.end())
      << "Registering multiple channel callbacks cid:" << cid;
      << "Registering multiple channel callbacks "
         "connection_interface_descriptor:"
      << cid;
  cid_to_callback_map_[cid] = callbacks;
  bluetooth::shim::GetL2cap()->SetOnReadDataReady(
      cid, [this](uint16_t cid, std::vector<const uint8_t> data) {
        LOG_INFO(LOG_TAG, "Got data on cid:%hd len:%zd", cid, data.size());
        LOG_INFO(LOG_TAG,
                 "Got data on connection_interface_descriptor:%hd len:%zd", cid,
                 data.size());

        BT_HDR* bt_hdr =
            static_cast<BT_HDR*>(osi_calloc(data.size() + kBtHdrSize));
@@ -200,7 +196,8 @@ bool bluetooth::shim::L2cap::SetCallbacks(uint16_t cid,
        cid_to_callback_map_[cid]->pL2CA_DataInd_Cb(cid, bt_hdr);
      });
  bluetooth::shim::GetL2cap()->SetOnClose(cid, [this, &cid](int error_code) {
    LOG_DEBUG(LOG_TAG, "Channel closed cid:%hd", cid);
    LOG_DEBUG(LOG_TAG, "Channel closed connection_interface_descriptor:%hd",
              cid);
    cid_to_callback_map_[cid]->pL2CA_DisconnectInd_Cb(cid, true);
  });
  return true;
@@ -208,6 +205,7 @@ bool bluetooth::shim::L2cap::SetCallbacks(uint16_t cid,

void bluetooth::shim::L2cap::ClearCallbacks(uint16_t cid) {
  LOG_ASSERT(cid_to_callback_map_.find(cid) == cid_to_callback_map_.end())
      << "Clearing callbacks that do not exist cid:" << cid;
      << "Clearing callbacks that do not exist connection_interface_descriptor:"
      << cid;
  cid_to_callback_map_.erase(cid);
}