blob: 8859950d715c209a70d1477ffa378ad917715cf6 [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>
Jeremy Kerr147086d2024-08-27 13:46:38 +080020#include <stdplus/numeric/str.hpp>
21#include <stdplus/str/buf.hpp>
Patrick Williams89d734b2023-05-10 07:50:25 -050022
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053023#include <string>
Eddie Jamesfa1f5c02020-09-17 15:12:46 -050024#include <vector>
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053025
26static void exitWithError(const char* err, char** argv)
27{
28 phosphor::network::ncsi::ArgumentParser::usage(argv);
Jagpal Singh Gilld423beb2023-04-18 11:28:03 -070029 lg2::error("ERROR: {ERROR}", "ERROR", err);
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053030 exit(EXIT_FAILURE);
31}
32
Jeremy Kerr7f7c0852024-08-08 11:32:55 +080033static void printInfo(phosphor::network::ncsi::InterfaceInfo& info)
34{
35 using namespace phosphor::network::ncsi;
36
37 for (PackageInfo& pkg : info.packages)
38 {
39 lg2::debug("Package id : {ID}", "ID", pkg.id);
40 if (pkg.forced)
41 {
42 lg2::debug(" package is forced");
43 }
44 for (ChannelInfo& chan : pkg.channels)
45 {
46 lg2::debug(" Channel id : {ID}", "ID", chan.id);
47 if (chan.forced)
48 {
49 lg2::debug(" channel is forced");
50 }
51 if (chan.active)
52 {
53 lg2::debug(" channel is active");
54 }
55
56 lg2::debug(" version {MAJOR}.{MINOR} ({STR})", "MAJOR",
57 chan.version_major, "MINOR", chan.version_minor, "STR",
58 chan.version);
59
60 lg2::debug(" link state {LINK}", "LINK", lg2::hex,
61 chan.link_state);
62
63 auto& vlans = chan.vlan_ids;
64
65 if (!vlans.empty())
66 {
67 lg2::debug(" Actve VLAN IDs:");
68 for (uint16_t vlan : vlans)
69 {
70 lg2::debug(" VID: {VLAN_ID}", "VLAN_ID", vlan);
71 }
72 }
73 }
74 }
75}
76
Jeremy Kerr147086d2024-08-27 13:46:38 +080077static stdplus::StrBuf toHexStr(std::span<const uint8_t> c) noexcept
78{
79 stdplus::StrBuf ret;
80 if (c.empty())
81 {
82 return ret;
83 }
84 stdplus::IntToStr<16, uint8_t> its;
85 auto oit = ret.append(c.size() * 3);
86 auto cit = c.begin();
87 oit = its(oit, *cit++, 2);
88 for (; cit != c.end(); ++cit)
89 {
90 *oit++ = ' ';
91 oit = its(oit, *cit, 2);
92 }
93 *oit = 0;
94 return ret;
95}
96
Ratan Guptab38401b2018-03-16 12:44:26 +053097int main(int argc, char** argv)
98{
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053099 using namespace phosphor::network;
100 using namespace phosphor::network::ncsi;
101 // Read arguments.
102 auto options = ArgumentParser(argc, argv);
Gunnar Mills57d9c502018-09-14 14:42:34 -0500103 int packageInt{};
104 int channelInt{};
105 int indexInt{};
Johnathan Mantey1ebea282024-02-15 10:26:06 -0800106 int operationInt{DEFAULT_VALUE};
Ratan Guptaed5d7ff2018-03-23 00:27:52 +0530107
108 // Parse out interface argument.
109 auto ifIndex = (options)["index"];
110 try
111 {
112 indexInt = stoi(ifIndex, nullptr);
113 }
114 catch (const std::exception& e)
115 {
116 exitWithError("Interface not specified.", argv);
117 }
118
119 if (indexInt < 0)
120 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500121 exitWithError("Interface value should be greater than equal to 0",
122 argv);
Ratan Guptaed5d7ff2018-03-23 00:27:52 +0530123 }
124
Jeremy Kerr2d0b48d2024-09-16 13:03:26 +0800125 NetlinkInterface interface(indexInt);
Jeremy Kerr8d9af022024-07-26 16:47:16 +0800126
Ratan Guptaed5d7ff2018-03-23 00:27:52 +0530127 // Parse out package argument.
128 auto package = (options)["package"];
129 try
130 {
131 packageInt = stoi(package, nullptr);
132 }
133 catch (const std::exception& e)
134 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500135 packageInt = DEFAULT_VALUE;
Ratan Guptaed5d7ff2018-03-23 00:27:52 +0530136 }
137
138 if (packageInt < 0)
139 {
140 packageInt = DEFAULT_VALUE;
141 }
142
143 // Parse out channel argument.
144 auto channel = (options)["channel"];
145 try
146 {
147 channelInt = stoi(channel, nullptr);
148 }
149 catch (const std::exception& e)
150 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500151 channelInt = DEFAULT_VALUE;
Ratan Guptaed5d7ff2018-03-23 00:27:52 +0530152 }
153
154 if (channelInt < 0)
155 {
156 channelInt = DEFAULT_VALUE;
157 }
158
Eddie Jamesfa1f5c02020-09-17 15:12:46 -0500159 auto payloadStr = (options)["oem-payload"];
160 if (!payloadStr.empty())
161 {
162 std::string byte(2, '\0');
163 std::vector<unsigned char> payload;
164
165 if (payloadStr.size() % 2)
166 exitWithError("Payload invalid: specify two hex digits per byte.",
167 argv);
168
Johnathan Mantey1ebea282024-02-15 10:26:06 -0800169 // Parse the payload string (e.g. "50000001572100") to byte data
170 // The first two characters (i.e. "50") represent the Send Cmd Operation
171 // All remaining pairs, interpreted in hex radix, represent the command
172 // payload
173 int sendCmdSelect{};
Eddie Jamesfa1f5c02020-09-17 15:12:46 -0500174 for (unsigned int i = 1; i < payloadStr.size(); i += 2)
175 {
176 byte[0] = payloadStr[i - 1];
177 byte[1] = payloadStr[i];
178
179 try
180 {
Johnathan Mantey1ebea282024-02-15 10:26:06 -0800181 sendCmdSelect = stoi(byte, nullptr, 16);
Eddie Jamesfa1f5c02020-09-17 15:12:46 -0500182 }
183 catch (const std::exception& e)
184 {
185 exitWithError("Payload invalid.", argv);
186 }
Johnathan Mantey1ebea282024-02-15 10:26:06 -0800187 if (i == 1)
188 {
189 operationInt = sendCmdSelect;
190 }
191 else
192 {
193 payload.push_back(sendCmdSelect);
194 }
Eddie Jamesfa1f5c02020-09-17 15:12:46 -0500195 }
196
Johnathan Mantey1ebea282024-02-15 10:26:06 -0800197 if (operationInt == DEFAULT_VALUE)
Eddie Jamesfa1f5c02020-09-17 15:12:46 -0500198 {
199 exitWithError("No payload specified.", argv);
200 }
201
202 if (packageInt == DEFAULT_VALUE)
203 {
204 exitWithError("Package not specified.", argv);
205 }
206
Jeremy Kerr147086d2024-08-27 13:46:38 +0800207 if (!payload.empty())
208 {
209 lg2::debug("Payload: {PAYLOAD}", "PAYLOAD", toHexStr(payload));
210 }
211
Jeremy Kerrb7885242024-09-16 12:43:36 +0800212 std::optional<uint8_t> chan = channelInt != DEFAULT_VALUE
213 ? std::make_optional(channelInt)
214 : std::nullopt;
215 NCSICommand cmd(operationInt, packageInt, chan, payload);
216
217 auto resp = interface.sendCommand(cmd);
Jeremy Kerr147086d2024-08-27 13:46:38 +0800218 if (!resp)
219 {
220 return EXIT_FAILURE;
221 }
222 lg2::debug("Response {DATA_LEN} bytes: {DATA}", "DATA_LEN",
Jeremy Kerrb7885242024-09-16 12:43:36 +0800223 resp->full_payload.size(), "DATA",
224 toHexStr(resp->full_payload));
Eddie Jamesfa1f5c02020-09-17 15:12:46 -0500225 }
226 else if ((options)["set"] == "true")
Ratan Guptaed5d7ff2018-03-23 00:27:52 +0530227 {
Gunnar Mills6af61442018-04-08 14:50:06 -0500228 // Can not perform set operation without package.
Ratan Guptaed5d7ff2018-03-23 00:27:52 +0530229 if (packageInt == DEFAULT_VALUE)
230 {
231 exitWithError("Package not specified.", argv);
232 }
Jeremy Kerrbc22f812024-07-29 17:43:35 +0800233 return interface.setChannel(packageInt, channelInt);
Ratan Guptaed5d7ff2018-03-23 00:27:52 +0530234 }
235 else if ((options)["info"] == "true")
236 {
Jeremy Kerr7f7c0852024-08-08 11:32:55 +0800237 auto info = interface.getInfo(packageInt);
238 if (!info)
239 {
240 return EXIT_FAILURE;
241 }
242 printInfo(*info);
Ratan Guptaed5d7ff2018-03-23 00:27:52 +0530243 }
244 else if ((options)["clear"] == "true")
245 {
Jeremy Kerrbc22f812024-07-29 17:43:35 +0800246 return interface.clearInterface();
Ratan Guptaed5d7ff2018-03-23 00:27:52 +0530247 }
Johnathan Mantey5a456062024-02-15 08:45:08 -0800248 else if (!(options)["pmask"].empty())
249 {
250 unsigned int mask{};
251 try
252 {
253 size_t lastChar{};
254 mask = std::stoul((options)["pmask"], &lastChar, 0);
255 if (lastChar < (options["pmask"].size()))
256 {
257 exitWithError("Package mask value is not valid", argv);
258 }
259 }
260 catch (const std::exception& e)
261 {
262 exitWithError("Package mask value is not valid", argv);
263 }
Jeremy Kerrbc22f812024-07-29 17:43:35 +0800264 return interface.setPackageMask(mask);
Johnathan Mantey5a456062024-02-15 08:45:08 -0800265 }
266 else if (!(options)["cmask"].empty())
267 {
268 if (packageInt == DEFAULT_VALUE)
269 {
270 exitWithError("Package is not specified", argv);
271 }
272 unsigned int mask{};
273 try
274 {
275 size_t lastChar{};
276 mask = stoul((options)["cmask"], &lastChar, 0);
277 if (lastChar < (options["cmask"].size()))
278 {
279 exitWithError("Channel mask value is not valid", argv);
280 }
281 }
282 catch (const std::exception& e)
283 {
284 exitWithError("Channel mask value is not valid", argv);
285 }
Jeremy Kerrbc22f812024-07-29 17:43:35 +0800286 return interface.setChannelMask(packageInt, mask);
Johnathan Mantey5a456062024-02-15 08:45:08 -0800287 }
Ratan Guptaed5d7ff2018-03-23 00:27:52 +0530288 else
289 {
290 exitWithError("No Command specified", argv);
291 }
Ratan Guptab38401b2018-03-16 12:44:26 +0530292 return 0;
293}