#include "./lib/IdCardLib.h"
#include <string.h>
#include <stdio.h>
#include "stdlib.h"
#include <dlfcn.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "pthread.h"

static void EsIDCardCB(int notify,void *userdata)
{
  if(notify == ESIDCARD_NOTIFY_FIND)
	{
		printf("EsIdcardCB: %s\r\n",(char *)userdata);
	}
	if(notify == ESIDCARD_NOTIFY_HEARTBEAT)
	{
		printf("EsIdcardCB: %s\r\n",(char *)userdata);
	}
	if(notify == ESIDCARD_NOTIFY_LOG)
	{
		printf("EsIdcardCB: %s\r\n",(char *)userdata);
	}
}

static void* Readcard_thread(void *arg)
{
	int res;
	IDInfo idinfo;
	while (1)
	{
		memset(&idinfo,0,sizeof(IDInfo));
		res=ReadIDCardLib(&idinfo,Tpye_USB);

		if(res== STA_OK)//身份证
		{
			printf("Usage:");
			printf("\tname:%s\n", idinfo.name);
			printf("\tcardno:%s\n", idinfo.cardId);
			printf("\taddress:%s\n", idinfo.address);
			printf("\tnation:%s\n",idinfo.nation);
			if(1==atoi((char *)idinfo.sex) ){
				printf("\tsex:男\n");
			}
			else printf("\tsex:女\n");
			printf("\tbirdth :%s\n",idinfo.birthDay);
			printf("\tethnicity:%s\n",idinfo.grantDept);
			printf("\tstarttime:%s\n",idinfo.begainTime);
			printf("\tendtime:%s\n",idinfo.endTime);
			char PersonalName[120];
			sprintf(PersonalName,"/mnt/%s.jpg",idinfo.cardId);
			printf("\t%s\n",PersonalName);
			SaveImage(idinfo.img, PersonalName);//本地解码   "/home/photo.bmp"
		}
		else if(res== STA_IC_OK)//IC卡
		{
			printf("ICUsage:\n");
			printf("Size :%d\r\n",idinfo.icinfo.ICbufflen);
			for(int i=0;i<idinfo.icinfo.ICbufflen;i++)
				printf("%02x ",idinfo.icinfo.ICbuff[i]);
			printf("\r\n");
		}
		sleep(1);
	}
	pthread_exit(0) ;
	return 0;
}
int main()
{	

	SetServiceInfo("153.99.44.234",9977);
	CARD_SetReadInfoCB(EsIDCardCB);//回调函数
	pthread_t pthid;
	pthread_create(&pthid, NULL, Readcard_thread,NULL);
  	pthread_detach(pthid);//主线程与子线程分离
	while (1)
	{
		printf("...\r\n");
		sleep(1);
	}
	
	return  0;
}

