blob: bbe1940e42333ad7b2d4a4bcb89663a02c6409ef [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>
Patrick Venture4d49ae62018-09-17 11:35:32 -070023#include <string>
Patrick Venturef085d912019-03-15 08:50:00 -070024#include <tuple>
Willy Tuff3cd8e2021-09-14 22:49:55 -070025#include <vector>
Patrick Venture4d49ae62018-09-17 11:35:32 -070026
27namespace google
28{
29namespace ipmi
30{
31
Patrick Venture4d49ae62018-09-17 11:35:32 -070032// TOOD(venture): The ipmid.h has this macro, which is a header we
33// can't normally access.
34#ifndef MAX_IPMI_BUFFER
35#define MAX_IPMI_BUFFER 64
36#endif
37
Willy Tuff3cd8e2021-09-14 22:49:55 -070038Resp getEthDevice(const std::vector<std::uint8_t>& data,
39 const HandlerInterface* handler)
Patrick Venture4d49ae62018-09-17 11:35:32 -070040{
Willy Tuff3cd8e2021-09-14 22:49:55 -070041 std::tuple<std::uint8_t, std::string> details =
42 handler->getEthDetails(std::string(data.begin(), data.end()));
Patrick Venturef085d912019-03-15 08:50:00 -070043
44 std::string device = std::get<1>(details);
Patrick Venture4d49ae62018-09-17 11:35:32 -070045 if (device.length() == 0)
46 {
Patrick Venturece07ee02018-09-19 18:09:32 -070047 std::fprintf(stderr, "Invalid eth string\n");
Willy Tuff3cd8e2021-09-14 22:49:55 -070048 return ::ipmi::responseReqDataLenInvalid();
Patrick Venture4d49ae62018-09-17 11:35:32 -070049 }
50
51 if ((sizeof(struct EthDeviceReply) + device.length()) > MAX_IPMI_BUFFER)
52 {
Patrick Venturece07ee02018-09-19 18:09:32 -070053 std::fprintf(stderr, "Response would overflow response buffer\n");
Willy Tuff3cd8e2021-09-14 22:49:55 -070054 return ::ipmi::responseRetBytesUnavailable();
Patrick Venture4d49ae62018-09-17 11:35:32 -070055 }
56
Willy Tuff3cd8e2021-09-14 22:49:55 -070057 std::vector<std::uint8_t> reply;
58 reply.reserve(device.length() + sizeof(struct EthDeviceReply));
59 reply.emplace_back(std::get<0>(details)); /* channel */
60 reply.emplace_back(device.length()); /* ifNameLength */
61 reply.insert(reply.end(), device.begin(), device.end()); /* name */
Patrick Venture4d49ae62018-09-17 11:35:32 -070062
Willy Tuff3cd8e2021-09-14 22:49:55 -070063 return ::ipmi::responseSuccess(SysOEMCommands::SysGetEthDevice, reply);
Patrick Venture4d49ae62018-09-17 11:35:32 -070064}
65
66} // namespace ipmi
67} // namespace google