blob: da2f5ee9a039485b6288aa22ce67a18a67150aad [file] [log] [blame]
Shawn McCarneydb0b8332020-04-06 14:13:04 -05001/**
2 * Copyright © 2020 IBM Corporation
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 "device.hpp"
18
Shawn McCarneyeb7bec42020-04-14 09:38:15 -050019#include "chassis.hpp"
Shawn McCarney81a2f902021-03-23 21:41:34 -050020#include "error_logging_utils.hpp"
Shawn McCarneyb4d18a42020-06-02 10:27:05 -050021#include "exception_utils.hpp"
Shawn McCarneyeb7bec42020-04-14 09:38:15 -050022#include "system.hpp"
23
Shawn McCarneyb4d18a42020-06-02 10:27:05 -050024#include <exception>
25
Shawn McCarneydb0b8332020-04-06 14:13:04 -050026namespace phosphor::power::regulators
27{
28
29void Device::addToIDMap(IDMap& idMap)
30{
31 // Add this device to the map
32 idMap.addDevice(*this);
33
34 // Add rails to the map
35 for (std::unique_ptr<Rail>& rail : rails)
36 {
37 idMap.addRail(*rail);
38 }
39}
40
Shawn McCarney9bd94d32021-01-25 19:40:42 -060041void Device::clearCache()
42{
43 // If presence detection is defined for this device
44 if (presenceDetection)
45 {
46 // Clear cached presence data
47 presenceDetection->clearCache();
48 }
49}
50
Shawn McCarney371e2442021-05-14 14:18:07 -050051void Device::clearErrorHistory()
52{
53 // Clear error history in each rail
54 for (std::unique_ptr<Rail>& rail : rails)
55 {
56 rail->clearErrorHistory();
57 }
58}
59
Bob Kingd692d6d2020-09-14 13:42:57 +080060void Device::close(Services& services)
Shawn McCarneyb4d18a42020-06-02 10:27:05 -050061{
62 try
63 {
64 // Close I2C interface if it is open
65 if (i2cInterface->isOpen())
66 {
67 i2cInterface->close();
68 }
69 }
70 catch (const std::exception& e)
71 {
72 // Log error messages in journal
Bob Kingd692d6d2020-09-14 13:42:57 +080073 services.getJournal().logError(exception_utils::getMessages(e));
74 services.getJournal().logError("Unable to close device " + id);
Shawn McCarneyb4d18a42020-06-02 10:27:05 -050075
Shawn McCarney81a2f902021-03-23 21:41:34 -050076 // Create error log entry
77 error_logging_utils::logError(std::current_exception(),
78 Entry::Level::Notice, services);
Shawn McCarneyb4d18a42020-06-02 10:27:05 -050079 }
80}
81
Bob King23243f82020-07-29 10:38:57 +080082void Device::configure(Services& services, System& system, Chassis& chassis)
Shawn McCarneyeb7bec42020-04-14 09:38:15 -050083{
Shawn McCarney48033bf2021-01-27 17:56:49 -060084 // Verify device is present
85 if (isPresent(services, system, chassis))
Shawn McCarneyeb7bec42020-04-14 09:38:15 -050086 {
Shawn McCarney48033bf2021-01-27 17:56:49 -060087 // If configuration changes are defined for this device, apply them
88 if (configuration)
89 {
90 configuration->execute(services, system, chassis, *this);
91 }
Shawn McCarneyeb7bec42020-04-14 09:38:15 -050092
Shawn McCarney48033bf2021-01-27 17:56:49 -060093 // Configure rails
94 for (std::unique_ptr<Rail>& rail : rails)
95 {
96 rail->configure(services, system, chassis, *this);
97 }
Shawn McCarneyeb7bec42020-04-14 09:38:15 -050098 }
99}
100
Bob King8a552922020-08-05 17:02:31 +0800101void Device::monitorSensors(Services& services, System& system,
102 Chassis& chassis)
Bob King8e1cd0b2020-07-08 13:30:27 +0800103{
Shawn McCarney48033bf2021-01-27 17:56:49 -0600104 // Verify device is present
105 if (isPresent(services, system, chassis))
Bob King8e1cd0b2020-07-08 13:30:27 +0800106 {
Shawn McCarney48033bf2021-01-27 17:56:49 -0600107 // Monitor sensors in each rail
108 for (std::unique_ptr<Rail>& rail : rails)
109 {
110 rail->monitorSensors(services, system, chassis, *this);
111 }
Bob King8e1cd0b2020-07-08 13:30:27 +0800112 }
113}
114
Shawn McCarneydb0b8332020-04-06 14:13:04 -0500115} // namespace phosphor::power::regulators