2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2020 Gerhard Sittig <gerhard.sittig@gmx.net>
6 ## This program is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 2 of the License, or
9 ## (at your option) any later version.
11 ## This program is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ## GNU General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
20 # See the https://www.pjon.org/ PJON project page and especially the
21 # https://www.pjon.org/PJON-protocol-specification-v3.2.php protocol
22 # specification, which can use different link layers.
25 # - Handle RX and TX identifiers in separate routines, including the
26 # optional bus identifers. Track those addresses, add formatters for
27 # them, and emit "communication relation" details.
28 # - Check for correct endianess in variable width fields. The spec says
29 # "network order", that's what this implementation uses.
30 # - Check for the correct order of optional fields (the spec is not as
31 # explicit on these details as I'd expect).
32 # - Check decoder's robustness, completeness, and correctness when more
33 # captures become available. Currently there are only few, which only
34 # cover minimal communication, and none of the protocol's flexibility.
35 # The decoder was essentially written based on the available docs, and
36 # then took some arbitrary choices and liberties to cope with real life
37 # data from an example setup. Strictly speaking this decoder violates
38 # the spec, and errs towards the usability side.
40 import sigrokdecode as srd
43 ANN_RX_INFO, ANN_HDR_CFG, ANN_PKT_LEN, ANN_META_CRC, ANN_TX_INFO, \
44 ANN_SVC_ID, ANN_PKT_ID, ANN_ANON_DATA, ANN_PAYLOAD, ANN_END_CRC, \
73 class Decoder(srd.Decoder):
78 desc = 'The PJON protocol.'
80 inputs = ['pjon-link']
84 ('rx_info', 'Receiver ID'),
85 ('hdr_cfg', 'Header config'),
86 ('pkt_len', 'Packet length'),
87 ('meta_crc', 'Meta CRC'),
88 ('tx_info', 'Sender ID'),
89 ('port', 'Service ID'),
90 ('pkt_id', 'Packet ID'),
91 ('anon', 'Anonymous data'),
92 ('payload', 'Payload'),
93 ('end_crc', 'End CRC'),
94 ('syn_rsp', 'Sync response'),
95 ('relation', 'Relation'),
96 ('warning', 'Warning'),
99 ('fields', 'Fields', (
100 ANN_RX_INFO, ANN_HDR_CFG, ANN_PKT_LEN, ANN_META_CRC, ANN_TX_INFO,
101 ANN_SVC_ID, ANN_ANON_DATA, ANN_PAYLOAD, ANN_END_CRC, ANN_SYN_RSP,
103 ('relations', 'Relations', (ANN_RELATION,)),
104 ('warnings', 'Warnings', (ANN_WARN,)),
113 def reset_frame(self):
114 self.frame_bytes = None
115 self.ack_bytes = None
120 self.out_ann = self.register(srd.OUTPUT_ANN)
122 def putg(self, ss, es, ann, data):
123 self.put(ss, es, self.out_ann, [ann, data])
125 def frame_flush(self):
126 if not self.frame_bytes:
128 # TODO Emit "communication relation" details here?
130 def handle_field_get_desc(self, idx = None):
131 '''Lookup description of a PJON frame field.'''
132 if not self.field_desc:
135 idx = self.field_desc_idx
136 if idx >= 0 and idx >= len(self.field_desc):
138 if idx < 0 and abs(idx) > len(self.field_desc):
140 desc = self.field_desc[idx]
143 def handle_field_add_desc(self, fmt, hdl, cls = None):
144 '''Register description for a PJON frame field.'''
147 'width': struct.calcsize(fmt),
151 self.field_desc.append(item)
153 def handle_field_seed_desc(self):
154 '''Seed list of PJON frame fields' descriptions.'''
156 # At the start of a PJON frame, the layout of only two fields
157 # is known. Subsequent fields (their presence, and width) depend
158 # on the content of the header config field.
161 self.handle_field_add_desc('<B', self.handle_field_rx_id, ANN_RX_INFO)
162 self.handle_field_add_desc('<B', self.handle_field_config, ANN_HDR_CFG)
164 self.field_desc_idx = 0
165 self.field_desc_got = 0
167 self.frame_rx_id = None
168 self.frame_is_broadcast = None
169 self.frame_tx_id = None
170 self.frame_payload = None
171 self.frame_has_ack = None
173 def handle_field_rx_id(self, b):
174 '''Process receiver ID field of a PJON frame.'''
178 # Track RX info for communication relation emission.
180 self.frame_is_broadcast = b == 0
182 # Provide text presentation, caller emits frame field annotation.
183 if b == 255: # "not assigned"
185 elif b == 0: # "broadcast"
188 id_txt = '{:d}'.format(b)
190 'RX_ID {}'.format(id_txt),
195 def handle_field_config(self, b):
196 '''Process header config field of a PJON frame.'''
198 # Caller provides a list of values. We want a single scalar.
201 # Get the config flags.
202 self.cfg_shared = b & (1 << 0)
203 self.cfg_tx_info = b & (1 << 1)
204 self.cfg_sync_ack = b & (1 << 2)
205 self.cfg_async_ack = b & (1 << 3)
206 self.cfg_port = b & (1 << 4)
207 self.cfg_crc32 = b & (1 << 5)
208 self.cfg_len16 = b & (1 << 6)
209 self.cfg_pkt_id = b & (1 << 7)
211 # Get a textual presentation of the flags.
213 text.append('pkt_id' if self.cfg_pkt_id else '-') # packet number
214 text.append('len16' if self.cfg_len16 else '-') # 16bit length not 8bit
215 text.append('crc32' if self.cfg_crc32 else '-') # 32bit CRC not 8bit
216 text.append('svc_id' if self.cfg_port else '-') # port aka service ID
217 text.append('ack_mode' if self.cfg_async_ack else '-') # async response
218 text.append('ack' if self.cfg_sync_ack else '-') # synchronous response
219 text.append('tx_info' if self.cfg_tx_info else '-')
220 text.append('bus_id' if self.cfg_shared else '-') # "shared" vs "local"
221 text = ' '.join(text)
222 bits = '{:08b}'.format(b)
224 'CFG {:s}'.format(text),
225 'CFG {}'.format(bits),
229 # TODO Come up with the most appropriate phrases for this logic.
230 # Are separate instruction groups with repeated conditions more
231 # readable than one common block which registers fields _and_
232 # updates the overhead size? Or is the latter preferrable due to
233 # easier maintenance and less potential for inconsistency?
235 # Get the size of variable width fields, to calculate the size
236 # of the packet overhead (the part that is not the payload data).
237 # This lets us derive the payload length when we later receive
242 len_fmt = u16_fmt if self.cfg_len16 else u8_fmt
243 crc_fmt = u32_fmt if self.cfg_crc32 else u8_fmt
244 self.cfg_overhead = 0
245 self.cfg_overhead += struct.calcsize(u8_fmt) # receiver ID
246 self.cfg_overhead += struct.calcsize(u8_fmt) # header config
247 self.cfg_overhead += struct.calcsize(len_fmt) # packet length
248 self.cfg_overhead += struct.calcsize(u8_fmt) # initial CRC, always CRC8
249 # TODO Check for completeness and correctness.
251 self.cfg_overhead += struct.calcsize(u32_fmt) # receiver bus
254 self.cfg_overhead += struct.calcsize(u32_fmt) # sender bus
255 self.cfg_overhead += struct.calcsize(u8_fmt) # sender ID
257 self.cfg_overhead += struct.calcsize(u16_fmt) # service ID
259 self.cfg_overhead += struct.calcsize(u16_fmt) # packet ID
260 self.cfg_overhead += struct.calcsize(crc_fmt) # end CRC
262 # Register more frame fields as we learn about their presence and
263 # format. Up to this point only receiver ID and header config were
264 # registered since their layout is fixed.
266 # Packet length and meta CRC are always present but can be of
267 # variable width. Optional fields follow the meta CRC and preceed
268 # the payload bytes. Notice that payload length isn't known here
269 # either, though its position is known already. The packet length
270 # is yet to get received. Subtracting the packet overhead from it
271 # (which depends on the header configuration) will provide that
274 # TODO Check for completeness and correctness.
275 # TODO Optionally fold overhead size arith and field registration
276 # into one block of instructions, to reduce the redundancy in the
277 # condition checks, and raise awareness for incomplete sequences
278 # during maintenance.
279 self.handle_field_add_desc(len_fmt, self.handle_field_pkt_len, ANN_PKT_LEN)
280 self.handle_field_add_desc(u8_fmt, self.handle_field_init_crc, ANN_META_CRC)
282 self.handle_field_add_desc(u32_fmt, ['RX_BUS {:08x}', '{:08x}'], ANN_ANON_DATA)
285 self.handle_field_add_desc(u32_fmt, ['TX_BUS {:08x}', '{:08x}'], ANN_ANON_DATA)
286 self.handle_field_add_desc(u8_fmt, ['TX_ID {:d}', '{:d}'], ANN_ANON_DATA)
288 self.handle_field_add_desc(u16_fmt, ['PORT {:d}', '{:d}'], ANN_ANON_DATA)
290 self.handle_field_add_desc(u16_fmt, ['PKT {:04x}', '{:04x}'], ANN_ANON_DATA)
291 pl_fmt = '>{:d}B'.format(0)
292 self.handle_field_add_desc(pl_fmt, self.handle_field_payload, ANN_PAYLOAD)
293 self.handle_field_add_desc(crc_fmt, self.handle_field_end_crc, ANN_END_CRC)
295 # Emit warning annotations for invalid flag combinations.
297 wants_ack = self.cfg_sync_ack or self.cfg_async_ack
298 if wants_ack and not self.cfg_tx_info:
299 warn_texts.append('ACK request without TX info')
300 if wants_ack and self.frame_is_broadcast:
301 warn_texts.append('ACK request for broadcast')
302 if self.cfg_sync_ack and self.cfg_async_ack:
303 warn_texts.append('sync and async ACK request')
304 if self.cfg_len16 and not self.cfg_crc32:
305 warn_texts.append('extended length needs CRC32')
307 warn_texts = ', '.join(warn_texts)
308 self.putg(self.ann_ss, self.ann_es, ANN_WARN, [warn_texts])
310 # Have the caller emit the annotation for configuration data.
313 def handle_field_pkt_len(self, b):
314 '''Process packet length field of a PJON frame.'''
316 # Caller provides a list of values. We want a single scalar.
319 # The wire communicates the total packet length. Some of it is
320 # overhead (non-payload data), while its volume is variable in
321 # size (dpends on the header configuration).
323 # Derive the payload size from previously observed flags. Update
324 # the previously registered field description (the second last
325 # item in the list, before the end CRC).
328 pl_len = b - self.cfg_overhead
330 if pkt_len not in range(self.cfg_overhead, 65536):
331 warn_texts.append('suspicious packet length')
332 if pkt_len > 15 and not self.cfg_crc32:
333 warn_texts.append('length above 15 needs CRC32')
335 warn_texts.append('suspicious payload length')
338 warn_texts = ', '.join(warn_texts)
339 self.putg(self.ann_ss, self.ann_es, ANN_WARN, [warn_texts])
340 pl_fmt = '>{:d}B'.format(pl_len)
342 desc = self.handle_field_get_desc(-2)
343 desc['format'] = pl_fmt
344 desc['width'] = struct.calcsize(pl_fmt)
346 # Have the caller emit the annotation for the packet length.
347 # Provide information of different detail level for zooming.
349 'LENGTH {:d} (PAYLOAD {:d})'.format(pkt_len, pl_len),
350 'LEN {:d} (PL {:d})'.format(pkt_len, pl_len),
351 '{:d} ({:d})'.format(pkt_len, pl_len),
352 '{:d}'.format(pkt_len),
356 def handle_field_common_crc(self, have, is_meta):
357 '''Process a CRC field of a PJON frame.'''
359 # CRC algorithm and width are configurable, and can differ
360 # across meta and end checksums in a frame's fields.
361 caption = 'META' if is_meta else 'END'
362 crc_len = 8 if is_meta else 32 if self.cfg_crc32 else 8
363 crc_fmt = '{:08x}' if crc_len == 32 else '{:02x}'
364 have_text = crc_fmt.format(have)
366 # Check received against expected checksum. Emit warnings.
368 data = self.frame_bytes[:-1]
369 want = calc_crc32(data) if crc_len == 32 else calc_crc8(data)
371 want_text = crc_fmt.format(want)
372 warn_texts.append('CRC mismatch - want {} have {}'.format(want_text, have_text))
374 warn_texts = ', '.join(warn_texts)
375 self.putg(self.ann_ss, self.ann_es, ANN_WARN, [warn_texts])
377 # Provide text representation for frame field, caller emits
380 '{}_CRC {}'.format(caption, have_text),
381 'CRC {}'.format(have_text),
386 def handle_field_init_crc(self, b):
387 '''Process initial CRC (meta) field of a PJON frame.'''
388 # Caller provides a list of values. We want a single scalar.
390 return self.handle_field_common_crc(b, True)
392 def handle_field_end_crc(self, b):
393 '''Process end CRC (total frame) field of a PJON frame.'''
394 # Caller provides a list of values. We want a single scalar.
396 return self.handle_field_common_crc(b, False)
398 def handle_field_payload(self, b):
399 '''Process payload data field of a PJON frame.'''
401 self.frame_payload = b[:]
403 text = ' '.join(['{:02x}'.format(v) for v in b])
405 'PAYLOAD {}'.format(text),
410 def handle_field_sync_resp(self, b):
411 '''Process synchronous response for a PJON frame.'''
413 self.frame_has_ack = b
416 'ACK {:02x}'.format(b),
421 def decode(self, ss, es, data):
424 # Start frame bytes accumulation when FRAME_INIT is seen. Flush
425 # previously accumulated frame bytes when a new frame starts.
426 if ptype == 'FRAME_INIT':
429 self.frame_bytes = []
430 self.handle_field_seed_desc()
433 # Use IDLE as another (earlier) trigger to flush frames. Also
434 # trigger flushes on FRAME-DATA which mean that the link layer
435 # inspection has seen the end of a protocol frame.
437 # TODO Improve usability? Emit warnings for PJON frames where
438 # FRAME_DATA was seen but FRAME_INIT wasn't? So that users can
439 # become aware of broken frames.
440 if ptype in ('IDLE', 'FRAME_DATA'):
445 # Switch from data bytes to response bytes when WAIT is seen.
446 if ptype == 'SYNC_RESP_WAIT':
448 self.ann_ss, self.ann_es = None, None
451 # Accumulate data bytes as they arrive. Put them in the bucket
452 # which corresponds to its most recently seen leader.
453 if ptype == 'DATA_BYTE':
456 # Are we collecting response bytes (ACK)?
457 if self.ack_bytes is not None:
460 self.ack_bytes.append(b)
462 text = self.handle_field_sync_resp(b)
464 self.putg(self.ann_ss, self.ann_es, ANN_SYN_RSP, text)
465 self.ann_ss, self.ann_es = None, None
468 # Are we collecting frame content?
469 if self.frame_bytes is not None:
472 self.frame_bytes.append(b)
475 # Has the field value become available yet?
476 desc = self.handle_field_get_desc()
479 width = desc.get('width', None)
482 self.field_desc_got += 1
483 if self.field_desc_got != width:
486 # Grab most recent received field as a byte array. Get
487 # the values that it contains.
488 fmt = desc.get('format', '>B')
489 raw = bytearray(self.frame_bytes[-width:])
490 values = struct.unpack(fmt, raw)
492 # Process the value, and get its presentation. Can be
493 # mere formatting, or serious execution of logic.
494 hdl = desc.get('handler', '{!r}')
495 if isinstance(hdl, str):
496 text = [hdl.format(*values)]
497 elif isinstance(hdl, (list, tuple)):
498 text = [f.format(*values) for f in hdl]
501 cls = desc.get('anncls', ANN_ANON_DATA)
503 # Emit annotation unless the handler routine already did.
504 if cls is not None and text:
505 self.putg(self.ann_ss, self.ann_es, cls, text)
506 self.ann_ss, self.ann_es = None, None
508 # Advance scan position for to-get-received field.
509 self.field_desc_idx += 1
510 self.field_desc_got = 0
513 # Unknown phase, not collecting. Not synced yet to the input?
516 # Unknown or unhandled kind of link layer output.