def read_process(intf, is_isotp, end, auto_end, test, lock, queue):
    '''Process to read to CAN interface

    :param intf: the socket can interface
    :param is_isotp: to indicate if the write is at isotp stack level
    :param end: the flag to indicate the end of the process
    :param auto_end: RFU
    :param test: the flag to indicate the end of the test
    :param lock: RFU
    :param queue: the queue to exchange data
    '''
    read_ctx = context.create_ctx(channel=intf,
                                  bustype=BusType.SOCKETCAN,
                                  bitrate=0,
                                  canid_recv=0,
                                  canid_send=0,
                                  timeout=1,
                                  trace=1)
    while not end.value:
        if test.value:
            read_data = []
            while True:
                if is_isotp:
                    ret, msg = isotp.read(read_ctx)
                else:
                    ret, msg = vcan.read(read_ctx)
                if msg is None:
                    break
                else:
                    data = [msg.arbitration_id] + list(msg.data)
                    read_data.append(list(data))
            if read_data:
                queue.put(read_data)
        else:
            time.sleep(1)
Beispiel #2
0
def virtualECU(ctx):
    limit = 0
    while limit < 5:
        (ret, message) = vcan.read(ctx)
        if ret != 0 or message == None:
            limit = limit + 1
            time.sleep(0.1)
            continue

        if message.arbitration_id != ctx.canid_recv():
            print("read message not forme")
            continue

        if len(message.data) >= 2:
            data_send = array(
                'B', [0x03, 0x7F, message.data[0x01], 0x11, 0, 0, 0, 0])
            if message.data[0x01] == 0x10:
                if message.data[0x02] == 0x1 or message.data[
                        0x02] == 0x2 or message.data[0x02] == 0x3:
                    data_send = array(
                        'B', [0x02, 0x50, message.data[0x02], 0, 0, 0, 0, 0])
                else:
                    data_send = array(
                        'B',
                        [0x03, 0x7F, message.data[0x01], 0x12, 0, 0, 0, 0])
            vcan.write(ctx, can.Message(data=data_send))
def virtualECU(ctx):
    virtualECUIsRun = True 
    while virtualECUIsRun:
        (ret, message) = vcan.read(ctx)
        if ret == 0 and message != None and len(message.data) > 3:
                data_recv = message.data
                data_send = array('B', [0x03, 0x7F, 0x10, 0x12, 0, 0, 0, 0])
                if data_recv[0x01] == 0x10 and data_recv[0x02] == 0x01:
                    data_send = array('B', [0x02, 0x50, 0x01, 0, 0, 0, 0, 0])
                vcan.write(ctx,can.Message(data = data_send))
                virtualECUIsRun = False
        time.sleep(0.01)
def ecu_process(intf, end, test, lock):
    ctx = context.create_ctx(channel=intf,
                             bustype=BusType.SOCKETCAN,
                             bitrate=0,
                             canid_recv=0x10,
                             canid_send=0x10,
                             timeout=2,
                             trace=1)
    check = 0
    lock.acquire()
    while not end.value:
        if check != 3:
            ret, msg = vcan.read(ctx)
            if msg is None:
                continue
            else:
                data = list(msg.data)
                # Generate an ECU response
                if data == [0x02, 0x10, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00]:
                    print("OK received single frame")
                    msg = can.Message(data=[0x30, 0x02, 0x14])
                    vcan.write(ctx, msg, 0x10)
                    check += 1
                elif data == [0x10, 0x0B, 0x27, 0x02, 0x01, 0x02, 0x03, 0x04]:
                    print("OK received Multiple frame (part 1)")
                    msg = can.Message(data=[0x30, 0x01, 0x14])
                    vcan.write(ctx, msg, 0x10)
                    check += 1
                elif data == [0x21, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00, 0x00]:
                    print("OK received Multiple frame (part 2)")
                    msg = can.Message(data=[0x30, 0x01, 0x14])
                    vcan.write(ctx, msg, 0x10)
                    check += 1
            continue
        else:
            lock.release()
            time.sleep(1)
            print("ECU send ISO TP start")
            vcan.write(
                ctx,
                can.Message(
                    data=[0x10, 0x0B, 0x27, 0x02, 0x01, 0x02, 0x03, 0x04]))
            vcan.write(
                ctx,
                can.Message(
                    data=[0x21, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00, 0x00]))
            break
Beispiel #5
0
def virtualECU(ctx):
    global VALUE
    virtualECUIsRun = True
    while virtualECUIsRun:
        (ret, message) = vcan.read(ctx)
        if ret == 0 and message != None and len(message.data) > 3:
            data_recv = message.data
            if data_recv[0x01] == 0x2E and data_recv[
                    0x02] == 0x12 and data_recv[0x03] == 0x34:
                VALUE = data_recv[0x04]
                data_send = array('B',
                                  [0x04, 0x2E + 0x40, 0x12, 0x34, 0, 0, 0, 0])
                vcan.write(ctx, can.Message(data=data_send))
            if data_recv[0x01] == 0x2E and data_recv[
                    0x02] == 0x12 and data_recv[0x03] == 0x34:
                data_send = array(
                    'B', [0x04, 0x22 + 0x40, 0x12, 0x34, VALUE, 0, 0, 0])
                vcan.write(ctx, can.Message(data=data_send))
                virtualECUIsRun = False
        time.sleep(0.01)
def _canread_process(intf, intfs, calibration, end_process):
    intf_phy = intf
    intf_virt = intfs[intf]['channel']

    print("canread_process: START [physical={} virtual={}]".format(
        intf_phy, intf_virt))

    # Create the read context
    try:
        ctx_read = context.create_ctx(channel=intf_virt,
                                      bustype=BusType.SOCKETCAN,
                                      bitrate=intfs[intf]['bitrate'],
                                      canid_recv=0,
                                      canid_send=0,
                                      timeout=2,
                                      trace=0)
    except:
        print("Error creating context on interface {}".format(intf_virt))
        return

    while not end_process.value:
        ret, msg = vcan.read(ctx_read)
        if msg is not None:
            canid = msg.arbitration_id

            if len(msg.data) == 0:
                print("R: {} [0x{:03x} - empty]".format(
                    intf_phy, msg.arbitration_id))
                continue

            # Check if GW write the same message on the same interface recently
            found = False
            for e in write_cache:
                # Clean very old cache
                try:
                    if e['time'] > msg.timestamp + CACHE_TIMEOUT:
                        write_cache.remove(e)
                    elif e['msg']['intf'] == intf_phy and \
                         e['msg']['canid'] == msg.arbitration_id and \
                         e['msg']['data'] == msg.data:
                        found = True
                        write_cache.remove(e)
                        break
                except ValueError:
                    break

            if found:
                if verbose:
                    print("R: {} [0x{:03x} - 0x{}] (from GW)".format(
                        intf_phy, msg.arbitration_id,
                        codecs.encode(msg.data, 'hex')))
                continue

            print("R: {} [0x{:03x} - 0x{}]".format(
                intf_phy, msg.arbitration_id, codecs.encode(msg.data, 'hex')))
            # for each output interface
            for k, v in intfs.items():

                # Ignore the same interface
                if k == intf_phy:
                    continue

                # Check rules
                if cal.calibration_matches(calibration, intf_phy, k,
                                           msg.arbitration_id, list(msg.data)):
                    out = "    F: {} -> {} [0x{:03x} - 0x{}]".format(
                        intf_phy, k, msg.arbitration_id,
                        codecs.encode(msg.data, 'hex'))
                    print(colored(out, 'green'))
                    forward_queue.put({
                        'intf': k,
                        'canid': msg.arbitration_id,
                        'data': msg.data
                    })
Beispiel #7
0
def _can_process(intf, intfs, calibration, end_validation, sync, gctx_op,
                 gctx_data, gdata):
    intf_phy = intf
    intf_virt = intfs[intf]['channel']

    print("can_process: START [physical={} virtual={}]".format(
        intf_phy, intf_virt))

    # Create the read/write context
    ctx = context.create_ctx(channel=intf_virt,
                             port_nr=intfs[intf]['port_nr'],
                             bustype=intfs[intf]['bustype'],
                             bitrate=intfs[intf]['bitrate'],
                             canid_recv=0,
                             canid_send=0,
                             timeout=0.010,
                             trace=0)
    if ctx == None:
        print("Error creating context on interface {}".format(intf_virt))
        return

    # Wait for synch processes started
    sync.wait()

    while not end_validation.value:
        # Wait for synch for updated context, before operation
        sync.wait()
        dataReceived = gdata[0].copy()
        if gctx_op[intf] and len(gctx_data[intf]) > 0:
            data = list(gctx_data[intf])

            ctx.set_canid_send(data[0])
            msg = can.Message(data=data[1:],
                              arbitration_id=data[0],
                              dlc=len(data[1:]))
            vcan.write(ctx, msg)
        else:
            for i in range(2):
                ret, msg = vcan.read(ctx)
                if msg is not None and msg.dlc >= len(dataReceived):
                    size = len(dataReceived)
                    match = True

                    for i in range(size):
                        if dataReceived[i] != msg.data[i]:
                            match = False

                    if match == True:
                        gctx_data[intf] = [
                            msg.arbitration_id,
                        ] + list(msg.data)
                        break
                    else:
                        print("{} R: {} [{}] expected [{}]".format(
                            colored('[WARNING]', 'yellow'), intf,
                            ','.join(hex(x) for x in msg.data),
                            ','.join(hex(x) for x in dataReceived)))

        # Wait for synch for end operation
        sync.wait()

    print("can_process: END [physical={} virtual={}]".format(
        intf_phy, intf_virt))
    lcanid = list()

    if ctx == None:
        context.output("Error initialization")
        sys.exit(-1)

    for i in range(0, 0x7ff):
        ctx.set_canid_send(i)

        vcan.write(
            ctx,
            can.Message(data=[0x02, 0x10, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff],
                        arbitration_id=i))
        time.sleep(0.10)

        ret, msg = vcan.read(ctx)

        while msg != None:
            if msg != None and msg.dlc >= 3 and msg.data[0] > 1 and msg.data[
                    0] < 8 and (
                        (msg.data[0x1] == 0x50 and msg.data[0x2] == 0x01)
                        or msg.data[0x1] == 0x7F):
                context.output("Receive id " + hex(msg.arbitration_id) +
                               " data " + hex_array(msg.data) +
                               " with canid " + hex(i))
                lcanid.append([i, msg.arbitration_id])
            ret, msg = vcan.read(ctx)

    for item in lcanid:
        context.output(
            "UDS service detected (canid_send=%3x, canid_receive=%3x)" %