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