Chromium Code Reviews| Index: components/proximity_auth/ble/bluetooth_low_energy_weave_server_connection.cc |
| diff --git a/components/proximity_auth/ble/bluetooth_low_energy_weave_server_connection.cc b/components/proximity_auth/ble/bluetooth_low_energy_weave_server_connection.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..883a37edbb08dce398bda94e0b1ef8c7e162142c |
| --- /dev/null |
| +++ b/components/proximity_auth/ble/bluetooth_low_energy_weave_server_connection.cc |
| @@ -0,0 +1,158 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/proximity_auth/ble/bluetooth_low_energy_weave_server_connection.h" |
| +#include "components/proximity_auth/logging/logging.h" |
| + |
| +namespace proximity_auth { |
| +namespace weave { |
| +namespace { |
| + |
| +using ReceiverState = BluetoothLowEnergyWeavePacketReceiver::State; |
| +} |
| + |
| +// static. |
| +BluetoothLowEnergyWeaveServerConnection::Factory* |
| + BluetoothLowEnergyWeaveServerConnection::Factory::factory_instance_ = |
| + nullptr; |
| + |
| +// static. |
| +std::unique_ptr<BluetoothLowEnergyWeaveServerConnection> |
| +BluetoothLowEnergyWeaveServerConnection::Factory::NewInstance( |
| + const BluetoothDevice* bluetooth_device, |
| + base::WeakPtr<BluetoothLowEnergyWeaveServer> server, |
| + base::WeakPtr<BluetoothLocalGattCharacteristic> rx_characteristic) { |
| + if (factory_instance_ == nullptr) { |
| + factory_instance_ = new Factory(); |
| + } |
| + return factory_instance_->BuildInstance(bluetooth_device, server, |
| + rx_characteristic); |
| +} |
| + |
| +// static. |
| +void BluetoothLowEnergyWeaveServerConnection::Factory::SetInstanceForTesting( |
| + Factory* factory) { |
| + factory_instance_ = factory; |
| +} |
| + |
| +std::unique_ptr<BluetoothLowEnergyWeaveServerConnection> |
| +BluetoothLowEnergyWeaveServerConnection::Factory::BuildInstance( |
| + const BluetoothDevice* bluetooth_device, |
| + base::WeakPtr<BluetoothLowEnergyWeaveServer> server, |
| + base::WeakPtr<BluetoothLocalGattCharacteristic> rx_characteristic) { |
| + return std::unique_ptr<BluetoothLowEnergyWeaveServerConnection>( |
| + new BluetoothLowEnergyWeaveServerConnection(bluetooth_device, server, |
| + rx_characteristic)); |
| +} |
| + |
| +BluetoothLowEnergyWeaveServerConnection:: |
| + BluetoothLowEnergyWeaveServerConnection( |
| + const BluetoothDevice* bluetooth_device, |
| + base::WeakPtr<BluetoothLowEnergyWeaveServer> server, |
| + base::WeakPtr<BluetoothLocalGattCharacteristic> rx_characteristic) |
| + : Connection(server->GetRemoteDevice(bluetooth_device)), |
| + remote_device_(server->GetRemoteDevice(bluetooth_device)), |
| + bluetooth_device_(bluetooth_device), |
| + server_(server), |
| + rx_characteristic_(rx_characteristic), |
| + packet_generator_( |
| + BluetoothLowEnergyWeavePacketGenerator::Factory::NewInstance()), |
| + packet_receiver_( |
| + BluetoothLowEnergyWeavePacketReceiver::Factory::NewInstance( |
| + BluetoothLowEnergyWeavePacketReceiver::ReceiverType::SERVER)) {} |
| + |
| +BluetoothLowEnergyWeaveServerConnection:: |
| + ~BluetoothLowEnergyWeaveServerConnection() {} |
| + |
| +void BluetoothLowEnergyWeaveServerConnection::Connect() { |
| + if (status() == Status::DISCONNECTED) { |
| + server_->DisableAdvertising(remote_device_); |
| + SetStatus(Status::IN_PROGRESS); |
| + } |
| +} |
| + |
| +void BluetoothLowEnergyWeaveServerConnection::Disconnect() { |
| + if (status() != Status::DISCONNECTED) { |
| + server_->EnableAdvertising(remote_device_); |
| + SetStatus(Status::DISCONNECTED); |
| + } |
| +} |
| + |
| +std::string BluetoothLowEnergyWeaveServerConnection::GetDeviceAddress() { |
| + return bluetooth_device_->GetAddress(); |
| +} |
| + |
| +void BluetoothLowEnergyWeaveServerConnection::ReceivePacket(Packet packet) { |
| + packet_receiver_->ReceivePacket(packet); |
| + |
| + ReceiverState state = packet_receiver_->ReceivePacket(packet); |
| + |
| + switch (state) { |
| + case ReceiverState::DATA_READY: |
| + OnBytesReceived(packet_receiver_->GetDataMessage()); |
| + break; |
| + case ReceiverState::CONNECTION_CLOSED: |
| + PA_LOG(ERROR) << "Connection closed due to: " << GetReasonForClose(); |
| + Disconnect(); |
| + break; |
| + case ReceiverState::ERROR_DETECTED: |
| + rx_characteristic_->NotifyValueChanged( |
|
Kyle Horimoto
2016/07/29 03:15:39
You need to do the same sort of handling as you di
jingxuy
2016/07/29 18:45:43
The client class is different because the sending
|
| + bluetooth_device_, packet_generator_->CreateConnectionClose( |
| + packet_receiver_->GetReasonToClose()), |
| + false); |
| + break; |
| + case ReceiverState::WAITING: |
| + // Receiver state should have changed from CONNECTING to WAITING if |
| + // a proper connection request had been received. |
| + // The max packet size from the connection request will be |
| + // used to generate future packets. |
| + packet_generator_->SetMaxPacketSize(packet_receiver_->GetMaxPacketSize()); |
| + rx_characteristic_->NotifyValueChanged( |
| + bluetooth_device_, packet_generator_->CreateConnectionResponse(), |
| + false); |
| + SetStatus(Status::CONNECTED); |
| + break; |
| + case ReceiverState::RECEIVING_DATA: |
| + // Normal in between states, so do nothing. |
| + break; |
| + default: |
| + NOTREACHED(); |
| + } |
| +} |
| + |
| +void BluetoothLowEnergyWeaveServerConnection::SendMessageImpl( |
| + std::unique_ptr<WireMessage> message) { |
| + DCHECK(status() == Status::CONNECTED); |
| + |
| + std::string serialized_msg = message->Serialize(); |
| + |
| + std::vector<Packet> packets = |
| + packet_generator_->EncodeDataMessage(serialized_msg); |
| + |
| + for (auto packet : packets) { |
| + rx_characteristic_->NotifyValueChanged(bluetooth_device_, packet, false); |
|
Kyle Horimoto
2016/07/29 03:15:39
You don't call the OnDidSendMessage() callback.
jingxuy
2016/07/29 18:45:43
Done.
|
| + } |
| +} |
| + |
| +std::string BluetoothLowEnergyWeaveServerConnection::GetReasonForClose() { |
| + switch (packet_receiver_->GetReasonForClose()) { |
| + case ReasonForClose::CLOSE_WITHOUT_ERROR: |
| + return "CLOSE_WITHOUT_ERROR"; |
| + case ReasonForClose::UNKNOWN_ERROR: |
| + return "UNKNOWN_ERROR"; |
| + case ReasonForClose::NO_COMMON_VERSION_SUPPORTED: |
| + return "NO_COMMON_VERSION_SUPPORTED"; |
| + case ReasonForClose::RECEIVED_PACKET_OUT_OF_SEQUENCE: |
| + return "RECEIVED_PACKET_OUT_OF_SEQUENCE"; |
| + case ReasonForClose::APPLICATION_ERROR: |
| + return "APPLICATION_ERROR"; |
| + default: |
| + NOTREACHED(); |
| + return ""; |
| + } |
| +} |
| + |
| +} // namespace weave |
| + |
| +} // namespace proximity_auth |