示例#1
0
        /// <summary>
        /// Authenticate the device.
        /// </summary>
        /// <param name="lockDisk">Whether to try to obtain exclusive lock on the disk.</param>
        /// <returns>The result of the authentication attempt.</returns>
        public AuthenticationResult Authenticate(bool lockDisk)
        {
            if (!disk.IsOkForCheck())
            {
                return(AuthenticationResult.UnsupportedDiskState);
            }

            if (lockDisk)
            {
                if (!disk.Lock())
                {
                    throw new IOException("Failed to lock disk.");
                }
            }

            try
            {
                ResetSession(); // Exits authentication process in case previous attempt was cut short
                int tries = 5;
                while (tries > 0)
                {
                    if (EnterHealthReportMode())
                    {
                        break;
                    }
                    --tries;
                }
                if (tries == 0)
                {
                    return(AuthenticationResult.FailedToEnterHealthReportMode);
                }

                byte controllerType;
                try
                {
                    controllerType = GetControllerType(); // Note this is required prior to beginning authentication
                }
                catch
                {
                    return(AuthenticationResult.FailedToGetControllerType);
                }

                return(DoAuthenticate(controllerType) ? AuthenticationResult.Successful : AuthenticationResult.FailedToAuthenticate);
            }
            finally
            {
                if (lockDisk)
                {
                    disk.Unlock();
                }
            }
        }