-
Notifications
You must be signed in to change notification settings - Fork 1
/
AX25.py
158 lines (115 loc) · 3.32 KB
/
AX25.py
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
#!/usr/bin/python
#
# Copyright 2014 Angus Ainslie <[email protected]>
# This software is licensed GPL V2
#
import array
debug = False
def AXsign( s ) :
arr = array.array( "B" );
display_s = ''
signEnd = False
if len( s ) >= 6 :
sLen = 6
else :
sLen = len( s )
for i in range( 0, sLen ) :
if s[i] == '-' :
signEnd = True
if signEnd :
arr.append( ord( ' ' ) << 1 )
display_s += "_"
else :
arr.append( ord( s[i] ) << 1 )
display_s += s[i]
if signEnd or ( len( s ) == 8 and s[7] == '-' ) :
arr.append( ord( s[-1] ) << 1 )
display_s += "-" + s[-1] + " "
else :
arr.append( ord( '0' ) << 1 )
display_s += " "
return ( arr, display_s )
def AX25( to, frm, path, s ) :
arr = array.array( "B" );
display_s = ''
toArr , toS = AXsign( to )
arr += toArr
display_s += toS
frmArr , frmS = AXsign( frm )
arr += frmArr
display_s += frmS
for item in path :
if debug :
print "item: ", item, len( item )
itemArr, itemS = AXsign( item )
arr += itemArr
display_s += itemS
if debug :
print "frame: ", display_s + " " + s
arr[-1] = arr[-1] | 0x01;
arr.append( 0x03 )# control
arr.append( 0xF0 ) # pid
frame_str = ''.join( chr(x) for x in arr )
frame_str += s
return frame_str
def deAX25( s ) :
frame_str = ''
last = False
arr = array.array( 'B', s )
if len( arr ) <= 0 :
return ''
flag = arr[0]
if debug :
print "flag %c %x" % ( chr( arr[0] ), arr[0] )
arr = arr[1:]
while not last :
if len( arr ) < 7 :
print "missing last address"
return frame_str
call_sign = ''
for i in range( 0, 6 ) :
call_sign += str( chr( arr[i] >> 1 ))
if arr[6] & 0x01 :
last = True
if debug and arr[6] & 0x80 :
print "repeated"
ssid = str( chr( (( arr[6] >> 1 ) & 0x0F ) + ord( '0' )) )
frame_str += call_sign + '-' + ssid + ' '
arr = arr[7:]
if len( arr ) < 2 :
return ''
control = arr[0]
if debug :
if control & 0x01 :
if control & 0x02 :
print "U frame"
m = control & 0xEF
print "UI frame type:",
if m == 3 :
print "Unnumbered Information"
if m == 0x87 :
print "Frame Reject"
if m == 0x63 :
print "Unnumbered Acknowledge"
if m == 0x0F :
print "Disconnected Mode"
if m == 0x43 :
print "Disconnect"
if m == 0x2F :
print "Set Asynchronous Balanced Mode"
else :
print "S frame"
else :
print "I frame"
pid = arr[1]
pid_test = pid & 0x30
if debug :
if pid_test == 0x10 or pid_test == 0x20 :
print "AX25 frame"
else :
print "X25 frame type %x" % pid
if debug :
print "Control %x PID %x" % ( control, pid )
arr = arr[2:]
frame_str += ''.join( chr(x) for x in arr )
return frame_str