-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReSearch.py
More file actions
281 lines (212 loc) · 8.7 KB
/
ReSearch.py
File metadata and controls
281 lines (212 loc) · 8.7 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
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
import re
from datetime import datetime
from saleae.analyzers import HighLevelAnalyzer, AnalyzerFrame
from saleae.analyzers import NumberSetting, StringSetting
from saleae.data import GraphTimeDelta as GTD
kTimeZero = datetime(1, 1, 1)
def asNum(str):
matches = re.findall("\d+", str)
return int(matches[0])
def AsObj(**kargs): return type('', (object,), kargs)()
class StrBlockBuffer:
def __init__(self, matchStr):
self.blocks = []
self.SetMatch(matchStr)
def SetMatch(self, matchStr):
self.pattern = matchStr
def AddBlock(self, str, start_time, end_time):
if not len(str):
return
charTime = float(end_time - start_time) / len(str)
if len(self.blocks):
lastBlock = self.blocks[-1]
prevEnd = lastBlock.end
if prevEnd + GTD(charTime / 2.0) >= start_time:
# We can concatenate the new string onto the previous block
lastBlock.str += str;
lastBlock.end = end_time
lastBlock.charTime = \
float(lastBlock.end - lastBlock.start) / len(lastBlock.str)
return
self.blocks.append(AsObj(
str = str, start = start_time, end = end_time, charTime = charTime
))
def Drop(self, lastIndex):
# Remove all blocks up to the block containing lastIndex. Trim the
# string in the block containing lastIndex to remove all characters
# up to and including lastIndex.
if not len(self.blocks):
return
# blockStartIndex is the index of first char in the current block wrt
# the state of the blocks on entry.
blockStartIndex = 0
while len(self.blocks):
block = self.blocks[0]
strLen = len(block.str)
if blockStartIndex > lastIndex:
# Somehow we have passed lastIndex. Maybe it was bogus?
# In any case, we are done.
return
if strLen + blockStartIndex <= lastIndex or not strLen:
# This block doesn't contain lastIndex or lastIndex is the final
# character in the block. In either case we drop the whole block
blockStartIndex += len(block.str)
del self.blocks[0]
continue
# The current block contains lastIndex somewhere before the end of
# the string. Trim the string and update the block start time
startEpoch = block.start
endEpoch = block.end
delChars = lastIndex - blockStartIndex
block.start = startEpoch + GTD(block.charTime * delChars)
block.str = block.str[lastIndex - blockStartIndex : ]
return
def DropBefore(self, timeDelta):
# Remove all blocks before the end time of the last block - timeDelta
if not len(self.blocks):
return
# Calculate the cut off time
cutOffTime = self.blocks[-1].end - GTD(timeDelta)
while len(self.blocks):
block = self.blocks[0]
strLen = len(block.str)
if block.start > cutOffTime:
# Whole block is within allowed time span. Done
return
if block.end < cutOffTime:
# None of the block is within the allowed time span. Drop it.
del self.blocks[0]
continue
# The block spans the start of the allowed time span. Trim the
# block.
delCount = int((float(cutOffTime - block.start) + block.charTime) / block.charTime)
block.start += GTD(delCount * block.charTime)
block.str = block.str[delCount : ]
return
def Match(self, drop = False):
if not len(self.blocks):
return
searchStr = ""
starts = []
for block in self.blocks:
startIndex = len(searchStr)
endIndex = len(searchStr) + len(block.str)
searchStr += block.str
starts.append(AsObj(
startIndex = startIndex,
endIndex = endIndex,
startTime = block.start,
charTime = float(block.charTime)
))
match = re.search(self.pattern, searchStr)
if match is None:
return None
# Get first and last match indexs for the entire match
firstIdx = match.start(0)
lastIdx = match.end(0)
for start in starts:
if start.endIndex <= firstIdx:
continue # Haven't reached the start block yet
# This block's endIndex is greater than the index we are looking for
# so the start must be in this block
offset = firstIdx - start.startIndex
startEpoch = start.startTime + GTD(offset * start.charTime)
break
# Set a fallback endEpoch value
endEpoch = startEpoch + GTD(starts[-1].charTime)
for start in starts:
if start.endIndex < lastIdx:
continue
offset = lastIdx - start.startIndex
endEpoch = start.startTime + GTD(offset * start.charTime)
break
result = AsObj(
str = match.string[firstIdx:lastIdx],
start = startEpoch, end = endEpoch
)
if drop:
self.Drop(lastIdx)
return result
class ReSearch(HighLevelAnalyzer):
kMatch = StringSetting(label = "Match")
kMatchTime = StringSetting(label = "Max span (s)")
result_types = {
"Matched": {"format": "{{{data.Matched}}}"},
"bad": {"format": "{{{data.bad}}}"},
}
def Reset(self):
self.startTime = None
self.dataStart = None
self.address = 0
self.data = []
''' Utility routines
'''
def MakeFrame(self, start, stop, text):
return AnalyzerFrame(self.device, start, stop, {self.device: text})
def MakeListFrame(self, start, stop, list):
listStr = ", ".join((str(item) for item in list))
return AnalyzerFrame(self.device, start, stop, {self.device: listStr})
''' Logic2 API entry points and class construction
'''
def AddAddress(self, params):
if bool == type(params.data["address"]):
# Data is an address byte for an async serial protocol
self.haveAddress = True
if bytes != type(params.data["address"]):
print("!AddAddress can't handle " + str(type(params.data["address"])))
return False
self.haveAddress = True
asStr = "@" + hex(params.data["address"][0]) + " "
self.blocks.AddBlock(asStr, params.start_time, params.end_time)
return False
def AddData(self, params):
result = True
if 'address' in params.data and params.data["address"]:
asStr = '@' + hex(params.data["data"][0]) + ' '
elif self.haveAddress or params.data["data"][0] > 127:
# I2C or other bus protocol with addressed devices or the character
# from an async serial analyzer is outside the ASCII range
asStr = hex(params.data["data"][0]) + ' '
result = False # Wait for a stop before we try to match
else:
asStr = params.data["data"].decode("ascii")
self.blocks.AddBlock(asStr, params.start_time, params.end_time)
return result
def AddResult(self, params):
self.blocks.AddBlock(params.data["data"], params.start_time, params.end_time)
return True
def AddStart(self, params):
return False
def AddStop(self, params):
return True
def __init__(self):
self.blocks = StrBlockBuffer(self.kMatch)
self.haveAddress = False
self.isSource = False
self.Reset()
if not len(self.kMatchTime):
self.kMatchTime = "0"
self.kMatchTime = float(str(self.kMatchTime))
# handlers return True if a match should be attempted
self.handlerDispatch = {
"address": self.AddAddress,
"data": self.AddData,
"result": self.AddResult,
"start": self.AddStart,
"stop": self.AddStop,
}
# Main Logic2 entry point
def decode(self, newFrame: AnalyzerFrame):
self.frame = newFrame
if not newFrame.type in self.handlerDispatch:
print("decode() can't handle " + newFrame.type)
return
if not self.handlerDispatch[newFrame.type](newFrame):
return
if self.kMatchTime:
self.blocks.DropBefore(self.kMatchTime)
match = self.blocks.Match(drop = True)
if not match:
return
return AnalyzerFrame \
('Matched', match.start, match.end, {'Matched': match.str})