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> |
Patrick Williams | 89d734b | 2023-05-10 07:50:25 -0500 | [diff] [blame] | 20 | |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 21 | #include <string> |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 22 | #include <vector> |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 23 | |
| 24 | static void exitWithError(const char* err, char** argv) |
| 25 | { |
| 26 | phosphor::network::ncsi::ArgumentParser::usage(argv); |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 27 | lg2::error("ERROR: {ERROR}", "ERROR", err); |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 28 | exit(EXIT_FAILURE); |
| 29 | } |
| 30 | |
Jeremy Kerr | 7f7c085 | 2024-08-08 11:32:55 +0800 | [diff] [blame^] | 31 | static void printInfo(phosphor::network::ncsi::InterfaceInfo& info) |
| 32 | { |
| 33 | using namespace phosphor::network::ncsi; |
| 34 | |
| 35 | for (PackageInfo& pkg : info.packages) |
| 36 | { |
| 37 | lg2::debug("Package id : {ID}", "ID", pkg.id); |
| 38 | if (pkg.forced) |
| 39 | { |
| 40 | lg2::debug(" package is forced"); |
| 41 | } |
| 42 | for (ChannelInfo& chan : pkg.channels) |
| 43 | { |
| 44 | lg2::debug(" Channel id : {ID}", "ID", chan.id); |
| 45 | if (chan.forced) |
| 46 | { |
| 47 | lg2::debug(" channel is forced"); |
| 48 | } |
| 49 | if (chan.active) |
| 50 | { |
| 51 | lg2::debug(" channel is active"); |
| 52 | } |
| 53 | |
| 54 | lg2::debug(" version {MAJOR}.{MINOR} ({STR})", "MAJOR", |
| 55 | chan.version_major, "MINOR", chan.version_minor, "STR", |
| 56 | chan.version); |
| 57 | |
| 58 | lg2::debug(" link state {LINK}", "LINK", lg2::hex, |
| 59 | chan.link_state); |
| 60 | |
| 61 | auto& vlans = chan.vlan_ids; |
| 62 | |
| 63 | if (!vlans.empty()) |
| 64 | { |
| 65 | lg2::debug(" Actve VLAN IDs:"); |
| 66 | for (uint16_t vlan : vlans) |
| 67 | { |
| 68 | lg2::debug(" VID: {VLAN_ID}", "VLAN_ID", vlan); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
Ratan Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 75 | int main(int argc, char** argv) |
| 76 | { |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 77 | using namespace phosphor::network; |
| 78 | using namespace phosphor::network::ncsi; |
| 79 | // Read arguments. |
| 80 | auto options = ArgumentParser(argc, argv); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 81 | int packageInt{}; |
| 82 | int channelInt{}; |
| 83 | int indexInt{}; |
Johnathan Mantey | 1ebea28 | 2024-02-15 10:26:06 -0800 | [diff] [blame] | 84 | int operationInt{DEFAULT_VALUE}; |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 85 | |
| 86 | // Parse out interface argument. |
| 87 | auto ifIndex = (options)["index"]; |
| 88 | try |
| 89 | { |
| 90 | indexInt = stoi(ifIndex, nullptr); |
| 91 | } |
| 92 | catch (const std::exception& e) |
| 93 | { |
| 94 | exitWithError("Interface not specified.", argv); |
| 95 | } |
| 96 | |
| 97 | if (indexInt < 0) |
| 98 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 99 | exitWithError("Interface value should be greater than equal to 0", |
| 100 | argv); |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 101 | } |
| 102 | |
Jeremy Kerr | 8d9af02 | 2024-07-26 16:47:16 +0800 | [diff] [blame] | 103 | Interface interface{indexInt}; |
| 104 | |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 105 | // Parse out package argument. |
| 106 | auto package = (options)["package"]; |
| 107 | try |
| 108 | { |
| 109 | packageInt = stoi(package, nullptr); |
| 110 | } |
| 111 | catch (const std::exception& e) |
| 112 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 113 | packageInt = DEFAULT_VALUE; |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | if (packageInt < 0) |
| 117 | { |
| 118 | packageInt = DEFAULT_VALUE; |
| 119 | } |
| 120 | |
| 121 | // Parse out channel argument. |
| 122 | auto channel = (options)["channel"]; |
| 123 | try |
| 124 | { |
| 125 | channelInt = stoi(channel, nullptr); |
| 126 | } |
| 127 | catch (const std::exception& e) |
| 128 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 129 | channelInt = DEFAULT_VALUE; |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | if (channelInt < 0) |
| 133 | { |
| 134 | channelInt = DEFAULT_VALUE; |
| 135 | } |
| 136 | |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 137 | auto payloadStr = (options)["oem-payload"]; |
| 138 | if (!payloadStr.empty()) |
| 139 | { |
| 140 | std::string byte(2, '\0'); |
| 141 | std::vector<unsigned char> payload; |
| 142 | |
| 143 | if (payloadStr.size() % 2) |
| 144 | exitWithError("Payload invalid: specify two hex digits per byte.", |
| 145 | argv); |
| 146 | |
Johnathan Mantey | 1ebea28 | 2024-02-15 10:26:06 -0800 | [diff] [blame] | 147 | // Parse the payload string (e.g. "50000001572100") to byte data |
| 148 | // The first two characters (i.e. "50") represent the Send Cmd Operation |
| 149 | // All remaining pairs, interpreted in hex radix, represent the command |
| 150 | // payload |
| 151 | int sendCmdSelect{}; |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 152 | for (unsigned int i = 1; i < payloadStr.size(); i += 2) |
| 153 | { |
| 154 | byte[0] = payloadStr[i - 1]; |
| 155 | byte[1] = payloadStr[i]; |
| 156 | |
| 157 | try |
| 158 | { |
Johnathan Mantey | 1ebea28 | 2024-02-15 10:26:06 -0800 | [diff] [blame] | 159 | sendCmdSelect = stoi(byte, nullptr, 16); |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 160 | } |
| 161 | catch (const std::exception& e) |
| 162 | { |
| 163 | exitWithError("Payload invalid.", argv); |
| 164 | } |
Johnathan Mantey | 1ebea28 | 2024-02-15 10:26:06 -0800 | [diff] [blame] | 165 | if (i == 1) |
| 166 | { |
| 167 | operationInt = sendCmdSelect; |
| 168 | } |
| 169 | else |
| 170 | { |
| 171 | payload.push_back(sendCmdSelect); |
| 172 | } |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 173 | } |
| 174 | |
Johnathan Mantey | 1ebea28 | 2024-02-15 10:26:06 -0800 | [diff] [blame] | 175 | if (operationInt == DEFAULT_VALUE) |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 176 | { |
| 177 | exitWithError("No payload specified.", argv); |
| 178 | } |
| 179 | |
| 180 | if (packageInt == DEFAULT_VALUE) |
| 181 | { |
| 182 | exitWithError("Package not specified.", argv); |
| 183 | } |
| 184 | |
Jeremy Kerr | bc22f81 | 2024-07-29 17:43:35 +0800 | [diff] [blame] | 185 | return interface.sendOemCommand( |
| 186 | packageInt, channelInt, operationInt, |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 187 | std::span<const unsigned char>(payload.begin(), payload.end())); |
| 188 | } |
| 189 | else if ((options)["set"] == "true") |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 190 | { |
Gunnar Mills | 6af6144 | 2018-04-08 14:50:06 -0500 | [diff] [blame] | 191 | // Can not perform set operation without package. |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 192 | if (packageInt == DEFAULT_VALUE) |
| 193 | { |
| 194 | exitWithError("Package not specified.", argv); |
| 195 | } |
Jeremy Kerr | bc22f81 | 2024-07-29 17:43:35 +0800 | [diff] [blame] | 196 | return interface.setChannel(packageInt, channelInt); |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 197 | } |
| 198 | else if ((options)["info"] == "true") |
| 199 | { |
Jeremy Kerr | 7f7c085 | 2024-08-08 11:32:55 +0800 | [diff] [blame^] | 200 | auto info = interface.getInfo(packageInt); |
| 201 | if (!info) |
| 202 | { |
| 203 | return EXIT_FAILURE; |
| 204 | } |
| 205 | printInfo(*info); |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 206 | } |
| 207 | else if ((options)["clear"] == "true") |
| 208 | { |
Jeremy Kerr | bc22f81 | 2024-07-29 17:43:35 +0800 | [diff] [blame] | 209 | return interface.clearInterface(); |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 210 | } |
Johnathan Mantey | 5a45606 | 2024-02-15 08:45:08 -0800 | [diff] [blame] | 211 | else if (!(options)["pmask"].empty()) |
| 212 | { |
| 213 | unsigned int mask{}; |
| 214 | try |
| 215 | { |
| 216 | size_t lastChar{}; |
| 217 | mask = std::stoul((options)["pmask"], &lastChar, 0); |
| 218 | if (lastChar < (options["pmask"].size())) |
| 219 | { |
| 220 | exitWithError("Package mask value is not valid", argv); |
| 221 | } |
| 222 | } |
| 223 | catch (const std::exception& e) |
| 224 | { |
| 225 | exitWithError("Package mask value is not valid", argv); |
| 226 | } |
Jeremy Kerr | bc22f81 | 2024-07-29 17:43:35 +0800 | [diff] [blame] | 227 | return interface.setPackageMask(mask); |
Johnathan Mantey | 5a45606 | 2024-02-15 08:45:08 -0800 | [diff] [blame] | 228 | } |
| 229 | else if (!(options)["cmask"].empty()) |
| 230 | { |
| 231 | if (packageInt == DEFAULT_VALUE) |
| 232 | { |
| 233 | exitWithError("Package is not specified", argv); |
| 234 | } |
| 235 | unsigned int mask{}; |
| 236 | try |
| 237 | { |
| 238 | size_t lastChar{}; |
| 239 | mask = stoul((options)["cmask"], &lastChar, 0); |
| 240 | if (lastChar < (options["cmask"].size())) |
| 241 | { |
| 242 | exitWithError("Channel mask value is not valid", argv); |
| 243 | } |
| 244 | } |
| 245 | catch (const std::exception& e) |
| 246 | { |
| 247 | exitWithError("Channel mask value is not valid", argv); |
| 248 | } |
Jeremy Kerr | bc22f81 | 2024-07-29 17:43:35 +0800 | [diff] [blame] | 249 | return interface.setChannelMask(packageInt, mask); |
Johnathan Mantey | 5a45606 | 2024-02-15 08:45:08 -0800 | [diff] [blame] | 250 | } |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 251 | else |
| 252 | { |
| 253 | exitWithError("No Command specified", argv); |
| 254 | } |
Ratan Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 255 | return 0; |
| 256 | } |