Add AX25 protocol decoder and test (preliminary).
[bertos.git] / bertos / net / ax25.h
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  * $WIZ$ module_name = "ax25"
41  * $WIZ$ module_configuration = "bertos/cfg/cfg_ax25.h"
42  * $WIZ$ module_depends = "kfile"
43  */
44
45
46 #ifndef NET_AX25_H
47 #define NET_AX25_H
48
49 #include "cfg/cfg_ax25.h"
50
51 #include <cfg/compiler.h>
52 #include <kern/kfile.h>
53
54 /**
55  * Maximum size of a AX25 frame.
56  */
57 #define AX25_MIN_FRAME_LEN 18
58
59 /**
60  * CRC computation on correct AX25 packets should
61  * give this result (don't ask why).
62  */
63 #define AX25_CRC_CORRECT 0xF0B8
64
65 struct AX25Msg; // fwd declaration
66
67 /**
68  * Type for AX25 messages callback.
69  */
70 typedef void (*ax25_callback_t)(struct AX25Msg *msg);
71
72
73 /**
74  * AX25 Protocol context.
75  */
76 typedef struct AX25Ctx
77 {
78         uint8_t buf[CONFIG_AX25_FRAME_BUF_LEN]; ///< buffer for received chars
79         KFile *ch;        ///< KFile used to access the physical medium
80         size_t frm_len;   ///< received frame length.
81         uint16_t crc_in;  ///< CRC for current received frame
82         uint16_t crc_out; ///< CRC of current sent frame
83         ax25_callback_t hook; ///< Hook function to be called when a message is received
84         bool sync;   ///< True if we have received a HDLC flag.
85         bool escape; ///< True when we have to escape the following char.
86 } AX25Ctx;
87
88
89 /**
90  * AX25 Call sign.
91  */
92 typedef struct AX25Call
93 {
94         char call[6]; ///< Call string, max 6 character
95         uint8_t ssid; ///< SSID (secondary station ID) for the call
96 } AX25Call;
97
98 /**
99  * Maximum number of Repeaters in a AX25 message.
100  */
101 #define AX25_MAX_RPT 8
102
103
104 /**
105  * AX25 Message.
106  * Used to handle AX25 sent/received messages.
107  */
108 typedef struct AX25Msg
109 {
110         AX25Call src;  ///< Source adress
111         AX25Call dst;  ///< Destination address
112         #if CONFIG_AX25_RPT_LST
113         AX25Call rpt_lst[AX25_MAX_RPT]; ///< List of repeaters
114         uint8_t rpt_cnt; ///< Number of repeaters in this message
115         #endif
116         uint16_t ctrl; ///< AX25 control field
117         uint8_t pid;   ///< AX25 PID field
118         uint8_t *info; ///< Pointer to the info field (payload) of the message
119         size_t len;    ///< Payload length
120 } AX25Msg;
121
122 #define AX25_CTRL_UI      0x03
123 #define AX25_PID_NOLAYER3 0xF0
124
125 /**
126  * HDLC flags
127  * These should be moved in
128  * a separated HDLC related file one day...
129  * \{
130  */
131 #define HDLC_FLAG  0x7E
132 #define HDLC_RESET 0x7F
133 #define AX25_ESC   0x1B
134 /* \} */
135
136
137 /**
138  * Check if there are any AX25 messages to be processed.
139  * This function read available characters from the medium and search for
140  * any AX25 messages.
141  * If a message is found it is decoded and the linked callback executed.
142  * This function may be blocking if there are no available chars and the KFile
143  * used in \a ctx to access the medium is configured in blocking mode.
144  *
145  * \param ctx AX25 context to operate on.
146  */
147 void ax25_poll(AX25Ctx *ctx);
148
149 /**
150  * Init the AX25 protocol decoder.
151  *
152  * \param ctx AX25 context to init.
153  * \param channel Used to gain access to the physical medium
154  * \param hook Callback function called when a message is received
155  */
156 void ax25_init(AX25Ctx *ctx, KFile *channel, ax25_callback_t hook);
157
158 #endif /* NET_AX25_H */