blob: c6db174f49f6f1e7fe9dce621af0f3682742111d [file] [log] [blame]
Josh Lehan2a40e932020-09-02 11:48:14 -07001#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
20ExternalSensor::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 Lin7b7a9de2021-02-22 11:16:27 +080024 std::vector<thresholds::Threshold>&& thresholdsIn,
Josh Lehan2a40e932020-09-02 11:48:14 -070025 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 Lin7b7a9de2021-02-22 11:16:27 +080031 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
32 std::move(thresholdsIn), sensorConfiguration, objectType, maxReading,
33 minReading, conn, powerState),
Josh Lehan2a40e932020-09-02 11:48:14 -070034 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 Tanous6cb732a2021-02-18 15:33:51 -080039 std::string dbusPath = sensor_paths::getPathForUnits(sensorUnits);
Josh Lehan2a40e932020-09-02 11:48:14 -070040 if (dbusPath.empty())
41 {
42 throw std::runtime_error("Units not in allow list");
43 }
Ed Tanous6cb732a2021-02-18 15:33:51 -080044 std::string objectPath = "/xyz/openbmc_project/sensors/";
Josh Lehan2a40e932020-09-02 11:48:14 -070045 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
68ExternalSensor::~ExternalSensor()
69{
70 objServer.remove_interface(association);
71 objServer.remove_interface(thresholdInterfaceCritical);
72 objServer.remove_interface(thresholdInterfaceWarning);
73 objServer.remove_interface(sensorInterface);
74}
75
76void ExternalSensor::checkThresholds(void)
77{
78 thresholds::checkThresholds(this);
79}