From 41482e904c7859881eeed6f882609ed56bc6e62d Mon Sep 17 00:00:00 2001 From: imag0r Date: Sun, 29 Nov 2026 09:49:31 -0800 Subject: [PATCH] fix(uv5rmini): preserve transmit lockout on write and readback - Encode Forbid TX as a blank transmit frequency and restore it on readback. - Keep receive-only CSV channels in write preflight and cover the round trip with a simulated clone. --- CHANGELOG.md | 2 + src/components/channels/ChannelEditModal.tsx | 1 + src/radios/uv5rmini/capabilities.ts | 2 + src/radios/uv5rmini/channelFormat.ts | 8 +- src/radios/uv5rmini/channelMapping.ts | 27 +++-- src/services/validation/channelProblems.ts | 7 +- src/services/validation/channelValidator.ts | 11 +-- src/services/validation/frequencyValidator.ts | 29 +++--- src/services/validation/writeFilter.ts | 10 +- src/services/writeFilterOptions.ts | 1 + src/types/radioCapabilities.ts | 2 + tests/fixtures/uv5rmini/receive-only.csv | 26 +++++ tests/unit/uv5rMiniProtocol.test.ts | 99 +++++++++++++++++++ 13 files changed, 171 insertions(+), 54 deletions(-) create mode 100644 tests/fixtures/uv5rmini/receive-only.csv diff --git a/CHANGELOG.md b/CHANGELOG.md index 956fd304..e0ad32a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,8 @@ version says how old a build is, not how big its changes are. Whether a saved ## [Unreleased] +- UV5R-Mini: Forbid TX is preserved when reading and writing channels, including CSV imports with TX frequency set to zero. + ## [2026.11.0] — 2026-11-27 - Channel wizard: new Marine (Australia) preset, the Australian VHF marine voice channels (#209). diff --git a/src/components/channels/ChannelEditModal.tsx b/src/components/channels/ChannelEditModal.tsx index cc5fc566..9d3e0808 100644 --- a/src/components/channels/ChannelEditModal.tsx +++ b/src/components/channels/ChannelEditModal.tsx @@ -98,6 +98,7 @@ export const ChannelEditModal: React.FC = ({ ...validateChannel(editedChannel, bandLimits, maxChannels, { maxNameLength, blankTxAnyBand: caps?.blankTxAnyBand, + forbidTxBlanksFrequency: caps?.forbidTxBlanksFrequency, checkRxBand, }), ]; diff --git a/src/radios/uv5rmini/capabilities.ts b/src/radios/uv5rmini/capabilities.ts index f1fc6e46..5cdd72e8 100644 --- a/src/radios/uv5rmini/capabilities.ts +++ b/src/radios/uv5rmini/capabilities.ts @@ -45,6 +45,8 @@ const UV5RMINI_MEMORY_REGIONS: MemoryRegionSpec[] = [ export const UV5RMINI_CAPABILITIES: RadioCapabilities = { memoryRegions: UV5RMINI_MEMORY_REGIONS, bandLimits: DEFAULT_BAND_LIMITS, + blankTxAnyBand: true, + forbidTxBlanksFrequency: true, // Its settings write lays every mapped field over the radio's own block. writesSettingsOnEveryWrite: true, writeValidations: { diff --git a/src/radios/uv5rmini/channelFormat.ts b/src/radios/uv5rmini/channelFormat.ts index 0ef2fd8c..b71047a3 100644 --- a/src/radios/uv5rmini/channelFormat.ts +++ b/src/radios/uv5rmini/channelFormat.ts @@ -153,12 +153,8 @@ export function writeChannelToImage( } out.set(raw.rawBytes); out.set(encodeBcdFreq(raw.rxFreqHz), 0); - if (raw.duplex === 'off') { - for (let i = 4; i < 8; i++) out[i] = 0xff; - } else { - const txHz = raw.txFreqHz || raw.rxFreqHz; - out.set(encodeBcdFreq(txHz), 4); - } + if (raw.duplex === 'off' || raw.txFreqHz === 0) out.fill(0xff, 4, 8); + else out.set(encodeBcdFreq(raw.txFreqHz), 4); out.set(encodeTone(raw.rxtone), 8); out.set(encodeTone(raw.txtone), 10); const nameStr = (raw.name || '').trim().slice(0, 12); diff --git a/src/radios/uv5rmini/channelMapping.ts b/src/radios/uv5rmini/channelMapping.ts index cb69dbbd..b0227b94 100644 --- a/src/radios/uv5rmini/channelMapping.ts +++ b/src/radios/uv5rmini/channelMapping.ts @@ -3,6 +3,7 @@ */ import type { Channel, CTCSSDCS } from '../../models'; +import { isNoTxFrequency, NO_TX_FREQUENCY } from '../../services/validation/frequencyValidator'; import type { Uv5rMiniChannelRaw } from './channelFormat'; import { encodeBcdFreq, encodeTone } from './channelFormat'; @@ -32,12 +33,6 @@ function ctcssDcsToStr(c: CTCSSDCS): string { /** Convert NeonPlug Channel to UV5R-Mini raw shape (for write). */ export function channelToUv5rMiniRaw(ch: Channel): Uv5rMiniChannelRaw { const rxHz = Math.round((ch.rxFrequency || 0) * 1e6); - const txHz = Math.round((ch.txFrequency ?? ch.rxFrequency ?? 0) * 1e6); - let duplex = ''; - if (rxHz > 0) { - if (txHz === 0 || txHz === rxHz) duplex = ''; - else duplex = txHz > rxHz ? '+' : '-'; - } const rawBytes = new Uint8Array(32); if (rxHz === 0) { rawBytes.fill(0xff); @@ -55,13 +50,13 @@ export function channelToUv5rMiniRaw(ch: Channel): Uv5rMiniChannelRaw { rawBytes, }; } - rawBytes.fill(0); + const txHz = ch.forbidTx || isNoTxFrequency(ch.txFrequency) + ? 0 + : Math.round((ch.txFrequency ?? 0) * 1e6); + const duplex = txHz === 0 ? 'off' : txHz === rxHz ? '' : txHz > rxHz ? '+' : '-'; rawBytes.set(encodeBcdFreq(rxHz), 0); - if (duplex !== 'off') { - rawBytes.set(encodeBcdFreq(txHz), 4); - } else { - rawBytes[4] = rawBytes[5] = rawBytes[6] = rawBytes[7] = 0xff; - } + if (duplex === 'off') rawBytes.fill(0xff, 4, 8); + else rawBytes.set(encodeBcdFreq(txHz), 4); rawBytes.set(encodeTone(ctcssDcsToStr(ch.rxCtcssDcs ?? DEFAULT_CTCSSDCS)), 8); rawBytes.set(encodeTone(ctcssDcsToStr(ch.txCtcssDcs ?? DEFAULT_CTCSSDCS)), 10); rawBytes[12] = 1; @@ -79,7 +74,7 @@ export function channelToUv5rMiniRaw(ch: Channel): Uv5rMiniChannelRaw { num: ch.number, empty: false, rxFreqHz: rxHz, - txFreqHz: duplex === 'off' ? 0 : txHz, + txFreqHz: txHz, duplex, rxtone: ctcssDcsToStr(ch.rxCtcssDcs ?? DEFAULT_CTCSSDCS), txtone: ctcssDcsToStr(ch.txCtcssDcs ?? DEFAULT_CTCSSDCS), @@ -93,14 +88,14 @@ export function channelToUv5rMiniRaw(ch: Channel): Uv5rMiniChannelRaw { /** Convert UV5R-Mini raw channel to NeonPlug Channel. */ export function uv5rMiniRawToChannel(raw: Uv5rMiniChannelRaw): Channel { const rxMhz = raw.rxFreqHz / 1e6; - const txMhz = raw.txFreqHz > 0 ? raw.txFreqHz / 1e6 : rxMhz; + const forbidTx = raw.duplex === 'off' || raw.txFreqHz === 0; return { number: raw.num, name: raw.name || '—', rxFrequency: rxMhz, - txFrequency: txMhz, + txFrequency: forbidTx ? NO_TX_FREQUENCY : raw.txFreqHz / 1e6, mode: 'Analog', - forbidTx: false, + forbidTx, loneWorker: false, bandwidth: raw.mode === 'FM' ? '25kHz' : '12.5kHz', scanAdd: false, diff --git a/src/services/validation/channelProblems.ts b/src/services/validation/channelProblems.ts index 1725787a..d069e4b8 100644 --- a/src/services/validation/channelProblems.ts +++ b/src/services/validation/channelProblems.ts @@ -8,7 +8,7 @@ */ import type { Channel } from '../../models/Channel'; -import { isNoTxFrequency, isRxInNoTxBand, isValidFrequencyRange } from './frequencyValidator'; +import { isChannelTxOmitted, isNoTxFrequency, isValidFrequencyRange } from './frequencyValidator'; import { isChannelWritable, type WriteFilterOptions } from './writeFilter'; export interface ChannelWriteRule extends WriteFilterOptions { @@ -40,10 +40,7 @@ export function channelProblems(channel: Channel, rule: ChannelWriteRule): Chann : rule.outOfBand ? `Too high to store, ${LEFT_OUT}` : `Outside this radio's bands, ${LEFT_OUT}`; - const blankTx = - rule.outOfBand || rule.blankTxAnyBand - ? isNoTxFrequency(channel.txFrequency) - : isRxInNoTxBand(channel.rxFrequency) && channel.forbidTx && isNoTxFrequency(channel.txFrequency); + const blankTx = isChannelTxOmitted(channel, rule) || (rule.outOfBand && isNoTxFrequency(channel.txFrequency)); if (bad(channel.rxFrequency)) problems.rx = why(channel.rxFrequency); if (!blankTx && bad(channel.txFrequency)) problems.tx = why(channel.txFrequency); diff --git a/src/services/validation/channelValidator.ts b/src/services/validation/channelValidator.ts index 199a42bf..ed848228 100644 --- a/src/services/validation/channelValidator.ts +++ b/src/services/validation/channelValidator.ts @@ -1,6 +1,6 @@ import type { Channel } from '../../models/Channel'; import type { RadioBandLimits } from '../../types/radioCapabilities'; -import { isNoTxFrequency, isRxInNoTxBand, isValidFrequencyRange } from './frequencyValidator'; +import { isChannelTxOmitted, isValidFrequencyRange, type ChannelTxOptions } from './frequencyValidator'; import { isValidColorCode, isValidTimeSlot } from './dmrValidator'; export interface ValidationError { @@ -14,11 +14,9 @@ const DEFAULT_MAX_CHANNELS = 4000; /** Default longest name when capabilities don't specify: the DM-32's 16. */ const DEFAULT_MAX_NAME_LENGTH = 16; -export interface ChannelValidationOptions { +export interface ChannelValidationOptions extends ChannelTxOptions { /** capabilities.maxChannelNameLength */ maxNameLength?: number; - /** capabilities.blankTxAnyBand: a blank TX is allowed whatever the RX. */ - blankTxAnyBand?: boolean; /** False where a write keeps a channel whatever its RX (the DA-7X2's planner checks TX only). */ checkRxBand?: boolean; } @@ -48,10 +46,7 @@ export function validateChannel( if (channel.rxFrequency <= 0) { errors.push({ field: 'rxFrequency', message: 'RX frequency must be greater than 0' }); } - // The same blank-TX rule as isValidChannelFrequency. - const blankTx = options.blankTxAnyBand - ? isNoTxFrequency(channel.txFrequency) - : isRxInNoTxBand(channel.rxFrequency) && channel.forbidTx && isNoTxFrequency(channel.txFrequency); + const blankTx = isChannelTxOmitted(channel, options); if (!blankTx && channel.txFrequency <= 0) { errors.push({ field: 'txFrequency', message: 'TX frequency must be greater than 0' }); } diff --git a/src/services/validation/frequencyValidator.ts b/src/services/validation/frequencyValidator.ts index a269f159..f0ebc885 100644 --- a/src/services/validation/frequencyValidator.ts +++ b/src/services/validation/frequencyValidator.ts @@ -1,15 +1,15 @@ import type { Channel } from '../../models/Channel'; -import type { RadioBandLimits } from '../../types/radioCapabilities'; +import type { RadioBandLimits, RadioCapabilities } from '../../types/radioCapabilities'; import { DEFAULT_BAND_LIMITS } from '../../types/radioCapabilities'; /** RX range where TX is not used (aviation/FM receive-only). TX bytes stored as 0xFF on radio. */ export const NO_TX_BAND_RX_MIN_MHZ = 87; export const NO_TX_BAND_RX_MAX_MHZ = 136; -/** Display value for "no TX" (0xFF on radio). Shown when RX is in 87–136 MHz and Forbid TX. */ +/** Display value for "no TX" (0xFF on radio). */ export const NO_TX_FREQUENCY = 1666.666; -/** True if txFrequency is the sentinel for "no TX" (receive-only in 87–136 band). */ +/** True if txFrequency is the sentinel for "no TX". */ export function isNoTxFrequency(txFrequency: number): boolean { return txFrequency >= 1666 && txFrequency < 1667; } @@ -40,22 +40,26 @@ export function hasBlankTx(channel: Pick return isNoTxFrequency(channel.txFrequency) && (blankTxAnyBand === true || isRxInNoTxBand(channel.rxFrequency)); } +export type ChannelTxOptions = Pick; + +/** Whether the radio stores no TX frequency, so only RX needs a band check. */ +export function isChannelTxOmitted(channel: Channel, options?: ChannelTxOptions): boolean { + return (options?.forbidTxBlanksFrequency === true && channel.forbidTx) || + ((options?.blankTxAnyBand === true || channel.forbidTx) && hasBlankTx(channel, options?.blankTxAnyBand)); +} + /** * Check if a channel's frequencies are within supported ranges. * When limits is provided (e.g. from getCapabilitiesForModel(radioInfo?.model)?.bandLimits), uses those. - * A channel with a blank TX only has its RX checked: in 87–136 MHz with Forbid TX on any radio, - * and in any band, Forbid TX or not, on a radio with `blankTxAnyBand`. + * When the radio stores no TX frequency, only RX is checked. */ export function isValidChannelFrequency( channel: Channel, limits?: RadioBandLimits | null, - options?: { blankTxAnyBand?: boolean } + options?: ChannelTxOptions ): boolean { if (channel.rxFrequency <= 0) return false; - const blankTx = options?.blankTxAnyBand - ? isNoTxFrequency(channel.txFrequency) - : isRxInNoTxBand(channel.rxFrequency) && channel.forbidTx && isNoTxFrequency(channel.txFrequency); - if (blankTx) { + if (isChannelTxOmitted(channel, options)) { return isValidFrequencyRange(channel.rxFrequency, limits); } if (channel.txFrequency <= 0) return false; @@ -72,11 +76,12 @@ export function isValidChannelFrequency( export function isWritableChannelFrequency( channel: Channel, limits: RadioBandLimits | null | undefined, - options: { blankTxAnyBand?: boolean; outOfBand?: boolean } + options: ChannelTxOptions & { outOfBand?: boolean } ): boolean { if (!options.outOfBand) return isValidChannelFrequency(channel, limits, options); const storable = (mhz: number) => mhz > 0 && mhz < 1000; - return storable(channel.rxFrequency) && (isNoTxFrequency(channel.txFrequency) || storable(channel.txFrequency)); + return storable(channel.rxFrequency) && + (isNoTxFrequency(channel.txFrequency) || isChannelTxOmitted(channel, options) || storable(channel.txFrequency)); } /** Band limits from radio capabilities (e.g. getCapabilitiesForModel(radioInfo?.model)?.bandLimits). */ diff --git a/src/services/validation/writeFilter.ts b/src/services/validation/writeFilter.ts index 0e335582..a91c33f8 100644 --- a/src/services/validation/writeFilter.ts +++ b/src/services/validation/writeFilter.ts @@ -13,16 +13,15 @@ import type { Channel } from '../../models/Channel'; import type { Zone } from '../../models/Zone'; import type { ScanList } from '../../models/ScanList'; import type { RadioBandLimits } from '../../types/radioCapabilities'; -import { isWritableChannelFrequency } from './frequencyValidator'; +import { isWritableChannelFrequency, type ChannelTxOptions } from './frequencyValidator'; -export interface WriteFilterOptions { +export interface WriteFilterOptions extends ChannelTxOptions { /** * False on the DA-7X2, whose write planner checks channel frequencies itself * and which sends every scan list by its slot, empty ones included. */ filterBand: boolean; bandLimits?: RadioBandLimits | null; - blankTxAnyBand?: boolean; outOfBand?: boolean; } @@ -42,10 +41,7 @@ export interface WritableCodeplug { export function isChannelWritable(channel: Channel, options: WriteFilterOptions): boolean { return ( !options.filterBand || - isWritableChannelFrequency(channel, options.bandLimits, { - blankTxAnyBand: options.blankTxAnyBand, - outOfBand: options.outOfBand, - }) + isWritableChannelFrequency(channel, options.bandLimits, options) ); } diff --git a/src/services/writeFilterOptions.ts b/src/services/writeFilterOptions.ts index c4a602c8..da0deeb3 100644 --- a/src/services/writeFilterOptions.ts +++ b/src/services/writeFilterOptions.ts @@ -35,6 +35,7 @@ export function writeFilterOptionsFor( !(model != null && (D890_MODEL_IDS as readonly string[]).includes(model)) && !caps?.writeKeepsEveryBand, bandLimits: caps?.bandLimits, blankTxAnyBand: caps?.blankTxAnyBand, + forbidTxBlanksFrequency: caps?.forbidTxBlanksFrequency, outOfBand, }; } diff --git a/src/types/radioCapabilities.ts b/src/types/radioCapabilities.ts index 0ddbc2b8..09136d36 100644 --- a/src/types/radioCapabilities.ts +++ b/src/types/radioCapabilities.ts @@ -187,6 +187,8 @@ export interface RadioCapabilities { * TX may have a blank TX, and a write leaves any other out. */ blankTxAnyBand?: boolean; + /** Forbid TX blanks the stored TX frequency rather than setting a separate flag. */ + forbidTxBlanksFrequency?: boolean; /** * The write never drops a channel for being out of band. * diff --git a/tests/fixtures/uv5rmini/receive-only.csv b/tests/fixtures/uv5rmini/receive-only.csv new file mode 100644 index 00000000..1622d9c6 --- /dev/null +++ b/tests/fixtures/uv5rmini/receive-only.csv @@ -0,0 +1,26 @@ +Channel Number,Name,RX Frequency,TX Frequency,Forbid TX +14,ISS,145.800000,0.000000,Yes +15,APRS,144.390000,0.000000,Yes +16,WX400,162.400000,0.000000,Yes +17,WX425,162.425000,0.000000,Yes +18,WXOC,162.450000,0.000000,Yes +19,WX475,162.475000,0.000000,Yes +20,WX500,162.500000,0.000000,Yes +21,WX525,162.525000,0.000000,Yes +22,WXLA,162.550000,0.000000,Yes +23,LAXATIS,133.800000,0.000000,Yes +24,LAXDEP,135.650000,0.000000,Yes +25,LAXTWS,120.950000,0.000000,Yes +26,LAXTWN,133.900000,0.000000,Yes +27,LAXGND,121.650000,0.000000,Yes +28,SOCAL,124.300000,0.000000,Yes +29,MAR16,156.800000,0.000000,Yes +30,MAR13,156.650000,0.000000,Yes +31,MAR14,156.700000,0.000000,Yes +32,MAR77,156.875000,0.000000,Yes +33,MAR73,156.675000,0.000000,Yes +34,MAR09,156.450000,0.000000,Yes +35,AMTRAK,160.455000,0.000000,Yes +36,ALAMEDA,160.365000,0.000000,Yes +37,BNSF,160.590000,0.000000,Yes +38,UPROAD,160.320000,0.000000,Yes diff --git a/tests/unit/uv5rMiniProtocol.test.ts b/tests/unit/uv5rMiniProtocol.test.ts index a9db035b..bc2ab146 100644 --- a/tests/unit/uv5rMiniProtocol.test.ts +++ b/tests/unit/uv5rMiniProtocol.test.ts @@ -1,5 +1,19 @@ import { describe, it, expect } from 'vitest'; import { UV5RMiniProtocol } from '../../src/radios/uv5rmini/protocol'; +import { + parseChannelsFromImage, + writeChannelToImage, +} from '../../src/radios/uv5rmini/channelFormat'; +import { channelToUv5rMiniRaw, uv5rMiniRawToChannel } from '../../src/radios/uv5rmini/channelMapping'; +import { BAOFENG_BLOCK_SIZE, BAOFENG_CHANNEL_SIZE, BAOFENG_MEM_SIZES } from '../../src/radios/uv5rmini/constants'; +import { UV5RMINI_CAPABILITIES } from '../../src/radios/uv5rmini/capabilities'; +import { importChannelsFromCSV } from '../../src/services/csv/csvImporter'; +import { validateChannel } from '../../src/services/validation/channelValidator'; +import { NO_TX_FREQUENCY } from '../../src/services/validation/frequencyValidator'; +import { planWritableCodeplug } from '../../src/services/validation/writeFilter'; +import { writeFilterOptionsFor } from '../../src/services/writeFilterOptions'; +import { createDefaultChannel } from '../../src/utils/channelHelpers'; +import receiveOnlyCsv from '../fixtures/uv5rmini/receive-only.csv?raw'; import type { Channel } from '../../src/models'; // writeChannels fills a fresh image with 0xff (the radio's empty-channel marker) @@ -27,3 +41,88 @@ describe('UV5RMiniProtocol write guards', () => { await expect(proto.writeChannels([outOfRange])).rejects.toThrow(/Refusing to write/); }); }); + +const TX_OFF = [0xff, 0xff, 0xff, 0xff]; + +describe('UV5R-Mini channel TX lockout', () => { + it.each([ + { rxFrequency: 162.55, txFrequency: 162.55, forbidTx: true }, + { rxFrequency: 156.8, txFrequency: 0, forbidTx: true }, + { rxFrequency: 156.8, txFrequency: 0, forbidTx: false }, + { rxFrequency: 156.8, txFrequency: NO_TX_FREQUENCY, forbidTx: false }, + ])('encodes disabled TX for %o', (frequencies) => { + const channel = createDefaultChannel({ number: 1, name: 'RX only', ...frequencies }); + const raw = channelToUv5rMiniRaw(channel); + expect(raw).toMatchObject({ duplex: 'off', txFreqHz: 0 }); + expect([...raw.rawBytes.subarray(4, 8)]).toEqual(TX_OFF); + + const image = new Uint8Array(BAOFENG_CHANNEL_SIZE); + writeChannelToImage(image, 0, raw); + expect([...image.subarray(4, 8)]).toEqual(TX_OFF); + const [parsed] = parseChannelsFromImage(image); + expect(parsed).toMatchObject({ duplex: 'off', txFreqHz: 0 }); + expect(uv5rMiniRawToChannel(parsed)).toMatchObject({ + rxFrequency: channel.rxFrequency, txFrequency: NO_TX_FREQUENCY, forbidTx: true, + }); + }); + + it('reads a legacy all-zero TX field as locked out and rewrites it as all-FF', () => { + const channel = createDefaultChannel({ number: 1, rxFrequency: 156.8 }); + const image = channelToUv5rMiniRaw(channel).rawBytes; + image.fill(0, 4, 8); + const [parsed] = parseChannelsFromImage(image); + const readback = uv5rMiniRawToChannel(parsed); + expect(readback).toMatchObject({ rxFrequency: 156.8, txFrequency: NO_TX_FREQUENCY, forbidTx: true }); + writeChannelToImage(image, 0, channelToUv5rMiniRaw(readback)); + expect([...image.subarray(4, 8)]).toEqual(TX_OFF); + }); + + it.each([ + { rxFrequency: 146.52, txFrequency: 146.52, duplex: '', txCtcssDcs: { type: 'None' } }, + { rxFrequency: 145.32, txFrequency: 144.72, duplex: '-', txCtcssDcs: { type: 'CTCSS', value: 114.8 } }, + ] as const)('keeps simplex and repeater TX enabled for %o', ({ duplex, ...frequencies }) => { + const channel = createDefaultChannel({ number: 1, forbidTx: false, ...frequencies }); + const image = new Uint8Array(BAOFENG_CHANNEL_SIZE); + writeChannelToImage(image, 0, channelToUv5rMiniRaw(channel)); + const [parsed] = parseChannelsFromImage(image); + expect(parsed.duplex).toBe(duplex); + expect(uv5rMiniRawToChannel(parsed)).toMatchObject({ ...frequencies, forbidTx: false }); + }); + + it('carries all 25 receive-only CSV channels through write preflight and protocol readback', async () => { + const { channels = [], errors } = importChannelsFromCSV(receiveOnlyCsv); + expect(errors).toBeUndefined(); + expect(channels).toHaveLength(25); + const options = writeFilterOptionsFor('UV5R-Mini', UV5RMINI_CAPABILITIES, false); + const [first] = channels; + const missingTx = { ...first, number: 100, forbidTx: false }; + const outsideRx = { ...first, number: 101, rxFrequency: 300 }; + const plan = planWritableCodeplug([...channels, missingTx, outsideRx], [], [], options); + expect(plan.droppedChannels).toEqual([missingTx, outsideRx]); + expect(validateChannel(first, options.bandLimits, 999, options)).toEqual([]); + + const image = new Uint8Array(BAOFENG_MEM_SIZES[0]).fill(0xff); + const erasedBlock = new Uint8Array(BAOFENG_BLOCK_SIZE).fill(0xff); + const writer = Object.assign(new UV5RMiniProtocol(), { + connection: { + handshakeUpload: async () => {}, + writeBlock: async (addr: number, block: Uint8Array) => { image.set(block, addr); }, + }, + }); + await writer.writeChannels(plan.channels); + for (const channel of channels) { + const offset = (channel.number - 1) * BAOFENG_CHANNEL_SIZE; + expect([...image.subarray(offset + 4, offset + 8)]).toEqual(TX_OFF); + } + + const reader = Object.assign(new UV5RMiniProtocol(), { + connection: { + readBlock: async (addr: number) => + addr < image.length ? image.subarray(addr, addr + BAOFENG_BLOCK_SIZE) : erasedBlock, + }, + }); + expect(await reader.readChannels()).toMatchObject(channels.map(({ number, name, rxFrequency }) => ({ + number, name, rxFrequency, txFrequency: NO_TX_FREQUENCY, forbidTx: true, + }))); + }); +});