-
Notifications
You must be signed in to change notification settings - Fork 1
/
target_format.py
319 lines (300 loc) · 10.8 KB
/
target_format.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
# Copyright (c) 2014 Instituto Nokia de Tecnologia (INdT)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import re
from binascii import crc_hqx
import struct
from construct import *
# FlagsContainer and FlagsAdapter are copied from Construct, and a one-line fix
# is applied to FlagsContainer as shown below.
class FlagsContainer(Container):
def __pretty_str__(self, nesting = 1, indentation = " "):
attrs = []
ind = indentation * nesting
for k in self.keys():
# This gives KeyError, so use getattr() instead
#v = self.__dict__[k]
v = getattr(self, k)
if not k.startswith("_") and v:
attrs.append(ind + k)
if not attrs:
return "%s()" % (self.__class__.__name__,)
attrs.insert(0, self.__class__.__name__+ ":")
return "\n".join(attrs)
class FlagsAdapter(Adapter):
__slots__ = ["flags"]
def __init__(self, subcon, flags):
Adapter.__init__(self, subcon)
self.flags = flags
def _encode(self, obj, context):
flags = 0
for name, value in self.flags.items():
if getattr(obj, name, False):
flags |= value
return flags
def _decode(self, obj, context):
obj2 = FlagsContainer()
for name, value in self.flags.items():
setattr(obj2, name, bool(obj & value))
return obj2
def CheckBox(subcon):
return Enum(subcon,
Disabled = 0,
Enabled = 1,
)
def AdvBitmap(name):
return FlagsAdapter(UBInt16(name),
{
"Custom AD": 1 << 14,
"Service solicitation (GATT client)": 1 << 10,
"Slave connection interval range": 1 << 8,
"TX power level": 1 << 6,
"Local name - use shortened": 1 << 5,
"Local name - use complete": 1 << 4,
"Local services": 1 << 0,
},
)
def AdvCustom(name):
return FlagsAdapter(UBInt8(name),
{
"Enable custom AD #1": 1,
"Enable custom AD #2": 2,
},
)
Target_00 = Struct("Target_00",
Const(UBInt8("Setup format"), 3),
Const(UBInt8("Unknown"), 2),
UBInt16("DLL version"),
Terminator,
)
Target_10 = Struct("Target_10",
Const(Field("Unknown @ 0x00", 4), "\x00" * 4),
SymmetricMapping(UBInt8("Device security"),
{
"No security required": 0,
"Security required": 2,
},
),
# FIXME: Number of remote services?
Const(UBInt8("Unknown @ 0x05"), 0),
CheckBox(UBInt8("Writeable device name (over the air)")),
Const(UBInt8("Unknown @ 0x07"), 0),
CheckBox(UBInt8("Writeable device name (from app controller)")),
SymmetricMapping(UBInt8("32 kHz clock source"),
{
"External crystal": 0,
"Internal RC oscillator": 1,
"External analog source": 3,
"External digital source": 4,
},
),
SymmetricMapping(UBInt8("32 kHz clock accuracy"),
{
"250-500 PPM": 0,
"150-250 PPM": 1,
"100-150 PPM": 2,
"75-100 PPM": 3,
"50-75 PPM": 4,
"30-50 PPM": 5,
"20-30 PPM": 6,
"Less than 20 PPM": 7,
},
),
Enum(UBInt8("16 MHz clock source"),
Crystal = 0,
Digital = 1,
),
EmbeddedBitStruct(
BitField("Time before start", 6),
Enum(BitField("Active signal", 2),
Disable = 0,
Active_high = 1,
Active_low = 2,
),
),
EmbeddedBitStruct(
SymmetricMapping(BitField("Initial TX power", 7),
{
"-18 dBm": 0,
"-12 dBm": 1,
"-6 dBm": 2,
"0 dBm": 3,
},
),
Flag("DC/DC converter"),
),
SBInt8("External antenna gain"),
UBInt8("Bytes in shortened name"),
# FIXME: 0xC1 if Gapsettings/ServiceToAdvertise is not empty; 0x00 otherwise
Const(UBInt8("Unknown @ 0x10"), 0),
# FIXME: Gapsettings/ServiceToAdvertise (array of 16-bit Service UUIDs)
Const(Field("Unknown @ 0x11", 13), "\x00" * 13),
# FIXME: 0x01 if Gapsettings/SercieToSolicitate is not empty; 0x00 otherwise
Const(UBInt8("Unknown @ 0x1E"), 0),
# FIXME: Gapsettings/SercieToSolicitate (array of 16-bit Service UUIDs)
Const(Field("Unknown @ 0x1F", 15), "\x00" * 15),
AdvBitmap("ACI Bond Advertising"),
Const(Field("Unknown @ 0x30", 2), "\x00" * 2),
AdvBitmap("ACI Connect Advertising"),
EmbeddedBitStruct(
SymmetricMapping(Nibble("Required level of security"),
{
"Unauthenticated (Just Works)": 0,
"Authenticated (Passkey)": 1,
},
),
SymmetricMapping(Nibble("I/O capabilities"),
{
"Display only": 0,
"Display yes/no": 1,
"Keyboard only": 2,
"None": 3,
"Keyboard and display": 4,
},
),
),
EmbeddedBitStruct(
ExprAdapter(Nibble("Maximum encryption key size"),
encoder = lambda obj, ctx: obj - 7,
decoder = lambda obj, ctx: obj + 7,
),
ExprAdapter(Nibble("Mininum encryption key size"),
encoder = lambda obj, ctx: obj - 7,
decoder = lambda obj, ctx: obj + 7,
),
),
Const(UBInt8("Unknown @ 0x36"), 0),
Enum(UBInt16("Dynamic window limiting"),
On = 0x0264,
Off = 0xffff,
),
Const(UBInt8("Unknown @ 0x39"), 0xFF),
UBInt16("Bond timeout (seconds)"),
UBInt8("Security request delay (seconds)"),
Const(Field("Unknown @ 0x3D", 3), "\x05\x00\x00"),
AdvBitmap("ACI Bond Scan Response"),
Const(Field("Unknown @ 0x42", 2), "\x00" * 2),
AdvBitmap("ACI Connect Scan Response"),
Const(Field("Unknown @ 0x46", 2), "\x00" * 2),
AdvBitmap("ACI Broadcast Advertising"),
Const(Field("Unknown @ 0x4A", 2), "\x00" * 2),
AdvBitmap("ACI Broadcast Scan Response"),
AdvCustom("ACI Bond Advertising custom ADs"),
AdvCustom("ACI Bond Scan Response custom ADs"),
AdvCustom("ACI Connect Advertising custom ADs"),
AdvCustom("ACI Connect Scan Response custom ADs"),
AdvCustom("ACI Broadcast Advertising custom ADs"),
AdvCustom("ACI Broadcast Scan Response custom ADs"),
# FIXME: this field is set when Security is required, but not what it means
# individually. Maybe Whitelist?
IfThenElse("Unknown @ 0x54", lambda ctx: ctx["Device security"] ==
"Security required",
Const(UBInt8("Unknown @ 0x54"), 1),
Const(UBInt8("Unknown @ 0x54"), 0),
),
FlagsAdapter(UBInt8("Available custom ADs"),
{
"Custom AD #1 is present": 1,
"Custom AD #2 is present": 2,
},
),
Terminator,
)
def UUID(name):
return SymmetricMapping(UBInt16(name),
{
"Device Name": 0x2A00,
"Appearance": 0x2A01,
"Peripheral Preferred Connection Parameters": 0x2A04,
"GATT primary service declaration": 0x2800,
"GATT characteristic declaration": 0x2803,
},
)
def Attribute(name):
return Struct(name,
# FIXME: most likely there are bits for write and read permissions
UBInt8("Permissions #1"),
UBInt8("Permissions #2"),
# FIXME: It is not clear yet why there are two different length fields,
# but the field is selected based on the fields above.
UBInt8("Length #1"),
UBInt8("Length #2"),
UBInt16("Handle"),
UUID("Type"),
Const(UBInt8("Unknown"), 1),
# FIXME: It is still not clear how the value length is calculted.
IfThenElse("Value", lambda ctx: ctx["Permissions #1"] == 0x06,
Field("Value", lambda ctx: ctx["Length #2"]),
Field("Value", lambda ctx: ctx["Length #1"]),
),
)
Target_20 = Struct("Target_20",
Attribute("GAP service"),
Attribute("GAP Device Name characteristic"),
Attribute("GAP Device Name characteristic value"),
Attribute("GAP Appearance characteristic"),
Attribute("GAP Appearance characteristic value"),
Attribute("GAP PPCP characteristic"),
Attribute("GAP PPCP characteristic value"),
Attribute("GATT service"),
Const(UBInt8("Unknown"), 0),
Terminator,
)
Target_F0 = Struct("Target_F0",
Enum(UBInt8("Configuration OTP locking"),
Enabled = 0x83,
Disabled = 0x03,
),
UBInt16("16-bit CCITT CRC"),
Terminator,
)
def parse_setup(report_file):
setup_data = None
crc = 0xffff
version = None
for line in report_file.readlines():
m = re.match("Generated with uBlue setup DLL version: 1.0.0.(\d+)$",
line.strip())
if m:
version = int(m.group(1))
if line.strip() == "[Setup Data]":
setup_data = {}
if not line.strip() or setup_data is None:
continue
if not re.match("^[0-9A-F-]+$", line.strip()):
continue
data = line.strip().split("-")
assert int(data[0], 16) == len(data[1:])
# Opcode: Setup (0x06)
assert data[1] == "06"
target = int(data[2], 16)
if not setup_data.get(target):
setup_data[target] = ""
# Offset into target
assert int(data[3], 16) == len(setup_data[target].decode("hex"))
setup_data[target] += "".join(data[4:])
if target == 0xf0:
# Remove existing CRC when calculating new CRC
data = data[:-2]
crc = crc_hqx("".join(data).decode("hex"), crc)
# Check CRC
expected_crc = struct.unpack(">H",
"".join(setup_data[0xf0].decode("hex")[-2:]))[0]
assert crc == expected_crc
return (version, setup_data)