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