Example #1
0
 def test_parse_signal(self, line, signal_name, start_bit, length, little_endian, signed, factor, offset, value_min,\
                       value_max, unit, is_multiplexer, multiplexer_id, nodes, add_multiplexer):
     parser = DBCParser()
     parser._can_network.add_node(CANNode('Sender'))
     for node in nodes:
         parser._can_network.add_node(CANNode(node))
     msg = CANMessage(2, 'CANMessage', 8)
     if add_multiplexer:
         msg.add_signal(CANSignal('MultiplexerSignal', 8, 8, is_multiplexer=True))
     parser._can_network.nodes['Sender'].add_message(msg)
     parser._mode = ('MESSAGE', msg)
     parser._parse_line(line)
     sig = msg.signals[signal_name]
     assert sig.name == signal_name
     assert sig.start_bit == start_bit
     assert sig.length == length
     assert sig.little_endian == little_endian
     assert sig.signed == signed
     assert sig.factor == factor
     assert sig.value_min == value_min
     assert sig.value_max == value_max
     assert sig.unit == unit
     assert sig.is_multiplexer == is_multiplexer
     assert sig.multiplexer_id == multiplexer_id
     for node in nodes:
         assert parser._can_network.nodes[node] in sig.receiver
Example #2
0
 def test_get_signal(self):
     cn = CANNetwork()
     node = CANNode('TestNode')
     msg = CANMessage(1234, 'Message', 8)
     node.add_message(msg)
     sig = CANSignal('Signal', 0, 8)
     msg.add_signal(sig)
     cn.add_node(node)
     assert cn.get_signal(can_id=1234, name='Signal') == sig
Example #3
0
 def test_parse_signal_desc(self, line, can_id, signal_name, expected_desc):
     parser = DBCParser()
     node = CANNode('Node')
     msg = CANMessage(can_id, 'Message', 8)
     sig = CANSignal(signal_name, 0, 8)
     msg.add_signal(sig)
     node.add_message(msg)
     parser._can_network.add_node(node)
     parser._parse_line(line)
     assert sig.description == expected_desc
Example #4
0
    def test_parse_val(self, line, msg_id, signal_name, expected_dict, val_table_name):
        parser = DBCParser()
        if val_table_name:
            parser._can_network.add_value_dict(val_table_name, expected_dict)

        node = CANNode('Node')
        parser._can_network.add_node(node)
        msg = CANMessage(msg_id, 'Message', 1)
        node.add_message(msg)
        sig = CANSignal(signal_name, 0, 8)
        msg.add_signal(sig)

        parser._parse_line(line)

        assert sig.value_dict == expected_dict
Example #5
0
 def test_signal_fit_check(self, msg_size, new_sig_def, existing_sigs_def,
                           expected_result):
     msg = CANMessage(1, 'Message', msg_size)
     for i, sig_def in enumerate(existing_sigs_def):
         msg.add_signal(
             CANSignal('Signal{}'.format(i),
                       sig_def[0],
                       sig_def[1],
                       is_multiplexer=sig_def[2],
                       multiplexer_id=sig_def[3]))
     assert msg._check_if_signal_fits(
         CANSignal('NewSignal',
                   new_sig_def[0],
                   new_sig_def[1],
                   is_multiplexer=new_sig_def[2],
                   multiplexer_id=new_sig_def[3])) == expected_result
Example #6
0
 def test_get_message(self):
     cn = CANNetwork()
     node = CANNode('TestNode')
     msg = CANMessage(1234, 'Message', 8)
     node.add_message(msg)
     cn.add_node(node)
     assert cn.get_message(can_id=1234) == msg
Example #7
0
 def test_parse_message_desc(self, line, can_id, expected_desc):
     parser = DBCParser()
     node = CANNode('Node0')
     node.add_message(CANMessage(can_id, 'CANMessage', 8))
     parser._can_network.add_node(node)
     parser._parse_line(line)
     assert parser._can_network.nodes['Node0'].messages[1234].description == expected_desc
Example #8
0
 def test_signal_multiplexer_settings_check(self, old_sig_is_m,
                                            old_sig_m_id, new_sig_is_m,
                                            new_sig_m_id, expected_result):
     msg = CANMessage(1, 'Message', 2)
     msg.add_signal(
         CANSignal('OldSignal',
                   0,
                   8,
                   multiplexer_id=old_sig_m_id,
                   is_multiplexer=old_sig_is_m))
     assert msg._check_if_multiplexer_settings_are_valid(
         CANSignal('OldSignal',
                   0,
                   8,
                   multiplexer_id=new_sig_m_id,
                   is_multiplexer=new_sig_is_m)) == expected_result
Example #9
0
 def test_get_consumed_messages(self):
     cn = CANNetwork()
     cn.add_node(CANNode('SendingNode'))
     cn.add_node(CANNode('ReceivingNode'))
     cn.nodes['SendingNode'].add_message(CANMessage(1, 'Message', 1))
     sig = CANSignal('Signal', 0, 8)
     sig.add_receiver(cn.nodes['ReceivingNode'])
     cn.nodes['SendingNode'].messages[1].add_signal(sig)
     assert cn.nodes['SendingNode'].messages[1] in cn.get_consumed_messages(
         cn.nodes['ReceivingNode'])
Example #10
0
 def test_int_two_signals(self):
     msg = CANMessage(1, 'Name', 2)
     sig0 = CANSignal('Signal0', 0, 8)
     sig0.raw_value = 159
     msg.add_signal(sig0)
     sig1 = CANSignal('Signal1', 8, 8)
     sig1.raw_value = 96
     msg.add_signal(sig1)
     assert int(msg) == int(sig0.bits) + (int(sig1.bits) << 8)
Example #11
0
    def _parse_message(self, message_str):
        """Parses a message string and updates the CANBus

        Args:
            message_str: String with message informations
        """
        reg = re.search(
            'BO_\s+(?P<can_id>\d+)\s+(?P<name>\S+)\s*:\s*(?P<length>\d+)\s+(?P<sender>\S+)',
            message_str)
        message = CANMessage(int(reg.group('can_id')),
                             reg.group('name').strip(),
                             int(reg.group('length')))
        self._can_network.nodes[reg.group('sender').strip()].add_message(
            message)
        self._mode = ('MESSAGE', message)
Example #12
0
    def test_parse_attribute(self, line, expected_value, attr_definition, cfg_dict):
        parser = DBCParser()
        parser._can_network.attributes.add_definition(attr_definition)

        if attr_definition.obj_type == CANNetwork:
            can_object = parser._can_network
        if attr_definition.obj_type in [CANNode, CANMessage, CANSignal]:
            cfg_dict['NODE'] = CANNode(cfg_dict['NODE'])
            parser._can_network.add_node(cfg_dict['NODE'])
            can_object = cfg_dict['NODE']
        if attr_definition.obj_type in [CANMessage, CANSignal]:
            cfg_dict['MESSAGE'] = CANMessage(cfg_dict['MESSAGE'], 'Message', 1)
            cfg_dict['NODE'].add_message(cfg_dict['MESSAGE'])
            can_object = cfg_dict['MESSAGE']
        if attr_definition.obj_type in [CANSignal]:
            cfg_dict['SIGNAL'] = CANSignal(cfg_dict['SIGNAL'], 0, 8)
            cfg_dict['MESSAGE'].add_signal(cfg_dict['SIGNAL'])
            can_object = cfg_dict['SIGNAL']

        parser._parse_line(line)
        assert can_object.attributes[attr_definition.name].value == expected_value
Example #13
0
 def test_signal_fit_check_fail(self):
     msg = CANMessage(1, 'Message', 1)
     with pytest.raises(RuntimeError):
         msg.add_signal(CANSignal('Signal', 0, 10))
Example #14
0
 def test_set_message_sender(self):
     node = CANNode('TestNode')
     node.add_message(CANMessage(1, 'Name', 8))
     assert node.messages[1].sender == node
Example #15
0
 def test_add_message_with_sender(self):
     msg = CANMessage(1, 'Name', 8)
     msg.sender = CANNode('TestNode')
     node = CANNode('TestNode')
     with pytest.raises(RuntimeError):
         node.add_message(msg)
Example #16
0
 def test_add_signal(self):
     msg = CANMessage(1, 'Name', 8)
     sig = CANSignal('Signal', 0, 8)
     msg.add_signal(sig)
     assert 'Signal' in msg.signals
     assert sig.parent == msg
Example #17
0
 def test_add_signal_with_message(self):
     sig = CANSignal('Signal', 0, 8)
     sig.message = CANMessage(1, 'Name', 8)
     msg = CANMessage(2, 'Name', 8)
     with pytest.raises(RuntimeError):
         msg.add_signal(sig)
Example #18
0
 def test_int(self):
     msg = CANMessage(1, 'Name', 1)
     sig = CANSignal('Signal', 0, 8)
     sig.raw_value = 100
     msg.add_signal(sig)
     assert int(msg) == 100
Example #19
0
 def test_int_splitted(self):
     msg = CANMessage(1, 'Name', 2)
     sig = CANSignal('Signal', 4, 8)
     sig.raw_value = 100
     msg.add_signal(sig)
     assert int(msg) == int(sig.bits) << 4
Example #20
0
 def test_signal_multiplexer_settings_check_fail(self):
     msg = CANMessage(1, 'Message', 1)
     with pytest.raises(RuntimeError):
         msg.add_signal(CANSignal('Signal', 0, 8, multiplexer_id=1))
Example #21
0
 def test_add_message(self):
     node = CANNode('TestNode')
     msg = CANMessage(1, 'Name', 6)
     node.add_message(msg)
     assert 1 in node.messages
     assert msg.parent == node