Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 1 | #include "handler.hpp" |
| 2 | |
| 3 | #include <CLI/CLI.hpp> |
| 4 | |
| 5 | int main(int argc, char** argv) |
| 6 | { |
| 7 | |
| 8 | CLI::App app{"PLDM requester tool for OpenBMC"}; |
| 9 | |
| 10 | // TODO: To enable it later |
| 11 | // bool verbose_flag = false; |
| 12 | // app.add_flag("-v, --verbose", verbose_flag, "Output debug logs "); |
| 13 | std::vector<std::string> args{}; |
Lakshminarayana R. Kammath | 58229b2 | 2019-08-09 06:30:04 -0500 | [diff] [blame] | 14 | app.add_option("-c, —command", args, " PLDM request command"); |
| 15 | |
| 16 | std::string rawCmd; |
| 17 | app.add_option("raw", rawCmd, "Send a RAW PLDM request and print response"); |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 18 | |
| 19 | std::string pldmCmdName; |
| 20 | app.add_option("GetPLDMTypes", pldmCmdName, "Get PLDM Type"); |
| 21 | app.add_option("GetPLDMVersion", pldmCmdName, "Get PLDM Version"); |
| 22 | |
Lakshminarayana R. Kammath | 58229b2 | 2019-08-09 06:30:04 -0500 | [diff] [blame] | 23 | app.add_subcommand("BASE", "PLDM Command Type = BASE"); |
| 24 | app.add_subcommand("BIOS", "PLDM Command Type = BIOS"); |
| 25 | app.add_subcommand("OEM", "PLDM Command Type = OEM"); |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 26 | |
| 27 | CLI11_PARSE(app, argc, argv); |
| 28 | |
Lakshminarayana R. Kammath | 58229b2 | 2019-08-09 06:30:04 -0500 | [diff] [blame] | 29 | std::string cmdName; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 30 | int rc = 0; |
| 31 | |
Lakshminarayana R. Kammath | 58229b2 | 2019-08-09 06:30:04 -0500 | [diff] [blame] | 32 | if (args[0] != "raw") |
| 33 | { |
| 34 | // Parse args to program |
| 35 | cmdName = args[0]; |
| 36 | } |
| 37 | else |
| 38 | { |
| 39 | // removing 'raw' from the args list since it is positional argument |
| 40 | // and not an input value. |
| 41 | args.erase(args.begin()); |
| 42 | |
| 43 | // loop through the remaining argument list |
| 44 | for (auto&& item : args) |
| 45 | { |
| 46 | |
| 47 | if (item[0] == '0' && (item[1] == 'x' || item[1] == 'X')) |
| 48 | { |
| 49 | |
| 50 | // Erase 0x from input |
| 51 | item.erase(0, 2); |
| 52 | |
| 53 | // Check for hex input value validity |
| 54 | if (std::all_of(item.begin(), item.end(), ::isxdigit)) |
| 55 | { |
| 56 | |
| 57 | // Parse args to program |
| 58 | cmdName = "HandleRawOp"; |
| 59 | } |
| 60 | else |
| 61 | { |
| 62 | std::cerr << item << " contains non hex digits. Re-enter" |
| 63 | << std::endl; |
| 64 | rc = -1; |
| 65 | return rc; |
| 66 | } |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | std::cout << item << " Input hex value starting with 0x " |
| 71 | << std::endl; |
| 72 | rc = -1; |
| 73 | return rc; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | Handler handler; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 79 | try |
| 80 | { |
| 81 | handler.dispatcher.at(cmdName)(std::move(args)); |
| 82 | } |
| 83 | catch (const std::out_of_range& e) |
| 84 | { |
| 85 | std::cerr << cmdName << " is not supported!" << std::endl; |
| 86 | rc = -1; |
| 87 | } |
Lakshminarayana R. Kammath | 58229b2 | 2019-08-09 06:30:04 -0500 | [diff] [blame] | 88 | return rc; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 89 | } |