/**
 * @file tc_tm_mngr.h
 *
 * @brief This file contains function prototypes and types necessary
 *        for packing, unpacking and routing the TC TM data
 *
 * @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.
 *        usage of TC TM Library APIs
 */

#ifndef EXO_SERVICES_TC_TM_MNGR_INC_TC_TM_MNGR_H_
#define EXO_SERVICES_TC_TM_MNGR_INC_TC_TM_MNGR_H_

#include "exo_osal.h"
#include "exo_ral_cmn.h"

/// TC/TM manager init and operation status
typedef enum {
    TC_TM_MNGR_ERR = -1,  ///< Error in operation
    TC_TM_MNGR_OK = 0    ///< success
}e_tc_tm_mngr_sts;

/*
 * @brief ground contact valid function pointer
 */
typedef uint8_t (*gs_contact_valid_fptr_t)(void);

/*
 * @brief Radio Module structure definition
 */
typedef struct
{
    gs_contact_valid_fptr_t gs_contact_valid; /*!<ground contact valid
                                                         function pointer*/
    os_queue_handle_ptr tm_storage_queue;     /*!<os queue handle pointer */
    uint8_t thread_id;                        /*!<Radio Module Thread ID  */
    uint8_t radio_set_flag;                   /*!<Radio Module validity flag*/
}s_radio_module;

/**
 * @brief This API used to set radio module parameters
 *
 * @param[in] radio_id              : Radio Module ID.
 * @param[in] thread_id             : Radio Module Thread Id .
 * @param[in] ground_contact_api    : Pointer to ground contact valid api.
 *
 */

void set_radio_module(uint8_t radio_id,uint8_t thread_id,
        gs_contact_valid_fptr_t ground_contact_api);

///Maximum TM storage queue depth
#define MAX_TM_STORAGE_DEPTH (512)

/**
 * @brief This API invokes two threads to handle receive,
 * store and forward the IPC messages
 *
 * @return status of the thread creation
 * @retval ral_success -> Success / else -> failure.
 */
os_status_t tc_tm_mngr_main(void);

/**
 * @brief This API unpacks the tc data and routes to the proper destination
 * based on da_id
 *
 * @param[in] data  : Pointer to received data.
 * @param[in] size  : size of data
 *
 * @return status of the unpack and IPC api
 * @retval greater than or equal to zero -> Success / less than zero ->failure.
 */

int32 handle_tc_data(uint8* data, uint16 size);

/**
 * @brief This API is used to get the Telemetry IPC receive handler
 * of TC/TM manager
 *
 * @param[out] hdlr  : Pointer to receive handler.
 *
 * @return status of the TC/TM mngr thread creation
 * @retval TC_TM_MNGR_OK -> Success / TC_TM_MNGR_ERR ->failure.
 */
e_tc_tm_mngr_sts get_tm_mngr_recv_hdlr(os_thread_handle_ptr *hdlr);

/**
 * @brief This API is used to get the Telecommand IPC receive handler
 * of TC/TM manager
 *
 * @param[out] hdlr  : Pointer to receive handler.
 *
 * @return status of the TC/TM mngr thread creation
 * @retval TC_TM_MNGR_OK -> Success / TC_TM_MNGR_ERR ->failure.
 */
e_tc_tm_mngr_sts get_tc_mngr_recv_hdlr(os_thread_handle_ptr *hdlr);


/**
 * @brief This API packs the tm data and computes the tm size
 *
 * @param[in] tx_data   : pointer to transmitted data.
 * @param[out] rx_data   : pointer to Received data.
 *
 * @return Telemetry size
 * @retval Received data size
 */
int32 handle_tm_data(os_itc_msg_handle_t* rx_data, uint8* tx_data);

/**
 * @brief This API process custome IPC from scheduler and route TC to destination
 *
 * @param[in] payload   : pointer to scheduler event structure.
 *
 * @return Telemetry size
 * @retval Received data size
 */
int32 tc_process_sched_msg(uint8 *payload);

/**
 * @brief This function deletes the tc_tm_mngr queue and
 * release its memory
 *
 * @return status of os thread suspend
 * @retval Success->ral_success, Error->ral_err_invld_arg, ral_error
 */
os_status_t tc_tm_mngr_suspend(void);

/**
 * @brief This functionality resumes the tc_tm_mngr thread
 *
 * @return status of os thread resume
 * @retval Success->ral_success, Error->ral_err_invld_arg, ral_error
 */
os_status_t tc_tm_mngr_resume(void);
size_t xPortGetMinimumEverFreeHeapSize_eram( void );

/**
 * @brief This function is used to initiate the HM data
 *
 */
void tc_tm_hm_dl_req(uint8 radio_id);
void ft_send_tc_to_ftm(uint16 tc_id, uint8 *tc_data, uint16 tc_data_len, uint16_t app_id);

#endif /* EXO_SERVICES_TC_TM_MNGR_INC_TC_TM_MNGR_H_ */
