Exemplo n.º 1
0
        /// <summary>
        /// Unlock the PCM by requesting a 'seed' and then sending the corresponding 'key' value.
        /// </summary>
        public async Task <Response <bool> > UnlockEcu(int keyAlgorithm)
        {
            Message            seedRequest  = this.messageFactory.CreateSeedRequest();
            Response <Message> seedResponse = await this.device.SendRequest(seedRequest);

            if (seedResponse.Status != ResponseStatus.Success)
            {
                if (seedResponse.Status != ResponseStatus.UnexpectedResponse)
                {
                    Response.Create(ResponseStatus.Success, true);
                }
                return(Response.Create(seedResponse.Status, false));
            }

            Response <UInt16> seedValueResponse = this.messageParser.ParseSeed(seedResponse.Value.GetBytes());

            if (seedValueResponse.Status != ResponseStatus.Success)
            {
                return(Response.Create(seedValueResponse.Status, false));
            }

            if (seedValueResponse.Value == 0x0000)
            {
                this.logger.AddUserMessage("PCM Unlock not required");
                return(Response.Create(seedValueResponse.Status, true));
            }

            UInt16 key = KeyAlgorithm.GetKey(keyAlgorithm, seedValueResponse.Value);

            Message            unlockRequest  = this.messageFactory.CreateUnlockRequest(key);
            Response <Message> unlockResponse = await this.device.SendRequest(unlockRequest);

            if (unlockResponse.Status != ResponseStatus.Success)
            {
                return(Response.Create(unlockResponse.Status, false));
            }

            string          errorMessage;
            Response <bool> result = this.messageParser.ParseUnlockResponse(unlockResponse.Value.GetBytes(), out errorMessage);

            if (errorMessage != null)
            {
                this.logger.AddUserMessage(errorMessage);
            }
            else
            {
                this.logger.AddUserMessage("PCM Unlocked");
            }

            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Unlock the PCM by requesting a 'seed' and then sending the corresponding 'key' value.
        /// </summary>
        public async Task <bool> UnlockEcu(int keyAlgorithm)
        {
            await this.device.SetTimeout(TimeoutScenario.ReadProperty);

            this.device.ClearMessageQueue();

            this.logger.AddDebugMessage("Sending seed request.");
            Message seedRequest = this.messageFactory.CreateSeedRequest();

            if (!await this.TrySendMessage(seedRequest, "seed request"))
            {
                this.logger.AddUserMessage("Unable to send seed request.");
                return(false);
            }

            bool   seedReceived = false;
            UInt16 seedValue    = 0;

            for (int attempt = 1; attempt < MaxReceiveAttempts; attempt++)
            {
                Message seedResponse = await this.device.ReceiveMessage();

                if (seedResponse == null)
                {
                    this.logger.AddDebugMessage("No response to seed request.");
                    return(false);
                }

                if (this.messageParser.IsUnlocked(seedResponse.GetBytes()))
                {
                    this.logger.AddUserMessage("PCM is already unlocked");
                    return(true);
                }

                this.logger.AddDebugMessage("Parsing seed value.");
                Response <UInt16> seedValueResponse = this.messageParser.ParseSeed(seedResponse.GetBytes());
                if (seedValueResponse.Status == ResponseStatus.Success)
                {
                    seedValue    = seedValueResponse.Value;
                    seedReceived = true;
                    break;
                }

                this.logger.AddDebugMessage("Unable to parse seed response. Attempt #" + attempt.ToString());
            }

            if (!seedReceived)
            {
                this.logger.AddUserMessage("No seed reponse received, unable to unlock PCM.");
                return(false);
            }

            if (seedValue == 0x0000)
            {
                this.logger.AddUserMessage("PCM Unlock not required");
                return(true);
            }

            UInt16 key = KeyAlgorithm.GetKey(keyAlgorithm, seedValue);

            this.logger.AddDebugMessage("Sending unlock request (" + seedValue.ToString("X4") + ", " + key.ToString("X4") + ")");
            Message unlockRequest = this.messageFactory.CreateUnlockRequest(key);

            if (!await this.TrySendMessage(unlockRequest, "unlock request"))
            {
                this.logger.AddDebugMessage("Unable to send unlock request.");
                return(false);
            }

            for (int attempt = 1; attempt < MaxReceiveAttempts; attempt++)
            {
                Message unlockResponse = await this.device.ReceiveMessage();

                if (unlockResponse == null)
                {
                    this.logger.AddDebugMessage("No response to unlock request. Attempt #" + attempt.ToString());
                    continue;
                }

                string          errorMessage;
                Response <bool> result = this.messageParser.ParseUnlockResponse(unlockResponse.GetBytes(), out errorMessage);
                if (errorMessage == null)
                {
                    return(result.Value);
                }

                this.logger.AddUserMessage(errorMessage);
            }

            this.logger.AddUserMessage("Unable to process unlock response.");
            return(false);
        }