添加遥控、参数装载、文件传输等业务
This commit is contained in:
@@ -1,83 +1,487 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Build the Windows slave example and optionally run host tests.
|
||||
.EXAMPLE
|
||||
./scripts/build.ps1 -Test
|
||||
.EXAMPLE
|
||||
./scripts/build.ps1 -Test -Suite Read -BuildName read-check
|
||||
#>
|
||||
param(
|
||||
[switch]$Test,
|
||||
[string]$Compiler = 'gcc'
|
||||
[ValidateSet('All', 'Dtu', 'Common', 'Transport', 'Slave', 'Example', 'Command', 'Read', 'SystemCommand', 'Parameter', 'RemoteParameter', 'FileService', 'SoftwareUpgrade', 'FirmwareUpdate', 'Spontaneous', 'FaultEvent', 'Recording', 'History', 'UserLog', 'EnergyFreeze', 'PeriodicScan', 'Rules', 'Loopback')]
|
||||
[string]$Suite = 'All',
|
||||
[string]$Compiler = 'gcc',
|
||||
[ValidatePattern('^[A-Za-z0-9][A-Za-z0-9_-]*$')]
|
||||
[string]$BuildName = 'slave-example'
|
||||
)
|
||||
$ErrorActionPreference = 'Stop'
|
||||
if ($PSBoundParameters.ContainsKey('Suite') -and -not $Test) {
|
||||
throw '-Suite requires -Test'
|
||||
}
|
||||
Get-Command $Compiler -ErrorAction Stop | Out-Null
|
||||
if ($Test -and $Suite -in @('All', 'Dtu', 'Loopback', 'History', 'UserLog', 'EnergyFreeze', 'FirmwareUpdate')) {
|
||||
Get-Command node -ErrorAction Stop | Out-Null
|
||||
}
|
||||
$projectRoot = Split-Path -Parent $PSScriptRoot
|
||||
$protocolInclude = Join-Path $projectRoot '104'
|
||||
$businessInclude = Join-Path $protocolInclude 'business'
|
||||
$commonInclude = Join-Path $projectRoot 'common'
|
||||
$exampleBuild = Join-Path $projectRoot '.build/slave-example'
|
||||
$exampleInclude = Join-Path $projectRoot 'examples'
|
||||
$exampleBuild = Join-Path (Join-Path $projectRoot '.build') $BuildName
|
||||
New-Item -ItemType Directory -Path $exampleBuild -Force | Out-Null
|
||||
Get-Command $Compiler -ErrorAction Stop | Out-Null
|
||||
|
||||
# Shared dependencies; the example adds the actual TCP and Windows Net implementations.
|
||||
$libraryNames = @(
|
||||
'104/business/IEC104_Interrogation.c', '104/business/IEC104_GeneralInterrogation.c',
|
||||
'104/business/IEC104_CounterInterrogation.c', '104/business/IEC104_ClockSynchronization.c',
|
||||
'104/business/IEC104_Command.c',
|
||||
'104/business/IEC104_Command.c', '104/business/IEC104_Read.c',
|
||||
'104/business/IEC104_SystemCommand.c',
|
||||
'104/business/IEC104_Startup.c',
|
||||
'104/business/IEC104_Parameter.c',
|
||||
'104/business/IEC104_RemoteParameter.c',
|
||||
'104/business/IEC104_FileService.c',
|
||||
'104/business/IEC104_SoftwareUpgrade.c',
|
||||
'104/business/IEC104_Spontaneous.c',
|
||||
'104/business/IEC104_PeriodicScan.c',
|
||||
'104/IEC104_Slave.c',
|
||||
'common/IEC60870_Slave.c', 'common/IEC60870_Asdu.c',
|
||||
'common/IEC60870_InformationObjects.c', 'common/IEC60870_Time.c', 'common/IEC60870_Clock.c',
|
||||
'common/IEC60870_Codec.c',
|
||||
'common/IEC60870_Codec.c', 'common/IEC60870_FaultEvent.c',
|
||||
'common/IEC60870_FaultRecorder.c', 'common/IEC60870_Comtrade.c',
|
||||
'common/IEC60870_History.c', 'common/IEC60870_HistoryFile.c',
|
||||
'common/IEC60870_EnergyFreeze.c', 'common/IEC60870_EnergyFreezeFile.c',
|
||||
'common/IEC60870_UserLog.c', 'common/IEC60870_UserLogFile.c',
|
||||
'common/IEC60870_FirmwareUpdate.c',
|
||||
'104/IEC104_Connection.c', '104/IEC104_Receiver.c', '104/IEC104_SendManager.c',
|
||||
'104/IEC104_SendBuffer.c', '104/IEC104_AsduQueue.c', '104/IEC104_Codec.c',
|
||||
'104/IEC104_Link.c', '104/IEC104_Link_Slave_StartStop.c'
|
||||
)
|
||||
$librarySources = @($libraryNames | ForEach-Object { Join-Path $projectRoot $_ })
|
||||
$compileFlags = @('-std=c11', '-O2', '-Wall', '-Wextra', '-Werror', "-I$protocolInclude", "-I$businessInclude", "-I$commonInclude")
|
||||
$compileFlags = @('-std=c11', '-O2', '-Wall', '-Wextra', '-Werror', "-I$protocolInclude", "-I$businessInclude", "-I$commonInclude", "-I$exampleInclude")
|
||||
$exampleOutput = Join-Path $exampleBuild 'iec104_slave_example.exe'
|
||||
$exampleSources = @(
|
||||
(Join-Path $projectRoot 'examples/iec104_slave_example.c'),
|
||||
(Join-Path $projectRoot 'examples/iec104_example_config.c'),
|
||||
(Join-Path $projectRoot 'examples/iec104_example_recording.c'),
|
||||
(Join-Path $projectRoot 'examples/iec104_example_history.c'),
|
||||
(Join-Path $projectRoot 'examples/iec104_example_energy_freeze.c'),
|
||||
(Join-Path $projectRoot 'examples/iec104_example_energy_meter.c'),
|
||||
(Join-Path $projectRoot 'examples/iec104_example_user_log.c'),
|
||||
(Join-Path $projectRoot 'examples/iec104_example_upgrade.c'),
|
||||
(Join-Path $projectRoot '104/IEC104_TcpServer.c'),
|
||||
(Join-Path $projectRoot '104/IEC104_Net_Windows.c')
|
||||
) + $librarySources
|
||||
& $Compiler @compileFlags @exampleSources -o $exampleOutput -lws2_32
|
||||
# The runtime upgrade switch enables a module; capacity is fixed at build time.
|
||||
& $Compiler @compileFlags '-DIEC104_TCP_ACK_WATCH_CAPACITY=2' @exampleSources -o $exampleOutput -lws2_32
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Slave example build failed' }
|
||||
Write-Output "Built: $exampleOutput"
|
||||
Write-Output "Run: & '$exampleOutput' --ca 1"
|
||||
|
||||
# Dedicated DTU profile; the generic eight-point example remains a protocol fixture.
|
||||
$dtuOutput = Join-Path $exampleBuild 'iec104_dtu_example.exe'
|
||||
$dtuSources = @(
|
||||
(Join-Path $projectRoot 'examples/iec104_dtu_example.c'),
|
||||
(Join-Path $projectRoot 'examples/iec104_dtu_settings.c'),
|
||||
(Join-Path $projectRoot 'examples/iec104_dtu_measurement.c'),
|
||||
(Join-Path $projectRoot 'examples/iec104_dtu_protection.c')
|
||||
) + $exampleSources[1..($exampleSources.Count - 1)]
|
||||
& $Compiler @compileFlags '-DIEC104_EXAMPLE_DTU_PROFILE=1' '-DIEC104_TCP_ACK_WATCH_CAPACITY=2' @dtuSources -o $dtuOutput -lws2_32
|
||||
if ($LASTEXITCODE -ne 0) { throw 'DTU example build failed' }
|
||||
Write-Output "Built: $dtuOutput"
|
||||
Write-Output "DTU 100V/1A demo: & '$dtuOutput' --ca 1"
|
||||
|
||||
if ($Test) {
|
||||
# Shared Station/Slave APIs, including isolation between two independent services.
|
||||
foreach ($capacity in @(1, 2)) {
|
||||
$slaveTestOutput = Join-Path $exampleBuild "iec104_slave_test_$capacity.exe"
|
||||
$slaveTestSources = @((Join-Path $projectRoot 'test/iec104_slave_test.c')) + $librarySources
|
||||
& $Compiler @compileFlags "-DIEC104_TCP_SERVER_CAPACITY=$capacity" @slaveTestSources -o $slaveTestOutput
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Slave/Station test build failed' }
|
||||
& $slaveTestOutput
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Slave/Station tests failed' }
|
||||
$testBuild = Join-Path $exampleBuild 'tests'
|
||||
New-Item -ItemType Directory -Path $testBuild -Force | Out-Null
|
||||
$script:passedTests = 0
|
||||
|
||||
function Test-Suite([string]$Name) {
|
||||
return $Suite -eq 'All' -or $Suite -eq $Name
|
||||
}
|
||||
# The controlled-backend test includes TcpServer.c itself. Do not link a second TCP/Net implementation.
|
||||
$testSources = @((Join-Path $projectRoot 'test/iec104_slave_example_test.c')) + $librarySources
|
||||
foreach ($capacity in @(1, 2)) {
|
||||
$testOutput = Join-Path $exampleBuild "iec104_slave_example_test_$capacity.exe"
|
||||
& $Compiler @compileFlags "-DIEC104_TCP_SERVER_CAPACITY=$capacity" -DIEC104_TCP_HIGH_CAPACITY=251 -DIEC104_INTERROGATION_EVENT_CAPACITY=2 @testSources -o $testOutput
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Slave example test build failed' }
|
||||
& $testOutput
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Slave example tests failed' }
|
||||
|
||||
function Invoke-HostTest {
|
||||
param([string]$Name, [string[]]$Sources, [string[]]$ExtraFlags = @(), [string[]]$LinkFlags = @(), [string[]]$RunArgs = @())
|
||||
$output = Join-Path $testBuild "$Name.exe"
|
||||
Write-Output "Testing: $Name"
|
||||
& $Compiler @compileFlags @ExtraFlags @Sources -o $output @LinkFlags
|
||||
# Never execute an old binary after a failed compilation.
|
||||
if ($LASTEXITCODE -ne 0) { throw "$Name build failed" }
|
||||
& $output @RunArgs
|
||||
if ($LASTEXITCODE -ne 0) { throw "$Name failed" }
|
||||
$script:passedTests++
|
||||
}
|
||||
# Command transactions use one fixed slot and a bounded independent reply pool.
|
||||
# The controlled backend includes TcpServer.c; do not link TCP/Windows Net again.
|
||||
$commandTestSources = @((Join-Path $projectRoot 'test/iec104_command_test.c')) + $librarySources
|
||||
foreach ($capacity in @(1, 2)) {
|
||||
foreach ($replyCapacity in @(1, 4)) {
|
||||
$commandOutput = Join-Path $exampleBuild "iec104_command_test_${capacity}_${replyCapacity}.exe"
|
||||
& $Compiler @compileFlags "-DIEC104_TCP_SERVER_CAPACITY=$capacity" "-DIEC104_COMMAND_REPLY_CAPACITY=$replyCapacity" -DIEC104_TCP_HIGH_CAPACITY=251 @commandTestSources -o $commandOutput
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Command test build failed' }
|
||||
& $commandOutput
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Command tests failed' }
|
||||
|
||||
if (Test-Suite 'Dtu') {
|
||||
foreach ($model in @('settings_model', 'measurement', 'protection')) {
|
||||
$modelSources = @((Join-Path $projectRoot "test/iec104_dtu_${model}_test.c"),
|
||||
(Join-Path $projectRoot 'examples/iec104_dtu_settings.c'))
|
||||
if ($model -ne 'settings_model') { $modelSources += (Join-Path $projectRoot "examples/iec104_dtu_${model}.c") }
|
||||
Invoke-HostTest -Name "iec104_dtu_${model}_test" -Sources $modelSources -LinkFlags @('-lm')
|
||||
}
|
||||
$triggerSources = @('test/iec104_dtu_trigger_recording_test.c', 'examples/iec104_example_recording.c',
|
||||
'common/IEC60870_FaultRecorder.c', 'common/IEC60870_Comtrade.c', 'common/IEC60870_Time.c') |
|
||||
ForEach-Object { Join-Path $projectRoot $_ }
|
||||
Invoke-HostTest -Name 'iec104_dtu_trigger_recording_test' -Sources $triggerSources `
|
||||
-ExtraFlags @('-std=c99', '-pedantic', '-DIEC104_EXAMPLE_DTU_PROFILE=1', '-DIEC60870_TIME_USE_PLATFORM=0') -LinkFlags @('-lm')
|
||||
$gainSources = @('test/iec104_dtu_energy_settings_test.c', 'examples/iec104_example_energy_meter.c',
|
||||
'common/IEC60870_EnergyFreeze.c', 'common/IEC60870_Time.c') |
|
||||
ForEach-Object { Join-Path $projectRoot $_ }
|
||||
Invoke-HostTest -Name 'iec104_dtu_energy_settings_test' -Sources $gainSources `
|
||||
-ExtraFlags @('-std=c99', '-pedantic', '-DIEC104_EXAMPLE_DTU_PROFILE=1', '-DIEC60870_TIME_USE_PLATFORM=0')
|
||||
& node (Join-Path $projectRoot 'test/dtu_example_loopback_test.cjs') $dtuOutput
|
||||
if ($LASTEXITCODE -ne 0) { throw 'DTU XML/profile/TCP test failed' }
|
||||
& node (Join-Path $projectRoot 'test/dtu_settings_loopback_test.cjs') $dtuOutput
|
||||
if ($LASTEXITCODE -ne 0) { throw 'DTU settings/persistence TCP test failed' }
|
||||
& node (Join-Path $projectRoot 'test/dtu_features_loopback_test.cjs') $dtuOutput
|
||||
if ($LASTEXITCODE -ne 0) { throw 'DTU complete features TCP test failed' }
|
||||
& node (Join-Path $projectRoot 'test/dtu_process_loopback_test.cjs') $dtuOutput
|
||||
if ($LASTEXITCODE -ne 0) { throw 'DTU process/settings effect TCP test failed' }
|
||||
$dtuStorageDirectory = Join-Path $testBuild 'dtu-storage'
|
||||
New-Item -ItemType Directory -Path $dtuStorageDirectory -Force | Out-Null
|
||||
$dtuStorageSources = @('test/iec104_dtu_storage_test.c',
|
||||
'examples/iec104_example_recording.c', 'examples/iec104_example_energy_meter.c',
|
||||
'examples/iec104_example_energy_freeze.c', 'common/IEC60870_FaultRecorder.c',
|
||||
'common/IEC60870_Comtrade.c', 'common/IEC60870_EnergyFreeze.c',
|
||||
'common/IEC60870_EnergyFreezeFile.c', 'common/IEC60870_Time.c') |
|
||||
ForEach-Object { Join-Path $projectRoot $_ }
|
||||
Invoke-HostTest -Name 'iec104_dtu_storage_test' -Sources $dtuStorageSources `
|
||||
-ExtraFlags @('-std=c99', '-pedantic', '-DIEC104_EXAMPLE_DTU_PROFILE=1', '-DIEC60870_TIME_USE_PLATFORM=0') `
|
||||
-RunArgs @($dtuStorageDirectory)
|
||||
}
|
||||
|
||||
$asduSources = @('common/IEC60870_Asdu.c', 'common/IEC60870_InformationObjects.c',
|
||||
'common/IEC60870_Codec.c', 'common/IEC60870_Time.c') |
|
||||
ForEach-Object { Join-Path $projectRoot $_ }
|
||||
if (Test-Suite 'Common') {
|
||||
foreach ($name in @('iec60870_asdu_test', 'iec60870_asdu_extended_test')) {
|
||||
$sources = @((Join-Path $projectRoot "test/$name.c")) + $asduSources
|
||||
Invoke-HostTest -Name $name -Sources $sources
|
||||
Invoke-HostTest -Name "${name}_no_platform" -Sources $sources -ExtraFlags '-DIEC60870_TIME_USE_PLATFORM=0'
|
||||
}
|
||||
}
|
||||
$rulesOutput = Join-Path $exampleBuild 'iec104_interrogation_rules_test.exe'
|
||||
$rulesSources = @(
|
||||
(Join-Path $projectRoot 'test/iec104_interrogation_rules_test.c'),
|
||||
(Join-Path $projectRoot '104/IEC104_TcpServer.c'),
|
||||
(Join-Path $projectRoot '104/IEC104_Net_Windows.c')
|
||||
) + $librarySources
|
||||
& $Compiler @compileFlags @rulesSources -o $rulesOutput -lws2_32
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Interrogation rules test build failed' }
|
||||
& $rulesOutput
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Interrogation rules tests failed' }
|
||||
Get-Command node -ErrorAction Stop | Out-Null
|
||||
& node (Join-Path $projectRoot 'test/slave_example_loopback_test.cjs') $exampleOutput
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Slave example TCP loopback test failed' }
|
||||
if ((Test-Suite 'Common') -or $Suite -eq 'FaultEvent') {
|
||||
$faultCodecSources = @((Join-Path $projectRoot 'test/iec60870_fault_event_test.c'),
|
||||
(Join-Path $projectRoot 'common/IEC60870_FaultEvent.c')) + $asduSources
|
||||
Invoke-HostTest -Name 'iec60870_fault_event_test' -Sources $faultCodecSources
|
||||
Invoke-HostTest -Name 'iec60870_fault_event_no_platform' -Sources $faultCodecSources -ExtraFlags '-DIEC60870_TIME_USE_PLATFORM=0'
|
||||
}
|
||||
if ((Test-Suite 'Common') -or $Suite -eq 'Recording') {
|
||||
$recordingSources = @('common/IEC60870_FaultRecorder.c', 'common/IEC60870_Comtrade.c',
|
||||
'common/IEC60870_Time.c') | ForEach-Object { Join-Path $projectRoot $_ }
|
||||
foreach ($name in @('iec60870_fault_recorder_test', 'iec60870_comtrade_test')) {
|
||||
Invoke-HostTest -Name $name -Sources (@((Join-Path $projectRoot "test/$name.c")) + $recordingSources) `
|
||||
-ExtraFlags @('-std=c99', '-pedantic', '-DIEC60870_TIME_USE_PLATFORM=0')
|
||||
}
|
||||
$recordingDirectory = Join-Path $exampleBuild 'recording-adapter'
|
||||
New-Item -ItemType Directory -Path $recordingDirectory -Force | Out-Null
|
||||
Invoke-HostTest -Name 'iec104_example_recording_test' -Sources (@(
|
||||
(Join-Path $projectRoot 'test/iec104_example_recording_test.c'),
|
||||
(Join-Path $projectRoot 'examples/iec104_example_recording.c')) + $recordingSources) `
|
||||
-ExtraFlags @('-std=c99', '-pedantic', '-DIEC60870_TIME_USE_PLATFORM=0') -RunArgs @($recordingDirectory)
|
||||
}
|
||||
|
||||
# Historical storage and XML stay in independent C99 modules; the host adapter
|
||||
# additionally exercises real durable writes. The TCP peer checks the complete
|
||||
# integration without borrowing the library's encoders or changing host time.
|
||||
if (Test-Suite 'History') {
|
||||
$historySources = @('common/IEC60870_History.c', 'common/IEC60870_HistoryFile.c',
|
||||
'common/IEC60870_Time.c') | ForEach-Object { Join-Path $projectRoot $_ }
|
||||
foreach ($name in @('iec60870_history_test', 'iec60870_history_file_test')) {
|
||||
Invoke-HostTest -Name $name -Sources (@((Join-Path $projectRoot "test/$name.c")) + $historySources) `
|
||||
-ExtraFlags @('-std=c99', '-pedantic', '-DIEC60870_TIME_USE_PLATFORM=0')
|
||||
}
|
||||
$historyDirectory = Join-Path $exampleBuild 'history-adapter'
|
||||
New-Item -ItemType Directory -Path $historyDirectory -Force | Out-Null
|
||||
Invoke-HostTest -Name 'iec104_example_history_test' -Sources (@(
|
||||
(Join-Path $projectRoot 'test/iec104_example_history_test.c'),
|
||||
(Join-Path $projectRoot 'examples/iec104_example_history.c')) + $historySources) `
|
||||
-ExtraFlags @('-std=c99', '-pedantic', '-DIEC60870_TIME_USE_PLATFORM=0', '-DIEC104_EXAMPLE_HISTORY_TEST') -RunArgs @($historyDirectory)
|
||||
# Reuse this build's executable, never an older default-directory binary.
|
||||
& node (Join-Path $projectRoot 'test/history_loopback_test.cjs') $exampleOutput
|
||||
if ($LASTEXITCODE -ne 0) { throw 'History TCP/XML/restart test failed' }
|
||||
}
|
||||
|
||||
if (Test-Suite 'EnergyFreeze') {
|
||||
$energySources = @('common/IEC60870_EnergyFreeze.c', 'common/IEC60870_EnergyFreezeFile.c',
|
||||
'common/IEC60870_Time.c') | ForEach-Object { Join-Path $projectRoot $_ }
|
||||
foreach ($name in @('iec60870_energy_freeze_test', 'iec60870_energy_freeze_file_test')) {
|
||||
Invoke-HostTest -Name $name -Sources (@((Join-Path $projectRoot "test/$name.c")) + $energySources) `
|
||||
-ExtraFlags @('-std=c99', '-pedantic', '-DIEC60870_TIME_USE_PLATFORM=0')
|
||||
}
|
||||
Invoke-HostTest -Name 'iec104_example_energy_meter_test' -Sources (@(
|
||||
(Join-Path $projectRoot 'test/iec104_example_energy_meter_test.c'),
|
||||
(Join-Path $projectRoot 'examples/iec104_example_energy_meter.c')) + $energySources) `
|
||||
-ExtraFlags @('-std=c99', '-pedantic', '-DIEC60870_TIME_USE_PLATFORM=0')
|
||||
$energyDirectory = Join-Path $exampleBuild 'energy-adapter'
|
||||
New-Item -ItemType Directory -Path $energyDirectory -Force | Out-Null
|
||||
Invoke-HostTest -Name 'iec104_example_energy_freeze_test' -Sources (@(
|
||||
(Join-Path $projectRoot 'test/iec104_example_energy_freeze_test.c'),
|
||||
(Join-Path $projectRoot 'examples/iec104_example_energy_freeze.c')) + $energySources) `
|
||||
-ExtraFlags @('-std=c99', '-pedantic', '-DIEC60870_TIME_USE_PLATFORM=0', '-DIEC104_EXAMPLE_ENERGY_FREEZE_TEST') `
|
||||
-RunArgs @($energyDirectory)
|
||||
Invoke-HostTest -Name 'iec104_energy_freeze_transitions_test' -Sources (@(
|
||||
(Join-Path $projectRoot 'test/iec104_energy_freeze_transitions_test.c'),
|
||||
(Join-Path $projectRoot 'examples/iec104_example_energy_freeze.c')) + $energySources) `
|
||||
-ExtraFlags @('-std=c99', '-pedantic', '-DIEC60870_TIME_USE_PLATFORM=0', '-DIEC104_EXAMPLE_ENERGY_FREEZE_TEST') `
|
||||
-RunArgs @($energyDirectory)
|
||||
& node (Join-Path $projectRoot 'test/energy_freeze_loopback_test.cjs') $exampleOutput
|
||||
if ($LASTEXITCODE -ne 0) { throw 'EnergyFreeze TCP/XML/restart test failed' }
|
||||
}
|
||||
|
||||
# Annex C uses its own durable ring and XML schema; its restart test does not
|
||||
# depend on a third-party UI or on changing the host wall clock.
|
||||
# Core/XML fixtures use C99 and the integer calendar backend to expose hidden
|
||||
# host-time dependencies; they do not link the Windows storage adapter.
|
||||
if (Test-Suite 'UserLog') {
|
||||
$userLogSources = @('common/IEC60870_UserLog.c', 'common/IEC60870_UserLogFile.c',
|
||||
'common/IEC60870_Time.c') | ForEach-Object { Join-Path $projectRoot $_ }
|
||||
foreach ($name in @('iec60870_user_log_test', 'iec60870_user_log_file_test')) {
|
||||
Invoke-HostTest -Name $name -Sources (@((Join-Path $projectRoot "test/$name.c")) + $userLogSources) `
|
||||
-ExtraFlags @('-std=c99', '-pedantic', '-DIEC60870_TIME_USE_PLATFORM=0')
|
||||
}
|
||||
# Keep every real-file fixture beneath this build. The hook fails only
|
||||
# after a completed flush; production builds contain no fault injector.
|
||||
$userLogDirectory = Join-Path $exampleBuild 'user-log-adapter'
|
||||
New-Item -ItemType Directory -Path $userLogDirectory -Force | Out-Null
|
||||
Invoke-HostTest -Name 'iec104_example_user_log_test' -Sources (@(
|
||||
(Join-Path $projectRoot 'test/iec104_example_user_log_test.c'),
|
||||
(Join-Path $projectRoot 'examples/iec104_example_user_log.c')) + $userLogSources) `
|
||||
-ExtraFlags @('-std=c99', '-pedantic', '-DIEC60870_TIME_USE_PLATFORM=0', '-DIEC104_EXAMPLE_USER_LOG_TEST') -RunArgs @($userLogDirectory)
|
||||
# Use the executable just built, so the real TCP/restart check cannot
|
||||
# silently exercise an older default-directory binary.
|
||||
& node (Join-Path $projectRoot 'test/user_log_loopback_test.cjs') $exampleOutput
|
||||
if ($LASTEXITCODE -ne 0) { throw 'UserLog TCP/XML/restart test failed' }
|
||||
}
|
||||
|
||||
# Verify three layers: portable C99 state, durable Windows files, and real TCP.
|
||||
# Both C fixtures add pedantic checks even though the example uses C11.
|
||||
# Fault injection is linked only into the host fixture, never the example.
|
||||
# These tests install payload files; they do not execute the received bytes.
|
||||
if (Test-Suite 'FirmwareUpdate') {
|
||||
$firmwareSource = Join-Path $projectRoot 'common/IEC60870_FirmwareUpdate.c'
|
||||
Invoke-HostTest -Name 'iec60870_firmware_update_test' -Sources @(
|
||||
(Join-Path $projectRoot 'test/iec60870_firmware_update_test.c'), $firmwareSource) `
|
||||
-ExtraFlags @('-std=c99', '-pedantic')
|
||||
# Isolate real-file fixtures under this build, away from other upgrade stores.
|
||||
$upgradeDirectory = Join-Path $exampleBuild 'upgrade-adapter'
|
||||
New-Item -ItemType Directory -Path $upgradeDirectory -Force | Out-Null
|
||||
Invoke-HostTest -Name 'iec104_example_upgrade_test' -Sources @(
|
||||
(Join-Path $projectRoot 'test/iec104_example_upgrade_test.c'),
|
||||
(Join-Path $projectRoot 'examples/iec104_example_upgrade.c'), $firmwareSource) `
|
||||
-ExtraFlags @('-std=c99', '-pedantic', '-DIEC104_EXAMPLE_UPGRADE_TEST') -RunArgs @($upgradeDirectory)
|
||||
# Pass only this build's successful executable; Node constructs independent
|
||||
# package bytes, CRC expectations, and installed/backup file comparisons.
|
||||
& node (Join-Path $projectRoot 'test/firmware_update_loopback_test.cjs') $exampleOutput
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Firmware installation TCP/restart test failed' }
|
||||
}
|
||||
|
||||
if (Test-Suite 'Transport') {
|
||||
Invoke-HostTest -Name 'iec104_asdu_transport_test' -Sources (@(
|
||||
(Join-Path $projectRoot 'test/iec104_asdu_transport_test.c'),
|
||||
(Join-Path $projectRoot '104/IEC104_Codec.c')) + $asduSources)
|
||||
Invoke-HostTest -Name 'iec104_response_queue_test' -Sources @(
|
||||
(Join-Path $projectRoot 'test/iec104_response_queue_test.c'),
|
||||
(Join-Path $projectRoot '104/IEC104_AsduQueue.c'))
|
||||
Invoke-HostTest -Name 'iec104_strict_priority_queue_test' -Sources @(
|
||||
(Join-Path $projectRoot 'test/iec104_strict_priority_queue_test.c'),
|
||||
(Join-Path $projectRoot '104/IEC104_AsduQueue.c'))
|
||||
# These fixtures include TcpServer.c and a controlled Net backend themselves.
|
||||
foreach ($name in @('iec104_tcp_event_ring_test', 'iec104_tcp_scheduler_test', 'iec104_response_transport_test', 'iec104_ack_tracking_test', 'iec104_idle_transport_test', 'iec104_strict_priority_transport_test')) {
|
||||
Invoke-HostTest -Name $name -Sources (@((Join-Path $projectRoot "test/$name.c")) + $librarySources)
|
||||
}
|
||||
$ringSources = @((Join-Path $projectRoot 'test/iec104_tcp_event_ring_test.c')) + $librarySources
|
||||
Invoke-HostTest -Name 'iec104_tcp_event_ring_test_small' -Sources $ringSources -ExtraFlags @(
|
||||
'-DIEC104_TCP_RECORD_CAPACITY=3', '-DIEC104_TCP_EVENT_CAPACITY=12', '-DIEC104_TCP_EVENT_STATE_RESERVE=2')
|
||||
Invoke-HostTest -Name 'iec104_tcp_event_ring_test_large' -Sources $ringSources -ExtraFlags @(
|
||||
'-DIEC104_TCP_RECORD_CAPACITY=40', '-DIEC104_TCP_EVENT_CAPACITY=100',
|
||||
'-DIEC104_TCP_EVENT_STATE_RESERVE=6', '-DIEC104_TCP_RX_CAPACITY=4096')
|
||||
}
|
||||
|
||||
foreach ($capacity in @(1, 2)) {
|
||||
$capacityFlag = "-DIEC104_TCP_SERVER_CAPACITY=$capacity"
|
||||
if (Test-Suite 'Slave') {
|
||||
Invoke-HostTest -Name "iec104_slave_test_$capacity" -Sources (@(
|
||||
(Join-Path $projectRoot 'test/iec104_slave_test.c')) + $librarySources) -ExtraFlags $capacityFlag
|
||||
}
|
||||
if (Test-Suite 'Example') {
|
||||
Invoke-HostTest -Name "iec104_slave_example_test_$capacity" -Sources (@(
|
||||
(Join-Path $projectRoot 'test/iec104_slave_example_test.c'),
|
||||
(Join-Path $projectRoot 'examples/iec104_example_recording.c'),
|
||||
(Join-Path $projectRoot 'examples/iec104_example_history.c'),
|
||||
(Join-Path $projectRoot 'examples/iec104_example_energy_freeze.c'),
|
||||
(Join-Path $projectRoot 'examples/iec104_example_energy_meter.c'),
|
||||
(Join-Path $projectRoot 'examples/iec104_example_user_log.c'),
|
||||
(Join-Path $projectRoot 'examples/iec104_example_upgrade.c'),
|
||||
(Join-Path $projectRoot 'examples/iec104_example_config.c')) + $librarySources) -ExtraFlags @(
|
||||
$capacityFlag, '-DIEC104_TCP_HIGH_CAPACITY=251', '-DIEC104_INTERROGATION_EVENT_CAPACITY=2',
|
||||
'-DIEC104_TCP_ACK_WATCH_CAPACITY=2')
|
||||
Invoke-HostTest -Name "iec104_slave_example_strict_test_$capacity" -Sources (@(
|
||||
(Join-Path $projectRoot 'test/iec104_slave_example_test.c'),
|
||||
(Join-Path $projectRoot 'examples/iec104_example_recording.c'),
|
||||
(Join-Path $projectRoot 'examples/iec104_example_history.c'),
|
||||
(Join-Path $projectRoot 'examples/iec104_example_energy_freeze.c'),
|
||||
(Join-Path $projectRoot 'examples/iec104_example_energy_meter.c'),
|
||||
(Join-Path $projectRoot 'examples/iec104_example_user_log.c'),
|
||||
(Join-Path $projectRoot 'examples/iec104_example_upgrade.c'),
|
||||
(Join-Path $projectRoot 'examples/iec104_example_config.c')) + $librarySources) -ExtraFlags @(
|
||||
$capacityFlag, '-DIEC104_TCP_HIGH_CAPACITY=1024', '-DIEC104_INTERROGATION_EVENT_CAPACITY=2',
|
||||
'-DIEC104_TCP_ACK_WATCH_CAPACITY=2', '-DIEC104_EXAMPLE_STRICT_TEST_ONLY=1')
|
||||
}
|
||||
if (Test-Suite 'Command') {
|
||||
foreach ($replyCapacity in @(1, 4)) {
|
||||
Invoke-HostTest -Name "iec104_command_test_${capacity}_${replyCapacity}" -Sources (@(
|
||||
(Join-Path $projectRoot 'test/iec104_command_test.c')) + $librarySources) -ExtraFlags @(
|
||||
$capacityFlag, "-DIEC104_COMMAND_REPLY_CAPACITY=$replyCapacity",
|
||||
'-DIEC104_TCP_HIGH_CAPACITY=251', '-DIEC104_COMMAND_TEST_HOOKS')
|
||||
}
|
||||
}
|
||||
if (Test-Suite 'Read') {
|
||||
foreach ($taskCapacity in @(1, 4)) {
|
||||
Invoke-HostTest -Name "iec104_read_test_${capacity}_${taskCapacity}" -Sources (@(
|
||||
(Join-Path $projectRoot 'test/iec104_read_test.c')) + $librarySources) -ExtraFlags @(
|
||||
$capacityFlag, "-DIEC104_READ_TASK_CAPACITY=$taskCapacity",
|
||||
'-DIEC104_TCP_HIGH_CAPACITY=251', '-DIEC104_READ_TEST_HOOKS')
|
||||
}
|
||||
}
|
||||
if (Test-Suite 'SystemCommand') {
|
||||
foreach ($taskCapacity in @(1, 4)) {
|
||||
Invoke-HostTest -Name "iec104_system_command_test_${capacity}_${taskCapacity}" -Sources (@(
|
||||
(Join-Path $projectRoot 'test/iec104_system_command_test.c')) + $librarySources) -ExtraFlags @(
|
||||
$capacityFlag, "-DIEC104_SYSTEM_COMMAND_TASK_CAPACITY=$taskCapacity",
|
||||
'-DIEC104_TCP_HIGH_CAPACITY=251', '-DIEC104_SYSTEM_COMMAND_TEST_HOOKS')
|
||||
}
|
||||
}
|
||||
if (Test-Suite 'RemoteParameter') {
|
||||
foreach ($taskCapacity in @(1, 2, 4)) {
|
||||
Invoke-HostTest -Name "iec104_remote_parameter_test_${capacity}_${taskCapacity}" -Sources (@(
|
||||
(Join-Path $projectRoot 'test/iec104_remote_parameter_test.c')) + $librarySources) -ExtraFlags @(
|
||||
$capacityFlag, "-DIEC104_REMOTE_PARAMETER_TASK_CAPACITY=$taskCapacity",
|
||||
'-DIEC104_TCP_HIGH_CAPACITY=251', '-DIEC104_REMOTE_PARAMETER_TEST_HOOKS')
|
||||
}
|
||||
}
|
||||
if (Test-Suite 'FileService') {
|
||||
foreach ($taskCapacity in @(1, 2)) {
|
||||
foreach ($segmentCapacity in @(1, 2, 4)) {
|
||||
Invoke-HostTest -Name "iec104_file_service_test_${capacity}_${taskCapacity}_${segmentCapacity}" -Sources (@(
|
||||
(Join-Path $projectRoot 'test/iec104_file_service_test.c')) + $librarySources) -ExtraFlags @(
|
||||
$capacityFlag, "-DIEC104_FILE_SERVICE_TASK_CAPACITY=$taskCapacity",
|
||||
"-DIEC104_FILE_SERVICE_SEGMENT_CAPACITY=$segmentCapacity",
|
||||
'-DIEC104_TCP_HIGH_CAPACITY=251', '-DIEC104_FILE_SERVICE_TEST_HOOKS')
|
||||
}
|
||||
}
|
||||
}
|
||||
if (Test-Suite 'SoftwareUpgrade') {
|
||||
foreach ($taskCapacity in @(1, 2)) {
|
||||
foreach ($policy in @(0, 1)) {
|
||||
Invoke-HostTest -Name "iec104_software_upgrade_test_${capacity}_${taskCapacity}_${policy}" -Sources (@(
|
||||
(Join-Path $projectRoot 'test/iec104_software_upgrade_test.c')) + $librarySources) -ExtraFlags @(
|
||||
$capacityFlag, "-DIEC104_SOFTWARE_UPGRADE_TASK_CAPACITY=$taskCapacity",
|
||||
"-DUPGRADE_TEST_POLICY=$policy", '-DIEC104_TCP_ACK_WATCH_CAPACITY=1',
|
||||
'-DIEC104_TCP_HIGH_CAPACITY=251', '-DIEC104_SOFTWARE_UPGRADE_TEST_HOOKS')
|
||||
}
|
||||
}
|
||||
}
|
||||
if (Test-Suite 'Parameter') {
|
||||
foreach ($taskCapacity in @(1, 4)) {
|
||||
Invoke-HostTest -Name "iec104_parameter_test_${capacity}_${taskCapacity}" -Sources (@(
|
||||
(Join-Path $projectRoot 'test/iec104_parameter_test.c')) + $librarySources) -ExtraFlags @(
|
||||
$capacityFlag, "-DIEC104_PARAMETER_TASK_CAPACITY=$taskCapacity",
|
||||
'-DIEC104_TCP_HIGH_CAPACITY=251', '-DIEC104_PARAMETER_TEST_HOOKS')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (Test-Suite 'RemoteParameter') {
|
||||
# Boundary probes include the implementation to reach exhausted counters.
|
||||
$remoteLibrary = $librarySources | Where-Object { $_ -ne (Join-Path $projectRoot '104/business/IEC104_RemoteParameter.c') }
|
||||
Invoke-HostTest -Name 'iec104_remote_parameter_internal_test' -Sources (@(
|
||||
(Join-Path $projectRoot 'test/iec104_remote_parameter_internal_test.c')) + $remoteLibrary) -ExtraFlags @(
|
||||
'-DIEC104_TCP_HIGH_CAPACITY=251', '-DIEC104_REMOTE_PARAMETER_TEST_HOOKS')
|
||||
}
|
||||
|
||||
if (Test-Suite 'FileService') {
|
||||
# Include the implementation once to reach non-wrapping identity/index boundaries.
|
||||
$fileLibrary = $librarySources | Where-Object { $_ -ne (Join-Path $projectRoot '104/business/IEC104_FileService.c') }
|
||||
Invoke-HostTest -Name 'iec104_file_service_internal_test' -Sources (@(
|
||||
(Join-Path $projectRoot 'test/iec104_file_service_internal_test.c')) + $fileLibrary) -ExtraFlags @(
|
||||
'-DIEC104_TCP_HIGH_CAPACITY=251', '-DIEC104_FILE_SERVICE_TEST_HOOKS')
|
||||
}
|
||||
|
||||
if (Test-Suite 'Parameter') {
|
||||
Invoke-HostTest -Name 'iec104_parameter_edge_test' -Sources (@(
|
||||
(Join-Path $projectRoot 'test/iec104_parameter_edge_test.c')) + $librarySources) -ExtraFlags '-DIEC104_TCP_HIGH_CAPACITY=251'
|
||||
}
|
||||
|
||||
if (Test-Suite 'SoftwareUpgrade') {
|
||||
$upgradeLibrary = $librarySources | Where-Object { $_ -ne (Join-Path $projectRoot '104/business/IEC104_SoftwareUpgrade.c') }
|
||||
Invoke-HostTest -Name 'iec104_software_upgrade_internal_test' -Sources (@(
|
||||
(Join-Path $projectRoot 'test/iec104_software_upgrade_internal_test.c')) + $upgradeLibrary) -ExtraFlags @(
|
||||
'-DIEC104_TCP_HIGH_CAPACITY=251', '-DIEC104_SOFTWARE_UPGRADE_TEST_HOOKS')
|
||||
}
|
||||
|
||||
if (Test-Suite 'Spontaneous') {
|
||||
# This fixture also includes Spontaneous.c for failure injection.
|
||||
$spontaneousLibrary = $librarySources | Where-Object { $_ -ne (Join-Path $projectRoot '104/business/IEC104_Spontaneous.c') }
|
||||
$spontaneousSources = @((Join-Path $projectRoot 'test/iec104_spontaneous_test.c')) + $spontaneousLibrary
|
||||
foreach ($matrix in @(@(1, 4096), @(2, 4096), @(1, 272))) {
|
||||
Invoke-HostTest -Name "iec104_spontaneous_test_$($matrix[0])_$($matrix[1])" -Sources $spontaneousSources -ExtraFlags @(
|
||||
"-DIEC104_TCP_SERVER_CAPACITY=$($matrix[0])", "-DIEC104_TCP_NORMAL_CAPACITY=$($matrix[1])")
|
||||
}
|
||||
}
|
||||
if (Test-Suite 'FaultEvent') {
|
||||
# Fault-event publishing uses the real transport and injects submission failures.
|
||||
$faultLibrary = $librarySources | Where-Object { $_ -ne (Join-Path $projectRoot '104/business/IEC104_Spontaneous.c') }
|
||||
$faultSources = @((Join-Path $projectRoot 'test/iec104_fault_event_test.c')) + $faultLibrary
|
||||
foreach ($matrix in @(@(1, 4096), @(2, 4096), @(1, 272))) {
|
||||
Invoke-HostTest -Name "iec104_fault_event_test_$($matrix[0])_$($matrix[1])" -Sources $faultSources -ExtraFlags @(
|
||||
"-DIEC104_TCP_SERVER_CAPACITY=$($matrix[0])", "-DIEC104_TCP_NORMAL_CAPACITY=$($matrix[1])")
|
||||
}
|
||||
}
|
||||
if (Test-Suite 'PeriodicScan') {
|
||||
# The fixture includes PeriodicScan.c and TcpServer.c for controlled faults.
|
||||
$scanLibrary = $librarySources | Where-Object { $_ -ne (Join-Path $projectRoot '104/business/IEC104_PeriodicScan.c') }
|
||||
$scanSources = @((Join-Path $projectRoot 'test/iec104_periodic_scan_test.c')) + $scanLibrary
|
||||
foreach ($capacity in @(1, 2)) {
|
||||
foreach ($rows in @(256, 4)) {
|
||||
foreach ($groups in @(1, 4)) {
|
||||
Invoke-HostTest -Name "iec104_periodic_scan_${capacity}_${rows}_${groups}" -Sources $scanSources -ExtraFlags @(
|
||||
"-DIEC104_TCP_SERVER_CAPACITY=$capacity", "-DIEC104_PERIODIC_SCAN_ROW_BUDGET=$rows",
|
||||
"-DIEC104_PERIODIC_SCAN_GROUP_CAPACITY=$groups")
|
||||
}
|
||||
}
|
||||
}
|
||||
Invoke-HostTest -Name 'iec104_periodic_scan_budget14' -Sources $scanSources -ExtraFlags @(
|
||||
'-DIEC104_PERIODIC_SCAN_ROW_BUDGET=14', '-DIEC104_PERIODIC_SCAN_GROUP_CAPACITY=4')
|
||||
}
|
||||
if (Test-Suite 'Rules') {
|
||||
Invoke-HostTest -Name 'iec104_interrogation_rules_test' -Sources (@(
|
||||
(Join-Path $projectRoot 'test/iec104_interrogation_rules_test.c'),
|
||||
(Join-Path $projectRoot '104/IEC104_TcpServer.c'),
|
||||
(Join-Path $projectRoot '104/IEC104_Net_Windows.c')) + $librarySources) -LinkFlags '-lws2_32'
|
||||
}
|
||||
if (Test-Suite 'Loopback') {
|
||||
Invoke-HostTest -Name 'iec104_net_pending_input_test' -Sources @(
|
||||
(Join-Path $projectRoot 'test/iec104_net_pending_input_test.c'),
|
||||
(Join-Path $projectRoot '104/IEC104_Net_Windows.c')) -LinkFlags '-lws2_32'
|
||||
& node (Join-Path $projectRoot 'test/slave_example_loopback_test.cjs') $exampleOutput
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Slave example TCP loopback test failed' }
|
||||
& node (Join-Path $projectRoot 'test/parameter_config_loopback_test.cjs') $exampleOutput
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Parameter configuration persistence test failed' }
|
||||
& node (Join-Path $projectRoot 'test/periodic_scan_loopback_test.cjs') $exampleOutput
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Periodic scan TCP loopback test failed' }
|
||||
& node (Join-Path $projectRoot 'test/remote_parameter_loopback_test.cjs') $exampleOutput
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Remote parameter TCP loopback test failed' }
|
||||
& node (Join-Path $projectRoot 'test/software_upgrade_loopback_test.cjs') $exampleOutput
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Software upgrade TCP loopback test failed' }
|
||||
$multiOutput = Join-Path $exampleBuild 'iec104_parameter_multi_master.exe'
|
||||
$multiSources = @(
|
||||
(Join-Path $projectRoot 'examples/iec104_parameter_multi_master.c'),
|
||||
(Join-Path $projectRoot '104/IEC104_TcpServer.c'),
|
||||
(Join-Path $projectRoot '104/IEC104_Net_Windows.c')) + $librarySources
|
||||
& $Compiler @compileFlags '-DIEC104_TCP_SERVER_CAPACITY=2' @multiSources -o $multiOutput -lws2_32
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Parameter multi-master example build failed' }
|
||||
& node (Join-Path $projectRoot 'test/parameter_multi_master_loopback_test.cjs') $multiOutput
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Parameter multi-master loopback test failed' }
|
||||
}
|
||||
Write-Output "Passed: $script:passedTests C test executables (suite: $Suite)"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
/* 模块职责:把已有非空负载封装成示例使用的 I104 工程包,供基线登记或210上传。
|
||||
* 调用方式:node scripts/make_upgrade_package.cjs --version N --input payload --output package。
|
||||
* 版本、长度和CRC按小端写入16字节头,CRC只覆盖负载;此格式不提供签名或机型验证。
|
||||
* 只创建新输出,不覆盖已有文件、不执行负载。写入失败可留下未完成包,不能继续上传。
|
||||
* 只依赖Node内置模块;固定块读写,不把整个映像放入内存。 */
|
||||
'use strict';
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const MAX_PACKAGE = 16 * 1024 * 1024;
|
||||
|
||||
/* 参数按键值对处理,缺项、重复键和未知键都在创建输出前拒绝。
|
||||
* 任一步抛出异常交末尾入口设置非零退出码;只有头、负载及fsync均成功才打印Created。 */
|
||||
function main() {
|
||||
const options = {};
|
||||
for (let i = 2; i < process.argv.length; i += 2) {
|
||||
const key = process.argv[i];
|
||||
if (!['--version', '--input', '--output'].includes(key) || options[key] !== undefined || !process.argv[i + 1])
|
||||
throw Error('Usage: node scripts/make_upgrade_package.cjs --version <1..4294967295> --input <payload> --output <new-package>');
|
||||
options[key] = process.argv[i + 1];
|
||||
}
|
||||
if (!options['--input'] || !options['--output'] || !/^[1-9][0-9]*$/.test(options['--version'] || ''))
|
||||
throw Error('Provide --version, --input and --output');
|
||||
/* 先限制十进制正整数文本,再检查uint32范围,避免NaN、负数或精度损失进入包头。
|
||||
* 该工具只验证字段范围;上传版本是否高于设备当前版本由接收端判断。 */
|
||||
const version = Number(options['--version']);
|
||||
if (!Number.isSafeInteger(version) || version > 0xffffffff) throw Error('Version must fit uint32');
|
||||
const input = path.resolve(options['--input']), output = path.resolve(options['--output']);
|
||||
if (input === output) throw Error('Input and output must differ');
|
||||
/* 源文件先只读打开;目的文件稍后用wx独占新建,已有输出即使路径写法不同也不覆盖。
|
||||
* 源句柄不锁定外部写入者,调用方应提供稳定负载;下方短读及末次尺寸检查仅作防御。 */
|
||||
const source = fs.openSync(input, 'r');
|
||||
let destination;
|
||||
try {
|
||||
const size = fs.fstatSync(source).size;
|
||||
if (!Number.isSafeInteger(size) || size < 1 || size > MAX_PACKAGE - 16) throw Error('Payload size outside example limits');
|
||||
// 整包上限包含16字节头;先从偏移16写负载,最后才填入完整有效头。
|
||||
// CRC直接累计本轮实际写出的块,避免先算一遍源文件、再读另一版本的数据。
|
||||
destination = fs.openSync(output, 'wx');
|
||||
const chunk = Buffer.alloc(4096), header = Buffer.alloc(16);
|
||||
let offset = 0, crc = 0xffffffff;
|
||||
while (offset < size) {
|
||||
const wanted = Math.min(chunk.length, size - offset);
|
||||
if (fs.readSync(source, chunk, 0, wanted, offset) !== wanted) throw Error('Payload changed or short read');
|
||||
for (let i = 0; i < wanted; ++i) {
|
||||
crc ^= chunk[i];
|
||||
for (let bit = 0; bit < 8; ++bit) crc = (crc >>> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
|
||||
}
|
||||
if (fs.writeSync(destination, chunk, 0, wanted, offset + 16) !== wanted) throw Error('Short package write');
|
||||
offset += wanted;
|
||||
}
|
||||
if (fs.fstatSync(source).size !== size) throw Error('Payload size changed');
|
||||
/* CRC使用反射多项式0xEDB88320、全1初值和最终取反;>>>0转成无符号32位值。
|
||||
* fsync发生在包头回填之后,打印成功前确保已请求同步整个输出文件。 */
|
||||
header.write('I104', 0, 'ascii'); header.writeUInt32LE(version, 4); header.writeUInt32LE(size, 8);
|
||||
header.writeUInt32LE((crc ^ 0xffffffff) >>> 0, 12);
|
||||
if (fs.writeSync(destination, header, 0, header.length, 0) !== header.length) throw Error('Short header write');
|
||||
fs.fsyncSync(destination);
|
||||
console.log(`Created ${output}: version=${version} payload=${size} crc=${header.readUInt32LE(12)}`);
|
||||
} finally {
|
||||
/* 正常与异常共用关闭路径,仅关闭已取得的句柄;不删除残留输出,也不尝试原地续写。 */
|
||||
if (destination !== undefined) fs.closeSync(destination);
|
||||
fs.closeSync(source);
|
||||
}
|
||||
}
|
||||
/* 命令行边界只打印原因并保留失败退出码;不把部分包当作成功产物。 */
|
||||
try { main(); } catch (error) { console.error(error.message); process.exitCode = 1; }
|
||||
Reference in New Issue
Block a user