blob: 3e95eceafadbb116a3ab786ef01df3b57ad3d891 [file] [log] [blame]
/*
// Copyright (c) 2021 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/
#include <peci.h>
#include <boost/asio/io_context.hpp>
#include <sdbusplus/asio/object_server.hpp>
#include <iostream>
int main()
{
boost::asio::io_context io;
std::shared_ptr<sdbusplus::asio::connection> conn;
std::shared_ptr<sdbusplus::asio::object_server> server;
// setup connection to dbus
conn = std::make_shared<sdbusplus::asio::connection>(io);
conn->request_name("com.intel.peci");
server = std::make_shared<sdbusplus::asio::object_server>(conn);
// Send Raw PECI Interface
std::shared_ptr<sdbusplus::asio::dbus_interface> ifaceRawPeci =
server->add_interface("/com/intel/peci", "com.intel.Protocol.PECI.Raw");
// Send a Raw PECI command
ifaceRawPeci->register_method(
"Send", [](const std::string& peciDev,
const std::vector<std::vector<uint8_t>>& rawCmds) {
peci_SetDevName(const_cast<char*>(peciDev.c_str()));
// D-Bus will time out after too long, so set a deadline for when to
// abort the PECI commands (at 25s, it mostly times out, at 24s it
// doesn't, so use 23s to be safe)
constexpr int peciTimeout = 23;
std::chrono::steady_clock::time_point peciDeadline =
std::chrono::steady_clock::now() +
std::chrono::duration<int>(peciTimeout);
std::vector<std::vector<uint8_t>> rawResp;
rawResp.resize(rawCmds.size());
for (size_t i = 0; i < rawCmds.size(); i++)
{
const std::vector<uint8_t>& rawCmd = rawCmds[i];
// If the commands are taking too long, return early to avoid a
// D-Bus timeout
if (std::chrono::steady_clock::now() > peciDeadline)
{
std::cerr << peciTimeout
<< " second deadline reached. Aborting PECI "
"commands to avoid a timeout\n";
break;
}
if (rawCmd.size() < 3)
{
peci_SetDevName(NULL);
throw std::invalid_argument("Command Length too short");
}
rawResp[i].resize(rawCmd[2]);
peci_raw(rawCmd[0], rawCmd[2], &rawCmd[3], rawCmd[1],
rawResp[i].data(),
static_cast<uint32_t>(rawResp[i].size()));
}
peci_SetDevName(NULL);
return rawResp;
});
ifaceRawPeci->initialize();
io.run();
return 0;
}