blob: 5f65777b54a09c3ea540808a1eab1596debf1b6b [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
Jagpal Singh Gilld423beb2023-04-18 11:28:03 -070019#include <phosphor-logging/lg2.hpp>
Patrick Williams89d734b2023-05-10 07:50:25 -050020
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053021#include <string>
Eddie Jamesfa1f5c02020-09-17 15:12:46 -050022#include <vector>
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053023
24static void exitWithError(const char* err, char** argv)
25{
26 phosphor::network::ncsi::ArgumentParser::usage(argv);
Jagpal Singh Gilld423beb2023-04-18 11:28:03 -070027 lg2::error("ERROR: {ERROR}", "ERROR", err);
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053028 exit(EXIT_FAILURE);
29}
30
Ratan Guptab38401b2018-03-16 12:44:26 +053031int main(int argc, char** argv)
32{
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053033 using namespace phosphor::network;
34 using namespace phosphor::network::ncsi;
35 // Read arguments.
36 auto options = ArgumentParser(argc, argv);
Gunnar Mills57d9c502018-09-14 14:42:34 -050037 int packageInt{};
38 int channelInt{};
39 int indexInt{};
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053040
41 // Parse out interface argument.
42 auto ifIndex = (options)["index"];
43 try
44 {
45 indexInt = stoi(ifIndex, nullptr);
46 }
47 catch (const std::exception& e)
48 {
49 exitWithError("Interface not specified.", argv);
50 }
51
52 if (indexInt < 0)
53 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050054 exitWithError("Interface value should be greater than equal to 0",
55 argv);
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053056 }
57
58 // Parse out package argument.
59 auto package = (options)["package"];
60 try
61 {
62 packageInt = stoi(package, nullptr);
63 }
64 catch (const std::exception& e)
65 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050066 packageInt = DEFAULT_VALUE;
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053067 }
68
69 if (packageInt < 0)
70 {
71 packageInt = DEFAULT_VALUE;
72 }
73
74 // Parse out channel argument.
75 auto channel = (options)["channel"];
76 try
77 {
78 channelInt = stoi(channel, nullptr);
79 }
80 catch (const std::exception& e)
81 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050082 channelInt = DEFAULT_VALUE;
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053083 }
84
85 if (channelInt < 0)
86 {
87 channelInt = DEFAULT_VALUE;
88 }
89
Eddie Jamesfa1f5c02020-09-17 15:12:46 -050090 auto payloadStr = (options)["oem-payload"];
91 if (!payloadStr.empty())
92 {
93 std::string byte(2, '\0');
94 std::vector<unsigned char> payload;
95
96 if (payloadStr.size() % 2)
97 exitWithError("Payload invalid: specify two hex digits per byte.",
98 argv);
99
100 // Parse the payload string (e.g. "000001572100") to byte data
101 for (unsigned int i = 1; i < payloadStr.size(); i += 2)
102 {
103 byte[0] = payloadStr[i - 1];
104 byte[1] = payloadStr[i];
105
106 try
107 {
108 payload.push_back(stoi(byte, nullptr, 16));
109 }
110 catch (const std::exception& e)
111 {
112 exitWithError("Payload invalid.", argv);
113 }
114 }
115
116 if (payload.empty())
117 {
118 exitWithError("No payload specified.", argv);
119 }
120
121 if (packageInt == DEFAULT_VALUE)
122 {
123 exitWithError("Package not specified.", argv);
124 }
125
126 return ncsi::sendOemCommand(
127 indexInt, packageInt, channelInt,
128 std::span<const unsigned char>(payload.begin(), payload.end()));
129 }
130 else if ((options)["set"] == "true")
Ratan Guptaed5d7ff2018-03-23 00:27:52 +0530131 {
Gunnar Mills6af61442018-04-08 14:50:06 -0500132 // Can not perform set operation without package.
Ratan Guptaed5d7ff2018-03-23 00:27:52 +0530133 if (packageInt == DEFAULT_VALUE)
134 {
135 exitWithError("Package not specified.", argv);
136 }
137 return ncsi::setChannel(indexInt, packageInt, channelInt);
138 }
139 else if ((options)["info"] == "true")
140 {
141 return ncsi::getInfo(indexInt, packageInt);
142 }
143 else if ((options)["clear"] == "true")
144 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500145 return ncsi::clearInterface(indexInt);
Ratan Guptaed5d7ff2018-03-23 00:27:52 +0530146 }
147 else
148 {
149 exitWithError("No Command specified", argv);
150 }
Ratan Guptab38401b2018-03-16 12:44:26 +0530151 return 0;
152}