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