blob: 748aa187ccbda70aee94a37e2edd7dd8e00dd087 [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
20#include <cstdint>
Patrick Venturece07ee02018-09-19 18:09:32 -070021#include <cstring>
Willy Tuff3cd8e2021-09-14 22:49:55 -070022#include <ipmid/api-types.hpp>
Willy Tub4e37042021-10-12 17:12:30 -070023#include <span>
Patrick Venture4d49ae62018-09-17 11:35:32 -070024#include <string>
Patrick Venturef085d912019-03-15 08:50:00 -070025#include <tuple>
Willy Tuff3cd8e2021-09-14 22:49:55 -070026#include <vector>
Patrick Venture4d49ae62018-09-17 11:35:32 -070027
28namespace google
29{
30namespace ipmi
31{
32
Patrick Venture4d49ae62018-09-17 11:35:32 -070033// TOOD(venture): The ipmid.h has this macro, which is a header we
34// can't normally access.
35#ifndef MAX_IPMI_BUFFER
36#define MAX_IPMI_BUFFER 64
37#endif
38
Willy Tub4e37042021-10-12 17:12:30 -070039Resp getEthDevice(std::span<const uint8_t> data,
Willy Tuff3cd8e2021-09-14 22:49:55 -070040 const HandlerInterface* handler)
Patrick Venture4d49ae62018-09-17 11:35:32 -070041{
Willy Tuff3cd8e2021-09-14 22:49:55 -070042 std::tuple<std::uint8_t, std::string> details =
43 handler->getEthDetails(std::string(data.begin(), data.end()));
Patrick Venturef085d912019-03-15 08:50:00 -070044
45 std::string device = std::get<1>(details);
Patrick Venture4d49ae62018-09-17 11:35:32 -070046 if (device.length() == 0)
47 {
Patrick Venturece07ee02018-09-19 18:09:32 -070048 std::fprintf(stderr, "Invalid eth string\n");
Willy Tuff3cd8e2021-09-14 22:49:55 -070049 return ::ipmi::responseReqDataLenInvalid();
Patrick Venture4d49ae62018-09-17 11:35:32 -070050 }
51
52 if ((sizeof(struct EthDeviceReply) + device.length()) > MAX_IPMI_BUFFER)
53 {
Patrick Venturece07ee02018-09-19 18:09:32 -070054 std::fprintf(stderr, "Response would overflow response buffer\n");
Willy Tuff3cd8e2021-09-14 22:49:55 -070055 return ::ipmi::responseRetBytesUnavailable();
Patrick Venture4d49ae62018-09-17 11:35:32 -070056 }
57
Willy Tuff3cd8e2021-09-14 22:49:55 -070058 std::vector<std::uint8_t> reply;
59 reply.reserve(device.length() + sizeof(struct EthDeviceReply));
60 reply.emplace_back(std::get<0>(details)); /* channel */
61 reply.emplace_back(device.length()); /* ifNameLength */
62 reply.insert(reply.end(), device.begin(), device.end()); /* name */
Patrick Venture4d49ae62018-09-17 11:35:32 -070063
Willy Tuff3cd8e2021-09-14 22:49:55 -070064 return ::ipmi::responseSuccess(SysOEMCommands::SysGetEthDevice, reply);
Patrick Venture4d49ae62018-09-17 11:35:32 -070065}
66
67} // namespace ipmi
68} // namespace google