blob: 3702ad5682ab413d7e8dbea8206659b3d45db660 [file] [log] [blame]
Willy Tua2056e92021-10-10 13:36:16 -07001// 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.
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080014
15#include "entity_name.hpp"
16
Patrick Venture0e9aae52020-08-13 13:07:09 -070017#include "commands.hpp"
Patrick Venture07f85152019-03-15 21:36:56 -070018#include "errors.hpp"
19#include "handler.hpp"
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080020
21#include <cstdint>
22#include <cstring>
Willy Tuff3cd8e2021-09-14 22:49:55 -070023#include <ipmid/api-types.hpp>
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080024#include <string>
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080025#include <vector>
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080026
27namespace google
28{
29namespace ipmi
30{
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080031
32namespace
33{
34
35// TODO (jaghu) : Add a call to get getChannelMaxTransferSize.
36#ifndef MAX_IPMI_BUFFER
37#define MAX_IPMI_BUFFER 64
38#endif
39
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080040} // namespace
41
42struct GetEntityNameRequest
43{
Patrick Venture45fad1b2019-03-18 16:52:14 -070044 uint8_t entityId;
45 uint8_t entityInstance;
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080046} __attribute__((packed));
47
Willy Tuff3cd8e2021-09-14 22:49:55 -070048Resp getEntityName(const std::vector<std::uint8_t>& data,
49 HandlerInterface* handler)
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080050{
51 struct GetEntityNameRequest request;
52
Willy Tuff3cd8e2021-09-14 22:49:55 -070053 if (data.size() < sizeof(request))
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080054 {
55 std::fprintf(stderr, "Invalid command length: %u\n",
Willy Tuff3cd8e2021-09-14 22:49:55 -070056 static_cast<uint32_t>(data.size()));
57 return ::ipmi::responseReqDataLenInvalid();
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080058 }
59
Willy Tuff3cd8e2021-09-14 22:49:55 -070060 std::memcpy(&request, data.data(), sizeof(request));
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080061 std::string entityName;
62 try
63 {
Patrick Venture07f85152019-03-15 21:36:56 -070064 entityName =
Patrick Venture45fad1b2019-03-18 16:52:14 -070065 handler->getEntityName(request.entityId, request.entityInstance);
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080066 }
Patrick Venture07f85152019-03-15 21:36:56 -070067 catch (const IpmiException& e)
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080068 {
Willy Tuff3cd8e2021-09-14 22:49:55 -070069 return ::ipmi::response(e.getIpmiError());
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080070 }
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");
Willy Tuff3cd8e2021-09-14 22:49:55 -070078 return ::ipmi::responseInvalidCommand();
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080079 }
80
Willy Tuff3cd8e2021-09-14 22:49:55 -070081 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 */
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080086
Willy Tuff3cd8e2021-09-14 22:49:55 -070087 return ::ipmi::responseSuccess(SysOEMCommands::SysEntityName, reply);
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080088}
89} // namespace ipmi
90} // namespace google