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