blob: 8bc17514988d033494075bc60c86c5f94745ad73 [file] [log] [blame]
Vishwanatha Subbannab21fda72016-10-17 17:46:37 +05301#include <iostream>
2#include <cstring>
3#include <sdbusplus/vtable.hpp>
4#include <sdbusplus/message.hpp>
5#include <sdbusplus/bus.hpp>
6#include "led-manager.hpp"
7#include "led-gen.hpp"
8
9namespace phosphor
10{
11
12namespace led
13{
14
15/** @brief Called when the group's property is read
16 * Signature as needed by sd_bus
17 */
18int getGroupState(sd_bus *bus, const char *path, const char *interface,
19 const char *property, sd_bus_message *reply,
20 void *data, sd_bus_error* error)
21{
22 auto group = strrchr(path, '/');
23 if (group)
24 {
25 // Removing the starting '/' in /group
26 group++;
27 }
28
29 //TODO : Need to see how to represent group specific asserted state
30 // May be a new tuple / map ?
31 sd_bus_message_append(reply, "b", 0);
32 return 0;
33}
34
35/** @brief Called when the group's asserted state is updated
36 * Signature as needed by sd_bus
37 */
38int setGroupState(sd_bus *bus, const char *path, const char *interface,
39 const char *property, sd_bus_message *value,
40 void *data, sd_bus_error* error)
41{
42 bool state {};
43 auto group = strrchr(path, '/');
44 if (group)
45 {
46 // Removing the starting '/' in /group
47 group++;
48 }
49
50 auto msg = sdbusplus::message::message(value);
51 sd_bus_message_ref(value);
52 msg.read(state);
53
54 //TODO : Need to see how to represent group specific asserted state
55 // May be a new tuple / map ?
56 return 1;
57}
58
59/** @brief Users having to assert a group will just turn this property to 1
60 * similarly, setting this property to 0 will deassert the group
61 */
62constexpr sdbusplus::vtable::vtable_t led_vtable[] =
63{
64 sdbusplus::vtable::start(),
65 sdbusplus::vtable::property("Assert", "b",
66 getGroupState, setGroupState, sdbusplus::vtable::property_::emits_change),
67 sdbusplus::vtable::end()
68};
69
70/** @brief Initialize the bus and announce services */
71Manager::Manager(const char* busName,
72 const char* objPath,
73 const char* intfName) :
74 iv_bus(sdbusplus::bus::new_system()),
75 objManager(iv_bus, objPath)
76{
77 // Like /org/openbmc/ledmanager/groups/
78 auto path = std::string(objPath) + "/";
79
80 /** Now create so many dbus objects as there are groups */
81 for (auto &grp: Manager::cv_LedMap)
82 {
83 auto grpPath = path + grp.first;
84 intfContainer.emplace_back(sdbusplus::server::interface::interface(
85 iv_bus, grpPath.c_str(), intfName, led_vtable, this));
86
87 // These are now set of structs having LED name and the action. Do not
88 // have anything to be done here at the moment but need to create a
89 // mapping between led names to device strigs eventually
90 //for (auto &set: grp.second)
91 //{
92
93 //}
94 }
95 // Once done, claim the bus and systemd will
96 // consider this service started
97 iv_bus.request_name(busName);
98}
99
100/** @brief Wait for client requests */
101void Manager::run()
102{
103 while(true)
104 {
105 try
106 {
107 iv_bus.process_discard();
108 iv_bus.wait();
109 }
110 catch (std::exception &e)
111 {
112 std::cerr << e.what() << std::endl;
113 }
114 }
115}
116
117} // namespace led
118
119} // namespace phosphor