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
Upstream Ingress Flow (Indications): - The
LinkLayerbackground thread listens on the raw socket or C-V2X radio interface. - When a frame with the GeoNetworking EtherType arrives, it passes the packet to itsreceive_callback, the router’sgn_data_indicate. - The GeoNetworking router evaluates the packet (updating itsLocationTableand 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).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 aGNDataRequestto the GeoNetworking router. - GeoNetworking builds the headers for the packet transport type, assigns the sequence number, signs the packet with theSignServicewhen security is enabled, and callssendon 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 interfacesend(packet),receive(), and holds thereceive_callback.RawLinkLayer: Interfaces directly with LinuxPF_PACKETsockets, handling EtherType0x8947over IEEE 802.11p or Ethernet interfaces.PythonCV2XLinkLayer: Interfaces with the Qualcomm Telematics SDK through a C++pybind11extension, for LTE-V2X PC5 sidelink communication.
Thread Safety & Execution State
Location Threads: Location providers (e.g.,
ThreadStaticLocationServiceorGPSDLocationService) 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()andstop(); the location services stop when theirstop_eventis set.