sedbgmux/peer.py

78 lines
2.7 KiB
Python
Raw Normal View History

2022-01-15 20:44:34 +00:00
# This file is a part of sedbgmux, an open source DebugMux client.
# Copyright (c) 2022 Vadim Yanitskiy <axilirator@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
import logging as log
from typing import Any
2022-01-15 20:44:34 +00:00
from construct import Container, Int16ul
from transport import Transport
2022-01-15 20:44:34 +00:00
from proto import DbgMuxFrame
2022-01-15 20:44:34 +00:00
class DbgMuxPeer:
def __init__(self, io: Transport):
self.tx_count: int = 0
self.rx_count: int = 0
self.io = io
2022-01-15 20:44:34 +00:00
def send(self, msg_type: DbgMuxFrame.MsgType, msg: Any = b'') -> None:
# Encode the inner message first
msg_data = DbgMuxFrame.Msg.build(msg, MsgType=msg_type)
c = Container({
'TxCount': (self.tx_count + 1) % 256,
'RxCount': self.rx_count % 256,
'MsgType': msg_type,
'MsgData': msg_data,
'FCS': 0 # Calculated below
})
2022-01-15 20:44:34 +00:00
# ACK is a bit special
if msg_type == DbgMuxFrame.MsgType.Ack:
c['TxCount'] = 0xf1
2022-01-15 20:44:34 +00:00
# There is a Checksum construct, but it requires all checksummed fields
# to be wrapped into an additional RawCopy construct. This is ugly and
# inconvinient from the API point of view, so we calculate the FCS manually:
frame = DbgMuxFrame.Frame.build(c)[:-2] # strip b'\x00\x00'
c['FCS'] = DbgMuxFrame.fcs_func(frame)
2022-01-15 20:44:34 +00:00
log.debug('Tx frame (Ns=%03u, Nr=%03u, fcs=0x%04x) %s %s',
c['TxCount'], c['RxCount'], c['FCS'],
c['MsgType'], c['MsgData'].hex())
2022-01-15 20:44:34 +00:00
self.io.write(frame + Int16ul.build(c['FCS']))
2022-01-15 20:44:34 +00:00
# ACK is not getting accounted
if msg_type != DbgMuxFrame.MsgType.Ack:
self.tx_count += 1
2022-01-15 20:44:34 +00:00
def recv(self) -> Container:
c = DbgMuxFrame.Frame.parse_stream(self.io)
2022-01-15 20:44:34 +00:00
log.debug('Rx frame (Ns=%03u, Nr=%03u, fcs=0x%04x) %s %s',
c['TxCount'], c['RxCount'], c['FCS'],
c['MsgType'], c['MsgData'].hex())
2022-01-15 20:44:34 +00:00
# Parse the inner message
c['Msg'] = DbgMuxFrame.Msg.parse(c['MsgData'], MsgType=c['MsgType'])
self.rx_count += 1
return c