76 lines
2.7 KiB
Python
76 lines
2.7 KiB
Python
# 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
|
|
from construct import Container, Int16ul
|
|
from proto import DbgMuxFrame
|
|
|
|
|
|
class DbgMuxPeer:
|
|
def __init__(self, sl):
|
|
self.tx_count: int = 0
|
|
self.rx_count: int = 0
|
|
self._sl = sl
|
|
|
|
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
|
|
})
|
|
|
|
# ACK is a bit special
|
|
if msg_type == DbgMuxFrame.MsgType.Ack:
|
|
c['TxCount'] = 0xf1
|
|
|
|
# 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)
|
|
|
|
log.debug('Tx frame (Ns=%03u, Nr=%03u, fcs=0x%04x) %s %s',
|
|
c['TxCount'], c['RxCount'], c['FCS'],
|
|
c['MsgType'], c['MsgData'].hex())
|
|
|
|
self._sl.write(frame + Int16ul.build(c['FCS']))
|
|
|
|
# ACK is not getting accounted
|
|
if msg_type != DbgMuxFrame.MsgType.Ack:
|
|
self.tx_count += 1
|
|
|
|
def recv(self) -> Container:
|
|
c = DbgMuxFrame.Frame.parse_stream(self._sl)
|
|
|
|
log.debug('Rx frame (Ns=%03u, Nr=%03u, fcs=0x%04x) %s %s',
|
|
c['TxCount'], c['RxCount'], c['FCS'],
|
|
c['MsgType'], c['MsgData'].hex())
|
|
|
|
# Parse the inner message
|
|
c['Msg'] = DbgMuxFrame.Msg.parse(c['MsgData'], MsgType=c['MsgType'])
|
|
|
|
self.rx_count += 1
|
|
return c
|