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