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 | b4d18a4 | 2020-06-02 10:27:05 -0500 | [diff] [blame] | 20 | #include "exception_utils.hpp" |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 21 | #include "system.hpp" |
| 22 | |
Shawn McCarney | b4d18a4 | 2020-06-02 10:27:05 -0500 | [diff] [blame] | 23 | #include <exception> |
| 24 | |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 25 | namespace phosphor::power::regulators |
| 26 | { |
| 27 | |
| 28 | void Device::addToIDMap(IDMap& idMap) |
| 29 | { |
| 30 | // Add this device to the map |
| 31 | idMap.addDevice(*this); |
| 32 | |
| 33 | // Add rails to the map |
| 34 | for (std::unique_ptr<Rail>& rail : rails) |
| 35 | { |
| 36 | idMap.addRail(*rail); |
| 37 | } |
| 38 | } |
| 39 | |
Shawn McCarney | 9bd94d3 | 2021-01-25 19:40:42 -0600 | [diff] [blame] | 40 | void Device::clearCache() |
| 41 | { |
| 42 | // If presence detection is defined for this device |
| 43 | if (presenceDetection) |
| 44 | { |
| 45 | // Clear cached presence data |
| 46 | presenceDetection->clearCache(); |
| 47 | } |
| 48 | } |
| 49 | |
Bob King | d692d6d | 2020-09-14 13:42:57 +0800 | [diff] [blame] | 50 | void Device::close(Services& services) |
Shawn McCarney | b4d18a4 | 2020-06-02 10:27:05 -0500 | [diff] [blame] | 51 | { |
| 52 | try |
| 53 | { |
| 54 | // Close I2C interface if it is open |
| 55 | if (i2cInterface->isOpen()) |
| 56 | { |
| 57 | i2cInterface->close(); |
| 58 | } |
| 59 | } |
| 60 | catch (const std::exception& e) |
| 61 | { |
| 62 | // Log error messages in journal |
Bob King | d692d6d | 2020-09-14 13:42:57 +0800 | [diff] [blame] | 63 | services.getJournal().logError(exception_utils::getMessages(e)); |
| 64 | services.getJournal().logError("Unable to close device " + id); |
Shawn McCarney | b4d18a4 | 2020-06-02 10:27:05 -0500 | [diff] [blame] | 65 | |
| 66 | // TODO: Create error log entry |
| 67 | } |
| 68 | } |
| 69 | |
Bob King | 23243f8 | 2020-07-29 10:38:57 +0800 | [diff] [blame] | 70 | void Device::configure(Services& services, System& system, Chassis& chassis) |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 71 | { |
Shawn McCarney | 48033bf | 2021-01-27 17:56:49 -0600 | [diff] [blame] | 72 | // Verify device is present |
| 73 | if (isPresent(services, system, chassis)) |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 74 | { |
Shawn McCarney | 48033bf | 2021-01-27 17:56:49 -0600 | [diff] [blame] | 75 | // If configuration changes are defined for this device, apply them |
| 76 | if (configuration) |
| 77 | { |
| 78 | configuration->execute(services, system, chassis, *this); |
| 79 | } |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 80 | |
Shawn McCarney | 48033bf | 2021-01-27 17:56:49 -0600 | [diff] [blame] | 81 | // Configure rails |
| 82 | for (std::unique_ptr<Rail>& rail : rails) |
| 83 | { |
| 84 | rail->configure(services, system, chassis, *this); |
| 85 | } |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
Bob King | 8a55292 | 2020-08-05 17:02:31 +0800 | [diff] [blame] | 89 | void Device::monitorSensors(Services& services, System& system, |
| 90 | Chassis& chassis) |
Bob King | 8e1cd0b | 2020-07-08 13:30:27 +0800 | [diff] [blame] | 91 | { |
Shawn McCarney | 48033bf | 2021-01-27 17:56:49 -0600 | [diff] [blame] | 92 | // Verify device is present |
| 93 | if (isPresent(services, system, chassis)) |
Bob King | 8e1cd0b | 2020-07-08 13:30:27 +0800 | [diff] [blame] | 94 | { |
Shawn McCarney | 48033bf | 2021-01-27 17:56:49 -0600 | [diff] [blame] | 95 | // Monitor sensors in each rail |
| 96 | for (std::unique_ptr<Rail>& rail : rails) |
| 97 | { |
| 98 | rail->monitorSensors(services, system, chassis, *this); |
| 99 | } |
Bob King | 8e1cd0b | 2020-07-08 13:30:27 +0800 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 103 | } // namespace phosphor::power::regulators |