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

Commit 1daf027d authored by Myles Watson's avatar Myles Watson Committed by android-build-merger
Browse files

Bluetooth: Make Send() send a complete packet

am: df765eab

Change-Id: I742da2de02c1205ced2c9c62a28dba22d9060520
parents 00802c2a df765eab
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -83,8 +83,7 @@ Return<void> BluetoothHci::sendScoData(const hidl_vec<uint8_t>& data) {

void BluetoothHci::sendDataToController(const uint8_t type,
                                        const hidl_vec<uint8_t>& data) {
  VendorInterface::get()->Send(&type, 1);
  VendorInterface::get()->Send(data.data(), data.size());
  VendorInterface::get()->Send(type, data.data(), data.size());
}

IBluetoothHci* HIDL_FETCH_IBluetoothHci(const char* /* name */) {
+30 −23
Original line number Diff line number Diff line
@@ -70,6 +70,30 @@ HC_BT_HDR* WrapPacketAndCopy(uint16_t event, const hidl_vec<uint8_t>& data) {
  return packet;
}

size_t write_safely(int fd, const uint8_t* data, size_t length) {
  size_t transmitted_length = 0;
  while (length > 0) {
    ssize_t ret =
        TEMP_FAILURE_RETRY(write(fd, data + transmitted_length, length));

    if (ret == -1) {
      if (errno == EAGAIN) continue;
      ALOGE("%s error writing to UART (%s)", __func__, strerror(errno));
      break;

    } else if (ret == 0) {
      // Nothing written :(
      ALOGE("%s zero bytes written - something went wrong...", __func__);
      break;
    }

    transmitted_length += ret;
    length -= ret;
  }

  return transmitted_length;
}

bool internal_command_event_match(const hidl_vec<uint8_t>& packet) {
  uint8_t event_code = packet[0];
  if (event_code != HCI_COMMAND_COMPLETE_EVENT) {
@@ -92,9 +116,8 @@ uint8_t transmit_cb(uint16_t opcode, void* buffer, tINT_CMD_CBACK callback) {
  internal_command_cb = callback;
  internal_command_opcode = opcode;
  uint8_t type = HCI_PACKET_TYPE_COMMAND;
  VendorInterface::get()->Send(&type, 1);
  HC_BT_HDR* bt_hdr = reinterpret_cast<HC_BT_HDR*>(buffer);
  VendorInterface::get()->Send(bt_hdr->data, bt_hdr->len);
  VendorInterface::get()->Send(type, bt_hdr->data, bt_hdr->len);
  return true;
}

@@ -273,30 +296,14 @@ void VendorInterface::Close() {
  }
}

size_t VendorInterface::Send(const uint8_t* data, size_t length) {
size_t VendorInterface::Send(uint8_t type, const uint8_t* data, size_t length) {
  if (uart_fd_ == INVALID_FD) return 0;

  size_t transmitted_length = 0;
  while (length > 0) {
    ssize_t ret =
        TEMP_FAILURE_RETRY(write(uart_fd_, data + transmitted_length, length));

    if (ret == -1) {
      if (errno == EAGAIN) continue;
      ALOGE("%s error writing to UART (%s)", __func__, strerror(errno));
      break;

    } else if (ret == 0) {
      // Nothing written :(
      ALOGE("%s zero bytes written - something went wrong...", __func__);
      break;
    }
  int rv = write_safely(uart_fd_, &type, sizeof(type));
  if (rv == sizeof(type))
    rv = write_safely(uart_fd_, data, length);

    transmitted_length += ret;
    length -= ret;
  }

  return transmitted_length;
  return rv;
}

void VendorInterface::OnFirmwareConfigured(uint8_t result) {
+1 −1
Original line number Diff line number Diff line
@@ -42,7 +42,7 @@ class VendorInterface {
  static void Shutdown();
  static VendorInterface *get();

  size_t Send(const uint8_t *data, size_t length);
  size_t Send(uint8_t type, const uint8_t *data, size_t length);

  void OnFirmwareConfigured(uint8_t result);