James Feist | 0c8223b | 2019-05-08 15:33:33 -0700 | [diff] [blame] | 1 | #include "util.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 2 | |
Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 3 | #include <phosphor-logging/log.hpp> |
| 4 | |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 5 | #include <cmath> |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 6 | #include <iostream> |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 7 | #include <set> |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 8 | #include <variant> |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 9 | |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 10 | using Property = std::string; |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 11 | using Value = std::variant<int64_t, double, std::string, bool>; |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 12 | using PropertyMap = std::map<Property, Value>; |
| 13 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame^] | 14 | namespace pid_control |
| 15 | { |
| 16 | |
Patrick Venture | 34ddc90 | 2018-10-30 11:05:17 -0700 | [diff] [blame] | 17 | using namespace phosphor::logging; |
| 18 | |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 19 | /* TODO(venture): Basically all phosphor apps need this, maybe it should be a |
| 20 | * part of sdbusplus. There is an old version in libmapper. |
| 21 | */ |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 22 | std::string DbusHelper::getService(sdbusplus::bus::bus& bus, |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame] | 23 | const std::string& intf, |
| 24 | const std::string& path) |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 25 | { |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 26 | auto mapper = |
| 27 | bus.new_method_call("xyz.openbmc_project.ObjectMapper", |
| 28 | "/xyz/openbmc_project/object_mapper", |
| 29 | "xyz.openbmc_project.ObjectMapper", "GetObject"); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 30 | |
| 31 | mapper.append(path); |
| 32 | mapper.append(std::vector<std::string>({intf})); |
| 33 | |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 34 | std::map<std::string, std::vector<std::string>> response; |
Patrick Venture | 34ddc90 | 2018-10-30 11:05:17 -0700 | [diff] [blame] | 35 | |
| 36 | try |
| 37 | { |
| 38 | auto responseMsg = bus.call(mapper); |
| 39 | |
| 40 | responseMsg.read(response); |
| 41 | } |
| 42 | catch (const sdbusplus::exception::SdBusError& ex) |
| 43 | { |
| 44 | log<level::ERR>("ObjectMapper call failure", |
| 45 | entry("WHAT=%s", ex.what())); |
| 46 | throw; |
| 47 | } |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 48 | |
| 49 | if (response.begin() == response.end()) |
| 50 | { |
| 51 | throw std::runtime_error("Unable to find Object: " + path); |
| 52 | } |
| 53 | |
| 54 | return response.begin()->first; |
| 55 | } |
| 56 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 57 | void DbusHelper::getProperties(sdbusplus::bus::bus& bus, |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame] | 58 | const std::string& service, |
| 59 | const std::string& path, |
| 60 | struct SensorProperties* prop) |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 61 | { |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 62 | auto pimMsg = bus.new_method_call(service.c_str(), path.c_str(), |
| 63 | propertiesintf.c_str(), "GetAll"); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 64 | |
| 65 | pimMsg.append(sensorintf); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 66 | |
Patrick Venture | 7dbc517 | 2018-10-30 12:18:45 -0700 | [diff] [blame] | 67 | PropertyMap propMap; |
| 68 | |
| 69 | try |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 70 | { |
Patrick Venture | 7dbc517 | 2018-10-30 12:18:45 -0700 | [diff] [blame] | 71 | auto valueResponseMsg = bus.call(pimMsg); |
| 72 | valueResponseMsg.read(propMap); |
| 73 | } |
| 74 | catch (const sdbusplus::exception::SdBusError& ex) |
| 75 | { |
| 76 | log<level::ERR>("GetAll Properties Failed", |
| 77 | entry("WHAT=%s", ex.what())); |
| 78 | throw; |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | // The PropertyMap returned will look like this because it's always |
| 82 | // reading a Sensor.Value interface. |
| 83 | // a{sv} 3: |
| 84 | // "Value" x 24875 |
| 85 | // "Unit" s "xyz.openbmc_project.Sensor.Value.Unit.DegreesC" |
| 86 | // "Scale" x -3 |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 87 | |
Patrick Venture | 0d73b10 | 2018-05-09 10:29:42 -0700 | [diff] [blame] | 88 | // If no error was set, the values should all be there. |
James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 89 | auto findUnit = propMap.find("Unit"); |
| 90 | if (findUnit != propMap.end()) |
| 91 | { |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 92 | prop->unit = std::get<std::string>(findUnit->second); |
James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 93 | } |
| 94 | auto findScale = propMap.find("Scale"); |
James Feist | 75eb769 | 2019-02-25 12:50:02 -0800 | [diff] [blame] | 95 | auto findMax = propMap.find("MaxValue"); |
| 96 | auto findMin = propMap.find("MinValue"); |
| 97 | |
| 98 | prop->min = 0; |
| 99 | prop->max = 0; |
| 100 | prop->scale = 0; |
James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 101 | if (findScale != propMap.end()) |
| 102 | { |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 103 | prop->scale = std::get<int64_t>(findScale->second); |
James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 104 | } |
James Feist | 75eb769 | 2019-02-25 12:50:02 -0800 | [diff] [blame] | 105 | if (findMax != propMap.end()) |
James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 106 | { |
James Feist | 75eb769 | 2019-02-25 12:50:02 -0800 | [diff] [blame] | 107 | prop->max = std::visit(VariantToDoubleVisitor(), findMax->second); |
| 108 | } |
| 109 | if (findMin != propMap.end()) |
| 110 | { |
| 111 | prop->min = std::visit(VariantToDoubleVisitor(), findMin->second); |
James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 112 | } |
| 113 | |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 114 | prop->value = std::visit(VariantToDoubleVisitor(), propMap["Value"]); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 115 | |
| 116 | return; |
| 117 | } |
| 118 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 119 | bool DbusHelper::thresholdsAsserted(sdbusplus::bus::bus& bus, |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 120 | const std::string& service, |
| 121 | const std::string& path) |
| 122 | { |
| 123 | |
| 124 | auto critical = bus.new_method_call(service.c_str(), path.c_str(), |
| 125 | propertiesintf.c_str(), "GetAll"); |
| 126 | critical.append(criticalThreshInf); |
| 127 | PropertyMap criticalMap; |
| 128 | |
| 129 | try |
| 130 | { |
| 131 | auto msg = bus.call(critical); |
Patrick Venture | 4fd8cff | 2018-10-31 14:24:12 -0700 | [diff] [blame] | 132 | msg.read(criticalMap); |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 133 | } |
| 134 | catch (sdbusplus::exception_t&) |
| 135 | { |
| 136 | // do nothing, sensors don't have to expose critical thresholds |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | auto findCriticalLow = criticalMap.find("CriticalAlarmLow"); |
| 141 | auto findCriticalHigh = criticalMap.find("CriticalAlarmHigh"); |
| 142 | |
| 143 | bool asserted = false; |
| 144 | if (findCriticalLow != criticalMap.end()) |
| 145 | { |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 146 | asserted = std::get<bool>(findCriticalLow->second); |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // as we are catching properties changed, a sensor could theoretically jump |
| 150 | // from one threshold to the other in one event, so check both thresholds |
| 151 | if (!asserted && findCriticalHigh != criticalMap.end()) |
| 152 | { |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 153 | asserted = std::get<bool>(findCriticalHigh->second); |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 154 | } |
| 155 | return asserted; |
| 156 | } |
| 157 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 158 | std::string getSensorPath(const std::string& type, const std::string& id) |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 159 | { |
| 160 | std::string layer = type; |
| 161 | if (type == "fan") |
| 162 | { |
| 163 | layer = "fan_tach"; |
| 164 | } |
| 165 | else if (type == "temp") |
| 166 | { |
| 167 | layer = "temperature"; |
| 168 | } |
| 169 | else |
| 170 | { |
| 171 | layer = "unknown"; // TODO(venture): Need to handle. |
| 172 | } |
| 173 | |
| 174 | return std::string("/xyz/openbmc_project/sensors/" + layer + "/" + id); |
| 175 | } |
| 176 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 177 | std::string getMatch(const std::string& type, const std::string& id) |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 178 | { |
| 179 | return std::string("type='signal'," |
| 180 | "interface='org.freedesktop.DBus.Properties'," |
| 181 | "member='PropertiesChanged'," |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 182 | "path='" + |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 183 | getSensorPath(type, id) + "'"); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 186 | bool validType(const std::string& type) |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 187 | { |
| 188 | static std::set<std::string> valid = {"fan", "temp"}; |
| 189 | return (valid.find(type) != valid.end()); |
| 190 | } |
James Feist | 75eb769 | 2019-02-25 12:50:02 -0800 | [diff] [blame] | 191 | |
| 192 | void scaleSensorReading(const double min, const double max, double& value) |
| 193 | { |
Kun Yi | 6aae76f | 2019-07-10 21:58:32 -0700 | [diff] [blame] | 194 | if (max <= 0 || max <= min) |
James Feist | 75eb769 | 2019-02-25 12:50:02 -0800 | [diff] [blame] | 195 | { |
| 196 | return; |
| 197 | } |
| 198 | value /= (max - min); |
James Feist | 0c8223b | 2019-05-08 15:33:33 -0700 | [diff] [blame] | 199 | } |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame^] | 200 | |
| 201 | } // namespace pid_control |