blob: f4257ad5719a625a4b8d4b80884f5e782c0afb2d [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>
Patrick Venture5e929092018-06-08 10:55:23 -070021
22/* Configuration. */
23#include "conf.hpp"
Patrick Ventureaadb30d2020-08-10 09:17:11 -070024#include "dbus/dbushelper.hpp"
Patrick Venture5e929092018-06-08 10:55:23 -070025#include "dbus/dbuspassive.hpp"
James Feist7136a5a2018-07-19 09:52:05 -070026#include "dbus/dbuswrite.hpp"
Patrick Venturec404b3e2018-10-30 14:17:49 -070027#include "errors/exception.hpp"
Patrick Venture5e929092018-06-08 10:55:23 -070028#include "interfaces.hpp"
29#include "notimpl/readonly.hpp"
30#include "notimpl/writeonly.hpp"
Patrick Venturecdd61342020-08-07 15:49:56 -070031#include "sensors/build_utils.hpp"
Patrick Venture5e929092018-06-08 10:55:23 -070032#include "sensors/builder.hpp"
Patrick Venture5e929092018-06-08 10:55:23 -070033#include "sensors/host.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070034#include "sensors/manager.hpp"
Patrick Venture5e929092018-06-08 10:55:23 -070035#include "sensors/pluggable.hpp"
36#include "sysfs/sysfsread.hpp"
37#include "sysfs/sysfswrite.hpp"
38#include "util.hpp"
39
Patrick Venturea0764872020-08-08 07:48:43 -070040namespace pid_control
41{
42
Patrick Venture5e929092018-06-08 10:55:23 -070043static constexpr bool deferSignals = true;
44
Patrick Venturef3252312018-10-30 08:42:53 -070045SensorManager
Patrick Venture1df9e872020-10-08 15:35:01 -070046 buildSensors(const std::map<std::string, conf::SensorConfig>& config,
James Feist1fe08952019-05-07 09:17:16 -070047 sdbusplus::bus::bus& passive, sdbusplus::bus::bus& host)
Patrick Venture5e929092018-06-08 10:55:23 -070048{
James Feist1fe08952019-05-07 09:17:16 -070049 SensorManager mgmr(passive, host);
Patrick Venturec179d402018-10-30 19:51:55 -070050 auto& hostSensorBus = mgmr.getHostBus();
51 auto& passiveListeningBus = mgmr.getPassiveBus();
Patrick Venture5e929092018-06-08 10:55:23 -070052
Patrick Venture4a2dc4d2018-10-23 09:02:55 -070053 for (const auto& it : config)
Patrick Venture5e929092018-06-08 10:55:23 -070054 {
55 std::unique_ptr<ReadInterface> ri;
56 std::unique_ptr<WriteInterface> wi;
57
58 std::string name = it.first;
Patrick Venture1df9e872020-10-08 15:35:01 -070059 const conf::SensorConfig* info = &it.second;
Patrick Venture5e929092018-06-08 10:55:23 -070060
61 std::cerr << "Sensor: " << name << " " << info->type << " ";
Patrick Venture69c51062019-02-11 09:46:03 -080062 std::cerr << info->readPath << " " << info->writePath << "\n";
Patrick Venture5e929092018-06-08 10:55:23 -070063
Patrick Venture69c51062019-02-11 09:46:03 -080064 IOInterfaceType rtype = getReadInterfaceType(info->readPath);
65 IOInterfaceType wtype = getWriteInterfaceType(info->writePath);
Patrick Venture5e929092018-06-08 10:55:23 -070066
67 // fan sensors can be ready any way and written others.
68 // fan sensors are the only sensors this is designed to write.
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070069 // 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 Venture5e929092018-06-08 10:55:23 -070071 // 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 Feist98b704e2019-06-03 16:24:53 -070076 // 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 Venture8729eb92020-08-10 10:38:44 -070084 passiveListeningBus, info->type, name,
85 std::make_unique<DbusHelper>(
86 sdbusplus::bus::new_system()),
87 info, redundancy);
James Feist98b704e2019-06-03 16:24:53 -070088 }
89 else
90 {
Patrick Venture8729eb92020-08-10 10:38:44 -070091 ri = DbusPassive::createDbusPassive(
92 passiveListeningBus, info->type, name,
93 std::make_unique<DbusHelper>(
94 sdbusplus::bus::new_system()),
95 info, nullptr);
James Feist98b704e2019-06-03 16:24:53 -070096 }
Patrick Venturee7252862018-10-30 14:23:32 -070097 if (ri == nullptr)
98 {
99 throw SensorBuildException(
100 "Failed to create dbus passive sensor: " + name +
101 " of type: " + info->type);
102 }
Patrick Venture5e929092018-06-08 10:55:23 -0700103 break;
104 case IOInterfaceType::EXTERNAL:
105 // These are a special case for read-only.
106 break;
107 case IOInterfaceType::SYSFS:
Patrick Venture69c51062019-02-11 09:46:03 -0800108 ri = std::make_unique<SysFsRead>(info->readPath);
Patrick Venture5e929092018-06-08 10:55:23 -0700109 break;
110 default:
111 ri = std::make_unique<WriteOnly>();
112 break;
113 }
114
115 if (info->type == "fan")
116 {
117 switch (wtype)
118 {
119 case IOInterfaceType::SYSFS:
120 if (info->max > 0)
121 {
122 wi = std::make_unique<SysFsWritePercent>(
Patrick Venture69c51062019-02-11 09:46:03 -0800123 info->writePath, info->min, info->max);
Patrick Venture5e929092018-06-08 10:55:23 -0700124 }
125 else
126 {
Patrick Venture69c51062019-02-11 09:46:03 -0800127 wi = std::make_unique<SysFsWrite>(info->writePath,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700128 info->min, info->max);
Patrick Venture5e929092018-06-08 10:55:23 -0700129 }
130
131 break;
James Feist7136a5a2018-07-19 09:52:05 -0700132 case IOInterfaceType::DBUSACTIVE:
133 if (info->max > 0)
134 {
Patrick Venturef5e770b2018-10-30 12:28:53 -0700135 wi = DbusWritePercent::createDbusWrite(
Patrick Venture8729eb92020-08-10 10:38:44 -0700136 info->writePath, info->min, info->max,
137 std::make_unique<DbusHelper>(
138 sdbusplus::bus::new_system()));
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,
144 std::make_unique<DbusHelper>(
145 sdbusplus::bus::new_system()));
Patrick Venturee7252862018-10-30 14:23:32 -0700146 }
147
148 if (wi == nullptr)
149 {
150 throw SensorBuildException(
151 "Unable to create write dbus interface for path: " +
Patrick Venture69c51062019-02-11 09:46:03 -0800152 info->writePath);
James Feist7136a5a2018-07-19 09:52:05 -0700153 }
154
155 break;
Patrick Venture5e929092018-06-08 10:55:23 -0700156 default:
157 wi = std::make_unique<ReadOnlyNoExcept>();
158 break;
159 }
160
161 auto sensor = std::make_unique<PluggableSensor>(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700162 name, info->timeout, std::move(ri), std::move(wi));
Patrick Venturefe75b192018-06-08 11:19:43 -0700163 mgmr.addSensor(info->type, name, std::move(sensor));
Patrick Venture5e929092018-06-08 10:55:23 -0700164 }
165 else if (info->type == "temp" || info->type == "margin")
166 {
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