1 /*
2 * Copyright 2020 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 <cstdint>
20 #include <string>
21 #include "types/raw_address.h"
22
23 #define BLE_ADDR_PUBLIC 0x00
24 #define BLE_ADDR_RANDOM 0x01
25 #define BLE_ADDR_PUBLIC_ID 0x02
26 #define BLE_ADDR_RANDOM_ID 0x03
27 #define BLE_ADDR_ANONYMOUS 0xFF
28 typedef uint8_t tBLE_ADDR_TYPE;
29 #ifdef __cplusplus
AddressTypeText(tBLE_ADDR_TYPE type)30 inline std::string AddressTypeText(tBLE_ADDR_TYPE type) {
31 switch (type) {
32 case BLE_ADDR_PUBLIC:
33 return std::string("public");
34 case BLE_ADDR_RANDOM:
35 return std::string("random");
36 case BLE_ADDR_PUBLIC_ID:
37 return std::string("public identity");
38 case BLE_ADDR_RANDOM_ID:
39 return std::string("random identity");
40 case BLE_ADDR_ANONYMOUS:
41 return std::string("anonymous");
42 default:
43 return std::string("unknown");
44 }
45 }
46 #endif // __cplusplus
47
is_ble_addr_type_valid(uint8_t raw_type)48 inline bool is_ble_addr_type_valid(uint8_t raw_type) { return raw_type < 4; }
49
is_ble_addr_type_known(tBLE_ADDR_TYPE type)50 inline bool is_ble_addr_type_known(tBLE_ADDR_TYPE type) {
51 switch (type) {
52 case BLE_ADDR_PUBLIC:
53 case BLE_ADDR_PUBLIC_ID:
54 case BLE_ADDR_RANDOM:
55 case BLE_ADDR_RANDOM_ID:
56 return true;
57 default:
58 return false;
59 }
60 }
61
to_ble_addr_type(uint8_t raw_type)62 inline tBLE_ADDR_TYPE to_ble_addr_type(uint8_t raw_type) {
63 return (tBLE_ADDR_TYPE)raw_type;
64 }
from_ble_addr_type(tBLE_ADDR_TYPE type)65 inline uint8_t from_ble_addr_type(tBLE_ADDR_TYPE type) { return (uint8_t)type; }
66
67 /* BLE ADDR type ID bit */
68 #define BLE_ADDR_TYPE_ID_BIT 0x02
is_identity_type(const tBLE_ADDR_TYPE & type)69 inline bool is_identity_type(const tBLE_ADDR_TYPE& type) {
70 return type & BLE_ADDR_TYPE_ID_BIT;
71 }
72
73 #define STREAM_TO_BLE_ADDR_TYPE(type, p) \
74 { \
75 (type) = (tBLE_ADDR_TYPE)(*(p)); \
76 (p) += sizeof(tBLE_ADDR_TYPE); \
77 }
78 #define BLE_ADDR_TYPE_TO_STREAM(p, type) \
79 { *(p)++ = (tBLE_ADDR_TYPE)(type); }
80
81 #ifdef __cplusplus
82 constexpr uint8_t kBleAddressPublicDevice = BLE_ADDR_PUBLIC;
83 constexpr uint8_t kBleAddressRandomDevice = BLE_ADDR_RANDOM;
84 constexpr uint8_t kBleAddressIdentityBit = BLE_ADDR_TYPE_ID_BIT;
85 constexpr uint8_t kBleAddressPublicIdentity =
86 kBleAddressIdentityBit | kBleAddressPublicDevice;
87 constexpr uint8_t kBleAddressRandomIdentity =
88 kBleAddressIdentityBit | kBleAddressRandomDevice;
89
90 constexpr uint8_t kResolvableAddressMask = 0xc0;
91 constexpr uint8_t kResolvableAddressMsb = 0x40;
92
93 struct tBLE_BD_ADDR {
94 tBLE_ADDR_TYPE type;
95 RawAddress bda;
AddressEqualstBLE_BD_ADDR96 bool AddressEquals(const RawAddress& other) const { return other == bda; }
IsPublicDeviceTypetBLE_BD_ADDR97 bool IsPublicDeviceType() const { return type == kBleAddressPublicDevice; }
IsRandomDeviceTypetBLE_BD_ADDR98 bool IsRandomDeviceType() const { return type == kBleAddressRandomDevice; }
IsPublicIdentityTypetBLE_BD_ADDR99 bool IsPublicIdentityType() const {
100 return type == kBleAddressPublicIdentity;
101 }
lsRandomIdentityTypetBLE_BD_ADDR102 bool lsRandomIdentityType() const {
103 return type == kBleAddressRandomIdentity;
104 }
IsAddressResolvabletBLE_BD_ADDR105 bool IsAddressResolvable() const {
106 return ((bda.address)[0] & kResolvableAddressMask) == kResolvableAddressMsb;
107 }
IsPublictBLE_BD_ADDR108 bool IsPublic() const { return type & 0x01; }
IsResolvablePrivateAddresstBLE_BD_ADDR109 bool IsResolvablePrivateAddress() const {
110 return IsAddressResolvable() && IsRandomDeviceType();
111 }
IsIdentityTypetBLE_BD_ADDR112 bool IsIdentityType() const {
113 return IsPublicIdentityType() || lsRandomIdentityType();
114 }
TypeWithoutIdentityEqualstBLE_BD_ADDR115 bool TypeWithoutIdentityEquals(const tBLE_ADDR_TYPE other) const {
116 return (other & ~kBleAddressIdentityBit) ==
117 (type & ~kBleAddressIdentityBit);
118 }
119
ToStringtBLE_BD_ADDR120 std::string ToString() const {
121 return std::string(bda.ToString() + "[" + AddressTypeText(type) + "]");
122 }
123
ToStringForLoggingtBLE_BD_ADDR124 std::string ToStringForLogging() const {
125 return bda.ToStringForLogging() + "[" + AddressTypeText(type) + "]";
126 }
127
ToRedactedStringForLoggingtBLE_BD_ADDR128 std::string ToRedactedStringForLogging() const {
129 return bda.ToRedactedStringForLogging() + "[" + AddressTypeText(type) + "]";
130 }
131
132 bool operator==(const tBLE_BD_ADDR rhs) const {
133 return rhs.type == type && rhs.bda == bda;
134 }
135 bool operator!=(const tBLE_BD_ADDR rhs) const { return !(*this == rhs); }
136 };
137
138 template <>
139 struct std::hash<tBLE_BD_ADDR> {
140 std::size_t operator()(const tBLE_BD_ADDR& val) const {
141 static_assert(sizeof(uint64_t) >=
142 (RawAddress::kLength + sizeof(tBLE_ADDR_TYPE)));
143 uint64_t int_addr = 0;
144 memcpy(reinterpret_cast<uint8_t*>(&int_addr), val.bda.address,
145 RawAddress::kLength);
146 memcpy(reinterpret_cast<uint8_t*>(&int_addr) + RawAddress::kLength,
147 (const void*)&val.type, sizeof(tBLE_ADDR_TYPE));
148 return std::hash<uint64_t>{}(int_addr);
149 }
150 };
151
152 #endif
153