xref: /openbmc/phosphor-power/phosphor-regulators/src/interfaces/manager_interface.cpp (revision 72e584c5bae66b00a55e513dffc410e510348c26)
1 /**
2  * Copyright © 2020 IBM Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "manager_interface.hpp"
18 
19 #include <phosphor-logging/lg2.hpp>
20 #include <sdbusplus/exception.hpp>
21 #include <sdbusplus/sdbus.hpp>
22 #include <sdbusplus/server.hpp>
23 
24 #include <string>
25 #include <tuple>
26 
27 namespace phosphor
28 {
29 namespace power
30 {
31 namespace regulators
32 {
33 namespace interface
34 {
35 
ManagerInterface(sdbusplus::bus_t & bus,const char * path)36 ManagerInterface::ManagerInterface(sdbusplus::bus_t& bus, const char* path) :
37     _serverInterface(bus, path, interface, _vtable, this)
38 {}
39 
callbackConfigure(sd_bus_message * msg,void * context,sd_bus_error * error)40 int ManagerInterface::callbackConfigure(sd_bus_message* msg, void* context,
41                                         sd_bus_error* error)
42 {
43     if (msg != nullptr && context != nullptr)
44     {
45         try
46         {
47             auto m = sdbusplus::message_t(msg);
48 
49             auto mgrObj = static_cast<ManagerInterface*>(context);
50             mgrObj->configure();
51 
52             auto reply = m.new_method_return();
53 
54             reply.method_return();
55         }
56         catch (const sdbusplus::exception_t& e)
57         {
58             return sd_bus_error_set(error, e.name(), e.description());
59         }
60     }
61     else
62     {
63         // The message or context were null
64         lg2::error("Unable to service Configure method callback");
65         return -1;
66     }
67 
68     return 1;
69 }
70 
callbackMonitor(sd_bus_message * msg,void * context,sd_bus_error * error)71 int ManagerInterface::callbackMonitor(sd_bus_message* msg, void* context,
72                                       sd_bus_error* error)
73 {
74     if (msg != nullptr && context != nullptr)
75     {
76         try
77         {
78             bool enable{};
79             auto m = sdbusplus::message_t(msg);
80 
81             m.read(enable);
82 
83             auto mgrObj = static_cast<ManagerInterface*>(context);
84             mgrObj->monitor(enable);
85 
86             auto reply = m.new_method_return();
87 
88             reply.method_return();
89         }
90         catch (const sdbusplus::exception_t& e)
91         {
92             return sd_bus_error_set(error, e.name(), e.description());
93         }
94     }
95     else
96     {
97         // The message or context were null
98         lg2::error("Unable to service Monitor method callback");
99         return -1;
100     }
101 
102     return 1;
103 }
104 
105 const sdbusplus::vtable::vtable_t ManagerInterface::_vtable[] = {
106     sdbusplus::vtable::start(),
107     // No configure method parameters and returns void
108     sdbusplus::vtable::method("Configure", "", "", callbackConfigure),
109     // Monitor method takes a boolean parameter and returns void
110     sdbusplus::vtable::method("Monitor", "b", "", callbackMonitor),
111     sdbusplus::vtable::end()};
112 
113 } // namespace interface
114 } // namespace regulators
115 } // namespace power
116 } // namespace phosphor
117