dnp3 1.6.0

Rust implementation of DNP3 (IEEE 1815) with idiomatic bindings for C, C++, .NET, and Java
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
use std::time::Duration;

use crate::app::format::write::HeaderWriter;
use crate::app::gen::count::CountVariation;
use crate::app::parse::parser::Response;
use crate::app::variations::{Group50Var1, Group50Var3};
use crate::app::FunctionCode;
use crate::app::Timestamp;
use crate::master::association::Association;
use crate::master::error::{TaskError, TimeSyncError};
use crate::master::promise::Promise;
use crate::master::request::TimeSyncProcedure;
use crate::master::tasks::{AppTask, NonReadTask, Task};

use tokio::time::Instant;

enum State {
    MeasureDelay(Option<Instant>),
    WriteAbsoluteTime(Timestamp),
    RecordCurrentTime(Option<Timestamp>),
    WriteLastRecordedTime(Timestamp),
}

pub(crate) struct TimeSyncTask {
    state: State,
    promise: Option<Promise<Result<(), TimeSyncError>>>,
}

impl From<TimeSyncTask> for Task {
    fn from(value: TimeSyncTask) -> Self {
        Task::App(AppTask::NonRead(NonReadTask::TimeSync(value)))
    }
}

impl TimeSyncProcedure {
    fn get_start_state(&self) -> State {
        match self {
            TimeSyncProcedure::Lan => State::RecordCurrentTime(None),
            TimeSyncProcedure::NonLan => State::MeasureDelay(None),
        }
    }
}

impl TimeSyncTask {
    fn new(state: State, promise: Option<Promise<Result<(), TimeSyncError>>>) -> Self {
        Self { state, promise }
    }

    fn change_state(self, state: State) -> Self {
        TimeSyncTask::new(state, self.promise)
    }

    pub(crate) fn get_procedure(
        procedure: TimeSyncProcedure,
        promise: Option<Promise<Result<(), TimeSyncError>>>,
    ) -> Self {
        Self::new(procedure.get_start_state(), promise)
    }

    pub(crate) fn wrap(self) -> NonReadTask {
        NonReadTask::TimeSync(self)
    }

    pub(crate) fn start(mut self, association: &mut Association) -> Option<Self> {
        match &mut self.state {
            State::MeasureDelay(time) => {
                time.replace(Instant::now());

                match association.get_system_time() {
                    Some(_) => Some(self),
                    None => {
                        self.report_error(association, TimeSyncError::SystemTimeNotAvailable);
                        None
                    }
                }
            }
            State::WriteAbsoluteTime(_) => Some(self),
            State::RecordCurrentTime(time) => {
                *time = association.get_system_time();

                match time {
                    Some(_) => Some(self),
                    None => {
                        self.report_error(association, TimeSyncError::SystemTimeNotAvailable);
                        None
                    }
                }
            }
            State::WriteLastRecordedTime(_) => Some(self),
        }
    }

    pub(crate) fn function(&self) -> FunctionCode {
        match self.state {
            State::MeasureDelay(_) => FunctionCode::DelayMeasure,
            State::WriteAbsoluteTime(_) => FunctionCode::Write,
            State::RecordCurrentTime(_) => FunctionCode::RecordCurrentTime,
            State::WriteLastRecordedTime(_) => FunctionCode::Write,
        }
    }

    pub(crate) fn write(&self, writer: &mut HeaderWriter) -> Result<(), scursor::WriteError> {
        match self.state {
            State::MeasureDelay(_) => Ok(()),
            State::WriteAbsoluteTime(x) => writer.write_count_of_one(Group50Var1 { time: x }),
            State::RecordCurrentTime(_) => Ok(()),
            State::WriteLastRecordedTime(x) => writer.write_count_of_one(Group50Var3 { time: x }),
        }
    }

    pub(crate) fn on_task_error(self, association: Option<&mut Association>, err: TaskError) {
        match self.promise {
            None => {
                if let Some(association) = association {
                    association.on_time_sync_failure(err.into());
                }
            }
            Some(x) => x.complete(Err(err.into())),
        }
    }

    pub(crate) fn handle(
        self,
        association: &mut Association,
        response: Response,
    ) -> Result<Option<NonReadTask>, TaskError> {
        match self.state {
            State::MeasureDelay(time) => self.handle_delay_measure(association, time, response),
            State::WriteAbsoluteTime(_) => self.handle_write_absolute_time(association, response),
            State::RecordCurrentTime(time) => {
                self.handle_record_current_time(association, time, response)
            }
            State::WriteLastRecordedTime(_) => {
                self.handle_write_last_recorded_time(association, response)
            }
        }
    }

    fn handle_delay_measure(
        self,
        association: &mut Association,
        request_tx: Option<Instant>,
        response: Response,
    ) -> Result<Option<NonReadTask>, TaskError> {
        let request_tx = request_tx.unwrap_or_else(Instant::now);
        let now = Instant::now();

        let interval = match now.checked_duration_since(request_tx) {
            Some(x) => x,
            None => {
                // This should NEVER happen. `tokio::time::Instant` is guaranteed to be monotonic
                tracing::error!("clock rollback detected while synchronizing outstation");
                self.report_error(association, TimeSyncError::ClockRollback);
                return Err(TaskError::UnexpectedResponseHeaders);
            }
        };

        let header = match response.get_only_object_header() {
            Ok(x) => x,
            Err(err) => {
                let err: TaskError = err.into();
                self.report_error(association, err.into());
                return Err(err);
            }
        };

        let delay_ms: Option<u16> =
            if let Some(CountVariation::Group52Var2(seq)) = header.details.count() {
                seq.single().map(|x| x.time)
            } else {
                None
            };

        let delay_ms = match delay_ms {
            Some(x) => x,
            None => {
                tracing::warn!("received unexpected header(s) in response to delay measure");
                self.report_error(association, TaskError::UnexpectedResponseHeaders.into());
                return Err(TaskError::UnexpectedResponseHeaders);
            }
        };

        // IEEE 1815-2012, pg 301:(Time at [D] – Time at [A] – outstation processing delay) / 2.
        let propagation_delay: Duration =
            match interval.checked_sub(Duration::from_millis(delay_ms as u64)) {
                Some(x) => x / 2,
                None => {
                    tracing::warn!("outstation time delay is larger than the response delay");
                    self.report_error(association, TimeSyncError::BadOutstationTimeDelay(delay_ms));
                    return Err(TaskError::UnexpectedResponseHeaders);
                }
            };

        let time = match association.get_system_time() {
            Some(time) => time,
            None => {
                tracing::warn!("system time not available");
                self.report_error(association, TimeSyncError::SystemTimeNotAvailable);
                return Err(TaskError::UnexpectedResponseHeaders);
            }
        };

        let timestamp = match Self::get_timestamp(time, propagation_delay) {
            Err(err) => {
                tracing::error!("{}", err);
                self.report_error(association, err);
                return Err(TaskError::UnexpectedResponseHeaders);
            }
            Ok(ts) => ts,
        };

        Ok(Some(
            self.change_state(State::WriteAbsoluteTime(timestamp))
                .wrap(),
        ))
    }

    fn handle_write_absolute_time(
        self,
        association: &mut Association,
        response: Response,
    ) -> Result<Option<NonReadTask>, TaskError> {
        if !response.raw_objects.is_empty() {
            let err = TaskError::UnexpectedResponseHeaders;
            self.report_error(association, TimeSyncError::Task(err));
            return Err(err);
        }

        if response.header.iin.iin1.get_need_time() {
            self.report_error(association, TimeSyncError::StillNeedsTime);
            return Err(TaskError::UnexpectedResponseHeaders);
        }

        self.report_success(association);

        Ok(None)
    }

    fn handle_record_current_time(
        self,
        association: &mut Association,
        recorded_time: Option<Timestamp>,
        response: Response,
    ) -> Result<Option<NonReadTask>, TaskError> {
        if !response.raw_objects.is_empty() {
            self.report_error(
                association,
                TimeSyncError::Task(TaskError::UnexpectedResponseHeaders),
            );
            return Err(TaskError::UnexpectedResponseHeaders);
        }

        let recorded_time = recorded_time.expect("Recorded time should be set by the start method");
        Ok(Some(
            self.change_state(State::WriteLastRecordedTime(recorded_time))
                .wrap(),
        ))
    }

    fn handle_write_last_recorded_time(
        self,
        association: &mut Association,
        response: Response,
    ) -> Result<Option<NonReadTask>, TaskError> {
        if !response.raw_objects.is_empty() {
            self.report_error(
                association,
                TimeSyncError::Task(TaskError::UnexpectedResponseHeaders),
            );
            return Err(TaskError::UnexpectedResponseHeaders);
        }

        if response.header.iin.iin1.get_need_time() {
            self.report_error(association, TimeSyncError::StillNeedsTime);
            return Err(TaskError::UnexpectedResponseHeaders);
        }

        self.report_success(association);

        Ok(None)
    }

    fn get_timestamp(
        now: Timestamp,
        propagation_delay: Duration,
    ) -> Result<Timestamp, TimeSyncError> {
        match now.checked_add(propagation_delay) {
            Some(x) => Ok(x),
            None => Err(TimeSyncError::Overflow),
        }
    }

    fn report_success(self, association: &mut Association) {
        match self.promise {
            None => association.on_time_sync_success(),
            Some(x) => x.complete(Ok(())),
        }
    }

    fn report_error(self, association: &mut Association, error: TimeSyncError) {
        match self.promise {
            None => association.on_time_sync_failure(error),
            Some(x) => x.complete(Err(error)),
        }
    }
}

impl From<std::num::TryFromIntError> for TimeSyncError {
    fn from(_: std::num::TryFromIntError) -> Self {
        TimeSyncError::Overflow
    }
}

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

    use crate::app::format::write::*;
    use crate::app::Sequence;
    use crate::app::*;
    use crate::link::EndpointAddress;
    use crate::master::handler::AssociationHandler;
    use crate::master::tasks::RequestWriter;
    use crate::master::{AssociationConfig, AssociationInformation, ReadHandler};

    struct NullReadHandler;
    impl ReadHandler for NullReadHandler {}
    struct NullAssociationInformation;
    impl AssociationInformation for NullAssociationInformation {}

    use crate::transport::FragmentAddr;
    use crate::util::phys::PhysAddr;
    use scursor::WriteCursor;

    use super::*;

    fn response_control_field(seq: Sequence) -> ControlField {
        ControlField::response(seq, true, true, false)
    }

    struct SingleTimestampTestHandler {
        time: Arc<Mutex<Option<Timestamp>>>,
    }

    impl SingleTimestampTestHandler {
        fn new(time: Timestamp) -> Self {
            Self {
                time: Arc::new(Mutex::new(Some(time))),
            }
        }
    }

    struct TestHandler {
        time: Timestamp,
    }

    impl TestHandler {
        fn new(time: Timestamp) -> Self {
            Self { time }
        }
    }

    impl AssociationHandler for TestHandler {
        fn get_current_time(&self) -> Option<Timestamp> {
            Some(self.time)
        }
    }

    impl AssociationHandler for SingleTimestampTestHandler {
        fn get_current_time(&self) -> Option<Timestamp> {
            self.time.lock().unwrap().take()
        }
    }

    fn time_sync_setup(
        procedure: TimeSyncProcedure,
        handler: fn(Timestamp) -> Box<dyn AssociationHandler>,
    ) -> (
        NonReadTask,
        Timestamp,
        Association,
        tokio::sync::oneshot::Receiver<Result<(), TimeSyncError>>,
    ) {
        let dest = FragmentAddr {
            link: EndpointAddress::try_new(1).unwrap(),
            phys: PhysAddr::None,
        };

        let system_time = Timestamp::try_from_system_time(SystemTime::now()).unwrap();
        let association = Association::new(
            dest,
            AssociationConfig::default(),
            Box::new(NullReadHandler),
            handler(system_time),
            Box::new(NullAssociationInformation),
        );
        let (promise, rx) = Promise::one_shot();
        let task = NonReadTask::TimeSync(TimeSyncTask::get_procedure(procedure, Some(promise)));

        (task, system_time, association, rx)
    }

    async fn send_write_response(task: NonReadTask, association: &mut Association) {
        let mut buffer = [0; 20];
        let mut cursor = WriteCursor::new(&mut buffer);
        let writer = start_response(
            ControlField::request(Sequence::default()),
            ResponseFunction::Response,
            Iin::default(),
            &mut cursor,
        )
        .unwrap();
        let response = writer.to_parsed().to_response().unwrap();

        assert!(task.handle_response(association, response).await.is_ok());
    }

    mod non_lan {
        use crate::app::parse::parser::ParsedFragment;
        use crate::app::parse::traits::FixedSize;
        use crate::app::parse::traits::FixedSizeVariation;
        use crate::app::variations::Group52Var2;
        use crate::app::QualifierCode;

        use super::*;

        const OUTSTATION_DELAY_MS: u16 = 100;
        const TOTAL_DELAY_MS: u16 = 200;
        const PROPAGATION_DELAY_MS: u16 = (TOTAL_DELAY_MS - OUTSTATION_DELAY_MS) / 2;

        fn non_lan_time_sync_setup() -> (
            NonReadTask,
            Timestamp,
            Association,
            tokio::sync::oneshot::Receiver<Result<(), TimeSyncError>>,
        ) {
            time_sync_setup(TimeSyncProcedure::NonLan, |time| {
                Box::new(TestHandler::new(time))
            })
        }

        fn non_lan_time_sync_single_time_setup() -> (
            NonReadTask,
            Timestamp,
            Association,
            tokio::sync::oneshot::Receiver<Result<(), TimeSyncError>>,
        ) {
            time_sync_setup(TimeSyncProcedure::NonLan, |time| {
                Box::new(SingleTimestampTestHandler::new(time))
            })
        }

        #[tokio::test(start_paused = true)]
        async fn success() {
            let (task, system_time, mut association, mut rx) = non_lan_time_sync_setup();
            let task = check_measure_delay_request(task, &mut association);
            tokio::time::advance(Duration::from_millis(TOTAL_DELAY_MS as u64)).await;
            let task = send_measure_delay_response(task, &mut association)
                .await
                .unwrap()
                .unwrap();
            let task = check_write_request(task, &mut association, system_time);
            send_write_response(task, &mut association).await;
            assert!(rx.try_recv().unwrap().is_ok());
        }

        #[tokio::test(start_paused = true)]
        async fn with_16bit_count() {
            let (task, system_time, mut association, mut rx) = non_lan_time_sync_setup();

            let task = check_measure_delay_request(task, &mut association);
            tokio::time::advance(Duration::from_millis(TOTAL_DELAY_MS as u64)).await;

            let task = {
                let mut buffer = [0; 20];
                let mut cursor = WriteCursor::new(&mut buffer);
                let writer = start_response(
                    response_control_field(Sequence::default()),
                    ResponseFunction::Response,
                    Iin::default(),
                    &mut cursor,
                )
                .unwrap();
                {
                    let cursor = writer.inner();
                    Group52Var2::VARIATION.write(cursor).unwrap();
                    QualifierCode::Count16.write(cursor).unwrap();
                    cursor.write_u16_le(1).unwrap();
                    Group52Var2 {
                        time: OUTSTATION_DELAY_MS,
                    }
                    .write(cursor)
                    .unwrap();
                }
                let response = ParsedFragment::parse(cursor.written())
                    .unwrap()
                    .to_response()
                    .unwrap();

                task.handle_response(&mut association, response).await
            }
            .unwrap()
            .unwrap();

            let task = check_write_request(task, &mut association, system_time);
            send_write_response(task, &mut association).await;
            assert!(rx.try_recv().unwrap().is_ok());
        }

        #[tokio::test(start_paused = true)]
        async fn delay_reported_by_outstation_greater_than_actual_delay() {
            let (task, _system_time, mut association, mut rx) = non_lan_time_sync_setup();
            let task = check_measure_delay_request(task, &mut association);
            tokio::time::advance(Duration::from_millis(1)).await; // This delay is less than the reported delay of the outstation
            assert!(send_measure_delay_response(task, &mut association)
                .await
                .is_err());
            assert_eq!(
                Err(TimeSyncError::BadOutstationTimeDelay(OUTSTATION_DELAY_MS)),
                rx.try_recv().unwrap()
            );
        }

        #[tokio::test(start_paused = true)]
        async fn empty_measure_delay_response() {
            let (task, _system_time, mut association, mut rx) = non_lan_time_sync_setup();
            let task = check_measure_delay_request(task, &mut association);
            tokio::time::advance(Duration::from_millis(TOTAL_DELAY_MS as u64)).await;
            {
                let mut buffer = [0; 20];
                let mut cursor = WriteCursor::new(&mut buffer);
                let writer = start_response(
                    response_control_field(Sequence::default()),
                    ResponseFunction::Response,
                    Iin::default(),
                    &mut cursor,
                )
                .unwrap();

                let response = writer.to_parsed().to_response().unwrap();

                assert!(task
                    .handle_response(&mut association, response)
                    .await
                    .is_err());
            }
            assert_eq!(
                rx.try_recv().unwrap(),
                Err(TimeSyncError::Task(TaskError::UnexpectedResponseHeaders))
            );
        }

        #[tokio::test(start_paused = true)]
        async fn non_empty_write_response() {
            let (task, system_time, mut association, mut rx) = non_lan_time_sync_setup();
            let task = check_measure_delay_request(task, &mut association);
            tokio::time::advance(Duration::from_millis(TOTAL_DELAY_MS as u64)).await;
            let task = send_measure_delay_response(task, &mut association)
                .await
                .unwrap()
                .unwrap();
            let task = check_write_request(task, &mut association, system_time);
            {
                let mut buffer = [0; 20];
                let mut cursor = WriteCursor::new(&mut buffer);
                let mut writer = start_response(
                    response_control_field(Sequence::default()),
                    ResponseFunction::Response,
                    Iin::default(),
                    &mut cursor,
                )
                .unwrap();
                writer.write_class1230().unwrap();

                let response = writer.to_parsed().to_response().unwrap();

                assert!(task
                    .handle_response(&mut association, response)
                    .await
                    .is_err());
            }
            assert_matches!(
                rx.try_recv().unwrap(),
                Err(TimeSyncError::Task(TaskError::UnexpectedResponseHeaders))
            );
        }

        #[tokio::test(start_paused = true)]
        async fn iin_bit_not_reset() {
            let (task, system_time, mut association, mut rx) = non_lan_time_sync_setup();

            let task = check_measure_delay_request(task, &mut association);
            tokio::time::advance(Duration::from_millis(TOTAL_DELAY_MS as u64)).await;
            let task = send_measure_delay_response(task, &mut association)
                .await
                .unwrap()
                .unwrap();
            let task = check_write_request(task, &mut association, system_time);
            {
                let mut buffer = [0; 20];
                let mut cursor = WriteCursor::new(&mut buffer);
                let writer = start_response(
                    response_control_field(Sequence::default()),
                    ResponseFunction::Response,
                    Iin::new(Iin1::new(0x10), Iin2::new(0x00)),
                    &mut cursor,
                )
                .unwrap();

                let response = writer.to_parsed().to_response().unwrap();

                assert!(task
                    .handle_response(&mut association, response)
                    .await
                    .is_err());
            }
            assert_matches!(rx.try_recv().unwrap(), Err(TimeSyncError::StillNeedsTime));
        }

        #[tokio::test(start_paused = true)]
        async fn error_response() {
            let (task, system_time, mut association, mut rx) = non_lan_time_sync_setup();
            let task = check_measure_delay_request(task, &mut association);
            tokio::time::advance(Duration::from_millis(TOTAL_DELAY_MS as u64)).await;
            let task = send_measure_delay_response(task, &mut association)
                .await
                .unwrap()
                .unwrap();
            let task = check_write_request(task, &mut association, system_time);
            task.on_task_error(Some(&mut association), TaskError::ResponseTimeout);
            assert_eq!(
                rx.try_recv().unwrap(),
                Err(TimeSyncError::Task(TaskError::ResponseTimeout))
            );
        }

        #[test]
        fn no_system_time_at_start() {
            let (task, _system_time, mut association, mut rx) =
                non_lan_time_sync_single_time_setup();
            association.get_system_time(); // Empty the time

            assert!(task.start(&mut association).is_none());
            assert_eq!(
                rx.try_recv().unwrap(),
                Err(TimeSyncError::SystemTimeNotAvailable)
            );
        }

        #[tokio::test(start_paused = true)]
        async fn no_system_time_at_delay() {
            let (task, _system_time, mut association, mut rx) =
                non_lan_time_sync_single_time_setup();
            let task = check_measure_delay_request(task, &mut association);
            tokio::time::advance(Duration::from_millis(TOTAL_DELAY_MS as u64)).await;
            assert!(send_measure_delay_response(task, &mut association)
                .await
                .is_err());
            assert_eq!(
                rx.try_recv().unwrap(),
                Err(TimeSyncError::SystemTimeNotAvailable)
            );
        }

        fn check_measure_delay_request(
            task: NonReadTask,
            association: &mut Association,
        ) -> NonReadTask {
            let mut buffer = [0; 20];
            let mut cursor = WriteCursor::new(&mut buffer);
            let task = task.start(association).unwrap();
            let mut writer = start_request(
                ControlField::request(Sequence::default()),
                task.function(),
                &mut cursor,
            )
            .unwrap();
            task.write(&mut writer).unwrap();
            let request = writer.to_parsed().to_request().unwrap();

            assert_eq!(request.header.function, FunctionCode::DelayMeasure);
            assert!(request.raw_objects.is_empty());

            task
        }

        async fn send_measure_delay_response(
            task: NonReadTask,
            association: &mut Association,
        ) -> Result<Option<NonReadTask>, TaskError> {
            let mut buffer = [0; 20];
            let mut cursor = WriteCursor::new(&mut buffer);
            let mut writer = start_response(
                response_control_field(Sequence::default()),
                ResponseFunction::Response,
                Iin::default(),
                &mut cursor,
            )
            .unwrap();
            writer
                .write_count_of_one(Group52Var2 {
                    time: OUTSTATION_DELAY_MS,
                })
                .unwrap();
            let response = writer.to_parsed().to_response().unwrap();

            task.handle_response(association, response).await
        }

        fn check_write_request(
            task: NonReadTask,
            association: &mut Association,
            system_time: Timestamp,
        ) -> NonReadTask {
            let mut buffer = [0; 20];
            let mut cursor = WriteCursor::new(&mut buffer);
            let task = task.start(association).unwrap();
            let mut writer = start_request(
                ControlField::request(Sequence::default()),
                task.function(),
                &mut cursor,
            )
            .unwrap();
            task.write(&mut writer).unwrap();
            let request = writer.to_parsed().to_request().unwrap();

            assert_eq!(request.header.function, FunctionCode::Write);
            let header = request.objects.unwrap().get_only_header().unwrap();
            match header.details.count().unwrap() {
                CountVariation::Group50Var1(seq) => {
                    assert_eq!(
                        seq.single().unwrap().time,
                        system_time
                            .checked_add(Duration::from_millis(PROPAGATION_DELAY_MS as u64))
                            .unwrap()
                    );
                }
                _ => panic!("wrong WRITE content"),
            }

            task
        }
    }

    mod lan {

        use super::*;

        const DELAY_MS: u16 = 200;

        #[tokio::test(start_paused = true)]
        async fn success() {
            let (task, system_time, mut association, mut rx) = lan_time_sync_setup();
            let task = check_record_current_time_request(task, &mut association);
            tokio::time::advance(Duration::from_millis(DELAY_MS as u64)).await;
            let task = send_record_current_time_response(task, &mut association)
                .await
                .unwrap()
                .unwrap();
            let task = check_write_request(task, &mut association, system_time);
            send_write_response(task, &mut association).await;
            assert!(rx.try_recv().unwrap().is_ok());
        }

        #[tokio::test(start_paused = true)]
        async fn non_empty_record_current_time_response() {
            let (task, _system_time, mut association, mut rx) = lan_time_sync_setup();
            let task = check_record_current_time_request(task, &mut association);
            tokio::time::advance(Duration::from_millis(DELAY_MS as u64)).await;
            {
                let mut buffer = [0; 20];
                let mut cursor = WriteCursor::new(&mut buffer);
                let mut writer = start_response(
                    response_control_field(Sequence::default()),
                    ResponseFunction::Response,
                    Iin::default(),
                    &mut cursor,
                )
                .unwrap();
                writer.write_class1230().unwrap();

                let response = writer.to_parsed().to_response().unwrap();

                assert!(task
                    .handle_response(&mut association, response)
                    .await
                    .is_err());
            }
            assert_eq!(
                rx.try_recv().unwrap(),
                Err(TimeSyncError::Task(TaskError::UnexpectedResponseHeaders))
            );
        }

        #[tokio::test(start_paused = true)]
        async fn non_empty_write_response() {
            let (task, system_time, mut association, mut rx) = lan_time_sync_setup();
            let task = check_record_current_time_request(task, &mut association);
            tokio::time::advance(Duration::from_millis(DELAY_MS as u64)).await;
            let task = send_record_current_time_response(task, &mut association)
                .await
                .unwrap()
                .unwrap();
            let task = check_write_request(task, &mut association, system_time);
            {
                let mut buffer = [0; 20];
                let mut cursor = WriteCursor::new(&mut buffer);
                let mut writer = start_response(
                    response_control_field(Sequence::default()),
                    ResponseFunction::Response,
                    Iin::default(),
                    &mut cursor,
                )
                .unwrap();
                writer.write_class1230().unwrap();

                let response = writer.to_parsed().to_response().unwrap();

                assert!(task
                    .handle_response(&mut association, response)
                    .await
                    .is_err());
            }
            assert_eq!(
                rx.try_recv().unwrap(),
                Err(TimeSyncError::Task(TaskError::UnexpectedResponseHeaders))
            );
        }

        #[tokio::test(start_paused = true)]
        async fn iin_bit_not_reset() {
            let (task, system_time, mut association, mut rx) = lan_time_sync_setup();
            let task = check_record_current_time_request(task, &mut association);
            tokio::time::advance(Duration::from_millis(DELAY_MS as u64)).await;
            let task = send_record_current_time_response(task, &mut association)
                .await
                .unwrap()
                .unwrap();
            let task = check_write_request(task, &mut association, system_time);
            {
                let mut buffer = [0; 20];
                let mut cursor = WriteCursor::new(&mut buffer);
                let writer = start_response(
                    response_control_field(Sequence::default()),
                    ResponseFunction::Response,
                    Iin::new(Iin1::new(0x10), Iin2::new(0x00)),
                    &mut cursor,
                )
                .unwrap();

                let response = writer.to_parsed().to_response().unwrap();

                assert!(task
                    .handle_response(&mut association, response)
                    .await
                    .is_err())
            }
            assert_matches!(rx.try_recv().unwrap(), Err(TimeSyncError::StillNeedsTime));
        }

        #[tokio::test(start_paused = true)]
        async fn error_response() {
            let (task, system_time, mut association, mut rx) = lan_time_sync_setup();
            let task = check_record_current_time_request(task, &mut association);
            tokio::time::advance(Duration::from_millis(DELAY_MS as u64)).await;
            let task = send_record_current_time_response(task, &mut association)
                .await
                .unwrap()
                .unwrap();
            let task = check_write_request(task, &mut association, system_time);
            task.on_task_error(Some(&mut association), TaskError::ResponseTimeout);
            assert_eq!(
                rx.try_recv().unwrap(),
                Err(TimeSyncError::Task(TaskError::ResponseTimeout))
            );
        }

        #[test]
        fn no_system_time_available() {
            let (task, _system_time, mut association, mut rx) = lan_time_sync_setup();
            association.get_system_time(); // Empty the time

            assert!(task.start(&mut association).is_none());
            assert_eq!(
                rx.try_recv().unwrap(),
                Err(TimeSyncError::SystemTimeNotAvailable)
            );
        }

        fn lan_time_sync_setup() -> (
            NonReadTask,
            Timestamp,
            Association,
            tokio::sync::oneshot::Receiver<Result<(), TimeSyncError>>,
        ) {
            time_sync_setup(TimeSyncProcedure::Lan, |time| {
                Box::new(SingleTimestampTestHandler::new(time))
            })
        }

        fn check_record_current_time_request(
            task: NonReadTask,
            association: &mut Association,
        ) -> NonReadTask {
            let mut buffer = [0; 20];
            let mut cursor = WriteCursor::new(&mut buffer);
            let task = task.start(association).unwrap();
            let mut writer = start_request(
                ControlField::request(Sequence::default()),
                task.function(),
                &mut cursor,
            )
            .unwrap();
            task.write(&mut writer).unwrap();
            let request = writer.to_parsed().to_request().unwrap();

            assert_eq!(request.header.function, FunctionCode::RecordCurrentTime);
            assert!(request.raw_objects.is_empty());
            assert!(association.get_system_time().is_none());
            task
        }

        async fn send_record_current_time_response(
            task: NonReadTask,
            association: &mut Association,
        ) -> Result<Option<NonReadTask>, TaskError> {
            let mut buffer = [0; 20];
            let mut cursor = WriteCursor::new(&mut buffer);
            let writer = start_response(
                response_control_field(Sequence::default()),
                ResponseFunction::Response,
                Iin::default(),
                &mut cursor,
            )
            .unwrap();
            let response = writer.to_parsed().to_response().unwrap();

            task.handle_response(association, response).await
        }

        fn check_write_request(
            task: NonReadTask,
            association: &mut Association,
            system_time: Timestamp,
        ) -> NonReadTask {
            let mut buffer = [0; 20];
            let mut cursor = WriteCursor::new(&mut buffer);
            let task = task.start(association).unwrap();
            let mut writer = start_request(
                ControlField::request(Sequence::default()),
                task.function(),
                &mut cursor,
            )
            .unwrap();
            task.write(&mut writer).unwrap();
            let request = writer.to_parsed().to_request().unwrap();

            assert_eq!(request.header.function, FunctionCode::Write);
            let header = request.objects.unwrap().get_only_header().unwrap();
            match header.details.count().unwrap() {
                CountVariation::Group50Var3(seq) => {
                    assert_eq!(seq.single().unwrap().time, system_time);
                }
                _ => panic!("wrong WRITE content"),
            }

            task
        }
    }
}