blob: cc2bdee673e1e75f40385792788b700ba550ccb0 [file] [log] [blame]
Willy Tua2056e92021-10-10 13:36:16 -07001// 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 III29f35bc2020-11-03 23:30:31 -080014
15#include "machine_name.hpp"
16
Willy Tuff3cd8e2021-09-14 22:49:55 -070017#include "commands.hpp"
William A. Kennington III29f35bc2020-11-03 23:30:31 -080018#include "errors.hpp"
19
Patrick Williams444b5ea2023-05-19 13:56:42 -050020#include <ipmid/api-types.hpp>
Michael Shen8d618532023-10-25 09:14:07 +000021#include <stdplus/print.hpp>
Patrick Williams444b5ea2023-05-19 13:56:42 -050022
William A. Kennington III29f35bc2020-11-03 23:30:31 -080023#include <cstddef>
24#include <cstdio>
25#include <cstring>
26#include <optional>
Willy Tub4e37042021-10-12 17:12:30 -070027#include <span>
William A. Kennington III29f35bc2020-11-03 23:30:31 -080028#include <string>
Willy Tuff3cd8e2021-09-14 22:49:55 -070029#include <vector>
William A. Kennington III29f35bc2020-11-03 23:30:31 -080030
31namespace google
32{
33namespace ipmi
34{
35
Willy Tub4e37042021-10-12 17:12:30 -070036Resp getMachineName(std::span<const uint8_t>, HandlerInterface* handler)
William A. Kennington III29f35bc2020-11-03 23:30:31 -080037{
William A. Kennington III29f35bc2020-11-03 23:30:31 -080038 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 Tuff3cd8e2021-09-14 22:49:55 -070047 return ::ipmi::response(e.getIpmiError());
William A. Kennington III29f35bc2020-11-03 23:30:31 -080048 }
49 }
50
Willy Tuff3cd8e2021-09-14 22:49:55 -070051 size_t len = sizeof(struct GetMachineNameReply) + machineName->size();
William A. Kennington III29f35bc2020-11-03 23:30:31 -080052 if (len > MAX_IPMI_BUFFER)
53 {
Michael Shen8d618532023-10-25 09:14:07 +000054 stdplus::print(stderr, "Response would overflow response buffer\n");
Willy Tuff3cd8e2021-09-14 22:49:55 -070055 return ::ipmi::responseInvalidCommand();
William A. Kennington III29f35bc2020-11-03 23:30:31 -080056 }
Willy Tuff3cd8e2021-09-14 22:49:55 -070057
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 Tu1209ccc2023-05-19 00:49:51 -070062 machineName->end()); /* machineName */
Willy Tuff3cd8e2021-09-14 22:49:55 -070063
64 return ::ipmi::responseSuccess(SysOEMCommands::SysMachineName, reply);
William A. Kennington III29f35bc2020-11-03 23:30:31 -080065}
66
67} // namespace ipmi
68} // namespace google