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 #include "discovery/device/eir_data.h"
18 
19 #include <algorithm>
20 #include <array>
21 #include <iterator>
22 #include <vector>
23 
24 #include "hci/hci_packets.h"
25 #include "hci/uuid.h"
26 
27 using namespace bluetooth;
28 
29 using namespace bluetooth::hci;
30 using namespace bluetooth::packet;
31 
32 namespace bluetooth::discovery::device {
33 
EirData(const std::vector<uint8_t> & data)34 EirData::EirData(const std::vector<uint8_t>& data) : DataParser(data) {}
35 
GetCompleteNames(std::vector<std::array<uint8_t,240>> & names) const36 bool EirData::GetCompleteNames(std::vector<std::array<uint8_t, 240>>& names) const {
37   for (const auto& gap_data : gap_data_) {
38     if (gap_data.data_type_ == hci::GapDataType::COMPLETE_LOCAL_NAME) {
39       std::array<uint8_t, 240> array;
40       std::copy(gap_data.data_.begin(), gap_data.data_.end(), array.begin());
41       names.push_back(array);
42     }
43   }
44   return !names.empty();
45 }
46 
GetShortenedNames(std::vector<std::array<uint8_t,240>> & names) const47 bool EirData::GetShortenedNames(std::vector<std::array<uint8_t, 240>>& names) const {
48   for (const auto& gap_data : gap_data_) {
49     if (gap_data.data_type_ == hci::GapDataType::SHORTENED_LOCAL_NAME) {
50       std::array<uint8_t, 240> array;
51       std::copy(gap_data.data_.begin(), gap_data.data_.end(), array.begin());
52       names.push_back(array);
53     }
54   }
55   return !names.empty();
56 }
57 
GetUuids16(std::vector<uint16_t> & uuids) const58 bool EirData::GetUuids16(std::vector<uint16_t>& uuids) const {
59   for (const auto& gap_data : gap_data_) {
60     if (gap_data.data_type_ == hci::GapDataType::COMPLETE_LIST_16_BIT_UUIDS) {
61       auto it = gap_data.data_.begin();
62       while (std::distance(it, gap_data.data_.end()) >= (signed)Uuid::kNumBytes16) {
63         uuids.push_back(*it | *(it + 1) << 8);
64         it += Uuid::kNumBytes16;
65       }
66     }
67   }
68   return !uuids.empty();
69 }
70 
GetUuidsIncomplete16(std::vector<uint16_t> & uuids) const71 bool EirData::GetUuidsIncomplete16(std::vector<uint16_t>& uuids) const {
72   for (const auto& gap_data : gap_data_) {
73     if (gap_data.data_type_ == hci::GapDataType::INCOMPLETE_LIST_16_BIT_UUIDS) {
74       auto it = gap_data.data_.begin();
75       while (std::distance(it, gap_data.data_.end()) >= (signed)Uuid::kNumBytes16) {
76         uuids.push_back(*it | *(it + 1) << 8);
77         it += Uuid::kNumBytes16;
78       }
79     }
80   }
81   return !uuids.empty();
82 }
83 
GetUuids32(std::vector<uint32_t> & uuids) const84 bool EirData::GetUuids32(std::vector<uint32_t>& uuids) const {
85   for (const auto& gap_data : gap_data_) {
86     if (gap_data.data_type_ == hci::GapDataType::COMPLETE_LIST_32_BIT_UUIDS) {
87       auto it = gap_data.data_.begin();
88       while (std::distance(it, gap_data.data_.end()) >= (signed)Uuid::kNumBytes32) {
89         uuids.push_back(*it | *(it + 1) << 8 | *(it + 2) << 16 | *(it + 3) << 24);
90         it += Uuid::kNumBytes32;
91       }
92     }
93   }
94   return !uuids.empty();
95 }
96 
GetUuidsIncomplete32(std::vector<uint32_t> & uuids) const97 bool EirData::GetUuidsIncomplete32(std::vector<uint32_t>& uuids) const {
98   for (const auto& gap_data : gap_data_) {
99     if (gap_data.data_type_ == hci::GapDataType::INCOMPLETE_LIST_32_BIT_UUIDS) {
100       auto it = gap_data.data_.begin();
101       while (std::distance(it, gap_data.data_.end()) >= (signed)Uuid::kNumBytes32) {
102         uuids.push_back(*it | *(it + 1) << 8 | *(it + 2) << 16 | *(it + 3) << 24);
103         it += Uuid::kNumBytes32;
104       }
105     }
106   }
107   return !uuids.empty();
108 }
109 
GetUuids128(std::vector<hci::Uuid> & uuids) const110 bool EirData::GetUuids128(std::vector<hci::Uuid>& uuids) const {
111   for (const auto& gap_data : gap_data_) {
112     if (gap_data.data_type_ == hci::GapDataType::COMPLETE_LIST_128_BIT_UUIDS) {
113       auto it = gap_data.data_.begin();
114       while (std::distance(it, gap_data.data_.end()) >= (long)Uuid::kNumBytes128) {
115         auto uuid = bluetooth::hci::Uuid::From128BitLE(&it[0]);
116         uuids.push_back(uuid);
117         it += Uuid::kNumBytes128;
118       }
119     }
120   }
121   return !uuids.empty();
122 }
123 
GetUuidsIncomplete128(std::vector<hci::Uuid> & uuids) const124 bool EirData::GetUuidsIncomplete128(std::vector<hci::Uuid>& uuids) const {
125   for (const auto& gap_data : gap_data_) {
126     if (gap_data.data_type_ == hci::GapDataType::INCOMPLETE_LIST_128_BIT_UUIDS) {
127       auto it = gap_data.data_.begin();
128       while (std::distance(it, gap_data.data_.end()) >= (long)Uuid::kNumBytes128) {
129         auto uuid = bluetooth::hci::Uuid::From128BitLE(&it[0]);
130         uuids.push_back(uuid);
131         it += Uuid::kNumBytes128;
132       }
133     }
134   }
135   return !uuids.empty();
136 }
137 
GetDeviceId(std::vector<std::vector<uint8_t>> & device_ids) const138 bool EirData::GetDeviceId(std::vector<std::vector<uint8_t>>& device_ids) const {
139   for (const auto& gap_data : gap_data_) {
140     if (gap_data.data_type_ == hci::GapDataType::DEVICE_ID) {
141       device_ids.push_back(gap_data.data_);
142     }
143   }
144   return !device_ids.empty();
145 }
146 
GetManufacturerSpecificData(std::vector<std::vector<uint8_t>> & data) const147 bool EirData::GetManufacturerSpecificData(std::vector<std::vector<uint8_t>>& data) const {
148   for (const auto& gap_data : gap_data_) {
149     if (gap_data.data_type_ == hci::GapDataType::MANUFACTURER_SPECIFIC_DATA) {
150       data.push_back(gap_data.data_);
151     }
152   }
153   return !data.empty();
154 }
155 
GetSecurityManagerOobFlags(std::vector<std::vector<uint8_t>> & flags) const156 bool EirData::GetSecurityManagerOobFlags(std::vector<std::vector<uint8_t>>& flags) const {
157   for (const auto& gap_data : gap_data_) {
158     if (gap_data.data_type_ == hci::GapDataType::SECURITY_MANAGER_OOB_FLAGS) {
159       flags.push_back(gap_data.data_);
160     }
161   }
162   return !flags.empty();
163 }
164 
GetServiceUuuids16(std::vector<service_uuid16_t> & uuids) const165 bool EirData::GetServiceUuuids16(std::vector<service_uuid16_t>& uuids) const {
166   for (const auto& gap_data : gap_data_) {
167     if (gap_data.data_type_ == hci::GapDataType::SERVICE_DATA_16_BIT_UUIDS) {
168       if (gap_data.data_.size() < Uuid::kNumBytes16) continue;
169       auto it = gap_data.data_.begin();
170       uuids.push_back({
171           .uuid = (uint16_t)(*it | *(it + 1) << 8),
172           .data = std::vector<uint8_t>(it + Uuid::kNumBytes16, gap_data.data_.end()),
173       });
174     }
175   }
176   return !uuids.empty();
177 }
178 
GetServiceUuuids32(std::vector<service_uuid32_t> & uuids) const179 bool EirData::GetServiceUuuids32(std::vector<service_uuid32_t>& uuids) const {
180   for (const auto& gap_data : gap_data_) {
181     if (gap_data.data_type_ == hci::GapDataType::SERVICE_DATA_32_BIT_UUIDS) {
182       if (gap_data.data_.size() < Uuid::kNumBytes32) continue;
183       auto it = gap_data.data_.begin();
184       uuids.push_back({
185           .uuid = (uint32_t)(*it | *(it + 1) << 8 | *(it + 2) << 16 | *(it + 3) << 24),
186           .data = std::vector<uint8_t>(it + Uuid::kNumBytes32, gap_data.data_.end()),
187       });
188     }
189   }
190   return !uuids.empty();
191 }
192 
GetTxPowerLevel(std::vector<int8_t> & tx_power_level) const193 bool EirData::GetTxPowerLevel(std::vector<int8_t>& tx_power_level) const {
194   for (const auto& gap_data : gap_data_) {
195     if (gap_data.data_type_ == hci::GapDataType::TX_POWER_LEVEL) {
196       if (gap_data.data_.size() == 1U)
197         tx_power_level.push_back(static_cast<int8_t>(gap_data.data_[0]));
198     }
199   }
200   return !tx_power_level.empty();
201 }
202 
203 }  // namespace bluetooth::discovery::device
204