spvirit-tools 0.1.18

PVAccess client/server tools for EPICS
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
mod protocol;

use std::collections::HashSet;
use std::time::Duration;

use protocol::frame_harness::TestServer;
use protocol::scenario_harness::ScenarioSession;
use spvirit_codec::epics_decode::PvaPacketCommand;
use spvirit_codec::spvd_decode::{DecodedValue, extract_nt_scalar_value};
use spvirit_codec::spvirit_encode::encode_header;
use tokio::net::UdpSocket;

const PV_REQUEST_EMPTY: [u8; 6] = [0xfd, 0x02, 0x00, 0x80, 0x00, 0x00];

fn encode_size(size: usize) -> Vec<u8> {
    if size == 0 {
        return vec![0];
    }
    if size < 254 {
        return vec![size as u8];
    }
    let mut out = vec![0xFE];
    out.extend_from_slice(&(size as u32).to_le_bytes());
    out
}

fn encode_string(value: &str) -> Vec<u8> {
    let mut out = encode_size(value.len());
    out.extend_from_slice(value.as_bytes());
    out
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn lifecycle_get_put_monitor_paths() {
    let server = match TestServer::spawn() {
        Ok(s) => s,
        Err(msg) => {
            eprintln!("Skipping test, cannot spawn server: {}", msg);
            return;
        }
    };

    let mut scenario = ScenarioSession::connect(&server)
        .await
        .expect("connect scenario");
    let initial = scenario
        .get_scalar_value("SIM:AO")
        .await
        .expect("get initial");
    let initial_value = extract_nt_scalar_value(&initial).expect("value field");
    match initial_value {
        DecodedValue::Float64(v) => assert!((*v - 2.50).abs() < 1e-6),
        other => panic!("unexpected initial value type: {:?}", other),
    }

    scenario
        .put_scalar_number("SIM:AO", 4.321)
        .await
        .expect("put");
    let after = scenario
        .get_scalar_value("SIM:AO")
        .await
        .expect("get after put");
    let after_value = extract_nt_scalar_value(&after).expect("value field");
    match after_value {
        DecodedValue::Float64(v) => assert!((*v - 4.321).abs() < 1e-6),
        other => panic!("unexpected updated value type: {:?}", other),
    }

    scenario
        .monitor_init_start_stop("SIM:AO")
        .await
        .expect("monitor lifecycle");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn lifecycle_multiple_channels_and_ioids() {
    let server = match TestServer::spawn() {
        Ok(s) => s,
        Err(msg) => {
            eprintln!("Skipping test, cannot spawn server: {}", msg);
            return;
        }
    };

    let mut scenario = ScenarioSession::connect(&server)
        .await
        .expect("connect scenario");
    let sid_ai = scenario.ensure_channel("SIM:AI").await.expect("sid ai");
    let sid_ao = scenario.ensure_channel("SIM:AO").await.expect("sid ao");

    let ioid_ai = 4001u32;
    let ioid_ao = 4002u32;
    scenario
        .session
        .send_client_op(10, sid_ai, ioid_ai, 0x08, &PV_REQUEST_EMPTY)
        .await
        .expect("get init ai");
    scenario
        .session
        .send_client_op(10, sid_ao, ioid_ao, 0x08, &PV_REQUEST_EMPTY)
        .await
        .expect("get init ao");

    let mut init_seen = HashSet::new();
    while init_seen.len() < 2 {
        let cmd = scenario
            .session
            .read_until(Duration::from_secs(2), |cmd| {
                matches!(cmd, PvaPacketCommand::Op(op) if op.command == 10 && (op.subcmd & 0x08) != 0)
            })
            .await
            .expect("init response");
        if let PvaPacketCommand::Op(op) = cmd {
            init_seen.insert(op.ioid);
        }
    }
    assert!(init_seen.contains(&ioid_ai));
    assert!(init_seen.contains(&ioid_ao));

    scenario
        .session
        .send_client_op(10, sid_ai, ioid_ai, 0x00, &[])
        .await
        .expect("get data ai");
    scenario
        .session
        .send_client_op(10, sid_ao, ioid_ao, 0x00, &[])
        .await
        .expect("get data ao");

    let mut data_seen = HashSet::new();
    while data_seen.len() < 2 {
        let cmd = scenario
            .session
            .read_until(Duration::from_secs(2), |cmd| {
                matches!(cmd, PvaPacketCommand::Op(op) if op.command == 10 && op.subcmd == 0x00)
            })
            .await
            .expect("data response");
        if let PvaPacketCommand::Op(op) = cmd {
            data_seen.insert(op.ioid);
        }
    }
    assert!(data_seen.contains(&ioid_ai));
    assert!(data_seen.contains(&ioid_ao));
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn lifecycle_put_without_init_returns_error() {
    let server = match TestServer::spawn() {
        Ok(s) => s,
        Err(msg) => {
            eprintln!("Skipping test, cannot spawn server: {}", msg);
            return;
        }
    };

    let mut scenario = ScenarioSession::connect(&server)
        .await
        .expect("connect scenario");
    let sid = scenario.ensure_channel("SIM:AO").await.expect("sid ao");
    let ioid = 5010u32;

    scenario
        .session
        .send_client_op(11, sid, ioid, 0x00, &[0x00, 0x02])
        .await
        .expect("put data without init");
    let cmd = scenario
        .session
        .read_until(
            Duration::from_secs(2),
            |cmd| matches!(cmd, PvaPacketCommand::Op(op) if op.command == 11 && op.ioid == ioid),
        )
        .await
        .expect("put error response");

    match cmd {
        PvaPacketCommand::Op(op) => {
            let status = op.status.expect("status");
            assert_ne!(status.code, 0xFF);
            let msg = status.message.unwrap_or_default();
            assert!(
                msg.contains("PUT without init"),
                "unexpected message: {}",
                msg
            );
        }
        other => panic!("unexpected command: {:?}", other),
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn lifecycle_destroy_channel_invalidates_sid() {
    let server = match TestServer::spawn() {
        Ok(s) => s,
        Err(msg) => {
            eprintln!("Skipping test, cannot spawn server: {}", msg);
            return;
        }
    };

    let mut scenario = ScenarioSession::connect(&server)
        .await
        .expect("connect scenario");
    let sid = scenario.ensure_channel("SIM:AI").await.expect("sid ai");
    let cid = 9901u32;
    let ioid = 9902u32;

    let mut destroy_payload = Vec::new();
    destroy_payload.extend_from_slice(&sid.to_le_bytes());
    destroy_payload.extend_from_slice(&cid.to_le_bytes());
    scenario
        .session
        .send_client_application(8, &destroy_payload)
        .await
        .expect("destroy channel");

    scenario
        .session
        .send_client_op(10, sid, ioid, 0x08, &PV_REQUEST_EMPTY)
        .await
        .expect("get after destroy");
    let cmd = scenario
        .session
        .read_until(Duration::from_secs(2), |cmd| {
            matches!(cmd, PvaPacketCommand::Op(op) if op.command == 10 && op.ioid == ioid && (op.subcmd & 0x08) != 0)
        })
        .await
        .expect("op error");

    match cmd {
        PvaPacketCommand::Op(op) => {
            let status = op.status.expect("status");
            let msg = status.message.unwrap_or_default();
            assert!(msg.contains("Unknown SID"), "unexpected error msg: {}", msg);
        }
        other => panic!("unexpected command after destroy: {:?}", other),
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn lifecycle_application_echo_roundtrip() {
    let server = match TestServer::spawn() {
        Ok(s) => s,
        Err(msg) => {
            eprintln!("Skipping test, cannot spawn server: {}", msg);
            return;
        }
    };

    let mut scenario = ScenarioSession::connect(&server)
        .await
        .expect("connect scenario");

    let payload = b"echo-smoke";
    scenario
        .session
        .send_client_application(2, payload)
        .await
        .expect("send application echo");

    let cmd = scenario
        .session
        .read_until(Duration::from_secs(2), |cmd| {
            matches!(cmd, PvaPacketCommand::Echo(_))
        })
        .await
        .expect("echo response");

    match cmd {
        PvaPacketCommand::Echo(bytes) => assert_eq!(bytes, payload),
        other => panic!("unexpected echo response: {:?}", other),
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn lifecycle_rpc_list_mode_gated_behavior() {
    let server = match TestServer::spawn_with_args(&["--pvlist-mode", "list"]) {
        Ok(s) => s,
        Err(msg) => {
            eprintln!("Skipping test, cannot spawn server: {}", msg);
            return;
        }
    };

    let mut scenario = ScenarioSession::connect(&server)
        .await
        .expect("connect scenario");
    let sid = scenario.ensure_channel("server").await.expect("sid server");
    let ioid = 6101u32;

    scenario
        .session
        .send_client_op(20, sid, ioid, 0x08, &PV_REQUEST_EMPTY)
        .await
        .expect("rpc init");
    let init_cmd = scenario
        .session
        .read_until(Duration::from_secs(2), |cmd| {
            matches!(cmd, PvaPacketCommand::Op(op) if op.command == 20 && op.ioid == ioid && (op.subcmd & 0x08) != 0)
        })
        .await
        .expect("rpc init response");
    match init_cmd {
        PvaPacketCommand::Op(op) => {
            if let Some(status) = op.status {
                assert_eq!(
                    status.code, 0xFF,
                    "unexpected rpc init status: {:?}",
                    status
                );
            }
        }
        other => panic!("unexpected rpc init response: {:?}", other),
    }

    scenario
        .session
        .send_client_op(20, sid, ioid, 0x00, &[])
        .await
        .expect("rpc data");
    let data_cmd = scenario
        .session
        .read_until(Duration::from_secs(2), |cmd| {
            matches!(cmd, PvaPacketCommand::Op(op) if op.command == 20 && op.ioid == ioid && op.subcmd == 0x00)
        })
        .await
        .expect("rpc data response");
    match data_cmd {
        PvaPacketCommand::Op(op) => {
            assert!(
                !op.body.is_empty(),
                "rpc data response should contain payload body"
            );
        }
        other => panic!("unexpected rpc data response: {:?}", other),
    }

    let server = match TestServer::spawn_with_args(&["--pvlist-mode", "off"]) {
        Ok(s) => s,
        Err(msg) => {
            eprintln!("Skipping test, cannot spawn server: {}", msg);
            return;
        }
    };
    let mut scenario = ScenarioSession::connect(&server)
        .await
        .expect("connect scenario");
    let sid = scenario.ensure_channel("SIM:AI").await.expect("sid ai");
    let ioid = 6201u32;
    scenario
        .session
        .send_client_op(20, sid, ioid, 0x08, &PV_REQUEST_EMPTY)
        .await
        .expect("rpc init off mode");
    let denied_cmd = scenario
        .session
        .read_until(Duration::from_secs(2), |cmd| {
            matches!(cmd, PvaPacketCommand::Op(op) if op.command == 20 && op.ioid == ioid && (op.subcmd & 0x08) != 0)
        })
        .await
        .expect("rpc deny response");
    match denied_cmd {
        PvaPacketCommand::Op(op) => {
            let status = op.status.expect("expected error status");
            let msg = status.message.unwrap_or_default();
            assert!(
                msg.contains("Operation not supported"),
                "unexpected rpc deny message: {}",
                msg
            );
        }
        other => panic!("unexpected rpc denied response: {:?}", other),
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn lifecycle_udp_search_returns_matching_cid() {
    let server = match TestServer::spawn() {
        Ok(s) => s,
        Err(msg) => {
            eprintln!("Skipping test, cannot spawn server: {}", msg);
            return;
        }
    };

    let socket = UdpSocket::bind("127.0.0.1:0").await.expect("bind udp");
    let local = socket.local_addr().expect("local addr");

    let seq = 0x12345678u32;
    let cid = 0x01020304u32;
    let mut payload = Vec::new();
    payload.extend_from_slice(&seq.to_le_bytes());
    payload.push(0x81);
    payload.extend_from_slice(&[0u8; 3]);
    payload.extend_from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 127, 0, 0, 1]);
    payload.extend_from_slice(&local.port().to_le_bytes());
    payload.extend_from_slice(&encode_size(1));
    payload.extend_from_slice(&encode_string("tcp"));
    payload.extend_from_slice(&1u16.to_le_bytes());
    payload.extend_from_slice(&cid.to_le_bytes());
    payload.extend_from_slice(&encode_string("SIM:AI"));

    let mut frame = encode_header(false, false, false, 2, 3, payload.len() as u32);
    frame.extend_from_slice(&payload);
    socket
        .send_to(&frame, format!("127.0.0.1:{}", server.udp_port))
        .await
        .expect("send search");

    let mut buf = vec![0u8; 2048];
    let (len, _) = tokio::time::timeout(Duration::from_secs(2), socket.recv_from(&mut buf))
        .await
        .expect("search timeout")
        .expect("search recv");
    buf.truncate(len);

    let mut packet = spvirit_codec::epics_decode::PvaPacket::new(&buf);
    let decoded = packet.decode_payload().expect("search decode");
    match decoded {
        PvaPacketCommand::SearchResponse(resp) => {
            assert!(resp.found);
            assert!(resp.cids.contains(&cid));
            assert_eq!(resp.protocol, "tcp");
        }
        other => panic!("unexpected search response {:?}", other),
    }
}