James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 Intel Corporation |
| 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 | */ |
Patrick Venture | 7e952d9 | 2020-10-05 15:58:52 -0700 | [diff] [blame] | 16 | #include "dbusconfiguration.hpp" |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 17 | |
Patrick Venture | 0771659 | 2018-10-14 11:46:40 -0700 | [diff] [blame] | 18 | #include "conf.hpp" |
Patrick Venture | ef1f886 | 2020-08-17 09:34:35 -0700 | [diff] [blame] | 19 | #include "dbushelper.hpp" |
| 20 | #include "dbusutil.hpp" |
James Feist | 0c8223b | 2019-05-08 15:33:33 -0700 | [diff] [blame] | 21 | #include "util.hpp" |
Patrick Venture | 0771659 | 2018-10-14 11:46:40 -0700 | [diff] [blame] | 22 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 23 | #include <boost/asio/steady_timer.hpp> |
Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 24 | #include <sdbusplus/bus.hpp> |
| 25 | #include <sdbusplus/bus/match.hpp> |
| 26 | #include <sdbusplus/exception.hpp> |
| 27 | |
| 28 | #include <algorithm> |
James Feist | 64f072a | 2018-08-10 16:39:24 -0700 | [diff] [blame] | 29 | #include <chrono> |
James Feist | 64f072a | 2018-08-10 16:39:24 -0700 | [diff] [blame] | 30 | #include <functional> |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 31 | #include <iostream> |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 32 | #include <list> |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 33 | #include <set> |
| 34 | #include <unordered_map> |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 35 | #include <variant> |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 36 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 37 | namespace pid_control |
| 38 | { |
| 39 | |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 40 | extern std::map<std::string, struct conf::SensorConfig> sensorConfig; |
| 41 | extern std::map<int64_t, conf::PIDConf> zoneConfig; |
| 42 | extern std::map<int64_t, struct conf::ZoneConfig> zoneDetailsConfig; |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 43 | |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 44 | constexpr const char* pidConfigurationInterface = |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 45 | "xyz.openbmc_project.Configuration.Pid"; |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 46 | constexpr const char* objectManagerInterface = |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 47 | "org.freedesktop.DBus.ObjectManager"; |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 48 | constexpr const char* pidZoneConfigurationInterface = |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 49 | "xyz.openbmc_project.Configuration.Pid.Zone"; |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 50 | constexpr const char* stepwiseConfigurationInterface = |
| 51 | "xyz.openbmc_project.Configuration.Stepwise"; |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 52 | constexpr const char* thermalControlIface = |
| 53 | "xyz.openbmc_project.Control.ThermalMode"; |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 54 | constexpr const char* sensorInterface = "xyz.openbmc_project.Sensor.Value"; |
Patrick Venture | 0911bfe | 2020-08-10 12:51:40 -0700 | [diff] [blame] | 55 | constexpr const char* defaultPwmInterface = |
| 56 | "xyz.openbmc_project.Control.FanPwm"; |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 57 | |
James Feist | 991ebd8 | 2020-07-21 11:14:52 -0700 | [diff] [blame] | 58 | using Association = std::tuple<std::string, std::string, std::string>; |
| 59 | using Associations = std::vector<Association>; |
| 60 | |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 61 | namespace thresholds |
| 62 | { |
| 63 | constexpr const char* warningInterface = |
| 64 | "xyz.openbmc_project.Sensor.Threshold.Warning"; |
| 65 | constexpr const char* criticalInterface = |
| 66 | "xyz.openbmc_project.Sensor.Threshold.Critical"; |
| 67 | const std::array<const char*, 4> types = {"CriticalLow", "CriticalHigh", |
| 68 | "WarningLow", "WarningHigh"}; |
| 69 | |
| 70 | } // namespace thresholds |
| 71 | |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 72 | namespace dbus_configuration |
| 73 | { |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 74 | using SensorInterfaceType = std::pair<std::string, std::string>; |
| 75 | |
| 76 | inline std::string getSensorNameFromPath(const std::string& dbusPath) |
| 77 | { |
| 78 | return dbusPath.substr(dbusPath.find_last_of("/") + 1); |
| 79 | } |
| 80 | |
| 81 | inline std::string sensorNameToDbusName(const std::string& sensorName) |
| 82 | { |
| 83 | std::string retString = sensorName; |
| 84 | std::replace(retString.begin(), retString.end(), ' ', '_'); |
| 85 | return retString; |
| 86 | } |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 87 | |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 88 | std::vector<std::string> getSelectedProfiles(sdbusplus::bus::bus& bus) |
| 89 | { |
| 90 | std::vector<std::string> ret; |
| 91 | auto mapper = |
| 92 | bus.new_method_call("xyz.openbmc_project.ObjectMapper", |
| 93 | "/xyz/openbmc_project/object_mapper", |
| 94 | "xyz.openbmc_project.ObjectMapper", "GetSubTree"); |
| 95 | mapper.append("/", 0, std::array<const char*, 1>{thermalControlIface}); |
| 96 | std::unordered_map< |
| 97 | std::string, std::unordered_map<std::string, std::vector<std::string>>> |
| 98 | respData; |
| 99 | |
| 100 | try |
| 101 | { |
| 102 | auto resp = bus.call(mapper); |
| 103 | resp.read(respData); |
| 104 | } |
| 105 | catch (sdbusplus::exception_t&) |
| 106 | { |
| 107 | // can't do anything without mapper call data |
| 108 | throw std::runtime_error("ObjectMapper Call Failure"); |
| 109 | } |
| 110 | if (respData.empty()) |
| 111 | { |
| 112 | // if the user has profiles but doesn't expose the interface to select |
| 113 | // one, just go ahead without using profiles |
| 114 | return ret; |
| 115 | } |
| 116 | |
| 117 | // assumption is that we should only have a small handful of selected |
| 118 | // profiles at a time (probably only 1), so calling each individually should |
| 119 | // not incur a large cost |
| 120 | for (const auto& objectPair : respData) |
| 121 | { |
| 122 | const std::string& path = objectPair.first; |
| 123 | for (const auto& ownerPair : objectPair.second) |
| 124 | { |
| 125 | const std::string& busName = ownerPair.first; |
| 126 | auto getProfile = |
| 127 | bus.new_method_call(busName.c_str(), path.c_str(), |
| 128 | "org.freedesktop.DBus.Properties", "Get"); |
| 129 | getProfile.append(thermalControlIface, "Current"); |
| 130 | std::variant<std::string> variantResp; |
| 131 | try |
| 132 | { |
| 133 | auto resp = bus.call(getProfile); |
| 134 | resp.read(variantResp); |
| 135 | } |
| 136 | catch (sdbusplus::exception_t&) |
| 137 | { |
| 138 | throw std::runtime_error("Failure getting profile"); |
| 139 | } |
| 140 | std::string mode = std::get<std::string>(variantResp); |
| 141 | ret.emplace_back(std::move(mode)); |
| 142 | } |
| 143 | } |
Patrick Venture | 39199b4 | 2020-10-08 14:40:29 -0700 | [diff] [blame^] | 144 | if constexpr (pid_control::conf::DEBUG) |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 145 | { |
| 146 | std::cout << "Profiles selected: "; |
| 147 | for (const auto& profile : ret) |
| 148 | { |
| 149 | std::cout << profile << " "; |
| 150 | } |
| 151 | std::cout << "\n"; |
| 152 | } |
| 153 | return ret; |
| 154 | } |
| 155 | |
James Feist | 991ebd8 | 2020-07-21 11:14:52 -0700 | [diff] [blame] | 156 | int eventHandler(sd_bus_message* m, void* context, sd_bus_error*) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 157 | { |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 158 | |
James Feist | 991ebd8 | 2020-07-21 11:14:52 -0700 | [diff] [blame] | 159 | if (context == nullptr || m == nullptr) |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 160 | { |
| 161 | throw std::runtime_error("Invalid match"); |
| 162 | } |
James Feist | 991ebd8 | 2020-07-21 11:14:52 -0700 | [diff] [blame] | 163 | |
| 164 | // we skip associations because the mapper populates these, not the sensors |
| 165 | const std::array<const char*, 1> skipList = { |
| 166 | "xyz.openbmc_project.Association"}; |
| 167 | |
| 168 | sdbusplus::message::message message(m); |
| 169 | if (std::string(message.get_member()) == "InterfacesAdded") |
| 170 | { |
| 171 | sdbusplus::message::object_path path; |
| 172 | std::unordered_map< |
| 173 | std::string, |
| 174 | std::unordered_map<std::string, std::variant<Associations, bool>>> |
| 175 | data; |
| 176 | |
| 177 | message.read(path, data); |
| 178 | |
| 179 | for (const char* skip : skipList) |
| 180 | { |
| 181 | auto find = data.find(skip); |
| 182 | if (find != data.end()) |
| 183 | { |
| 184 | data.erase(find); |
| 185 | if (data.empty()) |
| 186 | { |
| 187 | return 1; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 193 | boost::asio::steady_timer* timer = |
| 194 | static_cast<boost::asio::steady_timer*>(context); |
| 195 | |
| 196 | // do a brief sleep as we tend to get a bunch of these events at |
| 197 | // once |
| 198 | timer->expires_after(std::chrono::seconds(2)); |
| 199 | timer->async_wait([](const boost::system::error_code ec) { |
| 200 | if (ec == boost::asio::error::operation_aborted) |
| 201 | { |
| 202 | /* another timer started*/ |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | std::cout << "New configuration detected, reloading\n."; |
Yong Li | 298a95c | 2020-04-07 15:11:02 +0800 | [diff] [blame] | 207 | tryRestartControlLoops(); |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 208 | }); |
| 209 | |
| 210 | return 1; |
| 211 | } |
| 212 | |
| 213 | void createMatches(sdbusplus::bus::bus& bus, boost::asio::steady_timer& timer) |
| 214 | { |
| 215 | // this is a list because the matches can't be moved |
| 216 | static std::list<sdbusplus::bus::match::match> matches; |
| 217 | |
James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 218 | const std::array<std::string, 4> interfaces = { |
| 219 | thermalControlIface, pidConfigurationInterface, |
| 220 | pidZoneConfigurationInterface, stepwiseConfigurationInterface}; |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 221 | |
| 222 | // this list only needs to be created once |
| 223 | if (!matches.empty()) |
| 224 | { |
| 225 | return; |
| 226 | } |
| 227 | |
| 228 | // we restart when the configuration changes or there are new sensors |
| 229 | for (const auto& interface : interfaces) |
| 230 | { |
| 231 | matches.emplace_back( |
| 232 | bus, |
| 233 | "type='signal',member='PropertiesChanged',arg0namespace='" + |
| 234 | interface + "'", |
| 235 | eventHandler, &timer); |
| 236 | } |
| 237 | matches.emplace_back( |
| 238 | bus, |
| 239 | "type='signal',member='InterfacesAdded',arg0path='/xyz/openbmc_project/" |
| 240 | "sensors/'", |
| 241 | eventHandler, &timer); |
| 242 | } |
| 243 | |
Jason Ling | 6fc301f | 2020-07-23 12:39:57 -0700 | [diff] [blame] | 244 | /** |
| 245 | * retrieve an attribute from the pid configuration map |
| 246 | * @param[in] base - the PID configuration map, keys are the attributes and |
| 247 | * value is the variant associated with that attribute. |
| 248 | * @param attributeName - the name of the attribute |
| 249 | * @return a variant holding the value associated with a key |
| 250 | * @throw runtime_error : attributeName is not in base |
| 251 | */ |
| 252 | inline DbusVariantType getPIDAttribute( |
| 253 | const std::unordered_map<std::string, DbusVariantType>& base, |
| 254 | const std::string& attributeName) |
| 255 | { |
| 256 | auto search = base.find(attributeName); |
| 257 | if (search == base.end()) |
| 258 | { |
| 259 | throw std::runtime_error("missing attribute " + attributeName); |
| 260 | } |
| 261 | return search->second; |
| 262 | } |
| 263 | |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 264 | void populatePidInfo( |
| 265 | sdbusplus::bus::bus& bus, |
| 266 | const std::unordered_map<std::string, DbusVariantType>& base, |
| 267 | struct conf::ControllerInfo& info, const std::string* thresholdProperty) |
| 268 | { |
Jason Ling | 6fc301f | 2020-07-23 12:39:57 -0700 | [diff] [blame] | 269 | info.type = std::get<std::string>(getPIDAttribute(base, "Class")); |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 270 | if (info.type == "fan") |
| 271 | { |
| 272 | info.setpoint = 0; |
| 273 | } |
| 274 | else |
| 275 | { |
Jason Ling | 6fc301f | 2020-07-23 12:39:57 -0700 | [diff] [blame] | 276 | info.setpoint = std::visit(VariantToDoubleVisitor(), |
| 277 | getPIDAttribute(base, "SetPoint")); |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | if (thresholdProperty != nullptr) |
| 281 | { |
| 282 | std::string interface; |
| 283 | if (*thresholdProperty == "WarningHigh" || |
| 284 | *thresholdProperty == "WarningLow") |
| 285 | { |
| 286 | interface = thresholds::warningInterface; |
| 287 | } |
| 288 | else |
| 289 | { |
| 290 | interface = thresholds::criticalInterface; |
| 291 | } |
| 292 | const std::string& path = sensorConfig[info.inputs.front()].readPath; |
| 293 | |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 294 | DbusHelper helper(sdbusplus::bus::new_system()); |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 295 | std::string service = helper.getService(interface, path); |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 296 | double reading = 0; |
| 297 | try |
| 298 | { |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 299 | helper.getProperty(service, path, interface, *thresholdProperty, |
| 300 | reading); |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 301 | } |
| 302 | catch (const sdbusplus::exception::SdBusError& ex) |
| 303 | { |
| 304 | // unsupported threshold, leaving reading at 0 |
| 305 | } |
| 306 | |
| 307 | info.setpoint += reading; |
| 308 | } |
| 309 | |
| 310 | info.pidInfo.ts = 1.0; // currently unused |
Jason Ling | 6fc301f | 2020-07-23 12:39:57 -0700 | [diff] [blame] | 311 | info.pidInfo.proportionalCoeff = std::visit( |
| 312 | VariantToDoubleVisitor(), getPIDAttribute(base, "PCoefficient")); |
| 313 | info.pidInfo.integralCoeff = std::visit( |
| 314 | VariantToDoubleVisitor(), getPIDAttribute(base, "ICoefficient")); |
| 315 | info.pidInfo.feedFwdOffset = std::visit( |
| 316 | VariantToDoubleVisitor(), getPIDAttribute(base, "FFOffCoefficient")); |
| 317 | info.pidInfo.feedFwdGain = std::visit( |
| 318 | VariantToDoubleVisitor(), getPIDAttribute(base, "FFGainCoefficient")); |
| 319 | info.pidInfo.integralLimit.max = std::visit( |
| 320 | VariantToDoubleVisitor(), getPIDAttribute(base, "ILimitMax")); |
| 321 | info.pidInfo.integralLimit.min = std::visit( |
| 322 | VariantToDoubleVisitor(), getPIDAttribute(base, "ILimitMin")); |
| 323 | info.pidInfo.outLim.max = std::visit(VariantToDoubleVisitor(), |
| 324 | getPIDAttribute(base, "OutLimitMax")); |
| 325 | info.pidInfo.outLim.min = std::visit(VariantToDoubleVisitor(), |
| 326 | getPIDAttribute(base, "OutLimitMin")); |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 327 | info.pidInfo.slewNeg = |
Jason Ling | 6fc301f | 2020-07-23 12:39:57 -0700 | [diff] [blame] | 328 | std::visit(VariantToDoubleVisitor(), getPIDAttribute(base, "SlewNeg")); |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 329 | info.pidInfo.slewPos = |
Jason Ling | 6fc301f | 2020-07-23 12:39:57 -0700 | [diff] [blame] | 330 | std::visit(VariantToDoubleVisitor(), getPIDAttribute(base, "SlewPos")); |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 331 | double negativeHysteresis = 0; |
| 332 | double positiveHysteresis = 0; |
| 333 | |
| 334 | auto findNeg = base.find("NegativeHysteresis"); |
| 335 | auto findPos = base.find("PositiveHysteresis"); |
| 336 | |
| 337 | if (findNeg != base.end()) |
| 338 | { |
| 339 | negativeHysteresis = |
| 340 | std::visit(VariantToDoubleVisitor(), findNeg->second); |
| 341 | } |
| 342 | |
| 343 | if (findPos != base.end()) |
| 344 | { |
| 345 | positiveHysteresis = |
| 346 | std::visit(VariantToDoubleVisitor(), findPos->second); |
| 347 | } |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 348 | info.pidInfo.negativeHysteresis = negativeHysteresis; |
| 349 | info.pidInfo.positiveHysteresis = positiveHysteresis; |
| 350 | } |
| 351 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 352 | bool init(sdbusplus::bus::bus& bus, boost::asio::steady_timer& timer) |
| 353 | { |
| 354 | |
| 355 | sensorConfig.clear(); |
| 356 | zoneConfig.clear(); |
| 357 | zoneDetailsConfig.clear(); |
| 358 | |
| 359 | createMatches(bus, timer); |
| 360 | |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 361 | auto mapper = |
| 362 | bus.new_method_call("xyz.openbmc_project.ObjectMapper", |
| 363 | "/xyz/openbmc_project/object_mapper", |
| 364 | "xyz.openbmc_project.ObjectMapper", "GetSubTree"); |
James Feist | 26e8c6a | 2018-10-25 10:38:26 -0700 | [diff] [blame] | 365 | mapper.append("/", 0, |
Patrick Venture | 0911bfe | 2020-08-10 12:51:40 -0700 | [diff] [blame] | 366 | std::array<const char*, 6>{ |
| 367 | objectManagerInterface, pidConfigurationInterface, |
| 368 | pidZoneConfigurationInterface, |
| 369 | stepwiseConfigurationInterface, sensorInterface, |
| 370 | defaultPwmInterface}); |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 371 | std::unordered_map< |
| 372 | std::string, std::unordered_map<std::string, std::vector<std::string>>> |
| 373 | respData; |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 374 | try |
| 375 | { |
| 376 | auto resp = bus.call(mapper); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 377 | resp.read(respData); |
| 378 | } |
| 379 | catch (sdbusplus::exception_t&) |
| 380 | { |
| 381 | // can't do anything without mapper call data |
| 382 | throw std::runtime_error("ObjectMapper Call Failure"); |
| 383 | } |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 384 | |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 385 | if (respData.empty()) |
| 386 | { |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 387 | // can't do anything without mapper call data |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 388 | throw std::runtime_error("No configuration data available from Mapper"); |
| 389 | } |
| 390 | // create a map of pair of <has pid configuration, ObjectManager path> |
| 391 | std::unordered_map<std::string, std::pair<bool, std::string>> owners; |
| 392 | // and a map of <path, interface> for sensors |
| 393 | std::unordered_map<std::string, std::string> sensors; |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 394 | for (const auto& objectPair : respData) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 395 | { |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 396 | for (const auto& ownerPair : objectPair.second) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 397 | { |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 398 | auto& owner = owners[ownerPair.first]; |
| 399 | for (const std::string& interface : ownerPair.second) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 400 | { |
| 401 | |
| 402 | if (interface == objectManagerInterface) |
| 403 | { |
| 404 | owner.second = objectPair.first; |
| 405 | } |
| 406 | if (interface == pidConfigurationInterface || |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 407 | interface == pidZoneConfigurationInterface || |
| 408 | interface == stepwiseConfigurationInterface) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 409 | { |
| 410 | owner.first = true; |
| 411 | } |
Patrick Venture | 0911bfe | 2020-08-10 12:51:40 -0700 | [diff] [blame] | 412 | if (interface == sensorInterface || |
| 413 | interface == defaultPwmInterface) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 414 | { |
| 415 | // we're not interested in pwm sensors, just pwm control |
| 416 | if (interface == sensorInterface && |
| 417 | objectPair.first.find("pwm") != std::string::npos) |
| 418 | { |
| 419 | continue; |
| 420 | } |
| 421 | sensors[objectPair.first] = interface; |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | ManagedObjectType configurations; |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 427 | for (const auto& owner : owners) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 428 | { |
| 429 | // skip if no pid configuration (means probably a sensor) |
| 430 | if (!owner.second.first) |
| 431 | { |
| 432 | continue; |
| 433 | } |
| 434 | auto endpoint = bus.new_method_call( |
| 435 | owner.first.c_str(), owner.second.second.c_str(), |
| 436 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 437 | ManagedObjectType configuration; |
| 438 | try |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 439 | { |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 440 | auto responce = bus.call(endpoint); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 441 | responce.read(configuration); |
| 442 | } |
| 443 | catch (sdbusplus::exception_t&) |
| 444 | { |
| 445 | // this shouldn't happen, probably means daemon crashed |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 446 | throw std::runtime_error("Error getting managed objects from " + |
| 447 | owner.first); |
| 448 | } |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 449 | |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 450 | for (auto& pathPair : configuration) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 451 | { |
| 452 | if (pathPair.second.find(pidConfigurationInterface) != |
| 453 | pathPair.second.end() || |
| 454 | pathPair.second.find(pidZoneConfigurationInterface) != |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 455 | pathPair.second.end() || |
| 456 | pathPair.second.find(stepwiseConfigurationInterface) != |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 457 | pathPair.second.end()) |
| 458 | { |
| 459 | configurations.emplace(pathPair); |
| 460 | } |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
| 464 | // remove controllers from config that aren't in the current profile(s) |
James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 465 | std::vector<std::string> selectedProfiles = getSelectedProfiles(bus); |
| 466 | if (selectedProfiles.size()) |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 467 | { |
James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 468 | for (auto pathIt = configurations.begin(); |
| 469 | pathIt != configurations.end();) |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 470 | { |
James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 471 | for (auto confIt = pathIt->second.begin(); |
| 472 | confIt != pathIt->second.end();) |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 473 | { |
James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 474 | auto profilesFind = confIt->second.find("Profiles"); |
| 475 | if (profilesFind == confIt->second.end()) |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 476 | { |
James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 477 | confIt++; |
| 478 | continue; // if no profiles selected, apply always |
| 479 | } |
| 480 | auto profiles = |
| 481 | std::get<std::vector<std::string>>(profilesFind->second); |
| 482 | if (profiles.empty()) |
| 483 | { |
| 484 | confIt++; |
| 485 | continue; |
| 486 | } |
| 487 | |
| 488 | bool found = false; |
| 489 | for (const std::string& profile : profiles) |
| 490 | { |
| 491 | if (std::find(selectedProfiles.begin(), |
| 492 | selectedProfiles.end(), |
| 493 | profile) != selectedProfiles.end()) |
| 494 | { |
| 495 | found = true; |
| 496 | break; |
| 497 | } |
| 498 | } |
| 499 | if (found) |
| 500 | { |
| 501 | confIt++; |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 502 | } |
| 503 | else |
| 504 | { |
James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 505 | confIt = pathIt->second.erase(confIt); |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 506 | } |
| 507 | } |
James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 508 | if (pathIt->second.empty()) |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 509 | { |
James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 510 | pathIt = configurations.erase(pathIt); |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 511 | } |
James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 512 | else |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 513 | { |
James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 514 | pathIt++; |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 515 | } |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 516 | } |
| 517 | } |
James Feist | 8c3c51e | 2018-08-08 16:31:43 -0700 | [diff] [blame] | 518 | |
Josh Lehan | 998fbe6 | 2020-09-20 21:21:05 -0700 | [diff] [blame] | 519 | // On D-Bus, although not necessary, |
| 520 | // having the "zoneID" field can still be useful, |
| 521 | // as it is used for diagnostic messages, |
| 522 | // logging file names, and so on. |
| 523 | // Accept optional "ZoneIndex" parameter to explicitly specify. |
| 524 | // If not present, or not unique, auto-assign index, |
| 525 | // using 0-based numbering, ensuring uniqueness. |
| 526 | std::map<std::string, int64_t> foundZones; |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 527 | for (const auto& configuration : configurations) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 528 | { |
| 529 | auto findZone = |
| 530 | configuration.second.find(pidZoneConfigurationInterface); |
| 531 | if (findZone != configuration.second.end()) |
| 532 | { |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 533 | const auto& zone = findZone->second; |
James Feist | ffd418b | 2018-11-15 14:46:36 -0800 | [diff] [blame] | 534 | |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 535 | const std::string& name = std::get<std::string>(zone.at("Name")); |
Josh Lehan | 998fbe6 | 2020-09-20 21:21:05 -0700 | [diff] [blame] | 536 | |
| 537 | auto findZoneIndex = zone.find("ZoneIndex"); |
| 538 | if (findZoneIndex == zone.end()) |
| 539 | { |
| 540 | continue; |
| 541 | } |
| 542 | |
| 543 | auto ptrZoneIndex = std::get_if<double>(&(findZoneIndex->second)); |
| 544 | if (!ptrZoneIndex) |
| 545 | { |
| 546 | continue; |
| 547 | } |
| 548 | |
| 549 | auto desiredIndex = static_cast<int64_t>(*ptrZoneIndex); |
| 550 | auto grantedIndex = setZoneIndex(name, foundZones, desiredIndex); |
| 551 | std::cout << "Zone " << name << " is at ZoneIndex " << grantedIndex |
| 552 | << "\n"; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | for (const auto& configuration : configurations) |
| 557 | { |
| 558 | auto findZone = |
| 559 | configuration.second.find(pidZoneConfigurationInterface); |
| 560 | if (findZone != configuration.second.end()) |
| 561 | { |
| 562 | const auto& zone = findZone->second; |
| 563 | |
| 564 | const std::string& name = std::get<std::string>(zone.at("Name")); |
| 565 | |
| 566 | auto index = getZoneIndex(name, foundZones); |
James Feist | 8c3c51e | 2018-08-08 16:31:43 -0700 | [diff] [blame] | 567 | |
Patrick Venture | c54fbd8 | 2018-10-30 19:40:05 -0700 | [diff] [blame] | 568 | auto& details = zoneDetailsConfig[index]; |
Josh Lehan | 998fbe6 | 2020-09-20 21:21:05 -0700 | [diff] [blame] | 569 | |
James Feist | 3484bed | 2019-02-25 13:28:18 -0800 | [diff] [blame] | 570 | details.minThermalOutput = std::visit(VariantToDoubleVisitor(), |
| 571 | zone.at("MinThermalOutput")); |
Patrick Venture | 8e2fdb3 | 2019-02-11 09:39:59 -0800 | [diff] [blame] | 572 | details.failsafePercent = std::visit(VariantToDoubleVisitor(), |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 573 | zone.at("FailSafePercent")); |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 574 | } |
| 575 | auto findBase = configuration.second.find(pidConfigurationInterface); |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 576 | // loop through all the PID configurations and fill out a sensor config |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 577 | if (findBase != configuration.second.end()) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 578 | { |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 579 | const auto& base = |
| 580 | configuration.second.at(pidConfigurationInterface); |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 581 | const std::string pidName = std::get<std::string>(base.at("Name")); |
| 582 | const std::string pidClass = |
| 583 | std::get<std::string>(base.at("Class")); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 584 | const std::vector<std::string>& zones = |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 585 | std::get<std::vector<std::string>>(base.at("Zones")); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 586 | for (const std::string& zone : zones) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 587 | { |
Josh Lehan | 998fbe6 | 2020-09-20 21:21:05 -0700 | [diff] [blame] | 588 | auto index = getZoneIndex(zone, foundZones); |
| 589 | |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 590 | conf::PIDConf& conf = zoneConfig[index]; |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 591 | std::vector<std::string> inputSensorNames( |
| 592 | std::get<std::vector<std::string>>(base.at("Inputs"))); |
| 593 | std::vector<std::string> outputSensorNames; |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 594 | |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 595 | // assumption: all fan pids must have at least one output |
| 596 | if (pidClass == "fan") |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 597 | { |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 598 | outputSensorNames = std::get<std::vector<std::string>>( |
| 599 | getPIDAttribute(base, "Outputs")); |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 600 | } |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 601 | |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 602 | std::vector<SensorInterfaceType> inputSensorInterfaces; |
| 603 | std::vector<SensorInterfaceType> outputSensorInterfaces; |
| 604 | /* populate an interface list for different sensor direction |
| 605 | * types (input,output) |
| 606 | */ |
| 607 | /* take the Inputs from the configuration and generate |
| 608 | * a list of dbus descriptors (path, interface). |
| 609 | * Mapping can be many-to-one since an element of Inputs can be |
| 610 | * a regex |
| 611 | */ |
| 612 | for (const std::string& sensorName : inputSensorNames) |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 613 | { |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 614 | findSensors(sensors, sensorNameToDbusName(sensorName), |
| 615 | inputSensorInterfaces); |
| 616 | } |
| 617 | for (const std::string& sensorName : outputSensorNames) |
| 618 | { |
| 619 | findSensors(sensors, sensorNameToDbusName(sensorName), |
| 620 | outputSensorInterfaces); |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 621 | } |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 622 | |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 623 | inputSensorNames.clear(); |
| 624 | for (const SensorInterfaceType& inputSensorInterface : |
| 625 | inputSensorInterfaces) |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 626 | { |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 627 | const std::string& dbusInterface = |
| 628 | inputSensorInterface.second; |
| 629 | const std::string& inputSensorPath = |
| 630 | inputSensorInterface.first; |
| 631 | std::string inputSensorName = |
| 632 | getSensorNameFromPath(inputSensorPath); |
| 633 | auto& config = sensorConfig[inputSensorName]; |
| 634 | inputSensorNames.push_back(inputSensorName); |
| 635 | config.type = pidClass; |
| 636 | config.readPath = inputSensorInterface.first; |
| 637 | // todo: maybe un-hardcode this if we run into slower |
| 638 | // timeouts with sensors |
| 639 | if (config.type == "temp") |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 640 | { |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 641 | config.timeout = 0; |
| 642 | config.ignoreDbusMinMax = true; |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 643 | } |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 644 | if (dbusInterface != sensorInterface) |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 645 | { |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 646 | /* all expected inputs in the configuration are expected |
| 647 | * to be sensor interfaces |
| 648 | */ |
| 649 | throw std::runtime_error( |
| 650 | "sensor at dbus path [" + inputSensorPath + |
| 651 | "] has an interface [" + dbusInterface + |
| 652 | "] that does not match the expected interface of " + |
| 653 | sensorInterface); |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 654 | } |
| 655 | } |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 656 | |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 657 | /* fan pids need to pair up tach sensors with their pwm |
| 658 | * counterparts |
| 659 | */ |
| 660 | if (pidClass == "fan") |
| 661 | { |
| 662 | /* If a PID is a fan there should be either |
| 663 | * (1) one output(pwm) per input(tach) |
| 664 | * OR |
| 665 | * (2) one putput(pwm) for all inputs(tach) |
| 666 | * everything else indicates a bad configuration. |
| 667 | */ |
| 668 | bool singlePwm = false; |
| 669 | if (outputSensorInterfaces.size() == 1) |
| 670 | { |
| 671 | /* one pwm, set write paths for all fan sensors to it */ |
| 672 | singlePwm = true; |
| 673 | } |
| 674 | else if (inputSensorInterfaces.size() == |
| 675 | outputSensorInterfaces.size()) |
| 676 | { |
| 677 | /* one to one mapping, each fan sensor gets its own pwm |
| 678 | * control */ |
| 679 | singlePwm = false; |
| 680 | } |
| 681 | else |
| 682 | { |
| 683 | throw std::runtime_error( |
| 684 | "fan PID has invalid number of Outputs"); |
| 685 | } |
| 686 | std::string fanSensorName; |
| 687 | std::string pwmPath; |
| 688 | std::string pwmInterface; |
| 689 | if (singlePwm) |
| 690 | { |
| 691 | /* if just a single output(pwm) is provided then use |
| 692 | * that pwm control path for all the fan sensor write |
| 693 | * path configs |
| 694 | */ |
| 695 | pwmPath = outputSensorInterfaces.at(0).first; |
| 696 | pwmInterface = outputSensorInterfaces.at(0).second; |
| 697 | } |
| 698 | for (uint32_t idx = 0; idx < inputSensorInterfaces.size(); |
| 699 | idx++) |
| 700 | { |
| 701 | if (!singlePwm) |
| 702 | { |
| 703 | pwmPath = outputSensorInterfaces.at(idx).first; |
| 704 | pwmInterface = |
| 705 | outputSensorInterfaces.at(idx).second; |
| 706 | } |
Patrick Venture | 0911bfe | 2020-08-10 12:51:40 -0700 | [diff] [blame] | 707 | if (defaultPwmInterface != pwmInterface) |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 708 | { |
| 709 | throw std::runtime_error( |
| 710 | "fan pwm control at dbus path [" + pwmPath + |
| 711 | "] has an interface [" + pwmInterface + |
| 712 | "] that does not match the expected interface " |
| 713 | "of " + |
Patrick Venture | 0911bfe | 2020-08-10 12:51:40 -0700 | [diff] [blame] | 714 | defaultPwmInterface); |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 715 | } |
| 716 | const std::string& fanPath = |
| 717 | inputSensorInterfaces.at(idx).first; |
| 718 | fanSensorName = getSensorNameFromPath(fanPath); |
| 719 | auto& fanConfig = sensorConfig[fanSensorName]; |
| 720 | fanConfig.writePath = pwmPath; |
| 721 | // todo: un-hardcode this if there are fans with |
| 722 | // different ranges |
| 723 | fanConfig.max = 255; |
| 724 | fanConfig.min = 0; |
| 725 | } |
| 726 | } |
James Feist | 11d243d | 2019-06-24 16:18:40 -0700 | [diff] [blame] | 727 | // if the sensors aren't available in the current state, don't |
| 728 | // add them to the configuration. |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 729 | if (inputSensorNames.empty()) |
James Feist | 11d243d | 2019-06-24 16:18:40 -0700 | [diff] [blame] | 730 | { |
| 731 | continue; |
| 732 | } |
| 733 | |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 734 | std::string offsetType; |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 735 | |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 736 | // SetPointOffset is a threshold value to pull from the sensor |
| 737 | // to apply an offset. For upper thresholds this means the |
| 738 | // setpoint is usually negative. |
| 739 | auto findSetpointOffset = base.find("SetPointOffset"); |
| 740 | if (findSetpointOffset != base.end()) |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 741 | { |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 742 | offsetType = |
| 743 | std::get<std::string>(findSetpointOffset->second); |
| 744 | if (std::find(thresholds::types.begin(), |
| 745 | thresholds::types.end(), |
| 746 | offsetType) == thresholds::types.end()) |
| 747 | { |
| 748 | throw std::runtime_error("Unsupported type: " + |
| 749 | offsetType); |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | if (offsetType.empty()) |
| 754 | { |
| 755 | struct conf::ControllerInfo& info = |
| 756 | conf[std::get<std::string>(base.at("Name"))]; |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 757 | info.inputs = std::move(inputSensorNames); |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 758 | populatePidInfo(bus, base, info, nullptr); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 759 | } |
| 760 | else |
| 761 | { |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 762 | // we have to split up the inputs, as in practice t-control |
| 763 | // values will differ, making setpoints differ |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 764 | for (const std::string& input : inputSensorNames) |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 765 | { |
| 766 | struct conf::ControllerInfo& info = conf[input]; |
| 767 | info.inputs.emplace_back(input); |
| 768 | populatePidInfo(bus, base, info, &offsetType); |
| 769 | } |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 770 | } |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 771 | } |
| 772 | } |
| 773 | auto findStepwise = |
| 774 | configuration.second.find(stepwiseConfigurationInterface); |
| 775 | if (findStepwise != configuration.second.end()) |
| 776 | { |
| 777 | const auto& base = findStepwise->second; |
| 778 | const std::vector<std::string>& zones = |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 779 | std::get<std::vector<std::string>>(base.at("Zones")); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 780 | for (const std::string& zone : zones) |
| 781 | { |
Josh Lehan | 998fbe6 | 2020-09-20 21:21:05 -0700 | [diff] [blame] | 782 | auto index = getZoneIndex(zone, foundZones); |
| 783 | |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 784 | conf::PIDConf& conf = zoneConfig[index]; |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 785 | |
| 786 | std::vector<std::string> inputs; |
| 787 | std::vector<std::string> sensorNames = |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 788 | std::get<std::vector<std::string>>(base.at("Inputs")); |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 789 | |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 790 | bool sensorFound = false; |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 791 | for (const std::string& sensorName : sensorNames) |
| 792 | { |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 793 | std::vector<std::pair<std::string, std::string>> |
| 794 | sensorPathIfacePairs; |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 795 | if (!findSensors(sensors, sensorNameToDbusName(sensorName), |
| 796 | sensorPathIfacePairs)) |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 797 | { |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 798 | break; |
| 799 | } |
| 800 | |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 801 | for (const auto& sensorPathIfacePair : sensorPathIfacePairs) |
| 802 | { |
| 803 | size_t idx = |
| 804 | sensorPathIfacePair.first.find_last_of("/") + 1; |
| 805 | std::string shortName = |
| 806 | sensorPathIfacePair.first.substr(idx); |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 807 | |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 808 | inputs.push_back(shortName); |
| 809 | auto& config = sensorConfig[shortName]; |
Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 810 | config.readPath = sensorPathIfacePair.first; |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 811 | config.type = "temp"; |
James Feist | 3660b38 | 2019-11-11 16:29:19 -0800 | [diff] [blame] | 812 | config.ignoreDbusMinMax = true; |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 813 | // todo: maybe un-hardcode this if we run into slower |
| 814 | // timeouts with sensors |
| 815 | |
James Feist | 2642cb5 | 2019-02-25 13:00:16 -0800 | [diff] [blame] | 816 | config.timeout = 0; |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 817 | sensorFound = true; |
| 818 | } |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 819 | } |
| 820 | if (!sensorFound) |
| 821 | { |
| 822 | continue; |
| 823 | } |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 824 | struct conf::ControllerInfo& info = |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 825 | conf[std::get<std::string>(base.at("Name"))]; |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 826 | info.inputs = std::move(inputs); |
| 827 | |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 828 | info.type = "stepwise"; |
| 829 | info.stepwiseInfo.ts = 1.0; // currently unused |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 830 | info.stepwiseInfo.positiveHysteresis = 0.0; |
| 831 | info.stepwiseInfo.negativeHysteresis = 0.0; |
James Feist | 608304d | 2019-02-25 10:01:42 -0800 | [diff] [blame] | 832 | std::string subtype = std::get<std::string>(base.at("Class")); |
| 833 | |
| 834 | info.stepwiseInfo.isCeiling = (subtype == "Ceiling"); |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 835 | auto findPosHyst = base.find("PositiveHysteresis"); |
| 836 | auto findNegHyst = base.find("NegativeHysteresis"); |
| 837 | if (findPosHyst != base.end()) |
| 838 | { |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 839 | info.stepwiseInfo.positiveHysteresis = std::visit( |
James Feist | 208abce | 2018-12-06 09:59:10 -0800 | [diff] [blame] | 840 | VariantToDoubleVisitor(), findPosHyst->second); |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 841 | } |
| 842 | if (findNegHyst != base.end()) |
| 843 | { |
James Feist | 5782ab8 | 2019-04-02 08:38:48 -0700 | [diff] [blame] | 844 | info.stepwiseInfo.negativeHysteresis = std::visit( |
James Feist | 208abce | 2018-12-06 09:59:10 -0800 | [diff] [blame] | 845 | VariantToDoubleVisitor(), findNegHyst->second); |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 846 | } |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 847 | std::vector<double> readings = |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 848 | std::get<std::vector<double>>(base.at("Reading")); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 849 | if (readings.size() > ec::maxStepwisePoints) |
| 850 | { |
| 851 | throw std::invalid_argument("Too many stepwise points."); |
| 852 | } |
| 853 | if (readings.empty()) |
| 854 | { |
| 855 | throw std::invalid_argument( |
| 856 | "Must have one stepwise point."); |
| 857 | } |
| 858 | std::copy(readings.begin(), readings.end(), |
| 859 | info.stepwiseInfo.reading); |
| 860 | if (readings.size() < ec::maxStepwisePoints) |
| 861 | { |
| 862 | info.stepwiseInfo.reading[readings.size()] = |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 863 | std::numeric_limits<double>::quiet_NaN(); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 864 | } |
| 865 | std::vector<double> outputs = |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 866 | std::get<std::vector<double>>(base.at("Output")); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 867 | if (readings.size() != outputs.size()) |
| 868 | { |
| 869 | throw std::invalid_argument( |
| 870 | "Outputs size must match readings"); |
| 871 | } |
| 872 | std::copy(outputs.begin(), outputs.end(), |
| 873 | info.stepwiseInfo.output); |
| 874 | if (outputs.size() < ec::maxStepwisePoints) |
| 875 | { |
| 876 | info.stepwiseInfo.output[outputs.size()] = |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 877 | std::numeric_limits<double>::quiet_NaN(); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 878 | } |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | } |
Patrick Venture | 39199b4 | 2020-10-08 14:40:29 -0700 | [diff] [blame^] | 882 | if constexpr (pid_control::conf::DEBUG) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 883 | { |
Patrick Venture | 39199b4 | 2020-10-08 14:40:29 -0700 | [diff] [blame^] | 884 | debugPrint(sensorConfig, zoneConfig, zoneDetailsConfig); |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 885 | } |
James Feist | c959c42 | 2018-11-01 12:33:40 -0700 | [diff] [blame] | 886 | if (zoneConfig.empty() || zoneDetailsConfig.empty()) |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 887 | { |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 888 | std::cerr |
| 889 | << "No fan zones, application pausing until new configuration\n"; |
| 890 | return false; |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 891 | } |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 892 | return true; |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 893 | } |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 894 | |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 895 | } // namespace dbus_configuration |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 896 | } // namespace pid_control |