/* SPDX-License-Identifier: LGPL-2.1-only OR BSD-3-Clause */
/******************************************************************************
 *
 * This file is provided under a dual license.  When you use or
 * distribute this software, you may choose to be licensed under
 * version 2.1 of the GNU Lesser General Public License
 * ("LGPLv2.1 License") or BSD License.
 *
 * LGPLv2.1 License
 *
 * Copyright(C) 2019 MediaTek Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2.1 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This program 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 https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html for more
 * details.
 *
 * BSD LICENSE
 *
 * Copyright(C) 2019 MediaTek Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *  * Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *****************************************************************************/

/*----------------------------------------------------------------------------*/
/*! @file u_sb_dvb_eng.h 
 *  $RCSfile: $
 *  $Revision: #9 $
 *  $Date: 2016/08/15 $
 *  $Author: zhicheng.wang $
 *  
 *  @par Description:
 *         This header file contains exported definitions and delcarations of
 *         SVL Builder DVB engine.
 */
/*----------------------------------------------------------------------------*/
 
#ifndef _U_SB_DVB_CP_ENG_H_
#define _U_SB_DVB_CP_ENG_H_

/*-----------------------------------------------------------------------------
                    include files
 ----------------------------------------------------------------------------*/
#include "u_common.h"
#include "u_tuner.h"

#include "u_sb_dvb_eng.h"

/*----------------------------------------------------------------------------*/
/*! @defgroup groupMW_SVL_BLDR_SB_DVB_ENG SVL Builder DVB engine
 *
 *  @ingroup groupMW_SVL_BLDR
 *  @brief The DVB-T engine module is used to scan all digital channel in DVB-T spec.
 *
 *  %MW Application API would be used by Application, lot of these APIs would
 *   be wrapped from Exported API. Using these APIs, application could create scan engine
 *   or start scan from the exported API of DVB-T svl builder.
 *
 *  @see groupMW_SVL_BLDR
 *  @{ 
 */
/*----------------------------------------------------------------------------*/

/*-----------------------------------------------------------------------------
                    macros, defines, typedefs, enums
 ----------------------------------------------------------------------------*/
#include "u_common.h"
#include "u_tuner.h"

#include "u_sb_dvb_eng.h"


#if 1
#define DVB_CP_ENG_NAME             "sb_dvb_cp"    /**< Define the DVB build engine name */
#else
#define DVB_CP_ENG_NAME             DVB_ENG_NAME    /**< Define the DVB build engine name */
#endif

#define DVB_CP_ENG_SATELLITE_START_REC_ID                   ((UINT32)  0xCACA)
#define DVB_CP_ENG_SATELLITE_START_NAME                     "CA_reserved"


#define SB_DVB_CP_LOL_RECORD_ID_FOR_BASE                    ((UINT8)   (100))
#define SB_DVB_CP_LOL_RECORD_ID_FOR_CONTENT_LABEL           ((UINT8)   (1 + SB_DVB_CP_LOL_RECORD_ID_FOR_BASE))
#define SB_DVB_CP_LOL_RECORD_ID_FOR_VIRTUAL_CHANNEL         ((UINT8)   (1 + SB_DVB_CP_LOL_RECORD_ID_FOR_CONTENT_LABEL))
#define SB_DVB_CP_LOL_RECORD_ID_FOR_INTERNAL_1              ((UINT8)   (1 + SB_DVB_CP_LOL_RECORD_ID_FOR_VIRTUAL_CHANNEL))   /* following 4 lol records are used to store original service name bytes */
#define SB_DVB_CP_LOL_RECORD_ID_FOR_INTERNAL_2              ((UINT8)   (1 + SB_DVB_CP_LOL_RECORD_ID_FOR_INTERNAL_1))
#define SB_DVB_CP_LOL_RECORD_ID_FOR_INTERNAL_3              ((UINT8)   (1 + SB_DVB_CP_LOL_RECORD_ID_FOR_INTERNAL_2))
#define SB_DVB_CP_LOL_RECORD_ID_FOR_INTERNAL_4              ((UINT8)   (1 + SB_DVB_CP_LOL_RECORD_ID_FOR_INTERNAL_3))


#define SB_DVB_CP_LOL_DATA_TAG_BASE                         ((UINT32)  (100))
#define SB_DVB_CP_LOL_DATA_TAG_CONTENT_LABEL                ((UINT32)  (1 + SB_DVB_CP_LOL_DATA_TAG_BASE))
#define SB_DVB_CP_LOL_DATA_TAG_VIRTUAL_CHANNEL              ((UINT32)  (1 + SB_DVB_CP_LOL_DATA_TAG_CONTENT_LABEL))
#define SB_DVB_CP_LOL_DATA_TAG_INTERNAL_1                   ((UINT32)  (1 + SB_DVB_CP_LOL_DATA_TAG_VIRTUAL_CHANNEL))
#define SB_DVB_CP_LOL_DATA_TAG_INTERNAL_2                   ((UINT32)  (1 + SB_DVB_CP_LOL_DATA_TAG_INTERNAL_1))
#define SB_DVB_CP_LOL_DATA_TAG_INTERNAL_3                   ((UINT32)  (1 + SB_DVB_CP_LOL_DATA_TAG_INTERNAL_2))
#define SB_DVB_CP_LOL_DATA_TAG_INTERNAL_4                   ((UINT32)  (1 + SB_DVB_CP_LOL_DATA_TAG_INTERNAL_3))


#define SB_DVB_CP_TSL_ID_START                              ((UINT16)  (0xFFFF-1000))   /* CAM and broadcast svc TSL id are in sperate range */


#define SB_DVB_CP_CONFIG_USER_TUNE_INFO                     (MAKE_BIT_MASK_32(0))
#define SB_DVB_CP_CONFIG_CAM_NIT                            (MAKE_BIT_MASK_32(1))
#define SB_DVB_CP_CONFIG_USER_OPERATOR                      (MAKE_BIT_MASK_32(2))
#define SB_DVB_CP_CONFIG_TBL_SRC                            (MAKE_BIT_MASK_32(3))
#define SB_DVB_CP_CONFIG_USER_OPERATOR_NFY                  (MAKE_BIT_MASK_32(4))
#define SB_DVB_CP_CONFIG_DEL_SVL_BEFORE_UPDATING            (MAKE_BIT_MASK_32(5)) 


typedef enum _SB_DVB_DELIVERY_TYPE
{
    SB_DVB_DELIVERY_UNKNOWN = 0,
    SB_DVB_DELIVERY_DVBC,
    SB_DVB_DELIVERY_DVBC2,
    SB_DVB_DELIVERY_DVBS,
    SB_DVB_DELIVERY_DVBS2,
    SB_DVB_DELIVERY_DVBT,
    SB_DVB_DELIVERY_DVBT2,
    SB_DVB_DELIVERY_DVBSH,
    SB_DVB_DELIVERY_LAST_ONE
}   SB_DVB_DELIVERY_TYPE;


typedef struct _SB_DVB_C_DELIVERY_T
{
    UINT32                          ui4_frequency;
    UINT8                           ui1_FEC_outer;    
    TUNER_MODULATION_T              e_mod;
    UINT32                          ui4_symbol_rate;
    UINT8                           ui1_FEC_inner;
}   SB_DVB_C_DELIVERY_T;


typedef struct _SB_DVB_C2_DELIVERY_T
{
    UINT8                           ui1_plp_id;
    UINT8                           ui1_data_slice_id;
    UINT32                          ui4_C2_tuning_frequency;
    UINT8                           ui1_C2_tuning_frequency_type;
    UINT8                           ui1_active_OFDM_symbol_duration;
    UINT8                           ui1_guard_interval;
}   SB_DVB_C2_DELIVERY_T;


typedef struct _SB_DVB_S_DELIVERY_T
{
    UINT32                          ui4_frequency;          /* MHZ */
    UINT16                          ui2_orbital_position;   /* 10 degree */
    UINT8                           ui1_west_east_flag;     /* "1" indicates the eastern position */
    TUNER_POLARIZATION_T            t_polarization;
    UINT8                           ui1_roll_off;
    UINT8                           ui1_modulation_system;
    UINT8                           ui1_modulation_type;
    UINT32                          ui4_symbol_rate;        /* Ksymbol/s */
    UINT8                           ui1_FEC_inner;
}   SB_DVB_S_DELIVERY_T;


typedef struct _SB_DVB_S2_DELIVERY_T
{
    UINT8                           ui1_scrambling_sequence_selector;
    UINT8                           ui1_multiple_input_stream_flag;
    UINT8                           ui1_backwards_compatibility_indicator;
    UINT32                          ui4_scrambling_sequence_index;
    UINT8                           ui1_input_stream_identifier;
}   SB_DVB_S2_DELIVERY_T;


typedef struct _SB_DVB_T_DELIVERY_T
{
    UINT32                          ui4_centre_frequency;
    TUNER_BANDWIDTH_T               e_bandwidth;
    UINT8                           ui1_priority;
    UINT8                           ui1_time_slicing_indicator;
    UINT8                           ui1_MPE_FEC_indicator;
    UINT8                           ui1_constellation;
    UINT8                           ui1_hierarchy_information;
    UINT8                           ui1_code_rate_HP_stream;
    UINT8                           ui1_code_rate_LP_stream;
    UINT8                           ui1_guard_interval;
    UINT8                           ui1_transmission_mode;
    UINT8                           ui1_other_frequency_flag;
}   SB_DVB_T_DELIVERY_T;


typedef struct _SB_DVB_T2_DELIVERY_T
{
    UINT8                           ui1_plp_id;
    UINT16                          ui2_T2_system_id;
    BOOL                            fg_is_len_bigger_than4;
    
    UINT8                           ui1_SISO_MISO;
    TUNER_BANDWIDTH_T               e_bandwidth;
    UINT8                           ui1_reserved_future_use;
    UINT8                           ui1_guard_interval;
    UINT8                           ui1_transmission_mode;
    UINT8                           ui1_other_frequency_flag;
    UINT8                           ui1_tfs_flag;
    UINT16                          ui2_cell_id;
    UINT32                          ui4_centre_frequency;
}   SB_DVB_T2_DELIVERY_T;


typedef struct _SB_DVB_DELIVERY_T
{
    SB_DVB_DELIVERY_TYPE            t_type;
    
    union {
        SB_DVB_T_DELIVERY_T         t_t_delivery;
        SB_DVB_C_DELIVERY_T         t_c_delivery;
        SB_DVB_S_DELIVERY_T         t_s_delivery;
        SB_DVB_T2_DELIVERY_T        t_t2_delivery;
        SB_DVB_C2_DELIVERY_T        t_c2_delivery;
        SB_DVB_S2_DELIVERY_T        t_s2_delivery;
    } data;
}   SB_DVB_DELIVERY_T;


typedef enum _SB_DVB_CP_CUR_SIGNAL_STATUS
{
    DVB_CP_SIGNAL_STATUS_UNKNOWN    = 0,
    DVB_CP_SIGNAL_STATUS_AVAILABLE,
    DVB_CP_SIGNAL_STATUS_NO_SUPPORT,
    DVB_CP_SIGNAL_STATUS_PARA_INVALID,
    DVB_CP_SIGNAL_STATUS_NO_SIGNAL,
    DVB_CP_SIGNAL_STATUS_LAST_ONE
}   SB_DVB_CP_CUR_SIGNAL_STATUS;


typedef struct _SB_DVB_CP_USER_OP_DATA_T
{
    UINT8                           ui1_signal_strength;
    UINT8                           ui1_signal_quality;
    SB_DVB_CP_CUR_SIGNAL_STATUS     t_signal_status;
    UINT16                          ui2_descripor_number;
    UINT8*                          pui1_descriptor;
    UINT16                          ui2_descriptor_len;
} SB_DVB_CP_USER_OP_DATA_T;


typedef struct _SB_DVB_CP_OP_NIT_MNGT_T
{
    UINT8*                          pui1_nit_mngt;
    UINT32                          ui4_nit_mngt_size;
}   SB_DVB_CP_OP_NIT_MNGT_T;


typedef struct _SB_DVB_CP_SVC_UPDATED_T
{
    UINT32                          ui4_reserved;
}   SB_DVB_CP_SVC_UPDATED_T;


typedef enum _SB_DVB_CP_NFY_TYPE_T
{
    DVB_CP_NFY_TYPE_UNKNOWN    = 0,
    DVB_CP_NFY_TYPE_USER_OP,
    DVB_CP_NFY_TYPE_NIT_MNGT,
    DVB_CP_NFY_TYPE_SVC_UPDATED,
    DVB_CP_NFY_TYPE_OSDT_REQ,
    DVB_CP_NFY_TYPE_LAST_ONE
}   SB_DVB_CP_NFY_TYPE_T;


typedef struct _SB_DVB_CP_NFY_DATA_T
{
    SB_DVB_CP_NFY_TYPE_T            t_nfy_type;
    
    union {
        SB_DVB_CP_USER_OP_DATA_T        t_dvbcp_user_op;
        SB_DVB_CP_OP_NIT_MNGT_T         t_op_nit_mngt;
        SB_DVB_CP_SVC_UPDATED_T         t_svc_updated;
    } data;
}   SB_DVB_CP_NFY_DATA_T;


typedef VOID (*FUNC_DVB_CP_NFY) (
      SB_DVB_CP_NFY_DATA_T*     pt_nfy_data,
      VOID*                     pv_tag );


typedef UINT32                      SB_DVB_SRC_TBL_MASK;
#define SB_DVB_SRC_NONE_MASK        ((UINT32) 0)
#define SB_DVB_SRC_SDT_MASK         MAKE_BIT_MASK_32 (0)
#define SB_DVB_SRC_NIT_MASK         MAKE_BIT_MASK_32 (1)
#define SB_DVB_SRC_CAM_SDT_MASK     MAKE_BIT_MASK_32 (2)
#define SB_DVB_SRC_CAM_NIT_MASK     MAKE_BIT_MASK_32 (3)

#define SB_DVB_SRC_TABLE            (SB_DVB_SRC_SDT_MASK | SB_DVB_SRC_NIT_MASK)


#define SB_DVB_CP_STRING_PREFIX_MAX_LEN     10
typedef struct _SB_DVB_CP_STRING_PREFIX_T
{
    CHAR                            ac_prefix[SB_DVB_CP_STRING_PREFIX_MAX_LEN];
    UINT16                          ui2_valid_len;
}   SB_DVB_CP_STRING_PREFIX_T;


typedef enum _SB_DVB_CP_CAM_PROFILE_TYPE
{
    SB_DVB_CP_CAM_PROFILE_TYPE_UNKNOWN  = 0,
    SB_DVB_CP_CAM_PROFILE_TYPE_0        = 1,
    SB_DVB_CP_CAM_PROFILE_TYPE_1        = 2,
    SB_DVB_CP_CAM_PROFILE_TYPE_2        = 3
}   SB_DVB_CP_CAM_PROFILE_TYPE;


typedef struct _SB_DVB_CP_CAM_PROFILE_2_T
{
    UINT16                          ui2_brdcst_svl_id;
}   SB_DVB_CP_CAM_PROFILE_2_T;


typedef struct _SB_DVB_CP_CAM_PROFILE_T
{
    SB_DVB_CP_CAM_PROFILE_TYPE      t_profile_type;
    
    union {
        SB_DVB_CP_CAM_PROFILE_2_T       t_profile_2;
        UINT32                          ui4_reserved;
    } data;
}   SB_DVB_CP_CAM_PROFILE_T;


typedef struct _SB_DVB_CP_SCAN_DATA_T
{
    SB_DVB_SCAN_DATA_T              t_dvbt_scan_data;
    SB_DVB_CONFIG_T                 t_cp_eng_cfg;
    SB_DVB_SRC_TBL_MASK             t_table_mask;
    FUNC_DVB_CP_NFY                 pf_dvb_cp_nfy;
    VOID*                           pv_dvb_cp_nfy_tag;
    SB_DVB_CP_STRING_PREFIX_T       t_cp_string_prefix;
    UINT16                          ui2_refer_brdcst_satl_id;
    SB_DVB_CP_CAM_PROFILE_TYPE      t_profile_type;
} SB_DVB_CP_SCAN_DATA_T;


#define CP_ENG_SCAN_CFG(x)      (x & pt_cp_scan_data->t_cp_eng_cfg)
#define CP_ENG_CFG(x)           (x & pt_cp_eng_data->t_user_scan_data.t_cp_eng_cfg)


typedef struct _SB_DVB_CAM_DELIVERY_T
{
    UINT16                          ui2_descripor_number;
    UINT16                          ui2_desc_len;
    UINT8*                          pui1_delivery_desc;
}   SB_DVB_CAM_DELIVERY_T;


typedef struct _SB_DVB_CP_CONN_T
{
    BOOL                            fg_connected;
    SB_DVB_CAM_DELIVERY_T*          pt_cur_cam_delivery;    /* CAM operator_tune_status */
    
    BRDCST_TYPE_T                   e_bcst_type;
    BRDCST_MEDIUM_T                 e_bcst_medium;
    UINT16                          ui2_satl_id;            /* valid when BRDCST_MEDIUM_DIG_SATELLITE == e_bcst_medium */
    UINT16                          ui2_satl_rec_id;        /* valid when BRDCST_MEDIUM_DIG_SATELLITE == e_bcst_medium*/
    
    SB_DVB_DELIVERY_T               t_delivery;
    INT32                           i4_cur_ssi;
    INT32                           i4_cur_sqi;
}   SB_DVB_CP_CONN_T;


typedef struct _SB_DVB_CP_TUNE_STATUS_T
{
    SB_DVB_CP_CONN_T                t_cp_conn;

    UINT8*                          pui1_delivery_desc;
    UINT16                          ui2_delivery_desc_len;
}   SB_DVB_CP_TUNE_STATUS_T;


#define         MAX_DVB_CP_CHAR_LEN            40
typedef struct _SB_DVB_CP_CONTENT_LABEL_T
{
    UINT8                           ui1_content_byte_min;
    UINT8                           ui1_content_byte_max;
    ISO_639_LANG_T                  t_lang_code;
    CHAR                            s_label_char[MAX_DVB_CP_CHAR_LEN];
}   SB_DVB_CP_CONTENT_LABEL_T;


typedef struct _SB_DVB_CP_SVC_VI_CH_T
{
    CHAR                            s_event_information_char[256 + 1];
    UINT16                          ui2_app_domain_id_len;
    UINT8                           aui1_app_domain_identifier[256 + 1];
}   SB_DVB_CP_SVC_VI_CH_T;


typedef struct _SB_DVB_CP_OPEN_DATA_T
{
    SB_DVB_OPEN_DATA_T              t_dvb_open_data;
    SB_DVB_DELIVERY_TYPE            t_delivery_type;
    SB_DVB_CP_CAM_PROFILE_2_T       t_profile_2;
    UINT32                          ui4_nit_timeout;
    UINT32                          ui4_osdt_timeout;
} SB_DVB_CP_OPEN_DATA_T;


#endif /* _U_SB_DVB_CP_ENG_H_ */

/*----------------------------------------------------------------------------*/
/*! @} */                               /* end of groupMW_SVL_BLDR_SB_DVB_ENG */
/*----------------------------------------------------------------------------*/

