Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 1 | #include "virtualSensor.hpp" |
| 2 | |
| 3 | #include "config.hpp" |
| 4 | |
Matt Spinler | ddc6dcd | 2020-11-09 11:16:31 -0600 | [diff] [blame] | 5 | #include <fmt/format.h> |
| 6 | |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 7 | #include <phosphor-logging/log.hpp> |
| 8 | #include <sdeventplus/event.hpp> |
| 9 | |
| 10 | #include <fstream> |
| 11 | #include <iostream> |
| 12 | |
| 13 | static constexpr bool DEBUG = false; |
| 14 | static constexpr auto busName = "xyz.openbmc_project.VirtualSensor"; |
| 15 | static constexpr auto sensorDbusPath = "/xyz/openbmc_project/sensors/"; |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 16 | |
| 17 | using namespace phosphor::logging; |
| 18 | |
Vijay Khemka | 51f898e | 2020-09-09 22:24:18 -0700 | [diff] [blame] | 19 | int handleDbusSignal(sd_bus_message* msg, void* usrData, sd_bus_error*) |
| 20 | { |
| 21 | if (usrData == nullptr) |
| 22 | { |
| 23 | throw std::runtime_error("Invalid match"); |
| 24 | } |
| 25 | |
| 26 | auto sdbpMsg = sdbusplus::message::message(msg); |
| 27 | std::string msgIfce; |
| 28 | std::map<std::string, std::variant<int64_t, double, bool>> msgData; |
| 29 | |
| 30 | sdbpMsg.read(msgIfce, msgData); |
| 31 | |
| 32 | if (msgData.find("Value") != msgData.end()) |
| 33 | { |
| 34 | using namespace phosphor::virtualSensor; |
| 35 | VirtualSensor* obj = static_cast<VirtualSensor*>(usrData); |
| 36 | // TODO(openbmc/phosphor-virtual-sensor#1): updateVirtualSensor should |
| 37 | // be changed to take the information we got from the signal, to avoid |
| 38 | // having to do numerous dbus queries. |
| 39 | obj->updateVirtualSensor(); |
| 40 | } |
| 41 | return 0; |
| 42 | } |
| 43 | |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 44 | namespace phosphor |
| 45 | { |
| 46 | namespace virtualSensor |
| 47 | { |
| 48 | |
| 49 | void printParams(const VirtualSensor::ParamMap& paramMap) |
| 50 | { |
| 51 | for (const auto& p : paramMap) |
| 52 | { |
| 53 | const auto& p1 = p.first; |
| 54 | const auto& p2 = p.second; |
| 55 | auto val = p2->getParamValue(); |
| 56 | std::cout << p1 << " = " << val << "\n"; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | double SensorParam::getParamValue() |
| 61 | { |
| 62 | switch (paramType) |
| 63 | { |
| 64 | case constParam: |
| 65 | return value; |
| 66 | break; |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 67 | case dbusParam: |
| 68 | return dbusSensor->getSensorValue(); |
| 69 | break; |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 70 | default: |
| 71 | throw std::invalid_argument("param type not supported"); |
| 72 | } |
| 73 | } |
| 74 | |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 75 | void VirtualSensor::initVirtualSensor(const Json& sensorConfig, |
| 76 | const std::string& objPath) |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 77 | { |
| 78 | |
| 79 | static const Json empty{}; |
| 80 | |
| 81 | /* Get threshold values if defined in config */ |
| 82 | auto threshold = sensorConfig.value("Threshold", empty); |
| 83 | if (!threshold.empty()) |
| 84 | { |
Matt Spinler | f15189e | 2021-01-15 10:13:28 -0600 | [diff] [blame] | 85 | // Only create the threshold interfaces if |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 86 | // at least one of their values is present. |
Matt Spinler | f15189e | 2021-01-15 10:13:28 -0600 | [diff] [blame] | 87 | |
| 88 | if (threshold.contains("CriticalHigh") || |
| 89 | threshold.contains("CriticalLow")) |
| 90 | { |
| 91 | criticalIface = |
| 92 | std::make_unique<CriticalObject>(bus, objPath.c_str()); |
| 93 | |
| 94 | criticalIface->criticalHigh(threshold.value( |
| 95 | "CriticalHigh", std::numeric_limits<double>::quiet_NaN())); |
| 96 | criticalIface->criticalLow(threshold.value( |
| 97 | "CriticalLow", std::numeric_limits<double>::quiet_NaN())); |
| 98 | } |
| 99 | |
| 100 | if (threshold.contains("WarningHigh") || |
| 101 | threshold.contains("WarningLow")) |
| 102 | { |
| 103 | warningIface = |
| 104 | std::make_unique<WarningObject>(bus, objPath.c_str()); |
| 105 | |
| 106 | warningIface->warningHigh(threshold.value( |
| 107 | "WarningHigh", std::numeric_limits<double>::quiet_NaN())); |
| 108 | warningIface->warningLow(threshold.value( |
| 109 | "WarningLow", std::numeric_limits<double>::quiet_NaN())); |
| 110 | } |
| 111 | |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 112 | if (threshold.contains("HardShutdownHigh") || |
| 113 | threshold.contains("HardShutdownLow")) |
| 114 | { |
| 115 | hardShutdownIface = |
| 116 | std::make_unique<HardShutdownObject>(bus, objPath.c_str()); |
| 117 | |
| 118 | hardShutdownIface->hardShutdownHigh(threshold.value( |
| 119 | "HardShutdownHigh", std::numeric_limits<double>::quiet_NaN())); |
| 120 | hardShutdownIface->hardShutdownLow(threshold.value( |
| 121 | "HardShutdownLow", std::numeric_limits<double>::quiet_NaN())); |
| 122 | } |
| 123 | |
| 124 | if (threshold.contains("SoftShutdownHigh") || |
| 125 | threshold.contains("SoftShutdownLow")) |
| 126 | { |
| 127 | softShutdownIface = |
| 128 | std::make_unique<SoftShutdownObject>(bus, objPath.c_str()); |
| 129 | |
| 130 | softShutdownIface->softShutdownHigh(threshold.value( |
| 131 | "SoftShutdownHigh", std::numeric_limits<double>::quiet_NaN())); |
| 132 | softShutdownIface->softShutdownLow(threshold.value( |
| 133 | "SoftShutdownLow", std::numeric_limits<double>::quiet_NaN())); |
| 134 | } |
Vijay Khemka | c62a554 | 2020-09-10 14:45:49 -0700 | [diff] [blame] | 135 | } |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 136 | |
| 137 | /* Get expression string */ |
| 138 | exprStr = sensorConfig.value("Expression", ""); |
| 139 | |
| 140 | /* Get all the parameter listed in configuration */ |
| 141 | auto params = sensorConfig.value("Params", empty); |
| 142 | |
| 143 | /* Check for constant parameter */ |
| 144 | const auto& consParams = params.value("ConstParam", empty); |
| 145 | if (!consParams.empty()) |
| 146 | { |
| 147 | for (auto& j : consParams) |
| 148 | { |
| 149 | if (j.find("ParamName") != j.end()) |
| 150 | { |
| 151 | auto paramPtr = std::make_unique<SensorParam>(j["Value"]); |
Vijay Khemka | 3ed9a51 | 2020-08-21 16:13:05 -0700 | [diff] [blame] | 152 | std::string name = j["ParamName"]; |
| 153 | symbols.create_variable(name); |
| 154 | paramMap.emplace(std::move(name), std::move(paramPtr)); |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 155 | } |
| 156 | else |
| 157 | { |
| 158 | /* Invalid configuration */ |
| 159 | throw std::invalid_argument( |
| 160 | "ParamName not found in configuration"); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 165 | /* Check for dbus parameter */ |
| 166 | auto dbusParams = params.value("DbusParam", empty); |
| 167 | if (!dbusParams.empty()) |
| 168 | { |
| 169 | for (auto& j : dbusParams) |
| 170 | { |
| 171 | /* Get parameter dbus sensor descriptor */ |
| 172 | auto desc = j.value("Desc", empty); |
| 173 | if ((!desc.empty()) && (j.find("ParamName") != j.end())) |
| 174 | { |
| 175 | std::string sensorType = desc.value("SensorType", ""); |
| 176 | std::string name = desc.value("Name", ""); |
| 177 | |
| 178 | if (!sensorType.empty() && !name.empty()) |
| 179 | { |
| 180 | std::string objPath(sensorDbusPath); |
| 181 | objPath += sensorType + "/" + name; |
| 182 | |
Vijay Khemka | 51f898e | 2020-09-09 22:24:18 -0700 | [diff] [blame] | 183 | auto paramPtr = |
| 184 | std::make_unique<SensorParam>(bus, objPath, this); |
Vijay Khemka | 3ed9a51 | 2020-08-21 16:13:05 -0700 | [diff] [blame] | 185 | std::string name = j["ParamName"]; |
| 186 | symbols.create_variable(name); |
| 187 | paramMap.emplace(std::move(name), std::move(paramPtr)); |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 192 | |
Vijay Khemka | 3ed9a51 | 2020-08-21 16:13:05 -0700 | [diff] [blame] | 193 | symbols.add_constants(); |
Matt Spinler | 9f1ef4f | 2020-11-09 15:59:11 -0600 | [diff] [blame] | 194 | symbols.add_package(vecopsPackage); |
Vijay Khemka | 3ed9a51 | 2020-08-21 16:13:05 -0700 | [diff] [blame] | 195 | expression.register_symbol_table(symbols); |
| 196 | |
| 197 | /* parser from exprtk */ |
| 198 | exprtk::parser<double> parser{}; |
Matt Spinler | ddc6dcd | 2020-11-09 11:16:31 -0600 | [diff] [blame] | 199 | if (!parser.compile(exprStr, expression)) |
| 200 | { |
| 201 | log<level::ERR>("Expression compilation failed"); |
| 202 | |
| 203 | for (std::size_t i = 0; i < parser.error_count(); ++i) |
| 204 | { |
| 205 | auto error = parser.get_error(i); |
| 206 | |
| 207 | log<level::ERR>( |
| 208 | fmt::format( |
| 209 | "Position: {} Type: {} Message: {}", error.token.position, |
| 210 | exprtk::parser_error::to_str(error.mode), error.diagnostic) |
| 211 | .c_str()); |
| 212 | } |
| 213 | throw std::runtime_error("Expression compilation failed"); |
| 214 | } |
Vijay Khemka | 3ed9a51 | 2020-08-21 16:13:05 -0700 | [diff] [blame] | 215 | |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 216 | /* Print all parameters for debug purpose only */ |
| 217 | if (DEBUG) |
| 218 | printParams(paramMap); |
| 219 | } |
| 220 | |
| 221 | void VirtualSensor::setSensorValue(double value) |
| 222 | { |
| 223 | ValueIface::value(value); |
| 224 | } |
| 225 | |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 226 | void VirtualSensor::updateVirtualSensor() |
Vijay Khemka | 3ed9a51 | 2020-08-21 16:13:05 -0700 | [diff] [blame] | 227 | { |
| 228 | for (auto& param : paramMap) |
| 229 | { |
| 230 | auto& name = param.first; |
| 231 | auto& data = param.second; |
| 232 | if (auto var = symbols.get_variable(name)) |
| 233 | { |
| 234 | var->ref() = data->getParamValue(); |
| 235 | } |
| 236 | else |
| 237 | { |
| 238 | /* Invalid parameter */ |
| 239 | throw std::invalid_argument("ParamName not found in symbols"); |
| 240 | } |
| 241 | } |
| 242 | double val = expression.value(); |
Vijay Khemka | 32a7156 | 2020-09-10 15:29:18 -0700 | [diff] [blame] | 243 | |
| 244 | /* Set sensor value to dbus interface */ |
Vijay Khemka | 3ed9a51 | 2020-08-21 16:13:05 -0700 | [diff] [blame] | 245 | setSensorValue(val); |
Vijay Khemka | 32a7156 | 2020-09-10 15:29:18 -0700 | [diff] [blame] | 246 | |
Vijay Khemka | 3ed9a51 | 2020-08-21 16:13:05 -0700 | [diff] [blame] | 247 | if (DEBUG) |
| 248 | std::cout << "Sensor value is " << val << "\n"; |
Vijay Khemka | 32a7156 | 2020-09-10 15:29:18 -0700 | [diff] [blame] | 249 | |
Matt Spinler | 8f5e611 | 2021-01-15 10:44:32 -0600 | [diff] [blame] | 250 | /* Check sensor thresholds and log required message */ |
| 251 | checkThresholds(val, warningIface.get()); |
| 252 | checkThresholds(val, criticalIface.get()); |
| 253 | checkThresholds(val, softShutdownIface.get()); |
| 254 | checkThresholds(val, hardShutdownIface.get()); |
Vijay Khemka | 3ed9a51 | 2020-08-21 16:13:05 -0700 | [diff] [blame] | 255 | } |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 256 | |
| 257 | /** @brief Parsing Virtual Sensor config JSON file */ |
| 258 | Json VirtualSensors::parseConfigFile(const std::string configFile) |
| 259 | { |
| 260 | std::ifstream jsonFile(configFile); |
| 261 | if (!jsonFile.is_open()) |
| 262 | { |
| 263 | log<level::ERR>("config JSON file not found", |
| 264 | entry("FILENAME = %s", configFile.c_str())); |
| 265 | throw std::exception{}; |
| 266 | } |
| 267 | |
| 268 | auto data = Json::parse(jsonFile, nullptr, false); |
| 269 | if (data.is_discarded()) |
| 270 | { |
| 271 | log<level::ERR>("config readings JSON parser failure", |
| 272 | entry("FILENAME = %s", configFile.c_str())); |
| 273 | throw std::exception{}; |
| 274 | } |
| 275 | |
| 276 | return data; |
| 277 | } |
| 278 | |
Vijay Khemka | e0d371e | 2020-09-21 18:35:52 -0700 | [diff] [blame] | 279 | std::map<std::string, ValueIface::Unit> unitMap = { |
| 280 | {"temperature", ValueIface::Unit::DegreesC}, |
| 281 | {"fan_tach", ValueIface::Unit::RPMS}, |
| 282 | {"voltage", ValueIface::Unit::Volts}, |
| 283 | {"altitude", ValueIface::Unit::Meters}, |
| 284 | {"current", ValueIface::Unit::Amperes}, |
| 285 | {"power", ValueIface::Unit::Watts}, |
| 286 | {"energy", ValueIface::Unit::Joules}, |
| 287 | {"utilization", ValueIface::Unit::Percent}}; |
| 288 | |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 289 | void VirtualSensors::createVirtualSensors() |
| 290 | { |
| 291 | static const Json empty{}; |
| 292 | |
| 293 | auto data = parseConfigFile(VIRTUAL_SENSOR_CONFIG_FILE); |
| 294 | // print values |
| 295 | if (DEBUG) |
| 296 | std::cout << "Config json data:\n" << data << "\n\n"; |
| 297 | |
| 298 | /* Get virtual sensors config data */ |
| 299 | for (const auto& j : data) |
| 300 | { |
| 301 | auto desc = j.value("Desc", empty); |
| 302 | if (!desc.empty()) |
| 303 | { |
| 304 | std::string sensorType = desc.value("SensorType", ""); |
| 305 | std::string name = desc.value("Name", ""); |
| 306 | |
| 307 | if (!name.empty() && !sensorType.empty()) |
| 308 | { |
Vijay Khemka | e0d371e | 2020-09-21 18:35:52 -0700 | [diff] [blame] | 309 | if (unitMap.find(sensorType) == unitMap.end()) |
| 310 | { |
| 311 | log<level::ERR>("Sensor type is not supported", |
| 312 | entry("TYPE = %s", sensorType.c_str())); |
| 313 | } |
| 314 | else |
| 315 | { |
| 316 | std::string objPath(sensorDbusPath); |
| 317 | objPath += sensorType + "/" + name; |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 318 | |
Vijay Khemka | e0d371e | 2020-09-21 18:35:52 -0700 | [diff] [blame] | 319 | auto virtualSensorPtr = std::make_unique<VirtualSensor>( |
| 320 | bus, objPath.c_str(), j, name); |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 321 | |
Vijay Khemka | e0d371e | 2020-09-21 18:35:52 -0700 | [diff] [blame] | 322 | log<level::INFO>("Added a new virtual sensor", |
| 323 | entry("NAME = %s", name.c_str())); |
| 324 | virtualSensorPtr->updateVirtualSensor(); |
| 325 | |
| 326 | /* Initialize unit value for virtual sensor */ |
| 327 | virtualSensorPtr->ValueIface::unit(unitMap[sensorType]); |
| 328 | |
| 329 | virtualSensorsMap.emplace(std::move(name), |
| 330 | std::move(virtualSensorPtr)); |
| 331 | } |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 332 | } |
| 333 | else |
| 334 | { |
| 335 | log<level::ERR>("Sensor type or name not found in config file"); |
| 336 | } |
| 337 | } |
| 338 | else |
| 339 | { |
| 340 | log<level::ERR>( |
| 341 | "Descriptor for new virtual sensor not found in config file"); |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | } // namespace virtualSensor |
| 347 | } // namespace phosphor |
| 348 | |
| 349 | /** |
| 350 | * @brief Main |
| 351 | */ |
| 352 | int main() |
| 353 | { |
| 354 | |
| 355 | // Get a default event loop |
| 356 | auto event = sdeventplus::Event::get_default(); |
| 357 | |
| 358 | // Get a handle to system dbus |
| 359 | auto bus = sdbusplus::bus::new_default(); |
| 360 | |
Matt Spinler | 6c19e7d | 2021-01-12 16:26:45 -0600 | [diff] [blame] | 361 | // Add the ObjectManager interface |
| 362 | sdbusplus::server::manager::manager objManager(bus, "/"); |
| 363 | |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 364 | // Create an virtual sensors object |
| 365 | phosphor::virtualSensor::VirtualSensors virtualSensors(bus); |
| 366 | |
| 367 | // Request service bus name |
| 368 | bus.request_name(busName); |
| 369 | |
| 370 | // Attach the bus to sd_event to service user requests |
| 371 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 372 | event.loop(); |
| 373 | |
| 374 | return 0; |
| 375 | } |