Files
pyxis/lib/ble_interface/BLEFragmenter.h
torlando-agent[bot] 70d4aa6be9 feat: graft pyxis onto upstream microReticulum 0.4.1
Repins microReticulum + microLXMF onto the upstream-0.4.1 graft and adapts
pyxis to the new src/microReticulum/ layout and 0.4.x APIs. The far-diverged
0.3.0 fork's Resource/Transport/Identity work is subsumed by upstream's
reimplementation; only the still-needed fixes ride on the pinned branches
(PKCS7/HMAC/X25519 crypto -- proven byte-identical to python RNS 1.3.1 --
Packet link-proof callback, Identity short-sig guard, and the bz2 layer +
decompress-on-receive in Resource::assemble()).

Consumer-side changes:
- platformio.ini: pin microReticulum @2f21fee (pyxis-fixes-on-0.4.1) and
  microLXMF @33760d0 (chore/microreticulum-0.4.1-layout); bump microStore
  ceea8f5 -> c5fb69d (0.4.x requires the new BasicFileStore::init API);
  -std=gnu++11 -> gnu++17 (upstream requires C++17).
- Namespace all microReticulum includes (angle + quote) to <microReticulum/...>
  for the relocated layout; shim-local Utilities/Stream.h|Print.h preserved.
- Interface::send_outgoing now returns bool: update TCP/BLE/SX1262/Auto
  overrides with correct success/failure returns.
- SDArchiveFileSystem::init(bool reformatOnFail=true) to match new microStore.
- Static Transport::get_path_table() -> path_table(); instance getter unchanged.
- Remove duplicate shim Cryptography/BZ2 (microReticulum provides it now; keep
  lib/libbz2 as the ESP32 bzlib provider).
- patch_littlefs_paths.py: normalize microStore's LittleFS adapter paths to a
  leading "/" -- ESP32 Arduino LittleFS rejects "./"-prefixed paths, which
  silently broke the path store (no peer paths learned, all messaging blocked).

Validated on T-Deck Plus: builds (RAM 27.5% / Flash 77.7%), boots stable
(no WDT/panic), and a full on-device LXMF e2e (DIRECT + OPPORTUNISTIC +
bz2-compressed-Resource receive) passes 5/5.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UWZuYkHBRqNb6BZHV8sTG5
2026-06-19 15:49:44 -04:00

126 lines
3.9 KiB
C++

/**
* @file BLEFragmenter.h
* @brief BLE-Reticulum Protocol v2.2 packet fragmenter
*
* Fragments outgoing Reticulum packets into BLE-sized chunks with the v2.2
* 5-byte header format. This class has no BLE dependencies and can be used
* for testing on native builds.
*
* Fragment Header Format (5 bytes):
* Byte 0: Type (0x01=START, 0x02=CONTINUE, 0x03=END)
* Bytes 1-2: Sequence number (big-endian uint16_t)
* Bytes 3-4: Total fragments (big-endian uint16_t)
* Bytes 5+: Payload data
*/
#pragma once
#include "BLETypes.h"
#include <microReticulum/Bytes.h>
#include <vector>
#include <cstdint>
namespace RNS { namespace BLE {
class BLEFragmenter {
public:
/**
* @brief Construct a fragmenter with specified MTU
* @param mtu The negotiated BLE MTU (default: minimum BLE MTU of 23)
*/
explicit BLEFragmenter(size_t mtu = MTU::MINIMUM);
/**
* @brief Set the MTU for fragmentation calculations
*
* Call this when MTU is renegotiated with a peer.
* @param mtu The new MTU value
*/
void setMTU(size_t mtu);
/**
* @brief Get the current MTU
*/
size_t getMTU() const { return _mtu; }
/**
* @brief Get the maximum payload size per fragment (MTU - HEADER_SIZE)
*/
size_t getPayloadSize() const { return _payload_size; }
/**
* @brief Check if a packet needs fragmentation
* @param data The packet to check
* @return true if packet exceeds single fragment payload capacity
*/
bool needsFragmentation(const Bytes& data) const;
/**
* @brief Calculate number of fragments needed for a packet
* @param data_size Size of the data to fragment
* @return Number of fragments needed (minimum 1)
*/
uint16_t calculateFragmentCount(size_t data_size) const;
/**
* @brief Fragment a packet into BLE-sized chunks
*
* @param data The complete packet to fragment
* @param sequence_base Starting sequence number (default 0)
* @return Vector of fragments, each with the 5-byte header prepended
*
* For a single-fragment packet, returns one fragment with type=END.
* For multi-fragment packets:
* - First fragment has type=START
* - Middle fragments have type=CONTINUE
* - Last fragment has type=END
*/
std::vector<Bytes> fragment(const Bytes& data, uint16_t sequence_base = 0);
/**
* @brief Create a single fragment with proper header
*
* @param type Fragment type (START, CONTINUE, END)
* @param sequence Sequence number for this fragment
* @param total_fragments Total number of fragments in this message
* @param payload The fragment payload data
* @return Complete fragment with 5-byte header prepended
*/
static Bytes createFragment(Fragment::Type type, uint16_t sequence,
uint16_t total_fragments, const Bytes& payload);
/**
* @brief Parse the header from a received fragment
*
* @param fragment The received fragment data (must be at least HEADER_SIZE bytes)
* @param type Output: fragment type
* @param sequence Output: sequence number
* @param total_fragments Output: total fragment count
* @return true if header is valid and was parsed successfully
*/
static bool parseHeader(const Bytes& fragment, Fragment::Type& type,
uint16_t& sequence, uint16_t& total_fragments);
/**
* @brief Extract payload from a fragment (removes header)
*
* @param fragment The complete fragment with header
* @return The payload portion (empty if fragment is too small)
*/
static Bytes extractPayload(const Bytes& fragment);
/**
* @brief Validate a fragment header
*
* @param fragment The fragment to validate
* @return true if the fragment has a valid header
*/
static bool isValidFragment(const Bytes& fragment);
private:
size_t _mtu;
size_t _payload_size;
};
}} // namespace RNS::BLE