Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 1 | #include "ExternalSensor.hpp" |
| 2 | |
| 3 | #include "SensorPaths.hpp" |
| 4 | |
| 5 | #include <unistd.h> |
| 6 | |
| 7 | #include <boost/algorithm/string/predicate.hpp> |
| 8 | #include <boost/algorithm/string/replace.hpp> |
| 9 | #include <boost/date_time/posix_time/posix_time.hpp> |
| 10 | #include <sdbusplus/asio/connection.hpp> |
| 11 | #include <sdbusplus/asio/object_server.hpp> |
| 12 | |
| 13 | #include <iostream> |
| 14 | #include <istream> |
| 15 | #include <limits> |
| 16 | #include <memory> |
| 17 | #include <string> |
| 18 | #include <vector> |
| 19 | |
| 20 | ExternalSensor::ExternalSensor( |
| 21 | const std::string& objectType, sdbusplus::asio::object_server& objectServer, |
| 22 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 23 | const std::string& sensorName, const std::string& sensorUnits, |
Jeff Lin | 7b7a9de | 2021-02-22 11:16:27 +0800 | [diff] [blame] | 24 | std::vector<thresholds::Threshold>&& thresholdsIn, |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 25 | const std::string& sensorConfiguration, const double& maxReading, |
| 26 | const double& minReading, const PowerState& powerState) : |
| 27 | // TODO(): When the Mutable feature is integrated, |
| 28 | // make sure all ExternalSensor instances are mutable, |
| 29 | // because that is the entire point of ExternalSensor, |
| 30 | // to accept sensor values written by an external source. |
Jeff Lin | 7b7a9de | 2021-02-22 11:16:27 +0800 | [diff] [blame] | 31 | Sensor(boost::replace_all_copy(sensorName, " ", "_"), |
| 32 | std::move(thresholdsIn), sensorConfiguration, objectType, maxReading, |
| 33 | minReading, conn, powerState), |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 34 | std::enable_shared_from_this<ExternalSensor>(), objServer(objectServer) |
| 35 | { |
| 36 | // The caller must specify what physical characteristic |
| 37 | // an external sensor is expected to be measuring, such as temperature, |
| 38 | // as, unlike others, this is not implied by device type name. |
Ed Tanous | 6cb732a | 2021-02-18 15:33:51 -0800 | [diff] [blame] | 39 | std::string dbusPath = sensor_paths::getPathForUnits(sensorUnits); |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 40 | if (dbusPath.empty()) |
| 41 | { |
| 42 | throw std::runtime_error("Units not in allow list"); |
| 43 | } |
Ed Tanous | 6cb732a | 2021-02-18 15:33:51 -0800 | [diff] [blame] | 44 | std::string objectPath = "/xyz/openbmc_project/sensors/"; |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 45 | objectPath += dbusPath; |
| 46 | objectPath += '/'; |
| 47 | objectPath += sensorName; |
| 48 | |
| 49 | sensorInterface = objectServer.add_interface( |
| 50 | objectPath, "xyz.openbmc_project.Sensor.Value"); |
| 51 | |
| 52 | if (thresholds::hasWarningInterface(thresholds)) |
| 53 | { |
| 54 | thresholdInterfaceWarning = objectServer.add_interface( |
| 55 | objectPath, "xyz.openbmc_project.Sensor.Threshold.Warning"); |
| 56 | } |
| 57 | if (thresholds::hasCriticalInterface(thresholds)) |
| 58 | { |
| 59 | thresholdInterfaceCritical = objectServer.add_interface( |
| 60 | objectPath, "xyz.openbmc_project.Sensor.Threshold.Critical"); |
| 61 | } |
| 62 | |
| 63 | association = |
| 64 | objectServer.add_interface(objectPath, association::interface); |
| 65 | setInitialProperties(conn); |
| 66 | } |
| 67 | |
| 68 | ExternalSensor::~ExternalSensor() |
| 69 | { |
| 70 | objServer.remove_interface(association); |
| 71 | objServer.remove_interface(thresholdInterfaceCritical); |
| 72 | objServer.remove_interface(thresholdInterfaceWarning); |
| 73 | objServer.remove_interface(sensorInterface); |
| 74 | } |
| 75 | |
| 76 | void ExternalSensor::checkThresholds(void) |
| 77 | { |
| 78 | thresholds::checkThresholds(this); |
| 79 | } |