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/log.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         using namespace phosphor::logging;
65         log<level::ERR>("Unable to service Configure method callback");
66         return -1;
67     }
68 
69     return 1;
70 }
71 
callbackMonitor(sd_bus_message * msg,void * context,sd_bus_error * error)72 int ManagerInterface::callbackMonitor(sd_bus_message* msg, void* context,
73                                       sd_bus_error* error)
74 {
75     if (msg != nullptr && context != nullptr)
76     {
77         try
78         {
79             bool enable{};
80             auto m = sdbusplus::message_t(msg);
81 
82             m.read(enable);
83 
84             auto mgrObj = static_cast<ManagerInterface*>(context);
85             mgrObj->monitor(enable);
86 
87             auto reply = m.new_method_return();
88 
89             reply.method_return();
90         }
91         catch (const sdbusplus::exception_t& e)
92         {
93             return sd_bus_error_set(error, e.name(), e.description());
94         }
95     }
96     else
97     {
98         // The message or context were null
99         using namespace phosphor::logging;
100         log<level::ERR>("Unable to service Monitor method callback");
101         return -1;
102     }
103 
104     return 1;
105 }
106 
107 const sdbusplus::vtable::vtable_t ManagerInterface::_vtable[] = {
108     sdbusplus::vtable::start(),
109     // No configure method parameters and returns void
110     sdbusplus::vtable::method("Configure", "", "", callbackConfigure),
111     // Monitor method takes a boolean parameter and returns void
112     sdbusplus::vtable::method("Monitor", "b", "", callbackMonitor),
113     sdbusplus::vtable::end()};
114 
115 } // namespace interface
116 } // namespace regulators
117 } // namespace power
118 } // namespace phosphor
119