Ratan Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2018 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 16 | #include "argument.hpp" |
| 17 | #include "ncsi_util.hpp" |
| 18 | |
| 19 | #include <iostream> |
| 20 | #include <string> |
| 21 | |
| 22 | static void exitWithError(const char* err, char** argv) |
| 23 | { |
| 24 | phosphor::network::ncsi::ArgumentParser::usage(argv); |
| 25 | std::cerr << "ERROR: " << err << "\n"; |
| 26 | exit(EXIT_FAILURE); |
| 27 | } |
| 28 | |
Ratan Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 29 | int main(int argc, char** argv) |
| 30 | { |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 31 | using namespace phosphor::network; |
| 32 | using namespace phosphor::network::ncsi; |
| 33 | // Read arguments. |
| 34 | auto options = ArgumentParser(argc, argv); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 35 | int packageInt{}; |
| 36 | int channelInt{}; |
| 37 | int indexInt{}; |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 38 | |
| 39 | // Parse out interface argument. |
| 40 | auto ifIndex = (options)["index"]; |
| 41 | try |
| 42 | { |
| 43 | indexInt = stoi(ifIndex, nullptr); |
| 44 | } |
| 45 | catch (const std::exception& e) |
| 46 | { |
| 47 | exitWithError("Interface not specified.", argv); |
| 48 | } |
| 49 | |
| 50 | if (indexInt < 0) |
| 51 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 52 | exitWithError("Interface value should be greater than equal to 0", |
| 53 | argv); |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | // Parse out package argument. |
| 57 | auto package = (options)["package"]; |
| 58 | try |
| 59 | { |
| 60 | packageInt = stoi(package, nullptr); |
| 61 | } |
| 62 | catch (const std::exception& e) |
| 63 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 64 | packageInt = DEFAULT_VALUE; |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | if (packageInt < 0) |
| 68 | { |
| 69 | packageInt = DEFAULT_VALUE; |
| 70 | } |
| 71 | |
| 72 | // Parse out channel argument. |
| 73 | auto channel = (options)["channel"]; |
| 74 | try |
| 75 | { |
| 76 | channelInt = stoi(channel, nullptr); |
| 77 | } |
| 78 | catch (const std::exception& e) |
| 79 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 80 | channelInt = DEFAULT_VALUE; |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | if (channelInt < 0) |
| 84 | { |
| 85 | channelInt = DEFAULT_VALUE; |
| 86 | } |
| 87 | |
| 88 | auto setCmd = (options)["set"]; |
| 89 | if (setCmd == "true") |
| 90 | { |
Gunnar Mills | 6af6144 | 2018-04-08 14:50:06 -0500 | [diff] [blame] | 91 | // Can not perform set operation without package. |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 92 | if (packageInt == DEFAULT_VALUE) |
| 93 | { |
| 94 | exitWithError("Package not specified.", argv); |
| 95 | } |
| 96 | return ncsi::setChannel(indexInt, packageInt, channelInt); |
| 97 | } |
| 98 | else if ((options)["info"] == "true") |
| 99 | { |
| 100 | return ncsi::getInfo(indexInt, packageInt); |
| 101 | } |
| 102 | else if ((options)["clear"] == "true") |
| 103 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 104 | return ncsi::clearInterface(indexInt); |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 105 | } |
| 106 | else |
| 107 | { |
| 108 | exitWithError("No Command specified", argv); |
| 109 | } |
Ratan Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 110 | return 0; |
| 111 | } |