diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 28e2637..332309c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,6 +33,8 @@ jobs: with: path: .tools/arduino key: ${{ runner.os }}-protected-arduino-1.1.1-${{ matrix.core }}-${{ hashFiles('Build-PulseShift.ps1') }} + restore-keys: | + ${{ runner.os }}-protected-arduino-1.1.1-${{ matrix.core }}- - name: Compile - production firmware (default) run: pwsh -NoProfile -File ./Build-PulseShift.ps1 -ArduinoCli arduino-cli -CoreVersion '${{ matrix.core }}' -BuildDirectory "$RUNNER_TEMP/build-production" diff --git a/Build-PulseShift.ps1 b/Build-PulseShift.ps1 index fe33aad..6848c93 100644 --- a/Build-PulseShift.ps1 +++ b/Build-PulseShift.ps1 @@ -5,11 +5,13 @@ param( [string]$BuildDirectory, [string]$CacheDirectory, [string]$ArduinoCli, - [string]$SketchDirectory + [string]$SketchDirectory, + [switch]$Wokwi ) $ErrorActionPreference = 'Stop' Set-StrictMode -Version Latest +if ($Wokwi -and $Headless) { throw 'The Wokwi build needs serial diagnostics.' } $CacheDirectory = if ($CacheDirectory) { $CacheDirectory } else { Join-Path $PSScriptRoot '.tools/arduino' } $SketchDirectory = if ($SketchDirectory) { $SketchDirectory } else { Join-Path $PSScriptRoot 'PulseShift' } $cliVersion = '1.1.1' @@ -26,7 +28,7 @@ $data = Join-Path $coreCache 'data' $sketchbook = Join-Path $coreCache 'sketchbook' $index = 'https://espressif.github.io/arduino-esp32/package_esp32_index.json' if (!$BuildDirectory) { - $variant = if ($Headless) { 'headless' } else { 'production' } + $variant = if ($Wokwi) { 'wokwi' } elseif ($Headless) { 'headless' } else { 'production' } $BuildDirectory = Join-Path $PSScriptRoot "build/esp32-$CoreVersion/$variant" } $build = [IO.Path]::GetFullPath($BuildDirectory) @@ -114,6 +116,7 @@ if (!(Test-Path -LiteralPath $library) -or !(Select-String -LiteralPath $library $defines = '-DPULSESHIFT_PROTECTED_BUILD=1' if ($Headless) { $defines += ' -DPULSESHIFT_SERIAL_DIAGNOSTICS=0' } +if ($Wokwi) { $defines += ' -DPULSESHIFT_BLE_ENABLED=0 -DPULSESHIFT_OTA_ENABLED=0' } $compile = @('compile', '--fqbn', 'esp32:esp32:esp32doit-devkit-v1', '--build-path', $build, '--build-property', "compiler.cpp.extra_flags=$defines", '--build-property', 'compiler.c.elf.extra_flags=-Wl,--wrap=nvs_flash_init') @@ -154,7 +157,7 @@ if ($bytes.Length -lt 65536 -or $bytes.Length + 76 -gt 0x140000 -or throw 'The build did not produce a classic ESP32 application that fits the signed OTA slot.' } -[ordered]@{ +$verification = [ordered]@{ arduinoCli = $cliVersion esp32Core = $CoreVersion nimble = $nimble @@ -168,5 +171,7 @@ if ($bytes.Length -lt 65536 -or $bytes.Length + 76 -gt 0x140000 -or applicationBytes = $bytes.Length signedPackageBytes = $bytes.Length + 76 slotBytes = 0x140000 -} | ConvertTo-Json | Set-Content -LiteralPath $reportPath -Encoding UTF8 +} +if ($Wokwi) { $verification['wokwi'] = $true } +$verification | ConvertTo-Json | Set-Content -LiteralPath $reportPath -Encoding UTF8 Write-Host "Build and protection checks passed. Application: $app" diff --git a/PulseShift.code-workspace b/PulseShift.code-workspace index df0eb6d..4df8107 100644 --- a/PulseShift.code-workspace +++ b/PulseShift.code-workspace @@ -42,6 +42,35 @@ }, "group": { "kind": "build", "isDefault": true }, "problemMatcher": "$gcc" + }, + { + "label": "PulseShift: Build Wokwi simulator", + "type": "process", + "command": "pwsh", + "args": [ + "-NoProfile", + "-File", + "${workspaceFolder:PulseShift Firmware}/Build-PulseShift.ps1", + "-ArduinoCli", + "arduino-cli", + "-Wokwi" + ], + "windows": { + "command": "powershell.exe", + "args": [ + "-NoProfile", + "-ExecutionPolicy", + "Bypass", + "-File", + "${workspaceFolder:PulseShift Firmware}/Build-PulseShift.ps1", + "-Wokwi" + ] + }, + "options": { + "cwd": "${workspaceFolder:PulseShift Firmware}" + }, + "group": "build", + "problemMatcher": "$gcc" } ] } diff --git a/PulseShift/src/ble_manager.cpp b/PulseShift/src/ble_manager.cpp index c6288c2..d8440a7 100644 --- a/PulseShift/src/ble_manager.cpp +++ b/PulseShift/src/ble_manager.cpp @@ -286,6 +286,10 @@ bool BleManager::isConfigSnapshotSubscribed() const { #endif } +bool BleManager::isEnabled() const { + return config::bleEnabled && initialized_; +} + bool BleManager::isAdvertising() const { #if defined(ARDUINO) && defined(PULSESHIFT_BLE_ENABLED) && (PULSESHIFT_BLE_ENABLED == 1) if (!initialized_) return false; diff --git a/PulseShift/src/ble_manager.h b/PulseShift/src/ble_manager.h index 992fd6b..f885b0b 100644 --- a/PulseShift/src/ble_manager.h +++ b/PulseShift/src/ble_manager.h @@ -50,7 +50,7 @@ class BleManager { bool isLogsSubscribed() const; bool isConfigResultSubscribed() const; bool isConfigSnapshotSubscribed() const; - bool isEnabled() const { return initialized_; } + bool isEnabled() const; bool isAdvertising() const; // Called by Application::tick() on the same loop task after poll(). A BLE diff --git a/docs/WINDOWS-BUILD.md b/docs/WINDOWS-BUILD.md index da32c1d..cbe88ec 100644 --- a/docs/WINDOWS-BUILD.md +++ b/docs/WINDOWS-BUILD.md @@ -16,10 +16,11 @@ Arduino IDE can edit the sketch and monitor a supported diagnostic build at 1152 ## VS Code and Wokwi 1. Open [PulseShift.code-workspace](../PulseShift.code-workspace) in VS Code. Run **Tasks: Run Task** and select **PulseShift: Protected ESP32 build**. On Windows, **ESP-IDF: Execute Custom Task** is an alternative that runs the same build with the installed Espressif extension. -2. Run **Wokwi: Start Simulator**. [wokwi.toml](../wokwi.toml) selects the merged 4 MiB image and matching ELF in `build/esp32-3.3.12/production`. The simulator boots the project partition table and uses the same GPIO27 output and six WS2812B pixels as the default hardware configuration. [Wokwi full-image configuration](https://docs.wokwi.com/vscode/project-config#esp32-application-image-vs-full-image) -3. Enter `STATUS` or `LED STATUS` in the simulator serial monitor to inspect watchdog, flash, CAN and LED state. Use `RPM TEST 6000` to exercise the built-in serial LED bench mode. For device-side OTA or live CAN and BLE verification, use the [hardware bench plan](OTA-BENCH-TEST-PLAN.md). +2. Select **PulseShift: Build Wokwi simulator**, or run `./Build-PulseShift.cmd -Wokwi` on Windows. The profile keeps the protected build, serial console, GPIO27, six default WS2812B pixels and project partition table. It disables BLE and OTA because Wokwi does not emulate Bluetooth. Its outputs in `build/esp32-3.3.12/wokwi` are for simulation. Use the production build for hardware and OTA. [Wokwi ESP32 simulation features](https://docs.wokwi.com/guides/esp32#simulation-features) +3. Run **Wokwi: Start Simulator**. [wokwi.toml](../wokwi.toml) selects the merged 4 MiB flash image and matching ELF from the Wokwi build. The extension may require an active [Wokwi license](https://docs.wokwi.com/vscode/getting-started#installation). +4. Enter `STATUS` or `LED STATUS` in the simulator serial monitor. Use `RPM TEST 6000` to exercise the built-in serial LED bench mode. The diagram leaves the CAN pins disconnected, and a blank simulator flash may report that LittleFS needs provisioning. For device-side OTA or live CAN and BLE verification, use the [hardware bench plan](OTA-BENCH-TEST-PLAN.md). -The firmware is an Arduino CLI sketch. Use the protected task for compilation, including when the ESP-IDF extension is installed. The ESP-IDF CMake build and flash actions target native ESP-IDF projects. Wokwi may require a [local extension license](https://docs.wokwi.com/vscode/getting-started#installation). The diagram leaves GPIO16 and GPIO17 disconnected from an external CAN node. +The firmware is an Arduino CLI sketch. Use the protected task for compilation, including when the ESP-IDF extension is installed. The ESP-IDF CMake build and flash actions target native ESP-IDF projects. ## First-time USB install on a new board diff --git a/test/test_ota_release.py b/test/test_ota_release.py index d938adf..ffc816d 100644 --- a/test/test_ota_release.py +++ b/test/test_ota_release.py @@ -62,6 +62,12 @@ def test_tag_layout_and_key_must_match(self): with self.assertRaisesRegex(ValueError, "partition table"): self.prepare() + def test_rejects_simulator_builds_before_signing(self): + self.app.with_name("build-verification.json").write_text('{"wokwi": true}', encoding="utf-8-sig") + with self.assertRaisesRegex(ValueError, "Wokwi simulator firmware"): + self.prepare() + self.assertFalse(self.output.exists()) + def test_rejects_non_application_artifacts(self): self.app.write_bytes(b"PK\x03\x04" + bytes(65536)) with self.assertRaisesRegex(ValueError, "application-only"): diff --git a/tools/ota_release.py b/tools/ota_release.py index 9893adb..609835e 100644 --- a/tools/ota_release.py +++ b/tools/ota_release.py @@ -74,6 +74,11 @@ def prepare(app, partitions, notes, tag, source, private_key, output): version = source_version() if not valid_version(version) or tag != f"v{version}" or not re.fullmatch(r"[0-9a-f]{40}", source): raise ValueError("Tag must match the stable firmware source version and include its commit SHA") + report = app.with_name("build-verification.json") + if report.is_file(): + metadata = json.loads(report.read_text(encoding="utf-8-sig")) + if isinstance(metadata, dict) and metadata.get("wokwi") is True: + raise ValueError("Wokwi simulator firmware cannot be packaged for OTA") check_layout(partitions) check_app(app.read_bytes()) release_notes = notes.read_text(encoding="utf-8").strip() diff --git a/wokwi.toml b/wokwi.toml index 5a38ecd..b08a52f 100644 --- a/wokwi.toml +++ b/wokwi.toml @@ -1,4 +1,4 @@ [wokwi] version = 1 -firmware = 'build/esp32-3.3.12/production/PulseShift.ino.merged.bin' -elf = 'build/esp32-3.3.12/production/PulseShift.ino.elf' +firmware = 'build/esp32-3.3.12/wokwi/PulseShift.ino.merged.bin' +elf = 'build/esp32-3.3.12/wokwi/PulseShift.ino.elf'