blob: 3f7fc64303bc07025645b83c684adb5adabde2ea [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>
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080023#include <string>
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080024#include <vector>
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080025
26namespace google
27{
28namespace ipmi
29{
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080030
31namespace
32{
33
34// TODO (jaghu) : Add a call to get getChannelMaxTransferSize.
35#ifndef MAX_IPMI_BUFFER
36#define MAX_IPMI_BUFFER 64
37#endif
38
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080039} // namespace
40
41struct GetEntityNameRequest
42{
43 uint8_t subcommand;
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
48struct GetEntityNameReply
49{
50 uint8_t subcommand;
Patrick Venture45fad1b2019-03-18 16:52:14 -070051 uint8_t entityNameLength;
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080052} __attribute__((packed));
53
Patrick Venture45fad1b2019-03-18 16:52:14 -070054ipmi_ret_t getEntityName(const uint8_t* reqBuf, uint8_t* replyBuf,
Patrick Venture07f85152019-03-15 21:36:56 -070055 size_t* dataLen, HandlerInterface* handler)
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080056{
57 struct GetEntityNameRequest request;
58
59 if ((*dataLen) < sizeof(request))
60 {
61 std::fprintf(stderr, "Invalid command length: %u\n",
62 static_cast<uint32_t>(*dataLen));
63 return IPMI_CC_REQ_DATA_LEN_INVALID;
64 }
65
66 std::memcpy(&request, &reqBuf[0], sizeof(request));
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080067 std::string entityName;
68 try
69 {
Patrick Venture07f85152019-03-15 21:36:56 -070070 entityName =
Patrick Venture45fad1b2019-03-18 16:52:14 -070071 handler->getEntityName(request.entityId, request.entityInstance);
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080072 }
Patrick Venture07f85152019-03-15 21:36:56 -070073 catch (const IpmiException& e)
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080074 {
Patrick Venture07f85152019-03-15 21:36:56 -070075 return e.getIpmiError();
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080076 }
77
78 int length = sizeof(struct GetEntityNameReply) + entityName.length();
79
80 // TODO (jaghu) : Add a call to get getChannelMaxTransferSize.
81 if (length > MAX_IPMI_BUFFER)
82 {
83 std::fprintf(stderr, "Response would overflow response buffer\n");
84 return IPMI_CC_INVALID;
85 }
86
87 auto reply = reinterpret_cast<struct GetEntityNameReply*>(&replyBuf[0]);
88 reply->subcommand = SysEntityName;
Patrick Venture45fad1b2019-03-18 16:52:14 -070089 reply->entityNameLength = entityName.length();
William A. Kennington III5d789732021-06-24 03:39:31 -070090 std::memcpy(reply + 1, entityName.c_str(), entityName.length());
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080091
92 (*dataLen) = length;
93 return IPMI_CC_OK;
94}
95} // namespace ipmi
96} // namespace google