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 | |
William A. Kennington III | 4a0c061 | 2018-11-12 17:07:28 -0800 | [diff] [blame] | 101 | prop->value = sdbusplus::message::variant_ns::visit( |
James Feist | d7a55bf | 2018-10-11 14:40:07 -0700 | [diff] [blame] | 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); |
Patrick Venture | 4fd8cff | 2018-10-31 14:24:12 -0700 | [diff] [blame] | 120 | msg.read(criticalMap); |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 121 | } |
| 122 | catch (sdbusplus::exception_t&) |
| 123 | { |
| 124 | // do nothing, sensors don't have to expose critical thresholds |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | auto findCriticalLow = criticalMap.find("CriticalAlarmLow"); |
| 129 | auto findCriticalHigh = criticalMap.find("CriticalAlarmHigh"); |
| 130 | |
| 131 | bool asserted = false; |
| 132 | if (findCriticalLow != criticalMap.end()) |
| 133 | { |
| 134 | asserted = |
| 135 | sdbusplus::message::variant_ns::get<bool>(findCriticalLow->second); |
| 136 | } |
| 137 | |
| 138 | // as we are catching properties changed, a sensor could theoretically jump |
| 139 | // from one threshold to the other in one event, so check both thresholds |
| 140 | if (!asserted && findCriticalHigh != criticalMap.end()) |
| 141 | { |
| 142 | asserted = |
| 143 | sdbusplus::message::variant_ns::get<bool>(findCriticalHigh->second); |
| 144 | } |
| 145 | return asserted; |
| 146 | } |
| 147 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 148 | std::string getSensorPath(const std::string& type, const std::string& id) |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 149 | { |
| 150 | std::string layer = type; |
| 151 | if (type == "fan") |
| 152 | { |
| 153 | layer = "fan_tach"; |
| 154 | } |
| 155 | else if (type == "temp") |
| 156 | { |
| 157 | layer = "temperature"; |
| 158 | } |
| 159 | else |
| 160 | { |
| 161 | layer = "unknown"; // TODO(venture): Need to handle. |
| 162 | } |
| 163 | |
| 164 | return std::string("/xyz/openbmc_project/sensors/" + layer + "/" + id); |
| 165 | } |
| 166 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 167 | std::string getMatch(const std::string& type, const std::string& id) |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 168 | { |
| 169 | return std::string("type='signal'," |
| 170 | "interface='org.freedesktop.DBus.Properties'," |
| 171 | "member='PropertiesChanged'," |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 172 | "path='" + |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 173 | getSensorPath(type, id) + "'"); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 176 | bool validType(const std::string& type) |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 177 | { |
| 178 | static std::set<std::string> valid = {"fan", "temp"}; |
| 179 | return (valid.find(type) != valid.end()); |
| 180 | } |