xref: /openbmc/phosphor-led-manager/manager/led-main.cpp (revision 158b2c14f54c78d334a21ca3656dc9364f598321)
1953315d2SPatrick Williams #include "config.h"
2953315d2SPatrick Williams 
3953315d2SPatrick Williams #include "group.hpp"
4*158b2c14SPatrick Williams #include "ledlayout.hpp"
5953315d2SPatrick Williams #ifdef LED_USE_JSON
6953315d2SPatrick Williams #include "json-parser.hpp"
7953315d2SPatrick Williams #else
8953315d2SPatrick Williams #include "led-gen.hpp"
9953315d2SPatrick Williams #endif
10953315d2SPatrick Williams #include "manager.hpp"
11953315d2SPatrick Williams #include "serialize.hpp"
12953315d2SPatrick Williams #include "utils.hpp"
13953315d2SPatrick Williams #ifdef USE_LAMP_TEST
14953315d2SPatrick Williams #include "lamptest/lamptest.hpp"
15953315d2SPatrick Williams #endif
16953315d2SPatrick Williams 
177217c035SPatrick Williams #include <CLI/CLI.hpp>
18953315d2SPatrick Williams #include <sdeventplus/event.hpp>
19953315d2SPatrick Williams 
20953315d2SPatrick Williams #include <algorithm>
21953315d2SPatrick Williams #include <iostream>
22953315d2SPatrick Williams 
237217c035SPatrick Williams int main(int argc, char** argv)
24953315d2SPatrick Williams {
257217c035SPatrick Williams     CLI::App app("phosphor-led-manager");
267217c035SPatrick Williams 
277217c035SPatrick Williams #ifdef LED_USE_JSON
287217c035SPatrick Williams     std::string configFile{};
297217c035SPatrick Williams     app.add_option("-c,--config", configFile, "Path to JSON config");
307217c035SPatrick Williams #endif
317217c035SPatrick Williams 
327217c035SPatrick Williams     CLI11_PARSE(app, argc, argv);
337217c035SPatrick Williams 
34953315d2SPatrick Williams     // Get a default event loop
35953315d2SPatrick Williams     auto event = sdeventplus::Event::get_default();
36953315d2SPatrick Williams 
37953315d2SPatrick Williams     /** @brief Dbus constructs used by LED Group manager */
38953315d2SPatrick Williams     auto& bus = phosphor::led::utils::DBusHandler::getBus();
39953315d2SPatrick Williams 
40953315d2SPatrick Williams #ifdef LED_USE_JSON
417217c035SPatrick Williams     auto systemLedMap = getSystemLedMap(configFile);
42953315d2SPatrick Williams #endif
43953315d2SPatrick Williams 
44953315d2SPatrick Williams     /** @brief Group manager object */
45953315d2SPatrick Williams     phosphor::led::Manager manager(bus, systemLedMap);
46953315d2SPatrick Williams 
47953315d2SPatrick Williams     /** @brief sd_bus object manager */
48953315d2SPatrick Williams     sdbusplus::server::manager::manager objManager(bus, OBJPATH);
49953315d2SPatrick Williams 
50953315d2SPatrick Williams     /** @brief vector of led groups */
51953315d2SPatrick Williams     std::vector<std::unique_ptr<phosphor::led::Group>> groups;
52953315d2SPatrick Williams 
53953315d2SPatrick Williams     /** @brief store and re-store Group */
54953315d2SPatrick Williams     phosphor::led::Serialize serialize(SAVED_GROUPS_FILE);
55953315d2SPatrick Williams 
56953315d2SPatrick Williams #ifdef USE_LAMP_TEST
57953315d2SPatrick Williams     phosphor::led::LampTest lampTest(event, manager);
58953315d2SPatrick Williams 
59953315d2SPatrick Williams     groups.emplace_back(std::make_unique<phosphor::led::Group>(
60953315d2SPatrick Williams         bus, LAMP_TEST_OBJECT, manager, serialize,
61953315d2SPatrick Williams         std::bind(std::mem_fn(&phosphor::led::LampTest::requestHandler),
62953315d2SPatrick Williams                   &lampTest, std::placeholders::_1, std::placeholders::_2)));
63953315d2SPatrick Williams 
64953315d2SPatrick Williams     // Register a lamp test method in the manager class, and call this method
65953315d2SPatrick Williams     // when the lamp test is started
66953315d2SPatrick Williams     manager.setLampTestCallBack(
67953315d2SPatrick Williams         std::bind(std::mem_fn(&phosphor::led::LampTest::processLEDUpdates),
68953315d2SPatrick Williams                   &lampTest, std::placeholders::_1, std::placeholders::_2));
69953315d2SPatrick Williams #endif
70953315d2SPatrick Williams 
71953315d2SPatrick Williams     /** Now create so many dbus objects as there are groups */
72*158b2c14SPatrick Williams     std::ranges::transform(systemLedMap, std::back_inserter(groups),
73*158b2c14SPatrick Williams                            [&bus, &manager, &serialize](auto& grp) {
74*158b2c14SPatrick Williams                                return std::make_unique<phosphor::led::Group>(
75*158b2c14SPatrick Williams                                    bus, grp.first, manager, serialize);
76953315d2SPatrick Williams                            });
77953315d2SPatrick Williams 
78953315d2SPatrick Williams     // Attach the bus to sd_event to service user requests
79953315d2SPatrick Williams     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
80953315d2SPatrick Williams 
81953315d2SPatrick Williams     /** @brief Claim the bus */
82953315d2SPatrick Williams     bus.request_name(BUSNAME);
83953315d2SPatrick Williams     event.loop();
84953315d2SPatrick Williams 
85953315d2SPatrick Williams     return 0;
86953315d2SPatrick Williams }
87