Skip to content

Py3.7 multi protocol #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: multi-protocol
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/about.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
🎊🎉 This fork is target to run `modem` on Python3

=============
About modem
=============
Expand Down
19 changes: 9 additions & 10 deletions modem/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from modem.const import CRC16_MAP, CRC32_MAP
from modem.tools import crc16, crc32


Expand All @@ -22,21 +21,21 @@ def calc_checksum(self, data, checksum=0):
'0x3c'

'''
return (sum(map(ord, data)) + checksum) % 256
return (sum(map(lambda x: x, data)) + checksum) % 256

def calc_crc16(self, data, crc=0):
'''
Calculate the 16 bit Cyclic Redundancy Check for a given block of data,
can also be used to update a CRC.

>>> crc = modem.calc_crc16('hello')
>>> crc = modem.calc_crc16('world', crc)
>>> crc = modem.calc_crc16(b'hello')
>>> crc = modem.calc_crc16(b'world', crc)
>>> hex(crc)
'0xd5e3'

'''
for char in data:
crc = crc16(char, crc)
for byte in data:
crc = crc16(byte, crc)
return crc

def calc_crc32(self, data, crc=0):
Expand All @@ -50,8 +49,8 @@ def calc_crc32(self, data, crc=0):
'0x20ad'

'''
for char in data:
crc = crc32(char, crc)
for byte in data:
crc = crc32(byte, crc)
return crc

def _check_crc(self, data, crc_mode):
Expand All @@ -68,13 +67,13 @@ def _check_crc(self, data, crc_mode):
or returns False in case of invalid checksum/CRC
'''
if crc_mode:
csum = (ord(data[-2]) << 8) + ord(data[-1])
csum = (data[-2] << 8) + data[-1]
data = data[:-2]
mine = self.calc_crc16(data)
if csum == mine:
return data
else:
csum = ord(data[-3])
csum = data[-3]
data = data[:-1]
mine = self.calc_checksum(data)
if csum == mine:
Expand Down
158 changes: 79 additions & 79 deletions modem/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,110 +279,110 @@

'''

SOH = chr(0x01)
STX = chr(0x02)
EOT = chr(0x04)
ACK = chr(0x06)
XON = chr(0x11)
XOFF = chr(0x13)
NAK = chr(0x15)
CAN = chr(0x18)
CRC = chr(0x43)

TIMEOUT = None
ZPAD = 0x2a
ZDLE = 0x18
ZDLEE = 0x58
ZBIN = 0x41
ZHEX = 0x42
ZBIN32 = 0x43
ZBINR32 = 0x44
ZVBIN = 0x61
ZVHEX = 0x62
ZVBIN32 = 0x63
ZVBINR32 = 0x64
ZRESC = 0x7e
SOH = b'\x01'
STX = b'\x02'
EOT = b'\x04'
ACK = b'\x06'
XON = b'\x11'
XOFF = b'\x13'
NAK = b'\x15'
CAN = b'\x18'
CRC = b'\x43'

TIMEOUT = None
ZPAD = 0x2a
ZDLE = 0x18
ZDLEE = 0x58
ZBIN = 0x41
ZHEX = 0x42
ZBIN32 = 0x43
ZBINR32 = 0x44
ZVBIN = 0x61
ZVHEX = 0x62
ZVBIN32 = 0x63
ZVBINR32 = 0x64
ZRESC = 0x7e

# ZMODEM Frame types
ZRQINIT = 0x00
ZRINIT = 0x01
ZSINIT = 0x02
ZACK = 0x03
ZFILE = 0x04
ZSKIP = 0x05
ZNAK = 0x06
ZABORT = 0x07
ZFIN = 0x08
ZRPOS = 0x09
ZDATA = 0x0a
ZEOF = 0x0b
ZFERR = 0x0c
ZCRC = 0x0d
ZCHALLENGE = 0x0e
ZCOMPL = 0x0f
ZCAN = 0x10
ZFREECNT = 0x11
ZCOMMAND = 0x12
ZSTDERR = 0x13
ZRQINIT = 0x00
ZRINIT = 0x01
ZSINIT = 0x02
ZACK = 0x03
ZFILE = 0x04
ZSKIP = 0x05
ZNAK = 0x06
ZABORT = 0x07
ZFIN = 0x08
ZRPOS = 0x09
ZDATA = 0x0a
ZEOF = 0x0b
ZFERR = 0x0c
ZCRC = 0x0d
ZCHALLENGE = 0x0e
ZCOMPL = 0x0f
ZCAN = 0x10
ZFREECNT = 0x11
ZCOMMAND = 0x12
ZSTDERR = 0x13

# ZMODEM ZDLE sequences
ZCRCE = 0x68
ZCRCG = 0x69
ZCRCQ = 0x6a
ZCRCW = 0x6b
ZRUB0 = 0x6c
ZRUB1 = 0x6d
ZCRCE = 0x68
ZCRCG = 0x69
ZCRCQ = 0x6a
ZCRCW = 0x6b
ZRUB0 = 0x6c
ZRUB1 = 0x6d

# ZMODEM Receiver capability flags
CANFDX = 0x01
CANOVIO = 0x02
CANBRK = 0x04
CANCRY = 0x08
CANLZW = 0x10
CANFC32 = 0x20
ESCCTL = 0x40
ESC8 = 0x80
CANFDX = 0x01
CANOVIO = 0x02
CANBRK = 0x04
CANCRY = 0x08
CANLZW = 0x10
CANFC32 = 0x20
ESCCTL = 0x40
ESC8 = 0x80

# ZMODEM ZRINIT frame
ZF0_CANFDX = 0x01
ZF0_CANFDX = 0x01
ZF0_CANOVIO = 0x02
ZF0_CANBRK = 0x04
ZF0_CANCRY = 0x08
ZF0_CANLZW = 0x10
ZF0_CANBRK = 0x04
ZF0_CANCRY = 0x08
ZF0_CANLZW = 0x10
ZF0_CANFC32 = 0x20
ZF0_ESCCTL = 0x40
ZF0_ESC8 = 0x80
ZF0_ESCCTL = 0x40
ZF0_ESC8 = 0x80
ZF1_CANVHDR = 0x01

# ZMODEM ZSINIT frame
ZF0_TESCCTL = 0x40
ZF0_TESC8 = 0x80
ZF0_TESC8 = 0x80

# ZMODEM Byte positions within header array
ZF0, ZF1, ZF2, ZF3 = range(4, 0, -1)
ZP0, ZP1, ZP2, ZP3 = range(1, 5)

# ZMODEM Frame contents
ENDOFFRAME = 2
FRAMEOK = 1
TIMEOUT = -1 # Rx routine did not receive a character within timeout
INVHDR = -2 # Invalid header received; but within timeout
INVDATA = -3 # Invalid data subpacket received
ZDLEESC = 0x8000 # One of ZCRCE/ZCRCG/ZCRCQ/ZCRCW was ZDLE escaped
ENDOFFRAME = 2
FRAMEOK = 1
TIMEOUT = -1 # Rx routine did not receive a character within timeout
INVHDR = -2 # Invalid header received; but within timeout
INVDATA = -3 # Invalid data subpacket received
ZDLEESC = 0x8000 # One of ZCRCE/ZCRCG/ZCRCQ/ZCRCW was ZDLE escaped

# MODEM Protocol types
PROTOCOL_XMODEM = 0x00
PROTOCOL_XMODEMCRC = 0x01
PROTOCOL_XMODEM1K = 0x02
PROTOCOL_YMODEM = 0x03
PROTOCOL_ZMODEM = 0x04
PROTOCOL_XMODEM = 0x00
PROTOCOL_XMODEMCRC = 0x01
PROTOCOL_XMODEM1K = 0x02
PROTOCOL_YMODEM = 0x03
PROTOCOL_ZMODEM = 0x04

PACKET_SIZE = {
PROTOCOL_XMODEM: 128,
PROTOCOL_XMODEM: 128,
PROTOCOL_XMODEMCRC: 128,
PROTOCOL_XMODEM1K: 1024,
PROTOCOL_YMODEM: 1024,
PROTOCOL_ZMODEM: 1024,
PROTOCOL_XMODEM1K: 1024,
PROTOCOL_YMODEM: 1024,
PROTOCOL_ZMODEM: 1024,
}

# CRC tab calculated by Mark G. Mendel, Network Systems Corporation
Expand Down
52 changes: 28 additions & 24 deletions modem/error.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
from gettext import gettext as _

ABORT = _('Aborting transfer')
ABORT_WHY = _('Aborting transfer; %s')
ERROR = _('Error')
ERROR_WHY = _('Error; %s')
WARNS = _('Warning')
WARNS_WHY = _('Warnings; %s')
ABORT = _('Aborting transfer')
ABORT_WHY = _('Aborting transfer; %s')
ERROR = _('Error')
ERROR_WHY = _('Error; %s')
WARNS = _('Warning')
WARNS_WHY = _('Warnings; %s')

ABORT_ERROR_LIMIT = ABORT_WHY % _('error limit reached')
ABORT_ERROR_LIMIT = ABORT_WHY % _('error limit reached')
ABORT_EXPECT_NAK_CRC = ABORT_WHY % _('expected <NAK>/<CRC>, got "%02x"')
ABORT_EXPECT_SOH_EOT = ABORT_WHY % _('expected <SOH>/<EOT>, got "%02x"')
ABORT_INIT_NEXT = ABORT_WHY % _('initialisation of next failed')
ABORT_OPEN_FILE = ABORT_WHY % _('error opening file')
ABORT_PACKET_SIZE = ABORT_WHY % _('incompatible packet size')
ABORT_PROTOCOL = ABORT_WHY % _('protocol error')
ABORT_RECV_CAN_CAN = ABORT_WHY % _('second <CAN> received')
ABORT_RECV_PACKET = ABORT_WHY % _('packet recv failed')
ABORT_RECV_STREAM = ABORT_WHY % _('stream recv failed')
ABORT_SEND_PACKET = ABORT_WHY % _('packet send failed')
ABORT_SEND_STREAM = ABORT_WHY % _('stream send failed')
ABORT_INIT_NEXT = ABORT_WHY % _('initialisation of next failed')
ABORT_OPEN_FILE = ABORT_WHY % _('error opening file')
ABORT_PACKET_SIZE = ABORT_WHY % _('incompatible packet size')
ABORT_PROTOCOL = ABORT_WHY % _('protocol error')
ABORT_RECV_CAN_CAN = ABORT_WHY % _('second <CAN> received')
ABORT_RECV_PACKET = ABORT_WHY % _('packet recv failed')
ABORT_RECV_STREAM = ABORT_WHY % _('stream recv failed')
ABORT_SEND_PACKET = ABORT_WHY % _('packet send failed')
ABORT_SEND_STREAM = ABORT_WHY % _('stream send failed')

DEBUG_RECV_CAN = _('First <CAN> received')
DEBUG_SEND_CAN = _('First <CAN> sent')
DEBUG_RECV_CAN = _('First <CAN> received')
DEBUG_SEND_CAN = _('First <CAN> sent')
DEBUG_START_FILENAME = _('Start sending "%s"')
DEBUG_TRY_CRC = _('Try CRC mode')
DEBUG_TRY_CHECKSUM = _('Try check sum mode')
DEBUG_TRY_CRC = _('Try CRC mode')
DEBUG_TRY_CHECKSUM = _('Try check sum mode')
DEBUG_SEND_EOT = _('Send <EOT>')
DEBUG_FILENAME_SENT = _('File name {} sent')
DEBUG_FILE_SENT = _('File {} sent')
DEBUG_SEND_PROGRESS = _('Progress: |{}>{:3.0f}%{}|')

ERROR_EXPECT_NAK_CRC = ERROR_WHY % _('expected <NAK>/<CRC>, got "%02x"')
ERROR_EXPECT_SOH_EOT = ERROR_WHY % _('expected <SOH>/<EOT>, got "%02x"')
ERROR_PROTOCOL = ERROR_WHY % _('protocol error')
ERROR_SEND_EOT = ERROR_WHY % _('failed sending <EOT>')
ERROR_SEND_PACKET = ERROR_WHY % _('failed to send packet')
ERROR_PROTOCOL = ERROR_WHY % _('protocol error')
ERROR_SEND_EOT = ERROR_WHY % _('failed sending <EOT>')
ERROR_SEND_PACKET = ERROR_WHY % _('failed to send packet')

WARNS_SEQUENCE = WARNS_WHY % \
WARNS_SEQUENCE = WARNS_WHY % \
_('invalid sequence; expected %02x got %02x/%02x')
Loading