Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
DiscoveryMessage.cs
1using System;
2using System.IO;
3using System.Text;
7
9{
14 {
27 {
28 this.DiscoveryService = DiscoveryService;
29 }
30
35
39 public override string NetworkServiceIdName => this.DiscoveryService.ToString();
40
46 public static byte[] SerializeRequest(StringBuilder SnifferOutput)
47 {
48 using (MemoryStream ms = new MemoryStream())
49 {
50 byte[] AppId = MeteringTopology.Root.ObjectId.ToByteArray();
51 ms.Write(AppId, 0, 16);
52
53 byte[] Result = Ieee1451Parser.SerializeMessage(DiscoveryService.NCAPDiscovery, MessageType.Command, ms.ToArray(), SnifferOutput);
54
55 if (!(SnifferOutput is null))
56 {
57 SnifferOutput.Append("App ID: ");
58 SnifferOutput.AppendLine(Hashes.BinaryToString(AppId, true));
59 }
60
61 return Result;
62 }
63 }
64
71 public static byte[] SerializeRequest(byte[] NcapId, StringBuilder SnifferOutput)
72 {
73 if (NcapId is null || NcapId.Length != 16)
74 throw new ArgumentException("Invalid NCAP UUID.", nameof(NcapId));
75
76 using (MemoryStream ms = new MemoryStream())
77 {
78 byte[] AppId = MeteringTopology.Root.ObjectId.ToByteArray();
79
80 ms.Write(AppId, 0, 16);
81 ms.Write(NcapId, 0, 16);
82
83 byte[] Result = Ieee1451Parser.SerializeMessage(DiscoveryService.NCAPTIMDiscovery, MessageType.Command, ms.ToArray(), SnifferOutput);
84
85 if (!(SnifferOutput is null))
86 {
87 SnifferOutput.Append("App ID: ");
88 SnifferOutput.AppendLine(Hashes.BinaryToString(AppId));
89 SnifferOutput.Append("NCAP ID: ");
90 SnifferOutput.AppendLine(Hashes.BinaryToString(NcapId));
91 }
92
93 return Result;
94 }
95 }
96
104 public static byte[] SerializeRequest(byte[] NcapId, byte[] TimId, StringBuilder SnifferOutput)
105 {
106 if (NcapId is null || NcapId.Length != 16)
107 throw new ArgumentException("Invalid NCAP UUID.", nameof(NcapId));
108
109 if (TimId is null || TimId.Length != 16)
110 throw new ArgumentException("Invalid TIM UUID.", nameof(TimId));
111
112 using (MemoryStream ms = new MemoryStream())
113 {
114 byte[] AppId = MeteringTopology.Root.ObjectId.ToByteArray();
115
116 ms.Write(AppId, 0, 16);
117 ms.Write(NcapId, 0, 16);
118 ms.Write(TimId, 0, 16);
119
120 byte[] Result = Ieee1451Parser.SerializeMessage(DiscoveryService.NCAPTIMTransducerDiscovery, MessageType.Command, ms.ToArray(), SnifferOutput);
121
122 if (!(SnifferOutput is null))
123 {
124 SnifferOutput.Append("App ID: ");
125 SnifferOutput.AppendLine(Hashes.BinaryToString(AppId));
126 SnifferOutput.Append("NCAP ID: ");
127 SnifferOutput.AppendLine(Hashes.BinaryToString(NcapId));
128 SnifferOutput.Append("TIM ID: ");
129 SnifferOutput.AppendLine(Hashes.BinaryToString(TimId));
130 }
131
132 return Result;
133 }
134 }
135
144 public static byte[] SerializeResponse(ushort ErrorCode, byte[] NcapId,
145 string Name, StringBuilder SnifferOutput)
146 {
147 if (NcapId is null || NcapId.Length != 16)
148 throw new ArgumentException("Invalid NCAP UUID.", nameof(NcapId));
149
150 using (MemoryStream ms = new MemoryStream())
151 {
152 byte[] AppId = MeteringTopology.Root.ObjectId.ToByteArray();
153
154 ms.WriteByte((byte)(ErrorCode >> 8));
155 ms.WriteByte((byte)ErrorCode);
156 ms.Write(AppId, 0, 16);
157 ms.Write(NcapId, 0, 16);
158
159 byte[] Bin = Encoding.UTF8.GetBytes(Name);
160 ms.Write(Bin, 0, Bin.Length);
161 ms.WriteByte(0);
162 ms.WriteByte(1); // IPv4
163 ms.WriteByte(127);
164 ms.WriteByte(0);
165 ms.WriteByte(0);
166 ms.WriteByte(1);
167
168 byte[] Result = Ieee1451Parser.SerializeMessage(DiscoveryService.NCAPDiscovery, MessageType.Reply, ms.ToArray(), SnifferOutput);
169
170 if (!(SnifferOutput is null))
171 {
172 SnifferOutput.Append("Error Code: ");
173 SnifferOutput.AppendLine(ErrorCode.ToString());
174 SnifferOutput.Append("App ID: ");
175 SnifferOutput.AppendLine(Hashes.BinaryToString(AppId));
176 SnifferOutput.Append("NCAP ID: ");
177 SnifferOutput.AppendLine(Hashes.BinaryToString(NcapId));
178 SnifferOutput.Append("Name: ");
179 SnifferOutput.AppendLine(Name);
180 SnifferOutput.AppendLine("IPv4: 127.0.0.1");
181 }
182
183 return Result;
184 }
185 }
186
196 public static byte[] SerializeResponse(ushort ErrorCode, byte[] NcapId,
197 byte[][] TimIds, string[] TimNames, StringBuilder SnifferOutput)
198 {
199 if (NcapId is null || NcapId.Length != 16)
200 throw new ArgumentException("Invalid NCAP UUID.", nameof(NcapId));
201
202 if (TimIds is null || TimIds.Length == 0)
203 throw new ArgumentException("No TIM IDs.", nameof(TimIds));
204
205 int i, c = TimIds.Length;
206 if (TimNames.Length != c)
207 throw new ArgumentException("Invalid number of TIM names.", nameof(TimNames));
208
209 for (i = 0; i < c; i++)
210 {
211 if (TimIds[i] is null || TimIds[i].Length != 16)
212 throw new ArgumentException("Invalid TIM UUID.", nameof(TimIds));
213 }
214
215 using (MemoryStream ms = new MemoryStream())
216 {
217 byte[] AppId = MeteringTopology.Root.ObjectId.ToByteArray();
218
219 ms.WriteByte((byte)(ErrorCode >> 8));
220 ms.WriteByte((byte)ErrorCode);
221 ms.Write(AppId, 0, 16);
222 ms.Write(NcapId, 0, 16);
223
224 ms.WriteByte((byte)(c >> 8));
225 ms.WriteByte((byte)c);
226
227 for (i = 0; i < c; i++)
228 ms.Write(TimIds[i], 0, 16);
229
230 for (i = 0; i < c; i++)
231 {
232 byte[] Bin = Encoding.UTF8.GetBytes(TimNames[i]);
233 ms.Write(Bin, 0, Bin.Length);
234 ms.WriteByte(0);
235 }
236
237 byte[] Result = Ieee1451Parser.SerializeMessage(DiscoveryService.NCAPTIMDiscovery, MessageType.Reply, ms.ToArray(), SnifferOutput);
238
239 if (!(SnifferOutput is null))
240 {
241 SnifferOutput.Append("Error Code: ");
242 SnifferOutput.AppendLine(ErrorCode.ToString());
243 SnifferOutput.Append("App ID: ");
244 SnifferOutput.AppendLine(Hashes.BinaryToString(AppId));
245 SnifferOutput.Append("NCAP ID: ");
246 SnifferOutput.AppendLine(Hashes.BinaryToString(NcapId));
247
248 for (i = 0; i < c; i++)
249 {
250 SnifferOutput.Append("TIM ID ");
251 SnifferOutput.Append((i + 1).ToString());
252 SnifferOutput.Append(": ");
253 SnifferOutput.AppendLine(Hashes.BinaryToString(TimIds[i], true));
254 }
255
256 for (i = 0; i < c; i++)
257 {
258 SnifferOutput.Append("TIM Name ");
259 SnifferOutput.Append((i + 1).ToString());
260 SnifferOutput.Append(": ");
261 SnifferOutput.AppendLine(TimNames[i]);
262 }
263 }
264
265 return Result;
266 }
267 }
268
279 public static byte[] SerializeResponse(ushort ErrorCode, byte[] NcapId,
280 byte[] TimId, ushort[] ChannelIds, string[] ChannelNames, StringBuilder SnifferOutput)
281 {
282 if (NcapId is null || NcapId.Length != 16)
283 throw new ArgumentException("Invalid NCAP UUID.", nameof(NcapId));
284
285 if (TimId is null || TimId.Length != 16)
286 throw new ArgumentException("Invalid TIM UUID.", nameof(TimId));
287
288 if (ChannelIds is null || ChannelIds.Length == 0)
289 throw new ArgumentException("No Transducer channel IDs.", nameof(ChannelIds));
290
291 int i, c = ChannelIds.Length;
292 if (ChannelNames.Length != c)
293 throw new ArgumentException("Invalid number of Transducer channel names.", nameof(ChannelNames));
294
295 using (MemoryStream ms = new MemoryStream())
296 {
297 byte[] AppId = MeteringTopology.Root.ObjectId.ToByteArray();
298
299 ms.WriteByte((byte)(ErrorCode >> 8));
300 ms.WriteByte((byte)ErrorCode);
301 ms.Write(AppId, 0, 16);
302 ms.Write(NcapId, 0, 16);
303 ms.Write(TimId, 0, 16);
304
305 ms.WriteByte((byte)(c >> 8));
306 ms.WriteByte((byte)c);
307
308 for (i = 0; i < c; i++)
309 {
310 ushort ChannelId = ChannelIds[i];
311
312 ms.WriteByte((byte)(ChannelId >> 8));
313 ms.WriteByte((byte)ChannelId);
314 }
315
316 for (i = 0; i < c; i++)
317 {
318 byte[] Bin = Encoding.UTF8.GetBytes(ChannelNames[i]);
319 ms.Write(Bin, 0, Bin.Length);
320 ms.WriteByte(0);
321 }
322
323 byte[] Result = Ieee1451Parser.SerializeMessage(DiscoveryService.NCAPTIMTransducerDiscovery, MessageType.Reply, ms.ToArray(), SnifferOutput);
324
325 if (!(SnifferOutput is null))
326 {
327 SnifferOutput.Append("Error Code: ");
328 SnifferOutput.AppendLine(ErrorCode.ToString());
329 SnifferOutput.Append("App ID: ");
330 SnifferOutput.AppendLine(Hashes.BinaryToString(AppId));
331 SnifferOutput.Append("NCAP ID: ");
332 SnifferOutput.AppendLine(Hashes.BinaryToString(NcapId));
333 SnifferOutput.Append("TIM ID: ");
334 SnifferOutput.AppendLine(Hashes.BinaryToString(TimId));
335
336 for (i = 0; i < c; i++)
337 {
338 SnifferOutput.Append("Channel ID ");
339 SnifferOutput.Append((i + 1).ToString());
340 SnifferOutput.Append(": ");
341 SnifferOutput.AppendLine(ChannelIds[i].ToString());
342 }
343
344 for (i = 0; i < c; i++)
345 {
346 SnifferOutput.Append("Channel Name ");
347 SnifferOutput.Append((i + 1).ToString());
348 SnifferOutput.Append(": ");
349 SnifferOutput.AppendLine(ChannelNames[i]);
350 }
351 }
352
353 return Result;
354 }
355 }
356
363 public bool TryParseMessage(out ushort ErrorCode, out DiscoveryData Data)
364 {
365 try
366 {
367 ChannelAddress Channel;
368
369 if (this.MessageType == MessageType.Command)
370 {
371 ErrorCode = 0;
372
373 switch (this.DiscoveryService)
374 {
375 case DiscoveryService.NCAPDiscovery:
376 Channel = this.NextAppId();
377 break;
378
379 case DiscoveryService.NCAPTIMDiscovery:
380 Channel = this.NextNcapId(true);
381 break;
382
383 case DiscoveryService.NCAPTIMTransducerDiscovery:
384 Channel = this.NextTimId(true);
385 break;
386
387 default:
388 Data = null;
389 return false;
390 }
391
392 Data = new DiscoveryData(Channel);
393 return true;
394 }
395 else
396 {
397 if (this.MessageType == MessageType.Reply)
398 ErrorCode = this.NextUInt16(nameof(ErrorCode));
399 else
400 ErrorCode = 0;
401
402 switch (this.DiscoveryService)
403 {
404 case DiscoveryService.NCAPAnnouncement:
405 Channel = this.NextNcapId(false);
406 string Name = this.NextString(nameof(Name));
407 AddressType AddressType = this.NextUInt8<AddressType>(nameof(AddressType));
408 byte[] Address = this.NextUInt8Array(nameof(Address));
409 Data = new DiscoveryDataIpEntity(Channel, Name, AddressType, Address);
410 return true;
411
412 case DiscoveryService.NCAPDeparture:
413 Channel = this.NextNcapId(false);
414 Data = new DiscoveryData(Channel);
415 return true;
416
417 case DiscoveryService.NCAPAbandonment:
418 Channel = this.NextNcapId(false);
419 Data = new DiscoveryData(Channel);
420 return true;
421
422 case DiscoveryService.NCAPTIMAnnouncement:
423 Channel = this.NextTimId(false);
424 Name = this.NextString(nameof(Name));
425 Data = new DiscoveryDataEntity(Channel, Name);
426 return true;
427
428 case DiscoveryService.NCAPTIMDeparture:
429 Channel = this.NextTimId(false);
430 Data = new DiscoveryData(Channel);
431 return true;
432
433 case DiscoveryService.NCAPTIMTransducerAnnouncement:
434 Channel = this.NextChannelId(false);
435 Name = this.NextString(nameof(Name));
436 Data = new DiscoveryDataEntity(Channel, Name);
437 return true;
438
439 case DiscoveryService.NCAPTIMTransducerDeparture:
440 Channel = this.NextChannelId(false);
441 Data = new DiscoveryData(Channel);
442 return true;
443
444 case DiscoveryService.NCAPDiscovery:
445 Channel = this.NextNcapId(true);
446 Name = this.NextString(nameof(Name));
447 AddressType = this.NextUInt8<AddressType>(nameof(AddressType));
448 Address = this.NextUInt8Array(nameof(Address));
449 Data = new DiscoveryDataIpEntity(Channel, Name, AddressType, Address);
450 return true;
451
452 case DiscoveryService.NCAPTIMDiscovery:
453 Channel = this.NextNcapId(true);
454 ushort Nr = this.NextUInt16(nameof(Nr));
455 byte[][] Ids = this.NextUuidArray(nameof(Ids), Nr);
456 string[] Names = this.NextStringArray(nameof(Names), Nr);
457 Data = new DiscoveryDataEntities(Channel, Names, Ids);
458 return true;
459
460 case DiscoveryService.NCAPTIMTransducerDiscovery:
461 Channel = this.NextTimId(true);
462 Nr = this.NextUInt16(nameof(Nr));
463 ushort[] Channels = this.NextUInt16Array(nameof(Channels), Nr);
464 Names = this.NextStringArray(nameof(Names), Nr);
465 Data = new DiscoveryDataChannels(Channel, Names, Channels);
466 return true;
467
468 default:
469 Data = null;
470 return false;
471 }
472 }
473 }
474 catch (Exception)
475 {
476 ErrorCode = 0xffff;
477 Data = null;
478
479 return false;
480 }
481 }
482
483 }
484}
Contains methods for simple hash calculations.
Definition: Hashes.cs:59
static string BinaryToString(byte[] Data)
Converts an array of bytes to a string with their hexadecimal representations (in lower case).
Definition: Hashes.cs:65
ushort[] NextUInt16Array(string Name)
Gets the next UInt16 array
Definition: Binary.cs:705
string[] NextStringArray(string Name, int NrItems)
Gets the next string array
Definition: Binary.cs:566
string NextString(string Name)
Gets the next String.
Definition: Binary.cs:421
ICommunicationLayer ComLayer
Sniffable interface on which the message was received.
Definition: Binary.cs:75
byte[] NextUInt8Array(string Name)
Gets the next Byte array
Definition: Binary.cs:532
byte[][] NextUuidArray(string Name)
Gets the next UUID array
Definition: Binary.cs:895
ushort NextUInt16(string Name)
Gets the next ushort.
Definition: Binary.cs:206
static byte[] SerializeRequest(StringBuilder SnifferOutput)
Serializes an NCAP Discovery request.
static byte[] SerializeRequest(byte[] NcapId, byte[] TimId, StringBuilder SnifferOutput)
Serializes an NCAP TIM Transducer Discovery request.
static byte[] SerializeResponse(ushort ErrorCode, byte[] NcapId, string Name, StringBuilder SnifferOutput)
Serializes an NCAP discovery response.
static byte[] SerializeRequest(byte[] NcapId, StringBuilder SnifferOutput)
Serializes an NCAP TIM Discovery request.
static byte[] SerializeResponse(ushort ErrorCode, byte[] NcapId, byte[][] TimIds, string[] TimNames, StringBuilder SnifferOutput)
Serializes a TIM discovery response.
override string NetworkServiceIdName
Name of Message.NetworkServiceId
DiscoveryMessage(NetworkServiceType NetworkServiceType, DiscoveryService DiscoveryService, MessageType MessageType, byte[] Body, byte[] Tail, ICommunicationLayer ComLayer)
IEEE 1451.0 Discovery Message
static byte[] SerializeResponse(ushort ErrorCode, byte[] NcapId, byte[] TimId, ushort[] ChannelIds, string[] ChannelNames, StringBuilder SnifferOutput)
Serializes a Transducer channel discovery response.
bool TryParseMessage(out ushort ErrorCode, out DiscoveryData Data)
Tries to parse a Discovery message.
ChannelAddress NextTimId(bool AppId)
Parses an TIM ID from the message.
ChannelAddress NextAppId()
Parses an Application ID from the message.
ChannelAddress NextChannelId(bool AppId)
Parses a Channel ID from the message.
byte[] Tail
Bytes that are received after the body.
Definition: Message.cs:57
ChannelAddress NextNcapId(bool AppId)
Parses an NCAP ID from the message.
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...
Interface for observable classes implementing communication protocols.
MessageType
Network Service Message Type
Definition: MessageType.cs:7
NetworkServiceType
IEEE 1451.0 Network Service Type