diff options
author | Matthew Barth <msbarth@us.ibm.com> | 2018-04-17 11:37:15 -0500 |
---|---|---|
committer | Matthew Barth <msbarth@us.ibm.com> | 2018-04-23 13:18:17 -0500 |
commit | 979c806dc6d236711a5eb7752d61b8895036192d (patch) | |
tree | 18cb08dd64fd98bb07e9cd569837d8ada5e785a9 | |
parent | b798527cb8c3425f6ad26c68ca67456272e87afa (diff) | |
download | phosphor-hwmon-979c806dc6d236711a5eb7752d61b8895036192d.tar.gz phosphor-hwmon-979c806dc6d236711a5eb7752d61b8895036192d.zip |
Get sensor identifiers function
To minimize handling the return cases when getting a sensor object,
retrieve the sensor identifiers from a function. The identifiers
returned are then checked to be valid before proceeding in creating the
sensor object.
Tested:
Sensor objects created the same
Empty id or label cause sensor object to not be created
Change-Id: I7c2f0df3fee99448af5365e0a40c4282f9a235fa
Signed-off-by: Matthew Barth <msbarth@us.ibm.com>
-rw-r--r-- | mainloop.cpp | 62 | ||||
-rw-r--r-- | mainloop.hpp | 19 |
2 files changed, 63 insertions, 18 deletions
diff --git a/mainloop.cpp b/mainloop.cpp index 03c856d..9a48755 100644 --- a/mainloop.cpp +++ b/mainloop.cpp @@ -195,17 +195,8 @@ auto addValue(const SensorSet::key_type& sensor, return iface; } -/** - * Reads the environment parameters of a sensor and creates an object with - * atleast the `Value` interface, otherwise returns without creating the object. - * If the `Value` interface is successfully created, by reading the sensor's - * corresponding sysfs file's value, the additional interfaces for the sensor - * are created and the InterfacesAdded signal is emitted. The sensor is then - * moved to the list for sensor state monitoring within the main loop. - */ -void MainLoop::getObject(SensorSet::container_t::const_reference sensor) +std::string MainLoop::getID(SensorSet::container_t::const_reference sensor) { - std::string label; std::string id; /* @@ -224,7 +215,7 @@ void MainLoop::getObject(SensorSet::container_t::const_reference sensor) if (id.empty()) { - return; + return id; } } @@ -232,9 +223,38 @@ void MainLoop::getObject(SensorSet::container_t::const_reference sensor) // otherwise use the standard one. id = (id.empty()) ? sensor.first.second : id; - // Ignore inputs without a label. - label = env::getEnv("LABEL", sensor.first.first, id); - if (label.empty()) + return id; +} + +SensorIdentifiers MainLoop::getIdentifiers( + SensorSet::container_t::const_reference sensor) +{ + std::string id = getID(sensor); + std::string label; + + if (!id.empty()) + { + // Ignore inputs without a label. + label = env::getEnv("LABEL", sensor.first.first, id); + } + + return std::make_tuple(std::move(id), + std::move(label)); +} + +/** + * Reads the environment parameters of a sensor and creates an object with + * atleast the `Value` interface, otherwise returns without creating the object. + * If the `Value` interface is successfully created, by reading the sensor's + * corresponding sysfs file's value, the additional interfaces for the sensor + * are created and the InterfacesAdded signal is emitted. The sensor is then + * moved to the list for sensor state monitoring within the main loop. + */ +void MainLoop::getObject(SensorSet::container_t::const_reference sensor) +{ + auto properties = getIdentifiers(sensor); + if (std::get<sensorID>(properties).empty() || + std::get<sensorLabel>(properties).empty()) { return; } @@ -254,7 +274,7 @@ void MainLoop::getObject(SensorSet::container_t::const_reference sensor) objectPath.append(1, '/'); objectPath.append(hwmon::getNamespace(attrs)); objectPath.append(1, '/'); - objectPath.append(label); + objectPath.append(std::get<sensorLabel>(properties)); ObjectInfo info(&_bus, std::move(objectPath), Object()); RetryIO retryIO(sysfs::hwmonio::retries, sysfs::hwmonio::delay); @@ -317,8 +337,14 @@ void MainLoop::getObject(SensorSet::container_t::const_reference sensor) #endif } auto sensorValue = valueInterface->value(); - addThreshold<WarningObject>(sensor.first.first, id, sensorValue, info); - addThreshold<CriticalObject>(sensor.first.first, id, sensorValue, info); + addThreshold<WarningObject>(sensor.first.first, + std::get<sensorID>(properties), + sensorValue, + info); + addThreshold<CriticalObject>(sensor.first.first, + std::get<sensorID>(properties), + sensorValue, + info); auto target = addTarget<hwmon::FanSpeed>( sensor.first, ioAccess, _devPath, info); @@ -334,7 +360,7 @@ void MainLoop::getObject(SensorSet::container_t::const_reference sensor) auto value = std::make_tuple( std::move(sensor.second), - std::move(label), + std::move(std::get<sensorLabel>(properties)), std::move(info)); state[std::move(sensor.first)] = std::move(value); diff --git a/mainloop.hpp b/mainloop.hpp index 9a51580..0453d8e 100644 --- a/mainloop.hpp +++ b/mainloop.hpp @@ -16,6 +16,10 @@ using Object = std::map<InterfaceType, std::experimental::any>; using ObjectInfo = std::tuple<sdbusplus::bus::bus*, std::string, Object>; using RetryIO = std::tuple<size_t, std::chrono::milliseconds>; +static constexpr auto sensorID = 0; +static constexpr auto sensorLabel = 1; +using SensorIdentifiers = std::tuple<std::string, std::string>; + /** @class MainLoop * @brief hwmon-readd main application loop. */ @@ -104,6 +108,21 @@ class MainLoop std::map<SensorSet::key_type, SensorSet::mapped_type> rmSensors; /** + * @brief Get the ID of the sensor + * + * @param[in] sensor - Sensor to get the ID of + */ + std::string getID(SensorSet::container_t::const_reference sensor); + + /** + * @brief Get the sensor identifiers + * + * @param[in] sensor - Sensor to get the identifiers of + */ + SensorIdentifiers getIdentifiers( + SensorSet::container_t::const_reference sensor); + + /** * @brief Used to create and add sensor objects * * @param[in] sensor - Sensor to create/add object for |