Commit 2a46a8e3 authored by Unknown's avatar Unknown
Browse files

Actually remove the old bluetooth hal

parent 6b2ff4a0
Loading
Loading
Loading
Loading

bluetooth/Android.bp

deleted100644 → 0
+0 −40
Original line number Diff line number Diff line
//
// Copyright (C) 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.

cc_binary {
    name: "android.hardware.bluetooth@1.0-service.mtk",
    proprietary: true,
    relative_install_path: "hw",
    srcs: [
        "async_fd_watcher.cc",
        "bluetooth_hci.cc",
        "h4_protocol.cc",
        "hci_packetizer.cc",
        "hci_protocol.cc",
        "service.cc",
    ],
    shared_libs: [
        "android.hardware.bluetooth@1.0",
        "libbase",
        "libcutils",
        "libhardware",
        "libhwbinder",
        "libhidlbase",
        "libhidltransport",
        "liblog",
        "libutils",
    ],
    init_rc: ["android.hardware.bluetooth@1.0-service.mtk.rc"],
}
+0 −5
Original line number Diff line number Diff line
service bluetooth-1-0 /vendor/bin/hw/android.hardware.bluetooth@1.0-service.mtk
    class hal
    user bluetooth
    group bluetooth
    writepid /dev/stune/foreground/tasks

bluetooth/async_fd_watcher.cc

deleted100644 → 0
+0 −181
Original line number Diff line number Diff line
//
// Copyright 2016 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 "async_fd_watcher.h"

#include <algorithm>
#include <atomic>
#include <condition_variable>
#include <map>
#include <mutex>
#include <thread>
#include <vector>
#include "fcntl.h"
#include "sys/select.h"
#include "unistd.h"

static const int INVALID_FD = -1;

namespace android {
namespace hardware {
namespace bluetooth {
namespace async {

int AsyncFdWatcher::WatchFdForNonBlockingReads(
    int file_descriptor, const ReadCallback& on_read_fd_ready_callback) {
  // Add file descriptor and callback
  {
    std::unique_lock<std::mutex> guard(internal_mutex_);
    watched_fds_[file_descriptor] = on_read_fd_ready_callback;
  }

  // Start the thread if not started yet
  return tryStartThread();
}

int AsyncFdWatcher::ConfigureTimeout(
    const std::chrono::milliseconds timeout,
    const TimeoutCallback& on_timeout_callback) {
  // Add timeout and callback
  {
    std::unique_lock<std::mutex> guard(timeout_mutex_);
    timeout_cb_ = on_timeout_callback;
    timeout_ms_ = timeout;
  }

  notifyThread();
  return 0;
}

void AsyncFdWatcher::StopWatchingFileDescriptors() { stopThread(); }

AsyncFdWatcher::~AsyncFdWatcher() {}

// Make sure to call this with at least one file descriptor ready to be
// watched upon or the thread routine will return immediately
int AsyncFdWatcher::tryStartThread() {
  if (std::atomic_exchange(&running_, true)) return 0;

  // Set up the communication channel
  int pipe_fds[2];
  if (pipe2(pipe_fds, O_NONBLOCK)) return -1;

  notification_listen_fd_ = pipe_fds[0];
  notification_write_fd_ = pipe_fds[1];

  thread_ = std::thread([this]() { ThreadRoutine(); });
  if (!thread_.joinable()) return -1;

  return 0;
}

int AsyncFdWatcher::stopThread() {
  if (!std::atomic_exchange(&running_, false)) return 0;

  notifyThread();
  if (std::this_thread::get_id() != thread_.get_id()) {
    thread_.join();
  }

  {
    std::unique_lock<std::mutex> guard(internal_mutex_);
    watched_fds_.clear();
  }

  {
    std::unique_lock<std::mutex> guard(timeout_mutex_);
    timeout_cb_ = nullptr;
  }

  return 0;
}

int AsyncFdWatcher::notifyThread() {
  uint8_t buffer[] = {0};
  if (TEMP_FAILURE_RETRY(write(notification_write_fd_, &buffer, 1)) < 0) {
    return -1;
  }
  return 0;
}

void AsyncFdWatcher::ThreadRoutine() {
  while (running_) {
    fd_set read_fds;
    FD_ZERO(&read_fds);
    FD_SET(notification_listen_fd_, &read_fds);
    int max_read_fd = INVALID_FD;
    for (auto& it : watched_fds_) {
      FD_SET(it.first, &read_fds);
      max_read_fd = std::max(max_read_fd, it.first);
    }

    struct timeval timeout;
    struct timeval* timeout_ptr = NULL;
    if (timeout_ms_ > std::chrono::milliseconds(0)) {
      timeout.tv_sec = timeout_ms_.count() / 1000;
      timeout.tv_usec = (timeout_ms_.count() % 1000) * 1000;
      timeout_ptr = &timeout;
    }

    // Wait until there is data available to read on some FD.
    int nfds = std::max(notification_listen_fd_, max_read_fd);
    int retval = select(nfds + 1, &read_fds, NULL, NULL, timeout_ptr);

    // There was some error.
    if (retval < 0) continue;

    // Timeout.
    if (retval == 0) {
      // Allow the timeout callback to modify the timeout.
      TimeoutCallback saved_cb;
      {
        std::unique_lock<std::mutex> guard(timeout_mutex_);
        if (timeout_ms_ > std::chrono::milliseconds(0)) saved_cb = timeout_cb_;
      }
      if (saved_cb != nullptr) saved_cb();
      continue;
    }

    // Read data from the notification FD.
    if (FD_ISSET(notification_listen_fd_, &read_fds)) {
      char buffer[] = {0};
      TEMP_FAILURE_RETRY(read(notification_listen_fd_, buffer, 1));
      continue;
    }

    // Invoke the data ready callbacks if appropriate.
    std::vector<decltype(watched_fds_)::value_type> saved_callbacks;
    {
      std::unique_lock<std::mutex> guard(internal_mutex_);
      for (auto& it : watched_fds_) {
        if (FD_ISSET(it.first, &read_fds)) {
          saved_callbacks.push_back(it);
        }
      }
    }

    for (auto& it : saved_callbacks) {
      if (it.second) {
        it.second(it.first);
      }
    }
  }
}

}  // namespace async
}  // namespace bluetooth
}  // namespace hardware
}  // namespace android

bluetooth/async_fd_watcher.h

deleted100644 → 0
+0 −66
Original line number Diff line number Diff line
//
// Copyright 2016 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 <map>
#include <mutex>
#include <thread>

namespace android {
namespace hardware {
namespace bluetooth {
namespace async {

using ReadCallback = std::function<void(int)>;
using TimeoutCallback = std::function<void(void)>;

class AsyncFdWatcher {
 public:
  AsyncFdWatcher() = default;
  ~AsyncFdWatcher();

  int WatchFdForNonBlockingReads(int file_descriptor,
                                 const ReadCallback& on_read_fd_ready_callback);
  int ConfigureTimeout(const std::chrono::milliseconds timeout,
                       const TimeoutCallback& on_timeout_callback);
  void StopWatchingFileDescriptors();

 private:
  AsyncFdWatcher(const AsyncFdWatcher&) = delete;
  AsyncFdWatcher& operator=(const AsyncFdWatcher&) = delete;

  int tryStartThread();
  int stopThread();
  int notifyThread();
  void ThreadRoutine();

  std::atomic_bool running_{false};
  std::thread thread_;
  std::mutex internal_mutex_;
  std::mutex timeout_mutex_;

  std::map<int, ReadCallback> watched_fds_;
  int notification_listen_fd_;
  int notification_write_fd_;
  TimeoutCallback timeout_cb_;
  std::chrono::milliseconds timeout_ms_;
};

}  // namespace async
}  // namespace bluetooth
}  // namespace hardware
}  // namespace android

bluetooth/bluetooth_hci.cc

deleted100644 → 0
+0 −116
Original line number Diff line number Diff line
//
// Copyright 2016 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.
//

#define LOG_TAG "android.hardware.bluetooth@1.0.mtk"

#include "bluetooth_hci.h"

#include <android-base/logging.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <unistd.h>
#include <utils/Log.h>

namespace android {
namespace hardware {
namespace bluetooth {
namespace V1_0 {
namespace mtk {

using android::hardware::hidl_vec;

BluetoothHci::BluetoothHci()
    : deathRecipient(new BluetoothDeathRecipient(this)) {}

Return<void> BluetoothHci::initialize(
    const ::android::sp<IBluetoothHciCallbacks>& cb) {
  ALOGI("BluetoothHci::initialize()");

  stpbt_fd_ = open("/dev/stpbt", O_RDWR);
  if (stpbt_fd_ < 0) {
    ALOGE("%s: Can't open stpbt (%s)", __func__, strerror(errno));
    cb->initializationComplete(Status::INITIALIZATION_ERROR);
    return Void();
  }

  event_cb_ = cb;
  event_cb_->linkToDeath(deathRecipient, 0);

  hci_ = new hci::H4Protocol(
      stpbt_fd_,
      [cb](const hidl_vec<uint8_t>& packet) { cb->hciEventReceived(packet); },
      [cb](const hidl_vec<uint8_t>& packet) { cb->aclDataReceived(packet); },
      [cb](const hidl_vec<uint8_t>& packet) { cb->scoDataReceived(packet); });

  fd_watcher_.WatchFdForNonBlockingReads(stpbt_fd_, [hci_](int fd) {
    int tty_bytes = 0;
    ALOGV("%s:tty_bytes = %d", __func__, tty_bytes);

    uint8_t* tmp_buffer = new uint8_t[tty_bytes];
    size_t bytes_read = TEMP_FAILURE_RETRY(read(fd, tmp_buffer, tty_bytes));
    CHECK(static_cast<int>(bytes_read) == tty_bytes);
    size_t bytes_written =
        TEMP_FAILURE_RETRY(write(shim_fd, tmp_buffer, tty_bytes));
    CHECK(static_cast<int>(bytes_written) == tty_bytes);
    delete[] tmp_buffer;
  });

  fd_watcher_.WatchFdForNonBlockingReads(
      for_hci, [this](int fd) { hci_->OnDataReady(fd); });

  cb->initializationComplete(Status::SUCCESS);
  return Void();
}

Return<void> BluetoothHci::close() {
  ALOGI("BluetoothHci::close()");

  if (stpbt_fd_ >= 0) {
    fd_watcher_.StopWatchingFileDescriptors();
    ::close(stpbt_fd_);
    stpbt_fd_ = -1;
  }

  event_cb_->unlinkToDeath(deathRecipient);

  if (hci_ != nullptr) {
    delete hci_;
    hci_ = nullptr;
  }

  return Void();
}

Return<void> BluetoothHci::sendHciCommand(const hidl_vec<uint8_t>& packet) {
  hci_->Send(HCI_PACKET_TYPE_COMMAND, packet.data(), packet.size());
  return Void();
}

Return<void> BluetoothHci::sendAclData(const hidl_vec<uint8_t>& packet) {
  hci_->Send(HCI_PACKET_TYPE_ACL_DATA, packet.data(), packet.size());
  return Void();
}

Return<void> BluetoothHci::sendScoData(const hidl_vec<uint8_t>& packet) {
  hci_->Send(HCI_PACKET_TYPE_SCO_DATA, packet.data(), packet.size());
  return Void();
}

}  // namespace mtk
}  // namespace V1_0
}  // namespace bluetooth
}  // namespace hardware
}  // namespace android
Loading