RadioLib
Universal wireless communication library for Arduino
AX25.h
1 #if !defined(_RADIOLIB_RADIOLIB_AX25_H)
2 #define _RADIOLIB_RADIOLIB_AX25_H
3 
4 #include "../../TypeDef.h"
5 
6 #if !defined(RADIOLIB_EXCLUDE_AX25)
7 
8 #include "../PhysicalLayer/PhysicalLayer.h"
9 #include "../AFSK/AFSK.h"
10 #include "../BellModem/BellModem.h"
11 
12 // macros to access bits in byte array, from http://www.mathcs.emory.edu/~cheung/Courses/255/Syllabus/1-C-intro/bit-array.html
13 #define SET_BIT_IN_ARRAY(A, k) ( A[(k/8)] |= (1 << (k%8)) )
14 #define CLEAR_BIT_IN_ARRAY(A, k) ( A[(k/8)] &= ~(1 << (k%8)) )
15 #define TEST_BIT_IN_ARRAY(A, k) ( A[(k/8)] & (1 << (k%8)) )
16 #define GET_BIT_IN_ARRAY(A, k) ( (A[(k/8)] & (1 << (k%8))) ? 1 : 0 )
17 
18 // CRC-CCITT calculation macros
19 #define XOR(A, B) ( ((A) || (B)) && !((A) && (B)) )
20 #define CRC_CCITT_POLY 0x1021 // generator polynomial
21 #define CRC_CCITT_POLY_REVERSED 0x8408 // CRC_CCITT_POLY in reversed bit order
22 #define CRC_CCITT_INIT 0xFFFF // initial value
23 
24 // maximum callsign length in bytes
25 #define RADIOLIB_AX25_MAX_CALLSIGN_LEN 6
26 
27 // flag field MSB LSB DESCRIPTION
28 #define RADIOLIB_AX25_FLAG 0b01111110 // 7 0 AX.25 frame start/end flag
29 
30 // address field
31 #define RADIOLIB_AX25_SSID_COMMAND_DEST 0b10000000 // 7 7 frame type: command (set in destination SSID)
32 #define RADIOLIB_AX25_SSID_COMMAND_SOURCE 0b00000000 // 7 7 command (set in source SSID)
33 #define RADIOLIB_AX25_SSID_RESPONSE_DEST 0b00000000 // 7 7 response (set in destination SSID)
34 #define RADIOLIB_AX25_SSID_RESPONSE_SOURCE 0b10000000 // 7 7 response (set in source SSID)
35 #define RADIOLIB_AX25_SSID_HAS_NOT_BEEN_REPEATED 0b00000000 // 7 7 not repeated yet (set in repeater SSID)
36 #define RADIOLIB_AX25_SSID_HAS_BEEN_REPEATED 0b10000000 // 7 7 repeated (set in repeater SSID)
37 #define RADIOLIB_AX25_SSID_RESERVED_BITS 0b01100000 // 6 5 reserved bits in SSID
38 #define RADIOLIB_AX25_SSID_HDLC_EXTENSION_CONTINUE 0b00000000 // 0 0 HDLC extension bit: next octet contains more address information
39 #define RADIOLIB_AX25_SSID_HDLC_EXTENSION_END 0b00000001 // 0 0 address field end
40 
41 // control field
42 #define RADIOLIB_AX25_CONTROL_U_SET_ASYNC_BAL_MODE 0b01101100 // 7 2 U frame type: set asynchronous balanced mode (connect request)
43 #define RADIOLIB_AX25_CONTROL_U_SET_ASYNC_BAL_MODE_EXT 0b00101100 // 7 2 set asynchronous balanced mode extended (connect request with module 128)
44 #define RADIOLIB_AX25_CONTROL_U_DISCONNECT 0b01000000 // 7 2 disconnect request
45 #define RADIOLIB_AX25_CONTROL_U_DISCONNECT_MODE 0b00001100 // 7 2 disconnect mode (system busy or disconnected)
46 #define RADIOLIB_AX25_CONTROL_U_UNNUMBERED_ACK 0b01100000 // 7 2 unnumbered acknowledge
47 #define RADIOLIB_AX25_CONTROL_U_FRAME_REJECT 0b10000100 // 7 2 frame reject
48 #define RADIOLIB_AX25_CONTROL_U_UNNUMBERED_INFORMATION 0b00000000 // 7 2 unnumbered information
49 #define RADIOLIB_AX25_CONTROL_U_EXHANGE_IDENTIFICATION 0b10101100 // 7 2 exchange ID
50 #define RADIOLIB_AX25_CONTROL_U_TEST 0b11100000 // 7 2 test
51 #define RADIOLIB_AX25_CONTROL_POLL_FINAL_ENABLED 0b00010000 // 4 4 control field poll/final bit: enabled
52 #define RADIOLIB_AX25_CONTROL_POLL_FINAL_DISABLED 0b00000000 // 4 4 disabled
53 #define RADIOLIB_AX25_CONTROL_S_RECEIVE_READY 0b00000000 // 3 2 S frame type: receive ready (system ready to receive)
54 #define RADIOLIB_AX25_CONTROL_S_RECEIVE_NOT_READY 0b00000100 // 3 2 receive not ready (TNC buffer full)
55 #define RADIOLIB_AX25_CONTROL_S_REJECT 0b00001000 // 3 2 reject (out of sequence or duplicate)
56 #define RADIOLIB_AX25_CONTROL_S_SELECTIVE_REJECT 0b00001100 // 3 2 selective reject (single frame repeat request)
57 #define RADIOLIB_AX25_CONTROL_INFORMATION_FRAME 0b00000000 // 0 0 frame type: information (I frame)
58 #define RADIOLIB_AX25_CONTROL_SUPERVISORY_FRAME 0b00000001 // 1 0 supervisory (S frame)
59 #define RADIOLIB_AX25_CONTROL_UNNUMBERED_FRAME 0b00000011 // 1 0 unnumbered (U frame)
60 
61 // protocol identifier field
62 #define RADIOLIB_AX25_PID_ISO_8208 0x01
63 #define RADIOLIB_AX25_PID_TCP_IP_COMPRESSED 0x06
64 #define RADIOLIB_AX25_PID_TCP_IP_UNCOMPRESSED 0x07
65 #define RADIOLIB_AX25_PID_SEGMENTATION_FRAGMENT 0x08
66 #define RADIOLIB_AX25_PID_TEXNET_DATAGRAM_PROTOCOL 0xC3
67 #define RADIOLIB_AX25_PID_LINK_QUALITY_PROTOCOL 0xC4
68 #define RADIOLIB_AX25_PID_APPLETALK 0xCA
69 #define RADIOLIB_AX25_PID_APPLETALK_ARP 0xCB
70 #define RADIOLIB_AX25_PID_ARPA_INTERNET_PROTOCOL 0xCC
71 #define RADIOLIB_AX25_PID_ARPA_ADDRESS_RESOLUTION 0xCD
72 #define RADIOLIB_AX25_PID_FLEXNET 0xCE
73 #define RADIOLIB_AX25_PID_NET_ROM 0xCF
74 #define RADIOLIB_AX25_PID_NO_LAYER_3 0xF0
75 #define RADIOLIB_AX25_PID_ESCAPE_CHARACTER 0xFF
76 
81 class AX25Frame {
82  public:
86  char destCallsign[RADIOLIB_AX25_MAX_CALLSIGN_LEN + 1];
87 
91  uint8_t destSSID;
92 
96  char srcCallsign[RADIOLIB_AX25_MAX_CALLSIGN_LEN + 1];
97 
101  uint8_t srcSSID;
102 
106  uint8_t numRepeaters;
107 
111  uint8_t control;
112 
116  uint8_t protocolID;
117 
121  uint16_t infoLen;
122 
126  uint8_t rcvSeqNumber;
127 
131  uint16_t sendSeqNumber;
132 
133  #if !defined(RADIOLIB_STATIC_ONLY)
137  uint8_t* info;
138 
143 
147  uint8_t* repeaterSSIDs;
148  #else
152  uint8_t info[RADIOLIB_STATIC_ARRAY_SIZE];
153 
157  char repeaterCallsigns[8][RADIOLIB_AX25_MAX_CALLSIGN_LEN + 1];
158 
162  uint8_t repeaterSSIDs[8];
163  #endif
164 
173  AX25Frame(const char* destCallsign, uint8_t destSSID, const char* srcCallsign, uint8_t srcSSID, uint8_t control);
174 
185  AX25Frame(const char* destCallsign, uint8_t destSSID, const char* srcCallsign, uint8_t srcSSID, uint8_t control, uint8_t protocolID, const char* info);
186 
198  AX25Frame(const char* destCallsign, uint8_t destSSID, const char* srcCallsign, uint8_t srcSSID, uint8_t control, uint8_t protocolID, uint8_t* info, uint16_t infoLen);
199 
204  AX25Frame(const AX25Frame& frame);
205 
209  ~AX25Frame();
210 
215  AX25Frame& operator=(const AX25Frame& frame);
216 
224  int16_t setRepeaters(char** repeaterCallsigns, uint8_t* repeaterSSIDs, uint8_t numRepeaters);
225 
230  void setRecvSequence(uint8_t seqNumber);
231 
236  void setSendSequence(uint8_t seqNumber);
237 };
238 
243 class AX25Client {
244  public:
249  explicit AX25Client(PhysicalLayer* phy);
250 
251  #if !defined(RADIOLIB_EXCLUDE_AFSK)
256  explicit AX25Client(AFSKClient* audio);
257 
266  int16_t setCorrection(int16_t mark, int16_t space, float length = 1.0f);
267  #endif
268 
269  // basic methods
270 
280  int16_t begin(const char* srcCallsign, uint8_t srcSSID = 0x00, uint8_t preLen = 8);
281 
282  #if defined(RADIOLIB_BUILD_ARDUINO)
291  int16_t transmit(String& str, const char* destCallsign, uint8_t destSSID = 0x00);
292  #endif
293 
302  int16_t transmit(const char* str, const char* destCallsign, uint8_t destSSID = 0x00);
303 
309  int16_t sendFrame(AX25Frame* frame);
310 
311 #if !defined(RADIOLIB_GODMODE)
312  private:
313 #endif
314  friend class APRSClient;
315 
316  PhysicalLayer* phyLayer;
317  #if !defined(RADIOLIB_EXCLUDE_AFSK)
318  BellClient* bellModem;
319  #endif
320 
321  char sourceCallsign[RADIOLIB_AX25_MAX_CALLSIGN_LEN + 1] = {0, 0, 0, 0, 0, 0, 0};
322  uint8_t sourceSSID = 0;
323  uint16_t preambleLen = 0;
324 
325  static uint16_t getFrameCheckSequence(uint8_t* buff, size_t len);
326 
327  void getCallsign(char* buff);
328  uint8_t getSSID();
329 };
330 
331 #endif
332 
333 #endif
Client for audio-based transmissions. Requires Arduino tone() function, and a module capable of direc...
Definition: AFSK.h:16
Client for APRS communication.
Definition: APRS.h:65
Client for AX25 communication.
Definition: AX25.h:243
int16_t sendFrame(AX25Frame *frame)
Transmit arbitrary AX.25 frame.
Definition: AX25.cpp:217
int16_t setCorrection(int16_t mark, int16_t space, float length=1.0f)
Set AFSK tone correction offset. On some platforms, this is required to get the audio produced by the...
Definition: AX25.cpp:168
int16_t transmit(const char *str, const char *destCallsign, uint8_t destSSID=0x00)
Transmit unnumbered information (UI) frame.
Definition: AX25.cpp:206
int16_t begin(const char *srcCallsign, uint8_t srcSSID=0x00, uint8_t preLen=8)
Initialization method.
Definition: AX25.cpp:180
AX25Client(PhysicalLayer *phy)
Constructor for 2-FSK mode.
Definition: AX25.cpp:154
Abstraction of AX.25 frame format.
Definition: AX25.h:81
void setSendSequence(uint8_t seqNumber)
Method to set send sequence number.
Definition: AX25.cpp:150
AX25Frame(const char *destCallsign, uint8_t destSSID, const char *srcCallsign, uint8_t srcSSID, uint8_t control)
Overloaded constructor, for frames without info field.
Definition: AX25.cpp:5
char ** repeaterCallsigns
Array of repeater callsigns.
Definition: AX25.h:142
char srcCallsign[RADIOLIB_AX25_MAX_CALLSIGN_LEN+1]
Callsign of the source station.
Definition: AX25.h:96
void setRecvSequence(uint8_t seqNumber)
Method to set receive sequence number.
Definition: AX25.cpp:146
uint16_t sendSeqNumber
Send sequence number.
Definition: AX25.h:131
uint8_t srcSSID
SSID of the source station.
Definition: AX25.h:101
AX25Frame & operator=(const AX25Frame &frame)
Overload for assignment operator.
Definition: AX25.cpp:75
uint8_t control
The control field.
Definition: AX25.h:111
uint8_t * repeaterSSIDs
Array of repeater SSIDs.
Definition: AX25.h:147
uint16_t infoLen
Number of bytes in the information field.
Definition: AX25.h:121
int16_t setRepeaters(char **repeaterCallsigns, uint8_t *repeaterSSIDs, uint8_t numRepeaters)
Method to set the repeater callsigns and SSIDs.
Definition: AX25.cpp:110
char destCallsign[RADIOLIB_AX25_MAX_CALLSIGN_LEN+1]
Callsign of the destination station.
Definition: AX25.h:86
uint8_t * info
The info field.
Definition: AX25.h:137
uint8_t protocolID
The protocol identifier (PID) field.
Definition: AX25.h:116
~AX25Frame()
Default destructor.
Definition: AX25.cpp:57
uint8_t numRepeaters
Number of repeaters to be used.
Definition: AX25.h:106
uint8_t rcvSeqNumber
Receive sequence number.
Definition: AX25.h:126
uint8_t destSSID
SSID of the destination station.
Definition: AX25.h:91
Client for Bell modem communication. The public interface is the same as Arduino Serial.
Definition: BellModem.h:55
Provides common interface for protocols that run on LoRa/FSK modules, such as RTTY or LoRaWAN....
Definition: PhysicalLayer.h:15