blob: e2dfc5076610d2c2eee5b81e0969776fc6037b43 [file] [log] [blame]
Patrick Ventured82d0b72020-08-16 09:17:37 -07001/**
2 * Copyright 2017 Google Inc.
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
Patrick Venture09334bb2020-08-16 12:22:54 -070017#include "dbus_mode.hpp"
18
George Liu92110f82025-07-02 15:07:35 +080019#include <ipmid/api-types.hpp>
Patrick Ventured82d0b72020-08-16 09:17:37 -070020#include <sdbusplus/bus.hpp>
Ed Tanousf8b6e552025-06-27 13:27:50 -070021#include <sdbusplus/exception.hpp>
Patrick Ventured82d0b72020-08-16 09:17:37 -070022#include <sdbusplus/message.hpp>
23
24#include <cstdint>
Ed Tanousf8b6e552025-06-27 13:27:50 -070025#include <map>
Patrick Ventured82d0b72020-08-16 09:17:37 -070026#include <string>
Patrick Venture09334bb2020-08-16 12:22:54 -070027#include <variant>
Patrick Ventured82d0b72020-08-16 09:17:37 -070028
George Liu92110f82025-07-02 15:07:35 +080029namespace pid_control::ipmi
Patrick Ventured82d0b72020-08-16 09:17:37 -070030{
31
32static constexpr auto objectPath = "/xyz/openbmc_project/settings/fanctrl/zone";
33static constexpr auto busName = "xyz.openbmc_project.State.FanCtrl";
34static constexpr auto intf = "xyz.openbmc_project.Control.Mode";
Patrick Ventured82d0b72020-08-16 09:17:37 -070035static constexpr auto propertiesintf = "org.freedesktop.DBus.Properties";
36
37using Property = std::string;
38using Value = std::variant<bool>;
39using PropertyMap = std::map<Property, Value>;
40
41/* The following was copied directly from my manual thread handler. */
42static std::string getControlPath(int8_t zone)
43{
44 return std::string(objectPath) + std::to_string(zone);
45}
46
Patrick Venture09334bb2020-08-16 12:22:54 -070047uint8_t DbusZoneControl::getFanCtrlProperty(uint8_t zoneId, bool* value,
48 const std::string& property)
Patrick Ventured82d0b72020-08-16 09:17:37 -070049{
50 std::string path = getControlPath(zoneId);
51
52 auto propertyReadBus = sdbusplus::bus::new_system();
53 auto pimMsg = propertyReadBus.new_method_call(busName, path.c_str(),
54 propertiesintf, "GetAll");
55 pimMsg.append(intf);
56
57 try
58 {
59 PropertyMap propMap;
60
61 /* a method could error but the call not error. */
62 auto valueResponseMsg = propertyReadBus.call(pimMsg);
63
64 valueResponseMsg.read(propMap);
65
66 *value = std::get<bool>(propMap[property]);
67 }
Patrick Williamsb228bc32022-07-22 19:26:56 -050068 catch (const sdbusplus::exception_t& ex)
Patrick Ventured82d0b72020-08-16 09:17:37 -070069 {
George Liu92110f82025-07-02 15:07:35 +080070 return ::ipmi::ccInvalidCommand;
Patrick Ventured82d0b72020-08-16 09:17:37 -070071 }
72
George Liu92110f82025-07-02 15:07:35 +080073 return ::ipmi::ccSuccess;
Patrick Ventured82d0b72020-08-16 09:17:37 -070074}
75
Patrick Venture09334bb2020-08-16 12:22:54 -070076uint8_t DbusZoneControl::setFanCtrlProperty(uint8_t zoneId, bool value,
77 const std::string& property)
78{
79 using Value = std::variant<bool>;
80 Value v{value};
81
82 std::string path = getControlPath(zoneId);
83
84 auto PropertyWriteBus = sdbusplus::bus::new_system();
85 auto pimMsg = PropertyWriteBus.new_method_call(busName, path.c_str(),
86 propertiesintf, "Set");
87 pimMsg.append(intf);
88 pimMsg.append(property);
89 pimMsg.append(v);
90
91 try
92 {
93 PropertyWriteBus.call_noreply(pimMsg);
94 }
Patrick Williamsb228bc32022-07-22 19:26:56 -050095 catch (const sdbusplus::exception_t& ex)
Patrick Venture09334bb2020-08-16 12:22:54 -070096 {
George Liu92110f82025-07-02 15:07:35 +080097 return ::ipmi::ccInvalidCommand;
Patrick Venture09334bb2020-08-16 12:22:54 -070098 }
99
100 /* TODO(venture): Should sanity check the result. */
George Liu92110f82025-07-02 15:07:35 +0800101 return ::ipmi::ccSuccess;
Patrick Venture09334bb2020-08-16 12:22:54 -0700102}
103
George Liu92110f82025-07-02 15:07:35 +0800104} // namespace pid_control::ipmi