epicars 0.1.2

Standalone, pure rust implementation of EPICS CA protocol
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
use std::{
    collections::HashMap,
    marker::PhantomData,
    sync::{Arc, Mutex},
    time::SystemTime,
};

use tokio::sync::{
    broadcast::{self},
    mpsc::{self, error::TrySendError},
};
use tracing::{error, info};

use crate::{
    Provider,
    dbr::{Dbr, DbrBasicType, DbrType, DbrValue, IntoDbrBasicType, Status},
    messages::{self, ErrorCondition, MonitorMask},
};

#[derive(Clone, Debug)]
struct PV {
    name: String,
    value: Arc<Mutex<DbrValue>>,
    /// Minimum array length. If set, at least this many array items will
    /// be sent to subscribers, and if a longer value is assigned then this
    /// minimum length will be increased. If None, then only the current
    /// array length items will be sent.
    minimum_length: Option<usize>,
    /// The optional type to serialize as. This is useful for forcing an
    /// otherwise String DbrValue to be DbrValue::Char when sending off.
    force_dbr_type: Option<DbrBasicType>,
    /// The last time this value was written
    timestamp: SystemTime,
    /// Channel to send updates to EPIC clients
    sender: broadcast::Sender<Dbr>,
    /// Trigger channel, to notify the server there is a new broadcast available
    triggers: Vec<mpsc::Sender<String>>,
}

impl PV {
    pub fn load(&self) -> DbrValue {
        let value = self.value.lock().unwrap();
        value.clone()
    }

    /// Load the value to a Dbr ready to send to an CA client
    ///
    /// This includes adjustments for minimum size, and encoding (e.g.
    /// sending a string as a Char array instead of restricting to 40-chars)
    pub fn load_for_ca(&self) -> Dbr {
        let mut value = self.value.lock().unwrap().clone();
        if let Some(to_type) = self.force_dbr_type
            && value.get_type() != to_type
        {
            value = value
                .convert_to(to_type)
                .expect("PV Logic should ensure stored value can always be converted")
        }
        // Handle minimum length
        if let Some(size) = self.minimum_length
            && value.get_count() < size
        {
            let _ = value.resize(size);
        }
        Dbr::Time {
            status: Status::default(),
            timestamp: self.timestamp,
            value,
        }
    }
    /// Store a value from the CA protocol to the PV
    ///
    /// In this case, there are special behaviour like e.g. parsing
    /// numbers out of string data type
    fn store_from_ca(&mut self, value: &DbrValue) -> Result<(), ErrorCondition> {
        let native_type = self.value.lock().unwrap().get_type();
        let value = if value.get_type() == DbrBasicType::String {
            value
                .parse_into(native_type)
                .map_err(|_| ErrorCondition::NoConvert)?
        } else {
            value.clone()
        };
        self.store(&value)
    }

    pub fn store(&mut self, value: &DbrValue) -> Result<(), ErrorCondition> {
        // Now update the shared value
        {
            let stored_value = &mut *self.value.lock().unwrap();
            *stored_value = value.convert_to(stored_value.get_type())?;
            // Update the minimum length, if we are now longer
            if let Some(size) = self.minimum_length
                && stored_value.get_count() > size
            {
                self.minimum_length = Some(stored_value.get_count());
            }
            // Ensure lock is dropped
        }
        self.timestamp = SystemTime::now();
        // Now send off the new value to any listeners
        let _ = self.sender.send(self.load_for_ca());
        // Send the "please look at" triggers, filtering out any that are dead
        self.triggers = self
            .triggers
            .iter()
            .filter_map(|t| match t.try_send(self.name.clone()) {
                Ok(_) => Some(t.clone()),
                Err(TrySendError::Full(_)) => Some(t.clone()),
                Err(TrySendError::Closed(_)) => None,
            })
            .collect();
        Ok(())
    }
}

impl Default for PV {
    fn default() -> Self {
        PV {
            name: String::new(),
            value: Arc::new(Mutex::new(DbrValue::Int(vec![0]))),
            minimum_length: None,
            force_dbr_type: None,
            timestamp: SystemTime::now(),
            sender: broadcast::Sender::new(16),
            triggers: Vec::new(),
        }
    }
}

/// Typed interface to reading single values to/from a PV
#[derive(Clone)]
pub struct Intercom<T>
where
    T: IntoDbrBasicType,
{
    pv: Arc<Mutex<PV>>,
    _marker: PhantomData<T>,
}

impl<T> Intercom<T>
where
    T: IntoDbrBasicType + Clone + Default,
    for<'a> Vec<T>: TryFrom<&'a DbrValue>,
    DbrValue: From<Vec<T>>,
{
    fn new(pv: Arc<Mutex<PV>>) -> Self {
        Self {
            pv,
            _marker: PhantomData,
        }
    }

    pub fn load(&self) -> T {
        let value = self.pv.lock().unwrap().load();
        // Convert the DbrValue into T
        let ex: Vec<T> = match (&value).try_into() {
            Ok(v) => v,
            _ => panic!("Provider logic should ensure this conversion never fails!"),
        };
        // Extract the zeroth value from this vector
        ex.first().unwrap_or(&T::default()).clone()
    }

    pub fn store(&mut self, value: &T) {
        self.pv
            .lock()
            .unwrap()
            .store(&(vec![value.clone()]).into())
            .expect("Provider logic should ensure this never fails");
    }
}

#[derive(Clone)]
pub struct VecIntercom<T>
where
    T: IntoDbrBasicType,
{
    pv: Arc<Mutex<PV>>,
    _marker: PhantomData<T>,
}

impl<T> VecIntercom<T>
where
    T: IntoDbrBasicType + Clone + Default,
    for<'a> Vec<T>: TryFrom<&'a DbrValue>,
    DbrValue: From<Vec<T>>,
{
    fn new(pv: Arc<Mutex<PV>>) -> Self {
        Self {
            pv,
            _marker: PhantomData,
        }
    }

    pub fn load(&self) -> Vec<T> {
        match (&self.pv.lock().unwrap().load()).try_into() {
            Ok(v) => v,
            _ => panic!("Provider logic should ensure this conversion never fails!"),
        }
    }

    pub fn store(&mut self, value: &[T]) {
        self.pv
            .lock()
            .unwrap()
            .store(&value.to_vec().into())
            .expect("Provider logic should ensure this never fails");
    }
}

#[derive(Debug)]
pub struct StringIntercom {
    pv: Arc<Mutex<PV>>,
}

impl StringIntercom {
    fn new(pv: Arc<Mutex<PV>>) -> Self {
        assert!(pv.lock().unwrap().value.lock().unwrap().get_type() == DbrBasicType::String);
        Self { pv }
    }
    pub fn load(&self) -> String {
        let DbrValue::String(value) = self.pv.lock().unwrap().load() else {
            panic!("StringIntercom PV is not of string type!");
        };
        match value.as_slice() {
            [] => String::new(),
            [value] => value.clone(),
            _ => panic!("Got multi-value string DbrValue in StringIntercom!"),
        }
    }
    pub fn store(&mut self, value: &str) {
        self.pv
            .lock()
            .unwrap()
            .store(&vec![value.to_owned()].into())
            .expect("Provider logic should ensure this never fails");
    }
}

#[derive(Debug)]
pub struct PVAlreadyExists;

#[derive(Clone, Default)]
pub struct IntercomProvider {
    pvs: Arc<Mutex<HashMap<String, Arc<Mutex<PV>>>>>,
}

impl IntercomProvider {
    pub fn new() -> IntercomProvider {
        IntercomProvider {
            pvs: Arc::new(Mutex::new(HashMap::new())),
        }
    }

    fn register_pv(&mut self, pv: Arc<Mutex<PV>>) -> Result<(), PVAlreadyExists> {
        let name = &pv.lock().unwrap().name;
        let mut pvmap = self.pvs.lock().unwrap();
        if pvmap.contains_key(name) {
            return Err(PVAlreadyExists);
        }
        let _ = pvmap.insert(name.to_owned(), pv.clone());
        Ok(())
    }

    pub fn add_pv<T>(
        &mut self,
        name: &str,
        initial_value: T,
    ) -> Result<Intercom<T>, PVAlreadyExists>
    where
        T: IntoDbrBasicType + Clone + Default,
        for<'a> Vec<T>: TryFrom<&'a DbrValue>,
        DbrValue: From<Vec<T>>,
    {
        let pv = Arc::new(Mutex::new(PV {
            name: name.to_owned(),
            value: Arc::new(Mutex::new(DbrValue::from(vec![initial_value.clone()]))),
            ..Default::default()
        }));
        self.register_pv(pv.clone())?;
        Ok(Intercom::<T>::new(pv))
    }

    pub fn add_vec_pv<T>(
        &mut self,
        name: &str,
        initial_value: Vec<T>,
        minimum_length: Option<usize>,
    ) -> Result<VecIntercom<T>, PVAlreadyExists>
    where
        T: IntoDbrBasicType + Clone + Default,
        for<'a> Vec<T>: TryFrom<&'a DbrValue>,
        DbrValue: From<Vec<T>>,
    {
        // let pv = self.create_pv(name, DbrValue::from(initial_value.clone()), minimum_length)?;
        let pv = Arc::new(Mutex::new(PV {
            name: name.to_owned(),
            value: Arc::new(Mutex::new(DbrValue::from(initial_value.clone()))),
            minimum_length,
            ..Default::default()
        }));
        self.register_pv(pv.clone())?;
        Ok(VecIntercom::<T>::new(pv))
    }

    pub fn add_string_pv(
        &mut self,
        name: &str,
        initial_value: &str,
        minimum_u8_len: Option<usize>,
    ) -> Result<StringIntercom, PVAlreadyExists> {
        let pv = Arc::new(Mutex::new(PV {
            name: name.to_owned(),
            minimum_length: minimum_u8_len,
            force_dbr_type: Some(DbrBasicType::Char),
            value: Arc::new(Mutex::new(DbrValue::String(vec![initial_value.to_owned()]))),
            ..Default::default()
        }));
        self.register_pv(pv.clone())?;
        Ok(StringIntercom::new(pv))
    }
}

impl Provider for IntercomProvider {
    fn provides(&self, pv_name: &str) -> bool {
        self.pvs.lock().unwrap().contains_key(pv_name)
    }

    fn read_value(
        &self,
        pv_name: &str,
        _requested_type: Option<DbrType>,
    ) -> Result<Dbr, ErrorCondition> {
        let pv = {
            let pvmap = self.pvs.lock().unwrap();
            pvmap
                .get(pv_name)
                .ok_or(ErrorCondition::UnavailInServ)?
                .clone()
        };
        let pv = pv.lock().unwrap();
        Ok(pv.load_for_ca())
    }

    fn get_access_right(
        &self,
        _pv_name: &str,
        _client_user_name: Option<&str>,
        _client_host_name: Option<&str>,
    ) -> messages::Access {
        messages::Access::ReadWrite
    }

    fn write_value(&mut self, pv_name: &str, value: Dbr) -> Result<(), ErrorCondition> {
        let mut pvmap = self.pvs.lock().unwrap();
        let mut pv = pvmap
            .get_mut(pv_name)
            .ok_or(ErrorCondition::UnavailInServ)?
            .lock()
            .unwrap();
        info!("Provider: Processing write: {value:?}");
        if let Err(e) = pv.store_from_ca(value.value()) {
            error!("    Error: {e:?}");
            Err(e)
        } else {
            Ok(())
        }
    }

    fn monitor_value(
        &mut self,
        pv_name: &str,
        _data_type: DbrType,
        _data_count: usize,
        _mask: MonitorMask,
        trigger: mpsc::Sender<String>,
    ) -> Result<broadcast::Receiver<Dbr>, ErrorCondition> {
        let mut pvmap = self.pvs.lock().unwrap();
        let mut pv = pvmap
            .get_mut(pv_name)
            .ok_or(ErrorCondition::UnavailInServ)?
            .lock()
            .unwrap();
        pv.triggers.push(trigger);
        Ok(pv.sender.subscribe())
    }
}

#[cfg(test)]
mod tests {
    use std::sync::{Arc, Mutex};

    use crate::{
        dbr::DbrBasicType,
        providers::intercom::{PV, StringIntercom},
    };

    #[test]
    fn test_string_intercom() {
        let pv = Arc::new(Mutex::new(PV {
            name: "TEST".to_owned(),
            value: Arc::new(Mutex::new(vec!["Test String".to_owned()].into())),
            force_dbr_type: Some(DbrBasicType::Char),
            ..Default::default()
        }));
        let si = StringIntercom::new(pv.clone());
        assert_eq!(si.load(), "Test String");
        assert_eq!(
            pv.lock().unwrap().load_for_ca().data_type().basic_type,
            DbrBasicType::Char
        );
    }
}