xref: /openbmc/boost-dbus/include/dbus/connection.hpp (revision fc79e461e10e6fb6085e35a01bc58c4685c4e889)
1 #ifndef DBUS_CONNECTION_HPP
2 #define DBUS_CONNECTION_HPP
3 
4 #include <string>
5 #include <boost/asio.hpp>
6 //#include <dbus/match.hpp>
7 //#include <dbus/filter.hpp>
8 #include <dbus/time.hpp>
9 #include <dbus/message.hpp>
10 #include <dbus/connection_service.hpp>
11 
12 namespace dbus {
13 
14 using std::string;
15 using namespace boost::asio;
16 
17 /// Root D-Bus IO object
18 /**
19  * A connection to a bus, through which messages may be sent or received.
20  */
21 class connection
22   : public basic_io_object<connection_service>
23 {
24 public:
25 
26   /// Constructor.
27   /**
28    * @param io_service The io_service object that the connection will use to
29    * wire D-Bus for asynchronous operation.
30    */
31   explicit connection(io_service& io)
32     : basic_io_object<connection_service>(io)
33   {
34   }
35 
36   //TODO: should these be constructors...?
37 
38   /// Open a connection to a specified address.
39   /**
40    * @note This function or open(unsigned) should be called only once to initialize
41    * a connection.
42    *
43    * @param address The address of the bus to connect to.
44    *
45    * @param shared Set false to open a private connection.
46    *
47    * @throws boost::system::system_error When opening the connection failed.
48    */
49   void open(const string& address, bool shared=true)
50   {
51     this->get_service().open(
52       this->get_implementation(),
53       address,
54       shared);
55   }
56 
57   /// Open a connection to a well-known bus.
58   /**
59    * D-Bus connections are usually opened to well-known buses like the
60    * system or session bus.
61    *
62    * @note This function or open(string) should be called only once to initialize
63    * a connection.
64    *
65    * @param bus_type The well-known bus to connect to.
66    *
67    * @param shared Set false to open a private connection.
68    *
69    * @throws boost::system::system_error When opening the connection failed.
70    */
71   // TODO: change this unsigned to an enumeration
72   void open(const unsigned bus_type, bool shared=true)
73   {
74     this->get_service().open(
75       this->get_implementation(),
76       bus_type,
77       shared);
78   }
79 
80   //TODO add error_code catchers to all these
81 
82   /// Send a message.
83   /**
84    * @param m The message to send.
85    *
86    * @param t Time to wait for a reply. Passing 0 as the timeout means
87    * that you wish to ignore the reply. (Or catch it later somehow...)
88    *
89    * @return The reply received.
90    *
91    * @throws boost::system::system_error When the response timed out, if
92    * timeout was not 0.
93    */
94   template <typename Duration>
95   message send(message& m,
96       const Duration& t = timeout_default())
97   {
98     return this->get_service().send(
99       this->get_implementation(),
100       m,
101       t);
102   }
103 
104   /// Send a message asynchronously.
105   /**
106    * @param m The message to send.
107    *
108    * @param handler Handler for the reply.
109    *
110    * @return Asynchronous result
111    */
112   template<typename MessageHandler>
113   inline BOOST_ASIO_INITFN_RESULT_TYPE(MessageHandler,
114       void(boost::system::error_code, message))
115   async_send(message& m,
116       BOOST_ASIO_MOVE_ARG(MessageHandler) handler)
117   {
118     return this->get_service().async_send(
119       this->get_implementation(),
120       m,
121       BOOST_ASIO_MOVE_CAST(MessageHandler)(handler));
122   }
123 
124   /*
125 
126   /// Create a new match.
127   match new_match(
128       BOOST_ASIO_MOVE_ARG(string) expression)
129   {
130     return match(BOOST_ASIO_MOVE_CAST(string)(expression));
131   }
132 
133   /// Create a new filter.
134   template<typename MessagePredicate>
135   filter new_filter(
136       BOOST_ASIO_MOVE_ARG(MessagePredicate) p)
137   {
138     return filter(BOOST_ASIO_MOVE_CAST(MessagePredicate)(p));
139   }
140 
141   */
142 
143 };
144 
145 } // namespace dbus
146 
147 
148 #endif // DBUS_CONNECTION_HPP
149