1 /******************************************************************************
2  *
3  *  Copyright 2017 The Android Open Source Project
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #pragma once
20 
21 #include <array>
22 #include <cstdint>
23 #include <cstring>
24 #include <string>
25 
26 /** Bluetooth Address */
27 class RawAddress final {
28  public:
29   static constexpr unsigned int kLength = 6;
30 
31   uint8_t address[kLength];
32 
33   RawAddress() = default;
34   RawAddress(const uint8_t (&addr)[kLength]);
35   RawAddress(const std::array<uint8_t, kLength> array);
36 
37   bool operator<(const RawAddress& rhs) const {
38     return (std::memcmp(address, rhs.address, sizeof(address)) < 0);
39   }
40   bool operator==(const RawAddress& rhs) const {
41     return (std::memcmp(address, rhs.address, sizeof(address)) == 0);
42   }
43   bool operator>(const RawAddress& rhs) const { return (rhs < *this); }
44   bool operator<=(const RawAddress& rhs) const { return !(*this > rhs); }
45   bool operator>=(const RawAddress& rhs) const { return !(*this < rhs); }
46   bool operator!=(const RawAddress& rhs) const { return !(*this == rhs); }
47 
IsEmpty()48   bool IsEmpty() const { return *this == kEmpty; }
49 
50   // TODO (b/258090765): remove it and
51   // replace its usage with ToColonSepHexString
52   std::string ToString() const;
53 
54   // Return a string representation in the form of
55   // hexadecimal string separated by colon (:), e.g.,
56   // "12:34:56:ab:cd:ef"
57   std::string ToColonSepHexString() const;
58   // same as ToColonSepHexString
59   std::string ToStringForLogging() const;
60 
61   // Similar with ToColonHexString, ToRedactedStringForLogging returns a
62   // colon separated hexadecimal reprentation of the address but, with the
63   // leftmost 4 bytes masked with "xx", e.g., "xx:xx:xx:xx:ab:cd".
64   std::string ToRedactedStringForLogging() const;
65 
66   // Converts |string| to RawAddress and places it in |to|. If |from| does
67   // not represent a Bluetooth address, |to| is not modified and this function
68   // returns false. Otherwise, it returns true.
69   static bool FromString(const std::string& from, RawAddress& to);
70 
71   // Copies |from| raw Bluetooth address octets to the local object.
72   // Returns the number of copied octets - should be always RawAddress::kLength
73   size_t FromOctets(const uint8_t* from);
74 
75   std::array<uint8_t, kLength> ToArray() const;
76 
77   static bool IsValidAddress(const std::string& address);
78 
79   static const RawAddress kEmpty;  // 00:00:00:00:00:00
80   static const RawAddress kAny;    // FF:FF:FF:FF:FF:FF
81 };
82 
83 inline std::ostream& operator<<(std::ostream& os, const RawAddress& a) {
84   os << a.ToString();
85   return os;
86 }
87 
88 template <>
89 struct std::hash<RawAddress> {
90   std::size_t operator()(const RawAddress& val) const {
91     static_assert(sizeof(uint64_t) >= RawAddress::kLength);
92     uint64_t int_addr = 0;
93     memcpy(reinterpret_cast<uint8_t*>(&int_addr), val.address,
94            RawAddress::kLength);
95     return std::hash<uint64_t>{}(int_addr);
96   }
97 };
98 
99 #define BD_ADDR_LEN 6 /* Device address length */
100 
101 inline void BDADDR_TO_STREAM(uint8_t*& p, const RawAddress& a) {
102   for (int ijk = 0; ijk < BD_ADDR_LEN; ijk++)
103     *(p)++ = (uint8_t)(a.address)[BD_ADDR_LEN - 1 - ijk];
104 }
105 
106 inline void STREAM_TO_BDADDR(RawAddress& a, const uint8_t*& p) {
107   uint8_t* pbda = (uint8_t*)(a.address) + BD_ADDR_LEN - 1;
108   for (int ijk = 0; ijk < BD_ADDR_LEN; ijk++) *pbda-- = *(p)++;
109 }
110 
111 #if __has_include(<bluetooth/log.h>)
112 #include <bluetooth/log.h>
113 
114 namespace bluetooth::os {
115 bool should_log_be_redacted();
116 }  // namespace bluetooth::os
117 
118 namespace fmt {
119 template <>
120 struct formatter<RawAddress> : formatter<std::string> {
121   template <class Context>
122   typename Context::iterator format(const RawAddress& address,
123                                     Context& ctx) const {
124     std::string repr = bluetooth::os::should_log_be_redacted()
125                            ? address.ToRedactedStringForLogging()
126                            : address.ToStringForLogging();
127     return fmt::formatter<std::string>::format(repr, ctx);
128   }
129 };
130 }  // namespace fmt
131 
132 #endif  // __has_include(<bluetooth/log.h>)
133