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

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: change GetEid to take RemoteDevice 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 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
101 bluetooth_device_, packet_generator_->CreateConnectionClose(
102 packet_receiver_->GetReasonToClose()),
103 false);
104 break;
105 case ReceiverState::WAITING:
106 // Receiver state should have changed from CONNECTING to WAITING if
107 // a proper connection request had been received.
108 // The max packet size from the connection request will be
109 // used to generate future packets.
110 packet_generator_->SetMaxPacketSize(packet_receiver_->GetMaxPacketSize());
111 rx_characteristic_->NotifyValueChanged(
112 bluetooth_device_, packet_generator_->CreateConnectionResponse(),
113 false);
114 SetStatus(Status::CONNECTED);
115 break;
116 case ReceiverState::RECEIVING_DATA:
117 // Normal in between states, so do nothing.
118 break;
119 default:
120 NOTREACHED();
121 }
122 }
123
124 void BluetoothLowEnergyWeaveServerConnection::SendMessageImpl(
125 std::unique_ptr<WireMessage> message) {
126 DCHECK(status() == Status::CONNECTED);
127
128 std::string serialized_msg = message->Serialize();
129
130 std::vector<Packet> packets =
131 packet_generator_->EncodeDataMessage(serialized_msg);
132
133 for (auto packet : packets) {
134 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.
135 }
136 }
137
138 std::string BluetoothLowEnergyWeaveServerConnection::GetReasonForClose() {
139 switch (packet_receiver_->GetReasonForClose()) {
140 case ReasonForClose::CLOSE_WITHOUT_ERROR:
141 return "CLOSE_WITHOUT_ERROR";
142 case ReasonForClose::UNKNOWN_ERROR:
143 return "UNKNOWN_ERROR";
144 case ReasonForClose::NO_COMMON_VERSION_SUPPORTED:
145 return "NO_COMMON_VERSION_SUPPORTED";
146 case ReasonForClose::RECEIVED_PACKET_OUT_OF_SEQUENCE:
147 return "RECEIVED_PACKET_OUT_OF_SEQUENCE";
148 case ReasonForClose::APPLICATION_ERROR:
149 return "APPLICATION_ERROR";
150 default:
151 NOTREACHED();
152 return "";
153 }
154 }
155
156 } // namespace weave
157
158 } // namespace proximity_auth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698