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
// plctag-rs
//
// a rust wrapper of libplctag, with rust style APIs and useful extensions.
// Copyright: 2022, Joylei <leingliu@gmail.com>
// License: MIT

use crate::*;
use futures_util::{
    future::Future,
    task::{AtomicWaker, Context, Poll},
};
use plctag_core::ffi::PLCTAG_ERR_NOT_FOUND;
#[cfg(feature = "value")]
use plctag_core::{Decode, Encode};
use std::{
    ffi::c_void,
    pin::Pin,
    sync::atomic::{AtomicBool, AtomicI32, AtomicU8, Ordering},
};

const PLCTAG_EVENT_CREATED: i32 = plctag_core::ffi::PLCTAG_EVENT_CREATED as i32;
const PLCTAG_EVENT_READ_COMPLETED: i32 = plctag_core::ffi::PLCTAG_EVENT_READ_COMPLETED as i32;
const PLCTAG_EVENT_WRITE_COMPLETED: i32 = plctag_core::ffi::PLCTAG_EVENT_WRITE_COMPLETED as i32;
const PLCTAG_EVENT_DESTROYED: i32 = plctag_core::ffi::PLCTAG_EVENT_DESTROYED as i32;

const TAG_CREATED: u8 = 1;
const TAG_DESTROYED: u8 = 3;

/// tag entry, represents a tag in PLC controller
#[derive(Debug)]
pub struct AsyncTag {
    tag: RawTag,
    inner: Arc<Inner>,
    _guard: ArcGuard<Inner>,
}

#[derive(Debug)]
struct Inner {
    waker: AtomicWaker,
    state: AtomicU8,
    set: AtomicBool,
    event: AtomicI32,
    status: AtomicI32,
}

impl Inner {
    fn new() -> Self {
        Self {
            waker: AtomicWaker::new(),
            state: Default::default(),
            set: AtomicBool::new(false),
            event: AtomicI32::new(0),
            status: AtomicI32::new(0),
        }
    }

    #[inline]
    fn state(&self) -> u8 {
        self.state.load(Ordering::Acquire)
    }

    #[inline]
    fn take_event(&self) -> (i32, i32) {
        let event = self.event.swap(0, Ordering::Relaxed);
        let status = self.status.swap(0, Ordering::Relaxed);
        self.set.store(false, Ordering::Release);
        (event, status)
    }

    #[inline]
    fn set_event(&self, event: i32, status: i32) {
        match event {
            PLCTAG_EVENT_CREATED => {
                //dbg!("TAG_CREATED");
                self.state.store(TAG_CREATED, Ordering::Relaxed);
            }
            PLCTAG_EVENT_DESTROYED => {
                self.state.store(TAG_DESTROYED, Ordering::Relaxed);
            }
            _ => {}
        }
        self.event.store(event, Ordering::Relaxed);
        self.status.store(status, Ordering::Relaxed);
        self.set.store(true, Ordering::Relaxed);
        self.waker.wake();
    }

    fn notified(&self) -> Notified<'_> {
        Notified(self)
    }
}

struct Notified<'a>(&'a Inner);

impl Future for Notified<'_> {
    type Output = ();
    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        if self.0.set.load(Ordering::Relaxed) {
            return Poll::Ready(());
        }
        self.0.waker.register(cx.waker());
        if self.0.set.load(Ordering::Relaxed) {
            Poll::Ready(())
        } else {
            Poll::Pending
        }
    }
}

#[derive(Debug)]
struct ArcGuard<T> {
    ptr: *const T,
}

impl<T> Drop for ArcGuard<T> {
    #[inline]
    fn drop(&mut self) {
        unsafe {
            Arc::from_raw(self.ptr);
        }
    }
}

unsafe impl<T> Send for ArcGuard<T> {}
unsafe impl<T> Sync for ArcGuard<T> {}

impl AsyncTag {
    /// create instance of [`AsyncTag`]
    ///
    /// # Tag String Attributes
    /// See https://github.com/libplctag/libplctag/wiki/Tag-String-Attributes for tag string attributes.
    pub async fn create<P: Into<Vec<u8>>>(path: P) -> Result<Self> {
        extern "C" fn on_event(_tag: i32, event: i32, status: i32, user_data: *mut c_void) {
            match event {
                PLCTAG_EVENT_CREATED
                | PLCTAG_EVENT_DESTROYED
                | PLCTAG_EVENT_READ_COMPLETED
                | PLCTAG_EVENT_WRITE_COMPLETED => unsafe {
                    let ptr = user_data as *const Inner;
                    Arc::increment_strong_count(ptr);
                    let inner = Arc::from_raw(ptr);
                    inner.set_event(event, status);
                },
                _ => {}
            }
        }

        let inner = Arc::new(Inner::new());
        let guard = ArcGuard {
            ptr: Arc::into_raw(inner.clone()),
        };
        let tag = {
            let user_data = guard.ptr as *mut Inner as *mut c_void;
            unsafe { RawTag::new_with_callback(path, 0, Some(on_event), user_data) }?
        };
        // workaround for created event not triggered
        // if !tag.status().is_pending() {
        //     inner.state.store(TAG_FIRST_READ, Ordering::Release);
        // }
        Ok(Self {
            tag,
            inner,
            _guard: guard,
        })
    }

    /// wait until interested event is received
    #[inline]
    async fn recv_event(&self, event: i32) -> Status {
        let mut state = self.inner.state.load(Ordering::Acquire);
        loop {
            match state {
                TAG_DESTROYED => {
                    return Status::Err(PLCTAG_ERR_NOT_FOUND);
                }
                TAG_CREATED => {
                    let (evt, status) = self.inner.take_event();
                    if evt == event {
                        return Status::from(status);
                    }
                }
                _ => {}
            }
            self.inner.notified().await;
            state = self.inner.state.load(Ordering::Relaxed);
        }
    }

    /// wait until created
    #[inline]
    pub async fn ready(&mut self) -> Result<()> {
        let mut state = self.inner.state.load(Ordering::Acquire);
        loop {
            match state {
                TAG_DESTROYED => {
                    return Err(Status::Err(PLCTAG_ERR_NOT_FOUND).into());
                }
                TAG_CREATED => {
                    return Ok(());
                }
                _ => {}
            }
            self.inner.notified().await;
            state = self.inner.state.load(Ordering::Relaxed);
        }
    }

    /// perform read operation.
    #[inline]
    pub async fn read(&mut self) -> Result<()> {
        self.read_or_write(PLCTAG_EVENT_READ_COMPLETED).await
    }

    /// perform write operation
    #[inline]
    pub async fn write(&mut self) -> Result<()> {
        self.read_or_write(PLCTAG_EVENT_WRITE_COMPLETED).await
    }

    #[inline]
    async fn read_or_write(&mut self, event: i32) -> Result<()> {
        self.ready().await?;
        let mut guard = InflightGuard {
            tag: &self.tag,
            pending: true,
        };
        match event {
            PLCTAG_EVENT_WRITE_COMPLETED => {
                guard.write()?;
            }
            PLCTAG_EVENT_READ_COMPLETED => {
                guard.read()?;
            }
            _ => unreachable!(),
        }

        // pending
        let status = self.recv_event(event).await;
        debug_assert!(!status.is_pending());
        if status.is_err() {
            guard.pending = false;
            return Err(status.into());
        }
        drop(guard);
        Ok(())
    }

    /// poll status
    #[inline]
    pub fn status(&mut self) -> Status {
        match self.inner.state() {
            TAG_DESTROYED => Status::Err(PLCTAG_ERR_NOT_FOUND),
            _ => self.tag.status(),
        }
    }

    /// get tag attribute
    #[inline]
    pub fn get_attr(&mut self, attr: impl AsRef<str>, default_value: i32) -> Result<i32> {
        Ok(self.tag.get_attr(attr, default_value)?)
    }

    /// set tag attribute
    #[inline]
    pub fn set_attr(&mut self, attr: impl AsRef<str>, value: i32) -> Result<()> {
        Ok(self.tag.set_attr(attr, value)?)
    }

    /// element size
    #[inline]
    pub fn elem_size(&mut self) -> Result<i32> {
        Ok(self.tag.elem_size()?)
    }

    /// element count
    #[inline]
    pub fn elem_count(&mut self) -> Result<i32> {
        Ok(self.tag.elem_count()?)
    }

    /// tag size in bytes
    #[inline]
    pub fn size(&mut self) -> Result<u32> {
        Ok(self.tag.size()?)
    }

    /// set tag size in bytes, returns old size
    #[inline]
    pub fn set_size(&mut self, size: u32) -> Result<u32> {
        Ok(self.tag.set_size(size)?)
    }

    /// get bit value
    #[inline]
    pub fn get_bit(&mut self, bit_offset: u32) -> Result<bool> {
        Ok(self.tag.get_bit(bit_offset)?)
    }

    /// set bit value
    #[inline]
    pub fn set_bit(&mut self, bit_offset: u32, value: bool) -> Result<()> {
        Ok(self.tag.set_bit(bit_offset, value)?)
    }

    /// get value from mem, you should call read() before this operation
    #[inline]
    #[cfg(feature = "value")]
    pub fn get_value<T: Decode>(&mut self, byte_offset: u32) -> Result<T> {
        use plctag_core::ValueExt;
        let v = self.tag.get_value(byte_offset)?;
        Ok(v)
    }

    /// get value from mem, you should call read() before this operation
    #[inline]
    #[cfg(feature = "value")]
    pub fn get_value_in_place<T: Decode>(&mut self, byte_offset: u32, place: &mut T) -> Result<()> {
        use plctag_core::ValueExt;
        self.tag.get_value_in_place(byte_offset, place)?;
        Ok(())
    }

    /// set value in mem, you should call write() later
    #[cfg(feature = "value")]
    #[inline]
    pub fn set_value<T: Encode>(&mut self, byte_offset: u32, value: T) -> Result<()> {
        use plctag_core::ValueExt;
        self.tag.set_value(byte_offset, value)?;
        Ok(())
    }

    /// perform read & returns the value
    #[cfg(feature = "value")]
    #[inline]
    pub async fn read_value<T: Decode>(&mut self, offset: u32) -> Result<T> {
        use plctag_core::ValueExt;
        self.read().await?;
        //dbg!("read done", self.tag.status());
        Ok(self.tag.get_value(offset)?)
    }

    /// perform read & returns the value
    #[cfg(feature = "value")]
    #[inline]
    pub async fn read_value_in_place<T: Decode>(
        &mut self,
        offset: u32,
        place: &mut T,
    ) -> Result<()> {
        use plctag_core::ValueExt;
        self.read().await?;
        //dbg!("read done", self.tag.status());
        self.tag.get_value_in_place(offset, place)?;
        Ok(())
    }

    /// set the value and write to PLC Controller
    #[cfg(feature = "value")]
    #[inline]
    pub async fn write_value<T: Encode + Send>(&mut self, offset: u32, value: T) -> Result<()> {
        use plctag_core::ValueExt;
        self.ready().await?;
        self.tag.set_value(offset, value)?;
        self.write().await?;
        Ok(())
    }

    /// get raw bytes
    #[inline]
    pub fn get_bytes(&mut self, byte_offset: u32, buf: &mut [u8]) -> Result<usize> {
        let v = self.tag.get_bytes(byte_offset, buf)?;
        Ok(v)
    }

    /// get raw bytes.
    /// If buffer length would exceed the end of the data in the tag data buffer, an out of bounds error is returned
    #[inline]
    pub fn get_bytes_unchecked(&self, byte_offset: u32, buf: &mut [u8]) -> Result<usize> {
        Ok(self.tag.get_bytes_unchecked(byte_offset, buf)?)
    }

    /// set raw bytes
    #[inline]
    pub fn set_bytes(&mut self, byte_offset: u32, buf: &mut [u8]) -> Result<usize> {
        Ok(self.tag.set_bytes(byte_offset, buf)?)
    }

    /// set raw bytes.
    /// If buffer length would exceed the end of the data in the tag data buffer, an out of bounds error is returned
    #[inline]
    pub fn set_bytes_unchecked(&mut self, byte_offset: u32, buf: &[u8]) -> Result<usize> {
        Ok(self.tag.set_bytes_unchecked(byte_offset, buf)?)
    }

    /// take the inner
    pub fn into_raw(self) -> RawTag {
        self.tag.unregister_callback();
        self.tag
    }
}

struct InflightGuard<'a> {
    tag: &'a RawTag,
    pending: bool,
}

impl InflightGuard<'_> {
    #[inline]
    fn read(&mut self) -> Result<()> {
        let status = self.tag.read(0);
        match status {
            Status::Pending => {
                self.pending = true;
                Ok(())
            }
            Status::Err(_) => {
                self.pending = false;
                Err(status.into())
            }
            _ => unreachable!(),
        }
    }

    #[inline]
    fn write(&mut self) -> Result<()> {
        let status = self.tag.write(0);
        match status {
            Status::Pending => {
                self.pending = true;
                Ok(())
            }
            Status::Err(_) => {
                self.pending = false;
                Err(status.into())
            }
            _ => unreachable!(),
        }
    }
}

impl Drop for InflightGuard<'_> {
    #[inline]
    fn drop(&mut self) {
        if self.pending {
            let _ = self.tag.abort();
        }
    }
}