xref: /openbmc/phosphor-led-manager/manager/led-main.cpp (revision 16181d1d69f2590ac6919eccc4ee413c0f7989dc)
1953315d2SPatrick Williams #include "config.h"
2953315d2SPatrick Williams 
3953315d2SPatrick Williams #include "group.hpp"
4158b2c14SPatrick 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
10c0f7a8b4SAlexander Hansen #include "config-validator.hpp"
11953315d2SPatrick Williams #include "manager.hpp"
12953315d2SPatrick Williams #include "serialize.hpp"
13953315d2SPatrick Williams #include "utils.hpp"
14953315d2SPatrick Williams #ifdef USE_LAMP_TEST
15953315d2SPatrick Williams #include "lamptest/lamptest.hpp"
16953315d2SPatrick Williams #endif
17953315d2SPatrick Williams 
187217c035SPatrick Williams #include <CLI/CLI.hpp>
19953315d2SPatrick Williams #include <sdeventplus/event.hpp>
20953315d2SPatrick Williams 
21953315d2SPatrick Williams #include <algorithm>
22953315d2SPatrick Williams #include <iostream>
2354671853SGeorge Liu #include <memory>
24953315d2SPatrick Williams 
main(int argc,char ** argv)257217c035SPatrick Williams int main(int argc, char** argv)
26953315d2SPatrick Williams {
277217c035SPatrick Williams     CLI::App app("phosphor-led-manager");
287217c035SPatrick Williams 
297217c035SPatrick Williams #ifdef LED_USE_JSON
307217c035SPatrick Williams     std::string configFile{};
317217c035SPatrick Williams     app.add_option("-c,--config", configFile, "Path to JSON config");
327217c035SPatrick Williams #endif
337217c035SPatrick Williams 
347217c035SPatrick Williams     CLI11_PARSE(app, argc, argv);
357217c035SPatrick Williams 
36953315d2SPatrick Williams     // Get a default event loop
37953315d2SPatrick Williams     auto event = sdeventplus::Event::get_default();
38953315d2SPatrick Williams 
39953315d2SPatrick Williams     /** @brief Dbus constructs used by LED Group manager */
40953315d2SPatrick Williams     auto& bus = phosphor::led::utils::DBusHandler::getBus();
41953315d2SPatrick Williams 
42953315d2SPatrick Williams #ifdef LED_USE_JSON
437217c035SPatrick Williams     auto systemLedMap = getSystemLedMap(configFile);
44953315d2SPatrick Williams #endif
45953315d2SPatrick Williams 
46638d1487SAlexander Hansen     phosphor::led::validateConfigV1(systemLedMap);
47638d1487SAlexander Hansen 
48953315d2SPatrick Williams     /** @brief Group manager object */
49f1ed4796SPotin Lai     phosphor::led::Manager manager(bus, systemLedMap, event);
50953315d2SPatrick Williams 
51953315d2SPatrick Williams     /** @brief sd_bus object manager */
525d92f423SGeorge Liu     sdbusplus::server::manager_t objManager(bus,
535d92f423SGeorge Liu                                             "/xyz/openbmc_project/led/groups");
54953315d2SPatrick Williams 
55953315d2SPatrick Williams     /** @brief vector of led groups */
56953315d2SPatrick Williams     std::vector<std::unique_ptr<phosphor::led::Group>> groups;
57953315d2SPatrick Williams 
5854671853SGeorge Liu     std::shared_ptr<phosphor::led::Serialize> serializePtr = nullptr;
5954671853SGeorge Liu #ifdef PERSISTENT_LED_ASSERTED
60953315d2SPatrick Williams     /** @brief store and re-store Group */
6154671853SGeorge Liu     serializePtr =
6254671853SGeorge Liu         std::make_shared<phosphor::led::Serialize>(SAVED_GROUPS_FILE);
6354671853SGeorge Liu #endif
64953315d2SPatrick Williams 
65953315d2SPatrick Williams #ifdef USE_LAMP_TEST
66953315d2SPatrick Williams     phosphor::led::LampTest lampTest(event, manager);
67953315d2SPatrick Williams 
6894e894cbSManojkiran Eda     // Clear leds triggered by lamp test in previous boot
69*16181d1dSGeorge Liu     phosphor::led::LampTest::clearLamps();
70e3515c71SSunny Srivastava 
71953315d2SPatrick Williams     groups.emplace_back(std::make_unique<phosphor::led::Group>(
7254671853SGeorge Liu         bus, LAMP_TEST_OBJECT, manager, serializePtr,
73226059b8SGeorge Liu         [&lampTest](auto&& arg1, auto&& arg2) {
74226059b8SGeorge Liu             return lampTest.requestHandler(std::forward<decltype(arg1)>(arg1),
75226059b8SGeorge Liu                                            std::forward<decltype(arg2)>(arg2));
76226059b8SGeorge Liu         }));
77953315d2SPatrick Williams 
78953315d2SPatrick Williams     // Register a lamp test method in the manager class, and call this method
79953315d2SPatrick Williams     // when the lamp test is started
80226059b8SGeorge Liu     manager.setLampTestCallBack([&lampTest](auto&& arg1, auto&& arg2) {
81226059b8SGeorge Liu         return lampTest.processLEDUpdates(std::forward<decltype(arg1)>(arg1),
82226059b8SGeorge Liu                                           std::forward<decltype(arg2)>(arg2));
83226059b8SGeorge Liu     });
84953315d2SPatrick Williams #endif
85953315d2SPatrick Williams 
86953315d2SPatrick Williams     /** Now create so many dbus objects as there are groups */
87158b2c14SPatrick Williams     std::ranges::transform(systemLedMap, std::back_inserter(groups),
8854671853SGeorge Liu                            [&bus, &manager, serializePtr](auto& grp) {
89543ac9f1SPatrick Williams                                return std::make_unique<phosphor::led::Group>(
90543ac9f1SPatrick Williams                                    bus, grp.first, manager, serializePtr);
91953315d2SPatrick Williams                            });
92953315d2SPatrick Williams 
93953315d2SPatrick Williams     // Attach the bus to sd_event to service user requests
94953315d2SPatrick Williams     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
95953315d2SPatrick Williams 
96953315d2SPatrick Williams     /** @brief Claim the bus */
9725255212SGeorge Liu     bus.request_name("xyz.openbmc_project.LED.GroupManager");
98953315d2SPatrick Williams     event.loop();
99953315d2SPatrick Williams 
100953315d2SPatrick Williams     return 0;
101953315d2SPatrick Williams }
102