Willy Tu | a2056e9 | 2021-10-10 13:36:16 -0700 | [diff] [blame] | 1 | // Copyright 2021 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
William A. Kennington III | 29f35bc | 2020-11-03 23:30:31 -0800 | [diff] [blame] | 14 | |
| 15 | #include "machine_name.hpp" |
| 16 | |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 17 | #include "commands.hpp" |
William A. Kennington III | 29f35bc | 2020-11-03 23:30:31 -0800 | [diff] [blame] | 18 | #include "errors.hpp" |
| 19 | |
Patrick Williams | 444b5ea | 2023-05-19 13:56:42 -0500 | [diff] [blame] | 20 | #include <ipmid/api-types.hpp> |
Michael Shen | 8d61853 | 2023-10-25 09:14:07 +0000 | [diff] [blame] | 21 | #include <stdplus/print.hpp> |
Patrick Williams | 444b5ea | 2023-05-19 13:56:42 -0500 | [diff] [blame] | 22 | |
William A. Kennington III | 29f35bc | 2020-11-03 23:30:31 -0800 | [diff] [blame] | 23 | #include <cstddef> |
| 24 | #include <cstdio> |
| 25 | #include <cstring> |
| 26 | #include <optional> |
Willy Tu | b4e3704 | 2021-10-12 17:12:30 -0700 | [diff] [blame] | 27 | #include <span> |
William A. Kennington III | 29f35bc | 2020-11-03 23:30:31 -0800 | [diff] [blame] | 28 | #include <string> |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 29 | #include <vector> |
William A. Kennington III | 29f35bc | 2020-11-03 23:30:31 -0800 | [diff] [blame] | 30 | |
| 31 | namespace google |
| 32 | { |
| 33 | namespace ipmi |
| 34 | { |
| 35 | |
Willy Tu | b4e3704 | 2021-10-12 17:12:30 -0700 | [diff] [blame] | 36 | Resp getMachineName(std::span<const uint8_t>, HandlerInterface* handler) |
William A. Kennington III | 29f35bc | 2020-11-03 23:30:31 -0800 | [diff] [blame] | 37 | { |
William A. Kennington III | 29f35bc | 2020-11-03 23:30:31 -0800 | [diff] [blame] | 38 | static std::optional<std::string> machineName; |
| 39 | if (!machineName) |
| 40 | { |
| 41 | try |
| 42 | { |
| 43 | machineName = handler->getMachineName(); |
| 44 | } |
| 45 | catch (const IpmiException& e) |
| 46 | { |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 47 | return ::ipmi::response(e.getIpmiError()); |
William A. Kennington III | 29f35bc | 2020-11-03 23:30:31 -0800 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 51 | size_t len = sizeof(struct GetMachineNameReply) + machineName->size(); |
William A. Kennington III | 29f35bc | 2020-11-03 23:30:31 -0800 | [diff] [blame] | 52 | if (len > MAX_IPMI_BUFFER) |
| 53 | { |
Michael Shen | 8d61853 | 2023-10-25 09:14:07 +0000 | [diff] [blame] | 54 | stdplus::print(stderr, "Response would overflow response buffer\n"); |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 55 | return ::ipmi::responseInvalidCommand(); |
William A. Kennington III | 29f35bc | 2020-11-03 23:30:31 -0800 | [diff] [blame] | 56 | } |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 57 | |
| 58 | std::vector<std::uint8_t> reply; |
| 59 | reply.reserve(len); |
| 60 | reply.emplace_back(machineName->size()); /* machineNameLength */ |
| 61 | reply.insert(reply.end(), machineName->begin(), |
Willy Tu | 1209ccc | 2023-05-19 00:49:51 -0700 | [diff] [blame] | 62 | machineName->end()); /* machineName */ |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 63 | |
| 64 | return ::ipmi::responseSuccess(SysOEMCommands::SysMachineName, reply); |
William A. Kennington III | 29f35bc | 2020-11-03 23:30:31 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | } // namespace ipmi |
| 68 | } // namespace google |