diff options
Diffstat (limited to 'sensorhandler.cpp')
-rw-r--r-- | sensorhandler.cpp | 163 |
1 files changed, 106 insertions, 57 deletions
diff --git a/sensorhandler.cpp b/sensorhandler.cpp index 4562476..b6e7890 100644 --- a/sensorhandler.cpp +++ b/sensorhandler.cpp @@ -2,6 +2,7 @@ #include <math.h> #include <stdio.h> #include <string.h> +#include <set> #include <bitset> #include <xyz/openbmc_project/Sensor/Value/server.hpp> #include <systemd/sd-bus.h> @@ -408,6 +409,16 @@ ipmi_ret_t ipmi_sen_get_sensor_type(ipmi_netfn_t netfn, ipmi_cmd_t cmd, return rc; } +const std::set<std::string> analogSensorInterfaces = +{ + "xyz.openbmc_project.Sensor.Value", +}; + +bool isAnalogSensor(const std::string& interface) +{ + return (analogSensorInterfaces.count(interface)); +} + ipmi_ret_t setSensorReading(void *request) { SetSensorReadingReq cmdData = @@ -538,6 +549,13 @@ ipmi_ret_t ipmi_sen_get_sensor_reading(ipmi_netfn_t netfn, ipmi_cmd_t cmd, } sensor = sensors.at(reqptr->sennum); + if (ipmi::sensor::Mutability::Read != + (sensor.mutability & ipmi::sensor::Mutability::Read)) + { + log<level::ERR>("Sensor was not readable.\n"); + return IPMI_CC_SENSOR_INVALID; + } + // Get value r = sd_bus_get_property_trivial(bus, @@ -710,70 +728,75 @@ ipmi_ret_t ipmi_sen_reserve_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd, return IPMI_CC_OK; } -ipmi_ret_t populate_record_from_dbus(get_sdr::SensorDataFullRecordBody *body, - const ipmi::sensor::Info *info, - ipmi_data_len_t data_len) +void setUnitFieldsForObject(sd_bus *bus, + const dbus_interface_t &iface, + const ipmi::sensor::Info *info, + get_sdr::SensorDataFullRecordBody *body) { - /* Functional sensor case */ if (info->propertyInterfaces.begin()->first == - "xyz.openbmc_project.Sensor.Value") + "xyz.openbmc_project.Sensor.Value") { - // Get bus - sd_bus *bus = ipmid_get_sd_bus_connection(); - dbus_interface_t iface; - - if (0 > find_openbmc_path(body->entity_id, &iface)) - return IPMI_CC_SENSOR_INVALID; - - body->sensor_units_1 = 0; // unsigned, no rate, no modifier, not a % - - /* Unit info */ - char *raw_cstr; + std::string result {}; + char *raw_cstr = NULL; if (0 > sd_bus_get_property_string(bus, iface.bus, iface.path, iface.interface, "Unit", NULL, - &raw_cstr)) { - fprintf(stderr, "Expected to find Unit interface in bus %s, path %s, but it was missing.\n", - iface.bus, iface.path); - return IPMI_CC_SENSOR_INVALID; + &raw_cstr)) + { + log<level::WARNING>("Unit interface missing.", + entry("bus=%s, path=%s", iface.bus, iface.path)); + } + else + { + result = raw_cstr; } + free(raw_cstr); - std::string raw_str(raw_cstr); namespace server = sdbusplus::xyz::openbmc_project::Sensor::server; - server::Value::Unit unit = - server::Value::convertUnitFromString(raw_str); - - // Unit strings defined in - // phosphor-dbus-interfaces/xyz/openbmc_project/Sensor/Value.interface.yaml - switch (unit) + try { + auto unit = server::Value::convertUnitFromString(result); + // Unit strings defined in + // phosphor-dbus-interfaces/xyz/openbmc_project/Sensor/Value.interface.yaml + switch (unit) + { + case server::Value::Unit::DegreesC: + body->sensor_units_2_base = get_sdr::SENSOR_UNIT_DEGREES_C; + break; + case server::Value::Unit::RPMS: + body->sensor_units_2_base = get_sdr::SENSOR_UNIT_REVOLUTIONS; // revolutions + get_sdr::body::set_rate_unit(0b100, body); // per minute + break; + case server::Value::Unit::Volts: + body->sensor_units_2_base = get_sdr::SENSOR_UNIT_VOLTS; + break; + case server::Value::Unit::Meters: + body->sensor_units_2_base = get_sdr::SENSOR_UNIT_METERS; + break; + case server::Value::Unit::Amperes: + body->sensor_units_2_base = get_sdr::SENSOR_UNIT_AMPERES; + break; + case server::Value::Unit::Joules: + body->sensor_units_2_base = get_sdr::SENSOR_UNIT_JOULES; + break; + default: + // Cannot be hit. + fprintf(stderr, "Unknown value unit type: = %s\n", result.c_str()); + } + } + catch (sdbusplus::exception::InvalidEnumString e) { - case server::Value::Unit::DegreesC: - body->sensor_units_2_base = get_sdr::SENSOR_UNIT_DEGREES_C; - break; - case server::Value::Unit::RPMS: - body->sensor_units_2_base = get_sdr::SENSOR_UNIT_REVOLUTIONS; // revolutions - get_sdr::body::set_rate_unit(0b100, body); // per minute - break; - case server::Value::Unit::Volts: - body->sensor_units_2_base = get_sdr::SENSOR_UNIT_VOLTS; - break; - case server::Value::Unit::Meters: - body->sensor_units_2_base = get_sdr::SENSOR_UNIT_METERS; - break; - case server::Value::Unit::Amperes: - body->sensor_units_2_base = get_sdr::SENSOR_UNIT_AMPERES; - break; - case server::Value::Unit::Joules: - body->sensor_units_2_base = get_sdr::SENSOR_UNIT_JOULES; - break; - default: - fprintf(stderr, "Unknown value unit type: = %s\n", raw_cstr); + log<level::WARNING>("Warning: no unit provided for sensor!"); } + } +} - free(raw_cstr); - - /* Modifiers to reading info */ - // Get scale - int64_t scale; +int64_t getScaleForObject(sd_bus *bus, + const dbus_interface_t& iface, + const ipmi::sensor::Info *info) +{ + int64_t result = 0; + if (info->propertyInterfaces.begin()->first == + "xyz.openbmc_project.Sensor.Value") + { if (0 > sd_bus_get_property_trivial(bus, iface.bus, iface.path, @@ -781,11 +804,37 @@ ipmi_ret_t populate_record_from_dbus(get_sdr::SensorDataFullRecordBody *body, "Scale", NULL, 'x', - &scale)) { - fprintf(stderr, "Expected to find Scale interface in bus %s, path %s, but it was missing.\n", - iface.bus, iface.path); - return IPMI_CC_SENSOR_INVALID; + &result)) { + log<level::WARNING>("Scale interface missing.", + entry("bus=%s, path=%s", iface.bus, iface.path)); } + } + + return result; +} + +ipmi_ret_t populate_record_from_dbus(get_sdr::SensorDataFullRecordBody *body, + const ipmi::sensor::Info *info, + ipmi_data_len_t data_len) +{ + /* Functional sensor case */ + if (isAnalogSensor(info->propertyInterfaces.begin()->first)) + { + // Get bus + sd_bus *bus = ipmid_get_sd_bus_connection(); + dbus_interface_t iface; + + if (0 > find_openbmc_path(body->entity_id, &iface)) + return IPMI_CC_SENSOR_INVALID; + + body->sensor_units_1 = 0; // unsigned, no rate, no modifier, not a % + + /* Unit info */ + setUnitFieldsForObject(bus, iface, info, body); + + /* Modifiers to reading info */ + // Get scale + int64_t scale = getScaleForObject(bus, iface, info); get_sdr::body::set_b(info->coefficientB, body); get_sdr::body::set_m(info->coefficientM, body); |