Implemented simple serial control protocol for APRS stuff.

This commit is contained in:
Mark Qvist 2014-06-18 11:34:05 +02:00
parent 416247201e
commit a667301d57
12 changed files with 2895 additions and 2331 deletions

View file

@ -13,6 +13,8 @@ Modem_USER_CSRC = \
$(Modem_HW_PATH)/hardware.c \
$(Modem_HW_PATH)/afsk.c \
$(Modem_HW_PATH)/protocol/mp1.c \
$(Modem_HW_PATH)/protocol/SimpleSerial.c \
$(Modem_HW_PATH)/protocol/KISS.c \
$(Modem_HW_PATH)/compression/heatshrink_decoder.c \
$(Modem_HW_PATH)/compression/heatshrink_encoder.c \
#

View file

@ -15,6 +15,8 @@
#include "afsk.h" // Header for AFSK modem
#include "protocol/KISS.h" // KISS TNC protocol
#if SERIAL_DEBUG
#include "cfg/debug.h" // Debug configuration from BertOS
#endif
@ -31,17 +33,24 @@ static Serial ser; // Declare a serial interface struct
#define ADC_CH 0 // Define which channel (pin) we want
// for the ADC (this is A0 on arduino)
#define SERIAL_PROTOCOL PROTOCOL_SIMPLE_SERIAL
#define YOUR_CALLSIGN "nocall"
#define TO_CALL "apzmdm"
static AX25Call path[] = AX25_PATH(AX25_CALL(TO_CALL, 0), AX25_CALL(YOUR_CALLSIGN, 0), AX25_CALL("wide1", 1), AX25_CALL("wide2", 2));
#define SEND_TEST_PACKETS true
#define SEND_TEST_PACKETS false
#define TEST_INTERVAL 15000L
#define APRS_MSG "Test APRS packet"
static uint8_t serialBuffer[CONFIG_AX25_FRAME_BUF_LEN+1]; // Buffer for holding incoming serial data
static int sbyte; // For holding byte read from serial port
static size_t serialLen = 0; // Counter for counting length of data from serial
static bool sertx = false; // Flag signifying whether it's time to send data
// received on the serial port.
#define SER_BUFFER_FULL (serialLen < MP1_MAX_DATA_SIZE-1)
#include "protocol/SimpleSerial.h" // Simple serial control protocol
//////////////////////////////////////////////////////
// And here comes the actual program :) //
//////////////////////////////////////////////////////
@ -51,12 +60,12 @@ static AX25Call path[] = AX25_PATH(AX25_CALL(TO_CALL, 0), AX25_CALL(YOUR_CALLSIG
// Right now it just prints the packet to the serial port.
static void message_callback(struct AX25Msg *msg)
{
kfile_printf(&ser.fd, "\n\nSRC[%.6s-%d], DST[%.6s-%d]\r\n", msg->src.call, msg->src.ssid, msg->dst.call, msg->dst.ssid);
for (int i = 0; i < msg->rpt_cnt; i++)
kfile_printf(&ser.fd, "via: [%.6s-%d]\r\n", msg->rpt_lst[i].call, msg->rpt_lst[i].ssid);
kfile_printf(&ser.fd, "DATA: %.*s\r\n", msg->len, msg->info);
if (SERIAL_PROTOCOL == PROTOCOL_SIMPLE_SERIAL) {
ss_messageCallback(msg, &ser);
}
if (SERIAL_PROTOCOL == PROTOCOL_KISS) {
// Not implemented yet
}
}
// Simple initialization function.
@ -98,7 +107,68 @@ int main(void)
{
// First we instruct the protocol to check for
// incoming data
ax25_poll(&ax25);
ax25_poll(&ax25);
// Poll for incoming serial data
if (!sertx && ser_available(&ser)) {
// We then read a byte from the serial port.
// Notice that we use "_nowait" since we can't
// have this blocking execution until a byte
// comes in.
sbyte = ser_getchar_nowait(&ser);
// If SERIAL_DEBUG is specified we'll handle
// serial data as direct human input and only
// transmit when we get a LF character
#if SERIAL_DEBUG
// If we have not yet surpassed the maximum frame length
// and the byte is not a "transmit" (newline) character,
// we should store it for transmission.
if ((serialLen < MP1_MAX_DATA_SIZE) && (sbyte != 10)) {
// Put the read byte into the buffer;
serialBuffer[serialLen] = sbyte;
// Increment the read length counter
serialLen++;
} else {
// If one of the above conditions were actually the
// case, it means we have to transmit, se we set
// transmission flag to true.
sertx = true;
}
#else
// Otherwise we assume the modem is running
// in automated mode, and we push out data
// as it becomes available. We either transmit
// immediately when the max frame length has
// been reached, or when we get no input for
// a certain amount of time.
if (serialLen < CONFIG_AX25_FRAME_BUF_LEN-1) {
// Put the read byte into the buffer;
serialBuffer[serialLen] = sbyte;
// Increment the read length counter
serialLen++;
} else {
// If max frame length has been reached
// we need to transmit.
serialBuffer[serialLen] = sbyte;
serialLen++;
sertx = true;
}
start = timer_clock();
#endif
} else {
if (!SERIAL_DEBUG && serialLen > 0 && timer_clock() - start > ms_to_ticks(TX_MAXWAIT)) {
sertx = true;
}
}
if (sertx) {
ss_serialCallback(serialBuffer, serialLen, &ser, &ax25);
sertx = false;
serialLen = 0;
}
// Use AX.25 to send test data
if (SEND_TEST_PACKETS && timer_clock() - start > ms_to_ticks(TEST_INTERVAL))

0
Modem/protocol/KISS.c Normal file
View file

5
Modem/protocol/KISS.h Normal file
View file

@ -0,0 +1,5 @@
#ifndef PROTOCOL_KISS
#define PROTOCOL_KISS 0x02
#endif

View file

@ -0,0 +1,190 @@
#include <string.h>
#include "protocol/SimpleSerial.h"
bool PRINT_SRC = true;
bool PRINT_DST = true;
bool PRINT_PATH = true;
bool PRINT_DATA = true;
bool PRINT_INFO = true;
bool VERBOSE = false;
AX25Call src;
AX25Call dst;
AX25Call path1;
AX25Call path2;
char CALL[6] = "NOCALL";
int CALL_SSID = 0;
char DST[6] = "APZMDM";
int DST_SSID = 0;
char PATH1[6] = "WIDE1";
int PATH1_SSID = 1;
char PATH2[6] = "WIDE2";
int PATH2_SSID = 2;
AX25Call path[4];
void ss_messageCallback(struct AX25Msg *msg, Serial *ser) {
if (PRINT_SRC) {
if (PRINT_INFO) kfile_print(&ser->fd, "SRC: ");
kfile_printf(&ser->fd, "[%.6s-%d] ", msg->src.call, msg->src.ssid);
}
if (PRINT_DST) {
if (PRINT_INFO) kfile_printf(&ser->fd, "DST: ");
kfile_printf(&ser->fd, "[%.6s-%d] ", msg->dst.call, msg->dst.ssid);
}
if (PRINT_PATH) {
if (PRINT_INFO) kfile_print(&ser->fd, "PATH: ");
for (int i = 0; i < msg->rpt_cnt; i++)
kfile_printf(&ser->fd, "[%.6s-%d] ", msg->rpt_lst[i].call, msg->rpt_lst[i].ssid);
}
if (PRINT_DATA) {
if (PRINT_INFO) kfile_print(&ser->fd, "DATA: ");
kfile_printf(&ser->fd, "%.*s", msg->len, msg->info);
}
kfile_print(&ser->fd, "\r\n");
}
void ss_serialCallback(void *_buffer, size_t length, Serial *ser, AX25Ctx *ctx) {
if (VERBOSE) {
kfile_printf(&ser->fd, "Serial input");
}
uint8_t *buffer = (uint8_t *)_buffer;
if (length > 0) {
// ! as first char to send packet
if (buffer[0] == '!' && length > 1) {
buffer++; length--;
ss_sendMsg(buffer, length, ctx);
} else if (buffer[0] == 'c' && length > 3) {
buffer++; length--;
int count = 0;
while (length-- && count < 6) {
char c = buffer[count];
if (c != 0 && c != 10 && c != 13) {
CALL[count] = c;
} else {
CALL[count] = 0;
}
count++;
}
} else if (buffer[0] == 'd' && length > 3) {
buffer++; length--;
int count = 0;
while (length-- && count < 6) {
char c = buffer[count];
if (c != 0 && c != 10 && c != 13) {
DST[count] = c;
} else {
DST[count] = 0;
}
count++;
}
} else if (buffer[0] == '1' && length > 1) {
buffer++; length--;
int count = 0;
while (length-- && count < 6) {
char c = buffer[count];
if (c != 0 && c != 10 && c != 13) {
PATH1[count] = c;
} else {
PATH1[count] = 0;
}
count++;
}
} else if (buffer[0] == '2' && length > 1) {
buffer++; length--;
int count = 0;
while (length-- && count < 6) {
char c = buffer[count];
if (c != 0 && c != 10 && c != 13) {
PATH2[count] = c;
} else {
PATH2[count] = 0;
}
count++;
}
} else if (buffer[0] == 's' && length > 2) {
buffer++; length--;
if (buffer[0] == 'c') CALL_SSID = buffer[1]-48;
if (buffer[0] == 'd') DST_SSID = buffer[1]-48;
if (buffer[0] == '1') PATH1_SSID = buffer[1]-48;
if (buffer[0] == '2') PATH2_SSID = buffer[1]-48;
} else if (buffer[0] == 'p' && length > 2) {
buffer++; length--;
if (buffer[0] == 's') {
if (buffer[1] == 49) { PRINT_SRC = true; } else { PRINT_SRC = false; }
}
if (buffer[0] == 'd') {
if (buffer[1] == 49) { PRINT_DST = true; } else { PRINT_DST = false; }
}
if (buffer[0] == 'p') {
if (buffer[1] == 49) { PRINT_PATH = true; } else { PRINT_PATH = false; }
}
if (buffer[0] == 'm') {
if (buffer[1] == 49) { PRINT_DATA = true; } else { PRINT_DATA = false; }
}
if (buffer[0] == 'i') {
if (buffer[1] == 49) { PRINT_INFO = true; } else { PRINT_INFO = false; }
}
}
}
}
void ss_sendMsg(void *_buffer, size_t length, AX25Ctx *ax25) {
uint8_t *buffer = (uint8_t *)_buffer;
memcpy(dst.call, DST, 6);
dst.ssid = DST_SSID;
memcpy(src.call, CALL, 6);
src.ssid = CALL_SSID;
memcpy(path1.call, PATH1, 6);
path1.ssid = PATH1_SSID;
memcpy(path2.call, PATH2, 6);
path2.ssid = PATH2_SSID;
path[0] = dst;
path[1] = src;
path[2] = path1;
path[3] = path2;
ax25_sendVia(ax25, path, countof(path), buffer, length);
}
void ss_printSrc(bool val) {
PRINT_SRC = val;
}
void ss_printDst(bool val) {
PRINT_DST = val;
}
void ss_printPath(bool val) {
PRINT_PATH = val;
}
void ss_printData(bool val) {
PRINT_DATA = val;
}
void ss_printInfo(bool val) {
PRINT_INFO = val;
}

View file

@ -0,0 +1,17 @@
#ifndef PROTOCOL_SIMPLE_SERIAL
#include <net/ax25.h>
#include <drv/ser.h>
#define PROTOCOL_SIMPLE_SERIAL 0x01
void ss_messageCallback(struct AX25Msg *msg, Serial *ser);
void ss_serialCallback(void *_buffer, size_t length, Serial *ser, AX25Ctx *ctx);
void ss_printSrc(bool val);
void ss_printDst(bool val);
void ss_printPath(bool val);
void ss_printData(bool val);
void ss_printInfo(bool val);
void ss_sendMsg(void *_buffer, size_t length, AX25Ctx *ax25);
#endif