xref: /openbmc/phosphor-pid-control/setsensor.cpp (revision f8b6e55147148c3cfb42327ff267197a460b411c)
1 #include <sdbusplus/bus.hpp>
2 #include <sdbusplus/exception.hpp>
3 #include <sdbusplus/message.hpp>
4 
5 #include <cstdint>
6 #include <cstdio>
7 #include <string>
8 #include <variant>
9 
10 /* Fan Control */
11 static constexpr auto objectPath = "/xyz/openbmc_project/settings/fanctrl/zone";
12 static constexpr auto busName = "xyz.openbmc_project.State.FanCtrl";
13 static constexpr auto intf = "xyz.openbmc_project.Control.Mode";
14 static constexpr auto property = "Manual";
15 using Value = std::variant<bool>;
16 
17 /* Host Sensor. */
18 static constexpr auto sobjectPath =
19     "/xyz/openbmc_project/extsensors/margin/sluggish0";
20 static constexpr auto sbusName = "xyz.openbmc_project.Hwmon.external";
21 static constexpr auto sintf = "xyz.openbmc_project.Sensor.Value";
22 static constexpr auto sproperty = "Value";
23 using sValue = std::variant<int64_t>;
24 
25 static constexpr auto propertiesintf = "org.freedesktop.DBus.Properties";
26 
27 static void SetHostSensor(void)
28 {
29     int64_t value = 300;
30     sValue v{value};
31 
32     std::string busname{sbusName};
33     auto PropertyWriteBus = sdbusplus::bus::new_system();
34     std::string path{sobjectPath};
35 
36     auto pimMsg = PropertyWriteBus.new_method_call(
37         busname.c_str(), path.c_str(), propertiesintf, "Set");
38 
39     pimMsg.append(sintf);
40     pimMsg.append(sproperty);
41     pimMsg.append(v);
42 
43     try
44     {
45         auto responseMsg = PropertyWriteBus.call(pimMsg);
46         fprintf(stderr, "call to Set the host sensor value succeeded.\n");
47     }
48     catch (const sdbusplus::exception_t& ex)
49     {
50         fprintf(stderr, "call to Set the host sensor value failed.\n");
51     }
52 }
53 
54 static std::string GetControlPath(int8_t zone)
55 {
56     return std::string(objectPath) + std::to_string(zone);
57 }
58 
59 static void SetManualMode(int8_t zone)
60 {
61     bool setValue = true;
62 
63     Value v{setValue};
64 
65     std::string busname{busName};
66     auto PropertyWriteBus = sdbusplus::bus::new_system();
67     std::string path = GetControlPath(zone);
68 
69     auto pimMsg = PropertyWriteBus.new_method_call(
70         busname.c_str(), path.c_str(), propertiesintf, "Set");
71 
72     pimMsg.append(intf);
73     pimMsg.append(property);
74     pimMsg.append(v);
75 
76     try
77     {
78         auto responseMsg = PropertyWriteBus.call(pimMsg);
79         fprintf(stderr, "call to Set the manual mode succeeded.\n");
80     }
81     catch (const sdbusplus::exception_t& ex)
82     {
83         fprintf(stderr, "call to Set the manual mode failed.\n");
84     }
85 }
86 
87 int main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[])
88 {
89     int rc = 0;
90 
91     int64_t zone = 0x01;
92 
93     SetManualMode(zone);
94     SetHostSensor();
95     return rc;
96 }
97