1 /* 2 * Copyright 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #pragma once 17 18 #include <array> 19 #include <chrono> 20 #include <cstdint> 21 #include <list> 22 #include <memory> 23 #include <mutex> 24 #include <string> 25 26 #include "hci/address.h" 27 #include "module.h" 28 #include "storage/config_cache.h" 29 #include "storage/device.h" 30 #include "storage/mutation.h" 31 32 namespace bluetooth { 33 34 namespace shim { 35 class BtifConfigInterface; 36 } 37 38 namespace security::internal { 39 class SecurityManagerImpl; 40 } 41 42 namespace hci { 43 class AclManager; 44 } 45 46 namespace storage { 47 48 class StorageModule : public bluetooth::Module { 49 public: 50 static const std::string kInfoSection; 51 static const std::string kFileSourceProperty; 52 static const std::string kTimeCreatedProperty; 53 static const std::string kTimeCreatedFormat; 54 55 static const std::string kAdapterSection; 56 57 StorageModule(const StorageModule&) = delete; 58 StorageModule& operator=(const StorageModule&) = delete; 59 60 ~StorageModule(); 61 static const ModuleFactory Factory; 62 63 // Methods to access the storage layer via Device abstraction 64 // - Devices will be lazily created when methods below are called. Hence, no std::optional<> nor nullptr is used in 65 // the return type. User of the API can use the Device object's API to find out if the device has existed before 66 // - Devices with no config values will not be saved to config cache 67 // - Devices that are not paired will also be discarded when stack shutdown 68 69 // Concept: 70 // 71 // BR/EDR Address: 72 // -> Public static address only, begin with 3 byte IEEE assigned OUI number 73 // 74 // BLE Addresses 75 // -> Public Address: begin with IEEE assigned OUI number 76 // -> Static: static public address do not change 77 // -> Private/Variable: We haven't seen private/variable public address yet 78 // -> Random Address: randomly generated, does not begin with IEEE assigned OUI number 79 // -> Static: static random address do not change 80 // -> Private/Variable: private random address changes once so often 81 // -> Resolvable: this address can be resolved into a static address using identity resolving key (IRK) 82 // -> Non-resolvable: this address is for temporary use only, do not save this address 83 // 84 // MAC addresses are six bytes only and hence are only regionally unique 85 86 // Get a device object using the |legacy_key_address|. In legacy config, each device's config is stored in a config 87 // section keyed by a single MAC address. For BR/EDR device, this is straightforward as a BR/EDR device has only a 88 // single public static MAC address. However, for LE devices using private addresses, we only learn its real static 89 // address after pairing. Since we still need to store that device's information prior to pairing, we use the 90 // first-seen address of that device, no matter random private or static public, as a "key" to store that device's 91 // config. This method gives you a device object using this legacy key. If the key does not exist, the device will 92 // be lazily created in the config 93 Device GetDeviceByLegacyKey(hci::Address legacy_key_address); 94 95 // A classic (BR/EDR) or dual mode device can be uniquely located by its classic (BR/EDR) MAC address 96 Device GetDeviceByClassicMacAddress(hci::Address classic_address); 97 98 // A LE or dual mode device can be uniquely located by its identity address that is either: 99 // -> Public static address 100 // -> Random static address 101 // If remote device uses LE random private resolvable address, user of this API must resolve its identity address 102 // before calling this method to get the device object 103 // 104 // Note: A dual mode device's identity address is normally the same as its BR/EDR address, but they can also be 105 // different. Hence, please don't make such assumption and don't use GetDeviceByBrEdrMacAddress() interchangeably 106 Device GetDeviceByLeIdentityAddress(hci::Address le_identity_address); 107 108 // Get a list of bonded devices from config 109 std::vector<Device> GetBondedDevices(); 110 111 // Modify the underlying config by starting a mutation. All entries in the mutation will be applied atomically when 112 // Commit() is called. User should never touch ConfigCache() directly. 113 Mutation Modify(); 114 115 protected: 116 void ListDependencies(ModuleList* list) const override; 117 void Start() override; 118 void Stop() override; 119 std::string ToString() const override; 120 121 friend shim::BtifConfigInterface; 122 friend hci::AclManager; 123 friend security::internal::SecurityManagerImpl; 124 // For unit test only 125 ConfigCache* GetMemoryOnlyConfigCache(); 126 // Normally, underlying config will be saved at most 3 seconds after the first config change in a series of changes 127 // This method triggers the delayed saving automatically, the delay is equal to |config_save_delay_| 128 void SaveDelayed(); 129 // In some cases, one may want to save the config immediately to disk. Call this method with caution as it runs 130 // immediately on the calling thread 131 void SaveImmediately(); 132 // remove all content in this config cache, restore it to the state after the explicit constructor 133 void Clear(); 134 135 // Create the storage module where: 136 // - config_file_path is the path to the config file on disk, a .bak file will be created with the original 137 // - config_save_delay is the duration after which to dump config to disk after SaveDelayed() is called 138 // - temp_devices_capacity is the number of temporary, typically unpaired devices to hold in a memory based LRU 139 // - is_restricted_mode and is_single_user_mode are flags from upper layer 140 StorageModule( 141 std::string config_file_path, 142 std::chrono::milliseconds config_save_delay, 143 size_t temp_devices_capacity, 144 bool is_restricted_mode, 145 bool is_single_user_mode); 146 147 bool HasSection(const std::string& section) const; 148 bool HasProperty(const std::string& section, const std::string& property) const; 149 150 std::optional<std::string> GetProperty( 151 const std::string& section, const std::string& property) const; 152 void SetProperty(std::string section, std::string property, std::string value); 153 154 std::vector<std::string> GetPersistentSections() const; 155 156 void RemoveSection(const std::string& section); 157 bool RemoveProperty(const std::string& section, const std::string& property); 158 void ConvertEncryptOrDecryptKeyIfNeeded(); 159 // Remove sections with |property| set 160 void RemoveSectionWithProperty(const std::string& property); 161 162 void SetBool(const std::string& section, const std::string& property, bool value); 163 std::optional<bool> GetBool(const std::string& section, const std::string& property) const; 164 void SetUint64(const std::string& section, const std::string& property, uint64_t value); 165 std::optional<uint64_t> GetUint64(const std::string& section, const std::string& property) const; 166 void SetUint32(const std::string& section, const std::string& property, uint32_t value); 167 std::optional<uint32_t> GetUint32(const std::string& section, const std::string& property) const; 168 void SetInt64(const std::string& section, const std::string& property, int64_t value); 169 std::optional<int64_t> GetInt64(const std::string& section, const std::string& property) const; 170 void SetInt(const std::string& section, const std::string& property, int value); 171 std::optional<int> GetInt(const std::string& section, const std::string& property) const; 172 void SetBin( 173 const std::string& section, const std::string& property, const std::vector<uint8_t>& value); 174 std::optional<std::vector<uint8_t>> GetBin( 175 const std::string& section, const std::string& property) const; 176 177 private: 178 struct impl; 179 mutable std::recursive_mutex mutex_; 180 std::unique_ptr<impl> pimpl_; 181 std::string config_file_path_; 182 std::string config_backup_path_; 183 std::chrono::milliseconds config_save_delay_; 184 size_t temp_devices_capacity_; 185 bool is_restricted_mode_; 186 bool is_single_user_mode_; 187 static bool is_config_checksum_pass(int check_bit); 188 }; 189 190 } // namespace storage 191 } // namespace bluetooth 192