Files
pi-pm3/pi-gen/stageDangerousPi/04-pisugar
michael 2ec89041ef Phase 4 enhancements: WebSocket auth, HTTPS UI, plugin hooks, build fixes
Security:
- Add token-based WebSocket authentication (closes critical security gap)
  - In-memory token store with 24h TTL (token_store.py)
  - POST /api/auth/token exchanges Basic Auth for WS token
  - GET /api/auth/status public endpoint for auth check
  - WebSocket validates token query param, rejects with close code 4401
  - Frontend LoginPrompt modal for credential entry
  - WebSocket manager handles full auth flow with auth_required state
  - No-op when AUTH_ENABLED=false (preserves existing behavior)

HTTPS:
- Wire HTTPS toggle in Settings UI (POST /api/system/ssl/toggle)
- Add certificate regeneration button
- Display SSL info (expiration, SANs, SHA256 fingerprint)

Plugins:
- Wire trigger_hook("pm3_command") in PM3 service
- Wire trigger_hook("update_check") in update manager

Build/Infrastructure:
- Enable NetworkManager in pi-gen AP setup stage
- Add HF booster board detection patch for Proxmark3
- Update LED PWM control patch
- Fix BLE adapter, UPS drivers, WiFi manager improvements
- Update HTTPS support stage script

Documentation:
- Update PROJECT_STATUS.md and IMPLEMENTATION_PRIORITIES.md

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-03 11:45:11 -08:00
..
2026-01-06 13:46:22 -08:00
2026-01-06 13:46:22 -08:00

PiSugar UPS Support - Native I2C Driver

This substage configures native I2C support for PiSugar 2/3 series UPS HATs.

NO pisugar-server daemon is required - we communicate directly with the battery management chip via I2C for minimal CPU usage (<1% vs 350%+ for the daemon).

Enabling PiSugar Support

PiSugar configuration is disabled by default. To enable it during build:

# Remove the SKIP file
rm pi-gen/stageDangerousPi/04-pisugar/SKIP

# Then build the image
./build-image.sh

How It Works

Dangerous Pi includes a native I2C driver that communicates directly with the PiSugar hardware:

  • IP5209 (PiSugar 2 series) at I2C address 0x75
  • IP5312 (PiSugar 3 series) at I2C address 0x57
  • SD3078 RTC at I2C address 0x32

The driver auto-detects the hardware when UPS_TYPE=auto (default).

Features

  • Battery voltage, current, and percentage reading
  • Charging status detection (AC/battery power source)
  • Button tap detection (single, double, long press)
  • RTC read/write for scheduled wake-up alarms
  • Force shutdown capability
  • Low battery warnings and automatic safe shutdown

Configuring Dangerous Pi for PiSugar

By default, PiSugar is auto-detected. You can explicitly configure it in /opt/dangerous-pi/.env:

# Auto-detect UPS hardware (default, recommended)
UPS_TYPE=auto

# Or explicitly use native I2C driver
UPS_TYPE=pisugar

Then restart Dangerous Pi:

sudo systemctl restart dangerous-pi

Supported Hardware

Model Chip I2C Address Notes
PiSugar 2 (4-LEDs) IP5209 0x75 For Pi Zero
PiSugar 2 Pro IP5209 0x75 For larger Pis
PiSugar 3 IP5312 0x57 For Pi Zero 2W
PiSugar 3 Plus IP5312 0x57 For Pi 4/5

Verifying Installation

Check if PiSugar hardware is detected:

# List I2C devices
i2cdetect -y 1

# Expected output for PiSugar 2:
#      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
# 30: -- -- 32 -- -- -- -- -- -- -- -- -- -- -- -- --   <- RTC
# 70: -- -- -- -- -- 75 -- --                           <- Battery

Check UPS status via API:

curl http://localhost:8000/api/system/ups/status

Migrating from pisugar-server

If you previously installed pisugar-server:

# Stop and disable the daemon
sudo systemctl stop pisugar-server
sudo systemctl disable pisugar-server

# The native driver will automatically take over
sudo systemctl restart dangerous-pi

Additional Resources