Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Unified Diff: components/proximity_auth/ble/bluetooth_low_energy_weave_server_connection.cc

Issue 2183523006: Chrome OS uWeave Characteristics Server (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@migration
Patch Set: added channel Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « components/proximity_auth/ble/bluetooth_low_energy_weave_server_connection.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..2374118d49c629eb1665fd3f2dff68b04d262c67
--- /dev/null
+++ b/components/proximity_auth/ble/bluetooth_low_energy_weave_server_connection.cc
@@ -0,0 +1,164 @@
+// 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 RemoteDevice& remote_device,
+ const BluetoothDevice* bluetooth_device,
+ base::WeakPtr<BluetoothLocalGattCharacteristic> rx_characteristic) {
+ if (factory_instance_ == nullptr) {
+ factory_instance_ = new Factory();
+ }
+ return factory_instance_->BuildInstance(remote_device, bluetooth_device,
+ rx_characteristic);
+}
+
+// static.
+void BluetoothLowEnergyWeaveServerConnection::Factory::SetInstanceForTesting(
+ Factory* factory) {
+ factory_instance_ = factory;
+}
+
+std::unique_ptr<BluetoothLowEnergyWeaveServerConnection>
+BluetoothLowEnergyWeaveServerConnection::Factory::BuildInstance(
+ const RemoteDevice& remote_device,
+ const BluetoothDevice* bluetooth_device,
+ base::WeakPtr<BluetoothLocalGattCharacteristic> rx_characteristic) {
+ return std::unique_ptr<BluetoothLowEnergyWeaveServerConnection>(
+ new BluetoothLowEnergyWeaveServerConnection(
+ remote_device, bluetooth_device, rx_characteristic));
+}
+
+BluetoothLowEnergyWeaveServerConnection::
+ BluetoothLowEnergyWeaveServerConnection(
+ const RemoteDevice& remote_device,
+ const BluetoothDevice* bluetooth_device,
+ base::WeakPtr<BluetoothLocalGattCharacteristic> rx_characteristic)
+ : Connection(remote_device),
+ remote_device_(remote_device),
+ bluetooth_device_(bluetooth_device),
+ 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) {
+ SetStatus(Status::IN_PROGRESS);
+ }
+}
+
+void BluetoothLowEnergyWeaveServerConnection::Disconnect() {
+ if (status() != Status::DISCONNECTED) {
+ SetStatus(Status::DISCONNECTED);
+ }
+}
+
+std::string BluetoothLowEnergyWeaveServerConnection::GetDeviceAddress() {
+ return remote_device_.bluetooth_address;
+}
+
+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:
+ OnReceiverError();
+ 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.
+ OnConnected();
+ 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);
+ }
+ OnDidSendMessage(*message.release(), true);
+}
+
+void BluetoothLowEnergyWeaveServerConnection::OnReceiverError() {
+ rx_characteristic_->NotifyValueChanged(
+ bluetooth_device_, packet_generator_->CreateConnectionClose(
+ packet_receiver_->GetReasonToClose()),
+ false);
+ Disconnect();
+}
+
+void BluetoothLowEnergyWeaveServerConnection::OnConnected() {
+ packet_generator_->SetMaxPacketSize(packet_receiver_->GetMaxPacketSize());
+ rx_characteristic_->NotifyValueChanged(
+ bluetooth_device_, packet_generator_->CreateConnectionResponse(), false);
+ SetStatus(Status::CONNECTED);
+}
+
+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
« no previous file with comments | « components/proximity_auth/ble/bluetooth_low_energy_weave_server_connection.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698