xref: /openbmc/boost-dbus/include/dbus/message.hpp (revision 16d80fe9)
1 // Copyright (c) Benjamin Kietzman (github.com/bkietz)
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 #ifndef DBUS_MESSAGE_HPP
7 #define DBUS_MESSAGE_HPP
8 
9 #include <dbus/dbus.h>
10 #include <dbus/element.hpp>
11 #include <dbus/endpoint.hpp>
12 #include <boost/intrusive_ptr.hpp>
13 
14 void intrusive_ptr_add_ref(DBusMessage *m)
15 {
16   dbus_message_ref(m);
17 }
18 
19 void intrusive_ptr_release(DBusMessage *m)
20 {
21   dbus_message_unref(m);
22 }
23 
24 namespace dbus {
25 
26 class message
27 {
28   boost::intrusive_ptr<DBusMessage> message_;
29 public:
30   uint32 serial;
31 
32   /// Create a method call message
33   static message new_call(
34       const endpoint& destination,
35       const string& method_name);
36 
37   /// Create a method return message
38   static message new_return(
39       const message& call);
40 
41   /// Create an error message
42   static message new_error(
43       const message& call,
44       const string& error_name,
45       const string& error_message);
46 
47   /// Create a signal message
48   static message new_signal(
49       const endpoint& origin,
50       const string& signal_name);
51 
52   message() {}
53 
54   message(DBusMessage *m)
55     : message_(dbus_message_ref(m))
56   {
57   }
58 
59   operator DBusMessage *()
60   {
61     return message_.get();
62   }
63 
64   operator const DBusMessage *() const
65   {
66     return message_.get();
67   }
68 
69   string get_path() const
70   {
71     return dbus_message_get_path(message_.get());
72   }
73 
74   string get_interface() const
75   {
76     return dbus_message_get_interface(message_.get());
77   }
78 
79   string get_member() const
80   {
81     return dbus_message_get_member(message_.get());
82   }
83 
84   string get_sender() const
85   {
86     return dbus_message_get_sender(message_.get());
87   }
88 
89   string get_destination() const
90   {
91     return dbus_message_get_destination(message_.get());
92   }
93 
94   struct packer
95   {
96     packer(message&);
97     DBusMessageIter iter_;
98     template<typename Element> packer& pack(const Element&);
99     template<typename Element> packer& pack_array(const Element*, size_t);
100   };
101   struct unpacker
102   {
103     unpacker(message&);
104     DBusMessageIter iter_;
105     /// return type code of the next element in line for unpacking
106     int code();
107     template<typename Element> unpacker& unpack(Element&);
108     int array_code();
109     template<typename Element> unpacker& unpack_array(Element*&, size_t);
110   };
111 
112   template<typename Element> packer pack(const Element&);
113   template<typename Element> unpacker unpack(Element&);
114 
115 };
116 
117 } // namespace dbus
118 
119 #include <dbus/impl/packer.ipp>
120 #include <dbus/impl/unpacker.ipp>
121 #include <dbus/impl/message.ipp>
122 
123 #endif // DBUS_MESSAGE_HPP
124