| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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 #include "components/proximity_auth/ble/bluetooth_low_energy_weave_server_connec
tion.h" |
| 6 #include "components/proximity_auth/logging/logging.h" |
| 7 |
| 8 namespace proximity_auth { |
| 9 namespace weave { |
| 10 |
| 11 // static. |
| 12 BluetoothLowEnergyWeaveServerConnection::Factory* |
| 13 BluetoothLowEnergyWeaveServerConnection::Factory::factory_instance_ = |
| 14 nullptr; |
| 15 |
| 16 // static. |
| 17 std::unique_ptr<BluetoothLowEnergyWeaveServerConnection> |
| 18 BluetoothLowEnergyWeaveServerConnection::Factory::NewInstance( |
| 19 const BluetoothDevice* bluetooth_device, |
| 20 base::WeakPtr<BluetoothLowEnergyWeaveServer> server, |
| 21 base::WeakPtr<BluetoothLocalGattCharacteristic> rx_characteristic) { |
| 22 if (factory_instance_ == nullptr) { |
| 23 factory_instance_ = new Factory(); |
| 24 } |
| 25 return factory_instance_->BuildInstance(bluetooth_device, server, |
| 26 rx_characteristic); |
| 27 } |
| 28 |
| 29 // static. |
| 30 void BluetoothLowEnergyWeaveServerConnection::Factory::SetInstanceForTesting( |
| 31 Factory* factory) { |
| 32 factory_instance_ = factory; |
| 33 } |
| 34 |
| 35 std::unique_ptr<BluetoothLowEnergyWeaveServerConnection> |
| 36 BluetoothLowEnergyWeaveServerConnection::Factory::BuildInstance( |
| 37 const BluetoothDevice* bluetooth_device, |
| 38 base::WeakPtr<BluetoothLowEnergyWeaveServer> server, |
| 39 base::WeakPtr<BluetoothLocalGattCharacteristic> rx_characteristic) { |
| 40 return std::unique_ptr<BluetoothLowEnergyWeaveServerConnection>( |
| 41 new BluetoothLowEnergyWeaveServerConnection(bluetooth_device, server, |
| 42 rx_characteristic)); |
| 43 } |
| 44 |
| 45 BluetoothLowEnergyWeaveServerConnection:: |
| 46 BluetoothLowEnergyWeaveServerConnection( |
| 47 const BluetoothDevice* bluetooth_device, |
| 48 base::WeakPtr<BluetoothLowEnergyWeaveServer> server, |
| 49 base::WeakPtr<BluetoothLocalGattCharacteristic> rx_characteristic) |
| 50 : Connection(server->GetRemoteDevice(bluetooth_device)), |
| 51 remote_device_(server->GetRemoteDevice(bluetooth_device)), |
| 52 bluetooth_device_(bluetooth_device), |
| 53 server_(server), |
| 54 rx_characteristic_(rx_characteristic), |
| 55 packet_generator_( |
| 56 BluetoothLowEnergyWeavePacketGenerator::Factory::NewInstance()), |
| 57 packet_receiver_( |
| 58 BluetoothLowEnergyWeavePacketReceiver::Factory::NewInstance( |
| 59 BluetoothLowEnergyWeavePacketReceiver::ReceiverType::CLIENT)) {} |
| 60 |
| 61 BluetoothLowEnergyWeaveServerConnection:: |
| 62 ~BluetoothLowEnergyWeaveServerConnection() {} |
| 63 |
| 64 void BluetoothLowEnergyWeaveServerConnection::Connect() { |
| 65 if (status() == Status::DISCONNECTED) { |
| 66 server_->StopAdvertising(remote_device_); |
| 67 SetStatus(Status::CONNECTED); |
| 68 } |
| 69 } |
| 70 |
| 71 void BluetoothLowEnergyWeaveServerConnection::Disconnect() { |
| 72 if (status() != Status::DISCONNECTED) { |
| 73 server_->StartAdvertising(remote_device_); |
| 74 SetStatus(Status::DISCONNECTED); |
| 75 } |
| 76 } |
| 77 |
| 78 std::string BluetoothLowEnergyWeaveServerConnection::GetDeviceAddress() { |
| 79 return bluetooth_device_->GetAddress(); |
| 80 } |
| 81 |
| 82 bool BluetoothLowEnergyWeaveServerConnection::ReceivePacket(Packet packet) { |
| 83 packet_receiver_->ReceivePacket(packet); |
| 84 // TODO(jingxuy): error checking on this and possibly send connection close as |
| 85 // the result. |
| 86 return true; |
| 87 } |
| 88 |
| 89 void BluetoothLowEnergyWeaveServerConnection::SendMessageImpl( |
| 90 std::unique_ptr<WireMessage> message) { |
| 91 if (status() != Status::CONNECTED) |
| 92 return; |
| 93 |
| 94 std::string serialized_msg = message->Serialize(); |
| 95 |
| 96 std::vector<Packet> packets = |
| 97 packet_generator_->EncodeDataMessage(serialized_msg); |
| 98 |
| 99 for (auto packet : packets) { |
| 100 rx_characteristic_->NotifyValueChanged(bluetooth_device_, packet, false); |
| 101 } |
| 102 } |
| 103 |
| 104 } // namespace weave |
| 105 |
| 106 } // namespace proximity_auth |
| OLD | NEW |