/// <summary> /// Send unknown request /// </summary> /// <returns>KWPResult</returns> public KWPResult sendUnknownRequest() { LogDataString("sendUnknownRequest"); //Console.WriteLine("sendUnknownRequest"); KWPReply reply = new KWPReply(); KWPResult result; //Send request //Mode = 0x3E //PID = no PID used by this request //Expected result = 0x7E result = sendRequest(new KWPRequest(0x02, 0x3E), out reply); Console.WriteLine(reply.ToString()); if (reply.getMode() != 0x7E) return KWPResult.NOK; else return result; }
/// <summary> /// sendEraseRequest sends an erase request to the ECU. /// This method must be called before the ECU can be flashed. /// </summary> /// <returns>KWPResult</returns> public KWPResult sendEraseRequest() { LogDataString("sendEraseRequest"); KWPReply reply = new KWPReply(); KWPReply reply2 = new KWPReply(); KWPResult result = KWPResult.Timeout; int i = 0; //First erase message. Up to 5 retries. //Mode = 0x31 //PID = 0x52 //Expected result is 0x71 result = sendRequest(new KWPRequest(0x31, 0x52), out reply); Console.WriteLine("Erase(1) " + reply.ToString()); if (result != KWPResult.OK) return result; while (reply.getMode() != 0x71) { System.Threading.Thread.Sleep(1000); result = sendRequest(new KWPRequest(0x31, 0x52), out reply); Console.WriteLine("Erase(2:" + i.ToString() + ") " + reply.ToString()); if (i++ > 15) return KWPResult.Timeout; } if (result != KWPResult.OK) return result; //Second erase message. Up to 10 retries. //Mode = 0x31 //PID = 0x53 //Expected result is 0x71 i = 0; result = sendRequest(new KWPRequest(0x31, 0x53), out reply2); Console.WriteLine("Erase(3) " + reply2.ToString()); if (result != KWPResult.OK) return result; while (reply2.getMode() != 0x71) { System.Threading.Thread.Sleep(1000); result = sendRequest(new KWPRequest(0x31, 0x53), out reply2); Console.WriteLine("Erase(4:" + i.ToString() + ") " + reply2.ToString()); if (i++ > 20) return KWPResult.Timeout; } //Erase confirm message //Mode = 0x3E //Expected result is 0x7E result = sendRequest(new KWPRequest(0x3E, 0x53), out reply2); Console.WriteLine("Erase(5) " + reply2.ToString()); return result; }
public KWPResult sendEraseMemorySpaceRequest(Int32 StartAddress, Int32 Length) { /*02 FLASH memory erasure 1 start address (high byte) 2 start address (middle byte) 3 start address (low byte) 4 stop address (high byte) 5 stop address (middle byte) 6 stop address (low byte) */ Int32 StopAddress = StartAddress + Length; LogDataString("sendEraseRequest"); KWPReply reply = new KWPReply(); KWPReply reply2 = new KWPReply(); KWPResult result = KWPResult.Timeout; int i = 0; //First erase message. Up to 5 retries. //Mode = 0x31 //PID = 0x51 //Expected result is 0x71 byte[] a_data = new byte[6]; int bt = 0; //a_data[0] = (byte)(StartAddress >> 24); a_data[bt++] = (byte)(StartAddress >> 16); a_data[bt++] = (byte)(StartAddress >> 8); a_data[bt++] = (byte)(StartAddress); //a_data[4] = (byte)(Length >> 24); a_data[bt++] = (byte)(StopAddress >> 16); a_data[bt++] = (byte)(StopAddress >> 8); a_data[bt++] = (byte)(StopAddress); requestSequrityAccess(true); KWPRequest req = new KWPRequest(0x31, 0x50, a_data); result = sendRequest(req, out reply); Console.WriteLine("Erase(1) " + reply.ToString()); /*if (result != KWPResult.OK) return result; System.Threading.Thread.Sleep(10000); result = sendRequest(new KWPRequest(0x31, 0x53, a_data), out reply); Console.WriteLine("Erase(2:" + i.ToString() + ") " + reply.ToString()); */ if (result != KWPResult.OK) return result; result = sendRequest(new KWPRequest(0x3E, 0x50), out reply2); // tester present??? Console.WriteLine("reply on exit " + reply2.ToString()); return result; }
/// <summary> /// This method sends a KWPRequest and returns a KWPReply. /// </summary> /// <param name="a_request">The request.</param> /// <param name="a_reply">The reply.</param> /// <returns>KWPResult</returns> private KWPResult sendRequest(KWPRequest a_request, out KWPReply a_reply, int expectedLength) { int _maxSendRetries = 3; KWPResult _kwpResult = KWPResult.Timeout; LogDataString("sendRequest"); KWPReply reply = new KWPReply(); RequestResult result; a_reply = new KWPReply(); //<GS-11012010> was allemaal 1000 int keepAliveTimeout = 1000; //Console.WriteLine("Checking KWP device open"); if (!m_kwpDevice.isOpen()) return KWPResult.DeviceNotConnected; // reset the timer for keep alive (set to 1 seconds now) if (stateTimer == null) stateTimer = new System.Threading.Timer(sendKeepAlive, new Object(), keepAliveTimeout, keepAliveTimeout); stateTimer.Change(keepAliveTimeout, keepAliveTimeout); m_requestMutex.WaitOne(); int _retryCount = 0; result = RequestResult.Unknown; // <GS-11022010> while (_retryCount < _maxSendRetries && result != RequestResult.NoError) { LogDataString(a_request.ToString()); result = m_kwpDevice.sendRequest(a_request, out reply); if ((int)reply.getLength() != expectedLength) { result = RequestResult.InvalidLength; } if (result == RequestResult.NoError) { a_reply = reply; LogDataString(reply.ToString()); LogDataString(""); // empty line m_requestMutex.ReleaseMutex(); //return KWPResult.OK; _kwpResult = KWPResult.OK; } else { LogDataString("Timeout in KWPHandler::sendRequest"); m_requestMutex.ReleaseMutex(); //return KWPResult.Timeout; _kwpResult = KWPResult.Timeout; } } return _kwpResult; }
/// <summary> /// This method sends a KWPRequest and returns a KWPReply. /// </summary> /// <param name="a_request">The request.</param> /// <param name="a_reply">The reply.</param> /// <returns>KWPResult</returns> private KWPResult sendRequest(KWPRequest a_request, out KWPReply a_reply) { LogDataString("sendRequest"); KWPReply reply = new KWPReply(); RequestResult result; a_reply = new KWPReply(); //<GS-11012010> was allemaal 1000 int keepAliveTimeout = 1000; //Console.WriteLine("Checking KWP device open"); if (!m_kwpDevice.isOpen()) return KWPResult.DeviceNotConnected; // reset the timer for keep alive (set to 1 seconds now) if (stateTimer == null) stateTimer = new System.Threading.Timer(sendKeepAlive, new Object(), keepAliveTimeout, keepAliveTimeout); stateTimer.Change(keepAliveTimeout, keepAliveTimeout); m_requestMutex.WaitOne(); LogDataString(a_request.ToString()); Console.WriteLine("KWP send request: " + a_request.ToString()); result = m_kwpDevice.sendRequest(a_request, out reply); a_reply = reply; if (result == RequestResult.NoError) { LogDataString(reply.ToString()); LogDataString(""); // empty line m_requestMutex.ReleaseMutex(); return KWPResult.OK; } else { LogDataString("Timeout in KWPHandler::sendRequest: " + result.ToString()); Console.WriteLine("Timeout in KWPHandler::sendRequest: " + result.ToString()); m_requestMutex.ReleaseMutex(); return KWPResult.Timeout; } }
/// <summary> /// Send a request for a sequrity access with one out of two methods to /// calculate the key. /// </summary> /// <param name="a_method">Key calculation method [1,2]</param> /// <returns>true if sequrity access was granted, otherwise false</returns> private bool requestSequrityAccessLevel() { LogDataString("requestSequrityAccessLevel"); KWPReply reply = new KWPReply(); KWPResult result; byte[] seed = new byte[2]; byte[] key = new byte[2]; // Send a seed request. KWPRequest requestForKey = new KWPRequest(0x27, 0x05); // TODO: What is this for T8? Console.WriteLine("requestSequrityAccessLevel request for key: " + requestForKey.ToString()); result = sendRequest(requestForKey, out reply); Console.WriteLine("requestSequrityAccessLevel request for key result: " + reply.ToString()); if (result != KWPResult.OK) return false; if (reply.getData().Length < 2) return false; seed[0] = reply.getData()[0]; seed[1] = reply.getData()[1]; key = calculateKey(seed); // Send key reply. KWPRequest sendKeyRequest = new KWPRequest(0x27, 0x06, key); Console.WriteLine("requestSequrityAccessLevel send Key request: " + sendKeyRequest.ToString()); result = sendRequest(sendKeyRequest, out reply); Console.WriteLine("requestSequrityAccessLevel send Key reply: " + reply.ToString()); if (result != KWPResult.OK) { Console.WriteLine("Security access request was not send"); return false; } //Check if sequrity was granted. Console.WriteLine("Mode: " + reply.getMode().ToString("X2")); Console.WriteLine("Data: " + reply.getData()[0].ToString("X2")); if ((reply.getMode() == 0x67) && (reply.getData()[0] == 0x34)) // WAS [0] { Console.WriteLine("Security access granted"); return true; } Console.WriteLine("Security access was not granted: " + reply.ToString()); return false; }