Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 1 | /** |
| 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 McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 19 | #include "chassis.hpp" |
Shawn McCarney | 81a2f90 | 2021-03-23 21:41:34 -0500 | [diff] [blame] | 20 | #include "error_logging_utils.hpp" |
Shawn McCarney | b4d18a4 | 2020-06-02 10:27:05 -0500 | [diff] [blame] | 21 | #include "exception_utils.hpp" |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 22 | #include "system.hpp" |
| 23 | |
Shawn McCarney | b4d18a4 | 2020-06-02 10:27:05 -0500 | [diff] [blame] | 24 | #include <exception> |
| 25 | |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 26 | namespace phosphor::power::regulators |
| 27 | { |
| 28 | |
| 29 | void 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 McCarney | 9bd94d3 | 2021-01-25 19:40:42 -0600 | [diff] [blame] | 41 | void 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 McCarney | 371e244 | 2021-05-14 14:18:07 -0500 | [diff] [blame] | 51 | void Device::clearErrorHistory() |
| 52 | { |
Shawn McCarney | 2ee4494 | 2021-09-09 17:58:14 -0500 | [diff] [blame] | 53 | // Clear error history in phase fault detection, if defined |
| 54 | if (phaseFaultDetection) |
| 55 | { |
| 56 | phaseFaultDetection->clearErrorHistory(); |
| 57 | } |
| 58 | |
Shawn McCarney | 371e244 | 2021-05-14 14:18:07 -0500 | [diff] [blame] | 59 | // Clear error history in each rail |
| 60 | for (std::unique_ptr<Rail>& rail : rails) |
| 61 | { |
| 62 | rail->clearErrorHistory(); |
| 63 | } |
| 64 | } |
| 65 | |
Bob King | d692d6d | 2020-09-14 13:42:57 +0800 | [diff] [blame] | 66 | void Device::close(Services& services) |
Shawn McCarney | b4d18a4 | 2020-06-02 10:27:05 -0500 | [diff] [blame] | 67 | { |
| 68 | try |
| 69 | { |
| 70 | // Close I2C interface if it is open |
| 71 | if (i2cInterface->isOpen()) |
| 72 | { |
| 73 | i2cInterface->close(); |
| 74 | } |
| 75 | } |
| 76 | catch (const std::exception& e) |
| 77 | { |
| 78 | // Log error messages in journal |
Bob King | d692d6d | 2020-09-14 13:42:57 +0800 | [diff] [blame] | 79 | services.getJournal().logError(exception_utils::getMessages(e)); |
| 80 | services.getJournal().logError("Unable to close device " + id); |
Shawn McCarney | b4d18a4 | 2020-06-02 10:27:05 -0500 | [diff] [blame] | 81 | |
Shawn McCarney | 81a2f90 | 2021-03-23 21:41:34 -0500 | [diff] [blame] | 82 | // Create error log entry |
| 83 | error_logging_utils::logError(std::current_exception(), |
| 84 | Entry::Level::Notice, services); |
Shawn McCarney | b4d18a4 | 2020-06-02 10:27:05 -0500 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
Bob King | 23243f8 | 2020-07-29 10:38:57 +0800 | [diff] [blame] | 88 | void Device::configure(Services& services, System& system, Chassis& chassis) |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 89 | { |
Shawn McCarney | 48033bf | 2021-01-27 17:56:49 -0600 | [diff] [blame] | 90 | // Verify device is present |
| 91 | if (isPresent(services, system, chassis)) |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 92 | { |
Shawn McCarney | 48033bf | 2021-01-27 17:56:49 -0600 | [diff] [blame] | 93 | // If configuration changes are defined for this device, apply them |
| 94 | if (configuration) |
| 95 | { |
| 96 | configuration->execute(services, system, chassis, *this); |
| 97 | } |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 98 | |
Shawn McCarney | 48033bf | 2021-01-27 17:56:49 -0600 | [diff] [blame] | 99 | // Configure rails |
| 100 | for (std::unique_ptr<Rail>& rail : rails) |
| 101 | { |
| 102 | rail->configure(services, system, chassis, *this); |
| 103 | } |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
Shawn McCarney | 1fd0b14 | 2021-09-09 10:04:22 -0500 | [diff] [blame] | 107 | void Device::detectPhaseFaults(Services& services, System& system, |
| 108 | Chassis& chassis) |
| 109 | { |
| 110 | // Verify device is present |
| 111 | if (isPresent(services, system, chassis)) |
| 112 | { |
| 113 | // If phase fault detection is defined for this device, execute it |
| 114 | if (phaseFaultDetection) |
| 115 | { |
| 116 | phaseFaultDetection->execute(services, system, chassis, *this); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
Bob King | 8a55292 | 2020-08-05 17:02:31 +0800 | [diff] [blame] | 121 | void Device::monitorSensors(Services& services, System& system, |
| 122 | Chassis& chassis) |
Bob King | 8e1cd0b | 2020-07-08 13:30:27 +0800 | [diff] [blame] | 123 | { |
Shawn McCarney | 48033bf | 2021-01-27 17:56:49 -0600 | [diff] [blame] | 124 | // Verify device is present |
| 125 | if (isPresent(services, system, chassis)) |
Bob King | 8e1cd0b | 2020-07-08 13:30:27 +0800 | [diff] [blame] | 126 | { |
Shawn McCarney | 48033bf | 2021-01-27 17:56:49 -0600 | [diff] [blame] | 127 | // Monitor sensors in each rail |
| 128 | for (std::unique_ptr<Rail>& rail : rails) |
| 129 | { |
| 130 | rail->monitorSensors(services, system, chassis, *this); |
| 131 | } |
Bob King | 8e1cd0b | 2020-07-08 13:30:27 +0800 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 135 | } // namespace phosphor::power::regulators |