Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Alexander Hansen | 7ba70c8 | 2024-07-23 13:46:25 +0200 | [diff] [blame] | 3 | #include "grouplayout.hpp" |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 4 | #include "ledlayout.hpp" |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 5 | #include "utils.hpp" |
George Liu | a6c18f8 | 2020-06-22 10:50:04 +0800 | [diff] [blame] | 6 | |
Potin Lai | f1ed479 | 2023-07-13 18:45:14 +0800 | [diff] [blame] | 7 | #include <sdeventplus/event.hpp> |
| 8 | #include <sdeventplus/utility/timer.hpp> |
| 9 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 10 | #include <set> |
Andrew Geissler | d02c3cb | 2020-05-16 10:28:02 -0500 | [diff] [blame] | 11 | #include <string> |
Patrick Williams | f204403 | 2022-03-17 05:12:30 -0500 | [diff] [blame] | 12 | #include <unordered_map> |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 13 | |
Alexander Hansen | a6f9a41 | 2024-07-24 12:27:42 +0200 | [diff] [blame] | 14 | // to better see what the string is representing |
| 15 | using LedName = std::string; |
| 16 | |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 17 | namespace phosphor |
| 18 | { |
| 19 | namespace led |
| 20 | { |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 21 | using namespace phosphor::led::utils; |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 22 | |
George Liu | 1f0b715 | 2023-07-18 09:24:34 +0800 | [diff] [blame] | 23 | static constexpr auto phyLedPath = "/xyz/openbmc_project/led/physical/"; |
| 24 | static constexpr auto phyLedIntf = "xyz.openbmc_project.Led.Physical"; |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 25 | |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 26 | /** @class Manager |
| 27 | * @brief Manages group of LEDs and applies action on the elements of group |
| 28 | */ |
| 29 | class Manager |
| 30 | { |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 31 | public: |
| 32 | /** @brief Only need the default Manager */ |
| 33 | Manager() = delete; |
| 34 | ~Manager() = default; |
| 35 | Manager(const Manager&) = delete; |
| 36 | Manager& operator=(const Manager&) = delete; |
| 37 | Manager(Manager&&) = delete; |
| 38 | Manager& operator=(Manager&&) = delete; |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 39 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 40 | /** @brief Special comparator for finding set difference */ |
| 41 | static bool ledComp(const phosphor::led::Layout::LedAction& left, |
| 42 | const phosphor::led::Layout::LedAction& right) |
| 43 | { |
| 44 | // Example : |
| 45 | // If FIRST_1 is {fan0, 1, 1} and FIRST_2 is {fan0, 2, 2}, |
| 46 | // with default priority of Blink, this comparator would return |
| 47 | // false. But considering the priority, this comparator would need |
| 48 | // to return true so that we consider appropriate set and in |
| 49 | // this case its {fan0, 1, 1} |
| 50 | if (left.name == right.name) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 51 | { |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 52 | if (left.action == right.action) |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 53 | { |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 54 | return false; |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 55 | } |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 56 | else |
| 57 | { |
| 58 | return true; |
| 59 | } |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 60 | } |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 61 | return left.name < right.name; |
| 62 | } |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 63 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 64 | /** @brief Comparator for finding LEDs to be DeAsserted */ |
| 65 | static bool ledLess(const phosphor::led::Layout::LedAction& left, |
| 66 | const phosphor::led::Layout::LedAction& right) |
| 67 | { |
| 68 | return left.name < right.name; |
| 69 | } |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 70 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 71 | /** @brief Comparator for helping unique_copy */ |
| 72 | static bool ledEqual(const phosphor::led::Layout::LedAction& left, |
| 73 | const phosphor::led::Layout::LedAction& right) |
| 74 | { |
| 75 | return left.name == right.name; |
| 76 | } |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 77 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 78 | /** @brief static global map constructed at compile time */ |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 79 | const GroupMap& ledMap; |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 80 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 81 | /** @brief Refer the user supplied LED layout and sdbusplus handler |
| 82 | * |
| 83 | * @param [in] bus - sdbusplus handler |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 84 | * @param [in] GroupMap - LEDs group layout |
Potin Lai | f1ed479 | 2023-07-13 18:45:14 +0800 | [diff] [blame] | 85 | * @param [in] Event - sd event handler |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 86 | */ |
Potin Lai | f1ed479 | 2023-07-13 18:45:14 +0800 | [diff] [blame] | 87 | Manager( |
| 88 | sdbusplus::bus_t& bus, const GroupMap& ledLayout, |
| 89 | const sdeventplus::Event& event = sdeventplus::Event::get_default()) : |
Patrick Williams | 543ac9f | 2024-08-16 15:19:59 -0400 | [diff] [blame] | 90 | ledMap(ledLayout), bus(bus), |
| 91 | timer(event, [this](auto&) { driveLedsHandler(); }) |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 92 | { |
| 93 | // Nothing here |
| 94 | } |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 95 | |
Alexander Hansen | a6f9a41 | 2024-07-24 12:27:42 +0200 | [diff] [blame] | 96 | /* create the resulting map from all currently asserted groups */ |
Alexander Hansen | 7ba70c8 | 2024-07-23 13:46:25 +0200 | [diff] [blame] | 97 | static auto getNewMap(std::set<const Layout::GroupLayout*> assertedGroups) |
Alexander Hansen | a6f9a41 | 2024-07-24 12:27:42 +0200 | [diff] [blame] | 98 | -> std::map<LedName, Layout::LedAction>; |
| 99 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 100 | /** @brief Given a group name, applies the action on the group |
| 101 | * |
| 102 | * @param[in] path - dbus path of group |
| 103 | * @param[in] assert - Could be true or false |
| 104 | * @param[in] ledsAssert - LEDs that are to be asserted new |
| 105 | * or to a different state |
| 106 | * @param[in] ledsDeAssert - LEDs that are to be Deasserted |
| 107 | * |
| 108 | * @return - Success or exception thrown |
| 109 | */ |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 110 | bool setGroupState(const std::string& path, bool assert, |
| 111 | ActionSet& ledsAssert, ActionSet& ledsDeAssert); |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 112 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 113 | /** @brief Finds the set of LEDs to operate on and executes action |
| 114 | * |
| 115 | * @param[in] ledsAssert - LEDs that are to be asserted newly |
| 116 | * or to a different state |
| 117 | * @param[in] ledsDeAssert - LEDs that are to be Deasserted |
| 118 | * |
| 119 | * @return: None |
| 120 | */ |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 121 | void driveLEDs(ActionSet& ledsAssert, ActionSet& ledsDeAssert); |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 122 | |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 123 | /** @brief Chooses appropriate action to be triggered on physical LED |
| 124 | * and calls into function that applies the actual action. |
| 125 | * |
| 126 | * @param[in] objPath - D-Bus object path |
| 127 | * @param[in] action - Intended action to be triggered |
| 128 | * @param[in] dutyOn - Duty Cycle ON percentage |
| 129 | * @param[in] period - Time taken for one blink cycle |
Potin Lai | f1ed479 | 2023-07-13 18:45:14 +0800 | [diff] [blame] | 130 | * |
| 131 | * @return: - 0: success, -1: LED set failed |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 132 | */ |
George Liu | f059255 | 2024-08-23 09:46:17 +0800 | [diff] [blame] | 133 | static int drivePhysicalLED(const std::string& objPath, |
| 134 | Layout::Action action, uint8_t dutyOn, |
George Liu | 80f51bb | 2024-08-22 20:17:36 +0800 | [diff] [blame] | 135 | uint16_t period); |
George Liu | 87fd11c | 2020-11-23 16:40:14 +0800 | [diff] [blame] | 136 | |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 137 | /** @brief Set lamp test callback when enabled lamp test. |
| 138 | * |
| 139 | * @param[in] callBack - Custom callback when enabled lamp test |
| 140 | */ |
| 141 | void setLampTestCallBack( |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 142 | std::function<bool(ActionSet& ledsAssert, ActionSet& ledsDeAssert)> |
| 143 | callBack); |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 144 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 145 | private: |
| 146 | /** @brief sdbusplus handler */ |
Patrick Williams | 3e073ba | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 147 | sdbusplus::bus_t& bus; |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 148 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 149 | /** Map of physical LED path to service name */ |
George Liu | 391bec5 | 2024-08-22 20:10:55 +0800 | [diff] [blame] | 150 | std::unordered_map<std::string, std::string> phyLeds; |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 151 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 152 | /** @brief Pointers to groups that are in asserted state */ |
Alexander Hansen | 7ba70c8 | 2024-07-23 13:46:25 +0200 | [diff] [blame] | 153 | std::set<const Layout::GroupLayout*> assertedGroups; |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 154 | |
Alexander Hansen | a6f9a41 | 2024-07-24 12:27:42 +0200 | [diff] [blame] | 155 | /** Map of led name to current state */ |
| 156 | std::map<std::string, Layout::LedAction> ledStateMap; |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 157 | |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 158 | /** @brief Custom callback when enabled lamp test */ |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 159 | std::function<bool(ActionSet& ledsAssert, ActionSet& ledsDeAssert)> |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 160 | lampTestCallBack; |
| 161 | |
Potin Lai | f1ed479 | 2023-07-13 18:45:14 +0800 | [diff] [blame] | 162 | /** @brief Timer used for LEDs handler callback*/ |
| 163 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer; |
| 164 | |
| 165 | /** @brief Contains the required set of assert LEDs action */ |
| 166 | ActionSet reqLedsAssert; |
| 167 | |
| 168 | /** @brief Contains the required set of deassert LEDs action */ |
| 169 | ActionSet reqLedsDeAssert; |
| 170 | |
| 171 | /** @brief LEDs handler callback */ |
| 172 | void driveLedsHandler(); |
| 173 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 174 | /** @brief Returns action string based on enum |
| 175 | * |
| 176 | * @param[in] action - Action enum |
| 177 | * |
| 178 | * @return string equivalent of the passed in enumeration |
| 179 | */ |
| 180 | static std::string getPhysicalAction(Layout::Action action); |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | } // namespace led |
| 184 | } // namespace phosphor |