Gunnar Mills | 01a323b | 2017-01-18 09:48:13 -0600 | [diff] [blame^] | 1 | #include <algorithm> |
| 2 | #include <sdbusplus/server.hpp> |
| 3 | #include <sdbusplus/exception.hpp> |
| 4 | #include <xyz/openbmc_project/Software/Version/server.hpp> |
| 5 | |
| 6 | namespace sdbusplus |
| 7 | { |
| 8 | namespace xyz |
| 9 | { |
| 10 | namespace openbmc_project |
| 11 | { |
| 12 | namespace Software |
| 13 | { |
| 14 | namespace server |
| 15 | { |
| 16 | |
| 17 | Version::Version(bus::bus& bus, const char* path) |
| 18 | : _xyz_openbmc_project_Software_Version_interface( |
| 19 | bus, path, _interface, _vtable, this) |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | |
| 24 | |
| 25 | auto Version::version() const -> |
| 26 | std::string |
| 27 | { |
| 28 | return _version; |
| 29 | } |
| 30 | |
| 31 | int Version::_callback_get_Version( |
| 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<Version*>(context); |
| 43 | m.append(convertForMessage(o->version())); |
| 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 Version::version(std::string value) -> |
| 55 | std::string |
| 56 | { |
| 57 | if (_version != value) |
| 58 | { |
| 59 | _version = value; |
| 60 | _xyz_openbmc_project_Software_Version_interface.property_changed("Version"); |
| 61 | } |
| 62 | |
| 63 | return _version; |
| 64 | } |
| 65 | |
| 66 | int Version::_callback_set_Version( |
| 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<Version*>(context); |
| 76 | |
| 77 | std::string v{}; |
| 78 | m.read(v); |
| 79 | o->version(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 Version |
| 93 | { |
| 94 | static const auto _property_Version = |
| 95 | utility::tuple_to_array(message::types::type_id< |
| 96 | std::string>()); |
| 97 | } |
| 98 | } |
| 99 | auto Version::purpose() const -> |
| 100 | VersionPurpose |
| 101 | { |
| 102 | return _purpose; |
| 103 | } |
| 104 | |
| 105 | int Version::_callback_get_Purpose( |
| 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<Version*>(context); |
| 117 | m.append(convertForMessage(o->purpose())); |
| 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 Version::purpose(VersionPurpose value) -> |
| 129 | VersionPurpose |
| 130 | { |
| 131 | if (_purpose != value) |
| 132 | { |
| 133 | _purpose = value; |
| 134 | _xyz_openbmc_project_Software_Version_interface.property_changed("Purpose"); |
| 135 | } |
| 136 | |
| 137 | return _purpose; |
| 138 | } |
| 139 | |
| 140 | int Version::_callback_set_Purpose( |
| 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<Version*>(context); |
| 150 | |
| 151 | std::string v{}; |
| 152 | m.read(v); |
| 153 | o->purpose(convertVersionPurposeFromString(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 Version |
| 167 | { |
| 168 | static const auto _property_Purpose = |
| 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 Version::VersionPurpose */ |
| 178 | static const std::tuple<const char*, Version::VersionPurpose> mappingVersionVersionPurpose[] = |
| 179 | { |
| 180 | std::make_tuple( "xyz.openbmc_project.Software.Version.VersionPurpose.Unknown", Version::VersionPurpose::Unknown ), |
| 181 | std::make_tuple( "xyz.openbmc_project.Software.Version.VersionPurpose.Other", Version::VersionPurpose::Other ), |
| 182 | std::make_tuple( "xyz.openbmc_project.Software.Version.VersionPurpose.System", Version::VersionPurpose::System ), |
| 183 | std::make_tuple( "xyz.openbmc_project.Software.Version.VersionPurpose.BMC", Version::VersionPurpose::BMC ), |
| 184 | std::make_tuple( "xyz.openbmc_project.Software.Version.VersionPurpose.Host", Version::VersionPurpose::Host ), |
| 185 | }; |
| 186 | |
| 187 | } // anonymous namespace |
| 188 | |
| 189 | auto Version::convertVersionPurposeFromString(std::string& s) -> |
| 190 | VersionPurpose |
| 191 | { |
| 192 | auto i = std::find_if( |
| 193 | std::begin(mappingVersionVersionPurpose), |
| 194 | std::end(mappingVersionVersionPurpose), |
| 195 | [&s](auto& e){ return 0 == strcmp(s.c_str(), std::get<0>(e)); } ); |
| 196 | if (std::end(mappingVersionVersionPurpose) == i) |
| 197 | { |
| 198 | throw sdbusplus::exception::InvalidEnumString(); |
| 199 | } |
| 200 | else |
| 201 | { |
| 202 | return std::get<1>(*i); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | std::string convertForMessage(Version::VersionPurpose v) |
| 207 | { |
| 208 | auto i = std::find_if( |
| 209 | std::begin(mappingVersionVersionPurpose), |
| 210 | std::end(mappingVersionVersionPurpose), |
| 211 | [v](auto& e){ return v == std::get<1>(e); }); |
| 212 | return std::get<0>(*i); |
| 213 | } |
| 214 | |
| 215 | const vtable::vtable_t Version::_vtable[] = { |
| 216 | vtable::start(), |
| 217 | vtable::property("Version", |
| 218 | details::Version::_property_Version |
| 219 | .data(), |
| 220 | _callback_get_Version, |
| 221 | _callback_set_Version, |
| 222 | vtable::property_::emits_change), |
| 223 | vtable::property("Purpose", |
| 224 | details::Version::_property_Purpose |
| 225 | .data(), |
| 226 | _callback_get_Purpose, |
| 227 | _callback_set_Purpose, |
| 228 | vtable::property_::emits_change), |
| 229 | vtable::end() |
| 230 | }; |
| 231 | |
| 232 | } // namespace server |
| 233 | } // namespace Software |
| 234 | } // namespace openbmc_project |
| 235 | } // namespace xyz |
| 236 | } // namespace sdbusplus |
| 237 | |