xref: /openbmc/boost-dbus/include/dbus/message.hpp (revision d977442c)
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 #include <dbus/impl/message_iterator.hpp>
14 
15 void intrusive_ptr_add_ref(DBusMessage *m)
16 {
17   dbus_message_ref(m);
18 }
19 
20 void intrusive_ptr_release(DBusMessage *m)
21 {
22   dbus_message_unref(m);
23 }
24 
25 namespace dbus {
26 
27 class message
28 {
29   boost::intrusive_ptr<DBusMessage> message_;
30 public:
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       message& call);
40 
41   /// Create an error message
42   static message new_error(
43       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   uint32 get_serial()
95   {
96     return dbus_message_get_serial(message_.get());
97   }
98 
99   message& set_serial(uint32 serial)
100   {
101     dbus_message_set_serial(message_.get(), serial);
102     return *this;
103   }
104 
105   uint32 get_reply_serial()
106   {
107     return dbus_message_get_reply_serial(message_.get());
108   }
109 
110   message& set_reply_serial(uint32 reply_serial)
111   {
112     dbus_message_set_reply_serial(message_.get(), reply_serial);
113     return *this;
114   }
115 
116   struct packer
117   {
118     packer(message&);
119     impl::message_iterator iter_;
120     template<typename Element> packer& pack(const Element&);
121     template<typename Element> packer& pack_array(const Element*, size_t);
122   };
123   struct unpacker
124   {
125     unpacker(message&);
126     impl::message_iterator iter_;
127     /// return type code of the next element in line for unpacking
128     int code();
129     template<typename Element> unpacker& unpack(Element&);
130     int array_code();
131     template<typename Element> unpacker& unpack_array(Element*&, size_t);
132   };
133 
134   template<typename Element> packer pack(const Element&);
135   template<typename Element> unpacker unpack(Element&);
136 
137 };
138 
139 } // namespace dbus
140 
141 #include <dbus/impl/packer.ipp>
142 #include <dbus/impl/unpacker.ipp>
143 #include <dbus/impl/message.ipp>
144 #include <dbus/impl/message_iterator.ipp>
145 
146 #endif // DBUS_MESSAGE_HPP
147