blob: b9cdb6781c3287e53e0ad093d23d53ec2c58ca59 [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";
36static constexpr auto manualProperty = "Manual";
37static constexpr auto failsafeProperty = "FailSafe";
38static constexpr auto propertiesintf = "org.freedesktop.DBus.Properties";
39
40using Property = std::string;
41using Value = std::variant<bool>;
42using PropertyMap = std::map<Property, Value>;
43
44/* The following was copied directly from my manual thread handler. */
45static std::string getControlPath(int8_t zone)
46{
47 return std::string(objectPath) + std::to_string(zone);
48}
49
Patrick Venture09334bb2020-08-16 12:22:54 -070050uint8_t DbusZoneControl::getFanCtrlProperty(uint8_t zoneId, bool* value,
51 const std::string& property)
Patrick Ventured82d0b72020-08-16 09:17:37 -070052{
53 std::string path = getControlPath(zoneId);
54
55 auto propertyReadBus = sdbusplus::bus::new_system();
56 auto pimMsg = propertyReadBus.new_method_call(busName, path.c_str(),
57 propertiesintf, "GetAll");
58 pimMsg.append(intf);
59
60 try
61 {
62 PropertyMap propMap;
63
64 /* a method could error but the call not error. */
65 auto valueResponseMsg = propertyReadBus.call(pimMsg);
66
67 valueResponseMsg.read(propMap);
68
69 *value = std::get<bool>(propMap[property]);
70 }
Patrick Williamse7507a82021-09-02 09:46:06 -050071 catch (const sdbusplus::exception::exception& ex)
Patrick Ventured82d0b72020-08-16 09:17:37 -070072 {
73 return IPMI_CC_INVALID;
74 }
75
76 return IPMI_CC_OK;
77}
78
Patrick Venture09334bb2020-08-16 12:22:54 -070079uint8_t DbusZoneControl::setFanCtrlProperty(uint8_t zoneId, bool value,
80 const std::string& property)
81{
82 using Value = std::variant<bool>;
83 Value v{value};
84
85 std::string path = getControlPath(zoneId);
86
87 auto PropertyWriteBus = sdbusplus::bus::new_system();
88 auto pimMsg = PropertyWriteBus.new_method_call(busName, path.c_str(),
89 propertiesintf, "Set");
90 pimMsg.append(intf);
91 pimMsg.append(property);
92 pimMsg.append(v);
93
94 try
95 {
96 PropertyWriteBus.call_noreply(pimMsg);
97 }
Patrick Williamse7507a82021-09-02 09:46:06 -050098 catch (const sdbusplus::exception::exception& ex)
Patrick Venture09334bb2020-08-16 12:22:54 -070099 {
100 return IPMI_CC_INVALID;
101 }
102
103 /* TODO(venture): Should sanity check the result. */
104 return IPMI_CC_OK;
105}
106
Patrick Ventured82d0b72020-08-16 09:17:37 -0700107} // namespace ipmi
108} // namespace pid_control