VERSION 5.00
Begin VB.Form Form1 
   Caption         =   "Form1"
   ClientHeight    =   4590
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   7875
   LinkTopic       =   "Form1"
   ScaleHeight     =   4590
   ScaleWidth      =   7875
   StartUpPosition =   3  'Windows Default
   Begin VB.CommandButton TestBtn 
      Caption         =   "Test"
      Height          =   375
      Left            =   3000
      TabIndex        =   1
      Top             =   4080
      Width           =   1575
   End
   Begin VB.ListBox List1 
      Height          =   3765
      Left            =   120
      TabIndex        =   0
      Top             =   120
      Width           =   7575
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
' ==========================================================================
' Demo: File system
' Caution: no error check for the sake of briefness and understandability
' Tips:
' The file system is the kernel of ROCKEY environment. ROCKEY files system like DOS file system
' in some way, they both have directorys and files. But ROCKEY file system is defferent from
' DOS FS. There are some  Basic Introduction.
'
' (1) Directory/File ID (2 bytes)
'     Both files and directories have the identical attributes of two bytes. The file IDs must be not same
'     under same directory. It's unique for both directory and file.
'
'     Caution: those ID are pre-defined:
'     3F00: ID of Root directory.
'     2F01: ATR file ID.
'     3FFF: ID for get current directory.(We not use it)
'     0000: reserved by system.
'     You can not use those ID when creating files and directorys.
'
' (2) Directory/File Class (1 byte)
'     a) All directories and files have a file class code (FCLA) that is defined by one
'        byte (0x0-0xFF).Developers may use the file class code (FCLA) to define independent
'        subsystems. Files in the same class are generally assumed to belong to the same product.
'     b) Executable files are restricted by the FCLA - they can only read, write or create data
'        files of the same class code.
'     c) (FCLA) that is defined by one byte (0x0-0xFF).
'     d) There is one byte for the system to keep the current file class, called "system current
'        file class"(SYSFCLA).
'     e) There are specific FCLA rules: 0xFF means the file is a "none-class" file. Data files
'        of this type can be accessed by any kind of executable file. Executable files of this
'        type can only access 0xFF type data files.
'     f) When executing the "select file" command or calling a target file in dongle, the "system
'        current file class" would change according to the following rules: SYSFCLA = FCLA¡£
'     g) If the target file is "none-class", the system current file class remains unchanged
'     h) The "system current security level" is reset to 0 when the "system current file" class
'        is changed.
'     i) Resetting the dongle will put the system current file class to the default - 0xFF.
'     j) The class of root directory is 0xFF.(none-class).
'     k) In the ROCKEY system, "none-class" data files are public data and "none-class"
'        executables are public processing programs.¡£
'     l) Executable files of class 0xFF can only access 0xFF type data files.
'     m) The more important application of the class code is the executable filter file. This is
'        covered in chapter 12 of ROCKEY guide.
'
'     Suggest:
'     a) file class are generally used in product management. It's very convenient if develop have
'        more than one products or many developers wokes at one product.
'     b) You'd better set class of all files if ROCKEY is only for one product,one developer.
'     c) Fils and sub-directorys under same directory should have same class, then you can
'        use "File privilige" to limit access.
'
' (3) Dirctory/File Security Level (half byte)
'     a) In addition to "class", each file has a security level(FPRI) that may be used to
'        enhance file management.
'     b) FPRI only used for files with same FCLA. FPRI is lower level than FCLA.'
'     c) The security level takes the lower four bits (0x00-0x0F) of one byte, while the four
'        higher bits are taken by the file attribute. There are 16 levels of security privilege,
'        rising from 0 to 15.
'     d) The COS has its own security level (SYSFPRI). It consumes one byte for recording during
'        operation.'
'     e) ts security level can only be advanced by a system call to an executable residing in the
'        dongle. However, the COS security level set by the executable may not exceed that of the
'        executable it's self¡£
'     f) Executable files can only execute (read, write, and create) data files with the same or
'        lower security levels than them selves.
'     g) When an executable exits, the current system security level can be cleared to 0.
'     i) Executable files have FILEATTR_UPIGNORE attribute. when set it, it will not execute if SYSFPRI >= FPRI
'        of exe file.
'
'     Suggest:
'     a) The security level is a higher level of file management. File security levels may be set
'        to "0" if such complex management is unnecessary.
'     b) You should set file class firstly, then file attribute.
'
' (4) Directory/File Attributes (half byte)
'     a) There are five kinds of file attributes: normal, executable, directory, up-ignore and
'        internal. A directory or file may have more than attributes.
'        NORMAL    0x00    ' Normal file
'        EXEC      0x10    ' Executable file
'        DIR       0x20    ' Directory
'        UPIGNORE  0x40    ' Up-ignore
'        INTERNAL  0x80    ' Internal
'     b) The "executable" attribute indicates that the file may be run by the COS. The COS will
'        only run files under the root directory. "Executable" files can only be created or
'        deleted after super password verification.¡£
'     c) The "directory" attribute indicates that the file is a sub directory. If the file is
'        set to directory, other attribute bits can be ignored.
'     d) The "up-ignore" only applies to executable files. It means that the file can only be
'        executed on the condition that the system security level is lower than that of the
'        executable file its self. When the system security level is higher or equal to that of
'        the executable file, this file will not be executed.
'     e) If the "internal" file is a data file, it is only accessible to the executable files in
'        dongle. External operations can only delete "internal" files with prior super password
'        verification. But external operations can never read or write to "internal" files. If the
'        "internal" file is an executable, it will not be displayed without prior super password
'        verification. The "internal" attribute is generally used to protect executables that have
'        some hidden attributes.
'
' (5) File name
'     Both files and directories have 16 bytes of long file name (optional). You can access file via
'       file ID or file name if you set it.
' ==========================================================================
' Tips for program:
' (1) You must check supper password before format, and you can set ATR and VOLUME when format.
' (2) You must set directory ID, name, class&attribute when create.
' (3) LIST_DIR use enumerate method. You can do it until get an error code.
' (4) SET_CURRENT_DIR like DOS comman "cd xxxx". You need set file ID or file name when call it.
'     Set file ID to 0 when call it via file name.
' (5) You must set File ID, name, size,class&attribute when create. Set file name buffer to zero if
'     it have no file name.
' (6) File will be selected automatically after create, no need call SET_CURRENT_FILE.

Private Sub TestBtn_Click()
Dim errcode, num, hic, i, readsize As Long
Dim cmddata(0 To 255) As Byte ' Command buffer
Dim buffer(0 To 255) As Byte 'User buffer
Dim dirid As Integer
Dim curline As Integer

curline = 0

num = DIC_Find()
For i = 0 To num - 1
    hic = DIC_Open(i, 0&)
    If (hic >= 0) Then
        Exit For
    End If
Next i
If (i = num) Then
    Exit Sub
End If


' a.Check supper password,it's required by format.
For i = 0 To 7
    cmddata(i) = 255 ' Super password is 8 FFs for demo ROCKEY
Next i
errcode = DIC_Command(hic, CHECK_SUPER_PASS, cmddata(0))

' b.  Format ROCKEY. it will clear all files and directorys.
'  You can set ATR and VOLUME here.
Call Str2Array("DICSYSTEM", buffer)
errcode = DIC_Set(cmddata(0), VOLUME, BY_ARRAY, 0, buffer(0))
Call Str2Array("DIC Co.", buffer)
errcode = DIC_Set(cmddata(0), ATR, BY_ARRAY, 0, buffer(0))
errcode = DIC_Command(hic, FORMAT_CARD, cmddata(0))

' c. return to password not verified status
errcode = DIC_Close(hic)
errcode = DIC_Open(hic, 0&)

' d. Create directory, id is 0x1000, name is "Dir1"
errcode = DIC_Set(cmddata(0), FILL, 256, 0, 0&)                 ' Clear buffer
errcode = DIC_Set(cmddata(0), DIR_ID, BY_VALUE, &H1000, 0&)     ' dir ID
errcode = DIC_Set(cmddata(0), DIR_CLASS, BY_VALUE, 255, 0&)     ' dir class
Call Str2Array("Dir1", buffer)
errcode = DIC_Set(cmddata(0), DIR_NAME, BY_ARRAY, 0, buffer(0))    ' dir name
errcode = DIC_Command(hic, CREATE_DIR, cmddata(0))

' e. List all files in root dir.
'   Note: the first is root dir ID, the second is ATR file.
For i = 0 To 254
    errcode = DIC_Set(cmddata(0), FILE_NO, BY_VALUE, i, 0&)     ' set file index
    errcode = DIC_Command(hic, LIST_DIR, cmddata(0))
    If (errcode <> SCARD_S_SUCCESS) Then
        Exit For
    End If
    dirid = DIC_Get(cmddata(0), DIR_ID, BY_VALUE, 0&)           ' Get file and directory in same way.
    errcode = DIC_Get(cmddata(0), DIR_NAME, BY_ARRAY, buffer(0))
    List1.List(curline) = Hex(dirid) + " " + StrConv(buffer, vbUnicode)
    curline = curline + 1
Next i

' f. enter direcoty 0x1000
errcode = DIC_Set(cmddata(0), DIR_ID, BY_VALUE, &H1000, 0&)
errcode = DIC_Command(hic, SET_CURRENT_DIR, cmddata(0))

' g. create directory 0x2000 under 0x1000
errcode = DIC_Set(cmddata(0), FILL, 256, 0, 0&)                 ' Clear buffer
errcode = DIC_Set(cmddata(0), DIR_ID, BY_VALUE, &H2000, 0&)     ' dir ID
errcode = DIC_Set(cmddata(0), DIR_CLASS, BY_VALUE, 255, 0&)     ' dir class
Call Str2Array("Dir2", buffer)
errcode = DIC_Set(cmddata(0), DIR_NAME, BY_ARRAY, 0, buffer(0))    ' dir name
errcode = DIC_Command(hic, CREATE_DIR, cmddata(0))

' h. create file 0x0033 in 0x1000
errcode = DIC_Set(cmddata(0), FILL, 256, 0, 0&)                 ' Clear buffer
errcode = DIC_Set(cmddata(0), FILE_ID, BY_VALUE, &H33, 0&)      ' file ID
errcode = DIC_Set(cmddata(0), FILE_CLASS, BY_VALUE, 255, 0&)    ' file class
errcode = DIC_Set(cmddata(0), FILE_ATTRIBUTE, BY_VALUE, FILEATTR_NORMAL, 0&)  ' file attr
errcode = DIC_Set(cmddata(0), FILE_SIZE, BY_VALUE, 20, 0&)      ' file size
Call Str2Array("File1", buffer)
errcode = DIC_Set(cmddata(0), FILE_NAME, BY_ARRAY, 0, buffer(0))  ' file name
errcode = DIC_Command(hic, CREATE_FILE, cmddata(0))

' i. write 20 bytes to file 0x0033
'    File will be selected automatically after create, no need call SET_CURRENT_FILE.
Call Str2Array("abcdefghijklmnopqrst", buffer)
errcode = DIC_Set(cmddata(0), WRITE_DATA, 20, 0, buffer(0))
errcode = DIC_Command(hic, WRITE_FILE, cmddata(0))

' j. Enter dir 0x2000 via dir-name.
errcode = DIC_Set(cmddata(0), DIR_ID, BY_ARRAY, 0, 0&) ' Must set file ID to 0
Call Str2Array("Dir2", buffer)
errcode = DIC_Set(cmddata(0), DIR_NAME, BY_ARRAY, &H2000, buffer(0))   ' Set file name
errcode = DIC_Command(hic, SET_CURRENT_DIR, cmddata(0))

' k. Go to parent dir(0x1000)
errcode = DIC_Command(hic, SET_PARENT_DIR, cmddata(0))

' l. Select  file 0x0033
errcode = DIC_Set(cmddata(0), FILE_ID, BY_VALUE, &H33, 0&)
errcode = DIC_Command(hic, SET_CURRENT_FILE, cmddata(0))

' m. Read it.
errcode = DIC_Set(cmddata(0), READ_DATA, 20, 0, 0&)
errcode = DIC_Command(hic, READ_FILE, cmddata(0))
readsize = DIC_Get(cmddata(0), READ_DATA, BY_VALUE, buffer(0))
List1.List(curline) = str(readsize) + " " + StrConv(buffer, vbUnicode)
curline = curline + 1

errcode = DIC_Close(hic)
End Sub