| 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 namespace { |
| 11 |
| 12 using ReceiverState = BluetoothLowEnergyWeavePacketReceiver::State; |
| 13 } |
| 14 |
| 15 // static. |
| 16 BluetoothLowEnergyWeaveServerConnection::Factory* |
| 17 BluetoothLowEnergyWeaveServerConnection::Factory::factory_instance_ = |
| 18 nullptr; |
| 19 |
| 20 // static. |
| 21 std::unique_ptr<BluetoothLowEnergyWeaveServerConnection> |
| 22 BluetoothLowEnergyWeaveServerConnection::Factory::NewInstance( |
| 23 const RemoteDevice& remote_device, |
| 24 const BluetoothDevice* bluetooth_device, |
| 25 base::WeakPtr<BluetoothLocalGattCharacteristic> rx_characteristic) { |
| 26 if (factory_instance_ == nullptr) { |
| 27 factory_instance_ = new Factory(); |
| 28 } |
| 29 return factory_instance_->BuildInstance(remote_device, bluetooth_device, |
| 30 rx_characteristic); |
| 31 } |
| 32 |
| 33 // static. |
| 34 void BluetoothLowEnergyWeaveServerConnection::Factory::SetInstanceForTesting( |
| 35 Factory* factory) { |
| 36 factory_instance_ = factory; |
| 37 } |
| 38 |
| 39 std::unique_ptr<BluetoothLowEnergyWeaveServerConnection> |
| 40 BluetoothLowEnergyWeaveServerConnection::Factory::BuildInstance( |
| 41 const RemoteDevice& remote_device, |
| 42 const BluetoothDevice* bluetooth_device, |
| 43 base::WeakPtr<BluetoothLocalGattCharacteristic> rx_characteristic) { |
| 44 return std::unique_ptr<BluetoothLowEnergyWeaveServerConnection>( |
| 45 new BluetoothLowEnergyWeaveServerConnection( |
| 46 remote_device, bluetooth_device, rx_characteristic)); |
| 47 } |
| 48 |
| 49 BluetoothLowEnergyWeaveServerConnection:: |
| 50 BluetoothLowEnergyWeaveServerConnection( |
| 51 const RemoteDevice& remote_device, |
| 52 const BluetoothDevice* bluetooth_device, |
| 53 base::WeakPtr<BluetoothLocalGattCharacteristic> rx_characteristic) |
| 54 : Connection(remote_device), |
| 55 remote_device_(remote_device), |
| 56 bluetooth_device_(bluetooth_device), |
| 57 rx_characteristic_(rx_characteristic), |
| 58 packet_generator_( |
| 59 BluetoothLowEnergyWeavePacketGenerator::Factory::NewInstance()), |
| 60 packet_receiver_( |
| 61 BluetoothLowEnergyWeavePacketReceiver::Factory::NewInstance( |
| 62 BluetoothLowEnergyWeavePacketReceiver::ReceiverType::SERVER)) {} |
| 63 |
| 64 BluetoothLowEnergyWeaveServerConnection:: |
| 65 ~BluetoothLowEnergyWeaveServerConnection() {} |
| 66 |
| 67 void BluetoothLowEnergyWeaveServerConnection::Connect() { |
| 68 if (status() == Status::DISCONNECTED) { |
| 69 SetStatus(Status::IN_PROGRESS); |
| 70 } |
| 71 } |
| 72 |
| 73 void BluetoothLowEnergyWeaveServerConnection::Disconnect() { |
| 74 if (status() != Status::DISCONNECTED) { |
| 75 SetStatus(Status::DISCONNECTED); |
| 76 } |
| 77 } |
| 78 |
| 79 std::string BluetoothLowEnergyWeaveServerConnection::GetDeviceAddress() { |
| 80 return remote_device_.bluetooth_address; |
| 81 } |
| 82 |
| 83 void BluetoothLowEnergyWeaveServerConnection::ReceivePacket(Packet packet) { |
| 84 packet_receiver_->ReceivePacket(packet); |
| 85 |
| 86 ReceiverState state = packet_receiver_->ReceivePacket(packet); |
| 87 |
| 88 switch (state) { |
| 89 case ReceiverState::DATA_READY: |
| 90 OnBytesReceived(packet_receiver_->GetDataMessage()); |
| 91 break; |
| 92 case ReceiverState::CONNECTION_CLOSED: |
| 93 PA_LOG(ERROR) << "Connection closed due to: " << GetReasonForClose(); |
| 94 Disconnect(); |
| 95 break; |
| 96 case ReceiverState::ERROR_DETECTED: |
| 97 OnReceiverError(); |
| 98 break; |
| 99 case ReceiverState::WAITING: |
| 100 // Receiver state should have changed from CONNECTING to WAITING if |
| 101 // a proper connection request had been received. |
| 102 // The max packet size from the connection request will be |
| 103 // used to generate future packets. |
| 104 OnConnected(); |
| 105 break; |
| 106 case ReceiverState::RECEIVING_DATA: |
| 107 // Normal in between states, so do nothing. |
| 108 break; |
| 109 default: |
| 110 NOTREACHED(); |
| 111 } |
| 112 } |
| 113 |
| 114 void BluetoothLowEnergyWeaveServerConnection::SendMessageImpl( |
| 115 std::unique_ptr<WireMessage> message) { |
| 116 DCHECK(status() == Status::CONNECTED); |
| 117 |
| 118 std::string serialized_msg = message->Serialize(); |
| 119 |
| 120 std::vector<Packet> packets = |
| 121 packet_generator_->EncodeDataMessage(serialized_msg); |
| 122 |
| 123 for (auto packet : packets) { |
| 124 rx_characteristic_->NotifyValueChanged(bluetooth_device_, packet, false); |
| 125 } |
| 126 OnDidSendMessage(*message.release(), true); |
| 127 } |
| 128 |
| 129 void BluetoothLowEnergyWeaveServerConnection::OnReceiverError() { |
| 130 rx_characteristic_->NotifyValueChanged( |
| 131 bluetooth_device_, packet_generator_->CreateConnectionClose( |
| 132 packet_receiver_->GetReasonToClose()), |
| 133 false); |
| 134 Disconnect(); |
| 135 } |
| 136 |
| 137 void BluetoothLowEnergyWeaveServerConnection::OnConnected() { |
| 138 packet_generator_->SetMaxPacketSize(packet_receiver_->GetMaxPacketSize()); |
| 139 rx_characteristic_->NotifyValueChanged( |
| 140 bluetooth_device_, packet_generator_->CreateConnectionResponse(), false); |
| 141 SetStatus(Status::CONNECTED); |
| 142 } |
| 143 |
| 144 std::string BluetoothLowEnergyWeaveServerConnection::GetReasonForClose() { |
| 145 switch (packet_receiver_->GetReasonForClose()) { |
| 146 case ReasonForClose::CLOSE_WITHOUT_ERROR: |
| 147 return "CLOSE_WITHOUT_ERROR"; |
| 148 case ReasonForClose::UNKNOWN_ERROR: |
| 149 return "UNKNOWN_ERROR"; |
| 150 case ReasonForClose::NO_COMMON_VERSION_SUPPORTED: |
| 151 return "NO_COMMON_VERSION_SUPPORTED"; |
| 152 case ReasonForClose::RECEIVED_PACKET_OUT_OF_SEQUENCE: |
| 153 return "RECEIVED_PACKET_OUT_OF_SEQUENCE"; |
| 154 case ReasonForClose::APPLICATION_ERROR: |
| 155 return "APPLICATION_ERROR"; |
| 156 default: |
| 157 NOTREACHED(); |
| 158 return ""; |
| 159 } |
| 160 } |
| 161 |
| 162 } // namespace weave |
| 163 |
| 164 } // namespace proximity_auth |
| OLD | NEW |