Jason M. Bills | bdefaa3 | 2021-11-12 14:09:20 -0800 | [diff] [blame] | 1 | /* |
| 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 Tanous | 0b964d1 | 2022-09-19 15:27:46 -0700 | [diff] [blame] | 18 | #include <boost/asio/io_context.hpp> |
Jason M. Bills | bdefaa3 | 2021-11-12 14:09:20 -0800 | [diff] [blame] | 19 | #include <sdbusplus/asio/object_server.hpp> |
| 20 | |
Patrick Williams | 7169faa | 2023-05-10 07:51:24 -0500 | [diff] [blame] | 21 | #include <iostream> |
| 22 | |
Jason M. Bills | bdefaa3 | 2021-11-12 14:09:20 -0800 | [diff] [blame] | 23 | int main() |
| 24 | { |
Ed Tanous | 0b964d1 | 2022-09-19 15:27:46 -0700 | [diff] [blame] | 25 | boost::asio::io_context io; |
Jason M. Bills | bdefaa3 | 2021-11-12 14:09:20 -0800 | [diff] [blame] | 26 | 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 Williams | c96261e | 2024-08-16 15:22:05 -0400 | [diff] [blame] | 43 | 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. Bills | bdefaa3 | 2021-11-12 14:09:20 -0800 | [diff] [blame] | 54 | { |
Patrick Williams | c96261e | 2024-08-16 15:22:05 -0400 | [diff] [blame] | 55 | 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 Williams | 0621dc0 | 2023-10-20 11:19:59 -0500 | [diff] [blame] | 65 | |
Patrick Williams | c96261e | 2024-08-16 15:22:05 -0400 | [diff] [blame] | 66 | 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 Williams | 0621dc0 | 2023-10-20 11:19:59 -0500 | [diff] [blame] | 75 | } |
Patrick Williams | c96261e | 2024-08-16 15:22:05 -0400 | [diff] [blame] | 76 | peci_SetDevName(NULL); |
| 77 | return rawResp; |
| 78 | }); |
Jason M. Bills | bdefaa3 | 2021-11-12 14:09:20 -0800 | [diff] [blame] | 79 | ifaceRawPeci->initialize(); |
| 80 | |
| 81 | io.run(); |
| 82 | |
| 83 | return 0; |
Ed Tanous | 0b964d1 | 2022-09-19 15:27:46 -0700 | [diff] [blame] | 84 | } |