blob: 0f2f0c703df8cfa77f8283065380b416c8f1aff3 [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 Venture5e929092018-06-08 10:55:23 -070025#include "interfaces.hpp"
26#include "notimpl/readonly.hpp"
27#include "notimpl/writeonly.hpp"
28#include "sensors/builder.hpp"
Patrick Venture5e929092018-06-08 10:55:23 -070029#include "sensors/host.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070030#include "sensors/manager.hpp"
Patrick Venture5e929092018-06-08 10:55:23 -070031#include "sensors/pluggable.hpp"
32#include "sysfs/sysfsread.hpp"
33#include "sysfs/sysfswrite.hpp"
34#include "util.hpp"
35
36static constexpr bool deferSignals = true;
Patrick Venture0df7c0f2018-06-13 09:02:13 -070037static DbusHelper helper;
Patrick Venture5e929092018-06-08 10:55:23 -070038
Patrick Venturef3252312018-10-30 08:42:53 -070039SensorManager
Patrick Venture7af157b2018-10-30 11:24:40 -070040 buildSensors(const std::map<std::string, struct SensorConfig>& config)
Patrick Venture5e929092018-06-08 10:55:23 -070041{
Patrick Venturefe75b192018-06-08 11:19:43 -070042 SensorManager mgmr;
43 auto& HostSensorBus = mgmr.getHostBus();
44 auto& PassiveListeningBus = mgmr.getPassiveBus();
Patrick Venture5e929092018-06-08 10:55:23 -070045
Patrick Venture4a2dc4d2018-10-23 09:02:55 -070046 for (const auto& it : config)
Patrick Venture5e929092018-06-08 10:55:23 -070047 {
48 std::unique_ptr<ReadInterface> ri;
49 std::unique_ptr<WriteInterface> wi;
50
51 std::string name = it.first;
Patrick Venturef3252312018-10-30 08:42:53 -070052 const struct SensorConfig* info = &it.second;
Patrick Venture5e929092018-06-08 10:55:23 -070053
54 std::cerr << "Sensor: " << name << " " << info->type << " ";
55 std::cerr << info->readpath << " " << info->writepath << "\n";
56
Patrick Venture7af157b2018-10-30 11:24:40 -070057 IOInterfaceType rtype = getReadInterfaceType(info->readpath);
58 IOInterfaceType wtype = getWriteInterfaceType(info->writepath);
Patrick Venture5e929092018-06-08 10:55:23 -070059
60 // fan sensors can be ready any way and written others.
61 // fan sensors are the only sensors this is designed to write.
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070062 // Nothing here should be write-only, although, in theory a fan could
63 // be. I'm just not sure how that would fit together.
Patrick Venture5e929092018-06-08 10:55:23 -070064 // TODO(venture): It should check with the ObjectMapper to check if
65 // that sensor exists on the Dbus.
66 switch (rtype)
67 {
68 case IOInterfaceType::DBUSPASSIVE:
Patrick Venture563a3562018-10-30 09:31:26 -070069 ri = DbusPassive::createDbusPassive(PassiveListeningBus,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070070 info->type, name, &helper);
Patrick Venture0ef1faf2018-06-13 12:50:53 -070071 /* TODO(venture): if this returns nullptr */
Patrick Venture5e929092018-06-08 10:55:23 -070072 break;
73 case IOInterfaceType::EXTERNAL:
74 // These are a special case for read-only.
75 break;
76 case IOInterfaceType::SYSFS:
77 ri = std::make_unique<SysFsRead>(info->readpath);
78 break;
79 default:
80 ri = std::make_unique<WriteOnly>();
81 break;
82 }
83
84 if (info->type == "fan")
85 {
86 switch (wtype)
87 {
88 case IOInterfaceType::SYSFS:
89 if (info->max > 0)
90 {
91 wi = std::make_unique<SysFsWritePercent>(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070092 info->writepath, info->min, info->max);
Patrick Venture5e929092018-06-08 10:55:23 -070093 }
94 else
95 {
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070096 wi = std::make_unique<SysFsWrite>(info->writepath,
97 info->min, info->max);
Patrick Venture5e929092018-06-08 10:55:23 -070098 }
99
100 break;
James Feist7136a5a2018-07-19 09:52:05 -0700101 case IOInterfaceType::DBUSACTIVE:
102 if (info->max > 0)
103 {
Patrick Venturef5e770b2018-10-30 12:28:53 -0700104 wi = DbusWritePercent::createDbusWrite(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700105 info->writepath, info->min, info->max, helper);
Patrick Venturef5e770b2018-10-30 12:28:53 -0700106 /* TODO: handle this being nullptr. */
James Feist7136a5a2018-07-19 09:52:05 -0700107 }
108 else
109 {
Patrick Venturef5e770b2018-10-30 12:28:53 -0700110 wi = DbusWrite::createDbusWrite(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700111 info->writepath, info->min, info->max, helper);
Patrick Venturef5e770b2018-10-30 12:28:53 -0700112 /* TODO: handle this being nullptr. */
James Feist7136a5a2018-07-19 09:52:05 -0700113 }
114
115 break;
Patrick Venture5e929092018-06-08 10:55:23 -0700116 default:
117 wi = std::make_unique<ReadOnlyNoExcept>();
118 break;
119 }
120
121 auto sensor = std::make_unique<PluggableSensor>(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700122 name, info->timeout, std::move(ri), std::move(wi));
Patrick Venturefe75b192018-06-08 11:19:43 -0700123 mgmr.addSensor(info->type, name, std::move(sensor));
Patrick Venture5e929092018-06-08 10:55:23 -0700124 }
125 else if (info->type == "temp" || info->type == "margin")
126 {
127 // These sensors are read-only, but only for this application
128 // which only writes to fan sensors.
129 std::cerr << info->type << " readpath: " << info->readpath << "\n";
130
131 if (IOInterfaceType::EXTERNAL == rtype)
132 {
133 std::cerr << "Creating HostSensor: " << name
134 << " path: " << info->readpath << "\n";
135
136 /*
137 * The reason we handle this as a HostSensor is because it's
138 * not quite pluggable; but maybe it could be.
139 */
Patrick Venture563a3562018-10-30 09:31:26 -0700140 auto sensor = HostSensor::createTemp(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700141 name, info->timeout, HostSensorBus, info->readpath.c_str(),
142 deferSignals);
Patrick Venturefe75b192018-06-08 11:19:43 -0700143 mgmr.addSensor(info->type, name, std::move(sensor));
Patrick Venture5e929092018-06-08 10:55:23 -0700144 }
145 else
146 {
147 wi = std::make_unique<ReadOnlyNoExcept>();
148 auto sensor = std::make_unique<PluggableSensor>(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700149 name, info->timeout, std::move(ri), std::move(wi));
Patrick Venturefe75b192018-06-08 11:19:43 -0700150 mgmr.addSensor(info->type, name, std::move(sensor));
Patrick Venture5e929092018-06-08 10:55:23 -0700151 }
152 }
153 }
154
155 return mgmr;
156}