xref: /openbmc/sdbusplus/example/calculator-client.cpp (revision 8aea1d814330427a3668b48c347f1703c214748e)
1 #include <net/poettering/Calculator/client.hpp>
2 #include <sdbusplus/async.hpp>
3 
4 #include <iostream>
5 
startup(sdbusplus::async::context & ctx)6 auto startup(sdbusplus::async::context& ctx) -> sdbusplus::async::task<>
7 {
8     using Calculator = sdbusplus::client::net::poettering::Calculator<>;
9 
10     auto c = Calculator(ctx)
11                  .service(Calculator::default_service)
12                  .path(Calculator::instance_path);
13 
14     // Alternatively, sdbusplus::async::client_t<Calculator, ...>() could have
15     // been used to combine multiple interfaces into a single client-proxy.
16     auto alternative_c [[maybe_unused]] =
17         sdbusplus::async::client_t<
18             sdbusplus::client::net::poettering::Calculator>(ctx)
19             .service(Calculator::default_service)
20             .path(Calculator::instance_path);
21 
22     {
23         // Call the Multiply method.
24         auto _ = co_await c.multiply(7, 6);
25         std::cout << "Should be 42: " << _ << std::endl;
26     }
27 
28     {
29         // Get the LastResult property.
30         auto _ = co_await c.last_result();
31         std::cout << "Should be 42: " << _ << std::endl;
32     }
33 
34     {
35         // Call the Clear method.
36         co_await c.clear();
37     }
38 
39     {
40         // Get the LastResult property.
41         auto _ = co_await c.last_result();
42         std::cout << "Should be 0: " << _ << std::endl;
43     }
44 
45     {
46         // Set the LastResult property.
47         co_await c.last_result(1234);
48         // Get the LastResult property.
49         auto _ = co_await c.last_result();
50         std::cout << "Should be 1234: " << _ << std::endl;
51     }
52 
53     {
54         co_await c.owner("client");
55     }
56 
57     {
58         auto _ = co_await c.owner();
59         std::cout << "Should be 'client': " << _ << std::endl;
60     }
61 
62     {
63         // Grab all the properties and print them.
64         auto _ = co_await c.properties();
65         std::cout << "Should be 1234: " << _.last_result << std::endl;
66         std::cout << "Should be 'client': " << _.owner << std::endl;
67     }
68 
69     co_return;
70 }
71 
main()72 int main()
73 {
74     sdbusplus::async::context ctx;
75     ctx.spawn(startup(ctx));
76     ctx.spawn(
77         sdbusplus::async::execution::just() |
78         sdbusplus::async::execution::then([&ctx]() { ctx.request_stop(); }));
79     ctx.run();
80 
81     return 0;
82 }
83