blob: 41e426443f6ea1c405f3e9ab2acc6f6e5050962b [file] [log] [blame]
Ratan Guptab38401b2018-03-16 12:44:26 +05301/**
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
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053017#include "argument.hpp"
18#include "ncsi_util.hpp"
19
20#include <iostream>
21#include <string>
22
23static void exitWithError(const char* err, char** argv)
24{
25 phosphor::network::ncsi::ArgumentParser::usage(argv);
26 std::cerr << "ERROR: " << err << "\n";
27 exit(EXIT_FAILURE);
28}
29
Ratan Guptab38401b2018-03-16 12:44:26 +053030int main(int argc, char** argv)
31{
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053032 using namespace phosphor::network;
33 using namespace phosphor::network::ncsi;
34 // Read arguments.
35 auto options = ArgumentParser(argc, argv);
36 int packageInt {};
37 int channelInt {};
38 int indexInt {};
39
40 // Parse out interface argument.
41 auto ifIndex = (options)["index"];
42 try
43 {
44 indexInt = stoi(ifIndex, nullptr);
45 }
46 catch (const std::exception& e)
47 {
48 exitWithError("Interface not specified.", argv);
49 }
50
51 if (indexInt < 0)
52 {
53 exitWithError("Interface value should be greater than equal to 0", 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 {
Gunnar Mills6af61442018-04-08 14:50:06 -050091 // Can not perform set operation without package.
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053092 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 }
Ratan Guptab38401b2018-03-16 12:44:26 +0530110 return 0;
111}
112