blob: 1016a45e9378ada2cfc4095107625923bab4abc9 [file] [log] [blame]
Jason M. Billsbdefaa32021-11-12 14:09:20 -08001/*
2// Copyright (c) 2021 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#include <peci.h>
17
Ed Tanous0b964d12022-09-19 15:27:46 -070018#include <boost/asio/io_context.hpp>
Jason M. Billsbdefaa32021-11-12 14:09:20 -080019#include <sdbusplus/asio/object_server.hpp>
20
Patrick Williams7169faa2023-05-10 07:51:24 -050021#include <iostream>
22
Jason M. Billsbdefaa32021-11-12 14:09:20 -080023int main()
24{
Ed Tanous0b964d12022-09-19 15:27:46 -070025 boost::asio::io_context io;
Jason M. Billsbdefaa32021-11-12 14:09:20 -080026 std::shared_ptr<sdbusplus::asio::connection> conn;
27 std::shared_ptr<sdbusplus::asio::object_server> server;
28
29 // setup connection to dbus
30 conn = std::make_shared<sdbusplus::asio::connection>(io);
31
32 conn->request_name("com.intel.peci");
33 server = std::make_shared<sdbusplus::asio::object_server>(conn);
34
35 // Send Raw PECI Interface
36 std::shared_ptr<sdbusplus::asio::dbus_interface> ifaceRawPeci =
37 server->add_interface("/com/intel/peci", "com.intel.Protocol.PECI.Raw");
38
39 // Send a Raw PECI command
40 ifaceRawPeci->register_method(
41 "Send", [](const std::string& peciDev,
42 const std::vector<std::vector<uint8_t>>& rawCmds) {
Patrick Williamsc96261e2024-08-16 15:22:05 -040043 peci_SetDevName(const_cast<char*>(peciDev.c_str()));
44 // D-Bus will time out after too long, so set a deadline for when to
45 // abort the PECI commands (at 25s, it mostly times out, at 24s it
46 // doesn't, so use 23s to be safe)
47 constexpr int peciTimeout = 23;
48 std::chrono::steady_clock::time_point peciDeadline =
49 std::chrono::steady_clock::now() +
50 std::chrono::duration<int>(peciTimeout);
51 std::vector<std::vector<uint8_t>> rawResp;
52 rawResp.resize(rawCmds.size());
53 for (size_t i = 0; i < rawCmds.size(); i++)
Jason M. Billsbdefaa32021-11-12 14:09:20 -080054 {
Patrick Williamsc96261e2024-08-16 15:22:05 -040055 const std::vector<uint8_t>& rawCmd = rawCmds[i];
56 // If the commands are taking too long, return early to avoid a
57 // D-Bus timeout
58 if (std::chrono::steady_clock::now() > peciDeadline)
59 {
60 std::cerr << peciTimeout
61 << " second deadline reached. Aborting PECI "
62 "commands to avoid a timeout\n";
63 break;
64 }
Patrick Williams0621dc02023-10-20 11:19:59 -050065
Patrick Williamsc96261e2024-08-16 15:22:05 -040066 if (rawCmd.size() < 3)
67 {
68 peci_SetDevName(NULL);
69 throw std::invalid_argument("Command Length too short");
70 }
71 rawResp[i].resize(rawCmd[2]);
72 peci_raw(rawCmd[0], rawCmd[2], &rawCmd[3], rawCmd[1],
73 rawResp[i].data(),
74 static_cast<uint32_t>(rawResp[i].size()));
Patrick Williams0621dc02023-10-20 11:19:59 -050075 }
Patrick Williamsc96261e2024-08-16 15:22:05 -040076 peci_SetDevName(NULL);
77 return rawResp;
78 });
Jason M. Billsbdefaa32021-11-12 14:09:20 -080079 ifaceRawPeci->initialize();
80
81 io.run();
82
83 return 0;
Ed Tanous0b964d12022-09-19 15:27:46 -070084}