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 #pragma once
17 
18 #include <bluetooth/log.h>
19 
20 #include "address.h"
21 #include "hal/ranging_hal.h"
22 #include "module.h"
23 
24 namespace bluetooth {
25 namespace hci {
26 
27 enum DistanceMeasurementMethod {
28   METHOD_AUTO,
29   METHOD_RSSI,
30   METHOD_CS,
31 };
32 
33 enum DistanceMeasurementErrorCode {
34   REASON_FEATURE_NOT_SUPPORTED_LOCAL,
35   REASON_FEATURE_NOT_SUPPORTED_REMOTE,
36   REASON_LOCAL_REQUEST,
37   REASON_REMOTE_REQUEST,
38   REASON_DURATION_TIMEOUT,
39   REASON_NO_LE_CONNECTION,
40   REASON_INVALID_PARAMETERS,
41   REASON_INTERNAL_ERROR,
42 };
43 
44 struct DistanceMeasurementResult {
45   Address address;
46   uint32_t centimeter;
47   uint32_t error_centimeter;
48   DistanceMeasurementMethod method;
49 };
50 
51 class DistanceMeasurementCallbacks {
52  public:
53   virtual ~DistanceMeasurementCallbacks() = default;
54   virtual void OnDistanceMeasurementStarted(Address address, DistanceMeasurementMethod method) = 0;
55   virtual void OnDistanceMeasurementStartFail(
56       Address address, DistanceMeasurementErrorCode reason, DistanceMeasurementMethod method) = 0;
57   virtual void OnDistanceMeasurementStopped(
58       Address address, DistanceMeasurementErrorCode reason, DistanceMeasurementMethod method) = 0;
59   virtual void OnDistanceMeasurementResult(
60       Address address,
61       uint32_t centimeter,
62       uint32_t error_centimeter,
63       int azimuth_angle,
64       int error_azimuth_angle,
65       int altitude_angle,
66       int error_altitude_angle,
67       DistanceMeasurementMethod method) = 0;
68   virtual void OnRasFragmentReady(
69       Address address, uint16_t procedure_counter, bool is_last, std::vector<uint8_t> raw_data) = 0;
70   virtual void OnVendorSpecificCharacteristics(
71       std::vector<hal::VendorSpecificCharacteristic> vendor_specific_characteristics) = 0;
72   virtual void OnVendorSpecificReply(
73       Address address,
74       std::vector<bluetooth::hal::VendorSpecificCharacteristic>
75           vendor_specific_characteristics) = 0;
76   virtual void OnHandleVendorSpecificReplyComplete(Address address, bool success) = 0;
77 };
78 
79 class DistanceMeasurementManager : public bluetooth::Module {
80  public:
81   DistanceMeasurementManager();
82   ~DistanceMeasurementManager();
83   DistanceMeasurementManager(const DistanceMeasurementManager&) = delete;
84   DistanceMeasurementManager& operator=(const DistanceMeasurementManager&) = delete;
85 
86   void RegisterDistanceMeasurementCallbacks(DistanceMeasurementCallbacks* callbacks);
87   void StartDistanceMeasurement(
88       const Address&, uint16_t interval, DistanceMeasurementMethod method);
89   void StopDistanceMeasurement(const Address& address, DistanceMeasurementMethod method);
90   void HandleRasConnectedEvent(
91       const Address& address,
92       uint16_t att_handle,
93       const std::vector<hal::VendorSpecificCharacteristic>& vendor_specific_data);
94   void HandleVendorSpecificReply(
95       const Address& address,
96       const std::vector<hal::VendorSpecificCharacteristic>& vendor_specific_reply);
97   void HandleVendorSpecificReplyComplete(const Address& address, bool success);
98   void HandleRemoteData(const Address& address, const std::vector<uint8_t>& raw_data);
99 
100   static const ModuleFactory Factory;
101 
102  protected:
103   void ListDependencies(ModuleList* list) const override;
104 
105   void Start() override;
106 
107   void Stop() override;
108 
109   std::string ToString() const override;
110 
111  private:
112   struct impl;
113   std::unique_ptr<impl> pimpl_;
114 };
115 
116 }  // namespace hci
117 }  // namespace bluetooth
118 
119 namespace fmt {
120 template <>
121 struct formatter<bluetooth::hci::DistanceMeasurementMethod>
122     : enum_formatter<bluetooth::hci::DistanceMeasurementMethod> {};
123 }  // namespace fmt
124