#include "JQ_UIDGenerate.h"
#include "stm32f10x.h"
#include "string.h"
//#include "Common.h"


static uint32_t UINT32_ENDIAN_CHANGE(uint32_t val);

static bool YMUid_Valid_check(uint8_t *u8ArrayYMUid);

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


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;
	
	u16 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)
{
	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;
	
	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_YM2JQ(PST_JQUID_TYPEDEF pstJQUID,const PST_YMUID_TYPEDEF pstYMUID)
{
	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;
	
	if(!YMUid_Valid_check(pstYMUID->ucArrayYMUidCode))
		return false;
	
	
	
	stSerialCode.u32Value = 0;
	
	pstJQUID->u8ArrayJQUidCode[0] = (pstYMUID->ucArrayYMUidCode[0]-0x30)*10 + (pstYMUID->ucArrayYMUidCode[1]-0x30);//企业代码
	pstJQUID->u8ArrayJQUidCode[1] = pstYMUID->ucArrayYMUidCode[4];//特征码
	
	u16Year = (pstYMUID->ucArrayYMUidCode[2]-0x30)*10 + (pstYMUID->ucArrayYMUidCode[3]-0x30);
	u16Month = (pstYMUID->ucArrayYMUidCode[5]-0x30)*10 + (pstYMUID->ucArrayYMUidCode[6]-0x30);
	u8Day = (pstYMUID->ucArrayYMUidCode[7]-0x30)*10 + (pstYMUID->ucArrayYMUidCode[8]-0x30);
	u16Date = (u16Year&0x7F)<<9|(u16Month&0x0F)<<5|(u8Day&0x1F);
	pstJQUID->u8ArrayJQUidCode[2] =  (u16Date&0xFF00)>>8;
	pstJQUID->u8ArrayJQUidCode[3] =  (u16Date&0x00FF);
	

	for(i=10;i<15;i++)
	{
		stSerialCode.u32Value = stSerialCode.u32Value*10 + (pstYMUID->ucArrayYMUidCode[i]-0x30);
	}
	
	stSerialCode.u32Value |=  pstYMUID->ucArrayYMUidCode[DEF_YM_SPECIALCODE_INDEX]<<17;
	stSerialCode.u32Value = UINT32_ENDIAN_CHANGE(stSerialCode.u32Value);;

	memcpy(pstJQUID->u8ArrayJQUidCode+4,stSerialCode.u8Val+1,3);
	
	pstJQUID->u8ArrayJQUidCode[7] = _CalcCrc8(pstJQUID->u8ArrayJQUidCode,7);
	
	return true;
}



//============================================================================//
//8位CRC效验计算,只计算缓存区前6个字节
//输入参数：需要计算数据缓存区头指针
//返 回 值：效验值
//============================================================================//
static u8 _CalcCrc8(u8 *Ptr,u8 num)         
{
    u8 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;
}

static bool YMUid_Valid_check(uint8_t *u8ArrayYMUid)
{
	uint8_t i=0;
	uint8_t u8ValidDataCounter = 0;
	
	for(i=0;i<15;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 ));
}

