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: Remote Update
' Caution: no error check for the sake of briefness and understandability
' Tips:
' (1)  Remote Update Management (RUM) is a utility that may be utilized by ROCKEY developers
'      to implement remote updates to their software protection and licensing systems. RUM is
'      based on "one time use" passwordsĄŁ
' (2)  The RUM process is described below:( some steps is for developer, others for end user)
'
'      1) "a.Set new basic remote update password"
'         The software developer must first initialize the ROCKEY dongle to set the Basic
'         Remote Update Password (BRUP). This process requires use of the super password. Once
'         the BRUP is set it can only be changed by reformatting the dongle by means of the
'         super password. The initialized dongle may then be distributed to the end user.
'      2) "b.Get remote update information and the current dongle hardware ID"
'         When software developers need to remote update their software, ROCKEY software at
'         the user end gets the relevant information (the current remote update information and
'         hardware ID) and transfers such data by some means to the software developer.
'      3) "d.Change the privilege to super user and acquire the next remote update password."
'         Upon receiving the pertinent information, the software developer gets the next update
'         password for the end user and transfers the new remote update password to the end user.
'      4) "f.return to password not verified status"
'         next step is demo for end user, so return to password not verified status.
'      5) "g.Verify remote update password and check if the remote update password passes the
'          verification."
'         After receiving the new update password from the software developer, the user can send
'         the new password to dongle for verification. If it succeeds, the remote update tag of
'         dongle is set, the program may check the tag status to determine the corresponding
'         actions. Because the dongle internal program can also detect the tag, it is recommended
'         that some part of the update process be performed by the internal program.
' (3) The remote update tag, RemoteTag, is not the update password, it is the information set by
'     the software developer for identifying the current software update status. Usually, we
'     recommend you write the software version here, thus the software developer can tell from
'     which version the user intends to update. It is a 32-bit number, but its highest bit is used
'     to show whether the "hardware ID" is used or not. If the bit is 1, the update password is
'     related to the "Hardware ID" of dongle; if it is 0, it means the update password has nothing
'     to do with the "hardware ID", it only makes reference to the previous update password.
'     "one time password" simplifies update maintenance of the software developer.
' (4) Additionally, the RemoteTag and RemotePass provided to users are related to each other. The
'     user can not modify either of the two. If the highest bit of RemoteTag is 1, the update
'     password is only valid for some specific dongle. In the information the user offers to
'     software developer, only the hardware ID and the previous update password are necessary.
'     The last RemoteTag is merely a reference to the software developer, and it does not affect
'     the next update information. The software developer should set a new RemoteTag as the update
'     mark different from the previous ones for each update operation.
' ==========================================================================

Private Sub TestBtn_Click()
Dim errcode, num, hic, i As Long
Dim cmddata(0 To 255) As Byte ' Command buffer
Dim buffer(0 To 255) As Byte 'User buffer
Dim remotepass(0 To 7) As Byte
Dim RemoteTag, HardSerial As Long
Dim curline As Integer
Dim line As String

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.Set the new basic remote update password.
errocode = DIC_Set(cmddata(0), REMOTE_TAG, BY_VALUE, 1, 0&)    ' Set remote tag to 1
For i = 0 To 7
    buffer(i) = Asc("A")    ' set the new remote update password 8 As.
Next i
errcode = DIC_Set(cmddata(0), REMOTE_PASS, BY_ARRAY, 0, buffer(0))
errcode = DIC_Command(hic, SET_REMOTE_INFO, cmddata(0))   ' Update remote update info

' b.Read remote update info
errcode = DIC_Command(hic, GET_REMOTE_INFO, cmddata(0))
RemoteTag = DIC_Get(cmddata(0), REMOTE_TAG, BY_VALUE, 0&)
errcode = DIC_Get(cmddata(0), REMOTE_PASS, BY_ARRAY, buffer(0))
List1.List(curline) = "RemoteTag=" + Str(RemoteTag)
curline = curline + 1
line = "RemotePass="
For i = 0 To 7
    line = line + Hex(buffer(i)) + " "
    remotepass(i) = buffer(i)
Next i
List1.List(curline) = line
curline = curline + 1

' c.Get hardware ID of the current dongle
errcode = DIC_Command(hic, GET_HARDWARE_INFO, cmddata(0)) '  Get dongle hardware ID
HardSerial = DIC_Get(cmddata(0), HARD_SERIAL, BY_VALUE, 0&)

' d.Change to super user
'  Super password is 8 FFs.
For i = 0 To 7
    cmddata(i) = 255
Next i
errcode = DIC_Command(hic, CHECK_SUPER_PASS, cmddata(0))

' e.Get the next update password of the current one
errcode = DIC_Set(cmddata(0), UPGRADE_REMOTE_TAG, BY_VALUE, 2, 0&)    ' The new update tag is 2
errcode = DIC_Set(cmddata(0), UPGRADE_HARD_SERIAL, BY_VALUE, HardSerial, 0&)
For i = 0 To 7
    buffer(i) = remotepass(i)
Next i
errcode = DIC_Set(cmddata(0), UPGRADE_REMOTE_PASS, BY_ARRAY, 0, buffer(0))
errcode = DIC_Command(hic, GET_UPGRADE_REMOTE_PASS, cmddata(0))
For i = 0 To 7
    remotepass(i) = cmddata(i)  ' Transfer the new remote update password to remotepass
Next i

' f.Change the super user status to common user status
errcode = DIC_Close(hic)
errcode = DIC_Open(hic, 0&)

' g.Verify the remote update password
errcode = DIC_Set(cmddata(0), REMOTE_TAG, BY_VALUE, 2, 0&)
For i = 0 To 7
    buffer(i) = remotepass(i)
Next i
errcode = DIC_Set(cmddata(0), REMOTE_PASS, BY_ARRAY, 0, buffer(0))
errcode = DIC_Command(hic, CHECK_REMOTE_INFO, cmddata(0)) ' Verify remote update info

' h.Check if remote update password verification succeeds
errcode = DIC_Command(hic, GET_CARD_PRIVILEGE, cmddata(0))    '  Get privilege info
errcode = DIC_Get(cmddata(0), REMOTEPASS_STATUS, BY_VALUE, 0&)
If (errcode <> 0) Then
    List1.List(curline) = "Remote update password verification succeeded"
Else
    List1.List(curline) = "Remote update password verification failed"
End If
curline = curline + 1

' i.Read the new remote update information
errcode = DIC_Command(hic, GET_REMOTE_INFO, cmddata(0))
RemoteTag = DIC_Get(cmddata(0), REMOTE_TAG, BY_VALUE, 0&)
errcode = DIC_Get(cmddata(0), REMOTE_PASS, BY_ARRAY, buffer(0))
List1.List(curline) = "RemoteTag=" + Str(RemoteTag)
curline = curline + 1
line = "RemotePass="
For i = 0 To 7
    line = line + Hex(buffer(i)) + " "
    remotepass(i) = buffer(i)
Next i
List1.List(curline) = line
curline = curline + 1

errcode = DIC_Close(hic)
End Sub

