param( [string]$ConfigPath, [string]$Mac, [string]$Count, [string]$OnlineBinPath, [string]$OtaBinPath, [switch]$NoOta ) Set-StrictMode -Version 2.0 $ErrorActionPreference = "Stop" $scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path $repoRoot = Split-Path -Parent $scriptDir if ([string]::IsNullOrWhiteSpace($ConfigPath)) { $ConfigPath = Join-Path $repoRoot "project_info.ini" } function Read-ProjectInfo { param([string]$Path) if (-not (Test-Path -LiteralPath $Path)) { throw "Config file not found: $Path" } $result = @{} foreach ($line in Get-Content -LiteralPath $Path) { $trimmed = $line.Trim() if ($trimmed.Length -eq 0 -or $trimmed.StartsWith("#") -or $trimmed.StartsWith(";")) { continue } $parts = $trimmed.Split("=", 2) if ($parts.Count -ne 2) { continue } $key = $parts[0].Trim() $value = $parts[1].Trim() if ($key.Length -gt 0) { $result[$key] = $value } } return $result } function Require-Value { param( [hashtable]$Info, [string]$Key ) if (-not $Info.ContainsKey($Key) -or [string]::IsNullOrWhiteSpace([string]$Info[$Key])) { throw "Missing required config value: $Key" } return [string]$Info[$Key] } $info = Read-ProjectInfo -Path $ConfigPath $product = Require-Value -Info $info -Key "PRODUCT" $version = Require-Value -Info $info -Key "VERSION" $chip = Require-Value -Info $info -Key "CHIP" if ([string]::IsNullOrWhiteSpace($Mac)) { if ($info.ContainsKey("OFFLINE_MAC")) { $Mac = [string]$info["OFFLINE_MAC"] } else { $Mac = "40-ab-00-cd-01-01" } } if ([string]::IsNullOrWhiteSpace($Count)) { if ($info.ContainsKey("OFFLINE_COUNT")) { $Count = [string]$info["OFFLINE_COUNT"] } else { $Count = "1000000" } } if ([string]::IsNullOrWhiteSpace($OnlineBinPath)) { $OnlineBinPath = Join-Path $repoRoot "project\example\ble\ble_peripheral\mdk\ble_peripheral.bin" } if ([string]::IsNullOrWhiteSpace($OtaBinPath)) { $OtaBinPath = Join-Path $repoRoot "project\example\ble\ble_peripheral\mdk\output_bin\ota_data.bin" } $offlineDir = Join-Path $repoRoot "tools\offline_burn" $offlineTool = Join-Path $offlineDir "Xinc_mac_tool.exe" $offlineInput = Join-Path $offlineDir "download.bin" $releaseDir = Join-Path $repoRoot "release" $onlineReleaseDir = Join-Path $releaseDir "online" $otaReleaseDir = Join-Path $releaseDir "ota" if (-not (Test-Path -LiteralPath $OnlineBinPath)) { throw "Online BIN not found: $OnlineBinPath" } if (-not (Test-Path -LiteralPath $offlineTool)) { throw "Offline checksum tool not found: $offlineTool" } Copy-Item -LiteralPath $OnlineBinPath -Destination $offlineInput -Force Push-Location $offlineDir try { & $offlineTool $Mac $Count if ($LASTEXITCODE -ne 0) { throw "Xinc_mac_tool.exe failed with exit code $LASTEXITCODE" } } finally { Pop-Location } $namePattern = "^" + [regex]::Escape($Mac) + "_([0-9A-Fa-f]{8})_" + [regex]::Escape($Count) + "\.bin$" $generated = Get-ChildItem -LiteralPath $offlineDir -File -Filter "$Mac`_*.bin" | Where-Object { $_.Name -match $namePattern } | Sort-Object LastWriteTimeUtc -Descending | Select-Object -First 1 if ($null -eq $generated) { throw "Checksum output was not found in $offlineDir" } $checksum = ([regex]::Match($generated.Name, $namePattern)).Groups[1].Value.ToUpperInvariant() New-Item -ItemType Directory -Force -Path $onlineReleaseDir | Out-Null $onlineName = "{0}_{1}_{2}_{3}.bin" -f $product, $version, $chip, $checksum $onlineReleasePath = Join-Path $onlineReleaseDir $onlineName Copy-Item -LiteralPath $OnlineBinPath -Destination $onlineReleasePath -Force Write-Host "Checksum source: $($generated.FullName)" Write-Host "Online BIN: $onlineReleasePath" if (-not $NoOta) { if (Test-Path -LiteralPath $OtaBinPath) { New-Item -ItemType Directory -Force -Path $otaReleaseDir | Out-Null $otaName = "{0}_{1}_{2}_{3}_OTA.bin" -f $product, $version, $chip, $checksum $otaReleasePath = Join-Path $otaReleaseDir $otaName Copy-Item -LiteralPath $OtaBinPath -Destination $otaReleasePath -Force Write-Host "OTA BIN: $otaReleasePath" } else { Write-Warning "OTA BIN not found, skipped: $OtaBinPath" } }