blob: 5fba9b4822aa683754adbbe4f47541756ee7cc8a [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>
Josh Lehan2a40e932020-09-02 11:48:14 -07008#include <boost/date_time/posix_time/posix_time.hpp>
9#include <sdbusplus/asio/connection.hpp>
10#include <sdbusplus/asio/object_server.hpp>
11
Josh Lehan72432172021-03-17 13:35:43 -070012#include <chrono>
Josh Lehan2a40e932020-09-02 11:48:14 -070013#include <iostream>
14#include <istream>
15#include <limits>
16#include <memory>
17#include <string>
18#include <vector>
19
Josh Lehan72432172021-03-17 13:35:43 -070020static constexpr bool debug = false;
21
Josh Lehan2a40e932020-09-02 11:48:14 -070022ExternalSensor::ExternalSensor(
23 const std::string& objectType, sdbusplus::asio::object_server& objectServer,
24 std::shared_ptr<sdbusplus::asio::connection>& conn,
25 const std::string& sensorName, const std::string& sensorUnits,
Jeff Lin7b7a9de2021-02-22 11:16:27 +080026 std::vector<thresholds::Threshold>&& thresholdsIn,
Josh Lehan72432172021-03-17 13:35:43 -070027 const std::string& sensorConfiguration, double maxReading,
Josh Lehan03627382021-03-17 13:35:43 -070028 double minReading, double timeoutSecs, const PowerState& powerState) :
Zhikui Renda98f092021-11-01 09:41:08 -070029 Sensor(escapeName(sensorName), std::move(thresholdsIn), sensorConfiguration,
30 objectType, true, true, maxReading, minReading, conn, powerState),
Josh Lehan72432172021-03-17 13:35:43 -070031 std::enable_shared_from_this<ExternalSensor>(), objServer(objectServer),
32 writeLast(std::chrono::steady_clock::now()),
33 writeTimeout(
34 std::chrono::duration_cast<std::chrono::steady_clock::duration>(
35 std::chrono::duration<double>(timeoutSecs))),
Josh Lehan03627382021-03-17 13:35:43 -070036 writeAlive(false), writePerishable(timeoutSecs > 0.0)
Josh Lehan2a40e932020-09-02 11:48:14 -070037{
38 // The caller must specify what physical characteristic
39 // an external sensor is expected to be measuring, such as temperature,
40 // as, unlike others, this is not implied by device type name.
Ed Tanous6cb732a2021-02-18 15:33:51 -080041 std::string dbusPath = sensor_paths::getPathForUnits(sensorUnits);
Josh Lehan2a40e932020-09-02 11:48:14 -070042 if (dbusPath.empty())
43 {
44 throw std::runtime_error("Units not in allow list");
45 }
Ed Tanous6cb732a2021-02-18 15:33:51 -080046 std::string objectPath = "/xyz/openbmc_project/sensors/";
Josh Lehan2a40e932020-09-02 11:48:14 -070047 objectPath += dbusPath;
48 objectPath += '/';
49 objectPath += sensorName;
50
51 sensorInterface = objectServer.add_interface(
52 objectPath, "xyz.openbmc_project.Sensor.Value");
53
Jayashree Dhanapal56678082022-01-04 17:27:20 +053054 for (const auto& threshold : thresholds)
Josh Lehan2a40e932020-09-02 11:48:14 -070055 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053056 std::string interface = thresholds::getInterface(threshold.level);
57 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
58 objectServer.add_interface(objectPath, interface);
Josh Lehan2a40e932020-09-02 11:48:14 -070059 }
60
61 association =
62 objectServer.add_interface(objectPath, association::interface);
Andrei Kartashev39287412022-02-04 16:04:47 +030063 setInitialProperties(sensorUnits);
Josh Lehan72432172021-03-17 13:35:43 -070064
Josh Lehan72432172021-03-17 13:35:43 -070065 if constexpr (debug)
66 {
67 std::cerr << "ExternalSensor " << name << " constructed: path "
68 << configurationPath << ", type " << objectType << ", min "
69 << minReading << ", max " << maxReading << ", timeout "
70 << std::chrono::duration_cast<std::chrono::microseconds>(
71 writeTimeout)
72 .count()
73 << " us\n";
74 }
Josh Lehan2a40e932020-09-02 11:48:14 -070075}
76
Josh Lehan03627382021-03-17 13:35:43 -070077// Separate function from constructor, because of a gotcha: can't use the
78// enable_shared_from_this() API until after the constructor has completed.
79void ExternalSensor::initWriteHook(
80 std::function<void(std::chrono::steady_clock::time_point now)>&&
81 writeHookIn)
82{
83 // Connect ExternalSensorMain with ExternalSensor
84 writeHook = std::move(writeHookIn);
85
86 // Connect ExternalSensor with Sensor
87 auto weakThis = weak_from_this();
Ed Tanous8a17c302021-09-02 15:07:11 -070088 externalSetHook = [weakThis]() {
Josh Lehan03627382021-03-17 13:35:43 -070089 auto lockThis = weakThis.lock();
90 if (lockThis)
91 {
92 lockThis->externalSetTrigger();
93 return;
94 }
95 if constexpr (debug)
96 {
97 std::cerr << "ExternalSensor receive ignored, sensor gone\n";
98 }
Ed Tanous8a17c302021-09-02 15:07:11 -070099 };
Josh Lehan03627382021-03-17 13:35:43 -0700100}
101
Josh Lehan2a40e932020-09-02 11:48:14 -0700102ExternalSensor::~ExternalSensor()
103{
Josh Lehan72432172021-03-17 13:35:43 -0700104 // Make sure the write hook does not reference this object anymore
105 externalSetHook = nullptr;
106
Josh Lehan2a40e932020-09-02 11:48:14 -0700107 objServer.remove_interface(association);
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530108 for (const auto& iface : thresholdInterfaces)
109 {
110 objServer.remove_interface(iface);
111 }
Josh Lehan2a40e932020-09-02 11:48:14 -0700112 objServer.remove_interface(sensorInterface);
Josh Lehan72432172021-03-17 13:35:43 -0700113
114 if constexpr (debug)
115 {
116 std::cerr << "ExternalSensor " << name << " destructed\n";
117 }
Josh Lehan2a40e932020-09-02 11:48:14 -0700118}
119
120void ExternalSensor::checkThresholds(void)
121{
122 thresholds::checkThresholds(this);
123}
Josh Lehan72432172021-03-17 13:35:43 -0700124
125bool ExternalSensor::isAliveAndPerishable(void) const
126{
127 return (writeAlive && writePerishable);
128}
129
130bool ExternalSensor::isAliveAndFresh(
131 const std::chrono::steady_clock::time_point& now) const
132{
133 // Must be alive and perishable, to have possibility of being fresh
134 if (!isAliveAndPerishable())
135 {
136 return false;
137 }
138
139 // If age, as of now, is less than timeout, it is deemed fresh
Andrew Jeffery92b96292021-05-27 16:41:31 +0930140 // NOLINTNEXTLINE
Josh Lehan72432172021-03-17 13:35:43 -0700141 return (ageElapsed(now) < writeTimeout);
142}
143
144void ExternalSensor::writeBegin(
145 const std::chrono::steady_clock::time_point& now)
146{
147 if (!writeAlive)
148 {
149 std::cerr << "ExternalSensor " << name
150 << " online, receiving first value " << value << "\n";
151 }
152
153 writeLast = now;
154 writeAlive = true;
155}
156
157void ExternalSensor::writeInvalidate(void)
158{
159 writeAlive = false;
160
161 std::cerr << "ExternalSensor " << name << " offline, timed out\n";
162
163 // Take back control of this sensor from the external override,
164 // as the external source has timed out.
165 // This allows sensor::updateValue() to work normally,
166 // as it would do for internal sensors with values from hardware.
167 overriddenState = false;
168
169 // Invalidate the existing Value, similar to what internal sensors do,
170 // when they encounter errors trying to read from hardware.
171 updateValue(std::numeric_limits<double>::quiet_NaN());
172}
173
174std::chrono::steady_clock::duration ExternalSensor::ageElapsed(
175 const std::chrono::steady_clock::time_point& now) const
176{
177 // Comparing 2 time_point will return duration
178 return (now - writeLast);
179}
180
181std::chrono::steady_clock::duration ExternalSensor::ageRemaining(
182 const std::chrono::steady_clock::time_point& now) const
183{
184 // Comparing duration will return another duration
185 return (writeTimeout - ageElapsed(now));
186}
187
188void ExternalSensor::externalSetTrigger(void)
189{
190 if constexpr (debug)
191 {
192 std::cerr << "ExternalSensor " << name << " received " << value << "\n";
193 }
194
195 auto now = std::chrono::steady_clock::now();
196
197 writeBegin(now);
198
199 // Tell the owner to recalculate the expiration timer
200 writeHook(now);
201}