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 #pragma once
18 
19 #include <algorithm>
20 #include <cstdint>
21 #include <deque>
22 #include <memory>
23 #include <sstream>
24 #include <string>
25 
26 #include "include/hardware/bluetooth.h"
27 #include "macros.h"
28 #include "types/bluetooth/uuid.h"
29 
bt_property_type_text(const::bt_property_type_t type)30 inline std::string bt_property_type_text(const ::bt_property_type_t type) {
31   switch (type) {
32     CASE_RETURN_TEXT(BT_PROPERTY_BDNAME);
33     CASE_RETURN_TEXT(BT_PROPERTY_BDADDR);
34     CASE_RETURN_TEXT(BT_PROPERTY_UUIDS);
35     CASE_RETURN_TEXT(BT_PROPERTY_CLASS_OF_DEVICE);
36     CASE_RETURN_TEXT(BT_PROPERTY_TYPE_OF_DEVICE);
37     CASE_RETURN_TEXT(BT_PROPERTY_SERVICE_RECORD);
38     CASE_RETURN_TEXT(BT_PROPERTY_ADAPTER_SCAN_MODE);
39     CASE_RETURN_TEXT(BT_PROPERTY_ADAPTER_BONDED_DEVICES);
40     CASE_RETURN_TEXT(BT_PROPERTY_ADAPTER_DISCOVERABLE_TIMEOUT);
41     CASE_RETURN_TEXT(BT_PROPERTY_REMOTE_FRIENDLY_NAME);
42     CASE_RETURN_TEXT(BT_PROPERTY_REMOTE_RSSI);
43     CASE_RETURN_TEXT(BT_PROPERTY_REMOTE_VERSION_INFO);
44     CASE_RETURN_TEXT(BT_PROPERTY_LOCAL_LE_FEATURES);
45     CASE_RETURN_TEXT(BT_PROPERTY_RESERVED_0E);
46     CASE_RETURN_TEXT(BT_PROPERTY_RESERVED_0F);
47     CASE_RETURN_TEXT(BT_PROPERTY_DYNAMIC_AUDIO_BUFFER);
48     CASE_RETURN_TEXT(BT_PROPERTY_REMOTE_IS_COORDINATED_SET_MEMBER);
49     CASE_RETURN_TEXT(BT_PROPERTY_APPEARANCE);
50     CASE_RETURN_TEXT(BT_PROPERTY_VENDOR_PRODUCT_INFO);
51     CASE_RETURN_TEXT(BT_PROPERTY_REMOTE_ASHA_CAPABILITY);
52     CASE_RETURN_TEXT(BT_PROPERTY_REMOTE_ASHA_TRUNCATED_HISYNCID);
53     CASE_RETURN_TEXT(BT_PROPERTY_REMOTE_MODEL_NUM);
54     CASE_RETURN_TEXT(BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP);
55     CASE_RETURN_TEXT(BT_PROPERTY_REMOTE_ADDR_TYPE);
56     CASE_RETURN_TEXT(BT_PROPERTY_RESERVED_0x14);
57     default:
58       RETURN_UNKNOWN_TYPE_STRING(::bt_property_type_t, type);
59   }
60 }
61 
62 namespace bluetooth {
63 namespace test {
64 namespace headless {
65 
66 struct bt_property_t {
Typebt_property_t67   ::bt_property_type_t Type() const { return type; }
68 
69   virtual std::string ToString() const = 0;
70 
71   // TODO verify this prints as expected
ToRawbt_property_t72   std::string ToRaw() {
73     std::ostringstream oss;
74     const uint8_t* p = data.get();
75     for (size_t i = 0; i < sizeof(bt_property_t); i++, p++) {
76       oss << "0x" << std::hex << *p << " ";
77     }
78     return oss.str();
79   }
80 
81  protected:
bt_property_tbt_property_t82   bt_property_t(const uint8_t* data, const size_t len) {
83     this->len = len;
84     this->data = std::make_unique<uint8_t[]>(len);
85     std::copy(data, data + len, this->data.get());
86   }
87   virtual ~bt_property_t() = default;
88 
89   std::unique_ptr<uint8_t[]> data;
90   size_t len;
91   ::bt_property_type_t type;
92 };
93 
94 namespace property {
95 
96 struct void_t : public bt_property_t {
void_tvoid_t97   void_t(const uint8_t* data, const size_t len, int type)
98       : bt_property_t(data, len) {
99     this->type = (::bt_property_type_t)type;
100   }
101 
102  public:
ToStringvoid_t103   virtual std::string ToString() const override {
104     return fmt::format("Unimplemented property type:{} name:{}", type,
105                        bt_property_type_text(type));
106   }
107 };
108 
109 struct uuid_t : public bt_property_t {
110  public:
uuid_tuuid_t111   uuid_t(const uint8_t* data, const size_t len) : bt_property_t(data, len) {}
112 
get_uuidsuuid_t113   std::deque<bluetooth::Uuid> get_uuids() const {
114     std::deque<bluetooth::Uuid> uuids;
115     bluetooth::Uuid* p_uuid = reinterpret_cast<bluetooth::Uuid*>(data.get());
116     for (size_t i = 0; i < num_uuid(); i++, p_uuid++) {
117       bluetooth::Uuid uuid = bluetooth::Uuid::From128BitBE(
118           reinterpret_cast<const uint8_t*>(p_uuid));
119       uuids.push_back(uuid);
120     }
121     return uuids;
122   }
123 
ToStringuuid_t124   virtual std::string ToString() const override {
125     return fmt::format("Number of uuids:{}", get_uuids().size());
126   }
127 
128  private:
num_uuiduuid_t129   size_t num_uuid() const { return len / sizeof(bluetooth::Uuid); }
130 };
131 
132 struct name_t : public bt_property_t {
name_tname_t133   name_t(const uint8_t* data, const size_t len) : bt_property_t(data, len) {
134     type = BT_PROPERTY_BDNAME;
135   }
136 
get_namename_t137   std::string get_name() const {
138     char* s = reinterpret_cast<char*>(data.get());
139     return std::string(s);
140   }
141 
ToStringname_t142   virtual std::string ToString() const override {
143     return fmt::format("Name:{}", get_name());
144   }
145 };
146 
147 struct bdaddr_t : public bt_property_t {
bdaddr_tbdaddr_t148   bdaddr_t(const uint8_t* data, const size_t len) : bt_property_t(data, len) {
149     type = BT_PROPERTY_BDNAME;
150   }
151 
get_addrbdaddr_t152   RawAddress get_addr() const {
153     uint8_t* s = reinterpret_cast<uint8_t*>(data.get());
154     // TODO This may need to be reversed
155     RawAddress bd_addr;
156     log::assert_that(6U == bd_addr.FromOctets(s), "Mac address is not 6 bytes");
157     return bd_addr;
158   }
159 
ToStringbdaddr_t160   virtual std::string ToString() const override {
161     return fmt::format("bd_addr:{}", get_addr().ToString());
162   }
163 };
164 
165 struct class_of_device_t : public bt_property_t {
class_of_device_tclass_of_device_t166   class_of_device_t(const uint8_t* data, const size_t len)
167       : bt_property_t(data, len) {
168     type = BT_PROPERTY_CLASS_OF_DEVICE;
169   }
170 
get_class_of_deviceclass_of_device_t171   uint32_t get_class_of_device() const {
172     uint32_t* cod = reinterpret_cast<uint32_t*>(data.get());
173     return *cod;
174   }
175 
ToStringclass_of_device_t176   virtual std::string ToString() const override {
177     return fmt::format("cod:0x{:04x}", get_class_of_device());
178   }
179 };
180 
181 struct type_of_device_t : public bt_property_t {
type_of_device_ttype_of_device_t182   type_of_device_t(const uint8_t* data, const size_t len)
183       : bt_property_t(data, len) {
184     type = BT_PROPERTY_TYPE_OF_DEVICE;
185   }
186 
get_type_of_devicetype_of_device_t187   uint32_t get_type_of_device() const {
188     uint32_t* tod = reinterpret_cast<uint32_t*>(data.get());
189     return *tod;
190   }
191 
ToStringtype_of_device_t192   virtual std::string ToString() const override {
193     return fmt::format("tod:0x{:04x}", get_type_of_device());
194   }
195 };
196 
197 }  // namespace property
198 
199 bluetooth::test::headless::bt_property_t* property_factory(
200     const ::bt_property_t& bt_property);
201 
202 template <typename T>
get_property_type(bluetooth::test::headless::bt_property_t * bt_property)203 T* get_property_type(bluetooth::test::headless::bt_property_t* bt_property) {
204   return static_cast<T*>(bt_property);
205 }
206 
207 }  // namespace headless
208 }  // namespace test
209 }  // namespace bluetooth
210