Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 1 | #include <boost/asio.hpp> |
| 2 | #include <sdbusplus/asio/connection.hpp> |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 3 | #include <sdbusplus/asio/object_server.hpp> |
Krzysztof Grobelny | 807419d | 2020-09-28 11:39:22 +0200 | [diff] [blame] | 4 | #include <sdbusplus/asio/property.hpp> |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 5 | #include <sdbusplus/bus.hpp> |
| 6 | #include <sdbusplus/unpack_properties.hpp> |
| 7 | |
| 8 | #include <iostream> |
| 9 | |
Krzysztof Grobelny | 6adfe94 | 2021-12-21 13:57:22 +0100 | [diff] [blame] | 10 | const std::string demoServiceName = "demo.service"; |
| 11 | const std::string demoObjectPath = "/xyz/demo"; |
| 12 | const std::string demoInterfaceName = "xyz.demo"; |
| 13 | const std::string propertyGrettingName = "Greetings"; |
| 14 | const std::string propertyGoodbyesName = "Goodbyes"; |
| 15 | const std::string propertyValueName = "Value"; |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 16 | |
| 17 | class Application |
| 18 | { |
| 19 | public: |
Ed Tanous | 760e66a | 2023-01-05 10:12:00 -0800 | [diff] [blame] | 20 | Application(sdbusplus::asio::connection& bus, |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 21 | sdbusplus::asio::object_server& objServer) : |
Ed Tanous | 760e66a | 2023-01-05 10:12:00 -0800 | [diff] [blame] | 22 | bus_(bus), |
| 23 | objServer_(objServer) |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 24 | { |
Patrick Williams | 1a25a10 | 2022-09-29 17:18:26 -0500 | [diff] [blame] | 25 | demo_ = objServer_.add_unique_interface(demoObjectPath, |
| 26 | demoInterfaceName); |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 27 | |
Krzysztof Grobelny | 90fab6b | 2022-02-18 14:21:21 +0100 | [diff] [blame] | 28 | demo_->register_property_r<std::string>( |
| 29 | propertyGrettingName, sdbusplus::vtable::property_::const_, |
| 30 | [this](const auto&) { return greetings_; }); |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 31 | |
Krzysztof Grobelny | 90fab6b | 2022-02-18 14:21:21 +0100 | [diff] [blame] | 32 | demo_->register_property_rw<std::string>( |
| 33 | propertyGoodbyesName, sdbusplus::vtable::property_::emits_change, |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 34 | [this](const auto& newPropertyValue, const auto&) { |
| 35 | goodbyes_ = newPropertyValue; |
Jonathan Doman | ae47928 | 2022-06-03 13:29:50 -0700 | [diff] [blame] | 36 | return true; |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 37 | }, |
| 38 | [this](const auto&) { return goodbyes_; }); |
| 39 | |
Krzysztof Grobelny | 90fab6b | 2022-02-18 14:21:21 +0100 | [diff] [blame] | 40 | demo_->register_property_r<uint32_t>( |
| 41 | propertyValueName, sdbusplus::vtable::property_::const_, |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 42 | [](const auto& value) -> uint32_t { return value; }); |
| 43 | |
| 44 | demo_->initialize(); |
| 45 | } |
| 46 | |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 47 | uint32_t fatalErrors() const |
| 48 | { |
| 49 | return fatalErrors_; |
| 50 | } |
| 51 | |
Ed Tanous | 95874d9 | 2021-02-19 17:14:27 -0800 | [diff] [blame] | 52 | auto logSystemErrorCode(boost::system::error_code ec) |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 53 | { |
Ed Tanous | 95874d9 | 2021-02-19 17:14:27 -0800 | [diff] [blame] | 54 | std::cerr << "Error: " << ec << "\n"; |
| 55 | ++fatalErrors_; |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void logException(const std::exception& e) |
| 59 | { |
| 60 | std::cerr << "Error: " << e.what() << "\n"; |
| 61 | ++fatalErrors_; |
| 62 | } |
| 63 | |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 64 | void logUnpackError(const sdbusplus::UnpackErrorReason reason, |
| 65 | const std::string& property) |
Krzysztof Grobelny | 6adfe94 | 2021-12-21 13:57:22 +0100 | [diff] [blame] | 66 | { |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 67 | std::cerr << "UnpackError: " << static_cast<int>(reason) << ", " |
| 68 | << property << "\n"; |
Krzysztof Grobelny | 6adfe94 | 2021-12-21 13:57:22 +0100 | [diff] [blame] | 69 | ++fatalErrors_; |
| 70 | } |
| 71 | |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 72 | void logExpectedException( |
| 73 | const sdbusplus::exception::UnpackPropertyError& error) |
| 74 | { |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 75 | std::cout << "As expected " << error.what() << "\n"; |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void asyncGetAllPropertiesStringTypeOnly() |
| 79 | { |
| 80 | sdbusplus::asio::getAllProperties( |
Krzysztof Grobelny | 6adfe94 | 2021-12-21 13:57:22 +0100 | [diff] [blame] | 81 | bus_, demoServiceName, demoObjectPath, demoInterfaceName, |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 82 | [this](const boost::system::error_code ec, |
| 83 | const std::vector<std::pair< |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 84 | std::string, std::variant<std::monostate, std::string>>>& |
Ed Tanous | 95874d9 | 2021-02-19 17:14:27 -0800 | [diff] [blame] | 85 | properties) -> void { |
| 86 | if (ec) |
| 87 | { |
| 88 | logSystemErrorCode(ec); |
| 89 | return; |
| 90 | } |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 91 | { |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 92 | const std::string* greetings = nullptr; |
| 93 | const std::string* goodbyes = nullptr; |
| 94 | const bool success = sdbusplus::unpackPropertiesNoThrow( |
| 95 | [this](const sdbusplus::UnpackErrorReason reason, |
| 96 | const std::string& property) { |
| 97 | logUnpackError(reason, property); |
| 98 | }, |
| 99 | properties, propertyGrettingName, greetings, |
| 100 | propertyGoodbyesName, goodbyes); |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 101 | |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 102 | if (success) |
Krzysztof Grobelny | 6adfe94 | 2021-12-21 13:57:22 +0100 | [diff] [blame] | 103 | { |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 104 | std::cout << "value of greetings: " << *greetings |
| 105 | << "\n"; |
| 106 | std::cout << "value of goodbyes: " << *goodbyes << "\n"; |
Krzysztof Grobelny | 6adfe94 | 2021-12-21 13:57:22 +0100 | [diff] [blame] | 107 | } |
| 108 | else |
| 109 | { |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 110 | ++fatalErrors_; |
Krzysztof Grobelny | 6adfe94 | 2021-12-21 13:57:22 +0100 | [diff] [blame] | 111 | } |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | try |
| 115 | { |
| 116 | std::string value; |
Krzysztof Grobelny | 6adfe94 | 2021-12-21 13:57:22 +0100 | [diff] [blame] | 117 | sdbusplus::unpackProperties(properties, propertyValueName, |
| 118 | value); |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 119 | |
| 120 | std::cerr << "Error: it should fail because of " |
| 121 | "not matched type\n"; |
| 122 | ++fatalErrors_; |
| 123 | } |
| 124 | catch (const sdbusplus::exception::UnpackPropertyError& error) |
| 125 | { |
| 126 | logExpectedException(error); |
| 127 | } |
| 128 | }); |
| 129 | } |
| 130 | |
| 131 | void asyncGetAllProperties() |
| 132 | { |
| 133 | sdbusplus::asio::getAllProperties( |
Krzysztof Grobelny | 6adfe94 | 2021-12-21 13:57:22 +0100 | [diff] [blame] | 134 | bus_, demoServiceName, demoObjectPath, demoInterfaceName, |
Krzysztof Grobelny | c8447d5 | 2022-01-05 13:21:37 +0100 | [diff] [blame] | 135 | [this](const boost::system::error_code ec, |
| 136 | const std::vector<std::pair< |
Ed Tanous | 95874d9 | 2021-02-19 17:14:27 -0800 | [diff] [blame] | 137 | std::string, |
| 138 | std::variant<std::monostate, std::string, uint32_t>>>& |
| 139 | properties) -> void { |
| 140 | if (ec) |
| 141 | { |
| 142 | logSystemErrorCode(ec); |
| 143 | return; |
| 144 | } |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 145 | try |
| 146 | { |
| 147 | std::string greetings; |
| 148 | std::string goodbyes; |
| 149 | uint32_t value = 0u; |
Krzysztof Grobelny | 6adfe94 | 2021-12-21 13:57:22 +0100 | [diff] [blame] | 150 | sdbusplus::unpackProperties(properties, |
| 151 | propertyGrettingName, greetings, |
| 152 | propertyGoodbyesName, goodbyes, |
| 153 | propertyValueName, value); |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 154 | |
| 155 | std::cout << "value of greetings: " << greetings << "\n"; |
| 156 | std::cout << "value of goodbyes: " << goodbyes << "\n"; |
| 157 | std::cout << "value of value: " << value << "\n"; |
| 158 | } |
| 159 | catch (const sdbusplus::exception::UnpackPropertyError& error) |
| 160 | { |
| 161 | logException(error); |
| 162 | } |
| 163 | |
| 164 | try |
| 165 | { |
| 166 | std::string unknownProperty; |
| 167 | sdbusplus::unpackProperties( |
| 168 | properties, "UnknownPropertyName", unknownProperty); |
| 169 | |
| 170 | std::cerr << "Error: it should fail because of " |
| 171 | "missing property\n"; |
| 172 | ++fatalErrors_; |
| 173 | } |
| 174 | catch (const sdbusplus::exception::UnpackPropertyError& error) |
| 175 | { |
| 176 | logExpectedException(error); |
| 177 | } |
| 178 | |
| 179 | try |
| 180 | { |
| 181 | uint32_t notMatchingType; |
Krzysztof Grobelny | 6adfe94 | 2021-12-21 13:57:22 +0100 | [diff] [blame] | 182 | sdbusplus::unpackProperties( |
| 183 | properties, propertyGrettingName, notMatchingType); |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 184 | |
| 185 | std::cerr << "Error: it should fail because of " |
| 186 | "not matched type\n"; |
| 187 | ++fatalErrors_; |
| 188 | } |
| 189 | catch (const sdbusplus::exception::UnpackPropertyError& error) |
| 190 | { |
| 191 | logExpectedException(error); |
| 192 | } |
| 193 | }); |
| 194 | } |
| 195 | |
| 196 | private: |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 197 | sdbusplus::asio::connection& bus_; |
| 198 | sdbusplus::asio::object_server& objServer_; |
| 199 | |
Krzysztof Grobelny | 8fc0639 | 2020-09-28 13:11:22 +0200 | [diff] [blame] | 200 | std::unique_ptr<sdbusplus::asio::dbus_interface> demo_; |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 201 | std::string greetings_ = "Hello"; |
| 202 | std::string goodbyes_ = "Bye"; |
| 203 | |
| 204 | uint32_t fatalErrors_ = 0u; |
| 205 | }; |
| 206 | |
| 207 | int main(int, char**) |
| 208 | { |
| 209 | boost::asio::io_context ioc; |
| 210 | boost::asio::signal_set signals(ioc, SIGINT, SIGTERM); |
| 211 | |
| 212 | signals.async_wait( |
| 213 | [&ioc](const boost::system::error_code&, const int&) { ioc.stop(); }); |
| 214 | |
| 215 | auto bus = std::make_shared<sdbusplus::asio::connection>(ioc); |
| 216 | auto objServer = std::make_unique<sdbusplus::asio::object_server>(bus); |
| 217 | |
Krzysztof Grobelny | 6adfe94 | 2021-12-21 13:57:22 +0100 | [diff] [blame] | 218 | bus->request_name(demoServiceName.c_str()); |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 219 | |
Ed Tanous | 760e66a | 2023-01-05 10:12:00 -0800 | [diff] [blame] | 220 | Application app(*bus, *objServer); |
Krzysztof Grobelny | 09b88f2 | 2020-09-02 14:49:01 +0200 | [diff] [blame] | 221 | |
| 222 | boost::asio::post(ioc, |
| 223 | [&app] { app.asyncGetAllPropertiesStringTypeOnly(); }); |
| 224 | boost::asio::post(ioc, [&app] { app.asyncGetAllProperties(); }); |
| 225 | |
| 226 | ioc.run(); |
| 227 | |
| 228 | std::cout << "Fatal errors count: " << app.fatalErrors() << "\n"; |
| 229 | |
| 230 | return app.fatalErrors(); |
| 231 | } |