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.
14 
15 #include "machine_name.hpp"
16 
17 #include "commands.hpp"
18 #include "errors.hpp"
19 
20 #include <cstddef>
21 #include <cstdio>
22 #include <cstring>
23 #include <ipmid/api-types.hpp>
24 #include <optional>
25 #include <span>
26 #include <string>
27 #include <vector>
28 
29 namespace google
30 {
31 namespace ipmi
32 {
33 
34 Resp getMachineName(std::span<const uint8_t>, HandlerInterface* handler)
35 {
36     static std::optional<std::string> machineName;
37     if (!machineName)
38     {
39         try
40         {
41             machineName = handler->getMachineName();
42         }
43         catch (const IpmiException& e)
44         {
45             return ::ipmi::response(e.getIpmiError());
46         }
47     }
48 
49     size_t len = sizeof(struct GetMachineNameReply) + machineName->size();
50     if (len > MAX_IPMI_BUFFER)
51     {
52         std::fprintf(stderr, "Response would overflow response buffer\n");
53         return ::ipmi::responseInvalidCommand();
54     }
55 
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(),
60                  machineName->end()); /* machineName */
61 
62     return ::ipmi::responseSuccess(SysOEMCommands::SysMachineName, reply);
63 }
64 
65 } // namespace ipmi
66 } // namespace google
67