1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
use crate::{
message::{
header::{Header, HeaderFields},
MessageFlags, MessageType,
},
value::{Bus, Error, Interface, Member, ObjectPath, Type, TypeError, Value},
};
use std::convert::TryInto;
macro_rules! get_field {
($(#[$meta:meta])* $function:ident, $return:ty) => {
$(#[$meta])*
#[inline]
pub const fn $function(&self) -> Option<$return> {
self.header.$function()
}
};
}
macro_rules! has_field {
($(#[$meta:meta])* $function:ident) => {
$(#[$meta])*
#[inline]
pub const fn $function(&self) -> bool {
self.header.$function()
}
};
}
/// This represents a DBus [message].
///
/// [message]: https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol
#[derive(Debug, Clone, PartialOrd, PartialEq)]
pub struct Message {
pub(crate) header: Header,
pub(crate) body: Vec<Value>,
}
impl Message {
/// Create a [`Message`] object.
pub fn new(header: Header, body: Vec<Value>) -> Message {
Message { header, body }
}
/// Create a [`Message`] object as a [`MethodCall`].
///
/// [`MethodCall`]: crate::message::MessageType::MethodCall
pub fn method_call(
destination: Bus,
object_path: ObjectPath,
interface: Interface,
member: Member,
) -> Message {
let fields = HeaderFields {
destination: Some(destination),
path: Some(object_path),
interface: Some(interface),
member: Some(member),
..Default::default()
};
let header = Header {
is_le: true,
message_type: MessageType::MethodCall,
message_flags: MessageFlags::empty(),
version: 1,
serial: 0,
fields,
};
Message {
header,
body: Vec::new(),
}
}
/// Create a [`Message`] object as a [`Signal`].
///
/// [`Signal`]: crate::message::MessageType::Signal
pub fn signal(object_path: ObjectPath, interface: Interface, member: Member) -> Message {
let fields = HeaderFields {
path: Some(object_path),
interface: Some(interface),
member: Some(member),
..Default::default()
};
let header = Header {
is_le: true,
message_type: MessageType::Signal,
message_flags: MessageFlags::NO_REPLY_EXPECTED,
version: 1,
serial: 0,
fields,
};
Message {
header,
body: Vec::new(),
}
}
/// Create a [`Message`] to retrieve property value.
pub fn property_get(
destination: Bus,
object_path: ObjectPath,
interface: Interface,
property: &str,
) -> Message {
let mut msg = Message::method_call(
destination,
object_path,
"org.freedesktop.DBus.Properties".try_into().unwrap(),
"Get".try_into().unwrap(),
);
msg.add_value(Value::String(interface.to_string()));
msg.add_value(Value::String(property.to_string()));
msg
}
/// Create a [`Message`] to retrieve property value.
pub fn properties_get_all(
destination: Bus,
object_path: ObjectPath,
interface: Interface,
) -> Message {
let mut msg = Message::method_call(
destination,
object_path,
"org.freedesktop.DBus.Properties".try_into().unwrap(),
"GetAll".try_into().unwrap(),
);
msg.add_value(Value::String(interface.to_string()));
msg
}
/// Create a [`Message`] to retrieve property value.
pub fn property_set(
destination: Bus,
object_path: ObjectPath,
interface: Interface,
property: &str,
value: Value,
) -> Message {
let mut msg = Message::method_call(
destination,
object_path,
"org.freedesktop.DBus.Properties".try_into().unwrap(),
"Set".try_into().unwrap(),
);
msg.add_value(Value::String(interface.to_string()));
msg.add_value(Value::String(property.to_string()));
msg.add_value(Value::Variant(Box::new(value)));
msg
}
/// Get the serial number.
#[inline]
pub const fn get_serial(&self) -> u32 {
self.header.get_serial()
}
/// Set the serial number.
#[inline]
pub fn set_serial(&mut self, serial: u32) {
self.header.serial = serial;
}
get_field!(
/// Get the [`path`], if there is one in the header field.
///
/// [`path`]: crate::message::MessageHeaderFields::path
get_path,
&ObjectPath
);
has_field!(
/// It is true if the message contains a [`path`] in the header fields.
///
/// [`path`]: crate::message::MessageHeaderField::path
has_path
);
get_field!(
/// Get the [`interface`], if there is one in the header field.
///
/// [`interface`]: crate::message::MessageHeaderFields::interface
get_interface,
&Interface
);
has_field!(
/// It is true if the message contains an [`interface`] in the header fields.
///
/// [`interface`]: crate::message::MessageHeaderFields::interface
has_interface
);
get_field!(
/// Get the [`member`], if there is one in the header field.
///
/// [`member`]: crate::message::MessageHeaderFields::member
get_member,
&Member
);
has_field!(
/// It is true if the message contains a [`member`] in the header fields.
///
/// [`member`]: crate::message::MessageHeaderFields::member
has_member
);
get_field!(
/// Get the [`error_name`], if there is one in the header field.
///
/// [`error_name`]: crate::message::MessageHeaderFields::error_name
get_error_name,
&Error
);
has_field!(
/// It is true if the message contains an [`error_name`] in the header fields.
///
/// [`error_name`]: crate::message::MessageHeaderFields::error_name
has_error_name
);
get_field!(
/// Get the [`destination`], if there is one in the header field.
///
/// [`destination`]: crate::message::MessageHeaderFields::destination
get_destination,
&Bus
);
has_field!(
/// It is true if the message contains a [`destination`] in the header fields.
///
/// [`destination`]: crate::message::MessageHeaderFields::destination
has_destination
);
get_field!(
/// Get the [`sender`], if there is one in the header field.
///
/// [`sender`]: crate::message::MessageHeaderFields::sender
get_sender,
&Bus
);
has_field!(
/// It is true if the message contains a [`sender`] in the header fields.
///
/// [`sender`]: crate::message::MessageHeaderFields::sender
has_sender
);
get_field!(
/// Get the [`reply_serial`], if there is one in the header field.
///
/// [`reply_serial`]: crate::message::MessageHeaderFields::reply_serial
get_reply_serial,
u32
);
has_field!(
/// It is true if the message contains a [`reply_serial`] in the header fields.
///
/// [`reply_serial`]: crate::message::MessageHeaderFields::reply_serial
has_reply_serial
);
/// Get the [`signature`], if there is one in the header field.
///
/// [`signature`]: crate::message::MessageHeaderFields::signature
pub fn get_signature(&self) -> Result<Vec<Type>, TypeError> {
let mut signature = Vec::new();
for value in &self.body {
let type_ = value.get_type()?;
signature.push(type_);
}
Ok(signature)
}
has_field!(
/// It is true if the message contains a [`signature`] in the header fields.
///
/// [`signature`]: crate::message::MessageHeaderFields::signature
has_signature
);
#[cfg(target_family = "unix")]
get_field!(
/// Get the [`unix_fds`], if there is one in the header field.
///
/// [`unix_fds`]: crate::message::MessageHeaderFields::unix_fds
get_unix_fds,
u32
);
#[cfg(target_family = "unix")]
has_field!(
/// It is true if the message contains a [`unix_fds`] in the header fields.
///
/// [`unix_fds`]: crate::message::MessageHeaderFields::unix_fds
has_unix_fds
);
/// Add a new value to the body.
pub fn add_value(&mut self, value: Value) {
self.body.push(value);
}
/// Create a message return from this [`Message`].
/// Only works if this [`Message`] is a [`MethodCall`].
///
/// [`MethodCall`]: crate::message::MessageType::MethodCall
pub fn method_return(&self) -> Result<Message, Message> {
self.header.method_return()
}
/// Create a unknown property error message from this [`Message`].
pub fn unknown_property(&self, property: &str) -> Message {
self.header.unknown_property(property)
}
/// Create a unknown path error message from this [`Message`].
pub fn unknown_path(&self) -> Option<Message> {
self.header.unknown_path()
}
/// Create a unknown interface error message from this [`Message`].
pub fn unknown_interface(&self) -> Option<Message> {
self.header.unknown_interface()
}
/// Create a unknown member error message from this [`Message`].
pub fn unknown_member(&self) -> Option<Message> {
self.header.unknown_member()
}
/// Create an invalid args error message from this [`Message`].
pub fn invalid_args(&self, reason: String) -> Message {
self.header.invalid_args(reason)
}
/// Create an error message from this [`Message`].
pub fn error(&self, name: Error, message: String) -> Message {
self.header.error(name, message)
}
/// Get the body.
#[inline]
pub fn get_body(&self) -> &[Value] {
&self.body
}
/// Get the message type.
#[inline]
pub fn get_type(&self) -> MessageType {
self.header.get_type()
}
/// Split the [`Message`] object into the header and the body.
pub fn split(mut self) -> Result<(Header, Vec<Value>), TypeError> {
let signature = self.get_signature()?;
if !signature.is_empty() {
self.header.fields.signature = Some(signature);
}
Ok((self.header, self.body))
}
}