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 | |
26 namespace battor { | 7 namespace battor { |
27 | 8 |
28 BattOrAgent::BattOrAgent( | 9 BattOrAgent::BattOrAgent( |
29 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner, | 10 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner, |
30 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner, | 11 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner, |
31 const std::string& path, | 12 const std::string& path, |
32 Listener* listener) | 13 Listener* listener) |
33 : file_thread_task_runner_(file_thread_task_runner), | 14 : listener_(listener), |
34 ui_thread_task_runner_(ui_thread_task_runner), | 15 connection_(new BattOrConnection(path, |
35 path_(path), | 16 this, |
36 listener_(listener) { | 17 file_thread_task_runner, |
| 18 ui_thread_task_runner)) { |
37 DCHECK(thread_checker_.CalledOnValidThread()); | 19 DCHECK(thread_checker_.CalledOnValidThread()); |
38 } | 20 } |
39 | 21 |
40 BattOrAgent::~BattOrAgent() { | 22 BattOrAgent::~BattOrAgent() { |
41 DCHECK(thread_checker_.CalledOnValidThread()); | 23 DCHECK(thread_checker_.CalledOnValidThread()); |
42 } | 24 } |
43 | 25 |
44 void BattOrAgent::StartTracing() { | 26 void BattOrAgent::StartTracing() { |
45 DCHECK(thread_checker_.CalledOnValidThread()); | 27 DCHECK(thread_checker_.CalledOnValidThread()); |
46 | 28 |
47 ConnectIfNeeded( | 29 ConnectIfNeeded(); |
48 base::Bind(&BattOrAgent::DoStartTracing, AsWeakPtr()), | |
49 base::Bind(&Listener::OnStartTracingComplete, base::Unretained(listener_), | |
50 BATTOR_ERROR_CONNECTION_FAILED)); | |
51 } | 30 } |
52 | 31 |
53 void BattOrAgent::DoStartTracing() { | 32 void BattOrAgent::DoStartTracing() { |
54 DCHECK(thread_checker_.CalledOnValidThread()); | 33 DCHECK(thread_checker_.CalledOnValidThread()); |
55 | 34 |
56 // TODO(charliea): Tell the BattOr to start tracing. | 35 // TODO(charliea): Tell the BattOr to start tracing. |
57 listener_->OnStartTracingComplete(BATTOR_ERROR_NONE); | 36 listener_->OnStartTracingComplete(BATTOR_ERROR_NONE); |
58 } | 37 } |
59 | 38 |
60 void BattOrAgent::ConnectIfNeeded(const base::Closure& success_callback, | 39 void BattOrAgent::OnConnectionOpened(bool success) { |
61 const base::Closure& failure_callback) { | |
62 DCHECK(thread_checker_.CalledOnValidThread()); | 40 DCHECK(thread_checker_.CalledOnValidThread()); |
63 | 41 |
64 if (io_handler_) { | 42 // TODO(charliea): Rewrite this in a way that allows for multiple tracing |
65 success_callback.Run(); | 43 // commands. |
| 44 if (success) { |
| 45 DoStartTracing(); |
| 46 } else { |
| 47 connection_.reset(); |
| 48 listener_->OnStartTracingComplete(BATTOR_ERROR_CONNECTION_FAILED); |
| 49 } |
| 50 } |
| 51 |
| 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(); |
66 return; | 65 return; |
67 } | 66 } |
68 | 67 |
69 io_handler_ = device::SerialIoHandler::Create(file_thread_task_runner_, | 68 connection_->Open(); |
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 | |
90 if (success) { | |
91 success_callback.Run(); | |
92 } else { | |
93 io_handler_ = nullptr; | |
94 failure_callback.Run(); | |
95 } | |
96 } | 69 } |
97 | 70 |
98 } // namespace battor | 71 } // namespace battor |
OLD | NEW |