/*****************************************************************************************
*  Copyright (c) 2004, Beijing Senselock Data Security Centre
*  All rights reserved.
*
*  Filename£ºsample_2.c
*          
*  Briefs£º  The purpose of this snippet is to demonstrate how to invoke the written-in  
*            SenseLock EL executables
*            
*  History:        
*  --------------------------------------------------------        
*            11/08/2004 v1.0.0.0 Lihua created
*            
*            
*******************************************************************************************/


#include "sense4.h"
#include "malloc.h"
#include "stdio.h"
#include "conio.h"
int main(int argc, char* argv[])
{
	PSENSE4_CONTEXT pstS4CtxList = NULL;
	DWORD dwCtxListSize = 0;

	BYTE lpDevPin[24] = "123456781234567812345678";
	
	DWORD  btDeviceNumber = 0;
	DWORD dwResult = 0;

	BYTE lpOutBuf[8] = {0x00};
	DWORD dwBytesReturned = 0;
	DWORD i = 0;

	dwResult = S4Enum(NULL, &dwCtxListSize);
	if (dwResult != S4_SUCCESS && dwResult != S4_INSUFFICIENT_BUFFER)
	{
		printf("Enumerate SenseLock EL failed! <error code: 0x%08x>\n", dwResult);
		return 0;
	}
	if (0 == dwCtxListSize)
	{
		printf("No SenseLock EL device present!\n");
		return 1;
	}

	pstS4CtxList = (PSENSE4_CONTEXT)malloc(dwCtxListSize);
	if (0 == dwCtxListSize)
	{
		printf("No SenseLock EL device present!\n");
		return 1;
	}

	dwResult = S4Enum(pstS4CtxList, &dwCtxListSize);
	if (S4_SUCCESS != dwResult) 
	{
		printf("S4Enum failed with error code:%d\n",dwResult);
		free(pstS4CtxList);
		pstS4CtxList = NULL;
		return 1;
	}

	btDeviceNumber = dwCtxListSize/sizeof(SENSE4_CONTEXT);
	for(i=0; i<btDeviceNumber; i++)
	{
		/* an 8 octets customer ID */
		BYTE lpCustomerID[8] = "Sense";
		lpCustomerID[8] = (BYTE)(0x00+i);

		dwResult = S4Open(pstS4CtxList+i);
		if (S4_SUCCESS != dwResult) 
		{
			printf("S4Open for device No. %d failed! <error code: 0x%08x\n",i,dwResult);
			continue;
		}

		dwResult = S4ChangeDir(pstS4CtxList+i, "\\");
		if (dwResult != S4_SUCCESS) 
		{
			printf("Change directory failed! <error code: 0x%08x>\n",dwResult);
			S4Close(pstS4CtxList+i);
			continue;
		}

		dwResult = S4VerifyPin(pstS4CtxList+i, lpDevPin, 24, S4_DEV_PIN);
		if (S4_SUCCESS != dwResult) 
		{
			printf("Verify Pin for device No. %d failed! <error code: 0x%08x\n",i,dwResult);
			continue;
		}			
		dwResult = S4Control(pstS4CtxList+i, S4_SET_DEVICE_ID, lpCustomerID, 8, NULL, 0, NULL);
		if (S4_SUCCESS != dwResult) 
		{
			printf("S4Control(S4_SET_DEVICE_ID) for device No. %d failed! <error code: 0x%08x\n",i,dwResult);
			continue;
		}

		dwResult = S4Close(pstS4CtxList+i);
		if (S4_SUCCESS != dwResult) 
		{
			printf("S4Close for device No. %d failed! <error code: 0x%08x\n",i,dwResult);
			continue;
		}				
	}
	printf("Set Customer ID completed successfully! You can now pull ,replug all \
			\ndevices and then call S4Enum(...) to see the new customer ID in \
			\npstS4CtxList->bID nor you can skip replugging by calling S4Open(...), \
			\nS4Control(...S4_RESET_DEVICE...) to refresh the information by your\
			\nprogram.\n");
    getch();

	dwResult = S4Enum(pstS4CtxList, &dwCtxListSize);
	if (S4_SUCCESS != dwResult) 
	{
		printf("S4Enum failed with error code:%d\n",dwResult);
		free(pstS4CtxList);
		pstS4CtxList = NULL;
		return 1;
	}

	btDeviceNumber = dwCtxListSize/sizeof(SENSE4_CONTEXT);
	
	for(i=0; i<btDeviceNumber; i++)
	{

		char buffer[200];
		int j = 0;
        int k = 0;

		dwResult = S4Open(pstS4CtxList+i);
		if (S4_SUCCESS != dwResult) 
		{
			printf("S4Open for device No. %d failed with error code:%d\n",i,dwResult);
			continue;
		}
		/* */
		dwResult = S4Control(pstS4CtxList+i,S4_RESET_DEVICE,NULL,0,NULL,0,NULL);
		if (S4_SUCCESS != dwResult) 
		{
			printf("S4Control(S4_SET_DEVICE_ID) for device No. %d failed with error code:%d\n",i,dwResult);
			continue;
		}	


		k = sprintf(buffer,"The Customer ID for device No.%d is:\n [",i);
		for(j=0; j<8; j++)
		{
			k += sprintf(buffer+k,"0x%.2x ",(pstS4CtxList+i)->bID[0+j]);
		}
		k += sprintf(buffer+k,"]\n");
		printf("%s",buffer);

		S4Close(pstS4CtxList+i);
	}
	free(pstS4CtxList);
	pstS4CtxList = NULL;
	
	getch();
	return 0;
}










































