Willy Tu | a2056e9 | 2021-10-10 13:36:16 -0700 | [diff] [blame] | 1 | // 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 Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 14 | |
| 15 | #include "entity_name.hpp" |
| 16 | |
Patrick Venture | 0e9aae5 | 2020-08-13 13:07:09 -0700 | [diff] [blame] | 17 | #include "commands.hpp" |
Patrick Venture | 07f8515 | 2019-03-15 21:36:56 -0700 | [diff] [blame] | 18 | #include "errors.hpp" |
| 19 | #include "handler.hpp" |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 20 | |
Patrick Williams | 444b5ea | 2023-05-19 13:56:42 -0500 | [diff] [blame] | 21 | #include <ipmid/api-types.hpp> |
Michael Shen | 8d61853 | 2023-10-25 09:14:07 +0000 | [diff] [blame] | 22 | #include <stdplus/print.hpp> |
Patrick Williams | 444b5ea | 2023-05-19 13:56:42 -0500 | [diff] [blame] | 23 | |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 24 | #include <cstdint> |
| 25 | #include <cstring> |
Willy Tu | b4e3704 | 2021-10-12 17:12:30 -0700 | [diff] [blame] | 26 | #include <span> |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 27 | #include <string> |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 28 | #include <vector> |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 29 | |
| 30 | namespace google |
| 31 | { |
| 32 | namespace ipmi |
| 33 | { |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 34 | |
| 35 | namespace |
| 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 Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 43 | } // namespace |
| 44 | |
| 45 | struct GetEntityNameRequest |
| 46 | { |
Patrick Venture | 45fad1b | 2019-03-18 16:52:14 -0700 | [diff] [blame] | 47 | uint8_t entityId; |
| 48 | uint8_t entityInstance; |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 49 | } __attribute__((packed)); |
| 50 | |
Willy Tu | b4e3704 | 2021-10-12 17:12:30 -0700 | [diff] [blame] | 51 | Resp getEntityName(std::span<const uint8_t> data, HandlerInterface* handler) |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 52 | { |
| 53 | struct GetEntityNameRequest request; |
| 54 | |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 55 | if (data.size() < sizeof(request)) |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 56 | { |
Michael Shen | 8d61853 | 2023-10-25 09:14:07 +0000 | [diff] [blame] | 57 | stdplus::print(stderr, "Invalid command length: {}\n", data.size()); |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 58 | return ::ipmi::responseReqDataLenInvalid(); |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 61 | std::memcpy(&request, data.data(), sizeof(request)); |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 62 | std::string entityName; |
| 63 | try |
| 64 | { |
Patrick Williams | 8c0094e | 2024-08-16 15:22:37 -0400 | [diff] [blame] | 65 | entityName = |
| 66 | handler->getEntityName(request.entityId, request.entityInstance); |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 67 | } |
Patrick Venture | 07f8515 | 2019-03-15 21:36:56 -0700 | [diff] [blame] | 68 | catch (const IpmiException& e) |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 69 | { |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 70 | return ::ipmi::response(e.getIpmiError()); |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 71 | } |
| 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 Shen | 8d61853 | 2023-10-25 09:14:07 +0000 | [diff] [blame] | 78 | stdplus::print(stderr, "Response would overflow response buffer\n"); |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 79 | return ::ipmi::responseInvalidCommand(); |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 82 | 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 Tu | 1209ccc | 2023-05-19 00:49:51 -0700 | [diff] [blame] | 86 | entityName.end()); /* entityName */ |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 87 | |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 88 | return ::ipmi::responseSuccess(SysOEMCommands::SysEntityName, reply); |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 89 | } |
| 90 | } // namespace ipmi |
| 91 | } // namespace google |