OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROMEOS_COMPONENTS_TETHER_BLE_CONNECTION_MANAGER_H_ |
| 6 #define CHROMEOS_COMPONENTS_TETHER_BLE_CONNECTION_MANAGER_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <set> |
| 10 #include <string> |
| 11 #include <vector> |
| 12 |
| 13 #include "base/macros.h" |
| 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/timer/timer.h" |
| 16 #include "chromeos/components/tether/ble_advertisement_device_queue.h" |
| 17 #include "chromeos/components/tether/ble_advertiser.h" |
| 18 #include "chromeos/components/tether/ble_scanner.h" |
| 19 #include "chromeos/components/tether/proto/tether.pb.h" |
| 20 #include "components/cryptauth/remote_device.h" |
| 21 #include "components/cryptauth/secure_channel.h" |
| 22 |
| 23 namespace cryptauth { |
| 24 class BluetoothThrottler; |
| 25 } // namespace cryptauth |
| 26 |
| 27 namespace chromeos { |
| 28 |
| 29 namespace tether { |
| 30 |
| 31 // Manages connections to remote devices. When a device is registered, |
| 32 // BleConnectionManager intiates a connection to that device. If the connection |
| 33 // succeeds and authenticates successfully, messages can be sent/received |
| 34 // to/from the device. If the connection does not succeed, BleConnectionManager |
| 35 // continues attempting connections until a connection is established or the |
| 36 // device is unregistered. |
| 37 // |
| 38 // To use this class, construct an instance and observe it via AddObserver(). |
| 39 // Then, register device(s) to connect to via RegisterRemoteDevice() and wait |
| 40 // for the OnSecureChannelStatusChanged() callback to be invoked. If the |
| 41 // status for a device changes from |DISCONNECTED| to |CONNECTING| then back to |
| 42 // |DISCONNECTED|, a connection attempt has failed. Clients should set a retry |
| 43 // limit and unregister a device via |UnregsterRemoteDevice()| if multiple |
| 44 // connection attempts have failed. If, instead, a connection succeeds the |
| 45 // status changes to |AUTHENTICATED|, the device can safely send and receive |
| 46 // messages. To send a message, call SendMessage(), and to listen for received |
| 47 // messages, implement the OnMessageReceived() callback. |
| 48 // |
| 49 // Note that a single device can be registered for multiple connection reasons. |
| 50 // If a device is registered for more than one reason, its connections (and |
| 51 // connection attempts) will remain active until all connection reasons have |
| 52 // been unregistered for the device. |
| 53 class BleConnectionManager : public BleScanner::Observer { |
| 54 public: |
| 55 static std::string MessageTypeToString(const MessageType& reason); |
| 56 |
| 57 class Observer { |
| 58 public: |
| 59 virtual void OnSecureChannelStatusChanged( |
| 60 const cryptauth::RemoteDevice& remote_device, |
| 61 const cryptauth::SecureChannel::Status& old_status, |
| 62 const cryptauth::SecureChannel::Status& new_status) = 0; |
| 63 |
| 64 virtual void OnMessageReceived(const cryptauth::RemoteDevice& remote_device, |
| 65 const std::string& payload) = 0; |
| 66 }; |
| 67 |
| 68 class Delegate { |
| 69 public: |
| 70 virtual std::unique_ptr<cryptauth::SecureChannel::Delegate> |
| 71 CreateSecureChannelDelegate() = 0; |
| 72 }; |
| 73 |
| 74 BleConnectionManager( |
| 75 std::unique_ptr<Delegate> delegate, |
| 76 scoped_refptr<device::BluetoothAdapter> adapter, |
| 77 const LocalDeviceDataProvider* local_device_data_provider, |
| 78 const cryptauth::RemoteBeaconSeedFetcher* remote_beacon_seed_fetcher, |
| 79 cryptauth::BluetoothThrottler* bluetooth_throttler); |
| 80 virtual ~BleConnectionManager(); |
| 81 |
| 82 // Registers |remote_device| for |connection_reason|. Once registered, this |
| 83 // instance will continue to attempt to connect and authenticate to that |
| 84 // device until the device is unregistered. |
| 85 void RegisterRemoteDevice(const cryptauth::RemoteDevice& remote_device, |
| 86 const MessageType& connection_reason); |
| 87 |
| 88 // Unregisters |remote_device| for |connection_reason|. Once registered, a |
| 89 // device will continue trying to connect until *ALL* of its |
| 90 // MessageTypes have been unregistered. |
| 91 void UnregisterRemoteDevice(const cryptauth::RemoteDevice& remote_device, |
| 92 const MessageType& connection_reason); |
| 93 |
| 94 // Sends |message| to |remote_device|. This function can only be called if the |
| 95 // given device is authenticated. |
| 96 void SendMessage(const cryptauth::RemoteDevice& remote_device, |
| 97 const std::string& message); |
| 98 |
| 99 void AddObserver(Observer* observer); |
| 100 void RemoveObserver(Observer* observer); |
| 101 |
| 102 // BleScanner::Observer: |
| 103 void OnReceivedAdvertisementFromDevice( |
| 104 const std::string& device_address, |
| 105 cryptauth::RemoteDevice remote_device) override; |
| 106 |
| 107 private: |
| 108 friend class BleConnectionManagerTest; |
| 109 |
| 110 static const int64_t kAdvertisingTimeoutMillis; |
| 111 static const int64_t kFailImmediatelyTimeoutMillis; |
| 112 |
| 113 // Data associated with a registered device. Each registered device has an |
| 114 // associated |ConnectionMetadata| stored in |device_to_metadata_map_|, and |
| 115 // the |ConnectionMetadata| is removed when the device is unregistered. A |
| 116 // |ConnectionMetadata| stores the associated |SecureChannel| for registered |
| 117 // devices which have an active connection. |
| 118 class ConnectionMetadata : public cryptauth::SecureChannel::Observer { |
| 119 public: |
| 120 ConnectionMetadata(const cryptauth::RemoteDevice remote_device, |
| 121 std::shared_ptr<base::Timer> timer, |
| 122 base::WeakPtr<BleConnectionManager> manager); |
| 123 ~ConnectionMetadata(); |
| 124 |
| 125 void RegisterConnectionReason(const MessageType& connection_reason); |
| 126 void UnregisterConnectionReason(const MessageType& connection_reason); |
| 127 bool HasReasonForConnection() const; |
| 128 |
| 129 bool HasEstablishedConnection() const; |
| 130 cryptauth::SecureChannel::Status GetStatus() const; |
| 131 |
| 132 void StartConnectionAttemptTimer(bool use_short_error_timeout); |
| 133 void SetSecureChannel( |
| 134 std::unique_ptr<cryptauth::SecureChannel> secure_channel); |
| 135 void SendMessage(const std::string& payload); |
| 136 |
| 137 // cryptauth::SecureChannel::Observer: |
| 138 void OnSecureChannelStatusChanged( |
| 139 cryptauth::SecureChannel* secure_channel, |
| 140 const cryptauth::SecureChannel::Status& old_status, |
| 141 const cryptauth::SecureChannel::Status& new_status) override; |
| 142 void OnMessageReceived(cryptauth::SecureChannel* secure_channel, |
| 143 const std::string& feature, |
| 144 const std::string& payload) override; |
| 145 |
| 146 private: |
| 147 friend class BleConnectionManagerTest; |
| 148 |
| 149 void OnConnectionAttemptTimeout(); |
| 150 |
| 151 cryptauth::RemoteDevice remote_device_; |
| 152 std::set<MessageType> active_connection_reasons_; |
| 153 std::shared_ptr<cryptauth::SecureChannel> secure_channel_; |
| 154 std::shared_ptr<base::Timer> connection_attempt_timeout_timer_; |
| 155 base::WeakPtr<BleConnectionManager> manager_; |
| 156 |
| 157 base::WeakPtrFactory<ConnectionMetadata> weak_ptr_factory_; |
| 158 }; |
| 159 |
| 160 class TimerFactory { |
| 161 public: |
| 162 virtual std::unique_ptr<base::Timer> CreateTimer(); |
| 163 }; |
| 164 |
| 165 BleConnectionManager( |
| 166 std::unique_ptr<Delegate> delegate, |
| 167 scoped_refptr<device::BluetoothAdapter> adapter, |
| 168 std::unique_ptr<BleScanner> ble_scanner, |
| 169 std::unique_ptr<BleAdvertiser> ble_advertiser, |
| 170 std::unique_ptr<BleAdvertisementDeviceQueue> device_queue, |
| 171 std::unique_ptr<TimerFactory> timer_factory, |
| 172 cryptauth::BluetoothThrottler* bluetooth_throttler); |
| 173 |
| 174 std::shared_ptr<ConnectionMetadata> GetConnectionMetadata( |
| 175 const cryptauth::RemoteDevice& remote_device); |
| 176 std::shared_ptr<ConnectionMetadata> AddMetadataForDevice( |
| 177 const cryptauth::RemoteDevice& remote_device); |
| 178 |
| 179 void UpdateConnectionAttempts(); |
| 180 void UpdateAdvertisementQueue(); |
| 181 |
| 182 void StartConnectionAttempt(const cryptauth::RemoteDevice& remote_device); |
| 183 void StopConnectionAttemptAndMoveToEndOfQueue( |
| 184 const cryptauth::RemoteDevice& remote_device); |
| 185 |
| 186 void OnConnectionAttemptTimeout(const cryptauth::RemoteDevice& remote_device); |
| 187 void OnSecureChannelStatusChanged( |
| 188 const cryptauth::RemoteDevice& remote_device, |
| 189 const cryptauth::SecureChannel::Status& old_status, |
| 190 const cryptauth::SecureChannel::Status& new_status); |
| 191 |
| 192 void SendMessageReceivedEvent(const cryptauth::RemoteDevice& remote_device, |
| 193 const std::string& payload); |
| 194 void SendSecureChannelStatusChangeEvent( |
| 195 const cryptauth::RemoteDevice& remote_device, |
| 196 const cryptauth::SecureChannel::Status& old_status, |
| 197 const cryptauth::SecureChannel::Status& new_status); |
| 198 |
| 199 std::unique_ptr<Delegate> delegate_; |
| 200 scoped_refptr<device::BluetoothAdapter> adapter_; |
| 201 std::unique_ptr<BleScanner> ble_scanner_; |
| 202 std::unique_ptr<BleAdvertiser> ble_advertiser_; |
| 203 std::unique_ptr<BleAdvertisementDeviceQueue> device_queue_; |
| 204 std::unique_ptr<TimerFactory> timer_factory_; |
| 205 cryptauth::BluetoothThrottler* bluetooth_throttler_; |
| 206 |
| 207 std::map<cryptauth::RemoteDevice, std::shared_ptr<ConnectionMetadata>> |
| 208 device_to_metadata_map_; |
| 209 |
| 210 base::ObserverList<Observer> observer_list_; |
| 211 base::WeakPtrFactory<BleConnectionManager> weak_ptr_factory_; |
| 212 |
| 213 DISALLOW_COPY_AND_ASSIGN(BleConnectionManager); |
| 214 }; |
| 215 |
| 216 } // namespace tether |
| 217 |
| 218 } // namespace chromeos |
| 219 |
| 220 #endif // CHROMEOS_COMPONENTS_TETHER_BLE_CONNECTION_MANAGER_H_ |
OLD | NEW |