blob: c72a00aaa2e3de3dd59f32b15e855cbc549b0c83 [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
Patrick Ventured82d0b72020-08-16 09:17:37 -070019#include <ipmid/api.h>
20
21#include <sdbusplus/bus.hpp>
22#include <sdbusplus/message.hpp>
23
24#include <cstdint>
25#include <string>
Patrick Venture09334bb2020-08-16 12:22:54 -070026#include <variant>
Patrick Ventured82d0b72020-08-16 09:17:37 -070027
28namespace pid_control
29{
30namespace ipmi
31{
32
33static constexpr auto objectPath = "/xyz/openbmc_project/settings/fanctrl/zone";
34static constexpr auto busName = "xyz.openbmc_project.State.FanCtrl";
35static constexpr auto intf = "xyz.openbmc_project.Control.Mode";
Patrick Ventured82d0b72020-08-16 09:17:37 -070036static constexpr auto propertiesintf = "org.freedesktop.DBus.Properties";
37
38using Property = std::string;
39using Value = std::variant<bool>;
40using PropertyMap = std::map<Property, Value>;
41
42/* The following was copied directly from my manual thread handler. */
43static std::string getControlPath(int8_t zone)
44{
45 return std::string(objectPath) + std::to_string(zone);
46}
47
Patrick Venture09334bb2020-08-16 12:22:54 -070048uint8_t DbusZoneControl::getFanCtrlProperty(uint8_t zoneId, bool* value,
49 const std::string& property)
Patrick Ventured82d0b72020-08-16 09:17:37 -070050{
51 std::string path = getControlPath(zoneId);
52
53 auto propertyReadBus = sdbusplus::bus::new_system();
54 auto pimMsg = propertyReadBus.new_method_call(busName, path.c_str(),
55 propertiesintf, "GetAll");
56 pimMsg.append(intf);
57
58 try
59 {
60 PropertyMap propMap;
61
62 /* a method could error but the call not error. */
63 auto valueResponseMsg = propertyReadBus.call(pimMsg);
64
65 valueResponseMsg.read(propMap);
66
67 *value = std::get<bool>(propMap[property]);
68 }
Patrick Williamsb228bc32022-07-22 19:26:56 -050069 catch (const sdbusplus::exception_t& ex)
Patrick Ventured82d0b72020-08-16 09:17:37 -070070 {
71 return IPMI_CC_INVALID;
72 }
73
74 return IPMI_CC_OK;
75}
76
Patrick Venture09334bb2020-08-16 12:22:54 -070077uint8_t DbusZoneControl::setFanCtrlProperty(uint8_t zoneId, bool value,
78 const std::string& property)
79{
80 using Value = std::variant<bool>;
81 Value v{value};
82
83 std::string path = getControlPath(zoneId);
84
85 auto PropertyWriteBus = sdbusplus::bus::new_system();
86 auto pimMsg = PropertyWriteBus.new_method_call(busName, path.c_str(),
87 propertiesintf, "Set");
88 pimMsg.append(intf);
89 pimMsg.append(property);
90 pimMsg.append(v);
91
92 try
93 {
94 PropertyWriteBus.call_noreply(pimMsg);
95 }
Patrick Williamsb228bc32022-07-22 19:26:56 -050096 catch (const sdbusplus::exception_t& ex)
Patrick Venture09334bb2020-08-16 12:22:54 -070097 {
98 return IPMI_CC_INVALID;
99 }
100
101 /* TODO(venture): Should sanity check the result. */
102 return IPMI_CC_OK;
103}
104
Patrick Ventured82d0b72020-08-16 09:17:37 -0700105} // namespace ipmi
106} // namespace pid_control