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