-
Notifications
You must be signed in to change notification settings - Fork 2
/
ham.py
325 lines (285 loc) · 9.44 KB
/
ham.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/usr/bin/env python3
import re
from argparse import ArgumentParser
from struct import pack, unpack_from
HEX_EXP = re.compile(r"^[a-f\d]+$", re.IGNORECASE)
# https://github.com/arngll/arnce-spec/blob/main/n6drc-arnce.md#base-40-character-set
BASE40_TAB = "\x00ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/-^"
# https://github.com/arngll/arnce-spec/blob/main/n6drc-arnce.md#escapingunescaping-optional
ESCAPE_TAB = {
# indicator suffix
"/RPTR-": "^R", # Repeater/Digipeater Indicator
"/PORT-": "^P", # Portable Indicator
"/MOBI-": "^M", # Mobile Indicator
"/MARI-": "^S", # Maritime Mobile Indicator
"/AIRM-": "^A", # Aeronautical Mobile Indicator
"/CTST-": "^C", # Contest Indicator
"/RPTR": "^R",
"/PORT": "^P",
"/MOBI": "^M",
"/MARI": "^S",
"/AIRM": "^A",
"/CTST": "^C",
# characters
":": "^/",
"_": "^-",
".": "^D",
"|": "^V",
}
# https://github.com/arngll/arnce-spec/blob/main/n6drc-arnce.md#escapingunescaping-optional
UNESCAPE_TAB = {
# characters
"^/": ":",
"^-": "_",
"^D": ".",
"^V": "|",
# indicator suffix
"^R": "/RPTR", # Repeater/Digipeater Indicator
"^P": "/PORT", # Portable Indicator
"^M": "/MOBI", # Mobile Indicator
"^S": "/MARI", # Maritime Mobile Indicator
"^A": "/AIRM", # Aeronautical Mobile Indicator
"^C": "/CTST" # Contest Indicator
}
is_hex = lambda s: len(s) % 2 == 0 and HEX_EXP.fullmatch(s)
def escape(callsign: str) -> str:
# https://github.com/arngll/arnce-spec/blob/main/n6drc-arnce.md#escapingunescaping-optional
for (f, r) in ESCAPE_TAB.items():
loc = callsign.find(f)
if loc > -1:
callsign = f"{callsign[:loc]}{r}{callsign[loc + len(f):]}"
return callsign
def unescape(callsign: str) -> str:
# https://github.com/arngll/arnce-spec/blob/main/n6drc-arnce.md#escapingunescaping-optional
for (f, r) in UNESCAPE_TAB.items():
loc = callsign.find(f)
if loc > -1:
last_chars = callsign[loc + len(f):]
if len(last_chars) > 0:
callsign = f"{callsign[:loc]}{r}-{last_chars}"
else:
callsign = f"{callsign[:loc]}{r}{last_chars}"
return callsign
def _16_bit_encode(callsign: str) -> bytes:
# https://github.com/arngll/arnce-spec/blob/main/n6drc-arnce.md#16-bit-chunk-encoding-chunk-encoding
dst = b""
for i in range(0, len(callsign), 3):
chars_left = len(callsign) - i
if chars_left >= 3:
a = BASE40_TAB.index(callsign[i]) * 1600
b = BASE40_TAB.index(callsign[i + 1]) * 40
c = BASE40_TAB.index(callsign[i + 2])
elif chars_left == 2:
a = BASE40_TAB.index(callsign[i]) * 1600
b = BASE40_TAB.index(callsign[i + 1]) * 40
c = 0
elif chars_left == 1:
a = BASE40_TAB.index(callsign[i]) * 1600
b = 0
c = 0
else:
a = b = c = 0
dst += pack(">H", a + b + c)
return dst
def _16_bit_decode(callsign: str | bytes) -> str:
# https://github.com/arngll/arnce-spec/blob/main/n6drc-arnce.md#16-bit-chunk-encoding-chunk-encoding
if isinstance(callsign, str) and is_hex(callsign):
callsign = bytes.fromhex(callsign)
dst = ""
for i in range(0, len(callsign), 2):
(s,) = unpack_from(">H", callsign, i)
a = BASE40_TAB[s // 1600 % 40]
b = BASE40_TAB[s // 40 % 40]
c = BASE40_TAB[s % 40]
dst += (a + b + c)
dst = dst.rstrip("\x00")
return dst
def ham64_encode(callsign: str) -> bytes:
# https://github.com/arngll/arnce-spec/blob/main/n6drc-arnce.md#ham-64-link-layer-address-format
return _16_bit_encode(callsign).ljust(8, b"\x00")
def eui48_encode(callsign: str) -> bytes:
# https://github.com/arngll/arnce-spec/blob/main/n6drc-arnce.md#eui-48-encoding-details
if len(callsign) == 9:
cs_last = callsign[8]
if cs_last == "1":
callsign = callsign[:8] + "H"
elif cs_last == "2":
callsign = callsign[:8] + "P"
elif cs_last == "3":
callsign = callsign[:8] + "X"
elif cs_last == "4":
callsign = callsign[:8] + "5"
else:
return b""
enc = _16_bit_encode(callsign)
enc_len = len(enc)
enc_last_ord = enc[-1] & 0x7
enc = enc.ljust(6, b"\x00")
if enc_len > 5 and (enc_len != 6 or enc_last_ord != 0):
return b""
dst = pack(">B5s", (enc[5] & 0xF8) + 2, enc[:5])
return dst
def eui64_encode(callsign: str) -> bytes:
# https://github.com/arngll/arnce-spec/blob/main/n6drc-arnce.md#eui-64-encoding-details
no_eui48 = False
if len(callsign) == 9:
cs_last = callsign[8]
if cs_last == "1":
callsign = callsign[:8] + "H"
elif cs_last == "2":
callsign = callsign[:8] + "P"
elif cs_last == "3":
callsign = callsign[:8] + "X"
elif cs_last == "4":
callsign = callsign[:8] + "5"
else:
no_eui48 = True
if len(callsign) == 12:
cs_last = callsign[11]
if cs_last == "1":
callsign = callsign[:11] + "H"
elif cs_last == "2":
callsign = callsign[:11] + "P"
elif cs_last == "3":
callsign = callsign[:11] + "X"
elif cs_last == "4":
callsign = callsign[:11] + "5"
else:
return b""
enc = _16_bit_encode(callsign)
enc_len = len(enc)
enc_last_ord = enc[-1] & 0x7
enc = enc.ljust(8, b"\x00")
if enc_len > 7 and (enc_len != 8 or enc_last_ord != 0):
return b""
if (not no_eui48 and enc_len < 6) or (not no_eui48 and enc_len == 6 and enc_last_ord == 0):
first_byte = enc[5]
dst = pack(">B5s", (first_byte & 0xF8) + 2, enc[:5])
dst = dst[:3] + b"\xFF\xFE" + dst[3:6]
else:
first_byte = enc[7]
dst = pack(">B7s", (first_byte & 0xF8) + 2, enc[:7])
return dst
# formatters
format_16_bit = lambda data: "-".join([data[i:i + 2].hex().upper() for i in range(0, len(data), 2)])
format_ham64 = lambda data: "-".join([data[i:i + 2].hex().upper() for i in range(0, len(data), 2)])
format_eui48 = lambda data: ":".join([f"{x:02X}" for x in data])
format_eui64 = lambda data: ":".join([f"{x:02X}" for x in data])
# deformatters
deformat_16_bit = lambda data: bytes.fromhex(data.replace("-", ""))
deformat_ham64 = lambda data: bytes.fromhex(data.replace("-", ""))
deformat_eui48 = lambda data: bytes.fromhex(data.replace(":", ""))
deformat_eui64 = lambda data: bytes.fromhex(data.replace(":", ""))
def run_tests() -> None:
# https://github.com/arngll/arnce-spec/blob/main/n6drc-arnce.md#examples-and-test-vectors
tests = {
"N6DRC": {
"ham64": "5CAC-70F8-0000-0000",
"eui48": "02:5C:AC:70:F8:00",
"eui64": "02:5C:AC:FF:FE:70:F8:00"
},
"N6DRC^M2": {
"ham64": "5CAC-711F-55C8-0000",
"eui48": "CA:5C:AC:71:1F:55",
"eui64": "CA:5C:AC:FF:FE:71:1F:55"
},
"KJ6QOH/P": {
"ham64": "4671-6CA0-E9C0-0000",
"eui48": "C2:46:71:6C:A0:E9",
"eui64": "C2:46:71:FF:FE:6C:A0:E9"
},
"KJ6QOH-23": {
"ham64": "4671-6CA0-F226-0000",
"eui48": "22:46:71:6C:A0:F2",
"eui64": "22:46:71:FF:FE:6C:A0:F2"
},
"KJ6QOH-2X": {
"ham64": "4671-6CA0-F220-0000",
"eui48": "",
"eui64": "02:46:71:6C:A0:F2:20:00"
},
"KJ6QOH-99": {
"ham64": "4671-6CA0-F344-0000",
"eui48": "",
"eui64": "02:46:71:6C:A0:F3:44:00"
},
"D9K": {
"ham64": "1EAB-0000-0000-0000",
"eui48": "02:1E:AB:00:00:00",
"eui64": "02:1E:AB:FF:FE:00:00:00"
},
"NA1SS": {
"ham64": "57C4-79B8-0000-0000",
"eui48": "02:57:C4:79:B8:00",
"eui64": "02:57:C4:FF:FE:79:B8:00"
},
"VI2BMARC50": {
"ham64": "8B05-0E89-7118-A8C0",
"eui48": "",
"eui64": "C2:8B:05:0E:89:71:18:A8"
},
"VI2BMARC50-1": {
"ham64": "8B05-0E89-7118-AECC",
"eui48": "",
"eui64": "BA:8B:05:0E:89:71:18:AE"
},
"VI2BMARC50-X": {
"ham64": "8B05-0E89-7118-AEC8",
"eui48": "",
"eui64": ""
}
}
for (callsign, expected) in tests.items():
ham64 = ham64_encode(callsign)
eui48 = eui48_encode(callsign)
eui64 = eui64_encode(callsign)
assert ham64 == bytes.fromhex(expected["ham64"].replace("-", "")), f"Invalid HAM-64 for \"{callsign}\""
assert eui48 == bytes.fromhex(expected["eui48"].replace(":", "")), f"Invalid EUI-48 for \"{callsign}\""
assert eui64 == bytes.fromhex(expected["eui64"].replace(":", "")), f"Invalid EUI-64 for \"{callsign}\""
def main() -> int:
parser = ArgumentParser(description="A script to convert callsigns to 16-bit, HAM-64, EUI-48, and EUI-64")
subparsers = parser.add_subparsers(dest="command")
_16_bit_parser = subparsers.add_parser("16bit")
_16_bit_parser.add_argument("-f", "--format", action="store_true", help="Apply formatting")
_16_bit_parser.add_argument("callsign", type=str, help="The callsign to convert")
ham64_parser = subparsers.add_parser("ham64")
ham64_parser.add_argument("-f", "--format", action="store_true", help="Apply formatting")
ham64_parser.add_argument("callsign", type=str, help="The callsign to convert")
eui48_parser = subparsers.add_parser("eui48")
eui48_parser.add_argument("-f", "--format", action="store_true", help="Apply formatting")
eui48_parser.add_argument("callsign", type=str, help="The callsign to convert")
eui64_parser = subparsers.add_parser("eui64")
eui64_parser.add_argument("-f", "--format", action="store_true", help="Apply formatting")
eui64_parser.add_argument("callsign", type=str, help="The callsign to convert")
tests_parser = subparsers.add_parser("tests")
args = parser.parse_args()
if args.command == "16bit":
enc = _16_bit_encode(args.callsign)
if args.format:
print(format_16_bit(enc))
else:
print(enc.hex().upper())
elif args.command == "ham64":
enc = ham64_encode(args.callsign)
if args.format:
print(format_ham64(enc))
else:
print(enc.hex().upper())
elif args.command == "eui48":
enc = eui48_encode(args.callsign)
if args.format:
print(format_eui48(enc))
else:
print(enc.hex().upper())
elif args.command == "eui64":
enc = eui64_encode(args.callsign)
if args.format:
print(format_eui64(enc))
else:
print(enc.hex().upper())
elif args.command == "tests":
run_tests()
print("ALL TESTS PASSED!")
return 0
if __name__ == "__main__":
exit(main())