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> 18 #include <map> 19 #include <memory> 20 #include <string> 21 22 /* Configuration. */ 23 #include "conf.hpp" 24 #include "dbus/dbushelper.hpp" 25 #include "dbus/dbuspassive.hpp" 26 #include "dbus/dbuswrite.hpp" 27 #include "errors/exception.hpp" 28 #include "interfaces.hpp" 29 #include "notimpl/readonly.hpp" 30 #include "notimpl/writeonly.hpp" 31 #include "sensors/build_utils.hpp" 32 #include "sensors/builder.hpp" 33 #include "sensors/host.hpp" 34 #include "sensors/manager.hpp" 35 #include "sensors/pluggable.hpp" 36 #include "sysfs/sysfsread.hpp" 37 #include "sysfs/sysfswrite.hpp" 38 #include "util.hpp" 39 40 namespace pid_control 41 { 42 43 static constexpr bool deferSignals = true; 44 45 SensorManager 46 buildSensors(const std::map<std::string, conf::SensorConfig>& config, 47 sdbusplus::bus_t& passive, sdbusplus::bus_t& host) 48 { 49 SensorManager mgmr(passive, host); 50 auto& hostSensorBus = mgmr.getHostBus(); 51 auto& passiveListeningBus = mgmr.getPassiveBus(); 52 53 for (const auto& it : config) 54 { 55 std::unique_ptr<ReadInterface> ri; 56 std::unique_ptr<WriteInterface> wi; 57 58 std::string name = it.first; 59 const conf::SensorConfig* info = &it.second; 60 61 std::cerr << "Sensor: " << name << " " << info->type << " "; 62 std::cerr << info->readPath << " " << info->writePath << "\n"; 63 64 IOInterfaceType rtype = getReadInterfaceType(info->readPath); 65 IOInterfaceType wtype = getWriteInterfaceType(info->writePath); 66 67 // fan sensors can be ready any way and written others. 68 // fan sensors are the only sensors this is designed to write. 69 // Nothing here should be write-only, although, in theory a fan could 70 // be. I'm just not sure how that would fit together. 71 // 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: 76 // 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, 85 std::make_unique<DbusHelper>( 86 sdbusplus::bus::new_system()), 87 info, redundancy); 88 } 89 else 90 { 91 ri = DbusPassive::createDbusPassive( 92 passiveListeningBus, info->type, name, 93 std::make_unique<DbusHelper>( 94 sdbusplus::bus::new_system()), 95 info, nullptr); 96 } 97 if (ri == nullptr) 98 { 99 throw SensorBuildException( 100 "Failed to create dbus passive sensor: " + name + 101 " of type: " + info->type); 102 } 103 break; 104 case IOInterfaceType::EXTERNAL: 105 // These are a special case for read-only. 106 break; 107 case IOInterfaceType::SYSFS: 108 ri = std::make_unique<SysFsRead>(info->readPath); 109 break; 110 default: 111 ri = std::make_unique<WriteOnly>(); 112 break; 113 } 114 115 if (info->type == "fan") 116 { 117 switch (wtype) 118 { 119 case IOInterfaceType::SYSFS: 120 if (info->max > 0) 121 { 122 wi = std::make_unique<SysFsWritePercent>( 123 info->writePath, info->min, info->max); 124 } 125 else 126 { 127 wi = std::make_unique<SysFsWrite>(info->writePath, 128 info->min, info->max); 129 } 130 131 break; 132 case IOInterfaceType::DBUSACTIVE: 133 if (info->max > 0) 134 { 135 wi = DbusWritePercent::createDbusWrite( 136 info->writePath, info->min, info->max, 137 std::make_unique<DbusHelper>( 138 sdbusplus::bus::new_system())); 139 } 140 else 141 { 142 wi = DbusWrite::createDbusWrite( 143 info->writePath, info->min, info->max, 144 std::make_unique<DbusHelper>( 145 sdbusplus::bus::new_system())); 146 } 147 148 if (wi == nullptr) 149 { 150 throw SensorBuildException( 151 "Unable to create write dbus interface for path: " + 152 info->writePath); 153 } 154 155 break; 156 default: 157 wi = std::make_unique<ReadOnlyNoExcept>(); 158 break; 159 } 160 161 auto sensor = std::make_unique<PluggableSensor>( 162 name, info->timeout, std::move(ri), std::move(wi)); 163 mgmr.addSensor(info->type, name, std::move(sensor)); 164 } 165 else if (info->type == "temp" || info->type == "margin") 166 { 167 // These sensors are read-only, but only for this application 168 // which only writes to fan sensors. 169 std::cerr << info->type << " readPath: " << info->readPath << "\n"; 170 171 if (IOInterfaceType::EXTERNAL == rtype) 172 { 173 std::cerr << "Creating HostSensor: " << name 174 << " path: " << info->readPath << "\n"; 175 176 /* 177 * The reason we handle this as a HostSensor is because it's 178 * not quite pluggable; but maybe it could be. 179 */ 180 auto sensor = HostSensor::createTemp( 181 name, info->timeout, hostSensorBus, info->readPath.c_str(), 182 deferSignals); 183 mgmr.addSensor(info->type, name, std::move(sensor)); 184 } 185 else 186 { 187 wi = std::make_unique<ReadOnlyNoExcept>(); 188 auto sensor = std::make_unique<PluggableSensor>( 189 name, info->timeout, std::move(ri), std::move(wi)); 190 mgmr.addSensor(info->type, name, std::move(sensor)); 191 } 192 } 193 } 194 195 return mgmr; 196 } 197 198 } // namespace pid_control 199