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