Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature - add end-of-train and head-of-train message decoding #387

Open
wants to merge 40 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
709f229
Initial checkin for EOTD support.
dtiller Mar 18, 2022
c491655
More framework.
dtiller Mar 19, 2022
7bde86f
#ifdef'ed debugging.
dtiller Mar 19, 2022
507f04e
Added date tag for debugging.
dtiller Mar 20, 2022
3b17d45
#ifdef'ed a lot of stuff.
dtiller Mar 20, 2022
26ac27b
Added beginnings of BCH processing.
dtiller Mar 20, 2022
d898d1b
Added derived code to calc/apply BCH codes.
dtiller Mar 21, 2022
be0f9c4
debug and comment changes.
dtiller Mar 21, 2022
475f951
Put original program back for reference. Added fixed codes.
dtiller Mar 21, 2022
53d5b2b
checked return status and found that we _can_ use 'forward' packets (…
dtiller Mar 22, 2022
a707fc8
Cleaned up output.
dtiller Mar 22, 2022
5be9e44
Closer. Lots of ACCA data to chew on.
dtiller Mar 22, 2022
742dfb4
Last checkin before trying dev branch.
dtiller Mar 29, 2022
98b8949
Slight fixes before switching to uint64_t and MSB processing.
dtiller Mar 30, 2022
7d58e40
New branch for 64 bit processing. Probably not needed.
dtiller Mar 30, 2022
2cf23db
Default buffer was _way_ too small.
dtiller Apr 2, 2022
1fb18ed
changes for 64 bit uint. Not sure I like them.
dtiller Apr 2, 2022
07209b5
First fully-working BCH code.
dtiller Apr 4, 2022
fa49b6a
Starting to look like a real project.
dtiller Apr 4, 2022
7646fc5
Shortened timestamp.
dtiller Apr 4, 2022
9871ba8
Added f2r decoder.
dtiller Apr 5, 2022
72708ec
Added mS to time.
dtiller Apr 5, 2022
09d2d5f
Added ARM logic.
dtiller Apr 5, 2022
a12f9e0
Added option block processing.
dtiller Apr 5, 2022
b17d987
Merge branch 'wb2osz:master' into feat-EOTD-master
dtiller Apr 5, 2022
27c97e3
Debian needs stdint.h in more places.
dtiller Apr 6, 2022
16d102e
Cleanup and changes to fix DTMF message.
dtiller Apr 6, 2022
f20fcd0
Debian is _picky_. Fixed a few warnings.
dtiller Apr 6, 2022
971383a
Deleted unneeded file.
dtiller Apr 6, 2022
2ea1a18
Added support for gen_packets.
dtiller Apr 7, 2022
89bc82b
Moved unit_addr closer to the beginning so it's always in the same pl…
dtiller Apr 8, 2022
1d7a4b9
Changed FRONT>REAR to dir=f2r, etc for consistency.
dtiller Apr 8, 2022
c4ccbdc
Added msg re: BCH code and added protective #ifndefs.
dtiller Apr 9, 2022
52298c1
Made -e flag imply the right (wrong\!) mark and space freqs and baud …
dtiller Apr 9, 2022
67d5900
Removed unused variable.
dtiller Apr 9, 2022
2048747
Added XOR for t=3 packets, added more verbiage, removed variable-leng…
dtiller Apr 9, 2022
6c8cf2a
Added pkttest and bchapply to the CMake system.
dtiller Apr 9, 2022
52d3d84
Deleted spurious executable.
dtiller Apr 9, 2022
b17f6f0
Fixed CMake issue and missing inttypes.h
dtiller Apr 29, 2022
c8d8977
Fixed typo.
dtiller May 14, 2022
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
Prev Previous commit
Next Next commit
Closer. Lots of ACCA data to chew on.
  • Loading branch information
dtiller committed Mar 22, 2022
commit 5be9e44c5b9a835e60048b3ef5403c6e1843972b
76 changes: 65 additions & 11 deletions src/bch.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@
#include <string.h>
#include "bch.h"

int bch_init(bch_t *bch, int m, int length, int t) {
#undef DEBUG

int init_bch(bch_t *bch, int m, int length, int t) {

int p[21], n;

Expand Down Expand Up @@ -115,14 +117,20 @@ int bch_init(bch_t *bch, int m, int length, int t) {
else if (m == 18) p[7] = 1;
else if (m == 19) p[1] = p[5] = p[6] = 1;
else if (m == 20) p[3] = 1;
printf("p(x) = ");

n = 1;
#ifdef DEBUG
printf("p(x) = ");
#endif
for (int i = 0; i <= m; i++) {
n *= 2;
#ifdef DEBUG
printf("%1d", p[i]);
#endif
}
#ifdef DEBUG
printf("\n");
#endif
n = n / 2 - 1;
bch->n = n;
int ninf = (n + 1) / 2 - 1;
Expand Down Expand Up @@ -184,10 +192,12 @@ int bch_init(bch_t *bch, int m, int length, int t) {
cycle[1][0] = 1;
size[1] = 1;
jj = 1; /* cycle set index */
#ifdef DEBUG
if (bch->m > 9) {
printf("Computing cycle sets modulo %d\n", bch->n);
printf("(This may take some time)...\n");
}
#endif
do {
/* Generate the jj-th cycle set */
ii = 0;
Expand Down Expand Up @@ -247,11 +257,12 @@ int bch_init(bch_t *bch, int m, int length, int t) {

if (bch->k<0)
{
printf("Parameters invalid!\n");
return -4;
}

#ifdef DEBUG
printf("This is a (%d, %d, %d) binary BCH code\n", bch->length, bch->k, d);
#endif

/* Compute the generator polynomial */
bch->g = malloc((rdncy + 1) * sizeof(int));
Expand All @@ -266,12 +277,13 @@ int bch_init(bch_t *bch, int m, int length, int t) {
bch->g[jj] = bch->g[jj - 1];
bch->g[0] = bch->alpha_to[(bch->index_of[bch->g[0]] + zeros[ii]) % bch->n];
}
#ifdef DEBUG
printf("Generator polynomial:\ng(x) = ");
for (ii = 0; ii <= rdncy; ii++) {
printf("%d", bch->g[ii]);
}
printf("\n");

#endif
return 0;
}

Expand Down Expand Up @@ -303,8 +315,7 @@ void generate_bch(bch_t *bch, int *data, int *bb) {
}


int
apply_bch(bch_t *bch, int *recd)
int apply_bch(bch_t *bch, int *recd)
/*
* Simon Rockliff's implementation of Berlekamp's algorithm.
*
Expand Down Expand Up @@ -335,7 +346,9 @@ apply_bch(bch_t *bch, int *recd)
t2 = 2 * bch->t;

/* first form the syndromes */
#ifdef DEBUG
printf("S(x) = ");
#endif
for (i = 1; i <= t2; i++) {
s[i] = 0;
for (j = 0; j < bch->length; j++)
Expand All @@ -349,9 +362,13 @@ apply_bch(bch_t *bch, int *recd)
*/
/* convert syndrome from polynomial form to index form */
s[i] = bch->index_of[s[i]];
#ifdef DEBUG
printf("%3d ", s[i]);
#endif
}
#ifdef DEBUG
printf("\n");
#endif

if (syn_error) { /* if there are errors, try to correct them */
/*
Expand Down Expand Up @@ -451,12 +468,13 @@ apply_bch(bch_t *bch, int *recd)
for (i = 0; i <= l[u]; i++)
elp[u][i] = bch->index_of[elp[u][i]];

#ifdef DEBUG
printf("sigma(x) = ");
for (i = 0; i <= l[u]; i++)
printf("%3d ", elp[u][i]);
printf("\n");
printf("Roots: ");

#endif
/* Chien search: find roots of the error location polynomial */
for (i = 1; i <= l[u]; i++)
reg[i] = elp[u][i];
Expand All @@ -473,18 +491,24 @@ apply_bch(bch_t *bch, int *recd)
root[count] = i;
loc[count] = bch->n - i;
count++;
#ifdef DEBUG
printf("%3d ", bch->n - i);
#endif
}
}
#ifdef DEBUG
printf("\n");
#endif
if (count == l[u]) {
/* no. roots = degree of elp hence <= t errors */
for (i = 0; i < l[u]; i++)
recd[loc[i]] ^= 1;
return l[u];
}
else { /* elp has degree >t hence cannot solve */
#ifdef DEBUG
printf("Incomplete decoding: errors detected\n");
#endif
return -1;
}
} else {
Expand Down Expand Up @@ -521,15 +545,29 @@ void bits_to_bytes(int *bits, int *byte_dest, int num_bits) {

byte_dest[index] <<= 8 - (num_bits % 8);
}


void swap_format(int *bits, int cutoff, int num_bits) {
// Do it the easy way
int *temp = malloc(num_bits * sizeof(int));
for (int i = 0; i < num_bits; i++) {
if (i < cutoff) {
temp[num_bits - cutoff + i] = bits[i];
} else {
temp[i - cutoff] = bits[i];
}
}

memcpy(bits, temp, num_bits * sizeof(int));
}

void dump_bch(bch_t *bch) {
printf("m: %d length: %d t: %d n: %d k: %d\n", bch->m, bch->length, bch->t, bch->n, bch->k);
}

#undef MAIN
#undef TEST_BYTES_TO_BITS
#define TEST_APPLY

#define TEST_SWAP
#ifdef MAIN
int main()
{
int test[][8] = {
Expand All @@ -555,7 +593,7 @@ int main()
int temp[8];
bch_t bch;

bch_init(&bch, 6, 63, 3);
init_bch(&bch, 6, 63, 3);

for (int count = 0; count < sizeof(test) / sizeof(*test); count++) {
bytes_to_bits(test[count], bits, 63);
Expand Down Expand Up @@ -593,6 +631,21 @@ int main()
printf("\n");
#endif

#ifdef TEST_SWAP
printf("orig: ");
for (int i = 0; i < 63; i++) {
printf("%d ", bits[i]);
}
printf("\n");

swap_format(bits, 45, 63);

printf("rev: ");
for (int i = 0; i < 63; i++) {
printf("%d ", bits[i]);
}
printf("\n");
#endif
#ifdef TEST_APPLY
int recv[63];

Expand Down Expand Up @@ -635,3 +688,4 @@ int main()
#endif
}
}
#endif
10 changes: 9 additions & 1 deletion src/bch.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,16 @@ struct bch {

typedef struct bch bch_t;

int bch_init(bch_t *bch, int m, int length, int t);
int init_bch(bch_t *bch, int m, int length, int t);

void generate_bch(bch_t *bch, int *data, int *bb);

int apply_bch(bch_t *bch, int *recd);

void bytes_to_bits(int *bytes, int *bit_dest, int num_bits);

void bits_to_bytes(int *bits, int *byte_dest, int num_bits);

void swap_format(int *bits, int cutoff, int num_bits);

#endif
4 changes: 4 additions & 0 deletions src/mkpkts.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DATA_DIR=../build
cut -c22-41,54-56,59-61,64-66,69-71,74-76,79-81,84-86,89-90 $DATA_DIR/all.out >$DATA_DIR/all.cut
cut -d' ' -f5- $DATA_DIR/all.cut > $DATA_DIR/all.pkts
./pkttest < $DATA_DIR/all.pkts > $DATA_DIR/all.good
68 changes: 68 additions & 0 deletions src/pkttest.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <stdio.h>
#include "bch.h"

int main(int argc, char **argv) {
bch_t bch;
int bytes[8];
int bits[63];

init_bch(&bch, 6, 63, 3);
#ifdef ARGS
if (argc != 9) {
fprintf(stderr, "Expecting 8 arguments.\n");
return -1;
}

for (int i = 0; i < 8; i++) {
sscanf(argv[i + 1], "%x", bytes + i);
}
#else
while (1) {
for (int i = 0; i < 8; i++) {
int status = scanf("%x ", bytes + i);
if (status == EOF) {
return 0;
}

if (status != 1) {
fprintf(stderr, "Error: %d", status);
}
}
#endif
// UNEEDED
// swap_format(bits, 45, 63);
bytes_to_bits(bytes, bits, 63);
int corrected = apply_bch(&bch, bits);
if (corrected >= 0) {
#ifdef DEBUG
printf("%d corrected\n", corrected);
for (int i = 0; i < 8; i++) {
printf("%02x ", bytes[i]);
}
printf("\n");
#endif
bits_to_bytes(bits, bytes, 63);
for (int i = 0; i < 8; i++) {
printf("%02x ", bytes[i]);
}
// Slice packet

printf("chain=%1x,",(bytes[0] >> 6) & 0x03);
printf("devst=%1x,",(bytes[0] >> 4) & 0x03);
printf("msgid=%1x,",(bytes[0] >> 1) & 0x07);
printf("uaddr=%03x,",((bytes[0] & 0x01) << 16) | (bytes[1] << 8) | (bytes[2]));
printf("bpres=%d,",(bytes[3] >> 1) & 0x07f);
printf("dbit1=%02x,",((bytes[3] & 0x01) << 7) | ((bytes[4] >> 1) & 0x7f));
printf("confm=%x,",(bytes[5] >> 7) & 0x01);
printf("dbit2=%x,",(bytes[5] >> 6) & 0x01);
printf("motdt=%x,",(bytes[5] >> 5) & 0x01);
printf("ltbat=%x,",(bytes[5] >> 4) & 0x01);
printf("ltsta=%x",(bytes[5] >> 3) & 0x01);
printf("\n");
}
#ifndef ARGS
}
#endif

return 0;
}