# Path: conpot/utils/networking.py
# def chr_py3(x):
#     return bytearray((x,))
# 
# Path: conpot/protocols/kamstrup_meter/messages.py
# class KamstrupRequestGetRegisters(KamstrupRequestBase):
#     command_byte = 0x10
# 
#     def __init__(self, communication_address, command_byte, message_bytes):
#         assert command_byte is command_byte
#         super(KamstrupRequestGetRegisters, self).__init__(
#             communication_address,
#             KamstrupRequestGetRegisters.command_byte,
#             message_bytes,
#         )
#         self.registers = []
#         self._parse_register_bytes()
#         logger.debug(
#             "Kamstrup request for registers: %s", str(self.registers).strip("[]")
#         )
# 
#     def _parse_register_bytes(self):
#         register_count = self.message_bytes[0]
#         if len(self.message_bytes[1:]) * 2 < register_count:
#             raise Exception("Invalid register count in register request")
#         for count in range(register_count):
#             register = (
#                 self.message_bytes[1 + count * 2] * 256
#                 + self.message_bytes[2 + count * 2]
#             )
#             self.registers.append(register)
# 
# class KamstrupRequestUnknown(KamstrupRequestBase):
#     def __init__(self, communication_address, command_byte, message_bytes):
#         super(KamstrupRequestUnknown, self).__init__(
#             communication_address, command_byte, message_bytes
#         )
#         logger.warning("Unknown Kamstrup request: %s", self)


import logging
from crc16.crc16pure import crc16xmodem
from conpot.utils.networking import chr_py3
from . import kamstrup_constants
from .messages import KamstrupRequestGetRegisters, KamstrupRequestUnknown

                escape_escape_byte = False
                if self.data_escaped:
                    self.bytes[position] ^= 0xFF
                    if d is kamstrup_constants.EOT_MAGIC:
                        escape_escape_byte = True
                    self.data_escaped = False
                elif d is kamstrup_constants.ESCAPE:
                    self.data_escaped = True
                    del self.bytes[position]
                    bytes_len -= 1
                    continue

                assert self.data_escaped is False

                if d is kamstrup_constants.EOT_MAGIC and not escape_escape_byte:
                    if not self.valid_crc(self.bytes[1:position]):
                        self.parsing = False
                        del self.bytes[0:position]
                        logger.warning("Kamstrup CRC check failed for request.")
                    # now we expect (0x80, 0x3f, 0x10) =>
                    # (request magic, communication address, command byte)
                    comm_address = self.bytes[1]
                    command_byte = self.bytes[2]
                    if self.bytes[2] in self.request_map:
                        result = self.request_map[command_byte](
                            comm_address, command_byte, self.bytes[3:-3]
                        )
                        del self.bytes[: position + 1]
                    else:

# Path: conpot/utils/networking.py
# def chr_py3(x):
#     return bytearray((x,))
# 
# Path: conpot/protocols/kamstrup_meter/messages.py
# class KamstrupRequestGetRegisters(KamstrupRequestBase):
#     command_byte = 0x10
# 
#     def __init__(self, communication_address, command_byte, message_bytes):
#         assert command_byte is command_byte
#         super(KamstrupRequestGetRegisters, self).__init__(
#             communication_address,
#             KamstrupRequestGetRegisters.command_byte,
#             message_bytes,
#         )
#         self.registers = []
#         self._parse_register_bytes()
#         logger.debug(
#             "Kamstrup request for registers: %s", str(self.registers).strip("[]")
#         )
# 
#     def _parse_register_bytes(self):
#         register_count = self.message_bytes[0]
#         if len(self.message_bytes[1:]) * 2 < register_count:
#             raise Exception("Invalid register count in register request")
#         for count in range(register_count):
#             register = (
#                 self.message_bytes[1 + count * 2] * 256
#                 + self.message_bytes[2 + count * 2]
#             )
#             self.registers.append(register)
# 
# class KamstrupRequestUnknown(KamstrupRequestBase):
#     def __init__(self, communication_address, command_byte, message_bytes):
#         super(KamstrupRequestUnknown, self).__init__(
#             communication_address, command_byte, message_bytes
#         )
#         logger.warning("Unknown Kamstrup request: %s", self)


# Path: conpot/protocols/kamstrup_meter/request_parser.py
import logging
from crc16.crc16pure import crc16xmodem
from conpot.utils.networking import chr_py3
from . import kamstrup_constants
from .messages import KamstrupRequestGetRegisters, KamstrupRequestUnknown


                escape_escape_byte = False
                if self.data_escaped:
                    self.bytes[position] ^= 0xFF
                    if d is kamstrup_constants.EOT_MAGIC:
                        escape_escape_byte = True
                    self.data_escaped = False
                elif d is kamstrup_constants.ESCAPE:
                    self.data_escaped = True
                    del self.bytes[position]
                    bytes_len -= 1
                    continue

                assert self.data_escaped is False

                if d is kamstrup_constants.EOT_MAGIC and not escape_escape_byte:
                    if not self.valid_crc(self.bytes[1:position]):
                        self.parsing = False
                        del self.bytes[0:position]
                        logger.warning("Kamstrup CRC check failed for request.")
                    # now we expect (0x80, 0x3f, 0x10) =>
                    # (request magic, communication address, command byte)
                    comm_address = self.bytes[1]
                    command_byte = self.bytes[2]
                    if self.bytes[2] in self.request_map:
                        result = self.request_map[command_byte](
                            comm_address, command_byte, self.bytes[3:-3]
                        )
                        del self.bytes[: position + 1]
                    else:

                        result = KamstrupRequestUnknown(