blob: aeb26bbc6fce59df0125746d223ee962f9cdd054 [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 */
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053016#include "argument.hpp"
17#include "ncsi_util.hpp"
18
19#include <iostream>
20#include <string>
21
22static 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 Guptab38401b2018-03-16 12:44:26 +053029int main(int argc, char** argv)
30{
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053031 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", argv);
53 }
54
55 // Parse out package argument.
56 auto package = (options)["package"];
57 try
58 {
59 packageInt = stoi(package, nullptr);
60 }
61 catch (const std::exception& e)
62 {
63 packageInt = DEFAULT_VALUE;
64 }
65
66 if (packageInt < 0)
67 {
68 packageInt = DEFAULT_VALUE;
69 }
70
71 // Parse out channel argument.
72 auto channel = (options)["channel"];
73 try
74 {
75 channelInt = stoi(channel, nullptr);
76 }
77 catch (const std::exception& e)
78 {
79 channelInt = DEFAULT_VALUE;
80 }
81
82 if (channelInt < 0)
83 {
84 channelInt = DEFAULT_VALUE;
85 }
86
87 auto setCmd = (options)["set"];
88 if (setCmd == "true")
89 {
Gunnar Mills6af61442018-04-08 14:50:06 -050090 // Can not perform set operation without package.
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053091 if (packageInt == DEFAULT_VALUE)
92 {
93 exitWithError("Package not specified.", argv);
94 }
95 return ncsi::setChannel(indexInt, packageInt, channelInt);
96 }
97 else if ((options)["info"] == "true")
98 {
99 return ncsi::getInfo(indexInt, packageInt);
100 }
101 else if ((options)["clear"] == "true")
102 {
103 return ncsi::clearInterface(indexInt);
104 }
105 else
106 {
107 exitWithError("No Command specified", argv);
108 }
Ratan Guptab38401b2018-03-16 12:44:26 +0530109 return 0;
110}
111