/**
 * @file comms_uhf_api.h
 *
 * @brief This file contains APIs for S-Band parameters configuration
 *        programming and readback
 *
 * @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.
*
*/

#ifndef COMMS_UHF_API_H_
#define COMMS_UHF_API_H_

#include<comms_uhf_rf_cfg.h>
#include<comms_uhf_main.h>

#define mem_size 32768
#define FLASH_SIGNATURE_START  0x6BF0
#define FLASH_SIGNATURE_LEN  16
#define FLASH_APP_START  0x0400
#define FLASH_APP_END  0x6BFF
#define FLASH_PAGE_SIZE  128
//#define UHF_BOOTLOADER_MSG_WRITE_PAGE 0X02
//#define UHF_RADIO_MSG_REBOOT  0X12
//#define UHF_BOOTLOADER_MSG_ERASE  0X0C
//#define UHF_BOOTLOADER_MSG_ACK   0X01

typedef struct __attribute__((__packed__)){
	uint16_t sync_word;
	uint8_t lngth;
	uint16_t hwid;
	uint16_t seq;
	uint8_t dst;
	uint8_t cmd;
	uint8_t hx_data[129];
}s_seri_pack_send_t;





void sdr_uhf_init_reg_map(void);

/**
* @brief This function handle the uhf firmware configuration.
*
* @param tc_cmd : pointer to telecommand
*
* @return status of uhf boot.
*/
int32_t sdr_uhf_firmware_cfg(uint8_t *tc_cmd);

/**
* @brief This function get the uhf telemetry properties.
*
* @param tm : pointer to telemetry
* @param size : size of telemetry
*
* @return status of get telemetry properties
*/
int32_t sdr_uhf_get_tm_prop(s_comms_uhf_tm_prop *tm, uint16_t size);

void sdr_uhf_init_reg_map(void);
/**
 * @brief  This function is used for updating the firmware configuration of the UHF
 */
int32_t sdr_uhf_firmware_cfg(uint8_t *tc_cmd);

int32_t sdr_uhf_get_tm_prop(s_comms_uhf_tm_prop *tm, uint16_t size);

void sdr_uhf_upd_freq(float freq);

void sdr_uhf_upd_if_freq(float if_freq);

void sdr_uhf_upd_ch_spacing(float chan_spacing);

void sdr_uhf_upd_freq_offset(float freq_ofst);

void sdr_uhf_set_tx_modem_cfg(s_uhf_rf_tx_cfg *rf_cfg);

void sdr_uhf_set_rx_modem_cfg(s_uhf_rf_rx_cfg *rf_cfg);

void sdr_uhf_set_freq_dev_cfg(float freq_dev);

int32_t sdr_uhf_set_tx_cfg_req(s_uhf_rf_tx_cfg *rf_cfg);

int32_t sdr_uhf_set_rx_cfg_req(s_uhf_rf_rx_cfg *rf_cfg);

int32_t sdr_uhf_set_tx_pkt_cfg_req(s_uhf_rf_tx_pkt_cfg *tx_pkt_cfg);

int32_t sdr_uhf_set_rx_pkt_cfg_req(s_uhf_rf_rx_pkt_cfg *rx_pkt_cfg);

int32_t sdr_uhf_set_pwr_cfg_req(s_uhf_rf_pwr_cfg *pwr_cfg);

void sdr_uhf_get_tx_pkt_cfg(void);

void sdr_uhf_get_rx_pkt_cfg(void);

void sdr_uhf_get_pwr_cfg(void);

void sdr_uhf_get_rf_tx_cfg(void);

void sdr_uhf_get_rf_rx_cfg(void);

uint8 comms_uhf_beacon_data();

/**
 * @brief: UHF firmware data extract and upload
 */
uint8_t uhf_fw_data_upload();

/**
 * @brief: Convert ascii data to hex
 */
uint8_t asctohex(int8* hex, uint8_t ret_val);

/**
 * @brief new fsm for UHF firmware write
 */
void uhf_fw_update_fsm(uint8_t fw_state_id, int8* pld, uint8 data_len);

/**
 * @brief firmware write response
 */
uint8_t uhf_uart_boot_wr_rsp();

/**
 * @brief: Serial port rd_wr for linux
 */
uint8_t serial_port_rd_wr(uint8_t* data, uint8_t reply, uint16_t data_len);



/**
 *@brief: send serial command once
 */
void send_cmd_once(int8* cmd, int8_t len);


/**
 * @brief: send serial command with response to validate reply
 */
uint8_t send_cmd_resp(int8* cmd, uint8_t reply, int8_t len, int8_t ret);


/**
 * @brief: send serial command without length
 */
void send_cmd(uint8_t* cmd);

/**
 * @brief: format serial data and upload over uart
 */

uint8_t format_serial_data();

uint8_t serial_port_init();



#endif /*COMMS_UHF_API_H_*/
