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_agent.h" | 5 #include "tools/battor_agent/battor_agent.h" |
6 | 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #include "device/serial/serial.mojom.h" |
| 10 #include "device/serial/serial_io_handler.h" |
| 11 |
| 12 namespace { |
| 13 |
| 14 // Serial configuration parameters for the BattOr. |
| 15 const uint32 kBattOrBitrate = 2000000; |
| 16 const device::serial::DataBits kBattOrDataBits = |
| 17 device::serial::DATA_BITS_EIGHT; |
| 18 const device::serial::ParityBit kBattOrParityBit = |
| 19 device::serial::PARITY_BIT_NONE; |
| 20 const device::serial::StopBits kBattOrStopBit = device::serial::STOP_BITS_ONE; |
| 21 const bool kBattOrCtsFlowControl = true; |
| 22 const bool kBattOrHasCtsFlowControl = true; |
| 23 |
| 24 } // namespace |
| 25 |
7 namespace battor { | 26 namespace battor { |
8 | 27 |
9 BattOrAgent::BattOrAgent( | 28 BattOrAgent::BattOrAgent( |
10 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner, | 29 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner, |
11 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner, | 30 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner, |
12 const std::string& path, | 31 const std::string& path, |
13 Listener* listener) | 32 Listener* listener) |
14 : listener_(listener), | 33 : file_thread_task_runner_(file_thread_task_runner), |
15 connection_(new BattOrConnection(path, | 34 ui_thread_task_runner_(ui_thread_task_runner), |
16 this, | 35 path_(path), |
17 file_thread_task_runner, | 36 listener_(listener) { |
18 ui_thread_task_runner)) { | |
19 DCHECK(thread_checker_.CalledOnValidThread()); | 37 DCHECK(thread_checker_.CalledOnValidThread()); |
20 } | 38 } |
21 | 39 |
22 BattOrAgent::~BattOrAgent() { | 40 BattOrAgent::~BattOrAgent() { |
23 DCHECK(thread_checker_.CalledOnValidThread()); | 41 DCHECK(thread_checker_.CalledOnValidThread()); |
24 } | 42 } |
25 | 43 |
26 void BattOrAgent::StartTracing() { | 44 void BattOrAgent::StartTracing() { |
27 DCHECK(thread_checker_.CalledOnValidThread()); | 45 DCHECK(thread_checker_.CalledOnValidThread()); |
28 | 46 |
29 ConnectIfNeeded(); | 47 ConnectIfNeeded( |
| 48 base::Bind(&BattOrAgent::DoStartTracing, AsWeakPtr()), |
| 49 base::Bind(&Listener::OnStartTracingComplete, base::Unretained(listener_), |
| 50 BATTOR_ERROR_CONNECTION_FAILED)); |
30 } | 51 } |
31 | 52 |
32 void BattOrAgent::DoStartTracing() { | 53 void BattOrAgent::DoStartTracing() { |
33 DCHECK(thread_checker_.CalledOnValidThread()); | 54 DCHECK(thread_checker_.CalledOnValidThread()); |
34 | 55 |
35 // TODO(charliea): Tell the BattOr to start tracing. | 56 // TODO(charliea): Tell the BattOr to start tracing. |
36 listener_->OnStartTracingComplete(BATTOR_ERROR_NONE); | 57 listener_->OnStartTracingComplete(BATTOR_ERROR_NONE); |
37 } | 58 } |
38 | 59 |
39 void BattOrAgent::OnConnectionOpened(bool success) { | 60 void BattOrAgent::ConnectIfNeeded(const base::Closure& success_callback, |
| 61 const base::Closure& failure_callback) { |
40 DCHECK(thread_checker_.CalledOnValidThread()); | 62 DCHECK(thread_checker_.CalledOnValidThread()); |
41 | 63 |
42 // TODO(charliea): Rewrite this in a way that allows for multiple tracing | 64 if (io_handler_) { |
43 // commands. | 65 success_callback.Run(); |
| 66 return; |
| 67 } |
| 68 |
| 69 io_handler_ = device::SerialIoHandler::Create(file_thread_task_runner_, |
| 70 ui_thread_task_runner_); |
| 71 |
| 72 device::serial::ConnectionOptions options; |
| 73 options.bitrate = kBattOrBitrate; |
| 74 options.data_bits = kBattOrDataBits; |
| 75 options.parity_bit = kBattOrParityBit; |
| 76 options.stop_bits = kBattOrStopBit; |
| 77 options.cts_flow_control = kBattOrCtsFlowControl; |
| 78 options.has_cts_flow_control = kBattOrHasCtsFlowControl; |
| 79 |
| 80 io_handler_->Open(path_, options, |
| 81 base::Bind(&BattOrAgent::OnConnectComplete, AsWeakPtr(), |
| 82 success_callback, failure_callback)); |
| 83 } |
| 84 |
| 85 void BattOrAgent::OnConnectComplete(const base::Closure& success_callback, |
| 86 const base::Closure& failure_callback, |
| 87 bool success) { |
| 88 DCHECK(thread_checker_.CalledOnValidThread()); |
| 89 |
44 if (success) { | 90 if (success) { |
45 DoStartTracing(); | 91 success_callback.Run(); |
46 } else { | 92 } else { |
47 connection_.reset(); | 93 io_handler_ = nullptr; |
48 listener_->OnStartTracingComplete(BATTOR_ERROR_CONNECTION_FAILED); | 94 failure_callback.Run(); |
49 } | 95 } |
50 } | 96 } |
51 | 97 |
52 void BattOrAgent::OnBytesSent(bool success) {} | |
53 | |
54 void BattOrAgent::OnBytesRead(bool success, | |
55 BattOrMessageType type, | |
56 scoped_ptr<std::vector<char>> bytes) {} | |
57 | |
58 void BattOrAgent::ConnectIfNeeded() { | |
59 DCHECK(thread_checker_.CalledOnValidThread()); | |
60 | |
61 if (connection_->IsOpen()) { | |
62 // TODO(charliea): Rewrite this in a way that allows for multiple tracing | |
63 // commands. | |
64 DoStartTracing(); | |
65 return; | |
66 } | |
67 | |
68 connection_->Open(); | |
69 } | |
70 | |
71 } // namespace battor | 98 } // namespace battor |
OLD | NEW |