/// <summary> /// Load the extended parameters /// </summary> internal void LoadExtendedParameters(XPathDocument document) { XPathNavigator navigator = document.CreateNavigator(); foreach (XPathNavigator node in navigator.Select( "/logger/protocols/protocol/ecuparams/ecuparam/ecu[@id='" + this.EcuIdentifier + "']")) { node.MoveToFirstChild(); int address = SsmParameterSource.GetMemoryAddress(node); int length = SsmParameterSource.GetMemoryLength(node); node.MoveToParent(); node.MoveToParent(); string name = node.GetAttribute("name", ""); string id = node.GetAttribute("id", ""); XPathNodeIterator iterator = node.Select("conversions/conversion"); List <Conversion> conversions = SsmParameterSource.GetConversions(iterator); SsmParameter parameter = new SsmParameter( this, id, name, address, length, conversions.AsReadOnly()); // TODO: remove this when the duplicates in logger.xml are cleaned up if (!this.Parameters.Contains(parameter)) { this.AddParameter(parameter); } } }
/// <summary> /// Invoked by the SsmInterface when the ECU identifier has been received /// </summary> private void GetEcuIdentifierCallback(IAsyncResult asyncResult) { Trace.WriteLine("SsmBasicLogger.GetEcuIdentifierCallback"); ConnectAsyncResult internalState = (ConnectAsyncResult)asyncResult.AsyncState; try { this.ecu.EndGetEcuIdentifier(asyncResult); Trace.WriteLine("SsmBasicLogger.GetEcuIdentifierCallback: creating database"); // TODO: remove ParameterDatabase from SsmBasicLogger, pass ParameterSource to GetEcuIdentifier callback internalState.ParameterSource = SsmParameterSource.GetInstance( configurationDirectory, this.ecu.EcuIdentifier, this.ecu.CompatibilityMap); } catch (UnauthorizedAccessException ex) { internalState.Exception = ex; } catch (IOException ex) { internalState.Exception = ex; } catch (System.Security.SecurityException ex) { internalState.Exception = ex; } internalState.Completed(); }
/// <summary> /// Load the standard parameters /// </summary> internal void LoadStandardParameters(XPathDocument document) { XPathNavigator navigator = document.CreateNavigator(); foreach (XPathNavigator node in navigator.Select( "/logger/protocols/protocol/parameters/parameter")) { string id = node.GetAttribute("id", string.Empty); string name = node.GetAttribute("name", string.Empty); int byteIndex = SsmParameterSource.GetIndexFromAttribute(node, "ecubyteindex"); int bitIndex = SsmParameterSource.GetIndexFromAttribute(node, "ecubit"); XPathNodeIterator children = node.Select("address"); children.MoveNext(); int address = SsmParameterSource.GetMemoryAddress(children.Current); int length = SsmParameterSource.GetMemoryLength(children.Current); children.MoveNext(); children = node.Select("conversions/conversion"); List <Conversion> conversions = SsmParameterSource.GetConversions(children); children = node.Select("depends/ref"); ReadOnlyCollection <Parameter> dependencies = this.GetDependencies(children); if (this.EcuSupports(byteIndex, bitIndex)) { SsmParameter parameter = new SsmParameter( this, id, name, address, length, conversions.AsReadOnly(), byteIndex, bitIndex, dependencies); this.AddParameter(parameter); } else { Trace.WriteLine("Skipping parameter " + id + " / " + name); } } }
/// <summary> /// Get the address & length from an address element /// </summary> /// <exception cref="IOException">Unable to read from source stream</exception> /// <exception cref="InvalidDataException">Source document is corrupt</exception> private static void GetAddress(XmlReader reader, ref int address, ref int length) { reader.Read(); string addressString = reader.Value; address = SsmParameterSource.GetAddress(addressString); string lengthString = reader.GetAttribute("length"); length = GetInteger(lengthString, 1); int depth = reader.Depth; SsmParameterSource.ReadUntil( reader, delegate { return(reader.Depth >= depth); }, "Did not expect EOF after address element"); }
/// <summary> /// Use XPathDocument to load switches /// </summary> internal void LoadSwitches(XPathDocument document) { XPathNavigator navigator = document.CreateNavigator(); foreach (XPathNavigator node in navigator.Select( "/logger/protocols/protocol/switches/switch")) { string id = node.GetAttribute("id", string.Empty); string name = node.GetAttribute("name", string.Empty); string addressString = node.GetAttribute("byte", string.Empty); string bitString = node.GetAttribute("bit", string.Empty); int address = SsmParameterSource.GetAddressFromString(addressString); //int bit = (int) SsmParameterSource.GetAddressFromString(bitString); string expression = "x&(2^" + bitString + ")"; Conversion conversion = Conversion.GetInstance(Conversion.Boolean, expression, ""); /* * SsmSwitch s = SsmSwitch.GetInstance( * id, * name, * address, * bit); * this.switches.Add(s); * */ SsmParameter parameter = new SsmParameter( this, id, name, address, 1, new List <Conversion>(new Conversion[] { conversion }).AsReadOnly()); this.AddParameter(parameter); } }
/// <summary> /// Use XmlReader to load extended parameters /// </summary> /// <exception cref="IOException">Unable to read from source stream</exception> /// <exception cref="InvalidDataException">Source document is corrupt</exception> internal void LoadExtendedParameters(XmlReader reader) { while (!reader.EOF) { if (!reader.ReadToFollowing("ecuparam")) { return; } string id = reader.GetAttribute("id"); string name = reader.GetAttribute("name"); int depth = reader.Depth; reader.Read(); for (; ;) { if (reader.EOF) { return; } if (reader.Depth < depth) { break; } if (reader.IsStartElement("ecu")) { depth = reader.Depth; string ecuId = reader.GetAttribute("id"); int address = 0; int length = 0; if (ecuId == this.EcuIdentifier) { reader.Read(); SsmParameterSource.ReadUntil( reader, delegate { return(reader.IsStartElement()); }, "Expected address element after ecu element"); SsmParameterSource.GetAddress(reader, ref address, ref length); if (!reader.ReadToFollowing("conversion")) { throw new InvalidDataException("Invalid XML: expected conversion element after ecuparam element"); } List <Conversion> conversions = new List <Conversion>(); SsmParameterSource.GetConversions(reader, conversions); SsmParameter parameter = new SsmParameter( this, id, name, address, length, conversions.AsReadOnly()); this.AddParameter(parameter); break; } } reader.Read(); } } }
/// <summary> /// Load standard parameters with the XmlReader method /// </summary> /// <remarks> /// TODO: stop at the end of the parameters element, so the stream rewind won't be necessary /// </remarks> /// <exception cref="IOException">Unable to read from source stream</exception> /// <exception cref="InvalidDataException">Source document is corrupt</exception> internal void LoadStandardParameters(XmlReader reader) { while (!reader.EOF) { if (!reader.ReadToFollowing("parameter")) { return; } //string elementName = reader.LocalName; string id = reader.GetAttribute("id"); string name = reader.GetAttribute("name"); string byteString = reader.GetAttribute("ecubyteindex"); string bitString = reader.GetAttribute("ecubit"); int byteIndex = GetInteger(byteString, 0); int bitIndex = GetInteger(bitString, 0); int address = 0; int length = 0; ReadOnlyCollection <Parameter> dependencies = null; reader.Read(); SsmParameterSource.ReadUntil( reader, delegate { return(reader.IsStartElement()); }, "Expected address element after parameter element"); if (reader.IsStartElement("address")) { SsmParameterSource.GetAddress(reader, ref address, ref length); } else if (reader.IsStartElement("depends")) { List <Parameter> dependencyList = new List <Parameter>(); if (reader.ReadToDescendant("ref")) { while (reader.IsStartElement("ref")) { string dependencyId = reader.GetAttribute("parameter"); foreach (Parameter candidate in this.Parameters) { if (candidate.Id == dependencyId) { dependencyList.Add(candidate); break; } } if (!reader.ReadToNextSibling("ref")) { break; } } if (dependencyList.Count > 0) { dependencies = dependencyList.AsReadOnly(); } else { dependencies = null; } } else { throw new InvalidDataException("XML schema doesn't match expectations. Parameter \"" + name + "\" has depends but not ref"); } } else { throw new InvalidDataException("XML schema doesn't match expectations. Parameter \"" + name + "\" not followed by address or depends"); } if (!reader.ReadToFollowing("conversion")) { throw new InvalidDataException("XML schema doesn't match expectations (expected conversions node after address node, found " + reader.Name + ")"); } List <Conversion> conversions = new List <Conversion>(); SsmParameterSource.GetConversions(reader, conversions); SsmParameter parameter = new SsmParameter( this, id, name, address, length, conversions.AsReadOnly(), byteIndex, bitIndex, dependencies.Count == 0 ? null : dependencies); this.AddParameter(parameter); reader.Read(); } }