Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 1 | #include "dbus/util.hpp" |
| 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"); |
| 91 | if (findScale != propMap.end()) |
| 92 | { |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 93 | prop->scale = std::get<int64_t>(findScale->second); |
James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 94 | } |
| 95 | else |
| 96 | { |
| 97 | prop->scale = 0; |
| 98 | } |
| 99 | |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 100 | prop->value = std::visit(VariantToDoubleVisitor(), propMap["Value"]); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 101 | |
| 102 | return; |
| 103 | } |
| 104 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 105 | bool DbusHelper::thresholdsAsserted(sdbusplus::bus::bus& bus, |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 106 | const std::string& service, |
| 107 | const std::string& path) |
| 108 | { |
| 109 | |
| 110 | auto critical = bus.new_method_call(service.c_str(), path.c_str(), |
| 111 | propertiesintf.c_str(), "GetAll"); |
| 112 | critical.append(criticalThreshInf); |
| 113 | PropertyMap criticalMap; |
| 114 | |
| 115 | try |
| 116 | { |
| 117 | auto msg = bus.call(critical); |
Patrick Venture | 4fd8cff | 2018-10-31 14:24:12 -0700 | [diff] [blame] | 118 | msg.read(criticalMap); |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 119 | } |
| 120 | catch (sdbusplus::exception_t&) |
| 121 | { |
| 122 | // do nothing, sensors don't have to expose critical thresholds |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | auto findCriticalLow = criticalMap.find("CriticalAlarmLow"); |
| 127 | auto findCriticalHigh = criticalMap.find("CriticalAlarmHigh"); |
| 128 | |
| 129 | bool asserted = false; |
| 130 | if (findCriticalLow != criticalMap.end()) |
| 131 | { |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 132 | asserted = std::get<bool>(findCriticalLow->second); |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | // as we are catching properties changed, a sensor could theoretically jump |
| 136 | // from one threshold to the other in one event, so check both thresholds |
| 137 | if (!asserted && findCriticalHigh != criticalMap.end()) |
| 138 | { |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 139 | asserted = std::get<bool>(findCriticalHigh->second); |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 140 | } |
| 141 | return asserted; |
| 142 | } |
| 143 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 144 | std::string getSensorPath(const std::string& type, const std::string& id) |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 145 | { |
| 146 | std::string layer = type; |
| 147 | if (type == "fan") |
| 148 | { |
| 149 | layer = "fan_tach"; |
| 150 | } |
| 151 | else if (type == "temp") |
| 152 | { |
| 153 | layer = "temperature"; |
| 154 | } |
| 155 | else |
| 156 | { |
| 157 | layer = "unknown"; // TODO(venture): Need to handle. |
| 158 | } |
| 159 | |
| 160 | return std::string("/xyz/openbmc_project/sensors/" + layer + "/" + id); |
| 161 | } |
| 162 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 163 | std::string getMatch(const std::string& type, const std::string& id) |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 164 | { |
| 165 | return std::string("type='signal'," |
| 166 | "interface='org.freedesktop.DBus.Properties'," |
| 167 | "member='PropertiesChanged'," |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 168 | "path='" + |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 169 | getSensorPath(type, id) + "'"); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 172 | bool validType(const std::string& type) |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 173 | { |
| 174 | static std::set<std::string> valid = {"fan", "temp"}; |
| 175 | return (valid.find(type) != valid.end()); |
| 176 | } |