import ctypes
import struct

class UDP1553Header(ctypes.Structure):
    _pack_ = 1  # Forza l'allineamento a 1 byte
    _fields_ = [
        ("marker1553", ctypes.c_uint16),
        ("vmajor", ctypes.c_uint8),
        ("vminor", ctypes.c_uint8),
        ("otype", ctypes.c_uint16),
        ("ta", ctypes.c_uint8),
        ("flags", ctypes.c_uint8),
        ("fcounter", ctypes.c_uint32),
        ("mcounter", ctypes.c_uint32),
        ("scounter", ctypes.c_uint32),
        ("mmiss", ctypes.c_uint32),
        ("smiss", ctypes.c_uint32),
        ("ltt", ctypes.c_uint32),
        ("errors", ctypes.c_uint32),
        ("bc_reserved", ctypes.c_uint32 * 2),
        ("rt_reserved", ctypes.c_uint32 * 2),
        ("spare", ctypes.c_uint32 * 3)
    ]

    k_marker_1553 = 0x1553
    k_marker_end_1553 = 0x5315
    k_otype_bc = 0x4342
    k_otype_rt = 0x5452
    k_ctrl_marker_begin = 0x3C3C
    k_ctrl_marker_end = 0x3E3E

    k_err_unsupported_version = 1
    k_err_invalid_source = 2
    k_err_invalid_len = 3
    k_err_missed_msg_marker = 4
    

    def __init__(self):
        super().__init__()
        self.marker1553 = self.k_marker_1553
        self.otype = self.k_otype_bc
        self.ltt = 0

class udp_1553_msg_t(ctypes.Structure):
    _pack_ = 1
    _fields_ = [
        ("marker", ctypes.c_uint16),
        ("cw", ctypes.c_uint16),
        ("sw", ctypes.c_uint16),
        ("errcode", ctypes.c_uint16)
    ]

class cw_str(ctypes.Structure):
    _pack_ = 1
    _fields_ = [
        ("wc", ctypes.c_ushort,5),
        ("sa", ctypes.c_ushort,5),
        ("tr", ctypes.c_ushort,1),
        ("rt", ctypes.c_ushort,5)
    ]

class cw(ctypes.Union):
    _pack_ = 1  # Forza l'allineamento a 1 byte
    _fields_ = [
        ("raw", ctypes.c_uint16),
        ("str", cw_str)
    ]

    # e_ok = 0
    # e_msg_err = 1
    # e_incomplete = 2

    def __init__(self, wc, sa, tr, rt):
        super().__init__()
        self.str = cw_str()
        self.str.wc = wc if not wc == 32 else 0
        self.str.sa = sa
        self.str.tr = tr
        self.str.rt = rt
        # self.rt = 20
        
        
        
        
class sw_str(ctypes.Structure):
    _pack_ = 1
    _fields_ = [
        ("tf", ctypes.c_ushort,1),
        ("bca", ctypes.c_ushort,1),
        ("sf", ctypes.c_ushort,1),
        ("busy", ctypes.c_ushort,1),
        ("brcs", ctypes.c_ushort,1),
        ("reserved", ctypes.c_ushort,3),
        ("sr", ctypes.c_ushort,1),
        ("instr", ctypes.c_ushort,1),
        ("me", ctypes.c_ushort,1),
        ("rt", ctypes.c_ushort,5),

    ]

class sw(ctypes.Union):
    _pack_ = 1  # Forza l'allineamento a 1 byte
    _fields_ = [
        ("raw", ctypes.c_uint16),
        ("str", cw_str)
    ]
        
  

class UDP1553Message(ctypes.Structure):
    _pack_ = 1  # Forza l'allineamento a 1 byte
    _fields_ = [
        ("marker", ctypes.c_uint16),
        ("cw", cw),
        ("sw", ctypes.c_uint16),
        ("errcode", ctypes.c_uint16)
    ]

    e_ok = 0
    e_msg_err = 1
    e_incomplete = 2

    def __init__(self, marker, cw, sw = 0, errcode = 0):
        super().__init__()
        self.marker = marker
        self.cw = cw
        self.errcode = self.e_ok

# import sys
# from pympler import asizeof
# print(f"size of str: {asizeof.asizeof(1)}")