darra-ethercat-master 2.7.0

Commercial EtherCAT master protocol stack, real-time kernel driver integration, Windows and Linux support, multi-language SDKs, complex topology and hot-plug support.
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
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857

use crate::data::error::{
    CiA402State, DarraError, EcState, SlaveAlMirror, SyncWindowStatus, WcContribution, Result,
};
use crate::data::types::EcALState;
use crate::utils::ffi::{self, EcSlaveBlittable, EmcyRecord, SlaveIdentity, EsmTimeouts, FoEOptions};
use std::ffi::CString;
use std::os::raw::c_int;

#[repr(C, packed)]
#[derive(Debug, Clone, Copy)]
pub struct SlaveDetailedInfo {

    pub configadr: u16,

    pub eep_man: u32,

    pub eep_id: u32,

    pub eep_rev: u32,

    pub state: u8,

    pub ptype: u8,

    pub topology: u8,

    pub activeports: u8,

    pub parent: u16,

    pub hasdc: u8,

    pub group: u8,

    pub ibytes: u16,

    pub obytes: u16,

    pub mbx_proto: u16,

    pub pdelay: i32,

    pub dc_cycle: i32,

    pub dc_shift: i32,

    pub al_status_code: u16,
}

#[derive(Debug, Clone, Copy)]
pub struct Slave {
    master_index: u16,
    slave_index: u16,
}

impl Slave {

    pub(crate) fn new(master_index: u16, slave_index: u16) -> Self {
        Self { master_index, slave_index }
    }

    fn slave_ptr(&self) -> Option<*const EcSlaveBlittable> {
        let ptr = unsafe { ffi::GetSlave(self.master_index, self.slave_index) };
        if ptr.is_null() { None } else { Some(ptr as *const EcSlaveBlittable) }
    }

    fn read_slave(&self) -> Option<EcSlaveBlittable> {
        self.slave_ptr().map(|ptr| unsafe { std::ptr::read_unaligned(ptr) })
    }

    pub fn detailed_info(&self) -> Option<SlaveDetailedInfo> {
        let ptr = unsafe { ffi::GetSlaveDetailedInfo(self.master_index, self.slave_index) };
        if ptr.is_null() {
            None
        } else {
            Some(unsafe { std::ptr::read(ptr as *const SlaveDetailedInfo) })
        }
    }

    pub fn index(&self) -> u16 {
        self.slave_index
    }

    pub fn master_index(&self) -> u16 {
        self.master_index
    }

    pub fn name(&self) -> String {
        self.read_slave().map(|s| s.group_name_str()).unwrap_or_default()
    }

    pub fn drive_name(&self) -> String {
        self.read_slave().map(|s| s.device_name_str()).unwrap_or_default()
    }

    pub fn vendor_id(&self) -> u32 {
        let mut id = SlaveIdentity { vendor_id: 0, product_code: 0, revision_no: 0, serial_no: 0 };
        if unsafe { ffi::GetSlaveIdentity(self.master_index, self.slave_index, &mut id) } != 0
            && id.vendor_id != 0
        {
            return id.vendor_id;
        }
        self.read_slave().map(|s| s.metadata.identity.vendor_id).unwrap_or(0)
    }

    pub fn product_id(&self) -> u32 {
        let mut id = SlaveIdentity { vendor_id: 0, product_code: 0, revision_no: 0, serial_no: 0 };
        if unsafe { ffi::GetSlaveIdentity(self.master_index, self.slave_index, &mut id) } != 0
            && id.product_code != 0
        {
            return id.product_code;
        }
        self.read_slave().map(|s| s.metadata.identity.product_id).unwrap_or(0)
    }

    pub fn rev_id(&self) -> u32 {
        let mut id = SlaveIdentity { vendor_id: 0, product_code: 0, revision_no: 0, serial_no: 0 };
        if unsafe { ffi::GetSlaveIdentity(self.master_index, self.slave_index, &mut id) } != 0
            && (id.vendor_id != 0 || id.revision_no != 0)
        {
            return id.revision_no;
        }
        self.read_slave().map(|s| s.metadata.identity.revision).unwrap_or(0)
    }

    pub fn serial_number(&self) -> u32 {
        self.read_slave().map(|s| s.metadata.identity.serial).unwrap_or(0)
    }

    pub fn identity(&self) -> Result<SlaveIdentity> {
        let mut id = SlaveIdentity { vendor_id: 0, product_code: 0, revision_no: 0, serial_no: 0 };
        if unsafe { ffi::GetSlaveIdentity(self.master_index, self.slave_index, &mut id) } != 0 {
            Ok(id)
        } else {
            Err(DarraError::Other(obfstr::obfstr!("获取从站身份失败").into()))
        }
    }

    pub fn alias_address(&self) -> u16 {
        self.read_slave().map(|s| s.alias_addr).unwrap_or(0)
    }

    pub fn config_addr(&self) -> u16 {
        self.read_slave().map(|s| s.config_addr).unwrap_or(0)
    }

    pub fn state(&self) -> Option<EcState> {
        let raw = unsafe { ffi::GetSlaveState(self.master_index, self.slave_index) };
        EcState::from_raw(raw)
    }

    pub fn state_raw(&self) -> u8 {
        unsafe { ffi::GetSlaveState(self.master_index, self.slave_index) }
    }

    pub fn error_code(&self) -> EcALState {
        let raw = unsafe { ffi::GetSlaveALStatusCode(self.master_index, self.slave_index) };
        EcALState::from_value(raw as i32)
    }

    pub fn error_code_raw(&self) -> u16 {
        unsafe { ffi::GetSlaveALStatusCode(self.master_index, self.slave_index) }
    }

    pub fn state_live(&self) -> Option<EcState> {
        let raw = unsafe { ffi::GetSlaveStateLive(self.master_index, self.slave_index) };
        EcState::from_raw(raw)
    }

    pub fn error_code_live(&self) -> EcALState {
        let raw = unsafe { ffi::GetSlaveALStatusCodeLive(self.master_index, self.slave_index) };
        EcALState::from_value(raw as i32)
    }

    pub fn wc_contributed(&self) -> WcContribution {
        let raw = unsafe { ffi::GetSlaveWcState(self.master_index, self.slave_index) };
        WcContribution::from_raw(raw)
    }

    pub fn wc_contributed_raw(&self) -> u8 {
        unsafe { ffi::GetSlaveWcState(self.master_index, self.slave_index) }
    }

    pub fn al_status_mirror(&self) -> SlaveAlMirror {
        let raw = unsafe { ffi::GetSlaveAlStatusMirror(self.master_index, self.slave_index) };
        SlaveAlMirror::from_raw(raw)
    }

    pub fn al_status_mirror_raw(&self) -> u16 {
        unsafe { ffi::GetSlaveAlStatusMirror(self.master_index, self.slave_index) }
    }

    pub fn free_run_demoted(&self) -> bool {
        unsafe { ffi::GetSlaveFreeRunDemoted(self.master_index, self.slave_index) != 0 }
    }

    pub fn mailbox_health_raw(&self) -> u8 {
        unsafe { ffi::GetSlaveMailboxHealth(self.master_index, self.slave_index) }
    }

    pub fn mailbox_healthy(&self) -> bool {
        self.mailbox_health_raw() == 0
    }

    pub fn health_degraded_count(&self) -> u32 {
        unsafe { ffi::GetSlaveHealthDegradedCount(self.master_index, self.slave_index) }
    }

    pub fn recover_mailbox_health(&self) -> bool {
        unsafe { ffi::RecoverSlaveMailboxHealth(self.master_index, self.slave_index) != 0 }
    }

    pub fn recover_to_op(&self) -> Option<EcState> {
        let raw = unsafe { ffi::RecoverSlaveToOP(self.master_index, self.slave_index) };
        EcState::from_raw(raw as u8)
    }

    pub fn is_lost(&self) -> bool {
        self.read_slave().map(|s| s.runtime.is_lost != 0).unwrap_or(false)
    }

    pub fn block_lrw(&self) -> bool {
        self.read_slave().map(|s| s.runtime.block_lrw != 0).unwrap_or(false)
    }

    pub fn physical_type(&self) -> u8 {
        self.read_slave().map(|s| s.topo.phy_type).unwrap_or(0)
    }

    pub fn ebus_current(&self) -> i16 {
        self.read_slave().map(|s| s.runtime.ebus_current).unwrap_or(0)
    }

    pub fn input_bits(&self) -> u16 {
        self.read_slave().map(|s| s.input.bits).unwrap_or(0)
    }

    pub fn input_bytes(&self) -> u32 {
        self.read_slave().map(|s| s.input.bytes).unwrap_or(0)
    }

    pub fn output_bits(&self) -> u16 {
        self.read_slave().map(|s| s.output.bits).unwrap_or(0)
    }

    pub fn output_bytes(&self) -> u32 {
        self.read_slave().map(|s| s.output.bytes).unwrap_or(0)
    }

    pub fn input_offset(&self) -> u32 {
        self.read_slave().map(|s| s.input.offset).unwrap_or(0)
    }

    pub fn output_offset(&self) -> u32 {
        self.read_slave().map(|s| s.output.offset).unwrap_or(0)
    }

    pub fn input_startbit(&self) -> u8 {
        self.read_slave().map(|s| s.input.startbit).unwrap_or(0)
    }

    pub fn output_startbit(&self) -> u8 {
        self.read_slave().map(|s| s.output.startbit).unwrap_or(0)
    }

    pub fn io(&self) -> Result<(i32, *mut u8, i32, *mut u8)> {
        let mut out_size: c_int = 0;
        let mut out_ptr: *mut u8 = std::ptr::null_mut();
        let mut in_size: c_int = 0;
        let mut in_ptr: *mut u8 = std::ptr::null_mut();
        let ret = unsafe {
            ffi::GetIO(self.master_index, self.slave_index,
                       &mut out_size, &mut out_ptr, &mut in_size, &mut in_ptr)
        };
        if ret != 0 { Ok((out_size, out_ptr, in_size, in_ptr)) }
        else { Err(DarraError::NullPointer) }
    }

    pub fn mbx_length(&self) -> u16 {
        self.read_slave().map(|s| s.mbx.length).unwrap_or(0)
    }

    pub fn mbx_protocol(&self) -> u16 {
        self.read_slave().map(|s| s.mbx.supported_proto).unwrap_or(0)
    }

    pub fn mbx_read_length(&self) -> u16 {
        self.read_slave().map(|s| s.mbx.read_length).unwrap_or(0)
    }

    pub fn mbx_write_offset(&self) -> u16 {
        self.read_slave().map(|s| s.mbx.write_offset).unwrap_or(0)
    }

    pub fn mbx_read_offset(&self) -> u16 {
        self.read_slave().map(|s| s.mbx.read_offset).unwrap_or(0)
    }

    pub fn mbx_count(&self) -> u8 {
        self.read_slave().map(|s| s.mbx.cnt).unwrap_or(0)
    }

    pub fn has_dc(&self) -> bool {
        self.read_slave().map(|s| s.topo.has_dc != 0).unwrap_or(false)
    }

    pub fn dc_active(&self) -> u16 {
        self.read_slave().map(|s| s.dc.active).unwrap_or(0)
    }

    pub fn dc_cycle0(&self) -> i32 {
        self.read_slave().map(|s| s.dc.cycle0).unwrap_or(0)
    }

    pub fn dc_cycle1(&self) -> i32 {
        self.read_slave().map(|s| s.dc.cycle1).unwrap_or(0)
    }

    pub fn dc_shift(&self) -> i32 {
        self.read_slave().map(|s| s.dc.shift).unwrap_or(0)
    }

    pub fn dc_next(&self) -> u16 {
        self.read_slave().map(|s| s.dc.next).unwrap_or(0)
    }

    pub fn dc_previous(&self) -> u16 {
        self.read_slave().map(|s| s.dc.prev).unwrap_or(0)
    }

    pub fn dc_parent_port(&self) -> u8 {
        self.read_slave().map(|s| s.topo.parent_port).unwrap_or(0)
    }

    pub fn dc_receive_time_a(&self) -> i32 {
        self.read_slave().map(|s| s.dc.recvtime[0]).unwrap_or(0)
    }

    pub fn dc_receive_time_b(&self) -> i32 {
        self.read_slave().map(|s| s.dc.recvtime[1]).unwrap_or(0)
    }

    pub fn dc_receive_time_c(&self) -> i32 {
        self.read_slave().map(|s| s.dc.recvtime[2]).unwrap_or(0)
    }

    pub fn dc_receive_time_d(&self) -> i32 {
        self.read_slave().map(|s| s.dc.recvtime[3]).unwrap_or(0)
    }

    pub fn propagation_delay(&self) -> i32 {
        unsafe { ffi::GetSlavePropagationDelay(self.master_index, self.slave_index) }
    }

    pub fn active_ports(&self) -> u8 {
        unsafe { ffi::GetSlaveActivePorts(self.master_index, self.slave_index) }
    }

    pub fn topology(&self) -> u8 {
        self.read_slave().map(|s| s.topo.link_count).unwrap_or(0)
    }

    pub fn entry_port(&self) -> u8 {
        self.read_slave().map(|s| s.topo.entry_port).unwrap_or(0)
    }

    pub fn parent(&self) -> u16 {
        unsafe { ffi::GetSlaveParent(self.master_index, self.slave_index) }
    }

    pub fn parent_port(&self) -> u8 {
        self.read_slave().map(|s| s.topo.parent_port).unwrap_or(0)
    }

    pub fn eep_8byte_addressing(&self) -> bool {
        self.read_slave().map(|s| s.eeprom_config.read_8byte != 0).unwrap_or(false)
    }

    pub fn eep_pdi(&self) -> u8 {
        self.read_slave().map(|s| s.eeprom_config.pdi).unwrap_or(0)
    }

    pub fn coe_details(&self) -> u8 {
        self.read_slave().map(|s| s.capabilities.coe_details).unwrap_or(0)
    }

    pub fn foe_details(&self) -> u8 {
        self.read_slave().map(|s| s.capabilities.foe_details).unwrap_or(0)
    }

    pub fn eoe_details(&self) -> u8 {
        self.read_slave().map(|s| s.capabilities.eoe_details).unwrap_or(0)
    }

    pub fn soe_details(&self) -> u8 {
        self.read_slave().map(|s| s.capabilities.soe_details).unwrap_or(0)
    }

    pub fn fmmu0_func(&self) -> u8 {
        self.read_slave().map(|s| s.sm_fmmu.fmmu_func[0]).unwrap_or(0)
    }

    pub fn fmmu1_func(&self) -> u8 {
        self.read_slave().map(|s| s.sm_fmmu.fmmu_func[1]).unwrap_or(0)
    }

    pub fn fmmu2_func(&self) -> u8 {
        self.read_slave().map(|s| s.sm_fmmu.fmmu_func[2]).unwrap_or(0)
    }

    pub fn fmmu3_func(&self) -> u8 {
        self.read_slave().map(|s| s.sm_fmmu.fmmu_func[3]).unwrap_or(0)
    }

    pub fn sync_manager_count(&self) -> u16 {
        self.read_slave().map(|s| s.metadata.sm_count).unwrap_or(0)
    }

    pub fn supports_complete_access(&self) -> bool {
        self.read_slave().map(|s| s.pdo_config.supports_complete_access != 0).unwrap_or(false)
    }

    pub fn set_supports_complete_access(&self, value: bool) {
        if let Some(ptr) = self.slave_ptr() {
            unsafe { (*(ptr as *mut EcSlaveBlittable)).pdo_config.supports_complete_access = if value { 1 } else { 0 }; }
        }
    }

    pub fn pdo_assignment_enabled(&self) -> bool {
        self.read_slave().map(|s| s.pdo_config.assignment_enabled != 0).unwrap_or(true)
    }

    pub fn set_pdo_assignment_enabled(&self, value: bool) {
        if let Some(ptr) = self.slave_ptr() {
            unsafe { (*(ptr as *mut EcSlaveBlittable)).pdo_config.assignment_enabled = if value { 1 } else { 0 }; }
        }
    }

    pub fn pdo_configuration_enabled(&self) -> bool {
        self.read_slave().map(|s| s.pdo_config.configuration_enabled != 0).unwrap_or(false)
    }

    pub fn set_pdo_configuration_enabled(&self, value: bool) {
        if let Some(ptr) = self.slave_ptr() {
            unsafe { (*(ptr as *mut EcSlaveBlittable)).pdo_config.configuration_enabled = if value { 1 } else { 0 }; }
        }
    }

    pub fn group(&self) -> u8 {
        unsafe { ffi::GetSlaveGroup(self.master_index, self.slave_index) }
    }

    pub fn set_group(&self, group: u8) -> Result<()> {
        if unsafe { ffi::SetSlaveGroup(self.master_index, self.slave_index, group) } != 0 {
            Ok(())
        } else {
            Err(DarraError::Other(obfstr::obfstr!("设置从站分组失败").into()))
        }
    }

    pub fn is_optional(&self) -> bool {
        (unsafe { ffi::GetSlaveOptional(self.master_index, self.slave_index) }) != 0
    }

    pub fn set_optional(&self, optional: bool) -> Result<()> {
        if unsafe { ffi::SetSlaveOptional(self.master_index, self.slave_index, if optional { 1 } else { 0 }) } != 0 {
            Ok(())
        } else {
            Err(DarraError::Other(obfstr::obfstr!("设置可选标志失败").into()))
        }
    }

    pub fn supports_frame_repeat(&self) -> bool {
        (unsafe { ffi::GetSlaveSupportsFrameRepeat(self.master_index, self.slave_index) }) != 0
    }

    pub fn set_supports_frame_repeat(&self, supports: bool) -> Result<()> {
        if unsafe { ffi::SetSlaveSupportsFrameRepeat(self.master_index, self.slave_index, if supports { 1 } else { 0 }) } != 0 {
            Ok(())
        } else {
            Err(DarraError::Other(obfstr::obfstr!("设置帧重发支持标志失败").into()))
        }
    }

    pub fn set_watchdog(&self, timeout_ms: u32) -> Result<()> {
        if unsafe { ffi::SetSlaveWatchdog(self.master_index, self.slave_index, timeout_ms) } != 0 {
            Ok(())
        } else {
            Err(DarraError::Other(obfstr::obfstr!("设置看门狗失败").into()))
        }
    }

    pub fn set_pdi_watchdog(&self, timeout_ms: u32) -> Result<()> {
        if unsafe { ffi::SetSlavePdiWatchdog(self.master_index, self.slave_index, timeout_ms) } != 0 {
            Ok(())
        } else {
            Err(DarraError::Other(obfstr::obfstr!("设置 PDI 看门狗失败").into()))
        }
    }

    pub fn watchdog_config(&self) -> Result<ffi::WatchdogConfig> {
        let mut config = ffi::WatchdogConfig { wd_divider: 0, wd_time_pdi: 0, wd_time_pd: 0 };
        if unsafe { ffi::GetSlaveWatchdogConfig(self.master_index, self.slave_index, &mut config) } != 0 {
            Ok(config)
        } else {
            Err(DarraError::Other(obfstr::obfstr!("获取看门狗配置失败").into()))
        }
    }

    pub fn watchdog_status(&self) -> Result<ffi::WatchdogStatus> {
        let mut status = ffi::WatchdogStatus { wd_status: 0, wd_counter: 0, wd_divider: 0, wd_time_pd: 0 };
        if unsafe { ffi::GetSlaveWatchdogStatus(self.master_index, self.slave_index, &mut status) } != 0 {
            Ok(status)
        } else {
            Err(DarraError::Other(obfstr::obfstr!("获取看门狗状态失败").into()))
        }
    }

    pub fn set_state(&self, state: EcState, timeout_ms: u32) -> Result<()> {
        let ret = unsafe {
            ffi::SetSlaveStateWithTimeout(self.master_index, self.slave_index, state as c_int, timeout_ms)
        };
        if ret != 0 { Ok(()) } else { Err(DarraError::StateChangeFailed(state as u8)) }
    }

    pub fn set_dc_sync(&self, sync0_ns: u32, sync1_ns: u32, shift_ns: i32) {
        unsafe {
            ffi::SetSyncBySlaveIndex(self.master_index, self.slave_index, sync0_ns, sync1_ns, shift_ns);
        }
    }

    pub fn sync_window_status(&self) -> Result<SyncWindowStatus> {
        let mut diff: c_int = 0;
        let mut max_diff: c_int = 0;
        let mut min_diff: c_int = 0;
        let mut in_sync: i32 = 0;
        let mut out_count: u32 = 0;
        if unsafe {
            ffi::GetSlaveSyncWindowStatus(
                self.master_index, self.slave_index,
                &mut diff, &mut max_diff, &mut min_diff,
                &mut in_sync, &mut out_count,
            )
        } != 0 {
            Ok(SyncWindowStatus {
                diff_ns: diff,
                max_diff_ns: max_diff,
                min_diff_ns: min_diff,
                in_sync: in_sync != 0,
                out_of_sync_count: out_count,
            })
        } else {
            Err(DarraError::Other(obfstr::obfstr!("获取同步窗口状态失败").into()))
        }
    }

    pub fn reset_sync_window_stats(&self) {
        unsafe { ffi::ResetSlaveSyncWindowStats(self.master_index, self.slave_index) };
    }

    pub fn verify_identity(&self, expected: &SlaveIdentity, check_rev: bool, check_serial: bool) -> bool {
        unsafe {
            ffi::VerifySlaveIdentity(
                self.master_index, self.slave_index, expected,
                if check_rev { 1 } else { 0 },
                if check_serial { 1 } else { 0 },
            ) != 0
        }
    }

    pub fn esm_timeouts(&self) -> Result<EsmTimeouts> {
        let mut t = EsmTimeouts { ip: 0, ps: 0, so: 0, os: 0, sp: 0, pi: 0, bi: 0, ib: 0 };
        if unsafe { ffi::GetSlaveEsmTimeouts(self.master_index, self.slave_index, &mut t) } != 0 {
            Ok(t)
        } else {
            Err(DarraError::Other(obfstr::obfstr!("获取 ESM 超时失败").into()))
        }
    }

    pub fn set_esm_timeouts(&self, timeouts: &EsmTimeouts) -> Result<()> {
        if unsafe { ffi::SetSlaveEsmTimeouts(self.master_index, self.slave_index, timeouts) } != 0 {
            Ok(())
        } else {
            Err(DarraError::Other(obfstr::obfstr!("设置 ESM 超时失败").into()))
        }
    }

    pub fn write_register(&self, reg_addr: u16, data: &[u8]) -> Result<()> {
        if unsafe { ffi::WriteSlaveRegister(self.master_index, self.slave_index, reg_addr, data.as_ptr(), data.len() as u32) } != 0 {
            Ok(())
        } else {
            Err(DarraError::RegisterFailed)
        }
    }

    pub fn read_register(&self, reg_addr: u16, len: u32) -> Result<Vec<u8>> {
        let mut buf = vec![0u8; len as usize];
        if unsafe { ffi::ReadSlaveRegister(self.master_index, self.slave_index, reg_addr, buf.as_mut_ptr(), len) } != 0 {
            Ok(buf)
        } else {
            Err(DarraError::RegisterFailed)
        }
    }

    pub fn read_eeprom(&self, byte_offset: u16, byte_length: u16) -> Result<Vec<u8>> {
        if byte_length == 0 {
            return Ok(Vec::new());
        }
        let word_addr = byte_offset >> 1;
        let word_count = (byte_length + 1) >> 1;
        let first_byte_odd = byte_offset & 1;
        let mut result: Vec<u8> = Vec::with_capacity(byte_length as usize);
        for i in 0..word_count {
            let mut w: u16 = 0;
            if unsafe { ffi::SIIReadWord(self.master_index, self.slave_index, word_addr + i, &mut w) } == 0 {
                return Err(DarraError::Other(obfstr::obfstr!("EEPROM 读失败").into()));
            }
            let lo = (w & 0xFF) as u8;
            let hi = ((w >> 8) & 0xFF) as u8;
            if i == 0 && first_byte_odd != 0 {
                if (result.len() as u16) < byte_length { result.push(hi); }
            } else {
                if (result.len() as u16) < byte_length { result.push(lo); }
                if (result.len() as u16) < byte_length { result.push(hi); }
            }
        }
        Ok(result)
    }

    pub fn write_eeprom(&self, byte_offset: u16, data: &[u8]) -> Result<()> {

        use crate::logging::{LogManager, LogCategory};
        if data.is_empty() {
            let msg = format!("write_eeprom: data 为空 (slave={})", self.slave_index);
            LogManager::instance().add_log(LogCategory::Error, &msg);
            return Err(DarraError::InvalidParameter(msg));
        }
        if (byte_offset & 1) != 0 {
            let msg = format!("write_eeprom: byte_offset {} 必须偶数 (EEPROM 按 word 写入, slave={})",
                byte_offset, self.slave_index);
            LogManager::instance().add_log(LogCategory::Error, &msg);
            return Err(DarraError::InvalidParameter(msg));
        }
        if (data.len() & 1) != 0 {
            let msg = format!("write_eeprom: data.len() {} 必须偶数 (EEPROM 按 word 写入, slave={})",
                data.len(), self.slave_index);
            LogManager::instance().add_log(LogCategory::Error, &msg);
            return Err(DarraError::InvalidParameter(msg));
        }
        let word_addr = byte_offset >> 1;
        let word_count = (data.len() / 2) as u16;
        for i in 0..word_count {
            let idx = (i as usize) * 2;
            let w: u16 = (data[idx] as u16) | ((data[idx + 1] as u16) << 8);
            if unsafe { ffi::SIIWriteWord(self.master_index, self.slave_index, word_addr + i, w) } == 0 {
                let msg = format!("write_eeprom: SIIWriteWord 失败 word {} (slave={})",
                    word_addr + i, self.slave_index);
                LogManager::instance().add_log(LogCategory::Error, &msg);
                return Err(DarraError::Other(msg));
            }
        }
        Ok(())
    }

    pub fn sii_acquire(&self) -> Result<()> {
        if unsafe { ffi::SIIAcquire(self.master_index, self.slave_index) } != 0 {
            Ok(())
        } else {
            Err(DarraError::Other(obfstr::obfstr!("SII 申请访问权失败").into()))
        }
    }

    pub fn sii_release(&self) -> Result<()> {
        if unsafe { ffi::SIIRelease(self.master_index, self.slave_index) } != 0 {
            Ok(())
        } else {
            Err(DarraError::Other(obfstr::obfstr!("SII 释放访问权失败").into()))
        }
    }

    pub fn sii_read_control_reg(&self) -> Result<u8> {
        let mut ctrl: u8 = 0;
        if unsafe { ffi::SIIReadControlReg(self.master_index, self.slave_index, &mut ctrl) } != 0 {
            Ok(ctrl)
        } else {
            Err(DarraError::Other(obfstr::obfstr!("SII 读控制寄存器失败").into()))
        }
    }

    pub fn sii_find_category(&self, cat_type: u16) -> Result<(u32, u32)> {
        let mut byte_offset: u32 = 0;
        let mut byte_size: u32 = 0;
        if unsafe {
            ffi::SIIFindCategory(self.master_index, self.slave_index, cat_type,
                                 &mut byte_offset, &mut byte_size)
        } != 0 {
            Ok((byte_offset, byte_size))
        } else {
            Err(DarraError::Other(format!("SII 未找到 Category 类型 {}", cat_type)))
        }
    }

    pub fn sii_read_category(&self, cat_type: u16, max_bytes: u32) -> Result<Vec<u8>> {
        if max_bytes == 0 {
            return Ok(Vec::new());
        }
        let mut buffer = vec![0u8; max_bytes as usize];
        let mut actual_bytes: u32 = 0;
        if unsafe {
            ffi::SIIReadCategory(self.master_index, self.slave_index, cat_type,
                                 buffer.as_mut_ptr(), max_bytes, &mut actual_bytes)
        } != 0 {
            buffer.truncate(actual_bytes as usize);
            Ok(buffer)
        } else {
            Err(DarraError::Other(format!("SII 读 Category 类型 {} 失败", cat_type)))
        }
    }

    pub fn sii_enumerate_categories(&self, max_count: i32) -> Result<Vec<u16>> {
        if max_count <= 0 {
            return Ok(Vec::new());
        }
        let mut types = vec![0u16; max_count as usize];
        let count = unsafe {
            ffi::SIIEnumerateCategories(self.master_index, self.slave_index,
                                        types.as_mut_ptr(), max_count)
        };
        if count >= 0 {
            types.truncate(count as usize);
            Ok(types)
        } else {
            Err(DarraError::Other(obfstr::obfstr!("SII 枚举 Category 失败").into()))
        }
    }

    pub fn sii_get_strings(&self, max_bytes: u32) -> Result<Vec<u8>> {
        if max_bytes == 0 {
            return Ok(Vec::new());
        }
        let mut buffer = vec![0u8; max_bytes as usize];
        let written = unsafe {
            ffi::SIIGetStrings(self.master_index, self.slave_index, buffer.as_mut_ptr(), max_bytes)
        };
        if written >= 0 {
            buffer.truncate(written as usize);
            Ok(buffer)
        } else {
            Err(DarraError::Other(obfstr::obfstr!("SII 读字符串表失败").into()))
        }
    }

    pub fn sii_get_string_by_index(&self, string_index: u8, max_bytes: u32) -> Result<String> {
        if max_bytes == 0 {
            return Ok(String::new());
        }
        let mut buffer = vec![0u8; max_bytes as usize];
        let written = unsafe {
            ffi::SIIGetStringByIndex(self.master_index, self.slave_index, string_index,
                                     buffer.as_mut_ptr(), max_bytes)
        };
        if written >= 0 {

            buffer.truncate(written as usize);
            let end = buffer.iter().position(|&b| b == 0).unwrap_or(buffer.len());
            Ok(String::from_utf8_lossy(&buffer[..end]).into_owned())
        } else {
            Err(DarraError::Other(format!("SII 读字符串索引 {} 失败", string_index)))
        }
    }

    pub fn sii_general_info(&self) -> Result<ffi::SiiGeneralInfo> {
        let mut info: ffi::SiiGeneralInfo = unsafe { std::mem::zeroed() };
        if unsafe { ffi::SIIGetGeneralInfo(self.master_index, self.slave_index, &mut info) } != 0 {
            Ok(info)
        } else {
            Err(DarraError::Other(obfstr::obfstr!("SII 读 General 信息失败").into()))
        }
    }

    pub fn configure_sync_manager(&self, sm_index: u8, start_addr: u16, length: u16, control: u8, enable: bool) -> Result<()> {
        if unsafe { ffi::ConfigureSyncManager(self.master_index, self.slave_index, sm_index, start_addr, length, control, if enable { 1 } else { 0 }) } != 0 {
            Ok(())
        } else {
            Err(DarraError::Other(obfstr::obfstr!("配置 SyncManager 失败").into()))
        }
    }

    pub fn configure_fmmu(
        &self, fmmu_index: u8, logical_addr: u32, length: u16,
        logical_start_bit: u8, logical_end_bit: u8,
        physical_addr: u16, physical_start_bit: u8,
        fmmu_type: u8, enable: bool,
    ) -> Result<()> {
        if unsafe {
            ffi::ConfigureFMMU(
                self.master_index, self.slave_index, fmmu_index,
                logical_addr, length, logical_start_bit, logical_end_bit,
                physical_addr, physical_start_bit, fmmu_type,
                if enable { 1 } else { 0 },
            )
        } != 0 {
            Ok(())
        } else {
            Err(DarraError::Other(obfstr::obfstr!("配置 FMMU 失败").into()))
        }
    }

    #[cfg(any(debug_assertions, feature = "internal-dlport"))]
    pub fn write_dl_port(&self, value: u8) -> bool {
        unsafe { ffi::WriteSlaveDLPORT(self.master_index, self.slave_index, value) != 0 }
    }

    #[cfg(any(debug_assertions, feature = "internal-dlport"))]
    pub fn read_dl_port(&self) -> Option<u8> {
        let mut val: u8 = 0;
        let ok = unsafe { ffi::ReadSlaveDLPORT(self.master_index, self.slave_index, &mut val) };
        if ok != 0 { Some(val) } else { None }
    }

    pub fn is_op_only(&self) -> bool {
        unsafe { ffi::GetSlaveOpOnlyFlag(self.master_index, self.slave_index) != 0 }
    }

    pub fn is_device_emulation(&self) -> bool {
        unsafe { ffi::GetSlaveDeviceEmulationFlag(self.master_index, self.slave_index) != 0 }
    }

    pub fn enable_output_sync_manager(&self) -> Result<()> {
        if unsafe { ffi::EnableOutputSyncManager(self.master_index, self.slave_index) } != 0 {
            Ok(())
        } else {
            Err(DarraError::Other(obfstr::obfstr!("启用输出 SyncManager 失败").into()))
        }
    }

    pub fn disable_output_sync_manager(&self) -> Result<()> {
        if unsafe { ffi::DisableOutputSyncManager(self.master_index, self.slave_index) } != 0 {
            Ok(())
        } else {
            Err(DarraError::Other(obfstr::obfstr!("禁用输出 SyncManager 失败").into()))
        }
    }

    pub fn set_error_ack(&self, set_ack: bool) -> Result<()> {
        if unsafe { ffi::SetSlaveErrorAck(self.master_index, self.slave_index, if set_ack { 1 } else { 0 }) } != 0 {
            Ok(())
        } else {
            Err(DarraError::Other(obfstr::obfstr!("设置错误确认位失败").into()))
        }
    }

    pub fn needs_startup_reconfig(&self) -> bool {
        unsafe { ffi::GetSlaveNeedsStartupReconfig(self.master_index, self.slave_index) != 0 }
    }

    pub fn clear_startup_reconfig(&self) {
        unsafe { ffi::ClearSlaveNeedsStartupReconfig(self.master_index, self.slave_index) };
    }

    pub fn sdo_read(&self, index: u16, subindex: u8, complete_access: bool) -> Result<Vec<u8>> {
        let mut size: c_int = 0;
        let ptr = unsafe {
            ffi::SDOread(self.master_index, self.slave_index, index, subindex,
                         if complete_access { 1 } else { 0 }, &mut size)
        };
        if ptr.is_null() || size <= 0 {
            if !ptr.is_null() { unsafe { ffi::FreeMemory(ptr as *mut _) }; }
            return Err(DarraError::SdoReadFailed { index, subindex, abort_code: None });
        }
        let data = unsafe { std::slice::from_raw_parts(ptr, size as usize).to_vec() };
        unsafe { ffi::FreeMemory(ptr as *mut _) };
        Ok(data)
    }

    pub fn sdo_read_value<T: Copy>(&self, index: u16, subindex: u8) -> Result<T> {
        let data = self.sdo_read(index, subindex, false)?;
        if data.len() < std::mem::size_of::<T>() {
            return Err(DarraError::SdoReadFailed { index, subindex, abort_code: None });
        }
        Ok(unsafe { std::ptr::read_unaligned(data.as_ptr() as *const T) })
    }

    pub fn sdo_write(&self, index: u16, subindex: u8, complete_access: bool, data: &[u8]) -> Result<()> {
        let ok = unsafe {
            ffi::SDOwrite_raw(self.master_index, self.slave_index, index, subindex,
                              if complete_access { 1 } else { 0 }, data.as_ptr(), data.len() as c_int)
        };
        if ok != 0 { Ok(()) } else { Err(DarraError::SdoWriteFailed { index, subindex, abort_code: None }) }
    }

    pub fn sdo_write_value<T: Copy>(&self, index: u16, subindex: u8, value: &T) -> Result<()> {
        let bytes = unsafe {
            std::slice::from_raw_parts(value as *const T as *const u8, std::mem::size_of::<T>())
        };
        self.sdo_write(index, subindex, false, bytes)
    }

    pub fn soe_read(&self, idn: u16, drive_no: u8, element_flags: u8, timeout_us: i32) -> Result<Vec<u8>> {
        let mut data_ptr: *mut std::ffi::c_void = std::ptr::null_mut();
        let mut data_size: c_int = 0;
        let ok = unsafe {
            ffi::SoERead(self.master_index, self.slave_index, drive_no, element_flags, idn,
                         &mut data_ptr, &mut data_size, timeout_us)
        };
        if ok == 0 || data_ptr.is_null() || data_size <= 0 {
            if !data_ptr.is_null() { unsafe { ffi::FreeMemory(data_ptr) }; }
            return Err(DarraError::SoeFailed(idn));
        }
        let data = unsafe { std::slice::from_raw_parts(data_ptr as *const u8, data_size as usize).to_vec() };
        unsafe { ffi::FreeMemory(data_ptr) };
        Ok(data)
    }

    pub fn soe_write(&self, idn: u16, drive_no: u8, element_flags: u8, data: &[u8], timeout_us: i32) -> Result<()> {
        let ok = unsafe {
            ffi::SoEWrite(self.master_index, self.slave_index, drive_no, element_flags, idn,
                          data.as_ptr(), data.len() as c_int, timeout_us)
        };
        if ok != 0 { Ok(()) } else { Err(DarraError::SoeFailed(idn)) }
    }

    pub fn soe_read_name(&self, idn: u16, drive_no: u8, timeout_us: i32) -> Result<String> {
        let mut name_ptr: *mut std::ffi::c_void = std::ptr::null_mut();
        let mut name_len: c_int = 0;
        let ok = unsafe {
            ffi::SoEReadName(self.master_index, self.slave_index, drive_no, idn,
                             &mut name_ptr, &mut name_len, timeout_us)
        };
        if ok == 0 || name_ptr.is_null() || name_len <= 0 {
            if !name_ptr.is_null() { unsafe { ffi::FreeMemory(name_ptr) }; }
            return Err(DarraError::SoeFailed(idn));
        }
        let bytes = unsafe { std::slice::from_raw_parts(name_ptr as *const u8, name_len as usize) };

        let name = crate::utils::help::decode_ethercat_string(bytes);
        unsafe { ffi::FreeMemory(name_ptr) };
        Ok(name)
    }

    pub fn soe_read_unit(&self, idn: u16, drive_no: u8, timeout_us: i32) -> Result<String> {
        let mut unit_ptr: *mut std::ffi::c_void = std::ptr::null_mut();
        let mut unit_len: c_int = 0;
        let ok = unsafe {
            ffi::SoEReadUnit(self.master_index, self.slave_index, drive_no, idn,
                             &mut unit_ptr, &mut unit_len, timeout_us)
        };
        if ok == 0 || unit_ptr.is_null() || unit_len <= 0 {
            if !unit_ptr.is_null() { unsafe { ffi::FreeMemory(unit_ptr) }; }
            return Err(DarraError::SoeFailed(idn));
        }
        let bytes = unsafe { std::slice::from_raw_parts(unit_ptr as *const u8, unit_len as usize) };

        let unit = crate::utils::help::decode_ethercat_string(bytes);
        unsafe { ffi::FreeMemory(unit_ptr) };
        Ok(unit)
    }

    pub fn soe_read_min_max(&self, idn: u16, drive_no: u8, timeout_us: i32) -> Result<(Vec<u8>, Vec<u8>)> {
        let mut min_ptr: *mut std::ffi::c_void = std::ptr::null_mut();
        let mut min_size: c_int = 0;
        let mut max_ptr: *mut std::ffi::c_void = std::ptr::null_mut();
        let mut max_size: c_int = 0;
        let ok = unsafe {
            ffi::SoEReadMinMax(self.master_index, self.slave_index, drive_no, idn,
                               &mut min_ptr, &mut min_size, &mut max_ptr, &mut max_size, timeout_us)
        };
        if ok == 0 {
            return Err(DarraError::SoeFailed(idn));
        }
        let min_data = if !min_ptr.is_null() && min_size > 0 {
            let d = unsafe { std::slice::from_raw_parts(min_ptr as *const u8, min_size as usize).to_vec() };
            unsafe { ffi::FreeMemory(min_ptr) };
            d
        } else { Vec::new() };
        let max_data = if !max_ptr.is_null() && max_size > 0 {
            let d = unsafe { std::slice::from_raw_parts(max_ptr as *const u8, max_size as usize).to_vec() };
            unsafe { ffi::FreeMemory(max_ptr) };
            d
        } else { Vec::new() };
        Ok((min_data, max_data))
    }

    pub fn soe_read_attributes(&self, idn: u16, drive_no: u8, timeout_us: i32) -> Result<u32> {
        let mut attrs: u32 = 0;
        let ok = unsafe {
            ffi::SoEReadAttributes(self.master_index, self.slave_index, drive_no, idn,
                                   &mut attrs, timeout_us)
        };
        if ok != 0 { Ok(attrs) } else { Err(DarraError::SoeFailed(idn)) }
    }

    pub fn soe_read_idn_list(&self, drive_no: u8, timeout_us: i32) -> Result<Vec<u16>> {
        let mut list_ptr: *mut u16 = std::ptr::null_mut();
        let mut list_count: c_int = 0;
        let ok = unsafe {
            ffi::SoEReadIDNList(self.master_index, self.slave_index, drive_no,
                                &mut list_ptr, &mut list_count, timeout_us)
        };
        if ok == 0 || list_ptr.is_null() || list_count <= 0 {
            if !list_ptr.is_null() { unsafe { ffi::FreeMemory(list_ptr as *mut _) }; }
            return Err(DarraError::SoeFailed(0));
        }
        let idns = unsafe {
            std::slice::from_raw_parts(list_ptr, list_count as usize).to_vec()
        };
        unsafe { ffi::FreeMemory(list_ptr as *mut _) };
        Ok(idns)
    }

    pub fn foe_read(&self, filename: &str, password: u32, timeout_us: i32) -> Result<Vec<u8>> {
        let c_filename = CString::new(filename)
            .map_err(|_| DarraError::InvalidParameter(obfstr::obfstr!("文件名包含空字节").into()))?;
        let mut file_ptr: *mut std::ffi::c_void = std::ptr::null_mut();
        let mut file_size: c_int = 0;
        let ok = unsafe {
            ffi::FOERead(self.master_index, self.slave_index, c_filename.as_ptr(),
                         password, &mut file_ptr, &mut file_size, timeout_us)
        };
        if ok == 0 || file_ptr.is_null() {
            return Err(DarraError::FoeFailed(filename.to_string()));
        }
        let data = if file_size > 0 {
            unsafe { std::slice::from_raw_parts(file_ptr as *const u8, file_size as usize).to_vec() }
        } else { Vec::new() };
        unsafe { ffi::FreeMemory(file_ptr) };
        Ok(data)
    }

    pub fn foe_write(&self, filename: &str, password: u32, data: &[u8], timeout_us: i32) -> Result<()> {
        let c_filename = CString::new(filename)
            .map_err(|_| DarraError::InvalidParameter(obfstr::obfstr!("文件名包含空字节").into()))?;
        let ok = unsafe {
            ffi::FOEWrite(self.master_index, self.slave_index, c_filename.as_ptr(),
                          password, data.as_ptr() as *const _, data.len() as c_int, timeout_us)
        };
        if ok != 0 { Ok(()) } else { Err(DarraError::FoeFailed(filename.to_string())) }
    }

    pub fn foe_read_ex(&self, filename: &str, password: u32, timeout_us: i32, options: &mut FoEOptions) -> Result<Vec<u8>> {
        let c_filename = CString::new(filename)
            .map_err(|_| DarraError::InvalidParameter(obfstr::obfstr!("文件名包含空字节").into()))?;
        let mut file_ptr: *mut std::ffi::c_void = std::ptr::null_mut();
        let mut file_size: c_int = 0;
        let ok = unsafe {
            ffi::FOEReadEx(self.master_index, self.slave_index, c_filename.as_ptr(),
                           password, &mut file_ptr, &mut file_size, timeout_us, options)
        };
        if ok == 0 || file_ptr.is_null() {
            return Err(DarraError::FoeFailed(filename.to_string()));
        }
        let data = if file_size > 0 {
            unsafe { std::slice::from_raw_parts(file_ptr as *const u8, file_size as usize).to_vec() }
        } else { Vec::new() };
        unsafe { ffi::FreeMemory(file_ptr) };
        Ok(data)
    }

    pub fn foe_write_ex(&self, filename: &str, password: u32, data: &[u8], timeout_us: i32, options: &mut FoEOptions) -> Result<()> {
        let c_filename = CString::new(filename)
            .map_err(|_| DarraError::InvalidParameter(obfstr::obfstr!("文件名包含空字节").into()))?;
        let ok = unsafe {
            ffi::FOEWriteEx(self.master_index, self.slave_index, c_filename.as_ptr(),
                            password, data.as_ptr() as *const _, data.len() as c_int, timeout_us, options)
        };
        if ok != 0 { Ok(()) } else { Err(DarraError::FoeFailed(filename.to_string())) }
    }

    pub fn foe_set_progress_hook(master_index: u16, callback: ffi::FoEProgressCallback) -> Result<()> {
        if unsafe { ffi::FOESetProgressHook(master_index, Some(callback)) } != 0 {
            Ok(())
        } else {
            Err(DarraError::Other(obfstr::obfstr!("设置 FoE 进度回调失败").into()))
        }
    }

    pub fn foe_clear_progress_hook(master_index: u16) -> Result<()> {
        if unsafe { ffi::FOEClearProgressHook(master_index) } != 0 {
            Ok(())
        } else {
            Err(DarraError::Other(obfstr::obfstr!("清除 FoE 进度回调失败").into()))
        }
    }

    pub fn eoe_set_ip(&self, port: u8, ip: u32, subnet: u32, gateway: u32, timeout_us: i32) -> Result<()> {
        let ok = unsafe {
            ffi::EOESetIP(self.master_index, self.slave_index, port, ip, subnet, gateway, timeout_us)
        };
        if ok != 0 { Ok(()) } else { Err(DarraError::EoeFailed) }
    }

    pub fn eoe_ip(&self, port: u8, timeout_us: i32) -> Result<(u32, u32, u32)> {
        let mut ip: u32 = 0;
        let mut subnet: u32 = 0;
        let mut gateway: u32 = 0;
        let ok = unsafe {
            ffi::EOEGetIP(self.master_index, self.slave_index, port,
                          &mut ip, &mut subnet, &mut gateway, timeout_us)
        };
        if ok != 0 { Ok((ip, subnet, gateway)) } else { Err(DarraError::EoeFailed) }
    }

    pub fn eoe_send_frame(&self, port: u8, data: &[u8], timeout_us: i32) -> Result<()> {
        let ok = unsafe {
            ffi::EOESendFrame(self.master_index, self.slave_index, port,
                              data.as_ptr(), data.len() as c_int, timeout_us)
        };
        if ok != 0 { Ok(()) } else { Err(DarraError::EoeFailed) }
    }

    pub fn eoe_receive_frame(&self, port: u8, timeout_us: i32) -> Result<Vec<u8>> {
        let mut frame_ptr: *mut std::ffi::c_void = std::ptr::null_mut();
        let mut frame_size: c_int = 0;
        let ok = unsafe {
            ffi::EOEReceiveFrame(self.master_index, self.slave_index, port,
                                 &mut frame_ptr, &mut frame_size, timeout_us)
        };
        if ok == 0 || frame_ptr.is_null() {
            return Err(DarraError::EoeFailed);
        }

        let data = if frame_size > 0 {
            unsafe { std::slice::from_raw_parts(frame_ptr as *const u8, frame_size as usize).to_vec() }
        } else {
            Vec::new()
        };
        unsafe { ffi::FreeMemory(frame_ptr) };
        Ok(data)
    }

    pub fn eoe_set_mac(&self, port: u8, mac: &[u8; 6], timeout_us: i32) -> Result<()> {
        let ok = unsafe {
            ffi::EOESetMAC(self.master_index, self.slave_index, port, mac.as_ptr(), timeout_us)
        };
        if ok != 0 { Ok(()) } else { Err(DarraError::EoeFailed) }
    }

    pub fn eoe_mac(&self, port: u8, timeout_us: i32) -> Result<[u8; 6]> {
        let mut mac = [0u8; 6];
        let ok = unsafe {
            ffi::EOEGetMAC(self.master_index, self.slave_index, port, mac.as_mut_ptr(), timeout_us)
        };
        if ok != 0 { Ok(mac) } else { Err(DarraError::EoeFailed) }
    }

    pub fn eoe_set_dns(&self, port: u8, dns_ip: u32, dns_name: &str, timeout_us: i32) -> Result<()> {
        let c_name = CString::new(dns_name)
            .map_err(|_| DarraError::InvalidParameter(obfstr::obfstr!("DNS 名称包含空字节").into()))?;
        let ok = unsafe {
            ffi::EOESetDNS(self.master_index, self.slave_index, port, dns_ip, c_name.as_ptr(), timeout_us)
        };
        if ok != 0 { Ok(()) } else { Err(DarraError::EoeFailed) }
    }

    pub fn eoe_dns(&self, port: u8, timeout_us: i32) -> Result<(u32, String)> {
        let mut dns_ip: u32 = 0;
        let mut dns_name_buf = [0i8; 256];
        let ok = unsafe {
            ffi::EOEGetDNS(self.master_index, self.slave_index, port,
                           &mut dns_ip, dns_name_buf.as_mut_ptr(), timeout_us)
        };
        if ok == 0 { return Err(DarraError::EoeFailed); }
        let name = unsafe { std::ffi::CStr::from_ptr(dns_name_buf.as_ptr()) }
            .to_string_lossy().into_owned();
        Ok((dns_ip, name))
    }

    pub fn eoe_full_param(&self, port: u8, timeout_us: i32) -> Result<(u32, u32, u32, [u8; 6], u32, String)> {
        let mut ip: u32 = 0;
        let mut subnet: u32 = 0;
        let mut gateway: u32 = 0;
        let mut mac = [0u8; 6];
        let mut dns_ip: u32 = 0;
        let mut dns_name_buf = [0i8; 256];
        let ok = unsafe {
            ffi::EOEGetFullParam(
                self.master_index, self.slave_index, port,
                &mut ip, &mut subnet, &mut gateway,
                mac.as_mut_ptr(), &mut dns_ip, dns_name_buf.as_mut_ptr(), timeout_us,
            )
        };
        if ok == 0 { return Err(DarraError::EoeFailed); }
        let name = unsafe { std::ffi::CStr::from_ptr(dns_name_buf.as_ptr()) }
            .to_string_lossy().into_owned();
        Ok((ip, subnet, gateway, mac, dns_ip, name))
    }

    pub fn eoe_set_full_param(
        &self, port: u8, ip: u32, subnet: u32, gateway: u32,
        mac: &[u8; 6], dns_ip: u32, dns_name: &str, timeout_us: i32,
    ) -> Result<()> {
        let c_name = CString::new(dns_name)
            .map_err(|_| DarraError::InvalidParameter(obfstr::obfstr!("DNS 名称包含空字节").into()))?;
        let ok = unsafe {
            ffi::EOESetFullParam(
                self.master_index, self.slave_index, port,
                ip, subnet, gateway, mac.as_ptr(), dns_ip, c_name.as_ptr(), timeout_us,
            )
        };
        if ok != 0 { Ok(()) } else { Err(DarraError::EoeFailed) }
    }

    pub fn eoe_set_address_filter(&self, port: u8, mac_filters: &[u8], filter_count: u8, timeout_us: i32) -> Result<()> {
        let ok = unsafe {
            ffi::EOESetAddressFilter(
                self.master_index, self.slave_index, port,
                filter_count, mac_filters.as_ptr(), timeout_us,
            )
        };
        if ok != 0 { Ok(()) } else { Err(DarraError::EoeFailed) }
    }

    pub fn eoe_address_filter(&self, port: u8, max_filters: i32, timeout_us: i32) -> Result<(u8, Vec<u8>)> {
        let mut filter_count: u8 = 0;
        let buf_size = (max_filters as usize) * 6;
        let mut mac_buf = vec![0u8; buf_size];
        let ok = unsafe {
            ffi::EOEGetAddressFilter(
                self.master_index, self.slave_index, port,
                &mut filter_count, mac_buf.as_mut_ptr(), max_filters, timeout_us,
            )
        };
        if ok == 0 { return Err(DarraError::EoeFailed); }
        mac_buf.truncate((filter_count as usize) * 6);
        Ok((filter_count, mac_buf))
    }

    pub fn aoe_read_write(&self, index_group: u32, index_offset: u32, read_length: u32, write_data: &[u8], timeout_us: i32) -> Result<Vec<u8>> {
        let mut read_ptr: *mut std::ffi::c_void = std::ptr::null_mut();
        let mut bytes_read: u32 = 0;
        let ok = unsafe {
            ffi::AOEReadWrite(self.master_index, self.slave_index, index_group, index_offset,
                              read_length, write_data.len() as u32, write_data.as_ptr(),
                              &mut read_ptr, &mut bytes_read, timeout_us)
        };
        if ok == 0 { return Err(DarraError::AoeFailed); }
        let data = if !read_ptr.is_null() && bytes_read > 0 {
            let d = unsafe { std::slice::from_raw_parts(read_ptr as *const u8, bytes_read as usize).to_vec() };
            unsafe { ffi::FreeMemory(read_ptr) };
            d
        } else { Vec::new() };
        Ok(data)
    }

    pub fn aoe_read_device_info(&self, timeout_us: i32) -> Result<(u8, u8, u16, String)> {
        let mut major: u8 = 0;
        let mut minor: u8 = 0;
        let mut build: u16 = 0;
        let mut name_buf = [0u8; 64];
        let ok = unsafe {
            ffi::AOEReadDeviceInfo(self.master_index, self.slave_index,
                                   &mut major, &mut minor, &mut build,
                                   name_buf.as_mut_ptr(), 64, timeout_us)
        };
        if ok == 0 { return Err(DarraError::AoeFailed); }
        let name_len = name_buf.iter().position(|&b| b == 0).unwrap_or(64);

        let name = crate::utils::help::decode_ethercat_string(&name_buf[..name_len]);
        Ok((major, minor, build, name))
    }

    pub fn aoe_read_state(&self, timeout_us: i32) -> Result<(u16, u16)> {
        let mut ads_state: u16 = 0;
        let mut device_state: u16 = 0;
        let ok = unsafe {
            ffi::AOEReadState(self.master_index, self.slave_index,
                              &mut ads_state, &mut device_state, timeout_us)
        };
        if ok != 0 { Ok((ads_state, device_state)) } else { Err(DarraError::AoeFailed) }
    }

    pub fn aoe_write_control(&self, ads_state: u16, device_state: u16, data: &[u8], timeout_us: i32) -> Result<()> {
        let ok = unsafe {
            ffi::AOEWriteControl(self.master_index, self.slave_index,
                                 ads_state, device_state, data.as_ptr(), data.len() as c_int, timeout_us)
        };
        if ok != 0 { Ok(()) } else { Err(DarraError::AoeFailed) }
    }

    pub fn aoe_add_notification(
        &self, index_group: u32, index_offset: u32, length: u32,
        trans_mode: u32, max_delay: u32, cycle_time: u32, timeout_us: i32,
    ) -> Result<u32> {
        let mut handle: u32 = 0;
        let ok = unsafe {
            ffi::AOEAddNotification(self.master_index, self.slave_index,
                                    index_group, index_offset, length,
                                    trans_mode, max_delay, cycle_time,
                                    &mut handle, timeout_us)
        };
        if ok != 0 { Ok(handle) } else { Err(DarraError::AoeFailed) }
    }

    pub fn aoe_del_notification(&self, handle: u32, timeout_us: i32) -> Result<()> {
        let ok = unsafe {
            ffi::AOEDelNotification(self.master_index, self.slave_index, handle, timeout_us)
        };
        if ok != 0 { Ok(()) } else { Err(DarraError::AoeFailed) }
    }

    pub fn aoe_set_config(&self, target_net_id: &[u8; 6], target_port: u16, source_net_id: &[u8; 6], source_port: u16) -> Result<()> {
        let ok = unsafe {
            ffi::AOESetConfig(self.master_index, self.slave_index,
                              target_net_id.as_ptr(), target_port,
                              source_net_id.as_ptr(), source_port)
        };
        if ok != 0 { Ok(()) } else { Err(DarraError::AoeFailed) }
    }

    pub fn aoe_config(&self) -> Result<([u8; 6], u16, [u8; 6], u16)> {
        let mut target_net_id = [0u8; 6];
        let mut target_port: u16 = 0;
        let mut source_net_id = [0u8; 6];
        let mut source_port: u16 = 0;
        let ok = unsafe {
            ffi::AOEGetConfig(self.master_index, self.slave_index,
                              target_net_id.as_mut_ptr(), &mut target_port,
                              source_net_id.as_mut_ptr(), &mut source_port)
        };
        if ok != 0 { Ok((target_net_id, target_port, source_net_id, source_port)) }
        else { Err(DarraError::AoeFailed) }
    }

    pub fn aoe_send_command(&self, target_port: u16, command_id: u16, command_data: &[u8], timeout_us: i32) -> Result<Vec<u8>> {
        let mut response_ptr: *mut std::ffi::c_void = std::ptr::null_mut();
        let mut response_size: u32 = 0;
        let ok = unsafe {
            ffi::AOESendCommand(
                self.master_index, self.slave_index, target_port, command_id,
                command_data.as_ptr(), command_data.len() as u32,
                &mut response_ptr, &mut response_size, timeout_us,
            )
        };
        if ok == 0 { return Err(DarraError::AoeFailed); }
        let data = if !response_ptr.is_null() && response_size > 0 {
            let d = unsafe { std::slice::from_raw_parts(response_ptr as *const u8, response_size as usize).to_vec() };
            unsafe { ffi::FreeMemory(response_ptr) };
            d
        } else { Vec::new() };
        Ok(data)
    }

    pub fn voe_is_supported(&self) -> bool {
        (unsafe { ffi::VOEIsSupported(self.master_index, self.slave_index) }) != 0
    }

    pub fn voe_send(&self, vendor_id: u32, vendor_type: u16, data: &[u8], timeout_us: i32) -> Result<()> {
        let ok = unsafe {
            ffi::VOESend(self.master_index, self.slave_index, vendor_id, vendor_type,
                         data.as_ptr(), data.len() as c_int, timeout_us)
        };
        if ok != 0 { Ok(()) } else { Err(DarraError::VoeFailed) }
    }

    pub fn voe_receive(&self, timeout_us: i32) -> Result<(u32, u16, Vec<u8>)> {
        let mut vendor_id: u32 = 0;
        let mut vendor_type: u16 = 0;
        let mut data_ptr: *mut std::ffi::c_void = std::ptr::null_mut();
        let mut data_size: c_int = 0;
        let ok = unsafe {
            ffi::VOEReceive(self.master_index, self.slave_index,
                            &mut vendor_id, &mut vendor_type, &mut data_ptr, &mut data_size, timeout_us)
        };
        if ok == 0 { return Err(DarraError::VoeFailed); }
        let data = if !data_ptr.is_null() && data_size > 0 {
            let d = unsafe { std::slice::from_raw_parts(data_ptr as *const u8, data_size as usize).to_vec() };
            unsafe { ffi::FreeMemory(data_ptr) };
            d
        } else { Vec::new() };
        Ok((vendor_id, vendor_type, data))
    }

    pub fn voe_send_raw(&self, frame_data: &[u8], timeout_us: i32) -> Result<()> {
        let ok = unsafe {
            ffi::VOESendRaw(self.master_index, self.slave_index,
                            frame_data.as_ptr(), frame_data.len() as c_int, timeout_us)
        };
        if ok != 0 { Ok(()) } else { Err(DarraError::VoeFailed) }
    }

    pub fn voe_receive_raw(&self, timeout_us: i32) -> Result<Vec<u8>> {
        let mut data_ptr: *mut std::ffi::c_void = std::ptr::null_mut();
        let mut data_size: c_int = 0;
        let ok = unsafe {
            ffi::VOEReceiveRaw(self.master_index, self.slave_index,
                               &mut data_ptr, &mut data_size, timeout_us)
        };
        if ok == 0 { return Err(DarraError::VoeFailed); }
        let data = if !data_ptr.is_null() && data_size > 0 {
            let d = unsafe { std::slice::from_raw_parts(data_ptr as *const u8, data_size as usize).to_vec() };
            unsafe { ffi::FreeMemory(data_ptr) };
            d
        } else { Vec::new() };
        Ok(data)
    }

    pub fn add_startup_parameter(
        &self, index: u16, sub_index: u8, data: &[u8],
        transition: u8, timing: u8, complete_access: bool,
    ) -> Result<()> {
        let mut param = ffi::StartupParam {
            index, sub_index, data: [0u8; 256],
            data_len: data.len().min(256) as u16,
            transition, timing, priority: 0,
            complete_access: if complete_access { 1 } else { 0 },
            is_register_write: 0,
        };
        let copy_len = data.len().min(256);
        param.data[..copy_len].copy_from_slice(&data[..copy_len]);
        let ret = unsafe { ffi::AddStartupParameter(self.master_index, self.slave_index, &param) };
        if ret >= 0 { Ok(()) }
        else { Err(DarraError::Other(format!("添加启动参数失败 (返回码: {})", ret))) }
    }

    pub fn clear_startup_parameters(&self) -> Result<()> {
        let ret = unsafe { ffi::ClearStartupParameters(self.master_index, self.slave_index) };
        if ret >= 0 { Ok(()) } else { Err(DarraError::Other(obfstr::obfstr!("清除启动参数失败").into())) }
    }

    pub fn startup_parameter_count(&self) -> i32 {
        unsafe { ffi::GetStartupParameterCount(self.master_index, self.slave_index) }
    }

    pub fn apply_startup_parameters(&self, transition: u8, timing: u8) -> Result<i32> {
        let ret = unsafe {
            ffi::DarraCoreInvoke(
                self.master_index,
                ffi::CORE_OP_26,
                self.slave_index as u32,
                1u32 << transition,
                1u32 << timing)
        };
        if ret >= 0 { Ok(ret) }
        else { Err(DarraError::Other(format!("执行启动参数失败 (返回码: {})", ret))) }
    }

    pub fn write_output(&self, data: &[u8]) {
        unsafe { ffi::WriteSlaveOutput(self.master_index, self.slave_index, data.as_ptr(), data.len() as u32) };
    }

    pub fn write_output_byte(&self, offset: u32, value: u8) {
        unsafe { ffi::WriteSlaveOutputByte(self.master_index, self.slave_index, offset, value) };
    }

    pub fn cia402_state(&self) -> CiA402State {
        let sw = unsafe { ffi::CiA402_ReadStatusWord(self.master_index, self.slave_index) };
        let raw = unsafe { ffi::CiA402_ParseState(sw) };
        CiA402State::from_raw(raw)
    }

    pub fn cia402_enable(&self) -> Result<()> {
        let ret = unsafe { ffi::CiA402_Enable(self.master_index, self.slave_index, 50) };
        if ret >= 0 { Ok(()) }
        else { Err(DarraError::CiA402Failed(format!("使能失败 (返回码: {})", ret))) }
    }

    pub fn cia402_set_mode(&self, mode: i8) -> Result<()> {
        let ret = unsafe { ffi::CiA402_SetMode(self.master_index, self.slave_index, mode) };
        if ret >= 0 { Ok(()) }
        else { Err(DarraError::CiA402Failed(format!("设置模式失败 (返回码: {})", ret))) }
    }

    pub fn cia402_mode(&self) -> i8 {
        unsafe { ffi::CiA402_GetMode(self.master_index, self.slave_index) }
    }

    pub fn cia402_status_word(&self) -> u16 {
        unsafe { ffi::CiA402_ReadStatusWord(self.master_index, self.slave_index) }
    }

    pub fn cia402_write_control_word(&self, cw: u16) -> Result<()> {
        let ret = unsafe { ffi::CiA402_WriteControlWord(self.master_index, self.slave_index, cw) };
        if ret >= 0 { Ok(()) }
        else { Err(DarraError::CiA402Failed("写入控制字失败".into())) }
    }

    pub fn cia402_fault_reset(&self) -> Result<()> {
        let ret = unsafe { ffi::CiA402_FaultReset(self.master_index, self.slave_index) };
        if ret >= 0 { Ok(()) } else { Err(DarraError::CiA402Failed("故障复位失败".into())) }
    }

    pub fn emcy_history(&self) -> Vec<EmcyRecord> {
        let mut buf = vec![
            EmcyRecord { error_code: 0, error_register: 0, data: [0u8; 5], slave_index: 0, timestamp_ms: 0 };
            64
        ];
        let count = unsafe {
            ffi::EmcyGetHistory(self.master_index, self.slave_index, buf.as_mut_ptr(), 64)
        };
        if count > 0 { buf.truncate(count as usize); buf }
        else { Vec::new() }
    }

    pub fn emcy_count(&self) -> usize {
        let count = unsafe { ffi::EmcyGetCount(self.master_index, self.slave_index) };
        if count > 0 { count as usize } else { 0 }
    }

    pub fn clear_emcy(&self) {
        unsafe { ffi::EmcyClearHistory(self.master_index, self.slave_index) };
    }

    pub fn read_input_u8(&self, offset: u32) -> u8 {
        unsafe { ffi::PDOReadInputU8(self.master_index, self.slave_index, offset) }
    }

    pub fn read_input_i8(&self, offset: u32) -> i8 {
        unsafe { ffi::PDOReadInputU8(self.master_index, self.slave_index, offset) as i8 }
    }

    pub fn read_input_i16(&self, offset: u32) -> i16 {
        unsafe { ffi::PDOReadInputI16(self.master_index, self.slave_index, offset) }
    }

    pub fn read_input_u16(&self, offset: u32) -> u16 {
        unsafe { ffi::PDOReadInputU16(self.master_index, self.slave_index, offset) }
    }

    pub fn read_input_i32(&self, offset: u32) -> i32 {
        unsafe { ffi::PDOReadInputI32(self.master_index, self.slave_index, offset) }
    }

    pub fn read_input_u32(&self, offset: u32) -> u32 {
        unsafe { ffi::PDOReadInputU32(self.master_index, self.slave_index, offset) }
    }

    pub fn read_input_f32(&self, offset: u32) -> f32 {
        unsafe { ffi::PDOReadInputF32(self.master_index, self.slave_index, offset) }
    }

    pub fn write_output_u8(&self, offset: u32, value: u8) {
        let _ = unsafe { ffi::PDOWriteOutputU8(self.master_index, self.slave_index, offset, value) };
    }

    pub fn write_output_i8(&self, offset: u32, value: i8) {
        let _ = unsafe { ffi::PDOWriteOutputU8(self.master_index, self.slave_index, offset, value as u8) };
    }

    pub fn write_output_i16(&self, offset: u32, value: i16) {
        let _ = unsafe { ffi::PDOWriteOutputI16(self.master_index, self.slave_index, offset, value) };
    }

    pub fn write_output_u16(&self, offset: u32, value: u16) {
        let _ = unsafe { ffi::PDOWriteOutputU16(self.master_index, self.slave_index, offset, value) };
    }

    pub fn write_output_i32(&self, offset: u32, value: i32) {
        let _ = unsafe { ffi::PDOWriteOutputI32(self.master_index, self.slave_index, offset, value) };
    }

    pub fn write_output_u32(&self, offset: u32, value: u32) {
        let _ = unsafe { ffi::PDOWriteOutputU32(self.master_index, self.slave_index, offset, value) };
    }

    pub fn write_output_f32(&self, offset: u32, value: f32) {
        let _ = unsafe { ffi::PDOWriteOutputF32(self.master_index, self.slave_index, offset, value) };
    }

    pub fn pdo_read_input_u8(&self, offset: u32) -> u8 {
        unsafe { ffi::PDOReadInputU8(self.master_index, self.slave_index, offset) }
    }

    pub fn pdo_read_input_i16(&self, offset: u32) -> i16 {
        unsafe { ffi::PDOReadInputI16(self.master_index, self.slave_index, offset) }
    }

    pub fn pdo_read_input_u16(&self, offset: u32) -> u16 {
        unsafe { ffi::PDOReadInputU16(self.master_index, self.slave_index, offset) }
    }

    pub fn pdo_read_input_i32(&self, offset: u32) -> i32 {
        unsafe { ffi::PDOReadInputI32(self.master_index, self.slave_index, offset) }
    }

    pub fn pdo_read_input_u32(&self, offset: u32) -> u32 {
        unsafe { ffi::PDOReadInputU32(self.master_index, self.slave_index, offset) }
    }

    pub fn pdo_read_input_f32(&self, offset: u32) -> f32 {
        unsafe { ffi::PDOReadInputF32(self.master_index, self.slave_index, offset) }
    }

    pub fn pdo_write_output_u8(&self, offset: u32, val: u8) -> Result<()> {
        let ret = unsafe { ffi::PDOWriteOutputU8(self.master_index, self.slave_index, offset, val) };
        if ret >= 0 { Ok(()) } else { Err(DarraError::Other(format!("PDO 写入 u8 失败 (返回码: {})", ret))) }
    }

    pub fn pdo_write_output_i16(&self, offset: u32, val: i16) -> Result<()> {
        let ret = unsafe { ffi::PDOWriteOutputI16(self.master_index, self.slave_index, offset, val) };
        if ret >= 0 { Ok(()) } else { Err(DarraError::Other(format!("PDO 写入 i16 失败 (返回码: {})", ret))) }
    }

    pub fn pdo_write_output_u16(&self, offset: u32, val: u16) -> Result<()> {
        let ret = unsafe { ffi::PDOWriteOutputU16(self.master_index, self.slave_index, offset, val) };
        if ret >= 0 { Ok(()) } else { Err(DarraError::Other(format!("PDO 写入 u16 失败 (返回码: {})", ret))) }
    }

    pub fn pdo_write_output_i32(&self, offset: u32, val: i32) -> Result<()> {
        let ret = unsafe { ffi::PDOWriteOutputI32(self.master_index, self.slave_index, offset, val) };
        if ret >= 0 { Ok(()) } else { Err(DarraError::Other(format!("PDO 写入 i32 失败 (返回码: {})", ret))) }
    }

    pub fn pdo_write_output_u32(&self, offset: u32, val: u32) -> Result<()> {
        let ret = unsafe { ffi::PDOWriteOutputU32(self.master_index, self.slave_index, offset, val) };
        if ret >= 0 { Ok(()) } else { Err(DarraError::Other(format!("PDO 写入 u32 失败 (返回码: {})", ret))) }
    }

    pub fn pdo_write_output_f32(&self, offset: u32, val: f32) -> Result<()> {
        let ret = unsafe { ffi::PDOWriteOutputF32(self.master_index, self.slave_index, offset, val) };
        if ret >= 0 { Ok(()) } else { Err(DarraError::Other(format!("PDO 写入 f32 失败 (返回码: {})", ret))) }
    }

    pub fn link_quality(&self) -> i16 {
        unsafe { ffi::GetSlaveLinkQuality(self.master_index, self.slave_index) }
    }

    pub fn reset_port_error_counters(&self) -> bool {
        (unsafe { ffi::ResetSlavePortErrorCounters(self.master_index, self.slave_index) }) != 0
    }

    pub fn read_port_error_counters(&self) -> Option<([u8; 4], [u8; 4], [u8; 4])> {
        let mut rx_error = [0u8; 4];
        let mut invalid_frame = [0u8; 4];
        let mut lost_link = [0u8; 4];
        let ok = unsafe {
            ffi::ReadSlavePortErrorCounters(
                self.master_index, self.slave_index,
                rx_error.as_mut_ptr(), invalid_frame.as_mut_ptr(), lost_link.as_mut_ptr(),
            )
        };
        if ok != 0 { Some((rx_error, invalid_frame, lost_link)) } else { None }
    }

    pub fn port_error_stats(&self) -> *const std::ffi::c_void {
        unsafe { ffi::GetSlavePortErrorStats(self.master_index, self.slave_index) }
    }

    #[cfg(any(debug_assertions, feature = "internal-debug-probes"))]
    pub fn debug_dc(&self) {
        unsafe { ffi::DebugSlaveHasDC(self.master_index, self.slave_index) };
    }

    pub fn ibits(&self) -> u16 { self.input_bits() }

    pub fn ibytes(&self) -> u32 { self.input_bytes() }

    pub fn obits(&self) -> u16 { self.output_bits() }

    pub fn obytes(&self) -> u32 { self.output_bytes() }

    pub fn ioffset(&self) -> u32 { self.input_offset() }

    pub fn ooffset(&self) -> u32 { self.output_offset() }

    pub fn istartbit(&self) -> u8 { self.input_startbit() }

    pub fn ostartbit(&self) -> u8 { self.output_startbit() }

    pub fn revision(&self) -> u32 { self.rev_id() }

    pub fn pdelay(&self) -> i32 {
        self.read_slave().map(|s| s.dc.propagation_delay).unwrap_or(0)
    }

    pub fn parent_station(&self) -> u16 {
        self.read_slave().map(|s| s.topo.parent).unwrap_or(0)
    }

    pub fn mbx_proto(&self) -> u16 { self.mbx_protocol() }

    pub fn has_esi(&self) -> bool {
        unsafe { ffi::GetSlaveHasEsi(self.master_index, self.slave_index) != 0 }
    }

    pub fn set_esi_file(&self, name: &str) -> bool {
        let c_name = match CString::new(name) {
            Ok(s) => s,
            Err(_) => return false,
        };
        unsafe { ffi::SetSlaveEsiFile(self.master_index, self.slave_index, c_name.as_ptr()) != 0 }
    }

    pub fn esi_version(&self) -> String {
        let mut buf = [0i8; 64];
        let ok = unsafe {
            ffi::GetSlaveEsiVersion(self.master_index, self.slave_index, buf.as_mut_ptr(), 64)
        };
        if ok != 0 {
            unsafe { std::ffi::CStr::from_ptr(buf.as_ptr()) }
                .to_string_lossy().into_owned()
        } else {
            String::new()
        }
    }

    pub fn has_mdp(&self) -> bool {
        unsafe { ffi::GetSlaveHasMDP(self.master_index, self.slave_index) != 0 }
    }

    pub fn vendor_name(&self) -> String {
        let mut buf = [0i8; 128];
        let ok = unsafe {
            ffi::GetSlaveVendorName(self.master_index, self.slave_index, buf.as_mut_ptr(), 128)
        };
        if ok != 0 {
            unsafe { std::ffi::CStr::from_ptr(buf.as_ptr()) }
                .to_string_lossy().into_owned()
        } else {
            String::new()
        }
    }

    pub fn redundancy_activated(&self) -> bool {
        unsafe { ffi::GetSlaveRedundancyActivated(self.master_index, self.slave_index) != 0 }
    }

    pub fn primary_link_broken(&self) -> bool {
        unsafe { ffi::GetSlavePrimaryLinkBroken(self.master_index, self.slave_index) != 0 }
    }

    pub fn secondary_link_broken(&self) -> bool {
        unsafe { ffi::GetSlaveSecondaryLinkBroken(self.master_index, self.slave_index) != 0 }
    }

    pub fn child_count(&self) -> u16 {
        let mut buf = vec![0u16; 64];
        let count = unsafe {
            ffi::TopologyGetChildren(self.master_index, self.slave_index, buf.as_mut_ptr(), 64)
        };
        if count > 0 { count as u16 } else { 0 }
    }

    pub fn children(&self) -> Vec<u16> {
        let mut buf = vec![0u16; 64];
        let count = unsafe {
            ffi::TopologyGetChildren(self.master_index, self.slave_index, buf.as_mut_ptr(), 64)
        };
        if count > 0 { buf.truncate(count as usize); buf }
        else { Vec::new() }
    }

    pub fn sync_managers(&self) -> Vec<u8> {
        self.read_slave().map(|s| s.sm_buffer.to_vec()).unwrap_or_default()
    }

    pub fn sync_manager_types(&self) -> Vec<u8> {
        self.read_slave().map(|s| s.sm_fmmu.sm_type.to_vec()).unwrap_or_default()
    }

    pub fn fmmus(&self) -> Vec<u8> {
        self.read_slave().map(|s| s.fmmu_buffer.to_vec()).unwrap_or_default()
    }

    pub fn fmmus_raw(&self) -> Vec<u8> {
        self.read_register(0x0600, 256).unwrap_or_default()
    }

    pub fn sync_managers_raw(&self) -> Vec<u8> {
        self.read_register(0x0800, 128).unwrap_or_default()
    }

    pub fn refresh_pointer(&self) {

    }

    pub fn cia401(&self) -> crate::slave::cia401::CiA401 {
        crate::slave::cia401::CiA401::new(*self)
    }

    pub fn esi(&self) -> crate::slave::esi::EsiLoader {
        crate::slave::esi::EsiLoader::new(*self)
    }

    pub fn events(&self) -> crate::master::events::SlaveEvents {
        crate::master::events::SlaveEvents::new(self.master_index, self.slave_index)
    }

    pub fn voe(&self) -> crate::slave::voe::VoEInstance {
        crate::slave::voe::VoEInstance::new(self.master_index, self.slave_index)
    }

    pub fn aoe(&self) -> crate::slave::aoe::AoEInstance {
        crate::slave::aoe::AoEInstance::new(self.master_index, self.slave_index)
    }

    pub fn soe(&self) -> crate::slave::soe::SoEInstance {
        crate::slave::soe::SoEInstance::new(self.master_index, self.slave_index, 0)
    }

    pub fn soe_drive(&self, drive_number: u8) -> crate::slave::soe::SoEInstance {
        crate::slave::soe::SoEInstance::new(self.master_index, self.slave_index, drive_number)
    }

    pub fn eoe(&self) -> crate::slave::eoe::EoEInstance {
        crate::slave::eoe::EoEInstance::new(self.master_index, self.slave_index, 0)
    }

    pub fn foe(&self) -> crate::slave::foe::FoEInstance {
        crate::slave::foe::FoEInstance::new(self.master_index, self.slave_index)
    }

    pub fn startup(&self) -> crate::slave::startup::StartupParameterList {
        crate::slave::startup::StartupParameterList::new(self.master_index, self.slave_index)
    }

    pub fn dc(&self) -> crate::slave::dc::SlaveDC {
        crate::slave::dc::SlaveDC::new(self.master_index, self.slave_index)
    }

    pub fn diagnostics(&self) -> crate::slave::slave_stats::SlaveDiagnostics {
        crate::slave::slave_stats::SlaveDiagnostics::new(self.master_index, self.slave_index)
    }

    pub fn topology_info(&self) -> crate::slave::topology::SlaveTopology {
        crate::slave::topology::SlaveTopology::build(self.master_index)
    }

    pub fn configure_from_esi(&self) -> Result<()> {
        let ret = unsafe {
            ffi::DarraCoreInvoke(
                self.master_index,
                ffi::CORE_OP_25,
                self.slave_index as u32,
                0,
                0)
        };
        if ret >= 0 {
            Ok(())
        } else {
            Err(DarraError::Other(obfstr::obfstr!("ESI 自动配置失败").into()))
        }
    }

    pub fn config_by_esi(&self) -> Result<()> {

        self.configure_from_esi()?;

        Ok(())
    }

    #[doc(hidden)]
    pub fn voe_instance(&self) -> crate::slave::voe::VoEInstance { self.voe() }
    #[doc(hidden)]
    pub fn aoe_instance(&self) -> crate::slave::aoe::AoEInstance { self.aoe() }
    #[doc(hidden)]
    pub fn startup_params(&self) -> crate::slave::startup::StartupParameterList { self.startup() }
}

impl std::fmt::Display for Slave {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let name = self.name();
        let vid = self.vendor_id();
        let pid = self.product_id();
        write!(f, "Slave[{}] {} ({:#010x}:{:#010x})", self.slave_index, name, vid, pid)
    }
}