xref: /openbmc/phosphor-led-manager/manager/led-main.cpp (revision 5467185344a5a8c327d0c860ded34b51d939b6d6)
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
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>
22*54671853SGeorge Liu #include <memory>
23953315d2SPatrick Williams 
247217c035SPatrick Williams int main(int argc, char** argv)
25953315d2SPatrick Williams {
267217c035SPatrick Williams     CLI::App app("phosphor-led-manager");
277217c035SPatrick Williams 
287217c035SPatrick Williams #ifdef LED_USE_JSON
297217c035SPatrick Williams     std::string configFile{};
307217c035SPatrick Williams     app.add_option("-c,--config", configFile, "Path to JSON config");
317217c035SPatrick Williams #endif
327217c035SPatrick Williams 
337217c035SPatrick Williams     CLI11_PARSE(app, argc, argv);
347217c035SPatrick Williams 
35953315d2SPatrick Williams     // Get a default event loop
36953315d2SPatrick Williams     auto event = sdeventplus::Event::get_default();
37953315d2SPatrick Williams 
38953315d2SPatrick Williams     /** @brief Dbus constructs used by LED Group manager */
39953315d2SPatrick Williams     auto& bus = phosphor::led::utils::DBusHandler::getBus();
40953315d2SPatrick Williams 
41953315d2SPatrick Williams #ifdef LED_USE_JSON
427217c035SPatrick Williams     auto systemLedMap = getSystemLedMap(configFile);
43953315d2SPatrick Williams #endif
44953315d2SPatrick Williams 
45953315d2SPatrick Williams     /** @brief Group manager object */
46f1ed4796SPotin Lai     phosphor::led::Manager manager(bus, systemLedMap, event);
47953315d2SPatrick Williams 
48953315d2SPatrick Williams     /** @brief sd_bus object manager */
495d92f423SGeorge Liu     sdbusplus::server::manager_t objManager(bus,
505d92f423SGeorge Liu                                             "/xyz/openbmc_project/led/groups");
51953315d2SPatrick Williams 
52953315d2SPatrick Williams     /** @brief vector of led groups */
53953315d2SPatrick Williams     std::vector<std::unique_ptr<phosphor::led::Group>> groups;
54953315d2SPatrick Williams 
55*54671853SGeorge Liu     std::shared_ptr<phosphor::led::Serialize> serializePtr = nullptr;
56*54671853SGeorge Liu #ifdef PERSISTENT_LED_ASSERTED
57953315d2SPatrick Williams     /** @brief store and re-store Group */
58*54671853SGeorge Liu     serializePtr =
59*54671853SGeorge Liu         std::make_shared<phosphor::led::Serialize>(SAVED_GROUPS_FILE);
60*54671853SGeorge Liu #endif
61953315d2SPatrick Williams 
62953315d2SPatrick Williams #ifdef USE_LAMP_TEST
63953315d2SPatrick Williams     phosphor::led::LampTest lampTest(event, manager);
64953315d2SPatrick Williams 
65953315d2SPatrick Williams     groups.emplace_back(std::make_unique<phosphor::led::Group>(
66*54671853SGeorge Liu         bus, LAMP_TEST_OBJECT, manager, serializePtr,
67953315d2SPatrick Williams         std::bind(std::mem_fn(&phosphor::led::LampTest::requestHandler),
68953315d2SPatrick Williams                   &lampTest, std::placeholders::_1, std::placeholders::_2)));
69953315d2SPatrick Williams 
70953315d2SPatrick Williams     // Register a lamp test method in the manager class, and call this method
71953315d2SPatrick Williams     // when the lamp test is started
72953315d2SPatrick Williams     manager.setLampTestCallBack(
73953315d2SPatrick Williams         std::bind(std::mem_fn(&phosphor::led::LampTest::processLEDUpdates),
74953315d2SPatrick Williams                   &lampTest, std::placeholders::_1, std::placeholders::_2));
75953315d2SPatrick Williams #endif
76953315d2SPatrick Williams 
77953315d2SPatrick Williams     /** Now create so many dbus objects as there are groups */
78158b2c14SPatrick Williams     std::ranges::transform(systemLedMap, std::back_inserter(groups),
79*54671853SGeorge Liu                            [&bus, &manager, serializePtr](auto& grp) {
80857da383SPatrick Williams         return std::make_unique<phosphor::led::Group>(bus, grp.first, manager,
81*54671853SGeorge Liu                                                       serializePtr);
82953315d2SPatrick Williams     });
83953315d2SPatrick Williams 
84953315d2SPatrick Williams     // Attach the bus to sd_event to service user requests
85953315d2SPatrick Williams     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
86953315d2SPatrick Williams 
87953315d2SPatrick Williams     /** @brief Claim the bus */
8825255212SGeorge Liu     bus.request_name("xyz.openbmc_project.LED.GroupManager");
89953315d2SPatrick Williams     event.loop();
90953315d2SPatrick Williams 
91953315d2SPatrick Williams     return 0;
92953315d2SPatrick Williams }
93