Python Concurrency & Architecture

The Python implementation (v2xflexstack) is designed for flexibility, rapid iterative prototyping, and clean separation of concerns. This document details its concurrency architecture, threading patterns, and Service Access Point (SAP) interaction mechanics.

Callback-Driven Event Model

Vehicular communications are intrinsically event-driven: wireless packets arrive asynchronously, GNSS position vectors are refreshed at variable frequencies (1–10 Hz), and safety events trigger sudden alert bursts.

To prevent blocking execution threads, v2xflexstack implements a reactive callback-driven architecture:

flowchart BT
    LL["<b>RawLinkLayer</b><br/>OS raw packet socket (PF_PACKET)"]
    GN["<b>GeoNetworking Router</b><br/>Location Table & Neighbor Discovery"]
    BTP["<b>BTP Router</b><br/>Port Multiplexing (BTP-A / BTP-B)"]
    FAC["<b>Facilities / Services</b><br/>CAM, DENM, VAM, LDM"]

    LL -->|"receive_callback: gn_data_indicate"| GN
    GN -->|"indication callback: btp_data_indication"| BTP
    BTP -->|"per-port callback"| FAC
  1. Upstream Ingress Flow (Indications): - The LinkLayer background thread listens on the raw socket or C-V2X radio interface. - When a frame with the GeoNetworking EtherType arrives, it passes the packet to its receive_callback, the router’s gn_data_indicate. - The GeoNetworking router evaluates the packet (updating its LocationTable and neighbor positions) and fires the registered BTP callback. - The BTP router demultiplexes based on destination port and invokes the registered facility callback (e.g. CAM or DENM service).

  2. Downstream Egress Flow (Requests): - When a facility service triggers a packet (e.g. a CAM generation interval expires or a road hazard is detected), it issues a BTPDataRequest. - BTP constructs the BTP-A or BTP-B header and forwards a GNDataRequest to the GeoNetworking router. - GeoNetworking builds the headers for the packet transport type, assigns the sequence number, signs the packet with the SignService when security is enabled, and calls send on its link layer.

Strategy Pattern on the LinkLayer

The physical and MAC layer access is decoupled from protocol routing using the Strategy Pattern:

classDiagram
    class LinkLayer {
        <<abstract>>
        +Callable receive_callback
        +send(bytes packet)
        +receive()
    }
    class RawLinkLayer {
        +str iface
        +bytes mac_address
        +socket sock
        +send(bytes packet)
        +receive()
    }
    class PythonCV2XLinkLayer {
        +CV2XLinkLayer link_layer
        +send(bytes packet)
        +receive()
    }
    LinkLayer <|-- RawLinkLayer : implements
    LinkLayer <|-- PythonCV2XLinkLayer : implements
  • Base Class (LinkLayer): Declares common lifecycle and I/O interface send(packet), receive(), and holds the receive_callback.

  • RawLinkLayer: Interfaces directly with Linux PF_PACKET sockets, handling EtherType 0x8947 over IEEE 802.11p or Ethernet interfaces.

  • PythonCV2XLinkLayer: Interfaces with the Qualcomm Telematics SDK through a C++ pybind11 extension, for LTE-V2X PC5 sidelink communication.

Thread Safety & Execution State

  • Location Threads: Location providers (e.g., ThreadStaticLocationService or GPSDLocationService) run autonomous periodic daemon threads that notify listeners with updated ego coordinates.

  • LDM Maintenance: The Local Dynamic Map supports both Reactive mode (updated immediately on packet arrival) and Threaded maintenance mode (background periodic cleanup of expired station entries).

  • Shutdown: The Facilities services have start() and stop(); the location services stop when their stop_event is set.