AX25:add new print function compatible with TNC-2 format.
[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                 char c = (*(buf)++ >> 1); \
58                 (addr)[i] = (c == ' ') ? '\x0' : c; \
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         msg.dst.ssid = (*buf++ >> 1) & 0x0F;
68
69         DECODE_CALL(buf, msg.src.call);
70         msg.src.ssid = (*buf >> 1) & 0x0F;
71
72         LOG_INFO("SRC[%.6s-%d], DST[%.6s-%d]\n", msg.src.call, msg.src.ssid, msg.dst.call, msg.dst.ssid);
73
74         /* Repeater addresses */
75         #if CONFIG_AX25_RPT_LST
76                 for (msg.rpt_cnt = 0; !(*buf++ & 0x01) && (msg.rpt_cnt < countof(msg.rpt_lst)); msg.rpt_cnt++)
77                 {
78                         DECODE_CALL(buf, msg.rpt_lst[msg.rpt_cnt].call);
79                         msg.rpt_lst[msg.rpt_cnt].ssid = (*buf >> 1) & 0x0F;
80                         LOG_INFO("RPT%d[%.6s-%d]\n", msg.rpt_cnt, msg.rpt_lst[msg.rpt_cnt].call, msg.rpt_lst[msg.rpt_cnt].ssid);
81                 }
82         #else
83                 while (!(*buf++ & 0x01))
84                 {
85                         char rpt[6];
86                         uint8_t ssid;
87                         DECODE_CALL(buf, rpt);
88                         ssid = (*buf >> 1) & 0x0F;
89                         LOG_INFO("RPT[%.6s-%d]\n", rpt, ssid);
90                 }
91         #endif
92
93         msg.ctrl = *buf++;
94         if (msg.ctrl != AX25_CTRL_UI)
95         {
96                 LOG_WARN("Only UI frames are handled, got [%02X]\n", msg.ctrl);
97                 return;
98         }
99
100         msg.pid = *buf++;
101         if (msg.pid != AX25_PID_NOLAYER3)
102         {
103                 LOG_WARN("Only frames without layer3 protocol are handled, got [%02X]\n", msg.pid);
104                 return;
105         }
106
107         msg.len = ctx->frm_len - 2 - (buf - ctx->buf);
108         msg.info = buf;
109         LOG_INFO("DATA: %.*s\n", msg.len, msg.info);
110
111         if (ctx->hook)
112                 ctx->hook(&msg);
113 }
114
115
116 /**
117  * Check if there are any AX25 messages to be processed.
118  * This function read available characters from the medium and search for
119  * any AX25 messages.
120  * If a message is found it is decoded and the linked callback executed.
121  * This function may be blocking if there are no available chars and the KFile
122  * used in \a ctx to access the medium is configured in blocking mode.
123  *
124  * \param ctx AX25 context to operate on.
125  */
126 void ax25_poll(AX25Ctx *ctx)
127 {
128         int c;
129
130         while ((c = kfile_getc(ctx->ch)) != EOF)
131         {
132                 if (!ctx->escape && c == HDLC_FLAG)
133                 {
134                         if (ctx->frm_len >= AX25_MIN_FRAME_LEN)
135                         {
136                                 if (ctx->crc_in == AX25_CRC_CORRECT)
137                                 {
138                                         LOG_INFO("Frame found!\n");
139                                         ax25_decode(ctx);
140                                 }
141                                 else
142                                 {
143                                         LOG_INFO("CRC error, computed [%04X]\n", ctx->crc_in);
144                                 }
145                         }
146                         ctx->sync = true;
147                         ctx->crc_in = CRC_CCITT_INIT_VAL;
148                         ctx->frm_len = 0;
149                         continue;
150                 }
151
152                 if (!ctx->escape && c == HDLC_RESET)
153                 {
154                         LOG_INFO("HDLC reset\n");
155                         ctx->sync = false;
156                         continue;
157                 }
158
159                 if (!ctx->escape && c == AX25_ESC)
160                 {
161                         ctx->escape = true;
162                         continue;
163                 }
164
165                 if (ctx->sync)
166                 {
167                         if (ctx->frm_len < CONFIG_AX25_FRAME_BUF_LEN)
168                         {
169                                 ctx->buf[ctx->frm_len++] = c;
170                                 ctx->crc_in = updcrc_ccitt(c, ctx->crc_in);
171                         }
172                         else
173                         {
174                                 LOG_INFO("Buffer overrun");
175                                 ctx->sync = false;
176                         }
177                 }
178                 ctx->escape = false;
179         }
180
181         if (kfile_error(ctx->ch))
182         {
183                 LOG_ERR("Channel error [%04x]\n", kfile_error(ctx->ch));
184                 kfile_clearerr(ctx->ch);
185         }
186 }
187
188 static void ax25_putchar(AX25Ctx *ctx, uint8_t c)
189 {
190         if (c == HDLC_FLAG || c == HDLC_RESET
191                 || c == AX25_ESC)
192                 kfile_putc(AX25_ESC, ctx->ch);
193         ctx->crc_out = updcrc_ccitt(c, ctx->crc_out);
194         kfile_putc(c, ctx->ch);
195 }
196
197 static void ax25_sendCall(AX25Ctx *ctx, const AX25Call *addr, bool last)
198 {
199         unsigned len = MIN(sizeof(addr->call), strlen(addr->call));
200
201         for (unsigned i = 0; i < len; i++)
202         {
203                 uint8_t c = addr->call[i];
204                 ASSERT(isalnum(c) || c == ' ');
205                 c = toupper(c);
206                 ax25_putchar(ctx, c << 1);
207         }
208
209         /* Fill with spaces the rest of the CALL if it's shorter */
210         if (len < sizeof(addr->call))
211                 for (unsigned i = 0; i < sizeof(addr->call) - len; i++)
212                         ax25_putchar(ctx, ' ' << 1);
213
214         /* The bit0 of last call SSID should be set to 1 */
215         uint8_t ssid = addr->ssid << 1 | (last ? 0x01 : 0);
216         ax25_putchar(ctx, ssid);
217 }
218
219 /**
220  * Send an AX25 frame on the channel through a specific path.
221  * \param ctx AX25 context to operate on.
222  * \param path An array of callsigns used as path, \see AX25_PATH for
223  *        an handy way to create a path.
224  * \param path_len callsigns path lenght.
225  * \param _buf payload buffer.
226  * \param len length of the payload.
227  */
228 void ax25_sendVia(AX25Ctx *ctx, const AX25Call *path, size_t path_len, const void *_buf, size_t len)
229 {
230         const uint8_t *buf = (const uint8_t *)_buf;
231         ASSERT(path);
232         ASSERT(path_len >= 2);
233
234         ctx->crc_out = CRC_CCITT_INIT_VAL;
235         kfile_putc(HDLC_FLAG, ctx->ch);
236
237
238         /* Send call */
239         for (size_t i = 0; i < path_len; i++)
240                 ax25_sendCall(ctx, &path[i], (i == path_len - 1));
241
242         ax25_putchar(ctx, AX25_CTRL_UI);
243         ax25_putchar(ctx, AX25_PID_NOLAYER3);
244
245         while (len--)
246                 ax25_putchar(ctx, *buf++);
247
248         /*
249          * According to AX25 protocol,
250          * CRC is sent in reverse order!
251          */
252         uint8_t crcl = (ctx->crc_out & 0xff) ^ 0xff;
253         uint8_t crch = (ctx->crc_out >> 8) ^ 0xff;
254         ax25_putchar(ctx, crcl);
255         ax25_putchar(ctx, crch);
256
257         ASSERT(ctx->crc_out == AX25_CRC_CORRECT);
258
259         kfile_putc(HDLC_FLAG, ctx->ch);
260 }
261
262 static void print_call(KFile *ch, const AX25Call *call)
263 {
264         kfile_printf(ch, "%.6s", call->call);
265         if (call->ssid)
266                 kfile_printf(ch, "-%d", call->ssid);
267 }
268         
269 /**
270  * Print a AX25 message in TNC-2 packet monitor format.
271  * \param ch a kfile channel where the message will be printed.
272  * \param msg the message to be printed.
273  */
274 void ax25_print(KFile *ch, const AX25Msg *msg)
275 {
276         print_call(ch, &msg->src);
277         kfile_putc('>', ch);
278         print_call(ch, &msg->dst);      
279         
280         #if CONFIG_AX25_RPT_LST
281         for (int i = 0; i < msg->rpt_cnt; i++)
282         {
283                 kfile_putc(',', ch);
284                 print_call(ch, &msg->rpt_lst[i]);
285                 // TODO: add * to the trasmitting digi
286         }
287         #endif
288
289         kfile_printf(ch, ":%.*s\n", msg->len, msg->info);
290 }
291
292
293 /**
294  * Init the AX25 protocol decoder.
295  *
296  * \param ctx AX25 context to init.
297  * \param channel Used to gain access to the physical medium
298  * \param hook Callback function called when a message is received
299  */
300 void ax25_init(AX25Ctx *ctx, KFile *channel, ax25_callback_t hook)
301 {
302         ASSERT(ctx);
303         ASSERT(channel);
304
305         memset(ctx, 0, sizeof(*ctx));
306         ctx->ch = channel;
307         ctx->hook = hook;
308         ctx->crc_in = ctx->crc_out = CRC_CCITT_INIT_VAL;
309 }