blob: e523b264ad5196540a96f610c4b8473beba5ec70 [file] [log] [blame]
Patrick Venture5e929092018-06-08 10:55:23 -07001/**
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 Venturefe75b192018-06-08 11:19:43 -070018#include <map>
Patrick Venture8729eb92020-08-10 10:38:44 -070019#include <memory>
Patrick Venturefe75b192018-06-08 11:19:43 -070020#include <string>
Ed Tanousf8b6e552025-06-27 13:27:50 -070021#include <utility>
Patrick Venture5e929092018-06-08 10:55:23 -070022
23/* Configuration. */
24#include "conf.hpp"
Patrick Ventureaadb30d2020-08-10 09:17:11 -070025#include "dbus/dbushelper.hpp"
Patrick Venture5e929092018-06-08 10:55:23 -070026#include "dbus/dbuspassive.hpp"
James Feist7136a5a2018-07-19 09:52:05 -070027#include "dbus/dbuswrite.hpp"
Ed Tanousf8b6e552025-06-27 13:27:50 -070028#include "dbuspassiveredundancy.hpp"
Patrick Venturec404b3e2018-10-30 14:17:49 -070029#include "errors/exception.hpp"
Patrick Venture5e929092018-06-08 10:55:23 -070030#include "interfaces.hpp"
31#include "notimpl/readonly.hpp"
32#include "notimpl/writeonly.hpp"
Patrick Venturecdd61342020-08-07 15:49:56 -070033#include "sensors/build_utils.hpp"
Patrick Venture5e929092018-06-08 10:55:23 -070034#include "sensors/builder.hpp"
Patrick Venture5e929092018-06-08 10:55:23 -070035#include "sensors/host.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070036#include "sensors/manager.hpp"
Patrick Venture5e929092018-06-08 10:55:23 -070037#include "sensors/pluggable.hpp"
38#include "sysfs/sysfsread.hpp"
39#include "sysfs/sysfswrite.hpp"
Ed Tanousf8b6e552025-06-27 13:27:50 -070040
41#include <sdbusplus/bus.hpp>
Patrick Venture5e929092018-06-08 10:55:23 -070042
Patrick Venturea0764872020-08-08 07:48:43 -070043namespace pid_control
44{
45
Patrick Venture5e929092018-06-08 10:55:23 -070046static constexpr bool deferSignals = true;
47
Patrick Williams19300272025-02-01 08:22:48 -050048SensorManager buildSensors(
49 const std::map<std::string, conf::SensorConfig>& config,
50 sdbusplus::bus_t& passive, sdbusplus::bus_t& host)
Patrick Venture5e929092018-06-08 10:55:23 -070051{
Patrick Williamse3c60772025-04-07 17:53:42 -040052 SensorManager mgmr{passive, host};
Patrick Venturec179d402018-10-30 19:51:55 -070053 auto& hostSensorBus = mgmr.getHostBus();
54 auto& passiveListeningBus = mgmr.getPassiveBus();
Patrick Venture5e929092018-06-08 10:55:23 -070055
Patrick Venture4a2dc4d2018-10-23 09:02:55 -070056 for (const auto& it : config)
Patrick Venture5e929092018-06-08 10:55:23 -070057 {
58 std::unique_ptr<ReadInterface> ri;
59 std::unique_ptr<WriteInterface> wi;
60
61 std::string name = it.first;
Patrick Venture1df9e872020-10-08 15:35:01 -070062 const conf::SensorConfig* info = &it.second;
Patrick Venture5e929092018-06-08 10:55:23 -070063
64 std::cerr << "Sensor: " << name << " " << info->type << " ";
Patrick Venture69c51062019-02-11 09:46:03 -080065 std::cerr << info->readPath << " " << info->writePath << "\n";
Patrick Venture5e929092018-06-08 10:55:23 -070066
Patrick Venture69c51062019-02-11 09:46:03 -080067 IOInterfaceType rtype = getReadInterfaceType(info->readPath);
68 IOInterfaceType wtype = getWriteInterfaceType(info->writePath);
Patrick Venture5e929092018-06-08 10:55:23 -070069
70 // fan sensors can be ready any way and written others.
71 // fan sensors are the only sensors this is designed to write.
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070072 // Nothing here should be write-only, although, in theory a fan could
73 // be. I'm just not sure how that would fit together.
Patrick Venture5e929092018-06-08 10:55:23 -070074 // TODO(venture): It should check with the ObjectMapper to check if
75 // that sensor exists on the Dbus.
76 switch (rtype)
77 {
78 case IOInterfaceType::DBUSPASSIVE:
James Feist98b704e2019-06-03 16:24:53 -070079 // we only need to make one match based on the dbus object
80 static std::shared_ptr<DbusPassiveRedundancy> redundancy =
81 std::make_shared<DbusPassiveRedundancy>(
82 passiveListeningBus);
83
84 if (info->type == "fan")
85 {
86 ri = DbusPassive::createDbusPassive(
Patrick Venture8729eb92020-08-10 10:38:44 -070087 passiveListeningBus, info->type, name,
Patrick Williamscd1e78a2025-04-07 17:21:05 -040088 std::make_unique<DbusHelper>(passiveListeningBus), info,
89 redundancy);
James Feist98b704e2019-06-03 16:24:53 -070090 }
91 else
92 {
Patrick Venture8729eb92020-08-10 10:38:44 -070093 ri = DbusPassive::createDbusPassive(
94 passiveListeningBus, info->type, name,
Patrick Williamscd1e78a2025-04-07 17:21:05 -040095 std::make_unique<DbusHelper>(passiveListeningBus), info,
96 nullptr);
James Feist98b704e2019-06-03 16:24:53 -070097 }
Patrick Venturee7252862018-10-30 14:23:32 -070098 if (ri == nullptr)
99 {
100 throw SensorBuildException(
101 "Failed to create dbus passive sensor: " + name +
102 " of type: " + info->type);
103 }
Patrick Venture5e929092018-06-08 10:55:23 -0700104 break;
105 case IOInterfaceType::EXTERNAL:
106 // These are a special case for read-only.
107 break;
108 case IOInterfaceType::SYSFS:
Patrick Venture69c51062019-02-11 09:46:03 -0800109 ri = std::make_unique<SysFsRead>(info->readPath);
Patrick Venture5e929092018-06-08 10:55:23 -0700110 break;
111 default:
112 ri = std::make_unique<WriteOnly>();
113 break;
114 }
115
116 if (info->type == "fan")
117 {
118 switch (wtype)
119 {
120 case IOInterfaceType::SYSFS:
121 if (info->max > 0)
122 {
123 wi = std::make_unique<SysFsWritePercent>(
Patrick Venture69c51062019-02-11 09:46:03 -0800124 info->writePath, info->min, info->max);
Patrick Venture5e929092018-06-08 10:55:23 -0700125 }
126 else
127 {
Patrick Venture69c51062019-02-11 09:46:03 -0800128 wi = std::make_unique<SysFsWrite>(info->writePath,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700129 info->min, info->max);
Patrick Venture5e929092018-06-08 10:55:23 -0700130 }
131
132 break;
James Feist7136a5a2018-07-19 09:52:05 -0700133 case IOInterfaceType::DBUSACTIVE:
134 if (info->max > 0)
135 {
Patrick Venturef5e770b2018-10-30 12:28:53 -0700136 wi = DbusWritePercent::createDbusWrite(
Patrick Venture8729eb92020-08-10 10:38:44 -0700137 info->writePath, info->min, info->max,
Patrick Williamscd1e78a2025-04-07 17:21:05 -0400138 std::make_unique<DbusHelper>(passiveListeningBus));
James Feist7136a5a2018-07-19 09:52:05 -0700139 }
140 else
141 {
Patrick Venturef5e770b2018-10-30 12:28:53 -0700142 wi = DbusWrite::createDbusWrite(
Patrick Venture8729eb92020-08-10 10:38:44 -0700143 info->writePath, info->min, info->max,
Patrick Williamscd1e78a2025-04-07 17:21:05 -0400144 std::make_unique<DbusHelper>(passiveListeningBus));
Patrick Venturee7252862018-10-30 14:23:32 -0700145 }
146
147 if (wi == nullptr)
148 {
149 throw SensorBuildException(
150 "Unable to create write dbus interface for path: " +
Patrick Venture69c51062019-02-11 09:46:03 -0800151 info->writePath);
James Feist7136a5a2018-07-19 09:52:05 -0700152 }
153
154 break;
Patrick Venture5e929092018-06-08 10:55:23 -0700155 default:
156 wi = std::make_unique<ReadOnlyNoExcept>();
157 break;
158 }
159
160 auto sensor = std::make_unique<PluggableSensor>(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700161 name, info->timeout, std::move(ri), std::move(wi));
Patrick Venturefe75b192018-06-08 11:19:43 -0700162 mgmr.addSensor(info->type, name, std::move(sensor));
Patrick Venture5e929092018-06-08 10:55:23 -0700163 }
Josh Lehan23e22b92022-11-12 22:37:58 -0800164 else if (info->type == "temp" || info->type == "margin" ||
165 info->type == "power" || info->type == "powersum")
Patrick Venture5e929092018-06-08 10:55:23 -0700166 {
167 // These sensors are read-only, but only for this application
168 // which only writes to fan sensors.
Patrick Venture69c51062019-02-11 09:46:03 -0800169 std::cerr << info->type << " readPath: " << info->readPath << "\n";
Patrick Venture5e929092018-06-08 10:55:23 -0700170
171 if (IOInterfaceType::EXTERNAL == rtype)
172 {
173 std::cerr << "Creating HostSensor: " << name
Patrick Venture69c51062019-02-11 09:46:03 -0800174 << " path: " << info->readPath << "\n";
Patrick Venture5e929092018-06-08 10:55:23 -0700175
176 /*
177 * The reason we handle this as a HostSensor is because it's
178 * not quite pluggable; but maybe it could be.
179 */
Patrick Venture563a3562018-10-30 09:31:26 -0700180 auto sensor = HostSensor::createTemp(
Patrick Venture69c51062019-02-11 09:46:03 -0800181 name, info->timeout, hostSensorBus, info->readPath.c_str(),
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700182 deferSignals);
Patrick Venturefe75b192018-06-08 11:19:43 -0700183 mgmr.addSensor(info->type, name, std::move(sensor));
Patrick Venture5e929092018-06-08 10:55:23 -0700184 }
185 else
186 {
187 wi = std::make_unique<ReadOnlyNoExcept>();
188 auto sensor = std::make_unique<PluggableSensor>(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700189 name, info->timeout, std::move(ri), std::move(wi));
Patrick Venturefe75b192018-06-08 11:19:43 -0700190 mgmr.addSensor(info->type, name, std::move(sensor));
Patrick Venture5e929092018-06-08 10:55:23 -0700191 }
192 }
193 }
194
195 return mgmr;
196}
Patrick Venturea0764872020-08-08 07:48:43 -0700197
198} // namespace pid_control