Josh D. King | bdd9cb7 | 2016-12-19 11:13:43 -0600 | [diff] [blame^] | 1 | #include <algorithm> |
| 2 | #include <sdbusplus/server.hpp> |
| 3 | #include <sdbusplus/exception.hpp> |
| 4 | #include <xyz/openbmc_project/State/BMC/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 | BMC::BMC(bus::bus& bus, const char* path) |
| 18 | : _xyz_openbmc_project_State_BMC_interface( |
| 19 | bus, path, _interface, _vtable, this) |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | |
| 24 | |
| 25 | auto BMC::requestedBMCTransition() const -> |
| 26 | Transition |
| 27 | { |
| 28 | return _requestedBMCTransition; |
| 29 | } |
| 30 | |
| 31 | int BMC::_callback_get_RequestedBMCTransition( |
| 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<BMC*>(context); |
| 43 | m.append(convertForMessage(o->requestedBMCTransition())); |
| 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 BMC::requestedBMCTransition(Transition value) -> |
| 55 | Transition |
| 56 | { |
| 57 | if (_requestedBMCTransition != value) |
| 58 | { |
| 59 | _requestedBMCTransition = value; |
| 60 | _xyz_openbmc_project_State_BMC_interface.property_changed("RequestedBMCTransition"); |
| 61 | } |
| 62 | |
| 63 | return _requestedBMCTransition; |
| 64 | } |
| 65 | |
| 66 | int BMC::_callback_set_RequestedBMCTransition( |
| 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<BMC*>(context); |
| 76 | |
| 77 | std::string v{}; |
| 78 | m.read(v); |
| 79 | o->requestedBMCTransition(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 BMC |
| 93 | { |
| 94 | static const auto _property_RequestedBMCTransition = |
| 95 | utility::tuple_to_array(message::types::type_id< |
| 96 | std::string>()); |
| 97 | } |
| 98 | } |
| 99 | auto BMC::currentBMCState() const -> |
| 100 | BMCState |
| 101 | { |
| 102 | return _currentBMCState; |
| 103 | } |
| 104 | |
| 105 | int BMC::_callback_get_CurrentBMCState( |
| 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<BMC*>(context); |
| 117 | m.append(convertForMessage(o->currentBMCState())); |
| 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 BMC::currentBMCState(BMCState value) -> |
| 129 | BMCState |
| 130 | { |
| 131 | if (_currentBMCState != value) |
| 132 | { |
| 133 | _currentBMCState = value; |
| 134 | _xyz_openbmc_project_State_BMC_interface.property_changed("CurrentBMCState"); |
| 135 | } |
| 136 | |
| 137 | return _currentBMCState; |
| 138 | } |
| 139 | |
| 140 | int BMC::_callback_set_CurrentBMCState( |
| 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<BMC*>(context); |
| 150 | |
| 151 | std::string v{}; |
| 152 | m.read(v); |
| 153 | o->currentBMCState(convertBMCStateFromString(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 BMC |
| 167 | { |
| 168 | static const auto _property_CurrentBMCState = |
| 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 BMC::Transition */ |
| 178 | static const std::tuple<const char*, BMC::Transition> mappingBMCTransition[] = |
| 179 | { |
| 180 | std::make_tuple( "xyz.openbmc_project.State.BMC.Transition.Reboot", BMC::Transition::Reboot ), |
| 181 | std::make_tuple( "xyz.openbmc_project.State.BMC.Transition.None", BMC::Transition::None ), |
| 182 | }; |
| 183 | |
| 184 | } // anonymous namespace |
| 185 | |
| 186 | auto BMC::convertTransitionFromString(std::string& s) -> |
| 187 | Transition |
| 188 | { |
| 189 | auto i = std::find_if( |
| 190 | std::begin(mappingBMCTransition), |
| 191 | std::end(mappingBMCTransition), |
| 192 | [&s](auto& e){ return 0 == strcmp(s.c_str(), std::get<0>(e)); } ); |
| 193 | if (std::end(mappingBMCTransition) == i) |
| 194 | { |
| 195 | throw sdbusplus::exception::InvalidEnumString(); |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | return std::get<1>(*i); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | std::string convertForMessage(BMC::Transition v) |
| 204 | { |
| 205 | auto i = std::find_if( |
| 206 | std::begin(mappingBMCTransition), |
| 207 | std::end(mappingBMCTransition), |
| 208 | [v](auto& e){ return v == std::get<1>(e); }); |
| 209 | return std::get<0>(*i); |
| 210 | } |
| 211 | |
| 212 | namespace |
| 213 | { |
| 214 | /** String to enum mapping for BMC::BMCState */ |
| 215 | static const std::tuple<const char*, BMC::BMCState> mappingBMCBMCState[] = |
| 216 | { |
| 217 | std::make_tuple( "xyz.openbmc_project.State.BMC.BMCState.Ready", BMC::BMCState::Ready ), |
| 218 | std::make_tuple( "xyz.openbmc_project.State.BMC.BMCState.NotReady", BMC::BMCState::NotReady ), |
| 219 | }; |
| 220 | |
| 221 | } // anonymous namespace |
| 222 | |
| 223 | auto BMC::convertBMCStateFromString(std::string& s) -> |
| 224 | BMCState |
| 225 | { |
| 226 | auto i = std::find_if( |
| 227 | std::begin(mappingBMCBMCState), |
| 228 | std::end(mappingBMCBMCState), |
| 229 | [&s](auto& e){ return 0 == strcmp(s.c_str(), std::get<0>(e)); } ); |
| 230 | if (std::end(mappingBMCBMCState) == i) |
| 231 | { |
| 232 | throw sdbusplus::exception::InvalidEnumString(); |
| 233 | } |
| 234 | else |
| 235 | { |
| 236 | return std::get<1>(*i); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | std::string convertForMessage(BMC::BMCState v) |
| 241 | { |
| 242 | auto i = std::find_if( |
| 243 | std::begin(mappingBMCBMCState), |
| 244 | std::end(mappingBMCBMCState), |
| 245 | [v](auto& e){ return v == std::get<1>(e); }); |
| 246 | return std::get<0>(*i); |
| 247 | } |
| 248 | |
| 249 | const vtable::vtable_t BMC::_vtable[] = { |
| 250 | vtable::start(), |
| 251 | vtable::property("RequestedBMCTransition", |
| 252 | details::BMC::_property_RequestedBMCTransition |
| 253 | .data(), |
| 254 | _callback_get_RequestedBMCTransition, |
| 255 | _callback_set_RequestedBMCTransition, |
| 256 | vtable::property_::emits_change), |
| 257 | vtable::property("CurrentBMCState", |
| 258 | details::BMC::_property_CurrentBMCState |
| 259 | .data(), |
| 260 | _callback_get_CurrentBMCState, |
| 261 | _callback_set_CurrentBMCState, |
| 262 | vtable::property_::emits_change), |
| 263 | vtable::end() |
| 264 | }; |
| 265 | |
| 266 | } // namespace server |
| 267 | } // namespace State |
| 268 | } // namespace openbmc_project |
| 269 | } // namespace xyz |
| 270 | } // namespace sdbusplus |
| 271 | |