blob: f5c01b33f8c244780d0ab4d3995826dba1f18251 [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"
29#include "sensors/builder.hpp"
Patrick Venture5e929092018-06-08 10:55:23 -070030#include "sensors/host.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070031#include "sensors/manager.hpp"
Patrick Venture5e929092018-06-08 10:55:23 -070032#include "sensors/pluggable.hpp"
33#include "sysfs/sysfsread.hpp"
34#include "sysfs/sysfswrite.hpp"
35#include "util.hpp"
36
37static constexpr bool deferSignals = true;
Patrick Venture0df7c0f2018-06-13 09:02:13 -070038static DbusHelper helper;
Patrick Venture5e929092018-06-08 10:55:23 -070039
Patrick Venturef3252312018-10-30 08:42:53 -070040SensorManager
James Feist1fe08952019-05-07 09:17:16 -070041 buildSensors(const std::map<std::string, struct conf::SensorConfig>& config,
42 sdbusplus::bus::bus& passive, sdbusplus::bus::bus& host)
Patrick Venture5e929092018-06-08 10:55:23 -070043{
James Feist1fe08952019-05-07 09:17:16 -070044 SensorManager mgmr(passive, host);
Patrick Venturec179d402018-10-30 19:51:55 -070045 auto& hostSensorBus = mgmr.getHostBus();
46 auto& passiveListeningBus = mgmr.getPassiveBus();
Patrick Venture5e929092018-06-08 10:55:23 -070047
Patrick Venture4a2dc4d2018-10-23 09:02:55 -070048 for (const auto& it : config)
Patrick Venture5e929092018-06-08 10:55:23 -070049 {
50 std::unique_ptr<ReadInterface> ri;
51 std::unique_ptr<WriteInterface> wi;
52
53 std::string name = it.first;
James Feistf81f2882019-02-26 11:26:36 -080054 const struct conf::SensorConfig* info = &it.second;
Patrick Venture5e929092018-06-08 10:55:23 -070055
56 std::cerr << "Sensor: " << name << " " << info->type << " ";
Patrick Venture69c51062019-02-11 09:46:03 -080057 std::cerr << info->readPath << " " << info->writePath << "\n";
Patrick Venture5e929092018-06-08 10:55:23 -070058
Patrick Venture69c51062019-02-11 09:46:03 -080059 IOInterfaceType rtype = getReadInterfaceType(info->readPath);
60 IOInterfaceType wtype = getWriteInterfaceType(info->writePath);
Patrick Venture5e929092018-06-08 10:55:23 -070061
62 // fan sensors can be ready any way and written others.
63 // fan sensors are the only sensors this is designed to write.
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070064 // Nothing here should be write-only, although, in theory a fan could
65 // be. I'm just not sure how that would fit together.
Patrick Venture5e929092018-06-08 10:55:23 -070066 // TODO(venture): It should check with the ObjectMapper to check if
67 // that sensor exists on the Dbus.
68 switch (rtype)
69 {
70 case IOInterfaceType::DBUSPASSIVE:
James Feist75eb7692019-02-25 12:50:02 -080071 ri = DbusPassive::createDbusPassive(
72 passiveListeningBus, info->type, name, &helper, info);
Patrick Venturee7252862018-10-30 14:23:32 -070073 if (ri == nullptr)
74 {
75 throw SensorBuildException(
76 "Failed to create dbus passive sensor: " + name +
77 " of type: " + info->type);
78 }
Patrick Venture5e929092018-06-08 10:55:23 -070079 break;
80 case IOInterfaceType::EXTERNAL:
81 // These are a special case for read-only.
82 break;
83 case IOInterfaceType::SYSFS:
Patrick Venture69c51062019-02-11 09:46:03 -080084 ri = std::make_unique<SysFsRead>(info->readPath);
Patrick Venture5e929092018-06-08 10:55:23 -070085 break;
86 default:
87 ri = std::make_unique<WriteOnly>();
88 break;
89 }
90
91 if (info->type == "fan")
92 {
93 switch (wtype)
94 {
95 case IOInterfaceType::SYSFS:
96 if (info->max > 0)
97 {
98 wi = std::make_unique<SysFsWritePercent>(
Patrick Venture69c51062019-02-11 09:46:03 -080099 info->writePath, info->min, info->max);
Patrick Venture5e929092018-06-08 10:55:23 -0700100 }
101 else
102 {
Patrick Venture69c51062019-02-11 09:46:03 -0800103 wi = std::make_unique<SysFsWrite>(info->writePath,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700104 info->min, info->max);
Patrick Venture5e929092018-06-08 10:55:23 -0700105 }
106
107 break;
James Feist7136a5a2018-07-19 09:52:05 -0700108 case IOInterfaceType::DBUSACTIVE:
109 if (info->max > 0)
110 {
Patrick Venturef5e770b2018-10-30 12:28:53 -0700111 wi = DbusWritePercent::createDbusWrite(
Patrick Venture69c51062019-02-11 09:46:03 -0800112 info->writePath, info->min, info->max, helper);
James Feist7136a5a2018-07-19 09:52:05 -0700113 }
114 else
115 {
Patrick Venturef5e770b2018-10-30 12:28:53 -0700116 wi = DbusWrite::createDbusWrite(
Patrick Venture69c51062019-02-11 09:46:03 -0800117 info->writePath, info->min, info->max, helper);
Patrick Venturee7252862018-10-30 14:23:32 -0700118 }
119
120 if (wi == nullptr)
121 {
122 throw SensorBuildException(
123 "Unable to create write dbus interface for path: " +
Patrick Venture69c51062019-02-11 09:46:03 -0800124 info->writePath);
James Feist7136a5a2018-07-19 09:52:05 -0700125 }
126
127 break;
Patrick Venture5e929092018-06-08 10:55:23 -0700128 default:
129 wi = std::make_unique<ReadOnlyNoExcept>();
130 break;
131 }
132
133 auto sensor = std::make_unique<PluggableSensor>(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700134 name, info->timeout, std::move(ri), std::move(wi));
Patrick Venturefe75b192018-06-08 11:19:43 -0700135 mgmr.addSensor(info->type, name, std::move(sensor));
Patrick Venture5e929092018-06-08 10:55:23 -0700136 }
137 else if (info->type == "temp" || info->type == "margin")
138 {
139 // These sensors are read-only, but only for this application
140 // which only writes to fan sensors.
Patrick Venture69c51062019-02-11 09:46:03 -0800141 std::cerr << info->type << " readPath: " << info->readPath << "\n";
Patrick Venture5e929092018-06-08 10:55:23 -0700142
143 if (IOInterfaceType::EXTERNAL == rtype)
144 {
145 std::cerr << "Creating HostSensor: " << name
Patrick Venture69c51062019-02-11 09:46:03 -0800146 << " path: " << info->readPath << "\n";
Patrick Venture5e929092018-06-08 10:55:23 -0700147
148 /*
149 * The reason we handle this as a HostSensor is because it's
150 * not quite pluggable; but maybe it could be.
151 */
Patrick Venture563a3562018-10-30 09:31:26 -0700152 auto sensor = HostSensor::createTemp(
Patrick Venture69c51062019-02-11 09:46:03 -0800153 name, info->timeout, hostSensorBus, info->readPath.c_str(),
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700154 deferSignals);
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
158 {
159 wi = std::make_unique<ReadOnlyNoExcept>();
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 }
164 }
165 }
166
167 return mgmr;
168}