/**
 * @file comms_uhf_rf_cfg.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_RF_CFG_H_
#define COMMS_UHF_RF_CFG_H_

#include "comms_uhf_main.h"


#define MASK_8_BITS 0xFF
#define MASK_7_BITS 0x7F
#define MASK_6_BITS 0x3F
#define MASK_5_BITS 0x1F
#define MASK_4_BITS 0x0F
#define MASK_3_BITS 0x07
#define MASK_2_BITS 0x03
#define MASK_1_BITS 0x01
#define MASK_0_BITS 0x00

typedef enum {
    UHF_BOOTLOADER_MSG_PING        = 0X00,
    UHF_BOOTLOADER_MSG_ACK         = 0X01,
    UHF_BOOTLOADER_MSG_WRITE_PAGE  = 0X02,
    UHF_BOOTLOADER_MSG_ERASE       = 0X0C,
    UHF_BOOTLOADER_MSG_NACK        = 0X0F,
    UHF_CMN_MSG_ACK                = 0X10,
    UHF_CMN_MSG_ASCII              = 0X11,
    UHF_RADIO_MSG_REBOOT           = 0X12,
    UHF_RADIO_MSG_GET_TIME         = 0X13,
    UHF_RADIO_MSG_SET_TIME         = 0X14,
    UHF_RADIO_MSG_RANGING          = 0X15,
    UHF_RADIO_MSG_RANGING_ACK      = 0X16,
    UHF_RADIO_MSG_GET_TELEM        = 0X17,
    UHF_RADIO_MSG_TELEM            = 0X18,
    UHF_RADIO_MSG_GET_CALLSIGN     = 0X19,
    UHF_RADIO_MSG_SET_CALLSIGN     = 0X1A,
    UHF_RADIO_MSG_CALLSIGN         = 0X1B,
    UHF_RADIO_MSG_SET_CFG          = 0X1C, //amend gps time and gps data
    UHF_RADIO_MSG_GET_CFG          = 0X1D,
	UHF_SET_BCON_TIMER             = 0X1E,
    UHF_SET_ALL_RADIO_CFG_REQ      = 0X20,
    UHF_SET_ALL_RADIO_CFG_RSP      = 0X21,
    UHF_GET_ALL_RADIO_CFG_REQ      = 0X22,
    UHF_GET_ALL_RADIO_CFG_RSP      = 0X23,
    UHF_SET_TX_RADIO_CFG_REQ       = 0X24,
    UHF_SET_TX_RADIO_CFG_ACK       = 0X25,
    UHF_GET_TX_RADIO_CFG_REQ       = 0X26,
    UHF_GET_TX_RADIO_CFG_RSP       = 0X27,
    UHF_SET_RX_RADIO_CFG_REQ       = 0X28,
    UHF_SET_RX_RADIO_CFG_ACK       = 0X29,
    UHF_GET_RX_RADIO_CFG_REQ       = 0X2A,
    UHF_GET_RX_RADIO_CFG_RSP       = 0X2B,
    UHF_SET_TX_PKT_CFG_REQ         = 0X2C,
    UHF_SET_TX_PKT_CFG_ACK         = 0X2D,
    UHF_GET_TX_PKT_CFG_REQ         = 0X2E,
    UHF_GET_TX_PKT_CFG_RSP         = 0X2F,
    UHF_SET_RX_PKT_CFG_REQ         = 0X30,
    UHF_SET_RX_PKT_CFG_ACK         = 0X31,
    UHF_GET_RX_PKT_CFG_REQ         = 0X32,
    UHF_GET_RX_PKT_CFG_RSP         = 0X33,
    UHF_SET_PWR_CFG_REQ            = 0X34,
    UHF_SET_PWR_CFG_ACK            = 0X35,
    UHF_GET_PWR_CFG_REQ            = 0X36,
    UHF_GET_PWR_CFG_RSP            = 0X37,
    UHF_SET_BCON_CFG_STRT	       = 0X38, //beacon on
    UHF_SET_BCON_CFG_STP           = 0X39, // beacon stop
    UHF_GET_ADDR_CFG_REQ           = 0X3A,
    UHF_GET_ADDR_CFG_RSP           = 0X3B,
    UHF_SET_RADIO_CFG_RANGING_REQ  = 0X3C,
    UHF_SET_RADIO_CFG_RANGING_ACK  = 0X3D,
    UHF_GET_RADIO_CFG_RANGING_REQ  = 0X3E,
    UHF_GET_RADIO_CFG_RANGING_ACK  = 0X3F,
    UHF_REBOOT_COMMENCED           = 0X40,
	UHF_BEACON_MODE_STOPPED_OBC    = 0X41,
	UHF_BEACON_MODE_STOPPED_TIMEOUT= 0X42,
	UHF_TX_CPLT                    = 0X43,
	UHF_INVALID_CMD                = 0X44,
	UHF_MSG_LENGTH_SMALL           = 0X45,
	UHF_BEACON_MODE_RUNNING        = 0X46,
	UHF_DEBUG_RX_FRAME_DETECTED    = 0X48,
	UHF_DEBUG_RX_CPLT              = 0X49,
	UHF_TX_UNDERFLOW               = 0X50,
	UHF_RX_OVERFLOW                = 0X51,
	UHF_RX_CRC_ERROR               = 0X52,
	UHF_UART_DEBUG_ON               = 0X53,
	UHF_UART_DEBUG_OFF              = 0X54,
    UHF_CMN_MSG_NACK               = 0XFF
}e_comms_uhf_msg_no;

typedef struct {
    uint8_t sync1;
    uint8_t sync0;
    uint8_t pktlen;
    uint8_t pktctrl1;    // Packet Automation Control
    uint8_t pktctrl0;    // Packet Automation Control
    uint8_t addr;
    uint8_t channr;      // Channel Number
    uint8_t fsctrl1;     // Frequency Synthesizer Control
    uint8_t fsctrl0;     // Frequency Synthesizer Control
    uint8_t freq2;       // Frequency Control Word, High Byte
    uint8_t freq1;       // Frequency Control Word, Middle Byte
    uint8_t freq0;       // Frequency Control Word, Low Byte
    uint8_t mdmcfg4;     // Modem configuration
    uint8_t mdmcfg3;     // Modem Configuration
    uint8_t mdmcfg2;     // Modem Configuration
    uint8_t mdmcfg1;     // Modem Configuration
    uint8_t mdmcfg0;     // Modem Configuration
    uint8_t deviatn;     // Modem Deviation Setting
    uint8_t mcsm2;       // Main Radio Control State Machine Configuration
    uint8_t mcsm1;       // Main Radio Control State Machine Configuration
    uint8_t mcsm0;       // Main Radio Control State Machine Configuration
    uint8_t foccfg;      // Frequency Offset Compensation Configuration
    uint8_t bscfg;       // Bit Synchronization Configuration
    uint8_t agcctrl2;    // AGC Control
    uint8_t agcctrl1;    // AGC Control
    uint8_t agcctrl0;    // AGC Control
    uint8_t frend1;      // Front End RX Configuration
    uint8_t frend0;      // Front End TX Configuration
    uint8_t fscal3;      // Frequency Synthesizer Calibration
    uint8_t fscal2;      // Frequency Synthesizer Calibration
    uint8_t fscal1;      // Frequency Synthesizer Calibration
    uint8_t fscal0;      // Frequency Synthesizer Calibration
    uint8_t test2;       // Various Test Settings
    uint8_t test1;       // Various Test Settings
    uint8_t test0;       // Various Test Settings
    uint8_t pa_table7;   // PA Power Setting 0
    uint8_t pa_table6;
    uint8_t pa_table5;
    uint8_t pa_table4;
    uint8_t pa_table3;
    uint8_t pa_table2;
    uint8_t pa_table1;
    uint8_t pa_table0;
    uint8_t iocfg2;
    uint8_t iocfg1;
    uint8_t iocfg0;
}s_uhf_cc1110_reg_map;

typedef struct __attribute__((packed))
{
    uint8_t sync1;
    uint8_t sync2;
    uint8_t length;
    uint16_t hwid;
    uint16_t seqnum;
    uint8_t system;
    uint8_t command;
}s_comms_uhf_header;

typedef struct __attribute__((packed))
{
    uint16_t hwid;
    uint16_t seqnum;
    uint8_t system;
    uint8_t command;
}s_comms_uhf_uart_header;

typedef struct __attribute__((packed))
{
    s_comms_uhf_header header;
    uint8_t data[UHF_MAX_PLD - sizeof(s_comms_uhf_header)];
}s_comms_uhf_data;

typedef struct __attribute__((packed))
{
    uint8_t length;
    s_comms_uhf_uart_header header;
    uint8_t data[UHF_MAX_PLD - sizeof(s_comms_uhf_uart_header)];
}s_comms_uhf_uart_data;

typedef union command_buffer
{
    uint8_t msg[UHF_MAX_PLD];
    s_comms_uhf_data cmd;
}u_comms_uhf_command_buffer_t;

typedef struct{
    //base frequency
    uint8_t freq2; // freq[23:16]
    uint8_t freq1; // freq[15:8]
    uint8_t freq0; // freq[7:0]
    // if frequency
    uint8_t fsctrl1;
    // channel number selection
    uint8_t channr;
    // frequency offset
    uint8_t fsctrl0;
    // dc filter control
    uint8_t dem_dcfilt_off; // mdmcfg2[7]
    // modulation type
    uint8_t mod_format; // mdmcfg2[6:4]
    // manchester encoding control
    uint8_t manchester_en; // mdmcfg2[3]
    // sync mode
    uint8_t sync_mode; // mdmcfg2[2:0]
    // channel bandwidth
    uint8_t chanbw_e; // mdmcfg4[7:6]
    uint8_t chanbw_m; // mdmcfg4[5:4]
    // data rate
    uint8_t drate_e; // mdmcfg4[3:0]
    uint8_t drate_m; // mdmcfg3[7:0]
    // forward error correction (fec)
    uint8_t fec_en; // mdmcfg1[7]
    // preamble number
    uint8_t num_preamble; // mdmcfg1[6:4]
    // channel spacing
    uint8_t chanspc_e; // mdmcfg1[3:0]
    uint8_t chanspc_m; // mdmcfg0[7:0]
    // frequency deviation
    uint8_t deviation_e; // deviatn[7:4]
    uint8_t deviation_m; // deviatn[3:0]
    // rx sync word search timeout
    uint8_t rx_timeout; // mcsm2[2:0]
    // rx attenuation
    uint8_t rx_attenuation; // mcsm0[1:0]
}s_uhf_rf_cfg;

typedef struct{
    // demodulator sync words
    uint8_t sync1; // sync[15:8]
    uint8_t sync0; // sync[7:0]
    // maximum packet length
    uint8_t pktlen;
    // white noise control
    uint8_t white_data; // pktctrl0[7:6]
    // packet formatiing
    uint8_t pkt_format; // pktctrl0[5:4]
    // crc control
    uint8_t crc_en; // pktctrl0[3:2]
    // packet length configuration
    uint8_t length_config ; // pktctrl0[1:0]
    // device address
    uint8_t addr;
}s_uhf_pkt_cfg;

typedef struct{
    // maximum dvga gain
    uint8_t max_dvga_gain; // agcctrl2[7:6]
    // maximum lna gain
    uint8_t max_lna_gain; // agcctrl2[5:3]
    // lna priority
    uint8_t agc_lna_priority; // agcctrl1[6]
    // filter length
    uint8_t filter_length; // agcctrl0[1:0]
    // power table selection
    uint8_t frend0;
    // power table - output power values
    uint8_t pa_table7;
    uint8_t pa_table6;
    uint8_t pa_table5;
    uint8_t pa_table4;
    uint8_t pa_table3;
    uint8_t pa_table2;
    uint8_t pa_table1;
    uint8_t pa_table0;
}s_uhf_pa_cfg;

typedef struct __attribute__((packed))
{
    //uint8_t reserved;
    uint32_t uptime;
    uint32_t uart0_rx_count;
    uint32_t uart1_rx_count;
    uint8_t  rx_mode;
    uint8_t  tx_mode;
    int16_t  adc[UHF_ADC_NUM_CHANNELS];
    int8_t   last_rssi;
    uint8_t  last_lqi;
    int8_t   last_freqest;
    uint32_t packets_sent;
    uint32_t cs_count;
    uint32_t packets_good;
    uint32_t packets_rejected_checksum;
    uint32_t packets_rejected_reserved;
    uint32_t packets_rejected_other;
    uint32_t reserved0;
    //uint32_t reserved1;
    uint32_t custom0;
    uint32_t custom1;

}s_comms_uhf_tm_prop;

typedef struct __attribute__((packed))
{
	uint64_t time_stamp;
	s_comms_uhf_tm_prop comms_uhf_tm_stor;
}s_comms_uhf_get_tm_prop_time;

typedef struct __attribute__((packed))
{
        uint8_t cnt_hk_str;
        s_comms_uhf_get_tm_prop_time comms_uhf_tm_stor_time[PERI_UHF_HK_SYNC_ID];
}s_comms_uhf_get_tm_prop;

/*** TX_Beacon_DATA_STRUCT ***/
typedef struct
{
	//uint8 bcon_msg[BCON_MSG_SZ];
	uint8 len;//1
	uint32 utc_tim; //4
	uint8 utc_gps_tim; //UTC Time from GPS/RTC	Uint8	1
	uint8 lat_noso; //Latitude (North/South)// 	Uint8	1
	uint8 lon_etwt; //Longitude (East/West)//	Uint8	1
	float lat;// Latitude// 	Float	4
	float lon; //Longitude//	Float	4
	float alt; //Altitude//	Float	4
} s_comms_uhf_beacon_data;


/*** uhf tranmission timer interval ***/
typedef struct
{
	uint8 trans_tm_p_inter;
	uint8 inter_of_trans;
	uint32 bcon_mod_tout;
}s_uhf_comms_timer;


#endif /*COMMS_UHF_RF_CFG_H_*/
