blob: 8a1f54ab9603868feed2ae21b17d035770869579 [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>
19#include <string>
Patrick Venture5e929092018-06-08 10:55:23 -070020
21/* Configuration. */
22#include "conf.hpp"
Patrick Venture5e929092018-06-08 10:55:23 -070023#include "dbus/dbuspassive.hpp"
James Feist7136a5a2018-07-19 09:52:05 -070024#include "dbus/dbuswrite.hpp"
Patrick Venturec404b3e2018-10-30 14:17:49 -070025#include "errors/exception.hpp"
Patrick Venture5e929092018-06-08 10:55:23 -070026#include "interfaces.hpp"
27#include "notimpl/readonly.hpp"
28#include "notimpl/writeonly.hpp"
Patrick Venturecdd61342020-08-07 15:49:56 -070029#include "sensors/build_utils.hpp"
Patrick Venture5e929092018-06-08 10:55:23 -070030#include "sensors/builder.hpp"
Patrick Venture5e929092018-06-08 10:55:23 -070031#include "sensors/host.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070032#include "sensors/manager.hpp"
Patrick Venture5e929092018-06-08 10:55:23 -070033#include "sensors/pluggable.hpp"
34#include "sysfs/sysfsread.hpp"
35#include "sysfs/sysfswrite.hpp"
36#include "util.hpp"
37
Patrick Venturea0764872020-08-08 07:48:43 -070038namespace pid_control
39{
40
Patrick Venture5e929092018-06-08 10:55:23 -070041static constexpr bool deferSignals = true;
Patrick Venture0df7c0f2018-06-13 09:02:13 -070042static DbusHelper helper;
Patrick Venture5e929092018-06-08 10:55:23 -070043
Patrick Venturef3252312018-10-30 08:42:53 -070044SensorManager
James Feist1fe08952019-05-07 09:17:16 -070045 buildSensors(const std::map<std::string, struct conf::SensorConfig>& config,
46 sdbusplus::bus::bus& passive, sdbusplus::bus::bus& host)
Patrick Venture5e929092018-06-08 10:55:23 -070047{
James Feist1fe08952019-05-07 09:17:16 -070048 SensorManager mgmr(passive, host);
Patrick Venturec179d402018-10-30 19:51:55 -070049 auto& hostSensorBus = mgmr.getHostBus();
50 auto& passiveListeningBus = mgmr.getPassiveBus();
Patrick Venture5e929092018-06-08 10:55:23 -070051
Patrick Venture4a2dc4d2018-10-23 09:02:55 -070052 for (const auto& it : config)
Patrick Venture5e929092018-06-08 10:55:23 -070053 {
54 std::unique_ptr<ReadInterface> ri;
55 std::unique_ptr<WriteInterface> wi;
56
57 std::string name = it.first;
James Feistf81f2882019-02-26 11:26:36 -080058 const struct conf::SensorConfig* info = &it.second;
Patrick Venture5e929092018-06-08 10:55:23 -070059
60 std::cerr << "Sensor: " << name << " " << info->type << " ";
Patrick Venture69c51062019-02-11 09:46:03 -080061 std::cerr << info->readPath << " " << info->writePath << "\n";
Patrick Venture5e929092018-06-08 10:55:23 -070062
Patrick Venture69c51062019-02-11 09:46:03 -080063 IOInterfaceType rtype = getReadInterfaceType(info->readPath);
64 IOInterfaceType wtype = getWriteInterfaceType(info->writePath);
Patrick Venture5e929092018-06-08 10:55:23 -070065
66 // fan sensors can be ready any way and written others.
67 // fan sensors are the only sensors this is designed to write.
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070068 // Nothing here should be write-only, although, in theory a fan could
69 // be. I'm just not sure how that would fit together.
Patrick Venture5e929092018-06-08 10:55:23 -070070 // TODO(venture): It should check with the ObjectMapper to check if
71 // that sensor exists on the Dbus.
72 switch (rtype)
73 {
74 case IOInterfaceType::DBUSPASSIVE:
James Feist98b704e2019-06-03 16:24:53 -070075 // we only need to make one match based on the dbus object
76 static std::shared_ptr<DbusPassiveRedundancy> redundancy =
77 std::make_shared<DbusPassiveRedundancy>(
78 passiveListeningBus);
79
80 if (info->type == "fan")
81 {
82 ri = DbusPassive::createDbusPassive(
83 passiveListeningBus, info->type, name, &helper, info,
84 redundancy);
85 }
86 else
87 {
88 ri = DbusPassive::createDbusPassive(passiveListeningBus,
89 info->type, name,
90 &helper, info, nullptr);
91 }
Patrick Venturee7252862018-10-30 14:23:32 -070092 if (ri == nullptr)
93 {
94 throw SensorBuildException(
95 "Failed to create dbus passive sensor: " + name +
96 " of type: " + info->type);
97 }
Patrick Venture5e929092018-06-08 10:55:23 -070098 break;
99 case IOInterfaceType::EXTERNAL:
100 // These are a special case for read-only.
101 break;
102 case IOInterfaceType::SYSFS:
Patrick Venture69c51062019-02-11 09:46:03 -0800103 ri = std::make_unique<SysFsRead>(info->readPath);
Patrick Venture5e929092018-06-08 10:55:23 -0700104 break;
105 default:
106 ri = std::make_unique<WriteOnly>();
107 break;
108 }
109
110 if (info->type == "fan")
111 {
112 switch (wtype)
113 {
114 case IOInterfaceType::SYSFS:
115 if (info->max > 0)
116 {
117 wi = std::make_unique<SysFsWritePercent>(
Patrick Venture69c51062019-02-11 09:46:03 -0800118 info->writePath, info->min, info->max);
Patrick Venture5e929092018-06-08 10:55:23 -0700119 }
120 else
121 {
Patrick Venture69c51062019-02-11 09:46:03 -0800122 wi = std::make_unique<SysFsWrite>(info->writePath,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700123 info->min, info->max);
Patrick Venture5e929092018-06-08 10:55:23 -0700124 }
125
126 break;
James Feist7136a5a2018-07-19 09:52:05 -0700127 case IOInterfaceType::DBUSACTIVE:
128 if (info->max > 0)
129 {
Patrick Venturef5e770b2018-10-30 12:28:53 -0700130 wi = DbusWritePercent::createDbusWrite(
Patrick Venture69c51062019-02-11 09:46:03 -0800131 info->writePath, info->min, info->max, helper);
James Feist7136a5a2018-07-19 09:52:05 -0700132 }
133 else
134 {
Patrick Venturef5e770b2018-10-30 12:28:53 -0700135 wi = DbusWrite::createDbusWrite(
Patrick Venture69c51062019-02-11 09:46:03 -0800136 info->writePath, info->min, info->max, helper);
Patrick Venturee7252862018-10-30 14:23:32 -0700137 }
138
139 if (wi == nullptr)
140 {
141 throw SensorBuildException(
142 "Unable to create write dbus interface for path: " +
Patrick Venture69c51062019-02-11 09:46:03 -0800143 info->writePath);
James Feist7136a5a2018-07-19 09:52:05 -0700144 }
145
146 break;
Patrick Venture5e929092018-06-08 10:55:23 -0700147 default:
148 wi = std::make_unique<ReadOnlyNoExcept>();
149 break;
150 }
151
152 auto sensor = std::make_unique<PluggableSensor>(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700153 name, info->timeout, std::move(ri), std::move(wi));
Patrick Venturefe75b192018-06-08 11:19:43 -0700154 mgmr.addSensor(info->type, name, std::move(sensor));
Patrick Venture5e929092018-06-08 10:55:23 -0700155 }
156 else if (info->type == "temp" || info->type == "margin")
157 {
158 // These sensors are read-only, but only for this application
159 // which only writes to fan sensors.
Patrick Venture69c51062019-02-11 09:46:03 -0800160 std::cerr << info->type << " readPath: " << info->readPath << "\n";
Patrick Venture5e929092018-06-08 10:55:23 -0700161
162 if (IOInterfaceType::EXTERNAL == rtype)
163 {
164 std::cerr << "Creating HostSensor: " << name
Patrick Venture69c51062019-02-11 09:46:03 -0800165 << " path: " << info->readPath << "\n";
Patrick Venture5e929092018-06-08 10:55:23 -0700166
167 /*
168 * The reason we handle this as a HostSensor is because it's
169 * not quite pluggable; but maybe it could be.
170 */
Patrick Venture563a3562018-10-30 09:31:26 -0700171 auto sensor = HostSensor::createTemp(
Patrick Venture69c51062019-02-11 09:46:03 -0800172 name, info->timeout, hostSensorBus, info->readPath.c_str(),
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700173 deferSignals);
Patrick Venturefe75b192018-06-08 11:19:43 -0700174 mgmr.addSensor(info->type, name, std::move(sensor));
Patrick Venture5e929092018-06-08 10:55:23 -0700175 }
176 else
177 {
178 wi = std::make_unique<ReadOnlyNoExcept>();
179 auto sensor = std::make_unique<PluggableSensor>(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700180 name, info->timeout, std::move(ri), std::move(wi));
Patrick Venturefe75b192018-06-08 11:19:43 -0700181 mgmr.addSensor(info->type, name, std::move(sensor));
Patrick Venture5e929092018-06-08 10:55:23 -0700182 }
183 }
184 }
185
186 return mgmr;
187}
Patrick Venturea0764872020-08-08 07:48:43 -0700188
189} // namespace pid_control