blob: b02f204f85de47466927a3cba0242e692f2e6fda [file] [log] [blame]
Tomáš Pecka339bc672020-11-11 15:59:03 +01001/*
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#pragma once
9
10#include <functional>
11#include <map>
12#include <utility>
13#include "ietf-hardware/sysfs/EMMC.h"
14#include "ietf-hardware/sysfs/HWMon.h"
15#include "utils/log-fwd.h"
16
17using namespace std::literals;
18
19namespace velia::ietf_hardware {
20
21using DataTree = std::map<std::string, std::string>;
22
23/**
24 * @brief Readout of hardware-state related data according to RFC 8348 (App. A)
25 *
26 * IETFHardware implements readout of various hardware component data and provides them as a mapping
27 * nodePath -> value, where nodePath is an XPath conforming to ietf-hardware-state module (RFC 8348, App. A).
28 *
29 * The class is designed to be modular. IETFHardware does not do much, its only capabilities are:
30 * - Register data readers responsible for readout of data for individual hardware, and
31 * - ask registered data readers to provide the data.
32 *
33 * The data readers (IETFHardware::DataReader) are simple functors with signature DataTree() (i.e., std::map<std::string, std::string>())
34 * returning parts of the tree in the form described above (i.e., mapping nodePath -> value).
35 *
36 * The IETFHardware is *not aware* of Sysrepo.
37 * However, the data readers are aware of the tree structure of the YANG module ietf-hardware-state.
38 * That is because they also create the specified parts of the resulting tree.
39 *
40 * @see IETFHardware::DataReader The data reader.
41 * @see velia::ietf_hardware::data_reader Namespace containing several predefined components.
42 */
43class IETFHardware {
44
45public:
46 /** @brief The component */
47 using DataReader = std::function<DataTree()>;
48
49 IETFHardware();
50 ~IETFHardware();
51 void registerDataReader(const DataReader& callable);
52
53 DataTree process();
54
55private:
56 /** @brief registered components for individual modules */
57 std::vector<DataReader> m_callbacks;
58};
59
60/**
61 * This namespace contains several predefined data readers for IETFHardware.
62 * They are implemented as functors and fulfill the required interface -- std::function<DataTree()>
63 *
64 * The philosophy here is to initialize Component::m_staticData DataTree when constructing the object so the tree does not have to be completely reconstructed every time.
65 * When IETFHardwareState fetches the data from the data reader, an operator() is invoked.
66 * The dynamic data are fetched by IETFHardware class by the use of the operator().
67 * The result of call to operator() will be merged into the static data and returned to caller (IETFHardwareState).
68 *
69 * Note that a data reader can return any number of nodes and even multiple compoments.
70 * For example, Fans data reader will create multiple components in the tree, one for each fan.
71 */
72namespace data_reader {
73
74struct DataReader {
75 /** @brief name of the module component in the tree, e.g. ne:fans:fan1 */
76 std::string m_componentName;
77
78 /** @brief name of the parent module */
79 std::optional<std::string> m_parent;
80
81 /** @brief static hw-state related data */
82 DataTree m_staticData;
83
84 DataReader(std::string propertyPrefix, std::optional<std::string> parent);
85};
86
87/** @brief Manages any component composing of static data only. The static data are provided as a DataTree in construction time. */
88struct StaticData : private DataReader {
89 StaticData(std::string propertyPrefix, std::optional<std::string> parent, DataTree tree);
90 DataTree operator()() const;
91};
92
93/** @brief Manages fans component. Data is provided by a sysfs::HWMon object. */
94struct Fans : private DataReader {
95private:
96 std::shared_ptr<sysfs::HWMon> m_hwmon;
97 unsigned m_fanChannelsCount;
98
99public:
100 Fans(std::string propertyPrefix, std::optional<std::string> parent, std::shared_ptr<sysfs::HWMon> hwmon, unsigned fanChannelsCount);
101 DataTree operator()() const;
102};
103
104/** @brief Manages a single temperature sensor, data is provided by a sysfs::HWMon object. */
105struct SysfsTemperature : private DataReader {
106private:
107 std::shared_ptr<sysfs::HWMon> m_hwmon;
108 std::string m_sysfsTemperatureFile;
109
110public:
111 SysfsTemperature(std::string propertyPrefix, std::optional<std::string> parent, std::shared_ptr<sysfs::HWMon> hwmon, int sysfsChannelNr);
112 DataTree operator()() const;
113};
114
115/** @brief Manages a single eMMC block device hardware component. Data is provided by a sysfs::EMMC object. */
116struct EMMC : private DataReader {
117private:
118 std::shared_ptr<sysfs::EMMC> m_emmc;
119
120public:
121 EMMC(std::string propertyPrefix, std::optional<std::string> parent, std::shared_ptr<sysfs::EMMC> emmc);
122 DataTree operator()() const;
123};
124}
125}