Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016-2020 CESNET, https://photonics.cesnet.cz/ |
| 3 | * |
| 4 | * Written by Tomáš Pecka <tomas.pecka@fit.cvut.cz> |
| 5 | * |
| 6 | */ |
| 7 | |
Václav Kubernát | 069d3a9 | 2021-11-14 12:37:46 +0100 | [diff] [blame] | 8 | #include <chrono> |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 9 | #include <utility> |
| 10 | #include "IETFHardware.h" |
| 11 | #include "utils/log.h" |
| 12 | #include "utils/time.h" |
| 13 | |
| 14 | using namespace std::literals; |
| 15 | |
| 16 | namespace { |
| 17 | |
Tomáš Pecka | 83b62e1 | 2020-12-16 14:50:49 +0100 | [diff] [blame] | 18 | static const std::string ietfHardwareStatePrefix = "/ietf-hardware:hardware"; |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 19 | |
| 20 | /** @brief Constructs a full XPath for a specific component */ |
| 21 | std::string xpathForComponent(const std::string& componentName) |
| 22 | { |
| 23 | return ietfHardwareStatePrefix + "/component[name='" + componentName + "']/"; |
| 24 | } |
| 25 | |
| 26 | /** @brief Prefix all properties from values DataTree with a component name (calculated from @p componentName) and push them into the DataTree */ |
| 27 | void addComponent(velia::ietf_hardware::DataTree& res, const std::string& componentName, const std::optional<std::string>& parent, const velia::ietf_hardware::DataTree& values) |
| 28 | { |
| 29 | auto componentPrefix = xpathForComponent(componentName); |
| 30 | |
| 31 | if (parent) { |
| 32 | res[componentPrefix + "parent"] = *parent; |
| 33 | } |
| 34 | for (const auto& [k, v] : values) { |
| 35 | res[componentPrefix + k] = v; |
| 36 | } |
Tomáš Pecka | 7eb0c42 | 2023-04-21 15:36:33 +0200 | [diff] [blame] | 37 | |
| 38 | res[componentPrefix + "state/oper-state"] = "enabled"; |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 39 | } |
| 40 | |
Tomáš Pecka | 655062c | 2023-12-11 15:37:58 +0100 | [diff] [blame] | 41 | void writeSensorValue(velia::ietf_hardware::DataTree& res, const std::string& componentName, const std::string& value, const std::string& operStatus) |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 42 | { |
| 43 | const auto componentPrefix = xpathForComponent(componentName); |
| 44 | res[componentPrefix + "sensor-data/value"] = value; |
Tomáš Pecka | 655062c | 2023-12-11 15:37:58 +0100 | [diff] [blame] | 45 | res[componentPrefix + "sensor-data/oper-status"] = operStatus; |
| 46 | } |
| 47 | |
| 48 | /** @brief Write a sensor-data @p value for a component @p componentName and push it into the @p res DataTree */ |
Tomáš Pecka | cd7f9cc | 2023-12-11 15:52:48 +0100 | [diff] [blame] | 49 | void addSensorValue(velia::Log log, velia::ietf_hardware::DataTree& res, const std::string& componentName, int64_t value) |
Tomáš Pecka | 655062c | 2023-12-11 15:37:58 +0100 | [diff] [blame] | 50 | { |
Tomáš Pecka | cd7f9cc | 2023-12-11 15:52:48 +0100 | [diff] [blame] | 51 | static constexpr const int64_t YANG_SENSOR_VALUE_MIN = -1'000'000'000; |
| 52 | static constexpr const int64_t YANG_SENSOR_VALUE_MAX = 1'000'000'000; |
| 53 | |
| 54 | // FIXME: Valid value range depends on sensor-type as well, see ietf-hardware's sensor-value type description |
| 55 | |
| 56 | if (value <= YANG_SENSOR_VALUE_MIN) { |
| 57 | log->error("Sensor's '{}' value '{}' underflow. Setting sensor as nonoperational.", componentName, value); |
| 58 | writeSensorValue(res, componentName, std::to_string(YANG_SENSOR_VALUE_MIN), "nonoperational"); |
| 59 | } else if (value >= YANG_SENSOR_VALUE_MAX) { |
| 60 | log->error("Sensor's '{}' value '{}' overflow. Setting sensor as nonoperational.", componentName, value); |
| 61 | writeSensorValue(res, componentName, std::to_string(YANG_SENSOR_VALUE_MAX), "nonoperational"); |
| 62 | } else { |
| 63 | writeSensorValue(res, componentName, std::to_string(value), "ok"); |
| 64 | } |
Tomáš Pecka | 655062c | 2023-12-11 15:37:58 +0100 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /** @brief Write a sensor-data @p value for a component @p componentName and push it into the @p res DataTree */ |
Tomáš Pecka | cd7f9cc | 2023-12-11 15:52:48 +0100 | [diff] [blame] | 68 | void addSensorValue(velia::Log, velia::ietf_hardware::DataTree& res, const std::string& componentName, const std::string& value) |
Tomáš Pecka | 655062c | 2023-12-11 15:37:58 +0100 | [diff] [blame] | 69 | { |
| 70 | // TODO: Perhaps we should check if the string value is conforming to sensor-value type |
| 71 | writeSensorValue(res, componentName, value, "ok"); |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
| 75 | namespace velia::ietf_hardware { |
| 76 | |
Tomáš Pecka | c0991ce | 2023-12-20 15:46:03 +0100 | [diff] [blame] | 77 | void SensorPollData::merge(SensorPollData&& other) |
| 78 | { |
| 79 | data.merge(other.data); |
| 80 | thresholds.merge(other.thresholds); |
Tomáš Pecka | 26b3821 | 2024-01-16 17:23:31 +0100 | [diff] [blame] | 81 | sideLoadedAlarms.merge(other.sideLoadedAlarms); |
Tomáš Pecka | c0991ce | 2023-12-20 15:46:03 +0100 | [diff] [blame] | 82 | } |
| 83 | |
Tomáš Pecka | 0d8d8ee | 2023-05-10 12:22:58 +0200 | [diff] [blame] | 84 | IETFHardware::IETFHardware() |
| 85 | : m_log(spdlog::get("hardware")) |
| 86 | { |
| 87 | } |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 88 | |
| 89 | IETFHardware::~IETFHardware() = default; |
| 90 | |
Tomáš Pecka | 0d8d8ee | 2023-05-10 12:22:58 +0200 | [diff] [blame] | 91 | /** |
| 92 | * Calls individual registered data readers and processes information obtained from them. |
| 93 | * The sensor values are then passed to threshold watchers to detect if the new value violated a threshold. |
| 94 | * This function does not raise any alarms. It only returns the data tree *and* any changes in the threshold |
| 95 | * crossings (as a mapping from sensor XPath to threshold watcher State (@see Watcher)). |
| 96 | **/ |
| 97 | HardwareInfo IETFHardware::process() |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 98 | { |
Tomáš Pecka | c0991ce | 2023-12-20 15:46:03 +0100 | [diff] [blame] | 99 | SensorPollData pollData; |
| 100 | std::set<std::string> activeSensors; |
Tomáš Pecka | 0d8d8ee | 2023-05-10 12:22:58 +0200 | [diff] [blame] | 101 | std::map<std::string, State> alarms; |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 102 | |
| 103 | for (auto& dataReader : m_callbacks) { |
Tomáš Pecka | c0991ce | 2023-12-20 15:46:03 +0100 | [diff] [blame] | 104 | pollData.merge(dataReader()); |
| 105 | } |
| 106 | |
| 107 | /* the thresholds watchers are created dynamically |
| 108 | * - when a new sensor occurs then we add a new watcher |
| 109 | * - when a sensor disappears we remove the corresponding watcher |
| 110 | */ |
| 111 | for (const auto& [sensorXPath, sensorThresholds] : pollData.thresholds) { |
| 112 | if (!m_thresholdsWatchers.contains(sensorXPath)) { |
| 113 | m_thresholdsWatchers.emplace(sensorXPath, sensorThresholds); |
| 114 | } |
| 115 | activeSensors.emplace(sensorXPath); |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 116 | } |
| 117 | |
Tomáš Pecka | 0d8d8ee | 2023-05-10 12:22:58 +0200 | [diff] [blame] | 118 | for (auto& [sensorXPath, thresholdsWatcher] : m_thresholdsWatchers) { |
| 119 | std::optional<int64_t> newValue; |
| 120 | |
Tomáš Pecka | c0991ce | 2023-12-20 15:46:03 +0100 | [diff] [blame] | 121 | if (auto it = pollData.data.find(sensorXPath); it != pollData.data.end()) { |
Tomáš Pecka | 0d8d8ee | 2023-05-10 12:22:58 +0200 | [diff] [blame] | 122 | newValue = std::stoll(it->second); |
| 123 | } else { |
| 124 | newValue = std::nullopt; |
| 125 | } |
| 126 | |
| 127 | if (auto newState = thresholdsWatcher.update(newValue)) { |
| 128 | m_log->debug("threshold: {} {}", sensorXPath, *newState); |
| 129 | alarms.emplace(sensorXPath, *newState); |
| 130 | } |
| 131 | } |
| 132 | |
Tomáš Pecka | c0991ce | 2023-12-20 15:46:03 +0100 | [diff] [blame] | 133 | pollData.data[ietfHardwareStatePrefix + "/last-change"] = velia::utils::yangTimeFormat(std::chrono::system_clock::now()); |
Tomáš Pecka | 0d8d8ee | 2023-05-10 12:22:58 +0200 | [diff] [blame] | 134 | |
Tomáš Pecka | 26b3821 | 2024-01-16 17:23:31 +0100 | [diff] [blame] | 135 | return {pollData.data, alarms, activeSensors, pollData.sideLoadedAlarms}; |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Tomáš Pecka | c0991ce | 2023-12-20 15:46:03 +0100 | [diff] [blame] | 138 | void IETFHardware::registerDataReader(const IETFHardware::DataReader& callable) |
Tomáš Pecka | d2322ad | 2023-05-11 16:20:38 +0200 | [diff] [blame] | 139 | { |
Tomáš Pecka | c0991ce | 2023-12-20 15:46:03 +0100 | [diff] [blame] | 140 | m_callbacks.push_back(callable); |
Tomáš Pecka | d2322ad | 2023-05-11 16:20:38 +0200 | [diff] [blame] | 141 | } |
| 142 | |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 143 | /** @brief A namespace containing predefined data readers for IETFHardware class. |
| 144 | * @see IETFHardware for more information |
| 145 | */ |
| 146 | namespace data_reader { |
| 147 | |
| 148 | DataReader::DataReader(std::string componentName, std::optional<std::string> parent) |
| 149 | : m_componentName(std::move(componentName)) |
| 150 | , m_parent(std::move(parent)) |
Tomáš Pecka | cd7f9cc | 2023-12-11 15:52:48 +0100 | [diff] [blame] | 151 | , m_log(spdlog::get("hardware")) |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 152 | { |
| 153 | } |
| 154 | |
| 155 | /** @brief Constructs a component without any sensor-data and provide only the static data @p dataTree passed via constructor. |
| 156 | * @param componentName the name of the component in the resulting tree |
| 157 | * @param parent The component in YANG model has a link to parent. Specify who is the parent. |
| 158 | * @param dataTree static data to insert into the resulting tree. The dataTree keys should only contain the YANG node name, not full XPath. The full XPath is constructed from @componentName and the map key. |
| 159 | */ |
| 160 | StaticData::StaticData(std::string componentName, std::optional<std::string> parent, DataTree dataTree) |
| 161 | : DataReader(std::move(componentName), std::move(parent)) |
| 162 | { |
| 163 | addComponent(m_staticData, |
| 164 | m_componentName, |
| 165 | m_parent, |
| 166 | dataTree); |
| 167 | } |
| 168 | |
Tomáš Pecka | 26b3821 | 2024-01-16 17:23:31 +0100 | [diff] [blame] | 169 | SensorPollData StaticData::operator()() const { return {m_staticData, {}, {}}; } |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 170 | |
Tomáš Pecka | 4886db2 | 2023-05-10 10:46:15 +0200 | [diff] [blame] | 171 | Fans::Fans(std::string componentName, std::optional<std::string> parent, std::shared_ptr<sysfs::HWMon> hwmon, unsigned fanChannelsCount, Thresholds<int64_t> thresholds) |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 172 | : DataReader(std::move(componentName), std::move(parent)) |
| 173 | , m_hwmon(std::move(hwmon)) |
| 174 | , m_fanChannelsCount(fanChannelsCount) |
Tomáš Pecka | 4886db2 | 2023-05-10 10:46:15 +0200 | [diff] [blame] | 175 | , m_thresholds(std::move(thresholds)) |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 176 | { |
| 177 | // fans |
| 178 | addComponent(m_staticData, |
| 179 | m_componentName, |
| 180 | m_parent, |
Tomáš Pecka | 77e09c2 | 2023-05-04 20:39:57 +0200 | [diff] [blame] | 181 | DataTree{ |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 182 | {"class", "iana-hardware:module"}, // FIXME: Read (or pass via constructor) additional properties (mfg, model, ...). They should be in the fans' tray EEPROM. |
| 183 | }); |
| 184 | |
| 185 | for (unsigned i = 1; i <= m_fanChannelsCount; i++) { |
| 186 | // fans -> fan_i |
| 187 | addComponent(m_staticData, |
| 188 | m_componentName + ":fan" + std::to_string(i), |
| 189 | m_componentName, |
Tomáš Pecka | 77e09c2 | 2023-05-04 20:39:57 +0200 | [diff] [blame] | 190 | DataTree{ |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 191 | {"class", "iana-hardware:fan"}, |
| 192 | }); |
| 193 | |
| 194 | // fans -> fan_i -> sensor-data |
| 195 | addComponent(m_staticData, |
| 196 | m_componentName + ":fan" + std::to_string(i) + ":rpm", |
| 197 | m_componentName + ":fan" + std::to_string(i), |
Tomáš Pecka | 77e09c2 | 2023-05-04 20:39:57 +0200 | [diff] [blame] | 198 | DataTree{ |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 199 | {"class", "iana-hardware:sensor"}, |
| 200 | {"sensor-data/value-type", "rpm"}, |
| 201 | {"sensor-data/value-scale", "units"}, |
| 202 | {"sensor-data/value-precision", "0"}, |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 203 | }); |
| 204 | } |
| 205 | } |
| 206 | |
Tomáš Pecka | c0991ce | 2023-12-20 15:46:03 +0100 | [diff] [blame] | 207 | SensorPollData Fans::operator()() const |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 208 | { |
Tomáš Pecka | c0991ce | 2023-12-20 15:46:03 +0100 | [diff] [blame] | 209 | DataTree data(m_staticData); |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 210 | for (unsigned i = 1; i <= m_fanChannelsCount; i++) { |
| 211 | const auto sensorComponentName = m_componentName + ":fan" + std::to_string(i) + ":rpm"; |
| 212 | const auto attribute = "fan"s + std::to_string(i) + "_input"; |
| 213 | |
Tomáš Pecka | c0991ce | 2023-12-20 15:46:03 +0100 | [diff] [blame] | 214 | addSensorValue(m_log, data, sensorComponentName, m_hwmon->attribute(attribute)); |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 215 | } |
| 216 | |
Tomáš Pecka | c0991ce | 2023-12-20 15:46:03 +0100 | [diff] [blame] | 217 | ThresholdsBySensorPath thr; |
Tomáš Pecka | 4886db2 | 2023-05-10 10:46:15 +0200 | [diff] [blame] | 218 | for (unsigned i = 1; i <= m_fanChannelsCount; i++) { |
Tomáš Pecka | c0991ce | 2023-12-20 15:46:03 +0100 | [diff] [blame] | 219 | thr.emplace(xpathForComponent(m_componentName + ":fan" + std::to_string(i) + ":rpm") + "sensor-data/value", m_thresholds); |
Tomáš Pecka | 4886db2 | 2023-05-10 10:46:15 +0200 | [diff] [blame] | 220 | } |
| 221 | |
Tomáš Pecka | 26b3821 | 2024-01-16 17:23:31 +0100 | [diff] [blame] | 222 | return {data, thr, {}}; |
Tomáš Pecka | 4886db2 | 2023-05-10 10:46:15 +0200 | [diff] [blame] | 223 | } |
| 224 | |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 225 | std::string getSysfsFilename(const SensorType type, int sysfsChannelNr) |
| 226 | { |
| 227 | switch (type) { |
Tomáš Pecka | 77e09c2 | 2023-05-04 20:39:57 +0200 | [diff] [blame] | 228 | case SensorType::Temperature: |
| 229 | return "temp"s + std::to_string(sysfsChannelNr) + "_input"; |
| 230 | case SensorType::Current: |
| 231 | return "curr"s + std::to_string(sysfsChannelNr) + "_input"; |
| 232 | case SensorType::Power: |
| 233 | return "power"s + std::to_string(sysfsChannelNr) + "_input"; |
| 234 | case SensorType::VoltageAC: |
| 235 | case SensorType::VoltageDC: |
| 236 | return "in"s + std::to_string(sysfsChannelNr) + "_input"; |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | __builtin_unreachable(); |
| 240 | } |
| 241 | |
Tomáš Pecka | 77e09c2 | 2023-05-04 20:39:57 +0200 | [diff] [blame] | 242 | template <SensorType TYPE> |
| 243 | const DataTree sysfsStaticData; |
| 244 | template <> |
| 245 | const DataTree sysfsStaticData<SensorType::Temperature> = { |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 246 | {"class", "iana-hardware:sensor"}, |
| 247 | {"sensor-data/value-type", "celsius"}, |
| 248 | {"sensor-data/value-scale", "milli"}, |
| 249 | {"sensor-data/value-precision", "0"}, |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 250 | }; |
Tomáš Pecka | 77e09c2 | 2023-05-04 20:39:57 +0200 | [diff] [blame] | 251 | template <> |
| 252 | const DataTree sysfsStaticData<SensorType::Current> = { |
Václav Kubernát | 97e5ea1 | 2021-03-24 00:36:57 +0100 | [diff] [blame] | 253 | {"class", "iana-hardware:sensor"}, |
| 254 | {"sensor-data/value-type", "amperes"}, |
| 255 | {"sensor-data/value-scale", "milli"}, |
| 256 | {"sensor-data/value-precision", "0"}, |
Václav Kubernát | 97e5ea1 | 2021-03-24 00:36:57 +0100 | [diff] [blame] | 257 | }; |
Tomáš Pecka | 77e09c2 | 2023-05-04 20:39:57 +0200 | [diff] [blame] | 258 | template <> |
| 259 | const DataTree sysfsStaticData<SensorType::Power> = { |
Václav Kubernát | 97e5ea1 | 2021-03-24 00:36:57 +0100 | [diff] [blame] | 260 | {"class", "iana-hardware:sensor"}, |
| 261 | {"sensor-data/value-type", "watts"}, |
| 262 | {"sensor-data/value-scale", "micro"}, |
| 263 | {"sensor-data/value-precision", "0"}, |
Václav Kubernát | 97e5ea1 | 2021-03-24 00:36:57 +0100 | [diff] [blame] | 264 | }; |
Tomáš Pecka | 77e09c2 | 2023-05-04 20:39:57 +0200 | [diff] [blame] | 265 | template <> |
| 266 | const DataTree sysfsStaticData<SensorType::VoltageAC> = { |
Václav Kubernát | 97e5ea1 | 2021-03-24 00:36:57 +0100 | [diff] [blame] | 267 | {"class", "iana-hardware:sensor"}, |
| 268 | {"sensor-data/value-type", "volts-AC"}, |
Tomáš Pecka | 7632360 | 2022-05-11 09:33:59 +0200 | [diff] [blame] | 269 | {"sensor-data/value-scale", "milli"}, |
Václav Kubernát | 97e5ea1 | 2021-03-24 00:36:57 +0100 | [diff] [blame] | 270 | {"sensor-data/value-precision", "0"}, |
Tomáš Pecka | 5f07eea | 2023-12-11 15:23:01 +0100 | [diff] [blame] | 271 | }; |
Tomáš Pecka | 77e09c2 | 2023-05-04 20:39:57 +0200 | [diff] [blame] | 272 | template <> |
| 273 | const DataTree sysfsStaticData<SensorType::VoltageDC> = { |
Václav Kubernát | 97e5ea1 | 2021-03-24 00:36:57 +0100 | [diff] [blame] | 274 | {"class", "iana-hardware:sensor"}, |
| 275 | {"sensor-data/value-type", "volts-DC"}, |
Tomáš Pecka | 7632360 | 2022-05-11 09:33:59 +0200 | [diff] [blame] | 276 | {"sensor-data/value-scale", "milli"}, |
Václav Kubernát | 97e5ea1 | 2021-03-24 00:36:57 +0100 | [diff] [blame] | 277 | {"sensor-data/value-precision", "0"}, |
Tomáš Pecka | 5f07eea | 2023-12-11 15:23:01 +0100 | [diff] [blame] | 278 | }; |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 279 | |
| 280 | template <SensorType TYPE> |
Tomáš Pecka | 4886db2 | 2023-05-10 10:46:15 +0200 | [diff] [blame] | 281 | SysfsValue<TYPE>::SysfsValue(std::string componentName, std::optional<std::string> parent, std::shared_ptr<sysfs::HWMon> hwmon, int sysfsChannelNr, Thresholds<int64_t> thresholds) |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 282 | : DataReader(std::move(componentName), std::move(parent)) |
| 283 | , m_hwmon(std::move(hwmon)) |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 284 | , m_sysfsFile(getSysfsFilename(TYPE, sysfsChannelNr)) |
Tomáš Pecka | 4886db2 | 2023-05-10 10:46:15 +0200 | [diff] [blame] | 285 | , m_thresholds(std::move(thresholds)) |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 286 | { |
| 287 | addComponent(m_staticData, |
| 288 | m_componentName, |
| 289 | m_parent, |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 290 | sysfsStaticData<TYPE>); |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 291 | } |
| 292 | |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 293 | template <SensorType TYPE> |
Tomáš Pecka | c0991ce | 2023-12-20 15:46:03 +0100 | [diff] [blame] | 294 | SensorPollData SysfsValue<TYPE>::operator()() const |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 295 | { |
| 296 | DataTree res(m_staticData); |
| 297 | |
Václav Kubernát | 0dee6b9 | 2021-04-13 09:14:04 +0200 | [diff] [blame] | 298 | int64_t sensorValue = m_hwmon->attribute(m_sysfsFile); |
Tomáš Pecka | cd7f9cc | 2023-12-11 15:52:48 +0100 | [diff] [blame] | 299 | addSensorValue(m_log, res, m_componentName, sensorValue); |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 300 | |
Tomáš Pecka | 26b3821 | 2024-01-16 17:23:31 +0100 | [diff] [blame] | 301 | return {res, ThresholdsBySensorPath{{xpathForComponent(m_componentName) + "sensor-data/value", m_thresholds}}, {}}; |
Tomáš Pecka | 4886db2 | 2023-05-10 10:46:15 +0200 | [diff] [blame] | 302 | } |
| 303 | |
Václav Kubernát | 97e5ea1 | 2021-03-24 00:36:57 +0100 | [diff] [blame] | 304 | template struct SysfsValue<SensorType::Current>; |
| 305 | template struct SysfsValue<SensorType::Power>; |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 306 | template struct SysfsValue<SensorType::Temperature>; |
Václav Kubernát | 97e5ea1 | 2021-03-24 00:36:57 +0100 | [diff] [blame] | 307 | template struct SysfsValue<SensorType::VoltageAC>; |
| 308 | template struct SysfsValue<SensorType::VoltageDC>; |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 309 | |
Tomáš Pecka | 4886db2 | 2023-05-10 10:46:15 +0200 | [diff] [blame] | 310 | EMMC::EMMC(std::string componentName, std::optional<std::string> parent, std::shared_ptr<sysfs::EMMC> emmc, Thresholds<int64_t> thresholds) |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 311 | : DataReader(std::move(componentName), std::move(parent)) |
| 312 | , m_emmc(std::move(emmc)) |
Tomáš Pecka | 4886db2 | 2023-05-10 10:46:15 +0200 | [diff] [blame] | 313 | , m_thresholds(std::move(thresholds)) |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 314 | { |
| 315 | auto emmcAttrs = m_emmc->attributes(); |
| 316 | |
| 317 | // date is specified in MM/YYYY format (source: kernel core/mmc.c) and mfg-date is unfortunately of type yang:date-and-time |
| 318 | std::string mfgDate = emmcAttrs.at("date"); |
Tomáš Pecka | f1cba74 | 2023-05-03 16:26:37 +0200 | [diff] [blame] | 319 | std::chrono::year_month_day calendarDate( |
| 320 | std::chrono::year(std::stoi(mfgDate.substr(3, 4))), |
| 321 | std::chrono::month(std::stoi(mfgDate.substr(0, 2))), |
| 322 | std::chrono::day(1)); |
| 323 | mfgDate = velia::utils::yangTimeFormat(std::chrono::sys_days{calendarDate}); |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 324 | |
| 325 | addComponent(m_staticData, |
| 326 | m_componentName, |
| 327 | m_parent, |
Tomáš Pecka | 77e09c2 | 2023-05-04 20:39:57 +0200 | [diff] [blame] | 328 | DataTree{ |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 329 | {"class", "iana-hardware:module"}, |
| 330 | {"mfg-date", mfgDate}, |
| 331 | {"serial-num", emmcAttrs.at("serial")}, |
| 332 | {"model-name", emmcAttrs.at("name")}, |
| 333 | }); |
| 334 | |
| 335 | addComponent(m_staticData, |
| 336 | m_componentName + ":lifetime", |
| 337 | m_componentName, |
Tomáš Pecka | 77e09c2 | 2023-05-04 20:39:57 +0200 | [diff] [blame] | 338 | DataTree{ |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 339 | {"class", "iana-hardware:sensor"}, |
| 340 | {"sensor-data/value-type", "other"}, |
| 341 | {"sensor-data/value-scale", "units"}, |
| 342 | {"sensor-data/value-precision", "0"}, |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 343 | {"sensor-data/units-display", "percent"s}, |
| 344 | }); |
| 345 | } |
| 346 | |
Tomáš Pecka | c0991ce | 2023-12-20 15:46:03 +0100 | [diff] [blame] | 347 | SensorPollData EMMC::operator()() const |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 348 | { |
Tomáš Pecka | c0991ce | 2023-12-20 15:46:03 +0100 | [diff] [blame] | 349 | DataTree data(m_staticData); |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 350 | |
| 351 | auto emmcAttrs = m_emmc->attributes(); |
Tomáš Pecka | c0991ce | 2023-12-20 15:46:03 +0100 | [diff] [blame] | 352 | addSensorValue(m_log, data, m_componentName + ":lifetime", emmcAttrs.at("life_time")); |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 353 | |
Tomáš Pecka | 26b3821 | 2024-01-16 17:23:31 +0100 | [diff] [blame] | 354 | return {data, ThresholdsBySensorPath{{xpathForComponent(m_componentName + ":lifetime") + "sensor-data/value", m_thresholds}}, {}}; |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 355 | } |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 356 | } |
| 357 | } |