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 <string> 20 21 /* Configuration. */ 22 #include "conf.hpp" 23 #include "dbus/dbuspassive.hpp" 24 #include "dbus/dbuswrite.hpp" 25 #include "interfaces.hpp" 26 #include "notimpl/readonly.hpp" 27 #include "notimpl/writeonly.hpp" 28 #include "sensors/builder.hpp" 29 #include "sensors/host.hpp" 30 #include "sensors/manager.hpp" 31 #include "sensors/pluggable.hpp" 32 #include "sysfs/sysfsread.hpp" 33 #include "sysfs/sysfswrite.hpp" 34 #include "util.hpp" 35 36 static constexpr bool deferSignals = true; 37 static DbusHelper helper; 38 39 SensorManager 40 buildSensors(const std::map<std::string, struct SensorConfig>& config) 41 { 42 SensorManager mgmr; 43 auto& HostSensorBus = mgmr.getHostBus(); 44 auto& PassiveListeningBus = mgmr.getPassiveBus(); 45 46 for (const auto& it : config) 47 { 48 std::unique_ptr<ReadInterface> ri; 49 std::unique_ptr<WriteInterface> wi; 50 51 std::string name = it.first; 52 const struct SensorConfig* info = &it.second; 53 54 std::cerr << "Sensor: " << name << " " << info->type << " "; 55 std::cerr << info->readpath << " " << info->writepath << "\n"; 56 57 IOInterfaceType rtype = getReadInterfaceType(info->readpath); 58 IOInterfaceType wtype = getWriteInterfaceType(info->writepath); 59 60 // fan sensors can be ready any way and written others. 61 // fan sensors are the only sensors this is designed to write. 62 // Nothing here should be write-only, although, in theory a fan could 63 // be. I'm just not sure how that would fit together. 64 // 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: 69 ri = DbusPassive::createDbusPassive(PassiveListeningBus, 70 info->type, name, &helper); 71 /* TODO(venture): if this returns nullptr */ 72 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>( 92 info->writepath, info->min, info->max); 93 } 94 else 95 { 96 wi = std::make_unique<SysFsWrite>(info->writepath, 97 info->min, info->max); 98 } 99 100 break; 101 case IOInterfaceType::DBUSACTIVE: 102 if (info->max > 0) 103 { 104 wi = std::make_unique<DbusWritePercent>( 105 info->writepath, info->min, info->max, helper); 106 } 107 else 108 { 109 wi = std::make_unique<DbusWrite>( 110 info->writepath, info->min, info->max, helper); 111 } 112 113 break; 114 default: 115 wi = std::make_unique<ReadOnlyNoExcept>(); 116 break; 117 } 118 119 auto sensor = std::make_unique<PluggableSensor>( 120 name, info->timeout, std::move(ri), std::move(wi)); 121 mgmr.addSensor(info->type, name, std::move(sensor)); 122 } 123 else if (info->type == "temp" || info->type == "margin") 124 { 125 // These sensors are read-only, but only for this application 126 // which only writes to fan sensors. 127 std::cerr << info->type << " readpath: " << info->readpath << "\n"; 128 129 if (IOInterfaceType::EXTERNAL == rtype) 130 { 131 std::cerr << "Creating HostSensor: " << name 132 << " path: " << info->readpath << "\n"; 133 134 /* 135 * The reason we handle this as a HostSensor is because it's 136 * not quite pluggable; but maybe it could be. 137 */ 138 auto sensor = HostSensor::createTemp( 139 name, info->timeout, HostSensorBus, info->readpath.c_str(), 140 deferSignals); 141 mgmr.addSensor(info->type, name, std::move(sensor)); 142 } 143 else 144 { 145 wi = std::make_unique<ReadOnlyNoExcept>(); 146 auto sensor = std::make_unique<PluggableSensor>( 147 name, info->timeout, std::move(ri), std::move(wi)); 148 mgmr.addSensor(info->type, name, std::move(sensor)); 149 } 150 } 151 } 152 153 return mgmr; 154 } 155