Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2017 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "dbushelper.hpp" |
| 18 | |
| 19 | #include "dbushelper_interface.hpp" |
| 20 | #include "dbusutil.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 21 | |
Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 22 | #include <phosphor-logging/log.hpp> |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 23 | #include <sdbusplus/bus.hpp> |
Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 24 | |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 25 | #include <map> |
| 26 | #include <string> |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 27 | #include <variant> |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 28 | #include <vector> |
| 29 | |
| 30 | namespace pid_control |
| 31 | { |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 32 | |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 33 | using Property = std::string; |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 34 | using Value = std::variant<int64_t, double, std::string, bool>; |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 35 | using PropertyMap = std::map<Property, Value>; |
| 36 | |
Patrick Venture | 34ddc90 | 2018-10-30 11:05:17 -0700 | [diff] [blame] | 37 | using namespace phosphor::logging; |
| 38 | |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 39 | /* TODO(venture): Basically all phosphor apps need this, maybe it should be a |
| 40 | * part of sdbusplus. There is an old version in libmapper. |
| 41 | */ |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 42 | std::string DbusHelper::getService(const std::string& intf, |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame] | 43 | const std::string& path) |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 44 | { |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 45 | auto mapper = |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 46 | _bus.new_method_call("xyz.openbmc_project.ObjectMapper", |
| 47 | "/xyz/openbmc_project/object_mapper", |
| 48 | "xyz.openbmc_project.ObjectMapper", "GetObject"); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 49 | |
| 50 | mapper.append(path); |
| 51 | mapper.append(std::vector<std::string>({intf})); |
| 52 | |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 53 | std::map<std::string, std::vector<std::string>> response; |
Patrick Venture | 34ddc90 | 2018-10-30 11:05:17 -0700 | [diff] [blame] | 54 | |
| 55 | try |
| 56 | { |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 57 | auto responseMsg = _bus.call(mapper); |
Patrick Venture | 34ddc90 | 2018-10-30 11:05:17 -0700 | [diff] [blame] | 58 | |
| 59 | responseMsg.read(response); |
| 60 | } |
| 61 | catch (const sdbusplus::exception::SdBusError& ex) |
| 62 | { |
| 63 | log<level::ERR>("ObjectMapper call failure", |
| 64 | entry("WHAT=%s", ex.what())); |
| 65 | throw; |
| 66 | } |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 67 | |
| 68 | if (response.begin() == response.end()) |
| 69 | { |
| 70 | throw std::runtime_error("Unable to find Object: " + path); |
| 71 | } |
| 72 | |
| 73 | return response.begin()->first; |
| 74 | } |
| 75 | |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 76 | void DbusHelper::getProperties(const std::string& service, |
Patrick Venture | 1df9e87 | 2020-10-08 15:35:01 -0700 | [diff] [blame] | 77 | const std::string& path, SensorProperties* prop) |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 78 | { |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 79 | auto pimMsg = _bus.new_method_call(service.c_str(), path.c_str(), |
| 80 | propertiesintf, "GetAll"); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 81 | |
| 82 | pimMsg.append(sensorintf); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 83 | |
Patrick Venture | 7dbc517 | 2018-10-30 12:18:45 -0700 | [diff] [blame] | 84 | PropertyMap propMap; |
| 85 | |
| 86 | try |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 87 | { |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 88 | auto valueResponseMsg = _bus.call(pimMsg); |
Patrick Venture | 7dbc517 | 2018-10-30 12:18:45 -0700 | [diff] [blame] | 89 | valueResponseMsg.read(propMap); |
| 90 | } |
| 91 | catch (const sdbusplus::exception::SdBusError& ex) |
| 92 | { |
| 93 | log<level::ERR>("GetAll Properties Failed", |
| 94 | entry("WHAT=%s", ex.what())); |
| 95 | throw; |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // The PropertyMap returned will look like this because it's always |
| 99 | // reading a Sensor.Value interface. |
| 100 | // a{sv} 3: |
| 101 | // "Value" x 24875 |
| 102 | // "Unit" s "xyz.openbmc_project.Sensor.Value.Unit.DegreesC" |
| 103 | // "Scale" x -3 |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 104 | |
Patrick Venture | 0d73b10 | 2018-05-09 10:29:42 -0700 | [diff] [blame] | 105 | // If no error was set, the values should all be there. |
James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 106 | auto findUnit = propMap.find("Unit"); |
| 107 | if (findUnit != propMap.end()) |
| 108 | { |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 109 | prop->unit = std::get<std::string>(findUnit->second); |
James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 110 | } |
| 111 | auto findScale = propMap.find("Scale"); |
James Feist | 75eb769 | 2019-02-25 12:50:02 -0800 | [diff] [blame] | 112 | auto findMax = propMap.find("MaxValue"); |
| 113 | auto findMin = propMap.find("MinValue"); |
| 114 | |
| 115 | prop->min = 0; |
| 116 | prop->max = 0; |
| 117 | prop->scale = 0; |
James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 118 | if (findScale != propMap.end()) |
| 119 | { |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 120 | prop->scale = std::get<int64_t>(findScale->second); |
James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 121 | } |
James Feist | 75eb769 | 2019-02-25 12:50:02 -0800 | [diff] [blame] | 122 | if (findMax != propMap.end()) |
James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 123 | { |
James Feist | 75eb769 | 2019-02-25 12:50:02 -0800 | [diff] [blame] | 124 | prop->max = std::visit(VariantToDoubleVisitor(), findMax->second); |
| 125 | } |
| 126 | if (findMin != propMap.end()) |
| 127 | { |
| 128 | prop->min = std::visit(VariantToDoubleVisitor(), findMin->second); |
James Feist | c065cf1 | 2018-07-05 10:23:11 -0700 | [diff] [blame] | 129 | } |
| 130 | |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 131 | prop->value = std::visit(VariantToDoubleVisitor(), propMap["Value"]); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 132 | |
| 133 | return; |
| 134 | } |
| 135 | |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 136 | bool DbusHelper::thresholdsAsserted(const std::string& service, |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 137 | const std::string& path) |
| 138 | { |
| 139 | |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 140 | auto critical = _bus.new_method_call(service.c_str(), path.c_str(), |
| 141 | propertiesintf, "GetAll"); |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 142 | critical.append(criticalThreshInf); |
| 143 | PropertyMap criticalMap; |
| 144 | |
| 145 | try |
| 146 | { |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 147 | auto msg = _bus.call(critical); |
Patrick Venture | 4fd8cff | 2018-10-31 14:24:12 -0700 | [diff] [blame] | 148 | msg.read(criticalMap); |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 149 | } |
| 150 | catch (sdbusplus::exception_t&) |
| 151 | { |
| 152 | // do nothing, sensors don't have to expose critical thresholds |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | auto findCriticalLow = criticalMap.find("CriticalAlarmLow"); |
| 157 | auto findCriticalHigh = criticalMap.find("CriticalAlarmHigh"); |
| 158 | |
| 159 | bool asserted = false; |
| 160 | if (findCriticalLow != criticalMap.end()) |
| 161 | { |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 162 | asserted = std::get<bool>(findCriticalLow->second); |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | // as we are catching properties changed, a sensor could theoretically jump |
| 166 | // from one threshold to the other in one event, so check both thresholds |
| 167 | if (!asserted && findCriticalHigh != criticalMap.end()) |
| 168 | { |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 169 | asserted = std::get<bool>(findCriticalHigh->second); |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 170 | } |
| 171 | return asserted; |
| 172 | } |
| 173 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 174 | } // namespace pid_control |