* ==========================================================================
* Demo: Write and run exe-file.
* Caution: no error check for the sake of briefness and understandability
* Tips:
* (1) You can write exe-files to dongle and run it in dongle. We have Integrate 
*     Design Entironment(IDE), you can write,debug,and run program there.
* (2) dist.c is dongle program£¬we convert the compile result to bin file, and 
*     put it to PC program via array.     
* (3) You must write exe-file to root directory after super password verification.
* (4) Demo inclue 2 steps: write and run exe-file. The first step should finished befor
*     dongle vending. Then only second step is in final product.
* ==========================================================================
* dist.c calculates  a^2+b^2

*void main()
*{
*	unsigned int a,b,c;
*	get_input(&a,0,1,1);
*	get_input(&b,2,1,1);
*	a=a*a;
*	b=b*b;
*	c=a+b;
*	set_response(2,&c);
*	exit();
*}
* ==========================================================================

#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
store 0 to a, b, c ,temp
store 0 to j
dimension tmp[2]
cmddata=space(256)
buffer=space(256)
dimension g_progDIST[3] 
#define DIST_SIZE  193
store '002000177117014000117015000117016008228245017117018001117019001018000105117014000117015000117016010117017002117018001117019001018000105174008175009172008173009018000159142008143009174010175011172010173011018000159142010143011229009037011245' to g_progDIST[1]
store '013229008053010245012117014002117015000117016000117017012018000136002000189018255000229014036000229015036001229016036002229017036003229018036004229019036005018255011034018255000229015036000229016036001229017036002229014036003018255010034239' to g_progDIST[2]
store '141240164168240207140240164040206141240164046254034120127228246216253117129019002000003018255013034' to   g_progDIST[3]


	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. Check supper password,it's required by writing process.
    * 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. Create exe-file.
	DIC_Set(@cmddata, FILE_ID, BY_VALUE, 0x0001, NULL) 					&& File ID
	DIC_Set(@cmddata, FILE_CLASS, BY_VALUE, 0xff, NULL) 					&& File class
	DIC_Set(@cmddata, FILE_ATTRIBUTE, BY_VALUE, FILEATTR_EXEC, NULL) 	&& File attr
	DIC_Set(@cmddata, FILE_SIZE, BY_VALUE, DIST_SIZE, NULL) 				&& File size
	DIC_Set(@cmddata, FILE_NAME, BY_ARRAY, 0, "6002") 
	errcode = DIC_Command(hic, CREATE_FILE, @cmddata) 

	* c.  Write exe-file
	for j=1 to 3 
	       for i=0 to 80
	       temp = substr(g_progDIST[j],i*3 + 1 ,3)
	       DIC_Set( @buffer ,(j-1)*80+i ,bitor(BY_STRING ,1) ,0,@temp )
	       endfor
	endfor
	DIC_Set(@cmddata, WRITE_DATA, DIST_SIZE, 0, @buffer) 
	errcode = DIC_Command(hic, WRITE_FILE, @cmddata) 
	

	* d. return to password not verified status
	*    we cannot run program at super password verified status.
	errcode = DIC_Close(hic) 
	errcode = DIC_Open(hic, NULL) 

	* e. Run
	a = 3 
	b = 4 
	DIC_Set(@buffer, 0, 2, a, NULL) 			&& Set a
	DIC_Set(@buffer, 2, 2, b, NULL)  			&& Set b
	DIC_Set(@cmddata, RUN_DATA, 4, 0x6002, @buffer) 	&& Set data to buffer
	errcode = DIC_Command(hic, RUN, @cmddata) 
	DIC_Get(@cmddata, RUN_DATA, BY_VALUE, @buffer) 		&& Get data
	
	*convert byte order
	tmp[1]=DIC_Get(@buffer, 0, bitor(BY_VALUE,1), NULL)
	tmp[2]=DIC_Get(@buffer, 1, bitor(BY_VALUE,1), NULL)
	DIC_Set(@buffer, 0, bitor(BY_VALUE,1),tmp[1],NULL)
	DIC_Set(@buffer, 1, bitor(BY_VALUE,1),tmp[2],NULL)
	
	c = DIC_Get(@buffer, 1, bitor(BY_VALUE,1), NULL) 		&& read c
	? a,"*",a,"+", b,"*", b,"=", c

	errcode = DIC_Close(hic)  
clear dlls
return 	