/****************************************************************
// source file name		: PL_make_file.cc
// date of preperation	: 2016.03.09
// date of last update	: 2016.03.09
// developer			: mskim
// description 			: PL 파일을 해석하여, 
						 통신서버로 보낼 파일 생성 
****************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <sys/file.h>
#include <math.h>
#include "crd_def.h"
#include "util.h"
#include "dir_path.h"

// main.cc
extern	char 	gacHomePath[128];

// PL_file.cc
extern	PL_FILE_CHECK_LIST	gastPlFileCheckList[];

// local
int make_pl_file_WBSF(char *pcFileName);
int get_aliasno(char *pcFileNAme, int iNo);
int get_ppd_bit(char cData, char *pcUse, char *pcStudent);
int get_chg_bit(char cData, char *pcUse);

char gacTempFileName[8];

int term_PL(char* pcData, PL_RECORD* pstPlRecord)
{
	if(pcData != 0x00){
		proc_log("[mem chk] term_PL pcData free");
		free(pcData);
	}
	if(pstPlRecord != 0x00){
		proc_log("[mem chk] term_PL pstPlRecord free");
		free(pstPlRecord);
	}
	pcData = 0x00;
	pstPlRecord = 0x00;

	return 0;
}

/******************************************************************************
 * make_pl_file
 * parameter
 * Return
 ******************************************************************************/
int make_pl_file(char *pcFileName)
{
	char			*pcData = 0x00;
	char			acSrcFileFullPath[512];
	char			acDstFileFullPath[512];
	char			acMakeFile[512];
	PL_RECORD		*pstPlRecord = 0x00;
	int				iTotRecordCnt = 0;
	int				iOffSet = 0;
	int				iTotAliasCnt = 0;
	long lFileSize, lReadSize;
	int		iAliasNo = 0;
	int		iAliasNoTmp = 1;
	int		iAliasNoTmpSub = 1;
	int		iAliasSubFlag = 0;
	int		iWXSF_flag = 0;
	int		iWBSF_flag = 0;
	char	acChgUse[8];
	char	acPpdUse[4];
	char	acPpdStudent[4];
	int i, a, q;
	char	acChgUsrD[5000];
	int		iChgUsrOffSet = 0;
	char acCmd[256];

	memset(acSrcFileFullPath, 0x00, sizeof(acSrcFileFullPath));
	memset(acDstFileFullPath, 0x00, sizeof(acDstFileFullPath));
	memset(acMakeFile, 0x00, sizeof(acMakeFile));

	sprintf(acSrcFileFullPath, 	"%s/FILE/RECV/%.8s", DIR_TCS, pcFileName);
	sprintf(acDstFileFullPath, 	"%s/FILE/RECV/SAVE/%.8s", DIR_TCS, pcFileName);
	sprintf(acMakeFile, 		"%s/%s/PLTPCARDMST_S", DIR_TCS, DIR_BASEINFO);

	lFileSize = Get_File_Size_Char(acSrcFileFullPath);
	if(lFileSize > 0)
	{
		proc_log("-----------------------------------------------------------");
		proc_log("PL FILE READ START [name:%.8s, size:%d byte]", pcFileName, lFileSize);

		pcData = (char*)malloc(lFileSize+1);
		memset(pcData, 0x00, lFileSize+1);
		proc_log("[mem chk] 1-1 pcData malloc");

		lReadSize = Read_File_Char(pcData, acSrcFileFullPath);

		if(lReadSize < 512)
		{
			proc_log("File Size Error ... [size:%d]", lReadSize);
			proc_log("[mem chk] go term_PL");
			term_PL(pcData, pstPlRecord);
			return -1;
		}

		iAliasNo = 0;
		iAliasNoTmp = 1;
		iAliasNoTmpSub = 1;
		iAliasSubFlag = 0;
		iWXSF_flag = 0;
		iWBSF_flag = 0;

		memset(acChgUse, 		0x00, 	sizeof(acChgUse));
		memset(acPpdUse, 		0x00, 	sizeof(acPpdUse));
		memset(acPpdStudent, 	0x00, 	sizeof(acPpdStudent));

		iTotRecordCnt = (int)ceil((double)lFileSize/sizeof(PL_RECORD));
		pstPlRecord = (PL_RECORD *)malloc(sizeof(PL_RECORD)*iTotRecordCnt);
		proc_log("[mem chk] 1-2 pstPlRecord malloc");

		proc_log("Record Cnt [%d]", iTotRecordCnt);

		if(strncmp(pcFileName, "WXSF", 4) == 0)
		{
			iWXSF_flag = 1;
			memcpy(gacTempFileName, pcFileName, 8);
		}

		if(strncmp(pcFileName, "WBSF", 4) == 0)
		{
			iWBSF_flag = 1;
		}

		for(i=0; i<iTotRecordCnt; i++)
		{
			if(lFileSize >= sizeof(pstPlRecord[i].acData))
			{
				memcpy(pstPlRecord[i].acData, pcData+iOffSet, sizeof(pstPlRecord[i].acData));
				iOffSet += sizeof(pstPlRecord[i].acData);
				lFileSize -= sizeof(pstPlRecord[i].acData);

				iChgUsrOffSet = 0;
				memset(acChgUsrD, 0x00, sizeof(acChgUsrD));

				for(a=0; a<510; a++)
				{
					// 후불 PL 파일 (1byte에 8개 정보)
					if( strncmp(pcFileName, "WSSF", 4) == 0 ||
						strncmp(pcFileName, "WZSF", 4) == 0 ||
						strncmp(pcFileName, "VWSF", 4) == 0 ||
						strncmp(pcFileName, "WASF", 4) == 0 ||
						strncmp(pcFileName, "NTSF", 4) == 0 ||
						strncmp(pcFileName, "BHSF", 4) == 0 ||
						
						// ADD, 2017.04.26, mskim - Alias Add
						strncmp(pcFileName, "EPSF", 4) == 0 ||
						strncmp(pcFileName, "EQSF", 4) == 0 ||
						// ADD, 2019.05.01, mskim - Alias Add
						strncmp(pcFileName, "V3SF", 4) == 0 ||
						strncmp(pcFileName, "V4SF", 4) == 0 )
						// END
					{
						memset(acChgUse, 0x00, sizeof(acChgUse));
						get_chg_bit(pstPlRecord[i].acData[a], acChgUse);

						for(q=0; q<sizeof(acChgUse); q++)
						{
							iAliasNo = get_aliasno(pcFileName, iAliasNoTmp);
							iAliasNoTmp++;

							if(iAliasNo <= 0)
							{
								break;
							}
							
							// CARD TEST
							if (iAliasNo == 613999993)
							{
								proc_log("alias_no:'%d', use:'%c'", iAliasNo, acChgUse[q]);
								//acChgUse[q] = 0x31;
								//proc_log("alias_no:'%d', use:'%c'", iAliasNo, acChgUse[q]);
							}

							if (iAliasNo == 613999994)
							{
								proc_log("alias_no:'%d', use:'%c'", iAliasNo, acChgUse[q]);
								//acChgUse[q] = 0x31;
								//proc_log("alias_no:'%d', use:'%c'", iAliasNo, acChgUse[q]);
							}

							acChgUsrD[iChgUsrOffSet++] = acChgUse[q];
							iTotAliasCnt++;
						}
					}
					// 선불 PL 파일 (1bity에 4개 alias 정보)
					else 
					{
						memset(acPpdUse, 		0x00, 	sizeof(acPpdUse));
						memset(acPpdStudent, 	0x00, 	sizeof(acPpdStudent));
						
						get_ppd_bit(pstPlRecord[i].acData[a], acPpdUse, acPpdStudent);
						
						for (q=0; q<sizeof(acPpdUse); q++)
						{
							if(strncmp(pcFileName, "WXSF", 4)==0)
							{
								if(iAliasSubFlag == 0)
								{
									iAliasNo = WXSF_SUB1_START_NO_GET(iAliasNoTmp);
									if(iAliasNo > WXSF_SUB1_END_NO){
										iAliasSubFlag = 1;
									}else{
										iAliasNoTmp++;
									}
								}
								if(iAliasSubFlag == 1)
								{
									iAliasNo = WXSF_SUB2_START_NO_GET(iAliasNoTmpSub);
									iAliasNoTmpSub++;

									if(iAliasNo > WXSF_SUB2_END_NO){
										iAliasNo = -1;
									}
								}
							} // if(strncmp(pcFileName, "WXSF", 4)==0)
							else if (strncmp(pcFileName, "WBSF", 4)==0)
							{
								if(iAliasSubFlag == 0)
								{
									iAliasNo = WBSF_SUB1_START_NO_GET(iAliasNoTmp);
									if(iAliasNo > WBSF_SUB1_END_NO){
										iAliasSubFlag = 1;
									}else{
										iAliasNoTmp++;
									}
								}
								if(iAliasSubFlag == 1)
								{
									iAliasNo = WBSF_SUB2_START_NO_GET(iAliasNoTmpSub);
									iAliasNoTmpSub++;

									if(iAliasNo > WBSF_SUB2_END_NO){
										iAliasNo = -1;
									}
								}
							} // else if (strncmp(file_name, "WBSF", 4)==0)
							else
							{
								iAliasNo = get_aliasno(pcFileName, iAliasNoTmp);
								iAliasNoTmp++;
							}
							
							if(iAliasNo <= 0)
							{
						 		break;
							}
							
							// CARD TEST
							if (iAliasNo == 276513657 || iAliasNo == 276513656 || iAliasNo == 276513655 || iAliasNo == 276512068
							||	iAliasNo == 276512045 || iAliasNo == 276512046 || iAliasNo == 276512141 || iAliasNo == 276512174)
							{
								proc_log("before alias_no:'%d', use:'%c', student:'%c'", iAliasNo, acPpdUse[q], acPpdStudent[q]);
								acPpdUse[q] = 0x31;
								acPpdStudent[q] = 0x31;
								proc_log("after  alias_no:'%d', use:'%c', student:'%c'", iAliasNo, acPpdUse[q], acPpdStudent[q]);
							}

							//acChgUsrD[iChgUsrOffSet++] = acPpdUse[q];
							acChgUsrD[iChgUsrOffSet++] = ((acPpdUse[q] == 0x30 && acPpdStudent[q] == 0x30) ? 0x30 : 0x31);

							iTotAliasCnt++;
							
							if(iWXSF_flag == 1)
							{
								if(iAliasNo == WXSF_SUB1_END_NO){
									if(Add_Write_File_Char(acChgUsrD, iChgUsrOffSet, acMakeFile) != 0){
										proc_log("File Write Error.. [%s]", acMakeFile);
										return -1;
									}

									proc_log("[mem chk] go term_PL");
									term_PL(pcData, pstPlRecord);
									return iTotAliasCnt;
								}
							} // if(WXSF_flag == 1)
							
							if(iWBSF_flag == 1)
							{
								if(iAliasNo == WBSF_SUB1_END_NO){
									if(Add_Write_File_Char(acChgUsrD, iChgUsrOffSet, acMakeFile) != 0){
										proc_log("File Write Error.. [%s]", acMakeFile);
										proc_log("[mem chk] return");
										return -1;
									}

									memset(acChgUsrD, 0x00, sizeof(acChgUsrD));
									iChgUsrOffSet = 0;

									iTotAliasCnt += make_pl_file_WBSF(gacTempFileName);
								}
							} // if(WBSF_flag == 1)
							
						} // 	for (int q=0; q<sizeof(acPpdUse); q++)
					} // else
				} // for(int a=0; a<510; a++)
				
				if(iChgUsrOffSet > 0){
					if(Add_Write_File_Char(acChgUsrD, iChgUsrOffSet, acMakeFile) != 0){
						proc_log("File Write Error.. [%s]", acMakeFile);
						proc_log("[mem chk] return");
						return -1;
					}

					memset(acChgUsrD, 0x00, sizeof(acChgUsrD));
					iChgUsrOffSet = 0;
				}
				
			} // if(lFileSize >= sizeof(pstPlRecord[i].acData))
			else 
			{
				proc_log("Data in file struct Error.. [%d/%d]", i, iTotRecordCnt);
				proc_log("[mem chk] go term_PL");
				term_PL(pcData, pstPlRecord);
				return -1;
			}
		} // for(int i=0; i<iTotRecordCnt; i++)
	}
	else
	{ 
		proc_log("[mem chk] return");
		return -1;
	}

	proc_log("PL FILE READ END [%d]", iTotAliasCnt);
	
	// to do... DB 처리

	// end
	
	// 처리 완료된 파일은 옮긴다

	memset(acCmd, 0x00, sizeof(acCmd));
		
	sprintf(acCmd, "mv -f %s %s", acSrcFileFullPath, acDstFileFullPath);
	system(acCmd);
		
	proc_log("FILE MOVE [%s]", acCmd);

	proc_log("[mem chk] go term_PL");
	term_PL(pcData, pstPlRecord);

	return iTotAliasCnt;
}

/******************************************************************************
 * make_pl_file_WBSF
 * parameter
 * Return
 ******************************************************************************/
int make_pl_file_WBSF(char *pcFileName)
{
	char				*pcData = 0x00;
	char				acSrcFileFullPath[512];
	char				acDstFileFullPath[512];
	char				acMakeFile[512];
	PL_RECORD		*pstPlRecord = 0x00;
	int					iTotRecordCnt = 0;
	int					iOffSet = 0; 
	int					iTotAliasCnt = 0;
	long lFileSize, lReadSize;
	int	 	iAliasNo = 0;
	int		iAliasNoTmp = 1;
	int	 	iAliasNoTmpSub = 1;
	int  	iAliasSubFlag = 0;
	char 	acChgUse[8];
	char	acPpdUse[4];
	char	acPpdStudent[4];
	int i, a, q;
	char acChgUsrD[5000];
	int  iChgUsrOffSet = 0;

	memset(acSrcFileFullPath, 0x00, sizeof(acSrcFileFullPath));
	memset(acDstFileFullPath, 0x00, sizeof(acDstFileFullPath));
	memset(acMakeFile, 0x00, sizeof(acMakeFile));
	
	sprintf(acSrcFileFullPath, 	"%s/FILE/RECV/%.8s", DIR_TCS, pcFileName);
	sprintf(acDstFileFullPath, 	"%s/FILE/RECV/SAVE/%.8s", DIR_TCS, pcFileName);
	sprintf(acMakeFile, 		"%s/%s/PLTPCARDMST_S", DIR_TCS, DIR_BASEINFO);

	lFileSize = Get_File_Size_Char(acSrcFileFullPath);
	
	if(lFileSize > 0)
	{
		proc_log("WXSF file continuous use...");
		proc_log("PL FILE READ START [name:%.8s, size:%d byte]", pcFileName, lFileSize);

		pcData = (char *) malloc(lFileSize+1);
		memset(pcData, 0x00, lFileSize+1);
		proc_log("[mem chk] 1-3 pcData malloc");

		lReadSize = Read_File_Char(pcData, acSrcFileFullPath);
		
		if(lReadSize < 512){
			proc_log("File Size Error ... [size:%d]", lReadSize);
			proc_log("[mem chk] go term_PL");
			term_PL(pcData, pstPlRecord);
			return 0;
		}
		
		iAliasNo = 0;
		iAliasNoTmp = 1;
		iAliasNoTmpSub = 1;
		iAliasSubFlag = 0;
		
		memset(acChgUse, 0x00, sizeof(acChgUse));
		memset(acPpdUse, 0x00, sizeof(acPpdUse));
		memset(acPpdStudent, 0x00, sizeof(acPpdStudent));
		
		iTotRecordCnt = (int)ceil((double)lFileSize/sizeof(PL_RECORD));
		pstPlRecord = (PL_RECORD *)malloc(sizeof(PL_RECORD)*iTotRecordCnt);
		proc_log("[mem chk] 1-4 pstPlRecord malloc");
		
		proc_log("Record Cnt [%d]", iTotRecordCnt);
		
		for (i=0; i<iTotRecordCnt; i++)
		{
			if (lFileSize >= sizeof(pstPlRecord[i].acData))
			{
				memcpy(pstPlRecord[i].acData, pcData+iOffSet, sizeof(pstPlRecord[i].acData));
				iOffSet += sizeof(pstPlRecord[i].acData);
				lFileSize -= sizeof(pstPlRecord[i].acData);

				iChgUsrOffSet = 0;
				memset(acChgUsrD, 0x00, sizeof(acChgUsrD));
				
				for (a=0; a<510; a++)
				{
					memset(acPpdUse, 0x00, sizeof(acPpdUse));
					memset(acPpdStudent, 0x00, sizeof(acPpdStudent));
					
					get_ppd_bit(pstPlRecord[i].acData[a], acPpdUse, acPpdStudent);
					
					for (q=0; q<sizeof(acPpdUse); q++)
					{
						if(iAliasSubFlag == 0)
						{
							iAliasNo = WXSF_SUB1_START_NO_GET(iAliasNoTmp);
							if(iAliasNo > WXSF_SUB1_END_NO)
							{
								iAliasSubFlag = 1;
							}
							else
							{
								iAliasNoTmp++;
							}
						}
						if(iAliasSubFlag == 1)
						{
							iAliasNo = WXSF_SUB2_START_NO_GET(iAliasNoTmpSub);
							iAliasNoTmpSub++;

							if(iAliasNo > WXSF_SUB2_END_NO)
							{
								iAliasNo = -1;
							}
						}
						
						if(iAliasNo <= 0)
						{
							break;
						}
						
						if (iAliasNo >= 170000001)
						{
							
							// CARD TEST
							if (iAliasNo == 170910240)
							{
								proc_log("before alias_no:'%d', use:'%c', student:'%c'", iAliasNo, acPpdUse[q], acPpdStudent[q]);
								acPpdUse[q] = 0x31;
								acPpdStudent[q] = 0x31;
								proc_log("after  alias_no:'%d', use:'%c', student:'%c'", iAliasNo, acPpdUse[q], acPpdStudent[q]);
							}

							//acChgUsrD[iChgUsrOffSet++] = acPpdUse[q];
							acChgUsrD[iChgUsrOffSet++] = ((acPpdUse[q] == 0x30 && acPpdStudent[q] == 0x30) ? 0x30 : 0x31);

							iTotAliasCnt++;
						}
						
					} // for (int q=0; q<sizeof(ppd_use); q++)
					
				} // for (int a=0; a<510; a++)
				
				if (iChgUsrOffSet > 0)
				{
					if(Add_Write_File_Char(acChgUsrD, iChgUsrOffSet, acMakeFile) != 0)
					{
						proc_log("File Write Error.. [%s]", acMakeFile);
						proc_log("[mem chk] return");
						return 0;
					}
				}

				memset(acChgUsrD, 0x00, sizeof(acChgUsrD));
				iChgUsrOffSet = 0;
				
			} // if (file_size >= sizeof(record_data[i].data))
			else
			{
				proc_log("Data in file struct Error.. [%d/%d]", i, iTotRecordCnt);
				proc_log("[mem chk] go term_PL");
				term_PL(pcData, pstPlRecord);
				return 0;
			}
			
		} // for (int i=0; i<tot_record_cnt; i++)
		
	} // if(lFileSize > 0)
	else
	{
		proc_log("[mem chk] return");
		return 0;
	}
	
	proc_log("WXSF File Continuous Use END..");
	
	// 처리 완료된 파일은 옮긴다
	char acCmd[256];
	memset(acCmd, 0x00, sizeof(acCmd));
		
	sprintf(acCmd, "mv -f %s %s", acSrcFileFullPath, acDstFileFullPath);
	system(acCmd);
		
	proc_log("FILE MOVE [%s]", acCmd);
		
	proc_log("[mem chk] go term_PL");
	term_PL(pcData, pstPlRecord);

	return iTotAliasCnt;
}

/******************************************************************************
 * get_aliasno
 * parameter
 * Return
 ******************************************************************************/
int get_aliasno(char *pcFileName, int iNo)
{
	int	iAliasNo = 0;
	int iCnt = 0;
	enum FILE_ID iFileId = UNKNOWN;
	
	while(1)
	{
		if(gastPlFileCheckList[iCnt].pcFileName == (char*)0x00)
		{
			break;
		}
		
		if(strncmp(gastPlFileCheckList[iCnt].pcFileName, pcFileName, 4) == 0)
		{
			iFileId = gastPlFileCheckList[iCnt].iId;
			break;
		}
		
		iCnt++;
	}
	
	switch(iFileId)
	{
		case WSSF:
			iAliasNo = WSSF_START_NO_GET(iNo);
			if(iAliasNo > WSSF_END_NO)	iAliasNo = -1;	
			break;

		case WTSF:
			iAliasNo = WTSF_START_NO_GET(iNo);
			if(iAliasNo > WTSF_END_NO)	iAliasNo = -1;	
			break;
			
		case WYSF:
			iAliasNo = WYSF_START_NO_GET(iNo);
			if(iAliasNo > WYSF_END_NO)	iAliasNo = -1;	
			break;
			
		case WZSF:
			iAliasNo = WZSF_START_NO_GET(iNo);
			if(iAliasNo > WZSF_END_NO)	iAliasNo = -1;	
			break;
			
		case VWSF:
			iAliasNo = VWSF_START_NO_GET(iNo);
			if(iAliasNo > VWSF_END_NO)	iAliasNo = -1;	
			break;
			
		case VXSF:
			iAliasNo = VXSF_START_NO_GET(iNo);
			if(iAliasNo > VXSF_END_NO)	iAliasNo = -1;	
			break;
			
		case WASF:
			iAliasNo = WASF_START_NO_GET(iNo);
			if(iAliasNo > WASF_END_NO)	iAliasNo = -1;	
			break;
			
		case NVSF:
			iAliasNo = NVSF_START_NO_GET(iNo);
			if(iAliasNo > NVSF_END_NO)	iAliasNo = -1;	
			break;
			
		case NUSF:
			iAliasNo = NUSF_START_NO_GET(iNo);
			if(iAliasNo > NUSF_END_NO)	iAliasNo = -1;	
			break;
			
		case NTSF:
			iAliasNo = NTSF_START_NO_GET(iNo);
			if(iAliasNo > NTSF_END_NO)	iAliasNo = -1;	
			break;
			
		case BHSF:
			iAliasNo = BHSF_START_NO_GET(iNo);
			if(iAliasNo > BHSF_END_NO)	iAliasNo = -1;	
			break;
			
		case NSSF:
			iAliasNo = NSSF_START_NO_GET(iNo);
			if(iAliasNo > NSSF_END_NO)	iAliasNo = -1;	
			break;
			
		case NRSF:
			iAliasNo = NRSF_START_NO_GET(iNo);
			if(iAliasNo > NRSF_END_NO)	iAliasNo = -1;	
			break;
			
		case NQSF:
			iAliasNo = NQSF_START_NO_GET(iNo);
			if(iAliasNo > NQSF_END_NO)	iAliasNo = -1;	
			break;
		
		case HLSF:
			iAliasNo = HLSF_START_NO_GET(iNo);
			if(iAliasNo > HLSF_END_NO)	iAliasNo = -1;	
			break;
		
		case NPSF:
			iAliasNo = NPSF_START_NO_GET(iNo);
			if(iAliasNo > NPSF_END_NO)	iAliasNo = -1;	
			break;
			
		case NOSF:
			iAliasNo = NOSF_START_NO_GET(iNo);
			if(iAliasNo > NOSF_END_NO)	iAliasNo = -1;	
			break;

		case EPSF:
			iAliasNo = EPSF_START_NO_GET(iNo);
			if(iAliasNo > EPSF_END_NO)	iAliasNo = -1;	
			break;
			
		case EQSF:
			iAliasNo = EQSF_START_NO_GET(iNo);
			if(iAliasNo > EQSF_END_NO)	iAliasNo = -1;	
			break;

		case V3SF:
			iAliasNo = V3SF_START_NO_GET(iNo);
			if(iAliasNo > V3SF_END_NO)	iAliasNo = -1;	
			break;

		case V4SF:
			iAliasNo = V4SF_START_NO_GET(iNo);
			if(iAliasNo > V4SF_END_NO)	iAliasNo = -1;	
			break;
		case V5SF:
			iAliasNo = V5SF_START_NO_GET(iNo);
			if(iAliasNo > V5SF_END_NO)	iAliasNo = -1;	
			break;
		case V6SF:
			iAliasNo = V6SF_START_NO_GET(iNo);
			if(iAliasNo > V6SF_END_NO)	iAliasNo = -1;	
			break;
		case V7SF:
			iAliasNo = V7SF_START_NO_GET(iNo);
			if(iAliasNo > V7SF_END_NO)	iAliasNo = -1;	
			break;

		default:
			break;
	}
	
	return iAliasNo;
}

/******************************************************************************
 * get_ppd_bit
 * parameter
 * Return
 ******************************************************************************/
int get_ppd_bit(char cData, char *pcUse, char *pcStudent)
{
	char acUseTmp[4], acStudentTmp[4];
	memset(acUseTmp, 0x30, sizeof(acUseTmp));
	memset(acStudentTmp, 0x30, sizeof(acStudentTmp));

	if((cData & 0x80) == 0x80) acUseTmp		[0]	= 0x31;
	if((cData & 0x40) == 0x40) acStudentTmp	[0]	= 0x31;
	if((cData & 0x20) == 0x20) acUseTmp		[1]	= 0x31;
	if((cData & 0x10) == 0x10) acStudentTmp	[1]	= 0x31;
	if((cData & 0x08) == 0x08) acUseTmp		[2]	= 0x31;
	if((cData & 0x04) == 0x04) acStudentTmp	[2]	= 0x31;
	if((cData & 0x02) == 0x02) acUseTmp		[3]	= 0x31;
	if((cData & 0x01) == 0x01) acStudentTmp	[3]	= 0x31;

	memcpy(pcUse, acUseTmp, sizeof(acUseTmp));
	memcpy(pcStudent, acStudentTmp, sizeof(acStudentTmp));

	return 0;
}

/******************************************************************************
 * get_chg_bit
 * parameter
 * Return
 ******************************************************************************/
int get_chg_bit(char cData, char *pcUse)
{
	char acUseTmp[8];
	memset(acUseTmp, 0x30, sizeof(acUseTmp));

	if((cData & 0x80) == 0x80) acUseTmp[0] = 0x31;
	if((cData & 0x40) == 0x40) acUseTmp[1] = 0x31;
	if((cData & 0x20) == 0x20) acUseTmp[2] = 0x31;
	if((cData & 0x10) == 0x10) acUseTmp[3] = 0x31;
	if((cData & 0x08) == 0x08) acUseTmp[4] = 0x31;
	if((cData & 0x04) == 0x04) acUseTmp[5] = 0x31;
	if((cData & 0x02) == 0x02) acUseTmp[6] = 0x31;
	if((cData & 0x01) == 0x01) acUseTmp[7] = 0x31;

	memcpy(pcUse, acUseTmp, sizeof(acUseTmp));

	return 0;
}
