/**
 * @file tc_tm_mngr_buffer.h
 *
 * @brief This file contains function prototypes and types necessary
 *        for buffering framework.
 *
 * @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_BUFFER_H_
#define EXO_SERVICES_TC_TM_MNGR_INC_TC_TM_MNGR_BUFFER_H_
#include "exo_osal.h"
#include "exo_ral_cmn.h"

#define TC_BUFF_MAX_QUEUE 20
#define TC_INVLD_SUB_MODULE 0xFF

/// HM manager init and operation status
typedef enum {
    HM_MNGR_ERR = -1,  ///< Error in operation
    HM_MNGR_OK = 0    ///< success
}e_hm_mngr_sts;

/*
 * @brief application enum 
 */
typedef enum {
    TC_TM_BUFFER_APP_EPS,
    TC_TM_BUFFER_APP_ADCS,
    TC_TM_BUFFER_APP_COMMSS_SBAND,
    TC_TM_BUFFER_APP_COMMSS_UHF,
    TC_TM_BUFFER_APP_SENSOR,
    TC_TM_BUFFER_APP_OBC,
    TC_TM_BUFFER_APP_ERROR_HANDLER,
#ifdef TEST_LAYER
    TC_TM_BUFFER_TEST_TASK,
#endif
    TC_TM_BUFFER_APP_MAX,
}e_tc_buffer_apps;

/*
 * @brief TC buffer wrap enum
 */
typedef enum {

    TC_BUFFER_WRAP_DSB,
    TC_BUFFER_WRAP_ENB,
    TC_BUFFER_WRAP_MAX,

}e_tc_buffer_wrap;
/*
 * @brief TC buffer queue priority
 */
typedef enum {
    TC_BUFFER_APP_QUEUE_PRIORITY_HIGH,
    TC_BUFFER_APP_QUEUE_PRIORITY_MEDIUM,
    TC_BUFFER_APP_QUEUE_PRIORITY_LOW,
    TC_BUFFER_APP_QUEUE_PRIORITY_MAX

}e_tc_tm_mngr_buffer_q_priority;

/*
 * @brief TC buffer request status
 */
typedef enum {
    TC_TM_BUFFER_SUCC,
    TC_TM_BUFFER_FAIL,
    TC_TM_BUFFER_INVLD_PRIORITY,
    TC_TM_BUFFER_INVLD_MODULE,
    TC_TM_BUFFER_QUEUE_NOT_ACTIVE,
    TC_TM_BUFFER_QUEUE_NOT_VALID,
    TC_TM_BUFFER_DATA_DROPPED,
    TC_TM_BUFFER_QUEUE_EMPTY,
    TC_TM_BUFFER_QUEUE_ERROR,
    TC_TM_BUFFER_MEM_FAIL,
    TC_TM_BUFFER_DEQUEUE_FAIL,
    TC_TM_BUFFER_TM_PACK_FAILED,
    TC_TM_BUFFER_RADIO_NOT_ACTIVE,
	TC_TM_BUFFER_DELETE_QUEUE_SUCC,
	TC_TM_BUFFER_DELETE_QUEUE_FAIL,
    TC_TM_BUFFER_MAX_RET_STS,
}e_tc_tm_mngr_buffer_req_sts;

/*
 * @brief Buffer request informations
 */
typedef struct
{
    uint8 queue_id;        /*!<queue id*/
    uint32 instance_size;  /*!< size of the instance*/
    uint16 num_instance;   /*!< Number of instance */
    uint8 wrap_dsb;        /*!< Wrap queue disable*/
    uint8 priority;        /*!< Priority of Queue */
    uint8 strg_flag;       /*!< Storage flag */
    uint8 thresh;          /*!< Threshold */ 
}s_tc_tm_buffer_req;

/*
 * @brief DL Request information
 */
typedef struct
{
    uint8 queue_id;        /*!<queue id*/
    uint8 submod_id;       /*!< Submodule id */
    uint8  priority;       /*!< Priority */
}s_tc_tm_dl_q_req;

/*
 * @brief Buffer request informations
 */
typedef struct
{
    uint8 queue_id;        /*!<queue id*/
    uint8 submod_id;       /*!< Submodule id */
    uint8  priority;       /*!< Priority */
}s_tc_tm_flush_q_req;

/*
 * @brief Queue priority change informations
 */
typedef struct
{
    uint8 queue_id;            /*!<queue id*/
    uint8 submod_id;           /*!< Submodule id */
    uint8  priority;           /*!< Priority */
    uint8  cur_priority;       /*!< Current Priority */
}s_tc_tm_chng_q_priority;


/*
 * @brief Module priority change informations
 */
typedef struct
{
    uint8 submod_id;           /*!< Submodule id */
    uint8  priority;           /*!< Priority */
    uint8  cur_priority;       /*!< Current Priority */
}s_tc_tm_chng_mod_priority;
/**
 * @brief This API invokes thread to handle transmit and
 * receive IPC messages of health metric data
 */
os_status_t health_metric_buffer_main(void);
/*
 * @brief Structure to store app queue related informations
 */
typedef struct {
    uint8 app_id; /*!<Application/task id*/
    uint8 num_of_queue; /*!<total number of queue active */
    uint8 sub_mod_priority; /*!< Sub modules priority */ 
    uint32 queue_bitmap; /*!<To indicate which queue is active */
    os_queue_handle_ptr queue[TC_BUFF_MAX_QUEUE];     /*!<os queue handle pointer */
    s_tc_tm_buffer_req buffer_info[TC_BUFF_MAX_QUEUE];
}s_tc_tm_buff_app;

/**
 * @brief This API used to process the buffer framework related IPC's 
 * and return error if it is not handled
 * @param[in] tc_recv_msg   : pointer to ITC message
 *
 * @return success/failure of processing
 * @retval TC_TM_MNGR_OK -> Success / TC_TM_MNGR_ERR ->failure
 */
int8 tc_process_buff_fw_ipc(os_itc_msg_handle_t *tc_recv_msg);

void health_metric_ipc_rcv();

/**
 * @brief This API used to get the app id from source entity
 * and return error if it is not handled
 * @param[in] src_entity   : enum to source task
 *
 * @return app id if task is valid
 * @retval APP ID on success / TC_TM_BUFFER_APP_MAX on failure 
 */
uint8 tc_get_app_id_from_src_entity(uint8 src_entity);
void tc_set_queue_bitmap(uint8 queue_id, uint32 *bit_map);
void tc_clear_queue_bitmap(uint8 queue_id, uint32 *bit_map);
uint8 tc_check_queue_sts(uint8 queue_id, uint32 *bit_map);
uint8 tc_get_buff_idx(uint8 queue_id, uint8 app_id);
uint8 tc_buffer_dequeue_and_send_tm(os_queue_handle_ptr queue_id, uint8 app_id, uint8 buff_idx, uint8 radio_id);
void tc_buffer_send_itc_wo_pld(uint8 src_entity, uint16 Msg_id, uint32 pld_data, uint32 dst_id);
uint8 tc_send_queue_as_tm(uint8 *data_ptr, uint32 data_len, uint8 radio_id, uint8 submod_id, uint8 queue_id, uint16 num_instance);
void tc_buff_fw_init(void);

#endif /* EXO_SERVICES_TC_TM_MNGR_INC_TC_TM_MNGR_H_ */
