can_tools 2.1.2

Rust editor for CanDatabase. It allows to open and modify CAN database from .dbc and .arxml files.
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
use autosar_data::{AttributeName, AutosarModel, CharacterData, Element, ElementName, EnumItem};
use std::fs::File;
use std::io::{self, BufRead, BufReader};

use encoding_rs::WINDOWS_1252;

use crate::core;
use crate::types::{
    database::{BusType, CanDatabase, CanMessageKey, CanNodeKey},
    errors::{ArxmlConvertError, DatabaseError, DbcParseError},
    message::MuxRole,
    signal::{Endianness, Signess},
};

/// Parses a DBC file and returns a populated [`CanDatabase`] instance.
///
/// This function reads a DBC file from disk, parses its content line by line,
/// and fills the [`CanDatabase`] structure with all parsed information:
/// - **Name** (from `BA_ "DBName"` line)
/// - **Version** (from `VERSION` line)
/// - **Baudrate** (from `BA_ "Baudrate"` line)
/// - **Baudrate CAN FD** (from `BA_ "BaudrateCANFD"` line)
/// - **BusType** (from `BA_ "BusType"` line)
/// - **Nodes** (from `BU_` line)
/// - **Messages** (from `BO_` lines)
/// - **Signals** (from `SG_` lines)
/// - **Sender nodes** (from `BO_TX_BU_` lines)
/// - **Comments** for messages, signals, and nodes (from `CM_` lines)
/// - **Value tables** (from `VAL_` lines)
///
/// The parsing logic is tolerant to extra spaces, comments, and multi-line strings.
/// Multi-line comments for signals and nodes are correctly joined before parsing.
/// The reader decodes the file as Windows-1252 and transliterates a handful of characters
/// (e.g., `ü`, `ö`, `ß`) to ASCII fallbacks to keep downstream processing UTF-8 safe.
///
/// # Parameters
/// - `path`: Path to the `.dbc` file to parse.
///
/// # Returns
/// - `Ok(CanDatabase)` if the file was successfully read and parsed.
/// - `Err(DbcParseError)` detailing why the file could not be opened or read.
///
/// # Errors
/// Returns an `Err(DbcParseError)` if:
/// - The file cannot be opened.
/// - There are I/O errors while reading.
/// - The path does not end in `.dbc`.
///
/// # Notes
/// - This function is the main entry point for converting a DBC file into a structured [`CanDatabase`].
/// - Internal parsing details are handled by [`CanDatabase`] methods and are **not** part of the public API.
/// - Parsing stops only at the end of the file; malformed lines are skipped.
///
pub fn from_dbc_file(path: &str) -> Result<CanDatabase, DbcParseError> {
    // check if provided file has .dbc format
    if !path.ends_with(".dbc") {
        return Err(DbcParseError::InvalidExtension {
            path: path.to_string(),
        });
    }

    let path_owned: String = path.to_string();
    let file: File = File::open(path).map_err(|source| DbcParseError::OpenFile {
        path: path_owned.clone(),
        source,
    })?;
    let mut reader: BufReader<File> = BufReader::new(file);

    // Initialize CanDatabase
    let mut db: CanDatabase = CanDatabase::default();

    // Buffer for raw bytes of a line
    let mut raw_line: Vec<u8> = Vec::with_capacity(256);

    // For each line, transform german characters in UTF-8 compatible characters
    let read_decoded_line = |reader: &mut BufReader<File>,
                             buf: &mut Vec<u8>|
     -> Result<Option<String>, DbcParseError> {
        buf.clear();
        let read = reader
            .read_until(b'\n', buf)
            .map_err(|source| DbcParseError::Read {
                path: path_owned.clone(),
                source,
            })?;
        if read == 0 {
            return Ok(None);
        }
        let (decoded, _, _) = WINDOWS_1252.decode(buf);
        let decoded_ref: &str = decoded.as_ref();
        let mut replaced: Option<String> = None;

        for (idx, ch) in decoded_ref.char_indices() {
            match ch {
                'ü' => {
                    let buf = replaced.get_or_insert_with(|| {
                        let mut s = String::with_capacity(decoded_ref.len());
                        s.push_str(&decoded_ref[..idx]);
                        s
                    });
                    buf.push('u');
                }
                'ö' => {
                    let buf = replaced.get_or_insert_with(|| {
                        let mut s = String::with_capacity(decoded_ref.len());
                        s.push_str(&decoded_ref[..idx]);
                        s
                    });
                    buf.push('o');
                }
                'ä' => {
                    let buf = replaced.get_or_insert_with(|| {
                        let mut s = String::with_capacity(decoded_ref.len());
                        s.push_str(&decoded_ref[..idx]);
                        s
                    });
                    buf.push('a');
                }
                'ß' => {
                    let buf = replaced.get_or_insert_with(|| {
                        let mut s = String::with_capacity(decoded_ref.len());
                        s.push_str(&decoded_ref[..idx]);
                        s
                    });
                    buf.push('s');
                    buf.push('s');
                }
                'Ü' => {
                    let buf = replaced.get_or_insert_with(|| {
                        let mut s = String::with_capacity(decoded_ref.len());
                        s.push_str(&decoded_ref[..idx]);
                        s
                    });
                    buf.push('U');
                }
                'Ö' => {
                    let buf = replaced.get_or_insert_with(|| {
                        let mut s = String::with_capacity(decoded_ref.len());
                        s.push_str(&decoded_ref[..idx]);
                        s
                    });
                    buf.push('O');
                }
                'Ä' => {
                    let buf = replaced.get_or_insert_with(|| {
                        let mut s = String::with_capacity(decoded_ref.len());
                        s.push_str(&decoded_ref[..idx]);
                        s
                    });
                    buf.push('A');
                }
                '¿' => {
                    let buf = replaced.get_or_insert_with(|| {
                        let mut s = String::with_capacity(decoded_ref.len());
                        s.push_str(&decoded_ref[..idx]);
                        s
                    });
                    buf.push('?');
                }
                _ => {
                    if let Some(buf) = replaced.as_mut() {
                        buf.push(ch);
                    }
                }
            }
        }

        let mut line = match replaced {
            Some(s) => s,
            None => decoded.into_owned(),
        };
        // trim trailing CR/LF to behave like .lines()
        while line.ends_with(['\n', '\r']) {
            line.pop();
        }
        Ok(Some(line))
    };

    // Read and process each .dbc line
    loop {
        let Some(line) = read_decoded_line(&mut reader, &mut raw_line)? else {
            break;
        };

        // Work on a trimmed-start slice to preserve inner spaces elsewhere
        let line_trimmed: &str = line.trim_start();

        // skip comments and empty lines
        if line_trimmed.is_empty() || line_trimmed.starts_with("//") {
            continue;
        }

        // Extract first, second and third part from the line
        let mut parts = line_trimmed.split_ascii_whitespace();
        let first: &str = parts.next().unwrap_or("");
        let second: &str = parts.next().unwrap_or("");
        let third: &str = parts.next().unwrap_or("");

        match first {
            "VERSION" => {
                core::version::decode(&mut db, line_trimmed);
            }
            // Some DBCs use "BU_:" while others use "BU_". Accept both.
            "BU_:" => {
                core::bu_::decode(&mut db, line_trimmed);
            }
            "BO_" => {
                core::bo_::decode(&mut db, line_trimmed);
            }
            "SG_" => {
                core::sg_::decode(&mut db, line_trimmed);
            }
            "BO_TX_BU_" => {
                core::bo_tx_bu_::decode(&mut db, line_trimmed);
            }
            "CM_" => {
                if second.starts_with('"') {
                    // Network/global comment: CM_ "…";
                    core::comments::cm_::decode(&mut db, line_trimmed);
                } else if second == "BO_" {
                    core::comments::cm_bo_::decode(&mut db, line_trimmed);
                } else if second == "SG_" {
                    // Accumulate multiline until the comment has two unescaped quotes
                    let mut full_comment_line: String = line_trimmed.to_string();
                    if !core::strings::has_complete_quoted_segment(&full_comment_line) {
                        // Read subsequent lines until we close the quoted segment
                        while let Some(next) = read_decoded_line(&mut reader, &mut raw_line)? {
                            let next_trim = next.trim_start();
                            full_comment_line.push('\n');
                            full_comment_line.push_str(next_trim);
                            if core::strings::has_complete_quoted_segment(&full_comment_line) {
                                break;
                            }
                        }
                    }
                    core::comments::cm_sg_::decode(&mut db, &full_comment_line);
                } else if second == "BU_" {
                    let mut full_comment_line: String = line_trimmed.to_string();
                    if !core::strings::has_complete_quoted_segment(&full_comment_line) {
                        while let Some(next) = read_decoded_line(&mut reader, &mut raw_line)? {
                            let next_trim = next.trim_start();
                            full_comment_line.push('\n');
                            full_comment_line.push_str(next_trim);
                            if core::strings::has_complete_quoted_segment(&full_comment_line) {
                                break;
                            }
                        }
                    }
                    core::comments::cm_bu_::decode(&mut db, &full_comment_line);
                }
            }
            "BA_DEF_" => {
                if second == "BU_" {
                    core::attributes::ba_def_bu_::decode(&mut db, line_trimmed);
                } else if second == "BO_" {
                    core::attributes::ba_def_bo_::decode(&mut db, line_trimmed);
                } else if second == "SG_" {
                    core::attributes::ba_def_sg_::decode(&mut db, line_trimmed);
                } else {
                    core::attributes::ba_def_::decode(&mut db, line_trimmed);
                }
            }
            "BA_DEF_DEF_" => {
                core::attributes::ba_def_def_::decode(&mut db, line_trimmed);
            }
            "BA_" => {
                if third == "BU_" {
                    core::attributes::ba_bu_::decode(&mut db, line_trimmed);
                } else if third == "BO_" {
                    core::attributes::ba_bo_::decode(&mut db, line_trimmed);
                } else if third == "SG_" {
                    core::attributes::ba_sg_::decode(&mut db, line_trimmed);
                } else {
                    core::attributes::ba_::decode(&mut db, line_trimmed);
                }
            }
            "BA_DEF_REL_" => {
                core::attributes::ba_def_rel_::decode(&mut db, line_trimmed);
            }
            "BA_DEF_DEF_REL_" => {
                core::attributes::ba_def_def_rel_::decode(&mut db, line_trimmed);
            }
            "BA_REL_" => {
                core::attributes::ba_rel_::decode(&mut db, line_trimmed);
            }
            "VAL_" => {
                core::val_::decode(&mut db, line_trimmed);
            }
            "SIG_VALTYPE_" => {
                core::attributes::sig_valtype_::decode(&mut db, line_trimmed);
            }
            _ => {}
        }
    }

    // re-order
    CanDatabase::sort_attribute_map(&mut db.attributes);
    db.sort_db_nodes_by_name();
    db.sort_db_messages_by_name();
    db.sort_db_signals_by_name();
    db.sort_all_node_fields();
    db.sort_all_message_fields();
    db.sort_all_signal_fields();

    Ok(db)
}

/// Extracts one or more [`CanDatabase`] objects from a `.arxml` file by walking all
/// defined `CAN-CLUSTER`s. Each cluster becomes its own database, populated with
/// known messages, signals, and nodes derived from the frame ports.
pub fn from_arxml_file(path: &str) -> Result<Vec<CanDatabase>, ArxmlConvertError> {
    if !path.ends_with(".arxml") {
        return Err(ArxmlConvertError::InvalidExtension {
            path: path.to_string(),
        });
    }

    let model: AutosarModel = AutosarModel::new();
    let path_owned: String = path.to_string();

    model
        .load_file(path, false)
        .map_err(|source| ArxmlConvertError::OpenFile {
            path: path_owned.clone(),
            source: io::Error::other(source),
        })?;

    let mut databases: Vec<CanDatabase> = Vec::new();

    for element in model
        .identifiable_elements()
        .filter_map(|(_, weak)| weak.upgrade())
    {
        if element.element_name() == ElementName::CanCluster
            && let Some(mut db) = build_can_database(&element)
        {
            // re-order
            CanDatabase::sort_attribute_map(&mut db.attributes);
            db.sort_db_nodes_by_name();
            db.sort_db_messages_by_name();
            db.sort_db_signals_by_name();
            db.sort_all_node_fields();
            db.sort_all_message_fields();
            db.sort_all_signal_fields();
            databases.push(db);
        }
    }

    Ok(databases)
}

/// Converte un singolo `CAN-CLUSTER` in un [`CanDatabase`].
fn build_can_database(cluster: &Element) -> Option<CanDatabase> {
    let mut db: CanDatabase = CanDatabase {
        name: cluster.item_name().unwrap_or_default(),
        ..Default::default()
    };

    let ccc = cluster
        .get_sub_element(ElementName::CanClusterVariants)
        .and_then(|ccv| ccv.get_sub_element(ElementName::CanClusterConditional))?;

    if ccc
        .get_sub_element(ElementName::CanFdBaudrate)
        .and_then(|elem| elem.character_data())
        .is_some()
    {
        db.bustype = BusType::CanFd;
    } else {
        db.bustype = BusType::Can;
    }

    if let Some(channels) = ccc.get_sub_element(ElementName::PhysicalChannels) {
        for phys_channel in channels
            .sub_elements()
            .filter(|se| se.element_name() == ElementName::CanPhysicalChannel)
        {
            if let Some(frame_triggerings) =
                phys_channel.get_sub_element(ElementName::FrameTriggerings)
            {
                for ft in frame_triggerings.sub_elements() {
                    process_can_frame_triggering(&mut db, &ft);
                }
            }
        }
    }

    Some(db)
}

/// Estrae messaggio, segnali e relazioni da un `<CAN-FRAME-TRIGGERING>`.
fn process_can_frame_triggering(db: &mut CanDatabase, frame_triggering: &Element) {
    let frame = match frame_triggering
        .get_sub_element(ElementName::FrameRef)
        .and_then(|elem| elem.get_reference_target().ok())
    {
        Some(f) => f,
        None => return,
    };

    let frame_name: String = frame.item_name().unwrap_or_else(|| "CAN_Frame".to_string());
    let can_id: u32 = frame_triggering
        .get_sub_element(ElementName::Identifier)
        .and_then(|elem| elem.character_data())
        .and_then(|cdata| cdata.parse_integer::<u32>())
        .unwrap_or(0);
    let byte_length: u16 = frame
        .get_sub_element(ElementName::FrameLength)
        .and_then(|elem| elem.character_data())
        .and_then(|cdata| cdata.parse_integer::<u16>())
        .unwrap_or(0);

    let msg_key: CanMessageKey = ensure_message(db, &frame_name, can_id, byte_length);

    // Sender/receiver nodes
    let frame_ports: Vec<Element> = frame_triggering
        .get_sub_element(ElementName::FramePortRefs)
        .map(|elem| {
            elem.sub_elements()
                .filter(|se| se.element_name() == ElementName::FramePortRef)
                .filter_map(|fpr| fpr.get_reference_target().ok())
                .collect()
        })
        .unwrap_or_default();
    let (sender_ecus, receiver_ecus) = get_rx_tx_ecus(frame_ports);
    for ecu in sender_ecus {
        if let Some(nk) = ensure_node(db, &ecu) {
            let _ = db.add_sender_relation(msg_key, nk);
        }
    }

    // Signals mapped to this frame through its PDU mappings
    if let Some(mappings) = frame.get_sub_element(ElementName::PduToFrameMappings) {
        for pdu_mapping in mappings.sub_elements() {
            if let Some(pdu) = pdu_mapping
                .get_sub_element(ElementName::PduRef)
                .and_then(|pduref| pduref.get_reference_target().ok())
            {
                collect_isignal_mappings(db, msg_key, &pdu, &receiver_ecus);
            }
        }
    }
}

/// Converte un `<I-SIGNAL-I-PDU>` (o contenitori annidati) in segnali DBC.
fn collect_isignal_mappings(
    db: &mut CanDatabase,
    msg_key: CanMessageKey,
    pdu: &Element,
    receiver_ecus: &[String],
) {
    for native_sender in native_senders_of_pdu(pdu) {
        if let Some(nk) = ensure_node(db, &native_sender) {
            let _ = db.add_sender_relation(msg_key, nk);
        }
    }

    if pdu.element_name() == ElementName::ISignalIPdu || pdu.element_name() == ElementName::NmPdu {
        // NM-PDU condivide la stessa struttura di mapping degli I-SIGNAL-I-PDU
        process_isignal_ipdu(db, msg_key, pdu, receiver_ecus);
    } else if pdu.element_name() == ElementName::NPdu {
        process_npdu(db, msg_key, pdu);
    }
}

/// Processa un `I-SIGNAL-I-PDU` (o NM-PDU) convertendo i mapping in segnali DBC.
fn process_isignal_ipdu(
    db: &mut CanDatabase,
    msg_key: CanMessageKey,
    pdu: &Element,
    receiver_ecus: &[String],
) {
    let Some(mappings) = pdu
        .get_sub_element(ElementName::ISignalToPduMappings)
        .or_else(|| pdu.get_sub_element(ElementName::ISignalToIPduMappings))
    else {
        return;
    };

    for mapping in mappings.sub_elements() {
        let Some(signal_elem) = mapping
            .get_sub_element(ElementName::ISignalRef)
            .and_then(|elem| elem.get_reference_target().ok())
        else {
            continue;
        };

        let sig_name: String = signal_elem.item_name().unwrap_or_default();
        let bit_start: u16 = mapping
            .get_sub_element(ElementName::StartPosition)
            .and_then(|elem| elem.character_data())
            .and_then(|cdata| cdata.parse_integer::<u16>())
            .unwrap_or(0);
        let bit_length: u16 = signal_elem
            .get_sub_element(ElementName::Length)
            .and_then(|elem| elem.character_data())
            .and_then(|cdata| cdata.parse_integer::<u16>())
            .unwrap_or(0);
        let endian: Endianness = match mapping
            .get_sub_element(ElementName::PackingByteOrder)
            .and_then(|elem| elem.character_data())
        {
            Some(CharacterData::Enum(EnumItem::MostSignificantByteFirst)) => Endianness::Motorola,
            Some(CharacterData::Enum(EnumItem::MostSignificantByteLast)) => Endianness::Intel,
            Some(CharacterData::Enum(EnumItem::Opaque)) => Endianness::Intel, // treat opaque byte order as linear/Intel for fitting check
            _ => Endianness::Motorola,
        };

        let min: f64 = 0.0;
        let mut max: f64 = 0.0;
        if bit_length > 0 {
            // intervallo massimo assumendo segnale unsigned
            let max_raw: u64 = if bit_length < 64 {
                (1u64 << bit_length) - 1
            } else {
                u64::MAX
            };
            max = max_raw as f64;
        }

        let comment: Option<String> = extract_desc(&signal_elem);

        let sig_key = db.add_signal(&sig_name, endian, Signess::Unsigned, 1.0, 0.0, min, max, "");
        if let Some(signal) = db.get_sig_by_key_mut(sig_key) {
            signal.bit_start = bit_start;
            signal.bit_length = bit_length;
            if let Some(desc) = comment {
                signal.comment = desc;
            }
            signal.steps.clear();
            signal.compile_inline();
        }

        if db
            .add_msg_sig_relation(sig_key, msg_key, MuxRole::None, None)
            .is_ok()
        {
            for ecu in receiver_ecus {
                if let Some(nk) = ensure_node(db, ecu) {
                    let _ = db.add_sig_receiver_node(sig_key, nk);
                }
            }
        }
    }
}

/// Ricava le ECU trasmettenti/riceventi dai `<FRAME-PORT-REF>`.
fn get_rx_tx_ecus(frame_ports: Vec<Element>) -> (Vec<String>, Vec<String>) {
    let mut sender_ecus = Vec::new();
    let mut receiver_ecus = Vec::new();
    for fp in frame_ports {
        if let Some(CharacterData::Enum(direction)) = fp
            .get_sub_element(ElementName::CommunicationDirection)
            .and_then(|elem| elem.character_data())
        {
            match direction {
                EnumItem::In => {
                    if let Some(name) = ecu_of_frame_port(&fp) {
                        receiver_ecus.push(name);
                    }
                }
                EnumItem::Out => {
                    if let Some(name) = ecu_of_frame_port(&fp) {
                        sender_ecus.push(name);
                    }
                }
                _ => {}
            }
        }
    }
    (sender_ecus, receiver_ecus)
}

/// Risale l'arborescenza del frame port per ottenere il nome dell'ECU.
fn ecu_of_frame_port(frame_port: &Element) -> Option<String> {
    let ecu_comm_port_instance = frame_port.parent().ok()??;
    let comm_connector = ecu_comm_port_instance.parent().ok()??;
    let connectors = comm_connector.parent().ok()??;
    let ecu_instance = connectors.parent().ok()??;
    ecu_instance.item_name()
}

/// Ensures a node with the given name exists, returning its key.
///
/// If the node already exists, its key is returned; otherwise the node is
/// created. Any creation errors (e.g., conflicting names) simply return `None`
/// to keep parsing resilient.
fn ensure_node(db: &mut CanDatabase, name: &str) -> Option<CanNodeKey> {
    if let Some(nk) = db.get_node_key_by_name(name) {
        return Some(nk);
    }
    match db.add_node(name) {
        Ok(nk) => Some(nk),
        Err(DatabaseError::NodeAlreadyExists { .. }) => db.get_node_key_by_name(name),
        Err(_) => None,
    }
}

/// Ensures a message exists, resolving by ID first and falling back to name.
///
/// When both ID and name are free, the message is created; otherwise the first
/// existing match is returned. A suffixed fallback name is used if creation
/// fails because of collisions.
fn ensure_message(db: &mut CanDatabase, name: &str, id: u32, dlc: u16) -> CanMessageKey {
    if let Some(k) = db.get_msg_key_by_id(id) {
        return k;
    }
    if let Some(k) = db.get_msg_key_by_name(name) {
        return k;
    }

    match db.add_message(name, id, dlc) {
        Ok(k) => k,
        Err(_) => {
            let fallback_name = format!("{name}_{id}");
            db.add_message(&fallback_name, id, dlc)
                .expect("fallback message creation failed")
        }
    }
}

/// Extracts native sender ECU names from AUTOSAR `ADMIN-DATA` blocks.
fn native_senders_of_pdu(pdu: &Element) -> Vec<String> {
    let Some(admin) = pdu.get_sub_element(ElementName::AdminData) else {
        return Vec::new();
    };
    let Some(sdgs) = admin.get_sub_element(ElementName::Sdgs) else {
        return Vec::new();
    };

    let mut senders: Vec<String> = Vec::new();

    for sdg in sdgs
        .sub_elements()
        .filter(|se| se.element_name() == ElementName::Sdg)
    {
        let gid = sdg
            .attribute_value(AttributeName::Gid)
            .and_then(text_from_cdata);
        if gid.as_deref() != Some("NativeSender") {
            continue;
        }
        for sd in sdg
            .sub_elements()
            .filter(|se| se.element_name() == ElementName::Sd)
        {
            let sd_gid = sd
                .attribute_value(AttributeName::Gid)
                .and_then(text_from_cdata);
            if sd_gid.as_deref() != Some("ECU") {
                continue;
            }
            if let Some(CharacterData::String(ecu_list)) = sd.character_data() {
                for entry in ecu_list.split(',') {
                    let trimmed = entry.trim();
                    if !trimmed.is_empty() {
                        senders.push(trimmed.to_string());
                    }
                }
            }
        }
    }

    senders
}

/// Flattens `<DESC>` content and concatenates textual parts into a single string.
fn extract_desc(elem: &Element) -> Option<String> {
    let desc = elem.get_sub_element(ElementName::Desc)?;
    let mut parts: Vec<String> = Vec::new();

    if let Some(text) = desc.character_data().and_then(text_from_cdata) {
        parts.push(text);
    }

    for child in desc.sub_elements() {
        if let Some(text) = child.character_data().and_then(text_from_cdata) {
            parts.push(text);
        }
    }

    if parts.is_empty() {
        None
    } else {
        Some(parts.join(" "))
    }
}

/// Returns the inner string when the character data is textual.
fn text_from_cdata(cdata: CharacterData) -> Option<String> {
    match cdata {
        CharacterData::String(s) => Some(s),
        _ => None,
    }
}

/// Converts a `N-PDU` into a synthetic signal occupying the full PDU length.
fn process_npdu(db: &mut CanDatabase, msg_key: CanMessageKey, pdu: &Element) {
    let msg_name = db
        .get_message_by_key(msg_key)
        .map(|m| m.name.clone())
        .unwrap_or_else(|| pdu.item_name().unwrap_or_default());

    let msg_dlc: u16 = db
        .get_message_by_key(msg_key)
        .map(|m| m.byte_length)
        .unwrap_or(0);

    let pdu_len_bytes: u16 = pdu
        .get_sub_element(ElementName::Length)
        .and_then(|elem| elem.character_data())
        .and_then(|cdata| cdata.parse_integer::<u16>())
        .unwrap_or(msg_dlc);

    let byte_len: u16 = if pdu_len_bytes > 0 {
        pdu_len_bytes
    } else {
        msg_dlc
    };
    let bit_length: u16 = byte_len.saturating_mul(8);
    let max: f64 = if bit_length == 0 {
        0.0
    } else if bit_length < 64 {
        ((1u64 << bit_length) - 1) as f64
    } else {
        u64::MAX as f64
    };

    let sig_key = db.add_signal(
        &msg_name,
        Endianness::Intel,
        Signess::Unsigned,
        1.0,
        0.0,
        0.0,
        max,
        "",
    );
    if let Some(signal) = db.get_sig_by_key_mut(sig_key) {
        signal.bit_start = 0;
        signal.bit_length = bit_length;
        signal.steps.clear();
        signal.compile_inline();
    }

    let _ = db.add_msg_sig_relation(sig_key, msg_key, MuxRole::None, None);
}