# 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, optional BabyLin hardware integration, running tests headless, and installing as a systemd service.

> Note: If you plan to use BabyLin hardware on a Pi, verify vendor driver support for ARM Linux. If BabyLin provides only Windows DLLs, use the Mock interface on Pi or deploy a different hardware interface that supports Linux/ARM.

## 1) Choose your interface

- Mock (recommended for headless/dev on Pi): `interface.type: mock`
- BabyLIN (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.

## 2) Install prerequisites

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

Optional (for BabyLin 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 babylin (if supported on ARM/Linux)
  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 BabyLIN on Linux/ARM with the SDK wrapper, set:
```yaml
interface:
  type: babylin
  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:
  - 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, ensure vendor SDK supports Linux/ARM and provide a shared object (`.so`) and headers
- If only Windows is supported, run the hardware suite on a Windows host and use the Pi for lightweight tasks (archiving, reporting, quick checks)
