/*
 * File:   wf200s_rtcc_app.c
 * Author: KingRay
 *
 * Created on June 14, 2016, 4:22 PM
 */
 #include "sys_config.h"
#if CARD_USED
#include "UWater_timer1uS_driver.h"
#include "UWater_card_5557_driver.h"
#include "UWater_Valve_driver.h"

#include "UWater_rtcc_app.h"
#include "UWater_key_app.h"
#include "UWater_gp22_app.h"
#include "UWater_Valve_app.h"
#include "UWater_card_5557_app.h"

#include "libbase.h"

Card_Data card_source_data;
S_Card_Type1_Data card_source_data_congigue1_use;
BOOL card_in = FALSE;
BOOL card_read_sucess = FALSE;
static u8 card_output_msg = CARD_NONE_MSG; //脉冲模块输出消息
//Meter_Event_Para card_use_meter_state;
//S_Flash_Data_Processor_Para flash_data_processor_temp_para;
u8 card_key_machine_state = CARD_KEY_M_S0;
u8 card_key_check_num = 0;
BOOL card_key_flag = FALSE;

void card_generate_key(u8* buf, u8* key);

void SearchCardApp(void)
{
	if(RtccCheckTimer(RTCC_CARD_SEARCH_TIME) == 0)//休眠状态与GP30唤醒同步,2s检测一次卡片,唤醒状态1s检测一次卡片
	{
		RtccSetTimer(RTCC_CARD_SEARCH_TIME, CARD_SEACHR_TIME_SECOND); 
		RtccSetMsgApp(RtccMsg_Search_Card);
	}
	if(Gp30IfSleepApp() == TRUE)
	{
	    if(RtccCheckMsgApp(RtccMsg_Search_Card))
		{
			RtccClearMsgApp(RtccMsg_Search_Card);
			
			if(card_search())
			{
				card_output_msg |= CARD_IN;
			}
			else
			{
				ClearCardMsg(CARD_IN);
			}
		}
	}
}


void CardDataProcessorApp()
{
    u8 i = 0;
    u8 j = 0;
//    u32 temp = 0;
//    s32 tmpdata;
    u8 card_key_result[4] = {0};
//    card_read_sucess = FALSE;

    if(CheckCardMsg() & CARD_IN)
    {
        ClearCardMsg(CARD_IN);
        card_enable();

        for(j = 0; j < 6; j++)
        {
            memset(card_source_data.data, 0, 28);
            Card5557ClrIntCnt();

            if(card_read_all(card_source_data.data))
            {
                nop();

                for(i = 0; i < 28; i++)
                {
                    card_source_data.data [i] = (u8)( card_source_data.data [i] ^ i ^ 0xB9); // Card_Data. data为接收到的28字节
                }

                card_generate_key(card_source_data.data, card_key_result);

                if(0 == memcmp(card_source_data.code, card_key_result, 4))
                {
                    card_read_sucess = TRUE;
                    break;

                }
                else
                {
                    card_read_sucess = FALSE;
                }


            }
            else
            {
				ClearCardSta();
                card_read_sucess = FALSE;

            }
        }

        card_disable();

        if(card_read_sucess == FALSE)
        {
////            LcdAppDpUserCard2(51);									//密码错误
        }
        else
        {
            card_read_sucess = FALSE;
			KeySetLcdMsgApp(MENU_TWO);
            if(card_source_data.check == (add_check(card_source_data.data, 15) + add_check(&card_source_data.data[16], 8)) % 256)
            {
                switch(card_source_data.card_flag)
                {
                    case OPEN_VALVE_CARD:
                    {
                        card_key_flag = TRUE;
                        FrameSetMsgApp(Msg_ValveOpenEmergency);//应急开阀消息
                    }
                    break;

                    case CLOSE_VALVE_CARD:
                    {
                    }
                    break;
                }
            }
        }
    }
}
/*************************************************
Function: u8 ChecPulsekMsg(void)
Description: 检测当前事件Msg
Input:  None
Return:
Others: 处于MainSpace，第二类接口：工作接口。
 *************************************************/
u8 CheckCardMsg(void)
{
    return card_output_msg;
}

/*************************************************
Function: void ClearCardMsg(u8 msg)
Description: 清MSG
Input:
Return: None
Others:	处于MainSpace，第二类接口：工作接口。
 *************************************************/
void ClearCardMsg(u8 msg)
{
    card_output_msg = card_output_msg & (~msg);
}

const u8  const_data[24] = { 0xDF, 0x39, 0x9D, 0xC5, 0x40, 0x86, 0x18, 0xB3, 0x67, 0xF1, 0x5A, 0x72, 0x24, 0xEC, 0xAE, 0x0B
                             , 0xA3, 0XA5, 0X09, 0XC3, 0XAD, 0XEE, 0XAC, 0X08
                           };
void card_generate_key(u8* buf, u8* key)
{
    u8  p[24]; //存放临时数据
    u8 p_num = 0, d_num = 0;
    p[p_num++] = const_data[(buf[0x0A] ^ const_data[d_num++]) % 24];
    p[p_num++] = const_data[(buf[0x04] ^ const_data[d_num++]) % 24];
    p[p_num++] = const_data[(buf[0x06] ^ const_data[d_num++]) % 24];
    p[p_num++] = const_data[(buf[0x01] ^ const_data[d_num++]) % 24];
    p[p_num++] = const_data[(buf[0x09] ^ const_data[d_num++]) % 24];
    p[p_num++] = const_data[(buf[0x05] ^ const_data[d_num++]) % 24];
    p[p_num++] = const_data[(buf[0x02] ^ const_data[d_num++]) % 24];
    p[p_num++] = const_data[(buf[0x07] ^ const_data[d_num++]) % 24];
    p[p_num++] = const_data[(buf[0x08] ^ const_data[d_num++]) % 24];
    p[p_num++] = const_data[(buf[0x03] ^ const_data[d_num++]) % 24];

    p[p_num++] = const_data[(buf[0x0C] ^ const_data[d_num++]) % 24];
    p[p_num++] = const_data[(buf[0x10] ^ const_data[d_num++]) % 24];
    p[p_num++] = const_data[(buf[0x0E] ^ const_data[d_num++]) % 24];
    p[p_num++] = const_data[(buf[0x12] ^ const_data[d_num++]) % 24];
    p[p_num++] = const_data[(buf[0x14] ^ const_data[d_num++]) % 24];
    p[p_num++] = const_data[(buf[0x0B] ^ const_data[d_num++]) % 24];
    p[p_num++] = const_data[(buf[0x13] ^ const_data[d_num++]) % 24];
    p[p_num++] = const_data[(buf[0x0F] ^ const_data[d_num++]) % 24];
    p[p_num++] = const_data[(buf[0x11] ^ const_data[d_num++]) % 24];
    p[p_num++] = const_data[(buf[0x0D] ^ const_data[d_num++]) % 24];

    p[p_num++] = const_data[(buf[0x00] ^ const_data[d_num++]) % 24];
    p[p_num++] = const_data[(buf[0x16] ^ const_data[d_num++]) % 24];
    p[p_num++] = const_data[(buf[0x15] ^ const_data[d_num++]) % 24];
    p[p_num++] = const_data[(buf[0x17] ^ const_data[d_num++]) % 24];
    memset(key, 0, 4);

    for(int i = 0; i < 24; i++)
    {
        if(i % 2 == 0) // 0/2/4/6/8/10/12/14/16/18/20/22/
        {
            *key = (u8)(key[0] + p[i]);
        }

        if(i % 2 == 1) // 1/3/5/7/9/11/13/15/17/19/21/23/
        {
            *(key + 1) = (u8)(key[1] + p[i]);
        }

        if(i % 3 == 0) // 0/3/6/9/12/15/18/21/
        {
            *(key + 2) = (u8)(key[2] + p[i]);
        }

        if(i % 3 == 1) // 1/4/7/10/13/16/19/22
        {
            *(key + 3) = (u8)(key[3] + p[i]);
        }
    }
}
void CardKeyMsgMachine()
{
    switch (card_key_machine_state)
    {
        case CARD_KEY_M_S0:
        {
            if(card_key_flag && (card_key_check_num == 0))
            {
                card_key_flag = FALSE;
                card_key_check_num++;
                RtccSetTimer(RTCC_CARD_KEY_TIME, CARD_KEY_TIME_SECOND); //检测时间30S
            }

            if(RtccCheckTimer(RTCC_CARD_KEY_TIME) > 0)
            {
                if(card_key_flag)
                {
					card_key_flag = FALSE;
                    card_key_check_num++;
                }

                if(card_key_check_num >= 2)
                {
                    //抛上报消息
					card_key_check_num = 0;
					KeySetLcdMsgApp(MENU_ACTIVE_RF);
                }
            }
            else
            {
                card_key_flag = FALSE;
                card_key_check_num = 0;
            }
        }
        break;

        default:
        {
			card_key_flag = FALSE;
            card_key_check_num = 0;
            card_key_machine_state = CARD_KEY_M_S0;
        }
        break;
    }

}
void CardMachine()
{
    SearchCardApp();
    CardDataProcessorApp();
    CardKeyMsgMachine();
}

#endif

