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