# T1 MRSH Mesh Routing — main.cpp integration snippet

`mesh_routing.h/.cpp` is a compile-out module. To enable, add to
`platformio.ini` build_flags:

```
build_flags = -D PDL_ENABLE_MESH_ROUTING
```

## setup() addition

```cpp
#include "mesh_routing.h"

// Derive own node id from MAC (or reuse DEFAULT_SENSOR_ID)
char own_id[17];
snprintf(own_id, sizeof(own_id), "heltec-v4-%06x", (uint32_t)(ESP.getEfuseMac() & 0xFFFFFF));

// Advertise which radios are physically present
const uint8_t radios =
      MRSH_RADIO_LORA915      // SX1262
    | MRSH_RADIO_WIFI_2G4;    // ESP32-S3 builtin
mesh_routing_init(own_id, radios);
```

## Main loop — periodic tick

Immediately after the existing sensor loop work, add:

```cpp
mesh_routing_tick();  // ~1 Hz is enough; internally rate-limited
```

## Main loop RX dispatcher — add three new cases

Around line 277 in `main.cpp` the LoRa RX dispatcher already has a
`switch(msg_type)`; add three cases:

```cpp
switch (msg_type) {
    case MSG_TYPE_CANDIDATE:
    case MSG_TYPE_CORROBORATE:
        ffps_handle_packet(rx_buf, rx_len, rx_rssi);
        break;

    case MSG_TYPE_OTA_CHUNK:
        ota_handle_chunk(rx_buf, rx_len);
        break;

    case MSG_TYPE_COMMAND:
        handleIncomingCommand(rx_buf, (size_t)rx_len);
        break;

    case MSG_TYPE_UAV_CMD:                          // 0x0A
        uav_relay_handle(rx_buf, (size_t)rx_len);
        break;

    case MSG_TYPE_UAV_ACK:                          // 0x0B
        break;

    case MSG_TYPE_MESH_ROUTE:                       // 0x0D — NEW
        mesh_handle_route_packet(rx_buf, (size_t)rx_len);
        break;

    case MSG_TYPE_MESH_ACK:                         // 0x0E — NEW
        mesh_handle_ack(rx_buf, (size_t)rx_len);
        break;

    case MSG_TYPE_MESH_DISCOVER:                    // 0x0F — NEW
        mesh_handle_discover(rx_buf, (size_t)rx_len);
        break;

    default:
        break;
}
```

**Note**: the spec originally proposed 0x0B/0x0C/0x0D for MESH_ROUTE/ACK/DISCOVER
but 0x0B already belongs to `MSG_TYPE_UAV_ACK` (owned by `uav_relay.h`). To
avoid collision with the parallel UAV agent's files, MRSH uses
0x0D/0x0E/0x0F instead. The worker + gateway side both use these same
values (see `mesh_routing.h`).

## LoRa TX hook

MRSH forwards/ACKs packets via a weak `mesh_lora_send(buf, len)` symbol.
If your firmware already has a LoRa TX primitive (e.g., `mesh_transmit`
in `mesh_publish.cpp`), provide a thin shim:

```cpp
extern "C" int mesh_lora_send(const uint8_t* data, size_t len) {
    return mesh_transmit_raw(data, len);   // return 0 on success, -1 on error
}
```

## Compile-out behavior

When `PDL_ENABLE_MESH_ROUTING` is undefined, the `.cpp` implementation is
still compiled, but the module costs nothing in runtime work unless the
RX dispatcher actually delivers MESH_* msg_types. To fully compile it out,
wrap the `.cpp` body in `#ifdef PDL_ENABLE_MESH_ROUTING` and stub the
`.h` entry points.

## Delivery callback

By default, MESH_ROUTE packets addressed to this node get forwarded to
`uav_relay_handle()` (weak linkage — no-op if `uav_relay.cpp` is not
linked). Override with:

```cpp
mesh_set_deliver_callback([](const uint8_t* payload, size_t len) {
    // your own handler
});
```
