Do not assert on received data.
[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 <algo/crc_ccitt.h>
46
47 #define LOG_LEVEL  AX25_LOG_LEVEL
48 #define LOG_FORMAT AX25_LOG_FORMAT
49 #include <cfg/log.h>
50
51 #include <string.h> //memset, memcmp
52 #include <ctype.h>  //isalnum, toupper
53
54 #define DECODE_CALL(buf, addr) \
55         for (unsigned i = 0; i < sizeof((addr)); i++) \
56         { \
57                 (addr)[i] = *(buf)++ >> 1; \
58         }
59
60 static void ax25_decode(AX25Ctx *ctx)
61 {
62         AX25Msg msg;
63         uint8_t *buf = ctx->buf;
64
65         DECODE_CALL(buf, msg.dst.call);
66         msg.dst.ssid = (*buf++ >> 1) & 0x0F;
67
68         DECODE_CALL(buf, msg.src.call);
69         msg.src.ssid = (*buf >> 1) & 0x0F;
70
71         LOG_INFO("SRC[%.6s-%d], DST[%.6s-%d]\n", msg.src.call, msg.src.ssid, msg.dst.call, msg.dst.ssid);
72
73         /* Repeater addresses */
74         #if CONFIG_AX25_RPT_LST
75                 for (msg.rpt_cnt = 0; !(*buf++ & 0x01) && (msg.rpt_cnt < countof(msg.rpt_lst)); msg.rpt_cnt++)
76                 {
77                         DECODE_CALL(buf, msg.rpt_lst[msg.rpt_cnt].call);
78                         msg.rpt_lst[msg.rpt_cnt].ssid = (*buf >> 1) & 0x0F;
79                         LOG_INFO("RPT%d[%.6s-%d]\n", msg.rpt_cnt, msg.rpt_lst[msg.rpt_cnt].call, msg.rpt_lst[msg.rpt_cnt].ssid);
80                 }
81         #else
82                 while (!(*buf++ & 0x01))
83                 {
84                         char rpt[6];
85                         uint8_t ssid;
86                         DECODE_CALL(buf, rpt);
87                         ssid = (*buf >> 1) & 0x0F;
88                         LOG_INFO("RPT[%.6s-%d]\n", rpt, ssid);
89                 }
90         #endif
91
92         msg.ctrl = *buf++;
93         if (msg.ctrl != AX25_CTRL_UI)
94         {
95                 LOG_WARN("Only UI frames are handled, got [%02X]\n", msg.ctrl);
96                 return;
97         }
98
99         msg.pid = *buf++;
100         if (msg.pid != AX25_PID_NOLAYER3)
101         {
102                 LOG_WARN("Only frames without layer3 protocol are handled, got [%02X]\n", msg.pid);
103                 return;
104         }
105
106         msg.len = ctx->frm_len - 2 - (buf - ctx->buf);
107         msg.info = buf;
108         LOG_INFO("DATA: %.*s\n", msg.len, msg.info);
109
110         if (ctx->hook)
111                 ctx->hook(&msg);
112 }
113
114
115 /**
116  * Check if there are any AX25 messages to be processed.
117  * This function read available characters from the medium and search for
118  * any AX25 messages.
119  * If a message is found it is decoded and the linked callback executed.
120  * This function may be blocking if there are no available chars and the KFile
121  * used in \a ctx to access the medium is configured in blocking mode.
122  *
123  * \param ctx AX25 context to operate on.
124  */
125 void ax25_poll(AX25Ctx *ctx)
126 {
127         int c;
128
129         while ((c = kfile_getc(ctx->ch)) != EOF)
130         {
131                 if (!ctx->escape && c == HDLC_FLAG)
132                 {
133                         if (ctx->frm_len >= AX25_MIN_FRAME_LEN)
134                         {
135                                 if (ctx->crc_in == AX25_CRC_CORRECT)
136                                 {
137                                         LOG_INFO("Frame found!\n");
138                                         ax25_decode(ctx);
139                                 }
140                                 else
141                                 {
142                                         LOG_INFO("CRC error, computed [%04X]\n", ctx->crc_in);
143                                 }
144                         }
145                         ctx->sync = true;
146                         ctx->crc_in = CRC_CCITT_INIT_VAL;
147                         ctx->frm_len = 0;
148                         continue;
149                 }
150
151                 if (!ctx->escape && c == HDLC_RESET)
152                 {
153                         LOG_INFO("HDLC reset\n");
154                         ctx->sync = false;
155                         continue;
156                 }
157
158                 if (!ctx->escape && c == AX25_ESC)
159                 {
160                         ctx->escape = true;
161                         continue;
162                 }
163
164                 if (ctx->sync)
165                 {
166                         if (ctx->frm_len < CONFIG_AX25_FRAME_BUF_LEN)
167                         {
168                                 ctx->buf[ctx->frm_len++] = c;
169                                 ctx->crc_in = updcrc_ccitt(c, ctx->crc_in);
170                         }
171                         else
172                         {
173                                 LOG_INFO("Buffer overrun");
174                                 ctx->sync = false;
175                         }
176                 }
177                 ctx->escape = false;
178         }
179
180         if (kfile_error(ctx->ch))
181         {
182                 LOG_ERR("Channel error [%04x]\n", kfile_error(ctx->ch));
183                 kfile_clearerr(ctx->ch);
184         }
185 }
186
187 static void ax25_putchar(AX25Ctx *ctx, uint8_t c)
188 {
189         if (c == HDLC_FLAG || c == HDLC_RESET
190                 || c == AX25_ESC)
191                 kfile_putc(AX25_ESC, ctx->ch);
192         ctx->crc_out = updcrc_ccitt(c, ctx->crc_out);
193         kfile_putc(c, ctx->ch);
194 }
195
196 static void ax25_sendCall(AX25Ctx *ctx, const AX25Call *addr)
197 {
198         unsigned len = MIN(sizeof(addr->call), strlen(addr->call));
199
200         for (unsigned i = 0; i < len; i++)
201         {
202                 uint8_t c = addr->call[i];
203                 ASSERT(isalnum(c) || c == ' ');
204                 c = toupper(c);
205                 ax25_putchar(ctx, c << 1);
206         }
207
208         /* Fill with spaces the rest of the CALL if it's shorter */
209         if (len < sizeof(addr->call))
210                 for (unsigned i = 0; i < sizeof(addr->call) - len; i++)
211                         ax25_putchar(ctx, ' ' << 1);
212 }
213
214 /**
215  * Send an AX25 frame on the channel.
216  * \param ctx AX25 context to operate on.
217  * \param dst the destination callsign for the frame, \see AX25_CALL
218  *        for a handy way to create a callsign on the fly.
219  * \param src the source callsign for the frame, \see AX25_CALL
220  *        for a handy way to create a callsign on the fly.
221  * \param _buf payload buffer.
222  * \param len length of the payload.
223  */
224 void ax25_send(AX25Ctx *ctx, const AX25Call *dst, const AX25Call *src, const void *_buf, size_t len)
225 {
226         const uint8_t *buf = (const uint8_t *)_buf;
227
228         ctx->crc_out = CRC_CCITT_INIT_VAL;
229         kfile_putc(HDLC_FLAG, ctx->ch);
230
231         ax25_sendCall(ctx, dst);
232         ax25_putchar(ctx, dst->ssid << 1);
233
234         ax25_sendCall(ctx, src);
235         ax25_putchar(ctx, (src->ssid << 1) | 0x01);
236         ax25_putchar(ctx, AX25_CTRL_UI);
237         ax25_putchar(ctx, AX25_PID_NOLAYER3);
238
239         while (len--)
240                 ax25_putchar(ctx, *buf++);
241
242         /*
243          * According to AX25 protocol,
244          * CRC is sent in reverse order!
245          */
246         uint8_t crcl = (ctx->crc_out & 0xff) ^ 0xff;
247         uint8_t crch = (ctx->crc_out >> 8) ^ 0xff;
248         ax25_putchar(ctx, crcl);
249         ax25_putchar(ctx, crch);
250
251         ASSERT(ctx->crc_out == AX25_CRC_CORRECT);
252
253         kfile_putc(HDLC_FLAG, ctx->ch);
254 }
255
256
257 /**
258  * Init the AX25 protocol decoder.
259  *
260  * \param ctx AX25 context to init.
261  * \param channel Used to gain access to the physical medium
262  * \param hook Callback function called when a message is received
263  */
264 void ax25_init(AX25Ctx *ctx, KFile *channel, ax25_callback_t hook)
265 {
266         ASSERT(ctx);
267         ASSERT(channel);
268
269         memset(ctx, 0, sizeof(*ctx));
270         ctx->ch = channel;
271         ctx->hook = hook;
272         ctx->crc_in = ctx->crc_out = CRC_CCITT_INIT_VAL;
273 }