/*
*********************************************************************************************************
*                                       IAR Development Kits
*                                              on the
*
*                                    Nano130
*
* Filename      : def_data.c
* Version       : V1.00
* Programmer(s) : Qian Xianghong
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                             INCLUDE FILES
*********************************************************************************************************
*/

#include  "includes.h"

// 兰石版本
//#define _LANSHI_VER_

// 硬件版本号
const uint8_t HARDWARE_VER_MAJOR = 1;
const uint8_t HARDWARE_VER_MINOR = 137;	// 罐箱RF网关
// 软件版本号
const uint8_t SOFTWARE_VER_MAJOR = 1;
const uint8_t SOFTWARE_VER_MINOR = 1;	// 罐箱RF网关
// 软件日期
const uint8_t SOFTWARE_DATE_YEAR = 25;
const uint8_t SOFTWARE_DATE_MONTH = 5;
const uint8_t SOFTWARE_DATE_DAY = 16;

// 存储格式版本号=1.1
const uint8_t CONFIG_FORMAT_VER_MAJOR = 1;
const uint8_t CONFIG_FORMAT_VER_MINOR = 1;	// 新结构整体式，带双向通信

// 远传协议版本号
const uint8_t REPORT_PROTOCOL_VER_MAJOR = 0;
// 0: 压力单位为bar，1：压力单位为KPa, 107: 支持历史数据发送
const uint8_t REPORT_PROTOCOL_VER_MINOR = 107;

// DTU参数
const uint32_t DTU_BAUDRATE = 9600ul;
const uint8_t DTU_PARITY = PARITY_NONE;

// 蓝牙协议版本号
const uint8_t BLUETOOTH_PROTOCOL_VER_MAJOR = 1;
const uint8_t BLUETOOTH_PROTOCOL_VER_MINOR = 0;

// 蓝牙参数
const uint32_t BLUETOOTH_BAUDRATE = 9600ul;
const uint8_t BLUETOOTH_PARITY = PARITY_NONE;

// 存放全部配置和采集数据的结构体
data_buff_t dcBuff = {0};

// 各个数据块的长度
const uint8_t	Config_DataLen[] =
{
	sizeof(config_bottle_t),
	sizeof(config_sensor_t),
	sizeof(config_data_t),
	sizeof(config_display_t),
	sizeof(sysinfo_sample_t),
	sizeof(sysinfo_power_t),
	sizeof(sysinfo_display_t),
	sizeof(sysinfo_bluetooth_t),
	sizeof(data_dtu_t),
	sizeof(data_sample_t),
	sizeof(adc_sample_t),
};

// 各个数据区的地址
const void*	Config_DataPtr[] =
{
	&dcBuff.configBottle,
	&dcBuff.configSensor,
	&dcBuff.configData,
	&dcBuff.configDisplay,
	&dcBuff.sampleInfo,
	&dcBuff.powerInfo,
	&dcBuff.displayInfo,
	&dcBuff.bluetoothInfo,
	&dcBuff.dtuData,
	&dcBuff.sampleData,
	&dcBuff.adcData,	
};

// 液体密度：LNG=0.42g/cm3=420kg/m3
//            O2=1.143g/cm3=1143kg/m3
//            N2=0.81g/cm3=810kg/m3
//            AR=1.402g/cm3=1402kg/m3
//           CO2=1.024g/cm3=1024kg/m3
const float LSrc_Density[] = {0.42, 1.143, 0.81, 1.402, 1.024}; // 单位kg/L

// 多项式为x16+x15+x2+1,LSB顺序
uint16_t do_crc_16(unsigned short crc_reg, uint8_t *message, int16_t len)
{
    int16_t i, j;
    uint16_t current;
       
    for (i = 0; i < len; i++)
    {
        current = message[i];
        for (j = 0; j < 8; j++)
        {
            if ((crc_reg ^ current) & 0x0001)
                crc_reg = (crc_reg >> 1) ^ 0xA001;
            else
                crc_reg >>= 1;
            current >>= 1;
        }
    }
    return crc_reg;
}

// 检查固定的字段
uint32_t Config_CheckValid(rd_buff_t *rdBuff, uint8_t DC)
{
	if(rdBuff->header.mark != CONFIG_MARK_LS)
		return FALSE;
	if(rdBuff->header.product != CONFIG_PRODUCT_CY)
		return FALSE;
	if(rdBuff->header.DC != DC)
		return FALSE;
	if(rdBuff->header.ver.major != CONFIG_FORMAT_VER_MAJOR || rdBuff->header.ver.minor != CONFIG_FORMAT_VER_MINOR)
		return FALSE;
	if(do_crc_16(0, rdBuff->buff, CONFIG_DATA_LEN(DC)) != 0)
		return FALSE;
	return TRUE;
}

// 写入固定的字段
void Config_ForceValid(rd_buff_t *rdBuff, uint8_t DC)
{
	uint8_t pos = CONFIG_DATA_LEN(DC) - 2;
	
	rdBuff->header.mark = CONFIG_MARK_LS;
	rdBuff->header.product = CONFIG_PRODUCT_CY;
	rdBuff->header.DC = DC;
	rdBuff->header.ver.major = CONFIG_FORMAT_VER_MAJOR;
	rdBuff->header.ver.minor = CONFIG_FORMAT_VER_MINOR;
	*(uint16_t *) (rdBuff->buff + pos) = do_crc_16(0, rdBuff->buff, pos);
}

// 设置储罐参数的默认值
void Config_setBottleDefParam(data_buff_t *dataBuff)
{
	uint8_t i;
	
	memset(&dataBuff->configBottle, 0, sizeof(dataBuff->configBottle));

	dataBuff->configBottle.serverVer = 0;	// 服务器参数版本号
	dataBuff->configBottle.updateFlag = 1;	// 参数更新标志
	
	dataBuff->configBottle.addr = 1;
	dataBuff->configBottle.type = BOTTLE_TYPE_STAND;
	dataBuff->configBottle.source = SOURCE_N2;
	dataBuff->configBottle.len = 10000;
	dataBuff->configBottle.diameter = 2650;
	dataBuff->configBottle.lenExtra = 0;
	for(i = 0; i < sizeof(dataBuff->configBottle.density) / sizeof(float); i++)
		dataBuff->configBottle.density[i] = LSrc_Density[i];
	dataBuff->configBottle.chargePct = 90;
	dataBuff->configBottle.measureType = MEASURE_DPRESS;
	
	// 以下参数，如为0则表示无效
	dataBuff->configBottle.fullPct = dataBuff->configBottle.chargePct * 100;	// 满液位百分比
	dataBuff->configBottle.priPct = 0;		// 关键液位百分比
	dataBuff->configBottle.orderPct = 0;	// 订货液位百分比
	dataBuff->configBottle.emptyPct = 0;	// 空液位百分比
	dataBuff->configBottle.planMount = 0;	// 每日计划用量
	dataBuff->configBottle.predictMount = 0;	// 每日预测用量
	dataBuff->configBottle.warnVolt = 0;		// 电压报警低点: 10mV
	dataBuff->configBottle.warnVoltH = 0;		// 电压报警高点: 10mV
	dataBuff->configBottle.warnPress = 300;		// 压力报警低点: KPa
	dataBuff->configBottle.warnPressH = 0;		// 压力报警高点
	dataBuff->configBottle.options = 0x0003;	// 参数开关
	dataBuff->configBottle.ts = 0;				// 参数时间戳
	dataBuff->configBottle.dispMode = DISP_MODE_ALL; // 显示模式
	
	// 设置报警的默认参数
	dataBuff->configBottle.emptyPct = 0;
	dataBuff->configBottle.fullPct = 9800;
	dataBuff->configBottle.warnPress = 50;
	dataBuff->configBottle.warnPressH = 650;
	dataBuff->configBottle.warnTempr = -162;
	dataBuff->configBottle.warnTemprH = 50;
	dataBuff->configBottle.offlineSeconds = 14400;
	dataBuff->configBottle.lora_freq=0;
	Config_ForceValid((rd_buff_t *) &dataBuff->configBottle, CAN_DC_BOTTLE_CONFIG);
}

// 设置传感器参数的默认值
void Config_setSensorDefParam(data_buff_t *dataBuff)
{
	uint8_t i;
	
	memset(&dataBuff->configSensor, 0, sizeof(dataBuff->configSensor));

	dataBuff->configSensor.sensorWeight.lowRange = 0;
	dataBuff->configSensor.sensorWeight.highRange = 100000ul;	// 100000kg
	dataBuff->configSensor.sensorWeight.zeroValue = 84030ul;	// 经验值
	dataBuff->configSensor.sensorWeight.fullValue = 1613530ul;	// 经验值

	dataBuff->configSensor.sensorDPress.lowRange = 0;			// 0KPa
	dataBuff->configSensor.sensorDPress.highRange = 50;         // 50KPa
	dataBuff->configSensor.sensorDPress.zeroValue = 683;	 	// 理论值：0.5v / 3.0v
	dataBuff->configSensor.sensorDPress.fullValue = 3823; 		// 理论值：2.8v / 3.0v

	dataBuff->configSensor.sensorPress.lowRange = 0;			// 0MPa
	dataBuff->configSensor.sensorPress.highRange = 4000;        // 4MPa
	dataBuff->configSensor.sensorPress.zeroValue = 683;	 		// 理论值：0.5v / 3.0v
	dataBuff->configSensor.sensorPress.fullValue = 3823; 		// 理论值：2.8v / 3.0v
	
	for(i = 0; i < sizeof(dataBuff->configSensor.sensorEPress) / sizeof(press_param_t); i++)
	{
		dataBuff->configSensor.sensorEPress[i].lowRange = 0;		// 0MPa
		dataBuff->configSensor.sensorEPress[i].highRange = 4000;    // 4MPa
		dataBuff->configSensor.sensorEPress[i].zeroValue = 710;		// 理论值：4mA * 130Ω / 3.0v
		dataBuff->configSensor.sensorEPress[i].fullValue = 3550;	// 理论值：20mA * 130Ω / 3.0v
	}

	for(i = 0; i < sizeof(dataBuff->configSensor.sensorETempr) / sizeof(tempr_param_t); i++)
	{
		dataBuff->configSensor.sensorETempr[i].lowRange = -200;		// -200℃
		dataBuff->configSensor.sensorETempr[i].highRange = 80;		// 80℃
		dataBuff->configSensor.sensorETempr[i].zeroValue = 710;		// 理论值：4mA * 130Ω / 3.0v
		dataBuff->configSensor.sensorETempr[i].fullValue = 3550;	// 理论值：20mA * 130Ω / 3.0v
	}

	Config_ForceValid((rd_buff_t *) &dataBuff->configSensor, CAN_DC_SENSOR_CONFIG);
}

// 设置数据参数的默认值
void Config_setDataDefParam(data_buff_t *dataBuff)
{
	memset(&dataBuff->configData, 0, sizeof(dataBuff->configData));

#ifndef _LANSHI_VER_	
	strcpy(dataBuff->configData.server, "data.ajhiot.com");	// 2021.03.31修改
	dataBuff->configData.port = 8088;
#else
	strcpy(dataBuff->configData.server, "pazx.cd-ls.cn");
	dataBuff->configData.port = 8088;
#endif	
#ifndef _LANSHI_VER_	
	strcpy(dataBuff->configData.gpsServer, "track.ajhiot.com");
	dataBuff->configData.gpsPort = 8070;
#else
	strcpy(dataBuff->configData.gpsServer, "track.cd-ls.cn");
	dataBuff->configData.gpsPort = 8070;
#endif	
	dataBuff->configData.intervalSample = 300;
	dataBuff->configData.intervalTrans = 3600;
	dataBuff->configData.intervalGPS = 4;
	dataBuff->configData.intervalGPSTrans = 60;
	dataBuff->configData.timeLag = 8;

	strcpy(dataBuff->configData.upgServer, "extra.cd-ls.cn");
	dataBuff->configData.upgPort = 7006;
	
	Config_ForceValid((rd_buff_t *) &dataBuff->configData, CAN_DC_DATA_CONFIG);
}

// 设置显示参数的默认值
void Config_setDisplayDefParam(data_buff_t *dataBuff)
{
	memset(&dataBuff->configDisplay, 0, sizeof(dataBuff->configDisplay));

	dataBuff->configDisplay.lightDelay = 0;
	dataBuff->configDisplay.dispDelay = 30;
	dataBuff->configDisplay.unit = DISP_UNIT_METRIC;
	strcpy(dataBuff->configDisplay.pwd1, "2222");		// 储罐参数修改密码
	strcpy(dataBuff->configDisplay.pwd2, "3003");		// 传感器标定密码
	strcpy(dataBuff->configDisplay.APN, "CMIOT");

	Config_ForceValid((rd_buff_t *) &dataBuff->configDisplay, CAN_DC_DISPLAY_CONFIG);
}

// 填充采集板系统信息
void Config_fillSampleInfo(data_buff_t *dataBuff)
{
	memset(&dataBuff->sampleInfo, 0, sizeof(dataBuff->sampleInfo));
	
	dataBuff->sampleInfo.boardFlag = CONFIG_BOARD_SELF;
	dataBuff->sampleInfo.hardVer.major = HARDWARE_VER_MAJOR;
	dataBuff->sampleInfo.hardVer.minor = HARDWARE_VER_MINOR;
	dataBuff->sampleInfo.softVer.major = SOFTWARE_VER_MAJOR;
	dataBuff->sampleInfo.softVer.minor = SOFTWARE_VER_MINOR;
	dataBuff->sampleInfo.softDate.year = SOFTWARE_DATE_YEAR;
	dataBuff->sampleInfo.softDate.month = SOFTWARE_DATE_MONTH;
	dataBuff->sampleInfo.softDate.day = SOFTWARE_DATE_DAY;
}

// 填充电源板系统信息
void Config_fillPowerInfo(data_buff_t *dataBuff)
{
	memset(&dataBuff->powerInfo, 0, sizeof(dataBuff->powerInfo));
	
	dataBuff->powerInfo.boardFlag = CONFIG_BOARD_SELF;
	dataBuff->powerInfo.hardVer.major = HARDWARE_VER_MAJOR;
	dataBuff->powerInfo.hardVer.minor = HARDWARE_VER_MINOR;
	dataBuff->powerInfo.softVer.major = SOFTWARE_VER_MAJOR;
	dataBuff->powerInfo.softVer.minor = SOFTWARE_VER_MINOR;
	dataBuff->powerInfo.softDate.year = SOFTWARE_DATE_YEAR;
	dataBuff->powerInfo.softDate.month = SOFTWARE_DATE_MONTH;
	dataBuff->powerInfo.softDate.day = SOFTWARE_DATE_DAY;
	dataBuff->powerInfo.protocolVer.major = REPORT_PROTOCOL_VER_MAJOR;
	dataBuff->powerInfo.protocolVer.minor = REPORT_PROTOCOL_VER_MINOR;
	dataBuff->powerInfo.baudrate = DTU_BAUDRATE;
	dataBuff->powerInfo.parity = DTU_PARITY;
}

// 填充显示板系统信息
void Config_fillDisplayInfo(data_buff_t *dataBuff)
{
	memset(&dataBuff->displayInfo, 0, sizeof(dataBuff->displayInfo));
	
	dataBuff->displayInfo.boardFlag = CONFIG_BOARD_SELF;
	dataBuff->displayInfo.hardVer.major = HARDWARE_VER_MAJOR;
	dataBuff->displayInfo.hardVer.minor = HARDWARE_VER_MINOR;
	dataBuff->displayInfo.softVer.major = SOFTWARE_VER_MAJOR;
	dataBuff->displayInfo.softVer.minor = SOFTWARE_VER_MINOR;
	dataBuff->displayInfo.softDate.year = SOFTWARE_DATE_YEAR;
	dataBuff->displayInfo.softDate.month = SOFTWARE_DATE_MONTH;
	dataBuff->displayInfo.softDate.day = SOFTWARE_DATE_DAY;
}

// 填充蓝牙板系统信息
void Config_fillBluetoothInfo(data_buff_t *dataBuff)
{
	memset(&dataBuff->bluetoothInfo, 0, sizeof(dataBuff->bluetoothInfo));
	
	dataBuff->bluetoothInfo.boardFlag = CONFIG_BOARD_SELF;
	dataBuff->bluetoothInfo.hardVer.major = HARDWARE_VER_MAJOR;
	dataBuff->bluetoothInfo.hardVer.minor = HARDWARE_VER_MINOR;
	dataBuff->bluetoothInfo.softVer.major = SOFTWARE_VER_MAJOR;
	dataBuff->bluetoothInfo.softVer.minor = SOFTWARE_VER_MINOR;
	dataBuff->bluetoothInfo.softDate.year = SOFTWARE_DATE_YEAR;
	dataBuff->bluetoothInfo.softDate.month = SOFTWARE_DATE_MONTH;
	dataBuff->bluetoothInfo.softDate.day = SOFTWARE_DATE_DAY;
	dataBuff->bluetoothInfo.protocolVer.major = BLUETOOTH_PROTOCOL_VER_MAJOR;
	dataBuff->bluetoothInfo.protocolVer.minor = BLUETOOTH_PROTOCOL_VER_MINOR;
	dataBuff->bluetoothInfo.baudrate = BLUETOOTH_BAUDRATE;
	dataBuff->bluetoothInfo.parity = BLUETOOTH_PARITY;
}

// 检查参数修改标志
uint32_t Config_CheckUpdate(data_buff_t *dataBuff)
{
	uint8_t i;
	
	if(!dataBuff->configBottle.updateFlag)
		return FALSE;
	
	for(i = 0; i < 6; i++)
	{
		if(dataBuff->configBottle.PSN[i] != 0)
			return TRUE;
	}
	
	return FALSE;
}
