brainbit
A Rust library and terminal UI for streaming real-time EEG data from BrainBit headband devices via the NeuroSDK2 C library.
Rust FFI wrapper with 100% API parity — all 175 functions from sdk_api.h
bound, all types from cmn_type.h mapped to #[repr(C)] Rust equivalents,
SHA-256 integrity verification, and OS-level network sandboxing.
Installation
cargo add brainbit
Supported hardware
| Device | BLE Name | Scanner Family | Channels | Hz | How to distinguish |
|---|---|---|---|---|---|
| BrainBit (original) | BrainBit |
LEBrainBit |
4 (O1, O2, T3, T4) | 250 | FwMajor < 100 |
| Flex (v1) | BrainBit |
LEBrainBit |
4 (relocatable) | 250 | FwMajor >= 100 |
| BrainBit 2 | BrainBit |
LEBrainBit2 |
up to 8 | 250 | — |
| BrainBit Pro | BB Pro |
LEBrainBitPro |
up to 8 | 250 | — |
| Flex 4 | Flex |
LEBrainBitFlex |
4 | 250 | SensModel == 2 |
| Flex 8 | Flex Pro |
LEBrainBitFlex |
8 | 250 | SensModel == 3 |
| Callibri | — | LECallibri |
1 (EEG/EMG/ECG/EDA) | configurable | — |
| Headphones | — | LEHeadPhones |
7 | 250 | — |
| Headphones 2 | — | LEHeadPhones2 |
4 | 250 | — |
| Headband | — | LEHeadband |
4 | 250 | — |
| NeuroEEG | — | LENeuroEEG |
up to 24 | configurable | — |
All devices communicate over BLE. The NeuroSDK2 library handles the full Bluetooth stack internally — no external BLE library needed.
Cross-platform
Works on Windows, Linux, and macOS. The neurosdk2 shared library
(neurosdk2.dll / libneurosdk2.so / libneurosdk2.dylib) is loaded at
runtime via libloading — no build-time C dependencies, no bindgen, no
system headers.
Prerequisites
| Requirement | Notes |
|---|---|
| Rust ≥ 1.75 | rustup update stable |
| NeuroSDK2 native library | Run ./sdk/download.sh (auto-downloads + verifies) |
| BLE adapter | Built-in or USB Bluetooth adapter |
Native library sources
| Platform | Repository | File |
|---|---|---|
| Windows | neurosdk2-cpp | neurosdk2-x64.dll / neurosdk2-x32.dll |
| Linux | linux_neurosdk2 | libneurosdk2.so |
| macOS | apple_neurosdk2 | libneurosdk2.dylib (universal x86_64 + arm64) |
Features
Library
Use brainbit as a library in your own project:
[]
# Full build (includes the ratatui TUI feature):
= "0.0.2"
# Library only — skips ratatui / crossterm compilation:
= { = "0.0.2", = false }
use *;
use Duration;
CLI
RUST_LOG=debug
TUI
Real-time 4-channel EEG waveform display in the terminal:
Security
Three layers of defense for the closed-source native library:
1. SHA-256 integrity verification
# At download time (automatic)
# At runtime (opt-in)
BRAINBIT_VERIFY_SDK=1
# Programmatic
);
2. Pinned Git commits
Downloads from exact commit SHAs, not main. Update sdk/download.sh
to upgrade.
3. OS-level network sandbox
use *;
block_internet?; // irrevocable — process can never reach the internet
// BLE still works (uses IPC, not sockets)
| Platform | Mechanism | Blocks | Allows |
|---|---|---|---|
| Linux | seccomp-bpf | AF_INET/AF_INET6 sockets |
AF_UNIX (D-Bus), AF_BLUETOOTH |
| macOS | Seatbelt sandbox | network-outbound (remote) |
XPC (CoreBluetooth), IPC |
| Windows | Windows Firewall rule | Outbound TCP/UDP | WinRT Bluetooth APIs |
Project layout
brainbit-rs/
├── Cargo.toml
├── README.md
├── CHANGELOG.md
├── LICENSE
└── src/
├── lib.rs # Crate root: modules + prelude
├── main.rs # Headless CLI binary
├── bin/
│ └── tui.rs # Full-screen TUI binary (ratatui)
├── ffi.rs # Cross-platform NeuroSDK2 FFI (runtime-loaded, 175 functions)
├── types.rs # #[repr(C)] FFI types matching cmn_type.h
├── scanner.rs # BLE device scanner
├── device.rs # High-level device API (signal, resist, battery)
├── error.rs # BrainBitError
├── verify.rs # SHA-256 integrity verification
└── sandbox.rs # OS-level network sandboxing
├── sdk/
│ ├── download.sh # Download + verify native libraries
│ └── checksums.sha256 # Pinned SHA-256 hashes
├── examples/
│ ├── scan.rs # Device discovery
│ ├── stream.rs # Signal streaming with callback
│ ├── resist.rs # Electrode resistance measurement
│ └── sandbox_test.rs # Network sandbox verification
└── tests/
└── types_tests.rs # FFI type layouts, enum values, ABI checks (29 tests)
Examples
# Scan for devices
# Stream EEG data with callback
# Measure electrode resistance
# Verify network sandbox works
Dependencies
| Crate | Purpose |
|---|---|
| libloading | Runtime DLL/so/dylib loading for NeuroSDK2 |
| thiserror | Error type derivation |
| log | Logging facade |
| env_logger | Log output for binaries |
| libc | seccomp-bpf syscalls (Linux sandbox) |
| ratatui | Terminal UI (optional, tui feature) |
| crossterm | Terminal backend (optional, tui feature) |
Running tests
29 unit tests cover FFI type layouts, enum discriminant values, ABI struct sizes, string extraction, SHA-256 correctness, and sampling frequency conversion — all run without hardware.