python-doipclient

repository·main·Indexed 42 days ago

https://github.com/jacobschaer/python-doipclient

A pure Python 3 implementation of the ISO-13400 (2019) Diagnostic over IP (DoIP) protocol. It serves as a transport layer for automotive Ethernet communication and provides a connector for the udsoncan library to perform Unified Diagnostic Services (UDS) tasks.

Tokens
4.3K
Snippets
14
Records
24
Agent score
53%

What's inside python-doipclient

  1. Manage DoIP connection lifecycle and reconnection

    main

    When performing operations that cause an ECU to reset (such as ecu_reset), the DoIP socket may need to be reactivated. You can manually call doip_client.reconnect() after the ECU has finished restarting. Alternatively, you can use the auto_reconnect_tcp flag on the DoIPClient to handle this automatically.

    Note: ECUs do not restart instantly; you may need to implement a sleep() before attempting to reconnect or send further commands.

  2. Configure Windows IP and Firewall settings for DoIP

    main

    When using Windows, specific configurations are required to ensure successful communication, especially when using await_vehicle_announcement or relying on UDP broadcasts:

    • Subnet Mask: Ensure your IPv4 settings are correctly configured. A common mask is 255.255.255.0, allowing the ECU to broadcast to x.x.x.255.
    • Firewall: You may need to reconfigure or disable the Windows Defender Firewall on your Guest/Public network to allow DoIP traffic.
    • Troubleshooting: Use Wireshark in promiscuous mode to monitor the interface if connection issues persist.
  3. Discover ECUs using Vehicle Identification Response

    main

    Alternatively, you can actively request a Vehicle Identification Response message from an ECU using DoIPClient.get_entity(). This returns the network address and the announcement details containing the logical address.

    from doipclient import DoIPClient
    address, announcement = DoIPClient.get_entity()
    logical_address = announcement.logical_address
    ip, port = address
    print(ip, port, logical_address)
  4. Connect directly to ECUs using Automotive Ethernet

    main

    ECUs communicating via DoIP typically use Automotive Ethernet (100BASE-T1 or BroadR-Reach), which uses a single twisted pair and operates in master/slave pairs.

    Because this differs from the multi-pair 100BASE-TX/1000BASE-T standard used by desktop computers, you must use a media converter (such as the Intrepid RAD-Moon) to bridge the connection to a standard computer. No activation line is required for direct ECU connections.

  5. Connect to DoIP via the OBD2 Port

    main

    DoIP can be accessed through the OBD2/J1962 connector using one of two pinout configurations defined by ISO-13400-4. To initiate a connection, the tester must apply +5V to Pin 8 (Activation).

    Pinout Options:

    Option 1:

    • Pin 3 (RX+)
    • Pin 11 (RX-)
    • Pin 12 (TX+)
    • Pin 13 (TX-)
    • Pin 8 (Activation)

    Option 2:

    • Pin 1 (RX+)
    • Pin 9 (RX-)
    • Pin 12 (TX+)
    • Pin 13 (TX-)
    • Pin 8 (Activation)

    To determine which option is in use, sense the resistance between Pin 8 (Activation) and Pin 5 (Signal Ground). These configurations provide standard IEEE 802.3 100BASE-TX (Fast Ethernet).

  6. Configure IP settings for DoIP communication

    main

    After establishing the physical Ethernet connection, you must configure the IP layer.

    1. DHCP: Many DoIP implementations (especially via OBD2) use DHCP. Configure your computer's network adapter to use DHCP to negotiate an address automatically.
    2. Static IP: If DHCP is not present, you must manually configure your network adapter. Use a tool like Wireshark in promiscuous mode to monitor UDP broadcast messages or TCP traffic to identify the correct subnet and an available IP address.
  7. Use the DoIPClient connector with udsoncan

    main

    The doipclient library provides a connector class designed to allow the udsoncan library to communicate over DoIP (Diagnostics over IP). This allows you to use standard udsoncan diagnostic services while using python-doipclient as the underlying transport layer.

    Note: This connector is intended to be merged into the udsoncan library in the future. If you are using python-uds, a similar adapter is not currently provided but may be developed if demand arises.

  8. Install python-uds with DoIP support

    main

    To use python-uds with DoIP support, you must currently use a specific fork. Follow these steps to install it from source:

    1. Clone the fork.
    2. Checkout the doip branch.
    3. Install via pip.
    git checkout doip
    cd python-uds
    pip install .
    git clone https://github.com/jacobschaer/python-uds
    git checkout doip
    cd python-uds
    pip install .
  9. Discover ECUs using Vehicle Identification Announcement

    main

    You can discover ECUs on your network by listening for the Vehicle Identification Announcement broadcast message, which is typically sent by the ECU at power-up. Use DoIPClient.await_vehicle_announcement() to wait for this broadcast. Note that you may need to power cycle your ECU and wait a few seconds for the broadcast to be received.

    from doipclient import DoIPClient
    address, announcement = DoIPClient.await_vehicle_announcement()
    # Power cycle your ECU and wait for a few seconds for the broadcast to be
    # received
    logical_address = announcement.logical_address
    ip, port = address
    print(ip, port, logical_address)
  10. Discover ECU IP and Logical Address

    main

    If you do not know the ECU's IP address or logical address, you can use the following methods to discover them:

    1. await_vehicle_announcement(): Listens for UDP Vehicle Announcement messages broadcast by the ECU upon startup. Returns a tuple of (IP Address, VehicleAnnouncementMessage).
    2. get_entity(): Sends a VehicleIdentificationRequest (optionally with a VIN or EID) and awaits a VehicleIdentificationResponse. This can be called as a class method without instantiating a client first.

    get_entity parameters:

    • ecu_ip_address (str): Target IP for unicast. Defaults to broadcast (255.255.255.255).
    • protocol_version (int): DoIP protocol version.
    • eid (bytes, optional): EID of the Vehicle.
    • vin (str, optional): VIN of the Vehicle.
    from doipclient.client import DoIPClient
    
    # Discover ECU via broadcast
    addr, message = DoIPClient.await_vehicle_announcement(ipv6=False)
    print(f"ECU found at {addr}")
    
    # Discover ECU via VIN
    response = DoIPClient.get_entity(vin="YOUR_VIN_HERE")
    print(f"ECU Logical Address: {response.logical_address}")
  11. Troubleshooting DoIP connectivity

    main

    When establishing a connection with a DoIP-enabled ECU, keep the following potential blockers in mind:

    • ICMP/Ping: Do not assume standard features like ping will work; ECUs may not implement them.
    • Custom Implementations: Vendors may deviate from ISO-13400 with custom encryption or non-standard IP handling.
    • MAC Filtering: Production ECUs might filter connections based on MAC addresses. If blocked, you may need to spoof the MAC address of a known authorized device.
    • Firewalls: Always check if local desktop firewalls are blocking the connection.