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

Commit 3fe30d25 authored by Myles Watson's avatar Myles Watson
Browse files

RootCanal: Remove dead code

Tag: #feature
Test: cert/run --host
Bug: 163818400
Change-Id: I26a1a44961865f7356663c5a0087289a3601ef8c
parent 53cb692e
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -23,8 +23,6 @@ cc_library_static {
        "model/devices/device.cc",
        "model/devices/device_properties.cc",
        "model/devices/h4_packetizer.cc",
        "model/devices/h4_protocol.cc",
        "model/devices/hci_packetizer.cc",
        "model/devices/hci_protocol.cc",
        "model/devices/hci_socket_device.cc",
        "model/devices/keyboard.cc",
+0 −105
Original line number Diff line number Diff line
//
// Copyright 2017 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#include "h4_protocol.h"

#include <errno.h>
#include <fcntl.h>
#include <sys/uio.h>
#include <unistd.h>

#include "os/log.h"

namespace test_vendor_lib {
namespace hci {

H4Protocol::H4Protocol(int fd, PacketReadCallback command_cb, PacketReadCallback event_cb, PacketReadCallback acl_cb,
                       PacketReadCallback sco_cb)
    : uart_fd_(fd), command_cb_(command_cb), event_cb_(event_cb), acl_cb_(acl_cb), sco_cb_(sco_cb),
      hci_packetizer_([this]() {
        LOG_INFO("in lambda");
        this->OnPacketReady();
      }) {}

size_t H4Protocol::Send(uint8_t type, const uint8_t* data, size_t length) {
  struct iovec iov[] = {{&type, sizeof(type)}, {const_cast<uint8_t*>(data), length}};
  ssize_t ret = 0;
  do {
    ret = TEMP_FAILURE_RETRY(writev(uart_fd_, iov, sizeof(iov) / sizeof(iov[0])));
  } while (-1 == ret && EAGAIN == errno);

  if (ret == -1) {
    LOG_ERROR("%s error writing to UART (%s)", __func__, strerror(errno));
  } else if (ret < static_cast<ssize_t>(length + 1)) {
    LOG_ERROR("%s: %d / %d bytes written - something went wrong...", __func__, static_cast<int>(ret),
              static_cast<int>(length + 1));
  }
  return ret;
}

void H4Protocol::OnPacketReady() {
  LOG_ERROR("%s: before switch", __func__);
  switch (hci_packet_type_) {
    case hci::PacketType::COMMAND:
      command_cb_(hci_packetizer_.GetPacket());
      break;
    case hci::PacketType::ACL:
      acl_cb_(hci_packetizer_.GetPacket());
      break;
    case hci::PacketType::SCO:
      sco_cb_(hci_packetizer_.GetPacket());
      break;
    case hci::PacketType::EVENT:
      event_cb_(hci_packetizer_.GetPacket());
      break;
    default:
      LOG_ALWAYS_FATAL("%s: Unimplemented packet type %d", __func__, static_cast<int>(hci_packet_type_));
  }
  // Get ready for the next type byte.
  hci_packet_type_ = hci::PacketType::UNKNOWN;
}

void H4Protocol::OnDataReady(int fd) {
  if (hci_packet_type_ == hci::PacketType::UNKNOWN) {
    uint8_t buffer[1] = {0};
    ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, 1));
    if (bytes_read != 1) {
      if (bytes_read == 0) {
        LOG_INFO("%s: Nothing ready, will retry!", __func__);
        return;
      } else if (bytes_read < 0) {
        if (errno == EAGAIN) {
          // No data, try again later.
          return;
        } else {
          LOG_ALWAYS_FATAL("%s: Read packet type error: %s", __func__, strerror(errno));
        }
      } else {
        LOG_ALWAYS_FATAL("%s: More bytes read than expected (%u)!", __func__, static_cast<unsigned int>(bytes_read));
      }
    }
    hci_packet_type_ = static_cast<hci::PacketType>(buffer[0]);
    if (hci_packet_type_ != hci::PacketType::ACL && hci_packet_type_ != hci::PacketType::SCO &&
        hci_packet_type_ != hci::PacketType::COMMAND && hci_packet_type_ != hci::PacketType::EVENT) {
      LOG_ALWAYS_FATAL("%s: Unimplemented packet type %d", __func__, static_cast<int>(hci_packet_type_));
    }
  } else {
    hci_packetizer_.OnDataReady(fd, hci_packet_type_);
  }
}

}  // namespace hci
}  // namespace test_vendor_lib
+0 −49
Original line number Diff line number Diff line
//
// Copyright 2017 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#pragma once

#include "hci_protocol.h"
#include "include/hci.h"

namespace test_vendor_lib {
namespace hci {

class H4Protocol : public HciProtocol {
 public:
  H4Protocol(int fd, PacketReadCallback command_cb, PacketReadCallback event_cb, PacketReadCallback acl_cb,
             PacketReadCallback sco_cb);

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

  void OnPacketReady();

  void OnDataReady(int fd);

 private:
  int uart_fd_;

  PacketReadCallback command_cb_;
  PacketReadCallback event_cb_;
  PacketReadCallback acl_cb_;
  PacketReadCallback sco_cb_;

  hci::PacketType hci_packet_type_{hci::PacketType::UNKNOWN};
  hci::HciPacketizer hci_packetizer_;
};

}  // namespace hci
}  // namespace test_vendor_lib
+0 −128
Original line number Diff line number Diff line
//
// Copyright 2017 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#include "hci_packetizer.h"

#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

#include "os/log.h"

namespace test_vendor_lib {
namespace hci {
constexpr size_t HciPacketizer::COMMAND_PREAMBLE_SIZE;
constexpr size_t HciPacketizer::COMMAND_LENGTH_OFFSET;
constexpr size_t HciPacketizer::ACL_PREAMBLE_SIZE;
constexpr size_t HciPacketizer::ACL_LENGTH_OFFSET;
constexpr size_t HciPacketizer::SCO_PREAMBLE_SIZE;
constexpr size_t HciPacketizer::SCO_LENGTH_OFFSET;
constexpr size_t HciPacketizer::EVENT_PREAMBLE_SIZE;
constexpr size_t HciPacketizer::EVENT_LENGTH_OFFSET;

constexpr size_t HciPacketizer::PREAMBLE_SIZE_MAX;

size_t HciPacketizer::HciGetPacketLengthForType(hci::PacketType type, const uint8_t* preamble) {
  static const size_t packet_length_offset[static_cast<size_t>(hci::PacketType::EVENT) + 1] = {
      0,
      HciPacketizer::COMMAND_LENGTH_OFFSET,
      HciPacketizer::ACL_LENGTH_OFFSET,
      HciPacketizer::SCO_LENGTH_OFFSET,
      HciPacketizer::EVENT_LENGTH_OFFSET,
  };

  size_t offset = packet_length_offset[static_cast<size_t>(type)];
  if (type != hci::PacketType::ACL) return preamble[offset];
  return (((preamble[offset + 1]) << 8) | preamble[offset]);
}

const std::vector<uint8_t>& HciPacketizer::GetPacket() const {
  return packet_;
}

void HciPacketizer::OnDataReady(int fd, hci::PacketType packet_type) {
  static const size_t preamble_size[static_cast<size_t>(hci::PacketType::EVENT) + 1] = {
      0,
      HciPacketizer::COMMAND_PREAMBLE_SIZE,
      HciPacketizer::ACL_PREAMBLE_SIZE,
      HciPacketizer::SCO_PREAMBLE_SIZE,
      HciPacketizer::EVENT_PREAMBLE_SIZE,
  };

  switch (state_) {
    case HCI_PREAMBLE: {
      ssize_t bytes_read = TEMP_FAILURE_RETRY(
          read(fd, preamble_ + bytes_read_, preamble_size[static_cast<uint8_t>(packet_type)] - bytes_read_));
      if (bytes_read == 0) {
        LOG_ERROR("%s: Will try again to read the header!", __func__);
        return;
      }
      if (bytes_read < 0) {
        // Ignore temporary failures.
        if (errno == EAGAIN) {
          return;
        }
        LOG_ALWAYS_FATAL("Read header error: %s", strerror(errno));
      }
      bytes_read_ += bytes_read;
      if (bytes_read_ == preamble_size[static_cast<uint8_t>(packet_type)]) {
        size_t packet_length = HciGetPacketLengthForType(packet_type, preamble_);
        packet_.resize(preamble_size[static_cast<uint8_t>(packet_type)] + packet_length);
        memcpy(packet_.data(), preamble_, preamble_size[static_cast<uint8_t>(packet_type)]);
        LOG_INFO("%s: Read a preamble of size %d length %d %0x %0x %0x", __func__, static_cast<int>(bytes_read_),
                 static_cast<int>(packet_length), preamble_[0], preamble_[1], preamble_[2]);
        bytes_remaining_ = packet_length;
        if (bytes_remaining_ == 0) {
          packet_ready_cb_();
          state_ = HCI_PREAMBLE;
          bytes_read_ = 0;
        } else {
          state_ = HCI_PAYLOAD;
          bytes_read_ = 0;
        }
      }
      break;
    }

    case HCI_PAYLOAD: {
      ssize_t bytes_read = TEMP_FAILURE_RETRY(
          read(fd, packet_.data() + preamble_size[static_cast<uint8_t>(packet_type)] + bytes_read_, bytes_remaining_));
      if (bytes_read == 0) {
        LOG_INFO("Will try again to read the payload!");
        return;
      }
      if (bytes_read < 0) {
        // Ignore temporary failures.
        if (errno == EAGAIN) {
          return;
        }
        LOG_ALWAYS_FATAL("Read payload error: %s", strerror(errno));
      }
      bytes_remaining_ -= bytes_read;
      bytes_read_ += bytes_read;
      if (bytes_remaining_ == 0) {
        packet_ready_cb_();
        state_ = HCI_PREAMBLE;
        bytes_read_ = 0;
      }
      break;
    }
  }
}

}  // namespace hci
}  // namespace test_vendor_lib
+0 −68
Original line number Diff line number Diff line
//
// Copyright 2017 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#pragma once

#include <functional>
#include <vector>

#include "include/hci.h"

namespace test_vendor_lib {
namespace hci {

using HciPacketReadyCallback = std::function<void(void)>;

class HciPacketizer {
 public:
  HciPacketizer(HciPacketReadyCallback packet_cb) : packet_ready_cb_(packet_cb){};
  void OnDataReady(int fd, hci::PacketType packet_type);
  const std::vector<uint8_t>& GetPacket() const;

 private:
  // 2 bytes for opcode, 1 byte for parameter length (Volume 2, Part E, 5.4.1)
  static constexpr size_t COMMAND_PREAMBLE_SIZE = 3;
  static constexpr size_t COMMAND_LENGTH_OFFSET = 2;

  // 2 bytes for handle, 2 bytes for data length (Volume 2, Part E, 5.4.2)
  static constexpr size_t ACL_PREAMBLE_SIZE = 4;
  static constexpr size_t ACL_LENGTH_OFFSET = 2;

  // 2 bytes for handle, 1 byte for data length (Volume 2, Part E, 5.4.3)
  static constexpr size_t SCO_PREAMBLE_SIZE = 3;
  static constexpr size_t SCO_LENGTH_OFFSET = 2;

  // 1 byte for event code, 1 byte for parameter length (Volume 2, Part
  // E, 5.4.4)
  static constexpr size_t EVENT_PREAMBLE_SIZE = 2;
  static constexpr size_t EVENT_LENGTH_OFFSET = 1;

  static constexpr size_t PREAMBLE_SIZE_MAX = ACL_PREAMBLE_SIZE;

  size_t HciGetPacketLengthForType(hci::PacketType type, const uint8_t* preamble);

 protected:
  enum State { HCI_PREAMBLE, HCI_PAYLOAD };
  State state_{HCI_PREAMBLE};
  uint8_t preamble_[PREAMBLE_SIZE_MAX]{};
  std::vector<uint8_t> packet_;
  size_t bytes_remaining_{0};
  size_t bytes_read_{0};
  HciPacketReadyCallback packet_ready_cb_;
};

}  // namespace hci
}  // namespace test_vendor_lib
Loading