&& ==========================================================================
* counter
* (1) set counter
* (2) start counter
* description
*  (1) if set the counter to 0, it means no limitation
*  (2) if the counter decreases to 0, the it stops and becomes invalid
&& ==========================================================================


#include "Dic32_Vfp.h"
clear 
DECLARE integer   DIC_Find IN Dic32u.dll
DECLARE integer   DIC_Open IN Dic32u.dll integer hic, string @  reader_ROCKEY
DECLARE integer   DIC_Close  IN Dic32u.dll  integer   hic
DECLARE integer   DIC_Command  IN Dic32u.dll  integer  hic, integer cmd, string @  cmddata
DECLARE integer   DIC_Get  IN Dic32u.dll  string @xdata, integer  p1, integer   p2, string @buffer
DECLARE integer   DIC_Set  IN Dic32u.dll  string @xdata, integer  p1, integer  p2, integer   p3, string @buffer


store 0 to  errcode		   && error code
store 0 to  num		   && number of founded card reader(s)
store 0 to  hic		   && handle of ROCKEY device.
store 0 to  i,j
store 0 to temp
store 5 to dwMaxCounter

dimension temp[128]


cmddata = space(1024)
buffer = space(1024)

?"Step counter sample:"
	num = DIC_Find()
	for i=0 to num-1
		hic = DIC_Open(i, NULL)
		if (hic >= 0) 
		  exit  	&& Stop searching once one is found.
  	  endif
	endfor
	if (i == num) 
	     return
    endif

    * a. Change to super user status (Only super users are allowed to create executable files)
    * Super password is 8 FFs for demo ROCKEY
	for i=0 to 7
	    DIC_Set(@cmddata,i,1,0xff,NULL)
    endfor
	errcode = DIC_Command(hic, CHECK_SUPER_PASS, @cmddata) 


	* b. Format ROCKEY, all files and directorys will be cleared.
    *    You can set ATR and VOLUME here.
*	DIC_Set(@cmddata, DIC_VOLUME, BY_ARRAY, 0, "DICSYSTEM") 
*	DIC_Set(@cmddata, ATR, BY_ARRAY, 0, "DIC Co.") 
*	errcode = DIC_Command(hic, FORMAT_CARD, @cmddata) 

	&& set counter
	for i=0 to 7
	    DIC_Set(@cmddata,i,1,0,NULL)
    endfor
    DIC_Set(@cmddata,0,1,dwMaxCounter,NULL)
	errcode = DIC_Command(hic, SETCOUNTER, @cmddata)
	if (errcode!=0)
		?"SetCounter err"
	endif
	
	
	* i. cancel supper password status
	errcode = DIC_Close(hic) 
	errcode = DIC_Open(hic, NULL) 
	
	&& STEPCOUNTER
	for i=0 to dwMaxCounter
		errcode = DIC_Command(hic, STEPCOUNTER, NULL)
		if(errcode != SCARD_S_SUCCESS)
			if(i>=dwMaxCounter)
				?"i> dwMaxCounter"
				?"step counter ok"
				for i=0 to 7
	    			DIC_Set(@cmddata,i,1,0xff,NULL)
    			endfor
				errcode = DIC_Command(hic, CHECK_SUPER_PASS, @cmddata) 

				&& if set 0,it means no limitation
				for i=0 to 4
	    			DIC_Set(@cmddata,i,1,0,NULL)
    			endfor
				errcode = DIC_Command(hic, SETCOUNTER,@cmddata)
				if (errcode!=0)
					?"SetCounter err"
				endif			
			else
				?"step counter error"	
			endif
			
		endif
	endfor
	
	errcode = DIC_Close(hic) 
	
clear dlls
return 	