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
// unified macro for generating boilerplate getter and setter properties
macro_rules! impl_property {
// 1. boolean properties that require a channel
(bool, channel, $setter_name:ident, $getter_name:ident, $sys_setter:ident, $sys_getter:ident, $doc_name:expr) => {
#[doc = concat!("Set the ", $doc_name, ".")]
///
/// # Arguments
/// * `value` - The new state to apply.
/// * `channel` - The sensor channel (typically `1`).
///
/// # Errors
/// Returns a `TlpmError::VisaError` if the device responds with an error code.
pub fn $setter_name(&self, value: bool, channel: u16) -> Result<(), TlpmError> {
tracing::debug!(
concat!("setting ", $doc_name, " to {} on channel {}"),
value,
channel
);
let c_value = if value {
crate::VI_TRUE
} else {
crate::VI_FALSE
};
self.check_status(
unsafe { sys::$sys_setter(self.session, c_value, channel) },
stringify!($setter_name),
)
}
#[doc = concat!("Get the ", $doc_name, " state.")]
///
/// # Arguments
/// * `channel` - The sensor channel (typically `1`).
///
/// # Returns
/// The current boolean state.
///
/// # Errors
/// Returns a `TlpmError::VisaError` if the device responds with an error code.
pub fn $getter_name(&self, channel: u16) -> Result<bool, TlpmError> {
tracing::debug!(concat!("getting ", $doc_name, " on channel {}"), channel);
let mut c_value: sys::ViBoolean = 0;
self.check_status(
unsafe { sys::$sys_getter(self.session, &mut c_value, channel) },
stringify!($getter_name),
)?;
Ok(c_value == crate::VI_TRUE)
}
};
// 2. boolean global properties (no channel)
(bool, global, $setter_name:ident, $getter_name:ident, $sys_setter:ident, $sys_getter:ident, $doc_name:expr) => {
#[doc = concat!("Set the ", $doc_name, ".")]
///
/// # Arguments
/// * `value` - The new state to apply.
///
/// # Errors
/// Returns a `TlpmError::VisaError` if the device responds with an error code.
pub fn $setter_name(&self, value: bool) -> Result<(), TlpmError> {
tracing::debug!(concat!("setting ", $doc_name, " to {}"), value);
let c_value = if value {
crate::VI_TRUE
} else {
crate::VI_FALSE
};
self.check_status(
unsafe { sys::$sys_setter(self.session, c_value) },
stringify!($setter_name),
)
}
#[doc = concat!("Get the ", $doc_name, " state.")]
///
/// # Returns
/// The current boolean state.
///
/// # Errors
/// Returns a `TlpmError::VisaError` if the device responds with an error code.
pub fn $getter_name(&self) -> Result<bool, TlpmError> {
tracing::debug!(concat!("getting ", $doc_name));
let mut c_value: sys::ViBoolean = 0;
self.check_status(
unsafe { sys::$sys_getter(self.session, &mut c_value) },
stringify!($getter_name),
)?;
Ok(c_value == crate::VI_TRUE)
}
};
// 3. numeric properties that require a channel but no attribute
(numeric, channel, $setter_name:ident, $getter_name:ident, $sys_setter:ident, $sys_getter:ident, $ty:ty, $doc_name:expr) => {
#[doc = concat!("Set the ", $doc_name, ".")]
///
/// # Arguments
/// * `value` - The new numeric value to apply.
/// * `channel` - The sensor channel (typically `1`).
///
/// # Errors
/// Returns a `TlpmError::VisaError` if the device responds with an error code.
pub fn $setter_name(&self, value: $ty, channel: u16) -> Result<(), TlpmError> {
tracing::debug!(concat!("setting ", $doc_name, " on channel {}"), channel);
self.check_status(
unsafe { sys::$sys_setter(self.session, value, channel) },
stringify!($setter_name),
)
}
#[doc = concat!("Get the ", $doc_name, ".")]
///
/// # Arguments
/// * `channel` - The sensor channel (typically `1`).
///
/// # Returns
/// The queried numeric value.
///
/// # Errors
/// Returns a `TlpmError::VisaError` if the device responds with an error code.
pub fn $getter_name(&self, channel: u16) -> Result<$ty, TlpmError> {
tracing::debug!(concat!("getting ", $doc_name, " on channel {}"), channel);
let mut value: $ty = Default::default();
self.check_status(
unsafe { sys::$sys_getter(self.session, &mut value, channel) },
stringify!($getter_name),
)?;
Ok(value)
}
};
// 4. numeric properties that require an attribute and a channel
(numeric, attr_channel, $setter_name:ident, $getter_name:ident, $sys_setter:ident, $sys_getter:ident, $ty:ty, $doc_name:expr) => {
#[doc = concat!("Set the ", $doc_name, ".")]
///
/// # Arguments
/// * `value` - The new numeric value to apply.
/// * `channel` - The sensor channel (typically `1`).
///
/// # Errors
/// Returns a `TlpmError::VisaError` if the device responds with an error code.
pub fn $setter_name(&self, value: $ty, channel: u16) -> Result<(), TlpmError> {
tracing::debug!(concat!("setting ", $doc_name, " on channel {}"), channel);
self.check_status(
unsafe { sys::$sys_setter(self.session, value, channel) },
stringify!($setter_name),
)
}
#[doc = concat!("Get the ", $doc_name, ".")]
///
/// # Arguments
/// * `attribute` - The attribute to query (e.g., Set, Min, Max, Default).
/// * `channel` - The sensor channel (typically `1`).
///
/// # Returns
/// The queried numeric value.
///
/// # Errors
/// Returns a `TlpmError::VisaError` if the device responds with an error code.
pub fn $getter_name(
&self,
attribute: TlpmAttribute,
channel: u16,
) -> Result<$ty, TlpmError> {
tracing::debug!(concat!("getting ", $doc_name, " on channel {}"), channel);
let mut value: $ty = Default::default();
self.check_status(
unsafe { sys::$sys_getter(self.session, attribute as i16, &mut value, channel) },
stringify!($getter_name),
)?;
Ok(value)
}
};
// 5. simple numeric global properties (no attribute, no channel)
(numeric, global, $setter_name:ident, $getter_name:ident, $sys_setter:ident, $sys_getter:ident, $ty:ty, $doc_name:expr) => {
#[doc = concat!("Set the ", $doc_name, ".")]
///
/// # Arguments
/// * `value` - The new numeric value to apply.
///
/// # Errors
/// Returns a `TlpmError::VisaError` if the device responds with an error code.
pub fn $setter_name(&self, value: $ty) -> Result<(), TlpmError> {
tracing::debug!(concat!("setting ", $doc_name));
self.check_status(
unsafe { sys::$sys_setter(self.session, value) },
stringify!($setter_name),
)
}
#[doc = concat!("Get the ", $doc_name, ".")]
///
/// # Returns
/// The queried numeric value.
///
/// # Errors
/// Returns a `TlpmError::VisaError` if the device responds with an error code.
pub fn $getter_name(&self) -> Result<$ty, TlpmError> {
tracing::debug!(concat!("getting ", $doc_name));
let mut value: $ty = Default::default();
self.check_status(
unsafe { sys::$sys_getter(self.session, &mut value) },
stringify!($getter_name),
)?;
Ok(value)
}
};
// 6. string global properties (network and hostname configurations)
(string, global, $setter_name:ident, $getter_name:ident, $sys_setter:ident, $sys_getter:ident, $doc_name:expr) => {
#[doc = concat!("Set the ", $doc_name, ".")]
///
/// # Arguments
/// * `value` - The string value to apply.
///
/// # Errors
/// Returns a `TlpmError::StringConversion` if the string contains a null byte,
/// or a `TlpmError::VisaError` if the device responds with an error code.
pub fn $setter_name(&self, value: &str) -> Result<(), TlpmError> {
tracing::debug!(concat!("setting ", $doc_name));
let c_value = CString::new(value).map_err(|_| {
TlpmError::StringConversion("string contains null byte".to_string())
})?;
self.check_status(
unsafe { sys::$sys_setter(self.session, c_value.as_ptr() as *mut _) },
stringify!($setter_name),
)
}
#[doc = concat!("Get the ", $doc_name, ".")]
///
/// # Returns
/// The queried string value.
///
/// # Errors
/// Returns a `TlpmError::VisaError` if the device responds with an error code.
pub fn $getter_name(&self) -> Result<String, TlpmError> {
tracing::debug!(concat!("getting ", $doc_name));
let mut buffer = [0i8; sys::TLPM_BUFFER_SIZE as usize];
self.check_status(
unsafe { sys::$sys_getter(self.session, buffer.as_mut_ptr()) },
stringify!($getter_name),
)?;
let c_str = unsafe { CStr::from_ptr(buffer.as_ptr()) };
Ok(c_str.to_string_lossy().into_owned())
}
};
}
// macro for stamping out single-point data acquisition methods
macro_rules! impl_measure {
($method_name:ident, $sys_func:ident, $doc_desc:expr, $unit:expr) => {
#[doc = concat!("Read the current ", $doc_desc, " from the connected sensor.")]
///
/// # Arguments
/// * `channel` - The sensor channel to read from (typically `1`).
///
/// # Returns
#[doc = concat!("The measured ", $doc_desc, " in ", $unit, ".")]
///
/// # Errors
/// Returns a `TlpmError::VisaError` if the device responds with an error code.
pub fn $method_name(&self, channel: u16) -> Result<f64, TlpmError> {
tracing::debug!(concat!("measuring ", $doc_desc, " on channel {}"), channel);
let mut value: f64 = 0.0;
self.check_status(
unsafe { sys::$sys_func(self.session, &mut value, channel) },
stringify!($method_name),
)?;
Ok(value)
}
};
}