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

Commit 4e2e8ec4 authored by Myles Watson's avatar Myles Watson
Browse files

Bluetooth: Free memory from transmit_cb commands

Test: Bluetooth starts and stops
Change-Id: I540fcc77f9bae0178db325b7014f2b839d4ad77d
parent fc5a7797
Loading
Loading
Loading
Loading
+13 −10
Original line number Original line Diff line number Diff line
@@ -39,8 +39,10 @@ namespace {
using android::hardware::bluetooth::V1_0::implementation::VendorInterface;
using android::hardware::bluetooth::V1_0::implementation::VendorInterface;
using android::hardware::hidl_vec;
using android::hardware::hidl_vec;


tINT_CMD_CBACK internal_command_cb;
struct {
uint16_t internal_command_opcode;
  tINT_CMD_CBACK cb;
  uint16_t opcode;
} internal_command;


VendorInterface* g_vendor_interface = nullptr;
VendorInterface* g_vendor_interface = nullptr;


@@ -105,19 +107,20 @@ bool internal_command_event_match(const hidl_vec<uint8_t>& packet) {


  uint16_t opcode = packet[opcode_offset] | (packet[opcode_offset + 1] << 8);
  uint16_t opcode = packet[opcode_offset] | (packet[opcode_offset + 1] << 8);


  ALOGV("%s internal_command_opcode = %04X opcode = %04x", __func__,
  ALOGV("%s internal_command.opcode = %04X opcode = %04x", __func__,
        internal_command_opcode, opcode);
        internal_command.opcode, opcode);
  return opcode == internal_command_opcode;
  return opcode == internal_command.opcode;
}
}


uint8_t transmit_cb(uint16_t opcode, void* buffer, tINT_CMD_CBACK callback) {
uint8_t transmit_cb(uint16_t opcode, void* buffer, tINT_CMD_CBACK callback) {
  ALOGV("%s opcode: 0x%04x, ptr: %p, cb: %p", __func__, opcode, buffer,
  ALOGV("%s opcode: 0x%04x, ptr: %p, cb: %p", __func__, opcode, buffer,
        callback);
        callback);
  internal_command_cb = callback;
  internal_command.cb = callback;
  internal_command_opcode = opcode;
  internal_command.opcode = opcode;
  uint8_t type = HCI_PACKET_TYPE_COMMAND;
  uint8_t type = HCI_PACKET_TYPE_COMMAND;
  HC_BT_HDR* bt_hdr = reinterpret_cast<HC_BT_HDR*>(buffer);
  HC_BT_HDR* bt_hdr = reinterpret_cast<HC_BT_HDR*>(buffer);
  VendorInterface::get()->Send(type, bt_hdr->data, bt_hdr->len);
  VendorInterface::get()->Send(type, bt_hdr->data, bt_hdr->len);
  delete[] reinterpret_cast<uint8_t*>(buffer);
  return true;
  return true;
}
}


@@ -367,15 +370,15 @@ void VendorInterface::OnDataReady(int fd) {
      hci_packet_bytes_remaining_ -= bytes_read;
      hci_packet_bytes_remaining_ -= bytes_read;
      hci_packet_bytes_read_ += bytes_read;
      hci_packet_bytes_read_ += bytes_read;
      if (hci_packet_bytes_remaining_ == 0) {
      if (hci_packet_bytes_remaining_ == 0) {
        if (internal_command_cb != nullptr &&
        if (internal_command.cb != nullptr &&
            hci_packet_type_ == HCI_PACKET_TYPE_EVENT &&
            hci_packet_type_ == HCI_PACKET_TYPE_EVENT &&
            internal_command_event_match(hci_packet_)) {
            internal_command_event_match(hci_packet_)) {
          HC_BT_HDR* bt_hdr =
          HC_BT_HDR* bt_hdr =
              WrapPacketAndCopy(HCI_PACKET_TYPE_EVENT, hci_packet_);
              WrapPacketAndCopy(HCI_PACKET_TYPE_EVENT, hci_packet_);


          // The callbacks can send new commands, so don't zero after calling.
          // The callbacks can send new commands, so don't zero after calling.
          tINT_CMD_CBACK saved_cb = internal_command_cb;
          tINT_CMD_CBACK saved_cb = internal_command.cb;
          internal_command_cb = nullptr;
          internal_command.cb = nullptr;
          saved_cb(bt_hdr);
          saved_cb(bt_hdr);
        } else {
        } else {
          packet_read_cb_(hci_packet_type_, hci_packet_);
          packet_read_cb_(hci_packet_type_, hci_packet_);