blob: 49e1c5d17e67666f9c4118c90c48852eaba671dd [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
18#include <boost/asio/io_service.hpp>
19#include <iostream>
20#include <sdbusplus/asio/object_server.hpp>
21
22int main()
23{
24 boost::asio::io_service io;
25 std::shared_ptr<sdbusplus::asio::connection> conn;
26 std::shared_ptr<sdbusplus::asio::object_server> server;
27
28 // setup connection to dbus
29 conn = std::make_shared<sdbusplus::asio::connection>(io);
30
31 conn->request_name("com.intel.peci");
32 server = std::make_shared<sdbusplus::asio::object_server>(conn);
33
34 // Send Raw PECI Interface
35 std::shared_ptr<sdbusplus::asio::dbus_interface> ifaceRawPeci =
36 server->add_interface("/com/intel/peci", "com.intel.Protocol.PECI.Raw");
37
38 // Send a Raw PECI command
39 ifaceRawPeci->register_method(
40 "Send", [](const std::string& peciDev,
41 const std::vector<std::vector<uint8_t>>& rawCmds) {
42 peci_SetDevName(const_cast<char*>(peciDev.c_str()));
43 // D-Bus will time out after too long, so set a deadline for when to
44 // abort the PECI commands (at 25s, it mostly times out, at 24s it
45 // doesn't, so use 23s to be safe)
46 constexpr int peciTimeout = 23;
47 std::chrono::steady_clock::time_point peciDeadline =
48 std::chrono::steady_clock::now() +
49 std::chrono::duration<int>(peciTimeout);
50 std::vector<std::vector<uint8_t>> rawResp;
51 rawResp.resize(rawCmds.size());
52 for (size_t i = 0; i < rawCmds.size(); i++)
53 {
54 const std::vector<uint8_t>& rawCmd = rawCmds[i];
55 // If the commands are taking too long, return early to avoid a
56 // D-Bus timeout
57 if (std::chrono::steady_clock::now() > peciDeadline)
58 {
59 std::cerr << peciTimeout
60 << " second deadline reached. Aborting PECI "
61 "commands to avoid a timeout\n";
62 break;
63 }
64
65 if (rawCmd.size() < 3)
66 {
67 peci_SetDevName(NULL);
68 throw std::invalid_argument("Command Length too short");
69 }
70 rawResp[i].resize(rawCmd[2]);
71 peci_raw(rawCmd[0], rawCmd[2], &rawCmd[3], rawCmd[1],
72 rawResp[i].data(), rawResp[i].size());
73 }
74 peci_SetDevName(NULL);
75 return rawResp;
76 });
77 ifaceRawPeci->initialize();
78
79 io.run();
80
81 return 0;
82}