blob: 32a3fe2efe4e94a013c24684832b1a02d6c4ef8c [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 "psu.hpp"
16
Patrick Venture0e9aae52020-08-13 13:07:09 -070017#include "commands.hpp"
Patrick Ventureaa374122019-03-15 15:09:10 -070018#include "errors.hpp"
19#include "handler.hpp"
Patrick Venture4d49ae62018-09-17 11:35:32 -070020
21#include <cstdint>
Patrick Venturece07ee02018-09-19 18:09:32 -070022#include <cstring>
Willy Tuff3cd8e2021-09-14 22:49:55 -070023#include <ipmid/api-types.hpp>
Willy Tub4e37042021-10-12 17:12:30 -070024#include <span>
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
Willy Tub4e37042021-10-12 17:12:30 -070032Resp psuHardReset(std::span<const uint8_t> data,
Willy Tuff3cd8e2021-09-14 22:49:55 -070033 const HandlerInterface* handler)
Patrick Venture4d49ae62018-09-17 11:35:32 -070034{
Patrick Ventureacd54232018-10-16 16:58:59 -070035 struct PsuResetRequest request;
36
Willy Tuff3cd8e2021-09-14 22:49:55 -070037 if (data.size() < sizeof(request))
Patrick Venture4d49ae62018-09-17 11:35:32 -070038 {
Patrick Venturece07ee02018-09-19 18:09:32 -070039 std::fprintf(stderr, "Invalid command length: %u\n",
Willy Tuff3cd8e2021-09-14 22:49:55 -070040 static_cast<uint32_t>(data.size()));
41 return ::ipmi::responseReqDataLenInvalid();
Patrick Venture4d49ae62018-09-17 11:35:32 -070042 }
43
Willy Tuff3cd8e2021-09-14 22:49:55 -070044 std::memcpy(&request, data.data(), sizeof(struct PsuResetRequest));
Patrick Venture4d49ae62018-09-17 11:35:32 -070045 try
46 {
Patrick Ventureaa374122019-03-15 15:09:10 -070047 handler->psuResetDelay(request.delay);
Patrick Venture4d49ae62018-09-17 11:35:32 -070048 }
Patrick Ventureaa374122019-03-15 15:09:10 -070049 catch (const IpmiException& e)
Patrick Venture4d49ae62018-09-17 11:35:32 -070050 {
Willy Tuff3cd8e2021-09-14 22:49:55 -070051 return ::ipmi::response(e.getIpmiError());
Patrick Venture4d49ae62018-09-17 11:35:32 -070052 }
53
Willy Tuff3cd8e2021-09-14 22:49:55 -070054 return ::ipmi::responseSuccess(SysOEMCommands::SysPsuHardReset,
55 std::vector<uint8_t>{});
Patrick Venture4d49ae62018-09-17 11:35:32 -070056}
57
Willy Tub4e37042021-10-12 17:12:30 -070058Resp psuHardResetOnShutdown(std::span<const uint8_t>,
Willy Tuff3cd8e2021-09-14 22:49:55 -070059 const HandlerInterface* handler)
Shounak Mitraac4a16f2021-02-02 11:11:44 -080060{
Shounak Mitraac4a16f2021-02-02 11:11:44 -080061 try
62 {
63 handler->psuResetOnShutdown();
64 }
65 catch (const IpmiException& e)
66 {
Willy Tuff3cd8e2021-09-14 22:49:55 -070067 return ::ipmi::response(e.getIpmiError());
Shounak Mitraac4a16f2021-02-02 11:11:44 -080068 }
69
Willy Tuff3cd8e2021-09-14 22:49:55 -070070 return ::ipmi::responseSuccess(SysOEMCommands::SysPsuHardResetOnShutdown,
71 std::vector<uint8_t>{});
Shounak Mitraac4a16f2021-02-02 11:11:44 -080072}
73
Patrick Venture4d49ae62018-09-17 11:35:32 -070074} // namespace ipmi
75} // namespace google