-
Notifications
You must be signed in to change notification settings - Fork 7
/
ax25.cpp
214 lines (191 loc) · 5 KB
/
ax25.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* trackuino copyright (C) 2010 EA5HAV Javi
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "ax25.h"
#include "config.h"
#include "modem.h"
#if defined(ARDUINO) && ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
// Module globals
unsigned short int crc;
int ones_in_a_row;
// Module functions
static void
update_crc(unsigned char bit)
{
crc ^= bit;
if (crc & 1)
crc = (crc >> 1) ^ 0x8408; // X-modem CRC poly
else
crc = crc >> 1;
}
static void
send_byte(unsigned char byte)
{
int i;
for (i = 0; i < 8; i++) {
update_crc((byte >> i) & 1);
if ((byte >> i) & 1) {
// Next bit is a '1'
if (modem_packet_size >= MODEM_MAX_PACKET * 8) // Prevent buffer overrun
return;
modem_packet[modem_packet_size >> 3] |= (1 << (modem_packet_size & 7));
modem_packet_size++;
if (++ones_in_a_row < 5) continue;
}
// Next bit is a '0' or a zero padding after 5 ones in a row
if (modem_packet_size >= MODEM_MAX_PACKET * 8) // Prevent buffer overrun
return;
modem_packet[modem_packet_size >> 3] &= ~(1 << (modem_packet_size & 7));
modem_packet_size++;
ones_in_a_row = 0;
}
}
// Exported functions
void
ax25_send_byte(unsigned char byte)
{
// Wrap around send_byte, but prints debug info
send_byte(byte);
#ifdef DEBUG_AX25
Serial.print((char)byte);
#endif
}
void
ax25_send_sync()
{
unsigned char byte = 0x00;
int i;
for (i = 0; i < 8; i++, modem_packet_size++) {
if (modem_packet_size >= MODEM_MAX_PACKET * 8) // Prevent buffer overrun
return;
if ((byte >> i) & 1)
modem_packet[modem_packet_size >> 3] |= (1 << (modem_packet_size & 7));
else
modem_packet[modem_packet_size >> 3] &= ~(1 << (modem_packet_size & 7));
}
}
void
ax25_send_flag()
{
unsigned char byte = 0x7e;
int i;
for (i = 0; i < 8; i++, modem_packet_size++) {
if (modem_packet_size >= MODEM_MAX_PACKET * 8) // Prevent buffer overrun
return;
if ((byte >> i) & 1)
modem_packet[modem_packet_size >> 3] |= (1 << (modem_packet_size & 7));
else
modem_packet[modem_packet_size >> 3] &= ~(1 << (modem_packet_size & 7));
}
}
void
ax25_send_string(const char *string)
{
int i;
for (i = 0; string[i]; i++) {
ax25_send_byte(string[i]);
}
}
void
ax25_send_header(s_address addresses[], int num_addresses)
{
int i, j;
modem_packet_size = 0;
ones_in_a_row = 0;
crc = 0xffff;
// Send sync ("a bunch of 0s")
for (i = 0; i < 60; i++)
{
ax25_send_sync();
}
//start the actual frame. Send 3 of them (one empty frame and the real start)
for (i = 0; i < 3; i++)
{
ax25_send_flag();
}
for (i = 0; i < num_addresses; i++) {
// Transmit callsign
for (j = 0; addresses[i].callsign[j]; j++)
send_byte(addresses[i].callsign[j] << 1);
// Transmit pad
for ( ; j < 6; j++)
send_byte(' ' << 1);
// Transmit SSID. Termination signaled with last bit = 1
if (i == num_addresses - 1)
send_byte(('0' + addresses[i].ssid) << 1 | 1);
else
send_byte(('0' + addresses[i].ssid) << 1);
}
// Control field: 3 = APRS-UI frame
send_byte(0x03);
// Protocol ID: 0xf0 = no layer 3 data
send_byte(0xf0);
#ifdef DEBUG_AX25
// Print source callsign
Serial.println();
Serial.print(addresses[1].callsign);
if (addresses[1].ssid) {
Serial.print('-');
Serial.print((unsigned int)addresses[1].ssid);
}
Serial.print('>');
// Destination callsign
Serial.print(addresses[0].callsign);
if (addresses[0].ssid) {
Serial.print('-');
Serial.print((unsigned int)addresses[0].ssid);
}
for (i = 2; i < num_addresses; i++) {
Serial.print(',');
Serial.print(addresses[i].callsign);
if (addresses[i].ssid) {
Serial.print('-');
Serial.print((unsigned int)addresses[i].ssid);
}
}
Serial.print(':');
#endif
}
void
ax25_send_footer()
{
// Save the crc so that it can be treated it atomically
unsigned short int final_crc = crc;
// Send the CRC
send_byte(~(final_crc & 0xff));
final_crc >>= 8;
send_byte(~(final_crc & 0xff));
// Signal the end of frame
ax25_send_flag();
// Send sync ("a bunch of 0s")
// for (int i = 0; i < 30; i++)
// {
// ax25_send_sync();
// }
#ifdef DEBUG_AX25
Serial.println();
#endif
}
void
ax25_flush_frame()
{
// Key the transmitter and send the frame
modem_flush_frame();
}