Add AX25 protocol decoder and test (preliminary).
[bertos.git] / bertos / net / ax25.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2009 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  * \brief Simple AX25 data link layer implementation.
33  *
34  * For now, only UI frames without any Layer 3 protocol are handled.
35  * This however is enough to send/receive APRS packets.
36  *
37  * \version $Id$
38  * \author Francesco Sacchi <batt@develer.com>
39  *
40  */
41
42 #include "ax25.h"
43 #include "cfg/cfg_ax25.h"
44
45 #include <drv/afsk.h>
46 #include <algo/crc_ccitt.h>
47
48 #define LOG_LEVEL  AX25_LOG_LEVEL
49 #define LOG_FORMAT AX25_LOG_FORMAT
50 #include <cfg/log.h>
51
52 #include <string.h> //memset
53
54 #define DECODE_CALL(buf, addr) \
55         for (unsigned i = 0; i < sizeof((addr)); i++) \
56         { \
57                 ASSERT(!(*(buf) & 0x01)); \
58                 (addr)[i] = *(buf)++ >> 1; \
59         }
60
61 static void ax25_decode(AX25Ctx *ctx)
62 {
63         AX25Msg msg;
64         uint8_t *buf = ctx->buf;
65
66         DECODE_CALL(buf, msg.dst.call);
67         ASSERT(!(*buf & 0x01));
68         msg.dst.ssid = (*buf++ >> 1) & 0x0F;
69
70         DECODE_CALL(buf, msg.src.call);
71         msg.src.ssid = (*buf >> 1) & 0x0F;
72
73         LOG_INFO("SRC[%.6s-%d], DST[%.6s-%d]\n", msg.src.call, msg.src.ssid, msg.dst.call, msg.dst.ssid);
74
75         /* Repeater addresses */
76         #if CONFIG_AX25_RPT_LST
77                 for (msg.rpt_cnt = 0; !(*buf++ & 0x01) && (msg.rpt_cnt < countof(msg.rpt_lst)); msg.rpt_cnt++)
78                 {
79                         DECODE_CALL(buf, msg.rpt_lst[msg.rpt_cnt].call);
80                         msg.rpt_lst[msg.rpt_cnt].ssid = (*buf >> 1) & 0x0F;
81                         LOG_INFO("RPT%d[%.6s-%d]\n", msg.rpt_cnt, msg.rpt_lst[msg.rpt_cnt].call, msg.rpt_lst[msg.rpt_cnt].ssid);
82                 }
83         #else
84                 while (!(*buf++ & 0x01))
85                 {
86                         char rpt[6];
87                         uint8_t ssid;
88                         DECODE_CALL(buf, rpt);
89                         ssid = (*buf >> 1) & 0x0F;
90                         LOG_INFO("RPT[%.6s-%d]\n", rpt, ssid);
91                 }
92         #endif
93
94         msg.ctrl = *buf++;
95         if (msg.ctrl != AX25_CTRL_UI)
96         {
97                 LOG_INFO("Only UI frames are handled, got [%02X]\n", msg.ctrl);
98                 return;
99         }
100
101         msg.pid = *buf++;
102         if (msg.pid != AX25_PID_NOLAYER3)
103         {
104                 LOG_INFO("Only frames without layer3 protocol are handled, got [%02X]\n", msg.pid);
105                 return;
106         }
107
108         msg.len = ctx->frm_len - 2 - (buf - ctx->buf);
109         msg.info = buf;
110         LOG_INFO("DATA[%.*s]\n", msg.len, msg.info);
111
112         if (ctx->hook)
113                 ctx->hook(&msg);
114 }
115
116 void ax25_poll(AX25Ctx *ctx)
117 {
118         int c;
119
120         while ((c = kfile_getc(ctx->ch)) != EOF)
121         {
122                 if (!ctx->escape && c == HDLC_FLAG)
123                 {
124                         LOG_INFO("Frame start\n");
125                         if (ctx->frm_len >= AX25_MIN_FRAME_LEN)
126                         {
127                                 if (ctx->crc_in == AX25_CRC_CORRECT)
128                                 {
129                                         LOG_INFO("Frame found!\n");
130                                         ax25_decode(ctx);
131                                 }
132                                 else
133                                 {
134                                         LOG_INFO("CRC error, computed [%04X]\n", ctx->crc_in);
135                                 }
136                         }
137                         ctx->sync = true;
138                         ctx->crc_in = CRC_CCITT_INIT_VAL;
139                         ctx->frm_len = 0;
140                         continue;
141                 }
142
143                 if (!ctx->escape && c == HDLC_RESET)
144                 {
145                         LOG_INFO("HDLC reset\n");
146                         ctx->sync = false;
147                         continue;
148                 }
149
150                 if (!ctx->escape && c == AX25_ESC)
151                 {
152                         ctx->escape = true;
153                         continue;
154                 }
155
156                 if (ctx->sync)
157                 {
158                         if (ctx->frm_len < CONFIG_AX25_FRAME_BUF_LEN)
159                         {
160                                 ctx->buf[ctx->frm_len++] = c;
161                                 ctx->crc_in = updcrc_ccitt(c, ctx->crc_in);
162                         }
163                         else
164                         {
165                                 LOG_INFO("Buffer overrun");
166                                 ctx->sync = false;
167                         }
168                 }
169                 ctx->escape = false;
170         }
171
172         if (kfile_error(ctx->ch))
173         {
174                 LOG_ERR("Channel error [%04x]\n", kfile_error(ctx->ch));
175                 kfile_clearerr(ctx->ch);
176         }
177 }
178
179 void ax25_init(AX25Ctx *ctx, KFile *channel, ax25_callback_t hook)
180 {
181         ASSERT(ctx);
182         ASSERT(channel);
183
184         memset(ctx, 0, sizeof(*ctx));
185         ctx->ch = channel;
186         ctx->hook = hook;
187         ctx->crc_in = ctx->crc_out = CRC_CCITT_INIT_VAL;
188 }