/* 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_satl.h
 *  $File: //DTV/PROD_BR/DTV_X_IDTV1501/apollo/middleware/public/bsl/u_bsl.h $
 *  $Revision: #1 $
 *  $Date: 2015/05/29 $
 *  $Author: brianpc.huang $
 *  
 *  @par Description:
 *    This header file contains Satellite list (SATL) database
 *    API definitions, which are exported to other Middleware components
 *    and applications.
 *         
 */
/*----------------------------------------------------------------------------*/

#ifndef _U_BSL_H_
#define _U_BSL_H_
/*----------------------------------------------------------------------------*/
/*! @defgroup   groupMW_SATL Satellite List
 *  @ingroup    groupMW
 *  @brief      The SATL module is used to store and retrieve the tuning
 *              parameter information for the broadcast streams.
 *  @see        groupMW_CDB
 *  @{
 */
/*----------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------
                    include files
----------------------------------------------------------------------------*/

#include "u_common.h"
#include "u_handle.h"
#include "u_handle_grp.h"
#include "u_tuner.h"


/*--------------------------------------------------------------------------
 Constants, enumerations, and macros
---------------------------------------------------------------------------*/

/* Return code status from API */
#define     BSLR_OK                   ((INT32)  0)    /**< Operation succeeded. */
#define     BSLR_FAIL                 ((INT32) -1)    /**< Operation failed. */
#define     BSLR_OUT_OF_HANDLE        ((INT32) -2)    /**< Handle allocation failed. */
#define     BSLR_OUT_OF_MEM           ((INT32) -3)    /**< Memory exhausted. */
#define     BSLR_INV_HANDLE           ((INT32) -4)    /**< Bad handle. */
#define     BSLR_INV_ARG              ((INT32) -5)    /**< Invalid argument. */
#define     BSLR_REC_NOT_FOUND        ((INT32) -6)    /**< Record not found. */
#define     BSLR_NO_WRITE_LOCK        ((INT32) -7)    /**< Failed initializing RW Lock. */
#define     BSLR_NOT_FOUND            ((INT32) -8)    /**< Specified SATL object not found. */
#define     BSLR_END_OF_ITERATION     ((INT32) -9)    /**< Iteration reached end. */
#define     BSLR_MODIFIED             ((INT32) -10)    /**< Database modified since last iteration. */
#define     BSLR_DUPLICATE_REC        ((INT32) -11)    /**< Existing record with the same key value is found. */
#define     BSLR_INV_FILE_PATH        ((INT32) -12)    /**< Invalid file path. */
#define     BSLR_OUT_OF_SPACE         ((INT32) -13)    /**< Failed to allocate space on file system. */
#define     BSLR_AEE_OUT_OF_RESOURCES ((INT32) -14)    /**< Can not allocate AEE resources. */
#define     BSLR_AEE_NO_RIGHTS        ((INT32) -15)    /**< No right for the AEE resources. */
#define     BSLR_FILE_READ_FAIL       ((INT32) -16)    /**< Failed to read from file. */
#define     BSLR_FILE_WRITE_FAIL      ((INT32) -17)    /**< Failed to write to file. */
#define     BSLR_INV_FILE_VER         ((INT32) -18)    /**< Incompatible file version. */


/* Handle type for the SATL handle. */
#define BSLT_BISSKEY_LIST (HT_GROUP_BSL + ((HANDLE_TYPE_T) 0))    /**< Handle type for the Bisskey List. */

#define MAX_BS_NAME_LEN   25    /**< Maximum length for satellite name. */


/* Enumeration for SATL database condition */

/*------------------------------------------------------------------*/
typedef  enum
{
    BSL_UPDATING = 1,   /* When satl_lock is called. */
    BSL_UPDATED,        /* When satl_unlock is called. */
    BSL_CLOSED,         /* When satl_delete is called. */
    BSL_UNKNOWN
} BSL_COND_T;

/* Notification reasons */
#define     BSL_REASON_UNKNOWN  ((UINT32)(0x0000))    /**< Reason unknown */
#define     BSL_RECORD_ADD      (MAKE_BIT_MASK_32(1))    /**< Reason for adding records */
#define     BSL_RECORD_DEL      (MAKE_BIT_MASK_32(2))    /**< Reason for deleting records */
#define     BSL_RECORD_MOD      (MAKE_BIT_MASK_32(3))    /**< Reason for modifying records */

/* Max length of the satellite list name (bytes) */
#define     MAX_BSL_NAME_LEN           16    /**< Max length of the satellite list name (bytes). */


/* Size (byte) of SATL record header and data structure. */
#define   BSL_REC_HEADER_PADDING    16
#define   MAX_CW_KEY_LEN        8


/* Pre-set size of the SATL record structure. */
#define     BSL_REC_SIZE  (4+BSL_REC_HEADER_PADDING+MAX_CW_KEY_LEN) /**< Pre-set size of the SATL record structure. */


/* Typedef and constants for BSL record id query flag. */
typedef     UINT8                 BSL_REC_ID_QRY_T;   /**< Typedef for BSL record id query flag. */

#define     BSL_FIRST_REC_ID     ((BSL_REC_ID_QRY_T) 1)    /**< First record ID. */
#define     BSL_LAST_REC_ID      ((BSL_REC_ID_QRY_T) 2)    /**< Last record ID. */
#define     BSL_NEXT_REC_ID      ((BSL_REC_ID_QRY_T) 3)    /**< Next record ID. */


/* Typedef and constants for SATL memory mangement policy. */
typedef     UINT8               BSL_MEM_POLICY_T;   /**< Typedef for SATL memory mangement policy. */

#define     BSL_MEM_STATIC      ((BSL_MEM_POLICY_T) 0)    /**< Static memory policy. */
#define     BSL_MEM_DYNAMIC     ((BSL_MEM_POLICY_T) 1)    /**< Dynamic memory policy. */

/* Null SATL database version id. */
#define     BSL_NULL_VER_ID     ((UINT32)0)    /**< Null SATL database version id. */

/* NULL SATL record ID value */
#define     BSL_NULL_REC_ID     ((UINT16)0)    /**< NULL SATL record ID value */

/* NULL SATL ID value. */
#define     BSL_NULL_ID         ((UINT16)0)    /**< NULL SATL ID value. */

/* Typedef and constants for SATL record iteration flag. */
typedef     UINT8                 BSL_ITERATE_T;   /**< Typedef for SATL record iteration flag. */

#define     BSL_NEXT_REC         ((BSL_ITERATE_T) 1)    /**< Next record */
#define     BSL_PREV_REC         ((BSL_ITERATE_T) 2)    /**< Previous record */
#define     BSL_FIRST_REC        ((BSL_ITERATE_T) 3)    /**< First record */
#define     BSL_LAST_REC         ((BSL_ITERATE_T) 4)    /**< Last record */

/*---------------------------------------------------------------------------
 Structures
----------------------------------------------------------------------------*/
#pragma pack(4)
typedef struct _BSL_REC_HEADER_T
{
  UINT32      ui4_freq;
  UINT32      ui4_sym_rate;
  TUNER_POLARIZATION_T  e_pol;
  UINT16      ui2_prog_id;
}BSL_REC_HEADER_T;

/* Structure for the SATL data record */
/*------------------------------------------------------------------*/
/*! @brief  Structure for SATL data record
 *  @code
 *  typedef struct _SATL_REC_T
 *  {
 *      UINT16      ui2_satl_rec_id;
 *      UINT32      ui4_mask;                       
 *      union
 *      {
 *          TUNER_LNB_INFO_T            t_lnb;
 *          UINT8  _lnb_data_padding[SATL_LNB_DATA_PADDING];
 *      } ulnb;
 *      union
 *      {
 *          TUNER_DISEQC_INFO_T         t_diseqc_info;
 *          UINT8  _diseqc_data_padding[SATL_DISEQC_DATA_PADDING];
 *      } udiseqc;
 *      union
 *      {
 *          TUNER_DISEQC_INFO_T         t_diseqc_info;
 *          UINT8  _diseqc_data_padding[SATL_DISEQC_DATA_PADDING];
 *      } udiseqc_sub; 
 *      INT16       i2_orb_pos;                     
 *      CHAR        ac_sat_name[MAX_SAT_NAME_LEN];  
 *  } SATL_REC_T;
 *  @endcode
 *  @li@c  ui2_satl_rec_id               - SATL record ID
 *  @li@c  ui4_mask                      - User defined bit mask
 *  @li@c  t_lnb                         - LNB information
 *  @li@c  _lnb_data_padding             - LNB data padding
 *  @li@c  t_diseqc_info                 - DiSEqC information
 *  @li@c  _diseqc_data_padding          - DiSEqC data padding
 *  @li@c  udiseqc_sub                  - for dual tuner, the sub diseqc
 *  @li@c  i2_orb_pos                    - Orbit position
 *  @li@c  ac_sat_name[MAX_SAT_NAME_LEN] - Satellite name
 */
/*------------------------------------------------------------------*/
typedef struct _BSL_REC_T
{
    /* Satellite record id */
    UINT16      ui2_bsl_rec_id;

    union
    {
        BSL_REC_HEADER_T  t_rec_hdr;
        UINT8 _rec_header_padding[BSL_REC_HEADER_PADDING];
    } uheader;

  CHAR  s_cw_key[MAX_CW_KEY_LEN + 1];

} BSL_REC_T;

#pragma pack()

/*---------------------------------------------------------------------------
 Type definition
----------------------------------------------------------------------------*/

/*
  typedef for the SATL handle callback function.  This fucntion
  is implemented by the client of SATL. 
*/
/*------------------------------------------------------------------*/
/*! @brief  Typedef for the SATL handle callback function.  This fucntion
 *          is implemented by the client of SATL.
 *  @param  [in]  h_satl            - Handle to the SATL object.
 *  @param  [in]  e_cond            - The status flag for the SATL object.
 *  @param  [in]  ui4_reason        - The reason for the notification.
 *  @param  [in]  pv_tag            - Private tag for the client.
 *  @param  [in]  ui4_data          - Data pass back to the application.
 *  @note   It is very important that the client MUST NOT try to read the SATL in
 *          the context of notification function.  Otherwise, a `Dead lock'
 *          situation would arise.  The client should only send a message to
 *          itself from the notification function or set a condition flag, and
 *          execute the read operation outside the context of the notification
 *          function.
 *          @par
 *          Client should also avoid doing CPU intensive task in the
 *          notification function.  If CPU intensive task needs to be performed,
 *          then the notification function should send a message to the client
 *          thread, and have the CPU intensive task executed in the client
 *          thread context.
 *  @see    c_satl_create, c_satl_open, c_satl_open_by_name, c_satl_fs_load

 */
/*------------------------------------------------------------------*/
typedef VOID (*x_bsl_nfy_fct)
(
    HANDLE_T            h_bsl,
    BSL_COND_T          e_cond,
    UINT32              ui4_reason,
    VOID*               pv_tag,
    UINT32              ui4_data
);

/*----------------------------------------------------------------------------*/
/*! @} */
/*----------------------------------------------------------------------------*/
#endif /* _U_BSL_H_ */
