-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
194 lines (154 loc) · 7.19 KB
/
parser.py
File metadata and controls
194 lines (154 loc) · 7.19 KB
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
import argparse
import io
from enum import IntEnum
from construct import Struct, Int16ul, Int32ul, Bytes, Const, Pointer, Byte, FlagsEnum, PaddedString
from structs import MaskedBits, PasswordString, BcdLE4, ToneSquelch, FreqMHz, ChannelRing
button_functions = {
0xFF: "none",
0x48: "monitor",
0x62: "scan",
0x66: "squelch off",
0x75: "vox",
0x85: "ch and battery voice notification",
0x87: "battery saver",
}
class TXAllowMode(IntEnum):
AlwaysAllow = 0
AllowIfChannelFree = 1
FllowIfSubtoneMismatch = 2
class VOXSensitivity(IntEnum):
HIGH = 0
MID = 1
LOW = 2
ChannelEntry = Struct(
"prev" / Int16ul,
"next" / Int16ul,
"rx_freq" / FreqMHz(BcdLE4(Bytes(4))),
"tx_freq" / FreqMHz(BcdLE4(Bytes(4))),
"rx_tone_squelch" / ToneSquelch(Int16ul),
"tx_tone_squelch" / ToneSquelch(Int16ul),
"padding" / Int32ul,
)
Codeplug = Struct(
"magic" / Const(b"HYTX"),
"version_1_firmware" / Pointer(0x4B, PaddedString(16, "ascii")),
"version_2_rcdb_data" / Pointer(0x5B, PaddedString(16, "ascii")),
"model" / Pointer(0xB3, PaddedString(8, "ascii")),
"model_number" / Pointer(0xBF, PaddedString(20, "ascii")),
"freq_range" / Pointer(0xDF, Struct(
"from" / BcdLE4(Bytes(4)),
"to" / BcdLE4(Bytes(4)),
)),
"serial" / Pointer(0xFF, PaddedString(16, "ascii")),
"device_alias" / Pointer(0x105, PaddedString(4, "ascii")),
"buttons" / Pointer(0x12F, Struct(
"pwr_short" / Byte,
"unknown1" / Byte,
"unknown2" / Byte,
"volume_up_long" / Byte,
"unknown3" / Byte,
"volume_down_long" / Byte,
)),
"settings_13d" / Pointer(0x13d, FlagsEnum(Byte,
b0=0x01, KEYPRESS_SOUND=0x02, POWER_ONOFF_SOUND=0x04, SERVICE_SOUND=0x08,
b4=0x10, b5=0x20, b6=0x40, b7=0x80,
)),
"vox_level" / Pointer(0x1CF, Byte),
"vox_sensitivity" / Pointer(0x1CF, MaskedBits(Byte, 0b00001100, enum=VOXSensitivity)),
"squelch_settings" / Pointer(0x1d1, FlagsEnum(Byte,
SQUELCH_ENABLED=0x01, b1=0x02, SQUELCH_NORMAL=0x04,
SQUELCH_TIGHT=0x08,
b4=0x10, b5=0x20, b6=0x40, b7=0x80,
)),
"call_battery_save_time" / Pointer(0x1F6, Byte), # 0 - 60?
"settings_1f7" / Pointer(0x1F7, FlagsEnum(Byte,
b0=0x01, b1=0x02, CALL_CTCSS_TAIL_REVERSE_180=0x04, ADV_VOX=0x08,
b4=0x10, b5=0x20, b6=0x40, b7=0x80,
)),
"settings_1f8" / Pointer(0x1F8, FlagsEnum(Byte,
b0=0x01, BACKLIGHT_LED=0x02, VOICE_NOTIFICATIONS=0x04,
CALL_CTCSS_TAIL_REVERT=0x08,
ADV_PTT_CHANNEL_PRIORITY_SCAN=0x10, b5=0x20, b6=0x40, b7=0x80,
)),
"backlight_duration" / Pointer(0x1FA, Byte), # 0xFF - unlimited
# default channel spacing is 12.5khz
"settings_1fb" / Pointer(0x1FB, FlagsEnum(Byte,
CHANNEL_SPACING_20KHZ=0x01, CHANNEL_SPACING_25KHZ=0x02, TXPOW_HIGH_1=0x04,
TXPOW_HIGH_2=0x08,
CALL_PRE_EMPHASIS=0x10, COMPANDOR=0x20, b6=0x40, ADV_SCAN=0x80,
)),
"settings_1fc" / Pointer(0x1FC, FlagsEnum(Byte,
b0=0x01, b1=0x02, b2=0x04, b3=0x08,
b4=0x10, b5=0x20, b6=0x40, b7=0x80,
)),
"settings_1fd" / Pointer(0x1FD, FlagsEnum(Byte,
b0=0x01, b1=0x02, b2=0x04, ALLOW_CHANNEL_COPY=0x08,
b4=0x10, b5=0x20, b6=0x40, b7=0x80,
)),
"settings_1fe" / Pointer(0x1FE, FlagsEnum(Byte,
b0=0x01, b1=0x02, b2=0x04, b3=0x08,
TXLOW_HALF_1=0x10, TXLOW_HALF_2=0x20, DISPLAY_ALIAS=0x40, b7=0x80,
)),
"selected_channel" / Pointer(0x200, Byte),
"mic_gain" / Pointer(0x201, Byte), # 0x20 low, 0x25 normal 0x30 high
"settings_220" / Pointer(0x220, Byte),
"tx_allow_mode" / Pointer(0x220, MaskedBits(Byte, 0b00000011, enum=TXAllowMode)),
"call_timeout" / Pointer(0x221, Byte), # * 5 seconds
"settings_22d" / Pointer(0x22D, FlagsEnum(Byte,
WRITE_PASSWORD_ENABLED=0x01, b1=0x02, ALLOW_AIRCLONE=0x04,
READ_PASSWORD_ENABLED=0x08,
b4=0x10, b5=0x20, b6=0x40, b7=0x80,
)),
"write_password" / Pointer(0x22F, PasswordString),
"read_password" / Pointer(0x237, PasswordString),
"channels" / Pointer(0x05FD, ChannelRing(ChannelEntry, bias=0xA3)),
)
def create_channels():
# First 16 LPD channels
channels = []
for i in range(0, 16):
freq = 433.075 + i * 0.025
channels.append(
dict(
rx_freq=freq,
tx_freq=freq,
rx_tone_squelch={"kind": "none"},
tx_tone_squelch={"kind": "none"},
padding=0xFFFFFFFF,
)
)
return channels
def format_squelch(sq):
if sq["kind"] == "none":
return ""
if sq["kind"] == "ctcss":
return " squelch: %s, tone: %0.1f Hz" % (sq["kind"].upper(), sq["freq_hz"])
if sq["kind"] in ["dcs", "dcs_invert"]:
return " squelch: %s, octal code: %s" % (sq["kind"].upper(), sq["code_octal"])
return " UNKNOWN SQUELCH TYPE"
def main():
ap = argparse.ArgumentParser()
ap.add_argument("path", help="input codeplug file")
ap.add_argument("out_path", nargs="?", help="output codeplug file (optional)")
args = ap.parse_args()
data = open(args.path, "rb").read()
cp = Codeplug.parse(data)
print("== parsed codeplug:")
print(cp)
print("\n== parsed channels:")
channel_prefix = "H" if cp.settings_1fb.TXPOW_HIGH_1 and cp.settings_1fb.TXPOW_HIGH_2 else "L"
for idx, channel in enumerate(cp.channels):
print("%s-%02i\n\tRX: %0.5f MHz%s\n\tTX: %0.5f MHz%s" % (
channel_prefix,
idx + 1,
channel.rx_freq, format_squelch(channel.rx_tone_squelch),
channel.tx_freq, format_squelch(channel.tx_tone_squelch),
))
if args.out_path is not None:
print("NB! When programming the codeplug, the RCDB version in the codeplug must match the RCDB version in the radio.")
cp.channels = create_channels()
buf = io.BytesIO(bytes(data))
Codeplug.build_stream(cp, buf)
open(args.out_path, "wb").write(buf.getvalue())
if __name__ == "__main__":
main()