blob: 854b1dce869f85a4eaa12675a962c4a103d4556d [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>
Willy Tub4e37042021-10-12 17:12:30 -070024#include <span>
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080025#include <string>
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080026#include <vector>
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080027
28namespace google
29{
30namespace ipmi
31{
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080032
33namespace
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
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080041} // namespace
42
43struct GetEntityNameRequest
44{
Patrick Venture45fad1b2019-03-18 16:52:14 -070045 uint8_t entityId;
46 uint8_t entityInstance;
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080047} __attribute__((packed));
48
Willy Tub4e37042021-10-12 17:12:30 -070049Resp getEntityName(std::span<const uint8_t> data, 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