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 #ifndef TOOLS_BATTOR_AGENT_BATTOR_AGENT_H_ | 5 #ifndef TOOLS_BATTOR_AGENT_BATTOR_AGENT_H_ |
6 #define TOOLS_BATTOR_AGENT_BATTOR_AGENT_H_ | 6 #define TOOLS_BATTOR_AGENT_BATTOR_AGENT_H_ |
7 | 7 |
| 8 #include "base/callback_forward.h" |
8 #include "base/macros.h" | 9 #include "base/macros.h" |
9 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/weak_ptr.h" |
10 #include "base/threading/thread_checker.h" | 12 #include "base/threading/thread_checker.h" |
11 #include "tools/battor_agent/battor_connection.h" | |
12 #include "tools/battor_agent/battor_error.h" | 13 #include "tools/battor_agent/battor_error.h" |
13 | 14 |
| 15 namespace device { |
| 16 class SerialIoHandler; |
| 17 } |
| 18 |
14 namespace battor { | 19 namespace battor { |
15 | 20 |
16 // A BattOrAgent is a class used to asynchronously communicate with a BattOr for | 21 // A BattOrAgent is a class used to asynchronously communicate with a BattOr for |
17 // the purpose of collecting power samples. A BattOr is an external USB device | 22 // the purpose of collecting power samples. A BattOr is an external USB device |
18 // that's capable of recording accurate, high-frequency (2000Hz) power samples. | 23 // that's capable of recording accurate, high-frequency (2000Hz) power samples. |
19 // | 24 // |
20 // The serial connection is automatically opened when the first command | 25 // The serial connection is automatically opened when the first command |
21 // (e.g. StartTracing(), StopTracing(), etc.) is issued, and automatically | 26 // (e.g. StartTracing(), StopTracing(), etc.) is issued, and automatically |
22 // closed when either StopTracing() or the destructor is called. For Telemetry, | 27 // closed when either StopTracing() or the destructor is called. For Telemetry, |
23 // this means that the connection must be reinitialized for every command that's | 28 // this means that the connection must be reinitialized for every command that's |
24 // issued because a new BattOrAgent is constructed. For Chromium, we use the | 29 // issued because a new BattOrAgent is constructed. For Chromium, we use the |
25 // same BattOrAgent for multiple commands and thus avoid having to reinitialize | 30 // same BattOrAgent for multiple commands and thus avoid having to reinitialize |
26 // the serial connection. | 31 // the serial connection. |
27 // | 32 // |
28 // This class is NOT thread safe, and must be interacted with only from the IO | 33 // This class is NOT thread safe, and must be interacted with only from the IO |
29 // thread. The IO thread must also have a running MessageLoop. | 34 // thread. The IO thread must also have a running MessageLoop. |
30 class BattOrAgent : public BattOrConnection::Listener { | 35 class BattOrAgent : public base::SupportsWeakPtr<BattOrAgent> { |
31 public: | 36 public: |
32 // The listener interface that must be implemented in order to interact with | 37 // The listener interface that must be implemented in order to interact with |
33 // the BattOrAgent. | 38 // the BattOrAgent. |
34 class Listener { | 39 class Listener { |
35 public: | 40 public: |
36 virtual void OnStartTracingComplete(BattOrError error) = 0; | 41 virtual void OnStartTracingComplete(BattOrError error) = 0; |
37 }; | 42 }; |
38 | 43 |
39 BattOrAgent( | 44 BattOrAgent( |
40 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner, | 45 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner, |
41 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner, | 46 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_rnuner, |
42 const std::string& path, | 47 const std::string& path, |
43 Listener* listener); | 48 Listener* listener); |
44 virtual ~BattOrAgent(); | 49 virtual ~BattOrAgent(); |
45 | 50 |
46 // Tells the BattOr to start tracing. | 51 // Tells the BattOr to start tracing. |
47 void StartTracing(); | 52 void StartTracing(); |
48 | 53 |
49 // Returns whether the BattOr is able to record clock sync markers in its own | 54 // Returns whether the BattOr is able to record clock sync markers in its own |
50 // trace log. | 55 // trace log. |
51 static bool SupportsExplicitClockSync() { return true; } | 56 static bool SupportsExplicitClockSync() { return true; } |
52 | 57 |
53 // BattOrConnection::Listener implementation. | |
54 void OnConnectionOpened(bool success) override; | |
55 void OnBytesSent(bool success) override; | |
56 void OnBytesRead(bool success, | |
57 BattOrMessageType type, | |
58 scoped_ptr<std::vector<char>> bytes) override; | |
59 | |
60 private: | 58 private: |
61 // Initializes the serial connection (if not done already). | 59 // Initializes the serial connection (if not done already) and calls one of |
62 // OnConnectionOpened() will be called once the connection is made. | 60 // the two callbacks depending its success. The callback will be invoked on |
63 void ConnectIfNeeded(); | 61 // the same thread that this method is called on. |
| 62 void ConnectIfNeeded(const base::Closure& success_callback, |
| 63 const base::Closure& failure_callback); |
| 64 void OnConnectComplete(const base::Closure& success_callback, |
| 65 const base::Closure& failure_callback, |
| 66 bool success); |
64 | 67 |
65 // StartTracing continuation called once the connection is initialized. | 68 // StartTracing continuation called once the connection is initialized. |
66 void DoStartTracing(); | 69 void DoStartTracing(); |
67 | 70 |
| 71 // Resets the connection to its unopened state. |
| 72 void ResetConnection(); |
| 73 |
| 74 // Threads needed for serial communication. |
| 75 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner_; |
| 76 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner_; |
| 77 |
| 78 // The path of the BattOr (e.g. "/dev/tty.battor_serial"). |
| 79 std::string path_; |
| 80 |
68 // The listener that handles the commands' results. It must outlive the agent. | 81 // The listener that handles the commands' results. It must outlive the agent. |
69 Listener* listener_; | 82 Listener* listener_; |
70 | 83 |
71 // The serial connection to the BattOr. | 84 // IO handler capable of reading and writing from the serial connection. |
72 scoped_ptr<BattOrConnection> connection_; | 85 scoped_refptr<device::SerialIoHandler> io_handler_; |
73 | 86 |
74 // Checker to make sure that this is only ever called on the IO thread. | 87 // Checker to make sure that this is only ever called on the IO thread. |
75 base::ThreadChecker thread_checker_; | 88 base::ThreadChecker thread_checker_; |
76 | 89 |
77 DISALLOW_COPY_AND_ASSIGN(BattOrAgent); | 90 DISALLOW_COPY_AND_ASSIGN(BattOrAgent); |
78 }; | 91 }; |
79 | 92 |
80 } // namespace battor | 93 } // namespace battor |
81 | 94 |
82 #endif // TOOLS_BATTOR_AGENT_BATTOR_AGENT_H_ | 95 #endif // TOOLS_BATTOR_AGENT_BATTOR_AGENT_H_ |
OLD | NEW |