1 // Copyright 2023 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include "pw_bluetooth_sapphire/internal/host/hci/util.h"
16
17 #include <endian.h>
18
19 #include "pw_bluetooth_sapphire/internal/host/common/assert.h"
20 #include "pw_bluetooth_sapphire/internal/host/common/log.h"
21
22 #pragma clang diagnostic ignored "-Wswitch-enum"
23
24 namespace bt::hci {
25
DeviceAddressFromAdvReport(const hci_spec::LEAdvertisingReportData & report,DeviceAddress * out_address,bool * out_resolved)26 bool DeviceAddressFromAdvReport(const hci_spec::LEAdvertisingReportData& report,
27 DeviceAddress* out_address,
28 bool* out_resolved) {
29 BT_DEBUG_ASSERT(out_address);
30 BT_DEBUG_ASSERT(out_resolved);
31
32 DeviceAddress::Type type;
33 switch (report.address_type) {
34 case hci_spec::LEAddressType::kPublicIdentity:
35 type = DeviceAddress::Type::kLEPublic;
36 *out_resolved = true;
37 break;
38 case hci_spec::LEAddressType::kPublic:
39 type = DeviceAddress::Type::kLEPublic;
40 *out_resolved = false;
41 break;
42 case hci_spec::LEAddressType::kRandomIdentity:
43 type = DeviceAddress::Type::kLERandom;
44 *out_resolved = true;
45 break;
46 case hci_spec::LEAddressType::kRandom:
47 type = DeviceAddress::Type::kLERandom;
48 *out_resolved = false;
49 break;
50 default:
51 bt_log(WARN,
52 "hci",
53 "invalid address type in advertising report: %#.2x",
54 static_cast<uint8_t>(report.address_type));
55 return false;
56 }
57
58 *out_address = DeviceAddress(type, report.address);
59 return true;
60 }
61
62 } // namespace bt::hci
63