private void SortAndBuildInternalStructures() { if (PGNDefns != null) { Array.Sort(pgnDefns); // Identify any duplicate PGNs in the data for (int i = 0; i < pgnDefns.Length; i++) { pgnDefns[i].HasMultipleDefinitions = false; } for (int i = 0; i < pgnDefns.Length - 1; i++) { if (pgnDefns[i].PGN == pgnDefns[i + 1].PGN) { pgnDefns[i].HasMultipleDefinitions = true; pgnDefns[i + 1].HasMultipleDefinitions = true; } } // Now (re)build up a dictionary of the PGN indices, recording the *first* instance of each PGN PGNDictionary.Clear(); for (int i = pgnDefns.Length - 1; i >= 0; i--) { PGNDictionary[pgnDefns[i].PGN] = i; } // Finally we sort all the fields foreach (PGNDefn p in PGNDefns) { p.SortFields(); } // ReBuildPGNDictionary(); } }
// Given a (possibly partially) completed PGN message, // described by a PGN number, and the apparent 'byte length' // for the message (which is only relevant if the message is a // multiple packet message), return the // PGNDefn that allows this message to be decoded public PGNDefn GetPGNDefn(N2kFrame msg) { uint PGN = (uint)msg.Header.PGN; int pgnIndex; if (!PGNDictionary.TryGetValue(PGN, out pgnIndex)) { // No matching PGN definition; return UnknownPGNDefn return(UnknownPGNDefn); } else { PGNDefn pgn = PGNDefns[pgnIndex]; // Is this a multiple definition PGN? (If so, it must be a fast packet PGN?) if (!pgn.HasMultipleDefinitions) { // Only one matching definition available; return it // int iBytesTransmitted = pgn.ByteLength <= 8 ? msg.Data.Length : msg.Data[1]; return(PGNDefns[pgnIndex]); } else { // Find the matching definition while ((pgnIndex < PGNDefns.Length) && (PGNDefns[pgnIndex].PGN == pgn.PGN)) { if (PGNDefns[pgnIndex].ByteLength == msg.Data[1]) { return(PGNDefns[pgnIndex]); } else { // Find any PGN that is not too long, just on case we cannot find any with an exact match if (pgn.ByteLength < msg.Data[1]) { pgn = PGNDefns[pgnIndex]; } pgnIndex++; } } return(pgn); } } }