-
-
Notifications
You must be signed in to change notification settings - Fork 1
PacketParser
Static class for parsing MeshCore response packets received from the companion radio.
public static class PacketParserstatic
public static byte GetCode(ReadOnlySpan<byte> packet)Gets the response code byte from a packet.
| Parameter | Type | Description |
|---|---|---|
| packet | ReadOnlySpan<byte> |
Raw packet data |
Returns: byte - The response/push code.
static
public static byte GetBaseCode(ReadOnlySpan<byte> packet)Gets the base response code (without flags).
| Parameter | Type | Description |
|---|---|---|
| packet | ReadOnlySpan<byte> |
Raw packet data |
Returns: byte - Base code value.
static
public static bool IsPush(ReadOnlySpan<byte> packet)Checks if packet is a push notification (unsolicited message).
| Parameter | Type | Description |
|---|---|---|
| packet | ReadOnlySpan<byte> |
Raw packet data |
Returns: bool - true if push notification.
static
public static bool IsOk(ReadOnlySpan<byte> packet)Checks if packet indicates success.
| Parameter | Type | Description |
|---|---|---|
| packet | ReadOnlySpan<byte> |
Raw packet data |
Returns: bool - true if OK response.
static
public static bool IsError(ReadOnlySpan<byte> packet)Checks if packet indicates an error.
| Parameter | Type | Description |
|---|---|---|
| packet | ReadOnlySpan<byte> |
Raw packet data |
Returns: bool - true if error response.
static
public static bool IsSent(ReadOnlySpan<byte> packet)Checks if packet confirms a message was sent.
| Parameter | Type | Description |
|---|---|---|
| packet | ReadOnlySpan<byte> |
Raw packet data |
Returns: bool - true if sent confirmation.
static
public static bool IsMessageWaiting(ReadOnlySpan<byte> packet)Checks if packet indicates messages are waiting.
| Parameter | Type | Description |
|---|---|---|
| packet | ReadOnlySpan<byte> |
Raw packet data |
Returns: bool - true if messages pending.
static
public static bool IsNoMoreMessages(ReadOnlySpan<byte> packet)Checks if packet indicates no more messages.
| Parameter | Type | Description |
|---|---|---|
| packet | ReadOnlySpan<byte> |
Raw packet data |
Returns: bool - true if queue empty.
static
public static SelfInfo? ParseSelfInfo(ReadOnlySpan<byte> packet)Parses device self-information from AppStart response.
| Parameter | Type | Description |
|---|---|---|
| packet | ReadOnlySpan<byte> |
Raw packet data |
Returns: SelfInfo or null if invalid.
static
public static DeviceInfo? ParseDeviceInfo(ReadOnlySpan<byte> packet)Parses firmware/model information from DeviceQuery response.
| Parameter | Type | Description |
|---|---|---|
| packet | ReadOnlySpan<byte> |
Raw packet data |
Returns: DeviceInfo or null if invalid.
static
public static Contact? ParseContact(ReadOnlySpan<byte> packet)Parses a contact entry from GetContacts response.
| Parameter | Type | Description |
|---|---|---|
| packet | ReadOnlySpan<byte> |
Raw packet data |
Returns: Contact or null if invalid.
static
public static DirectMessage? ParseDirectMessage(ReadOnlySpan<byte> packet)Parses a direct message from GetMsg or push notification.
| Parameter | Type | Description |
|---|---|---|
| packet | ReadOnlySpan<byte> |
Raw packet data |
Returns: DirectMessage or null if invalid.
static
public static ChannelMessage? ParseChannelMessage(ReadOnlySpan<byte> packet)Parses a channel message from push notification.
| Parameter | Type | Description |
|---|---|---|
| packet | ReadOnlySpan<byte> |
Raw packet data |
Returns: ChannelMessage or null if invalid.
static
public static Advert? ParseAdvert(ReadOnlySpan<byte> packet)Parses an advertisement from push notification.
| Parameter | Type | Description |
|---|---|---|
| packet | ReadOnlySpan<byte> |
Raw packet data |
Returns: Advert or null if invalid.
static
public static Channel? ParseChannelInfo(ReadOnlySpan<byte> packet)Parses channel configuration from GetChannel response.
| Parameter | Type | Description |
|---|---|---|
| packet | ReadOnlySpan<byte> |
Raw packet data |
Returns: Channel or null if invalid.
static
public static BatteryInfo? ParseBatteryInfo(ReadOnlySpan<byte> packet)Parses battery status from GetBattery response.
| Parameter | Type | Description |
|---|---|---|
| packet | ReadOnlySpan<byte> |
Raw packet data |
Returns: BatteryInfo or null if invalid.
static
public static DateTime? ParseTime(ReadOnlySpan<byte> packet)Parses device time from GetTime response.
| Parameter | Type | Description |
|---|---|---|
| packet | ReadOnlySpan<byte> |
Raw packet data |
Returns: DateTime or null if invalid.
using MeshCS;
// Low-level packet parsing example
byte[] receivedData = /* from serial port */;
// Check packet type
if (PacketParser.IsPush(receivedData))
{
var baseCode = PacketParser.GetBaseCode(receivedData);
if (baseCode == (byte)Push.NewMsg)
{
var msg = PacketParser.ParseDirectMessage(receivedData);
if (msg != null)
{
Console.WriteLine($"Message from {msg.PubKeyPrefix}: {msg.Text}");
}
}
else if (baseCode == (byte)Push.Advert)
{
var advert = PacketParser.ParseAdvert(receivedData);
if (advert != null)
{
Console.WriteLine($"Advert from {advert.Name}");
}
}
}
else if (PacketParser.IsOk(receivedData))
{
Console.WriteLine("Command succeeded");
}
else if (PacketParser.IsError(receivedData))
{
Console.WriteLine("Command failed");
}- MeshRadio - High-level API (recommended)
- PacketBuilder - Build command packets
- Response - Response code enum
- Push - Push notification enum
MeshCS © 2026 Litruv | MIT License
MeshCS Documentation
Classes
Models
Enums
Links