xref: /openbmc/phosphor-pid-control/sensors/builder.cpp (revision 0ef1faf70ba2af62260d99bb10ab5b674c0ebbd0)
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 static DbusHelper helper;
38 
39 SensorManager BuildSensors(
40     const std::map<std::string, struct sensor>& config)
41 {
42     SensorManager mgmr;
43     auto& HostSensorBus = mgmr.getHostBus();
44     auto& PassiveListeningBus = mgmr.getPassiveBus();
45 
46     for (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 sensor* 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 be.
63         // 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,
71                                                     name,
72                                                     &helper);
73                 /* TODO(venture): if this returns nullptr */
74                 break;
75             case IOInterfaceType::EXTERNAL:
76                 // These are a special case for read-only.
77                 break;
78             case IOInterfaceType::SYSFS:
79                 ri = std::make_unique<SysFsRead>(info->readpath);
80                 break;
81             default:
82                 ri = std::make_unique<WriteOnly>();
83                 break;
84         }
85 
86         if (info->type == "fan")
87         {
88             switch (wtype)
89             {
90                 case IOInterfaceType::SYSFS:
91                     if (info->max > 0)
92                     {
93                         wi = std::make_unique<SysFsWritePercent>(
94                                  info->writepath,
95                                  info->min,
96                                  info->max);
97                     }
98                     else
99                     {
100                         wi = std::make_unique<SysFsWrite>(
101                                  info->writepath,
102                                  info->min,
103                                  info->max);
104                     }
105 
106                     break;
107                 default:
108                     wi = std::make_unique<ReadOnlyNoExcept>();
109                     break;
110             }
111 
112             auto sensor = std::make_unique<PluggableSensor>(
113                               name,
114                               info->timeout,
115                               std::move(ri),
116                               std::move(wi));
117             mgmr.addSensor(info->type, name, std::move(sensor));
118         }
119         else if (info->type == "temp" || info->type == "margin")
120         {
121             // These sensors are read-only, but only for this application
122             // which only writes to fan sensors.
123             std::cerr << info->type << " readpath: " << info->readpath << "\n";
124 
125             if (IOInterfaceType::EXTERNAL == rtype)
126             {
127                 std::cerr << "Creating HostSensor: " << name
128                           << " path: " << info->readpath << "\n";
129 
130                 /*
131                  * The reason we handle this as a HostSensor is because it's
132                  * not quite pluggable; but maybe it could be.
133                  */
134                 auto sensor = HostSensor::CreateTemp(
135                                   name,
136                                   info->timeout,
137                                   HostSensorBus,
138                                   info->readpath.c_str(),
139                                   deferSignals);
140                 mgmr.addSensor(info->type, name, std::move(sensor));
141             }
142             else
143             {
144                 wi = std::make_unique<ReadOnlyNoExcept>();
145                 auto sensor = std::make_unique<PluggableSensor>(
146                                   name,
147                                   info->timeout,
148                                   std::move(ri),
149                                   std::move(wi));
150                 mgmr.addSensor(info->type, name, std::move(sensor));
151             }
152         }
153     }
154 
155     return mgmr;
156 }
157 
158