blob: 4946cd6bb91ac2c715d389b35a369bec612e6458 [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
20#include <cstddef>
21#include <cstdio>
22#include <cstring>
Willy Tuff3cd8e2021-09-14 22:49:55 -070023#include <ipmid/api-types.hpp>
William A. Kennington III29f35bc2020-11-03 23:30:31 -080024#include <optional>
Willy Tub4e37042021-10-12 17:12:30 -070025#include <span>
William A. Kennington III29f35bc2020-11-03 23:30:31 -080026#include <string>
Willy Tuff3cd8e2021-09-14 22:49:55 -070027#include <vector>
William A. Kennington III29f35bc2020-11-03 23:30:31 -080028
29namespace google
30{
31namespace ipmi
32{
33
Willy Tub4e37042021-10-12 17:12:30 -070034Resp getMachineName(std::span<const uint8_t>, HandlerInterface* handler)
William A. Kennington III29f35bc2020-11-03 23:30:31 -080035{
William A. Kennington III29f35bc2020-11-03 23:30:31 -080036 static std::optional<std::string> machineName;
37 if (!machineName)
38 {
39 try
40 {
41 machineName = handler->getMachineName();
42 }
43 catch (const IpmiException& e)
44 {
Willy Tuff3cd8e2021-09-14 22:49:55 -070045 return ::ipmi::response(e.getIpmiError());
William A. Kennington III29f35bc2020-11-03 23:30:31 -080046 }
47 }
48
Willy Tuff3cd8e2021-09-14 22:49:55 -070049 size_t len = sizeof(struct GetMachineNameReply) + machineName->size();
William A. Kennington III29f35bc2020-11-03 23:30:31 -080050 if (len > MAX_IPMI_BUFFER)
51 {
52 std::fprintf(stderr, "Response would overflow response buffer\n");
Willy Tuff3cd8e2021-09-14 22:49:55 -070053 return ::ipmi::responseInvalidCommand();
William A. Kennington III29f35bc2020-11-03 23:30:31 -080054 }
Willy Tuff3cd8e2021-09-14 22:49:55 -070055
56 std::vector<std::uint8_t> reply;
57 reply.reserve(len);
58 reply.emplace_back(machineName->size()); /* machineNameLength */
59 reply.insert(reply.end(), machineName->begin(),
Willy Tu1209ccc2023-05-19 00:49:51 -070060 machineName->end()); /* machineName */
Willy Tuff3cd8e2021-09-14 22:49:55 -070061
62 return ::ipmi::responseSuccess(SysOEMCommands::SysMachineName, reply);
William A. Kennington III29f35bc2020-11-03 23:30:31 -080063}
64
65} // namespace ipmi
66} // namespace google