Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "entity_name.hpp" |
| 18 | |
Patrick Venture | 07f8515 | 2019-03-15 21:36:56 -0700 | [diff] [blame] | 19 | #include "errors.hpp" |
| 20 | #include "handler.hpp" |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 21 | #include "main.hpp" |
| 22 | |
| 23 | #include <cstdint> |
| 24 | #include <cstring> |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 25 | #include <string> |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 26 | #include <vector> |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 27 | |
| 28 | namespace google |
| 29 | { |
| 30 | namespace ipmi |
| 31 | { |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 32 | |
| 33 | namespace |
| 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 Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 41 | } // namespace |
| 42 | |
| 43 | struct GetEntityNameRequest |
| 44 | { |
| 45 | uint8_t subcommand; |
Patrick Venture | 45fad1b | 2019-03-18 16:52:14 -0700 | [diff] [blame] | 46 | uint8_t entityId; |
| 47 | uint8_t entityInstance; |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 48 | } __attribute__((packed)); |
| 49 | |
| 50 | struct GetEntityNameReply |
| 51 | { |
| 52 | uint8_t subcommand; |
Patrick Venture | 45fad1b | 2019-03-18 16:52:14 -0700 | [diff] [blame] | 53 | uint8_t entityNameLength; |
| 54 | uint8_t entityName[0]; |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 55 | } __attribute__((packed)); |
| 56 | |
Patrick Venture | 45fad1b | 2019-03-18 16:52:14 -0700 | [diff] [blame] | 57 | ipmi_ret_t getEntityName(const uint8_t* reqBuf, uint8_t* replyBuf, |
Patrick Venture | 07f8515 | 2019-03-15 21:36:56 -0700 | [diff] [blame] | 58 | size_t* dataLen, HandlerInterface* handler) |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 59 | { |
| 60 | struct GetEntityNameRequest request; |
| 61 | |
| 62 | if ((*dataLen) < sizeof(request)) |
| 63 | { |
| 64 | std::fprintf(stderr, "Invalid command length: %u\n", |
| 65 | static_cast<uint32_t>(*dataLen)); |
| 66 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
| 67 | } |
| 68 | |
| 69 | std::memcpy(&request, &reqBuf[0], sizeof(request)); |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 70 | std::string entityName; |
| 71 | try |
| 72 | { |
Patrick Venture | 07f8515 | 2019-03-15 21:36:56 -0700 | [diff] [blame] | 73 | entityName = |
Patrick Venture | 45fad1b | 2019-03-18 16:52:14 -0700 | [diff] [blame] | 74 | handler->getEntityName(request.entityId, request.entityInstance); |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 75 | } |
Patrick Venture | 07f8515 | 2019-03-15 21:36:56 -0700 | [diff] [blame] | 76 | catch (const IpmiException& e) |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 77 | { |
Patrick Venture | 07f8515 | 2019-03-15 21:36:56 -0700 | [diff] [blame] | 78 | return e.getIpmiError(); |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | int length = sizeof(struct GetEntityNameReply) + entityName.length(); |
| 82 | |
| 83 | // TODO (jaghu) : Add a call to get getChannelMaxTransferSize. |
| 84 | if (length > MAX_IPMI_BUFFER) |
| 85 | { |
| 86 | std::fprintf(stderr, "Response would overflow response buffer\n"); |
| 87 | return IPMI_CC_INVALID; |
| 88 | } |
| 89 | |
| 90 | auto reply = reinterpret_cast<struct GetEntityNameReply*>(&replyBuf[0]); |
| 91 | reply->subcommand = SysEntityName; |
Patrick Venture | 45fad1b | 2019-03-18 16:52:14 -0700 | [diff] [blame] | 92 | reply->entityNameLength = entityName.length(); |
| 93 | std::memcpy(reply->entityName, entityName.c_str(), entityName.length()); |
Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 94 | |
| 95 | (*dataLen) = length; |
| 96 | return IPMI_CC_OK; |
| 97 | } |
| 98 | } // namespace ipmi |
| 99 | } // namespace google |