| 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 | */ | 
|  | 16 |  | 
| Patrick Venture | 0771659 | 2018-10-14 11:46:40 -0700 | [diff] [blame] | 17 | #include "conf.hpp" | 
| James Feist | 0c8223b | 2019-05-08 15:33:33 -0700 | [diff] [blame] | 18 | #include "util.hpp" | 
| Patrick Venture | 0771659 | 2018-10-14 11:46:40 -0700 | [diff] [blame] | 19 |  | 
| Patrick Venture | 107a25d | 2018-10-13 14:08:09 -0700 | [diff] [blame] | 20 | #include <algorithm> | 
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 21 | #include <boost/asio/steady_timer.hpp> | 
| James Feist | 64f072a | 2018-08-10 16:39:24 -0700 | [diff] [blame] | 22 | #include <chrono> | 
| James Feist | 64f072a | 2018-08-10 16:39:24 -0700 | [diff] [blame] | 23 | #include <functional> | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 24 | #include <iostream> | 
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 25 | #include <list> | 
| James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 26 | #include <regex> | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 27 | #include <sdbusplus/bus.hpp> | 
| James Feist | 64f072a | 2018-08-10 16:39:24 -0700 | [diff] [blame] | 28 | #include <sdbusplus/bus/match.hpp> | 
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 29 | #include <sdbusplus/exception.hpp> | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 30 | #include <set> | 
|  | 31 | #include <unordered_map> | 
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 32 | #include <variant> | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 33 |  | 
|  | 34 | static constexpr bool DEBUG = false; // enable to print found configuration | 
|  | 35 |  | 
| James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 36 | extern std::map<std::string, struct conf::SensorConfig> sensorConfig; | 
|  | 37 | extern std::map<int64_t, conf::PIDConf> zoneConfig; | 
|  | 38 | extern std::map<int64_t, struct conf::ZoneConfig> zoneDetailsConfig; | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 39 |  | 
| Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 40 | constexpr const char* pidConfigurationInterface = | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 41 | "xyz.openbmc_project.Configuration.Pid"; | 
| Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 42 | constexpr const char* objectManagerInterface = | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 43 | "org.freedesktop.DBus.ObjectManager"; | 
| Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 44 | constexpr const char* pidZoneConfigurationInterface = | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 45 | "xyz.openbmc_project.Configuration.Pid.Zone"; | 
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 46 | constexpr const char* stepwiseConfigurationInterface = | 
|  | 47 | "xyz.openbmc_project.Configuration.Stepwise"; | 
| James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 48 | constexpr const char* thermalControlIface = | 
|  | 49 | "xyz.openbmc_project.Control.ThermalMode"; | 
| Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 50 | constexpr const char* sensorInterface = "xyz.openbmc_project.Sensor.Value"; | 
|  | 51 | constexpr const char* pwmInterface = "xyz.openbmc_project.Control.FanPwm"; | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 52 |  | 
|  | 53 | namespace dbus_configuration | 
|  | 54 | { | 
|  | 55 |  | 
| James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 56 | bool findSensors(const std::unordered_map<std::string, std::string>& sensors, | 
|  | 57 | const std::string& search, | 
|  | 58 | std::vector<std::pair<std::string, std::string>>& matches) | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 59 | { | 
| James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 60 | std::smatch match; | 
|  | 61 | std::regex reg(search); | 
|  | 62 | for (const auto& sensor : sensors) | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 63 | { | 
| James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 64 | if (std::regex_search(sensor.first, match, reg)) | 
|  | 65 | { | 
|  | 66 | matches.push_back(sensor); | 
|  | 67 | } | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 68 | } | 
| Patrick Venture | 107a25d | 2018-10-13 14:08:09 -0700 | [diff] [blame] | 69 |  | 
| James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 70 | return matches.size() > 0; | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 71 | } | 
|  | 72 |  | 
|  | 73 | // this function prints the configuration into a form similar to the cpp | 
|  | 74 | // generated code to help in verification, should be turned off during normal | 
|  | 75 | // use | 
|  | 76 | void debugPrint(void) | 
|  | 77 | { | 
|  | 78 | // print sensor config | 
|  | 79 | std::cout << "sensor config:\n"; | 
|  | 80 | std::cout << "{\n"; | 
| Patrick Venture | c54fbd8 | 2018-10-30 19:40:05 -0700 | [diff] [blame] | 81 | for (const auto& pair : sensorConfig) | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 82 | { | 
|  | 83 |  | 
|  | 84 | std::cout << "\t{" << pair.first << ",\n\t\t{"; | 
|  | 85 | std::cout << pair.second.type << ", "; | 
| Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 86 | std::cout << pair.second.readPath << ", "; | 
|  | 87 | std::cout << pair.second.writePath << ", "; | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 88 | std::cout << pair.second.min << ", "; | 
|  | 89 | std::cout << pair.second.max << ", "; | 
|  | 90 | std::cout << pair.second.timeout << "},\n\t},\n"; | 
|  | 91 | } | 
|  | 92 | std::cout << "}\n\n"; | 
|  | 93 | std::cout << "ZoneDetailsConfig\n"; | 
|  | 94 | std::cout << "{\n"; | 
| Patrick Venture | c54fbd8 | 2018-10-30 19:40:05 -0700 | [diff] [blame] | 95 | for (const auto& zone : zoneDetailsConfig) | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 96 | { | 
|  | 97 | std::cout << "\t{" << zone.first << ",\n"; | 
| James Feist | 3484bed | 2019-02-25 13:28:18 -0800 | [diff] [blame] | 98 | std::cout << "\t\t{" << zone.second.minThermalOutput << ", "; | 
| Patrick Venture | 8e2fdb3 | 2019-02-11 09:39:59 -0800 | [diff] [blame] | 99 | std::cout << zone.second.failsafePercent << "}\n\t},\n"; | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 100 | } | 
|  | 101 | std::cout << "}\n\n"; | 
|  | 102 | std::cout << "ZoneConfig\n"; | 
|  | 103 | std::cout << "{\n"; | 
| Patrick Venture | c54fbd8 | 2018-10-30 19:40:05 -0700 | [diff] [blame] | 104 | for (const auto& zone : zoneConfig) | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 105 | { | 
|  | 106 | std::cout << "\t{" << zone.first << "\n"; | 
| Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 107 | for (const auto& pidconf : zone.second) | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 108 | { | 
|  | 109 | std::cout << "\t\t{" << pidconf.first << ",\n"; | 
|  | 110 | std::cout << "\t\t\t{" << pidconf.second.type << ",\n"; | 
|  | 111 | std::cout << "\t\t\t{"; | 
| Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 112 | for (const auto& input : pidconf.second.inputs) | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 113 | { | 
|  | 114 | std::cout << "\n\t\t\t" << input << ",\n"; | 
|  | 115 | } | 
|  | 116 | std::cout << "\t\t\t}\n"; | 
|  | 117 | std::cout << "\t\t\t" << pidconf.second.setpoint << ",\n"; | 
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 118 | std::cout << "\t\t\t{" << pidconf.second.pidInfo.ts << ",\n"; | 
| Patrick Venture | 7442c37 | 2019-02-11 10:21:05 -0800 | [diff] [blame] | 119 | std::cout << "\t\t\t" << pidconf.second.pidInfo.proportionalCoeff | 
|  | 120 | << ",\n"; | 
|  | 121 | std::cout << "\t\t\t" << pidconf.second.pidInfo.integralCoeff | 
|  | 122 | << ",\n"; | 
|  | 123 | std::cout << "\t\t\t" << pidconf.second.pidInfo.feedFwdOffset | 
|  | 124 | << ",\n"; | 
|  | 125 | std::cout << "\t\t\t" << pidconf.second.pidInfo.feedFwdGain | 
|  | 126 | << ",\n"; | 
|  | 127 | std::cout << "\t\t\t{" << pidconf.second.pidInfo.integralLimit.min | 
|  | 128 | << "," << pidconf.second.pidInfo.integralLimit.max | 
|  | 129 | << "},\n"; | 
|  | 130 | std::cout << "\t\t\t{" << pidconf.second.pidInfo.outLim.min << "," | 
|  | 131 | << pidconf.second.pidInfo.outLim.max << "},\n"; | 
|  | 132 | std::cout << "\t\t\t" << pidconf.second.pidInfo.slewNeg << ",\n"; | 
|  | 133 | std::cout << "\t\t\t" << pidconf.second.pidInfo.slewPos << ",\n"; | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 134 | std::cout << "\t\t\t}\n\t\t}\n"; | 
|  | 135 | } | 
|  | 136 | std::cout << "\t},\n"; | 
|  | 137 | } | 
|  | 138 | std::cout << "}\n\n"; | 
|  | 139 | } | 
|  | 140 |  | 
| James Feist | ffd418b | 2018-11-15 14:46:36 -0800 | [diff] [blame] | 141 | size_t getZoneIndex(const std::string& name, std::vector<std::string>& zones) | 
|  | 142 | { | 
|  | 143 | auto it = std::find(zones.begin(), zones.end(), name); | 
|  | 144 | if (it == zones.end()) | 
|  | 145 | { | 
|  | 146 | zones.emplace_back(name); | 
|  | 147 | it = zones.end() - 1; | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | return it - zones.begin(); | 
|  | 151 | } | 
|  | 152 |  | 
| James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 153 | std::vector<std::string> getSelectedProfiles(sdbusplus::bus::bus& bus) | 
|  | 154 | { | 
|  | 155 | std::vector<std::string> ret; | 
|  | 156 | auto mapper = | 
|  | 157 | bus.new_method_call("xyz.openbmc_project.ObjectMapper", | 
|  | 158 | "/xyz/openbmc_project/object_mapper", | 
|  | 159 | "xyz.openbmc_project.ObjectMapper", "GetSubTree"); | 
|  | 160 | mapper.append("/", 0, std::array<const char*, 1>{thermalControlIface}); | 
|  | 161 | std::unordered_map< | 
|  | 162 | std::string, std::unordered_map<std::string, std::vector<std::string>>> | 
|  | 163 | respData; | 
|  | 164 |  | 
|  | 165 | try | 
|  | 166 | { | 
|  | 167 | auto resp = bus.call(mapper); | 
|  | 168 | resp.read(respData); | 
|  | 169 | } | 
|  | 170 | catch (sdbusplus::exception_t&) | 
|  | 171 | { | 
|  | 172 | // can't do anything without mapper call data | 
|  | 173 | throw std::runtime_error("ObjectMapper Call Failure"); | 
|  | 174 | } | 
|  | 175 | if (respData.empty()) | 
|  | 176 | { | 
|  | 177 | // if the user has profiles but doesn't expose the interface to select | 
|  | 178 | // one, just go ahead without using profiles | 
|  | 179 | return ret; | 
|  | 180 | } | 
|  | 181 |  | 
|  | 182 | // assumption is that we should only have a small handful of selected | 
|  | 183 | // profiles at a time (probably only 1), so calling each individually should | 
|  | 184 | // not incur a large cost | 
|  | 185 | for (const auto& objectPair : respData) | 
|  | 186 | { | 
|  | 187 | const std::string& path = objectPair.first; | 
|  | 188 | for (const auto& ownerPair : objectPair.second) | 
|  | 189 | { | 
|  | 190 | const std::string& busName = ownerPair.first; | 
|  | 191 | auto getProfile = | 
|  | 192 | bus.new_method_call(busName.c_str(), path.c_str(), | 
|  | 193 | "org.freedesktop.DBus.Properties", "Get"); | 
|  | 194 | getProfile.append(thermalControlIface, "Current"); | 
|  | 195 | std::variant<std::string> variantResp; | 
|  | 196 | try | 
|  | 197 | { | 
|  | 198 | auto resp = bus.call(getProfile); | 
|  | 199 | resp.read(variantResp); | 
|  | 200 | } | 
|  | 201 | catch (sdbusplus::exception_t&) | 
|  | 202 | { | 
|  | 203 | throw std::runtime_error("Failure getting profile"); | 
|  | 204 | } | 
|  | 205 | std::string mode = std::get<std::string>(variantResp); | 
|  | 206 | ret.emplace_back(std::move(mode)); | 
|  | 207 | } | 
|  | 208 | } | 
|  | 209 | if constexpr (DEBUG) | 
|  | 210 | { | 
|  | 211 | std::cout << "Profiles selected: "; | 
|  | 212 | for (const auto& profile : ret) | 
|  | 213 | { | 
|  | 214 | std::cout << profile << " "; | 
|  | 215 | } | 
|  | 216 | std::cout << "\n"; | 
|  | 217 | } | 
|  | 218 | return ret; | 
|  | 219 | } | 
|  | 220 |  | 
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 221 | int eventHandler(sd_bus_message*, void* context, sd_bus_error*) | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 222 | { | 
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 223 |  | 
|  | 224 | if (context == nullptr) | 
|  | 225 | { | 
|  | 226 | throw std::runtime_error("Invalid match"); | 
|  | 227 | } | 
|  | 228 | boost::asio::steady_timer* timer = | 
|  | 229 | static_cast<boost::asio::steady_timer*>(context); | 
|  | 230 |  | 
|  | 231 | // do a brief sleep as we tend to get a bunch of these events at | 
|  | 232 | // once | 
|  | 233 | timer->expires_after(std::chrono::seconds(2)); | 
|  | 234 | timer->async_wait([](const boost::system::error_code ec) { | 
|  | 235 | if (ec == boost::asio::error::operation_aborted) | 
|  | 236 | { | 
|  | 237 | /* another timer started*/ | 
|  | 238 | return; | 
|  | 239 | } | 
|  | 240 |  | 
|  | 241 | std::cout << "New configuration detected, reloading\n."; | 
|  | 242 | restartControlLoops(); | 
|  | 243 | }); | 
|  | 244 |  | 
|  | 245 | return 1; | 
|  | 246 | } | 
|  | 247 |  | 
|  | 248 | void createMatches(sdbusplus::bus::bus& bus, boost::asio::steady_timer& timer) | 
|  | 249 | { | 
|  | 250 | // this is a list because the matches can't be moved | 
|  | 251 | static std::list<sdbusplus::bus::match::match> matches; | 
|  | 252 |  | 
| James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 253 | const std::array<std::string, 4> interfaces = { | 
|  | 254 | thermalControlIface, pidConfigurationInterface, | 
|  | 255 | pidZoneConfigurationInterface, stepwiseConfigurationInterface}; | 
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 256 |  | 
|  | 257 | // this list only needs to be created once | 
|  | 258 | if (!matches.empty()) | 
|  | 259 | { | 
|  | 260 | return; | 
|  | 261 | } | 
|  | 262 |  | 
|  | 263 | // we restart when the configuration changes or there are new sensors | 
|  | 264 | for (const auto& interface : interfaces) | 
|  | 265 | { | 
|  | 266 | matches.emplace_back( | 
|  | 267 | bus, | 
|  | 268 | "type='signal',member='PropertiesChanged',arg0namespace='" + | 
|  | 269 | interface + "'", | 
|  | 270 | eventHandler, &timer); | 
|  | 271 | } | 
|  | 272 | matches.emplace_back( | 
|  | 273 | bus, | 
|  | 274 | "type='signal',member='InterfacesAdded',arg0path='/xyz/openbmc_project/" | 
|  | 275 | "sensors/'", | 
|  | 276 | eventHandler, &timer); | 
|  | 277 | } | 
|  | 278 |  | 
|  | 279 | bool init(sdbusplus::bus::bus& bus, boost::asio::steady_timer& timer) | 
|  | 280 | { | 
|  | 281 |  | 
|  | 282 | sensorConfig.clear(); | 
|  | 283 | zoneConfig.clear(); | 
|  | 284 | zoneDetailsConfig.clear(); | 
|  | 285 |  | 
|  | 286 | createMatches(bus, timer); | 
|  | 287 |  | 
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 288 | using DbusVariantType = | 
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 289 | std::variant<uint64_t, int64_t, double, std::string, | 
|  | 290 | std::vector<std::string>, std::vector<double>>; | 
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 291 |  | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 292 | using ManagedObjectType = std::unordered_map< | 
|  | 293 | sdbusplus::message::object_path, | 
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 294 | std::unordered_map<std::string, | 
|  | 295 | std::unordered_map<std::string, DbusVariantType>>>; | 
| James Feist | 64f072a | 2018-08-10 16:39:24 -0700 | [diff] [blame] | 296 |  | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 297 | auto mapper = | 
|  | 298 | bus.new_method_call("xyz.openbmc_project.ObjectMapper", | 
|  | 299 | "/xyz/openbmc_project/object_mapper", | 
|  | 300 | "xyz.openbmc_project.ObjectMapper", "GetSubTree"); | 
| James Feist | 26e8c6a | 2018-10-25 10:38:26 -0700 | [diff] [blame] | 301 | mapper.append("/", 0, | 
| James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 302 | std::array<const char*, 6>{objectManagerInterface, | 
|  | 303 | pidConfigurationInterface, | 
|  | 304 | pidZoneConfigurationInterface, | 
|  | 305 | stepwiseConfigurationInterface, | 
|  | 306 | sensorInterface, pwmInterface}); | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 307 | std::unordered_map< | 
|  | 308 | std::string, std::unordered_map<std::string, std::vector<std::string>>> | 
|  | 309 | respData; | 
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 310 | try | 
|  | 311 | { | 
|  | 312 | auto resp = bus.call(mapper); | 
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 313 | resp.read(respData); | 
|  | 314 | } | 
|  | 315 | catch (sdbusplus::exception_t&) | 
|  | 316 | { | 
|  | 317 | // can't do anything without mapper call data | 
|  | 318 | throw std::runtime_error("ObjectMapper Call Failure"); | 
|  | 319 | } | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 320 |  | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 321 | if (respData.empty()) | 
|  | 322 | { | 
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 323 | // can't do anything without mapper call data | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 324 | throw std::runtime_error("No configuration data available from Mapper"); | 
|  | 325 | } | 
|  | 326 | // create a map of pair of <has pid configuration, ObjectManager path> | 
|  | 327 | std::unordered_map<std::string, std::pair<bool, std::string>> owners; | 
|  | 328 | // and a map of <path, interface> for sensors | 
|  | 329 | std::unordered_map<std::string, std::string> sensors; | 
| Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 330 | for (const auto& objectPair : respData) | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 331 | { | 
| Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 332 | for (const auto& ownerPair : objectPair.second) | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 333 | { | 
| Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 334 | auto& owner = owners[ownerPair.first]; | 
|  | 335 | for (const std::string& interface : ownerPair.second) | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 336 | { | 
|  | 337 |  | 
|  | 338 | if (interface == objectManagerInterface) | 
|  | 339 | { | 
|  | 340 | owner.second = objectPair.first; | 
|  | 341 | } | 
|  | 342 | if (interface == pidConfigurationInterface || | 
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 343 | interface == pidZoneConfigurationInterface || | 
|  | 344 | interface == stepwiseConfigurationInterface) | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 345 | { | 
|  | 346 | owner.first = true; | 
|  | 347 | } | 
|  | 348 | if (interface == sensorInterface || interface == pwmInterface) | 
|  | 349 | { | 
|  | 350 | // we're not interested in pwm sensors, just pwm control | 
|  | 351 | if (interface == sensorInterface && | 
|  | 352 | objectPair.first.find("pwm") != std::string::npos) | 
|  | 353 | { | 
|  | 354 | continue; | 
|  | 355 | } | 
|  | 356 | sensors[objectPair.first] = interface; | 
|  | 357 | } | 
|  | 358 | } | 
|  | 359 | } | 
|  | 360 | } | 
|  | 361 | ManagedObjectType configurations; | 
| Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 362 | for (const auto& owner : owners) | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 363 | { | 
|  | 364 | // skip if no pid configuration (means probably a sensor) | 
|  | 365 | if (!owner.second.first) | 
|  | 366 | { | 
|  | 367 | continue; | 
|  | 368 | } | 
|  | 369 | auto endpoint = bus.new_method_call( | 
|  | 370 | owner.first.c_str(), owner.second.second.c_str(), | 
|  | 371 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); | 
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 372 | ManagedObjectType configuration; | 
|  | 373 | try | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 374 | { | 
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 375 | auto responce = bus.call(endpoint); | 
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 376 | responce.read(configuration); | 
|  | 377 | } | 
|  | 378 | catch (sdbusplus::exception_t&) | 
|  | 379 | { | 
|  | 380 | // this shouldn't happen, probably means daemon crashed | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 381 | throw std::runtime_error("Error getting managed objects from " + | 
|  | 382 | owner.first); | 
|  | 383 | } | 
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 384 |  | 
| Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 385 | for (auto& pathPair : configuration) | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 386 | { | 
|  | 387 | if (pathPair.second.find(pidConfigurationInterface) != | 
|  | 388 | pathPair.second.end() || | 
|  | 389 | pathPair.second.find(pidZoneConfigurationInterface) != | 
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 390 | pathPair.second.end() || | 
|  | 391 | pathPair.second.find(stepwiseConfigurationInterface) != | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 392 | pathPair.second.end()) | 
|  | 393 | { | 
|  | 394 | configurations.emplace(pathPair); | 
|  | 395 | } | 
| James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 396 | } | 
|  | 397 | } | 
|  | 398 |  | 
|  | 399 | // remove controllers from config that aren't in the current profile(s) | 
| James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 400 | std::vector<std::string> selectedProfiles = getSelectedProfiles(bus); | 
|  | 401 | if (selectedProfiles.size()) | 
| James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 402 | { | 
| James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 403 | for (auto pathIt = configurations.begin(); | 
|  | 404 | pathIt != configurations.end();) | 
| James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 405 | { | 
| James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 406 | for (auto confIt = pathIt->second.begin(); | 
|  | 407 | confIt != pathIt->second.end();) | 
| James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 408 | { | 
| James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 409 | auto profilesFind = confIt->second.find("Profiles"); | 
|  | 410 | if (profilesFind == confIt->second.end()) | 
| James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 411 | { | 
| James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 412 | confIt++; | 
|  | 413 | continue; // if no profiles selected, apply always | 
|  | 414 | } | 
|  | 415 | auto profiles = | 
|  | 416 | std::get<std::vector<std::string>>(profilesFind->second); | 
|  | 417 | if (profiles.empty()) | 
|  | 418 | { | 
|  | 419 | confIt++; | 
|  | 420 | continue; | 
|  | 421 | } | 
|  | 422 |  | 
|  | 423 | bool found = false; | 
|  | 424 | for (const std::string& profile : profiles) | 
|  | 425 | { | 
|  | 426 | if (std::find(selectedProfiles.begin(), | 
|  | 427 | selectedProfiles.end(), | 
|  | 428 | profile) != selectedProfiles.end()) | 
|  | 429 | { | 
|  | 430 | found = true; | 
|  | 431 | break; | 
|  | 432 | } | 
|  | 433 | } | 
|  | 434 | if (found) | 
|  | 435 | { | 
|  | 436 | confIt++; | 
| James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 437 | } | 
|  | 438 | else | 
|  | 439 | { | 
| James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 440 | confIt = pathIt->second.erase(confIt); | 
| James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 441 | } | 
|  | 442 | } | 
| James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 443 | if (pathIt->second.empty()) | 
| James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 444 | { | 
| James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 445 | pathIt = configurations.erase(pathIt); | 
| James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 446 | } | 
| James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 447 | else | 
| James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 448 | { | 
| James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 449 | pathIt++; | 
| James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 450 | } | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 451 | } | 
|  | 452 | } | 
| James Feist | 8c3c51e | 2018-08-08 16:31:43 -0700 | [diff] [blame] | 453 |  | 
|  | 454 | // on dbus having an index field is a bit strange, so randomly | 
|  | 455 | // assign index based on name property | 
| James Feist | ffd418b | 2018-11-15 14:46:36 -0800 | [diff] [blame] | 456 | std::vector<std::string> foundZones; | 
| Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 457 | for (const auto& configuration : configurations) | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 458 | { | 
|  | 459 | auto findZone = | 
|  | 460 | configuration.second.find(pidZoneConfigurationInterface); | 
|  | 461 | if (findZone != configuration.second.end()) | 
|  | 462 | { | 
| Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 463 | const auto& zone = findZone->second; | 
| James Feist | ffd418b | 2018-11-15 14:46:36 -0800 | [diff] [blame] | 464 |  | 
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 465 | const std::string& name = std::get<std::string>(zone.at("Name")); | 
| James Feist | ffd418b | 2018-11-15 14:46:36 -0800 | [diff] [blame] | 466 | size_t index = getZoneIndex(name, foundZones); | 
| James Feist | 8c3c51e | 2018-08-08 16:31:43 -0700 | [diff] [blame] | 467 |  | 
| Patrick Venture | c54fbd8 | 2018-10-30 19:40:05 -0700 | [diff] [blame] | 468 | auto& details = zoneDetailsConfig[index]; | 
| James Feist | 3484bed | 2019-02-25 13:28:18 -0800 | [diff] [blame] | 469 | details.minThermalOutput = std::visit(VariantToDoubleVisitor(), | 
|  | 470 | zone.at("MinThermalOutput")); | 
| Patrick Venture | 8e2fdb3 | 2019-02-11 09:39:59 -0800 | [diff] [blame] | 471 | details.failsafePercent = std::visit(VariantToDoubleVisitor(), | 
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 472 | zone.at("FailSafePercent")); | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 473 | } | 
|  | 474 | auto findBase = configuration.second.find(pidConfigurationInterface); | 
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 475 | if (findBase != configuration.second.end()) | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 476 | { | 
| James Feist | 8c3c51e | 2018-08-08 16:31:43 -0700 | [diff] [blame] | 477 |  | 
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 478 | const auto& base = | 
|  | 479 | configuration.second.at(pidConfigurationInterface); | 
|  | 480 | const std::vector<std::string>& zones = | 
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 481 | std::get<std::vector<std::string>>(base.at("Zones")); | 
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 482 | for (const std::string& zone : zones) | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 483 | { | 
| James Feist | ffd418b | 2018-11-15 14:46:36 -0800 | [diff] [blame] | 484 | size_t index = getZoneIndex(zone, foundZones); | 
| James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 485 | conf::PIDConf& conf = zoneConfig[index]; | 
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 486 |  | 
|  | 487 | std::vector<std::string> sensorNames = | 
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 488 | std::get<std::vector<std::string>>(base.at("Inputs")); | 
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 489 | auto findOutputs = | 
|  | 490 | base.find("Outputs"); // currently only fans have outputs | 
|  | 491 | if (findOutputs != base.end()) | 
|  | 492 | { | 
|  | 493 | std::vector<std::string> outputs = | 
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 494 | std::get<std::vector<std::string>>(findOutputs->second); | 
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 495 | sensorNames.insert(sensorNames.end(), outputs.begin(), | 
|  | 496 | outputs.end()); | 
|  | 497 | } | 
| James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 498 |  | 
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 499 | std::vector<std::string> inputs; | 
| James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 500 | std::vector<std::pair<std::string, std::string>> | 
|  | 501 | sensorInterfaces; | 
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 502 | for (const std::string& sensorName : sensorNames) | 
|  | 503 | { | 
|  | 504 | std::string name = sensorName; | 
|  | 505 | // replace spaces with underscores to be legal on dbus | 
|  | 506 | std::replace(name.begin(), name.end(), ' ', '_'); | 
| James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 507 | findSensors(sensors, name, sensorInterfaces); | 
|  | 508 | } | 
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 509 |  | 
| James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 510 | for (const auto& sensorPathIfacePair : sensorInterfaces) | 
|  | 511 | { | 
|  | 512 |  | 
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 513 | if (sensorPathIfacePair.second == sensorInterface) | 
|  | 514 | { | 
| James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 515 | size_t idx = | 
|  | 516 | sensorPathIfacePair.first.find_last_of("/") + 1; | 
|  | 517 | std::string shortName = | 
|  | 518 | sensorPathIfacePair.first.substr(idx); | 
|  | 519 |  | 
|  | 520 | inputs.push_back(shortName); | 
|  | 521 | auto& config = sensorConfig[shortName]; | 
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 522 | config.type = std::get<std::string>(base.at("Class")); | 
| Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 523 | config.readPath = sensorPathIfacePair.first; | 
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 524 | // todo: maybe un-hardcode this if we run into slower | 
|  | 525 | // timeouts with sensors | 
|  | 526 | if (config.type == "temp") | 
|  | 527 | { | 
| James Feist | 2642cb5 | 2019-02-25 13:00:16 -0800 | [diff] [blame] | 528 | config.timeout = 0; | 
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 529 | } | 
| James Feist | 75eb769 | 2019-02-25 12:50:02 -0800 | [diff] [blame] | 530 | else if (config.type == "fan") | 
|  | 531 | { | 
|  | 532 | config.max = conf::inheritValueFromDbus; | 
|  | 533 | config.min = conf::inheritValueFromDbus; | 
|  | 534 | } | 
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 535 | } | 
|  | 536 | else if (sensorPathIfacePair.second == pwmInterface) | 
|  | 537 | { | 
|  | 538 | // copy so we can modify it | 
|  | 539 | for (std::string otherSensor : sensorNames) | 
|  | 540 | { | 
| James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 541 | std::replace(otherSensor.begin(), otherSensor.end(), | 
|  | 542 | ' ', '_'); | 
|  | 543 | if (sensorPathIfacePair.first.find(otherSensor) != | 
|  | 544 | std::string::npos) | 
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 545 | { | 
|  | 546 | continue; | 
|  | 547 | } | 
| James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 548 |  | 
| Patrick Venture | c54fbd8 | 2018-10-30 19:40:05 -0700 | [diff] [blame] | 549 | auto& config = sensorConfig[otherSensor]; | 
| Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 550 | config.writePath = sensorPathIfacePair.first; | 
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 551 | // todo: un-hardcode this if there are fans with | 
|  | 552 | // different ranges | 
|  | 553 | config.max = 255; | 
|  | 554 | config.min = 0; | 
|  | 555 | } | 
|  | 556 | } | 
|  | 557 | } | 
| James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 558 |  | 
| James Feist | 11d243d | 2019-06-24 16:18:40 -0700 | [diff] [blame] | 559 | // if the sensors aren't available in the current state, don't | 
|  | 560 | // add them to the configuration. | 
|  | 561 | if (inputs.empty()) | 
|  | 562 | { | 
|  | 563 | continue; | 
|  | 564 | } | 
|  | 565 |  | 
| James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 566 | struct conf::ControllerInfo& info = | 
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 567 | conf[std::get<std::string>(base.at("Name"))]; | 
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 568 | info.inputs = std::move(inputs); | 
|  | 569 |  | 
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 570 | info.type = std::get<std::string>(base.at("Class")); | 
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 571 | // todo: auto generation yaml -> c script seems to discard this | 
|  | 572 | // value for fans, verify this is okay | 
|  | 573 | if (info.type == "fan") | 
|  | 574 | { | 
|  | 575 | info.setpoint = 0; | 
|  | 576 | } | 
|  | 577 | else | 
|  | 578 | { | 
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 579 | info.setpoint = std::visit(VariantToDoubleVisitor(), | 
|  | 580 | base.at("SetPoint")); | 
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 581 | } | 
|  | 582 | info.pidInfo.ts = 1.0; // currently unused | 
| Patrick Venture | 7442c37 | 2019-02-11 10:21:05 -0800 | [diff] [blame] | 583 | info.pidInfo.proportionalCoeff = std::visit( | 
|  | 584 | VariantToDoubleVisitor(), base.at("PCoefficient")); | 
|  | 585 | info.pidInfo.integralCoeff = std::visit( | 
|  | 586 | VariantToDoubleVisitor(), base.at("ICoefficient")); | 
|  | 587 | info.pidInfo.feedFwdOffset = std::visit( | 
|  | 588 | VariantToDoubleVisitor(), base.at("FFOffCoefficient")); | 
|  | 589 | info.pidInfo.feedFwdGain = std::visit( | 
|  | 590 | VariantToDoubleVisitor(), base.at("FFGainCoefficient")); | 
|  | 591 | info.pidInfo.integralLimit.max = | 
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 592 | std::visit(VariantToDoubleVisitor(), base.at("ILimitMax")); | 
| Patrick Venture | 7442c37 | 2019-02-11 10:21:05 -0800 | [diff] [blame] | 593 | info.pidInfo.integralLimit.min = | 
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 594 | std::visit(VariantToDoubleVisitor(), base.at("ILimitMin")); | 
| Patrick Venture | 7442c37 | 2019-02-11 10:21:05 -0800 | [diff] [blame] | 595 | info.pidInfo.outLim.max = std::visit(VariantToDoubleVisitor(), | 
|  | 596 | base.at("OutLimitMax")); | 
|  | 597 | info.pidInfo.outLim.min = std::visit(VariantToDoubleVisitor(), | 
|  | 598 | base.at("OutLimitMin")); | 
|  | 599 | info.pidInfo.slewNeg = | 
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 600 | std::visit(VariantToDoubleVisitor(), base.at("SlewNeg")); | 
| Patrick Venture | 7442c37 | 2019-02-11 10:21:05 -0800 | [diff] [blame] | 601 | info.pidInfo.slewPos = | 
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 602 | std::visit(VariantToDoubleVisitor(), base.at("SlewPos")); | 
| James Feist | 572c43d | 2019-01-31 15:52:22 -0800 | [diff] [blame] | 603 | double negativeHysteresis = 0; | 
|  | 604 | double positiveHysteresis = 0; | 
|  | 605 |  | 
|  | 606 | auto findNeg = base.find("NegativeHysteresis"); | 
|  | 607 | auto findPos = base.find("PositiveHysteresis"); | 
|  | 608 | if (findNeg != base.end()) | 
|  | 609 | { | 
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 610 | negativeHysteresis = | 
|  | 611 | std::visit(VariantToDoubleVisitor(), findNeg->second); | 
| James Feist | 572c43d | 2019-01-31 15:52:22 -0800 | [diff] [blame] | 612 | } | 
|  | 613 |  | 
|  | 614 | if (findPos != base.end()) | 
|  | 615 | { | 
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 616 | positiveHysteresis = | 
|  | 617 | std::visit(VariantToDoubleVisitor(), findPos->second); | 
| James Feist | 572c43d | 2019-01-31 15:52:22 -0800 | [diff] [blame] | 618 | } | 
|  | 619 | info.pidInfo.negativeHysteresis = negativeHysteresis; | 
|  | 620 | info.pidInfo.positiveHysteresis = positiveHysteresis; | 
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 621 | } | 
|  | 622 | } | 
|  | 623 | auto findStepwise = | 
|  | 624 | configuration.second.find(stepwiseConfigurationInterface); | 
|  | 625 | if (findStepwise != configuration.second.end()) | 
|  | 626 | { | 
|  | 627 | const auto& base = findStepwise->second; | 
|  | 628 | const std::vector<std::string>& zones = | 
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 629 | std::get<std::vector<std::string>>(base.at("Zones")); | 
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 630 | for (const std::string& zone : zones) | 
|  | 631 | { | 
| James Feist | ffd418b | 2018-11-15 14:46:36 -0800 | [diff] [blame] | 632 | size_t index = getZoneIndex(zone, foundZones); | 
| James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 633 | conf::PIDConf& conf = zoneConfig[index]; | 
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 634 |  | 
|  | 635 | std::vector<std::string> inputs; | 
|  | 636 | std::vector<std::string> sensorNames = | 
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 637 | std::get<std::vector<std::string>>(base.at("Inputs")); | 
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 638 |  | 
| James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 639 | bool sensorFound = false; | 
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 640 | for (const std::string& sensorName : sensorNames) | 
|  | 641 | { | 
|  | 642 | std::string name = sensorName; | 
|  | 643 | // replace spaces with underscores to be legal on dbus | 
|  | 644 | std::replace(name.begin(), name.end(), ' ', '_'); | 
| James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 645 | std::vector<std::pair<std::string, std::string>> | 
|  | 646 | sensorPathIfacePairs; | 
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 647 |  | 
| James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 648 | if (!findSensors(sensors, name, sensorPathIfacePairs)) | 
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 649 | { | 
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 650 | break; | 
|  | 651 | } | 
|  | 652 |  | 
| James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 653 | for (const auto& sensorPathIfacePair : sensorPathIfacePairs) | 
|  | 654 | { | 
|  | 655 | size_t idx = | 
|  | 656 | sensorPathIfacePair.first.find_last_of("/") + 1; | 
|  | 657 | std::string shortName = | 
|  | 658 | sensorPathIfacePair.first.substr(idx); | 
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 659 |  | 
| James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 660 | inputs.push_back(shortName); | 
|  | 661 | auto& config = sensorConfig[shortName]; | 
| Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 662 | config.readPath = sensorPathIfacePair.first; | 
| James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 663 | config.type = "temp"; | 
|  | 664 | // todo: maybe un-hardcode this if we run into slower | 
|  | 665 | // timeouts with sensors | 
|  | 666 |  | 
| James Feist | 2642cb5 | 2019-02-25 13:00:16 -0800 | [diff] [blame] | 667 | config.timeout = 0; | 
| James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 668 | sensorFound = true; | 
|  | 669 | } | 
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 670 | } | 
|  | 671 | if (!sensorFound) | 
|  | 672 | { | 
|  | 673 | continue; | 
|  | 674 | } | 
| James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 675 | struct conf::ControllerInfo& info = | 
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 676 | conf[std::get<std::string>(base.at("Name"))]; | 
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 677 | info.inputs = std::move(inputs); | 
|  | 678 |  | 
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 679 | info.type = "stepwise"; | 
|  | 680 | info.stepwiseInfo.ts = 1.0; // currently unused | 
| James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 681 | info.stepwiseInfo.positiveHysteresis = 0.0; | 
|  | 682 | info.stepwiseInfo.negativeHysteresis = 0.0; | 
| James Feist | 608304d | 2019-02-25 10:01:42 -0800 | [diff] [blame] | 683 | std::string subtype = std::get<std::string>(base.at("Class")); | 
|  | 684 |  | 
|  | 685 | info.stepwiseInfo.isCeiling = (subtype == "Ceiling"); | 
| James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 686 | auto findPosHyst = base.find("PositiveHysteresis"); | 
|  | 687 | auto findNegHyst = base.find("NegativeHysteresis"); | 
|  | 688 | if (findPosHyst != base.end()) | 
|  | 689 | { | 
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 690 | info.stepwiseInfo.positiveHysteresis = std::visit( | 
| James Feist | 208abce | 2018-12-06 09:59:10 -0800 | [diff] [blame] | 691 | VariantToDoubleVisitor(), findPosHyst->second); | 
| James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 692 | } | 
|  | 693 | if (findNegHyst != base.end()) | 
|  | 694 | { | 
| James Feist | 5782ab8 | 2019-04-02 08:38:48 -0700 | [diff] [blame] | 695 | info.stepwiseInfo.negativeHysteresis = std::visit( | 
| James Feist | 208abce | 2018-12-06 09:59:10 -0800 | [diff] [blame] | 696 | VariantToDoubleVisitor(), findNegHyst->second); | 
| James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 697 | } | 
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 698 | std::vector<double> readings = | 
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 699 | std::get<std::vector<double>>(base.at("Reading")); | 
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 700 | if (readings.size() > ec::maxStepwisePoints) | 
|  | 701 | { | 
|  | 702 | throw std::invalid_argument("Too many stepwise points."); | 
|  | 703 | } | 
|  | 704 | if (readings.empty()) | 
|  | 705 | { | 
|  | 706 | throw std::invalid_argument( | 
|  | 707 | "Must have one stepwise point."); | 
|  | 708 | } | 
|  | 709 | std::copy(readings.begin(), readings.end(), | 
|  | 710 | info.stepwiseInfo.reading); | 
|  | 711 | if (readings.size() < ec::maxStepwisePoints) | 
|  | 712 | { | 
|  | 713 | info.stepwiseInfo.reading[readings.size()] = | 
| Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 714 | std::numeric_limits<double>::quiet_NaN(); | 
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 715 | } | 
|  | 716 | std::vector<double> outputs = | 
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 717 | std::get<std::vector<double>>(base.at("Output")); | 
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 718 | if (readings.size() != outputs.size()) | 
|  | 719 | { | 
|  | 720 | throw std::invalid_argument( | 
|  | 721 | "Outputs size must match readings"); | 
|  | 722 | } | 
|  | 723 | std::copy(outputs.begin(), outputs.end(), | 
|  | 724 | info.stepwiseInfo.output); | 
|  | 725 | if (outputs.size() < ec::maxStepwisePoints) | 
|  | 726 | { | 
|  | 727 | info.stepwiseInfo.output[outputs.size()] = | 
| Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 728 | std::numeric_limits<double>::quiet_NaN(); | 
| James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 729 | } | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 730 | } | 
|  | 731 | } | 
|  | 732 | } | 
| James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 733 | if constexpr (DEBUG) | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 734 | { | 
|  | 735 | debugPrint(); | 
|  | 736 | } | 
| James Feist | c959c42 | 2018-11-01 12:33:40 -0700 | [diff] [blame] | 737 | if (zoneConfig.empty() || zoneDetailsConfig.empty()) | 
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 738 | { | 
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 739 | std::cerr | 
|  | 740 | << "No fan zones, application pausing until new configuration\n"; | 
|  | 741 | return false; | 
| James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 742 | } | 
| James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 743 | return true; | 
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 744 | } | 
|  | 745 | } // namespace dbus_configuration |