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 sanitize(dbus_message_get_path(message_.get())); 72 } 73 74 string get_interface() const 75 { 76 return sanitize(dbus_message_get_interface(message_.get())); 77 } 78 79 string get_member() const 80 { 81 return sanitize(dbus_message_get_member(message_.get())); 82 } 83 84 string get_type() const 85 { 86 return sanitize(dbus_message_type_to_string(dbus_message_get_type(message_.get()))); 87 } 88 89 string get_sender() const 90 { 91 return sanitize(dbus_message_get_sender(message_.get())); 92 } 93 94 string get_destination() const 95 { 96 return sanitize(dbus_message_get_destination(message_.get())); 97 } 98 99 uint32 get_serial() 100 { 101 return dbus_message_get_serial(message_.get()); 102 } 103 104 message& set_serial(uint32 serial) 105 { 106 dbus_message_set_serial(message_.get(), serial); 107 return *this; 108 } 109 110 uint32 get_reply_serial() 111 { 112 return dbus_message_get_reply_serial(message_.get()); 113 } 114 115 message& set_reply_serial(uint32 reply_serial) 116 { 117 dbus_message_set_reply_serial(message_.get(), reply_serial); 118 return *this; 119 } 120 121 struct packer 122 { 123 impl::message_iterator iter_; 124 packer(message& m); 125 template<typename Element> packer& pack(const Element& e) 126 { 127 return *this << e; 128 } 129 }; 130 struct unpacker 131 { 132 impl::message_iterator iter_; 133 unpacker(message& m); 134 template<typename Element> unpacker& unpack(Element& e) 135 { 136 return *this >> e; 137 } 138 }; 139 140 template<typename Element> packer pack(const Element& e) 141 { 142 return packer(*this).pack(e); 143 } 144 145 template<typename Element> unpacker unpack(Element& e) 146 { 147 return unpacker(*this).unpack(e); 148 } 149 150 private: 151 static std::string 152 sanitize(const char* str) 153 { 154 return (str == NULL) ? "(null)" : str; 155 } 156 }; 157 158 template<typename Element> 159 message::packer operator<<(message m, const Element& e) 160 { 161 return message::packer(m).pack(e); 162 } 163 164 template<typename Element> 165 message::unpacker operator>>(message m, Element& e) 166 { 167 return message::unpacker(m).unpack(e); 168 } 169 170 inline std::ostream& 171 operator<<(std::ostream& os, const message& m) 172 { 173 os << "type='" << m.get_type() << "'," 174 << "sender='" << m.get_sender() << "'," 175 << "interface='" << m.get_interface() << "'," 176 << "member='" << m.get_member() << "'," 177 << "path='" << m.get_path() << "'," 178 << "destination='" << m.get_destination() << "'"; 179 return os; 180 } 181 182 } // namespace dbus 183 184 #include <dbus/impl/packer.ipp> 185 #include <dbus/impl/unpacker.ipp> 186 #include <dbus/impl/message.ipp> 187 #include <dbus/impl/message_iterator.ipp> 188 189 #endif // DBUS_MESSAGE_HPP 190