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 = DbusWritePercent::createDbusWrite(
105                             info->writepath, info->min, info->max, helper);
106                         /* TODO: handle this being nullptr. */
107                     }
108                     else
109                     {
110                         wi = DbusWrite::createDbusWrite(
111                             info->writepath, info->min, info->max, helper);
112                         /* TODO: handle this being nullptr. */
113                     }
114 
115                     break;
116                 default:
117                     wi = std::make_unique<ReadOnlyNoExcept>();
118                     break;
119             }
120 
121             auto sensor = std::make_unique<PluggableSensor>(
122                 name, info->timeout, std::move(ri), std::move(wi));
123             mgmr.addSensor(info->type, name, std::move(sensor));
124         }
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                  */
140                 auto sensor = HostSensor::createTemp(
141                     name, info->timeout, HostSensorBus, info->readpath.c_str(),
142                     deferSignals);
143                 mgmr.addSensor(info->type, name, std::move(sensor));
144             }
145             else
146             {
147                 wi = std::make_unique<ReadOnlyNoExcept>();
148                 auto sensor = std::make_unique<PluggableSensor>(
149                     name, info->timeout, std::move(ri), std::move(wi));
150                 mgmr.addSensor(info->type, name, std::move(sensor));
151             }
152         }
153     }
154 
155     return mgmr;
156 }
157