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 */ 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 29 int main(int argc, char** argv) 30 { 31 using namespace phosphor::network; 32 using namespace phosphor::network::ncsi; 33 // Read arguments. 34 auto options = ArgumentParser(argc, argv); 35 int packageInt{}; 36 int channelInt{}; 37 int indexInt{}; 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 { 52 exitWithError("Interface value should be greater than equal to 0", 53 argv); 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 { 64 packageInt = DEFAULT_VALUE; 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 { 80 channelInt = DEFAULT_VALUE; 81 } 82 83 if (channelInt < 0) 84 { 85 channelInt = DEFAULT_VALUE; 86 } 87 88 auto setCmd = (options)["set"]; 89 if (setCmd == "true") 90 { 91 // Can not perform set operation without package. 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 { 104 return ncsi::clearInterface(indexInt); 105 } 106 else 107 { 108 exitWithError("No Command specified", argv); 109 } 110 return 0; 111 } 112