/*
 * exo_ahw_al_thruster_common.c
 *
 *  @brief This file contains abstracted driver function for thruster
 *
 * @copyright Copyright 2024 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 "exo_ahw_al_thruster_common.h"
#include "exo_ahw_vdp_linux_thruster.h"
#include "exo_osal.h"
#include "err_handler.h"

static ral_mutex_handle_ptr thruster_mutex;

/*!
 *  @brief This API initializes the control block memory and do the basic configurations for ADM1176 instance
 */
hal_ret_sts ahw_al_thruster_init(void)
{
    hal_ret_sts sts=HAL_MAX_ERR;
    if(os_mutex_create(&thruster_mutex) == os_success)
    {
        ahw_al_thruster_common_init();
        sts = HAL_SCS;
    }
    return sts;
}

/*!
 *  @brief This API deinitializes the control block memory
 */
hal_ret_sts ahw_al_thruster_deinit(void)
{
    hal_ret_sts sts=HAL_MAX_ERR;

    if(os_mutex_delete(thruster_mutex) == os_success)
    {
        sts = HAL_SCS;
    }
    return sts;
}

/*!
 *  @brief This API get thruster mode 
 */
hal_ret_sts ahw_al_thruster_get_operating_mode(uint8_t thruster_id, uint8_t* get_mde)
{
    hal_ret_sts sts = HAL_AHW_STE_BUSY;
    if(get_mde != NULL)
    {
        if(os_mutex_take(thruster_mutex,THRUSTER_MUTEX_TIME_OUT) == os_success)
        {
            if(ahw_al_thruster_common_get_operating_mode(thruster_id, get_mde) == HAL_SCS)
            {
                sts = HAL_SCS;
            }
            else
            {
                sts = HAL_AH_DRIVER_ERR;
            }
            os_mutex_give(thruster_mutex);
        }
    }
    else
    {
        sts = HAL_IO_INVLD_ARG;
    }
    return sts;
}


/*!
 *  @brief This API set thruster mode 
 */
hal_ret_sts ahw_al_thruster_set_operating_mode(uint8_t thruster_id, uint8_t set_mde)
{
    hal_ret_sts sts = HAL_AHW_STE_BUSY;
    if(os_mutex_take(thruster_mutex,THRUSTER_MUTEX_TIME_OUT) == os_success)
    {
        if(ahw_al_thruster_common_set_operating_mode(thruster_id, set_mde) == HAL_SCS)
        {
            sts = HAL_SCS;
        }
        else
        {
            sts = HAL_AH_DRIVER_ERR;
        }
        os_mutex_give(thruster_mutex);
    }
    return sts;
}


/*!
 *  @brief This API set thruster firing duration 
 */
hal_ret_sts ahw_al_thruster_set_firing_duration(uint8_t thruster_id, uint32_t thr_st_firng_dur)
{
    hal_ret_sts sts = HAL_AHW_STE_BUSY;
    if(os_mutex_take(thruster_mutex,THRUSTER_MUTEX_TIME_OUT) == os_success)
    {
        if(ahw_al_thruster_common_set_firing_duration(thruster_id, thr_st_firng_dur) == HAL_SCS)
        {
            sts = HAL_SCS;
        }
        else
        {
            sts = HAL_AH_DRIVER_ERR;
        }
        os_mutex_give(thruster_mutex);
    }
    return sts;
}


/*!
 *  @brief This API get thruster firing furation 
 */
hal_ret_sts ahw_al_thruster_get_firing_duration(uint8_t thruster_id, uint32_t* thr_gt_firng_dur)
{
    hal_ret_sts sts = HAL_AHW_STE_BUSY;
    if(thr_gt_firng_dur != NULL)
    {
        if(os_mutex_take(thruster_mutex,THRUSTER_MUTEX_TIME_OUT) == os_success)
        {
            if(ahw_al_thruster_common_get_firing_duration(thruster_id, thr_gt_firng_dur) == HAL_SCS)
            {
                sts = HAL_SCS;
            }
            else
            {
                sts = HAL_AH_DRIVER_ERR;
            }

            os_mutex_give(thruster_mutex);
        }
    }
    else
    {
        sts = HAL_IO_INVLD_ARG;
    }
    return sts;
}

/*!
 *  @brief This API get thruster input power voltage consmp 
 */
hal_ret_sts ahw_al_thruster_get_input_power_voltage_consmp(uint8_t thruster_id, float* thr_voltage)
{
    hal_ret_sts sts = HAL_AHW_STE_BUSY;
    if(thr_voltage != NULL)
    {
        if(os_mutex_take(thruster_mutex,THRUSTER_MUTEX_TIME_OUT) == os_success)
        {
            if(ahw_al_thruster_common_get_input_power_voltage_consmp(thruster_id, thr_voltage) == HAL_SCS)
            {
                sts = HAL_SCS;
            }
            else
            {
                sts = HAL_AH_DRIVER_ERR;
            }
            os_mutex_give(thruster_mutex);
        }
    }
    else
    {
        sts = HAL_IO_INVLD_ARG;
    }
    return sts;
}

/*!
 *  @brief This API get thruster input power current consmp 
 */
hal_ret_sts ahw_al_thruster_get_input_power_current_consmp(uint8_t thruster_id, float* thr_current)
{
    hal_ret_sts sts = HAL_AHW_STE_BUSY;
    if(thr_current != NULL)
    {
        if(os_mutex_take(thruster_mutex,THRUSTER_MUTEX_TIME_OUT) == os_success)
        {
            if(ahw_al_thruster_common_get_input_power_current_consmp(thruster_id, thr_current) == HAL_SCS)
            {
                sts = HAL_SCS;
            }
            else
            {
                sts = HAL_AH_DRIVER_ERR;
            }
            os_mutex_give(thruster_mutex);
        }
    }
    else
    {
        sts = HAL_IO_INVLD_ARG;
    }
    return sts;
}

/*!
 *  @brief This API get thruster onboard temp
 */
hal_ret_sts ahw_al_thruster_get_on_board_temperature(uint8_t thruster_id, float* get_onbrd_temp)
{
    hal_ret_sts sts = HAL_AHW_STE_BUSY;
    if(get_onbrd_temp != NULL)
    {
        if(os_mutex_take(thruster_mutex,THRUSTER_MUTEX_TIME_OUT) == os_success)
        {
            if(ahw_al_thruster_common_get_on_board_temperature(thruster_id, get_onbrd_temp) == HAL_SCS)
            {
                sts = HAL_SCS;
            }
            else
            {
                sts = HAL_AH_DRIVER_ERR;
            }

            os_mutex_give(thruster_mutex);
        }
    }
    else
    {
        sts = HAL_IO_INVLD_ARG;
    }
    return sts;
}

/*!
 *  @brief This API set thruster rad low temp threshold
 */
hal_ret_sts ahw_al_thruster_set_radiator_low_temp_threshold(uint8_t thruster_id, float thr_set_rad_lw_temp)
{
    hal_ret_sts sts = HAL_AHW_STE_BUSY;
    if(os_mutex_take(thruster_mutex,THRUSTER_MUTEX_TIME_OUT) == os_success)
    {
        if(ahw_al_thruster_common_set_radiator_low_temp_threshold(thruster_id, thr_set_rad_lw_temp) == HAL_SCS)
        {
            sts = HAL_SCS;
        }
        else
        {
            sts = HAL_AH_DRIVER_ERR;
        }

        os_mutex_give(thruster_mutex);
    }
    else
    {
        sts = HAL_IO_INVLD_ARG;
    }
    return sts;
}

/*!
 *  @brief This API get thruster rad low temp threshold
 */
hal_ret_sts ahw_al_thruster_get_radiator_low_temp_threshold(uint8_t thruster_id, float* thr_get_rad_lw_temp)
{
    hal_ret_sts sts = HAL_AHW_STE_BUSY;
    if(thr_get_rad_lw_temp != NULL)
    {
        if(os_mutex_take(thruster_mutex,THRUSTER_MUTEX_TIME_OUT) == os_success)
        {
            if(ahw_al_thruster_common_get_radiator_low_temp_threshold(thruster_id, thr_get_rad_lw_temp) == HAL_SCS)
            {
                sts = HAL_SCS;
            }
            else
            {
                sts = HAL_AH_DRIVER_ERR;
            }

            os_mutex_give(thruster_mutex);
        }
    }
    else
    {
        sts = HAL_IO_INVLD_ARG;
    }
    return sts;
}

/*!
 *  @brief This API set thruster rad high temp thresold
 */
hal_ret_sts ahw_al_thruster_set_radiator_high_temp_threshold(uint8_t thruster_id, float thr_set_rad_hg_temp)
{
    hal_ret_sts sts = HAL_AHW_STE_BUSY;
    if(os_mutex_take(thruster_mutex,THRUSTER_MUTEX_TIME_OUT) == os_success)
    {
        if(ahw_al_thruster_common_set_radiator_high_temp_threshold(thruster_id, thr_set_rad_hg_temp) == HAL_SCS)
        {
            sts = HAL_SCS;
        }
        else
        {
            sts = HAL_AH_DRIVER_ERR;
        }

        os_mutex_give(thruster_mutex);
    }
    else
    {
        sts = HAL_IO_INVLD_ARG;
    }
    return sts;
}

/*!
 *  @brief This API get thruster rad high temp threshold
 */
hal_ret_sts ahw_al_thruster_get_radiator_high_temp_threshold(uint8_t thruster_id, float* thr_get_rad_hg_temp)
{
    hal_ret_sts sts = HAL_AHW_STE_BUSY;
    if(thr_get_rad_hg_temp != NULL)
    {
        if(os_mutex_take(thruster_mutex,THRUSTER_MUTEX_TIME_OUT) == os_success)
        {
            if(ahw_al_thruster_common_get_radiator_high_temp_threshold(thruster_id, thr_get_rad_hg_temp) == HAL_SCS)
            {
                sts = HAL_SCS;
            }
            else
            {
                sts = HAL_AH_DRIVER_ERR;
            }

            os_mutex_give(thruster_mutex);
        }
    }
    else
    {
        sts = HAL_IO_INVLD_ARG;
    }
    return sts;
}

/*!
 *  @brief This API get thruster remaining prplnt lvl
 */
hal_ret_sts ahw_al_thruster_get_remaining_propellant_level(uint8_t thruster_id, float* get_rmg_prplnt_lvl)
{
    hal_ret_sts sts = HAL_AHW_STE_BUSY;
    if(get_rmg_prplnt_lvl != NULL)
    {
        if(os_mutex_take(thruster_mutex,THRUSTER_MUTEX_TIME_OUT) == os_success)
        {
            if(ahw_al_thruster_common_get_remaining_propellant_level(thruster_id, get_rmg_prplnt_lvl) == HAL_SCS)
            {
                sts = HAL_SCS;
            }
            else
            {
                sts = HAL_AH_DRIVER_ERR;
            }
            os_mutex_give(thruster_mutex);
        }
    }
    else
    {
        sts = HAL_IO_INVLD_ARG;
    }
    return sts;
}

/*!
 *  @brief This API set thruster min tank pressure limit
 */
hal_ret_sts ahw_al_thruster_set_min_tank_pressure_limit(uint8_t thruster_id, float set_min_tnk_prs)
{
    hal_ret_sts sts = HAL_AHW_STE_BUSY;
    if(os_mutex_take(thruster_mutex,THRUSTER_MUTEX_TIME_OUT) == os_success)
    {
        if(ahw_al_thruster_common_set_min_tank_pressure_limit(thruster_id,set_min_tnk_prs) == HAL_SCS)
        {
            sts = HAL_SCS;
        }
        else
        {
            sts = HAL_AH_DRIVER_ERR;
        }
        os_mutex_give(thruster_mutex);
    }
    else
    {
        sts = HAL_IO_INVLD_ARG;
    }
    return sts;
}

/*!
 *  @brief This API set thruster max tnk pressure limit
 */
hal_ret_sts ahw_al_thruster_set_max_tank_pressure_limit(uint8_t thruster_id, float set_max_tnk_prs)
{
    hal_ret_sts sts = HAL_AHW_STE_BUSY;
    if(os_mutex_take(thruster_mutex,THRUSTER_MUTEX_TIME_OUT) == os_success)
    {
        if(ahw_al_thruster_common_set_max_tank_pressure_limit(thruster_id,set_max_tnk_prs) == HAL_SCS)
        {
            sts = HAL_SCS;
        }
        else
        {
            sts = HAL_AH_DRIVER_ERR;
        }
        os_mutex_give(thruster_mutex);
    }
    else
    {
        sts = HAL_IO_INVLD_ARG;
    }
    return sts;
}

/*!
 *  @brief This API get thruster min tnk pressure limit 
 */
hal_ret_sts ahw_al_thruster_get_min_tank_pressure_limit(uint8_t thruster_id, float* get_min_tnk_prs)
{
    hal_ret_sts sts = HAL_AHW_STE_BUSY;
    if(get_min_tnk_prs != NULL)
    {
        if(os_mutex_take(thruster_mutex,THRUSTER_MUTEX_TIME_OUT) == os_success)
        {
            if(ahw_al_thruster_common_get_min_tank_pressure_limit(thruster_id,get_min_tnk_prs) == HAL_SCS)
            {
                sts = HAL_SCS;
            }
            else
            {
                sts = HAL_AH_DRIVER_ERR;
            }
            os_mutex_give(thruster_mutex);
        }
    }
    else
    {
        sts = HAL_IO_INVLD_ARG;
    }
    return sts;
}

/*!
 *  @brief This API get thruster max tnk pressure limit 
 */
hal_ret_sts ahw_al_thruster_get_max_tank_pressure_limit(uint8_t thruster_id, float* get_max_tnk_prs)
{
    hal_ret_sts sts = HAL_AHW_STE_BUSY;
    if(get_max_tnk_prs != NULL)
    {
        if(os_mutex_take(thruster_mutex,THRUSTER_MUTEX_TIME_OUT) == os_success)
        {
            if(ahw_al_thruster_common_get_max_tank_pressure_limit(thruster_id,get_max_tnk_prs) == HAL_SCS)
            {
                sts = HAL_SCS;
            }
            else
            {
                sts = HAL_AH_DRIVER_ERR;
            }
            os_mutex_give(thruster_mutex);
        }
    }
    else
    {
        sts = HAL_IO_INVLD_ARG;
    }
    return sts;
}
