xref: /openbmc/sdbusplus/example/calculator-aserver.cpp (revision 36137e09614746b13603b5fbae79e6f70819c46b)
1bf0283aeSPatrick Williams #include <net/poettering/Calculator/aserver.hpp>
2bf0283aeSPatrick Williams #include <sdbusplus/async.hpp>
3bf0283aeSPatrick Williams 
4603acb82SMatt Spinler #include <iostream>
5603acb82SMatt Spinler 
6bf0283aeSPatrick Williams class Calculator :
7bf0283aeSPatrick Williams     public sdbusplus::aserver::net::poettering::Calculator<Calculator>
8bf0283aeSPatrick Williams {
9bf0283aeSPatrick Williams   public:
Calculator(sdbusplus::async::context & ctx,auto path)103a363a6aSPatrick Williams     explicit Calculator(sdbusplus::async::context& ctx, auto path) :
113a363a6aSPatrick Williams         sdbusplus::aserver::net::poettering::Calculator<Calculator>(ctx, path)
123a363a6aSPatrick Williams     {}
1372f7116aSPatrick Williams 
method_call(multiply_t,auto x,auto y)14e15ff88bSPatrick Williams     auto method_call(multiply_t, auto x, auto y)
15e15ff88bSPatrick Williams     {
16e15ff88bSPatrick Williams         auto r = x * y;
17e15ff88bSPatrick Williams         last_result(r);
18e15ff88bSPatrick Williams         return r;
19e15ff88bSPatrick Williams     }
20e15ff88bSPatrick Williams 
method_call(divide_t,auto x,auto y)21*36137e09SPatrick Williams     auto method_call(divide_t, auto x, auto y)
22*36137e09SPatrick Williams         -> sdbusplus::async::task<divide_t::return_type>
23e15ff88bSPatrick Williams     {
24d731d263SPatrick Williams         using sdbusplus::error::net::poettering::Calculator::DivisionByZero;
253a363a6aSPatrick Williams         if (y == 0)
263a363a6aSPatrick Williams         {
273a363a6aSPatrick Williams             status(State::Error);
283a363a6aSPatrick Williams             throw DivisionByZero();
293a363a6aSPatrick Williams         }
303a363a6aSPatrick Williams 
31e15ff88bSPatrick Williams         auto r = x / y;
32e15ff88bSPatrick Williams         last_result(r);
33e15ff88bSPatrick Williams         co_return r;
34e15ff88bSPatrick Williams     }
35e15ff88bSPatrick Williams 
method_call(clear_t)36e15ff88bSPatrick Williams     auto method_call(clear_t) -> sdbusplus::async::task<>
37e15ff88bSPatrick Williams     {
383a363a6aSPatrick Williams         auto v = last_result();
39e15ff88bSPatrick Williams         last_result(0);
403a363a6aSPatrick Williams         cleared(v);
41e15ff88bSPatrick Williams         co_return;
42e15ff88bSPatrick Williams     }
43603acb82SMatt Spinler 
get_property(owner_t) const44603acb82SMatt Spinler     auto get_property(owner_t) const
45603acb82SMatt Spinler     {
46603acb82SMatt Spinler         std::cout << " get_property on owner\n";
47603acb82SMatt Spinler         return owner_;
48603acb82SMatt Spinler     }
49603acb82SMatt Spinler 
set_property(owner_t,auto owner)50603acb82SMatt Spinler     bool set_property(owner_t, auto owner)
51603acb82SMatt Spinler     {
52603acb82SMatt Spinler         std::cout << " set_property on owner\n";
53603acb82SMatt Spinler         std::swap(owner_, owner);
54603acb82SMatt Spinler         return owner_ == owner;
55603acb82SMatt Spinler     }
56bf0283aeSPatrick Williams };
57bf0283aeSPatrick Williams 
main()58bf0283aeSPatrick Williams int main()
59bf0283aeSPatrick Williams {
60b4bae8f6SPatrick Williams     constexpr auto path = Calculator::instance_path;
613a363a6aSPatrick Williams 
62bf0283aeSPatrick Williams     sdbusplus::async::context ctx;
633a363a6aSPatrick Williams     sdbusplus::server::manager_t manager{ctx, path};
643a363a6aSPatrick Williams 
653a363a6aSPatrick Williams     Calculator c{ctx, path};
663a363a6aSPatrick Williams 
673a363a6aSPatrick Williams     ctx.spawn([](sdbusplus::async::context& ctx) -> sdbusplus::async::task<> {
68c53fe51cSPatrick Williams         ctx.request_name(Calculator::default_service);
693a363a6aSPatrick Williams         co_return;
703a363a6aSPatrick Williams     }(ctx));
71bf0283aeSPatrick Williams 
72bf0283aeSPatrick Williams     ctx.run();
73bf0283aeSPatrick Williams 
74bf0283aeSPatrick Williams     return 0;
75bf0283aeSPatrick Williams }
76