Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 3 | #include "ledlayout.hpp" |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame^] | 4 | #include "utils.hpp" |
George Liu | a6c18f8 | 2020-06-22 10:50:04 +0800 | [diff] [blame] | 5 | |
| 6 | #include <map> |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 7 | #include <set> |
Andrew Geissler | d02c3cb | 2020-05-16 10:28:02 -0500 | [diff] [blame] | 8 | #include <string> |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 9 | |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 10 | namespace phosphor |
| 11 | { |
| 12 | namespace led |
| 13 | { |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame^] | 14 | using namespace phosphor::led::utils; |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 15 | |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 16 | /** @class Manager |
| 17 | * @brief Manages group of LEDs and applies action on the elements of group |
| 18 | */ |
| 19 | class Manager |
| 20 | { |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 21 | public: |
| 22 | /** @brief Only need the default Manager */ |
| 23 | Manager() = delete; |
| 24 | ~Manager() = default; |
| 25 | Manager(const Manager&) = delete; |
| 26 | Manager& operator=(const Manager&) = delete; |
| 27 | Manager(Manager&&) = delete; |
| 28 | Manager& operator=(Manager&&) = delete; |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 29 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 30 | /** @brief Special comparator for finding set difference */ |
| 31 | static bool ledComp(const phosphor::led::Layout::LedAction& left, |
| 32 | const phosphor::led::Layout::LedAction& right) |
| 33 | { |
| 34 | // Example : |
| 35 | // If FIRST_1 is {fan0, 1, 1} and FIRST_2 is {fan0, 2, 2}, |
| 36 | // with default priority of Blink, this comparator would return |
| 37 | // false. But considering the priority, this comparator would need |
| 38 | // to return true so that we consider appropriate set and in |
| 39 | // this case its {fan0, 1, 1} |
| 40 | if (left.name == right.name) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 41 | { |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 42 | if (left.action == right.action) |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 43 | { |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 44 | return false; |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 45 | } |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 46 | else |
| 47 | { |
| 48 | return true; |
| 49 | } |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 50 | } |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 51 | return left.name < right.name; |
| 52 | } |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 53 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 54 | /** @brief Comparator for finding LEDs to be DeAsserted */ |
| 55 | static bool ledLess(const phosphor::led::Layout::LedAction& left, |
| 56 | const phosphor::led::Layout::LedAction& right) |
| 57 | { |
| 58 | return left.name < right.name; |
| 59 | } |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 60 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 61 | /** @brief Comparator for helping unique_copy */ |
| 62 | static bool ledEqual(const phosphor::led::Layout::LedAction& left, |
| 63 | const phosphor::led::Layout::LedAction& right) |
| 64 | { |
| 65 | return left.name == right.name; |
| 66 | } |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 67 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 68 | using group = std::set<phosphor::led::Layout::LedAction>; |
| 69 | using LedLayout = std::map<std::string, group>; |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 70 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 71 | /** @brief static global map constructed at compile time */ |
| 72 | const LedLayout& ledMap; |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 73 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 74 | /** @brief Refer the user supplied LED layout and sdbusplus handler |
| 75 | * |
| 76 | * @param [in] bus - sdbusplus handler |
| 77 | * @param [in] LedLayout - LEDs group layout |
| 78 | */ |
| 79 | Manager(sdbusplus::bus::bus& bus, const LedLayout& ledLayout) : |
| 80 | ledMap(ledLayout), bus(bus) |
| 81 | { |
| 82 | // Nothing here |
| 83 | } |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 84 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 85 | /** @brief Given a group name, applies the action on the group |
| 86 | * |
| 87 | * @param[in] path - dbus path of group |
| 88 | * @param[in] assert - Could be true or false |
| 89 | * @param[in] ledsAssert - LEDs that are to be asserted new |
| 90 | * or to a different state |
| 91 | * @param[in] ledsDeAssert - LEDs that are to be Deasserted |
| 92 | * |
| 93 | * @return - Success or exception thrown |
| 94 | */ |
| 95 | bool setGroupState(const std::string& path, bool assert, group& ledsAssert, |
| 96 | group& ledsDeAssert); |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 97 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 98 | /** @brief Finds the set of LEDs to operate on and executes action |
| 99 | * |
| 100 | * @param[in] ledsAssert - LEDs that are to be asserted newly |
| 101 | * or to a different state |
| 102 | * @param[in] ledsDeAssert - LEDs that are to be Deasserted |
| 103 | * |
| 104 | * @return: None |
| 105 | */ |
| 106 | void driveLEDs(group& ledsAssert, group& ledsDeAssert); |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 107 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 108 | private: |
| 109 | /** @brief sdbusplus handler */ |
| 110 | sdbusplus::bus::bus& bus; |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 111 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 112 | /** Map of physical LED path to service name */ |
| 113 | std::map<std::string, std::string> phyLeds{}; |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 114 | |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame^] | 115 | /** DBusHandler class handles the D-Bus operations */ |
| 116 | DBusHandler dBusHandler; |
| 117 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 118 | /** @brief Pointers to groups that are in asserted state */ |
| 119 | std::set<const group*> assertedGroups; |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 120 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 121 | /** @brief Contains the highest priority actions for all |
| 122 | * asserted LEDs. |
| 123 | */ |
| 124 | group currentState; |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 125 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 126 | /** @brief Contains the set of all actions for asserted LEDs */ |
| 127 | group combinedState; |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 128 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 129 | /** @brief Returns action string based on enum |
| 130 | * |
| 131 | * @param[in] action - Action enum |
| 132 | * |
| 133 | * @return string equivalent of the passed in enumeration |
| 134 | */ |
| 135 | static std::string getPhysicalAction(Layout::Action action); |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 136 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 137 | /** @brief Chooses appropriate action to be triggered on physical LED |
| 138 | * and calls into function that applies the actual action. |
| 139 | * |
| 140 | * @param[in] objPath - dbus object path |
| 141 | * @param[in] action - Intended action to be triggered |
| 142 | * @param[in] dutyOn - Duty Cycle ON percentage |
tony lee | 6fd9e44 | 2019-04-23 09:09:15 +0800 | [diff] [blame] | 143 | * @param[in] period - Time taken for one blink cycle |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 144 | */ |
| 145 | void drivePhysicalLED(const std::string& objPath, Layout::Action action, |
tony lee | 6fd9e44 | 2019-04-23 09:09:15 +0800 | [diff] [blame] | 146 | uint8_t dutyOn, const uint16_t period); |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | } // namespace led |
| 150 | } // namespace phosphor |