Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 1 | /** |
| 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 Venture | fe75b19 | 2018-06-08 11:19:43 -0700 | [diff] [blame] | 18 | #include <map> |
| 19 | #include <string> |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 20 | |
| 21 | /* Configuration. */ |
| 22 | #include "conf.hpp" |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 23 | #include "dbus/dbuspassive.hpp" |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 24 | #include "dbus/dbuswrite.hpp" |
Patrick Venture | c404b3e | 2018-10-30 14:17:49 -0700 | [diff] [blame^] | 25 | #include "errors/exception.hpp" |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 26 | #include "interfaces.hpp" |
| 27 | #include "notimpl/readonly.hpp" |
| 28 | #include "notimpl/writeonly.hpp" |
| 29 | #include "sensors/builder.hpp" |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 30 | #include "sensors/host.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 31 | #include "sensors/manager.hpp" |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 32 | #include "sensors/pluggable.hpp" |
| 33 | #include "sysfs/sysfsread.hpp" |
| 34 | #include "sysfs/sysfswrite.hpp" |
| 35 | #include "util.hpp" |
| 36 | |
| 37 | static constexpr bool deferSignals = true; |
Patrick Venture | 0df7c0f | 2018-06-13 09:02:13 -0700 | [diff] [blame] | 38 | static DbusHelper helper; |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 39 | |
Patrick Venture | f325231 | 2018-10-30 08:42:53 -0700 | [diff] [blame] | 40 | SensorManager |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 41 | buildSensors(const std::map<std::string, struct SensorConfig>& config) |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 42 | { |
Patrick Venture | fe75b19 | 2018-06-08 11:19:43 -0700 | [diff] [blame] | 43 | SensorManager mgmr; |
| 44 | auto& HostSensorBus = mgmr.getHostBus(); |
| 45 | auto& PassiveListeningBus = mgmr.getPassiveBus(); |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 46 | |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 47 | for (const auto& it : config) |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 48 | { |
| 49 | std::unique_ptr<ReadInterface> ri; |
| 50 | std::unique_ptr<WriteInterface> wi; |
| 51 | |
| 52 | std::string name = it.first; |
Patrick Venture | f325231 | 2018-10-30 08:42:53 -0700 | [diff] [blame] | 53 | const struct SensorConfig* info = &it.second; |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 54 | |
| 55 | std::cerr << "Sensor: " << name << " " << info->type << " "; |
| 56 | std::cerr << info->readpath << " " << info->writepath << "\n"; |
| 57 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 58 | IOInterfaceType rtype = getReadInterfaceType(info->readpath); |
| 59 | IOInterfaceType wtype = getWriteInterfaceType(info->writepath); |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 60 | |
| 61 | // fan sensors can be ready any way and written others. |
| 62 | // fan sensors are the only sensors this is designed to write. |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 63 | // 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 Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 65 | // 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 Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 70 | ri = DbusPassive::createDbusPassive(PassiveListeningBus, |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 71 | info->type, name, &helper); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 72 | /* TODO(venture): if this returns nullptr */ |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 73 | break; |
| 74 | case IOInterfaceType::EXTERNAL: |
| 75 | // These are a special case for read-only. |
| 76 | break; |
| 77 | case IOInterfaceType::SYSFS: |
| 78 | ri = std::make_unique<SysFsRead>(info->readpath); |
| 79 | break; |
| 80 | default: |
| 81 | ri = std::make_unique<WriteOnly>(); |
| 82 | break; |
| 83 | } |
| 84 | |
| 85 | if (info->type == "fan") |
| 86 | { |
| 87 | switch (wtype) |
| 88 | { |
| 89 | case IOInterfaceType::SYSFS: |
| 90 | if (info->max > 0) |
| 91 | { |
| 92 | wi = std::make_unique<SysFsWritePercent>( |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 93 | info->writepath, info->min, info->max); |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 94 | } |
| 95 | else |
| 96 | { |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 97 | wi = std::make_unique<SysFsWrite>(info->writepath, |
| 98 | info->min, info->max); |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | break; |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 102 | case IOInterfaceType::DBUSACTIVE: |
| 103 | if (info->max > 0) |
| 104 | { |
Patrick Venture | f5e770b | 2018-10-30 12:28:53 -0700 | [diff] [blame] | 105 | wi = DbusWritePercent::createDbusWrite( |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 106 | info->writepath, info->min, info->max, helper); |
Patrick Venture | f5e770b | 2018-10-30 12:28:53 -0700 | [diff] [blame] | 107 | /* TODO: handle this being nullptr. */ |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 108 | } |
| 109 | else |
| 110 | { |
Patrick Venture | f5e770b | 2018-10-30 12:28:53 -0700 | [diff] [blame] | 111 | wi = DbusWrite::createDbusWrite( |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 112 | info->writepath, info->min, info->max, helper); |
Patrick Venture | f5e770b | 2018-10-30 12:28:53 -0700 | [diff] [blame] | 113 | /* TODO: handle this being nullptr. */ |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | break; |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 117 | default: |
| 118 | wi = std::make_unique<ReadOnlyNoExcept>(); |
| 119 | break; |
| 120 | } |
| 121 | |
| 122 | auto sensor = std::make_unique<PluggableSensor>( |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 123 | name, info->timeout, std::move(ri), std::move(wi)); |
Patrick Venture | fe75b19 | 2018-06-08 11:19:43 -0700 | [diff] [blame] | 124 | mgmr.addSensor(info->type, name, std::move(sensor)); |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 125 | } |
| 126 | else if (info->type == "temp" || info->type == "margin") |
| 127 | { |
| 128 | // These sensors are read-only, but only for this application |
| 129 | // which only writes to fan sensors. |
| 130 | std::cerr << info->type << " readpath: " << info->readpath << "\n"; |
| 131 | |
| 132 | if (IOInterfaceType::EXTERNAL == rtype) |
| 133 | { |
| 134 | std::cerr << "Creating HostSensor: " << name |
| 135 | << " path: " << info->readpath << "\n"; |
| 136 | |
| 137 | /* |
| 138 | * The reason we handle this as a HostSensor is because it's |
| 139 | * not quite pluggable; but maybe it could be. |
| 140 | */ |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 141 | auto sensor = HostSensor::createTemp( |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 142 | name, info->timeout, HostSensorBus, info->readpath.c_str(), |
| 143 | deferSignals); |
Patrick Venture | fe75b19 | 2018-06-08 11:19:43 -0700 | [diff] [blame] | 144 | mgmr.addSensor(info->type, name, std::move(sensor)); |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 145 | } |
| 146 | else |
| 147 | { |
| 148 | wi = std::make_unique<ReadOnlyNoExcept>(); |
| 149 | auto sensor = std::make_unique<PluggableSensor>( |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 150 | name, info->timeout, std::move(ri), std::move(wi)); |
Patrick Venture | fe75b19 | 2018-06-08 11:19:43 -0700 | [diff] [blame] | 151 | mgmr.addSensor(info->type, name, std::move(sensor)); |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | return mgmr; |
| 157 | } |