• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, RawAddress::kLength);
94     return std::hash<uint64_t>{}(int_addr);
95   }
96 };
97 
98 #define BD_ADDR_LEN 6 /* Device address length */
99 
100 inline void BDADDR_TO_STREAM(uint8_t*& p, const RawAddress& a) {
101   for (int ijk = 0; ijk < BD_ADDR_LEN; ijk++) {
102     *(p)++ = (uint8_t)(a.address)[BD_ADDR_LEN - 1 - ijk];
103   }
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++) {
109     *pbda-- = *(p)++;
110   }
111 }
112 
113 #if __has_include(<bluetooth/log.h>)
114 #include <bluetooth/log.h>
115 
116 namespace std {
117 template <>
118 struct formatter<RawAddress> : formatter<std::string> {
119   template <class Context>
120   typename Context::iterator format(const RawAddress& address, Context& ctx) const {
121     std::string repr = address.ToRedactedStringForLogging();
122     return std::formatter<std::string>::format(repr, ctx);
123   }
124 };
125 }  // namespace std
126 
127 #endif  // __has_include(<bluetooth/log.h>)
128