.. _python_link_layer: =============================== Python Link Layer Abstraction =============================== The link layer in ``v2xflexstack`` (package ``flexstack.linklayer``) is the only part of the stack that depends on the radio or network interface. Every implementation derives from ``LinkLayer``: it is created with a *receive callback*, which it calls with every received GeoNetworking frame, and it offers ``send(packet)`` to the GeoNetworking router. Supported Implementations ========================= .. list-table:: :widths: 25 35 40 :header-rows: 1 * - Implementation - Underlying Technology - Typical Environment * - ``RawLinkLayer`` - Linux raw sockets (``PF_PACKET``, EtherType ``0x8947``) - IEEE 802.11p (ITS-G5, OCB mode), Ethernet, virtual Ethernet pairs * - ``PythonCV2XLinkLayer`` - Qualcomm Telematics SDK, through a C++ extension built with ``pybind11`` - C-V2X PC5 sidelink (e.g. Cohda MK6) RawLinkLayer ============ ``RawLinkLayer(iface, mac_address, receive_callback)`` binds a raw socket to the interface, starts a receive thread, and passes every frame with the GeoNetworking EtherType to the callback. Root privileges or the ``CAP_NET_RAW`` capability are needed. .. code-block:: python from flexstack.linklayer.raw_link_layer import RawLinkLayer def on_frame_received(packet: bytes) -> None: print(f"Received a {len(packet)}-byte GeoNetworking packet") mac_address = b"\x02\x11\x22\x33\x44\x55" link_layer = RawLinkLayer("eth0", mac_address, receive_callback=on_frame_received) # Normally called by the GeoNetworking router: broadcast a GeoNetworking packet link_layer.send(b"\x11\x00\x1a\x01") In a station, the receive callback is the GeoNetworking router's ``gn_data_indicate`` and the router is given the link layer, as in the :ref:`python_getting_started` example: .. code-block:: python link_layer = RawLinkLayer("eth0", mac_address, receive_callback=gn_router.gn_data_indicate) gn_router.link_layer = link_layer Over ITS-G5 the MID of the station's GeoNetworking address must be the MAC address of the interface (ETSI TS 103 836-4-2, clause 6.1); ``gn_router.media_dependent_address_ok()`` checks it. PythonCV2XLinkLayer (Qualcomm C-V2X) ==================================== ``PythonCV2XLinkLayer`` wraps a C++ shared library (``cv2xlinklayer.so``) that opens the C-V2X transmit and receive flows of the Qualcomm Telematics SDK (``telux::cv2x``). It only runs on a device with that SDK. Building the C++ extension -------------------------- Prerequisites: CMake (3.12 or later), ``pybind11``, the Qualcomm ``telux_cv2x`` SDK and a C++ compiler or cross-compilation toolchain for the device. .. code-block:: bash cd FlexStack/src/flexstack/linklayer/cv2xlinklayer mkdir build && cd build cmake .. make -j$(nproc) Usage ----- .. code-block:: python from flexstack.linklayer.cv2x_link_layer import PythonCV2XLinkLayer link_layer = PythonCV2XLinkLayer(receive_callback=gn_router.gn_data_indicate) gn_router.link_layer = link_layer