Init Commit: Moved bproto to seperate repo
This commit is contained in:
67
template/python/static/bproto_base.py
Normal file
67
template/python/static/bproto_base.py
Normal file
@@ -0,0 +1,67 @@
|
||||
import abc
|
||||
|
||||
|
||||
class bproto_Package(abc.ABC):
|
||||
@abc.abstractmethod
|
||||
def to_bytes(self) -> bytes:
|
||||
raise NotImplementedError()
|
||||
|
||||
@abc.abstractmethod
|
||||
def from_bytes(self, data: bytes):
|
||||
raise NotImplementedError()
|
||||
|
||||
|
||||
class bproto_Bitfield(abc.ABC):
|
||||
@abc.abstractmethod
|
||||
def to_bytes(self) -> bytes:
|
||||
pass
|
||||
|
||||
@abc.abstractmethod
|
||||
def from_bytes(self, data: bytes):
|
||||
pass
|
||||
|
||||
def __str__(self):
|
||||
return self.__repr__()
|
||||
|
||||
|
||||
def crc16_calc_inital(data: bytearray) -> int:
|
||||
"""CRC-16/CITT-FALSE
|
||||
from https://stackoverflow.com/questions/35205702/calculating-crc16-in-python
|
||||
|
||||
Args:
|
||||
data (bytearray): protocol hash
|
||||
|
||||
Returns:
|
||||
bytes: initals for crc16_calc_with_initial
|
||||
"""
|
||||
crc = 0xFFFF
|
||||
for i in range(0, len(data)):
|
||||
crc ^= data[i] << 8
|
||||
for j in range(0, 8):
|
||||
if (crc & 0x8000) > 0:
|
||||
crc = (crc << 1) ^ 0x1021
|
||||
else:
|
||||
crc = crc << 1
|
||||
return crc
|
||||
|
||||
|
||||
def calc_crc_sum(data: bytearray, inital: int) -> bytes:
|
||||
"""CRC-16/CITT-FALSE
|
||||
form https://stackoverflow.com/questions/35205702/calculating-crc16-in-python
|
||||
|
||||
Args:
|
||||
data (bytearray): protocol data
|
||||
inital (int): Inital calculated from protocol hash
|
||||
|
||||
Returns:
|
||||
bytes: 2 byte CRC Checksum
|
||||
"""
|
||||
crc = inital
|
||||
for i in range(0, len(data)):
|
||||
crc ^= data[i] << 8
|
||||
for j in range(0, 8):
|
||||
if (crc & 0x8000) > 0:
|
||||
crc = (crc << 1) ^ 0x1021
|
||||
else:
|
||||
crc = crc << 1
|
||||
return crc & 0xFFFF
|
||||
19
template/python/static/bproto_error.py
Normal file
19
template/python/static/bproto_error.py
Normal file
@@ -0,0 +1,19 @@
|
||||
class bproto_Error(Exception):
|
||||
def __init__(self, message):
|
||||
self.message = message
|
||||
|
||||
|
||||
class bproto_PackageError(bproto_Error):
|
||||
pass
|
||||
|
||||
|
||||
class bproto_PackageErrorInvalidSize(bproto_PackageError):
|
||||
pass
|
||||
|
||||
|
||||
class bproto_PackageErrorInvalidMessageID(bproto_PackageError):
|
||||
pass
|
||||
|
||||
|
||||
class bproto_PackageErrorInvalidCRC(bproto_PackageError):
|
||||
pass
|
||||
Reference in New Issue
Block a user