blob: 966a581c05da50bb412eb9216188810c8c1d012f [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
Patrick Venture7af157b2018-10-30 11:24:40 -070041 buildSensors(const std::map<std::string, struct SensorConfig>& config)
Patrick Venture5e929092018-06-08 10:55:23 -070042{
Patrick Venturefe75b192018-06-08 11:19:43 -070043 SensorManager mgmr;
Patrick Venturec179d402018-10-30 19:51:55 -070044 auto& hostSensorBus = mgmr.getHostBus();
45 auto& passiveListeningBus = mgmr.getPassiveBus();
Patrick Venture5e929092018-06-08 10:55:23 -070046
Patrick Venture4a2dc4d2018-10-23 09:02:55 -070047 for (const auto& it : config)
Patrick Venture5e929092018-06-08 10:55:23 -070048 {
49 std::unique_ptr<ReadInterface> ri;
50 std::unique_ptr<WriteInterface> wi;
51
52 std::string name = it.first;
Patrick Venturef3252312018-10-30 08:42:53 -070053 const struct SensorConfig* info = &it.second;
Patrick Venture5e929092018-06-08 10:55:23 -070054
55 std::cerr << "Sensor: " << name << " " << info->type << " ";
56 std::cerr << info->readpath << " " << info->writepath << "\n";
57
Patrick Venture7af157b2018-10-30 11:24:40 -070058 IOInterfaceType rtype = getReadInterfaceType(info->readpath);
59 IOInterfaceType wtype = getWriteInterfaceType(info->writepath);
Patrick Venture5e929092018-06-08 10:55:23 -070060
61 // fan sensors can be ready any way and written others.
62 // fan sensors are the only sensors this is designed to write.
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070063 // Nothing here should be write-only, although, in theory a fan could
64 // be. I'm just not sure how that would fit together.
Patrick Venture5e929092018-06-08 10:55:23 -070065 // TODO(venture): It should check with the ObjectMapper to check if
66 // that sensor exists on the Dbus.
67 switch (rtype)
68 {
69 case IOInterfaceType::DBUSPASSIVE:
Patrick Venturec179d402018-10-30 19:51:55 -070070 ri = DbusPassive::createDbusPassive(passiveListeningBus,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070071 info->type, name, &helper);
Patrick Venturee7252862018-10-30 14:23:32 -070072 if (ri == nullptr)
73 {
74 throw SensorBuildException(
75 "Failed to create dbus passive sensor: " + name +
76 " of type: " + info->type);
77 }
Patrick Venture5e929092018-06-08 10:55:23 -070078 break;
79 case IOInterfaceType::EXTERNAL:
80 // These are a special case for read-only.
81 break;
82 case IOInterfaceType::SYSFS:
83 ri = std::make_unique<SysFsRead>(info->readpath);
84 break;
85 default:
86 ri = std::make_unique<WriteOnly>();
87 break;
88 }
89
90 if (info->type == "fan")
91 {
92 switch (wtype)
93 {
94 case IOInterfaceType::SYSFS:
95 if (info->max > 0)
96 {
97 wi = std::make_unique<SysFsWritePercent>(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070098 info->writepath, info->min, info->max);
Patrick Venture5e929092018-06-08 10:55:23 -070099 }
100 else
101 {
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700102 wi = std::make_unique<SysFsWrite>(info->writepath,
103 info->min, info->max);
Patrick Venture5e929092018-06-08 10:55:23 -0700104 }
105
106 break;
James Feist7136a5a2018-07-19 09:52:05 -0700107 case IOInterfaceType::DBUSACTIVE:
108 if (info->max > 0)
109 {
Patrick Venturef5e770b2018-10-30 12:28:53 -0700110 wi = DbusWritePercent::createDbusWrite(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700111 info->writepath, info->min, info->max, helper);
James Feist7136a5a2018-07-19 09:52:05 -0700112 }
113 else
114 {
Patrick Venturef5e770b2018-10-30 12:28:53 -0700115 wi = DbusWrite::createDbusWrite(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700116 info->writepath, info->min, info->max, helper);
Patrick Venturee7252862018-10-30 14:23:32 -0700117 }
118
119 if (wi == nullptr)
120 {
121 throw SensorBuildException(
122 "Unable to create write dbus interface for path: " +
123 info->writepath);
James Feist7136a5a2018-07-19 09:52:05 -0700124 }
125
126 break;
Patrick Venture5e929092018-06-08 10:55:23 -0700127 default:
128 wi = std::make_unique<ReadOnlyNoExcept>();
129 break;
130 }
131
132 auto sensor = std::make_unique<PluggableSensor>(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700133 name, info->timeout, std::move(ri), std::move(wi));
Patrick Venturefe75b192018-06-08 11:19:43 -0700134 mgmr.addSensor(info->type, name, std::move(sensor));
Patrick Venture5e929092018-06-08 10:55:23 -0700135 }
136 else if (info->type == "temp" || info->type == "margin")
137 {
138 // These sensors are read-only, but only for this application
139 // which only writes to fan sensors.
140 std::cerr << info->type << " readpath: " << info->readpath << "\n";
141
142 if (IOInterfaceType::EXTERNAL == rtype)
143 {
144 std::cerr << "Creating HostSensor: " << name
145 << " path: " << info->readpath << "\n";
146
147 /*
148 * The reason we handle this as a HostSensor is because it's
149 * not quite pluggable; but maybe it could be.
150 */
Patrick Venture563a3562018-10-30 09:31:26 -0700151 auto sensor = HostSensor::createTemp(
Patrick Venturec179d402018-10-30 19:51:55 -0700152 name, info->timeout, hostSensorBus, info->readpath.c_str(),
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700153 deferSignals);
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
157 {
158 wi = std::make_unique<ReadOnlyNoExcept>();
159 auto sensor = std::make_unique<PluggableSensor>(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700160 name, info->timeout, std::move(ri), std::move(wi));
Patrick Venturefe75b192018-06-08 11:19:43 -0700161 mgmr.addSensor(info->type, name, std::move(sensor));
Patrick Venture5e929092018-06-08 10:55:23 -0700162 }
163 }
164 }
165
166 return mgmr;
167}