blob: 1a68f9d7b6b87e2368ca6691f24181969f210a7e [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.
Patrick Venture4d49ae62018-09-17 11:35:32 -070014
15#include "eth.hpp"
16
Patrick Venture0e9aae52020-08-13 13:07:09 -070017#include "commands.hpp"
Patrick Venturef085d912019-03-15 08:50:00 -070018#include "handler.hpp"
Patrick Venture4d49ae62018-09-17 11:35:32 -070019
Patrick Williams444b5ea2023-05-19 13:56:42 -050020#include <ipmid/api-types.hpp>
Michael Shen8d618532023-10-25 09:14:07 +000021#include <stdplus/print.hpp>
Patrick Williams444b5ea2023-05-19 13:56:42 -050022
Patrick Venture4d49ae62018-09-17 11:35:32 -070023#include <cstdint>
Patrick Venturece07ee02018-09-19 18:09:32 -070024#include <cstring>
Willy Tub4e37042021-10-12 17:12:30 -070025#include <span>
Patrick Venture4d49ae62018-09-17 11:35:32 -070026#include <string>
Patrick Venturef085d912019-03-15 08:50:00 -070027#include <tuple>
Willy Tuff3cd8e2021-09-14 22:49:55 -070028#include <vector>
Patrick Venture4d49ae62018-09-17 11:35:32 -070029
30namespace google
31{
32namespace ipmi
33{
34
Manojkiran Eda11a5bb62024-06-17 14:52:05 +053035// TODO(venture): The ipmid.h has this macro, which is a header we
Patrick Venture4d49ae62018-09-17 11:35:32 -070036// can't normally access.
37#ifndef MAX_IPMI_BUFFER
38#define MAX_IPMI_BUFFER 64
39#endif
40
Willy Tub4e37042021-10-12 17:12:30 -070041Resp getEthDevice(std::span<const uint8_t> data,
Willy Tuff3cd8e2021-09-14 22:49:55 -070042 const HandlerInterface* handler)
Patrick Venture4d49ae62018-09-17 11:35:32 -070043{
Willy Tuff3cd8e2021-09-14 22:49:55 -070044 std::tuple<std::uint8_t, std::string> details =
45 handler->getEthDetails(std::string(data.begin(), data.end()));
Patrick Venturef085d912019-03-15 08:50:00 -070046
47 std::string device = std::get<1>(details);
Patrick Venture4d49ae62018-09-17 11:35:32 -070048 if (device.length() == 0)
49 {
Michael Shen8d618532023-10-25 09:14:07 +000050 stdplus::print(stderr, "Invalid eth string\n");
Willy Tuff3cd8e2021-09-14 22:49:55 -070051 return ::ipmi::responseReqDataLenInvalid();
Patrick Venture4d49ae62018-09-17 11:35:32 -070052 }
53
54 if ((sizeof(struct EthDeviceReply) + device.length()) > MAX_IPMI_BUFFER)
55 {
Michael Shen8d618532023-10-25 09:14:07 +000056 stdplus::print(stderr, "Response would overflow response buffer\n");
Willy Tuff3cd8e2021-09-14 22:49:55 -070057 return ::ipmi::responseRetBytesUnavailable();
Patrick Venture4d49ae62018-09-17 11:35:32 -070058 }
59
Willy Tuff3cd8e2021-09-14 22:49:55 -070060 std::vector<std::uint8_t> reply;
61 reply.reserve(device.length() + sizeof(struct EthDeviceReply));
62 reply.emplace_back(std::get<0>(details)); /* channel */
63 reply.emplace_back(device.length()); /* ifNameLength */
64 reply.insert(reply.end(), device.begin(), device.end()); /* name */
Patrick Venture4d49ae62018-09-17 11:35:32 -070065
Willy Tuff3cd8e2021-09-14 22:49:55 -070066 return ::ipmi::responseSuccess(SysOEMCommands::SysGetEthDevice, reply);
Patrick Venture4d49ae62018-09-17 11:35:32 -070067}
68
69} // namespace ipmi
70} // namespace google