4using System.Threading.Tasks;
19 private ushort errorCode;
63 if (!(this.data is
null))
64 return (this.errorCode, this.data);
72 ErrorCode = this.
NextUInt16(nameof(ErrorCode));
77 uint TedsOffset = this.
NextUInt32(nameof(TedsOffset));
81 return (ErrorCode,
null);
84 if (Len >
int.MaxValue)
85 return (ErrorCode,
null);
91 CheckSum += this.
Body[Start++];
95 ushort CheckSum2 = this.
NextUInt16(nameof(CheckSum));
96 if (CheckChecksum && CheckSum != CheckSum2)
97 return (ErrorCode,
null);
103 this.errorCode = ErrorCode;
108 return (ErrorCode,
Teds);
115 return (0xffff,
null);
128 out uint TedsOffset, out
double TimeoutSeconds)
133 TedsAccessCode = this.NextUInt8<TedsAccessCode>(nameof(
TedsAccessCode));
134 TedsOffset = this.
NextUInt32(nameof(TedsOffset));
164 using (MemoryStream ms =
new MemoryStream())
168 ms.Write(AppId, 0, 16);
171 ms.WriteByte((
byte)(ChannelId >> 8));
172 ms.WriteByte((
byte)ChannelId);
175 byte[] Bin = BitConverter.GetBytes(TedsOffset);
179 TimeoutSeconds *= 1e9 * 65536;
180 ulong l = (ulong)TimeoutSeconds;
181 Bin = BitConverter.GetBytes(l);
187 if (!(SnifferOutput is
null))
189 SnifferOutput.Append(
"App ID: ");
191 SnifferOutput.Append(
"NCAP ID: ");
193 SnifferOutput.Append(
"TIM ID: ");
195 SnifferOutput.Append(
"Channel ID: ");
196 SnifferOutput.AppendLine(ChannelId.ToString());
197 SnifferOutput.Append(
"TEDS Access Code: ");
199 SnifferOutput.Append(
"TEDS Offset: ");
200 SnifferOutput.AppendLine(TedsOffset.ToString());
201 SnifferOutput.Append(
"Timeout (s): ");
202 SnifferOutput.AppendLine(TimeoutSeconds.ToString());
221 ushort ChannelId, StringBuilder SnifferOutput,
TedsId TedsHeader, params
TedsRecord[] Records)
224 NcapId =
new byte[16];
225 else if (NcapId.Length != 16)
226 throw new ArgumentException(
"Invalid NCAP UUID.", nameof(NcapId));
229 TimId =
new byte[16];
230 else if (TimId.Length != 16)
231 throw new ArgumentException(
"Invalid TIM UUID.", nameof(TimId));
233 using (MemoryStream ms =
new MemoryStream())
237 ms.WriteByte((
byte)(ErrorCode >> 8));
238 ms.WriteByte((
byte)ErrorCode);
239 ms.Write(AppId, 0, 16);
240 ms.Write(NcapId, 0, 16);
241 ms.Write(TimId, 0, 16);
242 ms.WriteByte((
byte)(ChannelId >> 8));
243 ms.WriteByte((
byte)ChannelId);
249 using (MemoryStream ms2 =
new MemoryStream())
251 Append(ms2, TedsHeader);
256 byte[] Bin = ms2.ToArray();
257 int Len = Bin.Length;
264 for (i = 0; i < 4; i++)
273 for (i = 0; i < 4; i++)
275 ms.WriteByte((
byte)k);
281 ms.Write(Bin, 0, Len);
283 for (i = 0; i < Len; i++)
289 ms.WriteByte((
byte)(Checksum >> 8));
290 ms.WriteByte((
byte)Checksum);
294 if (!(SnifferOutput is
null))
296 SnifferOutput.Append(
"Error Code: ");
297 SnifferOutput.AppendLine(ErrorCode.ToString());
298 SnifferOutput.Append(
"App ID: ");
300 SnifferOutput.Append(
"NCAP ID: ");
302 SnifferOutput.Append(
"TIM ID: ");
304 SnifferOutput.Append(
"Channel ID: ");
305 SnifferOutput.AppendLine(ChannelId.ToString());
306 SnifferOutput.AppendLine(
"TEDS Offset: 0");
308 TedsHeader.
Append(SnifferOutput);
311 Record.
Append(SnifferOutput);
313 SnifferOutput.AppendLine(Checksum.ToString());
314 SnifferOutput.AppendLine(
"TEDS Offset: 0");
322 private static void Append(MemoryStream ms2,
TedsRecord Record)
325 int RecordLen = Bin?.Length ?? 0;
327 throw new Exception(
"Record length exceeds 255 bytes in length.");
329 ms2.WriteByte(Record.
Type);
330 ms2.WriteByte((
byte)RecordLen);
333 ms2.Write(Bin, 0, RecordLen);
Contains methods for simple hash calculations.
static string BinaryToString(byte[] Data)
Converts an array of bytes to a string with their hexadecimal representations (in lower case).
IEEE 1451.0 Binary object
bool HasSniffers
If sniffers are available.
int Position
Current position.
ICommunicationLayer ComLayer
Sniffable interface on which the message was received.
Task LogInformationToSniffer()
Logs accumulated sniffer output to associated sniffable interface.
uint NextUInt32(string Name)
Gets the next uint.
TedsRecord[] ParseTedsRecords(ParsingState State)
Parses a set of TEDS records.
byte[] NextUInt8Array(string Name)
Gets the next Byte array
double NextTimeDurationSeconds(string Name)
Gets the next time duration, expressed in seconds.
ushort NextUInt16(string Name)
Gets the next ushort.
ChannelAddress NextChannelId(bool AppId)
Parses a Channel ID from the message.
static readonly byte[] EmptyUuid
Empty UUID (16 zero bytes)
byte[] Tail
Bytes that are received after the body.
IEEE 1451.0 TEDS Access Message
TedsAccessMessage(NetworkServiceType NetworkServiceType, TedsAccessService TedsAccessService, MessageType MessageType, byte[] Body, byte[] Tail, ICommunicationLayer ComLayer)
IEEE 1451.0 TEDS Access Message
override string NetworkServiceIdName
Name of Message.NetworkServiceId
static byte[] SerializeRequest(byte[] NcapId, byte[] TimId, ushort ChannelId, TedsAccessCode TedsAccessCode, uint TedsOffset, double TimeoutSeconds, StringBuilder SnifferOutput)
Serializes a request for TEDS.
async Task<(ushort ErrorCode, Teds Teds)> TryParseTeds(bool CheckChecksum)
Tries to parse a TEDS from the message.
static byte[] SerializeResponse(ushort ErrorCode, byte[] NcapId, byte[] TimId, ushort ChannelId, StringBuilder SnifferOutput, TedsId TedsHeader, params TedsRecord[] Records)
Serializes a response to a TEDS request.
bool TryParseRequest(out ChannelAddress Channel, out TedsAccessCode TedsAccessCode, out uint TedsOffset, out double TimeoutSeconds)
Tries to parse a TEDS request from the message.
Task<(ushort ErrorCode, Teds Teds)> TryParseTeds()
Tries to parse a TEDS from the message.
TedsAccessService TedsAccessService
TEDS Access Service
Contains parsing information.
TEDS identification header (§6.3)
Represents one record in a TEDS
byte[] RawValue
TEDS Raw Record value
byte Type
TEDS Record Type
void Append(StringBuilder SnifferOutput)
Appends record to sniffer output.
Static class for IEEE 1451-related parsing tasks.
static byte[] SerializeMessage(NetworkServiceType NetworkServiceType, byte NetworkServiceId, MessageType MessageType, byte[] Payload)
Creates a binary IEEE 1451.0 message.
Defines the Metering Topology data source. This data source contains a tree structure of persistent r...
static Root Root
Root node.
Task Exception(string Exception)
Called to inform the viewer of an exception state.
Interface for observable classes implementing communication protocols.
TedsAccessCode
TEDS Access Code
MessageType
Network Service Message Type
NetworkServiceType
IEEE 1451.0 Network Service Type
TedsAccessService
TEDS Access Service