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 | |
| 8 | #include <utility> |
| 9 | #include "IETFHardware.h" |
| 10 | #include "utils/log.h" |
| 11 | #include "utils/time.h" |
| 12 | |
| 13 | using namespace std::literals; |
| 14 | |
| 15 | namespace { |
| 16 | |
Tomáš Pecka | 83b62e1 | 2020-12-16 14:50:49 +0100 | [diff] [blame] | 17 | static const std::string ietfHardwareStatePrefix = "/ietf-hardware:hardware"; |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 18 | |
| 19 | /** @brief Constructs a full XPath for a specific component */ |
| 20 | std::string xpathForComponent(const std::string& componentName) |
| 21 | { |
| 22 | return ietfHardwareStatePrefix + "/component[name='" + componentName + "']/"; |
| 23 | } |
| 24 | |
| 25 | /** @brief Prefix all properties from values DataTree with a component name (calculated from @p componentName) and push them into the DataTree */ |
| 26 | void addComponent(velia::ietf_hardware::DataTree& res, const std::string& componentName, const std::optional<std::string>& parent, const velia::ietf_hardware::DataTree& values) |
| 27 | { |
| 28 | auto componentPrefix = xpathForComponent(componentName); |
| 29 | |
| 30 | if (parent) { |
| 31 | res[componentPrefix + "parent"] = *parent; |
| 32 | } |
| 33 | for (const auto& [k, v] : values) { |
| 34 | res[componentPrefix + k] = v; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /** @brief Write a sensor-data @p value for a component @p componentName and push it into the @p res DataTree */ |
| 39 | void addSensorValue(velia::ietf_hardware::DataTree& res, const std::string& componentName, const std::string& value) |
| 40 | { |
| 41 | const auto componentPrefix = xpathForComponent(componentName); |
| 42 | res[componentPrefix + "sensor-data/value"] = value; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | namespace velia::ietf_hardware { |
| 47 | |
| 48 | IETFHardware::IETFHardware() = default; |
| 49 | |
| 50 | IETFHardware::~IETFHardware() = default; |
| 51 | |
| 52 | std::map<std::string, std::string> IETFHardware::process() |
| 53 | { |
| 54 | std::map<std::string, std::string> res; |
| 55 | |
| 56 | for (auto& dataReader : m_callbacks) { |
| 57 | res.merge(dataReader()); |
| 58 | } |
| 59 | |
| 60 | res[ietfHardwareStatePrefix + "/last-change"] = velia::utils::yangTimeFormat(std::chrono::system_clock::now()); |
| 61 | return res; |
| 62 | } |
| 63 | |
| 64 | void IETFHardware::registerDataReader(const DataReader& callable) |
| 65 | { |
| 66 | m_callbacks.push_back(callable); |
| 67 | } |
| 68 | |
| 69 | /** @brief A namespace containing predefined data readers for IETFHardware class. |
| 70 | * @see IETFHardware for more information |
| 71 | */ |
| 72 | namespace data_reader { |
| 73 | |
| 74 | DataReader::DataReader(std::string componentName, std::optional<std::string> parent) |
| 75 | : m_componentName(std::move(componentName)) |
| 76 | , m_parent(std::move(parent)) |
| 77 | { |
| 78 | } |
| 79 | |
| 80 | /** @brief Constructs a component without any sensor-data and provide only the static data @p dataTree passed via constructor. |
| 81 | * @param componentName the name of the component in the resulting tree |
| 82 | * @param parent The component in YANG model has a link to parent. Specify who is the parent. |
| 83 | * @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. |
| 84 | */ |
| 85 | StaticData::StaticData(std::string componentName, std::optional<std::string> parent, DataTree dataTree) |
| 86 | : DataReader(std::move(componentName), std::move(parent)) |
| 87 | { |
| 88 | addComponent(m_staticData, |
| 89 | m_componentName, |
| 90 | m_parent, |
| 91 | dataTree); |
| 92 | } |
| 93 | |
| 94 | DataTree StaticData::operator()() const { return m_staticData; } |
| 95 | |
| 96 | Fans::Fans(std::string componentName, std::optional<std::string> parent, std::shared_ptr<sysfs::HWMon> hwmon, unsigned fanChannelsCount) |
| 97 | : DataReader(std::move(componentName), std::move(parent)) |
| 98 | , m_hwmon(std::move(hwmon)) |
| 99 | , m_fanChannelsCount(fanChannelsCount) |
| 100 | { |
| 101 | // fans |
| 102 | addComponent(m_staticData, |
| 103 | m_componentName, |
| 104 | m_parent, |
| 105 | DataTree { |
| 106 | {"class", "iana-hardware:module"}, // FIXME: Read (or pass via constructor) additional properties (mfg, model, ...). They should be in the fans' tray EEPROM. |
| 107 | }); |
| 108 | |
| 109 | for (unsigned i = 1; i <= m_fanChannelsCount; i++) { |
| 110 | // fans -> fan_i |
| 111 | addComponent(m_staticData, |
| 112 | m_componentName + ":fan" + std::to_string(i), |
| 113 | m_componentName, |
| 114 | DataTree { |
| 115 | {"class", "iana-hardware:fan"}, |
| 116 | }); |
| 117 | |
| 118 | // fans -> fan_i -> sensor-data |
| 119 | addComponent(m_staticData, |
| 120 | m_componentName + ":fan" + std::to_string(i) + ":rpm", |
| 121 | m_componentName + ":fan" + std::to_string(i), |
| 122 | DataTree { |
| 123 | {"class", "iana-hardware:sensor"}, |
| 124 | {"sensor-data/value-type", "rpm"}, |
| 125 | {"sensor-data/value-scale", "units"}, |
| 126 | {"sensor-data/value-precision", "0"}, |
| 127 | {"sensor-data/oper-status", "ok"}, |
| 128 | }); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | DataTree Fans::operator()() const |
| 133 | { |
| 134 | DataTree res(m_staticData); |
| 135 | |
| 136 | auto attrs = m_hwmon->attributes(); |
| 137 | |
| 138 | for (unsigned i = 1; i <= m_fanChannelsCount; i++) { |
| 139 | const auto sensorComponentName = m_componentName + ":fan" + std::to_string(i) + ":rpm"; |
| 140 | const auto attribute = "fan"s + std::to_string(i) + "_input"; |
| 141 | |
| 142 | addSensorValue(res, sensorComponentName, std::to_string(attrs.at(attribute))); |
| 143 | } |
| 144 | |
| 145 | return res; |
| 146 | } |
| 147 | |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 148 | std::string getSysfsFilename(const SensorType type, int sysfsChannelNr) |
| 149 | { |
| 150 | switch (type) { |
| 151 | case SensorType::Temperature: |
| 152 | return "temp"s + std::to_string(sysfsChannelNr) + "_input"; |
| 153 | } |
| 154 | |
| 155 | __builtin_unreachable(); |
| 156 | } |
| 157 | |
| 158 | template <SensorType TYPE> const DataTree sysfsStaticData; |
| 159 | template <> const DataTree sysfsStaticData<SensorType::Temperature> = { |
| 160 | {"class", "iana-hardware:sensor"}, |
| 161 | {"sensor-data/value-type", "celsius"}, |
| 162 | {"sensor-data/value-scale", "milli"}, |
| 163 | {"sensor-data/value-precision", "0"}, |
| 164 | {"sensor-data/oper-status", "ok"}, |
| 165 | }; |
| 166 | |
| 167 | template <SensorType TYPE> |
| 168 | SysfsValue<TYPE>::SysfsValue(std::string componentName, std::optional<std::string> parent, std::shared_ptr<sysfs::HWMon> hwmon, int sysfsChannelNr) |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 169 | : DataReader(std::move(componentName), std::move(parent)) |
| 170 | , m_hwmon(std::move(hwmon)) |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 171 | , m_sysfsFile(getSysfsFilename(TYPE, sysfsChannelNr)) |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 172 | { |
| 173 | addComponent(m_staticData, |
| 174 | m_componentName, |
| 175 | m_parent, |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 176 | sysfsStaticData<TYPE>); |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 177 | } |
| 178 | |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 179 | template <SensorType TYPE> |
| 180 | DataTree SysfsValue<TYPE>::operator()() const |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 181 | { |
| 182 | DataTree res(m_staticData); |
| 183 | |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 184 | int64_t sensorValue = m_hwmon->attributes().at(m_sysfsFile); |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 185 | addSensorValue(res, m_componentName, std::to_string(sensorValue)); |
| 186 | |
| 187 | return res; |
| 188 | } |
| 189 | |
Václav Kubernát | 6c17d0a | 2021-03-29 04:55:31 +0200 | [diff] [blame] | 190 | template struct SysfsValue<SensorType::Temperature>; |
| 191 | |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 192 | EMMC::EMMC(std::string componentName, std::optional<std::string> parent, std::shared_ptr<sysfs::EMMC> emmc) |
| 193 | : DataReader(std::move(componentName), std::move(parent)) |
| 194 | , m_emmc(std::move(emmc)) |
| 195 | { |
| 196 | auto emmcAttrs = m_emmc->attributes(); |
| 197 | |
| 198 | // date is specified in MM/YYYY format (source: kernel core/mmc.c) and mfg-date is unfortunately of type yang:date-and-time |
| 199 | std::string mfgDate = emmcAttrs.at("date"); |
| 200 | mfgDate = mfgDate.substr(3, 4) + "-" + mfgDate.substr(0, 2) + "-01T00:00:00Z"; |
| 201 | |
| 202 | addComponent(m_staticData, |
| 203 | m_componentName, |
| 204 | m_parent, |
| 205 | DataTree { |
| 206 | {"class", "iana-hardware:module"}, |
| 207 | {"mfg-date", mfgDate}, |
| 208 | {"serial-num", emmcAttrs.at("serial")}, |
| 209 | {"model-name", emmcAttrs.at("name")}, |
| 210 | }); |
| 211 | |
| 212 | addComponent(m_staticData, |
| 213 | m_componentName + ":lifetime", |
| 214 | m_componentName, |
| 215 | DataTree { |
| 216 | {"class", "iana-hardware:sensor"}, |
| 217 | {"sensor-data/value-type", "other"}, |
| 218 | {"sensor-data/value-scale", "units"}, |
| 219 | {"sensor-data/value-precision", "0"}, |
| 220 | {"sensor-data/oper-status", "ok"}, |
| 221 | {"sensor-data/units-display", "percent"s}, |
| 222 | }); |
| 223 | } |
| 224 | |
| 225 | DataTree EMMC::operator()() const |
| 226 | { |
| 227 | DataTree res(m_staticData); |
| 228 | |
| 229 | auto emmcAttrs = m_emmc->attributes(); |
| 230 | addSensorValue(res, m_componentName + ":lifetime", emmcAttrs.at("life_time")); |
| 231 | |
| 232 | return res; |
| 233 | } |
| 234 | } |
| 235 | } |