blob: c06ce5c16a375e5739c9e1372e6f36e1db487f91 [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>
21
William A. Kennington III29f35bc2020-11-03 23:30:31 -080022#include <cstddef>
23#include <cstdio>
24#include <cstring>
25#include <optional>
Willy Tub4e37042021-10-12 17:12:30 -070026#include <span>
William A. Kennington III29f35bc2020-11-03 23:30:31 -080027#include <string>
Willy Tuff3cd8e2021-09-14 22:49:55 -070028#include <vector>
William A. Kennington III29f35bc2020-11-03 23:30:31 -080029
30namespace google
31{
32namespace ipmi
33{
34
Willy Tub4e37042021-10-12 17:12:30 -070035Resp getMachineName(std::span<const uint8_t>, HandlerInterface* handler)
William A. Kennington III29f35bc2020-11-03 23:30:31 -080036{
William A. Kennington III29f35bc2020-11-03 23:30:31 -080037 static std::optional<std::string> machineName;
38 if (!machineName)
39 {
40 try
41 {
42 machineName = handler->getMachineName();
43 }
44 catch (const IpmiException& e)
45 {
Willy Tuff3cd8e2021-09-14 22:49:55 -070046 return ::ipmi::response(e.getIpmiError());
William A. Kennington III29f35bc2020-11-03 23:30:31 -080047 }
48 }
49
Willy Tuff3cd8e2021-09-14 22:49:55 -070050 size_t len = sizeof(struct GetMachineNameReply) + machineName->size();
William A. Kennington III29f35bc2020-11-03 23:30:31 -080051 if (len > MAX_IPMI_BUFFER)
52 {
53 std::fprintf(stderr, "Response would overflow response buffer\n");
Willy Tuff3cd8e2021-09-14 22:49:55 -070054 return ::ipmi::responseInvalidCommand();
William A. Kennington III29f35bc2020-11-03 23:30:31 -080055 }
Willy Tuff3cd8e2021-09-14 22:49:55 -070056
57 std::vector<std::uint8_t> reply;
58 reply.reserve(len);
59 reply.emplace_back(machineName->size()); /* machineNameLength */
60 reply.insert(reply.end(), machineName->begin(),
Willy Tu1209ccc2023-05-19 00:49:51 -070061 machineName->end()); /* machineName */
Willy Tuff3cd8e2021-09-14 22:49:55 -070062
63 return ::ipmi::responseSuccess(SysOEMCommands::SysMachineName, reply);
William A. Kennington III29f35bc2020-11-03 23:30:31 -080064}
65
66} // namespace ipmi
67} // namespace google