1 /*
2 * Copyright 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "distance_measurement_manager.h"
18
19 #include "bta/include/bta_ras_api.h"
20 #include "btif/include/btif_common.h"
21 #include "hci/distance_measurement_manager.h"
22 #include "main/shim/entry.h"
23 #include "main/shim/helpers.h"
24
25 using bluetooth::hci::DistanceMeasurementErrorCode;
26 using bluetooth::hci::DistanceMeasurementMethod;
27
28 class DistanceMeasurementInterfaceImpl
29 : public DistanceMeasurementInterface,
30 public bluetooth::hci::DistanceMeasurementCallbacks,
31 public bluetooth::ras::RasServerCallbacks,
32 public bluetooth::ras::RasClientCallbacks {
33 public:
~DistanceMeasurementInterfaceImpl()34 ~DistanceMeasurementInterfaceImpl() override{};
35
Init()36 void Init() {
37 // Register callback
38 bluetooth::shim::GetDistanceMeasurementManager()
39 ->RegisterDistanceMeasurementCallbacks(this);
40 bluetooth::ras::GetRasServer()->RegisterCallbacks(this);
41 bluetooth::ras::GetRasClient()->RegisterCallbacks(this);
42 }
43
RegisterDistanceMeasurementCallbacks(::DistanceMeasurementCallbacks * callbacks)44 void RegisterDistanceMeasurementCallbacks(
45 ::DistanceMeasurementCallbacks* callbacks) {
46 distance_measurement_callbacks_ = callbacks;
47 }
48
StartDistanceMeasurement(RawAddress raw_address,uint16_t interval,uint8_t method)49 void StartDistanceMeasurement(RawAddress raw_address, uint16_t interval,
50 uint8_t method) {
51 bluetooth::shim::GetDistanceMeasurementManager()->StartDistanceMeasurement(
52 bluetooth::ToGdAddress(raw_address), interval,
53 static_cast<DistanceMeasurementMethod>(method));
54 bluetooth::ras::GetRasClient()->Connect(raw_address);
55 }
56
StopDistanceMeasurement(RawAddress raw_address,uint8_t method)57 void StopDistanceMeasurement(RawAddress raw_address, uint8_t method) {
58 bluetooth::shim::GetDistanceMeasurementManager()->StopDistanceMeasurement(
59 bluetooth::ToGdAddress(raw_address),
60 static_cast<DistanceMeasurementMethod>(method));
61 }
62
63 // Callbacks of bluetooth::hci::DistanceMeasurementCallbacks
OnDistanceMeasurementStarted(bluetooth::hci::Address address,DistanceMeasurementMethod method)64 void OnDistanceMeasurementStarted(bluetooth::hci::Address address,
65 DistanceMeasurementMethod method) override {
66 do_in_jni_thread(base::BindOnce(
67 &::DistanceMeasurementCallbacks::OnDistanceMeasurementStarted,
68 base::Unretained(distance_measurement_callbacks_),
69 bluetooth::ToRawAddress(address), static_cast<uint8_t>(method)));
70 }
71
OnDistanceMeasurementStartFail(bluetooth::hci::Address address,DistanceMeasurementErrorCode reason,DistanceMeasurementMethod method)72 void OnDistanceMeasurementStartFail(
73 bluetooth::hci::Address address, DistanceMeasurementErrorCode reason,
74 DistanceMeasurementMethod method) override {
75 do_in_jni_thread(base::BindOnce(
76 &::DistanceMeasurementCallbacks::OnDistanceMeasurementStartFail,
77 base::Unretained(distance_measurement_callbacks_),
78 bluetooth::ToRawAddress(address), static_cast<uint8_t>(reason),
79 static_cast<uint8_t>(method)));
80 }
81
OnDistanceMeasurementStopped(bluetooth::hci::Address address,DistanceMeasurementErrorCode reason,DistanceMeasurementMethod method)82 void OnDistanceMeasurementStopped(bluetooth::hci::Address address,
83 DistanceMeasurementErrorCode reason,
84 DistanceMeasurementMethod method) override {
85 do_in_jni_thread(base::BindOnce(
86 &::DistanceMeasurementCallbacks::OnDistanceMeasurementStopped,
87 base::Unretained(distance_measurement_callbacks_),
88 bluetooth::ToRawAddress(address), static_cast<uint8_t>(reason),
89 static_cast<uint8_t>(method)));
90 }
91
OnDistanceMeasurementResult(bluetooth::hci::Address address,uint32_t centimeter,uint32_t error_centimeter,int azimuth_angle,int error_azimuth_angle,int altitude_angle,int error_altitude_angle,DistanceMeasurementMethod method)92 void OnDistanceMeasurementResult(bluetooth::hci::Address address,
93 uint32_t centimeter,
94 uint32_t error_centimeter, int azimuth_angle,
95 int error_azimuth_angle, int altitude_angle,
96 int error_altitude_angle,
97 DistanceMeasurementMethod method) override {
98 do_in_jni_thread(base::BindOnce(
99 &::DistanceMeasurementCallbacks::OnDistanceMeasurementResult,
100 base::Unretained(distance_measurement_callbacks_),
101 bluetooth::ToRawAddress(address), centimeter, error_centimeter,
102 azimuth_angle, error_azimuth_angle, altitude_angle,
103 error_altitude_angle, static_cast<uint8_t>(method)));
104 }
105
OnRasFragmentReady(bluetooth::hci::Address address,uint16_t procedure_counter,bool is_last,std::vector<uint8_t> raw_data)106 void OnRasFragmentReady(bluetooth::hci::Address address,
107 uint16_t procedure_counter, bool is_last,
108 std::vector<uint8_t> raw_data) {
109 bluetooth::ras::GetRasServer()->PushProcedureData(
110 bluetooth::ToRawAddress(address), procedure_counter, is_last, raw_data);
111 }
112
OnVendorSpecificCharacteristics(std::vector<bluetooth::hal::VendorSpecificCharacteristic> vendor_specific_characteristics)113 void OnVendorSpecificCharacteristics(
114 std::vector<bluetooth::hal::VendorSpecificCharacteristic>
115 vendor_specific_characteristics) {
116 std::vector<bluetooth::ras::VendorSpecificCharacteristic>
117 ras_vendor_specific_characteristics;
118 for (auto& characteristic : vendor_specific_characteristics) {
119 bluetooth::ras::VendorSpecificCharacteristic
120 vendor_specific_characteristic;
121 vendor_specific_characteristic.characteristicUuid_ =
122 bluetooth::Uuid::From128BitBE(characteristic.characteristicUuid_);
123 vendor_specific_characteristic.value_ = characteristic.value_;
124 ras_vendor_specific_characteristics.emplace_back(
125 vendor_specific_characteristic);
126 }
127 bluetooth::ras::GetRasServer()->SetVendorSpecificCharacteristic(
128 ras_vendor_specific_characteristics);
129 }
130
OnVendorSpecificReply(bluetooth::hci::Address address,std::vector<bluetooth::hal::VendorSpecificCharacteristic> vendor_specific_characteristics)131 void OnVendorSpecificReply(
132 bluetooth::hci::Address address,
133 std::vector<bluetooth::hal::VendorSpecificCharacteristic>
134 vendor_specific_characteristics) {
135 std::vector<bluetooth::ras::VendorSpecificCharacteristic>
136 ras_vendor_specific_characteristics;
137 for (auto& characteristic : vendor_specific_characteristics) {
138 bluetooth::ras::VendorSpecificCharacteristic
139 vendor_specific_characteristic;
140 vendor_specific_characteristic.characteristicUuid_ =
141 bluetooth::Uuid::From128BitBE(characteristic.characteristicUuid_);
142 vendor_specific_characteristic.value_ = characteristic.value_;
143 ras_vendor_specific_characteristics.emplace_back(
144 vendor_specific_characteristic);
145 }
146 bluetooth::ras::GetRasClient()->SendVendorSpecificReply(
147 bluetooth::ToRawAddress(address), ras_vendor_specific_characteristics);
148 }
149
OnHandleVendorSpecificReplyComplete(bluetooth::hci::Address address,bool success)150 void OnHandleVendorSpecificReplyComplete(bluetooth::hci::Address address,
151 bool success) {
152 bluetooth::ras::GetRasServer()->HandleVendorSpecificReplyComplete(
153 bluetooth::ToRawAddress(address), success);
154 };
155
156 // Callbacks of bluetooth::ras::RasServerCallbacks
OnVendorSpecificReply(const RawAddress & address,const std::vector<bluetooth::ras::VendorSpecificCharacteristic> & vendor_specific_reply)157 void OnVendorSpecificReply(
158 const RawAddress& address,
159 const std::vector<bluetooth::ras::VendorSpecificCharacteristic>&
160 vendor_specific_reply) {
161 std::vector<bluetooth::hal::VendorSpecificCharacteristic>
162 hal_vendor_specific_characteristics;
163 for (auto& characteristic : vendor_specific_reply) {
164 bluetooth::hal::VendorSpecificCharacteristic
165 vendor_specific_characteristic;
166 vendor_specific_characteristic.characteristicUuid_ =
167 characteristic.characteristicUuid_.To128BitBE();
168 vendor_specific_characteristic.value_ = characteristic.reply_value_;
169 hal_vendor_specific_characteristics.emplace_back(
170 vendor_specific_characteristic);
171 }
172 bluetooth::shim::GetDistanceMeasurementManager()->HandleVendorSpecificReply(
173 bluetooth::ToGdAddress(address), hal_vendor_specific_characteristics);
174 }
175
176 // Callbacks of bluetooth::ras::RasClientCallbacks
OnConnected(const RawAddress & address,uint16_t att_handle,const std::vector<bluetooth::ras::VendorSpecificCharacteristic> & vendor_specific_characteristics)177 void OnConnected(
178 const RawAddress& address, uint16_t att_handle,
179 const std::vector<bluetooth::ras::VendorSpecificCharacteristic>&
180 vendor_specific_characteristics) {
181 std::vector<bluetooth::hal::VendorSpecificCharacteristic>
182 hal_vendor_specific_characteristics;
183 for (auto& characteristic : vendor_specific_characteristics) {
184 bluetooth::hal::VendorSpecificCharacteristic
185 vendor_specific_characteristic;
186 vendor_specific_characteristic.characteristicUuid_ =
187 characteristic.characteristicUuid_.To128BitBE();
188 vendor_specific_characteristic.value_ = characteristic.value_;
189 hal_vendor_specific_characteristics.emplace_back(
190 vendor_specific_characteristic);
191 }
192
193 bluetooth::shim::GetDistanceMeasurementManager()->HandleRasConnectedEvent(
194 bluetooth::ToGdAddress(address), att_handle,
195 hal_vendor_specific_characteristics);
196 }
197
OnWriteVendorSpecificReplyComplete(const RawAddress & address,bool success)198 void OnWriteVendorSpecificReplyComplete(const RawAddress& address,
199 bool success) {
200 bluetooth::shim::GetDistanceMeasurementManager()
201 ->HandleVendorSpecificReplyComplete(bluetooth::ToGdAddress(address),
202 success);
203 }
204
OnRemoteData(const RawAddress & address,const std::vector<uint8_t> & data)205 void OnRemoteData(const RawAddress& address,
206 const std::vector<uint8_t>& data) {
207 bluetooth::shim::GetDistanceMeasurementManager()->HandleRemoteData(
208 bluetooth::ToGdAddress(address), data);
209 }
210
211 private:
212 ::DistanceMeasurementCallbacks* distance_measurement_callbacks_;
213 };
214
215 DistanceMeasurementInterfaceImpl* distance_measurement_instance = nullptr;
216
init_distance_measurement_manager()217 void bluetooth::shim::init_distance_measurement_manager() {
218 static_cast<DistanceMeasurementInterfaceImpl*>(
219 bluetooth::shim::get_distance_measurement_instance())
220 ->Init();
221 }
222
223 DistanceMeasurementInterface*
get_distance_measurement_instance()224 bluetooth::shim::get_distance_measurement_instance() {
225 if (distance_measurement_instance == nullptr) {
226 distance_measurement_instance = new DistanceMeasurementInterfaceImpl();
227 }
228 return distance_measurement_instance;
229 };
230