xref: /openbmc/phosphor-hwmon/fan_speed.cpp (revision 5906173a)
1 #include "fan_speed.hpp"
2 
3 #include "env.hpp"
4 #include "hwmon.hpp"
5 #include "hwmonio.hpp"
6 #include "sensorset.hpp"
7 #include "sysfs.hpp"
8 
9 #include <phosphor-logging/elog-errors.hpp>
10 #include <xyz/openbmc_project/Control/Device/error.hpp>
11 
12 using namespace phosphor::logging;
13 
14 namespace hwmon
15 {
16 
17 uint64_t FanSpeed::target(uint64_t value)
18 {
19     auto curValue = FanSpeedObject::target();
20 
21     if (curValue != value)
22     {
23         // Write target out to sysfs
24         try
25         {
26             _ioAccess->write(value, _type, _id, entry::target, hwmonio::retries,
27                              hwmonio::delay);
28         }
29         catch (const std::system_error& e)
30         {
31             using namespace sdbusplus::xyz::openbmc_project::Control::Device::
32                 Error;
33             report<WriteFailure>(
34                 xyz::openbmc_project::Control::Device::WriteFailure::
35                     CALLOUT_ERRNO(e.code().value()),
36                 xyz::openbmc_project::Control::Device::WriteFailure::
37                     CALLOUT_DEVICE_PATH(_devPath.c_str()));
38 
39             auto file = sysfs::make_sysfs_path(_ioAccess->path(), _type, _id,
40                                                entry::target);
41 
42             log<level::INFO>("Logging failing sysfs file",
43                              phosphor::logging::entry("FILE=%s", file.c_str()));
44 
45             exit(EXIT_FAILURE);
46         }
47     }
48 
49     return FanSpeedObject::target(value);
50 }
51 
52 void FanSpeed::enable()
53 {
54     auto enable = env::getEnv("ENABLE", _type, _id);
55     if (!enable.empty())
56     {
57         auto val = std::stoul(enable);
58 
59         try
60         {
61             _ioAccess->write(val, type::pwm, _id, entry::enable,
62                              hwmonio::retries, hwmonio::delay);
63         }
64         catch (const std::system_error& e)
65         {
66             using namespace sdbusplus::xyz::openbmc_project::Control::Device::
67                 Error;
68             phosphor::logging::report<WriteFailure>(
69                 xyz::openbmc_project::Control::Device::WriteFailure::
70                     CALLOUT_ERRNO(e.code().value()),
71                 xyz::openbmc_project::Control::Device::WriteFailure::
72                     CALLOUT_DEVICE_PATH(_devPath.c_str()));
73 
74             auto fullPath = sysfs::make_sysfs_path(_ioAccess->path(), type::pwm,
75                                                    _id, entry::enable);
76 
77             log<level::INFO>(
78                 "Logging failing sysfs file",
79                 phosphor::logging::entry("FILE=%s", fullPath.c_str()));
80 
81             exit(EXIT_FAILURE);
82         }
83     }
84 }
85 
86 } // namespace hwmon
87