blob: 454a642aaf6881922dfe320033efc434881066d8 [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
Patrick Williams444b5ea2023-05-19 13:56:42 -050021#include <ipmid/api-types.hpp>
Michael Shen8d618532023-10-25 09:14:07 +000022#include <stdplus/print.hpp>
Patrick Williams444b5ea2023-05-19 13:56:42 -050023
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080024#include <cstdint>
25#include <cstring>
Willy Tub4e37042021-10-12 17:12:30 -070026#include <span>
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080027#include <string>
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080028#include <vector>
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080029
30namespace google
31{
32namespace ipmi
33{
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080034
35namespace
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
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080043} // namespace
44
45struct GetEntityNameRequest
46{
Patrick Venture45fad1b2019-03-18 16:52:14 -070047 uint8_t entityId;
48 uint8_t entityInstance;
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080049} __attribute__((packed));
50
Willy Tub4e37042021-10-12 17:12:30 -070051Resp getEntityName(std::span<const uint8_t> data, HandlerInterface* handler)
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080052{
53 struct GetEntityNameRequest request;
54
Willy Tuff3cd8e2021-09-14 22:49:55 -070055 if (data.size() < sizeof(request))
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080056 {
Michael Shen8d618532023-10-25 09:14:07 +000057 stdplus::print(stderr, "Invalid command length: {}\n", data.size());
Willy Tuff3cd8e2021-09-14 22:49:55 -070058 return ::ipmi::responseReqDataLenInvalid();
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080059 }
60
Willy Tuff3cd8e2021-09-14 22:49:55 -070061 std::memcpy(&request, data.data(), sizeof(request));
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080062 std::string entityName;
63 try
64 {
Patrick Williams8c0094e2024-08-16 15:22:37 -040065 entityName =
66 handler->getEntityName(request.entityId, request.entityInstance);
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080067 }
Patrick Venture07f85152019-03-15 21:36:56 -070068 catch (const IpmiException& e)
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080069 {
Willy Tuff3cd8e2021-09-14 22:49:55 -070070 return ::ipmi::response(e.getIpmiError());
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080071 }
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 {
Michael Shen8d618532023-10-25 09:14:07 +000078 stdplus::print(stderr, "Response would overflow response buffer\n");
Willy Tuff3cd8e2021-09-14 22:49:55 -070079 return ::ipmi::responseInvalidCommand();
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080080 }
81
Willy Tuff3cd8e2021-09-14 22:49:55 -070082 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(),
Willy Tu1209ccc2023-05-19 00:49:51 -070086 entityName.end()); /* entityName */
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080087
Willy Tuff3cd8e2021-09-14 22:49:55 -070088 return ::ipmi::responseSuccess(SysOEMCommands::SysEntityName, reply);
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080089}
90} // namespace ipmi
91} // namespace google