OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "tools/battor_agent/battor_connection_impl.h" | 5 #include "tools/battor_agent/battor_connection_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "device/serial/buffer.h" | 9 #include "device/serial/buffer.h" |
10 #include "device/serial/serial_io_handler.h" | 10 #include "device/serial/serial_io_handler.h" |
11 #include "net/base/io_buffer.h" | 11 #include "net/base/io_buffer.h" |
12 | 12 |
13 using std::vector; | 13 using std::vector; |
14 | 14 |
15 namespace battor { | 15 namespace battor { |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 // Serial configuration parameters for the BattOr. | 19 // Serial configuration parameters for the BattOr. |
20 const uint32_t kBattOrBitrate = 2000000; | 20 const uint32_t kBattOrBitrate = 2000000; |
21 const device::serial::DataBits kBattOrDataBits = | 21 const device::serial::DataBits kBattOrDataBits = |
22 device::serial::DATA_BITS_EIGHT; | 22 device::serial::DataBits::EIGHT; |
23 const device::serial::ParityBit kBattOrParityBit = | 23 const device::serial::ParityBit kBattOrParityBit = |
24 device::serial::PARITY_BIT_NONE; | 24 device::serial::ParityBit::NONE; |
25 const device::serial::StopBits kBattOrStopBit = device::serial::STOP_BITS_ONE; | 25 const device::serial::StopBits kBattOrStopBit = device::serial::StopBits::ONE; |
26 const bool kBattOrCtsFlowControl = true; | 26 const bool kBattOrCtsFlowControl = true; |
27 const bool kBattOrHasCtsFlowControl = true; | 27 const bool kBattOrHasCtsFlowControl = true; |
28 // The maximum BattOr message is 50kB long. | 28 // The maximum BattOr message is 50kB long. |
29 const size_t kMaxMessageSizeBytes = 50000; | 29 const size_t kMaxMessageSizeBytes = 50000; |
30 | 30 |
31 // Returns the maximum number of bytes that could be required to read a message | 31 // Returns the maximum number of bytes that could be required to read a message |
32 // of the specified type. | 32 // of the specified type. |
33 size_t GetMaxBytesForMessageType(BattOrMessageType type) { | 33 size_t GetMaxBytesForMessageType(BattOrMessageType type) { |
34 switch (type) { | 34 switch (type) { |
35 case BATTOR_MESSAGE_TYPE_CONTROL: | 35 case BATTOR_MESSAGE_TYPE_CONTROL: |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 | 154 |
155 auto on_receive_buffer_filled = | 155 auto on_receive_buffer_filled = |
156 base::Bind(&BattOrConnectionImpl::OnBytesRead, AsWeakPtr()); | 156 base::Bind(&BattOrConnectionImpl::OnBytesRead, AsWeakPtr()); |
157 | 157 |
158 io_handler_->Read(make_scoped_ptr(new device::ReceiveBuffer( | 158 io_handler_->Read(make_scoped_ptr(new device::ReceiveBuffer( |
159 pending_read_buffer_, max_bytes_to_read, on_receive_buffer_filled))); | 159 pending_read_buffer_, max_bytes_to_read, on_receive_buffer_filled))); |
160 } | 160 } |
161 | 161 |
162 void BattOrConnectionImpl::OnBytesRead(int bytes_read, | 162 void BattOrConnectionImpl::OnBytesRead(int bytes_read, |
163 device::serial::ReceiveError error) { | 163 device::serial::ReceiveError error) { |
164 if (bytes_read == 0 || error != device::serial::RECEIVE_ERROR_NONE) { | 164 if (bytes_read == 0 || error != device::serial::ReceiveError::NONE) { |
165 // If we didn't have a message before, and we weren't able to read any | 165 // If we didn't have a message before, and we weren't able to read any |
166 // additional bytes, then there's no valid message available. | 166 // additional bytes, then there's no valid message available. |
167 EndReadBytes(false, BATTOR_MESSAGE_TYPE_CONTROL, nullptr); | 167 EndReadBytes(false, BATTOR_MESSAGE_TYPE_CONTROL, nullptr); |
168 return; | 168 return; |
169 } | 169 } |
170 | 170 |
171 already_read_buffer_.insert(already_read_buffer_.end(), | 171 already_read_buffer_.insert(already_read_buffer_.end(), |
172 pending_read_buffer_->data(), | 172 pending_read_buffer_->data(), |
173 pending_read_buffer_->data() + bytes_read); | 173 pending_read_buffer_->data() + bytes_read); |
174 | 174 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 } | 248 } |
249 } | 249 } |
250 | 250 |
251 // If we made it to the end of the read buffer and no end byte was seen, then | 251 // If we made it to the end of the read buffer and no end byte was seen, then |
252 // we don't have a complete message. | 252 // we don't have a complete message. |
253 return false; | 253 return false; |
254 } | 254 } |
255 | 255 |
256 void BattOrConnectionImpl::OnBytesSent(int bytes_sent, | 256 void BattOrConnectionImpl::OnBytesSent(int bytes_sent, |
257 device::serial::SendError error) { | 257 device::serial::SendError error) { |
258 bool success = (error == device::serial::SEND_ERROR_NONE) && | 258 bool success = (error == device::serial::SendError::NONE) && |
259 (pending_write_length_ == static_cast<size_t>(bytes_sent)); | 259 (pending_write_length_ == static_cast<size_t>(bytes_sent)); |
260 listener_->OnBytesSent(success); | 260 listener_->OnBytesSent(success); |
261 } | 261 } |
262 | 262 |
263 } // namespace battor | 263 } // namespace battor |
OLD | NEW |