blob: 52666c0682c859468f51932178ea0985c344d0e1 [file] [log] [blame]
Alexander Hansen46a755f2025-10-27 16:31:08 +01001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright 2017 Google Inc
Patrick Ventured82d0b72020-08-16 09:17:37 -07003
Patrick Venture09334bb2020-08-16 12:22:54 -07004#include "dbus_mode.hpp"
5
George Liu92110f82025-07-02 15:07:35 +08006#include <ipmid/api-types.hpp>
Patrick Ventured82d0b72020-08-16 09:17:37 -07007#include <sdbusplus/bus.hpp>
Ed Tanousf8b6e552025-06-27 13:27:50 -07008#include <sdbusplus/exception.hpp>
Patrick Ventured82d0b72020-08-16 09:17:37 -07009#include <sdbusplus/message.hpp>
Alexander Hansen0ded2c22025-11-12 12:18:10 +010010#include <xyz/openbmc_project/Control/Mode/client.hpp>
Patrick Ventured82d0b72020-08-16 09:17:37 -070011
12#include <cstdint>
Ed Tanousf8b6e552025-06-27 13:27:50 -070013#include <map>
Patrick Ventured82d0b72020-08-16 09:17:37 -070014#include <string>
Patrick Venture09334bb2020-08-16 12:22:54 -070015#include <variant>
Patrick Ventured82d0b72020-08-16 09:17:37 -070016
Alexander Hansen0ded2c22025-11-12 12:18:10 +010017using ControlMode = sdbusplus::common::xyz::openbmc_project::control::Mode;
18
George Liu92110f82025-07-02 15:07:35 +080019namespace pid_control::ipmi
Patrick Ventured82d0b72020-08-16 09:17:37 -070020{
21
22static constexpr auto objectPath = "/xyz/openbmc_project/settings/fanctrl/zone";
23static constexpr auto busName = "xyz.openbmc_project.State.FanCtrl";
Patrick Ventured82d0b72020-08-16 09:17:37 -070024static constexpr auto propertiesintf = "org.freedesktop.DBus.Properties";
25
26using Property = std::string;
27using Value = std::variant<bool>;
28using PropertyMap = std::map<Property, Value>;
29
30/* The following was copied directly from my manual thread handler. */
31static std::string getControlPath(int8_t zone)
32{
33 return std::string(objectPath) + std::to_string(zone);
34}
35
Patrick Venture09334bb2020-08-16 12:22:54 -070036uint8_t DbusZoneControl::getFanCtrlProperty(uint8_t zoneId, bool* value,
37 const std::string& property)
Patrick Ventured82d0b72020-08-16 09:17:37 -070038{
39 std::string path = getControlPath(zoneId);
40
41 auto propertyReadBus = sdbusplus::bus::new_system();
42 auto pimMsg = propertyReadBus.new_method_call(busName, path.c_str(),
43 propertiesintf, "GetAll");
Alexander Hansen0ded2c22025-11-12 12:18:10 +010044 pimMsg.append(ControlMode::interface);
Patrick Ventured82d0b72020-08-16 09:17:37 -070045
46 try
47 {
48 PropertyMap propMap;
49
50 /* a method could error but the call not error. */
51 auto valueResponseMsg = propertyReadBus.call(pimMsg);
52
53 valueResponseMsg.read(propMap);
54
55 *value = std::get<bool>(propMap[property]);
56 }
Patrick Williamsb228bc32022-07-22 19:26:56 -050057 catch (const sdbusplus::exception_t& ex)
Patrick Ventured82d0b72020-08-16 09:17:37 -070058 {
George Liu92110f82025-07-02 15:07:35 +080059 return ::ipmi::ccInvalidCommand;
Patrick Ventured82d0b72020-08-16 09:17:37 -070060 }
61
George Liu92110f82025-07-02 15:07:35 +080062 return ::ipmi::ccSuccess;
Patrick Ventured82d0b72020-08-16 09:17:37 -070063}
64
Patrick Venture09334bb2020-08-16 12:22:54 -070065uint8_t DbusZoneControl::setFanCtrlProperty(uint8_t zoneId, bool value,
66 const std::string& property)
67{
68 using Value = std::variant<bool>;
69 Value v{value};
70
71 std::string path = getControlPath(zoneId);
72
73 auto PropertyWriteBus = sdbusplus::bus::new_system();
74 auto pimMsg = PropertyWriteBus.new_method_call(busName, path.c_str(),
75 propertiesintf, "Set");
Alexander Hansen0ded2c22025-11-12 12:18:10 +010076 pimMsg.append(ControlMode::interface);
Patrick Venture09334bb2020-08-16 12:22:54 -070077 pimMsg.append(property);
78 pimMsg.append(v);
79
80 try
81 {
82 PropertyWriteBus.call_noreply(pimMsg);
83 }
Patrick Williamsb228bc32022-07-22 19:26:56 -050084 catch (const sdbusplus::exception_t& ex)
Patrick Venture09334bb2020-08-16 12:22:54 -070085 {
George Liu92110f82025-07-02 15:07:35 +080086 return ::ipmi::ccInvalidCommand;
Patrick Venture09334bb2020-08-16 12:22:54 -070087 }
88
89 /* TODO(venture): Should sanity check the result. */
George Liu92110f82025-07-02 15:07:35 +080090 return ::ipmi::ccSuccess;
Patrick Venture09334bb2020-08-16 12:22:54 -070091}
92
George Liu92110f82025-07-02 15:07:35 +080093} // namespace pid_control::ipmi