Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Delphine CC Chiu | ccd7db0 | 2023-02-09 14:48:53 +0800 | [diff] [blame] | 3 | #include "button_config.hpp" |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 4 | #include "button_interface.hpp" |
Matt Spinler | ea47dd4 | 2025-08-20 13:07:20 -0500 | [diff] [blame] | 5 | #include "config.hpp" |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 6 | |
| 7 | #include <phosphor-logging/elog-errors.hpp> |
George Liu | 5b98f4d | 2022-06-20 13:31:14 +0800 | [diff] [blame] | 8 | |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 9 | #include <unordered_map> |
| 10 | |
| 11 | using buttonIfCreatorMethod = std::function<std::unique_ptr<ButtonIface>( |
Delphine CC Chiu | ccd7db0 | 2023-02-09 14:48:53 +0800 | [diff] [blame] | 12 | sdbusplus::bus_t& bus, EventPtr& event, ButtonConfig& buttonCfg)>; |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 13 | |
| 14 | /** |
| 15 | * @brief This is abstract factory for the creating phosphor buttons objects |
| 16 | * based on the button / formfactor type given. |
| 17 | */ |
| 18 | |
| 19 | class ButtonFactory |
| 20 | { |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 21 | public: |
| 22 | static ButtonFactory& instance() |
| 23 | { |
| 24 | static ButtonFactory buttonFactoryObj; |
| 25 | return buttonFactoryObj; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * @brief this method creates a key and value pair element |
| 30 | * for the given button interface where key is the form factor |
| 31 | * name and the value is lambda method to return |
| 32 | * the instance of the button interface. |
| 33 | * This key value pair is stored in the Map buttonIfaceRegistry. |
| 34 | */ |
| 35 | |
| 36 | template <typename T> |
| 37 | void addToRegistry() |
| 38 | { |
Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame] | 39 | buttonIfaceRegistry[T::getFormFactorName()] = |
Patrick Williams | 9a529a6 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 40 | [](sdbusplus::bus_t& bus, EventPtr& event, |
Delphine CC Chiu | ccd7db0 | 2023-02-09 14:48:53 +0800 | [diff] [blame] | 41 | ButtonConfig& buttonCfg) { |
Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame] | 42 | return std::make_unique<T>(bus, T::getDbusObjectPath().c_str(), |
| 43 | event, buttonCfg); |
Patrick Williams | d36b6b1 | 2024-08-16 15:20:34 -0400 | [diff] [blame] | 44 | }; |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 45 | } |
Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame] | 46 | |
| 47 | template <typename T> |
| 48 | void addToRegistry(size_t index) |
| 49 | { |
| 50 | auto indexStr = std::to_string(index); |
| 51 | buttonIfaceRegistry[T::getFormFactorName() + indexStr] = |
| 52 | [=](sdbusplus::bus_t& bus, EventPtr& event, |
| 53 | ButtonConfig& buttonCfg) { |
| 54 | return std::make_unique<T>( |
| 55 | bus, (T::getDbusObjectPath() + indexStr).c_str(), event, |
| 56 | buttonCfg); |
| 57 | }; |
| 58 | } |
| 59 | |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 60 | /** |
| 61 | * @brief this method returns the button interface object |
| 62 | * corresponding to the button formfactor name provided |
| 63 | */ |
Patrick Williams | efe5092 | 2025-02-01 08:22:17 -0500 | [diff] [blame] | 64 | std::unique_ptr<ButtonIface> createInstance( |
| 65 | const std::string& name, sdbusplus::bus_t& bus, EventPtr& event, |
| 66 | ButtonConfig& buttonCfg) |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 67 | { |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 68 | // find matching name in the registry and call factory method. |
Naveen Moses | eea8a4a | 2022-02-18 01:14:15 +0530 | [diff] [blame] | 69 | auto objectIter = buttonIfaceRegistry.find(name); |
| 70 | if (objectIter != buttonIfaceRegistry.end()) |
| 71 | { |
| 72 | return objectIter->second(bus, event, buttonCfg); |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | return nullptr; |
| 77 | } |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | private: |
| 81 | // This map is the registry for keeping supported button interface types. |
| 82 | std::unordered_map<std::string, buttonIfCreatorMethod> buttonIfaceRegistry; |
| 83 | }; |
| 84 | |
| 85 | template <class T> |
| 86 | class ButtonIFRegister |
| 87 | { |
| 88 | public: |
| 89 | ButtonIFRegister() |
| 90 | { |
| 91 | // register the class factory function |
| 92 | ButtonFactory::instance().addToRegistry<T>(); |
| 93 | } |
Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame] | 94 | |
| 95 | explicit ButtonIFRegister(size_t count) |
| 96 | { |
Matt Spinler | ea47dd4 | 2025-08-20 13:07:20 -0500 | [diff] [blame] | 97 | // The JSON power button definitions only have an instance in |
| 98 | // their name if there is more than 1 chassis. |
| 99 | if (instances.size() > 1) |
Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame] | 100 | { |
Matt Spinler | ea47dd4 | 2025-08-20 13:07:20 -0500 | [diff] [blame] | 101 | // register the class factory function |
| 102 | // The index, 'countIter', starts at 1 and increments, |
| 103 | // representing slot_1 through slot_N. |
| 104 | for (size_t countIter = 1; countIter <= count; countIter++) |
| 105 | { |
| 106 | ButtonFactory::instance().addToRegistry<T>(countIter); |
| 107 | } |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | ButtonFactory::instance().addToRegistry<T>(); |
Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame] | 112 | } |
| 113 | } |
Patrick Williams | 0d038f5 | 2023-05-10 07:50:40 -0500 | [diff] [blame] | 114 | }; |