blob: 9db624f5b1ed24cb817ee265cfb3d3ecf9a67573 [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);
Gunnar Mills57d9c502018-09-14 14:42:34 -050035 int packageInt{};
36 int channelInt{};
37 int indexInt{};
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053038
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 Mills57d9c502018-09-14 14:42:34 -050052 exitWithError("Interface value should be greater than equal to 0",
53 argv);
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053054 }
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 Mills57d9c502018-09-14 14:42:34 -050064 packageInt = DEFAULT_VALUE;
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053065 }
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 Mills57d9c502018-09-14 14:42:34 -050080 channelInt = DEFAULT_VALUE;
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053081 }
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 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500104 return ncsi::clearInterface(indexInt);
Ratan Guptaed5d7ff2018-03-23 00:27:52 +0530105 }
106 else
107 {
108 exitWithError("No Command specified", argv);
109 }
Ratan Guptab38401b2018-03-16 12:44:26 +0530110 return 0;
111}