blob: 0c76106ac26971ac244af206a9e3f7b3ee463e45 [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>
21
Patrick Venture4d49ae62018-09-17 11:35:32 -070022#include <cstdint>
Patrick Venturece07ee02018-09-19 18:09:32 -070023#include <cstring>
Willy Tub4e37042021-10-12 17:12:30 -070024#include <span>
Patrick Venture4d49ae62018-09-17 11:35:32 -070025#include <string>
Patrick Venturef085d912019-03-15 08:50:00 -070026#include <tuple>
Willy Tuff3cd8e2021-09-14 22:49:55 -070027#include <vector>
Patrick Venture4d49ae62018-09-17 11:35:32 -070028
29namespace google
30{
31namespace ipmi
32{
33
Patrick Venture4d49ae62018-09-17 11:35:32 -070034// TOOD(venture): The ipmid.h has this macro, which is a header we
35// can't normally access.
36#ifndef MAX_IPMI_BUFFER
37#define MAX_IPMI_BUFFER 64
38#endif
39
Willy Tub4e37042021-10-12 17:12:30 -070040Resp getEthDevice(std::span<const uint8_t> data,
Willy Tuff3cd8e2021-09-14 22:49:55 -070041 const HandlerInterface* handler)
Patrick Venture4d49ae62018-09-17 11:35:32 -070042{
Willy Tuff3cd8e2021-09-14 22:49:55 -070043 std::tuple<std::uint8_t, std::string> details =
44 handler->getEthDetails(std::string(data.begin(), data.end()));
Patrick Venturef085d912019-03-15 08:50:00 -070045
46 std::string device = std::get<1>(details);
Patrick Venture4d49ae62018-09-17 11:35:32 -070047 if (device.length() == 0)
48 {
Patrick Venturece07ee02018-09-19 18:09:32 -070049 std::fprintf(stderr, "Invalid eth string\n");
Willy Tuff3cd8e2021-09-14 22:49:55 -070050 return ::ipmi::responseReqDataLenInvalid();
Patrick Venture4d49ae62018-09-17 11:35:32 -070051 }
52
53 if ((sizeof(struct EthDeviceReply) + device.length()) > MAX_IPMI_BUFFER)
54 {
Patrick Venturece07ee02018-09-19 18:09:32 -070055 std::fprintf(stderr, "Response would overflow response buffer\n");
Willy Tuff3cd8e2021-09-14 22:49:55 -070056 return ::ipmi::responseRetBytesUnavailable();
Patrick Venture4d49ae62018-09-17 11:35:32 -070057 }
58
Willy Tuff3cd8e2021-09-14 22:49:55 -070059 std::vector<std::uint8_t> reply;
60 reply.reserve(device.length() + sizeof(struct EthDeviceReply));
61 reply.emplace_back(std::get<0>(details)); /* channel */
62 reply.emplace_back(device.length()); /* ifNameLength */
63 reply.insert(reply.end(), device.begin(), device.end()); /* name */
Patrick Venture4d49ae62018-09-17 11:35:32 -070064
Willy Tuff3cd8e2021-09-14 22:49:55 -070065 return ::ipmi::responseSuccess(SysOEMCommands::SysGetEthDevice, reply);
Patrick Venture4d49ae62018-09-17 11:35:32 -070066}
67
68} // namespace ipmi
69} // namespace google