diff options
author | 2017-09-07 13:54:28 -0700 | |
---|---|---|
committer | 2017-09-14 13:40:27 -0700 | |
commit | ded398e5b6530c4ced5bd42f9b75ca2caa527239 (patch) | |
tree | 0ec262655bf7b5f7bd587cd8fccaa991e7434dd3 | |
parent | 91e655dc2be238fafcf832c73e62166d7b69c848 (diff) |
lshal: Add class Command.
Command is the base class for all *Command classes.
Test: lshal_test
Bug: 35389839
Change-Id: I9aca19e66824536d13e618ffd0f012ac3da9880d
-rw-r--r-- | cmds/lshal/Command.h | 44 | ||||
-rw-r--r-- | cmds/lshal/DebugCommand.cpp | 3 | ||||
-rw-r--r-- | cmds/lshal/DebugCommand.h | 9 | ||||
-rw-r--r-- | cmds/lshal/ListCommand.cpp | 3 | ||||
-rw-r--r-- | cmds/lshal/ListCommand.h | 9 | ||||
-rw-r--r-- | cmds/lshal/Lshal.cpp | 20 | ||||
-rw-r--r-- | cmds/lshal/Lshal.h | 5 |
7 files changed, 72 insertions, 21 deletions
diff --git a/cmds/lshal/Command.h b/cmds/lshal/Command.h new file mode 100644 index 0000000000..b1efb97c3b --- /dev/null +++ b/cmds/lshal/Command.h @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FRAMEWORK_NATIVE_CMDS_LSHAL_COMMAND_H_ +#define FRAMEWORK_NATIVE_CMDS_LSHAL_COMMAND_H_ + +#include "utils.h" + +namespace android { +namespace lshal { + +class Lshal; + +// Base class for all *Commands +class Command { +public: + Command(Lshal& lshal) : mLshal(lshal) {} + virtual ~Command() = default; + // Expect optind to be set by Lshal::main and points to the next argument + // to process. + virtual Status main(const std::string &command, const Arg &arg) = 0; + +protected: + Lshal& mLshal; +}; + + +} // namespace lshal +} // namespace android + +#endif // FRAMEWORK_NATIVE_CMDS_LSHAL_LIST_COMMAND_H_ diff --git a/cmds/lshal/DebugCommand.cpp b/cmds/lshal/DebugCommand.cpp index 672cad67cb..e657bcf4e6 100644 --- a/cmds/lshal/DebugCommand.cpp +++ b/cmds/lshal/DebugCommand.cpp @@ -21,9 +21,6 @@ namespace android { namespace lshal { -DebugCommand::DebugCommand(Lshal &lshal) : mLshal(lshal) { -} - Status DebugCommand::parseArgs(const std::string &command, const Arg &arg) { if (optind >= arg.argc) { mLshal.usage(command); diff --git a/cmds/lshal/DebugCommand.h b/cmds/lshal/DebugCommand.h index fa0f0faafb..afa5e9788e 100644 --- a/cmds/lshal/DebugCommand.h +++ b/cmds/lshal/DebugCommand.h @@ -21,6 +21,7 @@ #include <android-base/macros.h> +#include "Command.h" #include "utils.h" namespace android { @@ -28,14 +29,14 @@ namespace lshal { class Lshal; -class DebugCommand { +class DebugCommand : public Command { public: - DebugCommand(Lshal &lshal); - Status main(const std::string &command, const Arg &arg); + DebugCommand(Lshal &lshal) : Command(lshal) {} + ~DebugCommand() = default; + Status main(const std::string &command, const Arg &arg) override; private: Status parseArgs(const std::string &command, const Arg &arg); - Lshal &mLshal; std::string mInterfaceName; std::vector<std::string> mOptions; diff --git a/cmds/lshal/ListCommand.cpp b/cmds/lshal/ListCommand.cpp index 4550e410a0..ffb442460c 100644 --- a/cmds/lshal/ListCommand.cpp +++ b/cmds/lshal/ListCommand.cpp @@ -44,9 +44,6 @@ using ::android::hidl::manager::V1_0::IServiceManager; namespace android { namespace lshal { -ListCommand::ListCommand(Lshal &lshal) : mLshal(lshal) { -} - NullableOStream<std::ostream> ListCommand::out() const { return mLshal.out(); } diff --git a/cmds/lshal/ListCommand.h b/cmds/lshal/ListCommand.h index 5047cf88bb..8d25d94b77 100644 --- a/cmds/lshal/ListCommand.h +++ b/cmds/lshal/ListCommand.h @@ -26,6 +26,7 @@ #include <android-base/macros.h> #include <android/hidl/manager/1.0/IServiceManager.h> +#include "Command.h" #include "NullableOStream.h" #include "TableEntry.h" #include "TextTable.h" @@ -42,11 +43,11 @@ struct PidInfo { uint32_t threadCount; // number of threads total }; -class ListCommand { +class ListCommand : public Command { public: - ListCommand(Lshal &lshal); + ListCommand(Lshal &lshal) : Command(lshal) {} virtual ~ListCommand() = default; - Status main(const std::string &command, const Arg &arg); + Status main(const std::string &command, const Arg &arg) override; protected: Status parseArgs(const std::string &command, const Arg &arg); Status fetch(); @@ -79,8 +80,6 @@ protected: NullableOStream<std::ostream> err() const; NullableOStream<std::ostream> out() const; - Lshal &mLshal; - Table mServicesTable{}; Table mPassthroughRefTable{}; Table mImplementationsTable{}; diff --git a/cmds/lshal/Lshal.cpp b/cmds/lshal/Lshal.cpp index da45d65e4a..9c5c234f16 100644 --- a/cmds/lshal/Lshal.cpp +++ b/cmds/lshal/Lshal.cpp @@ -218,6 +218,17 @@ void signalHandler(int sig) { } } +std::unique_ptr<Command> Lshal::selectCommand(const std::string& command) { + // Default command is list + if (command == "list" || command == "") { + return std::make_unique<ListCommand>(*this); + } + if (command == "debug") { + return std::make_unique<DebugCommand>(*this); + } + return nullptr; +} + Status Lshal::main(const Arg &arg) { // Allow SIGINT to terminate all threads. signal(SIGINT, signalHandler); @@ -230,12 +241,9 @@ Status Lshal::main(const Arg &arg) { usage(optind < arg.argc ? arg.argv[optind] : ""); return USAGE; } - // Default command is list - if (mCommand == "list" || mCommand == "") { - return ListCommand{*this}.main(mCommand, arg); - } - if (mCommand == "debug") { - return DebugCommand{*this}.main(mCommand, arg); + auto c = selectCommand(mCommand); + if (c != nullptr) { + return c->main(mCommand, arg); } usage(); return USAGE; diff --git a/cmds/lshal/Lshal.h b/cmds/lshal/Lshal.h index d3cc4e20a7..bfd6a404f8 100644 --- a/cmds/lshal/Lshal.h +++ b/cmds/lshal/Lshal.h @@ -24,6 +24,7 @@ #include <android/hidl/manager/1.0/IServiceManager.h> #include <utils/StrongPointer.h> +#include "Command.h" #include "NullableOStream.h" #include "utils.h" @@ -50,8 +51,12 @@ public: const std::vector<std::string> &options, std::ostream &out, NullableOStream<std::ostream> err) const; + + std::unique_ptr<Command> selectCommand(const std::string& command); + private: Status parseArgs(const Arg &arg); + std::string mCommand; Arg mCmdArgs; NullableOStream<std::ostream> mOut; |