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

use std::collections::HashMap;
use crate::data::error::{DarraError, Result};
use crate::utils::ffi::{self, AOENotificationCallback};
use std::os::raw::c_void;

const DEFAULT_TIMEOUT_US: i32 = 500_000;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u32)]
pub enum AoEResultCode {
    NoError                       = 0x0000,
    InternalError                 = 0x0001,
    NoRealTime                    = 0x0002,
    AllocationLockedMemoryError   = 0x0003,
    InsertMailboxError            = 0x0004,
    WrongReceiveHMsg              = 0x0005,
    TargetPortNotFound            = 0x0006,
    TargetMachineNotFound         = 0x0007,
    UnknownCommandId              = 0x0008,
    BadTaskId                     = 0x0009,
    NoIO                          = 0x000A,
    UnknownAmsCommand             = 0x000B,
    Win32Error                    = 0x000C,
    PortNotConnected              = 0x000D,
    InvalidAmsLength              = 0x000E,
    InvalidAmsNetId               = 0x000F,
    LowInstLevel                  = 0x0010,
    NoDebugAvailable              = 0x0011,
    PortDisabled                  = 0x0012,
    PortAlreadyConnected          = 0x0013,
    AmsSyncWin32Error             = 0x0014,
    AmsSyncTimeout                = 0x0015,
    AmsSyncAmsError               = 0x0016,
    AmsSyncNoIndexMap             = 0x0017,
    InvalidAmsPort                = 0x0018,
    NoMemory                      = 0x0019,
    TcpSendError                  = 0x001A,
    HostUnreachable               = 0x001B,
    InvalidAmsFragment            = 0x001C,
    WsaServiceNotStarted          = 0x274D,
    DeviceInvalidGroup            = 0x0700,
    DeviceInvalidOffset           = 0x0701,
    DeviceInvalidAccess           = 0x0702,
    DeviceInvalidSize             = 0x0703,
    DeviceInvalidData             = 0x0704,
    DeviceNotReady                = 0x0705,
    DeviceBusy                    = 0x0706,
    DeviceInvalidContext          = 0x0707,
    DeviceNotFound                = 0x0708,
    DeviceAlreadyExists           = 0x0709,
    DeviceSymbolNotFound          = 0x070A,
    DeviceSymbolVersionInvalid    = 0x070B,
    DeviceInvalidState            = 0x070C,
    DeviceTimeout                 = 0x0712,
    DeviceNoInterfaceQuery        = 0x0713,
    DeviceInvalidInterface        = 0x0714,
    DeviceInvalidClassId          = 0x0715,
    DeviceInvalidObjectId         = 0x0716,
    DevicePending                 = 0x0717,
    DeviceAborted                 = 0x0718,
    DeviceWarning                 = 0x0719,
    DeviceInvalidArrayIndex       = 0x071A,
    DeviceSymbolNotActive         = 0x071B,
    DeviceAccessDenied            = 0x071C,
    ClientError                   = 0x0740,
    ClientInvalidParam            = 0x0741,
    ClientListEmpty               = 0x0742,
    ClientVarAlreadyInUse         = 0x0743,
    ClientInvokeTimeout           = 0x0744,
    ClientPortNotOpen             = 0x0745,
    ClientNotStarted              = 0x0746,
    ClientQueueFull               = 0x0750,
}

impl AoEResultCode {

    pub fn from_u32(code: u32) -> Option<Self> {
        Some(match code {
            0x0000 => Self::NoError,
            0x0001 => Self::InternalError,
            0x0002 => Self::NoRealTime,
            0x0003 => Self::AllocationLockedMemoryError,
            0x0004 => Self::InsertMailboxError,
            0x0005 => Self::WrongReceiveHMsg,
            0x0006 => Self::TargetPortNotFound,
            0x0007 => Self::TargetMachineNotFound,
            0x0008 => Self::UnknownCommandId,
            0x0009 => Self::BadTaskId,
            0x000A => Self::NoIO,
            0x000B => Self::UnknownAmsCommand,
            0x000C => Self::Win32Error,
            0x000D => Self::PortNotConnected,
            0x000E => Self::InvalidAmsLength,
            0x000F => Self::InvalidAmsNetId,
            0x0010 => Self::LowInstLevel,
            0x0011 => Self::NoDebugAvailable,
            0x0012 => Self::PortDisabled,
            0x0013 => Self::PortAlreadyConnected,
            0x0014 => Self::AmsSyncWin32Error,
            0x0015 => Self::AmsSyncTimeout,
            0x0016 => Self::AmsSyncAmsError,
            0x0017 => Self::AmsSyncNoIndexMap,
            0x0018 => Self::InvalidAmsPort,
            0x0019 => Self::NoMemory,
            0x001A => Self::TcpSendError,
            0x001B => Self::HostUnreachable,
            0x001C => Self::InvalidAmsFragment,
            0x274D => Self::WsaServiceNotStarted,
            0x0700 => Self::DeviceInvalidGroup,
            0x0701 => Self::DeviceInvalidOffset,
            0x0702 => Self::DeviceInvalidAccess,
            0x0703 => Self::DeviceInvalidSize,
            0x0704 => Self::DeviceInvalidData,
            0x0705 => Self::DeviceNotReady,
            0x0706 => Self::DeviceBusy,
            0x0707 => Self::DeviceInvalidContext,
            0x0708 => Self::DeviceNotFound,
            0x0709 => Self::DeviceAlreadyExists,
            0x070A => Self::DeviceSymbolNotFound,
            0x070B => Self::DeviceSymbolVersionInvalid,
            0x070C => Self::DeviceInvalidState,
            0x0712 => Self::DeviceTimeout,
            0x0713 => Self::DeviceNoInterfaceQuery,
            0x0714 => Self::DeviceInvalidInterface,
            0x0715 => Self::DeviceInvalidClassId,
            0x0716 => Self::DeviceInvalidObjectId,
            0x0717 => Self::DevicePending,
            0x0718 => Self::DeviceAborted,
            0x0719 => Self::DeviceWarning,
            0x071A => Self::DeviceInvalidArrayIndex,
            0x071B => Self::DeviceSymbolNotActive,
            0x071C => Self::DeviceAccessDenied,
            0x0740 => Self::ClientError,
            0x0741 => Self::ClientInvalidParam,
            0x0742 => Self::ClientListEmpty,
            0x0743 => Self::ClientVarAlreadyInUse,
            0x0744 => Self::ClientInvokeTimeout,
            0x0745 => Self::ClientPortNotOpen,
            0x0746 => Self::ClientNotStarted,
            0x0750 => Self::ClientQueueFull,
            _ => return None,
        })
    }

    pub fn value(self) -> u32 { self as u32 }
}

pub struct AoEInstance {
    master_index: u16,
    slave_index: u16,
}

impl AoEInstance {

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

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

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

    pub fn is_supported(&self) -> bool {
        let proto = unsafe { ffi::GetSlaveMailboxProto(self.master_index, self.slave_index) };
        (proto & 0x01) != 0
    }

    pub fn read_write(
        &self,
        index_group: u32,
        index_offset: u32,
        read_length: u32,
        write_data: Option<&[u8]>,
        timeout_us: i32,
    ) -> Result<Option<Vec<u8>>> {
        let write_buf = write_data.unwrap_or(&[]);
        let write_len = write_buf.len() as u32;
        let mut read_ptr: *mut c_void = std::ptr::null_mut();
        let mut bytes_read: u32 = 0;

        let ret = unsafe {
            ffi::AOEReadWrite(
                self.master_index, self.slave_index,
                index_group, index_offset,
                read_length, write_len, write_buf.as_ptr(),
                &mut read_ptr, &mut bytes_read, timeout_us,
            )
        };

        if ret != 0 {
            if !read_ptr.is_null() && bytes_read > 0 {
                let mut data = vec![0u8; bytes_read as usize];
                unsafe {
                    std::ptr::copy_nonoverlapping(read_ptr as *const u8, data.as_mut_ptr(), bytes_read as usize);
                    ffi::FreeMemory(read_ptr);
                }
                Ok(Some(data))
            } else {
                if !read_ptr.is_null() { unsafe { ffi::FreeMemory(read_ptr) }; }
                Ok(None)
            }
        } else {
            Err(DarraError::AoeFailed)
        }
    }

    pub fn read(&self, index_group: u32, index_offset: u32, length: u32) -> Result<Option<Vec<u8>>> {
        self.read_write(index_group, index_offset, length, None, DEFAULT_TIMEOUT_US)
    }

    pub fn read_with_timeout(&self, index_group: u32, index_offset: u32, length: u32, timeout_us: i32) -> Result<Option<Vec<u8>>> {
        self.read_write(index_group, index_offset, length, None, timeout_us)
    }

    pub fn write(&self, index_group: u32, index_offset: u32, data: &[u8]) -> Result<()> {
        let mut read_ptr: *mut c_void = std::ptr::null_mut();
        let mut bytes_read: u32 = 0;
        let ret = unsafe {
            ffi::AOEReadWrite(
                self.master_index, self.slave_index,
                index_group, index_offset,
                0, data.len() as u32, data.as_ptr(),
                &mut read_ptr, &mut bytes_read, DEFAULT_TIMEOUT_US,
            )
        };
        if !read_ptr.is_null() { unsafe { ffi::FreeMemory(read_ptr) }; }
        if ret != 0 { Ok(()) } else { Err(DarraError::AoeFailed) }
    }

    pub fn send_command(
        &self,
        target_port: u16,
        command_id: u16,
        command_data: Option<&[u8]>,
        timeout_us: i32,
    ) -> Result<(bool, Option<Vec<u8>>)> {
        let cmd_data = command_data.unwrap_or(&[]);
        let cmd_size = cmd_data.len() as u32;
        let mut response_ptr: *mut c_void = std::ptr::null_mut();
        let mut response_size: u32 = 0;

        let ret = unsafe {
            ffi::AOESendCommand(
                self.master_index, self.slave_index,
                target_port, command_id,
                cmd_data.as_ptr(), cmd_size,
                &mut response_ptr, &mut response_size, timeout_us,
            )
        };

        if ret != 0 {
            if !response_ptr.is_null() && response_size > 0 {
                let mut data = vec![0u8; response_size as usize];
                unsafe {
                    std::ptr::copy_nonoverlapping(response_ptr as *const u8, data.as_mut_ptr(), response_size as usize);
                    ffi::FreeMemory(response_ptr);
                }
                Ok((true, Some(data)))
            } else {
                if !response_ptr.is_null() { unsafe { ffi::FreeMemory(response_ptr) }; }
                Ok((true, None))
            }
        } else {
            Ok((false, None))
        }
    }

    pub fn read_device_info(&self) -> Result<(u8, u8, u16, String)> {
        let mut major_ver: u8 = 0;
        let mut minor_ver: u8 = 0;
        let mut build: u16 = 0;
        let mut name_buf = vec![0u8; 256];
        let ret = unsafe {
            ffi::AOEReadDeviceInfo(
                self.master_index, self.slave_index,
                &mut major_ver, &mut minor_ver, &mut build,
                name_buf.as_mut_ptr(), name_buf.len() as i32,
                DEFAULT_TIMEOUT_US,
            )
        };
        if ret != 0 {
            let null_pos = name_buf.iter().position(|&b| b == 0).unwrap_or(name_buf.len());

            let name = crate::utils::help::decode_ethercat_string(&name_buf[..null_pos])
                .trim()
                .to_string();
            Ok((major_ver, minor_ver, build, name))
        } else {
            Err(DarraError::AoeFailed)
        }
    }

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

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

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

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

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

    pub fn 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 ret = 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 ret != 0 { Ok((target_net_id, target_port, source_net_id, source_port)) }
        else { Err(DarraError::AoeFailed) }
    }

    pub fn last_error(&self) -> Option<(u32, u16, u16, u16)> {
        let mut error_code: u32 = 0;
        let mut error_class: u16 = 0;
        let mut error_low: u16 = 0;
        let mut last_command: u16 = 0;
        let has_error = unsafe {
            ffi::AOEGetLastError(
                self.master_index, self.slave_index,
                &mut error_code, &mut error_class,
                &mut error_low, &mut last_command,
            )
        };
        if has_error != 0 {
            Some((error_code, error_class, error_low, last_command))
        } else {
            None
        }
    }

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

    pub fn init_slave_net_id(&self, master_net_id: Option<&[u8; 6]>, timeout_us: i32) -> Result<i32> {
        let net_id_ptr = master_net_id.map_or(std::ptr::null(), |id| id.as_ptr());
        let ret = unsafe {
            ffi::AOEInitSlaveNetId(self.master_index, net_id_ptr, timeout_us)
        };
        if ret >= 0 { Ok(ret) } else { Err(DarraError::AoeFailed) }
    }

    pub fn read_write_ex(
        &self,
        index_group: u32,
        index_offset: u32,
        read_length: u32,
        write_data: Option<&[u8]>,
        timeout_us: i32,
    ) -> Result<Option<Vec<u8>>> {
        let write_buf = write_data.unwrap_or(&[]);
        let write_len = write_buf.len() as u32;
        let mut read_ptr: *mut c_void = std::ptr::null_mut();
        let mut actual_read: u32 = 0;

        let ret = unsafe {
            ffi::AOEReadWriteEx(
                self.master_index, self.slave_index,
                index_group, index_offset,
                read_length, write_len, write_buf.as_ptr(),
                &mut read_ptr, &mut actual_read, timeout_us,
            )
        };

        if ret != 0 {
            if !read_ptr.is_null() && actual_read > 0 {
                let mut data = vec![0u8; actual_read as usize];
                unsafe {
                    std::ptr::copy_nonoverlapping(read_ptr as *const u8, data.as_mut_ptr(), actual_read as usize);
                    ffi::FreeMemory(read_ptr);
                }
                Ok(Some(data))
            } else {
                if !read_ptr.is_null() { unsafe { ffi::FreeMemory(read_ptr) }; }
                Ok(None)
            }
        } else {
            Err(DarraError::AoeFailed)
        }
    }

    pub fn send_fragmented(&self, payload: &[u8], packet_number: u16, timeout_us: i32) -> Result<()> {
        let ret = unsafe {
            ffi::AOESendFragmented(
                self.master_index, self.slave_index,
                payload.as_ptr(), payload.len() as u32,
                packet_number, timeout_us,
            )
        };
        if ret != 0 { Ok(()) } else { Err(DarraError::AoeFailed) }
    }

    pub fn recv_reassemble(&self, out_capacity: usize, packet_number: u16, timeout_us: i32) -> Result<Vec<u8>> {
        if out_capacity == 0 {
            return Err(DarraError::InvalidParameter("recv_reassemble: out_capacity 必须 > 0".into()));
        }
        let mut buffer = vec![0u8; out_capacity];
        let mut out_len: u32 = 0;
        let ret = unsafe {
            ffi::AOERecvReassemble(
                self.master_index, self.slave_index,
                buffer.as_mut_ptr(), out_capacity as u32,
                packet_number, timeout_us, &mut out_len,
            )
        };
        if ret != 0 {
            buffer.truncate(out_len as usize);
            Ok(buffer)
        } else {
            Err(DarraError::AoeFailed)
        }
    }

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

    const COE_INDEX_GROUP: u32 = 0xF302;

    const SOE_INDEX_GROUP: u32 = 0xF420;

    pub fn read_coe_via_aoe(&self, index: u16, subindex: u8, read_length: u32) -> Result<Option<Vec<u8>>> {
        let index_offset = ((index as u32) << 16) | (subindex as u32);
        self.read_write(Self::COE_INDEX_GROUP, index_offset, read_length, None, DEFAULT_TIMEOUT_US)
    }

    pub fn write_coe_via_aoe(&self, index: u16, subindex: u8, data: &[u8]) -> Result<()> {
        let index_offset = ((index as u32) << 16) | (subindex as u32);
        let mut read_ptr: *mut c_void = std::ptr::null_mut();
        let mut bytes_read: u32 = 0;
        let ret = unsafe {
            ffi::AOEReadWrite(
                self.master_index, self.slave_index,
                Self::COE_INDEX_GROUP, index_offset,
                0, data.len() as u32, data.as_ptr(),
                &mut read_ptr, &mut bytes_read, DEFAULT_TIMEOUT_US,
            )
        };
        if !read_ptr.is_null() { unsafe { ffi::FreeMemory(read_ptr) }; }
        if ret != 0 { Ok(()) } else { Err(DarraError::AoeFailed) }
    }

    pub fn read_soe_via_aoe(&self, idn: u32, read_length: u32) -> Result<Option<Vec<u8>>> {
        self.read_write(Self::SOE_INDEX_GROUP, idn, read_length, None, DEFAULT_TIMEOUT_US)
    }

    pub fn write_soe_via_aoe(&self, idn: u32, data: &[u8]) -> Result<()> {
        let mut read_ptr: *mut c_void = std::ptr::null_mut();
        let mut bytes_read: u32 = 0;
        let ret = unsafe {
            ffi::AOEReadWrite(
                self.master_index, self.slave_index,
                Self::SOE_INDEX_GROUP, idn,
                0, data.len() as u32, data.as_ptr(),
                &mut read_ptr, &mut bytes_read, DEFAULT_TIMEOUT_US,
            )
        };
        if !read_ptr.is_null() { unsafe { ffi::FreeMemory(read_ptr) }; }
        if ret != 0 { Ok(()) } else { Err(DarraError::AoeFailed) }
    }

    pub fn initialize_slave_net_id(&self, net_id: &[u8; 6]) -> Result<()> {
        self.write(1, 3, net_id)
    }

    pub fn ads_state_name(ads_state: u16) -> &'static str {
        match ads_state {
            0 => "Invalid", 1 => "Idle", 2 => "Reset", 3 => "Init",
            4 => "Start", 5 => "Run", 6 => "Stop", 7 => "SaveCfg",
            8 => "LoadCfg", 9 => "PowerFailure", 10 => "PowerGood",
            11 => "Error", 12 => "Shutdown", 13 => "Suspend",
            14 => "Resume", 15 => "Config", 16 => "ReConfig",
            _ => "Unknown",
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u32)]
pub enum AoeTransMode {

    NoTransmission = 0,

    Cyclic = 1,

    OnChange = 2,

    CyclicInDevice = 3,

    OnChangeInDevice = 4,
}

#[derive(Debug, Clone)]
pub struct AoeSubscription {

    pub slave_index: u16,

    pub index_group: u32,

    pub index_offset: u32,

    pub length: u32,

    pub trans_mode: AoeTransMode,

    pub max_delay_us: u32,

    pub cycle_time_us: u32,

    pub timeout_us: i32,
}

impl AoeSubscription {

    pub fn on_change(
        slave_index: u16, index_group: u32, index_offset: u32,
        length: u32, timeout_us: i32,
    ) -> Self {
        Self {
            slave_index, index_group, index_offset, length,
            trans_mode: AoeTransMode::OnChange,
            max_delay_us: 1000, cycle_time_us: 0, timeout_us,
        }
    }

    pub fn cyclic(
        slave_index: u16, index_group: u32, index_offset: u32,
        length: u32, cycle_time_us: u32, timeout_us: i32,
    ) -> Self {
        Self {
            slave_index, index_group, index_offset, length,
            trans_mode: AoeTransMode::Cyclic,
            max_delay_us: cycle_time_us, cycle_time_us, timeout_us,
        }
    }
}

#[derive(Debug)]
struct ActiveSubscription {
    slave_index: u16,
    aoe_handle: u32,
    subscription_index: i32,
    timeout_us: i32,
    config: AoeSubscription,
}

pub struct AoeSubscriptionManager {
    master_index: u16,
    subscriptions: HashMap<String, ActiveSubscription>,
    next_id: u32,
}

impl AoeSubscriptionManager {

    pub fn new(master_index: u16) -> Self {
        Self { master_index, subscriptions: HashMap::new(), next_id: 1 }
    }

    pub fn subscribe(
        &mut self, name: impl Into<String>, config: AoeSubscription,
        callback: AOENotificationCallback, user_data: *mut std::ffi::c_void,
    ) -> Result<u32> {
        let name = name.into();
        if self.subscriptions.contains_key(&name) {
            return Err(DarraError::Other(format!("订阅 '{}' 已存在", name)));
        }

        let mut aoe_handle: u32 = 0;
        let add_ok = unsafe {
            ffi::AOEAddNotification(
                self.master_index, config.slave_index,
                config.index_group, config.index_offset, config.length,
                config.trans_mode as u32,
                config.max_delay_us, config.cycle_time_us,
                &mut aoe_handle, config.timeout_us,
            )
        };
        if add_ok == 0 { return Err(DarraError::AoeFailed); }

        let sub_idx = unsafe {
            ffi::AOERegisterNotification(
                config.slave_index, aoe_handle,
                config.index_group, config.index_offset, config.length,
                callback, user_data,
            )
        };
        if sub_idx < 0 {
            unsafe { ffi::AOEDelNotification(self.master_index, config.slave_index, aoe_handle, config.timeout_us); }
            return Err(DarraError::AoeFailed);
        }

        let id = self.next_id;
        self.next_id += 1;
        self.subscriptions.insert(name, ActiveSubscription {
            slave_index: config.slave_index, aoe_handle,
            subscription_index: sub_idx, timeout_us: config.timeout_us, config,
        });
        Ok(id)
    }

    pub fn unsubscribe(&mut self, name: &str) -> Result<()> {
        let sub = self.subscriptions.remove(name)
            .ok_or_else(|| DarraError::Other(format!("订阅 '{}' 不存在", name)))?;
        unsafe { ffi::AOEUnregisterNotification(sub.subscription_index) };
        let del_ok = unsafe {
            ffi::AOEDelNotification(self.master_index, sub.slave_index, sub.aoe_handle, sub.timeout_us)
        };
        if del_ok == 0 { Err(DarraError::AoeFailed) } else { Ok(()) }
    }

    pub fn unsubscribe_all(&mut self) {
        let names: Vec<String> = self.subscriptions.keys().cloned().collect();
        for name in names { let _ = self.unsubscribe(&name); }
    }

    pub fn has_subscription(&self, name: &str) -> bool { self.subscriptions.contains_key(name) }

    pub fn subscription_count(&self) -> usize { self.subscriptions.len() }

    pub fn subscription_names(&self) -> Vec<&str> { self.subscriptions.keys().map(|s| s.as_str()).collect() }

    pub fn subscription_config(&self, name: &str) -> Option<&AoeSubscription> {
        self.subscriptions.get(name).map(|s| &s.config)
    }

    pub fn subscription_handle(&self, name: &str) -> Option<u32> {
        self.subscriptions.get(name).map(|s| s.aoe_handle)
    }
}

impl Drop for AoeSubscriptionManager {
    fn drop(&mut self) { self.unsubscribe_all(); }
}

impl std::fmt::Debug for AoeSubscriptionManager {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AoeSubscriptionManager")
            .field("master_index", &self.master_index)
            .field("active_subscriptions", &self.subscriptions.len())
            .finish()
    }
}

impl AoeSubscriptionManager {
    pub fn active_subscriptions(&self) -> Vec<&str> {
        self.subscriptions.keys().map(|k| k.as_str()).collect()
    }
}

pub fn ads_state_description(ads_state: u16) -> &'static str {
    match ads_state {
        0 => "无效",
        1 => "空闲",
        2 => "复位",
        3 => "初始化",
        4 => "启动",
        5 => "运行",
        6 => "停止",
        7 => "保存配置",
        8 => "加载配置",
        9 => "关机",
        10 => "暂停",
        11 => "重新配置",
        15 => "错误",
        _ => "未知状态",
    }
}

impl AoEInstance {

    pub fn read_blocking(
        master_index: u16,
        slave_index: u16,
        index_group: u32,
        index_offset: u32,
        length: u32,
    ) -> std::thread::JoinHandle<Result<Option<Vec<u8>>>> {
        std::thread::spawn(move || {
            let aoe = AoEInstance::new(master_index, slave_index);
            aoe.read(index_group, index_offset, length)
        })
    }

    pub fn write_blocking(
        master_index: u16,
        slave_index: u16,
        index_group: u32,
        index_offset: u32,
        data: Vec<u8>,
    ) -> std::thread::JoinHandle<Result<()>> {
        std::thread::spawn(move || {
            let aoe = AoEInstance::new(master_index, slave_index);
            aoe.write(index_group, index_offset, &data)
        })
    }
}

#[cfg(feature = "async-tokio")]
impl AoEInstance {

    pub async fn read_async(
        &self,
        index_group: u32,
        index_offset: u32,
        length: u32,
    ) -> Result<Option<Vec<u8>>> {
        let master = self.master_index;
        let slave = self.slave_index;
        tokio::task::spawn_blocking(move || {
            let aoe = AoEInstance::new(master, slave);
            aoe.read(index_group, index_offset, length)
        })
        .await
        .map_err(|e| DarraError::Other(format!("tokio join error: {}", e)))?
    }

    pub async fn write_async(
        &self,
        index_group: u32,
        index_offset: u32,
        data: Vec<u8>,
    ) -> Result<()> {
        let master = self.master_index;
        let slave = self.slave_index;
        tokio::task::spawn_blocking(move || {
            let aoe = AoEInstance::new(master, slave);
            aoe.write(index_group, index_offset, &data)
        })
        .await
        .map_err(|e| DarraError::Other(format!("tokio join error: {}", e)))?
    }
}

impl crate::abstractions::MailboxProtocol for AoEInstance {
    fn protocol_type(&self) -> u8 { 0x01 }
    fn protocol_name(&self) -> &'static str { "AoE" }

    fn is_supported(&self) -> bool {
        AoEInstance::is_supported(self)
    }

    fn statistics(&self) -> crate::abstractions::MailboxStatistics {
        let mut stats = ffi::EcMbxStatsC::default();
        let rc = unsafe {
            ffi::mbx_get_stats_by_master(
                self.master_index, self.slave_index, 0x01, &mut stats,
            )
        };
        if rc == 1 {
            stats.into()
        } else {
            crate::abstractions::MailboxStatistics::empty()
        }
    }

    fn reset_statistics(&self) {
        unsafe {
            ffi::mbx_reset_stats_by_master(self.master_index, self.slave_index, 0x01);
        }
    }
}