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
changes for 64 bit uint. Not sure I like them.
  • Loading branch information
dtiller committed Apr 2, 2022
commit 1fb18ed5423609a9491992f46f3be542e9db7341
244 changes: 239 additions & 5 deletions src/eotd.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,13 @@
#include <time.h>

#include "textcolor.h"
#include "eotd_defs.h"
#include "eotd.h"

#define EOTD_RAW
#define EOTD_TIMESTAMP
#define EOTD_APPEND_HEX

/*-------------------------------------------------------------------
*
* Convert EOTD binary block (from HDLC frame) to text.
Expand All @@ -48,14 +53,243 @@
*
*--------------------------------------------------------------------*/

void add_comma(char *text, int text_size) {
strlcat(text, ",", text_size);
}

void get_chain(uint64_t pkt, char *text, int text_size) {
uint32_t val;

val = pkt & 0x03ULL;

strlcat(text, "chain=", text_size);

strlcat(text, val & 0x02 ? "FIRST+" : "NOT_FIRST+", text_size);
strlcat(text, val & 0x01 ? "LAST" : "NOT_LAST", text_size);
}

void get_dev_batt_stat(uint64_t pkt, char *text, int text_size) {
uint32_t val;

pkt >>= 2;
val = pkt & 0x03ULL;

strlcat(text, "devbat=", text_size);

switch(val) {
case 3:
strlcat(text, "OK", text_size);
break;
case 2:
strlcat(text, "WEAK", text_size);
break;
case 1:
strlcat(text, "VERY_WEAK", text_size);
break;
case 0:
strlcat(text, "NOT_MONITORED", text_size);
break;
}
}

void get_msg_id_type(uint64_t pkt, char *text, int text_size) {
uint32_t val;
char temp[32];

pkt >>= 4;
val = pkt & 0x07ULL;

strlcat(text, "msgid=", text_size);

switch(val) {
case 0:
strlcat(text, "ONEWAY", text_size);
break;

default:
sprintf(temp, "CUSTOM(%d)", val);
strlcat(text, temp, text_size);
break;
}
}

void get_unit_addr_code(uint64_t pkt, char *text, int text_size) {
uint32_t val;
char temp[32];

pkt >>= 7;
val = pkt & 0x1ffffULL;
strlcat(text, "unit_addr=", text_size);
sprintf(temp, "%d", val);
strlcat(text, temp, text_size);
}

void get_brake_pressure(uint64_t pkt, char *text, int text_size) {
uint32_t val;
char temp[32];

pkt >>= 24;
val = pkt & 0x7fULL;

strlcat(text, "brake_status=", text_size);

switch (val) {
case 127:
strlcat(text, "GO", text_size);
break;

case 126:
strlcat(text, "NO-GO", text_size);
break;

default:
if (val < 45) {
sprintf(temp, "NO-GO(%d psig)", val);
} else {
sprintf(temp, "GO(%d psig)", val);
}

strlcat(text, temp, text_size);
break;
}
}

void get_disc_bits(uint64_t pkt, char *text, int text_size) {
uint32_t val;
char temp[32];

pkt >>= 31;
val = pkt & 0xffULL;

strlcat(text, "disc_bits=", text_size);
sprintf(temp, "%02x", val);
strlcat(text, temp, text_size);
}

void get_valve_bit(uint64_t pkt, char *text, int text_size) {
uint32_t val;

pkt >>= 39;
val = pkt & 0x01;

strlcat(text, "valve=", text_size);
strlcat(text, val == 0 ? "FAILED" : "OPERATIONAL", text_size);
}

void get_confirm_bit(uint64_t pkt, char *text, int text_size) {
uint32_t val;

pkt >>= 40;
val = pkt & 0x01;

strlcat(text, "confirm=", text_size);
strlcat(text, val == 0 ? "UPDATE" : "RESPONSE", text_size);
}

void get_disc_bit1(uint64_t pkt, char *text, int text_size) {
uint32_t val;
char temp[32];

pkt >>= 41;
val = pkt & 0x01;

strlcat(text, "disc_bit_1=", text_size);
sprintf(temp, "%d", val);
strlcat(text, temp, text_size);
}

void get_motion_bit(uint64_t pkt, char *text, int text_size) {
uint32_t val;

pkt >>= 42;
val = pkt & 0x01;

strlcat(text, "motion=", text_size);
strlcat(text, val == 0 ? "STOPPED/NOT_MONITORED" : "IN_MOTION", text_size);
}

void get_mkr_light_batt_bit(uint64_t pkt, char *text, int text_size) {
uint32_t val;

pkt >>= 43;
val = pkt & 0x01;

strlcat(text, "light_batt=", text_size);
strlcat(text, val == 0 ? "OK/NOT_MONITORED" : "WEAK", text_size);
}

void get_mkr_light_bit(uint64_t pkt, char *text, int text_size) {
uint32_t val;

pkt >>= 44;
val = pkt & 0x01;

strlcat(text, "light=", text_size);
strlcat(text, val == 0 ? "OFF/NOT_MONITORED" : "ON", text_size);
}

void eotd_to_text (unsigned char *eotd, int eotd_len, char *text, int text_size)
{
time_t now = time(NULL);
assert (eotd_len == EOTD_LENGTH + 1);

uint64_t pkt = 0ULL;

for (int i = 0; i < EOTD_LENGTH; i++) {
pkt <<= 8;
pkt |= eotd[i];
}

*text = '\0';
strcat(text, ctime(&now));
for (int i = 0; i < eotd_len; i++) {
char temp[32];
snprintf(temp, sizeof(temp), " %02x", eotd[i]);
#ifndef EOTD_RAW
char eotd_type = eotd[EOTD_LENGTH];

if (eotd_type == EOTD_TYPE_F2R) {
strlcat(text, "FRONT>REAR:", text_size);
} else {
strlcat(text, "REAR>FRONT:", text_size);
}

#ifdef EOTD_TIMESTAMP
time_t now = time(NULL);
strlcat(text, "timestamp=", text_size);
strlcat(text, ctime(&now), text_size);
text[strlen(text) -1] = ','; // zap lf
#endif
get_chain(pkt, text, text_size);
add_comma(text, text_size);
get_dev_batt_stat(pkt, text, text_size);
add_comma(text, text_size);
get_msg_id_type(pkt, text, text_size);
add_comma(text, text_size);
get_unit_addr_code(pkt, text, text_size);
add_comma(text, text_size);
get_brake_pressure(pkt, text, text_size);
add_comma(text, text_size);
get_disc_bits(pkt, text, text_size);
add_comma(text, text_size);
get_valve_bit(pkt, text, text_size);
add_comma(text, text_size);
get_confirm_bit(pkt, text, text_size);
add_comma(text, text_size);
get_disc_bit1(pkt, text, text_size);
add_comma(text, text_size);
get_motion_bit(pkt, text, text_size);
add_comma(text, text_size);
get_mkr_light_batt_bit(pkt, text, text_size);
add_comma(text, text_size);
get_mkr_light_bit(pkt, text, text_size);
#ifdef EOTD_APPEND_HEX
char hex[64];
add_comma(text, text_size);
snprintf(hex, sizeof(hex), "%llx", pkt);
strlcat(text, "hex=", text_size);
strlcat(text, hex, text_size);
#endif
#else
char temp[8];
for (int i = 0; i < 8; i++) {
sprintf(temp, " %02x", eotd[i]);
strlcat(text, temp, text_size);
}
#endif
}
24 changes: 11 additions & 13 deletions src/hdlc_rec.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "demod_9600.h" /* for descramble() */
#include "ptt.h"
#include "fx25.h"
#include "eotd_defs.h"


//#define TEST 1 /* Define for unit testing. */
Expand Down Expand Up @@ -116,6 +117,8 @@ struct hdlc_state_s {

uint64_t eotd_acc; /* Accumulate last recent 32 bits for EOTD. */

char eotd_type; /* E for End of train, H for head of train */

int eotd_gathering; /* Decoding in progress - valid frame. */
};

Expand Down Expand Up @@ -429,14 +432,6 @@ a good modem here and providing a result when it is received.
*
***********************************************************************************/

#define EOTD_MAX_BYTES 8

#define EOTD_PREAMBLE_AND_BARKER_CODE 0x48eaaaaa00000000ULL
#define EOTD_PREAMBLE_MASK 0xffffffff00000000ULL

#define HOTD_PREAMBLE_AND_BARKER_CODE 0x9488f1aa00000000ULL
#define HOTD_PREAMBLE_MASK 0xffffffff00000000ULL

static void eotd_rec_bit (int chan, int subchan, int slice, int raw, int future_use)
{
struct hdlc_state_s *H;
Expand All @@ -451,7 +446,7 @@ dw_printf("chan=%d subchan=%d slice=%d raw=%d\n", chan, subchan, slice, raw);
#endif
//dw_printf ("slice %d = %d\n", slice, raw);

// Accumulate most recent 32 bits in LSB-first order.
// Accumulate most recent 64 bits in LSB-first order.

H->eotd_acc >>= 1;
if (raw) {
Expand All @@ -466,6 +461,7 @@ dw_printf("chan=%d subchan=%d slice=%d raw=%d\n", chan, subchan, slice, raw);
#ifdef EOTD_DEBUG
dw_printf ("Barker Code Found %llx\n", H->eotd_acc);
#endif
H->eotd_type = (H->eotd_acc & EOTD_PREAMBLE_MASK) == EOTD_PREAMBLE_AND_BARKER_CODE ? EOTD_TYPE_R2F : EOTD_TYPE_F2R;
H->olen = 0;
H->eotd_gathering = 1;
H->frame_len = 0;
Expand All @@ -474,15 +470,17 @@ dw_printf("chan=%d subchan=%d slice=%d raw=%d\n", chan, subchan, slice, raw);
else if (H->eotd_gathering) {
H->olen++;

if (H->olen == EOTD_MAX_BYTES * 8) {
H->frame_len = EOTD_MAX_BYTES;
for (int i = EOTD_MAX_BYTES -1; i >=0; i--) {
if (H->olen == EOTD_LENGTH * 8) {
H->frame_len = EOTD_LENGTH + 1; // appended type
for (int i = EOTD_LENGTH -1; i >=0; i--) {
H->frame_buf[i] = H->eotd_acc & 0xff;
H->eotd_acc >>= 8;
}

H->frame_buf[EOTD_LENGTH] = H->eotd_type;
done = 1;
#ifdef EOTD_DEBUG
for (int ii=0; ii < EOTD_MAX_BYTES; ii++) {dw_printf("%02x ", H->frame_buf[ii]); } dw_printf("\n");
for (int ii=0; ii < EOTD_MAX_LENGTH; ii++) {dw_printf("%02x ", H->frame_buf[ii]); } dw_printf("\n");
#endif
}
}
Expand Down