DbgMuxPeer: encode/decode inner messages in send()/recv()

This commit is contained in:
Vadim Yanitskiy 2022-01-17 14:26:47 +06:00 committed by Vadim Yanitskiy
parent ba66506052
commit 712c2b79e5
2 changed files with 17 additions and 16 deletions

View File

@ -18,6 +18,7 @@
import logging as log import logging as log
from typing import Any
from construct import Container, Int16ul from construct import Container, Int16ul
from proto import DbgMuxFrame from proto import DbgMuxFrame
@ -27,7 +28,10 @@ class DbgMuxPeer:
self.rx_count: int = 0 self.rx_count: int = 0
self._sl = sl self._sl = sl
def send(self, msg_type: DbgMuxFrame.MsgType, msg_data: bytes = b'') -> None: 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({ c = Container({
'TxCount' : (self.tx_count + 1) % 256, 'TxCount' : (self.tx_count + 1) % 256,
'RxCount' : self.rx_count % 256, 'RxCount' : self.rx_count % 256,
@ -63,5 +67,8 @@ class DbgMuxPeer:
c['TxCount'], c['RxCount'], c['FCS'], c['TxCount'], c['RxCount'], c['FCS'],
c['MsgType'], c['MsgData'].hex()) c['MsgType'], c['MsgData'].hex())
# Parse the inner message
c['Msg'] = DbgMuxFrame.Msg.parse(c['MsgData'], MsgType=c['MsgType'])
self.rx_count += 1 self.rx_count += 1
return c return c

View File

@ -104,13 +104,12 @@ class SEDbgMuxApp(cmd2.Cmd):
while True: while True:
f = self.peer.recv() f = self.peer.recv()
if f['MsgType'] == DbgMuxFrame.MsgType.Ident: if f['MsgType'] == DbgMuxFrame.MsgType.Ident:
msg = DbgMuxFrame.MsgIdent.parse(f['MsgData'])
log.info("Identified target: '%s', IMEI=%s", log.info("Identified target: '%s', IMEI=%s",
msg['Ident'][:-15], msg['Ident'][-15:]) f['Msg']['Ident'][:-15],
f['Msg']['Ident'][-15:])
elif f['MsgType'] == DbgMuxFrame.MsgType.DPAnnounce: elif f['MsgType'] == DbgMuxFrame.MsgType.DPAnnounce:
msg = DbgMuxFrame.MsgDPAnnounce.parse(f['MsgData'])
log.info("Data Provider available (DPRef=0x%04x): '%s'", log.info("Data Provider available (DPRef=0x%04x): '%s'",
msg['DPRef'], msg['Name']) f['Msg']['DPRef'], f['Msg']['Name'])
# No more data in the buffer # No more data in the buffer
if self.sl.in_waiting == 0: if self.sl.in_waiting == 0:
@ -128,14 +127,12 @@ class SEDbgMuxApp(cmd2.Cmd):
@cmd2.with_category(CATEGORY_DBGMUX) @cmd2.with_category(CATEGORY_DBGMUX)
def do_ping(self, opts) -> None: def do_ping(self, opts) -> None:
''' Send a Ping to the target, expect Pong ''' ''' Send a Ping to the target, expect Pong '''
msg = DbgMuxFrame.MsgPingPong
log.info('Tx Ping with payload \'%s\'', opts.payload) log.info('Tx Ping with payload \'%s\'', opts.payload)
self.peer.send(DbgMuxFrame.MsgType.Ping, msg.build(opts.payload)) self.peer.send(DbgMuxFrame.MsgType.Ping, opts.payload)
f = self.peer.recv() f = self.peer.recv()
assert(f['MsgType'] == DbgMuxFrame.MsgType.Pong) assert(f['MsgType'] == DbgMuxFrame.MsgType.Pong)
log.info('Rx Pong with payload \'%s\'', msg.parse(f['MsgData'])) log.info('Rx Pong with payload \'%s\'', f['Msg'])
establish_parser = cmd2.Cmd2ArgumentParser() establish_parser = cmd2.Cmd2ArgumentParser()
establish_parser.add_argument('DPRef', establish_parser.add_argument('DPRef',
@ -147,18 +144,16 @@ class SEDbgMuxApp(cmd2.Cmd):
def do_establish(self, opts) -> None: def do_establish(self, opts) -> None:
''' Establish connection with a Data Provider ''' ''' Establish connection with a Data Provider '''
log.info("Establishing connection with DPRef=0x%04x", opts.DPRef) log.info("Establishing connection with DPRef=0x%04x", opts.DPRef)
self.peer.send(DbgMuxFrame.MsgType.ConnEstablish, self.peer.send(DbgMuxFrame.MsgType.ConnEstablish, dict(DPRef=opts.DPRef))
DbgMuxFrame.MsgConnEstablish.build({ 'DPRef' : opts.DPRef }))
f = self.peer.recv() f = self.peer.recv()
assert(f['MsgType'] == DbgMuxFrame.MsgType.ConnEstablished) assert(f['MsgType'] == DbgMuxFrame.MsgType.ConnEstablished)
ConnRef = DbgMuxFrame.MsgConnEstablished.parse(f['MsgData'])['ConnRef'] if f['Msg']['ConnRef'] == 0xffff:
if ConnRef == 0xffff:
log.warning("Connection failed: unknown DPRef=0x%04x?", opts.DPRef) log.warning("Connection failed: unknown DPRef=0x%04x?", opts.DPRef)
self.peer.send(DbgMuxFrame.MsgType.Ack) self.peer.send(DbgMuxFrame.MsgType.Ack)
return return
log.info("Connection established (ConnRef=0x%04x)", ConnRef) log.info("Connection established (ConnRef=0x%04x)", f['Msg']['ConnRef'])
# Read the messages # Read the messages
while True: while True:
@ -169,8 +164,7 @@ class SEDbgMuxApp(cmd2.Cmd):
self.peer.send(DbgMuxFrame.MsgType.Ack) self.peer.send(DbgMuxFrame.MsgType.Ack)
continue continue
try: # FIXME: there can be binary data try: # FIXME: there can be binary data
msg = DbgMuxFrame.MsgConnData.parse(f['MsgData']) self.stdout.write(f['Msg']['Data'].decode())
self.stdout.write(msg['Data'].decode())
except: # ... ignore it for now except: # ... ignore it for now
continue continue