blob: 8861a64f4dda787844dcb4a49f81e108727c066e [file] [log] [blame]
Patrick Venture91ac8d32018-11-01 17:03:22 -07001#include "config.h"
2
3#include "group.hpp"
George Liudef5f5a2020-04-10 11:23:52 +08004#ifdef LED_USE_JSON
George Liu616a0712021-02-18 10:50:24 +08005#include "json-parser.hpp"
George Liudef5f5a2020-04-10 11:23:52 +08006#else
Patrick Venture91ac8d32018-11-01 17:03:22 -07007#include "led-gen.hpp"
George Liudef5f5a2020-04-10 11:23:52 +08008#endif
Vishwanatha Subbannaed490732016-12-20 15:59:29 +05309#include "ledlayout.hpp"
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053010#include "manager.hpp"
George Liu2098aa62020-05-09 11:26:35 +080011#include "serialize.hpp"
George Liu1c737af2020-10-16 09:07:02 +080012#include "utils.hpp"
George Liuc777bef2020-11-23 17:04:21 +080013#ifdef USE_LAMP_TEST
Patrick Williams953315d2022-03-16 14:30:39 -050014#include "lamptest/lamptest.hpp"
George Liuc777bef2020-11-23 17:04:21 +080015#endif
16
Patrick Williams7217c032022-03-16 16:26:09 -050017#include <CLI/CLI.hpp>
George Liuc777bef2020-11-23 17:04:21 +080018#include <sdeventplus/event.hpp>
Patrick Venture91ac8d32018-11-01 17:03:22 -070019
George Liuc5e0f312021-12-27 15:54:31 +080020#include <algorithm>
Patrick Venture91ac8d32018-11-01 17:03:22 -070021#include <iostream>
Vishwanatha Subbannab21fda72016-10-17 17:46:37 +053022
Patrick Williams7217c032022-03-16 16:26:09 -050023int main(int argc, char** argv)
Vishwanatha Subbannab21fda72016-10-17 17:46:37 +053024{
Patrick Williams7217c032022-03-16 16:26:09 -050025 CLI::App app("phosphor-led-manager");
26
27#ifdef LED_USE_JSON
28 std::string configFile{};
29 app.add_option("-c,--config", configFile, "Path to JSON config");
30#endif
31
32 CLI11_PARSE(app, argc, argv);
33
George Liuc777bef2020-11-23 17:04:21 +080034 // Get a default event loop
35 auto event = sdeventplus::Event::get_default();
36
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053037 /** @brief Dbus constructs used by LED Group manager */
George Liu1c737af2020-10-16 09:07:02 +080038 auto& bus = phosphor::led::utils::DBusHandler::getBus();
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053039
George Liudef5f5a2020-04-10 11:23:52 +080040#ifdef LED_USE_JSON
Patrick Williams7217c032022-03-16 16:26:09 -050041 auto systemLedMap = getSystemLedMap(configFile);
George Liudef5f5a2020-04-10 11:23:52 +080042#endif
43
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +053044 /** @brief Group manager object */
45 phosphor::led::Manager manager(bus, systemLedMap);
46
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053047 /** @brief sd_bus object manager */
48 sdbusplus::server::manager::manager objManager(bus, OBJPATH);
49
50 /** @brief vector of led groups */
51 std::vector<std::unique_ptr<phosphor::led::Group>> groups;
52
George Liu2098aa62020-05-09 11:26:35 +080053 /** @brief store and re-store Group */
54 phosphor::led::Serialize serialize(SAVED_GROUPS_FILE);
55
George Liuc777bef2020-11-23 17:04:21 +080056#ifdef USE_LAMP_TEST
57 phosphor::led::LampTest lampTest(event, manager);
58
59 groups.emplace_back(std::make_unique<phosphor::led::Group>(
60 bus, LAMP_TEST_OBJECT, manager, serialize,
61 std::bind(std::mem_fn(&phosphor::led::LampTest::requestHandler),
George Liu87fd11c2020-11-23 16:40:14 +080062 &lampTest, std::placeholders::_1, std::placeholders::_2)));
George Liub6151622020-11-23 18:16:18 +080063
64 // Register a lamp test method in the manager class, and call this method
65 // when the lamp test is started
66 manager.setLampTestCallBack(
67 std::bind(std::mem_fn(&phosphor::led::LampTest::processLEDUpdates),
68 &lampTest, std::placeholders::_1, std::placeholders::_2));
George Liuc777bef2020-11-23 17:04:21 +080069#endif
70
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053071 /** Now create so many dbus objects as there are groups */
George Liuc5e0f312021-12-27 15:54:31 +080072 std::ranges::transform(
73 systemLedMap, std::back_inserter(groups),
74 [&bus, &manager, &serialize](
75 const std::pair<std::string,
76 std::set<phosphor::led::Layout::LedAction>>& grp) {
77 return std::make_unique<phosphor::led::Group>(bus, grp.first,
78 manager, serialize);
79 });
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053080
George Liuc777bef2020-11-23 17:04:21 +080081 // Attach the bus to sd_event to service user requests
82 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
83
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053084 /** @brief Claim the bus */
85 bus.request_name(BUSNAME);
George Liuc777bef2020-11-23 17:04:21 +080086 event.loop();
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053087
Vishwanatha Subbannab21fda72016-10-17 17:46:37 +053088 return 0;
89}