1a2056e9cSWilly Tu // Copyright 2021 Google LLC
2a2056e9cSWilly Tu //
3a2056e9cSWilly Tu // Licensed under the Apache License, Version 2.0 (the "License");
4a2056e9cSWilly Tu // you may not use this file except in compliance with the License.
5a2056e9cSWilly Tu // You may obtain a copy of the License at
6a2056e9cSWilly Tu //
7a2056e9cSWilly Tu //      http://www.apache.org/licenses/LICENSE-2.0
8a2056e9cSWilly Tu //
9a2056e9cSWilly Tu // Unless required by applicable law or agreed to in writing, software
10a2056e9cSWilly Tu // distributed under the License is distributed on an "AS IS" BASIS,
11a2056e9cSWilly Tu // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12a2056e9cSWilly Tu // See the License for the specific language governing permissions and
13a2056e9cSWilly Tu // limitations under the License.
14fd0f1cf9SJaghathiswari Rankappagounder Natarajan 
15fd0f1cf9SJaghathiswari Rankappagounder Natarajan #include "entity_name.hpp"
16fd0f1cf9SJaghathiswari Rankappagounder Natarajan 
170e9aae5dSPatrick Venture #include "commands.hpp"
1807f85150SPatrick Venture #include "errors.hpp"
1907f85150SPatrick Venture #include "handler.hpp"
20fd0f1cf9SJaghathiswari Rankappagounder Natarajan 
21444b5ea4SPatrick Williams #include <ipmid/api-types.hpp>
22*8d618532SMichael Shen #include <stdplus/print.hpp>
23444b5ea4SPatrick Williams 
24fd0f1cf9SJaghathiswari Rankappagounder Natarajan #include <cstdint>
25fd0f1cf9SJaghathiswari Rankappagounder Natarajan #include <cstring>
26b4e3704cSWilly Tu #include <span>
27fd0f1cf9SJaghathiswari Rankappagounder Natarajan #include <string>
28fd0f1cf9SJaghathiswari Rankappagounder Natarajan #include <vector>
29fd0f1cf9SJaghathiswari Rankappagounder Natarajan 
30fd0f1cf9SJaghathiswari Rankappagounder Natarajan namespace google
31fd0f1cf9SJaghathiswari Rankappagounder Natarajan {
32fd0f1cf9SJaghathiswari Rankappagounder Natarajan namespace ipmi
33fd0f1cf9SJaghathiswari Rankappagounder Natarajan {
34fd0f1cf9SJaghathiswari Rankappagounder Natarajan 
35fd0f1cf9SJaghathiswari Rankappagounder Natarajan namespace
36fd0f1cf9SJaghathiswari Rankappagounder Natarajan {
37fd0f1cf9SJaghathiswari Rankappagounder Natarajan 
38fd0f1cf9SJaghathiswari Rankappagounder Natarajan // TODO (jaghu) : Add a call to get getChannelMaxTransferSize.
39fd0f1cf9SJaghathiswari Rankappagounder Natarajan #ifndef MAX_IPMI_BUFFER
40fd0f1cf9SJaghathiswari Rankappagounder Natarajan #define MAX_IPMI_BUFFER 64
41fd0f1cf9SJaghathiswari Rankappagounder Natarajan #endif
42fd0f1cf9SJaghathiswari Rankappagounder Natarajan 
43fd0f1cf9SJaghathiswari Rankappagounder Natarajan } // namespace
44fd0f1cf9SJaghathiswari Rankappagounder Natarajan 
45fd0f1cf9SJaghathiswari Rankappagounder Natarajan struct GetEntityNameRequest
46fd0f1cf9SJaghathiswari Rankappagounder Natarajan {
4745fad1bbSPatrick Venture     uint8_t entityId;
4845fad1bbSPatrick Venture     uint8_t entityInstance;
49fd0f1cf9SJaghathiswari Rankappagounder Natarajan } __attribute__((packed));
50fd0f1cf9SJaghathiswari Rankappagounder Natarajan 
getEntityName(std::span<const uint8_t> data,HandlerInterface * handler)51b4e3704cSWilly Tu Resp getEntityName(std::span<const uint8_t> data, HandlerInterface* handler)
52fd0f1cf9SJaghathiswari Rankappagounder Natarajan {
53fd0f1cf9SJaghathiswari Rankappagounder Natarajan     struct GetEntityNameRequest request;
54fd0f1cf9SJaghathiswari Rankappagounder Natarajan 
55ff3cd8e9SWilly Tu     if (data.size() < sizeof(request))
56fd0f1cf9SJaghathiswari Rankappagounder Natarajan     {
57*8d618532SMichael Shen         stdplus::print(stderr, "Invalid command length: {}\n", data.size());
58ff3cd8e9SWilly Tu         return ::ipmi::responseReqDataLenInvalid();
59fd0f1cf9SJaghathiswari Rankappagounder Natarajan     }
60fd0f1cf9SJaghathiswari Rankappagounder Natarajan 
61ff3cd8e9SWilly Tu     std::memcpy(&request, data.data(), sizeof(request));
62fd0f1cf9SJaghathiswari Rankappagounder Natarajan     std::string entityName;
63fd0f1cf9SJaghathiswari Rankappagounder Natarajan     try
64fd0f1cf9SJaghathiswari Rankappagounder Natarajan     {
65444b5ea4SPatrick Williams         entityName = handler->getEntityName(request.entityId,
66444b5ea4SPatrick Williams                                             request.entityInstance);
67fd0f1cf9SJaghathiswari Rankappagounder Natarajan     }
6807f85150SPatrick Venture     catch (const IpmiException& e)
69fd0f1cf9SJaghathiswari Rankappagounder Natarajan     {
70ff3cd8e9SWilly Tu         return ::ipmi::response(e.getIpmiError());
71fd0f1cf9SJaghathiswari Rankappagounder Natarajan     }
72fd0f1cf9SJaghathiswari Rankappagounder Natarajan 
73fd0f1cf9SJaghathiswari Rankappagounder Natarajan     int length = sizeof(struct GetEntityNameReply) + entityName.length();
74fd0f1cf9SJaghathiswari Rankappagounder Natarajan 
75fd0f1cf9SJaghathiswari Rankappagounder Natarajan     // TODO (jaghu) : Add a call to get getChannelMaxTransferSize.
76fd0f1cf9SJaghathiswari Rankappagounder Natarajan     if (length > MAX_IPMI_BUFFER)
77fd0f1cf9SJaghathiswari Rankappagounder Natarajan     {
78*8d618532SMichael Shen         stdplus::print(stderr, "Response would overflow response buffer\n");
79ff3cd8e9SWilly Tu         return ::ipmi::responseInvalidCommand();
80fd0f1cf9SJaghathiswari Rankappagounder Natarajan     }
81fd0f1cf9SJaghathiswari Rankappagounder Natarajan 
82ff3cd8e9SWilly Tu     std::vector<std::uint8_t> reply;
83ff3cd8e9SWilly Tu     reply.reserve(entityName.length() + sizeof(struct GetEntityNameReply));
84ff3cd8e9SWilly Tu     reply.emplace_back(entityName.length()); /* entityNameLength */
85ff3cd8e9SWilly Tu     reply.insert(reply.end(), entityName.begin(),
86ff3cd8e9SWilly Tu                  entityName.end());          /* entityName */
87fd0f1cf9SJaghathiswari Rankappagounder Natarajan 
88ff3cd8e9SWilly Tu     return ::ipmi::responseSuccess(SysOEMCommands::SysEntityName, reply);
89fd0f1cf9SJaghathiswari Rankappagounder Natarajan }
90fd0f1cf9SJaghathiswari Rankappagounder Natarajan } // namespace ipmi
91fd0f1cf9SJaghathiswari Rankappagounder Natarajan } // namespace google
92