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

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: Created 4 years, 5 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
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..71ecd10aa261ca2962d96f85dc6b1c85fda4909b
--- /dev/null
+++ b/components/proximity_auth/ble/bluetooth_low_energy_weave_server_connection.cc
@@ -0,0 +1,106 @@
+// 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 {
+
+// 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::CLIENT)) {}
+
+BluetoothLowEnergyWeaveServerConnection::
+ ~BluetoothLowEnergyWeaveServerConnection() {}
+
+void BluetoothLowEnergyWeaveServerConnection::Connect() {
+ if (status() == Status::DISCONNECTED) {
+ server_->StopAdvertising(remote_device_);
+ SetStatus(Status::CONNECTED);
+ }
+}
+
+void BluetoothLowEnergyWeaveServerConnection::Disconnect() {
+ if (status() != Status::DISCONNECTED) {
+ server_->StartAdvertising(remote_device_);
+ SetStatus(Status::DISCONNECTED);
+ }
+}
+
+std::string BluetoothLowEnergyWeaveServerConnection::GetDeviceAddress() {
+ return bluetooth_device_->GetAddress();
+}
+
+bool BluetoothLowEnergyWeaveServerConnection::ReceivePacket(Packet packet) {
+ packet_receiver_->ReceivePacket(packet);
+ // TODO(jingxuy): error checking on this and possibly send connection close as
+ // the result.
+ return true;
+}
+
+void BluetoothLowEnergyWeaveServerConnection::SendMessageImpl(
+ std::unique_ptr<WireMessage> message) {
+ if (status() != Status::CONNECTED)
+ return;
+
+ 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);
+ }
+}
+
+} // namespace weave
+
+} // namespace proximity_auth

Powered by Google App Engine
This is Rietveld 408576698