blob: 39e35f24cfb907c61cb2e0afcc1c2714929ffba0 [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 Ventureaadb30d2020-08-10 09:17:11 -070023#include "dbus/dbushelper.hpp"
Patrick Venture5e929092018-06-08 10:55:23 -070024#include "dbus/dbuspassive.hpp"
James Feist7136a5a2018-07-19 09:52:05 -070025#include "dbus/dbuswrite.hpp"
Patrick Venturec404b3e2018-10-30 14:17:49 -070026#include "errors/exception.hpp"
Patrick Venture5e929092018-06-08 10:55:23 -070027#include "interfaces.hpp"
28#include "notimpl/readonly.hpp"
29#include "notimpl/writeonly.hpp"
Patrick Venturecdd61342020-08-07 15:49:56 -070030#include "sensors/build_utils.hpp"
Patrick Venture5e929092018-06-08 10:55:23 -070031#include "sensors/builder.hpp"
Patrick Venture5e929092018-06-08 10:55:23 -070032#include "sensors/host.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070033#include "sensors/manager.hpp"
Patrick Venture5e929092018-06-08 10:55:23 -070034#include "sensors/pluggable.hpp"
35#include "sysfs/sysfsread.hpp"
36#include "sysfs/sysfswrite.hpp"
37#include "util.hpp"
38
Patrick Venturea0764872020-08-08 07:48:43 -070039namespace pid_control
40{
41
Patrick Venture5e929092018-06-08 10:55:23 -070042static constexpr bool deferSignals = true;
Patrick Venture0df7c0f2018-06-13 09:02:13 -070043static DbusHelper helper;
Patrick Venture5e929092018-06-08 10:55:23 -070044
Patrick Venturef3252312018-10-30 08:42:53 -070045SensorManager
James Feist1fe08952019-05-07 09:17:16 -070046 buildSensors(const std::map<std::string, struct conf::SensorConfig>& config,
47 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;
James Feistf81f2882019-02-26 11:26:36 -080059 const struct 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(
84 passiveListeningBus, info->type, name, &helper, info,
85 redundancy);
86 }
87 else
88 {
89 ri = DbusPassive::createDbusPassive(passiveListeningBus,
90 info->type, name,
91 &helper, info, nullptr);
92 }
Patrick Venturee7252862018-10-30 14:23:32 -070093 if (ri == nullptr)
94 {
95 throw SensorBuildException(
96 "Failed to create dbus passive sensor: " + name +
97 " of type: " + info->type);
98 }
Patrick Venture5e929092018-06-08 10:55:23 -070099 break;
100 case IOInterfaceType::EXTERNAL:
101 // These are a special case for read-only.
102 break;
103 case IOInterfaceType::SYSFS:
Patrick Venture69c51062019-02-11 09:46:03 -0800104 ri = std::make_unique<SysFsRead>(info->readPath);
Patrick Venture5e929092018-06-08 10:55:23 -0700105 break;
106 default:
107 ri = std::make_unique<WriteOnly>();
108 break;
109 }
110
111 if (info->type == "fan")
112 {
113 switch (wtype)
114 {
115 case IOInterfaceType::SYSFS:
116 if (info->max > 0)
117 {
118 wi = std::make_unique<SysFsWritePercent>(
Patrick Venture69c51062019-02-11 09:46:03 -0800119 info->writePath, info->min, info->max);
Patrick Venture5e929092018-06-08 10:55:23 -0700120 }
121 else
122 {
Patrick Venture69c51062019-02-11 09:46:03 -0800123 wi = std::make_unique<SysFsWrite>(info->writePath,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700124 info->min, info->max);
Patrick Venture5e929092018-06-08 10:55:23 -0700125 }
126
127 break;
James Feist7136a5a2018-07-19 09:52:05 -0700128 case IOInterfaceType::DBUSACTIVE:
129 if (info->max > 0)
130 {
Patrick Venturef5e770b2018-10-30 12:28:53 -0700131 wi = DbusWritePercent::createDbusWrite(
Patrick Venture69c51062019-02-11 09:46:03 -0800132 info->writePath, info->min, info->max, helper);
James Feist7136a5a2018-07-19 09:52:05 -0700133 }
134 else
135 {
Patrick Venturef5e770b2018-10-30 12:28:53 -0700136 wi = DbusWrite::createDbusWrite(
Patrick Venture69c51062019-02-11 09:46:03 -0800137 info->writePath, info->min, info->max, helper);
Patrick Venturee7252862018-10-30 14:23:32 -0700138 }
139
140 if (wi == nullptr)
141 {
142 throw SensorBuildException(
143 "Unable to create write dbus interface for path: " +
Patrick Venture69c51062019-02-11 09:46:03 -0800144 info->writePath);
James Feist7136a5a2018-07-19 09:52:05 -0700145 }
146
147 break;
Patrick Venture5e929092018-06-08 10:55:23 -0700148 default:
149 wi = std::make_unique<ReadOnlyNoExcept>();
150 break;
151 }
152
153 auto sensor = std::make_unique<PluggableSensor>(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700154 name, info->timeout, std::move(ri), std::move(wi));
Patrick Venturefe75b192018-06-08 11:19:43 -0700155 mgmr.addSensor(info->type, name, std::move(sensor));
Patrick Venture5e929092018-06-08 10:55:23 -0700156 }
157 else if (info->type == "temp" || info->type == "margin")
158 {
159 // These sensors are read-only, but only for this application
160 // which only writes to fan sensors.
Patrick Venture69c51062019-02-11 09:46:03 -0800161 std::cerr << info->type << " readPath: " << info->readPath << "\n";
Patrick Venture5e929092018-06-08 10:55:23 -0700162
163 if (IOInterfaceType::EXTERNAL == rtype)
164 {
165 std::cerr << "Creating HostSensor: " << name
Patrick Venture69c51062019-02-11 09:46:03 -0800166 << " path: " << info->readPath << "\n";
Patrick Venture5e929092018-06-08 10:55:23 -0700167
168 /*
169 * The reason we handle this as a HostSensor is because it's
170 * not quite pluggable; but maybe it could be.
171 */
Patrick Venture563a3562018-10-30 09:31:26 -0700172 auto sensor = HostSensor::createTemp(
Patrick Venture69c51062019-02-11 09:46:03 -0800173 name, info->timeout, hostSensorBus, info->readPath.c_str(),
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700174 deferSignals);
Patrick Venturefe75b192018-06-08 11:19:43 -0700175 mgmr.addSensor(info->type, name, std::move(sensor));
Patrick Venture5e929092018-06-08 10:55:23 -0700176 }
177 else
178 {
179 wi = std::make_unique<ReadOnlyNoExcept>();
180 auto sensor = std::make_unique<PluggableSensor>(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700181 name, info->timeout, std::move(ri), std::move(wi));
Patrick Venturefe75b192018-06-08 11:19:43 -0700182 mgmr.addSensor(info->type, name, std::move(sensor));
Patrick Venture5e929092018-06-08 10:55:23 -0700183 }
184 }
185 }
186
187 return mgmr;
188}
Patrick Venturea0764872020-08-08 07:48:43 -0700189
190} // namespace pid_control