Beispiel #1
0
def test_wait_for_intf_remote_wakeup(can_in_interface, can_out_interface):
    """Verifies wait_for_intf_remote_wakeup does not fail catastrophically.

    We can at least see how long it takes us to wait.  Longer term, we should
    switch the test to start waiting and then call ``input_session.start`` and
    ensure it unblocks after that call.

    To see the wait time, run py.test with ``-s``_.

    Assumes test_frames.test_queued_loopback works.
    """
    database_name = 'NIXNET_example'
    cluster_name = 'CAN_Cluster'
    frame_name = 'CANEventFrame1'

    with nixnet.FrameInQueuedSession(
            can_in_interface,
            database_name,
            cluster_name,
            frame_name) as input_session:
        input_session.start()
        input_session.intf.can_tcvr_state = constants.CanTcvrState.SLEEP
        assert input_session.can_comm.sleep

        with pytest.raises(errors.XnetError) as excinfo:
            input_session.wait_for_intf_remote_wakeup(5)
        assert excinfo.value.error_type == constants.Err.EVENT_TIMEOUT
Beispiel #2
0
def test_queued_loopback(can_in_interface, can_out_interface):
    database_name = 'NIXNET_example'
    cluster_name = 'CAN_Cluster'
    frame_name = 'CANEventFrame1'

    with nixnet.FrameInQueuedSession(can_in_interface, database_name,
                                     cluster_name,
                                     frame_name) as input_session:
        with nixnet.FrameOutQueuedSession(can_out_interface, database_name,
                                          cluster_name,
                                          frame_name) as output_session:
            # Start the input session manually to make sure that the first
            # frame value sent before the initial read will be received.
            input_session.start()

            payload_list = [2, 4, 8, 16]
            expected_frames = [
                types.CanFrame(66, constants.FrameType.CAN_DATA,
                               bytes(bytearray(payload_list)))
            ]
            output_session.frames.write(expected_frames)

            # Wait 1 s and then read the received values.
            # They should be the same as the ones sent.
            time.sleep(1)

            actual_frames = list(input_session.frames.read(1))
            assert len(expected_frames) == len(actual_frames)
            for i, (expected,
                    actual) in enumerate(zip(expected_frames, actual_frames)):
                assert_can_frame(i, expected, actual)
Beispiel #3
0
def test_wait_for_intf_communicating(can_in_interface):
    """Verifies wait_for_intf_communicating does not catastrophically fail.

    Considering the wait time is so short, it'd be hard to verify it,
    especially in a reproducible way.

    Assumes test_frames.test_queued_loopback works.
    """
    database_name = 'NIXNET_example'
    cluster_name = 'CAN_Cluster'
    frame_name = 'CANEventFrame1'

    with nixnet.FrameInQueuedSession(
            can_in_interface,
            database_name,
            cluster_name,
            frame_name) as input_session:
        input_session.start()
        input_session.wait_for_intf_communicating()

        expected_frames = []
        actual_frames = list(input_session.frames.read(1))
        assert len(expected_frames) == len(actual_frames)
        for i, (expected, actual) in enumerate(zip(expected_frames, actual_frames)):
            assert_can_frame(i, expected, actual)
def test_frames_container(nixnet_in_interface):
    database_name = 'NIXNET_example'
    cluster_name = 'CAN_Cluster'
    frame_name = 'CANEventFrame1'

    with nixnet.FrameInQueuedSession(nixnet_in_interface, database_name,
                                     cluster_name,
                                     frame_name) as input_session:
        assert frame_name in input_session.frames
        assert 0 in input_session.frames

        assert len(input_session.frames) == 1
        frames = list(input_session.frames)
        assert len(frames) == 1
        frame = frames[0]

        assert str(frame) == frame_name
        assert int(frame) == 0

        assert frame == input_session.frames[0]
        assert frame == input_session.frames[frame_name]
        with pytest.raises(IndexError):
            input_session.frames[1]
        with pytest.raises(KeyError):
            input_session.frames["<random>"]

        assert frame == input_session.frames.get(0)
        assert frame == input_session.frames.get(frame_name)
        assert input_session.frames.get(1) is None
        assert input_session.frames.get("<random>") is None
Beispiel #5
0
def test_sleep_transition(can_in_interface):
    database_name = 'NIXNET_example'
    cluster_name = 'CAN_Cluster'
    frame_name = 'CANEventFrame1'

    with nixnet.FrameInQueuedSession(
            can_in_interface,
            database_name,
            cluster_name,
            frame_name) as input_session:
        assert not input_session.can_comm.sleep

        with pytest.raises(errors.XnetError) as excinfo:
            input_session.intf.can_tcvr_state = constants.CanTcvrState.SLEEP
        assert excinfo.value.error_type == constants.Err.SESSION_NOT_STARTED
        assert not input_session.can_comm.sleep

        input_session.start()
        assert not input_session.can_comm.sleep

        input_session.intf.can_tcvr_state = constants.CanTcvrState.SLEEP
        assert input_session.can_comm.sleep

        input_session.intf.can_tcvr_state = constants.CanTcvrState.NORMAL
        assert not input_session.can_comm.sleep
def test_session_j1939_properties(can_in_interface):
    database_name = 'NIXNET_example'
    cluster_name = 'J1939_Over_CAN'
    frame_name = 'J1939_Gloabal_Event_HighPrio'

    with nixnet.FrameInQueuedSession(can_in_interface, database_name,
                                     cluster_name,
                                     frame_name) as input_session:
        # todo: Add testing to cover more J1939 properties.
        input_session.j1939.include_dest_addr_in_pgn = True
        assert input_session.j1939.include_dest_addr_in_pgn
def test_intf_container(nixnet_in_interface):
    database_name = 'NIXNET_example'
    cluster_name = 'CAN_Cluster'
    frame_name = 'CANEventFrame1'

    with nixnet.FrameInQueuedSession(nixnet_in_interface, database_name,
                                     cluster_name,
                                     frame_name) as input_session:
        assert str(input_session.intf) == nixnet_in_interface
        assert input_session.intf == nixnet_in_interface
        assert input_session.intf != "<random>"
Beispiel #8
0
def test_wait_for_transmit_complete(can_in_interface, can_out_interface):
    """Verifies wait_for_transmit_complete does not fail catastrophically.

    We can at least see how long it takes us to wait.  Longer term, we should
    switch the test to start waiting and then call ``input_session.start`` and
    ensure it unblocks after that call.

    To see the wait time, run py.test with ``-s``_.

    Assumes test_frames.test_queued_loopback works.
    """
    database_name = 'NIXNET_example'
    cluster_name = 'CAN_Cluster'
    frame_name = 'CANEventFrame1'

    with nixnet.FrameInQueuedSession(
            can_in_interface,
            database_name,
            cluster_name,
            frame_name) as input_session:
        with nixnet.FrameOutQueuedSession(
                can_out_interface,
                database_name,
                cluster_name,
                frame_name) as output_session:
            output_session.auto_start = False
            input_session.start()

            expected_frames = [
                types.CanFrame(66, constants.FrameType.CAN_DATA, b'\x01\x02\x03\x04'),
                types.CanFrame(66, constants.FrameType.CAN_DATA, b'\x05\x06\x08\x09'),
                types.CanFrame(66, constants.FrameType.CAN_DATA, b'\x01\x02\x03\x04'),
                types.CanFrame(66, constants.FrameType.CAN_DATA, b'\x05\x06\x08\x09'),
                types.CanFrame(66, constants.FrameType.CAN_DATA, b'\x01\x02\x03\x04'),
                types.CanFrame(66, constants.FrameType.CAN_DATA, b'\x05\x06\x08\x09'),
                types.CanFrame(66, constants.FrameType.CAN_DATA, b'\x01\x02\x03\x04'),
                types.CanFrame(66, constants.FrameType.CAN_DATA, b'\x05\x06\x08\x09'),
                types.CanFrame(66, constants.FrameType.CAN_DATA, b'\x01\x02\x03\x04'),
                types.CanFrame(66, constants.FrameType.CAN_DATA, b'\x05\x06\x08\x09'),
                types.CanFrame(66, constants.FrameType.CAN_DATA, b'\x01\x02\x03\x04'),
                types.CanFrame(66, constants.FrameType.CAN_DATA, b'\x05\x06\x08\x09')]

            output_session.start()
            initial = time.time()
            output_session.frames.write(expected_frames)
            written = time.time()
            output_session.wait_for_transmit_complete(10)
            finished = time.time()

            print("Write took {} s".format(written - initial))
            print("Wait took {} s".format(finished - written))
Beispiel #9
0
def test_disconnect_terminals_failures(can_in_interface):
    """Verifies disconnect_terminals fails when expected to."""
    database_name = 'NIXNET_example'
    cluster_name = 'CAN_Cluster'
    frame_name = 'CANEventFrame1'

    with nixnet.FrameInQueuedSession(
            can_in_interface,
            database_name,
            cluster_name,
            frame_name) as input_session:
        with pytest.raises(errors.XnetError) as excinfo:
            input_session.disconnect_terminals("FrontPanel0", "FrontPanel1")
        assert excinfo.value.error_type in [
            constants.Err.SYNCHRONIZATION_NOT_ALLOWED,
            constants.Err.INVALID_SYNCHRONIZATION_COMBINATION]
Beispiel #10
0
def test_session_frame_container(can_in_interface):
    database_name = 'NIXNET_example'
    cluster_name = 'CAN_Cluster'
    frame_name = 'CANEventFrame1'

    with nixnet.FrameInQueuedSession(can_in_interface, database_name,
                                     cluster_name,
                                     frame_name) as input_session:
        frames = input_session.frames
        assert len(frames) == 1, "Pre-requisite failed"
        frame = frames[0]

        assert frame == frame
        assert not (frame == 5)

        assert not (frame != frame)
        assert frame != 5

        print(repr(frame))
Beispiel #11
0
def test_flush_baseline(can_in_interface, can_out_interface):
    """Demonstrate that the non-flush version of the code works.

    Assumes test_frames.test_queued_loopback works.
    """
    database_name = 'NIXNET_example'
    cluster_name = 'CAN_Cluster'
    frame_name = 'CANEventFrame1'

    with nixnet.FrameInQueuedSession(
            can_in_interface,
            database_name,
            cluster_name,
            frame_name) as input_session:
        with nixnet.FrameOutQueuedSession(
                can_out_interface,
                database_name,
                cluster_name,
                frame_name) as output_session:
            output_session.auto_start = False
            input_session.start()

            dropped_frames = [
                types.CanFrame(66, constants.FrameType.CAN_DATA, b'\x01\x02\x03\x04')]
            output_session.frames.write(dropped_frames)

            expected_frames = [
                types.CanFrame(66, constants.FrameType.CAN_DATA, b'\x05\x06\x08\x09')]
            output_session.frames.write(expected_frames)

            expected_frames = dropped_frames + expected_frames

            output_session.start()
            # Wait 1 s and then read the received values.
            # They should be the same as the ones sent.
            time.sleep(1)

            actual_frames = list(input_session.frames.read(2))
            assert len(expected_frames) == len(actual_frames)
            for i, (expected, actual) in enumerate(zip(expected_frames, actual_frames)):
                assert_can_frame(i, expected, actual)
Beispiel #12
0
def test_intf_container(can_in_interface):
    database_name = 'NIXNET_example'
    cluster_name = 'CAN_Cluster'
    frame_name = 'CANEventFrame1'

    with nixnet.FrameInQueuedSession(can_in_interface, database_name,
                                     cluster_name,
                                     frame_name) as input_session:
        assert input_session.intf == input_session.intf
        assert input_session.intf == can_in_interface
        assert not (input_session.intf == "<random>")
        assert not (input_session.intf == 5)

        assert not (input_session.intf != input_session.intf)
        assert not (input_session.intf != can_in_interface)
        assert input_session.intf != "<random>"
        assert input_session.intf != 5

        assert str(input_session.intf) == can_in_interface

        print(repr(input_session.intf))
Beispiel #13
0
def test_start_explicit(can_in_interface, can_out_interface):
    """Demonstrate that data is properly sent out on an explicit start.

    Assumes test_frames.test_queued_loopback works
    """
    database_name = 'NIXNET_example'
    cluster_name = 'CAN_Cluster'
    frame_name = 'CANEventFrame1'

    with nixnet.FrameInQueuedSession(
            can_in_interface,
            database_name,
            cluster_name,
            frame_name) as input_session:
        with nixnet.FrameOutQueuedSession(
                can_out_interface,
                database_name,
                cluster_name,
                frame_name) as output_session:
            output_session.auto_start = False
            # Start the input session manually to make sure that the first
            # frame value sent before the initial read will be received.
            input_session.start()

            expected_frames = [
                types.CanFrame(66, constants.FrameType.CAN_DATA, b'\x01\x02\x03\x04')]
            output_session.frames.write(expected_frames)

            output_session.start()
            # Wait 1 s and then read the received values.
            # They should be the same as the ones sent.
            time.sleep(1)

            actual_frames = list(input_session.frames.read(1))
            assert len(expected_frames) == len(actual_frames)
            for i, (expected, actual) in enumerate(zip(expected_frames, actual_frames)):
                assert_can_frame(i, expected, actual)
def main():
    database_name = 'NIXNET_example'
    cluster_name = 'CAN_Cluster'
    input_frame = 'CANEventFrame1'
    output_frame = 'CANEventFrame1'
    interface1 = 'CAN1'
    interface2 = 'CAN2'

    with nixnet.FrameInQueuedSession(interface1, database_name, cluster_name,
                                     input_frame) as input_session:
        with nixnet.FrameOutQueuedSession(interface2, database_name,
                                          cluster_name,
                                          output_frame) as output_session:
            terminated_cable = six.moves.input(
                'Are you using a terminated cable (Y or N)? ')
            if terminated_cable.lower() == "y":
                input_session.intf.can_term = constants.CanTerm.ON
                output_session.intf.can_term = constants.CanTerm.OFF
            elif terminated_cable.lower() == "n":
                input_session.intf.can_term = constants.CanTerm.ON
                output_session.intf.can_term = constants.CanTerm.ON
            else:
                print("Unrecognised input ({}), assuming 'n'".format(
                    terminated_cable))
                input_session.intf.can_term = constants.CanTerm.ON
                output_session.intf.can_term = constants.CanTerm.ON

            # Start the input session manually to make sure that the first
            # frame value sent before the initial read will be received.
            input_session.start()

            user_value = six.moves.input('Enter payload [int, int]: ')
            try:
                payload_list = [int(x.strip()) for x in user_value.split(",")]
            except ValueError:
                payload_list = [2, 4, 8, 16]
                print('Unrecognized input ({}). Setting data buffer to {}',
                      user_value, payload_list)

            id = 0
            extended = False
            payload = bytearray(payload_list)
            frame = types.CanFrame(id, extended, constants.FrameType.CAN_DATA,
                                   payload)

            i = 0
            while True:
                for index, byte in enumerate(payload):
                    payload[index] = byte + i

                frame.payload = payload
                output_session.frames.write_can([frame])
                print('Sent frame with ID %s payload: %s' % (id, payload))

                # Wait 1 s and then read the received values.
                # They should be the same as the ones sent.
                time.sleep(1)

                count = 1
                frames = input_session.frames.read_can(count)
                for frame in frames:
                    print('Received frame: ')
                    pp.pprint(frame)

                i += 1
                if max(payload) + i > 0xFF:
                    i = 0

                inp = six.moves.input('Hit enter to continue (q to quit): ')
                if inp.lower() == 'q':
                    break

            print('Data acquisition stopped.')