summaryrefslogtreecommitdiff
path: root/system/gd/rust/stack/src/hal/ffi/hidl.cc
blob: fd8e1710092dc628cdf9667256b6d8045859b942 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "src/hal/ffi/hidl.h"

#include <android/hardware/bluetooth/1.0/types.h>
#include <android/hardware/bluetooth/1.1/IBluetoothHci.h>
#include <android/hardware/bluetooth/1.1/IBluetoothHciCallbacks.h>
#include <stdlib.h>

#include "gd/os/log.h"

using ::android::hardware::hidl_vec;
using ::android::hardware::Return;
using ::android::hardware::Void;
using ::android::hardware::bluetooth::V1_1::IBluetoothHci;
using ::android::hardware::bluetooth::V1_1::IBluetoothHciCallbacks;
using HidlStatus = ::android::hardware::bluetooth::V1_0::Status;
using IBluetoothHci_1_0 = ::android::hardware::bluetooth::V1_0::IBluetoothHci;

namespace bluetooth {
namespace hal {
namespace {

class HciDeathRecipient : public ::android::hardware::hidl_death_recipient {
 public:
  virtual void serviceDied(uint64_t /*cookie*/, const android::wp<::android::hidl::base::V1_0::IBase>& /*who*/) {
    LOG_ERROR("Bluetooth HAL service died!");
    abort();
  }
};

class HciCallbackTrampoline : public IBluetoothHciCallbacks {
 public:
  HciCallbackTrampoline() {}

  Return<void> initializationComplete(HidlStatus status) {
    ASSERT(status == HidlStatus::SUCCESS);
    on_init_complete();
    return Void();
  }

  Return<void> hciEventReceived(const hidl_vec<uint8_t>& event) {
    on_event(rust::Slice(&event[0], event.size()));
    return Void();
  }

  Return<void> aclDataReceived(const hidl_vec<uint8_t>& data) {
    on_acl(rust::Slice(&data[0], data.size()));
    return Void();
  }

  Return<void> scoDataReceived(const hidl_vec<uint8_t>& data) {
    on_sco(rust::Slice(&data[0], data.size()));
    return Void();
  }

  Return<void> isoDataReceived(const hidl_vec<uint8_t>& data) {
    on_iso(rust::Slice(&data[0], data.size()));
    return Void();
  }
};

android::sp<HciDeathRecipient> hci_death_recipient_ = new HciDeathRecipient();
android::sp<IBluetoothHci_1_0> bt_hci_;
android::sp<IBluetoothHci> bt_hci_1_1_;
android::sp<HciCallbackTrampoline> trampoline_;

}  // namespace

void start_hal() {
  ASSERT(bt_hci_ == nullptr);

  bt_hci_1_1_ = IBluetoothHci::getService();
  if (bt_hci_1_1_ != nullptr) {
    bt_hci_ = bt_hci_1_1_;
  } else {
    bt_hci_ = IBluetoothHci_1_0::getService();
  }

  ASSERT(bt_hci_ != nullptr);
  auto death_link = bt_hci_->linkToDeath(hci_death_recipient_, 0);
  ASSERT_LOG(death_link.isOk(), "Unable to set the death recipient for the Bluetooth HAL");

  trampoline_ = new HciCallbackTrampoline();
  if (bt_hci_1_1_ != nullptr) {
    bt_hci_1_1_->initialize_1_1(trampoline_);
  } else {
    bt_hci_->initialize(trampoline_);
  }
}

void stop_hal() {
  ASSERT(bt_hci_ != nullptr);

  auto death_unlink = bt_hci_->unlinkToDeath(hci_death_recipient_);
  if (!death_unlink.isOk()) {
    LOG_ERROR("Error unlinking death recipient from the Bluetooth HAL");
  }
  bt_hci_->close();
  bt_hci_ = nullptr;
  bt_hci_1_1_ = nullptr;
  trampoline_ = nullptr;
}

void send_command(rust::Slice<const uint8_t> data) {
  ASSERT(bt_hci_ != nullptr);
  bt_hci_->sendHciCommand(hidl_vec<uint8_t>(data.data(), data.data() + data.length()));
}

void send_acl(rust::Slice<const uint8_t> data) {
  ASSERT(bt_hci_ != nullptr);
  bt_hci_->sendAclData(hidl_vec<uint8_t>(data.data(), data.data() + data.length()));
}

void send_sco(rust::Slice<const uint8_t> data) {
  ASSERT(bt_hci_ != nullptr);
  bt_hci_->sendScoData(hidl_vec<uint8_t>(data.data(), data.data() + data.length()));
}

void send_iso(rust::Slice<const uint8_t> data) {
  if (bt_hci_1_1_ == nullptr) {
    LOG_ERROR("ISO is not supported in HAL v1.0");
    return;
  }

  ASSERT(bt_hci_ != nullptr);
  bt_hci_1_1_->sendIsoData(hidl_vec<uint8_t>(data.data(), data.data() + data.length()));
}

}  // namespace hal
}  // namespace bluetooth