yoziru/tesla-ble
C++ library to communicate directly with Tesla vehicles over the BLE API
Featured achievements
The project's latest awards
#1 Repository of the week
#2 Repository of the week
#3 Repository of the week
TeslaBLE - A C++ library for communicating with Tesla vehicles over BLE
This library is designed to communicate with Tesla vehicles locally via the BLE API. It follows the same principles as the official Tesla vehicle-command library (Golang), and is intended for use in embedded systems.
It exists to:
- Provide a local and offline alternative to the official Tesla API.
- Avoid the rate limits of the official Tesla API.
The main purpose of this library is to locally manage charging of the vehicle to enable use cases such as charging during off-peak hours, or to manage charging based on solar production. It is not intended to replace the official Tesla API for all use cases.
Usage
This project is intended to be used as a library in your own project. It is not a standalone application.
yoziru/esphome-tesla-ble is an ESPHome project that uses this library to control your Tesla vehicle charging.
Several examples are included for your convenience.
cd examples/simple/
cmake .
make
Building and Testing
Quick Start
# Clone the repository
git clone https://github.com/yoziru/tesla-ble.git
cd tesla-ble
# Setup development environment (optional but recommended)
./scripts/setup_dev_environment.sh
# Build the project
cmake -B build
cmake --build build
# Run tests
./scripts/run_tests.sh
Manual Build
# Configure with CMake
cmake -B build -DCMAKE_BUILD_TYPE=Release
# Build the library and tests
cmake --build build --config Release
# Run tests with CTest
cd build
ctest --build-config Release --output-on-failure
Running Tests
Using the Test Runner Script
The repository includes a comprehensive test runner script with various options:
# Run tests in Debug mode (default)
./scripts/run_tests.sh
# Run tests in Release mode
./scripts/run_tests.sh --release
# Run tests with coverage analysis
./scripts/run_tests.sh --coverage
# Run tests with Valgrind (Linux only)
./scripts/run_tests.sh --valgrind
# Clean build and run tests
./scripts/run_tests.sh --clean
# Get help on available options
./scripts/run_tests.sh --help
Manual Test Execution
# Build and run all Tesla BLE tests (excludes dependency tests)
cd build
ctest --output-on-failure --verbose
# Run specific test suites
./tests/test_client
./tests/test_key_generation
./tests/test_message_building
./tests/test_message_parsing
./tests/test_session_management
./tests/test_protocol_compliance
./tests/test_utils
# Run the complete test suite
./tests/tesla_ble_tests
Test Coverage
To generate a coverage report:
# Install coverage tools (Ubuntu/Debian)
sudo apt-get install lcov
# Run tests with coverage
./scripts/run_tests.sh --coverage
# Coverage report will be generated in build/coverage_html/
Static Analysis
The project includes static analysis via cppcheck:
# Install cppcheck
sudo apt-get install cppcheck # Ubuntu/Debian
brew install cppcheck # macOS
# Run static analysis
cppcheck --enable=all --inconclusive \
--suppress=missingIncludeSystem \
--suppress=unusedFunction \
src/ include/ examples/
Code Formatting and Linting
The project uses clang-format for code formatting and clang-tidy for static analysis:
# Install clang tools
sudo apt-get install clang-format clang-tidy # Ubuntu/Debian
brew install clang-format clang-tidy # macOS
# Run formatting and linting checks (will configure CMake if needed)
./scripts/lint.sh
# Or run individually:
# Format code
find src include tests examples -name "*.cpp" -o -name "*.h" | xargs clang-format -i
# Check formatting
find src include tests examples -name "*.cpp" -o -name "*.h" | xargs clang-format --dry-run --Werror --style=file
# Run clang-tidy on source files (requires compile_commands.json)
find src -name "*.cpp" | xargs clang-tidy
Development Environment Setup
Use the provided script to set up your development environment:
./scripts/setup_dev_environment.sh
This script will:
- Detect your operating system
- Install required dependencies (cmake, compiler, etc.)
- Install optional development tools (lcov, cppcheck, valgrind)
- Set up git hooks for automatic testing
Continuous Integration
The project uses GitHub Actions for CI/CD with the following features:
- Cross-platform testing: Ubuntu, Windows, macOS
- Multiple build types: Debug and Release
- Code coverage: Automatic coverage reporting to Codecov
- Static analysis: cppcheck integration
- Memory testing: Valgrind and AddressSanitizer
- Performance testing: Various sanitizers (address, undefined, thread)
Test Structure
The test suite is organized into several categories:
test_client.cpp: Core client functionality and initializationtest_key_generation.cpp: Private key generation and loadingtest_message_building.cpp: Message construction for various commandstest_message_parsing.cpp: Parsing of received messagestest_session_management.cpp: Session handling and peer managementtest_utils.cpp: Utility functions and helper methodstest_vehicle.cpp: Vehicle state management and command processingtest_exponential_backoff.cpp: Exponential backoff retry logic
Each test file contains comprehensive unit tests covering both success and failure scenarios, edge cases, and parameter validation.
State Architecture
The library uses a unified state pattern for command processing with the following states:
IDLE: Initial state for new commandsAUTHENTICATING: Unified authentication initiation (replaces legacy domain-specific states)AUTH_RESPONSE_WAITING: Unified authentication response waitingREADY: Command ready to be sentWAITING_FOR_RESPONSE: Waiting for command responseCOMPLETED: Command completed successfullyFAILED: Command failed
This architecture provides a clean, maintainable approach to command lifecycle management with exponential backoff for retries.
Dependencies
- nanopb
- mbedtls 3.x
- NOTE: ESP-IDF <=4.4 includes mbedtls 2.x, and is not compatible with this library. You will need to use at least ESP-IDF 5.0.
Features
- Implements Tesla's BLE protocol
- AES-GCM key generation
- Metadata serialization
- Supports
UniversalMessage.RoutableMessageencoding and decoding- Supports Vehicle Security (VSSEC) payload
- Supports Infotainment payload
Credits
This fork builds on the original version by pmdroid.
IMPORTANT
Please take note that this library does not have official backing from Tesla, and its operational capabilities may be discontinued without prior notice. It's essential to recognize that this library retains private keys and other sensitive data on your device without encryption. I would like to stress that I assume no liability for any possible (although highly unlikely) harm that may befall your vehicle.
Similar Projects
Discover other similar projects and AI models.
Interact with Tesla vehicles over BLE using ESPHome and Home Assistant
A self-hosted data logger for your Tesla 🚘 [main maintainer=@JakobLichterfeld]
TeslaLogger is a self hosted data logger for your Tesla Model S/3/X/Y. Actually it supports RaspberryPi 3B, 3B+, 4B, Docker and Synology NAS.
Volta OS: in-car infotainment interface for Linux (Qt 6 / QML, Wayland compositor). Beta.
⚡Unofficial cross-platform desktop app to monitor and control Tesla vehicles, powered by Electron & React
A Model Context Protocol (MCP) server that provides access to your TeslaMate database, allowing AI assistants to query Tesla vehicle data and analytics.
Tesla Dash Cam Footage Management System | Automate video backup, cloud storage, and remote access for Tesla's built-in dash cam using a Raspberry Pi Zero W.