#include "UIDGenerate.h"
#include "string.h"


//static u8 _CalcCrc8(u8 *Ptr,u8 num);

static uint32_t UINT32_ENDIAN_CHANGE(uint32_t val);

static bool YMUid_Valid_check(uint8_t *u8ArrayYMUid);

static uint8_t CalcCrc8(uint8_t *Ptr,uint8_t num);

static void string2hex(char* src,size_t srclen,char* dst,size_t *dstlen);
static bool byte2String(uint8_t *src, size_t srcoffset, size_t srclen, char* dst, size_t dstoffset, size_t *dstlen);



bool UidCode_JQ2YM(const PST_JQUID_TYPEDEF pstJQUID,PST_YMUID_TYPEDEF pstYMUID)
{
	UNION_VALUE_CHANGE_TYPEDEF stDate;
	UNION_VALUE_CHANGE_TYPEDEF stSerialCode ;
	uint8_t i=0;
	uint16_t u16Year = 0;
	uint16_t u16Month = 0;
	uint8_t u8Day = 0;
	uint16_t u16Date =0;
	ST_JQUID_TYPEDEF _stJQUID;
	
	uint16_t sum=0;
	
	for(i=0;i<8;i++)
	{
		sum=sum+pstJQUID->u8ArrayJQUidCode[i];
		_stJQUID.u8ArrayJQUidCode[7-i]=pstJQUID->u8ArrayJQUidCode[i];				//逆序detid
	}
	memcpy(pstJQUID->u8ArrayJQUidCode,_stJQUID.u8ArrayJQUidCode,8);
	
	if(sum == 0)  						//全0时，CRC码也为0，所以需要避免这种情况	
		return 0;   
	
	
	if(pstJQUID->u8ArrayJQUidCode[7] != CalcCrc8(pstJQUID->u8ArrayJQUidCode,7))
		return false;
	
	pstYMUID->ucArrayYMUidCode[0] = pstJQUID->u8ArrayJQUidCode[0]/10+0x30;
	pstYMUID->ucArrayYMUidCode[1] = pstJQUID->u8ArrayJQUidCode[0]%10+0x30;
	
	
	
	pstYMUID->ucArrayYMUidCode[4] = pstJQUID->u8ArrayJQUidCode[1];//特征码
	
	u16Date = pstJQUID->u8ArrayJQUidCode[2];
	u16Date = u16Date<<8 | pstJQUID->u8ArrayJQUidCode[3];
	
	u16Year = (u16Date>>9)&0x7F;
	u16Month = (u16Date>>5)&0x0F;
	u8Day = u16Date&0x1F;
	
	pstYMUID->ucArrayYMUidCode[2] = u16Year/10+0x30;
	pstYMUID->ucArrayYMUidCode[3] = u16Year%10+0x30;
	
	pstYMUID->ucArrayYMUidCode[5] = u16Month/10+0x30;
	pstYMUID->ucArrayYMUidCode[6] = u16Month%10+0x30;
	
	pstYMUID->ucArrayYMUidCode[7] = u8Day/10+0x30;
	pstYMUID->ucArrayYMUidCode[8] = u8Day%10+0x30;
	
	
	memcpy(stSerialCode.u8Val+1,pstJQUID->u8ArrayJQUidCode+4,3);
	stSerialCode.u32Value = UINT32_ENDIAN_CHANGE( stSerialCode.u32Value);
	
	pstYMUID->ucArrayYMUidCode[DEF_YM_SPECIALCODE_INDEX] = (stSerialCode.u32Value>>17) & 0x7F;
	stSerialCode.u32Value = stSerialCode.u32Value&0x1FFFF;
	
	for(i=14;i>=10;i--)
	{
			pstYMUID->ucArrayYMUidCode[i] = (stSerialCode.u32Value%10+0x30);
			stSerialCode.u32Value = stSerialCode.u32Value/10;
		
	}
	
	
	if(!YMUid_Valid_check(pstYMUID->ucArrayYMUidCode))
		return false;
	
	return true;
}

bool UidCode_JQ2YM_NoBackwardSequence(const PST_JQUID_TYPEDEF pstJQUID,PST_YMUID_TYPEDEF pstYMUID)
{
	uint8_t i=0;
	uint8_t ucYear = 0;
	uint8_t ucMonth = 0;
	uint8_t ucDay = 0;
	uint32_t ulNumber =0;
	uint8_t ucTeZhengMa = 0;
	uint16_t usBoxCode = 0;
	uint8_t ucIndexCode = 0;
	size_t len = 16;
	
	if(pstJQUID->u8ArrayJQUidCode[7] != CalcCrc8(pstJQUID->u8ArrayJQUidCode,7))
		return false;
	
	byte2String(pstJQUID->u8ArrayJQUidCode,0,2,(char *)pstYMUID->ucArrayYMUidCode,0,&len);//AD58
	pstYMUID->ucArrayYMUidCode[4] = pstYMUID->ucArrayYMUidCode[2];
	pstYMUID->ucArrayYMUidCode[5] = pstYMUID->ucArrayYMUidCode[3];
	
	
	ucYear = pstJQUID->u8ArrayJQUidCode[2]>>1;
	pstYMUID->ucArrayYMUidCode[2] = ucYear/10+0x30;
	pstYMUID->ucArrayYMUidCode[3] = ucYear%10+0x30;
	
	ucMonth = ((pstJQUID->u8ArrayJQUidCode[2]&0x01)<<3)|((pstJQUID->u8ArrayJQUidCode[3]>>5));
	pstYMUID->ucArrayYMUidCode[6] = ucMonth/10+0x30;
	pstYMUID->ucArrayYMUidCode[7] = ucMonth%10+0x30;
	
	ucDay = (pstJQUID->u8ArrayJQUidCode[3]&0x1F);
	pstYMUID->ucArrayYMUidCode[8] = ucDay/10+0x30;
	pstYMUID->ucArrayYMUidCode[9] = ucDay%10+0x30;
	
	ulNumber = pstJQUID->u8ArrayJQUidCode[4];
	ulNumber <<= 8;
	ulNumber |= pstJQUID->u8ArrayJQUidCode[5];
	ulNumber <<= 8;
	ulNumber |= pstJQUID->u8ArrayJQUidCode[6];
	
	ucTeZhengMa = ulNumber/100000;
	pstYMUID->ucArrayYMUidCode[10] = ucTeZhengMa;
	
	ulNumber = ulNumber%100000;
	usBoxCode = ulNumber/100;
	pstYMUID->ucArrayYMUidCode[11] = usBoxCode/100 + 0x30;
	usBoxCode%=100;
	pstYMUID->ucArrayYMUidCode[12] = usBoxCode/10+0x30;
	pstYMUID->ucArrayYMUidCode[13] = usBoxCode%10 + 0x30;
	
	ucIndexCode = ulNumber%100;
	pstYMUID->ucArrayYMUidCode[14] = ucIndexCode/10+0x30;
	pstYMUID->ucArrayYMUidCode[15] = ucIndexCode%10 + 0x30;
		
	if(!YMUid_Valid_check(pstYMUID->ucArrayYMUidCode))
		return false;
	
	return true;
}



bool UidCode_YM2JQ(PST_JQUID_TYPEDEF pstJQUID,const PST_YMUID_TYPEDEF pstYMUID)
{

	uint8_t i=0;
	uint16_t usYear = 0;
	uint16_t usMonth = 0;
	uint16_t usDay = 0;
	uint32_t ulNumber =0;
	uint16_t usBoxCode = 0;
	uint8_t ucIndexCode = 0;
	size_t dataLen = 0;
	
	if(!YMUid_Valid_check(pstYMUID->ucArrayYMUidCode))
		return false;
	
	string2hex((char *)pstYMUID->ucArrayYMUidCode,6,(char *)pstJQUID->u8ArrayJQUidCode,&dataLen); //0xAD 0x23 0x58
	
	pstJQUID->u8ArrayJQUidCode[1] = pstJQUID->u8ArrayJQUidCode[2];//0xAD 0x58
	
	//转换 年 月 日
	usYear = (pstYMUID->ucArrayYMUidCode[2]-0x30)*10 + (pstYMUID->ucArrayYMUidCode[3]-0x30);
	usMonth = (pstYMUID->ucArrayYMUidCode[6]-0x30)*10 + (pstYMUID->ucArrayYMUidCode[7]-0x30);
	usDay = (pstYMUID->ucArrayYMUidCode[8]-0x30)*10 + (pstYMUID->ucArrayYMUidCode[9]-0x30);
	
	usYear = usYear<<9|usMonth<<5|usDay;

	pstJQUID->u8ArrayJQUidCode[2] = (usYear&0xFF00)>>8;
	pstJQUID->u8ArrayJQUidCode[3] = (usYear&0xFF);
	
	
	
	ulNumber = pstYMUID->ucArrayYMUidCode[10]*100000;
	usBoxCode = (pstYMUID->ucArrayYMUidCode[11]-0x30)*100+ (pstYMUID->ucArrayYMUidCode[12]-0x30)*10+(pstYMUID->ucArrayYMUidCode[13]-0x30);
	ucIndexCode = (pstYMUID->ucArrayYMUidCode[14]-0x30)*10+(pstYMUID->ucArrayYMUidCode[15]-0x30);
	
	ulNumber += usBoxCode*100+ucIndexCode;
	
	pstJQUID->u8ArrayJQUidCode[4] = (ulNumber&0xFF0000)>>16;
	pstJQUID->u8ArrayJQUidCode[5] = (ulNumber&0xFF00)>>8;
	pstJQUID->u8ArrayJQUidCode[6] = (ulNumber&0xFF);

	
	pstJQUID->u8ArrayJQUidCode[7] = CalcCrc8(pstJQUID->u8ArrayJQUidCode,7);
	
	return true;
}


static bool YMUid_Valid_check(uint8_t *u8ArrayYMUid)
{
	uint8_t i=0;
	uint8_t u8ValidDataCounter = 0;
	
	for(i=0;i<16;i++)
	{
		if(u8ArrayYMUid[i] >= '0' && u8ArrayYMUid[i] <= '9')
		{
			u8ValidDataCounter++;
			continue;
		}
		if(u8ArrayYMUid[i] >= 'A' && u8ArrayYMUid[i] <= 'Z')
		{
			u8ValidDataCounter++;
			continue;
		}
		if(u8ArrayYMUid[i] >= 'a' && u8ArrayYMUid[i] <= 'z')
		{
			u8ValidDataCounter++;
			continue;
		}
	}
	
	if(u8ValidDataCounter < 7)
		return false;
	return true;
}

static uint32_t UINT32_ENDIAN_CHANGE(uint32_t val)
{
	
	return  ((( val & 0x000000ff)<< 24 ) | ( ( val & 0x0000ff00 ) << 8 ) | ( ( val & 0x00ff0000 ) >> 8 ) | ( ( val & 0xff000000 ) >> 24 ));
}

static void string2hex(char* src,size_t srclen,char* dst,size_t *dstlen)
{
	size_t i=0;
	uint8_t tmp1=0,tmp2=0;
	for(;i<srclen;i++){
		if(src[i]<='9')
			tmp1 = src[i]-'0';
		else
			tmp1 = src[i]-'A'+10;
		i++;
		if(src[i]<='9')
			tmp2 = src[i]-'0';
		else
			tmp2 = src[i]-'A'+10;
		*dst++ = tmp1*16+tmp2;
		(*dstlen)++;
	}
}


static bool byte2String(uint8_t *src, size_t srcoffset, size_t srclen, char* dst, size_t dstoffset, size_t *dstlen)
{
	char MatirxHex[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
	size_t i = 0;
	
	if((srclen-srcoffset)*2 > *dstlen-dstoffset)
		return false;
	
	dst += dstoffset;
	
	for(i=srcoffset;i<srclen;i++)
	{
		*dst++ = MatirxHex[(src[i]&0xF0)>>4];
		*dst++ = MatirxHex[src[i]&0x0F];
	}
	
	*dstlen = (i-1)*2;
	
	return true;
}

//============================================================================//
//8位CRC效验计算,只计算缓存区前6个字节
//输入参数：需要计算数据缓存区头指针
//返 回 值：效验值
//============================================================================//
static uint8_t CalcCrc8(uint8_t *Ptr,uint8_t num)         
{
    uint8_t i,j,crc = 0;
 
    for (j = 0; j < num; j++)
    {
        crc ^= *(Ptr+j);
        for ( i = 0; i < 8; i++)
        {
            if ((crc & 0x01) != 0)
            {
                crc >>= 1;
                crc ^= 0x8c;
            }
            else
            {
                crc >>= 1;
            }
        }
    }
    return crc;
}




