Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 88 additions & 14 deletions backends/anytone/codeplug.c
Original file line number Diff line number Diff line change
Expand Up @@ -454,25 +454,78 @@ int at_channel_to_image(struct at_image *img, const struct at_channel *ch)
return 0;
}

/*
* Compare decoded channel fields explicitly.
* Structure padding and unmodeled image bytes are not compared.
*/
static int channel_fields_equal(const struct at_channel *a,
const struct at_channel *b)
{
return a->index == b->index
&& strcmp(a->name, b->name) == 0
&& a->rx_hz == b->rx_hz
&& a->tx_hz == b->tx_hz
&& a->mode == b->mode
&& a->power == b->power
&& a->bandwidth_wide == b->bandwidth_wide
&& a->color_code == b->color_code
&& a->timeslot == b->timeslot
&& a->contact_index == b->contact_index
&& a->radio_id_index == b->radio_id_index
&& a->scan_list_index == b->scan_list_index
&& a->rx_group_index == b->rx_group_index
&& a->rx_only == b->rx_only
&& a->admit == b->admit
&& a->rx_ctcss == b->rx_ctcss
&& a->tx_ctcss == b->tx_ctcss
&& a->rx_dcs == b->rx_dcs
&& a->tx_dcs == b->tx_dcs;
}

int at_codeplug_to_image(const struct at_codeplug *cp, struct at_image *img)
{
uint8_t *ch_bmp = at_image_find_mut(img, OFF_CH_BMP, CH_BMP_SIZE);
if (ch_bmp) {
memset(ch_bmp, 0, CH_BMP_SIZE);
uint8_t original_bitmap[CH_BMP_SIZE];

memcpy(original_bitmap, ch_bmp, CH_BMP_SIZE);
/* Clear valid channel bits, preserving bitmap padding. */
for (unsigned i = 0; i < AT_MAX_CHANNELS; i++)
at_bitmap_set(ch_bmp, i, 0);


for (int i = 0; i < cp->nchannels; i++) {
const struct at_channel *ch = &cp->channels[i];
if (ch->index < 0 || ch->index >= AT_MAX_CHANNELS)
continue;
at_bitmap_set(ch_bmp, (unsigned)ch->index, 1);
uint8_t *raw = at_image_find_mut(img, channel_addr((unsigned)ch->index), CH_SIZE);
if (raw)
if (raw) {
/*
* Preserve the complete original record when
* none of its decoded fields has changed.
*/
if (at_bitmap_get(original_bitmap,
(unsigned)ch->index)) {
struct at_channel existing;

decode_channel(&existing, raw, ch->index);

if (channel_fields_equal(&existing, ch))
continue;
}

encode_channel(raw, ch);
}
}
}

uint8_t *ct_bmp = at_image_find_mut(img, OFF_CONTACT_BMP, CONTACT_BMP_SIZE);
if (ct_bmp) {
memset(ct_bmp, 0xff, CONTACT_BMP_SIZE); /* inverted: all disabled */
/* Clear valid contact bits, preserving bitmap padding. */
for (unsigned i = 0; i < AT_MAX_CONTACTS; i++)
at_inv_bitmap_set(ct_bmp, i, 0);

for (int i = 0; i < cp->ncontacts; i++) {
const struct at_contact *c = &cp->contacts[i];
if (c->index < 0 || c->index >= AT_MAX_CONTACTS)
Expand Down Expand Up @@ -514,10 +567,7 @@ int at_codeplug_to_image(const struct at_codeplug *cp, struct at_image *img)
uint8_t *zb = at_image_find_mut(img, OFF_ZONE_B_CH, ZONE_AB_CH_BYTES);

memset(z_bmp, 0, ZONE_BMP_SIZE);
if (za)
memset(za, 0xff, ZONE_AB_CH_BYTES);
if (zb)
memset(zb, 0xff, ZONE_AB_CH_BYTES);
/* Preserve selection entries for inactive zones. */
for (int i = 0; i < cp->nzones; i++) {
const struct at_zone *z = &cp->zones[i];
if (z->index < 0 || z->index >= AT_MAX_ZONES)
Expand All @@ -533,16 +583,29 @@ int at_codeplug_to_image(const struct at_codeplug *cp, struct at_image *img)
memset(chans, 0xff, ZONE_CHAN_STRIDE);
for (int j = 0; j < z->nchannels && j < AT_ZONE_CHANS; j++)
at_put_u16_le(chans + j * 2, (uint16_t)z->channels[j]);
if (za && z->channel_a >= 0)
at_put_u16_le(za + (unsigned)z->index * 2, (uint16_t)z->channel_a);
if (zb && z->channel_b >= 0)
at_put_u16_le(zb + (unsigned)z->index * 2, (uint16_t)z->channel_b);
if (za)
at_put_u16_le(
za + (unsigned)z->index * 2,
z->channel_a >= 0
? (uint16_t)z->channel_a : UINT16_MAX);
if (zb)
at_put_u16_le(
zb + (unsigned)z->index * 2,
z->channel_b >= 0
? (uint16_t)z->channel_b : UINT16_MAX);
}
}

uint8_t *s_bmp = at_image_find_mut(img, OFF_SCAN_BMP, SCAN_BMP_SIZE);
if (s_bmp) {
memset(s_bmp, 0, SCAN_BMP_SIZE);
uint8_t original_bitmap[SCAN_BMP_SIZE];

memcpy(original_bitmap, s_bmp, SCAN_BMP_SIZE);

/* Clear valid scan-list bits without altering padding. */
for (unsigned i = 0; i < AT_MAX_SCANLISTS; i++)
at_bitmap_set(s_bmp, i, 0);

for (int i = 0; i < cp->nscan_lists; i++) {
const struct at_scan_list *s = &cp->scan_lists[i];
if (s->index < 0 || s->index >= AT_MAX_SCANLISTS)
Expand All @@ -551,9 +614,20 @@ int at_codeplug_to_image(const struct at_codeplug *cp, struct at_image *img)
uint8_t *raw = at_image_find_mut(img, scan_addr((unsigned)s->index), SCAN_SIZE);
if (!raw)
continue;
memset(raw, 0xff, SCAN_SIZE);
raw[0] = 0;
/*
* Initialize newly allocated records only.
* Preserve unmodeled settings in existing scan lists.
*/
if (!at_bitmap_get(original_bitmap,
(unsigned)s->index)) {
memset(raw, 0xff, SCAN_SIZE);
raw[0] = 0;
}

at_write_ascii(raw + 0x0f, 16, s->name);

/* Reset only the channel membership field. */
memset(raw + 0x20, 0xff, AT_SCAN_MEMBERS * 2);
for (int j = 0; j < s->nmembers && j < AT_SCAN_MEMBERS; j++)
at_put_u16_le(raw + 0x20 + j * 2, (uint16_t)s->members[j]);
}
Expand Down
132 changes: 132 additions & 0 deletions tests/anytone/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# AnyTone Lossless Round-Trip Regression

## Purpose

Verify that an unmodified AnyTone ATCP image survives
binary, structured, and JSON conversion without unintended
changes to its contents.

The regression was developed against CodeplugStudio 0.0.2,
upstream revision b229a85.

## Defects addressed

The original structured encoder reconstructs records and
initializes entire memory regions even when the corresponding
configuration has not changed.

The corrections address four observed problems:

1. Unchanged channel records are unnecessarily reconstructed,
discarding fields not represented in the structured model.
2. Channel and digital contact bitmap initialization overwrites
padding outside the valid record-index range.
3. Initialization of VFO A/B zone selection tables overwrites
entries belonging to inactive zones.
4. Scan-list encoding erases configuration fields not represented
in the structured model.

## Running the regression

Requirements:

- Standard Linux build environment
- C11 compiler
- GNU Make
- Perl
- pkg-config
- libusb development libraries

From the repository root:

./tests/anytone/run-roundtrip.sh

The runner builds the application, generates a synthetic ATCP
fixture, compiles the regression harness, and executes three
independent conversion paths.

Test A:

ATCP -> ATCP

Test B:

ATCP -> C structures -> ATCP

Test C:

ATCP -> C structures -> JSON -> C structures -> ATCP

All three resulting images must be byte-identical to the
original synthetic fixture.

A failed comparison produces a nonzero exit status.

## Synthetic fixture

The fixture generator constructs a deterministic, sparse
19,468-byte ATCP image containing seven memory segments.

No captured radio data, personal configuration, callsigns,
or subscriber databases are included.

The fixture is generated at runtime and removed with the
temporary regression output when the runner exits.

## Verified regression results

Synthetic fixture:

| Test | Original | Corrected |
| --- | ---: | ---: |
| Binary changed bytes | 0 | 0 |
| Structured changed bytes | 1006 | 0 |
| JSON changed bytes | 1006 | 0 |
| Changed memory segments | 5 | 0 |

Original implementation differences:

| Address | Changed bytes |
| --- | ---: |
| 0x00800000 | 3 |
| 0x01080000 | 1 |
| 0x024C1500 | 1 |
| 0x02500100 | 1000 |
| 0x02640000 | 1 |

The corrections were additionally verified against a private
D878UV2 acquisition containing 102 memory segments and
4,118,912 total bytes.

The original implementation introduced 7,436 unintended byte
changes during an unedited structured round trip. The corrected
implementation produced byte-identical output through all three
conversion paths.

The private acquisition is not distributed.

## Scope and limitations

This regression establishes no-edit preservation for the
tested fixtures only.

It does not establish:

- Lossless intentional channel editing.
- Correct initialization of newly allocated records.
- Correct record deletion and reference maintenance.
- Compatibility with every AnyTone model or firmware revision.
- Safe programming of physical radios.

Further validation is required for those operations.

## Hardware safety

All tests operate exclusively on local files.

The synthetic fixture is deliberately incomplete and is not
an operational radio codeplug.

NEVER upload the synthetic fixture to a physical radio.

No radio connection is required.
102 changes: 102 additions & 0 deletions tests/anytone/generate-fixture.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/usr/bin/perl

use strict;
use warnings;

my $path = shift or die "Output filename required\n";

my %segments;

# Synthetic channel bank.
my $bank = "\xff" x 16384;
my $channel = "\x00" x 64;

# Simplex frequency representation.
substr($channel, 0x00, 4) = pack('H8', '14550000');
substr($channel, 0x04, 4) = pack('H8', '14550000');

# Synthetic channel name.
substr($channel, 0x23, 16) =
pack('a16', 'TEST CHANNEL');

# Unmodeled field preservation marker.
substr($channel, 0x35, 1) = "\x01";

substr($bank, 0, 64) = $channel;

# Corresponding channel extension.
my $extension = "\x00" x 64;
substr($extension, 3, 1) = "\x01";
substr($bank, 8192, 64) = $extension;

$segments{0x00800000} = $bank;

# Channel bitmap with deliberate padding marker.
my $channel_bitmap = "\x00" x 512;

vec($channel_bitmap, 0, 1) = 1;
substr($channel_bitmap, 500, 1) = "\xa5";

$segments{0x024c1500} = $channel_bitmap;

# Inverted contact bitmap with padding marker.
my $contact_bitmap = "\xff" x 1264;

substr($contact_bitmap, 1250, 1) = "\x5a";

$segments{0x02640000} = $contact_bitmap;

# No active zones.
$segments{0x024c1300} = "\x00" x 32;

# Inactive VFO A/B zone selection tables.
$segments{0x02500100} = "\x00" x 1012;

# One active scan list.
my $scan_bitmap = "\x00" x 32;
vec($scan_bitmap, 0, 1) = 1;

$segments{0x024c1340} = $scan_bitmap;

# Synthetic scan-list record.
my $scan = "\xff" x 144;

substr($scan, 0x00, 1) = "\x00";
substr($scan, 0x01, 1) = "\x12";

substr($scan, 0x0f, 16) =
pack('a16', 'TEST SCAN');

substr($scan, 0x20, 2) = pack('v', 0);

$segments{0x01080000} = $scan;

# Serialize the synthetic ATCP container.
open my $fh, '>:raw', $path or die "$path: $!\n";

print {$fh} pack(
'a4 V a8 a8 C x3 V',
'ATCP',
1,
'D878UV2',
'TEST',
7,
scalar keys %segments
) or die "Header write failed\n";

for my $addr (sort {$a <=> $b} keys %segments) {
my $data = $segments{$addr};

print {$fh} pack('V2', $addr, length($data)),
$data or die "Segment write failed\n";

printf "Segment 0x%08X: %u bytes\n",
$addr, length($data);
}

close $fh or die "Close failed: $!\n";

printf "\nSynthetic segments generated: %u\n",
scalar keys %segments;

print "No original radio data used.\n";
Loading