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