mirror of
https://github.com/halleysfifthinc/Toyota-AVC-LAN
synced 2025-06-07 16:06:12 +00:00
Add binary printing and reading/parsing from serial
This commit is contained in:
parent
cabcd06e67
commit
ebb5ec95ca
@ -93,6 +93,7 @@
|
|||||||
#include <avr/io.h>
|
#include <avr/io.h>
|
||||||
#include <avr/sfr_defs.h>
|
#include <avr/sfr_defs.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "avclandrv.h"
|
#include "avclandrv.h"
|
||||||
#include "com232.h"
|
#include "com232.h"
|
||||||
@ -674,7 +675,7 @@ uint8_t AVCLAN_readframe() {
|
|||||||
STARTEvent;
|
STARTEvent;
|
||||||
|
|
||||||
if (printAllFrames)
|
if (printAllFrames)
|
||||||
AVCLAN_printframe(&frame);
|
AVCLAN_printframe(&frame, printBinary);
|
||||||
|
|
||||||
if (for_me) {
|
if (for_me) {
|
||||||
if (CheckCmd(&frame, stat1)) {
|
if (CheckCmd(&frame, stat1)) {
|
||||||
@ -746,6 +747,43 @@ uint8_t AVCLAN_readframe() {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
AVCLAN_frame_t *AVCLAN_parseframe(const uint8_t *bytes, uint8_t len) {
|
||||||
|
if (len < sizeof(AVCLAN_frame_t))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
AVCLAN_frame_t *frame = malloc(sizeof(AVCLAN_frame_t) + 1);
|
||||||
|
|
||||||
|
if (!frame)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
frame->broadcast = *bytes++;
|
||||||
|
frame->controller_addr = *(uint16_t *)bytes++;
|
||||||
|
bytes++;
|
||||||
|
frame->peripheral_addr = *(uint16_t *)bytes++;
|
||||||
|
bytes++;
|
||||||
|
frame->control = *bytes++;
|
||||||
|
frame->length = *bytes++;
|
||||||
|
|
||||||
|
if (frame->length <= (len - 8)) {
|
||||||
|
free(frame);
|
||||||
|
return NULL;
|
||||||
|
} else {
|
||||||
|
AVCLAN_frame_t *framedata =
|
||||||
|
realloc(frame, sizeof(AVCLAN_frame_t) + frame->length);
|
||||||
|
if (!framedata) {
|
||||||
|
free(frame);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
frame = framedata;
|
||||||
|
frame->data = (uint8_t *)frame + sizeof(AVCLAN_frame_t);
|
||||||
|
for (uint8_t i = 0; i < frame->length; i++) {
|
||||||
|
frame->data[i] = *bytes++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return frame;
|
||||||
|
}
|
||||||
|
|
||||||
uint8_t AVCLAN_sendframe(const AVCLAN_frame_t *frame) {
|
uint8_t AVCLAN_sendframe(const AVCLAN_frame_t *frame) {
|
||||||
STOPEvent;
|
STOPEvent;
|
||||||
|
|
||||||
@ -834,36 +872,39 @@ uint8_t AVCLAN_sendframe(const AVCLAN_frame_t *frame) {
|
|||||||
STARTEvent;
|
STARTEvent;
|
||||||
|
|
||||||
if (printAllFrames)
|
if (printAllFrames)
|
||||||
AVCLAN_printframe(frame);
|
AVCLAN_printframe(frame, printBinary);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AVCLAN_printframe(const AVCLAN_frame_t *frame) {
|
void AVCLAN_printframe(const AVCLAN_frame_t *frame, uint8_t binary) {
|
||||||
if (frame->peripheral_addr == CD_ID ||
|
if (binary) {
|
||||||
(frame->broadcast && frame->peripheral_addr == 0x1FF))
|
RS232_SendByte(0x10); // Data Link Escape, signaling binary data forthcoming
|
||||||
RS232_Print(" < ");
|
RS232_sendbytes((uint8_t *)frame,
|
||||||
else
|
sizeof(AVCLAN_frame_t) - sizeof(uint8_t *));
|
||||||
RS232_Print(">< ");
|
RS232_sendbytes(frame->data, frame->length);
|
||||||
|
RS232_SendByte(0x17); // End of transmission block
|
||||||
|
RS232_Print("\n");
|
||||||
|
} else {
|
||||||
|
RS232_PrintHex4(frame->broadcast);
|
||||||
|
|
||||||
RS232_PrintHex4(frame->broadcast);
|
|
||||||
|
|
||||||
RS232_Print(" 0x");
|
|
||||||
RS232_PrintHex12(frame->controller_addr);
|
|
||||||
RS232_Print(" 0x");
|
|
||||||
RS232_PrintHex12(frame->peripheral_addr);
|
|
||||||
|
|
||||||
RS232_Print(" 0x");
|
|
||||||
RS232_PrintHex4(frame->control);
|
|
||||||
|
|
||||||
RS232_Print(" 0x");
|
|
||||||
RS232_PrintHex4(frame->length);
|
|
||||||
|
|
||||||
for (uint8_t i = 0; i < frame->length; i++) {
|
|
||||||
RS232_Print(" 0x");
|
RS232_Print(" 0x");
|
||||||
RS232_PrintHex8(frame->data[i]);
|
RS232_PrintHex12(frame->controller_addr);
|
||||||
|
RS232_Print(" 0x");
|
||||||
|
RS232_PrintHex12(frame->peripheral_addr);
|
||||||
|
|
||||||
|
RS232_Print(" 0x");
|
||||||
|
RS232_PrintHex4(frame->control);
|
||||||
|
|
||||||
|
RS232_Print(" 0x");
|
||||||
|
RS232_PrintHex4(frame->length);
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < frame->length; i++) {
|
||||||
|
RS232_Print(" 0x");
|
||||||
|
RS232_PrintHex8(frame->data[i]);
|
||||||
|
}
|
||||||
|
RS232_Print("\n");
|
||||||
}
|
}
|
||||||
RS232_Print("\n");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t CheckCmd(const AVCLAN_frame_t *frame, const uint8_t *cmd) {
|
uint8_t CheckCmd(const AVCLAN_frame_t *frame, const uint8_t *cmd) {
|
||||||
|
@ -49,6 +49,7 @@ extern uint16_t HU_ID; // Head-unit ID
|
|||||||
|
|
||||||
extern uint8_t printAllFrames;
|
extern uint8_t printAllFrames;
|
||||||
extern uint8_t verbose;
|
extern uint8_t verbose;
|
||||||
|
extern uint8_t printBinary;
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
cm_Null = 0,
|
cm_Null = 0,
|
||||||
@ -97,7 +98,8 @@ typedef struct AVCLAN_frame_struct {
|
|||||||
|
|
||||||
uint8_t AVCLAN_readframe();
|
uint8_t AVCLAN_readframe();
|
||||||
uint8_t AVCLAN_sendframe(const AVCLAN_frame_t *frame);
|
uint8_t AVCLAN_sendframe(const AVCLAN_frame_t *frame);
|
||||||
void AVCLAN_printframe(const AVCLAN_frame_t *frame);
|
void AVCLAN_printframe(const AVCLAN_frame_t *frame, uint8_t binary);
|
||||||
|
AVCLAN_frame_t *AVCLAN_parseframe(const uint8_t *bytes, uint8_t len);
|
||||||
|
|
||||||
void AVCLAN_init();
|
void AVCLAN_init();
|
||||||
void AVCLan_Send_Status();
|
void AVCLan_Send_Status();
|
||||||
|
@ -69,6 +69,13 @@ void RS232_SendByte(uint8_t Data) {
|
|||||||
USART0_TXDATAL = Data; // send character
|
USART0_TXDATAL = Data; // send character
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void RS232_sendbytes(const uint8_t *bytes, uint8_t len) {
|
||||||
|
const uint8_t *end = bytes + len;
|
||||||
|
while (bytes < end) {
|
||||||
|
RS232_SendByte(*bytes++);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void RS232_Print(const char *pBuf) {
|
void RS232_Print(const char *pBuf) {
|
||||||
register uint8_t c;
|
register uint8_t c;
|
||||||
while ((c = *pBuf++)) {
|
while ((c = *pBuf++)) {
|
||||||
|
@ -31,12 +31,12 @@ extern uint8_t readkey;
|
|||||||
void RS232_Init(void);
|
void RS232_Init(void);
|
||||||
void RS232_Print_P(const char *str_addr);
|
void RS232_Print_P(const char *str_addr);
|
||||||
void RS232_SendByte(uint8_t Data);
|
void RS232_SendByte(uint8_t Data);
|
||||||
|
void RS232_sendbytes(const uint8_t *bytes, uint8_t len);
|
||||||
void RS232_Print(const char *pBuf);
|
void RS232_Print(const char *pBuf);
|
||||||
void RS232_PrintHex4(uint8_t Data);
|
void RS232_PrintHex4(uint8_t Data);
|
||||||
void RS232_PrintHex8(uint8_t Data);
|
void RS232_PrintHex8(uint8_t Data);
|
||||||
void RS232_PrintHex12(uint16_t x);
|
void RS232_PrintHex12(uint16_t x);
|
||||||
void RS232_PrintDec(uint8_t Data);
|
void RS232_PrintDec(uint8_t Data);
|
||||||
void RS232_PrintDec2(uint8_t Data);
|
void RS232_PrintDec2(uint8_t Data);
|
||||||
char *itoa(int i, char b[]);
|
|
||||||
|
|
||||||
#endif // __COM232_H
|
#endif // __COM232_H
|
||||||
|
134
src/sniffer.c
134
src/sniffer.c
@ -34,6 +34,7 @@
|
|||||||
|
|
||||||
uint8_t Event;
|
uint8_t Event;
|
||||||
uint8_t echoCharacters;
|
uint8_t echoCharacters;
|
||||||
|
uint8_t readBinary;
|
||||||
|
|
||||||
const char const *offon[] = {"OFF", "ON"};
|
const char const *offon[] = {"OFF", "ON"};
|
||||||
|
|
||||||
@ -88,6 +89,24 @@ int main() {
|
|||||||
case '?':
|
case '?':
|
||||||
print_help();
|
print_help();
|
||||||
break;
|
break;
|
||||||
|
case 'v':
|
||||||
|
verbose ^= 1;
|
||||||
|
RS232_Print("Verbose: ");
|
||||||
|
RS232_Print(offon[verbose]);
|
||||||
|
RS232_Print("\n");
|
||||||
|
break;
|
||||||
|
case 'X':
|
||||||
|
printBinary = 1;
|
||||||
|
RS232_Print("Binary: ");
|
||||||
|
RS232_Print(offon[1]);
|
||||||
|
RS232_Print("\n");
|
||||||
|
break;
|
||||||
|
case 'x':
|
||||||
|
printBinary = 0;
|
||||||
|
RS232_Print("Binary: ");
|
||||||
|
RS232_Print(offon[0]);
|
||||||
|
RS232_Print("\n");
|
||||||
|
break;
|
||||||
case 'S': // Read sequence
|
case 'S': // Read sequence
|
||||||
printAllFrames = 0;
|
printAllFrames = 0;
|
||||||
RS232_Print("READ SEQUENCE > \n");
|
RS232_Print("READ SEQUENCE > \n");
|
||||||
@ -122,24 +141,19 @@ int main() {
|
|||||||
case 'r': // Register into the abyss
|
case 'r': // Register into the abyss
|
||||||
AVCLan_Register();
|
AVCLan_Register();
|
||||||
break;
|
break;
|
||||||
case 'v':
|
|
||||||
verbose ^= 1;
|
|
||||||
RS232_Print("Verbose: ");
|
|
||||||
RS232_Print(offon[verbose]);
|
|
||||||
RS232_Print("\n");
|
|
||||||
break;
|
|
||||||
case 'l': // Print received messages
|
case 'l': // Print received messages
|
||||||
printAllFrames ^= 1;
|
printAllFrames ^= 1;
|
||||||
RS232_Print("Logging:");
|
RS232_Print("Logging: ");
|
||||||
RS232_Print(offon[printAllFrames]);
|
RS232_Print(offon[printAllFrames]);
|
||||||
RS232_Print("\n");
|
RS232_Print("\n");
|
||||||
break;
|
break;
|
||||||
case 'k': // Echo input
|
case 'k': // Echo input
|
||||||
echoCharacters ^= 1;
|
echoCharacters ^= 1;
|
||||||
RS232_Print("Echo characters:");
|
RS232_Print("Echo characters: ");
|
||||||
RS232_Print(offon[echoCharacters]);
|
RS232_Print(offon[echoCharacters]);
|
||||||
RS232_Print("\n");
|
RS232_Print("\n");
|
||||||
break;
|
break;
|
||||||
|
case 'b':
|
||||||
case 'B': // Beep
|
case 'B': // Beep
|
||||||
data_tmp[0] = 0x00;
|
data_tmp[0] = 0x00;
|
||||||
data_tmp[1] = 0x5E;
|
data_tmp[1] = 0x5E;
|
||||||
@ -149,56 +163,81 @@ int main() {
|
|||||||
s_len = 5;
|
s_len = 5;
|
||||||
msg.length = s_len;
|
msg.length = s_len;
|
||||||
msg.broadcast = UNICAST;
|
msg.broadcast = UNICAST;
|
||||||
|
msg.controller_addr = 0x110;
|
||||||
|
msg.peripheral_addr = 0x440;
|
||||||
|
AVCLAN_sendframe(&msg);
|
||||||
|
break;
|
||||||
|
case 'e': // Beep
|
||||||
|
data_tmp[0] = 0x00;
|
||||||
|
data_tmp[1] = 0x01;
|
||||||
|
data_tmp[2] = 0x11;
|
||||||
|
data_tmp[3] = 0x45;
|
||||||
|
data_tmp[4] = 0x63;
|
||||||
|
s_len = 5;
|
||||||
|
msg.length = s_len;
|
||||||
|
msg.broadcast = UNICAST;
|
||||||
|
msg.controller_addr = CD_ID;
|
||||||
|
msg.peripheral_addr = HU_ID;
|
||||||
AVCLAN_sendframe(&msg);
|
AVCLAN_sendframe(&msg);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
#ifdef HARDWARE_DEBUG
|
|
||||||
case '1':
|
|
||||||
SetHighLow();
|
|
||||||
break;
|
|
||||||
case 'E':
|
|
||||||
if (INPUT_IS_SET) {
|
|
||||||
RS232_Print("Set/High/1\n");
|
|
||||||
} else if (INPUT_IS_CLEAR) {
|
|
||||||
RS232_Print("Unset/Low/0\n");
|
|
||||||
} else {
|
|
||||||
RS232_Print("WTF?\n");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
#ifdef SOFTWARE_DEBUG
|
#ifdef SOFTWARE_DEBUG
|
||||||
case 'M':
|
case 'M':
|
||||||
AVCLan_Measure();
|
AVCLan_Measure();
|
||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
default:
|
case 0x10: // Signals binary sequence incoming
|
||||||
if (readSeq == 1) {
|
if (!readSeq && !readBinary) {
|
||||||
s_c[s_dig] = readkey;
|
readSeq = 1;
|
||||||
|
readBinary = 1;
|
||||||
s_dig++;
|
s_len = 0;
|
||||||
if (s_dig == 2) {
|
break;
|
||||||
if (s_c[0] < ':')
|
} // else (readSeq || readBinary); fall through to default
|
||||||
s_c[0] -= 48;
|
case '\n':
|
||||||
else
|
if (readSeq && readBinary && data_tmp[s_len] == 0x17) {
|
||||||
s_c[0] -= 55;
|
s_len--;
|
||||||
data_tmp[s_len] = 16 * s_c[0];
|
AVCLAN_frame_t *frame = AVCLAN_parseframe(data_tmp, s_len);
|
||||||
if (s_c[1] < ':')
|
if (frame) {
|
||||||
s_c[1] -= 48;
|
AVCLAN_sendframe(frame);
|
||||||
else
|
free(frame);
|
||||||
s_c[1] -= 55;
|
readSeq = 0;
|
||||||
data_tmp[s_len] += s_c[1];
|
readBinary = 0;
|
||||||
s_len++;
|
|
||||||
s_dig = 0;
|
|
||||||
s_c[0] = s_c[1] = 0;
|
|
||||||
}
|
}
|
||||||
if (echoCharacters) {
|
break;
|
||||||
RS232_Print("CURRENT SEQUENCE > ");
|
} // else (readSeq || readBinary || most recent char != 0x17);
|
||||||
for (i = 0; i < s_len; i++) {
|
// fall through to default
|
||||||
RS232_PrintHex8(data_tmp[i]);
|
default:
|
||||||
RS232_SendByte(' ');
|
if (readSeq) {
|
||||||
|
if (readBinary) {
|
||||||
|
data_tmp[s_len++] = readkey;
|
||||||
|
} else {
|
||||||
|
s_c[s_dig] = readkey;
|
||||||
|
|
||||||
|
s_dig++;
|
||||||
|
if (s_dig == 2) {
|
||||||
|
if (s_c[0] < ':')
|
||||||
|
s_c[0] -= 48;
|
||||||
|
else
|
||||||
|
s_c[0] -= 55;
|
||||||
|
data_tmp[s_len] = 16 * s_c[0];
|
||||||
|
if (s_c[1] < ':')
|
||||||
|
s_c[1] -= 48;
|
||||||
|
else
|
||||||
|
s_c[1] -= 55;
|
||||||
|
data_tmp[s_len] += s_c[1];
|
||||||
|
s_len++;
|
||||||
|
s_dig = 0;
|
||||||
|
s_c[0] = s_c[1] = 0;
|
||||||
|
}
|
||||||
|
if (echoCharacters) {
|
||||||
|
RS232_Print("CURRENT SEQUENCE > ");
|
||||||
|
for (i = 0; i < s_len; i++) {
|
||||||
|
RS232_PrintHex8(data_tmp[i]);
|
||||||
|
RS232_SendByte(' ');
|
||||||
|
}
|
||||||
|
RS232_Print("\n");
|
||||||
}
|
}
|
||||||
RS232_Print("\n");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // switch (readkey)
|
} // switch (readkey)
|
||||||
@ -213,6 +252,7 @@ void Setup() {
|
|||||||
|
|
||||||
printAllFrames = 1;
|
printAllFrames = 1;
|
||||||
echoCharacters = 1;
|
echoCharacters = 1;
|
||||||
|
readBinary = 0;
|
||||||
|
|
||||||
_PROTECTED_WRITE(CLKCTRL.MCLKCTRLB, (CLK_PRESCALE | CLK_PRESCALE_DIV));
|
_PROTECTED_WRITE(CLKCTRL.MCLKCTRLB, (CLK_PRESCALE | CLK_PRESCALE_DIV));
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user