spodes-rs 0.7.1

A pure-Rust DLMS/COSEM stack: interface classes, transport, xDLMS services and security (IEC 62056 and the СТО 34.01-5.1-006-2023 / Р 1323565.1 GOST profiles).
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
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
//! A server-side request dispatcher (IEC 62056-5-3 / IEC 62056-53 server CF).
//!
//! [`crate::server::RequestDispatcher`] holds a set of COSEM interface objects and turns an
//! incoming GET / SET / ACTION request APDU into the matching response APDU by
//! routing it to the addressed object: GET reads an attribute, SET writes one
//! (via [`crate::interface::InterfaceClass::set_attribute`]), ACTION invokes a method. Objects are
//! addressed by their (class-id, logical-name) pair; an unknown address yields a
//! `object-undefined` data-access-result rather than an error.
//!
//! Only the NORMAL and WITH-LIST variants are dispatched; block-transfer request
//! types produce an EXCEPTION-RESPONSE, since block reassembly is the caller's
//! responsibility.

use crate::classes::association_ln::AssociationLn;
use crate::interface::InterfaceClass;
use crate::obis::ObisCode;
use crate::security::AuthMechanism;

use crate::classes::push_setup::{PushDeliveryRequest, PushSetup};
use crate::service::acse::{self, AssociationResponse};
use crate::service::action::{ActionRequest, ActionResponse};
use crate::service::error::{service_error, state_error, ConfirmedServiceError, ExceptionResponse};
use crate::service::get::{GetDataResult, GetRequest, GetResponse};
use crate::service::initiate::{InitiateRequest, InitiateResponse};
use crate::service::notification::DataNotification;
use crate::service::set::{SetRequest, SetResponse};
use crate::service::{data_access_result, tag, AttributeDescriptor, DataBlockSa, MethodDescriptor, ServiceError};
use crate::types::CosemDataType;
#[cfg(feature = "tracing")]
use tracing::{debug, warn};

/// Default block-transfer payload size. A GET result larger than this is sent in
/// GET-RESPONSE-WITH-DATABLOCK blocks of at most this many octets.
const DEFAULT_MAX_PDU: usize = 256;

/// The DLMS version number negotiated by [`RequestDispatcher::handle_aarq`].
const DLMS_VERSION: u8 = 6;

/// The smallest client-max-receive-pdu-size the server accepts.
const MIN_CLIENT_PDU: u16 = 12;

/// Server-max-receive-pdu-size offered in the negotiated InitiateResponse.
const SERVER_MAX_PDU: u16 = 0x0800;

/// SPODES AA role conformance (AND with the client's proposal):
/// - Public (LOWEST): get + block-get
/// - Reader (LLS): + selective + action
/// - Configurator (HLS): + set + block-set + data-notification (+ general-protection)
const CONFORMANCE_PUBLIC: u32 = 0x00_10_10;
const CONFORMANCE_READER: u32 = 0x00_10_15;
const CONFORMANCE_CONFIGURATOR: u32 = 0x40_18_9D;

/// `association_status` values (IEC 62056-6-2, Association LN attribute 8).
mod association_status {
    /// No association is open.
    pub const NON_ASSOCIATED: u8 = 0;
    /// The HLS handshake is still in progress (pass 3/4 pending).
    pub const ASSOCIATION_PENDING: u8 = 1;
    /// The association is open.
    pub const ASSOCIATED: u8 = 2;
}

/// `initiateError` values of the ConfirmedServiceError sent when the
/// InitiateRequest is rejected (IEC 62056-5-3).
mod initiate_error {
    /// The proposed DLMS version is lower than 6.
    pub const DLMS_VERSION_TOO_LOW: u8 = 1;
    /// The proposed conformance has no supported service in common.
    pub const INCOMPATIBLE_CONFORMANCE: u8 = 2;
    /// The proposed client PDU size is too small.
    pub const PDU_SIZE_TOO_SHORT: u8 = 3;
}

/// An outbound GET result being delivered in blocks.
#[derive(Debug, Clone)]
pub struct PendingGet {
    /// The serialized data awaiting block-by-block delivery.
    pub data: Vec<u8>,
    /// The block number of the next block to send.
    pub next_block: u32,
}

/// An inbound SET value being reassembled from datablocks.
#[derive(Debug, Clone)]
pub struct PendingSet {
    /// Target attribute of the in-progress SET.
    pub attribute: AttributeDescriptor,
    /// The accumulated A-XDR value bytes.
    pub buffer: Vec<u8>,
}

/// Block-transfer state that must survive across APDUs on the same AA.
///
/// Hosts that rebuild a [`RequestDispatcher`] per request (e.g. from a shared
/// object registry) should stash this on the connection and restore it before
/// each `dispatch`.
#[derive(Debug, Default, Clone)]
pub struct PendingBlocks {
    /// Outstanding GET datablock delivery, if any.
    pub get: Option<PendingGet>,
    /// Outstanding SET datablock reassembly, if any.
    pub set: Option<PendingSet>,
}

/// A collection of COSEM objects that answers GET/SET/ACTION requests.
pub struct RequestDispatcher {
    objects: Vec<Box<dyn InterfaceClass>>,
    max_pdu: usize,
    pending_get: Option<PendingGet>,
    pending_set: Option<PendingSet>,
    /// Current association for access rights checking (IEC 62056-5-3, 5.3.7).
    /// When set, GET/SET/ACTION are checked against the association's object_list.
    association: Option<AssociationLn>,
}

impl Default for RequestDispatcher {
    fn default() -> Self {
        Self::new()
    }
}

impl RequestDispatcher {
    /// Creates an empty dispatcher with the default block size.
    pub fn new() -> Self {
        RequestDispatcher {
            objects: Vec::new(),
            max_pdu: DEFAULT_MAX_PDU,
            pending_get: None,
            pending_set: None,
            association: None,
        }
    }

    /// Sets the maximum block-transfer payload size (octets). Results larger
    /// than this are delivered in GET-RESPONSE-WITH-DATABLOCK blocks.
    pub fn set_max_pdu(&mut self, max_pdu: usize) {
        self.max_pdu = max_pdu.max(1);
    }

    /// Registers an object.
    pub fn add(&mut self, object: Box<dyn InterfaceClass>) {
        #[cfg(feature = "tracing")]
        debug!(
            class_id = object.class_id(),
            logical_name = %object.logical_name(),
            "registering COSEM object"
        );
        self.objects.push(object);
    }

    /// Sets the current association for access rights checking.
    /// When an association is set, all GET/SET/ACTION requests are checked
    /// against the association's object_list access_rights.
    pub fn set_association(&mut self, assoc: AssociationLn) {
        #[cfg(feature = "tracing")]
        debug!("setting association for access rights checking");
        self.association = Some(assoc);
    }

    /// Restores block-transfer state previously taken with [`Self::take_pending`].
    pub fn restore_pending(&mut self, pending: PendingBlocks) {
        self.pending_get = pending.get;
        self.pending_set = pending.set;
    }

    /// Takes block-transfer state so the host can persist it across requests.
    pub fn take_pending(&mut self) -> PendingBlocks {
        PendingBlocks { get: self.pending_get.take(), set: self.pending_set.take() }
    }

    /// Returns a reference to the current association, if any.
    pub fn association(&self) -> Option<&AssociationLn> {
        self.association.as_ref()
    }

    /// Returns a mutable reference to the current association, if any.
    pub fn association_mut(&mut self) -> Option<&mut AssociationLn> {
        self.association.as_mut()
    }

    /// Assembles a [`PushDeliveryRequest`] for `push_setup`'s ACTION method 1
    /// (`push`): reads each object in `push_object_list` from this
    /// dispatcher's registry, wraps the values in a DataNotification
    /// (an Array when more than one object is listed), and pairs it with the
    /// destination and client SAP from `push_setup`.
    ///
    /// Mirrors the reference implementation's `push_build_notification_body` /
    /// `push_try_schedule_delivery`: the host is expected to call this from
    /// the `push` ACTION handler (this dispatcher owns the object registry
    /// that `PushSetup` itself does not hold) and then deliver `body` over the
    /// configured transport.
    pub fn build_push_delivery_request(
        &mut self,
        push_setup: &PushSetup,
        long_invoke_id_and_priority: u32,
    ) -> Result<PushDeliveryRequest, String> {
        let objects = push_setup.push_object_list();
        if objects.is_empty() {
            return Err("No push objects configured".to_string());
        }
        let mut values = Vec::with_capacity(objects.len());
        for entry in objects {
            let obj = self
                .find(entry.class_id, &entry.logical_name)
                .ok_or_else(|| format!("Push object {} (class {}) not found", entry.logical_name, entry.class_id))?;
            let value = obj
                .attributes()
                .into_iter()
                .find(|(id, _)| *id == entry.attribute_index)
                .map(|(_, v)| v)
                .ok_or_else(|| format!("Push object has no attribute {}", entry.attribute_index))?;
            values.push(value);
        }
        let body = if values.len() == 1 { values.remove(0) } else { CosemDataType::Array(values) };
        let notification =
            DataNotification { long_invoke_id_and_priority, date_time: Vec::new(), notification_body: body };
        let encoded = notification.encode().map_err(|e| format!("Failed to encode push body: {e:?}"))?;
        let destination = push_setup.send_destination_and_method();
        Ok(PushDeliveryRequest {
            destination: destination.destination.clone(),
            transport_service: destination.transport_service,
            client_sap: push_setup.push_client_sap(),
            body: encoded,
        })
    }

    /// Locates a registered object by class-id and logical name.
    fn find(&mut self, class_id: u16, instance: &ObisCode) -> Option<&mut Box<dyn InterfaceClass>> {
        self.objects.iter_mut().find(|o| o.class_id() == class_id && o.logical_name() == instance)
    }

    /// Checks whether a read is allowed for the given object and attribute.
    /// Returns true if no association is set (unrestricted access).
    fn check_read(&self, class_id: u16, instance: &ObisCode, attribute_id: i8) -> bool {
        // No association — unrestricted.
        self.association.as_ref().is_none_or(|assoc| assoc.can_read(class_id, instance, attribute_id))
    }

    /// Checks whether a write is allowed for the given object and attribute.
    /// Returns true if no association is set (unrestricted access).
    fn check_write(&self, class_id: u16, instance: &ObisCode, attribute_id: i8) -> bool {
        // No association — unrestricted.
        self.association.as_ref().is_none_or(|assoc| assoc.can_write(class_id, instance, attribute_id))
    }

    /// Checks whether a method invocation is allowed.
    /// Returns true if no association is set (unrestricted access).
    fn check_invoke(&self, class_id: u16, instance: &ObisCode, method_id: i8) -> bool {
        // No association — unrestricted.
        self.association.as_ref().is_none_or(|assoc| assoc.can_invoke(class_id, instance, method_id))
    }

    /// Reads one attribute, returning its value or a data-access-result code.
    /// Checks access rights from the current association's object_list.
    fn read_attribute(&mut self, d: &AttributeDescriptor) -> GetDataResult {
        let attr_id = d.attribute_id;
        // Check access rights first.
        if !self.check_read(d.class_id, &d.instance_id, attr_id) {
            #[cfg(feature = "tracing")]
            warn!(
                class_id = d.class_id,
                instance = %d.instance_id,
                attr_id,
                "GET denied by access rights"
            );
            return GetDataResult::AccessResult(data_access_result::READ_WRITE_DENIED);
        }
        self.find(d.class_id, &d.instance_id).map_or_else(
            || {
                #[cfg(feature = "tracing")]
                debug!(
                    class_id = d.class_id,
                    instance = %d.instance_id,
                    attr_id,
                    "GET: object undefined"
                );
                GetDataResult::AccessResult(data_access_result::OBJECT_UNDEFINED)
            },
            // Attribute ids are always <128 in practice (i8-valued on the wire).
            #[allow(clippy::cast_possible_wrap)]
            |obj| match obj.attributes().into_iter().find(|(id, _)| *id as i8 == attr_id) {
                Some((_, value)) => {
                    #[cfg(feature = "tracing")]
                    debug!(
                        class_id = d.class_id,
                        instance = %d.instance_id,
                        attr_id,
                        "GET: success"
                    );
                    GetDataResult::Data(value)
                }
                None => GetDataResult::AccessResult(data_access_result::OBJECT_UNAVAILABLE),
            },
        )
    }

    /// Writes one attribute, returning a data-access-result code.
    /// Checks access rights from the current association's object_list.
    fn write_attribute(&mut self, d: &AttributeDescriptor, value: crate::types::CosemDataType) -> u8 {
        // Check access rights first.
        if !self.check_write(d.class_id, &d.instance_id, d.attribute_id) {
            #[cfg(feature = "tracing")]
            warn!(
                class_id = d.class_id,
                instance = %d.instance_id,
                attr_id = d.attribute_id,
                "SET denied by access rights"
            );
            return data_access_result::READ_WRITE_DENIED;
        }
        self.find(d.class_id, &d.instance_id).map_or_else(
            || {
                #[cfg(feature = "tracing")]
                debug!(
                    class_id = d.class_id,
                    instance = %d.instance_id,
                    attr_id = d.attribute_id,
                    "SET: object undefined"
                );
                data_access_result::OBJECT_UNDEFINED
            },
            |obj| {
                // Attribute ids are always <128 in practice (i8-valued on the wire).
                #[allow(clippy::cast_sign_loss)]
                let attribute_id = d.attribute_id as u8;
                match obj.set_attribute(attribute_id, value) {
                    Ok(()) => {
                        #[cfg(feature = "tracing")]
                        debug!(
                            class_id = d.class_id,
                            instance = %d.instance_id,
                            attr_id = d.attribute_id,
                            "SET: success"
                        );
                        data_access_result::SUCCESS
                    }
                    Err(_) => data_access_result::READ_WRITE_DENIED,
                }
            },
        )
    }

    /// Invokes one method, returning the action-result and optional return data.
    /// Checks method access rights from the current association's object_list.
    fn invoke(
        &mut self,
        d: &MethodDescriptor,
        params: Option<crate::types::CosemDataType>,
    ) -> (u8, Option<GetDataResult>) {
        // Check method access rights first.
        if !self.check_invoke(d.class_id, &d.instance_id, d.method_id) {
            #[cfg(feature = "tracing")]
            warn!(
                class_id = d.class_id,
                instance = %d.instance_id,
                method_id = d.method_id,
                "ACTION denied by access rights"
            );
            return (data_access_result::READ_WRITE_DENIED, None);
        }
        // HLS reply_to_authentication and other Association LN methods must hit
        // the live association state, not a cloned registry object.
        let routed_to_assoc = d.class_id == 15
            && self.association.is_some()
            && (d.instance_id == ObisCode::new(0, 0, 40, 0, 0, 255)
                || self.association.as_ref().is_some_and(|assoc| assoc.logical_name() == &d.instance_id));
        if routed_to_assoc {
            // Method ids are always <128 in practice (i8-valued on the wire).
            #[allow(clippy::cast_sign_loss)]
            let method_id = d.method_id as u8;
            return match self.association.as_mut().expect("association routing").invoke_method(method_id, params) {
                Ok(crate::types::CosemDataType::Null) => (data_access_result::SUCCESS, None),
                Ok(value) => (data_access_result::SUCCESS, Some(GetDataResult::Data(value))),
                Err(_) => (data_access_result::OTHER_REASON, None),
            };
        }
        self.find(d.class_id, &d.instance_id).map_or_else(
            || {
                #[cfg(feature = "tracing")]
                debug!(
                    class_id = d.class_id,
                    instance = %d.instance_id,
                    method_id = d.method_id,
                    "ACTION: object undefined"
                );
                (data_access_result::OBJECT_UNDEFINED, None)
            },
            |obj| {
                // Method ids are always <128 in practice (i8-valued on the wire).
                #[allow(clippy::cast_sign_loss)]
                let method_id = d.method_id as u8;
                match obj.invoke_method(method_id, params) {
                    Ok(crate::types::CosemDataType::Null) => {
                        #[cfg(feature = "tracing")]
                        debug!(
                            class_id = d.class_id,
                            instance = %d.instance_id,
                            method_id = d.method_id,
                            "ACTION: success"
                        );
                        (data_access_result::SUCCESS, None)
                    }
                    Ok(value) => {
                        #[cfg(feature = "tracing")]
                        debug!(
                            class_id = d.class_id,
                            instance = %d.instance_id,
                            method_id = d.method_id,
                            "ACTION: success (with return value)"
                        );
                        (data_access_result::SUCCESS, Some(GetDataResult::Data(value)))
                    }
                    Err(_) => {
                        #[cfg(feature = "tracing")]
                        warn!(
                            class_id = d.class_id,
                            instance = %d.instance_id,
                            method_id = d.method_id,
                            "ACTION: method returned error"
                        );
                        (data_access_result::OTHER_REASON, None)
                    }
                }
            },
        )
    }

    /// Dispatches one request APDU to the addressed object and returns the
    /// encoded response APDU. Malformed or unsupported requests yield an
    /// EXCEPTION-RESPONSE. An AARQ is answered with an AARE via
    /// [`Self::handle_aarq`].
    pub fn dispatch(&mut self, request: &[u8]) -> Result<Vec<u8>, ServiceError> {
        match request.first() {
            Some(&acse::AARQ_TAG) => Ok(self.handle_aarq(request)),
            Some(&acse::RLRQ_TAG) => Ok(self.handle_rlrq(request)),
            Some(&tag) => {
                if !self.xdlms_services_allowed(request) {
                    return Ok(not_possible());
                }
                match tag {
                    tag::GET_REQUEST => self.dispatch_get(request),
                    tag::SET_REQUEST => Ok(self.dispatch_set(request)),
                    tag::ACTION_REQUEST => self.dispatch_action(request),
                    other => Ok(unsupported(other)),
                }
            }
            None => Err(ServiceError::Truncated),
        }
    }

    /// xDLMS GET/SET/ACTION are allowed only when associated. While HLS is
    /// pending, only `reply_to_HLS_authentication` (Association LN method 1)
    /// is accepted — everything else yields EXCEPTION-RESPONSE (C++
    /// `hls_pending` gate / Yellow Book APPL_OPEN HLS pass 3).
    fn xdlms_services_allowed(&self, request: &[u8]) -> bool {
        match self.association.as_ref().map(AssociationLn::association_status) {
            None | Some(association_status::ASSOCIATED) => true,
            Some(association_status::ASSOCIATION_PENDING) => is_hls_pass3_action(request),
            Some(_) => false,
        }
    }

    /// Validates an AARQ against the configured association and answers with an
    /// AARE carrying a structured ACSE diagnostic (IEC 62056-5-3 §7.3.5):
    ///
    /// * unsupported application context → `application-context-name-not-supported`;
    /// * ciphering / title-bound HLS without an 8-octet calling-AP-title →
    ///   `calling-AP-title-not-recognized`;
    /// * bad InitiateRequest parameters → `initiateError` ConfirmedServiceError
    ///   in the user-information (DLMS version / conformance / PDU size);
    /// * missing, unknown, or mismatching authentication →
    ///   `authentication-required` / `-mechanism-name-not-recognised` /
    ///   `authentication-failure`;
    /// * LLS: the calling-authentication-value is compared with the
    ///   association's secret;
    /// * HLS: the CtoS challenge is stored, a random StoC is generated and
    ///   returned, and the association is left `association-pending` until the
    ///   `reply_to_HLS_authentication` ACTION (pass 3/4) completes.
    ///
    /// Requires an association to be configured via [`Self::set_association`];
    /// without one, only mechanism 0 (lowest) AARQs are accepted.
    pub fn handle_aarq(&mut self, request: &[u8]) -> Vec<u8> {
        use crate::service::acse::{
            acse_diagnostic as diag, acse_provider_diagnostic as pdiag, application_context, AssociationRequest,
            PROTOCOL_VERSION_1,
        };

        // Fresh AARQ clears any prior AA (C++ resets associated/hls_pending).
        if let Some(assoc) = self.association.as_mut() {
            assoc.set_association_status(association_status::NON_ASSOCIATED);
        }

        let Ok(aarq) = AssociationRequest::decode(request) else {
            return reject_aare(application_context::LN, diag::NULL, false, None);
        };
        let echo_context = aarq.application_context;

        // Application context: LN with or without ciphering.
        if aarq.application_context != application_context::LN
            && aarq.application_context != application_context::LN_CIPHERING
        {
            return reject_aare(application_context::LN, diag::APPLICATION_CONTEXT_NAME_NOT_SUPPORTED, false, None);
        }

        // Protocol-version must be absent or {version1} (`02 84`).
        if let Some(pv) = aarq.protocol_version {
            if pv != PROTOCOL_VERSION_1 {
                return reject_aare(echo_context, pdiag::NO_COMMON_ACSE_VERSION, true, None);
            }
        }

        // Ciphering and title-bound HLS mechanisms require an 8-octet client
        // system title in the calling-AP-title.
        let mech_id = aarq.mechanism_name.unwrap_or(0);
        let title_bound = aarq.application_context == application_context::LN_CIPHERING || mech_id >= 5;
        if title_bound && aarq.calling_ap_title.as_ref().map(Vec::len) != Some(8) {
            return reject_aare(echo_context, diag::CALLING_AP_TITLE_NOT_RECOGNIZED, false, None);
        }

        // Validate the xDLMS InitiateRequest, when present and well-formed.
        let initiate = InitiateRequest::decode(&aarq.user_information).ok();
        if let Some(ireq) = &initiate {
            let err = if ireq.proposed_dlms_version != 0 && ireq.proposed_dlms_version < DLMS_VERSION {
                Some(initiate_error::DLMS_VERSION_TOO_LOW)
            } else if ireq.proposed_conformance == 0 {
                Some(initiate_error::INCOMPATIBLE_CONFORMANCE)
            } else if ireq.client_max_receive_pdu_size > 0 && ireq.client_max_receive_pdu_size < MIN_CLIENT_PDU {
                Some(initiate_error::PDU_SIZE_TOO_SHORT)
            } else {
                None
            };
            if let Some(value) = err {
                return reject_aare(echo_context, diag::NULL, false, Some(value));
            }
        }

        // Authentication, against the configured association (C++ `aarq_validate` order).
        let required = self.association.as_ref().map(|a| a.authentication_mechanism()).unwrap_or(AuthMechanism::None);
        if required != AuthMechanism::None {
            let has_mech = aarq.mechanism_name.is_some();
            let has_auth = aarq.calling_authentication_value.is_some();
            if !has_mech && !has_auth {
                return reject_aare(echo_context, diag::AUTHENTICATION_REQUIRED, false, None);
            }
            if let Some(bits) = aarq.sender_acse_requirements {
                if bits != 0x80 {
                    return reject_aare(echo_context, diag::AUTHENTICATION_FAILURE, false, None);
                }
            }
            if !has_mech {
                return reject_aare(echo_context, diag::AUTHENTICATION_FAILURE, false, None);
            }
            let Some(mech) = aarq.mechanism_name.and_then(AuthMechanism::from_id) else {
                return reject_aare(echo_context, diag::AUTHENTICATION_MECHANISM_NAME_NOT_RECOGNISED, false, None);
            };
            if !has_auth && mech != AuthMechanism::None {
                return reject_aare(echo_context, diag::AUTHENTICATION_FAILURE, false, None);
            }
            if !Self::aarq_mechanism_matches_assoc(mech, required) {
                return reject_aare(echo_context, diag::AUTHENTICATION_FAILURE, false, None);
            }
        }

        let mechanism = aarq.mechanism_name.and_then(AuthMechanism::from_id).unwrap_or(AuthMechanism::None);
        let user_information = Self::negotiate_initiate_response(initiate.as_ref(), mechanism);
        match mechanism {
            AuthMechanism::None => {
                if let Some(assoc) = self.association.as_mut() {
                    assoc.set_association_status(association_status::ASSOCIATED);
                }
                AssociationResponse {
                    protocol_version: Some(PROTOCOL_VERSION_1),
                    application_context: echo_context,
                    result: acse::result::ACCEPTED,
                    diagnostic: diag::NULL,
                    user_information,
                    ..AssociationResponse::default()
                }
                .encode()
            }
            AuthMechanism::Lls => {
                let secret_ok = match (&self.association, &aarq.calling_authentication_value) {
                    (Some(assoc), Some(pw)) => !assoc.secret().is_empty() && assoc.secret() == pw.as_slice(),
                    _ => false,
                };
                if !secret_ok {
                    return reject_aare(echo_context, diag::AUTHENTICATION_FAILURE, false, None);
                }
                if let Some(assoc) = self.association.as_mut() {
                    assoc.set_association_status(association_status::ASSOCIATED);
                }
                AssociationResponse {
                    protocol_version: Some(PROTOCOL_VERSION_1),
                    application_context: echo_context,
                    result: acse::result::ACCEPTED,
                    diagnostic: diag::NULL,
                    user_information,
                    ..AssociationResponse::default()
                }
                .encode()
            }
            _ => {
                // HLS pass 1/2: store CtoS, reply with a fresh StoC; the
                // handshake completes with the reply_to_HLS_authentication
                // ACTION (pass 3/4), served by the Association LN object.
                let Some(ctos) = aarq.calling_authentication_value else {
                    return reject_aare(echo_context, diag::AUTHENTICATION_FAILURE, false, None);
                };
                let Some(assoc) = self.association.as_mut() else {
                    return reject_aare(echo_context, diag::AUTHENTICATION_FAILURE, false, None);
                };
                assoc.set_ctos(ctos);
                if let Some(title) = aarq.calling_ap_title {
                    assoc.set_client_system_title(title);
                }
                assoc.set_hls_handshake_mechanism(mechanism);
                let stoc = assoc.generate_stoc(16);
                assoc.set_association_status(association_status::ASSOCIATION_PENDING);
                // Responding-AP-title: required with ciphering / title-bound HLS
                // (GMAC+); omit for manufacturer-specific HIGH security (mech 2) over plain LN (etalon).
                let responding_ap_title = if echo_context == application_context::LN_CIPHERING
                    || matches!(
                        mechanism,
                        AuthMechanism::HlsGmac
                            | AuthMechanism::HlsSha256
                            | AuthMechanism::HlsEcdsa
                            | AuthMechanism::HlsGostCmac
                            | AuthMechanism::HlsGostStreebog
                            | AuthMechanism::HlsGostSignature
                    ) {
                    assoc.responding_ap_title_for_hls()
                } else {
                    None
                };
                AssociationResponse {
                    protocol_version: Some(PROTOCOL_VERSION_1),
                    application_context: echo_context,
                    // Yellow Book: accepted + authentication-required while HLS pending.
                    result: acse::result::ACCEPTED,
                    diagnostic: diag::AUTHENTICATION_REQUIRED,
                    responding_ap_title,
                    mechanism_name: Some(mechanism.id()),
                    responding_authentication_value: Some(stoc),
                    user_information,
                    ..AssociationResponse::default()
                }
                .encode()
            }
        }
    }

    /// Manufacturer-specific HIGH-level authentication advertises mechanism 2
    /// (`HlsManufacturer`); СПОДЭС Configurator associations are configured as
    /// HLS-GMAC (5). Accept that pairing for AARQ, then finish the handshake
    /// with GMAC.
    fn aarq_mechanism_matches_assoc(requested: AuthMechanism, required: AuthMechanism) -> bool {
        requested == required || (requested == AuthMechanism::HlsManufacturer && required == AuthMechanism::HlsGmac)
    }

    /// Answers an RLRQ with RLRE (IEC 62056-5-3 §7.3.6) and clears any
    /// configured association state back to non-associated. A malformed RLRQ
    /// is still answered with a normal-release RLRE, since releasing is
    /// best-effort.
    pub fn handle_rlrq(&mut self, request: &[u8]) -> Vec<u8> {
        use crate::service::acse::ReleaseRequest;
        if let Some(assoc) = self.association.as_mut() {
            assoc.set_association_status(association_status::NON_ASSOCIATED);
        }
        ReleaseRequest::decode_rlrq(request).map_or_else(
            |_| ReleaseRequest { reason: Some(acse::release_reason::NORMAL), user_information: None }.encode_rlre(),
            |release| release.encode_rlre(),
        )
    }

    /// Role conformance advertised in AARE, matching C++ `aare` fill order.
    fn role_conformance(mechanism: AuthMechanism) -> u32 {
        if mechanism == AuthMechanism::None {
            CONFORMANCE_PUBLIC
        } else if mechanism.is_hls() {
            CONFORMANCE_CONFIGURATOR
        } else {
            CONFORMANCE_READER
        }
    }

    /// Builds the negotiated InitiateResponse: role conformance is ANDed with
    /// the client's proposal and the PDU size capped by the client's maximum.
    fn negotiate_initiate_response(ireq: Option<&InitiateRequest>, mechanism: AuthMechanism) -> Vec<u8> {
        let mut conformance = Self::role_conformance(mechanism);
        let mut pdu = SERVER_MAX_PDU;
        if let Some(ireq) = ireq {
            if ireq.proposed_conformance != 0 {
                conformance &= ireq.proposed_conformance;
            }
            if ireq.client_max_receive_pdu_size > 0 && ireq.client_max_receive_pdu_size < pdu {
                pdu = ireq.client_max_receive_pdu_size;
            }
        }
        InitiateResponse {
            negotiated_quality_of_service: None,
            negotiated_dlms_version: DLMS_VERSION,
            negotiated_conformance: conformance,
            server_max_receive_pdu_size: pdu,
            vaa_name: 0x0007,
        }
        .encode()
    }

    fn dispatch_get(&mut self, request: &[u8]) -> Result<Vec<u8>, ServiceError> {
        // A malformed GET from an associated client is answered with a
        // data-access-result instead of dropping the session (the invoke-id is
        // salvaged from the raw APDU when present).
        let Ok(decoded) = GetRequest::decode(request) else {
            let invoke_id_and_priority = request.get(2).copied().unwrap_or(0);
            return GetResponse::Normal {
                invoke_id_and_priority,
                result: GetDataResult::AccessResult(data_access_result::OTHER_REASON),
            }
            .encode();
        };
        match decoded {
            GetRequest::Normal { invoke_id_and_priority, attribute, access_selection } => {
                let read = self.read_attribute(&attribute);
                match apply_selective_access(&attribute, read, access_selection.as_ref()) {
                    // A too-large value is segmented into datablocks.
                    GetDataResult::Data(value) => {
                        let mut raw = Vec::new();
                        value.serialize_ber(&mut raw)?;
                        if raw.len() > self.max_pdu {
                            return Ok(self.start_get_blocks(invoke_id_and_priority, raw));
                        }
                        GetResponse::Normal { invoke_id_and_priority, result: GetDataResult::Data(value) }.encode()
                    }
                    result => GetResponse::Normal { invoke_id_and_priority, result }.encode(),
                }
            }
            GetRequest::WithList { invoke_id_and_priority, attributes } => {
                let results = attributes
                    .iter()
                    .map(|(a, sel)| {
                        let read = self.read_attribute(a);
                        apply_selective_access(a, read, sel.as_ref())
                    })
                    .collect();
                GetResponse::WithList { invoke_id_and_priority, results }.encode()
            }
            // Deliver the next block of a segmented result.
            GetRequest::Next { invoke_id_and_priority, block_number } => {
                Ok(self.next_get_block(invoke_id_and_priority, block_number))
            }
        }
    }

    /// Stores a segmented GET result and returns the first datablock response.
    fn start_get_blocks(&mut self, invoke_id_and_priority: u8, data: Vec<u8>) -> Vec<u8> {
        self.pending_get = Some(PendingGet { data, next_block: 1 });
        self.next_get_block(invoke_id_and_priority, 0)
    }

    /// Returns the datablock following `acked_block` (0 for the first).
    fn next_get_block(&mut self, invoke_id_and_priority: u8, acked_block: u32) -> Vec<u8> {
        let max_pdu = self.max_pdu;
        let Some(pending) = self.pending_get.as_mut() else {
            return ExceptionResponse {
                state_error: state_error::SERVICE_NOT_ALLOWED,
                service_error: service_error::OPERATION_NOT_POSSIBLE,
            }
            .encode();
        };
        let block_number = pending.next_block;
        // The client acknowledges the previous block by its number in GET-NEXT.
        if acked_block != 0 && acked_block + 1 != block_number {
            return ExceptionResponse {
                state_error: state_error::SERVICE_NOT_ALLOWED,
                service_error: service_error::OTHER_REASON,
            }
            .encode();
        }
        let start = (block_number as usize - 1) * max_pdu;
        let end = (start + max_pdu).min(pending.data.len());
        let chunk = pending.data[start..end].to_vec();
        let last_block = end >= pending.data.len();
        pending.next_block += 1;
        let response =
            GetResponse::WithDataBlock { invoke_id_and_priority, last_block, block_number, raw_data: Ok(chunk) };
        if last_block {
            self.pending_get = None;
        }
        response.encode().unwrap_or_default()
    }

    fn dispatch_set(&mut self, request: &[u8]) -> Vec<u8> {
        // A malformed SET is answered with a data-access-result instead of
        // dropping the session.
        let Ok(decoded) = SetRequest::decode(request) else {
            let invoke_id_and_priority = request.get(2).copied().unwrap_or(0);
            return SetResponse::Normal { invoke_id_and_priority, result: data_access_result::OTHER_REASON }.encode();
        };
        match decoded {
            SetRequest::Normal { invoke_id_and_priority, attribute, value, .. } => {
                let result = self.write_attribute(&attribute, value);
                SetResponse::Normal { invoke_id_and_priority, result }.encode()
            }
            SetRequest::WithList { invoke_id_and_priority, attributes, values } => {
                let results = attributes.iter().zip(values).map(|((a, _), v)| self.write_attribute(a, v)).collect();
                SetResponse::WithList { invoke_id_and_priority, results }.encode()
            }
            // Begin reassembling a block-transferred value.
            SetRequest::WithFirstDatablock { invoke_id_and_priority, attribute, datablock, .. } => {
                self.pending_set = Some(PendingSet { attribute, buffer: Vec::new() });
                self.accumulate_set_block(invoke_id_and_priority, &datablock)
            }
            SetRequest::WithDatablock { invoke_id_and_priority, datablock } => {
                if self.pending_set.is_none() {
                    return not_possible();
                }
                self.accumulate_set_block(invoke_id_and_priority, &datablock)
            }
        }
    }

    /// Appends one SET datablock; on the last block, decodes and writes the value.
    fn accumulate_set_block(&mut self, invoke_id_and_priority: u8, datablock: &DataBlockSa) -> Vec<u8> {
        let Some(pending) = self.pending_set.as_mut() else {
            return not_possible();
        };
        pending.buffer.extend_from_slice(&datablock.raw_data);
        if !datablock.last_block {
            return SetResponse::Datablock { invoke_id_and_priority, block_number: datablock.block_number }.encode();
        }
        // Final block: decode the accumulated A-XDR value and write it.
        let PendingSet { attribute, buffer } = self.pending_set.take().unwrap();
        let result = match CosemDataType::deserialize_ber(&buffer) {
            Ok((value, _)) => self.write_attribute(&attribute, value),
            Err(_) => data_access_result::TYPE_UNMATCHED,
        };
        SetResponse::LastDatablock { invoke_id_and_priority, result, block_number: datablock.block_number }.encode()
    }

    fn dispatch_action(&mut self, request: &[u8]) -> Result<Vec<u8>, ServiceError> {
        match ActionRequest::decode(request)? {
            ActionRequest::Normal { invoke_id_and_priority, method, parameters } => {
                let (result, return_parameters) = self.invoke(&method, parameters);
                ActionResponse::Normal { invoke_id_and_priority, result, return_parameters }.encode()
            }
            ActionRequest::WithList { invoke_id_and_priority, methods, parameters } => {
                let mut params = parameters.into_iter();
                let results = methods
                    .iter()
                    .map(|m| self.invoke(m, params.next().filter(|p| *p != crate::types::CosemDataType::Null)))
                    .collect();
                ActionResponse::WithList { invoke_id_and_priority, results }.encode()
            }
            ActionRequest::NextPblock { .. }
            | ActionRequest::WithFirstPblock { .. }
            | ActionRequest::WithPblock { .. } => Ok(not_possible()),
        }
    }
}

/// Applies GET selective access to a read result (IEC 62056-5-3 §7.4.1.6 /
/// IEC 62056-6-2 §4.3.6.2). Only the ProfileGeneric buffer (class 7,
/// attribute 2) is filtered:
///
/// * selector 2 (`entry_descriptor`) — keeps the 1-based entry window
///   `[from_entry, to_entry]` of the buffer array; `to_entry` = 0 keeps all
///   remaining entries.
/// * selector 1 (`range_descriptor`) — value-range filtering needs the sort
///   column; like the reference implementation the buffer is returned
///   unfiltered.
///
/// Any other selector on the buffer, or a malformed descriptor, yields
/// `other-reason`; selective access on other attributes is ignored.
fn apply_selective_access(
    attribute: &AttributeDescriptor,
    read: GetDataResult,
    selection: Option<&crate::service::get::AccessSelection>,
) -> GetDataResult {
    let Some(sel) = selection else { return read };
    if attribute.class_id != 7 || attribute.attribute_id != 2 {
        return read;
    }
    let GetDataResult::Data(CosemDataType::Array(rows)) = read else { return read };
    match sel.selector {
        1 => GetDataResult::Data(CosemDataType::Array(rows)),
        2 => {
            let fields = match &sel.parameters {
                CosemDataType::Structure(fields) if fields.len() >= 2 => fields,
                _ => return GetDataResult::AccessResult(data_access_result::OTHER_REASON),
            };
            let (Some(from), Some(to)) = (selective_index(&fields[0]), selective_index(&fields[1])) else {
                return GetDataResult::AccessResult(data_access_result::OTHER_REASON);
            };
            if from == 0 {
                return GetDataResult::AccessResult(data_access_result::OTHER_REASON);
            }
            let start = (from - 1) as usize;
            let end = if to == 0 { rows.len() } else { (to as usize).min(rows.len()) };
            let slice = if start < end { rows[start..end].to_vec() } else { Vec::new() };
            GetDataResult::Data(CosemDataType::Array(slice))
        }
        _ => GetDataResult::AccessResult(data_access_result::OTHER_REASON),
    }
}

/// Reads a non-negative COSEM integer used as a selective-access entry index.
fn selective_index(value: &CosemDataType) -> Option<u32> {
    match value {
        CosemDataType::DoubleLongUnsigned(v) => Some(*v),
        CosemDataType::LongUnsigned(v) => Some(u32::from(*v)),
        CosemDataType::Unsigned(v) => Some(u32::from(*v)),
        _ => None,
    }
}

/// Builds a permanently-rejecting AARE with the given diagnostic;
/// `initiate_err` adds an `initiateError` ConfirmedServiceError to the
/// user-information. `diagnostic_is_provider` selects the acse-service-provider
/// CHOICE (e.g. no-common-acse-version).
fn reject_aare(
    application_context: u8,
    diagnostic: u8,
    diagnostic_is_provider: bool,
    initiate_err: Option<u8>,
) -> Vec<u8> {
    use crate::service::acse::PROTOCOL_VERSION_1;
    let user_information = initiate_err.map_or_else(Vec::new, |value| {
        ConfirmedServiceError {
            service: crate::service::error::service::INITIATE_ERROR,
            category: crate::service::error::category::INITIATE,
            value,
        }
        .encode()
    });
    AssociationResponse {
        protocol_version: Some(PROTOCOL_VERSION_1),
        application_context,
        result: acse::result::REJECTED_PERMANENT,
        diagnostic,
        diagnostic_is_provider,
        user_information,
        ..AssociationResponse::default()
    }
    .encode()
}

/// True when the APDU is Association LN `reply_to_HLS_authentication` (method 1).
fn is_hls_pass3_action(request: &[u8]) -> bool {
    match ActionRequest::decode(request) {
        Ok(ActionRequest::Normal { method, .. }) => method.class_id == 15 && method.method_id == 1,
        _ => false,
    }
}

/// EXCEPTION-RESPONSE for an unsupported service tag.
fn unsupported(_tag: u8) -> Vec<u8> {
    ExceptionResponse { state_error: state_error::SERVICE_UNKNOWN, service_error: service_error::SERVICE_NOT_SUPPORTED }
        .encode()
}

/// EXCEPTION-RESPONSE for a well-formed but unhandled request (e.g. block transfer).
fn not_possible() -> Vec<u8> {
    ExceptionResponse {
        state_error: state_error::SERVICE_NOT_ALLOWED,
        service_error: service_error::OPERATION_NOT_POSSIBLE,
    }
    .encode()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::classes::data::Data;
    use crate::types::CosemDataType;

    fn dispatcher_with_data() -> RequestDispatcher {
        let mut d = RequestDispatcher::new();
        let data = Data::new(ObisCode::new(0, 0, 0x80, 0, 0, 0xFF), CosemDataType::LongUnsigned(0x1234));
        d.add(Box::new(data));
        d
    }

    /// An AssociationLn requiring the given mechanism, with an LLS secret.
    fn association(mechanism: AuthMechanism) -> AssociationLn {
        use crate::classes::association_ln::{AssociationLnConfig, AssociationLnVersion};
        use crate::types::attrs::{AssociatedPartnersId, ContextName, XDLMSContextInfo};
        AssociationLn::new(AssociationLnConfig {
            logical_name: ObisCode::new(0, 0, 40, 0, 0, 255),
            version: AssociationLnVersion::Version1,
            object_list: vec![],
            associated_partners_id: AssociatedPartnersId { client_sap: 0, server_sap: 1 },
            application_context_name: ContextName::OctetString(vec![
                0x09, 0x07, 0x60, 0x85, 0x74, 0x05, 0x08, 0x01, 0x01,
            ]),
            xdlms_context_info: XDLMSContextInfo {
                conformance: vec![0x00; 18],
                max_receive_pdu_size: 1024,
                max_send_pdu_size: 1024,
                dlms_version_number: 6,
                quality_of_service: -1,
                cyphering_info: vec![],
            },
            authentication_mechanism: mechanism,
            secret: b"password".to_vec(),
            association_status: 0,
            security_setup_reference: ObisCode::new(0, 0, 43, 0, 0, 255),
            user_list: vec![],
            current_user: None,
        })
    }

    fn aarq(context: u8, mechanism: Option<u8>, auth: Option<&[u8]>) -> Vec<u8> {
        crate::service::acse::AssociationRequest {
            application_context: context,
            calling_ap_title: None,
            sender_acse_requirements: mechanism.map(|_| 0x80),
            mechanism_name: mechanism,
            calling_authentication_value: auth.map(<[u8]>::to_vec),
            user_information: InitiateRequest {
                dedicated_key: None,
                response_allowed: true,
                proposed_quality_of_service: None,
                proposed_dlms_version: 6,
                proposed_conformance: 0x00_18_5F,
                client_max_receive_pdu_size: 0x0400,
            }
            .encode(),
            ..Default::default()
        }
        .encode()
    }

    #[test]
    fn aarq_lowest_is_accepted_without_association() {
        let mut d = dispatcher_with_data();
        let resp = d.dispatch(&aarq(1, None, None)).unwrap();
        let aare = AssociationResponse::decode(&resp).unwrap();
        assert_eq!(aare.result, acse::result::ACCEPTED);
        // Negotiated InitiateResponse: version 6, PDU capped by the client.
        let iresp = InitiateResponse::decode(&aare.user_information).unwrap();
        assert_eq!(iresp.negotiated_dlms_version, 6);
        assert_eq!(iresp.server_max_receive_pdu_size, 0x0400);
        // Public AA: get + block-get only (0x001010), AND with client proposal.
        assert_eq!(iresp.negotiated_conformance, 0x00_10_10);
    }

    #[test]
    fn aarq_lls_reader_conformance() {
        let mut d = dispatcher_with_data();
        d.set_association(association(AuthMechanism::Lls));
        let resp = d.handle_aarq(&aarq(1, Some(1), Some(b"password")));
        let aare = AssociationResponse::decode(&resp).unwrap();
        assert_eq!(aare.result, acse::result::ACCEPTED);
        let iresp = InitiateResponse::decode(&aare.user_information).unwrap();
        assert_eq!(iresp.negotiated_conformance, 0x00_10_15);
    }

    #[test]
    fn aarq_unsupported_context_is_rejected() {
        let mut d = dispatcher_with_data();
        // Short-name referencing (context 2) is not supported.
        let resp = d.handle_aarq(&aarq(2, None, None));
        let aare = AssociationResponse::decode(&resp).unwrap();
        assert_eq!(aare.result, acse::result::REJECTED_PERMANENT);
        assert_eq!(aare.diagnostic, acse::acse_diagnostic::APPLICATION_CONTEXT_NAME_NOT_SUPPORTED);
    }

    #[test]
    fn aarq_without_auth_when_required_is_rejected() {
        let mut d = dispatcher_with_data();
        d.set_association(association(AuthMechanism::Lls));
        let resp = d.handle_aarq(&aarq(1, None, None));
        let aare = AssociationResponse::decode(&resp).unwrap();
        assert_eq!(aare.result, acse::result::REJECTED_PERMANENT);
        assert_eq!(aare.diagnostic, acse::acse_diagnostic::AUTHENTICATION_REQUIRED);
    }

    #[test]
    fn aarq_lls_secret_is_checked() {
        let mut d = dispatcher_with_data();
        d.set_association(association(AuthMechanism::Lls));
        // Wrong password.
        let aare = AssociationResponse::decode(&d.handle_aarq(&aarq(1, Some(1), Some(b"wrong")))).unwrap();
        assert_eq!(aare.result, acse::result::REJECTED_PERMANENT);
        assert_eq!(aare.diagnostic, acse::acse_diagnostic::AUTHENTICATION_FAILURE);
        // Right password.
        let aare = AssociationResponse::decode(&d.handle_aarq(&aarq(1, Some(1), Some(b"password")))).unwrap();
        assert_eq!(aare.result, acse::result::ACCEPTED);
    }

    #[test]
    fn aarq_mechanism_mismatch_is_rejected() {
        let mut d = dispatcher_with_data();
        d.set_association(association(AuthMechanism::HlsSha256));
        let resp = d.handle_aarq(&aarq(1, Some(1), Some(b"password")));
        let aare = AssociationResponse::decode(&resp).unwrap();
        assert_eq!(aare.result, acse::result::REJECTED_PERMANENT);
        assert_eq!(aare.diagnostic, acse::acse_diagnostic::AUTHENTICATION_FAILURE);
    }

    #[test]
    fn aarq_hls_pass1_returns_stoc_and_leaves_pending() {
        let mut d = dispatcher_with_data();
        d.set_association(association(AuthMechanism::HlsSha256));
        // Title-bound HLS without a calling-AP-title is rejected.
        let aare = AssociationResponse::decode(&d.handle_aarq(&aarq(1, Some(6), Some(b"CTOS0123CTOS0123")))).unwrap();
        assert_eq!(aare.diagnostic, acse::acse_diagnostic::CALLING_AP_TITLE_NOT_RECOGNIZED);

        // With an 8-octet title: accepted, StoC returned, pass 3/4 pending.
        let mut req =
            crate::service::acse::AssociationRequest::decode(&aarq(1, Some(6), Some(b"CTOS0123CTOS0123"))).unwrap();
        req.calling_ap_title = Some(b"CLIENT01".to_vec());
        let aare = AssociationResponse::decode(&d.handle_aarq(&req.encode())).unwrap();
        assert_eq!(aare.result, acse::result::ACCEPTED);
        assert_eq!(aare.diagnostic, acse::acse_diagnostic::AUTHENTICATION_REQUIRED);
        let stoc = aare.responding_authentication_value.expect("StoC missing");
        assert_eq!(stoc.len(), 16);
    }

    #[test]
    fn aarq_bad_protocol_version_is_provider_reject() {
        let mut d = dispatcher_with_data();
        let mut req = crate::service::acse::AssociationRequest::decode(&aarq(1, None, None)).unwrap();
        req.protocol_version = Some([0x02, 0x44]);
        let aare = AssociationResponse::decode(&d.handle_aarq(&req.encode())).unwrap();
        assert_eq!(aare.result, acse::result::REJECTED_PERMANENT);
        assert!(aare.diagnostic_is_provider);
        assert_eq!(aare.diagnostic, acse::acse_provider_diagnostic::NO_COMMON_ACSE_VERSION);
    }

    #[test]
    fn aarq_wrong_acse_requirements_is_auth_failure() {
        let mut d = dispatcher_with_data();
        d.set_association(association(AuthMechanism::Lls));
        let mut req = crate::service::acse::AssociationRequest::decode(&aarq(1, Some(1), Some(b"password"))).unwrap();
        req.sender_acse_requirements = Some(0x00);
        let aare = AssociationResponse::decode(&d.handle_aarq(&req.encode())).unwrap();
        assert_eq!(aare.result, acse::result::REJECTED_PERMANENT);
        assert_eq!(aare.diagnostic, acse::acse_diagnostic::AUTHENTICATION_FAILURE);
    }

    #[test]
    fn aarq_missing_mechanism_with_auth_is_auth_failure() {
        let mut d = dispatcher_with_data();
        d.set_association(association(AuthMechanism::Lls));
        let mut req = crate::service::acse::AssociationRequest::decode(&aarq(1, None, None)).unwrap();
        req.sender_acse_requirements = Some(0x80);
        req.calling_authentication_value = Some(b"password".to_vec());
        let aare = AssociationResponse::decode(&d.handle_aarq(&req.encode())).unwrap();
        assert_eq!(aare.result, acse::result::REJECTED_PERMANENT);
        assert_eq!(aare.diagnostic, acse::acse_diagnostic::AUTHENTICATION_FAILURE);
    }

    #[test]
    fn get_while_hls_pending_is_service_not_allowed() {
        let mut d = dispatcher_with_data();
        d.set_association(association(AuthMechanism::HlsSha1));
        let req =
            crate::service::acse::AssociationRequest::decode(&aarq(1, Some(4), Some(b"CTOS0123CTOS0123"))).unwrap();
        // HlsSha1 (4) is not title-bound (>=5); no calling-AP-title needed.
        let aare = AssociationResponse::decode(&d.handle_aarq(&req.encode())).unwrap();
        assert_eq!(aare.result, acse::result::ACCEPTED);
        assert_eq!(d.association().unwrap().association_status(), association_status::ASSOCIATION_PENDING);

        let get = GetRequest::Normal {
            invoke_id_and_priority: 0xC0,
            attribute: AttributeDescriptor {
                class_id: 15,
                instance_id: ObisCode::new(0, 0, 40, 0, 0, 255),
                attribute_id: 1,
            },
            access_selection: None,
        }
        .encode()
        .unwrap();
        let resp = d.dispatch(&get).unwrap();
        assert_eq!(resp[0], 0xD8); // EXCEPTION-RESPONSE
        let ex = ExceptionResponse::decode(&resp).unwrap();
        assert_eq!(ex.state_error, state_error::SERVICE_NOT_ALLOWED);
        assert_eq!(ex.service_error, service_error::OPERATION_NOT_POSSIBLE);

        // After HLS completes (status → associated), GET is allowed again.
        d.association_mut().unwrap().set_association_status(association_status::ASSOCIATED);
        let resp = d.dispatch(&get).unwrap();
        assert_ne!(resp[0], 0xD8, "GET after HLS must not be Exception");
    }

    #[test]
    fn aarq_bad_initiate_yields_confirmed_service_error() {
        let mut d = dispatcher_with_data();
        let mut req = crate::service::acse::AssociationRequest::decode(&aarq(1, None, None)).unwrap();
        req.user_information = InitiateRequest {
            dedicated_key: None,
            response_allowed: true,
            proposed_quality_of_service: None,
            proposed_dlms_version: 5, // too low
            proposed_conformance: 0x00_18_5F,
            client_max_receive_pdu_size: 0x0400,
        }
        .encode();
        let aare = AssociationResponse::decode(&d.handle_aarq(&req.encode())).unwrap();
        assert_eq!(aare.result, acse::result::REJECTED_PERMANENT);
        let cse = ConfirmedServiceError::decode(&aare.user_information).unwrap();
        assert_eq!(cse.value, initiate_error::DLMS_VERSION_TOO_LOW);
    }

    /// A registered object whose attribute 2 is writable, for SET reassembly.
    #[derive(Clone)]
    struct Writable {
        obis: ObisCode,
        value: CosemDataType,
    }

    impl crate::interface::InterfaceClass for Writable {
        fn class_id(&self) -> u16 {
            1
        }
        fn version(&self) -> u8 {
            0
        }
        fn logical_name(&self) -> &ObisCode {
            &self.obis
        }
        fn attributes(&self) -> Vec<(u8, CosemDataType)> {
            vec![(1, CosemDataType::OctetString(self.obis.to_bytes())), (2, self.value.clone())]
        }
        fn methods(&self) -> Vec<(u8, String)> {
            vec![]
        }
        fn serialize_ber(&self, _: &mut Vec<u8>) -> Result<(), crate::types::BerError> {
            Ok(())
        }
        fn deserialize_ber(&mut self, _: &[u8]) -> Result<(), crate::types::BerError> {
            Ok(())
        }
        fn set_attribute(&mut self, attribute_id: u8, value: CosemDataType) -> Result<(), String> {
            if attribute_id == 2 {
                self.value = value;
                Ok(())
            } else {
                Err("read only".to_string())
            }
        }
        fn invoke_method(&mut self, _: u8, _: Option<CosemDataType>) -> Result<CosemDataType, String> {
            Err("no methods".to_string())
        }
        fn as_any(&self) -> &dyn std::any::Any {
            self
        }
    }

    #[test]
    fn large_get_is_delivered_in_datablocks() {
        // A 300-octet octet-string exceeds the small block size and is segmented.
        let obis = ObisCode::new(0, 0, 0x80, 0, 0, 0xFF);
        let big = CosemDataType::OctetString(vec![0xAB; 300]);
        let mut d = RequestDispatcher::new();
        d.set_max_pdu(128);
        d.add(Box::new(Data::new(obis.clone(), big.clone())));

        let req = GetRequest::Normal {
            invoke_id_and_priority: 0xC1,
            attribute: AttributeDescriptor::new(1, obis, 2),
            access_selection: None,
        };
        let mut reassembled = Vec::new();
        let mut resp = GetResponse::decode(&d.dispatch(&req.encode().unwrap()).unwrap()).unwrap();
        let mut block = 1u32;
        loop {
            match resp {
                GetResponse::WithDataBlock { last_block, block_number, raw_data: Ok(chunk), .. } => {
                    assert_eq!(block_number, block);
                    reassembled.extend_from_slice(&chunk);
                    if last_block {
                        break;
                    }
                    let next = GetRequest::Next { invoke_id_and_priority: 0xC1, block_number };
                    resp = GetResponse::decode(&d.dispatch(&next.encode().unwrap()).unwrap()).unwrap();
                    block += 1;
                }
                other => panic!("expected datablock, got {other:?}"),
            }
        }
        // The reassembled blocks decode back to the original value.
        let (value, _) = CosemDataType::deserialize_ber(&reassembled).unwrap();
        assert_eq!(value, big);
    }

    #[test]
    fn set_reassembles_datablocks_and_writes() {
        let obis = ObisCode::new(1, 0, 1, 8, 0, 0xFF);
        let mut d = RequestDispatcher::new();
        d.add(Box::new(Writable { obis: obis.clone(), value: CosemDataType::LongUnsigned(0) }));

        // The value to write, split into two datablocks.
        let mut value_bytes = Vec::new();
        CosemDataType::OctetString(vec![0x5A; 40]).serialize_ber(&mut value_bytes).unwrap();
        let (first, second) = value_bytes.split_at(20);

        let req1 = SetRequest::WithFirstDatablock {
            invoke_id_and_priority: 0xC1,
            attribute: AttributeDescriptor::new(1, obis.clone(), 2),
            access_selection: None,
            datablock: DataBlockSa { last_block: false, block_number: 1, raw_data: first.to_vec() },
        };
        let r1 = SetResponse::decode(&d.dispatch(&req1.encode().unwrap()).unwrap()).unwrap();
        assert!(matches!(r1, SetResponse::Datablock { block_number: 1, .. }));

        let req2 = SetRequest::WithDatablock {
            invoke_id_and_priority: 0xC1,
            datablock: DataBlockSa { last_block: true, block_number: 2, raw_data: second.to_vec() },
        };
        let r2 = SetResponse::decode(&d.dispatch(&req2.encode().unwrap()).unwrap()).unwrap();
        assert_eq!(
            r2,
            SetResponse::LastDatablock {
                invoke_id_and_priority: 0xC1,
                result: data_access_result::SUCCESS,
                block_number: 2
            }
        );

        // The reassembled value was written to the object.
        let got = d
            .dispatch(
                &GetRequest::Normal {
                    invoke_id_and_priority: 0xC1,
                    attribute: AttributeDescriptor::new(1, obis, 2),
                    access_selection: None,
                }
                .encode()
                .unwrap(),
            )
            .unwrap();
        match GetResponse::decode(&got).unwrap() {
            GetResponse::Normal { result: GetDataResult::Data(v), .. } => {
                assert_eq!(v, CosemDataType::OctetString(vec![0x5A; 40]));
            }
            other => panic!("unexpected {other:?}"),
        }
    }

    #[test]
    fn malformed_get_returns_other_reason() {
        let mut d = dispatcher_with_data();
        // GET-REQUEST with an unknown choice (0x77) and a salvageable invoke-id.
        let resp = d.dispatch(&[tag::GET_REQUEST, 0x77, 0xC1]).unwrap();
        let decoded = GetResponse::decode(&resp).unwrap();
        assert_eq!(
            decoded,
            GetResponse::Normal {
                invoke_id_and_priority: 0xC1,
                result: GetDataResult::AccessResult(data_access_result::OTHER_REASON),
            }
        );
    }

    #[test]
    fn malformed_set_returns_other_reason() {
        let mut d = dispatcher_with_data();
        let resp = d.dispatch(&[tag::SET_REQUEST, 0x77, 0xC1]).unwrap();
        let decoded = SetResponse::decode(&resp).unwrap();
        assert_eq!(
            decoded,
            SetResponse::Normal { invoke_id_and_priority: 0xC1, result: data_access_result::OTHER_REASON }
        );
    }

    #[test]
    fn selective_access_by_entry_filters_profile_buffer() {
        use crate::classes::profile_generic::{ProfileGeneric, ProfileGenericConfig};
        use crate::service::get::AccessSelection;

        let obis = ObisCode::new(1, 0, 99, 1, 0, 0xFF);
        let buffer: Vec<CosemDataType> =
            (1u32..=5).map(|i| CosemDataType::Structure(vec![CosemDataType::DoubleLongUnsigned(i)])).collect();
        let profile = ProfileGeneric::new(ProfileGenericConfig {
            logical_name: obis.clone(),
            version: 1,
            buffer,
            capture_objects: vec![],
            capture_period: 0,
            sort_method: crate::types::attrs::SortMethod::Fifo,
            sort_object: None,
            entries_in_use: 5,
            profile_entries: 10,
        });
        let mut d = RequestDispatcher::new();
        d.add(Box::new(profile));

        // entry_descriptor: entries 2..4 (1-based, inclusive).
        let req = GetRequest::Normal {
            invoke_id_and_priority: 0xC1,
            attribute: AttributeDescriptor::new(7, obis, 2),
            access_selection: Some(AccessSelection {
                selector: 2,
                parameters: CosemDataType::Structure(vec![
                    CosemDataType::DoubleLongUnsigned(2),
                    CosemDataType::DoubleLongUnsigned(4),
                    CosemDataType::LongUnsigned(1),
                    CosemDataType::LongUnsigned(0),
                ]),
            }),
        };
        let resp = GetResponse::decode(&d.dispatch(&req.encode().unwrap()).unwrap()).unwrap();
        match resp {
            GetResponse::Normal { result: GetDataResult::Data(CosemDataType::Array(rows)), .. } => {
                assert_eq!(rows.len(), 3);
                assert_eq!(rows[0], CosemDataType::Structure(vec![CosemDataType::DoubleLongUnsigned(2)]));
                assert_eq!(rows[2], CosemDataType::Structure(vec![CosemDataType::DoubleLongUnsigned(4)]));
            }
            other => panic!("unexpected {other:?}"),
        }
    }

    #[test]
    fn get_reads_registered_attribute() {
        let mut d = dispatcher_with_data();
        let req = GetRequest::Normal {
            invoke_id_and_priority: 0xC1,
            attribute: AttributeDescriptor::new(1, ObisCode::new(0, 0, 0x80, 0, 0, 0xFF), 2),
            access_selection: None,
        };
        let resp = GetResponse::decode(&d.dispatch(&req.encode().unwrap()).unwrap()).unwrap();
        assert_eq!(
            resp,
            GetResponse::Normal {
                invoke_id_and_priority: 0xC1,
                result: GetDataResult::Data(CosemDataType::LongUnsigned(0x1234)),
            }
        );
    }

    #[test]
    fn get_unknown_object_returns_object_undefined() {
        let mut d = dispatcher_with_data();
        let req = GetRequest::Normal {
            invoke_id_and_priority: 0xC1,
            attribute: AttributeDescriptor::new(1, ObisCode::new(9, 9, 9, 9, 9, 9), 2),
            access_selection: None,
        };
        let resp = GetResponse::decode(&d.dispatch(&req.encode().unwrap()).unwrap()).unwrap();
        assert_eq!(
            resp,
            GetResponse::Normal {
                invoke_id_and_priority: 0xC1,
                result: GetDataResult::AccessResult(data_access_result::OBJECT_UNDEFINED),
            }
        );
    }

    #[test]
    fn set_on_data_value_attribute_succeeds() {
        let mut d = dispatcher_with_data();
        let req = SetRequest::Normal {
            invoke_id_and_priority: 0xC1,
            attribute: AttributeDescriptor::new(1, ObisCode::new(0, 0, 0x80, 0, 0, 0xFF), 2),
            access_selection: None,
            value: CosemDataType::LongUnsigned(0x9999),
        };
        let resp = SetResponse::decode(&d.dispatch(&req.encode().unwrap()).unwrap()).unwrap();
        assert_eq!(resp, SetResponse::Normal { invoke_id_and_priority: 0xC1, result: data_access_result::SUCCESS });
        let get = GetRequest::Normal {
            invoke_id_and_priority: 0xC1,
            attribute: AttributeDescriptor::new(1, ObisCode::new(0, 0, 0x80, 0, 0, 0xFF), 2),
            access_selection: None,
        };
        let get_resp = GetResponse::decode(&d.dispatch(&get.encode().unwrap()).unwrap()).unwrap();
        assert_eq!(
            get_resp,
            GetResponse::Normal {
                invoke_id_and_priority: 0xC1,
                result: GetDataResult::Data(CosemDataType::LongUnsigned(0x9999)),
            }
        );
    }

    #[test]
    fn get_with_list_reads_each_attribute() {
        let mut d = dispatcher_with_data();
        let req = GetRequest::WithList {
            invoke_id_and_priority: 0xC1,
            attributes: vec![
                (AttributeDescriptor::new(1, ObisCode::new(0, 0, 0x80, 0, 0, 0xFF), 2), None),
                (AttributeDescriptor::new(1, ObisCode::new(0, 0, 0x80, 0, 0, 0xFF), 1), None),
            ],
        };
        let resp = GetResponse::decode(&d.dispatch(&req.encode().unwrap()).unwrap()).unwrap();
        match resp {
            GetResponse::WithList { results, .. } => {
                assert_eq!(results[0], GetDataResult::Data(CosemDataType::LongUnsigned(0x1234)));
                // Attribute 1 is the logical name.
                assert!(matches!(results[1], GetDataResult::Data(CosemDataType::OctetString(_))));
            }
            _ => panic!("expected WITH-LIST response"),
        }
    }

    #[test]
    fn rlrq_is_answered_with_rlre() {
        let mut d = dispatcher_with_data();
        let rlrq =
            crate::service::acse::ReleaseRequest { reason: Some(acse::release_reason::NORMAL), user_information: None }
                .encode_rlrq();
        let resp = d.dispatch(&rlrq).unwrap();
        assert_eq!(resp.first(), Some(&acse::RLRE_TAG));
    }

    #[test]
    fn unsupported_tag_yields_exception_response() {
        let mut d = dispatcher_with_data();
        let resp = d.dispatch(&[tag::DATA_NOTIFICATION, 0x00]).unwrap();
        assert_eq!(resp[0], tag::EXCEPTION_RESPONSE);
    }

    fn push_setup_config() -> crate::classes::push_setup::PushSetupConfig {
        use crate::types::attrs::{ConfirmationParameters, DateTime, SendDestinationAndMethod};
        crate::classes::push_setup::PushSetupConfig {
            logical_name: ObisCode::new(0, 0, 25, 9, 0, 255),
            version: 0,
            push_object_list: vec![],
            send_destination_and_method: SendDestinationAndMethod {
                transport_service: 0,
                destination: b"192.168.0.1:4059".to_vec(),
                message: 0,
            },
            communication_window: vec![],
            randomisation_start_interval: 0,
            number_of_retries: 3,
            repetition_delay: CosemDataType::LongUnsigned(0),
            port_reference: vec![],
            push_client_sap: 1,
            push_protection_parameters: vec![],
            push_operation_method: 0,
            confirmation_parameters: ConfirmationParameters { data: vec![] },
            last_confirmation_date_time: DateTime::new([0u8; 12]),
        }
    }

    #[test]
    fn push_delivery_request_reads_registered_objects() {
        use crate::classes::push_setup::PushSetup;
        use crate::types::attrs::CaptureObjectDefinition;

        let mut d = dispatcher_with_data();
        let mut config = push_setup_config();
        config.push_object_list = vec![CaptureObjectDefinition::new(1, ObisCode::new(0, 0, 0x80, 0, 0, 0xFF), 2, 0)];
        let push = PushSetup::new(config);

        let req = d.build_push_delivery_request(&push, 1).unwrap();
        assert_eq!(req.destination, b"192.168.0.1:4059");
        assert_eq!(req.client_sap, 1);
        let decoded = DataNotification::decode(&req.body).unwrap();
        assert_eq!(decoded.notification_body, CosemDataType::LongUnsigned(0x1234));
    }

    #[test]
    fn push_delivery_request_rejects_missing_object() {
        use crate::classes::push_setup::PushSetup;
        use crate::types::attrs::CaptureObjectDefinition;

        let mut d = dispatcher_with_data();
        let mut config = push_setup_config();
        config.push_object_list = vec![CaptureObjectDefinition::new(1, ObisCode::new(9, 9, 9, 9, 9, 9), 2, 0)];
        let push = PushSetup::new(config);

        assert!(d.build_push_delivery_request(&push, 1).is_err());
    }
}