Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(158)

Side by Side Diff: components/arc/bluetooth/bluetooth_struct_traits.cc

Issue 2256003002: components/arc: implement multi advertising (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@plumb-incoming-connections
Patch Set: more testing Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/arc/bluetooth/bluetooth_struct_traits.h" 5 #include "components/arc/bluetooth/bluetooth_struct_traits.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/strings/string_number_conversions.h" 10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/stringprintf.h"
12 #include "device/bluetooth/bluetooth_advertisement.h"
11 #include "device/bluetooth/bluetooth_uuid.h" 13 #include "device/bluetooth/bluetooth_uuid.h"
12 14
13 namespace { 15 namespace {
14 16
17 // BluetoothUUID helpers.
15 constexpr size_t kUUIDSize = 16; 18 constexpr size_t kUUIDSize = 16;
16 19
17 bool IsNonHex(char c) { 20 bool IsNonHex(char c) {
18 return !isxdigit(c); 21 return !isxdigit(c);
19 } 22 }
20 23
21 std::string StripNonHex(const std::string& str) { 24 std::string StripNonHex(const std::string& str) {
22 std::string result = str; 25 std::string result = str;
23 result.erase(std::remove_if(result.begin(), result.end(), IsNonHex), 26 result.erase(std::remove_if(result.begin(), result.end(), IsNonHex),
24 result.end()); 27 result.end());
25 28
26 return result; 29 return result;
27 } 30 }
28 31
32 // BluetoothAdvertisement helpers.
33 struct AdvertisementEntry {
34 virtual void AddTo(device::BluetoothAdvertisement::Data* data) {}
35 };
36
37 struct ServiceUUIDEntry : public AdvertisementEntry {
38 std::vector<device::BluetoothUUID> service_uuids;
39
40 void AddTo(device::BluetoothAdvertisement::Data* data) override {
41 auto string_uuids = base::MakeUnique<std::vector<std::string>>();
42 for (const auto& uuid : service_uuids) {
43 string_uuids->emplace_back(uuid.value());
44 }
45 data->set_service_uuids(std::move(string_uuids));
46 }
47 };
48
49 struct ServiceDataEntry : public AdvertisementEntry {
50 uint16_t service_uuid;
51 std::vector<uint8_t> service_data;
52
53 void AddTo(device::BluetoothAdvertisement::Data* data) override {
54 std::string string_uuid = base::StringPrintf("%04x", service_uuid);
55 data->set_service_data(
56 base::WrapUnique(new std::map<std::string, std::vector<uint8_t>>{
57 {string_uuid, service_data}}));
58 }
59 };
60
61 struct ManufacturerDataEntry : public AdvertisementEntry {
62 uint16_t company_id_code;
63 std::vector<uint8_t> blob;
64
65 void AddTo(device::BluetoothAdvertisement::Data* data) override {
66 data->set_manufacturer_data(base::WrapUnique(
67 new std::map<uint16_t, std::vector<uint8_t>>{{company_id_code, blob}}));
68 }
69 };
70
71 uint16_t ExtractCompanyIdentifierCode(std::vector<uint8_t>* blob) {
72 // The company identifier code is in little-endian.
73 uint16_t company_id_code = (*blob)[1] << 8 | (*blob)[0];
74 blob->erase(blob->begin(), blob->begin() + sizeof(uint16_t));
75 return company_id_code;
76 }
77
29 } // namespace 78 } // namespace
30 79
31 namespace mojo { 80 namespace mojo {
32 81
33 // static 82 // static
34 std::vector<uint8_t> 83 std::vector<uint8_t>
35 StructTraits<arc::mojom::BluetoothUUIDDataView, device::BluetoothUUID>::uuid( 84 StructTraits<arc::mojom::BluetoothUUIDDataView, device::BluetoothUUID>::uuid(
36 const device::BluetoothUUID& input) { 85 const device::BluetoothUUID& input) {
37 std::string uuid_str = StripNonHex(input.canonical_value()); 86 std::string uuid_str = StripNonHex(input.canonical_value());
38 87
(...skipping 21 matching lines...) Expand all
60 for (auto pos : kUuidDashPos) 109 for (auto pos : kUuidDashPos)
61 uuid_str = uuid_str.insert(pos, "-"); 110 uuid_str = uuid_str.insert(pos, "-");
62 111
63 device::BluetoothUUID result(uuid_str); 112 device::BluetoothUUID result(uuid_str);
64 113
65 DCHECK(result.IsValid()); 114 DCHECK(result.IsValid());
66 *output = result; 115 *output = result;
67 return true; 116 return true;
68 } 117 }
69 118
119 template <>
120 struct EnumTraits<arc::mojom::BluetoothAdvertisementType,
121 device::BluetoothAdvertisement::AdvertisementType> {
122 static bool FromMojom(
123 arc::mojom::BluetoothAdvertisementType mojom_type,
124 device::BluetoothAdvertisement::AdvertisementType* type) {
125 switch (mojom_type) {
126 case arc::mojom::BluetoothAdvertisementType::ADV_TYPE_CONNECTABLE:
127 case arc::mojom::BluetoothAdvertisementType::ADV_TYPE_SCANNABLE:
128 *type = device::BluetoothAdvertisement::ADVERTISEMENT_TYPE_PERIPHERAL;
129 return true;
130 case arc::mojom::BluetoothAdvertisementType::ADV_TYPE_NON_CONNECTABLE:
131 *type = device::BluetoothAdvertisement::ADVERTISEMENT_TYPE_BROADCAST;
132 return true;
133 }
134 NOTREACHED() << "Invalid type: " << static_cast<uint32_t>(mojom_type);
135 return false;
136 }
137 };
138
139 template <>
140 struct StructTraits<arc::mojom::BluetoothServiceDataDataView,
141 ServiceDataEntry> {
142 static bool Read(arc::mojom::BluetoothServiceDataDataView data,
143 ServiceDataEntry* output) {
144 output->service_uuid = data.uuid_16bit();
145 data.ReadData(&output->service_data);
146 return true;
147 }
148 };
149
150 template <>
151 struct UnionTraits<arc::mojom::BluetoothAdvertisingDataDataView,
152 std::unique_ptr<AdvertisementEntry>> {
153 static bool Read(arc::mojom::BluetoothAdvertisingDataDataView data,
154 std::unique_ptr<AdvertisementEntry>* output) {
155 switch (data.tag()) {
156 case arc::mojom::BluetoothAdvertisingDataDataView::Tag::SERVICE_UUIDS: {
157 std::unique_ptr<ServiceUUIDEntry> service_uuids =
158 base::MakeUnique<ServiceUUIDEntry>();
159 data.ReadServiceUuids(&service_uuids->service_uuids);
160 *output = std::move(service_uuids);
161 break;
162 }
163 case arc::mojom::BluetoothAdvertisingDataDataView::Tag::SERVICE_DATA: {
164 std::unique_ptr<ServiceDataEntry> service_data =
165 base::MakeUnique<ServiceDataEntry>();
166 data.ReadServiceData(service_data.get());
167 *output = std::move(service_data);
168 break;
169 }
170 case arc::mojom::BluetoothAdvertisingDataDataView::Tag::
171 MANUFACTURER_DATA: {
172 std::unique_ptr<ManufacturerDataEntry> manufacturer_data =
173 base::MakeUnique<ManufacturerDataEntry>();
174 // We get manufacturer data as a big blob. The first two bytes
175 // should be a company identifier code and the rest is manufacturer-
176 // specific.
177 std::vector<uint8_t> blob;
178 data.ReadManufacturerData(&blob);
179 if (blob.size() < sizeof(uint16_t)) {
180 LOG(WARNING) << "Advertisement had malformed manufacturer data";
181 return false;
182 }
183
184 manufacturer_data->company_id_code =
185 ExtractCompanyIdentifierCode(&blob);
186 manufacturer_data->blob = std::move(blob);
187 *output = std::move(manufacturer_data);
188 break;
189 }
190 default: {
191 LOG(WARNING) << "Bluetooth advertising data case not implemented";
192 // Default AdvertisementEntry does nothing when added to the
193 // device::BluetoothAdvertisement::AdvertisementData, so data we
194 // don't know how to handle yet will be dropped but won't cause a
195 // failure to deserialize.
196 *output = base::MakeUnique<AdvertisementEntry>();
197 break;
198 }
199 }
200 return true;
201 }
202 };
203
204 // static
205 bool StructTraits<arc::mojom::BluetoothAdvertisementDataView,
206 std::unique_ptr<device::BluetoothAdvertisement::Data>>::
207 Read(arc::mojom::BluetoothAdvertisementDataView advertisement,
208 std::unique_ptr<device::BluetoothAdvertisement::Data>* output) {
209 device::BluetoothAdvertisement::AdvertisementType adv_type;
210 if (!advertisement.ReadType(&adv_type))
211 return false;
212 auto data = base::MakeUnique<device::BluetoothAdvertisement::Data>(adv_type);
213
214 std::vector<std::unique_ptr<AdvertisementEntry>> adv_entries;
215 if (!advertisement.ReadData(&adv_entries))
216 return false;
217 for (const auto& adv_entry : adv_entries)
218 adv_entry->AddTo(data.get());
219
220 data->set_include_tx_power(advertisement.include_tx_power());
221
222 *output = std::move(data);
223 return true;
224 }
225
70 } // namespace mojo 226 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698