/*****************************************************************************************
*  Copyright (c) 2004, Beijing Senselock Data Security Centre
*  All rights reserved.
*
*  Filename£ºsample_2_set_win32.c
*          
*  Briefs£º  The purpose of this snippet is to demonstrate how to enumerate all the SenseLock EL
*            devices connected to current PC and set their "Device ID"
*            
*            About "Device ID": an 8 octets array preset in SenseLock EL by software devloper
*            that can be used to distinguish devices.
*            
*  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;

	DWORD i = 0;

	DWORD tmp = 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);

	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 */
		char lpCustomerID[9] = "OurS4_00";
		if (i < 0xf)
		{
			_itoa(i, lpCustomerID+7, 16);
		}
		else
		{
			_itoa(i, lpCustomerID+6, 16);
		}
		
		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) && (dwResult != S4_DEVICE_UNSUPPORTED)) 
		{
			printf("Change directory failed! <error code: 0x%08x>\n",dwResult);
			S4Close(pstS4CtxList+i);
			continue;
		}
		else if (dwResult == S4_DEVICE_UNSUPPORTED) // no root dir
		{

			S4CREATEDIRINFO s4_CreateInfo;
			s4_CreateInfo.dwS4CreateDirInfoSize = sizeof(S4CREATEDIRINFO);
			memcpy(s4_CreateInfo.szAtr, lpCustomerID, 8);
			printf("no root dir, start to create...\n");
			dwResult = S4CreateDirEx(pstS4CtxList+i, "\\", 0, S4_CREATE_ROOT_DIR,
				&s4_CreateInfo);		
			
		}
		else
		{
			
			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 or you can skip replugging by calling S4Open(...), \
		   \nS4Control(...S4_RESET_DEVICE...) to refresh the information by your\
		   \nprogram.\n");
    getch();

	free(pstS4CtxList);
	pstS4CtxList = NULL;
	
	return 0;
}































































