1 #include "config.h"
2 
3 #include "group.hpp"
4 #include "ledlayout.hpp"
5 #ifdef LED_USE_JSON
6 #include "json-parser.hpp"
7 #else
8 #include "led-gen.hpp"
9 #endif
10 #include "manager.hpp"
11 #include "serialize.hpp"
12 #include "utils.hpp"
13 #ifdef USE_LAMP_TEST
14 #include "lamptest/lamptest.hpp"
15 #endif
16 
17 #include <CLI/CLI.hpp>
18 #include <sdeventplus/event.hpp>
19 
20 #include <algorithm>
21 #include <iostream>
22 #include <memory>
23 
24 int main(int argc, char** argv)
25 {
26     CLI::App app("phosphor-led-manager");
27 
28 #ifdef LED_USE_JSON
29     std::string configFile{};
30     app.add_option("-c,--config", configFile, "Path to JSON config");
31 #endif
32 
33     CLI11_PARSE(app, argc, argv);
34 
35     // Get a default event loop
36     auto event = sdeventplus::Event::get_default();
37 
38     /** @brief Dbus constructs used by LED Group manager */
39     auto& bus = phosphor::led::utils::DBusHandler::getBus();
40 
41 #ifdef LED_USE_JSON
42     auto systemLedMap = getSystemLedMap(configFile);
43 #endif
44 
45     phosphor::led::validateConfigV1(systemLedMap);
46 
47     /** @brief Group manager object */
48     phosphor::led::Manager manager(bus, systemLedMap, event);
49 
50     /** @brief sd_bus object manager */
51     sdbusplus::server::manager_t objManager(bus,
52                                             "/xyz/openbmc_project/led/groups");
53 
54     /** @brief vector of led groups */
55     std::vector<std::unique_ptr<phosphor::led::Group>> groups;
56 
57     std::shared_ptr<phosphor::led::Serialize> serializePtr = nullptr;
58 #ifdef PERSISTENT_LED_ASSERTED
59     /** @brief store and re-store Group */
60     serializePtr =
61         std::make_shared<phosphor::led::Serialize>(SAVED_GROUPS_FILE);
62 #endif
63 
64 #ifdef USE_LAMP_TEST
65     phosphor::led::LampTest lampTest(event, manager);
66 
67     // Clear leds triggered by lamp test in previous boot
68     lampTest.clearLamps();
69 
70     groups.emplace_back(std::make_unique<phosphor::led::Group>(
71         bus, LAMP_TEST_OBJECT, manager, serializePtr,
72         std::bind(std::mem_fn(&phosphor::led::LampTest::requestHandler),
73                   &lampTest, std::placeholders::_1, std::placeholders::_2)));
74 
75     // Register a lamp test method in the manager class, and call this method
76     // when the lamp test is started
77     manager.setLampTestCallBack(
78         std::bind(std::mem_fn(&phosphor::led::LampTest::processLEDUpdates),
79                   &lampTest, std::placeholders::_1, std::placeholders::_2));
80 #endif
81 
82     /** Now create so many dbus objects as there are groups */
83     std::ranges::transform(systemLedMap, std::back_inserter(groups),
84                            [&bus, &manager, serializePtr](auto& grp) {
85                                return std::make_unique<phosphor::led::Group>(
86                                    bus, grp.first, manager, serializePtr);
87                            });
88 
89     // Attach the bus to sd_event to service user requests
90     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
91 
92     /** @brief Claim the bus */
93     bus.request_name("xyz.openbmc_project.LED.GroupManager");
94     event.loop();
95 
96     return 0;
97 }
98