instrument-core 0.1.1

VISA-agnostic transport, SCPI session, discovery, and mock fixtures for instrument control
Documentation
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
use super::framing::extract_response;
use super::protocol::{
    is_no_error_reply, is_opc_supported_reply, is_syst_err_supported_reply, max_write_attempts,
    normalize_command, parse_f64, SessionCapabilities,
};
use crate::async_transport::{AsyncTransport, DynAsyncTransport};
use crate::connect::ConnectOptions;
use crate::diagnostics::{CommsEventKind, Diagnostics};
use crate::error::{Error, Result};
use crate::ieee4882::AsyncIeee4882;
use std::time::{Duration, Instant};

/// Restores I/O timeout on drop so cancel cannot leave a short VISA timeout.
struct IoTimeoutRestoreGuard<'a> {
    transport: &'a mut DynAsyncTransport,
    timeout: Duration,
}

impl Drop for IoTimeoutRestoreGuard<'_> {
    fn drop(&mut self) {
        let _ = self.transport.apply_read_timeout(self.timeout);
    }
}

/// Async SCPI session over a boxed transport.
pub struct AsyncScpiSession {
    transport: DynAsyncTransport,
    opts: ConnectOptions,
    capabilities: SessionCapabilities,
    read_buffer: Vec<u8>,
    diagnostics: Option<Diagnostics>,
    pending_command: Option<String>,
    pending_error_reply: Option<String>,
    skip_block_terminator: bool,
}

impl AsyncScpiSession {
    pub async fn new(mut transport: DynAsyncTransport, opts: ConnectOptions) -> Result<Self> {
        transport.configure(&opts).await?;
        let mut session = Self {
            transport,
            opts,
            capabilities: SessionCapabilities::default(),
            read_buffer: Vec::with_capacity(4096),
            diagnostics: None,
            pending_command: None,
            pending_error_reply: None,
            skip_block_terminator: false,
        };
        if session.opts.reset_on_connect {
            let _ = AsyncIeee4882::new(&mut session).clear_status().await;
            let _ = AsyncIeee4882::new(&mut session).reset().await;
            let _ = session.restore_io_timeout().await;
        }
        Ok(session)
    }

    pub fn with_diagnostics(mut self, diagnostics: Diagnostics) -> Self {
        self.diagnostics = Some(diagnostics);
        self
    }

    pub fn transport(&self) -> &dyn AsyncTransport {
        self.transport.as_ref()
    }

    pub fn options(&self) -> &ConnectOptions {
        &self.opts
    }

    /// Drains pending bytes from the transport read buffer.
    pub async fn flush(&mut self) -> Result<()> {
        let short = Duration::from_millis(50);
        let restore_to = self.opts.io_timeout();
        self.transport.set_read_timeout(short).await?;
        let guard = IoTimeoutRestoreGuard {
            transport: &mut self.transport,
            timeout: restore_to,
        };
        drain_read_buffer(&mut *guard.transport, &mut self.read_buffer).await
    }

    /// Writes a command without expecting a response.
    pub async fn write(&mut self, command: &str) -> Result<()> {
        self.write_with_retry(command, false).await
    }

    /// Sends a query and returns the response string.
    pub async fn query(&mut self, command: &str) -> Result<String> {
        self.query_with_timeout(command, self.effective_read_timeout())
            .await
    }

    pub async fn query_with_timeout(&mut self, command: &str, timeout: Duration) -> Result<String> {
        let max_attempts = max_write_attempts(true, self.opts.retries);
        let mut attempts = 0;
        loop {
            attempts += 1;
            self.write_with_retry(command, true).await?;
            match self.read_response(timeout).await {
                Ok(bytes) => {
                    return Ok(String::from_utf8_lossy(&bytes).trim().to_string());
                }
                Err(Error::Timeout) if attempts < max_attempts => {
                    let _ = self.flush().await;
                    if self.opts.reconnect_on_failure {
                        self.try_reconnect().await;
                    }
                    tokio::time::sleep(self.opts.retry_backoff * attempts).await;
                }
                Err(Error::Timeout) => {
                    let _ = self.flush().await;
                    return Err(Error::Timeout);
                }
                Err(e) => return Err(e),
            }
        }
    }

    async fn write_with_retry(&mut self, command: &str, idempotent: bool) -> Result<()> {
        let payload = normalize_command(command, &self.opts.terminator);
        let data = payload.as_bytes();

        let mut attempts = 0;
        let max_attempts = max_write_attempts(idempotent, self.opts.retries);

        loop {
            attempts += 1;
            let started = Instant::now();
            self.pending_command = Some(command.to_string());
            match self.transport.write(data).await {
                Ok(()) => {
                    record_success(
                        &self.diagnostics,
                        CommsEventKind::WriteOk,
                        Some(command),
                        attempts,
                        started,
                    );
                    return Ok(());
                }
                Err(Error::Timeout) if attempts < max_attempts => {
                    record_failure(
                        &self.diagnostics,
                        CommsEventKind::Timeout,
                        Some(command),
                        attempts,
                        started,
                        "write timeout",
                    );
                    if self.opts.reconnect_on_failure {
                        self.try_reconnect().await;
                    }
                    tokio::time::sleep(self.opts.retry_backoff).await;
                }
                Err(Error::Timeout) => {
                    record_failure(
                        &self.diagnostics,
                        CommsEventKind::Timeout,
                        Some(command),
                        attempts,
                        started,
                        "write timeout",
                    );
                    return Err(Error::Timeout);
                }
                Err(e) => {
                    record_failure(
                        &self.diagnostics,
                        CommsEventKind::WriteFailed,
                        Some(command),
                        attempts,
                        started,
                        &e.to_string(),
                    );
                    return Err(e);
                }
            }
        }
    }

    async fn read_response(&mut self, timeout: Duration) -> Result<Vec<u8>> {
        let restore_to = self.opts.io_timeout();
        self.transport.set_read_timeout(timeout).await?;
        let guard = IoTimeoutRestoreGuard {
            transport: &mut self.transport,
            timeout: restore_to,
        };
        read_framed_response(
            &mut *guard.transport,
            &mut self.read_buffer,
            &mut self.skip_block_terminator,
            &self.opts,
            self.pending_command.as_deref(),
            &self.diagnostics,
            timeout,
        )
        .await
    }

    fn effective_read_timeout(&self) -> Duration {
        self.opts.per_op_timeout.unwrap_or(self.opts.read_timeout)
    }

    /// Restores the session I/O timeout after a short probe or flush.
    async fn restore_io_timeout(&mut self) -> Result<()> {
        self.transport
            .set_read_timeout(self.opts.io_timeout())
            .await
    }

    async fn try_reconnect(&mut self) {
        if self.transport.reconnect().await.is_ok() {
            record_reconnect(&self.diagnostics);
        }
    }

    /// Probes and caches whether SYST:ERR? is supported.
    pub async fn probe_syst_err(&mut self) -> bool {
        if let Some(v) = self.capabilities.syst_err {
            return v;
        }
        match self
            .query_with_timeout("SYST:ERR?", Duration::from_millis(500))
            .await
        {
            Ok(resp) => {
                let supported = is_syst_err_supported_reply(&resp);
                self.capabilities.syst_err = Some(supported);
                // A zero reply describes the queue at probe time, not at the next check.
                if supported && !is_no_error_reply(&resp) {
                    self.pending_error_reply = Some(resp);
                }
                supported
            }
            Err(_) => false,
        }
    }

    /// Timeout is inconclusive and must not be cached as unsupported.
    pub async fn probe_opc(&mut self) -> bool {
        if let Some(v) = self.capabilities.opc {
            return v;
        }
        match self
            .query_with_timeout("*OPC?", Duration::from_millis(500))
            .await
        {
            Ok(resp) => {
                let supported = is_opc_supported_reply(&resp);
                self.capabilities.opc = Some(supported);
                supported
            }
            Err(_) => false,
        }
    }

    /// Drains the error queue, including any entry consumed by an earlier probe.
    pub async fn check_errors(&mut self) -> Result<Vec<String>> {
        if self.capabilities.syst_err == Some(false) {
            return Ok(Vec::new());
        }
        let mut errors = Vec::new();
        loop {
            let resp = match self.pending_error_reply.take() {
                Some(reply) => reply,
                None => self.query("SYST:ERR?").await?,
            };
            if !is_syst_err_supported_reply(&resp) {
                if self.capabilities.syst_err.is_none() {
                    self.capabilities.syst_err = Some(false);
                    return Ok(errors);
                }
                return Err(Error::Parse(format!("invalid error queue reply '{resp}'")));
            }
            self.capabilities.syst_err = Some(true);
            if is_no_error_reply(&resp) {
                break;
            }
            errors.push(resp);
            if errors.len() >= 50 {
                break;
            }
        }
        Ok(errors)
    }

    pub(crate) async fn query_completion(&mut self, timeout: Duration) -> Result<String> {
        self.ensure_opc_available()?;
        self.write_with_retry("*OPC?", false).await?;
        match self.read_response(timeout).await {
            Ok(bytes) => Ok(String::from_utf8_lossy(&bytes).trim().into()),
            Err(Error::Timeout) => {
                let _ = self.flush().await;
                Err(Error::Timeout)
            }
            Err(err) => Err(err),
        }
    }

    pub(crate) fn ensure_opc_available(&self) -> Result<()> {
        if self.capabilities.opc == Some(false) {
            return Err(Error::Unsupported(
                "operation completion requires *OPC? support",
            ));
        }
        Ok(())
    }

    /// Parses a numeric SCPI response.
    pub fn parse_f64(response: &str) -> Result<f64> {
        parse_f64(response)
    }
}

async fn drain_read_buffer(
    transport: &mut DynAsyncTransport,
    read_buffer: &mut Vec<u8>,
) -> Result<()> {
    let mut chunk = [0u8; 256];
    loop {
        match transport.read(&mut chunk).await {
            Ok(0) => break,
            Ok(_) => continue,
            Err(Error::Timeout) => break,
            Err(e) => return Err(e),
        }
    }
    read_buffer.clear();
    Ok(())
}

async fn read_framed_response(
    transport: &mut DynAsyncTransport,
    read_buffer: &mut Vec<u8>,
    skip_block_terminator: &mut bool,
    opts: &ConnectOptions,
    command: Option<&str>,
    diagnostics: &Option<Diagnostics>,
    timeout: Duration,
) -> Result<Vec<u8>> {
    let deadline = Instant::now() + timeout;
    read_buffer.clear();
    let mut chunk = [0u8; 1024];
    loop {
        let started = Instant::now();
        let remaining = deadline.saturating_duration_since(Instant::now());
        if remaining.is_zero() {
            return Err(Error::Timeout);
        }
        transport.set_read_timeout(remaining).await?;
        match transport.read(&mut chunk).await {
            Ok(0) => {
                if !read_buffer.is_empty() {
                    if let Ok((payload, _)) = extract_response(read_buffer, &opts.terminator) {
                        record_success(diagnostics, CommsEventKind::ReadOk, command, 1, started);
                        *skip_block_terminator =
                            read_buffer.starts_with(b"#") && read_buffer.get(1) != Some(&b'0');
                        return Ok(payload);
                    }
                }
                record_failure(
                    diagnostics,
                    CommsEventKind::Timeout,
                    command,
                    1,
                    started,
                    "zero-byte read",
                );
                return Err(Error::Timeout);
            }
            Ok(n) => {
                for &byte in &chunk[..n] {
                    if *skip_block_terminator && matches!(byte, b'\r' | b'\n') {
                        continue;
                    }
                    *skip_block_terminator = false;
                    read_buffer.push(byte);
                }
                if let Ok((payload, _)) = extract_response(read_buffer, &opts.terminator) {
                    record_success(diagnostics, CommsEventKind::ReadOk, command, 1, started);
                    *skip_block_terminator =
                        read_buffer.starts_with(b"#") && read_buffer.get(1) != Some(&b'0');
                    return Ok(payload);
                }
            }
            Err(Error::Timeout) => {
                if !read_buffer.is_empty() {
                    if let Ok((payload, _)) = extract_response(read_buffer, &opts.terminator) {
                        record_success(diagnostics, CommsEventKind::ReadOk, command, 1, started);
                        *skip_block_terminator =
                            read_buffer.starts_with(b"#") && read_buffer.get(1) != Some(&b'0');
                        return Ok(payload);
                    }
                }
                record_failure(
                    diagnostics,
                    CommsEventKind::Timeout,
                    command,
                    1,
                    started,
                    "read timeout",
                );
                return Err(Error::Timeout);
            }
            Err(e) => {
                record_failure(
                    diagnostics,
                    CommsEventKind::ReadFailed,
                    command,
                    1,
                    started,
                    &e.to_string(),
                );
                return Err(e);
            }
        }
    }
}

fn record_success(
    diagnostics: &Option<Diagnostics>,
    kind: CommsEventKind,
    command: Option<&str>,
    attempt: u32,
    started: Instant,
) {
    if let Some(diag) = diagnostics {
        diag.record_success(kind, command, attempt, started.elapsed());
    }
}

fn record_failure(
    diagnostics: &Option<Diagnostics>,
    kind: CommsEventKind,
    command: Option<&str>,
    attempt: u32,
    started: Instant,
    detail: &str,
) {
    if let Some(diag) = diagnostics {
        diag.record_failure(kind, command, attempt, started.elapsed(), detail);
    }
}

fn record_reconnect(diagnostics: &Option<Diagnostics>) {
    if let Some(diag) = diagnostics {
        diag.record_success(CommsEventKind::Reconnect, None, 1, Duration::ZERO);
    }
}