blob: 41556583b8831bb34301c2be7a3d46dae1e14343 [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>
10
11#include <cstdint>
Ed Tanousf8b6e552025-06-27 13:27:50 -070012#include <map>
Patrick Ventured82d0b72020-08-16 09:17:37 -070013#include <string>
Patrick Venture09334bb2020-08-16 12:22:54 -070014#include <variant>
Patrick Ventured82d0b72020-08-16 09:17:37 -070015
George Liu92110f82025-07-02 15:07:35 +080016namespace pid_control::ipmi
Patrick Ventured82d0b72020-08-16 09:17:37 -070017{
18
19static constexpr auto objectPath = "/xyz/openbmc_project/settings/fanctrl/zone";
20static constexpr auto busName = "xyz.openbmc_project.State.FanCtrl";
21static constexpr auto intf = "xyz.openbmc_project.Control.Mode";
Patrick Ventured82d0b72020-08-16 09:17:37 -070022static constexpr auto propertiesintf = "org.freedesktop.DBus.Properties";
23
24using Property = std::string;
25using Value = std::variant<bool>;
26using PropertyMap = std::map<Property, Value>;
27
28/* The following was copied directly from my manual thread handler. */
29static std::string getControlPath(int8_t zone)
30{
31 return std::string(objectPath) + std::to_string(zone);
32}
33
Patrick Venture09334bb2020-08-16 12:22:54 -070034uint8_t DbusZoneControl::getFanCtrlProperty(uint8_t zoneId, bool* value,
35 const std::string& property)
Patrick Ventured82d0b72020-08-16 09:17:37 -070036{
37 std::string path = getControlPath(zoneId);
38
39 auto propertyReadBus = sdbusplus::bus::new_system();
40 auto pimMsg = propertyReadBus.new_method_call(busName, path.c_str(),
41 propertiesintf, "GetAll");
42 pimMsg.append(intf);
43
44 try
45 {
46 PropertyMap propMap;
47
48 /* a method could error but the call not error. */
49 auto valueResponseMsg = propertyReadBus.call(pimMsg);
50
51 valueResponseMsg.read(propMap);
52
53 *value = std::get<bool>(propMap[property]);
54 }
Patrick Williamsb228bc32022-07-22 19:26:56 -050055 catch (const sdbusplus::exception_t& ex)
Patrick Ventured82d0b72020-08-16 09:17:37 -070056 {
George Liu92110f82025-07-02 15:07:35 +080057 return ::ipmi::ccInvalidCommand;
Patrick Ventured82d0b72020-08-16 09:17:37 -070058 }
59
George Liu92110f82025-07-02 15:07:35 +080060 return ::ipmi::ccSuccess;
Patrick Ventured82d0b72020-08-16 09:17:37 -070061}
62
Patrick Venture09334bb2020-08-16 12:22:54 -070063uint8_t DbusZoneControl::setFanCtrlProperty(uint8_t zoneId, bool value,
64 const std::string& property)
65{
66 using Value = std::variant<bool>;
67 Value v{value};
68
69 std::string path = getControlPath(zoneId);
70
71 auto PropertyWriteBus = sdbusplus::bus::new_system();
72 auto pimMsg = PropertyWriteBus.new_method_call(busName, path.c_str(),
73 propertiesintf, "Set");
74 pimMsg.append(intf);
75 pimMsg.append(property);
76 pimMsg.append(v);
77
78 try
79 {
80 PropertyWriteBus.call_noreply(pimMsg);
81 }
Patrick Williamsb228bc32022-07-22 19:26:56 -050082 catch (const sdbusplus::exception_t& ex)
Patrick Venture09334bb2020-08-16 12:22:54 -070083 {
George Liu92110f82025-07-02 15:07:35 +080084 return ::ipmi::ccInvalidCommand;
Patrick Venture09334bb2020-08-16 12:22:54 -070085 }
86
87 /* TODO(venture): Should sanity check the result. */
George Liu92110f82025-07-02 15:07:35 +080088 return ::ipmi::ccSuccess;
Patrick Venture09334bb2020-08-16 12:22:54 -070089}
90
George Liu92110f82025-07-02 15:07:35 +080091} // namespace pid_control::ipmi