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
use std::sync::Arc;
use dbus::{
channel::Sender,
nonblock::SyncConnection,
};
use crate::types::*;
pub struct SignalProp<A> {
data: A,
msgfn: Arc<PropChgMsgFn>,
dbuspath: Arc<SensorPath>,
conn: Arc<SyncConnection>,
}
impl<A: PartialEq + dbus::arg::RefArg> SignalProp<A> {
pub fn new(data: A, msgfn: &Arc<PropChgMsgFn>, dbuspath: &Arc<SensorPath>,
conn: &Arc<SyncConnection>) -> Self {
Self {
data,
msgfn: msgfn.clone(),
dbuspath: dbuspath.clone(),
conn: conn.clone(),
}
}
pub fn get(&self) -> A
where A: Copy
{
self.data
}
pub fn get_clone(&self) -> A
where A: Clone
{
self.data.clone()
}
pub fn set(&mut self, newdata: A) {
let changed = newdata != self.data;
self.data = newdata;
if changed {
self.send_propchg();
}
}
fn send_propchg(&self) {
if let Some(msg) = (self.msgfn)(&self.dbuspath.0, &dbus::arg::Variant(&self.data)) {
if self.conn.send(msg).is_err() {
eprintln!("Failed to send PropertiesChanged message for {:?}",
self.dbuspath);
}
} else {
eprintln!("Failed to create PropertiesChanged message for {:?}",
self.dbuspath);
}
}
}
pub mod props {
use super::*;
use std::error::Error;
use dbus::arg::PropMap;
pub fn prop_get_optional<'a, T: 'static>(map: &'a PropMap, key: &str)
-> ErrResult<Option<&'a T>>
{
let Some(value) = map.get(key) else {
return Ok(None);
};
let v = dbus::arg::cast::<T>(&value.0);
if v.is_some() {
Ok(v)
} else {
Err(err_invalid_data(format!("invalid value for '{}' key", key)))
}
}
pub fn prop_get_mandatory<'a, T: 'static>(map: &'a PropMap, key: &str) -> ErrResult<&'a T> {
match prop_get_optional(map, key) {
Ok(Some(v)) => Ok(v),
Ok(None) => Err(err_not_found(format!("required key '{}' not found", key))),
Err(e) => Err(e),
}
}
pub fn prop_get_default<'a, T: 'static>(map: &'a PropMap, key: &str, default: &'a T)
-> ErrResult<&'a T>
{
match prop_get_optional(map, key) {
Ok(Some(v)) => Ok(v),
Ok(None) => Ok(default),
Err(e) => Err(e),
}
}
pub fn prop_get_optional_from<'a, I, T>(map: &'a PropMap, key: &str) -> ErrResult<Option<T>>
where T: TryFrom<&'a I, Error = Box<dyn Error>>, I: 'static
{
match prop_get_optional::<I>(map, key) {
Ok(Some(v)) => Ok(Some(T::try_from(v)?)),
Ok(None) => Ok(None),
Err(e) => Err(e),
}
}
pub fn prop_get_mandatory_from<'a, I, T>(map: &'a PropMap, key: &str) -> ErrResult<T>
where T: TryFrom<&'a I, Error = Box<dyn Error>>, I: 'static
{
match prop_get_mandatory::<I>(map, key) {
Ok(v) => Ok(T::try_from(v)?),
Err(e) => Err(e),
}
}
pub fn prop_get_default_from<'a, I, T>(map: &'a PropMap, key: &str, default: T)
-> ErrResult<T>
where T: TryFrom<&'a I, Error = Box<dyn Error>>, I: 'static
{
match prop_get_optional::<I>(map, key) {
Ok(Some(v)) => Ok(T::try_from(v)?),
Ok(None) => Ok(default),
Err(e) => Err(e),
}
}
}