| 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" |
| 22 | 23 |
| 23 using std::cout; | 24 using std::cout; |
| 24 using std::endl; | 25 using std::endl; |
| 25 | 26 |
| 26 namespace { | 27 namespace { |
| 27 | 28 |
| 28 const char kIoThreadName[] = "BattOr IO Thread"; | 29 const char kIoThreadName[] = "BattOr IO Thread"; |
| 29 const char kFileThreadName[] = "BattOr File Thread"; | 30 const char kFileThreadName[] = "BattOr File Thread"; |
| 30 const char kUiThreadName[] = "BattOr UI Thread"; | 31 const char kUiThreadName[] = "BattOr UI Thread"; |
| 31 const int32_t kBattOrCommandTimeoutSeconds = 10; | 32 const int32_t kBattOrCommandTimeoutSeconds = 10; |
| 32 | 33 |
| 33 void PrintUsage() { | 34 void PrintUsage() { |
| 34 cout << "Usage: battor_agent <command> <arguments>" << endl | 35 cout << "Usage: battor_agent <command> <arguments>" << endl |
| 35 << endl | 36 << endl |
| 36 << "Commands:" << endl | 37 << "Commands:" << endl |
| 37 << endl | 38 << endl |
| 38 << " StartTracing <path>" << endl | 39 << " StartTracing <path>" << endl |
| 39 << " StopTracing <path>" << endl | 40 << " StopTracing <path>" << endl |
| 40 << " SupportsExplicitClockSync" << endl | 41 << " SupportsExplicitClockSync" << endl |
| 41 << " RecordClockSyncMarker <path> <marker>" << endl | 42 << " RecordClockSyncMarker <path> <marker>" << endl |
| 42 << " IssueClockSyncMarker <path>" << endl | 43 << " IssueClockSyncMarker <path>" << endl |
| 43 << " Help" << endl; | 44 << " Help" << endl; |
| 44 } | 45 } |
| 45 | 46 |
| 46 void PrintSupportsExplicitClockSync() { | 47 void PrintSupportsExplicitClockSync() { |
| 47 cout << battor::BattOrAgent::SupportsExplicitClockSync() << endl; | 48 cout << battor::BattOrAgent::SupportsExplicitClockSync() << endl; |
| 48 } | 49 } |
| 49 | 50 |
| 50 // Retrieves argument argnum from the argument list, printing the usage | 51 // Retrieves argument argnum from the argument list, or an empty string if the |
| 51 // guidelines and exiting with an error code if the argument doesn't exist. | 52 // argument doesn't exist. |
| 52 std::string GetArg(int argnum, int argc, char* argv[]) { | 53 std::string GetArg(int argnum, int argc, char* argv[]) { |
| 53 if (argnum >= argc) { | 54 if (argnum >= argc) { |
| 54 PrintUsage(); | 55 return std::string(); |
| 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 } |
| 92 | 96 |
| 93 // SupportsExplicitClockSync doesn't need to use the serial connection, so | 97 // SupportsExplicitClockSync doesn't need to use the serial connection, so |
| 94 // handle it separately. | 98 // handle it separately. |
| 95 if (cmd == "SupportsExplicitClockSync") { | 99 if (cmd == "SupportsExplicitClockSync") { |
| 96 PrintSupportsExplicitClockSync(); | 100 PrintSupportsExplicitClockSync(); |
| 97 return 0; | 101 return 0; |
| 98 } | 102 } |
| 99 | 103 |
| 100 std::string path = GetArg(2, argc, argv); | 104 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 |
| 101 SetUp(path); | 117 SetUp(path); |
| 102 | 118 |
| 103 if (cmd == "StartTracing") { | 119 if (cmd == "StartTracing") { |
| 104 StartTracing(); | 120 StartTracing(); |
| 105 } else if (cmd == "StopTracing") { | 121 } else if (cmd == "StopTracing") { |
| 106 StopTracing(); | 122 StopTracing(); |
| 107 } else if (cmd == "RecordClockSyncMarker") { | 123 } else if (cmd == "RecordClockSyncMarker") { |
| 108 // TODO(charliea): Write RecordClockSyncMarker. | 124 // TODO(charliea): Write RecordClockSyncMarker. |
| 109 } else if (cmd == "IssueClockSyncMarker") { | 125 } else if (cmd == "IssueClockSyncMarker") { |
| 110 // TODO(charliea): Write IssueClockSyncMarker. | 126 // TODO(charliea): Write IssueClockSyncMarker. |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 scoped_ptr<BattOrAgent> agent_; | 242 scoped_ptr<BattOrAgent> agent_; |
| 227 }; | 243 }; |
| 228 | 244 |
| 229 } // namespace battor | 245 } // namespace battor |
| 230 | 246 |
| 231 int main(int argc, char* argv[]) { | 247 int main(int argc, char* argv[]) { |
| 232 base::AtExitManager exit_manager; | 248 base::AtExitManager exit_manager; |
| 233 battor::BattOrAgentBin bin; | 249 battor::BattOrAgentBin bin; |
| 234 return bin.Run(argc, argv); | 250 return bin.Run(argc, argv); |
| 235 } | 251 } |
| OLD | NEW |