12a40e939SJosh Lehan #include "ExternalSensor.hpp"
22a40e939SJosh Lehan 
32a40e939SJosh Lehan #include "SensorPaths.hpp"
42a40e939SJosh Lehan 
52a40e939SJosh Lehan #include <unistd.h>
62a40e939SJosh Lehan 
72a40e939SJosh Lehan #include <boost/algorithm/string/predicate.hpp>
82a40e939SJosh Lehan #include <boost/algorithm/string/replace.hpp>
92a40e939SJosh Lehan #include <boost/date_time/posix_time/posix_time.hpp>
102a40e939SJosh Lehan #include <sdbusplus/asio/connection.hpp>
112a40e939SJosh Lehan #include <sdbusplus/asio/object_server.hpp>
122a40e939SJosh Lehan 
132a40e939SJosh Lehan #include <iostream>
142a40e939SJosh Lehan #include <istream>
152a40e939SJosh Lehan #include <limits>
162a40e939SJosh Lehan #include <memory>
172a40e939SJosh Lehan #include <string>
182a40e939SJosh Lehan #include <vector>
192a40e939SJosh Lehan 
202a40e939SJosh Lehan ExternalSensor::ExternalSensor(
212a40e939SJosh Lehan     const std::string& objectType, sdbusplus::asio::object_server& objectServer,
222a40e939SJosh Lehan     std::shared_ptr<sdbusplus::asio::connection>& conn,
232a40e939SJosh Lehan     const std::string& sensorName, const std::string& sensorUnits,
248a57ec09SEd Tanous     std::vector<thresholds::Threshold>&& thresholds,
252a40e939SJosh Lehan     const std::string& sensorConfiguration, const double& maxReading,
262a40e939SJosh Lehan     const double& minReading, const PowerState& powerState) :
272a40e939SJosh Lehan     // TODO(): When the Mutable feature is integrated,
282a40e939SJosh Lehan     // make sure all ExternalSensor instances are mutable,
292a40e939SJosh Lehan     // because that is the entire point of ExternalSensor,
302a40e939SJosh Lehan     // to accept sensor values written by an external source.
318a57ec09SEd Tanous     Sensor(boost::replace_all_copy(sensorName, " ", "_"), std::move(thresholds),
328a57ec09SEd Tanous            sensorConfiguration, objectType, maxReading, minReading, conn,
338a57ec09SEd Tanous            powerState),
342a40e939SJosh Lehan     std::enable_shared_from_this<ExternalSensor>(), objServer(objectServer)
352a40e939SJosh Lehan {
362a40e939SJosh Lehan     // The caller must specify what physical characteristic
372a40e939SJosh Lehan     // an external sensor is expected to be measuring, such as temperature,
382a40e939SJosh Lehan     // as, unlike others, this is not implied by device type name.
39*6cb732a3SEd Tanous     std::string dbusPath = sensor_paths::getPathForUnits(sensorUnits);
402a40e939SJosh Lehan     if (dbusPath.empty())
412a40e939SJosh Lehan     {
422a40e939SJosh Lehan         throw std::runtime_error("Units not in allow list");
432a40e939SJosh Lehan     }
44*6cb732a3SEd Tanous     std::string objectPath = "/xyz/openbmc_project/sensors/";
452a40e939SJosh Lehan     objectPath += dbusPath;
462a40e939SJosh Lehan     objectPath += '/';
472a40e939SJosh Lehan     objectPath += sensorName;
482a40e939SJosh Lehan 
492a40e939SJosh Lehan     sensorInterface = objectServer.add_interface(
502a40e939SJosh Lehan         objectPath, "xyz.openbmc_project.Sensor.Value");
512a40e939SJosh Lehan 
522a40e939SJosh Lehan     if (thresholds::hasWarningInterface(thresholds))
532a40e939SJosh Lehan     {
542a40e939SJosh Lehan         thresholdInterfaceWarning = objectServer.add_interface(
552a40e939SJosh Lehan             objectPath, "xyz.openbmc_project.Sensor.Threshold.Warning");
562a40e939SJosh Lehan     }
572a40e939SJosh Lehan     if (thresholds::hasCriticalInterface(thresholds))
582a40e939SJosh Lehan     {
592a40e939SJosh Lehan         thresholdInterfaceCritical = objectServer.add_interface(
602a40e939SJosh Lehan             objectPath, "xyz.openbmc_project.Sensor.Threshold.Critical");
612a40e939SJosh Lehan     }
622a40e939SJosh Lehan 
632a40e939SJosh Lehan     association =
642a40e939SJosh Lehan         objectServer.add_interface(objectPath, association::interface);
652a40e939SJosh Lehan     setInitialProperties(conn);
662a40e939SJosh Lehan }
672a40e939SJosh Lehan 
682a40e939SJosh Lehan ExternalSensor::~ExternalSensor()
692a40e939SJosh Lehan {
702a40e939SJosh Lehan     objServer.remove_interface(association);
712a40e939SJosh Lehan     objServer.remove_interface(thresholdInterfaceCritical);
722a40e939SJosh Lehan     objServer.remove_interface(thresholdInterfaceWarning);
732a40e939SJosh Lehan     objServer.remove_interface(sensorInterface);
742a40e939SJosh Lehan }
752a40e939SJosh Lehan 
762a40e939SJosh Lehan void ExternalSensor::checkThresholds(void)
772a40e939SJosh Lehan {
782a40e939SJosh Lehan     thresholds::checkThresholds(this);
792a40e939SJosh Lehan }
80