1 /****************************************************************************** 2 * 3 * Copyright 2019 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 <initializer_list> 24 #include <optional> 25 #include <ostream> 26 #include <string> 27 28 #include "common/interfaces/ILoggable.h" 29 #include "packet/custom_field_fixed_size_interface.h" 30 #include "storage/serializable.h" 31 32 namespace bluetooth { 33 namespace hci { 34 35 class Address final : public packet::CustomFieldFixedSizeInterface<Address>, 36 public storage::Serializable<Address>, 37 public bluetooth::common::IRedactableLoggable { 38 public: 39 static constexpr size_t kLength = 6; 40 41 std::array<uint8_t, kLength> address = {}; 42 43 Address() = default; 44 Address(const uint8_t (&addr)[kLength]); 45 Address(std::initializer_list<uint8_t> l); 46 47 // CustomFieldFixedSizeInterface methods data()48 inline uint8_t* data() override { 49 return address.data(); 50 } data()51 inline const uint8_t* data() const override { 52 return address.data(); 53 } 54 55 // storage::Serializable methods 56 std::string ToString() const override; 57 std::string ToColonSepHexString() const; 58 std::string ToStringForLogging() const override; 59 std::string ToRedactedStringForLogging() const override; 60 61 static std::optional<Address> FromString(const std::string& from); 62 std::string ToLegacyConfigString() const override; 63 static std::optional<Address> FromLegacyConfigString(const std::string& str); 64 65 bool operator<(const Address& rhs) const { 66 return address < rhs.address; 67 } 68 bool operator==(const Address& rhs) const { 69 return address == rhs.address; 70 } 71 bool operator>(const Address& rhs) const { 72 return (rhs < *this); 73 } 74 bool operator<=(const Address& rhs) const { 75 return !(*this > rhs); 76 } 77 bool operator>=(const Address& rhs) const { 78 return !(*this < rhs); 79 } 80 bool operator!=(const Address& rhs) const { 81 return !(*this == rhs); 82 } 83 IsEmpty()84 bool IsEmpty() const { 85 return *this == kEmpty; 86 } 87 88 // Converts |string| to Address and places it in |to|. If |from| does 89 // not represent a Bluetooth address, |to| is not modified and this function 90 // returns false. Otherwise, it returns true. 91 static bool FromString(const std::string& from, Address& to); 92 93 // Copies |from| raw Bluetooth address octets to the local object. 94 // Returns the number of copied octets - should be always Address::kLength 95 size_t FromOctets(const uint8_t* from); 96 97 static bool IsValidAddress(const std::string& address); 98 99 static const Address kEmpty; // 00:00:00:00:00:00 100 static const Address kAny; // FF:FF:FF:FF:FF:FF 101 private: 102 std::string _ToMaskedColonSepHexString(int bytes_to_mask) const; 103 }; 104 105 // TODO: to fine-tune this. 106 // we need an interface between the logger and ILoggable 107 inline std::ostream& operator<<(std::ostream& os, const Address& a) { 108 os << a.ToString(); 109 return os; 110 } 111 112 } // namespace hci 113 } // namespace bluetooth 114 115 namespace std { 116 template <> 117 struct hash<bluetooth::hci::Address> { 118 std::size_t operator()(const bluetooth::hci::Address& val) const { 119 static_assert(sizeof(uint64_t) >= bluetooth::hci::Address::kLength); 120 uint64_t int_addr = 0; 121 memcpy(reinterpret_cast<uint8_t*>(&int_addr), val.data(), bluetooth::hci::Address::kLength); 122 return std::hash<uint64_t>{}(int_addr); 123 } 124 }; 125 } // namespace std 126