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
Added support for gen_packets.
**** NOTE THAN THE MARK AND SPACE FREQUENCIES ARE BACKWARDS in gen_tone.c.

Lines 228-229 correctly set f1_change_per_sample to the MARK frequency and
f2_change_per_sample the space frequency.

Line 383, however, sends MARK on 0 and SPACE on 1:

tone_phase[chan] += dat ? f2_change_per_sample[chan] : f1_change_per_sample[chan];

As such, you have to generate packets like this:

	gen_packets -e R -m 1800 -s 1200 -o test.wav eotd.data

EOTD input to the program consists of lines of 8 byte packets in HCB+ATAD
format; i.e. the format that direwolf appends to the decoded packet.

Ex: 81 b0 32 fb 31 23 73 8f

A new option has been added to atest: -e type. That enables EOTD generation and
the 'type' signifies 'R'ear to front or 'F'ront to rear decoding.

Using "atest -B EOTD test.wav" the above packet decodes to:

DECODED[1] 0:00.123 EOTD audio level = 49(30/31)
[0] EOTD>APDW16:{DRREAR>FRONT:ts=2022-04-07T10:02:00.350,chain=ONLY,block=BASIC,devbat=OK,msgid=ONEWAY,unit_addr=18151,brake_status=GO(49 psig),disc_bits=f6,valve=OPERATIONAL,confirm=UPDATE,disc_bit_1=1,motion=STOPPED/NOT_MONITORED,light_batt=OK/NOT_MONITORED,light=ON,hex=81 b0 32 fb 31 23 73 8f
  • Loading branch information
dtiller committed Apr 7, 2022
commit 2ea1a1892f406320175ae7cb8404ce20bb9f1fab
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ list(APPEND gen_packets_SOURCES
dtmf.c
textcolor.c
dsp.c
eotd_send.c
)

add_executable(gen_packets
Expand Down
2 changes: 1 addition & 1 deletion src/bchapply.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ printf("data_len=%d, crc_len=%d\n", data_len, crc_len);
//
// THIS IS THE LSB-FIRST VERSION
//
fprintf(stderr, "Enter HCB+ATAD _WITH_ the parity bit intact.\n");
fprintf(stderr, "Enter HCB+ATAD _WITH_ the parity bit intact and XOR already applied.\n");
while (1) {
for (int i = 0; i < 8; i++) {
int temp;
Expand Down
102 changes: 102 additions & 0 deletions src/eotd_send.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include "direwolf.h"

#include <stdio.h>

#include "eotd_send.h"
#include "audio.h"
#include "gen_tone.h"
#include "eotd_defs.h"

#define EOTD_SILENCE_SAMPLES 1000
//#define EOTD_SEND_DEBUG

static int eotd_fs[] = {1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0};
static int hotd_fs[] = {1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1 };

void my_tone_gen_put_bit (int chan, int bit) {

#ifdef EOTD_SEND_DEBUG
printf("mytone bit %d\n", bit);
#endif
tone_gen_put_bit (chan, bit);
}

void my_gen_tone_put_sample (int chan, int a, int sam) {

#ifdef EOTD_SEND_DEBUG
printf("mysilence sample %d\n", sam);
#endif
gen_tone_put_sample (chan, a, sam);
}

void send_preamble(int chan, char type) {
int bit = 0;
int preamble_count;
int fs_count;
int *fs;

if (type == EOTD_TYPE_R2F) {
bit = 0;
preamble_count = 69;
fs_count = sizeof(eotd_fs) / sizeof(int);
fs = eotd_fs;
} else {
preamble_count = 456;
fs_count = sizeof(hotd_fs) / sizeof(int);
fs = hotd_fs;
}

for (int i = 0; i < preamble_count; i++) {
my_tone_gen_put_bit (chan, bit);
bit ^= 1;
}

#ifdef EOTD_SEND_DEBUG
printf("end-of-preamble\n");
#endif
// send FS
for (int i = 0; i < fs_count; i++) {
my_tone_gen_put_bit (chan, fs[i]);
}

#ifdef EOTD_SEND_DEBUG
printf("end-of-fs\n");
#endif
}

void send_silence(int chan) {
int a = ACHAN2ADEV(chan);

for (int i = 0; i < EOTD_SILENCE_SAMPLES; i++) {
my_gen_tone_put_sample (chan, a, 0);
}
}

int eotd_send_block (int chan, char *str, char type) {

unsigned int b[EOTD_LENGTH];

int status = sscanf(str, "%x %x %x %x %x %x %x %x", b, b+1, b+2, b+3, b+4, b+5, b+6, b+7);
if (status != EOTD_LENGTH) {
fprintf(stderr, "Error: expected 8, read %d", status);
return -1;
}

send_preamble(chan, type);

for (int i = 7; i >= 0; i--) {
int byte = b[i]; // Copy this non-destructively so we can repeat it later, per spec.
for (int j = 0; j < 8; j++) {
int bit = byte & 0x01;
byte >>= 1;
my_tone_gen_put_bit (chan, bit);
}
}

#ifdef EOTD_SEND_DEBUG
printf("end-of-data\n");
#endif
send_silence(chan);

return 0;
}
6 changes: 6 additions & 0 deletions src/eotd_send.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef __EOTD_SEND_H
#define __EOTD_SEND_H

int eotd_send_block (int chan, char *str, char type);

#endif
25 changes: 21 additions & 4 deletions src/gen_packets.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@
#include "morse.h"
#include "dtmf.h"
#include "fx25.h"
#include "eotd_send.h"
#include "eotd_defs.h"


/* Own random number generator so we can get */
Expand All @@ -97,6 +99,10 @@ static int audio_file_close (void);
static int g_add_noise = 0;
static float g_noise_level = 0;
static int g_morse_wpm = 0; /* Send morse code at this speed. */
static int g_eotd_type = 0;

static int byte_count; /* Number of data bytes written to file. */
/* Will be written to header when file is closed. */


static struct audio_s modem;
Expand All @@ -115,6 +121,11 @@ static void send_packet (char *str)

morse_send (0, str, g_morse_wpm, 100, 100);
}
else if (g_eotd_type > 0) {
for (c=0; c<modem.adev[0].num_channels; c++) {
eotd_send_block (c, str, g_eotd_type);
}
}
else {
pp = ax25_from_text (str, 1);
if (pp == NULL) {
Expand Down Expand Up @@ -227,7 +238,7 @@ int main(int argc, char **argv)

/* ':' following option character means arg is required. */

c = getopt_long(argc, argv, "gjJm:s:a:b:B:r:n:N:o:z:82M:X:",
c = getopt_long(argc, argv, "gjJm:s:a:b:B:r:n:N:o:z:82M:X:e:",
long_options, &option_index);
if (c == -1)
break;
Expand Down Expand Up @@ -462,6 +473,15 @@ int main(int argc, char **argv)
usage (argv);
break;

case 'e':
g_eotd_type = optarg[0];
if (g_eotd_type != EOTD_TYPE_F2R && g_eotd_type != EOTD_TYPE_R2F) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("EOTD type must be %c or %c\n", EOTD_TYPE_F2R, EOTD_TYPE_R2F);
exit(EXIT_FAILURE);
}
break;

default:

/* Should not be here. */
Expand Down Expand Up @@ -757,9 +777,6 @@ static FILE *out_fp = NULL;

static struct wav_header header;

static int byte_count; /* Number of data bytes written to file. */
/* Will be written to header when file is closed. */


static int audio_file_open (char *fname, struct audio_s *pa)
{
Expand Down
20 changes: 18 additions & 2 deletions src/hdlc_rec.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include "bch.h"
#include "eotd_defs.h"

//#define EOTD_DEBUG

//#define TEST 1 /* Define for unit testing. */

Expand Down Expand Up @@ -473,11 +474,20 @@ int is_eotd_valid(struct hdlc_state_s *H) {

// Note: bits are changed in-place.
int corrected = apply_bch(temp_bch, bits + 1);

// Put back in HCB+ATAD format
rotate_bits(bits + 1, temp_bits, crc_len);
memcpy(bits + 1, temp_bits, crc_len * sizeof(int));
bits_to_bytes(bits, H->frame_buf, 64);

// Put the XOR-ed bits back.
if (H->eotd_type == EOTD_TYPE_R2F) {
// The HCB needs to be XOR'ed with a special constant.
for (int i = 0; i < sizeof(r2f_mask); i++) {
H->frame_buf[i] ^= r2f_mask[i];
}
}

return corrected;
}

Expand Down Expand Up @@ -515,7 +525,8 @@ static void eotd_rec_bit (int chan, int subchan, int slice, int raw, int future_
H = &hdlc_state[chan][subchan][slice];

#ifdef EOTD_DEBUG
dw_printf("chan=%d subchan=%d slice=%d raw=%d\n", chan, subchan, slice, raw);
// dw_printf("chan=%d subchan=%d slice=%d raw=%d\n", chan, subchan, slice, raw);
dw_printf("%d ", raw);
#endif
//dw_printf ("slice %d = %d\n", slice, raw);

Expand Down Expand Up @@ -565,7 +576,12 @@ for (int ii=0; ii < EOTD_LENGTH; ii++) {dw_printf("%02x ", H->frame_buf[ii]); }
if (is_eotd_valid(H) >= 0) {
alevel_t alevel = demod_get_audio_level (chan, subchan);
multi_modem_process_rec_frame (chan, subchan, slice, H->frame_buf, H->frame_len, alevel, 0, 0);
}
} else {
#ifdef EOTD_DEBUG
print_bytes("BCH failed for packet (type byte appended) ", H->frame_buf, H->frame_len);
#endif

}

H->eotd_acc = 0;
H->eotd_gathering = 0;
Expand Down