blob: ac19bad3bcaf535f716f50f8b46727efff192f34 [file] [log] [blame]
Nikhil Namjoshi4aa93172022-09-22 18:51:37 +00001// Copyright 2022 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.
linyuny8cfa4c42021-06-16 13:53:08 -070014
15#include "host_power_off.hpp"
16
17#include "commands.hpp"
18#include "errors.hpp"
19#include "handler.hpp"
20
21#include <cstdint>
22#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>
linyuny8cfa4c42021-06-16 13:53:08 -070026
27namespace google
28{
29namespace ipmi
30{
31
Willy Tub4e37042021-10-12 17:12:30 -070032Resp hostPowerOff(std::span<const uint8_t> data,
Willy Tuff3cd8e2021-09-14 22:49:55 -070033 const HandlerInterface* handler)
linyuny8cfa4c42021-06-16 13:53:08 -070034{
35 struct HostPowerOffRequest request;
36
Willy Tuff3cd8e2021-09-14 22:49:55 -070037 if (data.size() < sizeof(request))
linyuny8cfa4c42021-06-16 13:53:08 -070038 {
39 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();
linyuny8cfa4c42021-06-16 13:53:08 -070042 }
43
Willy Tuff3cd8e2021-09-14 22:49:55 -070044 std::memcpy(&request, data.data(), sizeof(struct HostPowerOffRequest));
linyuny8cfa4c42021-06-16 13:53:08 -070045 try
46 {
47 handler->hostPowerOffDelay(request.delay);
48 }
49 catch (const IpmiException& e)
50 {
Willy Tuff3cd8e2021-09-14 22:49:55 -070051 return ::ipmi::response(e.getIpmiError());
linyuny8cfa4c42021-06-16 13:53:08 -070052 }
53
Willy Tuff3cd8e2021-09-14 22:49:55 -070054 return ::ipmi::responseSuccess(SysOEMCommands::SysHostPowerOff,
55 std::vector<uint8_t>{});
linyuny8cfa4c42021-06-16 13:53:08 -070056}
57} // namespace ipmi
58} // namespace google