George Liu | c777bef | 2020-11-23 17:04:21 +0800 | [diff] [blame] | 1 | #include "lamptest.hpp" |
| 2 | |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 3 | #include <phosphor-logging/lg2.hpp> |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 4 | |
George Liu | c5e0f31 | 2021-12-27 15:54:31 +0800 | [diff] [blame] | 5 | #include <algorithm> |
| 6 | |
George Liu | c777bef | 2020-11-23 17:04:21 +0800 | [diff] [blame] | 7 | namespace phosphor |
| 8 | { |
| 9 | namespace led |
| 10 | { |
| 11 | |
George Liu | 8215032 | 2021-03-03 17:13:13 +0800 | [diff] [blame] | 12 | using Json = nlohmann::json; |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 13 | |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 14 | bool LampTest::processLEDUpdates(const ActionSet& ledsAssert, |
| 15 | const ActionSet& ledsDeAssert) |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 16 | { |
| 17 | // If the physical LED status is updated during the lamp test, it should be |
| 18 | // saved to Queue, and the queue will be processed after the lamp test is |
| 19 | // stopped. |
| 20 | if (isLampTestRunning) |
| 21 | { |
George Liu | 8215032 | 2021-03-03 17:13:13 +0800 | [diff] [blame] | 22 | // Physical LEDs will be updated during lamp test |
| 23 | for (const auto& it : ledsDeAssert) |
| 24 | { |
| 25 | std::string path = std::string(PHY_LED_PATH) + it.name; |
| 26 | auto iter = std::find_if( |
| 27 | forceUpdateLEDs.begin(), forceUpdateLEDs.end(), |
| 28 | [&path](const auto& name) { return name == path; }); |
| 29 | |
| 30 | if (iter != forceUpdateLEDs.end()) |
| 31 | { |
| 32 | manager.drivePhysicalLED(path, Layout::Action::Off, it.dutyOn, |
| 33 | it.period); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | for (const auto& it : ledsAssert) |
| 38 | { |
| 39 | std::string path = std::string(PHY_LED_PATH) + it.name; |
| 40 | auto iter = std::find_if( |
| 41 | forceUpdateLEDs.begin(), forceUpdateLEDs.end(), |
| 42 | [&path](const auto& name) { return name == path; }); |
| 43 | |
| 44 | if (iter != forceUpdateLEDs.end()) |
| 45 | { |
| 46 | manager.drivePhysicalLED(path, it.action, it.dutyOn, it.period); |
| 47 | } |
| 48 | } |
| 49 | |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 50 | updatedLEDsDuringLampTest.emplace( |
| 51 | std::make_pair(ledsAssert, ledsDeAssert)); |
| 52 | return true; |
| 53 | } |
| 54 | return false; |
| 55 | } |
| 56 | |
George Liu | c777bef | 2020-11-23 17:04:21 +0800 | [diff] [blame] | 57 | void LampTest::stop() |
| 58 | { |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 59 | if (!isLampTestRunning) |
| 60 | { |
| 61 | return; |
| 62 | } |
| 63 | |
George Liu | c777bef | 2020-11-23 17:04:21 +0800 | [diff] [blame] | 64 | timer.setEnabled(false); |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 65 | |
George Liu | ce4d1c5 | 2021-01-25 11:32:37 +0800 | [diff] [blame] | 66 | // Stop host lamp test |
| 67 | doHostLampTest(false); |
| 68 | |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 69 | // Set all the Physical action to Off |
| 70 | for (const auto& path : physicalLEDPaths) |
| 71 | { |
George Liu | 8215032 | 2021-03-03 17:13:13 +0800 | [diff] [blame] | 72 | auto iter = |
| 73 | std::find_if(skipUpdateLEDs.begin(), skipUpdateLEDs.end(), |
| 74 | [&path](const auto& skip) { return skip == path; }); |
| 75 | |
| 76 | if (iter != skipUpdateLEDs.end()) |
| 77 | { |
| 78 | // Skip update physical path |
| 79 | continue; |
| 80 | } |
| 81 | |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 82 | manager.drivePhysicalLED(path, Layout::Action::Off, 0, 0); |
| 83 | } |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 84 | |
| 85 | isLampTestRunning = false; |
| 86 | restorePhysicalLedStates(); |
| 87 | } |
| 88 | |
| 89 | Layout::Action LampTest::getActionFromString(const std::string& str) |
| 90 | { |
Patrick Williams | ed80e88 | 2022-03-17 05:03:51 -0500 | [diff] [blame] | 91 | Layout::Action action = Layout::Action::Off; |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 92 | |
| 93 | if (str == "xyz.openbmc_project.Led.Physical.Action.On") |
| 94 | { |
Patrick Williams | ed80e88 | 2022-03-17 05:03:51 -0500 | [diff] [blame] | 95 | action = Layout::Action::On; |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 96 | } |
| 97 | else if (str == "xyz.openbmc_project.Led.Physical.Action.Blink") |
| 98 | { |
Patrick Williams | ed80e88 | 2022-03-17 05:03:51 -0500 | [diff] [blame] | 99 | action = Layout::Action::Blink; |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | return action; |
| 103 | } |
| 104 | |
| 105 | void LampTest::storePhysicalLEDsStates() |
| 106 | { |
| 107 | physicalLEDStatesPriorToLampTest.clear(); |
| 108 | |
| 109 | for (const auto& path : physicalLEDPaths) |
| 110 | { |
Patrick Williams | 857da38 | 2023-05-10 07:50:20 -0500 | [diff] [blame^] | 111 | auto iter = std::find_if(skipUpdateLEDs.begin(), skipUpdateLEDs.end(), |
| 112 | [&path](const auto& skipLed) { |
| 113 | return skipLed == path; |
| 114 | }); |
George Liu | 8215032 | 2021-03-03 17:13:13 +0800 | [diff] [blame] | 115 | |
| 116 | if (iter != skipUpdateLEDs.end()) |
| 117 | { |
| 118 | // Physical LEDs will be skipped |
| 119 | continue; |
| 120 | } |
| 121 | |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 122 | // Reverse intercept path, Get the name of each member of physical led |
| 123 | // e.g: path = /xyz/openbmc_project/led/physical/front_fan |
| 124 | // name = front_fan |
| 125 | sdbusplus::message::object_path object_path(path); |
| 126 | auto name = object_path.filename(); |
| 127 | if (name.empty()) |
| 128 | { |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 129 | lg2::error( |
| 130 | "Failed to get the name of member of physical LED path, PATH = {PATH}, NAME = {NAME}", |
| 131 | "PATH", path, "NAME", name); |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 132 | continue; |
| 133 | } |
| 134 | |
| 135 | std::string state{}; |
| 136 | uint16_t period{}; |
| 137 | uint8_t dutyOn{}; |
| 138 | try |
| 139 | { |
| 140 | auto properties = dBusHandler.getAllProperties(path, PHY_LED_IFACE); |
| 141 | |
| 142 | state = std::get<std::string>(properties["State"]); |
| 143 | period = std::get<uint16_t>(properties["Period"]); |
| 144 | dutyOn = std::get<uint8_t>(properties["DutyOn"]); |
| 145 | } |
Patrick Williams | 3e073ba | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 146 | catch (const sdbusplus::exception_t& e) |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 147 | { |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 148 | lg2::error( |
| 149 | "Failed to get All properties, ERROR = {ERROR}, PATH = {PATH}", |
| 150 | "ERROR", e, "PATH", path); |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 151 | continue; |
| 152 | } |
| 153 | |
| 154 | phosphor::led::Layout::Action action = getActionFromString(state); |
Patrick Williams | ed80e88 | 2022-03-17 05:03:51 -0500 | [diff] [blame] | 155 | if (action != phosphor::led::Layout::Action::Off) |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 156 | { |
| 157 | phosphor::led::Layout::LedAction ledAction{ |
Patrick Williams | ed80e88 | 2022-03-17 05:03:51 -0500 | [diff] [blame] | 158 | name, action, dutyOn, period, |
| 159 | phosphor::led::Layout::Action::On}; |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 160 | physicalLEDStatesPriorToLampTest.emplace(ledAction); |
| 161 | } |
| 162 | } |
George Liu | c777bef | 2020-11-23 17:04:21 +0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void LampTest::start() |
| 166 | { |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 167 | if (isLampTestRunning) |
| 168 | { |
| 169 | // reset the timer and then return |
| 170 | timer.restart(std::chrono::seconds(LAMP_TEST_TIMEOUT_IN_SECS)); |
| 171 | return; |
| 172 | } |
| 173 | |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 174 | // Get paths of all the Physical LED objects |
| 175 | physicalLEDPaths = dBusHandler.getSubTreePaths(PHY_LED_PATH, PHY_LED_IFACE); |
| 176 | |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 177 | // Get physical LEDs states before lamp test |
| 178 | storePhysicalLEDsStates(); |
| 179 | |
George Liu | c777bef | 2020-11-23 17:04:21 +0800 | [diff] [blame] | 180 | // restart lamp test, it contains initiate or reset the timer. |
| 181 | timer.restart(std::chrono::seconds(LAMP_TEST_TIMEOUT_IN_SECS)); |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 182 | isLampTestRunning = true; |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 183 | |
George Liu | ce4d1c5 | 2021-01-25 11:32:37 +0800 | [diff] [blame] | 184 | // Notify PHYP to start the lamp test |
| 185 | doHostLampTest(true); |
| 186 | |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 187 | // Set all the Physical action to On for lamp test |
| 188 | for (const auto& path : physicalLEDPaths) |
| 189 | { |
George Liu | 8215032 | 2021-03-03 17:13:13 +0800 | [diff] [blame] | 190 | auto iter = |
| 191 | std::find_if(skipUpdateLEDs.begin(), skipUpdateLEDs.end(), |
| 192 | [&path](const auto& skip) { return skip == path; }); |
| 193 | |
| 194 | if (iter != skipUpdateLEDs.end()) |
| 195 | { |
| 196 | // Skip update physical path |
| 197 | continue; |
| 198 | } |
| 199 | |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 200 | manager.drivePhysicalLED(path, Layout::Action::On, 0, 0); |
| 201 | } |
George Liu | c777bef | 2020-11-23 17:04:21 +0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | void LampTest::timeOutHandler() |
| 205 | { |
| 206 | // set the Asserted property of lamp test to false |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 207 | if (!groupObj) |
| 208 | { |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 209 | lg2::error("the Group object is nullptr"); |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 210 | throw std::runtime_error("the Group object is nullptr"); |
| 211 | } |
| 212 | |
| 213 | groupObj->asserted(false); |
George Liu | c777bef | 2020-11-23 17:04:21 +0800 | [diff] [blame] | 214 | } |
| 215 | |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 216 | void LampTest::requestHandler(Group* group, bool value) |
George Liu | c777bef | 2020-11-23 17:04:21 +0800 | [diff] [blame] | 217 | { |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 218 | if (groupObj == NULL) |
| 219 | { |
| 220 | groupObj = std::move(group); |
| 221 | } |
| 222 | |
George Liu | c777bef | 2020-11-23 17:04:21 +0800 | [diff] [blame] | 223 | if (value) |
| 224 | { |
| 225 | start(); |
| 226 | } |
| 227 | else |
| 228 | { |
| 229 | stop(); |
| 230 | } |
| 231 | } |
| 232 | |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 233 | void LampTest::restorePhysicalLedStates() |
| 234 | { |
| 235 | // restore physical LEDs states before lamp test |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 236 | ActionSet ledsDeAssert{}; |
George Liu | 8215032 | 2021-03-03 17:13:13 +0800 | [diff] [blame] | 237 | manager.driveLEDs(physicalLEDStatesPriorToLampTest, ledsDeAssert); |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 238 | physicalLEDStatesPriorToLampTest.clear(); |
| 239 | |
| 240 | // restore physical LEDs states during lamp test |
| 241 | while (!updatedLEDsDuringLampTest.empty()) |
| 242 | { |
| 243 | auto& [ledsAssert, ledsDeAssert] = updatedLEDsDuringLampTest.front(); |
| 244 | manager.driveLEDs(ledsAssert, ledsDeAssert); |
| 245 | updatedLEDsDuringLampTest.pop(); |
| 246 | } |
| 247 | } |
| 248 | |
George Liu | ce4d1c5 | 2021-01-25 11:32:37 +0800 | [diff] [blame] | 249 | void LampTest::doHostLampTest(bool value) |
| 250 | { |
| 251 | try |
| 252 | { |
| 253 | PropertyValue assertedValue{value}; |
| 254 | dBusHandler.setProperty(HOST_LAMP_TEST_OBJECT, |
| 255 | "xyz.openbmc_project.Led.Group", "Asserted", |
| 256 | assertedValue); |
| 257 | } |
Patrick Williams | 3e073ba | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 258 | catch (const sdbusplus::exception_t& e) |
George Liu | ce4d1c5 | 2021-01-25 11:32:37 +0800 | [diff] [blame] | 259 | { |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 260 | lg2::error( |
| 261 | "Failed to set Asserted property, ERROR = {ERROR}, PATH = {PATH}", |
| 262 | "ERROR", e, "PATH", std::string(HOST_LAMP_TEST_OBJECT)); |
George Liu | ce4d1c5 | 2021-01-25 11:32:37 +0800 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
George Liu | 8215032 | 2021-03-03 17:13:13 +0800 | [diff] [blame] | 266 | void LampTest::getPhysicalLEDNamesFromJson(const fs::path& path) |
| 267 | { |
| 268 | if (!fs::exists(path) || fs::is_empty(path)) |
| 269 | { |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 270 | lg2::info("The file does not exist or is empty, FILE_PATH = {PATH}", |
| 271 | "PATH", path); |
George Liu | 8215032 | 2021-03-03 17:13:13 +0800 | [diff] [blame] | 272 | return; |
| 273 | } |
| 274 | |
| 275 | try |
| 276 | { |
| 277 | std::ifstream jsonFile(path); |
| 278 | auto json = Json::parse(jsonFile); |
| 279 | |
| 280 | // define the default JSON as empty |
| 281 | const std::vector<std::string> empty{}; |
| 282 | auto forceLEDs = json.value("forceLEDs", empty); |
George Liu | c5e0f31 | 2021-12-27 15:54:31 +0800 | [diff] [blame] | 283 | std::ranges::transform(forceLEDs, std::back_inserter(forceUpdateLEDs), |
| 284 | [](const auto& i) { return PHY_LED_PATH + i; }); |
George Liu | 8215032 | 2021-03-03 17:13:13 +0800 | [diff] [blame] | 285 | |
| 286 | auto skipLEDs = json.value("skipLEDs", empty); |
George Liu | c5e0f31 | 2021-12-27 15:54:31 +0800 | [diff] [blame] | 287 | std::ranges::transform(skipLEDs, std::back_inserter(skipUpdateLEDs), |
| 288 | [](const auto& i) { return PHY_LED_PATH + i; }); |
George Liu | 8215032 | 2021-03-03 17:13:13 +0800 | [diff] [blame] | 289 | } |
| 290 | catch (const std::exception& e) |
| 291 | { |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 292 | lg2::error( |
| 293 | "Failed to parse config file, ERROR = {ERROR}, FILE_PATH = {PATH}", |
| 294 | "ERROR", e, "PATH", path); |
George Liu | 8215032 | 2021-03-03 17:13:13 +0800 | [diff] [blame] | 295 | } |
| 296 | return; |
| 297 | } |
| 298 | |
George Liu | c777bef | 2020-11-23 17:04:21 +0800 | [diff] [blame] | 299 | } // namespace led |
| 300 | } // namespace phosphor |