Ratan Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 1 | /** |
| 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 Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 16 | #include "argument.hpp" |
| 17 | #include "ncsi_util.hpp" |
| 18 | |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 19 | #include <phosphor-logging/lg2.hpp> |
Jeremy Kerr | 147086d | 2024-08-27 13:46:38 +0800 | [diff] [blame] | 20 | #include <stdplus/numeric/str.hpp> |
| 21 | #include <stdplus/str/buf.hpp> |
Patrick Williams | 89d734b | 2023-05-10 07:50:25 -0500 | [diff] [blame] | 22 | |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 23 | #include <string> |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 24 | #include <vector> |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 25 | |
| 26 | static void exitWithError(const char* err, char** argv) |
| 27 | { |
| 28 | phosphor::network::ncsi::ArgumentParser::usage(argv); |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 29 | lg2::error("ERROR: {ERROR}", "ERROR", err); |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 30 | exit(EXIT_FAILURE); |
| 31 | } |
| 32 | |
Jeremy Kerr | 7f7c085 | 2024-08-08 11:32:55 +0800 | [diff] [blame] | 33 | static 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 Kerr | 147086d | 2024-08-27 13:46:38 +0800 | [diff] [blame] | 77 | static 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 Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 97 | int main(int argc, char** argv) |
| 98 | { |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 99 | using namespace phosphor::network; |
| 100 | using namespace phosphor::network::ncsi; |
| 101 | // Read arguments. |
| 102 | auto options = ArgumentParser(argc, argv); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 103 | int packageInt{}; |
| 104 | int channelInt{}; |
| 105 | int indexInt{}; |
Johnathan Mantey | 1ebea28 | 2024-02-15 10:26:06 -0800 | [diff] [blame] | 106 | int operationInt{DEFAULT_VALUE}; |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 107 | |
| 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 Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 121 | exitWithError("Interface value should be greater than equal to 0", |
| 122 | argv); |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 123 | } |
| 124 | |
Jeremy Kerr | 8d9af02 | 2024-07-26 16:47:16 +0800 | [diff] [blame] | 125 | Interface interface{indexInt}; |
| 126 | |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 127 | // 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 Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 135 | packageInt = DEFAULT_VALUE; |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 136 | } |
| 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 Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 151 | channelInt = DEFAULT_VALUE; |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | if (channelInt < 0) |
| 155 | { |
| 156 | channelInt = DEFAULT_VALUE; |
| 157 | } |
| 158 | |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 159 | 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 Mantey | 1ebea28 | 2024-02-15 10:26:06 -0800 | [diff] [blame] | 169 | // 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 James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 174 | 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 Mantey | 1ebea28 | 2024-02-15 10:26:06 -0800 | [diff] [blame] | 181 | sendCmdSelect = stoi(byte, nullptr, 16); |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 182 | } |
| 183 | catch (const std::exception& e) |
| 184 | { |
| 185 | exitWithError("Payload invalid.", argv); |
| 186 | } |
Johnathan Mantey | 1ebea28 | 2024-02-15 10:26:06 -0800 | [diff] [blame] | 187 | if (i == 1) |
| 188 | { |
| 189 | operationInt = sendCmdSelect; |
| 190 | } |
| 191 | else |
| 192 | { |
| 193 | payload.push_back(sendCmdSelect); |
| 194 | } |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 195 | } |
| 196 | |
Johnathan Mantey | 1ebea28 | 2024-02-15 10:26:06 -0800 | [diff] [blame] | 197 | if (operationInt == DEFAULT_VALUE) |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 198 | { |
| 199 | exitWithError("No payload specified.", argv); |
| 200 | } |
| 201 | |
| 202 | if (packageInt == DEFAULT_VALUE) |
| 203 | { |
| 204 | exitWithError("Package not specified.", argv); |
| 205 | } |
| 206 | |
Jeremy Kerr | 147086d | 2024-08-27 13:46:38 +0800 | [diff] [blame] | 207 | if (!payload.empty()) |
| 208 | { |
| 209 | lg2::debug("Payload: {PAYLOAD}", "PAYLOAD", toHexStr(payload)); |
| 210 | } |
| 211 | |
| 212 | auto cmd = |
| 213 | std::span<const unsigned char>(payload.begin(), payload.end()); |
| 214 | auto resp = |
| 215 | interface.sendOemCommand(packageInt, channelInt, operationInt, cmd); |
| 216 | if (!resp) |
| 217 | { |
| 218 | return EXIT_FAILURE; |
| 219 | } |
| 220 | lg2::debug("Response {DATA_LEN} bytes: {DATA}", "DATA_LEN", |
| 221 | resp->size(), "DATA", toHexStr(*resp)); |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 222 | } |
| 223 | else if ((options)["set"] == "true") |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 224 | { |
Gunnar Mills | 6af6144 | 2018-04-08 14:50:06 -0500 | [diff] [blame] | 225 | // Can not perform set operation without package. |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 226 | if (packageInt == DEFAULT_VALUE) |
| 227 | { |
| 228 | exitWithError("Package not specified.", argv); |
| 229 | } |
Jeremy Kerr | bc22f81 | 2024-07-29 17:43:35 +0800 | [diff] [blame] | 230 | return interface.setChannel(packageInt, channelInt); |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 231 | } |
| 232 | else if ((options)["info"] == "true") |
| 233 | { |
Jeremy Kerr | 7f7c085 | 2024-08-08 11:32:55 +0800 | [diff] [blame] | 234 | auto info = interface.getInfo(packageInt); |
| 235 | if (!info) |
| 236 | { |
| 237 | return EXIT_FAILURE; |
| 238 | } |
| 239 | printInfo(*info); |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 240 | } |
| 241 | else if ((options)["clear"] == "true") |
| 242 | { |
Jeremy Kerr | bc22f81 | 2024-07-29 17:43:35 +0800 | [diff] [blame] | 243 | return interface.clearInterface(); |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 244 | } |
Johnathan Mantey | 5a45606 | 2024-02-15 08:45:08 -0800 | [diff] [blame] | 245 | else if (!(options)["pmask"].empty()) |
| 246 | { |
| 247 | unsigned int mask{}; |
| 248 | try |
| 249 | { |
| 250 | size_t lastChar{}; |
| 251 | mask = std::stoul((options)["pmask"], &lastChar, 0); |
| 252 | if (lastChar < (options["pmask"].size())) |
| 253 | { |
| 254 | exitWithError("Package mask value is not valid", argv); |
| 255 | } |
| 256 | } |
| 257 | catch (const std::exception& e) |
| 258 | { |
| 259 | exitWithError("Package mask value is not valid", argv); |
| 260 | } |
Jeremy Kerr | bc22f81 | 2024-07-29 17:43:35 +0800 | [diff] [blame] | 261 | return interface.setPackageMask(mask); |
Johnathan Mantey | 5a45606 | 2024-02-15 08:45:08 -0800 | [diff] [blame] | 262 | } |
| 263 | else if (!(options)["cmask"].empty()) |
| 264 | { |
| 265 | if (packageInt == DEFAULT_VALUE) |
| 266 | { |
| 267 | exitWithError("Package is not specified", argv); |
| 268 | } |
| 269 | unsigned int mask{}; |
| 270 | try |
| 271 | { |
| 272 | size_t lastChar{}; |
| 273 | mask = stoul((options)["cmask"], &lastChar, 0); |
| 274 | if (lastChar < (options["cmask"].size())) |
| 275 | { |
| 276 | exitWithError("Channel mask value is not valid", argv); |
| 277 | } |
| 278 | } |
| 279 | catch (const std::exception& e) |
| 280 | { |
| 281 | exitWithError("Channel mask value is not valid", argv); |
| 282 | } |
Jeremy Kerr | bc22f81 | 2024-07-29 17:43:35 +0800 | [diff] [blame] | 283 | return interface.setChannelMask(packageInt, mask); |
Johnathan Mantey | 5a45606 | 2024-02-15 08:45:08 -0800 | [diff] [blame] | 284 | } |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 285 | else |
| 286 | { |
| 287 | exitWithError("No Command specified", argv); |
| 288 | } |
Ratan Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 289 | return 0; |
| 290 | } |