xref: /openbmc/google-ipmi-sys/eth.cpp (revision 11a5bb6bec507a21f7f0228f00cf0405b4d2ea5e)
1a2056e9cSWilly Tu // Copyright 2021 Google LLC
2a2056e9cSWilly Tu //
3a2056e9cSWilly Tu // Licensed under the Apache License, Version 2.0 (the "License");
4a2056e9cSWilly Tu // you may not use this file except in compliance with the License.
5a2056e9cSWilly Tu // You may obtain a copy of the License at
6a2056e9cSWilly Tu //
7a2056e9cSWilly Tu //      http://www.apache.org/licenses/LICENSE-2.0
8a2056e9cSWilly Tu //
9a2056e9cSWilly Tu // Unless required by applicable law or agreed to in writing, software
10a2056e9cSWilly Tu // distributed under the License is distributed on an "AS IS" BASIS,
11a2056e9cSWilly Tu // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12a2056e9cSWilly Tu // See the License for the specific language governing permissions and
13a2056e9cSWilly Tu // limitations under the License.
144d49ae65SPatrick Venture 
154d49ae65SPatrick Venture #include "eth.hpp"
164d49ae65SPatrick Venture 
170e9aae5dSPatrick Venture #include "commands.hpp"
18f085d91dSPatrick Venture #include "handler.hpp"
194d49ae65SPatrick Venture 
20444b5ea4SPatrick Williams #include <ipmid/api-types.hpp>
218d618532SMichael Shen #include <stdplus/print.hpp>
22444b5ea4SPatrick Williams 
234d49ae65SPatrick Venture #include <cstdint>
24ce07ee0aSPatrick Venture #include <cstring>
25b4e3704cSWilly Tu #include <span>
264d49ae65SPatrick Venture #include <string>
27f085d91dSPatrick Venture #include <tuple>
28ff3cd8e9SWilly Tu #include <vector>
294d49ae65SPatrick Venture 
304d49ae65SPatrick Venture namespace google
314d49ae65SPatrick Venture {
324d49ae65SPatrick Venture namespace ipmi
334d49ae65SPatrick Venture {
344d49ae65SPatrick Venture 
35*11a5bb6bSManojkiran Eda // TODO(venture): The ipmid.h has this macro, which is a header we
364d49ae65SPatrick Venture // can't normally access.
374d49ae65SPatrick Venture #ifndef MAX_IPMI_BUFFER
384d49ae65SPatrick Venture #define MAX_IPMI_BUFFER 64
394d49ae65SPatrick Venture #endif
404d49ae65SPatrick Venture 
getEthDevice(std::span<const uint8_t> data,const HandlerInterface * handler)41b4e3704cSWilly Tu Resp getEthDevice(std::span<const uint8_t> data,
42ff3cd8e9SWilly Tu                   const HandlerInterface* handler)
434d49ae65SPatrick Venture {
44ff3cd8e9SWilly Tu     std::tuple<std::uint8_t, std::string> details =
45ff3cd8e9SWilly Tu         handler->getEthDetails(std::string(data.begin(), data.end()));
46f085d91dSPatrick Venture 
47f085d91dSPatrick Venture     std::string device = std::get<1>(details);
484d49ae65SPatrick Venture     if (device.length() == 0)
494d49ae65SPatrick Venture     {
508d618532SMichael Shen         stdplus::print(stderr, "Invalid eth string\n");
51ff3cd8e9SWilly Tu         return ::ipmi::responseReqDataLenInvalid();
524d49ae65SPatrick Venture     }
534d49ae65SPatrick Venture 
544d49ae65SPatrick Venture     if ((sizeof(struct EthDeviceReply) + device.length()) > MAX_IPMI_BUFFER)
554d49ae65SPatrick Venture     {
568d618532SMichael Shen         stdplus::print(stderr, "Response would overflow response buffer\n");
57ff3cd8e9SWilly Tu         return ::ipmi::responseRetBytesUnavailable();
584d49ae65SPatrick Venture     }
594d49ae65SPatrick Venture 
60ff3cd8e9SWilly Tu     std::vector<std::uint8_t> reply;
61ff3cd8e9SWilly Tu     reply.reserve(device.length() + sizeof(struct EthDeviceReply));
62ff3cd8e9SWilly Tu     reply.emplace_back(std::get<0>(details));                /* channel */
63ff3cd8e9SWilly Tu     reply.emplace_back(device.length());                     /* ifNameLength */
64ff3cd8e9SWilly Tu     reply.insert(reply.end(), device.begin(), device.end()); /* name */
654d49ae65SPatrick Venture 
66ff3cd8e9SWilly Tu     return ::ipmi::responseSuccess(SysOEMCommands::SysGetEthDevice, reply);
674d49ae65SPatrick Venture }
684d49ae65SPatrick Venture 
694d49ae65SPatrick Venture } // namespace ipmi
704d49ae65SPatrick Venture } // namespace google
71