00001
00042 #include "ax25.h"
00043 #include "cfg/cfg_ax25.h"
00044
00045 #include <algo/crc_ccitt.h>
00046
00047 #define LOG_LEVEL AX25_LOG_LEVEL
00048 #define LOG_FORMAT AX25_LOG_FORMAT
00049 #include <cfg/log.h>
00050
00051 #include <string.h>
00052 #include <ctype.h>
00053
00054 #define DECODE_CALL(buf, addr) \
00055 for (unsigned i = 0; i < sizeof((addr)); i++) \
00056 { \
00057 (addr)[i] = *(buf)++ >> 1; \
00058 }
00059
00060 static void ax25_decode(AX25Ctx *ctx)
00061 {
00062 AX25Msg msg;
00063 uint8_t *buf = ctx->buf;
00064
00065 DECODE_CALL(buf, msg.dst.call);
00066 msg.dst.ssid = (*buf++ >> 1) & 0x0F;
00067
00068 DECODE_CALL(buf, msg.src.call);
00069 msg.src.ssid = (*buf >> 1) & 0x0F;
00070
00071 LOG_INFO("SRC[%.6s-%d], DST[%.6s-%d]\n", msg.src.call, msg.src.ssid, msg.dst.call, msg.dst.ssid);
00072
00073
00074 #if CONFIG_AX25_RPT_LST
00075 for (msg.rpt_cnt = 0; !(*buf++ & 0x01) && (msg.rpt_cnt < countof(msg.rpt_lst)); msg.rpt_cnt++)
00076 {
00077 DECODE_CALL(buf, msg.rpt_lst[msg.rpt_cnt].call);
00078 msg.rpt_lst[msg.rpt_cnt].ssid = (*buf >> 1) & 0x0F;
00079 LOG_INFO("RPT%d[%.6s-%d]\n", msg.rpt_cnt, msg.rpt_lst[msg.rpt_cnt].call, msg.rpt_lst[msg.rpt_cnt].ssid);
00080 }
00081 #else
00082 while (!(*buf++ & 0x01))
00083 {
00084 char rpt[6];
00085 uint8_t ssid;
00086 DECODE_CALL(buf, rpt);
00087 ssid = (*buf >> 1) & 0x0F;
00088 LOG_INFO("RPT[%.6s-%d]\n", rpt, ssid);
00089 }
00090 #endif
00091
00092 msg.ctrl = *buf++;
00093 if (msg.ctrl != AX25_CTRL_UI)
00094 {
00095 LOG_WARN("Only UI frames are handled, got [%02X]\n", msg.ctrl);
00096 return;
00097 }
00098
00099 msg.pid = *buf++;
00100 if (msg.pid != AX25_PID_NOLAYER3)
00101 {
00102 LOG_WARN("Only frames without layer3 protocol are handled, got [%02X]\n", msg.pid);
00103 return;
00104 }
00105
00106 msg.len = ctx->frm_len - 2 - (buf - ctx->buf);
00107 msg.info = buf;
00108 LOG_INFO("DATA: %.*s\n", msg.len, msg.info);
00109
00110 if (ctx->hook)
00111 ctx->hook(&msg);
00112 }
00113
00114
00125 void ax25_poll(AX25Ctx *ctx)
00126 {
00127 int c;
00128
00129 while ((c = kfile_getc(ctx->ch)) != EOF)
00130 {
00131 if (!ctx->escape && c == HDLC_FLAG)
00132 {
00133 if (ctx->frm_len >= AX25_MIN_FRAME_LEN)
00134 {
00135 if (ctx->crc_in == AX25_CRC_CORRECT)
00136 {
00137 LOG_INFO("Frame found!\n");
00138 ax25_decode(ctx);
00139 }
00140 else
00141 {
00142 LOG_INFO("CRC error, computed [%04X]\n", ctx->crc_in);
00143 }
00144 }
00145 ctx->sync = true;
00146 ctx->crc_in = CRC_CCITT_INIT_VAL;
00147 ctx->frm_len = 0;
00148 continue;
00149 }
00150
00151 if (!ctx->escape && c == HDLC_RESET)
00152 {
00153 LOG_INFO("HDLC reset\n");
00154 ctx->sync = false;
00155 continue;
00156 }
00157
00158 if (!ctx->escape && c == AX25_ESC)
00159 {
00160 ctx->escape = true;
00161 continue;
00162 }
00163
00164 if (ctx->sync)
00165 {
00166 if (ctx->frm_len < CONFIG_AX25_FRAME_BUF_LEN)
00167 {
00168 ctx->buf[ctx->frm_len++] = c;
00169 ctx->crc_in = updcrc_ccitt(c, ctx->crc_in);
00170 }
00171 else
00172 {
00173 LOG_INFO("Buffer overrun");
00174 ctx->sync = false;
00175 }
00176 }
00177 ctx->escape = false;
00178 }
00179
00180 if (kfile_error(ctx->ch))
00181 {
00182 LOG_ERR("Channel error [%04x]\n", kfile_error(ctx->ch));
00183 kfile_clearerr(ctx->ch);
00184 }
00185 }
00186
00187 static void ax25_putchar(AX25Ctx *ctx, uint8_t c)
00188 {
00189 if (c == HDLC_FLAG || c == HDLC_RESET
00190 || c == AX25_ESC)
00191 kfile_putc(AX25_ESC, ctx->ch);
00192 ctx->crc_out = updcrc_ccitt(c, ctx->crc_out);
00193 kfile_putc(c, ctx->ch);
00194 }
00195
00196 static void ax25_sendCall(AX25Ctx *ctx, const AX25Call *addr)
00197 {
00198 unsigned len = MIN(sizeof(addr->call), strlen(addr->call));
00199
00200 for (unsigned i = 0; i < len; i++)
00201 {
00202 uint8_t c = addr->call[i];
00203 ASSERT(isalnum(c) || c == ' ');
00204 c = toupper(c);
00205 ax25_putchar(ctx, c << 1);
00206 }
00207
00208
00209 if (len < sizeof(addr->call))
00210 for (unsigned i = 0; i < sizeof(addr->call) - len; i++)
00211 ax25_putchar(ctx, ' ' << 1);
00212 }
00213
00224 void ax25_send(AX25Ctx *ctx, const AX25Call *dst, const AX25Call *src, const void *_buf, size_t len)
00225 {
00226 const uint8_t *buf = (const uint8_t *)_buf;
00227
00228 ctx->crc_out = CRC_CCITT_INIT_VAL;
00229 kfile_putc(HDLC_FLAG, ctx->ch);
00230
00231 ax25_sendCall(ctx, dst);
00232 ax25_putchar(ctx, dst->ssid << 1);
00233
00234 ax25_sendCall(ctx, src);
00235 ax25_putchar(ctx, (src->ssid << 1) | 0x01);
00236 ax25_putchar(ctx, AX25_CTRL_UI);
00237 ax25_putchar(ctx, AX25_PID_NOLAYER3);
00238
00239 while (len--)
00240 ax25_putchar(ctx, *buf++);
00241
00242
00243
00244
00245
00246 uint8_t crcl = (ctx->crc_out & 0xff) ^ 0xff;
00247 uint8_t crch = (ctx->crc_out >> 8) ^ 0xff;
00248 ax25_putchar(ctx, crcl);
00249 ax25_putchar(ctx, crch);
00250
00251 ASSERT(ctx->crc_out == AX25_CRC_CORRECT);
00252
00253 kfile_putc(HDLC_FLAG, ctx->ch);
00254 }
00255
00256
00264 void ax25_init(AX25Ctx *ctx, KFile *channel, ax25_callback_t hook)
00265 {
00266 ASSERT(ctx);
00267 ASSERT(channel);
00268
00269 memset(ctx, 0, sizeof(*ctx));
00270 ctx->ch = channel;
00271 ctx->hook = hook;
00272 ctx->crc_in = ctx->crc_out = CRC_CCITT_INIT_VAL;
00273 }