# Copyright (c) 2021 Satlab A/S <satlab@satlab.com>
#
# This is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this software; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.

import math


def frame_size(size, crc, rs, cc, idnum, crypt, auth, cc_term=True, include_asm=False):
    # Add header
    rx_size = size + 2
    # Add ID
    if idnum > 0:
        rx_size += 2
    # Add crypto
    if crypt or auth:
        rx_size += 12
    if auth:
        rx_size += 16
    # Add CRC
    if crc:
        rx_size += 4
    # Add RS
    if rs:
        blocks = int(math.ceil(rx_size / 223.0))
        rx_size += blocks * 32
    # Add ASM
    if include_asm:
        rx_size += 4
    # Add convolutional code
    if cc:
        rx_size = (rx_size + (1 if cc_term else 0)) * 2
    return int(rx_size)
