blob: 5e938b013c115b21f80faf0971120e5cbf6a00b8 [file] [log] [blame]
Matthew Barthbf7b7b12017-03-07 15:46:59 -06001#pragma once
2
3#include <experimental/filesystem>
Matt Spinlerb4e65572017-10-17 10:09:18 -05004#include <phosphor-logging/elog-errors.hpp>
5#include <phosphor-logging/log.hpp>
6#include <xyz/openbmc_project/Sensor/Device/error.hpp>
Matthew Barth048ac872017-03-09 14:36:08 -06007#include "fan_speed.hpp"
Matthew Barthbf7b7b12017-03-07 15:46:59 -06008
9/** @class Targets
10 * @brief Target type traits.
11 *
12 * @tparam T - The target type.
13 */
14template <typename T>
15struct Targets
16{
17 static void fail()
18 {
19 static_assert(sizeof(Targets) == -1, "Unsupported Target type");
20 }
21};
22
23/**@brief Targets specialization for fan speed. */
24template <>
Matthew Barth048ac872017-03-09 14:36:08 -060025struct Targets<hwmon::FanSpeed>
Matthew Barthbf7b7b12017-03-07 15:46:59 -060026{
27 static constexpr InterfaceType type = InterfaceType::FAN_SPEED;
Matthew Barthbf7b7b12017-03-07 15:46:59 -060028};
29
30/** @brief addTarget
31 *
32 * Creates the target type interface
33 *
34 * @tparam T - The target type
35 *
36 * @param[in] sensor - A sensor type and name
Matt Spinlerb4e65572017-10-17 10:09:18 -050037 * @param[in] ioAccess - hwmon sysfs access object
38 * @param[in] devPath - The /sys/devices sysfs path
Matthew Barthbf7b7b12017-03-07 15:46:59 -060039 * @param[in] info - The sdbusplus server connection and interfaces
Matt Spinler0a8de642017-05-11 10:59:39 -050040 *
41 * @return A shared pointer to the target interface object
42 * Will be empty if no interface was created
Matthew Barthbf7b7b12017-03-07 15:46:59 -060043 */
44template <typename T>
Matt Spinler0a8de642017-05-11 10:59:39 -050045std::shared_ptr<T> addTarget(const SensorSet::key_type& sensor,
Matt Spinlerb4e65572017-10-17 10:09:18 -050046 const sysfs::hwmonio::HwmonIO& ioAccess,
Brad Bishop751043e2017-08-29 11:13:46 -040047 const std::string& devPath,
Matt Spinler0a8de642017-05-11 10:59:39 -050048 ObjectInfo& info)
Matthew Barthbf7b7b12017-03-07 15:46:59 -060049{
Matt Spinler0a8de642017-05-11 10:59:39 -050050 std::shared_ptr<T> target;
Matthew Barthbf7b7b12017-03-07 15:46:59 -060051 namespace fs = std::experimental::filesystem;
52 static constexpr bool deferSignals = true;
53
54 auto& bus = *std::get<sdbusplus::bus::bus*>(info);
55 auto& obj = std::get<Object>(info);
56 auto& objPath = std::get<std::string>(info);
57
58 // Check if target sysfs file exists
Matt Spinlerb4e65572017-10-17 10:09:18 -050059 auto sysfsFullPath = sysfs::make_sysfs_path(ioAccess.path(),
Patrick Venture1e6324f2017-06-01 14:07:05 -070060 sensor.first,
61 sensor.second,
62 hwmon::entry::target);
Matthew Barth048ac872017-03-09 14:36:08 -060063 if (fs::exists(sysfsFullPath))
Matthew Barthbf7b7b12017-03-07 15:46:59 -060064 {
Matt Spinlerb4e65572017-10-17 10:09:18 -050065 uint32_t targetSpeed = 0;
66
67 try
68 {
69 targetSpeed = ioAccess.read(
70 sensor.first,
71 sensor.second,
72 hwmon::entry::target,
73 sysfs::hwmonio::retries,
74 sysfs::hwmonio::delay);
75
76 }
77 catch (const std::system_error& e)
78 {
79 using namespace phosphor::logging;
80 using namespace sdbusplus::xyz::openbmc_project::
81 Sensor::Device::Error;
82 using metadata = xyz::openbmc_project::Sensor::
83 Device::ReadFailure;
84
85 report<ReadFailure>(
86 metadata::CALLOUT_ERRNO(e.code().value()),
87 metadata::CALLOUT_DEVICE_PATH(devPath.c_str()));
88
89 log<level::INFO>("Logging failing sysfs file",
90 phosphor::logging::entry(
91 "FILE=%s", sysfsFullPath.c_str()));
92 }
93
94 target = std::make_shared<T>(ioAccess.path(),
Brad Bishop751043e2017-08-29 11:13:46 -040095 devPath,
Matt Spinler0a8de642017-05-11 10:59:39 -050096 sensor.second,
97 bus,
98 objPath.c_str(),
Matt Spinlerb4e65572017-10-17 10:09:18 -050099 deferSignals,
100 targetSpeed);
Matthew Barthbf7b7b12017-03-07 15:46:59 -0600101 auto type = Targets<T>::type;
Matt Spinler0a8de642017-05-11 10:59:39 -0500102 obj[type] = target;
Matthew Barthbf7b7b12017-03-07 15:46:59 -0600103 }
Matt Spinler0a8de642017-05-11 10:59:39 -0500104
105 return target;
Matthew Barthbf7b7b12017-03-07 15:46:59 -0600106}