Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 1 | #include <algorithm> |
| 2 | #include <sdbusplus/server.hpp> |
| 3 | #include <sdbusplus/exception.hpp> |
| 4 | #include <xyz/openbmc_project/State/Host/server.hpp> |
| 5 | |
| 6 | namespace sdbusplus |
| 7 | { |
| 8 | namespace xyz |
| 9 | { |
| 10 | namespace openbmc_project |
| 11 | { |
| 12 | namespace State |
| 13 | { |
| 14 | namespace server |
| 15 | { |
| 16 | |
| 17 | Host::Host(bus::bus& bus, const char* path) |
| 18 | : _xyz_openbmc_project_State_Host_interface( |
| 19 | bus, path, _interface, _vtable, this) |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | |
| 24 | |
| 25 | auto Host::requestedHostTransition() const -> |
| 26 | Transition |
| 27 | { |
| 28 | return _requestedHostTransition; |
| 29 | } |
| 30 | |
| 31 | int Host::_callback_get_RequestedHostTransition( |
| 32 | sd_bus* bus, const char* path, const char* interface, |
| 33 | const char* property, sd_bus_message* reply, void* context, |
| 34 | sd_bus_error* error) |
| 35 | { |
| 36 | using sdbusplus::server::binding::details::convertForMessage; |
| 37 | |
| 38 | try |
| 39 | { |
| 40 | auto m = message::message(sd_bus_message_ref(reply)); |
| 41 | |
| 42 | auto o = static_cast<Host*>(context); |
| 43 | m.append(convertForMessage(o->requestedHostTransition())); |
| 44 | } |
| 45 | catch(sdbusplus::internal_exception_t& e) |
| 46 | { |
| 47 | sd_bus_error_set_const(error, e.name(), e.description()); |
| 48 | return -EINVAL; |
| 49 | } |
| 50 | |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | auto Host::requestedHostTransition(Transition value) -> |
| 55 | Transition |
| 56 | { |
| 57 | if (_requestedHostTransition != value) |
| 58 | { |
| 59 | _requestedHostTransition = value; |
| 60 | _xyz_openbmc_project_State_Host_interface.property_changed("RequestedHostTransition"); |
| 61 | } |
| 62 | |
| 63 | return _requestedHostTransition; |
| 64 | } |
| 65 | |
| 66 | int Host::_callback_set_RequestedHostTransition( |
| 67 | sd_bus* bus, const char* path, const char* interface, |
| 68 | const char* property, sd_bus_message* value, void* context, |
| 69 | sd_bus_error* error) |
| 70 | { |
| 71 | try |
| 72 | { |
| 73 | auto m = message::message(sd_bus_message_ref(value)); |
| 74 | |
| 75 | auto o = static_cast<Host*>(context); |
| 76 | |
| 77 | std::string v{}; |
| 78 | m.read(v); |
| 79 | o->requestedHostTransition(convertTransitionFromString(v)); |
| 80 | } |
| 81 | catch(sdbusplus::internal_exception_t& e) |
| 82 | { |
| 83 | sd_bus_error_set_const(error, e.name(), e.description()); |
| 84 | return -EINVAL; |
| 85 | } |
| 86 | |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | namespace details |
| 91 | { |
| 92 | namespace Host |
| 93 | { |
| 94 | static const auto _property_RequestedHostTransition = |
| 95 | utility::tuple_to_array(message::types::type_id< |
| 96 | std::string>()); |
| 97 | } |
| 98 | } |
| 99 | auto Host::currentHostState() const -> |
| 100 | HostState |
| 101 | { |
| 102 | return _currentHostState; |
| 103 | } |
| 104 | |
| 105 | int Host::_callback_get_CurrentHostState( |
| 106 | sd_bus* bus, const char* path, const char* interface, |
| 107 | const char* property, sd_bus_message* reply, void* context, |
| 108 | sd_bus_error* error) |
| 109 | { |
| 110 | using sdbusplus::server::binding::details::convertForMessage; |
| 111 | |
| 112 | try |
| 113 | { |
| 114 | auto m = message::message(sd_bus_message_ref(reply)); |
| 115 | |
| 116 | auto o = static_cast<Host*>(context); |
| 117 | m.append(convertForMessage(o->currentHostState())); |
| 118 | } |
| 119 | catch(sdbusplus::internal_exception_t& e) |
| 120 | { |
| 121 | sd_bus_error_set_const(error, e.name(), e.description()); |
| 122 | return -EINVAL; |
| 123 | } |
| 124 | |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | auto Host::currentHostState(HostState value) -> |
| 129 | HostState |
| 130 | { |
| 131 | if (_currentHostState != value) |
| 132 | { |
| 133 | _currentHostState = value; |
| 134 | _xyz_openbmc_project_State_Host_interface.property_changed("CurrentHostState"); |
| 135 | } |
| 136 | |
| 137 | return _currentHostState; |
| 138 | } |
| 139 | |
| 140 | int Host::_callback_set_CurrentHostState( |
| 141 | sd_bus* bus, const char* path, const char* interface, |
| 142 | const char* property, sd_bus_message* value, void* context, |
| 143 | sd_bus_error* error) |
| 144 | { |
| 145 | try |
| 146 | { |
| 147 | auto m = message::message(sd_bus_message_ref(value)); |
| 148 | |
| 149 | auto o = static_cast<Host*>(context); |
| 150 | |
| 151 | std::string v{}; |
| 152 | m.read(v); |
| 153 | o->currentHostState(convertHostStateFromString(v)); |
| 154 | } |
| 155 | catch(sdbusplus::internal_exception_t& e) |
| 156 | { |
| 157 | sd_bus_error_set_const(error, e.name(), e.description()); |
| 158 | return -EINVAL; |
| 159 | } |
| 160 | |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | namespace details |
| 165 | { |
| 166 | namespace Host |
| 167 | { |
| 168 | static const auto _property_CurrentHostState = |
| 169 | utility::tuple_to_array(message::types::type_id< |
| 170 | std::string>()); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | |
| 175 | namespace |
| 176 | { |
| 177 | /** String to enum mapping for Host::Transition */ |
| 178 | static const std::tuple<const char*, Host::Transition> mappingHostTransition[] = |
| 179 | { |
| 180 | std::make_tuple( "xyz.openbmc_project.State.Host.Transition.Off", Host::Transition::Off ), |
| 181 | std::make_tuple( "xyz.openbmc_project.State.Host.Transition.On", Host::Transition::On ), |
| 182 | std::make_tuple( "xyz.openbmc_project.State.Host.Transition.Reboot", Host::Transition::Reboot ), |
| 183 | }; |
| 184 | |
| 185 | } // anonymous namespace |
| 186 | |
| 187 | auto Host::convertTransitionFromString(std::string& s) -> |
| 188 | Transition |
| 189 | { |
| 190 | auto i = std::find_if( |
| 191 | std::begin(mappingHostTransition), |
| 192 | std::end(mappingHostTransition), |
| 193 | [&s](auto& e){ return 0 == strcmp(s.c_str(), std::get<0>(e)); } ); |
| 194 | if (std::end(mappingHostTransition) == i) |
| 195 | { |
| 196 | throw sdbusplus::exception::InvalidEnumString(); |
| 197 | } |
| 198 | else |
| 199 | { |
| 200 | return std::get<1>(*i); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | std::string convertForMessage(Host::Transition v) |
| 205 | { |
| 206 | auto i = std::find_if( |
| 207 | std::begin(mappingHostTransition), |
| 208 | std::end(mappingHostTransition), |
| 209 | [v](auto& e){ return v == std::get<1>(e); }); |
| 210 | return std::get<0>(*i); |
| 211 | } |
| 212 | |
| 213 | namespace |
| 214 | { |
| 215 | /** String to enum mapping for Host::HostState */ |
| 216 | static const std::tuple<const char*, Host::HostState> mappingHostHostState[] = |
| 217 | { |
| 218 | std::make_tuple( "xyz.openbmc_project.State.Host.HostState.Off", Host::HostState::Off ), |
| 219 | std::make_tuple( "xyz.openbmc_project.State.Host.HostState.Running", Host::HostState::Running ), |
| 220 | }; |
| 221 | |
| 222 | } // anonymous namespace |
| 223 | |
| 224 | auto Host::convertHostStateFromString(std::string& s) -> |
| 225 | HostState |
| 226 | { |
| 227 | auto i = std::find_if( |
| 228 | std::begin(mappingHostHostState), |
| 229 | std::end(mappingHostHostState), |
| 230 | [&s](auto& e){ return 0 == strcmp(s.c_str(), std::get<0>(e)); } ); |
| 231 | if (std::end(mappingHostHostState) == i) |
| 232 | { |
| 233 | throw sdbusplus::exception::InvalidEnumString(); |
| 234 | } |
| 235 | else |
| 236 | { |
| 237 | return std::get<1>(*i); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | std::string convertForMessage(Host::HostState v) |
| 242 | { |
| 243 | auto i = std::find_if( |
| 244 | std::begin(mappingHostHostState), |
| 245 | std::end(mappingHostHostState), |
| 246 | [v](auto& e){ return v == std::get<1>(e); }); |
| 247 | return std::get<0>(*i); |
| 248 | } |
| 249 | |
| 250 | const vtable::vtable_t Host::_vtable[] = { |
| 251 | vtable::start(), |
| 252 | vtable::property("RequestedHostTransition", |
| 253 | details::Host::_property_RequestedHostTransition |
| 254 | .data(), |
| 255 | _callback_get_RequestedHostTransition, |
| 256 | _callback_set_RequestedHostTransition, |
| 257 | vtable::property_::emits_change), |
| 258 | vtable::property("CurrentHostState", |
| 259 | details::Host::_property_CurrentHostState |
| 260 | .data(), |
| 261 | _callback_get_CurrentHostState, |
| 262 | _callback_set_CurrentHostState, |
| 263 | vtable::property_::emits_change), |
| 264 | vtable::end() |
| 265 | }; |
| 266 | |
| 267 | } // namespace server |
| 268 | } // namespace State |
| 269 | } // namespace openbmc_project |
| 270 | } // namespace xyz |
| 271 | } // namespace sdbusplus |
| 272 | |