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