# Testing Guide

## Purpose

Bring up and validate board behavior in a staged way: power, flash, protocol output, and functional control on the cubley-base baseline.

Before running a full Phase A validation campaign, confirm that each baseline
component passes its functional smoke check. The per-component pass/fail
criteria are defined in:

> **[PHASE_A_FUNCTIONAL_SMOKE_CHECKS.md](./PHASE_A_FUNCTIONAL_SMOKE_CHECKS.md)**

Phase A exit is also gated by the [Phase A Exit Gate](./PHASE_A_FUNCTIONAL_SMOKE_CHECKS.md#phase-a-exit-gate) section in that document.

This guide is the cubley-base bring-up runbook. Later-profile network/USB/
FRAM/LNBH26/DiSEqC testing is intentionally deferred until those features are
reintroduced.

The Phase A freeze/handoff decision record is captured in [PHASE_A_EXIT_DECISION_2026-06-07.md](./PHASE_A_EXIT_DECISION_2026-06-07.md).


## Current Profile Notes

- The currently validated build profile is `cubley-base`.
- `System.Net`, MQTT/network, USB CDC, FRAM, LNBH26, and DiSEqC transmit test phases are deferred until their later reintroduction.
- Interop slot/checksum governance for Cubley native calls is defined in [INTEROP_CONTRACT_V1.md](../software/INTEROP_CONTRACT_V1.md).
- Interop compatibility and review rules are defined in [INTEROP_VERSIONING_POLICY.md](../software/INTEROP_VERSIONING_POLICY.md).
- Intentional drift regression check: run `./software/nanoFramework/toolchain/interop-negative-drift-test.sh` and require `PASS`.

## Prerequisites

### Hardware Needed
- ✅ STM32F407VGT6 DiSEqC controller board (assembled)
- ✅ USB-to-Serial adapter (for debug output) - USART3 on PB10/PB11
- ✅ ST-Link V2 (for programming)
- ✅ Oscilloscope or logic analyzer (for GPIO/diagnostics checks)
- ✅ Multimeter

### Software Needed
- ✅ nanoFramework firmware (built from `nf-native/`)
- ✅ Serial terminal (PuTTY, Arduino IDE, etc.)

---

## 🔌 Phase 1: Power-On & Serial Debug

### Step 1.1: Connect Serial Debug

```
STM32 Board          USB-Serial Adapter
-----------          ------------------
PB10 (TX) -------→   RX
PB11 (RX) ←-------   TX
GND      ←-------→   GND
```

**Serial Settings:**
- Baud: 115200
- Data: 8 bits
- Parity: None
- Stop: 1 bit

### Step 1.2: Flash Firmware

```bash
# Using ST-Link (flash both images)
st-flash write build/nanoBooter.bin 0x08000000
st-flash write build/nanoCLR.bin 0x08004000

# Or using OpenOCD
openocd -f interface/stlink.cfg -f target/stm32f4x.cfg \
    -c "program nanoBooter.bin 0x08000000 verify" \
    -c "program nanoCLR.bin 0x08004000 verify reset exit"
```

### Step 1.3: Verify Boot Sequence

Expected serial output:
```
==============================================
DiSEqC Controller Starting...
STM32F407VGT6 + cubley-base + nanoFramework
==============================================

Entering main loop...
[HEARTBEAT] Uptime: 0 seconds
```

✅ **Success Criteria:**
- Serial output appears
- Diagnostics and startup markers appear in the expected cubley-base order
- No legacy networking banner is required for this baseline

❌ **Troubleshooting:**
| Issue | Fix |
|-------|-----|
| No serial output | Check TX/RX wiring, baud rate |
| Startup markers missing | Check diagnostics mailbox, flash layout, and cubley-base build profile |

---

## 🌐 Phase 2: Network & MQTT Testing

This section is deferred until the network baseline returns in a later profile.

### Step 2.1: Verify MQTT Connection

On your PC/broker:
```bash
# Subscribe to all topics
mosquitto_sub -h localhost -t 'diseqc/#' -v

# Should see:
# diseqc/availability online
# diseqc/status/state idle
# diseqc/status/position/angle 0.0
# diseqc/status/busy false
```

### Step 2.2: Test MQTT Commands

#### Test Availability
```bash
# Device should be online
mosquitto_sub -h localhost -t 'diseqc/availability' -v
# Output: diseqc/availability online
```

#### Test Halt Command
```bash
mosquitto_pub -h localhost -t 'diseqc/command/halt' -m ''

# Check serial output:
# [MQTT] Topic: diseqc/command/halt
# [CMD] Halting rotor
```

#### Test Status Query
```bash
# Monitor status updates
mosquitto_sub -h localhost -t 'diseqc/status/#' -v

# Should show:
# diseqc/status/state idle
# diseqc/status/busy false
# diseqc/status/position/angle 0.0
```

✅ **Success Criteria:**
- MQTT messages published correctly
- Commands received and logged
- Status updates appear

### Step 2.3: Test Runtime Configuration Commands

```bash
# Request effective runtime configuration snapshot
mosquitto_pub -h localhost -t 'diseqc/command/config/get' -m ''

# Update broker and topic prefix
mosquitto_pub -h localhost -t 'diseqc/command/config/set' -m 'mqtt.broker=192.168.1.60'
mosquitto_pub -h localhost -t 'diseqc/command/config/set' -m 'mqtt.topic_prefix=diseqc_lab'
mosquitto_pub -h localhost -t 'diseqc/command/config/set' -m 'mqtt.transport_mode=w5500-native'

# Save then reset and reload
mosquitto_pub -h localhost -t 'diseqc/command/config/save' -m ''
mosquitto_pub -h localhost -t 'diseqc/command/config/reset' -m ''
mosquitto_pub -h localhost -t 'diseqc/command/config/reload' -m ''

# Guarded FRAM clear (DANGEROUS)
mosquitto_pub -h localhost -t 'diseqc/command/config/fram_clear' -m 'ERASE'
```

Expected status topics include:
- `diseqc/status/config/updated`
- `diseqc/status/config/saved`
- `diseqc/status/config/reset`
- `diseqc/status/config/reloaded`
- `diseqc/status/config/persisted`
- `diseqc/status/config/reload_source`
- `diseqc/status/config/fram_cleared`
- `diseqc/status/config/effective/...`

Persistence check:
- After `config/save`, power-cycle the board and publish `diseqc/command/config/get`
- Confirm saved values are still present and `config/reload_source` reports `fram` during reload

### Step 2.4: Test Serial Config Commands (MVP)

On the UART terminal (same link used for debug output), send:

```text
help
config get
config set network.static_ip=192.168.1.120
config save
config reset
config reload
config fram-dump
config fram-dump 128
config fram-clear ERASE
```

Expected debug output includes:
- `[SERIAL] Command interface active on COM3 @ 115200`
- `[SERIAL] Command: config get`
- `[CMD] Config updated: ...`
- `[CONFIG] FRAM dump (0..63)`
- `[FRAM] 0000: ...`
- `[CONFIG] FRAM cleared; runtime config reset to defaults`

### Step 2.5: On-Device W5500 Adapter MQTT Smoke Test

Use this test to validate that MQTT traffic can run through the injected `w5500-native` channel path.

1) Start two broker-side monitors (separate terminals):

```bash
mosquitto_sub -h <broker-ip> -t 'diseqc/availability' -v
mosquitto_sub -h <broker-ip> -t 'diseqc/status/#' -v
```

2) Set adapter mode and persist it:

```bash
mosquitto_pub -h <broker-ip> -t 'diseqc/command/config/set' -m 'mqtt.transport_mode=w5500-native'
mosquitto_pub -h <broker-ip> -t 'diseqc/command/config/save' -m ''
```

3) Reboot/power-cycle the board.

4) Verify UART boot logs include:

```text
Transport mode: w5500-native
[MQTT] transport_mode=w5500-native requested
[MQTT] W5500 IMqttNetworkChannel injected
✓ Connected to MQTT broker!
```

5) Verify broker-side smoke behavior:

```bash
# command path check
mosquitto_pub -h <broker-ip> -t 'diseqc/command/halt' -m ''

# observe config echo (effective config includes transport mode)
mosquitto_pub -h <broker-ip> -t 'diseqc/command/config/get' -m ''
```

Expected outcomes:
- `diseqc/availability` reports `online` after reboot.
- `diseqc/status/config/effective/mqtt/transport_mode` reports `w5500-native`.
- Command/status round-trip works (`halt` command handled and status still publishing).

Rollback (safe fallback):

```bash
mosquitto_pub -h <broker-ip> -t 'diseqc/command/config/set' -m 'mqtt.transport_mode=system-net'
mosquitto_pub -h <broker-ip> -t 'diseqc/command/config/save' -m ''
```

After reboot, verify UART line `Transport mode: system-net`.

---

## 🎛️ Phase 3: DiSEqC Signal Verification

### Step 3.1: Connect Oscilloscope

```
Oscilloscope Setup:
- Channel 1: TIM4_CH1 output pin (DiSEqC output)
- Channel 2: GND
- Trigger: Rising edge, 1V
- Timebase: 100µs/div
- Voltage: 2V/div
```

### Step 3.2: Send Test Command

```bash
# Send GotoAngle command
mosquitto_pub -h localhost -t 'diseqc/command/goto/angle' -m '0'
```

**Expected Serial Output:**
```
[MQTT] Topic: diseqc/command/goto/angle
[MQTT] Payload: 0
[CMD] Moving to angle: 0°
[CMD] Movement complete: 0°
```

**Expected on Oscilloscope:**
- Transmission duration: ~67ms (5 bytes)
- Carrier frequency: ~22kHz (verify with cursor)
- Bit '0': 1ms carrier + 0.5ms silence
- Bit '1': 0.5ms carrier + 1ms silence

### Step 3.3: Decode DiSEqC Bytes

For `GotoAngle(0)`, expect these bytes:
```
E0 31 6E D0 00

Breakdown:
E0 = Master command, no reply
31 = Any positioner
6E = GotoX command
D0 = Direction (East) + angle high bits
00 = Angle low bits (0° × 16 = 0x0000)
```

**Verify with oscilloscope:**
- Count 5 bytes × 9 bits = 45 bits
- Each bit ~1.5ms = ~67.5ms total
- 22kHz carrier visible during ON periods

✅ **Success Criteria:**
- DiSEqC waveform visible
- Correct frequency (~22kHz ±10%)
- Correct timing (1ms/0.5ms)
- All bytes transmitted

❌ **Troubleshooting:**
| Issue | Fix |
|-------|-----|
| No output | Check TIM4 initialization and TIM4_CH1 GPIO/AF config |
| Wrong frequency | Verify 168MHz system clock, check PSC/ARR |
| Distorted signal | Check LNBH26 connections, power supply |

---

## 🎮 Phase 4: Manual Control Testing

### Step 4.1: Test Step Commands

```bash
# Step East
mosquitto_pub -h localhost -t 'diseqc/command/manual/step_east' -m '1'

# Wait 2 seconds, then Step West
mosquitto_pub -h localhost -t 'diseqc/command/manual/step_west' -m '1'
```

**Expected Serial Output:**
```
[CMD] Step East: 1 steps
[CMD] Step West: 1 steps
```

**Oscilloscope:**
- Each command sends DiSEqC packet
- `StepEast(1)` = bytes `E0 31 68 01`
- `StepWest(1)` = bytes `E0 31 69 01`

### Step 4.2: Test Continuous Drive

```bash
# Start driving East
mosquitto_pub -h localhost -t 'diseqc/command/manual/drive_east' -m ''

# Wait 3 seconds, then halt
sleep 3
mosquitto_pub -h localhost -t 'diseqc/command/halt' -m ''
```

**Expected:**
- Drive command sends continuously
- Halt stops transmission

✅ **Success Criteria:**
- Commands execute
- Status updates correctly
- Oscilloscope shows correct bytes

---

## 🛰️ Phase 5: Rotor Integration

### Step 5.1: Connect Rotor

**IMPORTANT:** Ensure LNBH26 power supply is correct!
- 13V for vertical polarization
- 18V for horizontal polarization

```
LNBH26 Connections:
- DSQIN (from TIM4_CH1 output pin) → Rotor DiSEqC input
- LNB_OUT → LNB coax
- VCC → 18V power supply
- GND → Common ground
```

### Step 5.2: Test Movement

```bash
# Move to reference (0°)
mosquitto_pub -h localhost -t 'diseqc/command/goto/angle' -m '0'

# Wait for movement to complete
sleep 10

# Move to +20° East
mosquitto_pub -h localhost -t 'diseqc/command/goto/angle' -m '20'
```

**Expected:**
- Rotor moves (hear motor)
- LED on rotor indicates movement
- Movement stops at target

### Step 5.3: Test Satellite Positions

```bash
# Move to Astra 19.2°E
mosquitto_pub -h localhost -t 'diseqc/command/goto/satellite' -m 'astra_19.2e'

# Check status
mosquitto_sub -h localhost -t 'diseqc/status/position/satellite' -v
# Output: diseqc/status/position/satellite astra_19.2e
```

✅ **Success Criteria:**
- Rotor responds to commands
- Movement smooth
- Reaches target position
- Status updates correctly

---

## 📊 Phase 6: System Integration

### Step 6.1: Home Assistant Integration

Add to `configuration.yaml`:
```yaml
mqtt:
  select:
    - name: "Satellite Position"
      command_topic: "diseqc/command/goto/satellite"
      state_topic: "diseqc/status/position/satellite"
      options:
        - "astra_19.2e"
        - "hotbird_13e"
        - "astra_28.2e"
      availability_topic: "diseqc/availability"
```

**Test:**
1. Restart Home Assistant
2. Check Entities → `select.satellite_position`
3. Select satellite from dropdown
4. Verify rotor moves

### Step 6.2: Node-RED Flow

```json
[
    {
        "id": "mqtt_in",
        "type": "mqtt in",
        "topic": "diseqc/status/#",
        "broker": "mqtt_broker"
    },
    {
        "id": "dashboard",
        "type": "ui_text",
        "label": "Rotor Status"
    }
]
```

### Step 6.3: Python Automation

```python
import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
    client.subscribe("diseqc/status/#")

def on_message(client, userdata, msg):
    print(f"{msg.topic}: {msg.payload.decode()}")

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("192.168.1.50", 1883)

# Move to satellite
client.publish("diseqc/command/goto/satellite", "astra_19.2e")

client.loop_forever()
```

✅ **Success Criteria:**
- Home Assistant integration works
- Node-RED displays status
- Python control functions

---

## 🧪 Phase 7: Performance Testing

### Test 7.1: Response Time

```bash
# Measure command-to-execution time
time mosquitto_pub -h localhost -t 'diseqc/command/halt' -m ''
```

**Expected:** <100ms from publish to execution

### Test 7.2: Reliability

```bash
# Send 100 commands
for i in {1..100}; do
    mosquitto_pub -h localhost -t 'diseqc/command/goto/angle' -m '10'
    sleep 2
    mosquitto_pub -h localhost -t 'diseqc/command/goto/angle' -m '0'
    sleep 2
done
```

**Expected:** 0% packet loss, all commands execute

### Test 7.3: Network Stability

```bash
# Disconnect/reconnect Ethernet
# Controller should auto-reconnect within 5 seconds
```

**Monitor serial output:**
```
[MQTT] Connection lost! Attempting to reconnect...
[MQTT] Disconnected. Reconnecting...
✓ Connected to MQTT broker!
```

---

## 📝 Acceptance Checklist

### Hardware
- [  ] Board powers on without smoke
- [  ] Serial debug output works
- [  ] W5500 Ethernet link LED on
- [  ] Static IP network initialized
- [  ] TIM4_CH1 output pin produces 22kHz DiSEqC signal
- [  ] LNBH26 provides LNB power

### Software
- [  ] Firmware flashes successfully
- [  ] MQTT connection establishes
- [  ] All command topics work
- [  ] Status updates publish correctly
- [  ] Availability (LWT) works

### DiSEqC
- [  ] GotoAngle commands work
- [  ] GotoSatellite commands work
- [  ] Halt command works
- [  ] StepEast/West work
- [  ] DriveEast/West work
- [  ] Signal visible on oscilloscope
- [  ] Rotor responds to commands

### Integration
- [  ] Home Assistant integration works
- [  ] MQTT Explorer shows all topics
- [  ] Python control works
- [  ] Network disconnection handled

---

## 🚨 Common Issues & Solutions

### Issue: "Network not ready"
**Solution:**
- Check Ethernet cable
- Verify W5500 SPI connections (PB12-PB15, PC6-PC7)
- Verify static IP/subnet/gateway match your LAN

### Issue: "MQTT connection failed"
**Solution:**
- Ping MQTT broker from PC
- Check broker IP in code
- Verify firewall rules
- Try mosquitto_pub from PC first

### Issue: "No DiSEqC output"
**Solution:**
- Verify the DiSEqC output pin is configured for TIM4_CH1 alternate function
- Check native driver initialized
- Use oscilloscope to verify signal
- Check LNBH26 power supply

### Issue: "Rotor doesn't move"
**Solution:**
- Verify LNBH26 connections
- Check LNB power (13V/18V)
- Test DiSEqC signal with oscilloscope
- Check rotor power supply

---

## 📊 Expected Performance

| Metric | Value |
|--------|-------|
| Boot time | <5 seconds |
| Network initialization (static IP) | <1 second |
| MQTT connection | <1 second |
| Command response | <100ms |
| DiSEqC transmission | 67ms (5 bytes) |
| Network reconnect | <5 seconds |
| CPU usage | <10% |
| Memory usage | ~15KB RAM |

---

**When all tests pass, your DiSEqC controller is ready for production use!** 🎉

Next steps:
1. Add configuration management
2. Implement satellite database
3. Build web interface
4. Deploy to permanent installation

