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     /** @brief Group manager object */
46     phosphor::led::Manager manager(bus, systemLedMap, event);
47 
48     /** @brief sd_bus object manager */
49     sdbusplus::server::manager_t objManager(bus,
50                                             "/xyz/openbmc_project/led/groups");
51 
52     /** @brief vector of led groups */
53     std::vector<std::unique_ptr<phosphor::led::Group>> groups;
54 
55     std::shared_ptr<phosphor::led::Serialize> serializePtr = nullptr;
56 #ifdef PERSISTENT_LED_ASSERTED
57     /** @brief store and re-store Group */
58     serializePtr =
59         std::make_shared<phosphor::led::Serialize>(SAVED_GROUPS_FILE);
60 #endif
61 
62 #ifdef USE_LAMP_TEST
63     phosphor::led::LampTest lampTest(event, manager);
64 
65     // Clear leds triggered by lamp test in previous boot
66     lampTest.clearLamps();
67 
68     groups.emplace_back(std::make_unique<phosphor::led::Group>(
69         bus, LAMP_TEST_OBJECT, manager, serializePtr,
70         std::bind(std::mem_fn(&phosphor::led::LampTest::requestHandler),
71                   &lampTest, std::placeholders::_1, std::placeholders::_2)));
72 
73     // Register a lamp test method in the manager class, and call this method
74     // when the lamp test is started
75     manager.setLampTestCallBack(
76         std::bind(std::mem_fn(&phosphor::led::LampTest::processLEDUpdates),
77                   &lampTest, std::placeholders::_1, std::placeholders::_2));
78 #endif
79 
80     /** Now create so many dbus objects as there are groups */
81     std::ranges::transform(systemLedMap, std::back_inserter(groups),
82                            [&bus, &manager, serializePtr](auto& grp) {
83                                return std::make_unique<phosphor::led::Group>(
84                                    bus, grp.first, manager, serializePtr);
85                            });
86 
87     // Attach the bus to sd_event to service user requests
88     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
89 
90     /** @brief Claim the bus */
91     bus.request_name("xyz.openbmc_project.LED.GroupManager");
92     event.loop();
93 
94     return 0;
95 }
96