* ==========================================================================
* Demo: The Super Password and Privilege Status
* Caution: no error check for the sake of briefness and understandability
* Tips:
* (1) ROCKEY developers will get a 8-bytes length super password with dongles.Super 
*     password verification is required for specific dongle operations£¬it may never 
*     be shared with end users¡£
*     ROCEKY5 will be DICPR_SUPERPASS privilege status after Super password verification.
*	   Then developer can do operations below:
*
*     a) Create and delete files.
*     b) Create and delete internal files with API.
*     c) Format ROCKEY file system.
*     d) Reset the maximum retry numbers of super password verification.
*     e) Get remote upgrade password.

* (2) The super password of Demo ROCKEY is FF FF FF FF FF FF FF FF.
* (3) The GET_CARD_PRIVILEGE command will return the current privilege status of the dongle. 
*     Its return value is one byte in length, every bit of the byte represents different 
*     privilege status.
*     DICPR_NOPASS		0		* Password not verified status 
*     DICPR_SUPERPASS	4		* Super password verified status 
*     DICPR_REMOTEPASS	8		* Remote update password verified status

* (4) The SUPERPASS_STATUS macro of DIC_Get shows if the super password verification is 
*     successful. The return value of 1 represents successful verification, and 0 indicates 
*     super password verification failure. In same way,REMOTEPASS_STATUS macro detects whether the remote 
*     update password verification is successful.
* (5) You can set the maximum retry numbers when setting super password. Super password will be disabled if
*     if retry numbers exceed.
* ==========================================================================

#include "Dic32_Vfp.h"
clear 
DECLARE integer   DIC_Find IN DIC32U.dll
DECLARE integer   DIC_Open IN DIC32U.dll integer hic, string @  reader_name
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
cmddata=space(256)
buffer =space(256)

	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

	* Get privilege information of the dongle
	errcode = DIC_Command(hic, GET_CARD_PRIVILEGE,@ cmddata) 
	errcode = DIC_Get(@cmddata, SUPERPASS_STATUS, BY_VALUE, NULL)
	if (errcode<>0)
		?"Super password verified" 
	else
	   ?"Super password not verified" 
    endif

	* Super password is 8 FFs
	for i=0 to 7
	    DIC_Set(@cmddata,i,1,0xff,NULL)
    endfor
	errcode = DIC_Command(hic, CHECK_SUPER_PASS, @cmddata) 

	* Get privilege information of the dongle
	errcode = DIC_Command(hic, GET_CARD_PRIVILEGE, @cmddata) 
	errcode = DIC_Get(@cmddata, SUPERPASS_STATUS, BY_VALUE, NULL)
	if (errcode<>0)
		?"Super password verified" 
	else 
	   ?"Super password not verified" 
    endif

	* Set new super passwrod
	* copy new password (0 1 2 3 4 5 6 7) to buffer
	* p3 of DIC_Set is maximum retry numbers(10)
	for i=0 to 7
	    DIC_Set(@buffer,i,1,i,NULL)
    endfor	
	DIC_Set(@cmddata, SUPERPASS_DATA, BY_ARRAY, 10, @buffer) 
	errcode = DIC_Command(hic, SET_SUPER_PASS, @cmddata) 

	* return to password not verified status
	errcode = DIC_Close(hic) 
	errcode = DIC_Open(hic, NULL) 

	* Check with new super passwrod.
	for i=0 to 7
	    DIC_Set(@cmddata,i,1,i,NULL)
    endfor	
	errcode = DIC_Command(hic, CHECK_SUPER_PASS, @cmddata) 

	* Get privilege information of the dongle
	errcode = DIC_Command(hic, GET_CARD_PRIVILEGE, @cmddata)
	errcode =  DIC_Get(@cmddata, SUPERPASS_STATUS, BY_VALUE, NULL)
	if (errcode <> 0 )
		?"Super password verified" 
	else 
	   ?"Super password not verified" 
	endif
 
	* restore super password to 8 FFs.
	for i=0 to 7
	    DIC_Set(@buffer,i,1,0xff,NULL)
    endfor
	DIC_Set(@cmddata, SUPERPASS_DATA, BY_ARRAY, 10, @buffer) 
	errcode = DIC_Command(hic, SET_SUPER_PASS, @cmddata) 

	errcode = DIC_Close(hic)  
clear dlls
return 	