/**
 * @file xband_main.c
 *
 * @brief This file has the X-Band driver thread functions and other main functions for X-Band control
 *
 * @copyright Copyright 2023 Antaris, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
/*********************************************************************/
#include <stdio.h>
#include "sm_api.h"
#include "xband_ipc.h"
#include "xband_main.h"
#include "exo_osal.h"
#include "exo_ral_cmn.h"
#include "exo_common.h"
#include "logger.h"

/**
 * @brief This function step-by-step process the COMMS states to the help
 *  of event thread request
 */
void xband_main(void *param)
{
    //int32_t result;
    int8_t status = XBAND_IPC_OK;
    /* The below line has been added to fix the warning "variable set but not used"
     * note to developer : Can be removed if needed
     */
    status = status;

    uint8_t state;
    e_sm_sts sm_status;

    os_itc_msg_handle_t xband_ipc;

    DEBUG_CPRINT(("\n[XBAND] WAITING TO RECEIVE IPC "));
    log_id(LOG_INFO_L1,LOG_COMMS,XBAND_WAITING_TO_RECEIVE_IPC);
    sm_status = sm_alloc(COMMS_XBAND_FSM, XBAND_MAX_NUM_STATE);

    if(SM_OK == sm_status)
    {
//#ifdef LINUX_TEMP_PORT
        state = XBAND_BYPASS_STATE;
//#else
//        state = COMMS_INIT_STATE;
//#endif

        sm_set_state(COMMS_XBAND_FSM, state);

        DEBUG_CPRINT(("\nWAITING TO RECEIVE IPC in X-BAND thread handler "));
        log_id(LOG_INFO_L1,LOG_COMMS,WAITING_TO_RECEIVE_IPC_IN_XBAND_THREAD_HANDLER);
        while(1)
        {
            status = os_itc_msg_rcv(COMMS_XBAND_CTLR, &xband_ipc, os_wait_forever);

            //DEBUG_CPRINT(("\nRECEIVEd IPC in CSW thread handler %d",xband_ipc.pld.pld_data ));

            if(OBC_MAIN == xband_ipc.src_entity)
            {
                state = xband_fsm_hdlr_fn(state,xband_ipc.Msg_id,xband_ipc.pld.pld_ptr);
            }
            else
            {
                state = xband_fsm_hdlr_fn(state,xband_ipc.Msg_id,xband_ipc.pld.pld_ptr);
            }

            sm_set_state(COMMS_XBAND_FSM, state);
#ifndef LINUX_TEMP_PORT
            if(NULL!=xband_ipc.pld.pld_ptr)
            {
                os_free(xband_ipc.pld.pld_ptr);
                xband_ipc.pld.pld_ptr = NULL;
            }
#endif
        }
    }
    sm_status = sm_free(COMMS_XBAND_FSM);
}

/**
 * @brief This function create the COMMS state and event threads
 *
 */
int32_t xband_init(void)
{
    DEBUG_CPRINT(("\nX_BAND INIT is called\n"));
    log_id(LOG_INFO_L1,LOG_COMMS,X_BAND_INIT_IS_CALLED);
    os_status_t status;

    status = os_thread_create(COMMS_XBAND_CTLR,
    		T_XBAND_STACK_SIZE,  //TODO stack size need to be optimized
            P_COMMS_XBAND_CTLR,
            "comms_xband_thread",
            xband_main,
            NULL,
            NULL,
            NULL);

    return status;
}

/**
 * @brief This function deletes the XBAND queue and release its memory
 */
os_status_t xband_suspend(void)
{
    os_status_t status;
    status = os_thread_suspend(COMMS_XBAND_CTLR);
    return status;
}

/**
 * @brief This functionalities resumes the XBAND thread
 */
os_status_t xband_resume(void)
{
    os_status_t status;
    status = os_thread_resume(COMMS_XBAND_CTLR);
    return status;
}

