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 
35   bool operator<(const RawAddress& rhs) const {
36     return (std::memcmp(address, rhs.address, sizeof(address)) < 0);
37   }
38   bool operator==(const RawAddress& rhs) const {
39     return (std::memcmp(address, rhs.address, sizeof(address)) == 0);
40   }
41   bool operator>(const RawAddress& rhs) const { return (rhs < *this); }
42   bool operator<=(const RawAddress& rhs) const { return !(*this > rhs); }
43   bool operator>=(const RawAddress& rhs) const { return !(*this < rhs); }
44   bool operator!=(const RawAddress& rhs) const { return !(*this == rhs); }
45 
IsEmpty()46   bool IsEmpty() const { return *this == kEmpty; }
47 
48   std::string ToString() const;
49 
50   // Converts |string| to RawAddress and places it in |to|. If |from| does
51   // not represent a Bluetooth address, |to| is not modified and this function
52   // returns false. Otherwise, it returns true.
53   static bool FromString(const std::string& from, RawAddress& to);
54 
55   // Copies |from| raw Bluetooth address octets to the local object.
56   // Returns the number of copied octets - should be always RawAddress::kLength
57   size_t FromOctets(const uint8_t* from);
58 
59   static bool IsValidAddress(const std::string& address);
60 
61   static const RawAddress kEmpty;  // 00:00:00:00:00:00
62   static const RawAddress kAny;    // FF:FF:FF:FF:FF:FF
63 };
64 
65 inline std::ostream& operator<<(std::ostream& os, const RawAddress& a) {
66   os << a.ToString();
67   return os;
68 }
69 
70 template <>
71 struct std::hash<RawAddress> {
72   std::size_t operator()(const RawAddress& val) const {
73     static_assert(sizeof(uint64_t) >= RawAddress::kLength);
74     uint64_t int_addr = 0;
75     memcpy(reinterpret_cast<uint8_t*>(&int_addr), val.address,
76            RawAddress::kLength);
77     return std::hash<uint64_t>{}(int_addr);
78   }
79 };
80