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" |
| 18 | #include "dbus/util.hpp" |
| 19 | |
Patrick Venture | 107a25d | 2018-10-13 14:08:09 -0700 | [diff] [blame] | 20 | #include <algorithm> |
James Feist | 64f072a | 2018-08-10 16:39:24 -0700 | [diff] [blame] | 21 | #include <chrono> |
James Feist | 64f072a | 2018-08-10 16:39:24 -0700 | [diff] [blame] | 22 | #include <functional> |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 23 | #include <iostream> |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 24 | #include <regex> |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 25 | #include <sdbusplus/bus.hpp> |
James Feist | 64f072a | 2018-08-10 16:39:24 -0700 | [diff] [blame] | 26 | #include <sdbusplus/bus/match.hpp> |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 27 | #include <sdbusplus/exception.hpp> |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 28 | #include <set> |
James Feist | 64f072a | 2018-08-10 16:39:24 -0700 | [diff] [blame] | 29 | #include <thread> |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 30 | #include <unordered_map> |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 31 | #include <variant> |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 32 | |
| 33 | static constexpr bool DEBUG = false; // enable to print found configuration |
| 34 | |
Patrick Venture | c54fbd8 | 2018-10-30 19:40:05 -0700 | [diff] [blame] | 35 | std::map<std::string, struct SensorConfig> sensorConfig = {}; |
| 36 | std::map<int64_t, PIDConf> zoneConfig = {}; |
| 37 | std::map<int64_t, struct ZoneConfig> zoneDetailsConfig = {}; |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 38 | |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 39 | constexpr const char* pidConfigurationInterface = |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 40 | "xyz.openbmc_project.Configuration.Pid"; |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 41 | constexpr const char* objectManagerInterface = |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 42 | "org.freedesktop.DBus.ObjectManager"; |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 43 | constexpr const char* pidZoneConfigurationInterface = |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 44 | "xyz.openbmc_project.Configuration.Pid.Zone"; |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 45 | constexpr const char* stepwiseConfigurationInterface = |
| 46 | "xyz.openbmc_project.Configuration.Stepwise"; |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 47 | constexpr const char* sensorInterface = "xyz.openbmc_project.Sensor.Value"; |
| 48 | constexpr const char* pwmInterface = "xyz.openbmc_project.Control.FanPwm"; |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 49 | |
| 50 | namespace dbus_configuration |
| 51 | { |
| 52 | |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 53 | bool findSensors(const std::unordered_map<std::string, std::string>& sensors, |
| 54 | const std::string& search, |
| 55 | std::vector<std::pair<std::string, std::string>>& matches) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 56 | { |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 57 | std::smatch match; |
| 58 | std::regex reg(search); |
| 59 | for (const auto& sensor : sensors) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 60 | { |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 61 | if (std::regex_search(sensor.first, match, reg)) |
| 62 | { |
| 63 | matches.push_back(sensor); |
| 64 | } |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 65 | } |
Patrick Venture | 107a25d | 2018-10-13 14:08:09 -0700 | [diff] [blame] | 66 | |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 67 | return matches.size() > 0; |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | // this function prints the configuration into a form similar to the cpp |
| 71 | // generated code to help in verification, should be turned off during normal |
| 72 | // use |
| 73 | void debugPrint(void) |
| 74 | { |
| 75 | // print sensor config |
| 76 | std::cout << "sensor config:\n"; |
| 77 | std::cout << "{\n"; |
Patrick Venture | c54fbd8 | 2018-10-30 19:40:05 -0700 | [diff] [blame] | 78 | for (const auto& pair : sensorConfig) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 79 | { |
| 80 | |
| 81 | std::cout << "\t{" << pair.first << ",\n\t\t{"; |
| 82 | std::cout << pair.second.type << ", "; |
| 83 | std::cout << pair.second.readpath << ", "; |
| 84 | std::cout << pair.second.writepath << ", "; |
| 85 | std::cout << pair.second.min << ", "; |
| 86 | std::cout << pair.second.max << ", "; |
| 87 | std::cout << pair.second.timeout << "},\n\t},\n"; |
| 88 | } |
| 89 | std::cout << "}\n\n"; |
| 90 | std::cout << "ZoneDetailsConfig\n"; |
| 91 | std::cout << "{\n"; |
Patrick Venture | c54fbd8 | 2018-10-30 19:40:05 -0700 | [diff] [blame] | 92 | for (const auto& zone : zoneDetailsConfig) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 93 | { |
| 94 | std::cout << "\t{" << zone.first << ",\n"; |
Patrick Venture | 8e2fdb3 | 2019-02-11 09:39:59 -0800 | [diff] [blame^] | 95 | std::cout << "\t\t{" << zone.second.minThermalRpm << ", "; |
| 96 | std::cout << zone.second.failsafePercent << "}\n\t},\n"; |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 97 | } |
| 98 | std::cout << "}\n\n"; |
| 99 | std::cout << "ZoneConfig\n"; |
| 100 | std::cout << "{\n"; |
Patrick Venture | c54fbd8 | 2018-10-30 19:40:05 -0700 | [diff] [blame] | 101 | for (const auto& zone : zoneConfig) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 102 | { |
| 103 | std::cout << "\t{" << zone.first << "\n"; |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 104 | for (const auto& pidconf : zone.second) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 105 | { |
| 106 | std::cout << "\t\t{" << pidconf.first << ",\n"; |
| 107 | std::cout << "\t\t\t{" << pidconf.second.type << ",\n"; |
| 108 | std::cout << "\t\t\t{"; |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 109 | for (const auto& input : pidconf.second.inputs) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 110 | { |
| 111 | std::cout << "\n\t\t\t" << input << ",\n"; |
| 112 | } |
| 113 | std::cout << "\t\t\t}\n"; |
| 114 | std::cout << "\t\t\t" << pidconf.second.setpoint << ",\n"; |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 115 | std::cout << "\t\t\t{" << pidconf.second.pidInfo.ts << ",\n"; |
| 116 | std::cout << "\t\t\t" << pidconf.second.pidInfo.p_c << ",\n"; |
| 117 | std::cout << "\t\t\t" << pidconf.second.pidInfo.i_c << ",\n"; |
| 118 | std::cout << "\t\t\t" << pidconf.second.pidInfo.ff_off << ",\n"; |
| 119 | std::cout << "\t\t\t" << pidconf.second.pidInfo.ff_gain << ",\n"; |
| 120 | std::cout << "\t\t\t{" << pidconf.second.pidInfo.i_lim.min << "," |
| 121 | << pidconf.second.pidInfo.i_lim.max << "},\n"; |
| 122 | std::cout << "\t\t\t{" << pidconf.second.pidInfo.out_lim.min << "," |
| 123 | << pidconf.second.pidInfo.out_lim.max << "},\n"; |
| 124 | std::cout << "\t\t\t" << pidconf.second.pidInfo.slew_neg << ",\n"; |
| 125 | std::cout << "\t\t\t" << pidconf.second.pidInfo.slew_pos << ",\n"; |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 126 | std::cout << "\t\t\t}\n\t\t}\n"; |
| 127 | } |
| 128 | std::cout << "\t},\n"; |
| 129 | } |
| 130 | std::cout << "}\n\n"; |
| 131 | } |
| 132 | |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 133 | int eventHandler(sd_bus_message*, void*, sd_bus_error*) |
| 134 | { |
| 135 | // do a brief sleep as we tend to get a bunch of these events at |
| 136 | // once |
| 137 | std::this_thread::sleep_for(std::chrono::seconds(5)); |
| 138 | std::cout << "New configuration detected, restarting\n."; |
| 139 | std::exit(EXIT_SUCCESS); // service file should make us restart |
| 140 | return 1; |
| 141 | } |
| 142 | |
James Feist | ffd418b | 2018-11-15 14:46:36 -0800 | [diff] [blame] | 143 | size_t getZoneIndex(const std::string& name, std::vector<std::string>& zones) |
| 144 | { |
| 145 | auto it = std::find(zones.begin(), zones.end(), name); |
| 146 | if (it == zones.end()) |
| 147 | { |
| 148 | zones.emplace_back(name); |
| 149 | it = zones.end() - 1; |
| 150 | } |
| 151 | |
| 152 | return it - zones.begin(); |
| 153 | } |
| 154 | |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 155 | void init(sdbusplus::bus::bus& bus) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 156 | { |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 157 | using DbusVariantType = |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 158 | std::variant<uint64_t, int64_t, double, std::string, |
| 159 | std::vector<std::string>, std::vector<double>>; |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 160 | |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 161 | using ManagedObjectType = std::unordered_map< |
| 162 | sdbusplus::message::object_path, |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 163 | std::unordered_map<std::string, |
| 164 | std::unordered_map<std::string, DbusVariantType>>>; |
James Feist | 64f072a | 2018-08-10 16:39:24 -0700 | [diff] [blame] | 165 | |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 166 | // restart on configuration properties changed |
| 167 | static sdbusplus::bus::match::match configMatch( |
James Feist | 64f072a | 2018-08-10 16:39:24 -0700 | [diff] [blame] | 168 | bus, |
| 169 | "type='signal',member='PropertiesChanged',arg0namespace='" + |
| 170 | std::string(pidConfigurationInterface) + "'", |
| 171 | eventHandler); |
| 172 | |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 173 | // restart on sensors changed |
| 174 | static sdbusplus::bus::match::match sensorAdded( |
| 175 | bus, |
| 176 | "type='signal',member='InterfacesAdded',arg0path='/xyz/openbmc_project/" |
| 177 | "sensors/'", |
| 178 | eventHandler); |
| 179 | |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 180 | auto mapper = |
| 181 | bus.new_method_call("xyz.openbmc_project.ObjectMapper", |
| 182 | "/xyz/openbmc_project/object_mapper", |
| 183 | "xyz.openbmc_project.ObjectMapper", "GetSubTree"); |
James Feist | 26e8c6a | 2018-10-25 10:38:26 -0700 | [diff] [blame] | 184 | mapper.append("/", 0, |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 185 | std::array<const char*, 6>{objectManagerInterface, |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 186 | pidConfigurationInterface, |
| 187 | pidZoneConfigurationInterface, |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 188 | stepwiseConfigurationInterface, |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 189 | sensorInterface, pwmInterface}); |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 190 | std::unordered_map< |
| 191 | std::string, std::unordered_map<std::string, std::vector<std::string>>> |
| 192 | respData; |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 193 | try |
| 194 | { |
| 195 | auto resp = bus.call(mapper); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 196 | resp.read(respData); |
| 197 | } |
| 198 | catch (sdbusplus::exception_t&) |
| 199 | { |
| 200 | // can't do anything without mapper call data |
| 201 | throw std::runtime_error("ObjectMapper Call Failure"); |
| 202 | } |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 203 | |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 204 | if (respData.empty()) |
| 205 | { |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 206 | // can't do anything without mapper call data |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 207 | throw std::runtime_error("No configuration data available from Mapper"); |
| 208 | } |
| 209 | // create a map of pair of <has pid configuration, ObjectManager path> |
| 210 | std::unordered_map<std::string, std::pair<bool, std::string>> owners; |
| 211 | // and a map of <path, interface> for sensors |
| 212 | std::unordered_map<std::string, std::string> sensors; |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 213 | for (const auto& objectPair : respData) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 214 | { |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 215 | for (const auto& ownerPair : objectPair.second) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 216 | { |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 217 | auto& owner = owners[ownerPair.first]; |
| 218 | for (const std::string& interface : ownerPair.second) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 219 | { |
| 220 | |
| 221 | if (interface == objectManagerInterface) |
| 222 | { |
| 223 | owner.second = objectPair.first; |
| 224 | } |
| 225 | if (interface == pidConfigurationInterface || |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 226 | interface == pidZoneConfigurationInterface || |
| 227 | interface == stepwiseConfigurationInterface) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 228 | { |
| 229 | owner.first = true; |
| 230 | } |
| 231 | if (interface == sensorInterface || interface == pwmInterface) |
| 232 | { |
| 233 | // we're not interested in pwm sensors, just pwm control |
| 234 | if (interface == sensorInterface && |
| 235 | objectPair.first.find("pwm") != std::string::npos) |
| 236 | { |
| 237 | continue; |
| 238 | } |
| 239 | sensors[objectPair.first] = interface; |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | ManagedObjectType configurations; |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 245 | for (const auto& owner : owners) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 246 | { |
| 247 | // skip if no pid configuration (means probably a sensor) |
| 248 | if (!owner.second.first) |
| 249 | { |
| 250 | continue; |
| 251 | } |
| 252 | auto endpoint = bus.new_method_call( |
| 253 | owner.first.c_str(), owner.second.second.c_str(), |
| 254 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 255 | ManagedObjectType configuration; |
| 256 | try |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 257 | { |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 258 | auto responce = bus.call(endpoint); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 259 | responce.read(configuration); |
| 260 | } |
| 261 | catch (sdbusplus::exception_t&) |
| 262 | { |
| 263 | // this shouldn't happen, probably means daemon crashed |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 264 | throw std::runtime_error("Error getting managed objects from " + |
| 265 | owner.first); |
| 266 | } |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 267 | |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 268 | for (auto& pathPair : configuration) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 269 | { |
| 270 | if (pathPair.second.find(pidConfigurationInterface) != |
| 271 | pathPair.second.end() || |
| 272 | pathPair.second.find(pidZoneConfigurationInterface) != |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 273 | pathPair.second.end() || |
| 274 | pathPair.second.find(stepwiseConfigurationInterface) != |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 275 | pathPair.second.end()) |
| 276 | { |
| 277 | configurations.emplace(pathPair); |
| 278 | } |
| 279 | } |
| 280 | } |
James Feist | 8c3c51e | 2018-08-08 16:31:43 -0700 | [diff] [blame] | 281 | |
| 282 | // on dbus having an index field is a bit strange, so randomly |
| 283 | // assign index based on name property |
James Feist | ffd418b | 2018-11-15 14:46:36 -0800 | [diff] [blame] | 284 | std::vector<std::string> foundZones; |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 285 | for (const auto& configuration : configurations) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 286 | { |
| 287 | auto findZone = |
| 288 | configuration.second.find(pidZoneConfigurationInterface); |
| 289 | if (findZone != configuration.second.end()) |
| 290 | { |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 291 | const auto& zone = findZone->second; |
James Feist | ffd418b | 2018-11-15 14:46:36 -0800 | [diff] [blame] | 292 | |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 293 | const std::string& name = std::get<std::string>(zone.at("Name")); |
James Feist | ffd418b | 2018-11-15 14:46:36 -0800 | [diff] [blame] | 294 | size_t index = getZoneIndex(name, foundZones); |
James Feist | 8c3c51e | 2018-08-08 16:31:43 -0700 | [diff] [blame] | 295 | |
Patrick Venture | c54fbd8 | 2018-10-30 19:40:05 -0700 | [diff] [blame] | 296 | auto& details = zoneDetailsConfig[index]; |
Patrick Venture | 8e2fdb3 | 2019-02-11 09:39:59 -0800 | [diff] [blame^] | 297 | details.minThermalRpm = |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 298 | std::visit(VariantToDoubleVisitor(), zone.at("MinThermalRpm")); |
Patrick Venture | 8e2fdb3 | 2019-02-11 09:39:59 -0800 | [diff] [blame^] | 299 | details.failsafePercent = std::visit(VariantToDoubleVisitor(), |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 300 | zone.at("FailSafePercent")); |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 301 | } |
| 302 | auto findBase = configuration.second.find(pidConfigurationInterface); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 303 | if (findBase != configuration.second.end()) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 304 | { |
James Feist | 8c3c51e | 2018-08-08 16:31:43 -0700 | [diff] [blame] | 305 | |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 306 | const auto& base = |
| 307 | configuration.second.at(pidConfigurationInterface); |
| 308 | const std::vector<std::string>& zones = |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 309 | std::get<std::vector<std::string>>(base.at("Zones")); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 310 | for (const std::string& zone : zones) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 311 | { |
James Feist | ffd418b | 2018-11-15 14:46:36 -0800 | [diff] [blame] | 312 | size_t index = getZoneIndex(zone, foundZones); |
Patrick Venture | c54fbd8 | 2018-10-30 19:40:05 -0700 | [diff] [blame] | 313 | PIDConf& conf = zoneConfig[index]; |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 314 | |
| 315 | std::vector<std::string> sensorNames = |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 316 | std::get<std::vector<std::string>>(base.at("Inputs")); |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 317 | auto findOutputs = |
| 318 | base.find("Outputs"); // currently only fans have outputs |
| 319 | if (findOutputs != base.end()) |
| 320 | { |
| 321 | std::vector<std::string> outputs = |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 322 | std::get<std::vector<std::string>>(findOutputs->second); |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 323 | sensorNames.insert(sensorNames.end(), outputs.begin(), |
| 324 | outputs.end()); |
| 325 | } |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 326 | |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 327 | std::vector<std::string> inputs; |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 328 | std::vector<std::pair<std::string, std::string>> |
| 329 | sensorInterfaces; |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 330 | for (const std::string& sensorName : sensorNames) |
| 331 | { |
| 332 | std::string name = sensorName; |
| 333 | // replace spaces with underscores to be legal on dbus |
| 334 | std::replace(name.begin(), name.end(), ' ', '_'); |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 335 | findSensors(sensors, name, sensorInterfaces); |
| 336 | } |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 337 | |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 338 | // if the sensors aren't available in the current state, don't |
| 339 | // add them to the configuration. |
| 340 | if (sensorInterfaces.empty()) |
| 341 | { |
| 342 | continue; |
| 343 | } |
| 344 | for (const auto& sensorPathIfacePair : sensorInterfaces) |
| 345 | { |
| 346 | |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 347 | if (sensorPathIfacePair.second == sensorInterface) |
| 348 | { |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 349 | size_t idx = |
| 350 | sensorPathIfacePair.first.find_last_of("/") + 1; |
| 351 | std::string shortName = |
| 352 | sensorPathIfacePair.first.substr(idx); |
| 353 | |
| 354 | inputs.push_back(shortName); |
| 355 | auto& config = sensorConfig[shortName]; |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 356 | config.type = std::get<std::string>(base.at("Class")); |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 357 | config.readpath = sensorPathIfacePair.first; |
| 358 | // todo: maybe un-hardcode this if we run into slower |
| 359 | // timeouts with sensors |
| 360 | if (config.type == "temp") |
| 361 | { |
| 362 | config.timeout = 500; |
| 363 | } |
| 364 | } |
| 365 | else if (sensorPathIfacePair.second == pwmInterface) |
| 366 | { |
| 367 | // copy so we can modify it |
| 368 | for (std::string otherSensor : sensorNames) |
| 369 | { |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 370 | std::replace(otherSensor.begin(), otherSensor.end(), |
| 371 | ' ', '_'); |
| 372 | if (sensorPathIfacePair.first.find(otherSensor) != |
| 373 | std::string::npos) |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 374 | { |
| 375 | continue; |
| 376 | } |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 377 | |
Patrick Venture | c54fbd8 | 2018-10-30 19:40:05 -0700 | [diff] [blame] | 378 | auto& config = sensorConfig[otherSensor]; |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 379 | config.writepath = sensorPathIfacePair.first; |
| 380 | // todo: un-hardcode this if there are fans with |
| 381 | // different ranges |
| 382 | config.max = 255; |
| 383 | config.min = 0; |
| 384 | } |
| 385 | } |
| 386 | } |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 387 | |
Patrick Venture | f325231 | 2018-10-30 08:42:53 -0700 | [diff] [blame] | 388 | struct ControllerInfo& info = |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 389 | conf[std::get<std::string>(base.at("Name"))]; |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 390 | info.inputs = std::move(inputs); |
| 391 | |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 392 | info.type = std::get<std::string>(base.at("Class")); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 393 | // todo: auto generation yaml -> c script seems to discard this |
| 394 | // value for fans, verify this is okay |
| 395 | if (info.type == "fan") |
| 396 | { |
| 397 | info.setpoint = 0; |
| 398 | } |
| 399 | else |
| 400 | { |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 401 | info.setpoint = std::visit(VariantToDoubleVisitor(), |
| 402 | base.at("SetPoint")); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 403 | } |
| 404 | info.pidInfo.ts = 1.0; // currently unused |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 405 | info.pidInfo.p_c = std::visit(VariantToDoubleVisitor(), |
| 406 | base.at("PCoefficient")); |
| 407 | info.pidInfo.i_c = std::visit(VariantToDoubleVisitor(), |
| 408 | base.at("ICoefficient")); |
| 409 | info.pidInfo.ff_off = std::visit(VariantToDoubleVisitor(), |
| 410 | base.at("FFOffCoefficient")); |
| 411 | info.pidInfo.ff_gain = std::visit(VariantToDoubleVisitor(), |
| 412 | base.at("FFGainCoefficient")); |
| 413 | info.pidInfo.i_lim.max = |
| 414 | std::visit(VariantToDoubleVisitor(), base.at("ILimitMax")); |
| 415 | info.pidInfo.i_lim.min = |
| 416 | std::visit(VariantToDoubleVisitor(), base.at("ILimitMin")); |
| 417 | info.pidInfo.out_lim.max = std::visit(VariantToDoubleVisitor(), |
| 418 | base.at("OutLimitMax")); |
| 419 | info.pidInfo.out_lim.min = std::visit(VariantToDoubleVisitor(), |
| 420 | base.at("OutLimitMin")); |
| 421 | info.pidInfo.slew_neg = |
| 422 | std::visit(VariantToDoubleVisitor(), base.at("SlewNeg")); |
| 423 | info.pidInfo.slew_pos = |
| 424 | std::visit(VariantToDoubleVisitor(), base.at("SlewPos")); |
James Feist | 572c43d | 2019-01-31 15:52:22 -0800 | [diff] [blame] | 425 | double negativeHysteresis = 0; |
| 426 | double positiveHysteresis = 0; |
| 427 | |
| 428 | auto findNeg = base.find("NegativeHysteresis"); |
| 429 | auto findPos = base.find("PositiveHysteresis"); |
| 430 | if (findNeg != base.end()) |
| 431 | { |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 432 | negativeHysteresis = |
| 433 | std::visit(VariantToDoubleVisitor(), findNeg->second); |
James Feist | 572c43d | 2019-01-31 15:52:22 -0800 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | if (findPos != base.end()) |
| 437 | { |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 438 | positiveHysteresis = |
| 439 | std::visit(VariantToDoubleVisitor(), findPos->second); |
James Feist | 572c43d | 2019-01-31 15:52:22 -0800 | [diff] [blame] | 440 | } |
| 441 | info.pidInfo.negativeHysteresis = negativeHysteresis; |
| 442 | info.pidInfo.positiveHysteresis = positiveHysteresis; |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | auto findStepwise = |
| 446 | configuration.second.find(stepwiseConfigurationInterface); |
| 447 | if (findStepwise != configuration.second.end()) |
| 448 | { |
| 449 | const auto& base = findStepwise->second; |
| 450 | const std::vector<std::string>& zones = |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 451 | std::get<std::vector<std::string>>(base.at("Zones")); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 452 | for (const std::string& zone : zones) |
| 453 | { |
James Feist | ffd418b | 2018-11-15 14:46:36 -0800 | [diff] [blame] | 454 | size_t index = getZoneIndex(zone, foundZones); |
Patrick Venture | c54fbd8 | 2018-10-30 19:40:05 -0700 | [diff] [blame] | 455 | PIDConf& conf = zoneConfig[index]; |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 456 | |
| 457 | std::vector<std::string> inputs; |
| 458 | std::vector<std::string> sensorNames = |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 459 | std::get<std::vector<std::string>>(base.at("Inputs")); |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 460 | |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 461 | bool sensorFound = false; |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 462 | for (const std::string& sensorName : sensorNames) |
| 463 | { |
| 464 | std::string name = sensorName; |
| 465 | // replace spaces with underscores to be legal on dbus |
| 466 | std::replace(name.begin(), name.end(), ' ', '_'); |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 467 | std::vector<std::pair<std::string, std::string>> |
| 468 | sensorPathIfacePairs; |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 469 | |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 470 | if (!findSensors(sensors, name, sensorPathIfacePairs)) |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 471 | { |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 472 | break; |
| 473 | } |
| 474 | |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 475 | for (const auto& sensorPathIfacePair : sensorPathIfacePairs) |
| 476 | { |
| 477 | size_t idx = |
| 478 | sensorPathIfacePair.first.find_last_of("/") + 1; |
| 479 | std::string shortName = |
| 480 | sensorPathIfacePair.first.substr(idx); |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 481 | |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 482 | inputs.push_back(shortName); |
| 483 | auto& config = sensorConfig[shortName]; |
| 484 | config.readpath = sensorPathIfacePair.first; |
| 485 | config.type = "temp"; |
| 486 | // todo: maybe un-hardcode this if we run into slower |
| 487 | // timeouts with sensors |
| 488 | |
| 489 | config.timeout = 500; |
| 490 | sensorFound = true; |
| 491 | } |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 492 | } |
| 493 | if (!sensorFound) |
| 494 | { |
| 495 | continue; |
| 496 | } |
Patrick Venture | f325231 | 2018-10-30 08:42:53 -0700 | [diff] [blame] | 497 | struct ControllerInfo& info = |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 498 | conf[std::get<std::string>(base.at("Name"))]; |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 499 | info.inputs = std::move(inputs); |
| 500 | |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 501 | info.type = "stepwise"; |
| 502 | info.stepwiseInfo.ts = 1.0; // currently unused |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 503 | info.stepwiseInfo.positiveHysteresis = 0.0; |
| 504 | info.stepwiseInfo.negativeHysteresis = 0.0; |
| 505 | auto findPosHyst = base.find("PositiveHysteresis"); |
| 506 | auto findNegHyst = base.find("NegativeHysteresis"); |
| 507 | if (findPosHyst != base.end()) |
| 508 | { |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 509 | info.stepwiseInfo.positiveHysteresis = std::visit( |
James Feist | 208abce | 2018-12-06 09:59:10 -0800 | [diff] [blame] | 510 | VariantToDoubleVisitor(), findPosHyst->second); |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 511 | } |
| 512 | if (findNegHyst != base.end()) |
| 513 | { |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 514 | info.stepwiseInfo.positiveHysteresis = std::visit( |
James Feist | 208abce | 2018-12-06 09:59:10 -0800 | [diff] [blame] | 515 | VariantToDoubleVisitor(), findNegHyst->second); |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 516 | } |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 517 | std::vector<double> readings = |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 518 | std::get<std::vector<double>>(base.at("Reading")); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 519 | if (readings.size() > ec::maxStepwisePoints) |
| 520 | { |
| 521 | throw std::invalid_argument("Too many stepwise points."); |
| 522 | } |
| 523 | if (readings.empty()) |
| 524 | { |
| 525 | throw std::invalid_argument( |
| 526 | "Must have one stepwise point."); |
| 527 | } |
| 528 | std::copy(readings.begin(), readings.end(), |
| 529 | info.stepwiseInfo.reading); |
| 530 | if (readings.size() < ec::maxStepwisePoints) |
| 531 | { |
| 532 | info.stepwiseInfo.reading[readings.size()] = |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 533 | std::numeric_limits<double>::quiet_NaN(); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 534 | } |
| 535 | std::vector<double> outputs = |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 536 | std::get<std::vector<double>>(base.at("Output")); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 537 | if (readings.size() != outputs.size()) |
| 538 | { |
| 539 | throw std::invalid_argument( |
| 540 | "Outputs size must match readings"); |
| 541 | } |
| 542 | std::copy(outputs.begin(), outputs.end(), |
| 543 | info.stepwiseInfo.output); |
| 544 | if (outputs.size() < ec::maxStepwisePoints) |
| 545 | { |
| 546 | info.stepwiseInfo.output[outputs.size()] = |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 547 | std::numeric_limits<double>::quiet_NaN(); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 548 | } |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 549 | } |
| 550 | } |
| 551 | } |
| 552 | if (DEBUG) |
| 553 | { |
| 554 | debugPrint(); |
| 555 | } |
James Feist | c959c42 | 2018-11-01 12:33:40 -0700 | [diff] [blame] | 556 | if (zoneConfig.empty() || zoneDetailsConfig.empty()) |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 557 | { |
| 558 | std::cerr << "No fan zones, application pausing until reboot\n"; |
| 559 | while (1) |
| 560 | { |
| 561 | bus.process_discard(); |
James Feist | 65ea92e | 2018-10-26 16:21:37 -0700 | [diff] [blame] | 562 | bus.wait(); |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 563 | } |
| 564 | } |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 565 | } |
| 566 | } // namespace dbus_configuration |