• 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 <cstring>
23 #include <string>
24 
25 /** Bluetooth Address */
26 class RawAddress final {
27  public:
28   static constexpr unsigned int kLength = 6;
29 
30   uint8_t address[kLength];
31 
32   RawAddress() = default;
33   RawAddress(const uint8_t (&addr)[kLength]);
34   RawAddress(const std::array<uint8_t, kLength> array);
35 
36   bool operator<(const RawAddress& rhs) const {
37     return (std::memcmp(address, rhs.address, sizeof(address)) < 0);
38   }
39   bool operator==(const RawAddress& rhs) const {
40     return (std::memcmp(address, rhs.address, sizeof(address)) == 0);
41   }
42   bool operator>(const RawAddress& rhs) const { return (rhs < *this); }
43   bool operator<=(const RawAddress& rhs) const { return !(*this > rhs); }
44   bool operator>=(const RawAddress& rhs) const { return !(*this < rhs); }
45   bool operator!=(const RawAddress& rhs) const { return !(*this == rhs); }
46 
IsEmpty()47   bool IsEmpty() const { return *this == kEmpty; }
48 
49   // TODO (b/258090765): remove it and
50   // replace its usage with ToColonSepHexString
51   std::string ToString() const;
52 
53   // Return a string representation in the form of
54   // hexadecimal string separated by colon (:), e.g.,
55   // "12:34:56:ab:cd:ef"
56   std::string ToColonSepHexString() const;
57   // same as ToColonSepHexString
58   std::string ToStringForLogging() const;
59 
60   // Similar with ToColonHexString, ToRedactedStringForLogging returns a
61   // colon separated hexadecimal reprentation of the address but, with the
62   // leftmost 4 bytes masked with "xx", e.g., "xx:xx:xx:xx:ab:cd".
63   std::string ToRedactedStringForLogging() const;
64 
65   // Converts |string| to RawAddress and places it in |to|. If |from| does
66   // not represent a Bluetooth address, |to| is not modified and this function
67   // returns false. Otherwise, it returns true.
68   static bool FromString(const std::string& from, RawAddress& to);
69 
70   // Copies |from| raw Bluetooth address octets to the local object.
71   // Returns the number of copied octets - should be always RawAddress::kLength
72   size_t FromOctets(const uint8_t* from);
73 
74   std::array<uint8_t, kLength> ToArray() const;
75 
76   static bool IsValidAddress(const std::string& address);
77 
78   static const RawAddress kEmpty;  // 00:00:00:00:00:00
79   static const RawAddress kAny;    // FF:FF:FF:FF:FF:FF
80 };
81 
82 inline std::ostream& operator<<(std::ostream& os, const RawAddress& a) {
83   os << a.ToString();
84   return os;
85 }
86 
87 template <>
88 struct std::hash<RawAddress> {
89   std::size_t operator()(const RawAddress& val) const {
90     static_assert(sizeof(uint64_t) >= RawAddress::kLength);
91     uint64_t int_addr = 0;
92     memcpy(reinterpret_cast<uint8_t*>(&int_addr), val.address,
93            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 inline void STREAM_TO_BDADDR(RawAddress& a, const uint8_t*& p) {
106   uint8_t* pbda = (uint8_t*)(a.address) + BD_ADDR_LEN - 1;
107   for (int ijk = 0; ijk < BD_ADDR_LEN; ijk++) *pbda-- = *(p)++;
108 }
109 
110 // DEPRECATED
111 inline void STREAM_TO_BDADDR(RawAddress& a, uint8_t*& p) {
112   uint8_t* pbda = (uint8_t*)(a.address) + BD_ADDR_LEN - 1;
113   for (int ijk = 0; ijk < BD_ADDR_LEN; ijk++) *pbda-- = *(p)++;
114 }
115