Files
meshcore-tth/companion_protocol/index.html
T

1654 lines
84 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="generator" content="mkdocs-1.6.1, mkdocs-terminal-4.8.0">
<meta name="description" content="MeshCore fork meshcore-tth -- repeater antispam, diagnostics, OTA, Czech mesh defaults">
<link rel="canonical" href="https://tth-pub.pages.rfa.cz/meshcore-tth/companion_protocol/"><link rel="icon" type="image/png" sizes="192x192" href="../img/android-chrome-192x192.png" />
<link rel="icon" type="image/png" sizes="512x512" href="../img/android-chrome-512x512.png" />
<link rel="apple-touch-icon" sizes="180x180" href="../img/apple-touch-icon.png" />
<link rel="shortcut icon" type="image/png" sizes="48x48" href="../img/favicon.ico" />
<link rel="icon" type="image/png" sizes="16x16" href="../img/favicon-16x16.png" />
<link rel="icon" type="image/png" sizes="32x32" href="../img/favicon-32x32.png" />
<title>Companion protocol - MeshCore TTH</title>
<link href="../css/fontawesome/css/fontawesome.min.css" rel="stylesheet">
<link href="../css/fontawesome/css/solid.min.css" rel="stylesheet">
<link href="../css/normalize.css" rel="stylesheet">
<link href="../css/terminal.css" rel="stylesheet">
<link href="../css/theme.css" rel="stylesheet">
<link href="../css/theme.tile_grid.css" rel="stylesheet">
<link href="../css/theme.footer.css" rel="stylesheet">
<!-- dark color palette -->
<link href="../css/palettes/dark.css" rel="stylesheet">
<!-- page layout -->
<style>
/* initially set page layout to a one column grid */
.terminal-mkdocs-main-grid {
display: grid;
grid-column-gap: 1.4em;
grid-template-columns: auto;
grid-template-rows: auto;
}
/*
* when side navigation is not hidden, use a two column grid.
* if the screen is too narrow, fall back to the initial one column grid layout.
* in this case the main content will be placed under the navigation panel.
*/
@media only screen and (min-width: 70em) {
.terminal-mkdocs-main-grid {
grid-template-columns: 4fr 9fr;
}
}</style>
<!-- cursor_animation override -->
<style>
#mkdocs-terminal-site-name.terminal-prompt::after {
display: none;
}
</style>
<link href="../_stylesheets/tth.css" rel="stylesheet">
<!-- search css support -->
<link href="../css/search/bootstrap-modal.css" rel="stylesheet">
<!-- search scripts -->
<script>
var base_url = "..",
shortcuts = "{}";
</script>
<script src="../js/jquery/jquery-1.10.1.min.js" defer></script>
<script src="../js/bootstrap/bootstrap.min.js" defer></script>
<script src="../js/mkdocs/base.js" defer></script>
<script src="../search/main.js"></script>
</head>
<body class="terminal"><div class="container">
<div class="terminal-nav">
<header class="terminal-logo">
<div id="mkdocs-terminal-site-name" class="logo terminal-prompt"><a href="https://tth-pub.pages.rfa.cz/meshcore-tth/" class="no-style">MeshCore TTH</a></div>
</header>
<nav class="terminal-menu">
<ul vocab="https://schema.org/" typeof="BreadcrumbList">
<li property="itemListElement" typeof="ListItem">
<a href=".." class="menu-item " property="item" typeof="WebPage">
<span property="name">O forku</span>
</a>
<meta property="position" content="0">
</li>
<li property="itemListElement" typeof="ListItem">
<a href="../en/" class="menu-item " property="item" typeof="WebPage">
<span property="name">English</span>
</a>
<meta property="position" content="1">
</li>
<li property="itemListElement" typeof="ListItem">
<a href="#" class="menu-item" data-toggle="modal" data-target="#mkdocs_search_modal" property="item" typeof="SearchAction">
<i aria-hidden="true" class="fa fa-search"></i> <span property="name">Search</span>
</a>
<meta property="position" content="2">
</li>
</ul>
</nav>
</div>
</div>
<div class="container">
<div class="terminal-mkdocs-main-grid"><aside id="terminal-mkdocs-side-panel"><nav>
<ul class="terminal-mkdocs-side-nav-items">
<li class="terminal-mkdocs-side-nav-li">
<a class="terminal-mkdocs-side-nav-item" href="..">O forku</a>
</li>
<li class="terminal-mkdocs-side-nav-li">
<a class="terminal-mkdocs-side-nav-item" href="../en/">English</a>
</li>
<li class="terminal-mkdocs-side-nav-li">
<span class="
terminal-mkdocs-side-nav-item terminal-mkdocs-side-nav-section-no-index">Fork meshcore-tth</span>
<ul class="terminal-mkdocs-side-nav-li-ul">
<li class="terminal-mkdocs-side-nav-li-ul-li">
<a class="terminal-mkdocs-side-nav-item" href="../tth-commands/">Příkazy konzole · console commands</a>
</li>
<li class="terminal-mkdocs-side-nav-li-ul-li">
<a class="terminal-mkdocs-side-nav-item" href="../ota/">Aktualizace od admina · OTA</a>
</li>
<li class="terminal-mkdocs-side-nav-li-ul-li">
<a class="terminal-mkdocs-side-nav-item" href="../power/">Napájení · power</a>
</li>
<li class="terminal-mkdocs-side-nav-li-ul-li">
<a class="terminal-mkdocs-side-nav-item" href="../budka-sensor/">Budka jako senzor · Budka sensor</a>
</li>
<li class="terminal-mkdocs-side-nav-li-ul-li">
<a class="terminal-mkdocs-side-nav-item" href="../ble-lora-coexistence/">Koexistence BLE/LoRa</a>
</li>
</ul>
</li>
<li class="terminal-mkdocs-side-nav-li">
<span class="
terminal-mkdocs-side-nav-item--active terminal-mkdocs-side-nav-section-no-index">MeshCore (upstream)</span>
<ul class="terminal-mkdocs-side-nav-li-ul">
<li class="terminal-mkdocs-side-nav-li-ul-li">
<a class="terminal-mkdocs-side-nav-item" href="../meshcore/">Introduction</a>
</li>
<li class="terminal-mkdocs-side-nav-li-ul-li">
<a class="terminal-mkdocs-side-nav-item" href="../faq/">FAQ</a>
</li>
<li class="terminal-mkdocs-side-nav-li-ul-li">
<a class="terminal-mkdocs-side-nav-item" href="../cli_commands/">CLI commands</a>
</li>
<li class="terminal-mkdocs-side-nav-li-ul-li">
<a class="terminal-mkdocs-side-nav-item" href="../companion_cli/">Companion CLI</a>
</li>
<li class="terminal-mkdocs-side-nav-li-ul-li">
<span class="terminal-mkdocs-side-nav-item--active">Companion protocol</span>
</li>
<li class="terminal-mkdocs-side-nav-li-ul-li">
<a class="terminal-mkdocs-side-nav-item" href="../terminal_chat_cli/">Terminal chat CLI</a>
</li>
<li class="terminal-mkdocs-side-nav-li-ul-li">
<a class="terminal-mkdocs-side-nav-item" href="../kiss_modem_protocol/">KISS modem protocol</a>
</li>
<li class="terminal-mkdocs-side-nav-li-ul-li">
<a class="terminal-mkdocs-side-nav-item" href="../packet_format/">Packet format</a>
</li>
<li class="terminal-mkdocs-side-nav-li-ul-li">
<a class="terminal-mkdocs-side-nav-item" href="../payloads/">Payloads</a>
</li>
<li class="terminal-mkdocs-side-nav-li-ul-li">
<a class="terminal-mkdocs-side-nav-item" href="../stats_binary_frames/">Stats binary frames</a>
</li>
<li class="terminal-mkdocs-side-nav-li-ul-li">
<a class="terminal-mkdocs-side-nav-item" href="../number_allocations/">Number allocations</a>
</li>
<li class="terminal-mkdocs-side-nav-li-ul-li">
<a class="terminal-mkdocs-side-nav-item" href="../qr_codes/">QR codes</a>
</li>
<li class="terminal-mkdocs-side-nav-li-ul-li">
<a class="terminal-mkdocs-side-nav-item" href="../radio_presets/">Radio presets</a>
</li>
<li class="terminal-mkdocs-side-nav-li-ul-li">
<a class="terminal-mkdocs-side-nav-item" href="../nrf52_power_management/">nRF52 power management</a>
</li>
</ul>
</li>
</ul>
</nav><hr>
<nav>
<ul>
<li><a href="#companion-protocol">Companion Protocol</a></li>
<li><a href="#official-libraries">Official Libraries</a></li><li><a href="#important-security-note">Important Security Note</a></li><li><a href="#table-of-contents">Table of Contents</a></li><li><a href="#ble-connection">BLE Connection</a></li><li><a href="#packet-structure">Packet Structure</a></li><li><a href="#commands">Commands</a></li><li><a href="#channel-management">Channel Management</a></li><li><a href="#message-handling">Message Handling</a></li><li><a href="#response-parsing">Response Parsing</a></li><li><a href="#example-implementation-flow">Example Implementation Flow</a></li><li><a href="#best-practices">Best Practices</a></li><li><a href="#troubleshooting">Troubleshooting</a></li>
</ul>
</nav>
</aside>
<main id="terminal-mkdocs-main-content">
<section id="mkdocs-terminal-content">
<h1 id="companion-protocol">Companion Protocol</h1>
<ul>
<li><strong>Last Updated</strong>: 2026-03-08</li>
<li><strong>Protocol Version</strong>: Companion Firmware v1.12.0+</li>
</ul>
<blockquote>
<p>NOTE: This document is still in development. Some information may be inaccurate.</p>
</blockquote>
<p>This document provides a comprehensive guide for communicating with MeshCore devices over Bluetooth Low Energy (BLE).</p>
<p>It is platform-agnostic and can be used for Android, iOS, Python, JavaScript, or any other platform that supports BLE.</p>
<h2 id="official-libraries">Official Libraries</h2>
<p>Please see the following repos for existing MeshCore Companion Protocol libraries.</p>
<ul>
<li>JavaScript: <a href="https://github.com/meshcore-dev/meshcore.js">https://github.com/meshcore-dev/meshcore.js</a></li>
<li>Python: <a href="https://github.com/meshcore-dev/meshcore_py">https://github.com/meshcore-dev/meshcore_py</a></li>
</ul>
<h2 id="important-security-note">Important Security Note</h2>
<p>All secrets, hashes, and cryptographic values shown in this guide are example values only.</p>
<ul>
<li>All hex values, public keys and hashes are for demonstration purposes only</li>
<li>Never use example secrets in production</li>
<li>Always generate new cryptographically secure random secrets</li>
<li>Please implement proper security practices in your implementation</li>
<li>This guide is for protocol documentation only</li>
</ul>
<h2 id="table-of-contents">Table of Contents</h2>
<ol>
<li><a href="#ble-connection">BLE Connection</a></li>
<li><a href="#packet-structure">Packet Structure</a></li>
<li><a href="#commands">Commands</a></li>
<li><a href="#channel-management">Channel Management</a></li>
<li><a href="#message-handling">Message Handling</a></li>
<li><a href="#response-parsing">Response Parsing</a></li>
<li><a href="#example-implementation-flow">Example Implementation Flow</a></li>
<li><a href="#best-practices">Best Practices</a></li>
<li><a href="#troubleshooting">Troubleshooting</a></li>
</ol>
<hr />
<h2 id="ble-connection">BLE Connection</h2>
<h3 id="service-and-characteristics">Service and Characteristics</h3>
<p>MeshCore Companion devices expose a BLE service with the following UUIDs:</p>
<ul>
<li><strong>Service UUID</strong>: <code>6E400001-B5A3-F393-E0A9-E50E24DCCA9E</code></li>
<li><strong>RX Characteristic</strong> (App → Firmware): <code>6E400002-B5A3-F393-E0A9-E50E24DCCA9E</code></li>
<li><strong>TX Characteristic</strong> (Firmware → App): <code>6E400003-B5A3-F393-E0A9-E50E24DCCA9E</code></li>
</ul>
<h3 id="connection-steps">Connection Steps</h3>
<ol>
<li>
<p><strong>Scan for Devices</strong></p>
<ul>
<li>Scan for BLE devices advertising the MeshCore Service UUID</li>
<li>Optionally filter by device name (typically contains "MeshCore" prefix)</li>
<li>Note the device MAC address for reconnection</li>
</ul>
</li>
<li>
<p><strong>Connect to GATT</strong></p>
<ul>
<li>Connect to the device using the discovered MAC address</li>
<li>Wait for connection to be established</li>
</ul>
</li>
<li>
<p><strong>Discover Services and Characteristics</strong></p>
<ul>
<li>Discover the service with UUID <code>6E400001-B5A3-F393-E0A9-E50E24DCCA9E</code></li>
<li>Discover the RX characteristic <code>6E400002-B5A3-F393-E0A9-E50E24DCCA9E</code><ul>
<li>Your app writes to this, the firmware reads from this</li>
</ul>
</li>
<li>Discover the TX characteristic <code>6E400003-B5A3-F393-E0A9-E50E24DCCA9E</code><ul>
<li>The firmware writes to this, your app reads from this</li>
</ul>
</li>
</ul>
</li>
<li>
<p><strong>Enable Notifications</strong></p>
<ul>
<li>Subscribe to notifications on the TX characteristic to receive data from the firmware</li>
</ul>
</li>
<li>
<p><strong>Send Initial Commands</strong></p>
<ul>
<li>Send <code>CMD_APP_START</code> to identify your app to firmware and get radio settings</li>
<li>Send <code>CMD_DEVICE_QUERY</code> to fetch device info and negotiate supported protocol versions</li>
<li>Send <code>CMD_SET_DEVICE_TIME</code> to set the firmware clock</li>
<li>Send <code>CMD_GET_CONTACTS</code> to fetch all contacts</li>
<li>Send <code>CMD_GET_CHANNEL</code> multiple times to fetch all channel slots</li>
<li>Send <code>CMD_SYNC_NEXT_MESSAGE</code> to fetch the next message stored in firmware</li>
<li>Setup listeners for push codes, such as <code>PUSH_CODE_MSG_WAITING</code> or <code>PUSH_CODE_ADVERT</code></li>
<li>See <a href="#commands">Commands</a> section for information on other commands</li>
</ul>
</li>
</ol>
<p><strong>Note</strong>: MeshCore devices may disconnect after periods of inactivity. Implement auto-reconnect logic with exponential backoff.</p>
<h3 id="ble-write-type">BLE Write Type</h3>
<p>When writing commands to the RX characteristic, specify the write type:</p>
<ul>
<li><strong>Write with Response</strong> (default): Waits for acknowledgment from device</li>
<li><strong>Write without Response</strong>: Faster but no acknowledgment</li>
</ul>
<p><strong>Platform-specific</strong>:</p>
<ul>
<li><strong>Android</strong>: Use <code>BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT</code> or <code>WRITE_TYPE_NO_RESPONSE</code></li>
<li><strong>iOS</strong>: Use <code>CBCharacteristicWriteType.withResponse</code> or <code>.withoutResponse</code></li>
<li><strong>Python (bleak)</strong>: Use <code>write_gatt_char()</code> with <code>response=True</code> or <code>False</code></li>
</ul>
<p><strong>Recommendation</strong>: Use write with response for reliability.</p>
<h3 id="mtu-maximum-transmission-unit">MTU (Maximum Transmission Unit)</h3>
<p>The default BLE MTU is 23 bytes (20 bytes payload). For larger commands like <code>SET_CHANNEL</code> (50 bytes), you may need to:</p>
<ol>
<li><strong>Request Larger MTU</strong>: Request MTU of 512 bytes if supported<ul>
<li>Android: <code>gatt.requestMtu(512)</code></li>
<li>iOS: <code>peripheral.maximumWriteValueLength(for:)</code></li>
<li>Python (bleak): MTU is negotiated automatically</li>
</ul>
</li>
</ol>
<h3 id="command-sequencing">Command Sequencing</h3>
<p><strong>Critical</strong>: Commands must be sent in the correct sequence:</p>
<ol>
<li>
<p><strong>After Connection</strong>:</p>
<ul>
<li>Wait for BLE connection to be established</li>
<li>Wait for services/characteristics to be discovered</li>
<li>Wait for notifications to be enabled</li>
<li>Now you can safely send commands to the firmware</li>
</ul>
</li>
<li>
<p><strong>Command-Response Matching</strong>:</p>
<ul>
<li>Send one command at a time</li>
<li>Wait for a response before sending another command</li>
<li>Use a timeout (typically 5 seconds)</li>
<li>Match response to command by type (e.g: <code>CMD_GET_CHANNEL</code> → <code>RESP_CODE_CHANNEL_INFO</code>)</li>
</ul>
</li>
</ol>
<h3 id="command-queue-management">Command Queue Management</h3>
<p>For reliable operation, implement a command queue.</p>
<p><strong>Queue Structure</strong>:</p>
<ul>
<li>Maintain a queue of pending commands</li>
<li>Track which command is currently waiting for a response</li>
<li>Only send next command after receiving response or timeout</li>
</ul>
<p><strong>Error Handling</strong>:</p>
<ul>
<li>On timeout, clear current command, process next in queue</li>
<li>On error, log error, clear current command, process next</li>
</ul>
<hr />
<h2 id="packet-structure">Packet Structure</h2>
<p>The MeshCore protocol uses a binary format with the following structure:</p>
<ul>
<li><strong>Commands</strong>: Sent from app to firmware via RX characteristic</li>
<li><strong>Responses</strong>: Received from firmware via TX characteristic notifications</li>
<li><strong>All multi-byte integers</strong>: Little-endian byte order (except CayenneLPP which is Big-endian)</li>
<li><strong>All strings</strong>: UTF-8 encoding</li>
</ul>
<p>Most packets follow this format:
<div class="highlight"><pre><span></span><code>[Packet Type (1 byte)] [Data (variable length)]
</code></pre></div></p>
<p>The first byte indicates the packet type (see <a href="#response-parsing">Response Parsing</a>).</p>
<hr />
<h2 id="commands">Commands</h2>
<h3 id="1-app-start">1. App Start</h3>
<p><strong>Purpose</strong>: Initialize communication with the device. Must be sent first after connection.</p>
<p><strong>Command Format</strong>:
<div class="highlight"><pre><span></span><code>Byte 0: 0x01
Bytes 1-7: Reserved (currently ignored by firmware)
Bytes 8+: Application name (UTF-8, optional)
</code></pre></div></p>
<p><strong>Example</strong> (hex):
<div class="highlight"><pre><span></span><code>01 00 00 00 00 00 00 00 6d 63 63 6c 69
</code></pre></div></p>
<p><strong>Response</strong>: <code>PACKET_SELF_INFO</code> (0x05)</p>
<hr />
<h3 id="2-device-query">2. Device Query</h3>
<p><strong>Purpose</strong>: Query device information.</p>
<p><strong>Command Format</strong>:
<div class="highlight"><pre><span></span><code>Byte 0: 0x16
Byte 1: 0x03
</code></pre></div></p>
<p><strong>Example</strong> (hex):
<div class="highlight"><pre><span></span><code>16 03
</code></pre></div></p>
<p><strong>Response</strong>: <code>PACKET_DEVICE_INFO</code> (0x0D) with device information</p>
<hr />
<h3 id="3-get-channel-info">3. Get Channel Info</h3>
<p><strong>Purpose</strong>: Retrieve information about a specific channel.</p>
<p><strong>Command Format</strong>:
<div class="highlight"><pre><span></span><code>Byte 0: 0x1F
Byte 1: Channel Index (0 .. max_channels-1)
</code></pre></div></p>
<p><strong>Example</strong> (get channel 1):
<div class="highlight"><pre><span></span><code>1F 01
</code></pre></div></p>
<p><strong>Response</strong>: <code>PACKET_CHANNEL_INFO</code> (0x12) with channel details</p>
<hr />
<h3 id="4-set-channel">4. Set Channel</h3>
<p><strong>Purpose</strong>: Create or update a channel on the device.</p>
<p><strong>Command Format</strong>:
<div class="highlight"><pre><span></span><code>Byte 0: 0x20
Byte 1: Channel Index (0 .. max_channels-1)
Bytes 2-33: Channel Name (32 bytes, UTF-8, null-padded)
Bytes 34-49: Secret (16 bytes)
</code></pre></div></p>
<p><strong>Total Length</strong>: 50 bytes</p>
<p><strong>Channel Index</strong>:
- Valid range is <code>0</code> to <code>max_channels - 1</code>. Read <code>max_channels</code> from byte 3 of
the <code>PACKET_DEVICE_INFO</code> response; do not assume 8. Builds ship with
<code>MAX_GROUP_CHANNELS</code> set to 1, 8 or 40 depending on the board.
- Index 0: pre-populated by the firmware with the built-in "Public" channel.
It can be overwritten like any other slot.
- An out-of-range index is rejected with <code>PACKET_ERROR</code> / <code>ERR_CODE_NOT_FOUND</code>.</p>
<p><strong>Channel Name</strong>:
- UTF-8 encoded
- Maximum 32 bytes
- Padded with null bytes (0x00) if shorter</p>
<p><strong>Secret Field</strong> (16 bytes):
- Always a real 16-byte key; the firmware derives the channel hash from
<code>SHA256(secret)</code> (<code>BaseChatMesh.cpp:936-942</code>). There is no "no secret" form.
- Do <strong>not</strong> write an all-zero secret. <code>SHA256</code> over 16 zero bytes is a fixed,
globally identical value, so a zero-secret channel is a well-known channel
with an all-zero AES key that any node can read — not a private one. The
firmware skips <em>unnamed</em> slots when matching inbound group traffic for
exactly this reason (<code>BaseChatMesh.cpp:392-401</code>), but a slot that has been
given a name and a zero secret is not skipped, and will absorb null-key
group traffic from any node on the mesh.
- The built-in public channel uses the well-known key
<code>izOH6cXN6mrJ5e26oRXNcg==</code> (base64), i.e.
<code>8b3387e9c5cdea6ac9e5edbaa115cd72</code> in hex.</p>
<p><strong>Example</strong> (create channel "YourChannelName" at index 1 with secret):
<div class="highlight"><pre><span></span><code>20 01 53 4D 53 00 00 ... (name padded to 32 bytes)
[16 bytes of secret]
</code></pre></div></p>
<p><strong>Note</strong>: The 32-byte secret variant is unsupported and returns <code>PACKET_ERROR</code>.</p>
<p><strong>Response</strong>: <code>PACKET_OK</code> (0x00) on success, <code>PACKET_ERROR</code> (0x01) on failure</p>
<hr />
<h3 id="5-send-channel-message">5. Send Channel Message</h3>
<p><strong>Purpose</strong>: Send a text message to a channel.</p>
<p><strong>Command Format</strong>:
<div class="highlight"><pre><span></span><code>Byte 0: 0x03
Byte 1: 0x00
Byte 2: Channel Index (0 .. max_channels-1)
Bytes 3-6: Timestamp (32-bit little-endian Unix timestamp, seconds)
Bytes 7+: Message Text (UTF-8, variable length)
</code></pre></div></p>
<p><strong>Timestamp</strong>: Unix timestamp in seconds (32-bit unsigned integer, little-endian)</p>
<p><strong>Example</strong> (send "Hello" to channel 1 at timestamp 1234567890):
<div class="highlight"><pre><span></span><code>03 00 01 D2 02 96 49 48 65 6C 6C 6F
</code></pre></div></p>
<p><strong>Response</strong>: <code>PACKET_MSG_SENT</code> (0x06) on success</p>
<hr />
<h3 id="6-send-channel-data-datagram">6. Send Channel Data Datagram</h3>
<p><strong>Purpose</strong>: Send a binary datagram to a channel. Unlike channel text messages, datagrams carry no built-in sender identity and no timestamp — applications needing either must encode them inside the binary payload.</p>
<p><strong>Command Format</strong>:
<div class="highlight"><pre><span></span><code>Byte 0: 0x3E
Byte 1: Channel Index (0 .. max_channels-1)
Byte 2: Path Length (0xFF = flood, otherwise actual path length)
Bytes 3 .. 2+path_len: Path (omitted when path_len == 0xFF)
Next 2 bytes (little-endian): Data Type (`data_type`, uint16)
Remaining bytes: Binary payload (variable length)
</code></pre></div></p>
<p><strong>Example</strong> (flood, <code>DATA_TYPE_DEV</code>, payload <code>A1 B2 C3</code>, channel 1):
<div class="highlight"><pre><span></span><code>3E 01 FF FF FF A1 B2 C3
</code></pre></div></p>
<p><strong>Data Type / Transport Mapping</strong>:
- <code>0x0000</code> (<code>DATA_TYPE_RESERVED</code>) is invalid and rejected with <code>PACKET_ERROR</code>.
- <code>0xFFFF</code> (<code>DATA_TYPE_DEV</code>) is the developer namespace for experimenting and developing apps.
- Values <code>0x0001</code>–<code>0xFFFE</code> are available for registered application/community namespaces. See the <a href="#registered-data_type-values">Registered data_type values</a> table below.</p>
<p><strong>Limits</strong>:
- Maximum payload length is <strong>165</strong> bytes — <code>MAX_GROUP_DATA_LENGTH =
MAX_PACKET_PAYLOAD - CIPHER_BLOCK_SIZE - 3</code> (<code>src/MeshCore.h:21</code>). This is the
radio-side limit and the one clients should enforce.
- Two different bounds are checked, and they do not agree. The host-frame check
uses the larger <code>MAX_CHANNEL_DATA_LENGTH = MAX_FRAME_SIZE - 9 = 167</code>
(<code>companion_radio/MyMesh.cpp:1265</code>); the radio-side check uses 165
(<code>BaseChatMesh.cpp:544</code>).
- Payloads <strong>above 167</strong> are rejected with <code>ERR_CODE_ILLEGAL_ARG</code> (6).
- Payloads of <strong>166 or 167</strong> pass the frame check, then fail the radio-side
check and return <code>ERR_CODE_TABLE_FULL</code> (3). Despite that code's usual
meaning, such a send can never succeed — do not retry it. Keep payloads at
165 bytes or fewer and this case cannot arise.</p>
<p><strong>Response</strong>: <code>PACKET_OK</code> (0x00) on success, or <code>PACKET_ERROR</code> (0x01) with one of:
- <code>ERR_CODE_NOT_FOUND</code> (2) — unknown <code>channel_idx</code>
- <code>ERR_CODE_ILLEGAL_ARG</code> (6) — invalid <code>path_len</code>, reserved <code>data_type</code> (<code>0x0000</code>), or payload larger than <code>MAX_CHANNEL_DATA_LENGTH</code> (167)
- <code>ERR_CODE_TABLE_FULL</code> (3) — outbound send queue is full (retry later), <strong>or</strong> a payload of 166-167 bytes that exceeds the radio-side limit (permanent; see Limits above)</p>
<p><strong>Inbound datagrams</strong> are delivered to the host via <code>RESP_CODE_CHANNEL_DATA_RECV</code> (0x1B); see <a href="#receive-channel-data-datagram">Receive Channel Data Datagram</a>.</p>
<h4 id="registered-data_type-values">Registered <code>data_type</code> values</h4>
<p><code>data_type</code> is an <strong>application identifier</strong>, not a payload-format identifier. Each registered value identifies an application that owns its own internal payload schemas. The firmware does not inspect payload contents — <code>data_type</code> is transported opaquely.</p>
<table>
<thead>
<tr>
<th>Value</th>
<th>Constant</th>
<th>Purpose</th>
</tr>
</thead>
<tbody>
<tr>
<td>0x0000</td>
<td><code>DATA_TYPE_RESERVED</code></td>
<td>Reserved; invalid on send</td>
</tr>
<tr>
<td>0x0001 – 0x00FF</td>
<td>—</td>
<td>Reserved for internal use</td>
</tr>
<tr>
<td>0x0100 – 0xFEFF</td>
<td>—</td>
<td>Registered application namespaces (see <a href="../number_allocations/">number_allocations.md</a>)</td>
</tr>
<tr>
<td>0xFF00 – 0xFFFE</td>
<td>—</td>
<td>Testing/development; no registration required</td>
</tr>
<tr>
<td>0xFFFF</td>
<td><code>DATA_TYPE_DEV</code></td>
<td>Developer/experimental namespace</td>
</tr>
</tbody>
</table>
<p>To register a new application, submit a PR adding a row to the table in <a href="../number_allocations/">docs/number_allocations.md</a>. Internal sub-formats within an allocated application ID are owned by that application and are not tracked in MeshCore firmware or this document.</p>
<hr />
<h3 id="receive-channel-data-datagram">Receive Channel Data Datagram</h3>
<p>Inbound group datagrams (radio-level <code>PAYLOAD_TYPE_GRP_DATA</code>, 0x06) are forwarded to the host as <code>RESP_CODE_CHANNEL_DATA_RECV</code> notifications.</p>
<p><strong>Frame Format</strong> (<code>RESP_CODE_CHANNEL_DATA_RECV</code>, 0x1B):
<div class="highlight"><pre><span></span><code>Byte 0: 0x1B (packet type)
Byte 1: SNR (signed int8, scaled ×4 — divide by 4.0 to recover dB)
Bytes 2-3: Reserved (clients MUST ignore)
Byte 4: Channel Index (0 .. max_channels-1)
Byte 5: Path Length (actual path length when flooded, otherwise 0xFF for direct)
Bytes 6-7: Data Type (uint16 little-endian)
Byte 8: Data Length
Bytes 9 .. 8+data_len: Payload
</code></pre></div></p>
<p><strong>Path bytes are not forwarded</strong>: Only <code>path_len</code> is reported in the receive frame — the path itself is not copied to the host. There are no path bytes between byte 5 and the data_type field at bytes 6–7, regardless of <code>path_len</code>.</p>
<p><strong>Path Length semantics differ between send and receive</strong>:</p>
<table>
<thead>
<tr>
<th>Direction</th>
<th><code>path_len = 0xFF</code></th>
<th><code>path_len ≠ 0xFF</code></th>
</tr>
</thead>
<tbody>
<tr>
<td>Send</td>
<td>Flood the network</td>
<td>Direct route; the encoded path follows (low 6 bits = hash count, top 2 bits + 1 = hash size; on-wire byte count = <code>hash_count × hash_size</code>)</td>
</tr>
<tr>
<td>Receive</td>
<td>Packet arrived via direct route</td>
<td>Packet was flooded; this is the encoded <code>pkt-&gt;path_len</code> field as observed (no path bytes follow)</td>
</tr>
</tbody>
</table>
<p>In other words, the meaning of <code>0xFF</code> is inverted between the two directions, and on receive the field carries metadata only — never a routable path. <code>path_len</code> is an encoded byte (see <code>Packet::isValidPathLen</code> / <code>Packet::writePath</code> in <code>src/Packet.cpp</code>), not a raw byte count.</p>
<p><strong>Note</strong>: The device may also emit <code>PACKET_MESSAGES_WAITING</code> (0x83) to notify the host that datagrams are queued; poll with <code>CMD_SYNC_NEXT_MESSAGE</code> (0x0A) to retrieve them.</p>
<p><strong>Parsing Pseudocode</strong>:
<div class="highlight"><pre><span></span><code><span class="k">def</span><span class="w"> </span><span class="nf">parse_channel_data_recv</span><span class="p">(</span><span class="n">data</span><span class="p">):</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mi">9</span><span class="p">:</span>
<span class="k">return</span> <span class="kc">None</span>
<span class="n">snr_byte</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="n">snr</span> <span class="o">=</span> <span class="p">(</span><span class="n">snr_byte</span> <span class="k">if</span> <span class="n">snr_byte</span> <span class="o">&lt;</span> <span class="mi">128</span> <span class="k">else</span> <span class="n">snr_byte</span> <span class="o">-</span> <span class="mi">256</span><span class="p">)</span> <span class="o">/</span> <span class="mf">4.0</span>
<span class="n">channel_idx</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span>
<span class="n">path_len</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="mi">5</span><span class="p">]</span>
<span class="n">data_type</span> <span class="o">=</span> <span class="nb">int</span><span class="o">.</span><span class="n">from_bytes</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="mi">6</span><span class="p">:</span><span class="mi">8</span><span class="p">],</span> <span class="s1">'little'</span><span class="p">)</span>
<span class="n">data_len</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="mi">8</span><span class="p">]</span>
<span class="k">if</span> <span class="mi">9</span> <span class="o">+</span> <span class="n">data_len</span> <span class="o">&gt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">):</span>
<span class="k">return</span> <span class="kc">None</span>
<span class="n">payload</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="mi">9</span><span class="p">:</span><span class="mi">9</span> <span class="o">+</span> <span class="n">data_len</span><span class="p">]</span>
<span class="k">return</span> <span class="p">{</span>
<span class="s1">'snr'</span><span class="p">:</span> <span class="n">snr</span><span class="p">,</span>
<span class="s1">'channel_idx'</span><span class="p">:</span> <span class="n">channel_idx</span><span class="p">,</span>
<span class="s1">'path_len'</span><span class="p">:</span> <span class="n">path_len</span><span class="p">,</span>
<span class="s1">'data_type'</span><span class="p">:</span> <span class="n">data_type</span><span class="p">,</span>
<span class="s1">'payload'</span><span class="p">:</span> <span class="nb">bytes</span><span class="p">(</span><span class="n">payload</span><span class="p">),</span>
<span class="p">}</span>
</code></pre></div></p>
<hr />
<h3 id="7-get-message">7. Get Message</h3>
<p><strong>Purpose</strong>: Request the next queued message from the device.</p>
<p><strong>Command Format</strong>:
<div class="highlight"><pre><span></span><code>Byte 0: 0x0A
</code></pre></div></p>
<p><strong>Example</strong> (hex):
<div class="highlight"><pre><span></span><code>0A
</code></pre></div></p>
<p><strong>Response</strong>:
- <code>PACKET_CHANNEL_MSG_RECV</code> (0x08) or <code>PACKET_CHANNEL_MSG_RECV_V3</code> (0x11) for channel messages
- <code>PACKET_CONTACT_MSG_RECV</code> (0x07) or <code>PACKET_CONTACT_MSG_RECV_V3</code> (0x10) for contact messages
- <code>PACKET_CHANNEL_DATA_RECV</code> (0x1B) for channel data datagrams
- <code>PACKET_NO_MORE_MSGS</code> (0x0A) if no messages available</p>
<p><strong>Note</strong>: Poll this command periodically to retrieve queued messages. The device may also send <code>PACKET_MESSAGES_WAITING</code> (0x83) as a notification when messages are available.</p>
<hr />
<h3 id="8-get-battery-and-storage">8. Get Battery and Storage</h3>
<p><strong>Purpose</strong>: Query device battery voltage and storage usage.</p>
<p><strong>Command Format</strong>:
<div class="highlight"><pre><span></span><code>Byte 0: 0x14
</code></pre></div></p>
<p><strong>Example</strong> (hex):
<div class="highlight"><pre><span></span><code>14
</code></pre></div></p>
<p><strong>Response</strong>: <code>PACKET_BATTERY</code> (0x0C) with battery millivolts and storage information</p>
<hr />
<h2 id="channel-management">Channel Management</h2>
<h3 id="channel-types">Channel Types</h3>
<ol>
<li><strong>Public Channel</strong><ul>
<li>Uses a publicly known 16-byte key: <code>8b3387e9c5cdea6ac9e5edbaa115cd72</code></li>
<li>Anyone can join this channel, messages should be considered public</li>
<li>Used as the default public group chat</li>
</ul>
</li>
<li><strong>Hashtag Channels</strong><ul>
<li>Uses a secret key derived from the channel name</li>
<li>It is the first 16 bytes of <code>sha256("#test")</code></li>
<li>For example hashtag channel <code>#test</code> has the key: <code>9cd8fcf22a47333b591d96a2b848b73f</code></li>
<li>Traffic is encrypted on air, but anyone who knows or guesses the channel
name can derive the key. Hashtag channels should not be treated as private.</li>
<li>Used as a topic based public group chat, separate from the default public channel</li>
</ul>
</li>
<li><strong>Private Channels</strong><ul>
<li>Uses a randomly generated 16-byte secret key</li>
<li>Messages should be considered private between those that know the secret</li>
<li>Users should keep the key secret, and only share with those you want to communicate with</li>
<li>Used as a secure private group chat</li>
</ul>
</li>
</ol>
<h3 id="channel-lifecycle">Channel Lifecycle</h3>
<ol>
<li><strong>Set Channel</strong>:<ul>
<li>Fetch all channel slots, and find one with empty name and all-zero secret</li>
<li>Generate or provide a 16-byte secret</li>
<li>Send <code>CMD_SET_CHANNEL</code> with name and a 16-byte secret</li>
</ul>
</li>
<li><strong>Get Channel</strong>:<ul>
<li>Send <code>CMD_GET_CHANNEL</code> with channel index</li>
<li>Parse <code>RESP_CODE_CHANNEL_INFO</code> response</li>
</ul>
</li>
<li><strong>Delete Channel</strong>:<ul>
<li>Send <code>CMD_SET_CHANNEL</code> with empty name and all-zero secret</li>
<li>Or overwrite with a new channel</li>
</ul>
</li>
</ol>
<hr />
<h2 id="message-handling">Message Handling</h2>
<h3 id="receiving-messages">Receiving Messages</h3>
<p>Messages are received via the TX characteristic (notifications). The device sends:</p>
<ol>
<li><strong>Channel Messages</strong>:</li>
<li><code>PACKET_CHANNEL_MSG_RECV</code> (0x08) - Standard format</li>
<li>
<p><code>PACKET_CHANNEL_MSG_RECV_V3</code> (0x11) - Version 3 with SNR</p>
</li>
<li>
<p><strong>Contact Messages</strong>:</p>
</li>
<li><code>PACKET_CONTACT_MSG_RECV</code> (0x07) - Standard format</li>
<li>
<p><code>PACKET_CONTACT_MSG_RECV_V3</code> (0x10) - Version 3 with SNR</p>
</li>
<li>
<p><strong>Notifications</strong>:</p>
</li>
<li><code>PACKET_MESSAGES_WAITING</code> (0x83) - Indicates messages are queued</li>
</ol>
<h3 id="contact-message-format">Contact Message Format</h3>
<p><strong>Standard Format</strong> (<code>PACKET_CONTACT_MSG_RECV</code>, 0x07):
<div class="highlight"><pre><span></span><code>Byte 0: 0x07 (packet type)
Bytes 1-6: Public Key Prefix (6 bytes, hex)
Byte 7: Path Length
Byte 8: Text Type
Bytes 9-12: Timestamp (32-bit little-endian)
Bytes 13-16: Signature (4 bytes, only if txt_type == 2)
Bytes 17+: Message Text (UTF-8)
</code></pre></div></p>
<p><strong>V3 Format</strong> (<code>PACKET_CONTACT_MSG_RECV_V3</code>, 0x10):
<div class="highlight"><pre><span></span><code>Byte 0: 0x10 (packet type)
Byte 1: SNR (signed byte, multiplied by 4)
Bytes 2-3: Reserved
Bytes 4-9: Public Key Prefix (6 bytes, hex)
Byte 10: Path Length
Byte 11: Text Type
Bytes 12-15: Timestamp (32-bit little-endian)
Bytes 16-19: Signature (4 bytes, only if txt_type == 2)
Bytes 20+: Message Text (UTF-8)
</code></pre></div></p>
<p><strong>Parsing Pseudocode</strong>:
<div class="highlight"><pre><span></span><code><span class="k">def</span><span class="w"> </span><span class="nf">parse_contact_message</span><span class="p">(</span><span class="n">data</span><span class="p">):</span>
<span class="n">packet_type</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="n">offset</span> <span class="o">=</span> <span class="mi">1</span>
<span class="c1"># Check for V3 format</span>
<span class="k">if</span> <span class="n">packet_type</span> <span class="o">==</span> <span class="mh">0x10</span><span class="p">:</span> <span class="c1"># V3</span>
<span class="n">snr_byte</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">offset</span><span class="p">]</span>
<span class="n">snr</span> <span class="o">=</span> <span class="p">((</span><span class="n">snr_byte</span> <span class="k">if</span> <span class="n">snr_byte</span> <span class="o">&lt;</span> <span class="mi">128</span> <span class="k">else</span> <span class="n">snr_byte</span> <span class="o">-</span> <span class="mi">256</span><span class="p">)</span> <span class="o">/</span> <span class="mf">4.0</span><span class="p">)</span>
<span class="n">offset</span> <span class="o">+=</span> <span class="mi">3</span> <span class="c1"># Skip SNR + reserved</span>
<span class="n">pubkey_prefix</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">offset</span><span class="p">:</span><span class="n">offset</span><span class="o">+</span><span class="mi">6</span><span class="p">]</span><span class="o">.</span><span class="n">hex</span><span class="p">()</span>
<span class="n">offset</span> <span class="o">+=</span> <span class="mi">6</span>
<span class="n">path_len</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">offset</span><span class="p">]</span>
<span class="n">txt_type</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">offset</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span>
<span class="n">offset</span> <span class="o">+=</span> <span class="mi">2</span>
<span class="n">timestamp</span> <span class="o">=</span> <span class="nb">int</span><span class="o">.</span><span class="n">from_bytes</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">offset</span><span class="p">:</span><span class="n">offset</span><span class="o">+</span><span class="mi">4</span><span class="p">],</span> <span class="s1">'little'</span><span class="p">)</span>
<span class="n">offset</span> <span class="o">+=</span> <span class="mi">4</span>
<span class="c1"># If txt_type == 2, skip 4-byte signature</span>
<span class="k">if</span> <span class="n">txt_type</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
<span class="n">offset</span> <span class="o">+=</span> <span class="mi">4</span>
<span class="n">message</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">offset</span><span class="p">:]</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">'utf-8'</span><span class="p">)</span>
<span class="k">return</span> <span class="p">{</span>
<span class="s1">'pubkey_prefix'</span><span class="p">:</span> <span class="n">pubkey_prefix</span><span class="p">,</span>
<span class="s1">'path_len'</span><span class="p">:</span> <span class="n">path_len</span><span class="p">,</span>
<span class="s1">'txt_type'</span><span class="p">:</span> <span class="n">txt_type</span><span class="p">,</span>
<span class="s1">'timestamp'</span><span class="p">:</span> <span class="n">timestamp</span><span class="p">,</span>
<span class="s1">'message'</span><span class="p">:</span> <span class="n">message</span><span class="p">,</span>
<span class="s1">'snr'</span><span class="p">:</span> <span class="n">snr</span> <span class="k">if</span> <span class="n">packet_type</span> <span class="o">==</span> <span class="mh">0x10</span> <span class="k">else</span> <span class="kc">None</span>
<span class="p">}</span>
</code></pre></div></p>
<h3 id="channel-message-format">Channel Message Format</h3>
<p><strong>Standard Format</strong> (<code>PACKET_CHANNEL_MSG_RECV</code>, 0x08):
<div class="highlight"><pre><span></span><code>Byte 0: 0x08 (packet type)
Byte 1: Channel Index (0 .. max_channels-1)
Byte 2: Path Length
Byte 3: Text Type
Bytes 4-7: Timestamp (32-bit little-endian)
Bytes 8+: Message Text (UTF-8)
</code></pre></div></p>
<p><strong>V3 Format</strong> (<code>PACKET_CHANNEL_MSG_RECV_V3</code>, 0x11):
<div class="highlight"><pre><span></span><code>Byte 0: 0x11 (packet type)
Byte 1: SNR (signed byte, multiplied by 4)
Bytes 2-3: Reserved
Byte 4: Channel Index (0 .. max_channels-1)
Byte 5: Path Length
Byte 6: Text Type
Bytes 7-10: Timestamp (32-bit little-endian)
Bytes 11+: Message Text (UTF-8)
</code></pre></div></p>
<p><strong>Parsing Pseudocode</strong>:
<div class="highlight"><pre><span></span><code><span class="k">def</span><span class="w"> </span><span class="nf">parse_channel_message</span><span class="p">(</span><span class="n">data</span><span class="p">):</span>
<span class="n">packet_type</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="n">offset</span> <span class="o">=</span> <span class="mi">1</span>
<span class="c1"># Check for V3 format</span>
<span class="k">if</span> <span class="n">packet_type</span> <span class="o">==</span> <span class="mh">0x11</span><span class="p">:</span> <span class="c1"># V3</span>
<span class="n">snr_byte</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">offset</span><span class="p">]</span>
<span class="n">snr</span> <span class="o">=</span> <span class="p">((</span><span class="n">snr_byte</span> <span class="k">if</span> <span class="n">snr_byte</span> <span class="o">&lt;</span> <span class="mi">128</span> <span class="k">else</span> <span class="n">snr_byte</span> <span class="o">-</span> <span class="mi">256</span><span class="p">)</span> <span class="o">/</span> <span class="mf">4.0</span><span class="p">)</span>
<span class="n">offset</span> <span class="o">+=</span> <span class="mi">3</span> <span class="c1"># Skip SNR + reserved</span>
<span class="n">channel_idx</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">offset</span><span class="p">]</span>
<span class="n">path_len</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">offset</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span>
<span class="n">txt_type</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">offset</span> <span class="o">+</span> <span class="mi">2</span><span class="p">]</span>
<span class="n">timestamp</span> <span class="o">=</span> <span class="nb">int</span><span class="o">.</span><span class="n">from_bytes</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">offset</span><span class="o">+</span><span class="mi">3</span><span class="p">:</span><span class="n">offset</span><span class="o">+</span><span class="mi">7</span><span class="p">],</span> <span class="s1">'little'</span><span class="p">)</span>
<span class="n">message</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">offset</span><span class="o">+</span><span class="mi">7</span><span class="p">:]</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">'utf-8'</span><span class="p">)</span>
<span class="k">return</span> <span class="p">{</span>
<span class="s1">'channel_idx'</span><span class="p">:</span> <span class="n">channel_idx</span><span class="p">,</span>
<span class="s1">'timestamp'</span><span class="p">:</span> <span class="n">timestamp</span><span class="p">,</span>
<span class="s1">'message'</span><span class="p">:</span> <span class="n">message</span><span class="p">,</span>
<span class="s1">'snr'</span><span class="p">:</span> <span class="n">snr</span> <span class="k">if</span> <span class="n">packet_type</span> <span class="o">==</span> <span class="mh">0x11</span> <span class="k">else</span> <span class="kc">None</span>
<span class="p">}</span>
</code></pre></div></p>
<h3 id="sending-messages">Sending Messages</h3>
<p>Use the <code>SEND_CHANNEL_MESSAGE</code> command (see <a href="#commands">Commands</a>).</p>
<p><strong>Important</strong>:
- Messages are limited to 133 characters per MeshCore specification
- Long messages should be split into chunks
- Include a chunk indicator (e.g., "[1/3] message text")</p>
<hr />
<h2 id="response-parsing">Response Parsing</h2>
<h3 id="terminology">Terminology</h3>
<p>This document uses a spec-level naming convention (<code>PACKET_*</code>) for bytes the firmware sends back to the host. In the firmware source these same values are split across two <code>#define</code> families by purpose:</p>
<ul>
<li><code>RESP_CODE_*</code> — direct replies to a command (e.g. <code>RESP_CODE_CHANNEL_DATA_RECV</code> = <code>PACKET_CHANNEL_DATA_RECV</code> = 0x1B).</li>
<li><code>PUSH_CODE_*</code> — asynchronous notifications not tied to a specific command (e.g. <code>PUSH_CODE_MSG_WAITING</code> = <code>PACKET_MESSAGES_WAITING</code> = 0x83).</li>
</ul>
<p>Byte values are authoritative; names are aliases. When reading firmware source, <code>RESP_CODE_X</code> / <code>PUSH_CODE_X</code> correspond to this doc's <code>PACKET_X</code> of the same numeric value.</p>
<h3 id="packet-types">Packet Types</h3>
<table>
<thead>
<tr>
<th>Value</th>
<th>Name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>0x00</td>
<td>PACKET_OK</td>
<td>Command succeeded</td>
</tr>
<tr>
<td>0x01</td>
<td>PACKET_ERROR</td>
<td>Command failed</td>
</tr>
<tr>
<td>0x02</td>
<td>PACKET_CONTACT_START</td>
<td>Start of contact list</td>
</tr>
<tr>
<td>0x03</td>
<td>PACKET_CONTACT</td>
<td>Contact information</td>
</tr>
<tr>
<td>0x04</td>
<td>PACKET_CONTACT_END</td>
<td>End of contact list</td>
</tr>
<tr>
<td>0x05</td>
<td>PACKET_SELF_INFO</td>
<td>Device self-information</td>
</tr>
<tr>
<td>0x06</td>
<td>PACKET_MSG_SENT</td>
<td>Message sent confirmation</td>
</tr>
<tr>
<td>0x07</td>
<td>PACKET_CONTACT_MSG_RECV</td>
<td>Contact message (standard)</td>
</tr>
<tr>
<td>0x08</td>
<td>PACKET_CHANNEL_MSG_RECV</td>
<td>Channel message (standard)</td>
</tr>
<tr>
<td>0x09</td>
<td>PACKET_CURRENT_TIME</td>
<td>Current time response</td>
</tr>
<tr>
<td>0x0A</td>
<td>PACKET_NO_MORE_MSGS</td>
<td>No more messages available</td>
</tr>
<tr>
<td>0x0C</td>
<td>PACKET_BATTERY</td>
<td>Battery level</td>
</tr>
<tr>
<td>0x0D</td>
<td>PACKET_DEVICE_INFO</td>
<td>Device information</td>
</tr>
<tr>
<td>0x10</td>
<td>PACKET_CONTACT_MSG_RECV_V3</td>
<td>Contact message (V3 with SNR)</td>
</tr>
<tr>
<td>0x11</td>
<td>PACKET_CHANNEL_MSG_RECV_V3</td>
<td>Channel message (V3 with SNR)</td>
</tr>
<tr>
<td>0x12</td>
<td>PACKET_CHANNEL_INFO</td>
<td>Channel information</td>
</tr>
<tr>
<td>0x1B</td>
<td>PACKET_CHANNEL_DATA_RECV</td>
<td>Channel data datagram</td>
</tr>
<tr>
<td>0x80</td>
<td>PACKET_ADVERTISEMENT</td>
<td>Advertisement packet</td>
</tr>
<tr>
<td>0x82</td>
<td>PACKET_ACK</td>
<td>Acknowledgment</td>
</tr>
<tr>
<td>0x83</td>
<td>PACKET_MESSAGES_WAITING</td>
<td>Messages waiting notification</td>
</tr>
<tr>
<td>0x88</td>
<td>PACKET_LOG_DATA</td>
<td>RF log data (can be ignored)</td>
</tr>
</tbody>
</table>
<h3 id="parsing-responses">Parsing Responses</h3>
<p><strong>PACKET_OK</strong> (0x00):
<div class="highlight"><pre><span></span><code>Byte 0: 0x00
Bytes 1-4: Optional value (32-bit little-endian integer)
</code></pre></div></p>
<p><strong>PACKET_ERROR</strong> (0x01):
<div class="highlight"><pre><span></span><code>Byte 0: 0x01
Byte 1: Error code (optional)
</code></pre></div></p>
<p><strong>PACKET_CHANNEL_INFO</strong> (0x12):
<div class="highlight"><pre><span></span><code>Byte 0: 0x12
Byte 1: Channel Index
Bytes 2-33: Channel Name (32 bytes, null-terminated)
Bytes 34-49: Secret (16 bytes)
</code></pre></div></p>
<p><strong>Note</strong>: The device returns the 16-byte channel secret in this response.</p>
<p><strong>PACKET_DEVICE_INFO</strong> (0x0D):
<div class="highlight"><pre><span></span><code>Byte 0: 0x0D
Byte 1: Firmware Version (uint8)
Bytes 2+: Variable length based on firmware version
For firmware version &gt;= 3:
Byte 2: Max Contacts Raw (uint8, actual = value * 2)
Byte 3: Max Channels (uint8)
Bytes 4-7: BLE PIN (32-bit little-endian)
Bytes 8-19: Firmware Build (12 bytes, UTF-8, null-padded)
Bytes 20-59: Model (40 bytes, UTF-8, null-padded)
Bytes 60-79: Version (20 bytes, UTF-8, null-padded)
Byte 80: Client repeat enabled/preferred (firmware v9+)
Byte 81: Path hash mode (firmware v10+)
</code></pre></div></p>
<p><strong>Parsing Pseudocode</strong>:
<div class="highlight"><pre><span></span><code><span class="k">def</span><span class="w"> </span><span class="nf">parse_device_info</span><span class="p">(</span><span class="n">data</span><span class="p">):</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mi">2</span><span class="p">:</span>
<span class="k">return</span> <span class="kc">None</span>
<span class="n">fw_ver</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="n">info</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'fw_ver'</span><span class="p">:</span> <span class="n">fw_ver</span><span class="p">}</span>
<span class="k">if</span> <span class="n">fw_ver</span> <span class="o">&gt;=</span> <span class="mi">3</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="mi">80</span><span class="p">:</span>
<span class="n">info</span><span class="p">[</span><span class="s1">'max_contacts'</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">*</span> <span class="mi">2</span>
<span class="n">info</span><span class="p">[</span><span class="s1">'max_channels'</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span>
<span class="n">info</span><span class="p">[</span><span class="s1">'ble_pin'</span><span class="p">]</span> <span class="o">=</span> <span class="nb">int</span><span class="o">.</span><span class="n">from_bytes</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="mi">4</span><span class="p">:</span><span class="mi">8</span><span class="p">],</span> <span class="s1">'little'</span><span class="p">)</span>
<span class="n">info</span><span class="p">[</span><span class="s1">'fw_build'</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="mi">8</span><span class="p">:</span><span class="mi">20</span><span class="p">]</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">'utf-8'</span><span class="p">)</span><span class="o">.</span><span class="n">rstrip</span><span class="p">(</span><span class="s1">'</span><span class="se">\x00</span><span class="s1">'</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
<span class="n">info</span><span class="p">[</span><span class="s1">'model'</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="mi">20</span><span class="p">:</span><span class="mi">60</span><span class="p">]</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">'utf-8'</span><span class="p">)</span><span class="o">.</span><span class="n">rstrip</span><span class="p">(</span><span class="s1">'</span><span class="se">\x00</span><span class="s1">'</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
<span class="n">info</span><span class="p">[</span><span class="s1">'ver'</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="mi">60</span><span class="p">:</span><span class="mi">80</span><span class="p">]</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">'utf-8'</span><span class="p">)</span><span class="o">.</span><span class="n">rstrip</span><span class="p">(</span><span class="s1">'</span><span class="se">\x00</span><span class="s1">'</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
<span class="k">return</span> <span class="n">info</span>
</code></pre></div></p>
<p><strong>PACKET_BATTERY</strong> (0x0C):
<div class="highlight"><pre><span></span><code>Byte 0: 0x0C
Bytes 1-2: Battery Voltage (16-bit little-endian, millivolts)
Bytes 3-6: Used Storage (32-bit little-endian, KB)
Bytes 7-10: Total Storage (32-bit little-endian, KB)
</code></pre></div></p>
<p><strong>Parsing Pseudocode</strong>:
<div class="highlight"><pre><span></span><code><span class="k">def</span><span class="w"> </span><span class="nf">parse_battery</span><span class="p">(</span><span class="n">data</span><span class="p">):</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mi">3</span><span class="p">:</span>
<span class="k">return</span> <span class="kc">None</span>
<span class="n">mv</span> <span class="o">=</span> <span class="nb">int</span><span class="o">.</span><span class="n">from_bytes</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">3</span><span class="p">],</span> <span class="s1">'little'</span><span class="p">)</span>
<span class="n">info</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'battery_mv'</span><span class="p">:</span> <span class="n">mv</span><span class="p">}</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="mi">11</span><span class="p">:</span>
<span class="n">info</span><span class="p">[</span><span class="s1">'used_kb'</span><span class="p">]</span> <span class="o">=</span> <span class="nb">int</span><span class="o">.</span><span class="n">from_bytes</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="mi">3</span><span class="p">:</span><span class="mi">7</span><span class="p">],</span> <span class="s1">'little'</span><span class="p">)</span>
<span class="n">info</span><span class="p">[</span><span class="s1">'total_kb'</span><span class="p">]</span> <span class="o">=</span> <span class="nb">int</span><span class="o">.</span><span class="n">from_bytes</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="mi">7</span><span class="p">:</span><span class="mi">11</span><span class="p">],</span> <span class="s1">'little'</span><span class="p">)</span>
<span class="k">return</span> <span class="n">info</span>
</code></pre></div></p>
<p><strong>PACKET_SELF_INFO</strong> (0x05):
<div class="highlight"><pre><span></span><code>Byte 0: 0x05
Byte 1: Advertisement Type
Byte 2: TX Power
Byte 3: Max TX Power
Bytes 4-35: Public Key (32 bytes, hex)
Bytes 36-39: Advertisement Latitude (32-bit little-endian, divided by 1e6)
Bytes 40-43: Advertisement Longitude (32-bit little-endian, divided by 1e6)
Byte 44: Multi ACKs
Byte 45: Advertisement Location Policy
Byte 46: Telemetry Mode (bitfield)
Byte 47: Manual Add Contacts (bool)
Bytes 48-51: Radio Frequency (32-bit little-endian, divided by 1000.0)
Bytes 52-55: Radio Bandwidth (32-bit little-endian, divided by 1000.0)
Byte 56: Radio Spreading Factor
Byte 57: Radio Coding Rate
Bytes 58+: Device Name (UTF-8, variable length, no null terminator required)
</code></pre></div></p>
<p><strong>Parsing Pseudocode</strong>:
<div class="highlight"><pre><span></span><code><span class="k">def</span><span class="w"> </span><span class="nf">parse_self_info</span><span class="p">(</span><span class="n">data</span><span class="p">):</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mi">36</span><span class="p">:</span>
<span class="k">return</span> <span class="kc">None</span>
<span class="n">offset</span> <span class="o">=</span> <span class="mi">1</span>
<span class="n">info</span> <span class="o">=</span> <span class="p">{</span>
<span class="s1">'adv_type'</span><span class="p">:</span> <span class="n">data</span><span class="p">[</span><span class="n">offset</span><span class="p">],</span>
<span class="s1">'tx_power'</span><span class="p">:</span> <span class="n">data</span><span class="p">[</span><span class="n">offset</span> <span class="o">+</span> <span class="mi">1</span><span class="p">],</span>
<span class="s1">'max_tx_power'</span><span class="p">:</span> <span class="n">data</span><span class="p">[</span><span class="n">offset</span> <span class="o">+</span> <span class="mi">2</span><span class="p">],</span>
<span class="s1">'public_key'</span><span class="p">:</span> <span class="n">data</span><span class="p">[</span><span class="n">offset</span> <span class="o">+</span> <span class="mi">3</span><span class="p">:</span><span class="n">offset</span> <span class="o">+</span> <span class="mi">35</span><span class="p">]</span><span class="o">.</span><span class="n">hex</span><span class="p">()</span>
<span class="p">}</span>
<span class="n">offset</span> <span class="o">+=</span> <span class="mi">35</span>
<span class="n">lat</span> <span class="o">=</span> <span class="nb">int</span><span class="o">.</span><span class="n">from_bytes</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">offset</span><span class="p">:</span><span class="n">offset</span><span class="o">+</span><span class="mi">4</span><span class="p">],</span> <span class="s1">'little'</span><span class="p">)</span> <span class="o">/</span> <span class="mf">1e6</span>
<span class="n">lon</span> <span class="o">=</span> <span class="nb">int</span><span class="o">.</span><span class="n">from_bytes</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">offset</span><span class="o">+</span><span class="mi">4</span><span class="p">:</span><span class="n">offset</span><span class="o">+</span><span class="mi">8</span><span class="p">],</span> <span class="s1">'little'</span><span class="p">)</span> <span class="o">/</span> <span class="mf">1e6</span>
<span class="n">info</span><span class="p">[</span><span class="s1">'adv_lat'</span><span class="p">]</span> <span class="o">=</span> <span class="n">lat</span>
<span class="n">info</span><span class="p">[</span><span class="s1">'adv_lon'</span><span class="p">]</span> <span class="o">=</span> <span class="n">lon</span>
<span class="n">offset</span> <span class="o">+=</span> <span class="mi">8</span>
<span class="n">info</span><span class="p">[</span><span class="s1">'multi_acks'</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">offset</span><span class="p">]</span>
<span class="n">info</span><span class="p">[</span><span class="s1">'adv_loc_policy'</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">offset</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span>
<span class="n">telemetry_mode</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">offset</span> <span class="o">+</span> <span class="mi">2</span><span class="p">]</span>
<span class="n">info</span><span class="p">[</span><span class="s1">'telemetry_mode_env'</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">telemetry_mode</span> <span class="o">&gt;&gt;</span> <span class="mi">4</span><span class="p">)</span> <span class="o">&amp;</span> <span class="mb">0b11</span>
<span class="n">info</span><span class="p">[</span><span class="s1">'telemetry_mode_loc'</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">telemetry_mode</span> <span class="o">&gt;&gt;</span> <span class="mi">2</span><span class="p">)</span> <span class="o">&amp;</span> <span class="mb">0b11</span>
<span class="n">info</span><span class="p">[</span><span class="s1">'telemetry_mode_base'</span><span class="p">]</span> <span class="o">=</span> <span class="n">telemetry_mode</span> <span class="o">&amp;</span> <span class="mb">0b11</span>
<span class="n">info</span><span class="p">[</span><span class="s1">'manual_add_contacts'</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">offset</span> <span class="o">+</span> <span class="mi">3</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">0</span>
<span class="n">offset</span> <span class="o">+=</span> <span class="mi">4</span>
<span class="n">freq</span> <span class="o">=</span> <span class="nb">int</span><span class="o">.</span><span class="n">from_bytes</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">offset</span><span class="p">:</span><span class="n">offset</span><span class="o">+</span><span class="mi">4</span><span class="p">],</span> <span class="s1">'little'</span><span class="p">)</span> <span class="o">/</span> <span class="mf">1000.0</span>
<span class="n">bw</span> <span class="o">=</span> <span class="nb">int</span><span class="o">.</span><span class="n">from_bytes</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">offset</span><span class="o">+</span><span class="mi">4</span><span class="p">:</span><span class="n">offset</span><span class="o">+</span><span class="mi">8</span><span class="p">],</span> <span class="s1">'little'</span><span class="p">)</span> <span class="o">/</span> <span class="mf">1000.0</span>
<span class="n">info</span><span class="p">[</span><span class="s1">'radio_freq'</span><span class="p">]</span> <span class="o">=</span> <span class="n">freq</span>
<span class="n">info</span><span class="p">[</span><span class="s1">'radio_bw'</span><span class="p">]</span> <span class="o">=</span> <span class="n">bw</span>
<span class="n">info</span><span class="p">[</span><span class="s1">'radio_sf'</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">offset</span> <span class="o">+</span> <span class="mi">8</span><span class="p">]</span>
<span class="n">info</span><span class="p">[</span><span class="s1">'radio_cr'</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">offset</span> <span class="o">+</span> <span class="mi">9</span><span class="p">]</span>
<span class="n">offset</span> <span class="o">+=</span> <span class="mi">10</span>
<span class="k">if</span> <span class="n">offset</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">):</span>
<span class="n">name_bytes</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">offset</span><span class="p">:]</span>
<span class="n">info</span><span class="p">[</span><span class="s1">'name'</span><span class="p">]</span> <span class="o">=</span> <span class="n">name_bytes</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">'utf-8'</span><span class="p">)</span><span class="o">.</span><span class="n">rstrip</span><span class="p">(</span><span class="s1">'</span><span class="se">\x00</span><span class="s1">'</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
<span class="k">return</span> <span class="n">info</span>
</code></pre></div></p>
<p><strong>PACKET_MSG_SENT</strong> (0x06):
<div class="highlight"><pre><span></span><code>Byte 0: 0x06
Byte 1: Route Flag (0 = direct, 1 = flood)
Bytes 2-5: Tag / Expected ACK (4 bytes, little-endian)
Bytes 6-9: Suggested Timeout (32-bit little-endian, milliseconds)
</code></pre></div></p>
<p><strong>PACKET_ACK</strong> (0x82):
<div class="highlight"><pre><span></span><code>Byte 0: 0x82
Bytes 1-6: ACK Code (6 bytes, hex)
</code></pre></div></p>
<h3 id="error-codes">Error Codes</h3>
<p><code>PACKET_ERROR</code> (0x01) carries a single-byte error code in byte 1. Values match the <code>ERR_CODE_*</code> constants defined in <code>examples/companion_radio/MyMesh.cpp</code>:</p>
<table>
<thead>
<tr>
<th>Code</th>
<th>Constant (firmware)</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td><code>ERR_CODE_UNSUPPORTED_CMD</code></td>
<td>Unknown or unsupported command byte / sub-command</td>
</tr>
<tr>
<td>2</td>
<td><code>ERR_CODE_NOT_FOUND</code></td>
<td>Target not found (channel, contact, message, etc.)</td>
</tr>
<tr>
<td>3</td>
<td><code>ERR_CODE_TABLE_FULL</code></td>
<td>Internal queue or table is full — retry later</td>
</tr>
<tr>
<td>4</td>
<td><code>ERR_CODE_BAD_STATE</code></td>
<td>Operation not valid in current device state (e.g. iterator already running)</td>
</tr>
<tr>
<td>5</td>
<td><code>ERR_CODE_FILE_IO_ERROR</code></td>
<td>Filesystem or storage I/O failure</td>
</tr>
<tr>
<td>6</td>
<td><code>ERR_CODE_ILLEGAL_ARG</code></td>
<td>Invalid argument (bad length, out-of-range value, reserved field, etc.)</td>
</tr>
</tbody>
</table>
<p><strong>Note</strong>: Error codes may vary by firmware version. Always check byte 1 of <code>PACKET_ERROR</code> response, and treat unknown codes as generic errors.</p>
<h3 id="frame-handling">Frame Handling</h3>
<p>BLE implementations enqueue and deliver one protocol frame per BLE write/notification at the firmware layer.</p>
<ul>
<li>Apps should treat each characteristic write/notification as exactly one companion protocol frame</li>
<li>Apps should still validate frame lengths before parsing</li>
<li>Future transports or firmware revisions may differ, so avoid assuming fixed payload sizes for variable-length responses</li>
</ul>
<h3 id="response-handling">Response Handling</h3>
<ol>
<li><strong>Command-Response Pattern</strong>:</li>
<li>Send command via RX characteristic</li>
<li>Wait for response via TX characteristic (notification)</li>
<li>Match response to command using sequence numbers or command type</li>
<li>Handle timeout (typically 5 seconds)</li>
<li>
<p>Use command queue to prevent concurrent commands</p>
</li>
<li>
<p><strong>Asynchronous Messages</strong>:</p>
</li>
<li>Device may send messages at any time via TX characteristic</li>
<li>Handle <code>PACKET_MESSAGES_WAITING</code> (0x83) by polling <code>GET_MESSAGE</code> command</li>
<li>Parse incoming messages and route to appropriate handlers</li>
<li>
<p>Validate frame length before decoding</p>
</li>
<li>
<p><strong>Response Matching</strong>:</p>
</li>
<li>
<p>Match responses to commands by expected packet type:</p>
<ul>
<li><code>APP_START</code> → <code>PACKET_SELF_INFO</code></li>
<li><code>DEVICE_QUERY</code> → <code>PACKET_DEVICE_INFO</code></li>
<li><code>GET_CHANNEL</code> → <code>PACKET_CHANNEL_INFO</code></li>
<li><code>SET_CHANNEL</code> → <code>PACKET_OK</code> or <code>PACKET_ERROR</code></li>
<li><code>SEND_CHANNEL_MESSAGE</code> → <code>PACKET_MSG_SENT</code></li>
<li><code>GET_MESSAGE</code> → <code>PACKET_CHANNEL_MSG_RECV</code>, <code>PACKET_CONTACT_MSG_RECV</code>, <code>PACKET_CHANNEL_DATA_RECV</code>, or <code>PACKET_NO_MORE_MSGS</code></li>
<li><code>SEND_CHANNEL_DATA</code> → <code>PACKET_OK</code> or <code>PACKET_ERROR</code></li>
<li><code>GET_BATTERY</code> → <code>PACKET_BATTERY</code></li>
</ul>
</li>
<li>
<p><strong>Timeout Handling</strong>:</p>
</li>
<li>Default timeout: 5 seconds per command</li>
<li>On timeout: Log error, clear current command, proceed to next in queue</li>
<li>Some commands may take longer (e.g., <code>SET_CHANNEL</code> may need 1-2 seconds)</li>
<li>
<p>Consider longer timeout for channel operations</p>
</li>
<li>
<p><strong>Error Recovery</strong>:</p>
</li>
<li>On <code>PACKET_ERROR</code>: Log error code, clear current command</li>
<li>On connection loss: Clear command queue, attempt reconnection</li>
<li>On invalid response: Log warning, clear current command, proceed</li>
</ol>
<hr />
<h2 id="example-implementation-flow">Example Implementation Flow</h2>
<h3 id="initialization">Initialization</h3>
<div class="highlight"><pre><span></span><code><span class="c1"># 1. Scan for MeshCore device</span>
<span class="n">device</span> <span class="o">=</span> <span class="n">scan_for_device</span><span class="p">(</span><span class="s2">"MeshCore"</span><span class="p">)</span>
<span class="c1"># 2. Connect to BLE GATT</span>
<span class="n">gatt</span> <span class="o">=</span> <span class="n">connect_to_device</span><span class="p">(</span><span class="n">device</span><span class="p">)</span>
<span class="c1"># 3. Discover services and characteristics</span>
<span class="n">service</span> <span class="o">=</span> <span class="n">discover_service</span><span class="p">(</span><span class="n">gatt</span><span class="p">,</span> <span class="s2">"6E400001-B5A3-F393-E0A9-E50E24DCCA9E"</span><span class="p">)</span>
<span class="n">rx_char</span> <span class="o">=</span> <span class="n">discover_characteristic</span><span class="p">(</span><span class="n">service</span><span class="p">,</span> <span class="s2">"6E400002-B5A3-F393-E0A9-E50E24DCCA9E"</span><span class="p">)</span>
<span class="n">tx_char</span> <span class="o">=</span> <span class="n">discover_characteristic</span><span class="p">(</span><span class="n">service</span><span class="p">,</span> <span class="s2">"6E400003-B5A3-F393-E0A9-E50E24DCCA9E"</span><span class="p">)</span>
<span class="c1"># 4. Enable notifications on TX characteristic</span>
<span class="n">enable_notifications</span><span class="p">(</span><span class="n">tx_char</span><span class="p">,</span> <span class="n">on_notification_received</span><span class="p">)</span>
<span class="c1"># 5. Send AppStart command</span>
<span class="n">send_command</span><span class="p">(</span><span class="n">rx_char</span><span class="p">,</span> <span class="n">build_app_start</span><span class="p">())</span>
<span class="n">wait_for_response</span><span class="p">(</span><span class="n">PACKET_SELF_INFO</span><span class="p">)</span>
</code></pre></div>
<h3 id="creating-a-private-channel">Creating a Private Channel</h3>
<div class="highlight"><pre><span></span><code><span class="c1"># 1. Generate 16-byte secret</span>
<span class="n">secret_16_bytes</span> <span class="o">=</span> <span class="n">generate_secret</span><span class="p">(</span><span class="mi">16</span><span class="p">)</span> <span class="c1"># Use CSPRNG</span>
<span class="n">secret_hex</span> <span class="o">=</span> <span class="n">secret_16_bytes</span><span class="o">.</span><span class="n">hex</span><span class="p">()</span>
<span class="c1"># 2. Build SET_CHANNEL command</span>
<span class="n">channel_name</span> <span class="o">=</span> <span class="s2">"YourChannelName"</span>
<span class="n">channel_index</span> <span class="o">=</span> <span class="mi">1</span> <span class="c1"># Use 1-7 for private channels</span>
<span class="n">command</span> <span class="o">=</span> <span class="n">build_set_channel</span><span class="p">(</span><span class="n">channel_index</span><span class="p">,</span> <span class="n">channel_name</span><span class="p">,</span> <span class="n">secret_16_bytes</span><span class="p">)</span>
<span class="c1"># 3. Send command</span>
<span class="n">send_command</span><span class="p">(</span><span class="n">rx_char</span><span class="p">,</span> <span class="n">command</span><span class="p">)</span>
<span class="n">response</span> <span class="o">=</span> <span class="n">wait_for_response</span><span class="p">(</span><span class="n">PACKET_OK</span><span class="p">)</span>
<span class="c1"># 4. Store secret locally</span>
<span class="n">store_channel_secret</span><span class="p">(</span><span class="n">channel_index</span><span class="p">,</span> <span class="n">secret_hex</span><span class="p">)</span>
</code></pre></div>
<h3 id="sending-a-message">Sending a Message</h3>
<div class="highlight"><pre><span></span><code><span class="c1"># 1. Build channel message command</span>
<span class="n">channel_index</span> <span class="o">=</span> <span class="mi">1</span>
<span class="n">message</span> <span class="o">=</span> <span class="s2">"Hello, MeshCore!"</span>
<span class="n">timestamp</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span>
<span class="n">command</span> <span class="o">=</span> <span class="n">build_channel_message</span><span class="p">(</span><span class="n">channel_index</span><span class="p">,</span> <span class="n">message</span><span class="p">,</span> <span class="n">timestamp</span><span class="p">)</span>
<span class="c1"># 2. Send command</span>
<span class="n">send_command</span><span class="p">(</span><span class="n">rx_char</span><span class="p">,</span> <span class="n">command</span><span class="p">)</span>
<span class="n">response</span> <span class="o">=</span> <span class="n">wait_for_response</span><span class="p">(</span><span class="n">PACKET_MSG_SENT</span><span class="p">)</span>
</code></pre></div>
<h3 id="receiving-messages_1">Receiving Messages</h3>
<div class="highlight"><pre><span></span><code><span class="k">def</span><span class="w"> </span><span class="nf">on_notification_received</span><span class="p">(</span><span class="n">data</span><span class="p">):</span>
<span class="n">packet_type</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="k">if</span> <span class="n">packet_type</span> <span class="o">==</span> <span class="n">PACKET_CHANNEL_MSG_RECV</span> <span class="ow">or</span> <span class="n">packet_type</span> <span class="o">==</span> <span class="n">PACKET_CHANNEL_MSG_RECV_V3</span><span class="p">:</span>
<span class="n">message</span> <span class="o">=</span> <span class="n">parse_channel_message</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="n">handle_channel_message</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">packet_type</span> <span class="o">==</span> <span class="n">PACKET_MESSAGES_WAITING</span><span class="p">:</span>
<span class="c1"># Poll for messages</span>
<span class="n">send_command</span><span class="p">(</span><span class="n">rx_char</span><span class="p">,</span> <span class="n">build_get_message</span><span class="p">())</span>
</code></pre></div>
<hr />
<h2 id="best-practices">Best Practices</h2>
<ol>
<li><strong>Connection Management</strong>:</li>
<li>Implement auto-reconnect with exponential backoff</li>
<li>Handle disconnections gracefully</li>
<li>
<p>Store last connected device address for quick reconnection</p>
</li>
<li>
<p><strong>Secret Management</strong>:</p>
</li>
<li>Always use cryptographically secure random number generators</li>
<li>Store secrets securely (encrypted storage)</li>
<li>
<p>Never log or transmit secrets in plain text</p>
</li>
<li>
<p><strong>Message Handling</strong>:</p>
</li>
<li>Send <code>CMD_SYNC_NEXT_MESSAGE</code> when <code>PUSH_CODE_MSG_WAITING</code> is received</li>
<li>
<p>Implement message deduplication to avoid displaying the same message twice</p>
</li>
<li>
<p><strong>Channel Management</strong>:</p>
<ul>
<li>Fetch all channel slots even if you encounter an empty slot</li>
<li>Ideally save new channels into the first empty slot</li>
</ul>
</li>
<li>
<p><strong>Error Handling</strong>:</p>
</li>
<li>Implement timeouts for all commands (typically 5 seconds)</li>
<li>Handle <code>RESP_CODE_ERR</code> responses appropriately</li>
</ol>
<hr />
<h2 id="troubleshooting">Troubleshooting</h2>
<h3 id="connection-issues">Connection Issues</h3>
<ul>
<li><strong>Device not found</strong>: Ensure device is powered on and advertising</li>
<li><strong>Connection timeout</strong>: Check Bluetooth permissions and device proximity</li>
<li><strong>GATT errors</strong>: Ensure proper service/characteristic discovery</li>
</ul>
<h3 id="command-issues">Command Issues</h3>
<ul>
<li><strong>No response</strong>: Verify notifications are enabled, check connection state</li>
<li><strong>Error responses</strong>: Verify command format and check error code</li>
<li><strong>Timeout</strong>: Increase timeout value or try again</li>
</ul>
<h3 id="message-issues">Message Issues</h3>
<ul>
<li><strong>Messages not received</strong>: Poll <code>GET_MESSAGE</code> command periodically</li>
<li><strong>Duplicate messages</strong>: Implement message deduplication using timestamp/content as a unique id</li>
<li><strong>Message truncation</strong>: Send long messages as separate shorter messages</li>
</ul>
</section>
</main>
</div>
<hr><footer>
<div class="terminal-mkdocs-footer-grid">
<div id="terminal-mkdocs-footer-copyright-info">
Site built with <a href="http://www.mkdocs.org">MkDocs</a> and <a href="https://github.com/ntno/mkdocs-terminal">Terminal for MkDocs</a>.
</div>
</div>
</footer>
</div>
<div class="modal" id="mkdocs_search_modal" tabindex="-1" role="alertdialog" aria-modal="true" aria-labelledby="searchModalLabel">
<div class="modal-dialog modal-lg" role="search">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="searchModalLabel">Search</h5>
<button type="button" class="close btn btn-default btn-ghost" data-dismiss="modal" aria-label="Close">x</button>
</div>
<div class="modal-body">
<p id="searchInputLabel">Type to start searching</p>
<form>
<div class="form-group">
<input type="search" class="form-control" aria-labelledby="searchInputLabel" placeholder="" id="mkdocs-search-query" title="Please enter search terms here">
</div>
</form>
<div id="mkdocs-search-results" data-no-results-text="No document matches found"></div>
</div>
<div class="modal-footer">
</div>
</div>
</div>
</div>
</body>
</html>