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 | { |
George Liu | 1f0b715 | 2023-07-18 09:24:34 +0800 | [diff] [blame] | 25 | std::string path = std::string(phyLedPath) + it.name; |
George Liu | 8215032 | 2021-03-03 17:13:13 +0800 | [diff] [blame] | 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 | { |
George Liu | 1f0b715 | 2023-07-18 09:24:34 +0800 | [diff] [blame] | 39 | std::string path = std::string(phyLedPath) + it.name; |
George Liu | 8215032 | 2021-03-03 17:13:13 +0800 | [diff] [blame] | 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 | 605600e | 2023-10-20 11:18:04 -0500 | [diff] [blame] | 111 | auto iter = std::find_if( |
| 112 | skipUpdateLEDs.begin(), skipUpdateLEDs.end(), |
| 113 | [&path](const auto& skipLed) { return skipLed == path; }); |
George Liu | 8215032 | 2021-03-03 17:13:13 +0800 | [diff] [blame] | 114 | |
| 115 | if (iter != skipUpdateLEDs.end()) |
| 116 | { |
| 117 | // Physical LEDs will be skipped |
| 118 | continue; |
| 119 | } |
| 120 | |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 121 | // Reverse intercept path, Get the name of each member of physical led |
| 122 | // e.g: path = /xyz/openbmc_project/led/physical/front_fan |
| 123 | // name = front_fan |
| 124 | sdbusplus::message::object_path object_path(path); |
| 125 | auto name = object_path.filename(); |
| 126 | if (name.empty()) |
| 127 | { |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 128 | lg2::error( |
| 129 | "Failed to get the name of member of physical LED path, PATH = {PATH}, NAME = {NAME}", |
| 130 | "PATH", path, "NAME", name); |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 131 | continue; |
| 132 | } |
| 133 | |
| 134 | std::string state{}; |
| 135 | uint16_t period{}; |
| 136 | uint8_t dutyOn{}; |
| 137 | try |
| 138 | { |
George Liu | 1f0b715 | 2023-07-18 09:24:34 +0800 | [diff] [blame] | 139 | auto properties = dBusHandler.getAllProperties(path, phyLedIntf); |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 140 | |
| 141 | state = std::get<std::string>(properties["State"]); |
| 142 | period = std::get<uint16_t>(properties["Period"]); |
| 143 | dutyOn = std::get<uint8_t>(properties["DutyOn"]); |
| 144 | } |
Patrick Williams | 3e073ba | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 145 | catch (const sdbusplus::exception_t& e) |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 146 | { |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 147 | lg2::error( |
| 148 | "Failed to get All properties, ERROR = {ERROR}, PATH = {PATH}", |
| 149 | "ERROR", e, "PATH", path); |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 150 | continue; |
| 151 | } |
| 152 | |
| 153 | phosphor::led::Layout::Action action = getActionFromString(state); |
Patrick Williams | ed80e88 | 2022-03-17 05:03:51 -0500 | [diff] [blame] | 154 | if (action != phosphor::led::Layout::Action::Off) |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 155 | { |
| 156 | phosphor::led::Layout::LedAction ledAction{ |
Patrick Williams | ed80e88 | 2022-03-17 05:03:51 -0500 | [diff] [blame] | 157 | name, action, dutyOn, period, |
| 158 | phosphor::led::Layout::Action::On}; |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 159 | physicalLEDStatesPriorToLampTest.emplace(ledAction); |
| 160 | } |
| 161 | } |
George Liu | c777bef | 2020-11-23 17:04:21 +0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void LampTest::start() |
| 165 | { |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 166 | if (isLampTestRunning) |
| 167 | { |
| 168 | // reset the timer and then return |
| 169 | timer.restart(std::chrono::seconds(LAMP_TEST_TIMEOUT_IN_SECS)); |
PriyangaRamasamy | 180090c | 2022-10-10 10:50:52 +0530 | [diff] [blame] | 170 | |
| 171 | // Notify host to reset the timer |
| 172 | doHostLampTest(true); |
| 173 | |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 174 | return; |
| 175 | } |
| 176 | |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 177 | // Get paths of all the Physical LED objects |
George Liu | 785f505 | 2023-07-18 09:38:43 +0800 | [diff] [blame] | 178 | try |
| 179 | { |
George Liu | 1f0b715 | 2023-07-18 09:24:34 +0800 | [diff] [blame] | 180 | physicalLEDPaths = dBusHandler.getSubTreePaths(phyLedPath, phyLedIntf); |
George Liu | 785f505 | 2023-07-18 09:38:43 +0800 | [diff] [blame] | 181 | } |
| 182 | catch (const sdbusplus::exception_t& e) |
| 183 | { |
| 184 | lg2::error( |
| 185 | "Failed to call the SubTreePaths method: {ERROR}, ledPath: {PATH}, ledInterface: {INTERFACE}", |
George Liu | 1f0b715 | 2023-07-18 09:24:34 +0800 | [diff] [blame] | 186 | "ERROR", e, "PATH", phyLedPath, "INTERFACE", phyLedIntf); |
George Liu | 785f505 | 2023-07-18 09:38:43 +0800 | [diff] [blame] | 187 | return; |
| 188 | } |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 189 | |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 190 | // Get physical LEDs states before lamp test |
| 191 | storePhysicalLEDsStates(); |
| 192 | |
George Liu | c777bef | 2020-11-23 17:04:21 +0800 | [diff] [blame] | 193 | // restart lamp test, it contains initiate or reset the timer. |
| 194 | timer.restart(std::chrono::seconds(LAMP_TEST_TIMEOUT_IN_SECS)); |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 195 | isLampTestRunning = true; |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 196 | |
PriyangaRamasamy | 180090c | 2022-10-10 10:50:52 +0530 | [diff] [blame] | 197 | // Notify host to start the lamp test |
George Liu | ce4d1c5 | 2021-01-25 11:32:37 +0800 | [diff] [blame] | 198 | doHostLampTest(true); |
| 199 | |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 200 | // Set all the Physical action to On for lamp test |
| 201 | for (const auto& path : physicalLEDPaths) |
| 202 | { |
George Liu | 8215032 | 2021-03-03 17:13:13 +0800 | [diff] [blame] | 203 | auto iter = |
| 204 | std::find_if(skipUpdateLEDs.begin(), skipUpdateLEDs.end(), |
| 205 | [&path](const auto& skip) { return skip == path; }); |
| 206 | |
| 207 | if (iter != skipUpdateLEDs.end()) |
| 208 | { |
| 209 | // Skip update physical path |
| 210 | continue; |
| 211 | } |
| 212 | |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 213 | manager.drivePhysicalLED(path, Layout::Action::On, 0, 0); |
| 214 | } |
George Liu | c777bef | 2020-11-23 17:04:21 +0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | void LampTest::timeOutHandler() |
| 218 | { |
| 219 | // set the Asserted property of lamp test to false |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 220 | if (!groupObj) |
| 221 | { |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 222 | lg2::error("the Group object is nullptr"); |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 223 | throw std::runtime_error("the Group object is nullptr"); |
| 224 | } |
| 225 | |
| 226 | groupObj->asserted(false); |
George Liu | c777bef | 2020-11-23 17:04:21 +0800 | [diff] [blame] | 227 | } |
| 228 | |
PriyangaRamasamy | 180090c | 2022-10-10 10:50:52 +0530 | [diff] [blame] | 229 | bool LampTest::requestHandler(Group* group, bool value) |
George Liu | c777bef | 2020-11-23 17:04:21 +0800 | [diff] [blame] | 230 | { |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 231 | if (groupObj == NULL) |
| 232 | { |
| 233 | groupObj = std::move(group); |
| 234 | } |
| 235 | |
George Liu | c777bef | 2020-11-23 17:04:21 +0800 | [diff] [blame] | 236 | if (value) |
| 237 | { |
| 238 | start(); |
PriyangaRamasamy | 180090c | 2022-10-10 10:50:52 +0530 | [diff] [blame] | 239 | |
| 240 | // Return true in both cases (F -> T && T -> T) |
| 241 | return true; |
George Liu | c777bef | 2020-11-23 17:04:21 +0800 | [diff] [blame] | 242 | } |
| 243 | else |
| 244 | { |
PriyangaRamasamy | 180090c | 2022-10-10 10:50:52 +0530 | [diff] [blame] | 245 | if (timer.hasExpired()) |
| 246 | { |
| 247 | stop(); |
| 248 | |
| 249 | // Return true as the request to stop the lamptest is handled |
| 250 | // successfully. |
| 251 | return true; |
| 252 | } |
| 253 | else if (timer.isEnabled()) |
| 254 | { |
| 255 | lg2::info( |
| 256 | "Lamp test is still running. Cannot force stop the lamp test. Asserted is set back to true."); |
| 257 | |
| 258 | // Return false as the request to stop lamptest is not handled as |
| 259 | // the lamptest is still running. |
| 260 | return false; |
| 261 | } |
| 262 | return false; |
George Liu | c777bef | 2020-11-23 17:04:21 +0800 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 266 | void LampTest::restorePhysicalLedStates() |
| 267 | { |
| 268 | // restore physical LEDs states before lamp test |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 269 | ActionSet ledsDeAssert{}; |
George Liu | 8215032 | 2021-03-03 17:13:13 +0800 | [diff] [blame] | 270 | manager.driveLEDs(physicalLEDStatesPriorToLampTest, ledsDeAssert); |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 271 | physicalLEDStatesPriorToLampTest.clear(); |
| 272 | |
| 273 | // restore physical LEDs states during lamp test |
| 274 | while (!updatedLEDsDuringLampTest.empty()) |
| 275 | { |
| 276 | auto& [ledsAssert, ledsDeAssert] = updatedLEDsDuringLampTest.front(); |
| 277 | manager.driveLEDs(ledsAssert, ledsDeAssert); |
| 278 | updatedLEDsDuringLampTest.pop(); |
| 279 | } |
| 280 | } |
| 281 | |
George Liu | ce4d1c5 | 2021-01-25 11:32:37 +0800 | [diff] [blame] | 282 | void LampTest::doHostLampTest(bool value) |
| 283 | { |
| 284 | try |
| 285 | { |
| 286 | PropertyValue assertedValue{value}; |
| 287 | dBusHandler.setProperty(HOST_LAMP_TEST_OBJECT, |
| 288 | "xyz.openbmc_project.Led.Group", "Asserted", |
| 289 | assertedValue); |
| 290 | } |
Patrick Williams | 3e073ba | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 291 | catch (const sdbusplus::exception_t& e) |
George Liu | ce4d1c5 | 2021-01-25 11:32:37 +0800 | [diff] [blame] | 292 | { |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 293 | lg2::error( |
| 294 | "Failed to set Asserted property, ERROR = {ERROR}, PATH = {PATH}", |
| 295 | "ERROR", e, "PATH", std::string(HOST_LAMP_TEST_OBJECT)); |
George Liu | ce4d1c5 | 2021-01-25 11:32:37 +0800 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
George Liu | 8215032 | 2021-03-03 17:13:13 +0800 | [diff] [blame] | 299 | void LampTest::getPhysicalLEDNamesFromJson(const fs::path& path) |
| 300 | { |
| 301 | if (!fs::exists(path) || fs::is_empty(path)) |
| 302 | { |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 303 | lg2::info("The file does not exist or is empty, FILE_PATH = {PATH}", |
| 304 | "PATH", path); |
George Liu | 8215032 | 2021-03-03 17:13:13 +0800 | [diff] [blame] | 305 | return; |
| 306 | } |
| 307 | |
| 308 | try |
| 309 | { |
| 310 | std::ifstream jsonFile(path); |
| 311 | auto json = Json::parse(jsonFile); |
| 312 | |
| 313 | // define the default JSON as empty |
| 314 | const std::vector<std::string> empty{}; |
| 315 | auto forceLEDs = json.value("forceLEDs", empty); |
George Liu | c5e0f31 | 2021-12-27 15:54:31 +0800 | [diff] [blame] | 316 | std::ranges::transform(forceLEDs, std::back_inserter(forceUpdateLEDs), |
George Liu | 1f0b715 | 2023-07-18 09:24:34 +0800 | [diff] [blame] | 317 | [](const auto& i) { return phyLedPath + i; }); |
George Liu | 8215032 | 2021-03-03 17:13:13 +0800 | [diff] [blame] | 318 | |
| 319 | auto skipLEDs = json.value("skipLEDs", empty); |
George Liu | c5e0f31 | 2021-12-27 15:54:31 +0800 | [diff] [blame] | 320 | std::ranges::transform(skipLEDs, std::back_inserter(skipUpdateLEDs), |
George Liu | 1f0b715 | 2023-07-18 09:24:34 +0800 | [diff] [blame] | 321 | [](const auto& i) { return phyLedPath + i; }); |
George Liu | 8215032 | 2021-03-03 17:13:13 +0800 | [diff] [blame] | 322 | } |
| 323 | catch (const std::exception& e) |
| 324 | { |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 325 | lg2::error( |
| 326 | "Failed to parse config file, ERROR = {ERROR}, FILE_PATH = {PATH}", |
| 327 | "ERROR", e, "PATH", path); |
George Liu | 8215032 | 2021-03-03 17:13:13 +0800 | [diff] [blame] | 328 | } |
| 329 | return; |
| 330 | } |
George Liu | c777bef | 2020-11-23 17:04:21 +0800 | [diff] [blame] | 331 | } // namespace led |
| 332 | } // namespace phosphor |