xref: /openbmc/phosphor-pid-control/failsafeloggers/builder.cpp (revision f8b6e55147148c3cfb42327ff267197a460b411c)
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 "failsafeloggers/builder.hpp"
18 
19 #include "failsafeloggers/failsafe_logger.hpp"
20 #include "failsafeloggers/failsafe_logger_utility.hpp"
21 
22 #include <algorithm>
23 #include <cstddef>
24 #include <cstdint>
25 #include <iostream>
26 #include <memory>
27 #include <string>
28 #include <unordered_map>
29 #include <vector>
30 
31 namespace pid_control
32 {
33 
buildFailsafeLoggers(const std::unordered_map<int64_t,std::shared_ptr<ZoneInterface>> & zones,const size_t logMaxCountPerSecond)34 void buildFailsafeLoggers(
35     const std::unordered_map<int64_t, std::shared_ptr<ZoneInterface>>& zones,
36     const size_t logMaxCountPerSecond /* = 20 */)
37 {
38     zoneIdToFailsafeLogger =
39         std::unordered_map<int64_t,
40                            std::shared_ptr<pid_control::FailsafeLogger>>();
41     sensorNameToZoneId =
42         std::unordered_map<std::string, std::vector<int64_t>>();
43     for (const auto& zoneIdToZone : zones)
44     {
45         int64_t zoneId = zoneIdToZone.first;
46         // Create a failsafe logger for each zone.
47         zoneIdToFailsafeLogger[zoneId] = std::make_shared<FailsafeLogger>(
48             logMaxCountPerSecond, zoneIdToZone.second->getFailSafeMode());
49         // Build the sensor-zone topology map.
50         std::vector<std::string> sensorNames =
51             zoneIdToZone.second->getSensorNames();
52         for (const std::string& sensorName : sensorNames)
53         {
54             if (std::find(sensorNameToZoneId[sensorName].begin(),
55                           sensorNameToZoneId[sensorName].end(), zoneId) ==
56                 sensorNameToZoneId[sensorName].end())
57             {
58                 sensorNameToZoneId[sensorName].push_back(zoneId);
59             }
60         }
61         std::cerr << "Build failsafe logger for Zone " << zoneId
62                   << " with initial "
63                   << "failsafe mode: " << zoneIdToZone.second->getFailSafeMode()
64                   << "\n";
65     }
66 }
67 
68 } // namespace pid_control
69