Repository navigation
Expand file tree
/
Copy pathPacketParser.cs
More file actions
483 lines (425 loc) · 14.8 KB
/
Copy pathPacketParser.cs
File metadata and controls
483 lines (425 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
using System.Text;
namespace MeshCS;
/// <summary>
/// Parses MeshCore response packets received from the companion radio.
/// </summary>
public static class PacketParser
{
/// <summary>
/// Gets the response code from a packet.
/// </summary>
public static byte GetCode(ReadOnlySpan<byte> packet) => packet.Length > 0 ? packet[0] : (byte)0xFF;
/// <summary>
/// Checks if this is a push notification (unsolicited event).
/// </summary>
public static bool IsPush(ReadOnlySpan<byte> packet) => packet.Length > 0 && (packet[0] & 0x80) != 0;
/// <summary>
/// Gets the base response code (strips push bit).
/// </summary>
public static byte GetBaseCode(ReadOnlySpan<byte> packet) => (byte)(GetCode(packet) & 0x7F);
/// <summary>
/// Parses a SelfInfo response (0x05).
/// </summary>
public static SelfInfo? ParseSelfInfo(ReadOnlySpan<byte> packet)
{
if (packet.Length < 36 || GetBaseCode(packet) != (byte)Response.SelfInfo)
return null;
var pos = 1;
var pubKey = packet.Slice(pos, 32).ToArray();
pos += 32;
// Optional fields depend on packet length
var lat = 0;
var lon = 0;
uint freq = 0;
byte bw = 0, sf = 0, cr = 0;
sbyte txPower = 0;
var name = "";
if (packet.Length >= pos + 4)
{
lat = ReadInt32LE(packet, ref pos);
}
if (packet.Length >= pos + 4)
{
lon = ReadInt32LE(packet, ref pos);
}
if (packet.Length >= pos + 4)
{
freq = ReadUInt32LE(packet, ref pos);
}
if (packet.Length >= pos + 4)
{
bw = packet[pos++];
sf = packet[pos++];
cr = packet[pos++];
txPower = (sbyte)packet[pos++];
}
if (packet.Length > pos)
{
var nameEnd = packet[pos..].IndexOf((byte)0);
name = nameEnd > 0
? Encoding.UTF8.GetString(packet.Slice(pos, nameEnd))
: Encoding.UTF8.GetString(packet[pos..]);
}
return new SelfInfo
{
PublicKey = pubKey,
Latitude = lat,
Longitude = lon,
Frequency = freq,
Bandwidth = bw,
SpreadingFactor = sf,
CodingRate = cr,
TxPower = txPower,
Name = name.TrimEnd('\0')
};
}
/// <summary>
/// Parses a DeviceInfo response (0x0D).
/// </summary>
public static DeviceInfo? ParseDeviceInfo(ReadOnlySpan<byte> packet)
{
if (packet.Length < 2 || GetBaseCode(packet) != (byte)Response.DeviceInfo)
return null;
var pos = 1;
var version = "";
var model = "";
ushort maxPacket = 230;
byte protoVer = 3;
// Parse null-terminated strings and values
if (packet.Length > pos)
{
var versionEnd = packet[pos..].IndexOf((byte)0);
if (versionEnd > 0)
{
version = Encoding.UTF8.GetString(packet.Slice(pos, versionEnd));
pos += versionEnd + 1;
}
}
if (packet.Length > pos)
{
var modelEnd = packet[pos..].IndexOf((byte)0);
if (modelEnd > 0)
{
model = Encoding.UTF8.GetString(packet.Slice(pos, modelEnd));
pos += modelEnd + 1;
}
}
if (packet.Length >= pos + 2)
{
maxPacket = ReadUInt16LE(packet, ref pos);
}
if (packet.Length > pos)
{
protoVer = packet[pos];
}
return new DeviceInfo
{
FirmwareVersion = version,
Model = model,
MaxPacketSize = maxPacket,
ProtocolVersion = protoVer
};
}
/// <summary>
/// Parses a Contact response (0x03).
/// </summary>
public static Contact? ParseContact(ReadOnlySpan<byte> packet)
{
// Contact: code(1) + pubkey(32) + type(1) + flags(1) + pathLen(1) + path(16) + name(32) + lastSeen(4)
if (packet.Length < 88 || GetBaseCode(packet) != (byte)Response.Contact)
return null;
var pos = 1;
var pubKey = packet.Slice(pos, 32).ToArray();
pos += 32;
var type = (ContactType)packet[pos++];
var flags = packet[pos++];
var pathLen = packet[pos++];
var path = packet.Slice(pos, 16).ToArray();
pos += 16;
var nameBytes = packet.Slice(pos, 32);
pos += 32;
var lastSeen = ReadUInt32LE(packet, ref pos);
var nameEnd = nameBytes.IndexOf((byte)0);
var name = nameEnd > 0
? Encoding.UTF8.GetString(nameBytes[..nameEnd])
: Encoding.UTF8.GetString(nameBytes);
return new Contact
{
PublicKey = pubKey,
Type = type,
Flags = flags,
PathLen = pathLen,
Path = path,
Name = name.TrimEnd('\0'),
LastSeen = lastSeen
};
}
/// <summary>
/// Parses a ChannelInfo response (0x12).
/// </summary>
public static Channel? ParseChannelInfo(ReadOnlySpan<byte> packet)
{
// ChannelInfo: code(1) + index(1) + name(32) + secret(16) + [flags(1) optional]
// Some firmware versions omit the flags byte
if (packet.Length < 50 || GetBaseCode(packet) != (byte)Response.ChannelInfo)
return null;
var pos = 1;
var index = packet[pos++];
var nameBytes = packet.Slice(pos, 32);
pos += 32;
var secret = packet.Slice(pos, 16).ToArray();
pos += 16;
var forwardEnabled = packet.Length > pos && (packet[pos] & 0x01) != 0;
var nameEnd = nameBytes.IndexOf((byte)0);
var name = nameEnd > 0
? Encoding.UTF8.GetString(nameBytes[..nameEnd])
: Encoding.UTF8.GetString(nameBytes);
return new Channel
{
Index = index,
Name = name.TrimEnd('\0'),
Secret = secret,
ForwardEnabled = forwardEnabled
};
}
/// <summary>
/// Parses a DirectMessage (0x06, 0x10, 0x86, 0x90).
/// </summary>
public static DirectMessage? ParseDirectMessage(ReadOnlySpan<byte> packet)
{
var code = GetBaseCode(packet);
if (code != (byte)Response.ContactMsg && code != (byte)Response.ContactMsgV3)
return null;
var isV3 = code == (byte)Response.ContactMsgV3;
var pos = 1;
// V3: code(1) + txtType(1) + senderPubKey(6) + pathLen(1) + senderTs(4) + snr(1) + nameLen(1) + name... + payload
// V2: code(1) + senderPubKey(6) + pathLen(1) + txtType(1) + senderTs(4) + payload
byte[] senderPrefix;
TextType textType;
byte pathLen;
uint timestamp;
sbyte snr = 0;
string senderName = "";
byte[] payload;
if (isV3)
{
if (packet.Length < 16) return null;
textType = (TextType)packet[pos++];
senderPrefix = packet.Slice(pos, 6).ToArray();
pos += 6;
pathLen = packet[pos++];
timestamp = ReadUInt32LE(packet, ref pos);
snr = (sbyte)packet[pos++];
var nameLen = packet[pos++];
if (nameLen > 0 && packet.Length >= pos + nameLen)
{
senderName = Encoding.UTF8.GetString(packet.Slice(pos, nameLen));
pos += nameLen;
}
payload = packet[pos..].ToArray();
}
else
{
if (packet.Length < 13) return null;
senderPrefix = packet.Slice(pos, 6).ToArray();
pos += 6;
pathLen = packet[pos++];
textType = (TextType)packet[pos++];
timestamp = ReadUInt32LE(packet, ref pos);
payload = packet[pos..].ToArray();
}
return new DirectMessage
{
SenderPrefix = senderPrefix,
TextType = textType,
PathLen = pathLen,
Timestamp = timestamp,
Snr = snr,
SenderName = senderName.TrimEnd('\0'),
Payload = payload,
IsV3 = isV3
};
}
/// <summary>
/// Parses a ChannelMessage (0x08, 0x11, 0x88, 0x91).
/// </summary>
public static ChannelMessage? ParseChannelMessage(ReadOnlySpan<byte> packet)
{
var code = GetBaseCode(packet);
if (code != (byte)Response.ChannelMsg && code != (byte)Response.ChannelMsgV3)
return null;
var isV3 = code == (byte)Response.ChannelMsgV3;
var pos = 1;
// V3: code(1) + txtType(1) + channelIdx(1) + pathLen(1) + senderTs(4) + snr(1) + nameLen(1) + name... + payload
// V2: code(1) + channelIdx(1) + pathLen(1) + txtType(1) + senderTs(4) + senderPubKey(32) + ": " + text
byte channelIndex;
TextType textType;
byte pathLen;
uint timestamp;
sbyte snr = 0;
string senderName = "";
byte[] senderPubKey = [];
byte[] payload;
if (isV3)
{
if (packet.Length < 11) return null;
textType = (TextType)packet[pos++];
channelIndex = packet[pos++];
pathLen = packet[pos++];
timestamp = ReadUInt32LE(packet, ref pos);
snr = (sbyte)packet[pos++];
var nameLen = packet[pos++];
if (nameLen > 0 && packet.Length >= pos + nameLen)
{
senderName = Encoding.UTF8.GetString(packet.Slice(pos, nameLen));
pos += nameLen;
}
payload = packet[pos..].ToArray();
}
else
{
if (packet.Length < 8) return null;
channelIndex = packet[pos++];
pathLen = packet[pos++];
textType = (TextType)packet[pos++];
timestamp = ReadUInt32LE(packet, ref pos);
// V2 payload format: senderBlob + ": " (0x3A 0x20) + text
// The sender blob is a variable-length identity (pubkey hash/suffix).
// Search for the ": " separator to split sender from message.
var rawPayload = packet[pos..];
var separatorIdx = -1;
for (var i = 0; i < rawPayload.Length - 1; i++)
{
if (rawPayload[i] == (byte)':' && rawPayload[i + 1] == (byte)' ')
{
separatorIdx = i;
break;
}
}
if (separatorIdx > 0)
{
senderPubKey = rawPayload[..separatorIdx].ToArray();
payload = rawPayload[(separatorIdx + 2)..].ToArray();
}
else
{
payload = rawPayload.ToArray();
}
}
return new ChannelMessage
{
ChannelIndex = channelIndex,
TextType = textType,
PathLen = pathLen,
Timestamp = timestamp,
Snr = snr,
SenderName = senderName.TrimEnd('\0'),
SenderPubKey = senderPubKey,
Payload = payload,
IsV3 = isV3
};
}
/// <summary>
/// Parses an Advert push (0x80).
/// </summary>
public static Advert? ParseAdvert(ReadOnlySpan<byte> packet)
{
if (packet.Length < 43 || GetCode(packet) != (byte)Push.Advert)
return null;
var pos = 1;
var pubKey = packet.Slice(pos, 32).ToArray();
pos += 32;
var lat = ReadInt32LE(packet, ref pos);
var lon = ReadInt32LE(packet, ref pos);
var pathLen = packet[pos++];
var snr = (sbyte)packet[pos++];
var name = "";
if (packet.Length > pos)
{
var nameEnd = packet[pos..].IndexOf((byte)0);
name = nameEnd > 0
? Encoding.UTF8.GetString(packet.Slice(pos, nameEnd))
: Encoding.UTF8.GetString(packet[pos..]);
}
return new Advert
{
PublicKey = pubKey,
Latitude = lat,
Longitude = lon,
PathLen = pathLen,
Snr = snr,
Name = name.TrimEnd('\0')
};
}
/// <summary>
/// Parses a Time response (0x09).
/// </summary>
public static uint? ParseTime(ReadOnlySpan<byte> packet)
{
if (packet.Length < 5 || GetBaseCode(packet) != (byte)Response.Time)
return null;
var pos = 1;
return ReadUInt32LE(packet, ref pos);
}
/// <summary>
/// Parses a Battery response packet.
/// </summary>
public static BatteryInfo? ParseBatteryInfo(ReadOnlySpan<byte> packet)
{
if (packet.Length < 5 || GetBaseCode(packet) != (byte)Response.Battery)
return null;
var pos = 1;
var voltage = ReadUInt16LE(packet, ref pos);
var percentage = packet[pos++];
var isCharging = packet.Length > pos && packet[pos++] != 0;
var freeStorage = packet.Length >= pos + 4 ? ReadUInt32LE(packet, ref pos) : 0u;
return new BatteryInfo
{
VoltageMillivolts = voltage,
Percentage = percentage,
IsCharging = isCharging,
FreeStorageBytes = freeStorage
};
}
/// <summary>
/// Checks if this is a Sent acknowledgment (0x07).
/// </summary>
public static bool IsSent(ReadOnlySpan<byte> packet) =>
packet.Length > 0 && GetBaseCode(packet) == (byte)Response.Sent;
/// <summary>
/// Checks if this is an Error response (0x01).
/// </summary>
public static bool IsError(ReadOnlySpan<byte> packet) =>
packet.Length > 0 && GetBaseCode(packet) == (byte)Response.Err;
/// <summary>
/// Checks if this is an Ok response (0x00).
/// </summary>
public static bool IsOk(ReadOnlySpan<byte> packet) =>
packet.Length > 0 && GetBaseCode(packet) == (byte)Response.Ok;
/// <summary>
/// Checks if this is a NoMoreMessages response (0x0A).
/// </summary>
public static bool IsNoMoreMessages(ReadOnlySpan<byte> packet) =>
packet.Length > 0 && GetBaseCode(packet) == (byte)Response.NoMoreMsgs;
/// <summary>
/// Checks if this is a MessageWaiting push (0x83).
/// </summary>
public static bool IsMessageWaiting(ReadOnlySpan<byte> packet) =>
packet.Length > 0 && GetCode(packet) == (byte)Push.MsgWaiting;
private static uint ReadUInt32LE(ReadOnlySpan<byte> data, ref int pos)
{
var value = (uint)(data[pos] | (data[pos + 1] << 8) | (data[pos + 2] << 16) | (data[pos + 3] << 24));
pos += 4;
return value;
}
private static int ReadInt32LE(ReadOnlySpan<byte> data, ref int pos)
{
return (int)ReadUInt32LE(data, ref pos);
}
private static ushort ReadUInt16LE(ReadOnlySpan<byte> data, ref int pos)
{
var value = (ushort)(data[pos] | (data[pos + 1] << 8));
pos += 2;
return value;
}
}