1 /*
2  * Copyright 2024 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 #pragma once
18 
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 #include "hardware/bluetooth.h"
24 #include "include/hardware/bluetooth.h"
25 #include "stack/include/bt_name.h"
26 
27 std::string bt_property_text(const bt_property_t& property);
28 std::string bt_property_type_text(const bt_property_type_t& type);
29 
30 namespace bluetooth {
31 namespace property {
32 
33 class BtProperty {
34  public:
35   // Return size in bytes of property data value
36   virtual size_t Size() const = 0;
37   // Returns raw pointer to the data value
38   virtual const void* Val() const = 0;
39 
Type()40   bt_property_type_t Type() const {
41     return type_;
42   }
43 
ToString()44   std::string ToString() const {
45     return bt_property_type_text(type_);
46   }
47 
48  protected:
BtProperty(bt_property_type_t type)49   BtProperty(bt_property_type_t type) : type_(type) {}
50   virtual ~BtProperty() = default;
51 
52  private:
53   const bt_property_type_t type_;
54 };
55 
56 // Provide pointer/size access to properties for legacy jni API
57 class BtPropertyLegacy {
58  public:
59   BtPropertyLegacy(const std::vector<std::shared_ptr<BtProperty>>& bt_properties);
60 
61   void Export(bt_property_t* bt_properties, size_t size);
62 
63   size_t NumProperties() const;
64 
65   const std::vector<bt_property_t>& Properties() const;
66 
Ptr()67   bt_property_t* Ptr() const {
68     return const_cast<bt_property_t*>(&properties_[0]);
69   }
Len()70   int Len() const {
71     return static_cast<int>(properties_.size());
72   }
73 
74  private:
75   const std::vector<std::shared_ptr<BtProperty>> bt_properties_;
76   std::vector<bt_property_t> properties_;
77 };
78 
79 template <typename T>
80 class BtPropertySimple : public BtProperty {
81  public:
Size()82   virtual size_t Size() const override {
83     return sizeof(T);
84   }
85 
Val()86   const void* Val() const override {
87     return (const void*)val_.get();
88   }
89 
90  protected:
91   BtPropertySimple<T>(bt_property_type_t type, T val)
BtProperty(type)92       : BtProperty(type), val_(std::make_shared<T>(val)) {}
93 
94  private:
95   std::shared_ptr<T> val_;
96 };
97 
98 template <typename T>
99 class BtPropertyVector : public BtProperty {
100  public:
Size()101   virtual size_t Size() const override {
102     return sizeof(T) * val_->size();
103   }
Val()104   const void* Val() const override {
105     return (const void*)&(*val_)[0];
106   }
107 
108  protected:
109   // Create a vector property from another vector
110   BtPropertyVector<T>(bt_property_type_t type, const std::vector<T>& val)
BtProperty(type)111       : BtProperty(type), val_(std::make_shared<std::vector<T>>(val)) {}
112 
113   // Create a vector property from a raw pointer and size
114   BtPropertyVector<T>(bt_property_type_t type, const T* val, size_t size)
BtProperty(type)115       : BtProperty(type), val_(std::make_shared<std::vector<T>>(val, val + size)) {}
116 
117  protected:
118   std::shared_ptr<std::vector<T>> val_;
119 };
120 
121 template <typename T>
122 class BtPropertyVectorWithPad : public BtPropertyVector<T> {
123  protected:
124   // Create a vector property from a raw pointer and size with pad element
125   BtPropertyVectorWithPad<T>(bt_property_type_t type, const T* val, size_t size, T pad)
126       : BtPropertyVector<T>(type, val, size) {
127     BtPropertyVector<T>::val_->push_back(pad);
128   }
129 };
130 
131 class BdName : public BtPropertyVectorWithPad<uint8_t> {
132  public:
BdName(const BD_NAME bd_name)133   BdName(const BD_NAME bd_name)
134       : BtPropertyVectorWithPad<uint8_t>(BT_PROPERTY_BDNAME, bd_name, kBdNameLength, kBdNameDelim) {
135   }
136 
137   static std::shared_ptr<BdName> Create(const BD_NAME bd_name);
138 };
139 
140 class BdAddr : public BtPropertySimple<RawAddress> {
141  public:
BdAddr(const RawAddress & bd_addr)142   BdAddr(const RawAddress& bd_addr) : BtPropertySimple<RawAddress>(BT_PROPERTY_BDADDR, bd_addr) {}
143 
144   static std::shared_ptr<BdAddr> Create(const RawAddress& bd_addr);
145 };
146 
147 class Uuids : public BtPropertyVector<bluetooth::Uuid> {
148  public:
Uuids(const std::vector<bluetooth::Uuid> & uuids)149   Uuids(const std::vector<bluetooth::Uuid>& uuids)
150       : BtPropertyVector<bluetooth::Uuid>(BT_PROPERTY_UUIDS, uuids) {}
151 
152   static std::shared_ptr<Uuids> Create(const std::vector<bluetooth::Uuid>& uuids);
153 };
154 
155 class ClassOfDevice : public BtPropertySimple<uint32_t> {
156  public:
ClassOfDevice(const uint32_t & cod)157   ClassOfDevice(const uint32_t& cod)
158       : BtPropertySimple<uint32_t>(BT_PROPERTY_CLASS_OF_DEVICE, cod) {}
159   static std::shared_ptr<ClassOfDevice> Create(const uint32_t& bd_addr);
160 };
161 
162 class TypeOfDevice : public BtPropertySimple<bt_device_type_t> {
163  public:
TypeOfDevice(const bt_device_type_t & device_type)164   TypeOfDevice(const bt_device_type_t& device_type)
165       : BtPropertySimple<bt_device_type_t>(BT_PROPERTY_TYPE_OF_DEVICE, device_type) {}
166   static std::shared_ptr<TypeOfDevice> Create(const bt_device_type_t& device_type);
167 };
168 
169 class ServiceRecord : public BtPropertySimple<bt_service_record_t> {
170  public:
ServiceRecord(const bt_service_record_t & record)171   ServiceRecord(const bt_service_record_t& record)
172       : BtPropertySimple<bt_service_record_t>(BT_PROPERTY_SERVICE_RECORD, record) {}
173   static std::shared_ptr<ServiceRecord> Create(const bt_service_record_t& record);
174 };
175 
176 class AdapterScanMode : public BtPropertySimple<bt_scan_mode_t> {
177  public:
AdapterScanMode(const bt_scan_mode_t & mode)178   AdapterScanMode(const bt_scan_mode_t& mode)
179       : BtPropertySimple<bt_scan_mode_t>(BT_PROPERTY_ADAPTER_SCAN_MODE, mode) {}
180   static std::shared_ptr<AdapterScanMode> Create(const bt_scan_mode_t& mode);
181 };
182 
183 class AdapterBondedDevices : public BtPropertyVector<RawAddress> {
184  public:
AdapterBondedDevices(const RawAddress * bd_addr,size_t len)185   AdapterBondedDevices(const RawAddress* bd_addr, size_t len)
186       : BtPropertyVector<RawAddress>(BT_PROPERTY_ADAPTER_BONDED_DEVICES, bd_addr, len) {}
187 
188   static std::shared_ptr<AdapterBondedDevices> Create(const RawAddress* bd_addr, size_t len);
189 };
190 
191 class AdapterDiscoverableTimeout : public BtPropertySimple<uint32_t> {
192  public:
AdapterDiscoverableTimeout(const uint32_t & timeout)193   AdapterDiscoverableTimeout(const uint32_t& timeout)
194       : BtPropertySimple<uint32_t>(BT_PROPERTY_ADAPTER_DISCOVERABLE_TIMEOUT, timeout) {}
195 
196   static std::shared_ptr<AdapterDiscoverableTimeout> Create(const uint32_t& timeout);
197 };
198 
199 class RemoteFriendlyName : public BtPropertyVectorWithPad<uint8_t> {
200  public:
RemoteFriendlyName(const uint8_t bd_name[],size_t len)201   RemoteFriendlyName(const uint8_t bd_name[], size_t len)
202       : BtPropertyVectorWithPad<uint8_t>(
203             BT_PROPERTY_REMOTE_FRIENDLY_NAME, bd_name, len, kBdNameDelim) {}
204 
205   static std::shared_ptr<RemoteFriendlyName> Create(const uint8_t bd_name[], size_t len);
206 };
207 
208 class RemoteRSSI : public BtPropertySimple<int8_t> {
209  public:
RemoteRSSI(const int8_t & rssi)210   RemoteRSSI(const int8_t& rssi) : BtPropertySimple<int8_t>(BT_PROPERTY_REMOTE_RSSI, rssi) {}
211 
212   static std::shared_ptr<RemoteRSSI> Create(const int8_t& rssi);
213 };
214 
215 class RemoteVersionInfo : public BtPropertySimple<bt_remote_version_t> {
216  public:
RemoteVersionInfo(const bt_remote_version_t & info)217   RemoteVersionInfo(const bt_remote_version_t& info)
218       : BtPropertySimple<bt_remote_version_t>(BT_PROPERTY_REMOTE_VERSION_INFO, info) {}
219 
220   static std::shared_ptr<RemoteVersionInfo> Create(const bt_remote_version_t& info);
221 };
222 
223 class LocalLeFeatures : public BtPropertySimple<bt_local_le_features_t> {
224  public:
LocalLeFeatures(const bt_local_le_features_t & features)225   LocalLeFeatures(const bt_local_le_features_t& features)
226       : BtPropertySimple<bt_local_le_features_t>(BT_PROPERTY_LOCAL_LE_FEATURES, features) {}
227 
228   static std::shared_ptr<LocalLeFeatures> Create(const bt_local_le_features_t& features);
229 };
230 
231 class RemoteIsCoordinatedSetMember : public BtPropertySimple<bool> {
232  public:
RemoteIsCoordinatedSetMember(const bool & is_set_member)233   RemoteIsCoordinatedSetMember(const bool& is_set_member)
234       : BtPropertySimple<bool>(BT_PROPERTY_REMOTE_IS_COORDINATED_SET_MEMBER, is_set_member) {}
235 
236   static std::shared_ptr<RemoteIsCoordinatedSetMember> Create(const bool& is_set_member);
237 };
238 
239 class Appearance : public BtPropertySimple<uint16_t> {
240  public:
Appearance(const uint16_t & appearance)241   Appearance(const uint16_t& appearance)
242       : BtPropertySimple<uint16_t>(BT_PROPERTY_APPEARANCE, appearance) {}
243 
244   static std::shared_ptr<Appearance> Create(const uint16_t& appearance);
245 };
246 
247 class VendorProductInfo : public BtPropertySimple<bt_vendor_product_info_t> {
248  public:
VendorProductInfo(const bt_vendor_product_info_t & info)249   VendorProductInfo(const bt_vendor_product_info_t& info)
250       : BtPropertySimple<bt_vendor_product_info_t>(BT_PROPERTY_VENDOR_PRODUCT_INFO, info) {}
251 
252   static std::shared_ptr<VendorProductInfo> Create(const bt_vendor_product_info_t& info);
253 };
254 
255 class RemoteASHACapability : public BtPropertySimple<int16_t> {
256  public:
RemoteASHACapability(const int16_t capability)257   RemoteASHACapability(const int16_t capability)
258       : BtPropertySimple<int16_t>(BT_PROPERTY_REMOTE_ASHA_CAPABILITY, capability) {}
259 
260   static std::shared_ptr<RemoteASHACapability> Create(const int16_t& capability);
261 };
262 
263 class RemoteASHATruncatedHiSyncId : public BtPropertySimple<uint32_t> {
264  public:
RemoteASHATruncatedHiSyncId(const uint32_t id)265   RemoteASHATruncatedHiSyncId(const uint32_t id)
266       : BtPropertySimple<uint32_t>(BT_PROPERTY_REMOTE_ASHA_TRUNCATED_HISYNCID, id) {}
267 
268   static std::shared_ptr<RemoteASHATruncatedHiSyncId> Create(const uint32_t& id);
269 };
270 
271 class RemoteModelNum : public BtPropertyVectorWithPad<uint8_t> {
272  public:
RemoteModelNum(const bt_bdname_t & name)273   RemoteModelNum(const bt_bdname_t& name)
274       : BtPropertyVectorWithPad<uint8_t>(
275             BT_PROPERTY_REMOTE_MODEL_NUM,
276             name.name,
277             sizeof(bt_bdname_t) - sizeof(kBdNameDelim),
278             kBdNameDelim) {}
279 
280   static std::shared_ptr<RemoteModelNum> Create(const bt_bdname_t& name);
281 };
282 
283 class RemoteAddrType : public BtPropertySimple<uint8_t> {
284  public:
RemoteAddrType(const uint8_t & type)285   RemoteAddrType(const uint8_t& type)
286       : BtPropertySimple<uint8_t>(BT_PROPERTY_REMOTE_ADDR_TYPE, type) {}
287 
288   static std::shared_ptr<RemoteAddrType> Create(const uint8_t& type);
289 };
290 
291 class RemoteDeviceTimestamp : public BtPropertySimple<int> {
292  public:
RemoteDeviceTimestamp(const int & timestamp)293   RemoteDeviceTimestamp(const int& timestamp)
294       : BtPropertySimple<int>(BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP, timestamp) {}
295 
296   static std::shared_ptr<RemoteDeviceTimestamp> Create(const int& timestamp);
297 };
298 
299 }  // namespace property
300 }  // namespace bluetooth
301