How jonathandreyer/esp-modem works
Purpose and Status
esp-modem is an archived ESP-IDF managed component for controlling GSM/LTE modems through AT commands and exposing PPP/PPPoS connectivity as an ESP network interface. It supports C and C++ consumers and has Linux host portability for development/testing. The successor is esp-protocols.
Public Integration Surface
Applications consume the C API and C++ abstractions rather than transport internals:
• C entry points and configuration: include/esp_modem_api.h, include/esp_modem_dce_config.h
• C++ API, DCE/DTE model, and factories: include/cxx_include/esp_modem_api.hpp, include/cxx_include/esp_modem_dce_factory.hpp, include/cxx_include/esp_modem_dte.hpp
• Component build/package boundary: CMakeLists.txt, idf_component.yml
DTE Transport and Terminal Layer
The DTE represents the host-side data-terminal equipment: UART or virtual/file/socket-backed channels. Terminal implementations own byte I/O, while higher layers consume command/response streams.
• UART and Linux UART implementations: src/esp_modem_uart.cpp, src/esp_modem_uart_linux.cpp
• Terminal implementations and resource creators: src/esp_modem_term_uart.cpp, src/esp_modem_term_fs.cpp, src/esp_modem_vfs_socket_creator.cpp
• Private platform-compatible terminal interfaces: private_include/uart_terminal.hpp, private_include/vfs_termial.hpp
DCE, AT Command, and Module Layer
The DCE is the modem-facing control plane. It issues AT commands through the DTE terminal, parses modem responses, and exposes common operations plus module-specific behavior. The command library separates reusable command definitions from DCE lifecycle/state handling.
• Core DCE implementation: src/esp_modem_dce.cpp, include/cxx_include/esp_modem_dce.hpp
• Command execution/library: src/esp_modem_command_library.cpp, include/cxx_include/esp_modem_command_library.hpp
• Module selection and object construction: src/esp_modem_modules.cpp, src/esp_modem_factory.cpp, include/cxx_include/esp_modem_dce_module.hpp
CMUX Multiplexing
CMUX is an optional framing layer over the modem serial channel. It enables concurrent logical channels, allowing AT command traffic to continue while PPP data is active.
• CMUX protocol implementation: src/esp_modem_cmux.cpp
• Public CMUX contract: include/cxx_include/esp_modem_cmux.hpp
• Concurrent AT/PPP usage example: examples/simple_cmux_client/main/simple_cmux_client_main.cpp
PPP and Network-Interface Boundary
The networking integration binds modem data mode to ESP-IDF esp_netif/PPP facilities. Control transitions move the modem from AT-command mode into data mode; PPP packets then flow between the DTE transport and the network stack.
• Netif integration: src/esp_modem_netif.cpp, include/cxx_include/esp_modem_netif.hpp
• C API adaptation layer: src/esp_modem_c_api.cpp, include/esp_modem_c_api_types.h
• PPPoS client entry point: examples/pppos_client/main/pppos_client_main.c
Runtime Portability and Concurrency
ESP-IDF/FreeRTOS is the primary embedded runtime. Linux variants provide host execution through mocked ESP event APIs and a TUN-backed netif path, with platform-specific primitives selected underneath common abstractions.
• FreeRTOS and Linux primitives: src/esp_modem_primitives_freertos.cpp, src/esp_modem_primitives_linux.cpp
• Linux netif bridge: src/esp_modem_netif_linux.cpp, port/linux/esp_netif_linux/tun_io.c
• ESP event compatibility: port/linux/esp_event_mock/esp_event_mock.c
Application Entry Points and Network Scenarios
Examples are independent ESP-IDF applications demonstrating the intended orchestration: create transport/DCE, initialize modem and network, then run application traffic.
• Basic C PPP client: examples/pppos_client/main/pppos_client_main.c
• Interactive C++ modem command console: examples/modem_console/main/modem_console_main.cpp
• Wi-Fi AP/NAT forwarding over PPPoS: examples/ap_to_pppos/main/ap_to_pppos.c
• Linux modem/TUN application: examples/linux_modem/main/modem_main.cpp
Main entry points: esp_modem_netif.cpp PPPoS netif integration · esp_modem_dce.cpp DCE control plane · esp_modem_dte.hpp DTE abstraction · esp_modem_netif_linux.cpp Linux TUN netif
Components 20
Public Surface
- Component package · ESP-IDF managed-component boundary.
idf_component.yml - C API
include/esp_modem_api.h - C++ API & factories
include/cxx_include/esp_modem_api.hpp - C API adapter
src/esp_modem_c_api.cpp
Modem Control
- DCE control plane · Owns modem lifecycle, state, and common operations.
src/esp_modem_dce.cpp - AT command library · Reusable AT command definitions and response parsing.
src/esp_modem_command_library.cpp - Module selection
src/esp_modem_modules.cpp
Transport & Multiplexing
- DTE abstraction · Host-side data-terminal equipment exposed to the DCE.
include/cxx_include/esp_modem_dte.hpp - Terminal implementations · UART, filesystem, and socket-backed terminal channels.
src/esp_modem_term_uart.cpp - UART platforms · ESP and Linux UART backends.
src/esp_modem_uart_linux.cpp - CMUX framing · Optional logical channels for concurrent AT and PPP traffic.
src/esp_modem_cmux.cpp
Network & Runtime
- PPPoS netif integration · Connects modem data mode to ESP-IDF esp_netif/PPP.
src/esp_modem_netif.cpp - Runtime primitives · FreeRTOS and Linux implementations beneath common abstractions.
src/esp_modem_primitives_freertos.cpp - Linux TUN netif
src/esp_modem_netif_linux.cpp - ESP event mock
port/linux/esp_event_mock/esp_event_mock.c
Example Applications
- C PPPoS client
examples/pppos_client/main/pppos_client_main.c - Modem command console
examples/modem_console/main/modem_console_main.cpp - Concurrent CMUX client
examples/simple_cmux_client/main/simple_cmux_client_main.cpp - Linux modem app
examples/linux_modem/main/modem_main.cpp - AP-to-PPPoS forwarding
examples/ap_to_pppos/main/ap_to_pppos.c
Connections 23
- Component package → C API: packages
- Component package → C++ API & factories: packages
- C API → C API adapter: calls
- C API adapter → DCE control plane: adapts
- C++ API & factories → Module selection: constructs
- Module selection → DCE control plane: selects
- DCE control plane → AT command library: executes
- DCE control plane → DTE abstraction: uses
- DTE abstraction → Terminal implementations: owns I/O
- Terminal implementations → UART platforms: uses
- CMUX framing → DTE abstraction: multiplexes
- DCE control plane → PPPoS netif integration: enters data mode
- PPPoS netif integration → DTE abstraction: exchanges PPP packets
- PPPoS netif integration → Runtime primitives: uses
- Linux TUN netif → PPPoS netif integration: Linux variant
- Linux TUN netif → ESP event mock: uses events
- C PPPoS client → C API: uses
- C PPPoS client → PPPoS netif integration: runs PPPoS
- Modem command console → C++ API & factories: uses
- Concurrent CMUX client → CMUX framing: demonstrates
- Concurrent CMUX client → PPPoS netif integration: runs PPP concurrently
- Linux modem app → Linux TUN netif: uses
- AP-to-PPPoS forwarding → PPPoS netif integration: forwards over PPPoS