Beispiel #1
0
        /// <summary>
        /// Write a request to the mock ECU
        /// </summary>
        public override void Write(byte[] buffer, int offset, int count)
        {
            if (buffer[0] != 0x80)
            {
                throw new InvalidOperationException("First byte sent to ECU must be 0x80 (command start byte)");
            }

            /*
             * if (buffer[1] != 0x10)
             * {
             *  throw new InvalidOperationException("Second byte sent to ECU must be 0x10 (destination = ECU)");
             * }
             *
             * if (buffer[2] != 0xF0)
             * {
             *  throw new InvalidOperationException("Third byte sent to ECU must be 0xF0 (source = diagnostic tool)");
             * }
             */
            if (MockEcuStream.GetPacketLength(buffer) != buffer.Length - 5)
            {
                throw new InvalidOperationException("Fourth byte sent to ECU must be equal to \"buffer_length - 5\"");
            }

            if (this.corruptNextResponse)
            {
                byte[] data = new byte[100];
                for (int i = 0; i < data.Length; i++)
                {
                    data[i] = (byte)0x55;
                }

                this.SetResponse(data);
                this.corruptNextResponse = false;
                return;
            }

            SsmPacket packet = SsmPacket.ParseRequest(buffer, offset, count);

            switch (packet.Command)
            {
            case SsmCommand.ReadBlockRequest:
                this.ReadBlock(packet.BlockStart, packet.BlockLength);
                break;

            case SsmCommand.ReadAddressesRequest:
                this.ReadAddresses(packet.Addresses);
                break;

            case SsmCommand.WriteBlockRequest:
                //this.WriteBlock(packet.BlockStart, packet.BlockData);
                break;

            case SsmCommand.WriteAddressesRequest:
                //this.WriteAddresses(packet.Addresses, packet.Values);
                break;

            case SsmCommand.EcuInitRequest:
                this.Handshake();
                break;
            }

            List <byte> temporaryBuffer = new List <byte>(packet.Data.Length + this.response.Length);

            temporaryBuffer.AddRange(packet.Data);
            temporaryBuffer.AddRange(this.response);
            this.SetResponse(temporaryBuffer.ToArray());
        }