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

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

Powered by Google App Engine
This is Rietveld 408576698