Esempio n. 1
0
def init_socket(scan_params):
    print("Initializing socket for " + scan_params.channel)
    try:
        sock = CANSocket(scan_params.channel)
    except Exception as e:
        sys.stderr.write("\nSocket could not be created: " + str(e) + "\n")
        sys.exit(1)
    sock.basecls = XCPOnCAN
    return sock
Esempio n. 2
0
def get_can_socket(channel, interface, python_can_args):
    if PYTHON_CAN:
        if python_can_args:
            arg_dict = dict((k, literal_eval(v)) for k, v in
                            (pair.split('=') for pair in
                             re.split(', | |,', python_can_args)))
            return CANSocket(bustype=interface, channel=channel, **arg_dict)
        else:
            return CANSocket(bustype=interface, channel=channel)
    else:
        return CANSocket(channel=channel)
Esempio n. 3
0
def create_socket(python_can_args, interface, channel):

    if PYTHON_CAN:
        if python_can_args:
            interface_string = "CANSocket(bustype=" \
                               "'%s', channel='%s', %s)" % \
                               (interface, channel, python_can_args)
            arg_dict = dict(
                (k, literal_eval(v))
                for k, v in (pair.split('=')
                             for pair in re.split(', | |,', python_can_args)))
            sock = CANSocket(bustype=interface, channel=channel, **arg_dict)
        else:
            interface_string = "CANSocket(bustype=" \
                               "'%s', channel='%s')" % \
                               (interface, channel)
            sock = CANSocket(bustype=interface, channel=channel)
    else:
        sock = CANSocket(channel=channel)
        interface_string = "\"%s\"" % channel

    return sock, interface_string
def main():
    extended = False
    piso = False
    verbose = False
    sniff_time = 100
    noise_listen_time = 2
    start = None
    end = None
    channel = None
    interface = None
    bitrate = None

    options = getopt.getopt(sys.argv[1:], 'vxCt:n:i:c:b:s:e:h', [
        'verbose', 'noise_listen_time=', 'sniff_time=', 'interface=', 'piso',
        'channel=', 'bitrate=', 'start=', 'end=', 'help', 'extended'
    ])

    try:
        for opt, arg in options[0]:
            if opt in ('-v', '--verbose'):
                verbose = True
            elif opt in ('-x', '--extended'):
                extended = True
            elif opt in ('-C', '--piso'):
                piso = True
            elif opt in ('-h', '--help'):
                usage()
                sys.exit(-1)
            elif opt in ('-t', '--sniff_time'):
                sniff_time = int(arg)
            elif opt in ('-n', '--noise_listen_time'):
                noise_listen_time = int(arg)
            elif opt in ('-i', '--interface'):
                interface = arg
            elif opt in ('-c', '--channel'):
                channel = arg
            elif opt in ('-b', '--bitrate'):
                bitrate = int(arg)
            elif opt in ('-s', '--start'):
                start = int(arg, 16)
            elif opt in ('-e', '--end'):
                end = int(arg, 16)
    except getopt.GetoptError as msg:
        usage()
        print("ERROR:", msg, file=sys.stderr)
        raise SystemExit

    if PYTHON_CAN:
        if start is None or \
                end is None or \
                channel is None or \
                bitrate is None or \
                interface is None:
            usage()
            print("\nPlease provide all required arguments.\n",
                  file=sys.stderr)
            sys.exit(-1)
    else:
        if start is None or \
                end is None or \
                channel is None:
            usage()
            print("\nPlease provide all required arguments.\n",
                  file=sys.stderr)
            sys.exit(-1)

    if end >= 0x800:
        print("end must be < 0x800.", file=sys.stderr)
        sys.exit(-1)

    if end < start:
        print("start must be smaller than end.", file=sys.stderr)
        sys.exit(-1)

    if PYTHON_CAN:
        import can
        try:
            can.rc['interface'] = interface
            can.rc['channel'] = channel
            can.rc['bitrate'] = bitrate
            scan_interface = can.interface.Bus()
            interface_string = "CANSocket(iface=can.interface.Bus(bustype=" \
                               "'%s', channel='%s', bitrate=%d))" % \
                               (interface, channel, bitrate)
        except Exception as e:
            usage()
            print("\nCheck python-can interface assignment.\n",
                  file=sys.stderr)
            print(e, file=sys.stderr)
            sys.exit(-1)
    else:
        scan_interface = channel
        interface_string = "\"%s\"" % channel

    try:
        sock = CANSocket(iface=scan_interface)
    except Exception as e:
        usage()
        print("\nSocket couldn't be created. Check your arguments.\n",
              file=sys.stderr)
        print(e, file=sys.stderr)
        sys.exit(-1)

    if verbose:
        print("Start scan (%s - %s)" % (hex(start), hex(end)))

    signal.signal(signal.SIGINT, signal_handler)

    result = ISOTPScan(sock,
                       range(start, end + 1),
                       extended_addressing=extended,
                       noise_listen_time=noise_listen_time,
                       sniff_time=float(sniff_time) / 1000,
                       output_format="code" if piso else "text",
                       can_interface=interface_string,
                       verbose=verbose)

    print("Scan: \n%s" % result)
Esempio n. 5
0
    keep_awake = True
    awake_interface = args.k

# Dictionary with Send-to-ID as key and a tuple (received packet, Recv_ID)
found_packets = {}
# List with paket-IDs of background noise packets
noise_IDs = []
# Seconds to listen to noise
noise_listen_time = 10
# Number of pakets send in a single blow
extended_load = 100

dummy_pkt = CAN(identifier=0x123, data=b'\xaa\xbb\xcc\xdd\xee\xff\xaa\xbb')

# Interface for communication
CanSocket_communication = CANSocket(scan_interface)

# Keep ECU awake
if keep_awake:
    CanSocket_keep_awake = CANSocket(awake_interface)
    awake_thread = isotpscan.KeepAwakeThread(CanSocket_keep_awake, dummy_pkt)
    awake_thread.start()

# Listen for default messages on CAN-bus
print("Filtering background noise...")
CanSocket_communication.sniff(
    prn=get_background_noise_callback,
    timeout=noise_listen_time,
    started_callback=lambda: CanSocket_communication.send(dummy_pkt))

# delete duplicates
Esempio n. 6
0
def main():

    channel = None
    interface = None
    source = 0x7e0
    destination = 0x7df
    bitrate = None
    timeout = 0.1
    supported = False
    unsupported = False
    verbose = False

    options = getopt.getopt(
        sys.argv[1:],
        'i:c:s:d:b:t:hruv',
        ['interface=', 'channel=', 'source=', 'destination=', 'bitrate=',
         'help', 'timeout=', 'supported', 'unsupported', 'verbose'])

    try:
        for opt, arg in options[0]:
            if opt in ('-i', '--interface'):
                interface = arg
            elif opt in ('-c', '--channel'):
                channel = arg
            elif opt in ('-s', '--source'):
                source = int(arg, 16)
            elif opt in ('-d', '--destination'):
                destination = int(arg, 16)
            elif opt in ('-b', '--bitrate'):
                bitrate = int(arg)
            elif opt in ('-h', '--help'):
                usage()
                sys.exit(-1)
            elif opt in ('-r', '--supported'):
                supported = True
            elif opt in ('-u', '--unsupported'):
                unsupported = True
            elif opt in ('-v', '--verbose'):
                verbose = True
    except getopt.GetoptError as msg:
        usage()
        print("ERROR:", msg, file=sys.stderr)
        raise SystemExit

    if channel is None or \
            (PYTHON_CAN and (bitrate is None or interface is None)):
        usage()
        print("\nPlease provide all required arguments.\n",
              file=sys.stderr)
        sys.exit(-1)

    if 0 > source >= 0x800 or 0 > destination >= 0x800\
            or source == destination:
        print("The ids must be >= 0 and < 0x800 and not equal.",
              file=sys.stderr)
        sys.exit(-1)

    if 0 > timeout:
        print("The timeout must be a positive value")
        sys.exit(-1)

    csock = None
    try:
        if PYTHON_CAN:
            csock = CANSocket(bustype='socketcan', channel=channel,
                              bitrate=bitrate,)
        else:
            csock = CANSocket(channel)

        with ISOTPSocket(csock, source, destination, basecls=OBD,
                         padding=True) as isock:
            signal.signal(signal.SIGINT, signal_handler)
            obd_scan(isock, timeout, supported, unsupported, verbose)

    except Exception as e:
        usage()
        print("\nSocket couldn't be created. Check your arguments.\n",
              file=sys.stderr)
        print(e, file=sys.stderr)
        sys.exit(-1)

    finally:
        if csock is not None:
            csock.close()
Esempio n. 7
0
def main():

    channel = None
    interface = None
    source = 0x7e0
    destination = 0x7df
    timeout = 0.1
    full_scan = False
    specific_scan = False
    verbose = False
    python_can_args = None
    custom_enumerators = []
    conf.verb = -1

    options = getopt.getopt(sys.argv[1:], 'i:c:s:d:a:t:hfv1236789A', [
        'interface=', 'channel=', 'source=', 'destination=', 'help',
        'timeout=', 'python-can_args=', 'full', 'verbose'
    ])

    try:
        for opt, arg in options[0]:
            if opt in ('-i', '--interface'):
                interface = arg
            elif opt in ('-c', '--channel'):
                channel = arg
            elif opt in ('-a', '--python-can_args'):
                python_can_args = arg
            elif opt in ('-s', '--source'):
                source = int(arg, 16)
            elif opt in ('-d', '--destination'):
                destination = int(arg, 16)
            elif opt in ('-h', '--help'):
                usage(False)
                sys.exit(0)
            elif opt in ('-t', '--timeout'):
                timeout = float(arg)
            elif opt in ('-f', '--full'):
                full_scan = True
            elif opt == '-1':
                specific_scan = True
                custom_enumerators += [OBD_S01_Enumerator]
            elif opt == '-2':
                specific_scan = True
                custom_enumerators += [OBD_S02_Enumerator]
            elif opt == '-3':
                specific_scan = True
                custom_enumerators += [OBD_S03_Enumerator]
            elif opt == '-6':
                specific_scan = True
                custom_enumerators += [OBD_S06_Enumerator]
            elif opt == '-7':
                specific_scan = True
                custom_enumerators += [OBD_S07_Enumerator]
            elif opt == '-8':
                specific_scan = True
                custom_enumerators += [OBD_S08_Enumerator]
            elif opt == '-9':
                specific_scan = True
                custom_enumerators += [OBD_S09_Enumerator]
            elif opt == '-A':
                specific_scan = True
                custom_enumerators += [OBD_S0A_Enumerator]
            elif opt in ('-v', '--verbose'):
                verbose = True
    except getopt.GetoptError as msg:
        usage(True)
        print("ERROR:", msg, file=sys.stderr)
        raise SystemExit

    if channel is None or \
            (PYTHON_CAN and interface is None):
        usage(True)
        print("\nPlease provide all required arguments.\n", file=sys.stderr)
        sys.exit(1)

    if 0 > source >= 0x800 or 0 > destination >= 0x800\
            or source == destination:
        print("The ids must be >= 0 and < 0x800 and not equal.",
              file=sys.stderr)
        sys.exit(1)

    if 0 > timeout:
        print("The timeout must be a positive value")
        sys.exit(1)

    csock = None
    try:
        if PYTHON_CAN:
            if python_can_args:
                arg_dict = dict((k, literal_eval(v)) for k, v in (
                    pair.split('=')
                    for pair in re.split(', | |,', python_can_args)))
                csock = CANSocket(bustype=interface,
                                  channel=channel,
                                  **arg_dict)
            else:
                csock = CANSocket(bustype=interface, channel=channel)
        else:
            csock = CANSocket(channel=channel)

        with ISOTPSocket(csock, source, destination, basecls=OBD,
                         padding=True) as isock:
            signal.signal(signal.SIGINT, signal_handler)
            if specific_scan:
                es = custom_enumerators
            else:
                es = OBD_Scanner.default_enumerator_clss
            s = OBD_Scanner(isock,
                            enumerators=es,
                            full_scan=full_scan,
                            verbose=verbose,
                            timeout=timeout)
            print("Starting OBD-Scan...")
            s.scan()
            for e in s.enumerators:
                e.show()

    except Exception as e:
        usage(True)
        print("\nSocket couldn't be created. Check your arguments.\n",
              file=sys.stderr)
        print(e, file=sys.stderr)
        sys.exit(1)

    finally:
        if csock is not None:
            csock.close()
Esempio n. 8
0
def main():
    extended = False
    piso = False
    verbose = False
    extended_can_id = False
    sniff_time = 100
    noise_listen_time = 2
    start = None
    end = None
    channel = None
    interface = None
    bitrate = None

    log_handler = logging.StreamHandler(sys.stdout)
    log_handler.setLevel(logging.DEBUG)
    log_runtime.addHandler(log_handler)

    options = getopt.getopt(sys.argv[1:], 'vxCt:n:i:c:b:s:e:hw', [
        'verbose', 'noise_listen_time=', 'sniff_time=', 'interface=', 'piso',
        'channel=', 'bitrate=', 'start=', 'end=', 'help', 'extended',
        'extended_can_id'
    ])

    try:
        for opt, arg in options[0]:
            if opt in ('-v', '--verbose'):
                verbose = True
            elif opt in ('-x', '--extended'):
                extended = True
            elif opt in ('-C', '--piso'):
                piso = True
            elif opt in ('-h', '--help'):
                usage()
                sys.exit(-1)
            elif opt in ('-t', '--sniff_time'):
                sniff_time = int(arg)
            elif opt in ('-n', '--noise_listen_time'):
                noise_listen_time = int(arg)
            elif opt in ('-i', '--interface'):
                interface = arg
            elif opt in ('-c', '--channel'):
                channel = arg
            elif opt in ('-b', '--bitrate'):
                bitrate = int(arg)
            elif opt in ('-s', '--start'):
                start = int(arg, 16)
            elif opt in ('-e', '--end'):
                end = int(arg, 16)
            elif opt in '--extended_can_id':
                extended_can_id = True
    except getopt.GetoptError as msg:
        usage()
        warning("ERROR: %s" % msg)
        raise SystemExit

    if start is None or \
            end is None or \
            channel is None or \
            (PYTHON_CAN and (bitrate is None or interface is None)):
        usage()
        warning("\nPlease provide all required arguments.\n")
        sys.exit(-1)

    if end >= 2**29 or start >= 2**29:
        warning("Argument 'start' and 'end' must be < " + hex(2**29))
        sys.exit(-1)

    if not extended_can_id and (end >= 0x800 or start >= 0x800):
        warning("Standard can identifiers must be < 0x800.\n"
                "Use --extended_can_id option to scan with "
                "extended CAN identifiers.")
        sys.exit(-1)

    if end < start:
        warning("start must be equal or smaller than end.")
        sys.exit(-1)

    if PYTHON_CAN:
        import can
        try:
            can.rc['interface'] = interface
            can.rc['channel'] = channel
            can.rc['bitrate'] = bitrate
            scan_interface = can.interface.Bus()
            interface_string = "CANSocket(iface=can.interface.Bus(bustype=" \
                               "'%s', channel='%s', bitrate=%d))" % \
                               (interface, channel, bitrate)
        except Exception as e:
            usage()
            warning("\nCheck python-can interface assignment.\n")
            warning(e)
            sys.exit(-1)
    else:
        scan_interface = channel
        interface_string = "\"%s\"" % channel

    try:
        sock = CANSocket(iface=scan_interface)
    except Exception as e:
        usage()
        warning("\nSocket couldn't be created. Check your arguments.\n")
        warning(e)
        sys.exit(-1)

    if verbose:
        print("Start scan (%s - %s)" % (hex(start), hex(end)))

    signal.signal(signal.SIGINT, signal_handler)

    result = ISOTPScan(sock,
                       range(start, end + 1),
                       extended_addressing=extended,
                       noise_listen_time=noise_listen_time,
                       sniff_time=float(sniff_time) / 1000,
                       output_format="code" if piso else "text",
                       can_interface=interface_string,
                       extended_can_id=extended_can_id,
                       verbose=verbose)

    print("Scan: \n%s" % result)
def main():
    extended = False
    piso = False
    verbose = False
    extended_can_id = False
    sniff_time = 100
    noise_listen_time = 2
    start = None
    end = None
    channel = None
    interface = None
    python_can_args = None

    options = getopt.getopt(
        sys.argv[1:],
        'vxCt:n:i:c:a:s:e:h:w',
        ['verbose', 'noise_listen_time=', 'sniff_time=', 'interface=', 'piso',
         'channel=', 'python-can_args=', 'start=', 'end=', 'help', 'extended',
         'extended_can_id'])

    try:
        for opt, arg in options[0]:
            if opt in ('-v', '--verbose'):
                verbose = True
            elif opt in ('-x', '--extended'):
                extended = True
            elif opt in ('-C', '--piso'):
                piso = True
            elif opt in ('-h', '--help'):
                usage()
                sys.exit(-1)
            elif opt in ('-t', '--sniff_time'):
                sniff_time = int(arg)
            elif opt in ('-n', '--noise_listen_time'):
                noise_listen_time = int(arg)
            elif opt in ('-i', '--interface'):
                interface = arg
            elif opt in ('-c', '--channel'):
                channel = arg
            elif opt in ('-a', '--python-can_args'):
                python_can_args = arg
            elif opt in ('-s', '--start'):
                start = int(arg, 16)
            elif opt in ('-e', '--end'):
                end = int(arg, 16)
            elif opt in '--extended_can_id':
                extended_can_id = True
    except getopt.GetoptError as msg:
        usage()
        print("ERROR:", msg, file=sys.stderr)
        raise SystemExit

    if start is None or \
            end is None or \
            channel is None or \
            (PYTHON_CAN and interface is None):
        usage()
        print("\nPlease provide all required arguments.\n", file=sys.stderr)
        sys.exit(-1)

    if end >= 2**29 or start >= 2**29:
        print("Argument 'start' and 'end' must be < " + hex(2**29),
              file=sys.stderr)
        sys.exit(-1)

    if not extended_can_id and (end >= 0x800 or start >= 0x800):
        print("Standard can identifiers must be < 0x800.\n"
              "Use --extended_can_id option to scan with "
              "extended CAN identifiers.",
              file=sys.stderr)
        sys.exit(-1)

    if end < start:
        print("start must be equal or smaller than end.", file=sys.stderr)
        sys.exit(-1)

    sock = None

    try:
        if PYTHON_CAN:
            if python_can_args:
                interface_string = "CANSocket(bustype=" \
                                   "'%s', channel='%s', %s)" % \
                                   (interface, channel, python_can_args)
                arg_dict = dict((k, literal_eval(v)) for k, v in
                                (pair.split('=') for pair in
                                 re.split(', | |,', python_can_args)))
                sock = CANSocket(bustype=interface, channel=channel,
                                 **arg_dict)
            else:
                interface_string = "CANSocket(bustype=" \
                                   "'%s', channel='%s')" % \
                                   (interface, channel)
                sock = CANSocket(bustype=interface, channel=channel)
        else:
            sock = CANSocket(channel=channel)
            interface_string = "\"%s\"" % channel

        if verbose:
            print("Start scan (%s - %s)" % (hex(start), hex(end)))

        signal.signal(signal.SIGINT, signal_handler)

        result = ISOTPScan(sock,
                           range(start, end + 1),
                           extended_addressing=extended,
                           noise_listen_time=noise_listen_time,
                           sniff_time=float(sniff_time) / 1000,
                           output_format="code" if piso else "text",
                           can_interface=interface_string,
                           extended_can_id=extended_can_id,
                           verbose=verbose)

        print("Scan: \n%s" % result)

    except Exception as e:
        usage()
        print("\nSocket couldn't be created. Check your arguments.\n",
              file=sys.stderr)
        print(e, file=sys.stderr)
        sys.exit(-1)

    finally:
        if sock is not None:
            sock.close()
Esempio n. 10
0
# Get arguments
args = []
for i in range(0, len(sys.argv)):
    args.append(sys.argv[i])
if "-e" in args:
    extended = True
if "-k" in args:
    awake_interface = True
if "-eo" in args:
    extended_only = True

dummy_pkt = CAN(identifier=0x123, data=b'\xaa\xff\xff\xff\xff\xff\xff\xee')

# Interface
CanSocket_communication = CANSocket("can1")

# Keep ECU awake
if awake_interface:
    CanSocket_keep_awake = CANSocket("can0")
    awake_thread = keep_awake_thread(dummy_pkt)
    awake_thread.start()

# Listen for default messages on CAN-bus
print("Filtering background noise...")
CanSocket_communication.sniff(prn=get_background_noise,
                              timeout=noise_listen_time,
                              started_callback=lambda:
                              CanSocket_communication.send(dummy_pkt))

# scan with normal ID's
Esempio n. 11
0
def main():

    channel = None
    interface = None
    source = 0x7e0
    destination = 0x7df
    timeout = 0.1
    supported = False
    unsupported = False
    verbose = False
    python_can_args = None

    options = getopt.getopt(sys.argv[1:], 'i:c:s:d:a:t:hruv', [
        'interface=', 'channel=', 'source=', 'destination=', 'help',
        'timeout=', 'python-can_args=', 'supported', 'unsupported', 'verbose'
    ])

    try:
        for opt, arg in options[0]:
            if opt in ('-i', '--interface'):
                interface = arg
            elif opt in ('-c', '--channel'):
                channel = arg
            elif opt in ('-a', '--python-can_args'):
                python_can_args = arg
            elif opt in ('-s', '--source'):
                source = int(arg, 16)
            elif opt in ('-d', '--destination'):
                destination = int(arg, 16)
            elif opt in ('-h', '--help'):
                usage(False)
                sys.exit(0)
            elif opt in ('-t', '--timeout'):
                timeout = float(arg)
            elif opt in ('-r', '--supported'):
                supported = True
            elif opt in ('-u', '--unsupported'):
                unsupported = True
            elif opt in ('-v', '--verbose'):
                verbose = True
    except getopt.GetoptError as msg:
        usage(True)
        print("ERROR:", msg, file=sys.stderr)
        raise SystemExit

    if channel is None or \
            (PYTHON_CAN and interface is None):
        usage(True)
        print("\nPlease provide all required arguments.\n", file=sys.stderr)
        sys.exit(1)

    if 0 > source >= 0x800 or 0 > destination >= 0x800\
            or source == destination:
        print("The ids must be >= 0 and < 0x800 and not equal.",
              file=sys.stderr)
        sys.exit(1)

    if 0 > timeout:
        print("The timeout must be a positive value")
        sys.exit(1)

    csock = None
    try:
        if PYTHON_CAN:
            if python_can_args:
                arg_dict = dict((k, literal_eval(v)) for k, v in (
                    pair.split('=')
                    for pair in re.split(', | |,', python_can_args)))
                csock = CANSocket(bustype=interface,
                                  channel=channel,
                                  **arg_dict)
            else:
                csock = CANSocket(bustype=interface, channel=channel)
        else:
            csock = CANSocket(channel=channel)

        with ISOTPSocket(csock, source, destination, basecls=OBD,
                         padding=True) as isock:
            signal.signal(signal.SIGINT, signal_handler)
            obd_scan(isock, timeout, supported, unsupported, verbose)

    except Exception as e:
        usage(True)
        print("\nSocket couldn't be created. Check your arguments.\n",
              file=sys.stderr)
        print(e, file=sys.stderr)
        sys.exit(1)

    finally:
        if csock is not None:
            csock.close()