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