Chromium Code Reviews| Index: tools/battor_agent/battor_connection_impl.cc |
| diff --git a/tools/battor_agent/battor_connection_impl.cc b/tools/battor_agent/battor_connection_impl.cc |
| index cce643a6e18e4a75a115896bc3257b84c1176ce8..6433eed0e4d6ec5078fb3ec46e7def75a4f240b6 100644 |
| --- a/tools/battor_agent/battor_connection_impl.cc |
| +++ b/tools/battor_agent/battor_connection_impl.cc |
| @@ -6,18 +6,25 @@ |
| #include "base/bind.h" |
| #include "base/bind_helpers.h" |
| +#include "base/command_line.h" |
| #include "base/memory/ptr_util.h" |
| #include "base/thread_task_runner_handle.h" |
| #include "device/serial/buffer.h" |
| #include "device/serial/serial_io_handler.h" |
| #include "net/base/io_buffer.h" |
| +#include "tools/battor_agent/serial_utils.h" |
| +using std::endl; |
| using std::vector; |
| namespace battor { |
| namespace { |
| +// The command line switch used to specify a file to which serial communication |
| +// is logged. |
| +const char kSerialLogPathSwitch[] = "battor-serial-log"; |
| + |
| // Serial configuration parameters for the BattOr. |
| const uint32_t kBattOrBitrate = 2000000; |
| const device::serial::DataBits kBattOrDataBits = |
| @@ -58,7 +65,16 @@ BattOrConnectionImpl::BattOrConnectionImpl( |
| : BattOrConnection(listener), |
| path_(path), |
| file_thread_task_runner_(file_thread_task_runner), |
| - ui_thread_task_runner_(ui_thread_task_runner) {} |
| + ui_thread_task_runner_(ui_thread_task_runner) { |
| + std::string serial_log_path = |
| + base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| + kSerialLogPathSwitch); |
| + if (!serial_log_path.empty()) { |
| + serial_log_.open(serial_log_path.c_str(), |
| + std::fstream::out | std::fstream::trunc); |
| + serial_log_ << std::boolalpha; |
| + } |
| +} |
| BattOrConnectionImpl::~BattOrConnectionImpl() {} |
| @@ -78,11 +94,16 @@ void BattOrConnectionImpl::Open() { |
| options.cts_flow_control = kBattOrCtsFlowControl; |
| options.has_cts_flow_control = kBattOrHasCtsFlowControl; |
| + serial_log_ << "Opening serial connection." << endl << endl; |
| io_handler_->Open(path_, options, |
| base::Bind(&BattOrConnectionImpl::OnOpened, AsWeakPtr())); |
| } |
| void BattOrConnectionImpl::OnOpened(bool success) { |
| + serial_log_ << "Serial connection open finished with success: " << success |
| + << "." << endl |
| + << endl; |
| + |
| if (!success) |
| Close(); |
| @@ -92,6 +113,7 @@ void BattOrConnectionImpl::OnOpened(bool success) { |
| } |
| void BattOrConnectionImpl::Close() { |
| + serial_log_ << "Serial connection closed." << endl << endl; |
| io_handler_ = nullptr; |
| } |
| @@ -119,6 +141,9 @@ void BattOrConnectionImpl::SendBytes(BattOrMessageType type, |
| data.push_back(BATTOR_CONTROL_BYTE_END); |
| + serial_log_ << "Bytes sent: " << CharVectorToString(data) << "." << endl |
| + << endl; |
| + |
| pending_write_length_ = data.size(); |
| io_handler_->Write(base::WrapUnique(new device::SendBuffer( |
| data, base::Bind(&BattOrConnectionImpl::OnBytesSent, AsWeakPtr())))); |
| @@ -134,7 +159,13 @@ void BattOrConnectionImpl::ReadMessage(BattOrMessageType type) { |
| std::unique_ptr<vector<char>> bytes(new vector<char>()); |
| bytes->reserve(max_bytes_to_read); |
| + serial_log_ |
| + << "Checking if a complete message is in the 'already read' buffer." |
| + << endl |
| + << endl; |
| + |
| if (ParseMessage(&parsed_type, bytes.get())) { |
| + serial_log_ << "Complete message found." << endl << endl; |
| base::ThreadTaskRunnerHandle::Get()->PostTask( |
| FROM_HERE, |
| base::Bind(&Listener::OnMessageRead, base::Unretained(listener_), true, |
| @@ -142,6 +173,7 @@ void BattOrConnectionImpl::ReadMessage(BattOrMessageType type) { |
| return; |
| } |
| + serial_log_ << "No complete message found." << endl << endl; |
| BeginReadBytes(max_bytes_to_read - already_read_buffer_.size()); |
| } |
| @@ -156,6 +188,10 @@ scoped_refptr<device::SerialIoHandler> BattOrConnectionImpl::CreateIoHandler() { |
| } |
| void BattOrConnectionImpl::BeginReadBytes(size_t max_bytes_to_read) { |
| + serial_log_ << "Starting read of up to " << max_bytes_to_read << " bytes." |
| + << endl |
| + << endl; |
| + |
| pending_read_buffer_ = |
| make_scoped_refptr(new net::IOBuffer(max_bytes_to_read)); |
| @@ -169,13 +205,27 @@ void BattOrConnectionImpl::BeginReadBytes(size_t max_bytes_to_read) { |
| void BattOrConnectionImpl::OnBytesRead(int bytes_read, |
| device::serial::ReceiveError error) { |
| - if (bytes_read == 0 || error != device::serial::ReceiveError::NONE) { |
| + if (error != device::serial::ReceiveError::NONE) { |
| + serial_log_ << "Read failed with error: " << error << "." << endl << endl; |
| + EndReadBytes(false, BATTOR_MESSAGE_TYPE_CONTROL, nullptr); |
| + return; |
| + } |
| + |
| + if (bytes_read == 0) { |
| // If we didn't have a message before, and we weren't able to read any |
| // additional bytes, then there's no valid message available. |
| + serial_log_ << "Read failed due to no bytes being read." << endl << endl; |
| EndReadBytes(false, BATTOR_MESSAGE_TYPE_CONTROL, nullptr); |
| return; |
| } |
| + serial_log_ << bytes_read << " more bytes read: " |
| + << CharVectorToString( |
| + vector<char>(pending_read_buffer_->data(), |
| + pending_read_buffer_->data() + bytes_read)) |
| + << "." << endl |
| + << endl; |
| + |
| already_read_buffer_.insert(already_read_buffer_.end(), |
| pending_read_buffer_->data(), |
| pending_read_buffer_->data() + bytes_read); |
| @@ -185,14 +235,18 @@ void BattOrConnectionImpl::OnBytesRead(int bytes_read, |
| bytes->reserve(GetMaxBytesForMessageType(pending_read_message_type_)); |
| if (!ParseMessage(&type, bytes.get())) { |
| - // Even after reading the max number of bytes, we still don't have a valid |
| - // message. |
| + serial_log_ << "Read failed due to having no complete message after max " |
| + "read length." |
| + << endl |
| + << endl; |
|
nednguyen
2016/05/12 15:44:29
Let create a Log(const string& message) method ins
|
| EndReadBytes(false, BATTOR_MESSAGE_TYPE_CONTROL, nullptr); |
| return; |
| } |
| if (type != pending_read_message_type_) { |
| - // We received a complete message, but it wasn't the type we were expecting. |
| + serial_log_ << "Read failed due to receiving a message of the wrong type." |
| + << endl |
| + << endl; |
| EndReadBytes(false, BATTOR_MESSAGE_TYPE_CONTROL, nullptr); |
| return; |
| } |
| @@ -200,10 +254,12 @@ void BattOrConnectionImpl::OnBytesRead(int bytes_read, |
| EndReadBytes(true, type, std::move(bytes)); |
| } |
| -void BattOrConnectionImpl::EndReadBytes( |
| - bool success, |
| - BattOrMessageType type, |
| - std::unique_ptr<std::vector<char>> bytes) { |
| +void BattOrConnectionImpl::EndReadBytes(bool success, |
| + BattOrMessageType type, |
| + std::unique_ptr<vector<char>> bytes) { |
| + serial_log_ << "Read finished with success: " << success << "." << endl |
| + << endl; |
| + |
| pending_read_buffer_ = nullptr; |
| base::ThreadTaskRunnerHandle::Get()->PostTask( |
| FROM_HERE, |