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
|
# -*- coding: utf-8 -*-
# TRX Toolkit
# Helpers for DATA capture management
#
# (C) 2018 by Vadim Yanitskiy <axilirator@gmail.com>
#
# All Rights Reserved
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import logging as log
import struct
from data_msg import *
class DATADump:
# Constants
TAG_TxMsg = b'\x01'
TAG_RxMsg = b'\x02'
HDR_LENGTH = 3
# Generates raw bytes from a DATA message
# Return value: raw message bytes
def dump_msg(self, msg):
# Determine a message type
if isinstance(msg, TxMsg):
tag = self.TAG_TxMsg
elif isinstance(msg, RxMsg):
tag = self.TAG_RxMsg
else:
raise ValueError("Unknown message type")
# Generate a message payload
msg_raw = msg.gen_msg()
# Calculate and pack the message length
msg_len = len(msg_raw)
# Pack to unsigned short (2 bytes, BE)
msg_len = struct.pack(">H", msg_len)
# Concatenate a message with header
return bytearray(tag + msg_len) + msg_raw
def parse_hdr(self, hdr):
# Extract the header info
msg_len = struct.unpack(">H", hdr[1:3])[0]
tag = hdr[:1]
# Check if tag is known
if tag == self.TAG_TxMsg:
msg = TxMsg()
elif tag == self.TAG_RxMsg:
msg = RxMsg()
else:
# Unknown tag
return False
return (msg, msg_len)
class DATADumpFile(DATADump):
def __init__(self, capture):
# Check if capture file is already opened
if isinstance(capture, str):
log.info("Opening capture file '%s'..." % capture)
self.f = open(capture, "a+b")
else:
self.f = capture
def __del__(self):
log.info("Closing the capture file")
self.f.close()
# Moves the file descriptor before a specified message
# Return value:
# True in case of success,
# or False in case of EOF or header parsing error.
def _seek2msg(self, idx):
# Seek to the beginning of the capture
self.f.seek(0)
# Read the capture in loop...
for _ in range(idx):
# Attempt to read a message header
hdr_raw = self.f.read(self.HDR_LENGTH)
if len(hdr_raw) != self.HDR_LENGTH:
return False
# Attempt to parse it
rc = self.parse_hdr(hdr_raw)
if rc is False:
log.error("Couldn't parse a message header")
return False
# Expand the header
(_, msg_len) = rc
# Skip a message
self.f.seek(msg_len, 1)
return True
# Parses a single message at the current descriptor position
# Return value:
# a parsed message in case of success,
# or None in case of EOF or header parsing error,
# or False in case of message parsing error.
def _parse_msg(self):
# Attempt to read a message header
hdr_raw = self.f.read(self.HDR_LENGTH)
if len(hdr_raw) != self.HDR_LENGTH:
return None
# Attempt to parse it
rc = self.parse_hdr(hdr_raw)
if rc is False:
log.error("Couldn't parse a message header")
return None
# Expand the header
(msg, msg_len) = rc
# Attempt to read a message
msg_raw = self.f.read(msg_len)
if len(msg_raw) != msg_len:
log.error("Message length mismatch")
return None
# Attempt to parse a message
try:
msg_raw = bytearray(msg_raw)
msg.parse_msg(msg_raw)
except:
log.error("Couldn't parse a message, skipping...")
return False
# Success
return msg
# Parses a particular message defined by index idx
# Return value:
# a parsed message in case of success,
# or None in case of EOF, out of range, or header parsing error,
# or False in case of message parsing error.
def parse_msg(self, idx):
# Move descriptor to the beginning of requested message
rc = self._seek2msg(idx)
if not rc:
log.error("Couldn't find requested message")
return None
# Attempt to parse a message
return self._parse_msg()
# Parses all messages from a given file
# Return value:
# list of parsed messages,
# or False in case of range error.
def parse_all(self, skip = None, count = None):
result = []
# Should we skip some messages?
if skip is None:
# Seek to the beginning of the capture
self.f.seek(0)
else:
rc = self._seek2msg(skip)
if not rc:
log.error("Couldn't find requested message")
return False
# Read the capture in loop...
while True:
# Attempt to parse a message
msg = self._parse_msg()
# EOF or broken header
if msg is None:
break
# Skip unparsed messages
if msg is False:
continue
# Success, append a message
result.append(msg)
# Count limitation
if count is not None:
if len(result) == count:
break
return result
# Writes a new message at the end of the capture
def append_msg(self, msg):
# Generate raw bytes and write
msg_raw = self.dump_msg(msg)
self.f.write(msg_raw)
# Writes a list of messages at the end of the capture
def append_all(self, msgs):
for msg in msgs:
self.append_msg(msg)
|