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

use crate::utils::ffi;
use crate::master::core::EtherCATMaster;

pub struct MasterConfig<'a> {
    master: &'a EtherCATMaster,
}

impl<'a> MasterConfig<'a> {

    pub(crate) fn new(master: &'a EtherCATMaster) -> Self {
        Self { master }
    }

    pub fn loop_cycle(&self) -> u32 {

        unsafe { ffi::GetTimingMode(self.master.index()) }
    }

    pub fn set_loop_cycle(&self, time_ns: u32) {
        unsafe { ffi::SetMasterLoopCycleTime(self.master.index(), time_ns) };
    }

    pub fn frame_high_priority(&self) -> bool {
        let ptr = unsafe { ffi::GetMasterStateCache(self.master.index()) };
        if ptr.is_null() { return false; }

        unsafe {
            let byte_ptr = ptr as *const u8;

            *byte_ptr.add(OFFSET_FRAME_HIGH_PRIORITY) != 0
        }
    }

    pub fn set_frame_high_priority(&self, value: bool) {
        let ptr = unsafe { ffi::GetMasterState(self.master.index()) };
        if ptr.is_null() { return; }
        unsafe {
            let byte_ptr = ptr as *mut u8;
            *byte_ptr.add(OFFSET_FRAME_HIGH_PRIORITY) = if value { 1 } else { 0 };
        }
    }

    pub fn use_udp(&self) -> bool {
        (unsafe { ffi::GetUdpMode(self.master.index()) }) != 0
    }

    pub fn set_use_udp(&self, value: bool) {
        unsafe { ffi::SetUdpMode(self.master.index(), if value { 1 } else { 0 }) };
    }

    pub fn is_udp_available(&self) -> bool {
        (unsafe { ffi::IsUdpAvailable(self.master.index()) }) != 0
    }

    pub fn vlan_id(&self) -> u16 {
        let ptr = unsafe { ffi::GetMasterStateCache(self.master.index()) };
        if ptr.is_null() { return 0; }
        unsafe {
            let byte_ptr = ptr as *const u8;
            u16::from_le_bytes([
                *byte_ptr.add(OFFSET_VLAN_ID),
                *byte_ptr.add(OFFSET_VLAN_ID + 1),
            ])
        }
    }

    pub fn set_vlan_id(&self, value: u16) {
        if value > 4095 { return; }
        let ptr = unsafe { ffi::GetMasterState(self.master.index()) };
        if ptr.is_null() { return; }
        unsafe {
            let byte_ptr = ptr as *mut u8;
            let bytes = value.to_le_bytes();
            *byte_ptr.add(OFFSET_VLAN_ID) = bytes[0];
            *byte_ptr.add(OFFSET_VLAN_ID + 1) = bytes[1];
        }
    }

    pub fn vlan_priority(&self) -> u8 {
        let ptr = unsafe { ffi::GetMasterStateCache(self.master.index()) };
        if ptr.is_null() { return 0; }
        unsafe {
            let byte_ptr = ptr as *const u8;
            *byte_ptr.add(OFFSET_VLAN_PRIORITY)
        }
    }

    pub fn set_vlan_priority(&self, value: u8) {
        if value > 7 { return; }
        let ptr = unsafe { ffi::GetMasterState(self.master.index()) };
        if ptr.is_null() { return; }
        unsafe {
            let byte_ptr = ptr as *mut u8;
            *byte_ptr.add(OFFSET_VLAN_PRIORITY) = value;
        }
    }

    pub fn overlapping_groups(&self) -> bool {
        let ptr = unsafe { ffi::GetMasterStateCache(self.master.index()) };
        if ptr.is_null() { return false; }
        unsafe {
            let byte_ptr = ptr as *const u8;
            *byte_ptr.add(OFFSET_OVERLAPPING_GROUPS) != 0
        }
    }

    pub fn set_overlapping_groups(&self, value: bool) {
        let ptr = unsafe { ffi::GetMasterState(self.master.index()) };
        if ptr.is_null() { return; }
        unsafe {
            let byte_ptr = ptr as *mut u8;
            *byte_ptr.add(OFFSET_OVERLAPPING_GROUPS) = if value { 1 } else { 0 };
        }
    }

    pub fn packed_mode(&self) -> bool {
        let ptr = unsafe { ffi::GetMasterStateCache(self.master.index()) };
        if ptr.is_null() { return false; }
        unsafe {
            let byte_ptr = ptr as *const u8;
            *byte_ptr.add(OFFSET_PACKED_MODE) != 0
        }
    }

    pub fn set_packed_mode(&self, value: bool) {
        let ptr = unsafe { ffi::GetMasterState(self.master.index()) };
        if ptr.is_null() { return; }
        unsafe {
            let byte_ptr = ptr as *mut u8;
            *byte_ptr.add(OFFSET_PACKED_MODE) = if value { 1 } else { 0 };
        }
    }

    pub fn filter_threshold(&self) -> u32 {
        let ptr = unsafe { ffi::GetMasterStateCache(self.master.index()) };
        if ptr.is_null() { return 3; }
        unsafe {
            let byte_ptr = ptr as *const u8;
            u32::from_le_bytes([
                *byte_ptr.add(OFFSET_FILTER_THRESHOLD),
                *byte_ptr.add(OFFSET_FILTER_THRESHOLD + 1),
                *byte_ptr.add(OFFSET_FILTER_THRESHOLD + 2),
                *byte_ptr.add(OFFSET_FILTER_THRESHOLD + 3),
            ])
        }
    }

    pub fn set_filter_threshold(&self, value: u32) {
        let ptr = unsafe { ffi::GetMasterState(self.master.index()) };
        if ptr.is_null() { return; }
        unsafe {
            let byte_ptr = ptr as *mut u8;
            let bytes = value.to_le_bytes();
            for i in 0..4 {
                *byte_ptr.add(OFFSET_FILTER_THRESHOLD + i) = bytes[i];
            }
        }
    }

    pub fn frame_repeat_count(&self) -> u8 {
        let ptr = unsafe { ffi::GetMasterStateCache(self.master.index()) };
        if ptr.is_null() { return 1; }
        unsafe {
            let byte_ptr = ptr as *const u8;
            *byte_ptr.add(OFFSET_FRAME_REPEAT_COUNT)
        }
    }

    pub fn set_frame_repeat_count(&self, value: u8) {
        if value < 1 || value > 3 { return; }
        let ptr = unsafe { ffi::GetMasterState(self.master.index()) };
        if ptr.is_null() { return; }
        unsafe {
            let byte_ptr = ptr as *mut u8;
            *byte_ptr.add(OFFSET_FRAME_REPEAT_COUNT) = value;
        }
    }

    pub fn mutex_protection(&self) -> bool {
        (unsafe { ffi::GetMutexProtection(self.master.index()) }) != 0
    }

    pub fn set_mutex_protection(&self, value: bool) {
        unsafe { ffi::SetMutexProtection(self.master.index(), if value { 1 } else { 0 }) };
    }

    pub fn process_data_watchdog_ms(&self) -> u32 {
        let ptr = unsafe { ffi::GetMasterStateCache(self.master.index()) };
        if ptr.is_null() { return 0; }
        unsafe {
            let byte_ptr = ptr as *const u8;
            u16::from_le_bytes([
                *byte_ptr.add(OFFSET_WD_PD_TIMEOUT_MS),
                *byte_ptr.add(OFFSET_WD_PD_TIMEOUT_MS + 1),
            ]) as u32
        }
    }

    pub fn set_process_data_watchdog_ms(&self, value: u32) {
        let ptr = unsafe { ffi::GetMasterState(self.master.index()) };
        if ptr.is_null() { return; }
        let clamped = value.min(6553) as u16;
        unsafe {
            let byte_ptr = ptr as *mut u8;
            let bytes = clamped.to_le_bytes();
            *byte_ptr.add(OFFSET_WD_PD_TIMEOUT_MS) = bytes[0];
            *byte_ptr.add(OFFSET_WD_PD_TIMEOUT_MS + 1) = bytes[1];
        }
    }

    pub fn pdi_watchdog_ms(&self) -> u32 {
        let ptr = unsafe { ffi::GetMasterStateCache(self.master.index()) };
        if ptr.is_null() { return 0; }
        unsafe {
            let byte_ptr = ptr as *const u8;
            u16::from_le_bytes([
                *byte_ptr.add(OFFSET_WD_PDI_TIMEOUT_MS),
                *byte_ptr.add(OFFSET_WD_PDI_TIMEOUT_MS + 1),
            ]) as u32
        }
    }

    pub fn set_pdi_watchdog_ms(&self, value: u32) {
        let ptr = unsafe { ffi::GetMasterState(self.master.index()) };
        if ptr.is_null() { return; }
        let clamped = value.min(6553) as u16;
        unsafe {
            let byte_ptr = ptr as *mut u8;
            let bytes = clamped.to_le_bytes();
            *byte_ptr.add(OFFSET_WD_PDI_TIMEOUT_MS) = bytes[0];
            *byte_ptr.add(OFFSET_WD_PDI_TIMEOUT_MS + 1) = bytes[1];
        }
    }

    pub fn adaptive_timeout_enabled(&self) -> bool {
        let ptr = unsafe { ffi::GetMasterStateCache(self.master.index()) };
        if ptr.is_null() { return false; }
        unsafe {
            let byte_ptr = ptr as *const u8;
            *byte_ptr.add(OFFSET_ADAPTIVE_TIMEOUT_ENABLED) != 0
        }
    }

    pub fn set_adaptive_timeout_enabled(&self, value: bool) {
        let ptr = unsafe { ffi::GetMasterState(self.master.index()) };
        if ptr.is_null() { return; }
        unsafe {
            let byte_ptr = ptr as *mut u8;
            *byte_ptr.add(OFFSET_ADAPTIVE_TIMEOUT_ENABLED) = if value { 1 } else { 0 };
        }
    }

    pub fn timeout_init_to_preop(&self) -> u32 {
        let ptr = unsafe { ffi::GetMasterStateCache(self.master.index()) };
        if ptr.is_null() { return 3000; }
        unsafe {
            let byte_ptr = ptr as *const u8;
            u32::from_le_bytes([
                *byte_ptr.add(OFFSET_TIMEOUT_INIT_TO_PREOP),
                *byte_ptr.add(OFFSET_TIMEOUT_INIT_TO_PREOP + 1),
                *byte_ptr.add(OFFSET_TIMEOUT_INIT_TO_PREOP + 2),
                *byte_ptr.add(OFFSET_TIMEOUT_INIT_TO_PREOP + 3),
            ])
        }
    }

    pub fn set_timeout_init_to_preop(&self, value: u32) {
        let ptr = unsafe { ffi::GetMasterState(self.master.index()) };
        if ptr.is_null() { return; }
        unsafe {
            let byte_ptr = ptr as *mut u8;
            let bytes = value.to_le_bytes();
            for i in 0..4 {
                *byte_ptr.add(OFFSET_TIMEOUT_INIT_TO_PREOP + i) = bytes[i];
            }
        }
    }

    pub fn timeout_preop_to_safeop(&self) -> u32 {
        let ptr = unsafe { ffi::GetMasterStateCache(self.master.index()) };
        if ptr.is_null() { return 10000; }
        unsafe {
            let byte_ptr = ptr as *const u8;
            u32::from_le_bytes([
                *byte_ptr.add(OFFSET_TIMEOUT_PREOP_TO_SAFEOP),
                *byte_ptr.add(OFFSET_TIMEOUT_PREOP_TO_SAFEOP + 1),
                *byte_ptr.add(OFFSET_TIMEOUT_PREOP_TO_SAFEOP + 2),
                *byte_ptr.add(OFFSET_TIMEOUT_PREOP_TO_SAFEOP + 3),
            ])
        }
    }

    pub fn set_timeout_preop_to_safeop(&self, value: u32) {
        let ptr = unsafe { ffi::GetMasterState(self.master.index()) };
        if ptr.is_null() { return; }
        unsafe {
            let byte_ptr = ptr as *mut u8;
            let bytes = value.to_le_bytes();
            for i in 0..4 {
                *byte_ptr.add(OFFSET_TIMEOUT_PREOP_TO_SAFEOP + i) = bytes[i];
            }
        }
    }

    pub fn timeout_safeop_to_op(&self) -> u32 {
        let ptr = unsafe { ffi::GetMasterStateCache(self.master.index()) };
        if ptr.is_null() { return 5000; }
        unsafe {
            let byte_ptr = ptr as *const u8;
            u32::from_le_bytes([
                *byte_ptr.add(OFFSET_TIMEOUT_SAFEOP_TO_OP),
                *byte_ptr.add(OFFSET_TIMEOUT_SAFEOP_TO_OP + 1),
                *byte_ptr.add(OFFSET_TIMEOUT_SAFEOP_TO_OP + 2),
                *byte_ptr.add(OFFSET_TIMEOUT_SAFEOP_TO_OP + 3),
            ])
        }
    }

    pub fn set_timeout_safeop_to_op(&self, value: u32) {
        let ptr = unsafe { ffi::GetMasterState(self.master.index()) };
        if ptr.is_null() { return; }
        unsafe {
            let byte_ptr = ptr as *mut u8;
            let bytes = value.to_le_bytes();
            for i in 0..4 {
                *byte_ptr.add(OFFSET_TIMEOUT_SAFEOP_TO_OP + i) = bytes[i];
            }
        }
    }

    pub fn scan_revision_match(&self) -> u8 {
        let ptr = unsafe { ffi::GetMasterStateCache(self.master.index()) };
        if ptr.is_null() { return 0; }
        unsafe {
            let byte_ptr = ptr as *const u8;
            *byte_ptr.add(OFFSET_SCAN_REVISION_MATCH)
        }
    }

    pub fn set_scan_revision_match(&self, mode: u8) {
        if mode > 2 { return; }
        let ptr = unsafe { ffi::GetMasterState(self.master.index()) };
        if ptr.is_null() { return; }
        unsafe {
            let byte_ptr = ptr as *mut u8;
            *byte_ptr.add(OFFSET_SCAN_REVISION_MATCH) = mode;
        }
    }

    pub fn drift_compensation(&self) -> bool {
        let ptr = unsafe { ffi::GetMasterStateCache(self.master.index()) };
        if ptr.is_null() { return false; }
        unsafe {
            let byte_ptr = ptr as *const u8;
            *byte_ptr.add(OFFSET_DRIFT_COMPENSATION) != 0
        }
    }

    pub fn set_drift_compensation(&self, value: bool) {
        let ptr = unsafe { ffi::GetMasterState(self.master.index()) };
        if ptr.is_null() { return; }
        unsafe {
            let byte_ptr = ptr as *mut u8;
            *byte_ptr.add(OFFSET_DRIFT_COMPENSATION) = if value { 1 } else { 0 };
        }
    }

    pub fn mailbox_timeout(&self) -> u32 {
        let ptr = unsafe { ffi::GetMasterStateCache(self.master.index()) };
        if ptr.is_null() { return 5000; }
        unsafe {
            let byte_ptr = ptr as *const u8;
            u32::from_le_bytes([
                *byte_ptr.add(OFFSET_MAILBOX_TIMEOUT),
                *byte_ptr.add(OFFSET_MAILBOX_TIMEOUT + 1),
                *byte_ptr.add(OFFSET_MAILBOX_TIMEOUT + 2),
                *byte_ptr.add(OFFSET_MAILBOX_TIMEOUT + 3),
            ])
        }
    }

    pub fn set_mailbox_timeout(&self, ms: u32) {
        let ptr = unsafe { ffi::GetMasterState(self.master.index()) };
        if ptr.is_null() { return; }
        unsafe {
            let byte_ptr = ptr as *mut u8;
            let bytes = ms.to_le_bytes();
            for i in 0..4 {
                *byte_ptr.add(OFFSET_MAILBOX_TIMEOUT + i) = bytes[i];
            }
        }
    }

    pub fn mailbox_retries(&self) -> u32 {
        let ptr = unsafe { ffi::GetMasterStateCache(self.master.index()) };
        if ptr.is_null() { return 3; }
        unsafe {
            let byte_ptr = ptr as *const u8;
            u32::from_le_bytes([
                *byte_ptr.add(OFFSET_MAILBOX_RETRIES),
                *byte_ptr.add(OFFSET_MAILBOX_RETRIES + 1),
                *byte_ptr.add(OFFSET_MAILBOX_RETRIES + 2),
                *byte_ptr.add(OFFSET_MAILBOX_RETRIES + 3),
            ])
        }
    }

    pub fn set_mailbox_retries(&self, count: u32) {
        let ptr = unsafe { ffi::GetMasterState(self.master.index()) };
        if ptr.is_null() { return; }
        unsafe {
            let byte_ptr = ptr as *mut u8;
            let bytes = count.to_le_bytes();
            for i in 0..4 {
                *byte_ptr.add(OFFSET_MAILBOX_RETRIES + i) = bytes[i];
            }
        }
    }

    pub fn save_config_xml(&self, path: &str) -> bool {
        let loop_cycle_us = self.loop_cycle() / 1000;
        let xml = format!(
            "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\
             <DarraEtherCATConfiguration version=\"1.0\">\n\
             \x20 <Master>\n\
             \x20   <CycleTime unit=\"us\">{}</CycleTime>\n\
             \x20   <DcCycleTime unit=\"us\">{}</DcCycleTime>\n\
             \x20   <FrameHighPriority>{}</FrameHighPriority>\n\
             \x20   <UseUdp>{}</UseUdp>\n\
             \x20   <VlanId>{}</VlanId>\n\
             \x20   <VlanPriority>{}</VlanPriority>\n\
             \x20   <OverlappingGroups>{}</OverlappingGroups>\n\
             \x20   <PackedMode>{}</PackedMode>\n\
             \x20   <MutexProtection>{}</MutexProtection>\n\
             \x20   <FilterThreshold>{}</FilterThreshold>\n\
             \x20   <FrameRepeatCount>{}</FrameRepeatCount>\n\
             \x20   <ProcessDataWatchdogMs>{}</ProcessDataWatchdogMs>\n\
             \x20   <PdiWatchdogMs>{}</PdiWatchdogMs>\n\
             \x20   <AdaptiveTimeout>{}</AdaptiveTimeout>\n\
             \x20   <DriftCompensation>{}</DriftCompensation>\n\
             \x20   <ScanRevisionMatch>{}</ScanRevisionMatch>\n\
             \x20   <MailboxTimeout>{}</MailboxTimeout>\n\
             \x20   <MailboxRetries>{}</MailboxRetries>\n\
             \x20 </Master>\n\
             </DarraEtherCATConfiguration>",
            loop_cycle_us,
            loop_cycle_us,
            self.frame_high_priority(),
            self.use_udp(),
            self.vlan_id(),
            self.vlan_priority(),
            self.overlapping_groups(),
            self.packed_mode(),
            self.mutex_protection(),
            self.filter_threshold(),
            self.frame_repeat_count(),
            self.process_data_watchdog_ms(),
            self.pdi_watchdog_ms(),
            self.adaptive_timeout_enabled(),
            self.drift_compensation(),
            self.scan_revision_match(),
            self.mailbox_timeout(),
            self.mailbox_retries(),
        );

        std::fs::write(path, xml.as_bytes()).is_ok()
    }

    pub fn export_config_string(&self) -> Option<String> {

        let json = format!(
            "{{\n\
             \x20 \"loop_cycle_ns\": {},\n\
             \x20 \"frame_high_priority\": {},\n\
             \x20 \"use_udp\": {},\n\
             \x20 \"udp_available\": {},\n\
             \x20 \"vlan_id\": {},\n\
             \x20 \"vlan_priority\": {},\n\
             \x20 \"overlapping_groups\": {},\n\
             \x20 \"packed_mode\": {},\n\
             \x20 \"mutex_protection\": {},\n\
             \x20 \"filter_threshold\": {},\n\
             \x20 \"frame_repeat_count\": {},\n\
             \x20 \"process_data_watchdog_ms\": {},\n\
             \x20 \"pdi_watchdog_ms\": {},\n\
             \x20 \"adaptive_timeout_enabled\": {},\n\
             \x20 \"timeout_init_to_preop\": {},\n\
             \x20 \"timeout_preop_to_safeop\": {},\n\
             \x20 \"timeout_safeop_to_op\": {},\n\
             \x20 \"drift_compensation\": {},\n\
             \x20 \"scan_revision_match\": {},\n\
             \x20 \"mailbox_timeout\": {},\n\
             \x20 \"mailbox_retries\": {}\n\
             }}",
            self.loop_cycle(),
            self.frame_high_priority(),
            self.use_udp(),
            self.is_udp_available(),
            self.vlan_id(),
            self.vlan_priority(),
            self.overlapping_groups(),
            self.packed_mode(),
            self.mutex_protection(),
            self.filter_threshold(),
            self.frame_repeat_count(),
            self.process_data_watchdog_ms(),
            self.pdi_watchdog_ms(),
            self.adaptive_timeout_enabled(),
            self.timeout_init_to_preop(),
            self.timeout_preop_to_safeop(),
            self.timeout_safeop_to_op(),
            self.drift_compensation(),
            self.scan_revision_match(),
            self.mailbox_timeout(),
            self.mailbox_retries(),
        );

        Some(json)
    }
}

const OFFSET_FRAME_HIGH_PRIORITY: usize = 0x100;

const OFFSET_VLAN_ID: usize = 0x102;

const OFFSET_VLAN_PRIORITY: usize = 0x104;

const OFFSET_OVERLAPPING_GROUPS: usize = 0x105;

const OFFSET_PACKED_MODE: usize = 0x106;

const OFFSET_FILTER_THRESHOLD: usize = 0x108;

const OFFSET_FRAME_REPEAT_COUNT: usize = 0x10C;

const OFFSET_WD_PD_TIMEOUT_MS: usize = 0x110;

const OFFSET_WD_PDI_TIMEOUT_MS: usize = 0x112;

const OFFSET_ADAPTIVE_TIMEOUT_ENABLED: usize = 0x114;

const OFFSET_TIMEOUT_INIT_TO_PREOP: usize = 0x118;

const OFFSET_TIMEOUT_PREOP_TO_SAFEOP: usize = 0x11C;

const OFFSET_TIMEOUT_SAFEOP_TO_OP: usize = 0x120;

const OFFSET_DRIFT_COMPENSATION: usize = 0x124;

const OFFSET_SCAN_REVISION_MATCH: usize = 0x125;

const OFFSET_MAILBOX_TIMEOUT: usize = 0x128;

const OFFSET_MAILBOX_RETRIES: usize = 0x12C;

pub fn save_master_settings(path: &str, loop_cycle_us: u16, dc_cycle_us: u16, expected_slave_count: i32) -> bool {
    let xml = format!(
        "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\
         <DarraEtherCATConfiguration version=\"1.0\">\n\
         \x20 <Master>\n\
         \x20   <CycleTime unit=\"us\">{}</CycleTime>\n\
         \x20   <DcCycleTime unit=\"us\">{}</DcCycleTime>\n\
         \x20   <ExpectedSlaveCount>{}</ExpectedSlaveCount>\n\
         \x20 </Master>\n\
         </DarraEtherCATConfiguration>",
        loop_cycle_us, dc_cycle_us, expected_slave_count
    );

    std::fs::write(path, xml.as_bytes()).is_ok()
}

pub fn load_master_settings(path: &str) -> (bool, u16, u16) {

    let mut loop_cycle: u16 = 1000;
    let mut dc_cycle: u16 = 1000;

    let content = match std::fs::read_to_string(path) {
        Ok(c) => c,
        Err(_) => return (false, loop_cycle, dc_cycle),
    };

    if !content.contains("DarraEtherCATConfiguration") {
        return (false, loop_cycle, dc_cycle);
    }

    if let Some(val) = extract_xml_value(&content, "CycleTime") {
        if let Ok(v) = val.parse::<u16>() {
            if v > 0 { loop_cycle = v; }
        }
    }
    if let Some(val) = extract_xml_value(&content, "DcCycleTime") {
        if let Ok(v) = val.parse::<u16>() {
            if v > 0 { dc_cycle = v; }
        }
    }

    (true, loop_cycle, dc_cycle)
}

fn extract_xml_value(xml: &str, tag: &str) -> Option<String> {
    let open_tag = format!("<{}", tag);
    let close_tag = format!("</{}>", tag);

    let start_pos = xml.find(&open_tag)?;
    let after_open = &xml[start_pos..];

    let content_start = after_open.find('>')? + 1;
    let remaining = &after_open[content_start..];
    let end_pos = remaining.find(&close_tag)?;
    let value = remaining[..end_pos].trim();
    if value.is_empty() { None } else { Some(value.to_string()) }
}

impl<'a> std::fmt::Display for MasterConfig<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let cycle_us = self.loop_cycle() as f64 / 1000.0;
        write!(f, "MasterConfig(cycle={:.0}us, mutex={}, mbx_timeout={}ms, mbx_retries={})",
               cycle_us,
               if self.mutex_protection() { "on" } else { "off" },
               self.mailbox_timeout(),
               self.mailbox_retries())
    }
}