1db0b833cSShawn McCarney /**
2db0b833cSShawn McCarney * Copyright © 2020 IBM Corporation
3db0b833cSShawn McCarney *
4db0b833cSShawn McCarney * Licensed under the Apache License, Version 2.0 (the "License");
5db0b833cSShawn McCarney * you may not use this file except in compliance with the License.
6db0b833cSShawn McCarney * You may obtain a copy of the License at
7db0b833cSShawn McCarney *
8db0b833cSShawn McCarney * http://www.apache.org/licenses/LICENSE-2.0
9db0b833cSShawn McCarney *
10db0b833cSShawn McCarney * Unless required by applicable law or agreed to in writing, software
11db0b833cSShawn McCarney * distributed under the License is distributed on an "AS IS" BASIS,
12db0b833cSShawn McCarney * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13db0b833cSShawn McCarney * See the License for the specific language governing permissions and
14db0b833cSShawn McCarney * limitations under the License.
15db0b833cSShawn McCarney */
16db0b833cSShawn McCarney
17db0b833cSShawn McCarney #include "device.hpp"
18db0b833cSShawn McCarney
19eb7bec4aSShawn McCarney #include "chassis.hpp"
2081a2f90bSShawn McCarney #include "error_logging_utils.hpp"
21b4d18a45SShawn McCarney #include "exception_utils.hpp"
22eb7bec4aSShawn McCarney #include "system.hpp"
23eb7bec4aSShawn McCarney
24b4d18a45SShawn McCarney #include <exception>
25b4d18a45SShawn McCarney
26db0b833cSShawn McCarney namespace phosphor::power::regulators
27db0b833cSShawn McCarney {
28db0b833cSShawn McCarney
addToIDMap(IDMap & idMap)29db0b833cSShawn McCarney void Device::addToIDMap(IDMap& idMap)
30db0b833cSShawn McCarney {
31db0b833cSShawn McCarney // Add this device to the map
32db0b833cSShawn McCarney idMap.addDevice(*this);
33db0b833cSShawn McCarney
34db0b833cSShawn McCarney // Add rails to the map
35db0b833cSShawn McCarney for (std::unique_ptr<Rail>& rail : rails)
36db0b833cSShawn McCarney {
37db0b833cSShawn McCarney idMap.addRail(*rail);
38db0b833cSShawn McCarney }
39db0b833cSShawn McCarney }
40db0b833cSShawn McCarney
clearCache()419bd94d36SShawn McCarney void Device::clearCache()
429bd94d36SShawn McCarney {
439bd94d36SShawn McCarney // If presence detection is defined for this device
449bd94d36SShawn McCarney if (presenceDetection)
459bd94d36SShawn McCarney {
469bd94d36SShawn McCarney // Clear cached presence data
479bd94d36SShawn McCarney presenceDetection->clearCache();
489bd94d36SShawn McCarney }
499bd94d36SShawn McCarney }
509bd94d36SShawn McCarney
clearErrorHistory()51371e244aSShawn McCarney void Device::clearErrorHistory()
52371e244aSShawn McCarney {
53*2ee4494cSShawn McCarney // Clear error history in phase fault detection, if defined
54*2ee4494cSShawn McCarney if (phaseFaultDetection)
55*2ee4494cSShawn McCarney {
56*2ee4494cSShawn McCarney phaseFaultDetection->clearErrorHistory();
57*2ee4494cSShawn McCarney }
58*2ee4494cSShawn McCarney
59371e244aSShawn McCarney // Clear error history in each rail
60371e244aSShawn McCarney for (std::unique_ptr<Rail>& rail : rails)
61371e244aSShawn McCarney {
62371e244aSShawn McCarney rail->clearErrorHistory();
63371e244aSShawn McCarney }
64371e244aSShawn McCarney }
65371e244aSShawn McCarney
close(Services & services)66d692d6dfSBob King void Device::close(Services& services)
67b4d18a45SShawn McCarney {
68b4d18a45SShawn McCarney try
69b4d18a45SShawn McCarney {
70b4d18a45SShawn McCarney // Close I2C interface if it is open
71b4d18a45SShawn McCarney if (i2cInterface->isOpen())
72b4d18a45SShawn McCarney {
73b4d18a45SShawn McCarney i2cInterface->close();
74b4d18a45SShawn McCarney }
75b4d18a45SShawn McCarney }
76b4d18a45SShawn McCarney catch (const std::exception& e)
77b4d18a45SShawn McCarney {
78b4d18a45SShawn McCarney // Log error messages in journal
79d692d6dfSBob King services.getJournal().logError(exception_utils::getMessages(e));
80d692d6dfSBob King services.getJournal().logError("Unable to close device " + id);
81b4d18a45SShawn McCarney
8281a2f90bSShawn McCarney // Create error log entry
8381a2f90bSShawn McCarney error_logging_utils::logError(std::current_exception(),
8481a2f90bSShawn McCarney Entry::Level::Notice, services);
85b4d18a45SShawn McCarney }
86b4d18a45SShawn McCarney }
87b4d18a45SShawn McCarney
configure(Services & services,System & system,Chassis & chassis)8823243f84SBob King void Device::configure(Services& services, System& system, Chassis& chassis)
89eb7bec4aSShawn McCarney {
9048033bf6SShawn McCarney // Verify device is present
9148033bf6SShawn McCarney if (isPresent(services, system, chassis))
9248033bf6SShawn McCarney {
93eb7bec4aSShawn McCarney // If configuration changes are defined for this device, apply them
94eb7bec4aSShawn McCarney if (configuration)
95eb7bec4aSShawn McCarney {
9623243f84SBob King configuration->execute(services, system, chassis, *this);
97eb7bec4aSShawn McCarney }
98eb7bec4aSShawn McCarney
99eb7bec4aSShawn McCarney // Configure rails
100eb7bec4aSShawn McCarney for (std::unique_ptr<Rail>& rail : rails)
101eb7bec4aSShawn McCarney {
10223243f84SBob King rail->configure(services, system, chassis, *this);
103eb7bec4aSShawn McCarney }
104eb7bec4aSShawn McCarney }
10548033bf6SShawn McCarney }
106eb7bec4aSShawn McCarney
detectPhaseFaults(Services & services,System & system,Chassis & chassis)1071fd0b145SShawn McCarney void Device::detectPhaseFaults(Services& services, System& system,
1081fd0b145SShawn McCarney Chassis& chassis)
1091fd0b145SShawn McCarney {
1101fd0b145SShawn McCarney // Verify device is present
1111fd0b145SShawn McCarney if (isPresent(services, system, chassis))
1121fd0b145SShawn McCarney {
1131fd0b145SShawn McCarney // If phase fault detection is defined for this device, execute it
1141fd0b145SShawn McCarney if (phaseFaultDetection)
1151fd0b145SShawn McCarney {
1161fd0b145SShawn McCarney phaseFaultDetection->execute(services, system, chassis, *this);
1171fd0b145SShawn McCarney }
1181fd0b145SShawn McCarney }
1191fd0b145SShawn McCarney }
1201fd0b145SShawn McCarney
monitorSensors(Services & services,System & system,Chassis & chassis)1218a55292dSBob King void Device::monitorSensors(Services& services, System& system,
1228a55292dSBob King Chassis& chassis)
1238e1cd0b6SBob King {
12448033bf6SShawn McCarney // Verify device is present
12548033bf6SShawn McCarney if (isPresent(services, system, chassis))
12648033bf6SShawn McCarney {
1278e1cd0b6SBob King // Monitor sensors in each rail
1288e1cd0b6SBob King for (std::unique_ptr<Rail>& rail : rails)
1298e1cd0b6SBob King {
1308a55292dSBob King rail->monitorSensors(services, system, chassis, *this);
1318e1cd0b6SBob King }
1328e1cd0b6SBob King }
13348033bf6SShawn McCarney }
1348e1cd0b6SBob King
135db0b833cSShawn McCarney } // namespace phosphor::power::regulators
136