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