# Path: conpot/utils/networking.py
# def str_to_bytes(x):
#     return x if isinstance(x, bytes) else str(x).encode("ascii")


import struct
import logging
from modbus_tk.modbus import (
    Slave,
    ModbusError,
    ModbusInvalidRequestError,
    InvalidArgumentError,
    DuplicatedKeyError,
    InvalidModbusBlockError,
    OverlapModbusBlockError,
)
from modbus_tk import defines, utils
from conpot.utils.networking import str_to_bytes
from .modbus_block_databus_mediator import ModbusBlockDatabusMediator
        response += struct.pack(">B", 1)  # slave id
        response += struct.pack(">B", 0xFF)  # run status, OxFF on, 0x00 off
        return response

    def _device_info(self, request_pdu):
        info_root = self.dom.xpath("//modbus/device_info")[0]
        vendor_name = info_root.xpath("./VendorName/text()")[0]
        product_code = info_root.xpath("./ProductCode/text()")[0]
        major_minor_revision = info_root.xpath("./MajorMinorRevision/text()")[0]

        (req_device_id, _) = struct.unpack(">BB", request_pdu[2:4])
        device_info = {0: vendor_name, 1: product_code, 2: major_minor_revision}

        # MEI type
        response = struct.pack(">B", 0x0E)
        # requested device id
        response += struct.pack(">B", req_device_id)
        # conformity level
        response += struct.pack(">B", 0x01)
        # followup data 0x00 is False
        response += struct.pack(">B", 0x00)
        # No next object id
        response += struct.pack(">B", 0x00)
        # Number of objects
        response += struct.pack(">B", len(device_info))
        for i in range(len(device_info)):
            # Object id
            response += struct.pack(">B", i)
            # Object length
            response += struct.pack(">B", len(device_info[i]))

# Path: conpot/utils/networking.py
# def str_to_bytes(x):
#     return x if isinstance(x, bytes) else str(x).encode("ascii")


# Path: conpot/protocols/modbus/slave.py
import struct
import logging
from modbus_tk.modbus import (
    Slave,
    ModbusError,
    ModbusInvalidRequestError,
    InvalidArgumentError,
    DuplicatedKeyError,
    InvalidModbusBlockError,
    OverlapModbusBlockError,
)
from modbus_tk import defines, utils
from conpot.utils.networking import str_to_bytes
from .modbus_block_databus_mediator import ModbusBlockDatabusMediator

        response += struct.pack(">B", 1)  # slave id
        response += struct.pack(">B", 0xFF)  # run status, OxFF on, 0x00 off
        return response

    def _device_info(self, request_pdu):
        info_root = self.dom.xpath("//modbus/device_info")[0]
        vendor_name = info_root.xpath("./VendorName/text()")[0]
        product_code = info_root.xpath("./ProductCode/text()")[0]
        major_minor_revision = info_root.xpath("./MajorMinorRevision/text()")[0]

        (req_device_id, _) = struct.unpack(">BB", request_pdu[2:4])
        device_info = {0: vendor_name, 1: product_code, 2: major_minor_revision}

        # MEI type
        response = struct.pack(">B", 0x0E)
        # requested device id
        response += struct.pack(">B", req_device_id)
        # conformity level
        response += struct.pack(">B", 0x01)
        # followup data 0x00 is False
        response += struct.pack(">B", 0x00)
        # No next object id
        response += struct.pack(">B", 0x00)
        # Number of objects
        response += struct.pack(">B", len(device_info))
        for i in range(len(device_info)):
            # Object id
            response += struct.pack(">B", i)
            # Object length
            response += struct.pack(">B", len(device_info[i]))

            response += str_to_bytes(device_info[i])