/*****************************************************************************************
*  Copyright (c) 2004, Beijing Senselock Data Security Centre
*  All rights reserved.
*
*  Filename£ºsample_1.c
*          
*  Briefs£ºThe purpose of this snippet is to demonstrate how to enumerate all the devices currently
*          connected to the host and how to distinguish them acoording to the Global "Customer ID".
*
*          About Customer ID:An 8-octet data preset in SenseLock EL by Senselock that's 
*          strictly guaranteed to be exclusively unique among all the SenseLock EL devices manufactured.
*		   
*            
*  History:        
*  --------------------------------------------------------        
*            11/09/2004 v1.0.0.0 Lihua created
*            
*            
*******************************************************************************************/

#include "..\..\inc\win32\sense4.h"
#include <malloc.h>
#include <stdio.h>
#include <conio.h>

int main(int argc, char* argv[])
{
	PSENSE4_CONTEXT pstS4CtxList = NULL;
	DWORD dwCtxListSize = 0;
	DWORD  btDeviceNumber = 0;
	//BYTE lpOutBuf[8] = {0x00,};
	BYTE lpOutBuf[4] = {0x00,};
	DWORD dwBytesReturned = 0;
	DWORD i = 0;
	

	/*
		Use S4Enum(...) to Enumerate all the devices connected to the host.
		Using NULL as the 1st parameter will instruct S4Enum to return the buffer size needed to hold
		the corresponding SenseLock EL context in the 2nd parameter:dwCtxListSize.
		Generally only two results possible on return: (dwCtxListSize == 0) which means no SenseLock EL device 
		present or (dwCtxListSize != 0) and that's saying there are  dwCtxListSize/sizeof(SENSE4_CONTEXT) sets 
		of SenseLock EL devices connected currently.
	*/
	
	DWORD 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;
	}

	/* malloc memory for the pointer to SenseLock EL context(s) */
	pstS4CtxList = (PSENSE4_CONTEXT)malloc(dwCtxListSize);
	if (NULL == pstS4CtxList) 
	{
		printf("malloc failed! \n");
		return 1;
	}


	/*
		This time, call S4Enum(...) to do the real Enumeration.
		All the fields of the SenseLock EL context(s) pointed by pstS4CtxList will be filled 
		with the corresponding device information upon a successful function return.
	*/
	
	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++)
	{
		/* Call S4Open(...) to open a SenseLock EL device in "share mode" according to the context pointer */
		dwResult = S4Open(pstS4CtxList+i);
		if (S4_SUCCESS != dwResult) 
		{
			printf("S4Open for device No. %d failed with error code:%d\n",i,dwResult);
			continue;
		}							
		
		/*
			Call S4Control(...,S4_GET_CUSTOMER_NAME,...) to get the Customer name of the currently opened 
			SenseLock EL device .For more information about S4Control(...),please refer to the 
			"SenseLock EL user Manual".
		*/
		dwResult = S4Control(pstS4CtxList+i, S4_GET_CUSTOMER_NAME, NULL, 0, lpOutBuf, sizeof(lpOutBuf), &dwBytesReturned);
		if (S4_SUCCESS != dwResult) 
		{
			printf("S4Control for device No. %d failed with error code:%d\n",i,dwResult);
			continue;
		}else
		{
			char buffer[200];
			int j = 0;
			int k = sprintf(buffer,"The Customer ID for device No.%d is:\n [",i);
			for(j=0; j<4; j++)
			{
				k += sprintf(buffer+k,"0x%.2x ",lpOutBuf[j]);
			}
			k += sprintf(buffer+k,"]\n");
			printf("%s",buffer);
		}

		/* Call S4Close(...) to close the SenseLock EL device previously opened. */
		dwResult = S4Close(pstS4CtxList+i);
		if (S4_SUCCESS != dwResult) 
		{
			printf("S4Close for device No. %d failed with error code:%d\n",i,dwResult);
			continue;
		}				
	}
	getch();
	
	return 0;
}