/*****************************************************************************/
/**
 * @file phy_antenna.h
 * @author FanHao (hao_fan_bit_ee@163.com)
 * @brief Physical layer control antenna configuration functions.
 * @version 0.0
 * @date 2023-04-24
 *
 * @copyright Copyright(c) 2024 Beijing Institute of Technology.\n
 * Lab of Communication and Network
 *
 ******************************************************************************/

/**
 * @addtogroup PhysicalLayer
 * @{
 */

/**
 * @addtogroup PhysicalLayer_AntennaControl
 * @{
 */
#include <string.h>
#include <stdint.h>
#include <math.h>
#include "error.h"
#include "phy_layer.h"

/**
 * @name PhysicalLayer_AntennaConfig_Constants
 * @{
 */

#define ANT_PI 3.14159265358979323846

#define ANT_COINFO_HEAD 0xAA
#define ANT_COINFO_TAIL 0xCC

#define CTRL_WORD_DEFAULT 0x00

#define ANT_MODE_BEAM_MASK 0xF0
#define ANT_BEAM_NARROR 0x00	  // 8
#define ANT_BEAM_SUBNARROR_0 0x10 // 25
#define ANT_BEAM_SUBNARROR_1 0x20 // 60
#define ANT_BEAM_WIDE 0x30		  // 120

#define ANT_MODE_POWER_MASK 0x0F
#define ANT_POWER_FULL 0x00
#define ANT_POWER_LOW 0x04
#define ANT_POWER_STANDBY 0x0F
#define ATN_MODE_SILENT (ANT_BEAM_SUBNARROR_0 | ANT_POWER_STANDBY)
#define ATN_MODE_LOWPOWER (ANT_BEAM_SUBNARROR_0 | ANT_POWER_LOW)
#define ATN_MODE_FULLPOWER (ANT_BEAM_SUBNARROR_0 | ANT_POWER_FULL)
#define ATN_MODE_DEFAULT ATN_MODE_SILENT

#define DEFAULT_CENTRTAL_FREQ 0x06D7 // 26.25GHz

#define RXTXMODE_RX 0x00
#define RXTXMODE_TX 0x01

#define RXTXMODE_DEFAULT 0x00

#define CALIBRMODE_NORMAL 0x00
#define CALIBRMODE_SINGLE_TR_CAL 0x01

#define RF_CTRL_DEFAULT 0x00

// Pair by regex
// Regex: (([0-9]+, ){8})
const uint16_t freqTable[L1_FRAME_MAX_HOPS] =
{
	1084, 1097, 1110, 1123, 1136, 1149, 1162, 1175,
	1188, 1201, 1214, 1227, 1240, 1253, 1266, 1279,
	1292, 1305, 1318, 1331, 1344, 1357, 1370, 1383,
	1396, 1409, 1422, 1435, 1448, 1461, 1474, 1487,
	1500, 1513, 1526, 1539, 1552, 1565, 1578, 1591,
	1604, 1617, 1630, 1643, 1656, 1669, 1682, 1695,
	1708, 1721, 1734, 1747, 1760, 1773, 1786, 1799,
	1812, 1825, 1838, 1851, 1864, 1877, 1890, 1903,
	1916, 1929, 1942, 1955, 1968, 1981, 1994, 2007,
	2020, 2033, 2046, 2059, 2072, 2085, 2098, 2111,
	2124
};

const uint16_t antCalTable[L1_FRAME_MAX_HOPS][L1_ANT_NUM_CH] =
//Antenna-JG01
{
	{     0,    406, -10530,  -8223}, {     0,    697,  -9933,  -7988},
	{     0,    296, -10497,  -8272}, {     0,    515, -10264,  -8093},
	{     0,    689, -10212,  -8064}, {     0,     40, -10960,  -8572},
	{     0,    722, -10878,  -8518}, {     0,     59, -10987,  -8962},
	{     0,     56, -11264,  -8770}, {     0,   -464, -11341,  -9159},
	{     0,     19, -10924,  -8887}, {     0,   -669, -10927,  -8679},
	{     0,   -701, -11154,  -8712}, {     0,   -255, -10739,  -8009},
	{     0,   -192, -10509,  -8242}, {     0,  -1057, -11183,  -9007},
	{     0,    -57, -10606,  -8350}, {     0,   -719, -10949,  -8880},
	{     0,   -760, -11084,  -8660}, {     0,   -457, -10735,  -8833},
	{     0,   -188, -10623,  -9119}, {     0,   -628, -12609, -10119},
	{     0,   -434, -12714, -10225}, {     0,   -727, -13074, -10685},
	{     0,    -12, -12408, -10190}, {     0,     33, -12790, -10624},
	{     0,     93, -13235, -10819}, {     0,   -363, -13301, -11183},
	{     0,   -514, -13955, -11557}, {     0,   -458, -13878, -11722},
	{     0,     93, -13086, -11726}, {     0,   -663, -13597, -11868},
	{     0,    174, -12814, -11295}, {     0,    121, -12278, -11150},
	{     0,    425, -11255, -10363}, {     0,    430, -11645, -10189},
	{     0,    277, -11844,  -9499}, {     0,   -132, -12241,  -9420},
	{     0,    448, -11934,  -9612}, {     0,    200, -11778,  -9582},
	{     0,    -40, -12069,  -9336}, {     0,   1692, -11315,  -9085},
	{     0,  -1753, -11445,  -9482}, {     0,  -2520, -12620, -10068},
	{     0,  -1976, -12674, -10218}, {     0,  -1984, -12525, -10354},
	{     0,  -2272, -13476, -10932}, {     0,  -2123, -13918, -11095},
	{     0,  -2883, -14447, -11659}, {     0,  -2826, -13946, -11563},
	{     0,  -2697, -13680, -11328}, {     0,  -3068, -13393, -11614},
	{     0,  -2935, -13074, -11882}, {     0,  -3017, -13038, -11163},
	{     0,  -2490, -12379, -11288}, {     0,  -2463, -12286, -10726},
	{     0,  -2686, -12167, -10673}, {     0,  -2428, -12077, -10555},
	{     0,  -2282, -12564, -10388}, {     0,  -2017, -12303, -10844},
	{     0,  -2329, -12637, -11250}, {     0,  -2703, -13362, -11807},
	{     0,  -1820, -12605, -11733}, {     0,    596, -10833,  -9852},
	{     0,      9, -11140,  -9776}, {     0,     41, -11168,  -9478},
	{     0,   -261, -11368,  -9752}, {     0,    226, -11282,  -9879},
	{     0,    -68, -11454, -10148}, {     0,   -314, -12278, -10716},
	{     0,    -66, -11998, -10762}, {     0,    -58, -11469, -10664},
	{     0,    319, -10759, -10212}, {     0,    122, -11041,  -9901},
	{     0,   -269, -11085, -10008}, {     0,    637, -10569,  -9674},
	{     0,   1185, -10632,  -9521}, {     0,    902, -11053,  -9681},
	{     0,    560, -11408,  -9588}, {     0,   1162, -11079,  -9371},
	{     0,   1780, -10547,  -9133}, 
};

const uint8_t hopPattern_2KBPS_20MS[L1_FRAME_2KBPS_20MS_HOPS] =
{
	0
};

const uint8_t hopPattern_2KBPS_100MS[L1_FRAME_2KBPS_100MS_HOPS] =
{
	 0,  1,  2,  3,  4,  5,  6,  7,
	 8,  9, 10, 11, 12, 13, 14, 15,
	16, 17, 18, 19, 20, 21, 22, 23,
	24, 25, 26, 27, 28, 29, 30, 31,
	32, 33, 34, 35, 36, 37, 38, 39,
	40, 41, 42, 43, 44, 45, 46, 47,
	48, 49, 50, 51, 52, 53, 54, 55,
	56, 57, 58, 59, 60, 61, 62, 63,
	64, 65, 66, 67, 68, 69, 70, 71,
	72, 73, 74, 75, 76, 77, 78, 79,
	80
};

const uint8_t hopPattern_10KBPS_10MS[L1_FRAME_10KBPS_10MS_HOPS] =
{
	80
};

const uint8_t hopPattern_10KBPS_20MS[L1_FRAME_10KBPS_20MS_HOPS] =
{
	 80,  1,  2,  3,  4,  5,  6,  7,
	 8,  9, 10
};

const uint8_t hopPattern_40KBPS_10MS[L1_FRAME_40KBPS_10MS_HOPS] =
{
	 0,  1,  2,  3,  4,  5,  6,  7,
	 8,
};

const uint8_t hopPattern_40KBPS_20MS[L1_FRAME_40KBPS_20MS_HOPS] =
{
	 0,  1,  2,  3,  4,  5,  6,  7,
	 8,  9, 10, 11, 12, 13, 14, 15,
	16, 17, 18
};

const uint8_t hopPattern_200KBPS_5MS[L1_FRAME_200KBPS_5MS_HOPS] =
{
	 0,  1,  2,  3,  4,  5,  6,  7,
	 8,  9, 10, 11, 12, 13, 14, 15,
	16, 17, 18, 19
};

const uint8_t hopPattern_1M2BPS_5MS[L1_FRAME_1M2BPS_5MS_HOPS] =
{
	 0,  1,  2,  3,  4,  5,  6,  7,
	 8,  9, 10, 11, 12, 13, 14, 15,
	16, 17, 18, 19
};

/** @} PhysicalLayer_AntennaConfig_Constants*/

/*****************************************************************************/
/**
 * @brief	Initialize antenna configurations (load preset values)
 *
 * @param   antConfig	pointer to antenna configure
 *
 * @return	None.
 *
 ******************************************************************************/
void Phy_AntConfigInit(PHY_ANT_CONFIG *antConfig)
{
	ASSERT(antConfig != NULL);

	antConfig->ctrlWord = CTRL_WORD_DEFAULT;
	antConfig->antMode = ATN_MODE_DEFAULT;

	// FIXME: Dummy default value.
	// off axis angle setting
	antConfig->oaxis[0] = 0;
	antConfig->oaxis[1] = 0;
	// round axis angle setting
	antConfig->raxis[0] = 0;
	antConfig->raxis[1] = 0;
	// ***********************

	antConfig->antFreq[0] = (uint8_t)(DEFAULT_CENTRTAL_FREQ);
	antConfig->antFreq[1] = (uint8_t)(DEFAULT_CENTRTAL_FREQ >> 8);

	// FIXME: Dummy default value.
	// trSelect
	antConfig->trSlect = 0;
	// Antenna attenuation setting
	antConfig->antAtt = 0;
	// phase setting
	antConfig->phase = 0;
	// ***********************

	antConfig->rxtxMode = RXTXMODE_DEFAULT;
	antConfig->calibrMode = CALIBRMODE_NORMAL;
	antConfig->antReserved = 0x00;

	// FIXME: Dummy default value.
	// RF mode control
	antConfig->rfCtrl = RF_CTRL_DEFAULT;
	// RF frequency
	antConfig->rfFreq[0] = (uint8_t)(DEFAULT_CENTRTAL_FREQ);
	antConfig->rfFreq[1] = (uint8_t)(DEFAULT_CENTRTAL_FREQ >> 8);
	// RF MGC control
	antConfig->rfTxMgc = 0;
	// RF channel 1&2 calibration mgc
	antConfig->rf12CalMgc = 0;
	// RF channel 2&3 calibration mgc
	antConfig->rf34CalMgc = 0;
	// Receive channel 1-4 MGC
	antConfig->rcvMgc = 0;
	antConfig->rfReserved0 = 0x00;
	antConfig->rfReserved1 = 0x00;
	// ***********************
}

/*****************************************************************************/
/**
 * @brief	Update antenna power mode setting
 *
 * @param   instPtr	pointer to physical layer instance
 * @param   mode	new antenna mode
 *
 * @return	None.
 *
 ******************************************************************************/
void Phy_AntConfigMode(PHY_INST *instPtr, PHY_ANT_MODE mode)
{
	ASSERT(instPtr != NULL);
	uint8_t config = instPtr->antConfig.antMode & ANT_MODE_BEAM_MASK;
	switch (mode)
	{
	case PHY_ANT_MODE_STANDBY:
		config |= ANT_POWER_STANDBY;
		break;
	case PHY_ANT_MODE_POWER_LOW:
		config |= ANT_POWER_LOW;
		break;
	case PHY_ANT_MODE_POWER_FULL:
		config |= ANT_POWER_FULL;
		break;
	default:
		DBG_ERROR("Unknown antenna mode: %" PRIu8 ", remain previous mode.\n", mode);
		return;
	}
	instPtr->phyLog.antMode = mode;
	instPtr->antConfig.antMode = config;
	// Sync logs to system monitor
	SysMonitor_UpdatePhysicalLayerLog(&(instPtr->phyLog));
}

/*****************************************************************************/
/**
 * @brief	Update antenna beam table
 *
 * @param   instPtr	pointer to physical layer instance
 * @param   table	new beam table
 * @param   offset	table offset
 *
 * @return	None
 *
 ******************************************************************************/
void Phy_SetBeamTable(PHY_INST *instPtr, PHY_ANT_ANGLE *table, uint32_t offset)
{
	ASSERT(instPtr != NULL);
	ASSERT(table != NULL);
	for (int i = 0; i < L1_NUM_ANT_NRRW_PATTERN; i++)
	{
		instPtr->antNrrwPattern[i] = table[(i + offset) % L1_NUM_ANT_NRRW_PATTERN];
	}
	memcpy((uint32_t *)instPtr->antInfoAddr, instPtr->antNrrwPattern,
		   L1_NUM_ANT_NRRW_PATTERN * sizeof(PHY_ANT_ANGLE));
}

/*****************************************************************************/
/**
 * @brief	Update antenna point direction using predefined beam table
 *
 * @param   instPtr	pointer to physical layer instance
 * @param   beamNo	new beam index
 *
 * @return	SUCCESS.
 * 			FAILURE beamNo exceeds the table size
 *
 ******************************************************************************/
int32_t Phy_SetBeamNo(PHY_INST *instPtr, int32_t beamNo, PHY_ANT_BEAM_WIDTH beamWidth)
{
	ASSERT(instPtr != NULL);

	if (beamWidth == PHY_ANT_BEAM_16)
	{
		if (beamNo >= L1_NUM_ANT_NRRW_PATTERN)
		{
			return FAILURE;
		}
		else
		{
			instPtr->beamNo.next = beamNo;
			instPtr->antConfig.oaxis[0] = (uint8_t)(instPtr->antNrrwPattern[beamNo].oaxis >> 8);
			instPtr->antConfig.oaxis[1] = (uint8_t)(instPtr->antNrrwPattern[beamNo].oaxis);
			instPtr->antConfig.raxis[0] = (uint8_t)(instPtr->antNrrwPattern[beamNo].raxis >> 8);
			instPtr->antConfig.raxis[1] = (uint8_t)(instPtr->antNrrwPattern[beamNo].raxis);
			return SUCCESS;
		}
	}
	else
	{
		// Beam number assign not support.
		// Assign default angle (0,0)
		instPtr->beamNo.next = 0;
		instPtr->antConfig.oaxis[0] = 0;
		instPtr->antConfig.oaxis[1] = 0;
		instPtr->antConfig.raxis[0] = 0;
		instPtr->antConfig.raxis[1] = 0;
		DBG_WARNING("Using default beam number...\n");
		return SUCCESS;
	}
}

/*****************************************************************************/
/**
 * @brief	Update antenna beam width setting
 *
 * @param   instPtr		pointer to physical layer instance
 * @param   beamWidth	new beam width
 *
 * @return	SUCCESS.
 * 			EPARAM
 *
 ******************************************************************************/
int32_t Phy_SetBeamWidth(PHY_INST *instPtr, PHY_ANT_BEAM_WIDTH beamWidth)
{
	ASSERT(instPtr != NULL);
	uint8_t beamConfig = instPtr->antConfig.antMode & (~ANT_MODE_BEAM_MASK);
	switch (beamWidth)
	{
	case PHY_ANT_BEAM_120:
		beamConfig |= ANT_BEAM_WIDE;
		break;
	case PHY_ANT_BEAM_60:
		beamConfig |= ANT_BEAM_SUBNARROR_1;
		break;
	case PHY_ANT_BEAM_25:
		beamConfig |= ANT_BEAM_SUBNARROR_0;
		break;
	case PHY_ANT_BEAM_16:
		beamConfig |= ANT_BEAM_NARROR;
		break;
	default:
		DBG_ERROR("Beam width code: 0x%08X is illegal.\n\r", beamWidth);
		return EPARAM;
	}
	instPtr->antConfig.antMode = beamConfig;
	instPtr->beamWd.next = beamWidth;
	return SUCCESS;
}

/*****************************************************************************/
/**
 * @brief	Set antenna to a specific direction
 *
 * @param   instPtr	pointer to physical layer instance
 * @param   offAx	elevation angle
 * @param   rndAx	azimuth angle
 *
 * @return	SUCCESS.
 * 			EPARAM - the direction is exceeds predefined range
 *
 ******************************************************************************/
int32_t Phy_SetBeamAngle(PHY_INST *instPtr, double offAx, double rndAx)
{
	ASSERT(instPtr != NULL);
	uint16_t offAxDeg = 0;
	uint16_t rndAxDeg = 0;
	offAxDeg = (offAx >= 0) ? (((int32_t)(offAx * 180 / ANT_PI)) % 360) : (((int32_t)(offAx * 180 / ANT_PI)) % 360 + 360);
	rndAxDeg = (rndAx >= 0) ? (((int32_t)(rndAx * 180 / ANT_PI)) % 360) : (((int32_t)(rndAx * 180 / ANT_PI)) % 360 + 360);
	if (offAxDeg > 60)
	{
		DBG_ERROR("Angle out of range, off axis angle: %" PRIu16 " (should be 0~60), round axis angle: %" PRIu16 " (should be 0~360).\n",
				  offAxDeg, rndAxDeg);
		return EPARAM;
	}
	offAxDeg = offAxDeg * 10;
	rndAxDeg = rndAxDeg * 10;
	instPtr->antConfig.oaxis[0] = (uint8_t)(offAxDeg >> 8);
	instPtr->antConfig.oaxis[1] = (uint8_t)offAxDeg;
	instPtr->antConfig.raxis[0] = (uint8_t)(rndAxDeg >> 8);
	instPtr->antConfig.raxis[1] = (uint8_t)rndAxDeg;
	return SUCCESS;
}

/** @} PhysicalLayer_AntennaControl*/

/** @} PhysicalLayer*/