/* 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.
 *
 *****************************************************************************/

/*-----------------------------------------------------------------------------
 *
 * $Author: dtvbm11 $
 * $Date: 2015/11/10 $
 * $RCSfile: cb_dmx.c,v $
 * $Revision: #1 $
 *
 *---------------------------------------------------------------------------*/

/** @file cb_dmx.c
 *  Demux callback function
 */

//-----------------------------------------------------------------------------
// Include files
//-----------------------------------------------------------------------------

#include "cb_low.h"
#include "dmx_if.h"
#include <linux/mt53xx_cb.h>

#include "x_assert.h"

#define DEFINE_IS_LOG	DMX_IsLog
#include "x_debug.h"


//-----------------------------------------------------------------------------
// Configurations
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// Constant definitions
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// Type definitions
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// Macro definitions
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// Static variables
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// Static functions
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
/** _DmxOnPesNotify
 *  PES notification handler
 *
 *  @param  u1Pidx          PID index
 *  @param  u4Data          Handler-specific data
 *  @param  pvTag           Handler-specific tag
 *
 *  @retval TRUE	        Succeed
 *  @retval FALSE	        Fail
 */
//-----------------------------------------------------------------------------
static BOOL _DmxOnPesNotify(UINT8 u1Pidx, UINT32 u4Data, const void* pvTag)
{
    // To be defined
    return FALSE;
}


//-----------------------------------------------------------------------------
/** _DmxOnEsNotify
 *  ES notification handler
 *
 *  @param  u1Pidx          PID index
 *  @param  u4Data          Handler-specific data
 *  @param  pvTag           Handler-specific tag
 *
 *  @retval TRUE	        Succeed
 *  @retval FALSE	        Fail
 */
//-----------------------------------------------------------------------------
static BOOL _DmxOnEsNotify(UINT8 u1Pidx, UINT32 u4Data, const void* pvTag)
{
    // To be defined
    return FALSE;
}


//-----------------------------------------------------------------------------
/** _DmxOnPsiNotify
 *  PSI notification handler
 *
 *  @param  u1Pidx          PID index
 *  @param  u4Data          Handler-specific data
 *  @param  pvTag           Handler-specific tag
 *
 *  @retval TRUE	        Succeed
 *  @retval FALSE	        Fail
 */
//-----------------------------------------------------------------------------
static BOOL _DmxOnPsiNotify(UINT8 u1Pidx, UINT32 u4Data, const void* pvTag)
{
    DMX_CB_T rDmxCb;
    DMX_NOTIFY_INFO_PSI_T* prNotifyInfo;

    ASSERT(u1Pidx < DMX_NUM_PID_INDEX);

    prNotifyInfo = (DMX_NOTIFY_INFO_PSI_T*)u4Data;
    ASSERT(prNotifyInfo != NULL);

    rDmxCb.u1Pidx = u1Pidx;
    rDmxCb.u4SecAddr = prNotifyInfo->u4SecAddr;
    rDmxCb.u4SecLen = prNotifyInfo->u4SecLen;
    rDmxCb.u1SerialNumber = prNotifyInfo->u1SerialNumber;

    LOG(5, "PSI NOTIFY - Pidx: %u, section: 0x%08x, size: %u, serial number: %d\n", u1Pidx,
        prNotifyInfo->u4SecAddr, prNotifyInfo->u4SecLen, prNotifyInfo->u1SerialNumber);

    UNUSED(_CB_PutEvent(CB_DMX_TRIGGER, sizeof (DMX_CB_T), &rDmxCb));

    return TRUE;
}


//-----------------------------------------------------------------------------
/** _DmxCallbackHandler
 *  Demux callback handler
 *
 *  @param  u1Pidx          PID index
 *  @param  eCode           Notification code
 *  @param  u4Data          Handler-specific data
 *  @param  pvTag           Handler-specific tag
 *
 *  @retval TRUE	        Succeed
 *  @retval FALSE	        Fail
 */
//-----------------------------------------------------------------------------
static BOOL _DmxCallbackHandler(UINT8 u1Pidx, DMX_NOTIFY_CODE_T eCode,
    UINT32 u4Data, const void* pvTag)
{
    BOOL fgRet = FALSE;

    switch (eCode)
    {
    case DMX_NOTIFY_CODE_PES:
        fgRet = _DmxOnPesNotify(u1Pidx, u4Data, pvTag);
        break;

    case DMX_NOTIFY_CODE_ES:
        fgRet = _DmxOnEsNotify(u1Pidx, u4Data, pvTag);
        break;

    case DMX_NOTIFY_CODE_PSI:
        fgRet = _DmxOnPsiNotify(u1Pidx, u4Data, pvTag);
        break;

    default:
        // Unknown notification code
        LOG(1, "Pidx %u: Unknown notification code %u\n", u1Pidx,
            (UINT32)eCode);
        ASSERT(0);
        break;
    }

    return fgRet;
}


//-----------------------------------------------------------------------------
// Inter-file functions
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
/** _DMX_RegTrigger
 *  Demux call registration. Indirectly called by user program
 *
 *  @param  i4SubId         Sub-id. Not used
 *
 *  @retval CBR_OK          Succeed
 *  @retval Others          Fail
 */
//-----------------------------------------------------------------------------
INT32 _DMX_RegTrigger(INT32 i4SubId)
{
    DMX_PID_T rPid;
    UINT32 i;

    rPid.pfnNotify = _DmxCallbackHandler;
    rPid.pvNotifyTag = NULL;

    for (i = 0; i < DMX_NUM_PID_INDEX; i++)
    {
        if (!DMX_SetPid(i, DMX_PID_FLAG_CALLBACK, &rPid))
        {
            LOG(3, "Pidx %u: Fail to set callback function\n", i);
            return -1;
        }
    }

    return CBR_OK;
}


