xref: /openbmc/phosphor-pid-control/ipmi/dbus_mode.cpp (revision 7f1253cd7c7cc82699224ae04b826e1a6a13341e)
1d82d0b7dSPatrick Venture /**
2d82d0b7dSPatrick Venture  * Copyright 2017 Google Inc.
3d82d0b7dSPatrick Venture  *
4d82d0b7dSPatrick Venture  * Licensed under the Apache License, Version 2.0 (the "License");
5d82d0b7dSPatrick Venture  * you may not use this file except in compliance with the License.
6d82d0b7dSPatrick Venture  * You may obtain a copy of the License at
7d82d0b7dSPatrick Venture  *
8d82d0b7dSPatrick Venture  *     http://www.apache.org/licenses/LICENSE-2.0
9d82d0b7dSPatrick Venture  *
10d82d0b7dSPatrick Venture  * Unless required by applicable law or agreed to in writing, software
11d82d0b7dSPatrick Venture  * distributed under the License is distributed on an "AS IS" BASIS,
12d82d0b7dSPatrick Venture  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d82d0b7dSPatrick Venture  * See the License for the specific language governing permissions and
14d82d0b7dSPatrick Venture  * limitations under the License.
15d82d0b7dSPatrick Venture  */
16d82d0b7dSPatrick Venture 
1709334bbeSPatrick Venture #include "dbus_mode.hpp"
1809334bbeSPatrick Venture 
19d82d0b7dSPatrick Venture #include <ipmid/api.h>
20d82d0b7dSPatrick Venture 
21d82d0b7dSPatrick Venture #include <sdbusplus/bus.hpp>
22d82d0b7dSPatrick Venture #include <sdbusplus/message.hpp>
23d82d0b7dSPatrick Venture 
24d82d0b7dSPatrick Venture #include <cstdint>
25d82d0b7dSPatrick Venture #include <string>
2609334bbeSPatrick Venture #include <variant>
27d82d0b7dSPatrick Venture 
28d82d0b7dSPatrick Venture namespace pid_control
29d82d0b7dSPatrick Venture {
30d82d0b7dSPatrick Venture namespace ipmi
31d82d0b7dSPatrick Venture {
32d82d0b7dSPatrick Venture 
33d82d0b7dSPatrick Venture static constexpr auto objectPath = "/xyz/openbmc_project/settings/fanctrl/zone";
34d82d0b7dSPatrick Venture static constexpr auto busName = "xyz.openbmc_project.State.FanCtrl";
35d82d0b7dSPatrick Venture static constexpr auto intf = "xyz.openbmc_project.Control.Mode";
36d82d0b7dSPatrick Venture static constexpr auto propertiesintf = "org.freedesktop.DBus.Properties";
37d82d0b7dSPatrick Venture 
38d82d0b7dSPatrick Venture using Property = std::string;
39d82d0b7dSPatrick Venture using Value = std::variant<bool>;
40d82d0b7dSPatrick Venture using PropertyMap = std::map<Property, Value>;
41d82d0b7dSPatrick Venture 
42d82d0b7dSPatrick Venture /* The following was copied directly from my manual thread handler. */
getControlPath(int8_t zone)43d82d0b7dSPatrick Venture static std::string getControlPath(int8_t zone)
44d82d0b7dSPatrick Venture {
45d82d0b7dSPatrick Venture     return std::string(objectPath) + std::to_string(zone);
46d82d0b7dSPatrick Venture }
47d82d0b7dSPatrick Venture 
getFanCtrlProperty(uint8_t zoneId,bool * value,const std::string & property)4809334bbeSPatrick Venture uint8_t DbusZoneControl::getFanCtrlProperty(uint8_t zoneId, bool* value,
49d82d0b7dSPatrick Venture                                             const std::string& property)
50d82d0b7dSPatrick Venture {
51d82d0b7dSPatrick Venture     std::string path = getControlPath(zoneId);
52d82d0b7dSPatrick Venture 
53d82d0b7dSPatrick Venture     auto propertyReadBus = sdbusplus::bus::new_system();
54d82d0b7dSPatrick Venture     auto pimMsg = propertyReadBus.new_method_call(busName, path.c_str(),
55d82d0b7dSPatrick Venture                                                   propertiesintf, "GetAll");
56d82d0b7dSPatrick Venture     pimMsg.append(intf);
57d82d0b7dSPatrick Venture 
58d82d0b7dSPatrick Venture     try
59d82d0b7dSPatrick Venture     {
60d82d0b7dSPatrick Venture         PropertyMap propMap;
61d82d0b7dSPatrick Venture 
62d82d0b7dSPatrick Venture         /* a method could error but the call not error. */
63d82d0b7dSPatrick Venture         auto valueResponseMsg = propertyReadBus.call(pimMsg);
64d82d0b7dSPatrick Venture 
65d82d0b7dSPatrick Venture         valueResponseMsg.read(propMap);
66d82d0b7dSPatrick Venture 
67d82d0b7dSPatrick Venture         *value = std::get<bool>(propMap[property]);
68d82d0b7dSPatrick Venture     }
69*b228bc30SPatrick Williams     catch (const sdbusplus::exception_t& ex)
70d82d0b7dSPatrick Venture     {
71d82d0b7dSPatrick Venture         return IPMI_CC_INVALID;
72d82d0b7dSPatrick Venture     }
73d82d0b7dSPatrick Venture 
74d82d0b7dSPatrick Venture     return IPMI_CC_OK;
75d82d0b7dSPatrick Venture }
76d82d0b7dSPatrick Venture 
setFanCtrlProperty(uint8_t zoneId,bool value,const std::string & property)7709334bbeSPatrick Venture uint8_t DbusZoneControl::setFanCtrlProperty(uint8_t zoneId, bool value,
7809334bbeSPatrick Venture                                             const std::string& property)
7909334bbeSPatrick Venture {
8009334bbeSPatrick Venture     using Value = std::variant<bool>;
8109334bbeSPatrick Venture     Value v{value};
8209334bbeSPatrick Venture 
8309334bbeSPatrick Venture     std::string path = getControlPath(zoneId);
8409334bbeSPatrick Venture 
8509334bbeSPatrick Venture     auto PropertyWriteBus = sdbusplus::bus::new_system();
8609334bbeSPatrick Venture     auto pimMsg = PropertyWriteBus.new_method_call(busName, path.c_str(),
8709334bbeSPatrick Venture                                                    propertiesintf, "Set");
8809334bbeSPatrick Venture     pimMsg.append(intf);
8909334bbeSPatrick Venture     pimMsg.append(property);
9009334bbeSPatrick Venture     pimMsg.append(v);
9109334bbeSPatrick Venture 
9209334bbeSPatrick Venture     try
9309334bbeSPatrick Venture     {
9409334bbeSPatrick Venture         PropertyWriteBus.call_noreply(pimMsg);
9509334bbeSPatrick Venture     }
96*b228bc30SPatrick Williams     catch (const sdbusplus::exception_t& ex)
9709334bbeSPatrick Venture     {
9809334bbeSPatrick Venture         return IPMI_CC_INVALID;
9909334bbeSPatrick Venture     }
10009334bbeSPatrick Venture 
10109334bbeSPatrick Venture     /* TODO(venture): Should sanity check the result. */
10209334bbeSPatrick Venture     return IPMI_CC_OK;
10309334bbeSPatrick Venture }
10409334bbeSPatrick Venture 
105d82d0b7dSPatrick Venture } // namespace ipmi
106d82d0b7dSPatrick Venture } // namespace pid_control
107