• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/adapter_config.h"
29 #include "storage/config_cache.h"
30 #include "storage/device.h"
31 #include "storage/mutation.h"
32 
33 namespace bluetooth {
34 
35 namespace shim {
36 class BtifConfigInterface;
37 }
38 
39 namespace storage {
40 
41 class StorageModule : public bluetooth::Module {
42  public:
43   static const std::string kInfoSection;
44   static const std::string kFileSourceProperty;
45   static const std::string kTimeCreatedProperty;
46   static const std::string kTimeCreatedFormat;
47 
48   static const std::string kAdapterSection;
49 
50   ~StorageModule();
51   static const ModuleFactory Factory;
52 
53   // Methods to access the storage layer via Device abstraction
54   // - Devices will be lazily created when methods below are called. Hence, no std::optional<> nor nullptr is used in
55   //   the return type. User of the API can use the Device object's API to find out if the device has existed before
56   // - Devices with no config values will not be saved to config cache
57   // - Devices that are not paired will also be discarded when stack shutdown
58 
59   // Concept:
60   //
61   // BR/EDR Address:
62   //  -> Public static address only, begin with 3 byte IEEE assigned OUI number
63   //
64   // BLE Addresses
65   //  -> Public Address: begin with IEEE assigned OUI number
66   //     -> Static: static public address do not change
67   //     -> Private/Variable: We haven't seen private/variable public address yet
68   //  -> Random Address: randomly generated, does not begin with IEEE assigned OUI number
69   //     -> Static: static random address do not change
70   //     -> Private/Variable: private random address changes once so often
71   //        -> Resolvable: this address can be resolved into a static address using identity resolving key (IRK)
72   //        -> Non-resolvable: this address is for temporary use only, do not save this address
73   //
74   // MAC addresses are six bytes only and hence are only regionally unique
75 
76   // Get a device object using the |legacy_key_address|. In legacy config, each device's config is stored in a config
77   // section keyed by a single MAC address. For BR/EDR device, this is straightforward as a BR/EDR device has only a
78   // single public static MAC address. However, for LE devices using private addresses, we only learn its real static
79   // address after pairing. Since we still need to store that device's information prior to pairing, we use the
80   // first-seen address of that device, no matter random private or static public, as a "key" to store that device's
81   // config. This method gives you a device object using this legacy key. If the key does not exist, the device will
82   // be lazily created in the config
83   Device GetDeviceByLegacyKey(hci::Address legacy_key_address);
84 
85   // A classic (BR/EDR) or dual mode device can be uniquely located by its classic (BR/EDR) MAC address
86   Device GetDeviceByClassicMacAddress(hci::Address classic_address);
87 
88   // A LE or dual mode device can be uniquely located by its identity address that is either:
89   //   -> Public static address
90   //   -> Random static address
91   // If remote device uses LE random private resolvable address, user of this API must resolve its identity address
92   // before calling this method to get the device object
93   //
94   // Note: A dual mode device's identity address is normally the same as its BR/EDR address, but they can also be
95   // different. Hence, please don't make such assumption and don't use GetDeviceByBrEdrMacAddress() interchangeably
96   Device GetDeviceByLeIdentityAddress(hci::Address le_identity_address);
97 
98   // A think copyable, movable, comparable object that is used to access adapter level information
99   AdapterConfig GetAdapterConfig();
100 
101   // Get a list of bonded devices from config
102   std::vector<Device> GetBondedDevices();
103 
104   // Modify the underlying config by starting a mutation. All entries in the mutation will be applied atomically when
105   // Commit() is called. User should never touch ConfigCache() directly.
106   Mutation Modify();
107 
108  protected:
109   void ListDependencies(ModuleList* list) override;
110   void Start() override;
111   void Stop() override;
112   std::string ToString() const override;
113 
114   friend shim::BtifConfigInterface;
115   // For shim layer only
116   ConfigCache* GetConfigCache();
117   // For unit test only
118   ConfigCache* GetMemoryOnlyConfigCache();
119   // Normally, underlying config will be saved at most 3 seconds after the first config change in a series of changes
120   // This method triggers the delayed saving automatically, the delay is equal to |config_save_delay_|
121   void SaveDelayed();
122   // In some cases, one may want to save the config immediately to disk. Call this method with caution as it runs
123   // immediately on the calling thread
124   void SaveImmediately();
125 
126   // Create the storage module where:
127   // - config_file_path is the path to the config file on disk, a .bak file will be created with the original
128   // - config_save_delay is the duration after which to dump config to disk after SaveDelayed() is called
129   // - temp_devices_capacity is the number of temporary, typically unpaired devices to hold in a memory based LRU
130   // - is_restricted_mode and is_single_user_mode are flags from upper layer
131   StorageModule(
132       std::string config_file_path,
133       std::chrono::milliseconds config_save_delay,
134       size_t temp_devices_capacity,
135       bool is_restricted_mode,
136       bool is_single_user_mode);
137 
138  private:
139   struct impl;
140   mutable std::recursive_mutex mutex_;
141   std::unique_ptr<impl> pimpl_;
142   std::string config_file_path_;
143   std::string config_backup_path_;
144   std::chrono::milliseconds config_save_delay_;
145   size_t temp_devices_capacity_;
146   bool is_restricted_mode_;
147   bool is_single_user_mode_;
148 
149   DISALLOW_COPY_AND_ASSIGN(StorageModule);
150 };
151 
152 }  // namespace storage
153 }  // namespace bluetooth
154