xref: /openbmc/phosphor-pid-control/ipmi/dbus_mode.cpp (revision 0ded2c2dcb13dbb161b82a14f7094229b3de0b98)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright 2017 Google Inc
3 
4 #include "dbus_mode.hpp"
5 
6 #include <ipmid/api-types.hpp>
7 #include <sdbusplus/bus.hpp>
8 #include <sdbusplus/exception.hpp>
9 #include <sdbusplus/message.hpp>
10 #include <xyz/openbmc_project/Control/Mode/client.hpp>
11 
12 #include <cstdint>
13 #include <map>
14 #include <string>
15 #include <variant>
16 
17 using ControlMode = sdbusplus::common::xyz::openbmc_project::control::Mode;
18 
19 namespace pid_control::ipmi
20 {
21 
22 static constexpr auto objectPath = "/xyz/openbmc_project/settings/fanctrl/zone";
23 static constexpr auto busName = "xyz.openbmc_project.State.FanCtrl";
24 static constexpr auto propertiesintf = "org.freedesktop.DBus.Properties";
25 
26 using Property = std::string;
27 using Value = std::variant<bool>;
28 using PropertyMap = std::map<Property, Value>;
29 
30 /* The following was copied directly from my manual thread handler. */
getControlPath(int8_t zone)31 static std::string getControlPath(int8_t zone)
32 {
33     return std::string(objectPath) + std::to_string(zone);
34 }
35 
getFanCtrlProperty(uint8_t zoneId,bool * value,const std::string & property)36 uint8_t DbusZoneControl::getFanCtrlProperty(uint8_t zoneId, bool* value,
37                                             const std::string& property)
38 {
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");
44     pimMsg.append(ControlMode::interface);
45 
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     }
57     catch (const sdbusplus::exception_t& ex)
58     {
59         return ::ipmi::ccInvalidCommand;
60     }
61 
62     return ::ipmi::ccSuccess;
63 }
64 
setFanCtrlProperty(uint8_t zoneId,bool value,const std::string & property)65 uint8_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");
76     pimMsg.append(ControlMode::interface);
77     pimMsg.append(property);
78     pimMsg.append(v);
79 
80     try
81     {
82         PropertyWriteBus.call_noreply(pimMsg);
83     }
84     catch (const sdbusplus::exception_t& ex)
85     {
86         return ::ipmi::ccInvalidCommand;
87     }
88 
89     /* TODO(venture): Should sanity check the result. */
90     return ::ipmi::ccSuccess;
91 }
92 
93 } // namespace pid_control::ipmi
94