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 "entity_name.hpp"
16 
17 #include "commands.hpp"
18 #include "errors.hpp"
19 #include "handler.hpp"
20 
21 #include <cstdint>
22 #include <cstring>
23 #include <ipmid/api-types.hpp>
24 #include <span>
25 #include <string>
26 #include <vector>
27 
28 namespace google
29 {
30 namespace ipmi
31 {
32 
33 namespace
34 {
35 
36 // TODO (jaghu) : Add a call to get getChannelMaxTransferSize.
37 #ifndef MAX_IPMI_BUFFER
38 #define MAX_IPMI_BUFFER 64
39 #endif
40 
41 } // namespace
42 
43 struct GetEntityNameRequest
44 {
45     uint8_t entityId;
46     uint8_t entityInstance;
47 } __attribute__((packed));
48 
49 Resp getEntityName(std::span<const uint8_t> data, HandlerInterface* handler)
50 {
51     struct GetEntityNameRequest request;
52 
53     if (data.size() < sizeof(request))
54     {
55         std::fprintf(stderr, "Invalid command length: %u\n",
56                      static_cast<uint32_t>(data.size()));
57         return ::ipmi::responseReqDataLenInvalid();
58     }
59 
60     std::memcpy(&request, data.data(), sizeof(request));
61     std::string entityName;
62     try
63     {
64         entityName =
65             handler->getEntityName(request.entityId, request.entityInstance);
66     }
67     catch (const IpmiException& e)
68     {
69         return ::ipmi::response(e.getIpmiError());
70     }
71 
72     int length = sizeof(struct GetEntityNameReply) + entityName.length();
73 
74     // TODO (jaghu) : Add a call to get getChannelMaxTransferSize.
75     if (length > MAX_IPMI_BUFFER)
76     {
77         std::fprintf(stderr, "Response would overflow response buffer\n");
78         return ::ipmi::responseInvalidCommand();
79     }
80 
81     std::vector<std::uint8_t> reply;
82     reply.reserve(entityName.length() + sizeof(struct GetEntityNameReply));
83     reply.emplace_back(entityName.length()); /* entityNameLength */
84     reply.insert(reply.end(), entityName.begin(),
85                  entityName.end()); /* entityName */
86 
87     return ::ipmi::responseSuccess(SysOEMCommands::SysEntityName, reply);
88 }
89 } // namespace ipmi
90 } // namespace google
91