Skip to content

PacketParser

Max Litruv Boonzaayer edited this page Feb 15, 2026 · 2 revisions

Static class for parsing MeshCore response packets received from the companion radio.

public static class PacketParser

Table of Contents

🔍 Code Detection

⚙️ Parse Methods


Code Detection Methods

GetCode

static

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.


GetBaseCode

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.


IsPush

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.


IsOk

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.


IsError

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.


IsSent

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.


IsMessageWaiting

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.


IsNoMoreMessages

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.


Parse Methods

ParseSelfInfo

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.


ParseDeviceInfo

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.


ParseContact

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.


ParseDirectMessage

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.


ParseChannelMessage

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.


ParseAdvert

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.


ParseChannelInfo

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.


ParseBatteryInfo

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.


ParseTime

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.


Example

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");
}

See Also