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 // This file provides a thin binary wrapper around the BattOr Agent | 5 // This file provides a thin binary wrapper around the BattOr Agent |
6 // library. This binary wrapper provides a means for non-C++ tracing | 6 // library. This binary wrapper provides a means for non-C++ tracing |
7 // controllers, such as Telemetry and Android Systrace, to issue high-level | 7 // controllers, such as Telemetry and Android Systrace, to issue high-level |
8 // tracing commands to the BattOr.. | 8 // tracing commands to the BattOr.. |
9 | 9 |
10 #include <stdint.h> | 10 #include <stdint.h> |
11 | 11 |
12 #include <iostream> | 12 #include <iostream> |
13 | 13 |
14 #include "base/at_exit.h" | 14 #include "base/at_exit.h" |
15 #include "base/bind.h" | 15 #include "base/bind.h" |
16 #include "base/bind_helpers.h" | 16 #include "base/bind_helpers.h" |
17 #include "base/location.h" | 17 #include "base/location.h" |
18 #include "base/logging.h" | 18 #include "base/logging.h" |
19 #include "base/threading/thread.h" | 19 #include "base/threading/thread.h" |
20 #include "tools/battor_agent/battor_agent.h" | 20 #include "tools/battor_agent/battor_agent.h" |
21 #include "tools/battor_agent/battor_error.h" | 21 #include "tools/battor_agent/battor_error.h" |
22 #include "tools/battor_agent/battor_finder.h" | |
23 | 22 |
24 using std::cout; | 23 using std::cout; |
25 using std::endl; | 24 using std::endl; |
26 | 25 |
27 namespace { | 26 namespace { |
28 | 27 |
29 const char kIoThreadName[] = "BattOr IO Thread"; | 28 const char kIoThreadName[] = "BattOr IO Thread"; |
30 const char kFileThreadName[] = "BattOr File Thread"; | 29 const char kFileThreadName[] = "BattOr File Thread"; |
31 const char kUiThreadName[] = "BattOr UI Thread"; | 30 const char kUiThreadName[] = "BattOr UI Thread"; |
32 const int32_t kBattOrCommandTimeoutSeconds = 10; | 31 const int32_t kBattOrCommandTimeoutSeconds = 10; |
33 | 32 |
34 void PrintUsage() { | 33 void PrintUsage() { |
35 cout << "Usage: battor_agent <command> <arguments>" << endl | 34 cout << "Usage: battor_agent <command> <arguments>" << endl |
36 << endl | 35 << endl |
37 << "Commands:" << endl | 36 << "Commands:" << endl |
38 << endl | 37 << endl |
39 << " StartTracing <path>" << endl | 38 << " StartTracing <path>" << endl |
40 << " StopTracing <path>" << endl | 39 << " StopTracing <path>" << endl |
41 << " SupportsExplicitClockSync" << endl | 40 << " SupportsExplicitClockSync" << endl |
42 << " RecordClockSyncMarker <path> <marker>" << endl | 41 << " RecordClockSyncMarker <path> <marker>" << endl |
43 << " IssueClockSyncMarker <path>" << endl | 42 << " IssueClockSyncMarker <path>" << endl |
44 << " Help" << endl; | 43 << " Help" << endl; |
45 } | 44 } |
46 | 45 |
47 void PrintSupportsExplicitClockSync() { | 46 void PrintSupportsExplicitClockSync() { |
48 cout << battor::BattOrAgent::SupportsExplicitClockSync() << endl; | 47 cout << battor::BattOrAgent::SupportsExplicitClockSync() << endl; |
49 } | 48 } |
50 | 49 |
51 // Retrieves argument argnum from the argument list, or an empty string if the | 50 // Retrieves argument argnum from the argument list, printing the usage |
52 // argument doesn't exist. | 51 // guidelines and exiting with an error code if the argument doesn't exist. |
53 std::string GetArg(int argnum, int argc, char* argv[]) { | 52 std::string GetArg(int argnum, int argc, char* argv[]) { |
54 if (argnum >= argc) { | 53 if (argnum >= argc) { |
55 return std::string(); | 54 PrintUsage(); |
| 55 exit(1); |
56 } | 56 } |
57 | 57 |
58 return argv[argnum]; | 58 return argv[argnum]; |
59 } | 59 } |
60 | 60 |
61 // Checks if an error occurred and, if it did, prints the error and exits | 61 // Checks if an error occurred and, if it did, prints the error and exits |
62 // with an error code. | 62 // with an error code. |
63 void CheckError(battor::BattOrError error) { | 63 void CheckError(battor::BattOrError error) { |
64 if (error != battor::BATTOR_ERROR_NONE) | 64 if (error != battor::BATTOR_ERROR_NONE) |
65 LOG(FATAL) << "Fatal error when communicating with the BattOr: " << error; | 65 LOG(FATAL) << "Fatal error when communicating with the BattOr: " << error; |
(...skipping 16 matching lines...) Expand all Loading... |
82 : done_(false, false), | 82 : done_(false, false), |
83 io_thread_(kIoThreadName), | 83 io_thread_(kIoThreadName), |
84 file_thread_(kFileThreadName), | 84 file_thread_(kFileThreadName), |
85 ui_thread_(kUiThreadName) {} | 85 ui_thread_(kUiThreadName) {} |
86 | 86 |
87 ~BattOrAgentBin() { DCHECK(!agent_); } | 87 ~BattOrAgentBin() { DCHECK(!agent_); } |
88 | 88 |
89 // Runs the BattOr binary and returns the exit code. | 89 // Runs the BattOr binary and returns the exit code. |
90 int Run(int argc, char* argv[]) { | 90 int Run(int argc, char* argv[]) { |
91 std::string cmd = GetArg(1, argc, argv); | 91 std::string cmd = GetArg(1, argc, argv); |
92 if (cmd.empty()) { | |
93 PrintUsage(); | |
94 exit(1); | |
95 } | |
96 | 92 |
97 // SupportsExplicitClockSync doesn't need to use the serial connection, so | 93 // SupportsExplicitClockSync doesn't need to use the serial connection, so |
98 // handle it separately. | 94 // handle it separately. |
99 if (cmd == "SupportsExplicitClockSync") { | 95 if (cmd == "SupportsExplicitClockSync") { |
100 PrintSupportsExplicitClockSync(); | 96 PrintSupportsExplicitClockSync(); |
101 return 0; | 97 return 0; |
102 } | 98 } |
103 | 99 |
104 std::string path = GetArg(2, argc, argv); | 100 std::string path = GetArg(2, argc, argv); |
105 // If no path is specified, see if we can find a BattOr and use that. | |
106 if (path.empty()) | |
107 path = BattOrFinder::FindBattOr(); | |
108 | |
109 // If we don't have any BattOr to use, exit. | |
110 if (path.empty()) { | |
111 cout << "Unable to find a BattOr, and no explicit BattOr path was " | |
112 "specified." | |
113 << endl; | |
114 exit(1); | |
115 } | |
116 | |
117 SetUp(path); | 101 SetUp(path); |
118 | 102 |
119 if (cmd == "StartTracing") { | 103 if (cmd == "StartTracing") { |
120 StartTracing(); | 104 StartTracing(); |
121 } else if (cmd == "StopTracing") { | 105 } else if (cmd == "StopTracing") { |
122 StopTracing(); | 106 StopTracing(); |
123 } else if (cmd == "RecordClockSyncMarker") { | 107 } else if (cmd == "RecordClockSyncMarker") { |
124 // TODO(charliea): Write RecordClockSyncMarker. | 108 // TODO(charliea): Write RecordClockSyncMarker. |
125 } else if (cmd == "IssueClockSyncMarker") { | 109 } else if (cmd == "IssueClockSyncMarker") { |
126 // TODO(charliea): Write IssueClockSyncMarker. | 110 // TODO(charliea): Write IssueClockSyncMarker. |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 scoped_ptr<BattOrAgent> agent_; | 226 scoped_ptr<BattOrAgent> agent_; |
243 }; | 227 }; |
244 | 228 |
245 } // namespace battor | 229 } // namespace battor |
246 | 230 |
247 int main(int argc, char* argv[]) { | 231 int main(int argc, char* argv[]) { |
248 base::AtExitManager exit_manager; | 232 base::AtExitManager exit_manager; |
249 battor::BattOrAgentBin bin; | 233 battor::BattOrAgentBin bin; |
250 return bin.Run(argc, argv); | 234 return bin.Run(argc, argv); |
251 } | 235 } |
OLD | NEW |