using System; using ASAM.XIL.Implementation.Testbench.Common.ValueContainer; using ASAM.XIL.Interfaces.Testbench.Common.ValueContainer; using ASAM.XIL.Interfaces.Testbench.MAPort; namespace HILAPI.Tester { class Tester { static public bool RunSeatTest(IMAPort maPort, ITestEnvironment environment) { bool testLongitudinal = true; bool testHeight = true; IUintValue value2; string text; // 1.) change control variable to start diagnostic session environment.Output("Starting diagnostic session..."); IUintValue value = (UintValue)maPort.Read("Test/SeatGetVariant"); ++value.Value; maPort.Write("Test/SeatGetVariant", value); environment.Wait(TimeSpan.FromMilliseconds(100)); // 2.) read ECU variant environment.Output("Reading ECU variant..."); string variant = ReadStringValue(maPort, "Test/SeatVariant", TimeSpan.FromMilliseconds(1000), environment); bool testDiagnostics = !string.IsNullOrEmpty(variant); if (testDiagnostics) { text = "ECU variant is: "; text += variant; environment.Output(text); } else { environment.Output("Failed to read ECU variant!"); } // 3.) perform longitudinal test for both variants if ("Basis" == variant || "Comfort" == variant) { environment.Output("Performing longitudinal tests..."); // forward maPort.Write("Test/SeatPosForward", new UintValue(1)); maPort.Write("Test/SeatPosBackward", new UintValue(0)); if (!WaitForValue(maPort, "Test/SeatLongitudinalUpperLimit", 1, TimeSpan.FromMilliseconds(5000), environment)) { testLongitudinal = false; } // backward maPort.Write("Test/SeatPosForward", new UintValue(0)); maPort.Write("Test/SeatPosBackward", new UintValue(1)); if (!WaitForValue(maPort, "Test/SeatLongitudinalLowerLimit", 1, TimeSpan.FromMilliseconds(5000), environment)) { testLongitudinal = false; } maPort.Write("Test/SeatPosForward", new UintValue(0)); maPort.Write("Test/SeatPosBackward", new UintValue(0)); environment.Wait(TimeSpan.FromMilliseconds(100)); // both value = (IUintValue)maPort.Read("Test/SeatLongitudinalPos"); maPort.Write("Test/SeatPosForward", new UintValue(1)); maPort.Write("Test/SeatPosBackward", new UintValue(1)); environment.Wait(TimeSpan.FromMilliseconds(2000)); value2 = (IUintValue)maPort.Read("Test/SeatLongitudinalPos"); if (value.Value != value2.Value) { testLongitudinal = false; } maPort.Write("Test/SeatPosForward", new UintValue(0)); maPort.Write("Test/SeatPosBackward", new UintValue(0)); environment.Wait(TimeSpan.FromMilliseconds(100)); text = "Longitudinal tests "; text += testLongitudinal ? "succeeded" : " failed!"; environment.Output(text); } // 4.) perform height test for Comfort variant only if ("Comfort" == variant) { environment.Output("Performing height tests..."); // up maPort.Write("Test/SeatPosUp", new UintValue(1)); maPort.Write("Test/SeatPosDown", new UintValue(0)); if (!WaitForValue(maPort, "Test/SeatHeightUpperLimit", 1, TimeSpan.FromMilliseconds(5000), environment)) { testHeight = false; } // down maPort.Write("Test/SeatPosUp", new UintValue(0)); maPort.Write("Test/SeatPosDown", new UintValue(1)); if (!WaitForValue(maPort, "Test/SeatHeightLowerLimit", 1, TimeSpan.FromMilliseconds(5000), environment)) { testHeight = false; } maPort.Write("Test/SeatPosUp", new UintValue(0)); maPort.Write("Test/SeatPosDown", new UintValue(0)); environment.Wait(TimeSpan.FromMilliseconds(100)); // both value = (IUintValue)maPort.Read("Test/SeatHeightPos"); maPort.Write("Test/SeatPosUp", new UintValue(1)); maPort.Write("Test/SeatPosDown", new UintValue(1)); environment.Wait(TimeSpan.FromMilliseconds(2000)); value2 = (IUintValue)maPort.Read("Test/SeatHeightPos"); if (value.Value != value2.Value) { testHeight = false; } maPort.Write("Test/SeatPosUp", new UintValue(0)); maPort.Write("Test/SeatPosDown", new UintValue(0)); environment.Wait(TimeSpan.FromMilliseconds(100)); text = "Height tests "; text += testHeight ? "succeeded" : " failed!"; environment.Output(text); } environment.Output("Tests completed"); return testDiagnostics && testLongitudinal && testHeight; } static bool WaitForValue(IMAPort maPort, string name, uint value, TimeSpan timeout, ITestEnvironment environment) { // read variable var currentValue = (IUintValue)maPort.Read(name); if (value == currentValue.Value) { return true; } // check timeout TimeSpan wait = TimeSpan.FromMilliseconds(10); if (timeout < wait) { return false; } // wait & read up to maximum timeout do { if (!environment.Wait(wait)) { break; } timeout -= wait; // read variable again currentValue = (IUintValue)maPort.Read(name); } while (value != currentValue.Value && timeout > TimeSpan.Zero); return value == currentValue.Value; } static string ReadStringValue(IMAPort maPort, string name, TimeSpan timeout, ITestEnvironment environment) { // read variable var variant = (IStringValue)maPort.Read(name); if (0 != variant.Value.Length) { // return if available return variant.Value; } // check timeout TimeSpan wait = TimeSpan.FromMilliseconds(10); if (timeout < wait) { return variant.Value; } // wait & read up to maximum timeout do { if (!environment.Wait(wait)) { break; } timeout -= wait; // read variable again variant = (IStringValue)maPort.Read(name); } while (0 == variant.Value.Length && timeout > TimeSpan.Zero); return variant.Value; } } }