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 | |
| 21 | #include <cstdint> |
| 22 | #include <cstring> |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 23 | #include <ipmid/api-types.hpp> |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 24 | #include <string> |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 25 | #include <vector> |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 26 | |
| 27 | namespace google |
| 28 | { |
| 29 | namespace ipmi |
| 30 | { |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 31 | |
| 32 | namespace |
| 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 Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 40 | } // namespace |
| 41 | |
| 42 | struct GetEntityNameRequest |
| 43 | { |
Patrick Venture | 45fad1b | 2019-03-18 16:52:14 -0700 | [diff] [blame] | 44 | uint8_t entityId; |
| 45 | uint8_t entityInstance; |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 46 | } __attribute__((packed)); |
| 47 | |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 48 | Resp getEntityName(const std::vector<std::uint8_t>& data, |
| 49 | HandlerInterface* handler) |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 50 | { |
| 51 | struct GetEntityNameRequest request; |
| 52 | |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 53 | if (data.size() < sizeof(request)) |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 54 | { |
| 55 | std::fprintf(stderr, "Invalid command length: %u\n", |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 56 | static_cast<uint32_t>(data.size())); |
| 57 | return ::ipmi::responseReqDataLenInvalid(); |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 60 | std::memcpy(&request, data.data(), sizeof(request)); |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 61 | std::string entityName; |
| 62 | try |
| 63 | { |
Patrick Venture | 07f8515 | 2019-03-15 21:36:56 -0700 | [diff] [blame] | 64 | entityName = |
Patrick Venture | 45fad1b | 2019-03-18 16:52:14 -0700 | [diff] [blame] | 65 | handler->getEntityName(request.entityId, request.entityInstance); |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 66 | } |
Patrick Venture | 07f8515 | 2019-03-15 21:36:56 -0700 | [diff] [blame] | 67 | catch (const IpmiException& e) |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 68 | { |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 69 | return ::ipmi::response(e.getIpmiError()); |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 70 | } |
| 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 Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 78 | return ::ipmi::responseInvalidCommand(); |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 81 | 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 Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 86 | |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 87 | return ::ipmi::responseSuccess(SysOEMCommands::SysEntityName, reply); |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 88 | } |
| 89 | } // namespace ipmi |
| 90 | } // namespace google |