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 | |
| 20 | #include <cstddef> |
| 21 | #include <cstdio> |
| 22 | #include <cstring> |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 23 | #include <ipmid/api-types.hpp> |
William A. Kennington III | 29f35bc | 2020-11-03 23:30:31 -0800 | [diff] [blame] | 24 | #include <optional> |
| 25 | #include <string> |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 26 | #include <vector> |
William A. Kennington III | 29f35bc | 2020-11-03 23:30:31 -0800 | [diff] [blame] | 27 | |
| 28 | namespace google |
| 29 | { |
| 30 | namespace ipmi |
| 31 | { |
| 32 | |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 33 | Resp getMachineName(const std::vector<std::uint8_t>&, HandlerInterface* handler) |
William A. Kennington III | 29f35bc | 2020-11-03 23:30:31 -0800 | [diff] [blame] | 34 | { |
William A. Kennington III | 29f35bc | 2020-11-03 23:30:31 -0800 | [diff] [blame] | 35 | static std::optional<std::string> machineName; |
| 36 | if (!machineName) |
| 37 | { |
| 38 | try |
| 39 | { |
| 40 | machineName = handler->getMachineName(); |
| 41 | } |
| 42 | catch (const IpmiException& e) |
| 43 | { |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 44 | return ::ipmi::response(e.getIpmiError()); |
William A. Kennington III | 29f35bc | 2020-11-03 23:30:31 -0800 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 48 | size_t len = sizeof(struct GetMachineNameReply) + machineName->size(); |
William A. Kennington III | 29f35bc | 2020-11-03 23:30:31 -0800 | [diff] [blame] | 49 | if (len > MAX_IPMI_BUFFER) |
| 50 | { |
| 51 | std::fprintf(stderr, "Response would overflow response buffer\n"); |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 52 | return ::ipmi::responseInvalidCommand(); |
William A. Kennington III | 29f35bc | 2020-11-03 23:30:31 -0800 | [diff] [blame] | 53 | } |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 54 | |
| 55 | std::vector<std::uint8_t> reply; |
| 56 | reply.reserve(len); |
| 57 | reply.emplace_back(machineName->size()); /* machineNameLength */ |
| 58 | reply.insert(reply.end(), machineName->begin(), |
| 59 | machineName->end()); /* machineName */ |
| 60 | |
| 61 | return ::ipmi::responseSuccess(SysOEMCommands::SysMachineName, reply); |
William A. Kennington III | 29f35bc | 2020-11-03 23:30:31 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | } // namespace ipmi |
| 65 | } // namespace google |