# Raspberry Pi Deployment Guide

This guide explains how to run the ECU testing framework on a Raspberry Pi (Debian/Raspberry Pi OS). It covers environment setup, hardware integration via MUM (recommended) or the deprecated BabyLin (legacy rigs only), running tests headless, and installing as a systemd service.

> Note: The MUM (Melexis Universal Master) is **networked**, so the Pi only
> needs IP reachability to the MUM (default `192.168.7.2`) — there are no
> Pi-side native libs to worry about. BabyLin (deprecated) needs ARM Linux
> native libraries; if those aren't available, use Mock or MUM on the Pi
> instead. New deployments should not target BabyLin.

## 1) Choose your interface

- **MUM (recommended for hardware on Pi)**: `interface.type: mum`. Requires Melexis `pylin` + `pymumclient` (see `vendor/automated_lin_test/install_packages.sh`) and IP reachability to the MUM device.
- Mock (recommended for headless/dev on Pi): `interface.type: mock`
- BabyLIN (**DEPRECATED** — only for legacy rigs and only if ARM/Linux support is available): `interface.type: babylin` and ensure the SDK's `BabyLIN_library.py` and corresponding Linux/ARM shared libraries are available under `vendor/` or on PYTHONPATH/LD_LIBRARY_PATH. Selecting this path emits a `DeprecationWarning`.

## 2) Install prerequisites

```bash
sudo apt update
sudo apt install -y python3 python3-venv python3-pip git
```

Optional (for the deprecated BabyLin path or USB tools):
```bash
sudo apt install -y libusb-1.0-0 udev
```

## 3) Clone and set up

```bash
# clone your repo
git clone <your-repo-url> ~/ecu_tests
cd ~/ecu_tests

# create venv
python3 -m venv .venv
source .venv/bin/activate

# install deps
pip install -r requirements.txt
```

## 4) Configure

Create or edit `config/test_config.yaml`:

```yaml
interface:
  type: mock   # or "mum" for hardware (current); "babylin" is deprecated
  channel: 1
  bitrate: 19200
flash:
  enabled: false
```

Optionally point to another config file via env var:
```bash
export ECU_TESTS_CONFIG=$(pwd)/config/test_config.yaml
```

If using the MUM on the Pi, set:

```yaml
interface:
  type: mum
  host: 192.168.7.2          # adjust to your MUM IP
  lin_device: lin0
  power_device: power_out0
  bitrate: 19200
  boot_settle_seconds: 0.5
  frame_lengths:
    0x0A: 8
    0x11: 4
```

Confirm reachability before running tests:

```bash
ping -c 2 192.168.7.2
```

If using BabyLIN on Linux/ARM with the SDK wrapper (**DEPRECATED**, legacy rigs only), set:

```yaml
interface:
  type: babylin            # deprecated; prefer "mum"
  channel: 0
  sdf_path: "/home/pi/ecu_tests/vendor/Example.sdf"
  schedule_nr: 0
```

## 5) Run tests on Pi

```bash
source .venv/bin/activate
python -m pytest -m "not hardware" -v
```

Artifacts are in `reports/` (HTML, JUnit, JSON, summary MD).

## 6) Run as a systemd service (headless)

This section lets the Pi run the test suite on boot or on demand.

### Create a runner script

Create `scripts/run_tests.sh`:
```bash
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")/.."
source .venv/bin/activate
# optionally set custom config
# export ECU_TESTS_CONFIG=$(pwd)/config/test_config.yaml
python -m pytest -v
```
Make it executable:
```bash
chmod +x scripts/run_tests.sh
```

### Create a systemd unit

Create `scripts/ecu-tests.service`:
```ini
[Unit]
Description=ECU Tests Runner
After=network-online.target
Wants=network-online.target

[Service]
Type=oneshot
WorkingDirectory=/home/pi/ecu_tests
ExecStart=/home/pi/ecu_tests/scripts/run_tests.sh
User=pi
Group=pi
Environment=ECU_TESTS_CONFIG=/home/pi/ecu_tests/config/test_config.yaml
# Capture output to a log file
StandardOutput=append:/home/pi/ecu_tests/reports/service.log
StandardError=append:/home/pi/ecu_tests/reports/service.err

[Install]
WantedBy=multi-user.target
```

Install and run:
```bash
sudo mkdir -p /home/pi/ecu_tests/reports
sudo cp scripts/ecu-tests.service /etc/systemd/system/ecu-tests.service
sudo systemctl daemon-reload
sudo systemctl enable ecu-tests.service
# Start manually
sudo systemctl start ecu-tests.service
# Check status
systemctl status ecu-tests.service
```

## 7) USB and permissions (if using hardware)

- Create udev rules for your device (if required by vendor)
- Add user to dialout or plugdev groups if serial/USB access is needed
- Confirm your hardware library is found by Python and the dynamic linker:
  - For the deprecated BabyLIN path only: ensure `vendor/BabyLIN_library.py` is importable (add `vendor/` to PYTHONPATH if needed)
  - Ensure `.so` files are discoverable (e.g., place in `/usr/local/lib` and run `sudo ldconfig`, or set `LD_LIBRARY_PATH`)

## 8) Tips

- Use the mock interface on Pi for quick smoke tests and documentation/report generation
- For full HIL on Pi, the **MUM is the easiest path** — it's IP-reachable so the Pi doesn't need vendor-specific native libraries, just the Melexis Python packages (`pylin`, `pymumclient`)
- For the deprecated BabyLIN HIL path, ensure vendor SDK supports Linux/ARM and provide a shared object (`.so`) and headers — but prefer migrating these rigs to MUM
- If only Windows is supported by your hardware path, run the hardware suite on a Windows host and use the Pi for lightweight tasks (archiving, reporting, quick checks)
