diff --git a/src/eotd.c b/src/eotd.c index 193edd9..cec5ad1 100644 --- a/src/eotd.c +++ b/src/eotd.c @@ -37,8 +37,13 @@ #include #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. @@ -48,14 +53,243 @@ * *--------------------------------------------------------------------*/ -void eotd_to_text (unsigned char *eotd, int eotd_len, char *text, int text_size) -{ - time_t now = time(NULL); - *text = '\0'; - strcat(text, ctime(&now)); - for (int i = 0; i < eotd_len; i++) { - char temp[32]; - snprintf(temp, sizeof(temp), " %02x", eotd[i]); - strlcat(text, temp, text_size); +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) +{ + 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'; +#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 +} diff --git a/src/hdlc_rec.c b/src/hdlc_rec.c index d6ebdef..f6a024c 100644 --- a/src/hdlc_rec.c +++ b/src/hdlc_rec.c @@ -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. */ @@ -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. */ }; @@ -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; @@ -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) { @@ -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; @@ -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 } }