• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
48 /* BLE ADDR type ID bit */
49 #define BLE_ADDR_TYPE_ID_BIT 0x02
50 
51 #ifdef __cplusplus
52 constexpr uint8_t kBleAddressPublicDevice = BLE_ADDR_PUBLIC;
53 constexpr uint8_t kBleAddressRandomDevice = BLE_ADDR_RANDOM;
54 constexpr uint8_t kBleAddressIdentityBit = BLE_ADDR_TYPE_ID_BIT;
55 constexpr uint8_t kBleAddressPublicIdentity =
56     kBleAddressIdentityBit | kBleAddressPublicDevice;
57 constexpr uint8_t kBleAddressRandomIdentity =
58     kBleAddressIdentityBit | kBleAddressRandomDevice;
59 
60 constexpr uint8_t kResolvableAddressMask = 0xc0;
61 constexpr uint8_t kResolvableAddressMsb = 0x40;
62 
63 struct tBLE_BD_ADDR {
64   tBLE_ADDR_TYPE type;
65   RawAddress bda;
AddressEqualstBLE_BD_ADDR66   bool AddressEquals(const RawAddress& other) const { return other == bda; }
IsPublicDeviceTypetBLE_BD_ADDR67   bool IsPublicDeviceType() const { return type == kBleAddressPublicDevice; }
IsRandomDeviceTypetBLE_BD_ADDR68   bool IsRandomDeviceType() const { return type == kBleAddressRandomDevice; }
IsPublicIdentityTypetBLE_BD_ADDR69   bool IsPublicIdentityType() const {
70     return type == kBleAddressPublicIdentity;
71   }
lsRandomIdentityTypetBLE_BD_ADDR72   bool lsRandomIdentityType() const {
73     return type == kBleAddressRandomIdentity;
74   }
IsAddressResolvabletBLE_BD_ADDR75   bool IsAddressResolvable() const {
76     return ((bda.address)[0] & kResolvableAddressMask) == kResolvableAddressMsb;
77   }
IsPublictBLE_BD_ADDR78   bool IsPublic() const { return type & 0x01; }
IsResolvablePrivateAddresstBLE_BD_ADDR79   bool IsResolvablePrivateAddress() const {
80     return IsAddressResolvable() && IsRandomDeviceType();
81   }
IsIdentityTypetBLE_BD_ADDR82   bool IsIdentityType() const {
83     return IsPublicIdentityType() || lsRandomIdentityType();
84   }
TypeWithoutIdentityEqualstBLE_BD_ADDR85   bool TypeWithoutIdentityEquals(const tBLE_ADDR_TYPE other) const {
86     return (other & ~kBleAddressIdentityBit) ==
87            (type & ~kBleAddressIdentityBit);
88   }
ToStringtBLE_BD_ADDR89   std::string ToString() const {
90     return std::string(bda.ToString() + "[" + AddressTypeText(type) + "]");
91   }
92 };
93 #endif
94