Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2017 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <iostream> |
Patrick Venture | fe75b19 | 2018-06-08 11:19:43 -0700 | [diff] [blame] | 18 | #include <map> |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 19 | #include <memory> |
Patrick Venture | fe75b19 | 2018-06-08 11:19:43 -0700 | [diff] [blame] | 20 | #include <string> |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 21 | |
| 22 | /* Configuration. */ |
| 23 | #include "conf.hpp" |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 24 | #include "dbus/dbushelper.hpp" |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 25 | #include "dbus/dbuspassive.hpp" |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 26 | #include "dbus/dbuswrite.hpp" |
Patrick Venture | c404b3e | 2018-10-30 14:17:49 -0700 | [diff] [blame] | 27 | #include "errors/exception.hpp" |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 28 | #include "interfaces.hpp" |
| 29 | #include "notimpl/readonly.hpp" |
| 30 | #include "notimpl/writeonly.hpp" |
Patrick Venture | cdd6134 | 2020-08-07 15:49:56 -0700 | [diff] [blame] | 31 | #include "sensors/build_utils.hpp" |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 32 | #include "sensors/builder.hpp" |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 33 | #include "sensors/host.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 34 | #include "sensors/manager.hpp" |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 35 | #include "sensors/pluggable.hpp" |
| 36 | #include "sysfs/sysfsread.hpp" |
| 37 | #include "sysfs/sysfswrite.hpp" |
| 38 | #include "util.hpp" |
| 39 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 40 | namespace pid_control |
| 41 | { |
| 42 | |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 43 | static constexpr bool deferSignals = true; |
| 44 | |
Patrick Williams | 1930027 | 2025-02-01 08:22:48 -0500 | [diff] [blame] | 45 | SensorManager buildSensors( |
| 46 | const std::map<std::string, conf::SensorConfig>& config, |
| 47 | sdbusplus::bus_t& passive, sdbusplus::bus_t& host) |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 48 | { |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 49 | SensorManager mgmr(passive, host); |
Patrick Venture | c179d40 | 2018-10-30 19:51:55 -0700 | [diff] [blame] | 50 | auto& hostSensorBus = mgmr.getHostBus(); |
| 51 | auto& passiveListeningBus = mgmr.getPassiveBus(); |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 52 | |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 53 | for (const auto& it : config) |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 54 | { |
| 55 | std::unique_ptr<ReadInterface> ri; |
| 56 | std::unique_ptr<WriteInterface> wi; |
| 57 | |
| 58 | std::string name = it.first; |
Patrick Venture | 1df9e87 | 2020-10-08 15:35:01 -0700 | [diff] [blame] | 59 | const conf::SensorConfig* info = &it.second; |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 60 | |
| 61 | std::cerr << "Sensor: " << name << " " << info->type << " "; |
Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 62 | std::cerr << info->readPath << " " << info->writePath << "\n"; |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 63 | |
Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 64 | IOInterfaceType rtype = getReadInterfaceType(info->readPath); |
| 65 | IOInterfaceType wtype = getWriteInterfaceType(info->writePath); |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 66 | |
| 67 | // fan sensors can be ready any way and written others. |
| 68 | // fan sensors are the only sensors this is designed to write. |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 69 | // Nothing here should be write-only, although, in theory a fan could |
| 70 | // be. I'm just not sure how that would fit together. |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 71 | // TODO(venture): It should check with the ObjectMapper to check if |
| 72 | // that sensor exists on the Dbus. |
| 73 | switch (rtype) |
| 74 | { |
| 75 | case IOInterfaceType::DBUSPASSIVE: |
James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 76 | // we only need to make one match based on the dbus object |
| 77 | static std::shared_ptr<DbusPassiveRedundancy> redundancy = |
| 78 | std::make_shared<DbusPassiveRedundancy>( |
| 79 | passiveListeningBus); |
| 80 | |
| 81 | if (info->type == "fan") |
| 82 | { |
| 83 | ri = DbusPassive::createDbusPassive( |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 84 | passiveListeningBus, info->type, name, |
Patrick Williams | cd1e78a | 2025-04-07 17:21:05 -0400 | [diff] [blame^] | 85 | std::make_unique<DbusHelper>(passiveListeningBus), info, |
| 86 | redundancy); |
James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 87 | } |
| 88 | else |
| 89 | { |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 90 | ri = DbusPassive::createDbusPassive( |
| 91 | passiveListeningBus, info->type, name, |
Patrick Williams | cd1e78a | 2025-04-07 17:21:05 -0400 | [diff] [blame^] | 92 | std::make_unique<DbusHelper>(passiveListeningBus), info, |
| 93 | nullptr); |
James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 94 | } |
Patrick Venture | e725286 | 2018-10-30 14:23:32 -0700 | [diff] [blame] | 95 | if (ri == nullptr) |
| 96 | { |
| 97 | throw SensorBuildException( |
| 98 | "Failed to create dbus passive sensor: " + name + |
| 99 | " of type: " + info->type); |
| 100 | } |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 101 | break; |
| 102 | case IOInterfaceType::EXTERNAL: |
| 103 | // These are a special case for read-only. |
| 104 | break; |
| 105 | case IOInterfaceType::SYSFS: |
Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 106 | ri = std::make_unique<SysFsRead>(info->readPath); |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 107 | break; |
| 108 | default: |
| 109 | ri = std::make_unique<WriteOnly>(); |
| 110 | break; |
| 111 | } |
| 112 | |
| 113 | if (info->type == "fan") |
| 114 | { |
| 115 | switch (wtype) |
| 116 | { |
| 117 | case IOInterfaceType::SYSFS: |
| 118 | if (info->max > 0) |
| 119 | { |
| 120 | wi = std::make_unique<SysFsWritePercent>( |
Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 121 | info->writePath, info->min, info->max); |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 122 | } |
| 123 | else |
| 124 | { |
Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 125 | wi = std::make_unique<SysFsWrite>(info->writePath, |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 126 | info->min, info->max); |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | break; |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 130 | case IOInterfaceType::DBUSACTIVE: |
| 131 | if (info->max > 0) |
| 132 | { |
Patrick Venture | f5e770b | 2018-10-30 12:28:53 -0700 | [diff] [blame] | 133 | wi = DbusWritePercent::createDbusWrite( |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 134 | info->writePath, info->min, info->max, |
Patrick Williams | cd1e78a | 2025-04-07 17:21:05 -0400 | [diff] [blame^] | 135 | std::make_unique<DbusHelper>(passiveListeningBus)); |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 136 | } |
| 137 | else |
| 138 | { |
Patrick Venture | f5e770b | 2018-10-30 12:28:53 -0700 | [diff] [blame] | 139 | wi = DbusWrite::createDbusWrite( |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 140 | info->writePath, info->min, info->max, |
Patrick Williams | cd1e78a | 2025-04-07 17:21:05 -0400 | [diff] [blame^] | 141 | std::make_unique<DbusHelper>(passiveListeningBus)); |
Patrick Venture | e725286 | 2018-10-30 14:23:32 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | if (wi == nullptr) |
| 145 | { |
| 146 | throw SensorBuildException( |
| 147 | "Unable to create write dbus interface for path: " + |
Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 148 | info->writePath); |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | break; |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 152 | default: |
| 153 | wi = std::make_unique<ReadOnlyNoExcept>(); |
| 154 | break; |
| 155 | } |
| 156 | |
| 157 | auto sensor = std::make_unique<PluggableSensor>( |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 158 | name, info->timeout, std::move(ri), std::move(wi)); |
Patrick Venture | fe75b19 | 2018-06-08 11:19:43 -0700 | [diff] [blame] | 159 | mgmr.addSensor(info->type, name, std::move(sensor)); |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 160 | } |
Josh Lehan | 23e22b9 | 2022-11-12 22:37:58 -0800 | [diff] [blame] | 161 | else if (info->type == "temp" || info->type == "margin" || |
| 162 | info->type == "power" || info->type == "powersum") |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 163 | { |
| 164 | // These sensors are read-only, but only for this application |
| 165 | // which only writes to fan sensors. |
Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 166 | std::cerr << info->type << " readPath: " << info->readPath << "\n"; |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 167 | |
| 168 | if (IOInterfaceType::EXTERNAL == rtype) |
| 169 | { |
| 170 | std::cerr << "Creating HostSensor: " << name |
Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 171 | << " path: " << info->readPath << "\n"; |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 172 | |
| 173 | /* |
| 174 | * The reason we handle this as a HostSensor is because it's |
| 175 | * not quite pluggable; but maybe it could be. |
| 176 | */ |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 177 | auto sensor = HostSensor::createTemp( |
Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 178 | name, info->timeout, hostSensorBus, info->readPath.c_str(), |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 179 | deferSignals); |
Patrick Venture | fe75b19 | 2018-06-08 11:19:43 -0700 | [diff] [blame] | 180 | mgmr.addSensor(info->type, name, std::move(sensor)); |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 181 | } |
| 182 | else |
| 183 | { |
| 184 | wi = std::make_unique<ReadOnlyNoExcept>(); |
| 185 | auto sensor = std::make_unique<PluggableSensor>( |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 186 | name, info->timeout, std::move(ri), std::move(wi)); |
Patrick Venture | fe75b19 | 2018-06-08 11:19:43 -0700 | [diff] [blame] | 187 | mgmr.addSensor(info->type, name, std::move(sensor)); |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | return mgmr; |
| 193 | } |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 194 | |
| 195 | } // namespace pid_control |