Skip to content

Commit dbe6bd5

Browse files
committed
prove of concept for flexray dump (#432)
1 parent 41d0dad commit dbe6bd5

File tree

3 files changed

+110
-6
lines changed

3 files changed

+110
-6
lines changed

examples/fr_dump.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env python3
2+
import canmatrix.formats
3+
import sys
4+
cluster = canmatrix.formats.loadp(sys.argv[1], decode_flexray = True)
5+
6+
for cm in cluster:
7+
for frame in cluster[cm]:
8+
frame_info = "{}-{}-{}".format(frame.slot_id, frame.base_cycle, frame.repitition_cycle)
9+
for pdu in frame.pdus:
10+
for signal in pdu.signals:
11+
sig_group = pdu.get_signal_group_for_signal(signal)
12+
sig_group = "None" if sig_group is None else sig_group.name
13+
print("{}: {}, {}, {}, {}, {}".format(frame_info, frame.size, pdu.pdu_type, pdu.name, sig_group, signal.name))
14+

src/canmatrix/canmatrix.py

+83
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,73 @@ def __eq__(self, other):
721721
)
722722
)
723723

724+
@attr.s(cmp=False)
725+
class Pdu(object):
726+
"""
727+
Represents a PDU.
728+
729+
PDUs are hierarchical groups of signals which are needed to represent Flexray busses
730+
Whereas a PDU is the same than a frame on CAN bus, at flexray a frame may consist of
731+
multiple PDUs (a bit like multiple signal layout for multiplexed can frames).
732+
This class is only used for flexray busses.
733+
"""
734+
735+
name = attr.ib(default="") # type: str
736+
size = attr.ib(default=0) # type: int
737+
triggering_name = attr.ib(default="") # type: str
738+
pdu_type = attr.ib(default="") # type: str
739+
port_type = attr.ib(default="") # type: str
740+
signals = attr.ib(factory=list) # type: typing.MutableSequence[Signal]
741+
signalGroups = attr.ib(factory=list) # type: typing.MutableSequence[SignalGroup]
742+
743+
def add_signal(self, signal):
744+
# type: (Signal) -> Signal
745+
"""
746+
Add Signal to Pdu.
747+
748+
:param Signal signal: Signal to be added.
749+
:return: the signal added.
750+
"""
751+
self.signals.append(signal)
752+
return self.signals[len(self.signals) - 1]
753+
def add_signal_group(self, Name, Id, signalNames):
754+
# type: (str, int, typing.Sequence[str]) -> None
755+
"""Add new SignalGroup to the Frame. Add given signals to the group.
756+
757+
:param str Name: Group name
758+
:param int Id: Group id
759+
:param list of str signalNames: list of Signal names to add. Non existing names are ignored.
760+
"""
761+
newGroup = SignalGroup(Name, Id)
762+
self.signalGroups.append(newGroup)
763+
for signal in signalNames:
764+
signal = signal.strip()
765+
if signal.__len__() == 0:
766+
continue
767+
signalId = self.signal_by_name(signal)
768+
if signalId is not None:
769+
newGroup.add_signal(signalId)
770+
771+
def get_signal_group_for_signal(self, signal_to_find):
772+
for signal_group in self.signalGroups:
773+
for signal in signal_group:
774+
if signal == signal_to_find:
775+
return signal_group
776+
return None
777+
778+
def signal_by_name(self, name):
779+
# type: (str) -> typing.Union[Signal, None]
780+
"""
781+
Get signal by name.
782+
783+
:param str name: signal name to be found.
784+
:return: signal with given name or None if not found
785+
"""
786+
for signal in self.signals:
787+
if signal.name == name:
788+
return signal
789+
return None
790+
724791

725792
@attr.s(cmp=False)
726793
class Frame(object):
@@ -764,6 +831,9 @@ class Frame(object):
764831
# ('cycleTime', '_cycleTime', int, None),
765832
# ('sendType', '_sendType', str, None),
766833

834+
pdus = attr.ib(factory=list) # type: typing.MutableSequence[Pdu]
835+
836+
767837
@property
768838
def is_multiplexed(self): # type: () -> bool
769839
"""Frame is multiplexed if at least one of its signals is a multiplexer."""
@@ -879,6 +949,7 @@ def attribute(self, attribute_name, db=None, default=None):
879949

880950
def __iter__(self): # type: () -> typing.Iterator[Signal]
881951
"""Iterator over all signals."""
952+
882953
return iter(self.signals)
883954

884955
def add_signal_group(self, Name, Id, signalNames):
@@ -912,6 +983,18 @@ def signal_group_by_name(self, name):
912983
return signalGroup
913984
return None
914985

986+
def add_pdu(self, pdu):
987+
# type: (Pdu) -> Pdu
988+
"""
989+
Add Pdu to Frame.
990+
991+
:param Pdu pdu: Pdu to be added.
992+
:return: the pdu added.
993+
"""
994+
self.pdus.append(pdu)
995+
return self.pdus[len(self.pdus) - 1]
996+
997+
915998
def add_signal(self, signal):
916999
# type: (Signal) -> Signal
9171000
"""

src/canmatrix/formats/arxml.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -1631,30 +1631,37 @@ def decode_flexray_helper(root, root_or_cache, ns, float_factory):
16311631
physical_channels = fc.findall('.//' + ns + "FLEXRAY-PHYSICAL-CHANNEL")
16321632
for pc in physical_channels:
16331633
db = canmatrix.CanMatrix()
1634+
db.is_flexray = True
16341635
db.add_signal_defines("LongName", 'STRING')
16351636
channel_name = get_element_name(pc, ns)
16361637
found_matrixes[channel_name] = db
16371638

16381639
frames = pc.findall('.//' + ns + "FLEXRAY-FRAME-TRIGGERING")
16391640
for frame in frames:
1641+
frame_counter += 1
16401642
slot_id = int(get_child(frame, "SLOT-ID", root_or_cache, ns).text)
1641-
base_cycle = get_child(frame, "BASE-CYCLE", root_or_cache, ns)
1643+
base_cycle = get_child(frame, "BASE-CYCLE", root_or_cache, ns).text
16421644
ipdu_triggerings = get_children(frame, "I-PDU-TRIGGERING", root_or_cache, ns)
1643-
frame_repetition_cycle = find_children_by_path(frame, "CYCLE-REPETITION/CYCLE-REPETITION", root_or_cache, ns)[0]
1645+
frame_repetition_cycle = find_children_by_path(frame, "CYCLE-REPETITION/CYCLE-REPETITION", root_or_cache, ns)[0].text
16441646
network_endpoints = pc.findall('.//' + ns + "NETWORK-ENDPOINT")
16451647
frame_size = int(find_children_by_path(frame, "FRAME/FRAME-LENGTH", root_or_cache, ns)[0].text)
1648+
frame = canmatrix.Frame(size = frame_size, arbitration_id = frame_counter)
1649+
frame.slot_id = slot_id
1650+
frame.base_cycle = base_cycle
1651+
frame.repitition_cycle = frame_repetition_cycle.replace("CYCLE-REPETITION-","")
1652+
db.add_frame(frame)
16461653
for ipdu_triggering in ipdu_triggerings:
16471654
ipdu_triggering_name = get_element_name(ipdu_triggering, ns)
16481655
ipdu = get_child(ipdu_triggering, "I-PDU", root_or_cache, ns)
16491656
pdu_type = get_child(ipdu_triggering, "I-PDU-REF", root_or_cache, ns).attrib["DEST"]
16501657
ipdu_length = int(get_child(ipdu, "LENGTH", root_or_cache, ns).text)
16511658
pdu_port_type = get_child(ipdu_triggering, "I-PDU-PORT-REF", root_or_cache, ns).attrib["DEST"]
16521659
ipdu_name = get_element_name(ipdu, ns)
1653-
frame_counter += 1;
1654-
target_frame = canmatrix.Frame(name = ipdu_name, arbitration_id=frame_counter, size=ipdu_length)
1660+
target_pdu = canmatrix.Pdu(name = ipdu_name, size=ipdu_length,
1661+
triggering_name = ipdu_triggering_name, pdu_type=pdu_type, port_type=pdu_port_type)
16551662
pdu_sig_mapping = get_children(ipdu, "I-SIGNAL-TO-I-PDU-MAPPING", root_or_cache, ns)
1656-
get_signals(pdu_sig_mapping, target_frame, root_or_cache, ns, None, float_factory)
1657-
db.add_frame(target_frame)
1663+
get_signals(pdu_sig_mapping, target_pdu, root_or_cache, ns, None, float_factory)
1664+
frame.add_pdu(target_pdu)
16581665
return found_matrixes
16591666

16601667
def decode_can_helper(root, root_or_cache, ns, float_factory, ignore_cluster_info):

0 commit comments

Comments
 (0)