AlteriomPainlessMesh v2.1.1
Easy ESP32/ESP8266 mesh networking with Alteriom IoT extensions
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | List of all members
painlessmesh::gateway::GatewayDataPackage Class Reference

Gateway Data Package for routing Internet requests through mesh. More...

#include <gateway.hpp>

Inheritance diagram for painlessmesh::gateway::GatewayDataPackage:
Inheritance graph
[legend]
Collaboration diagram for painlessmesh::gateway::GatewayDataPackage:
Collaboration graph
[legend]

Public Member Functions

 GatewayDataPackage ()
 Default constructor.
 
 GatewayDataPackage (JsonObject jsonObj)
 Construct from JSON object.
 
JsonObject addTo (JsonObject &&jsonObj) const
 Serialize to JSON object.
 
size_t jsonObjectSize () const
 Calculate JSON object size for ArduinoJson v6.
 
- Public Member Functions inherited from painlessmesh::plugin::SinglePackage
 SinglePackage (int type)
 
 SinglePackage (JsonObject jsonObj)
 
JsonObject addTo (JsonObject &&jsonObj) const
 
- Public Member Functions inherited from painlessmesh::protocol::PackageInterface
virtual ~PackageInterface ()=default
 

Static Public Member Functions

static uint32_t generateMessageId (uint32_t nodeId)
 Generate a unique message ID.
 

Public Attributes

uint32_t messageId = 0
 Unique message identifier.
 
uint32_t originNode = 0
 Node ID that originated this message.
 
uint32_t timestamp = 0
 Creation timestamp.
 
uint8_t priority = static_cast<uint8_t>(GatewayPriority::PRIORITY_NORMAL)
 Message priority (0=CRITICAL, 1=HIGH, 2=NORMAL, 3=LOW)
 
TSTRING destination = ""
 Destination URL or endpoint.
 
TSTRING payload = ""
 Application payload data.
 
TSTRING contentType = "application/json"
 MIME content type.
 
uint8_t retryCount = 0
 Number of relay attempts.
 
bool requiresAck = false
 Whether acknowledgment is required.
 
uint32_t requestNonce = 0
 Random value drawn once per sendToInternet() call.
 
- Public Attributes inherited from painlessmesh::plugin::SinglePackage
uint32_t from = 0
 
uint32_t dest = 0
 
router::Type routing = router::SINGLE
 
int type = 0
 
int noJsonFields = 4
 

Static Public Attributes

static constexpr int numPackageFields = 10
 Number of additional JSON fields in this package.
 

Detailed Description

Gateway Data Package for routing Internet requests through mesh.

This package enables mesh nodes to send data through a gateway node to the Internet. It provides a standardized format for:

MEMORY FOOTPRINT

The GatewayDataPackage structure has an estimated memory footprint of:

For ESP8266 with ~80KB RAM, keep payload under 1KB for safety. For ESP32 with ~320KB RAM, larger payloads are acceptable.

MESSAGE ID GENERATION

Use generateMessageId(nodeId) to create unique message IDs. The ID combines a per-node counter with the node ID to ensure uniqueness across the mesh network.

Example usage:

pkg.originNode = mesh.getNodeId();
pkg.timestamp = mesh.getNodeTime();
pkg.priority = static_cast<uint8_t>(GatewayPriority::PRIORITY_NORMAL);
pkg.destination = "https://api.example.com/data";
pkg.payload = "{\"sensor\": 42}";
pkg.contentType = "application/json";
pkg.requiresAck = true;
mesh.sendPackage(&pkg);
Gateway Data Package for routing Internet requests through mesh.
Definition gateway.hpp:815
uint32_t timestamp
Creation timestamp.
Definition gateway.hpp:839
uint8_t priority
Message priority (0=CRITICAL, 1=HIGH, 2=NORMAL, 3=LOW)
Definition gateway.hpp:847
TSTRING destination
Destination URL or endpoint.
Definition gateway.hpp:857
TSTRING payload
Application payload data.
Definition gateway.hpp:865
TSTRING contentType
MIME content type.
Definition gateway.hpp:875
uint32_t messageId
Unique message identifier.
Definition gateway.hpp:823
bool requiresAck
Whether acknowledgment is required.
Definition gateway.hpp:891
uint32_t originNode
Node ID that originated this message.
Definition gateway.hpp:831
static uint32_t generateMessageId(uint32_t nodeId)
Generate a unique message ID.
Definition gateway.hpp:1011
@ PRIORITY_NORMAL
Normal priority - standard processing.

Type ID: 620 (GATEWAY_DATA) Base class: SinglePackage (routed to specific gateway node)

Constructor & Destructor Documentation

◆ GatewayDataPackage() [1/2]

painlessmesh::gateway::GatewayDataPackage::GatewayDataPackage ( )
inline

Default constructor.

Creates a GatewayDataPackage with type ID 620 (GATEWAY_DATA).

◆ GatewayDataPackage() [2/2]

painlessmesh::gateway::GatewayDataPackage::GatewayDataPackage ( JsonObject  jsonObj)
inline

Construct from JSON object.

Deserializes a GatewayDataPackage from a JSON object. Compatible with ArduinoJson v6 and v7.

Parameters
jsonObjJSON object containing package data

Member Function Documentation

◆ addTo()

JsonObject painlessmesh::gateway::GatewayDataPackage::addTo ( JsonObject &&  jsonObj) const
inlinevirtual

Serialize to JSON object.

Adds all package fields to the provided JSON object.

Parameters
jsonObjJSON object to add fields to
Returns
The modified JSON object

Implements painlessmesh::protocol::PackageInterface.

◆ generateMessageId()

static uint32_t painlessmesh::gateway::GatewayDataPackage::generateMessageId ( uint32_t  nodeId)
inlinestatic

Generate a unique message ID.

Creates a unique message ID by combining a per-node counter with the node ID. This ensures uniqueness across the mesh even if multiple nodes generate IDs simultaneously.

The ID format is:

  • Upper 16 bits: Lower 16 bits of node ID
  • Lower 16 bits: Incrementing counter (wraps at 65535)
Note
This function is not thread-safe. On ESP8266/ESP32, this is acceptable as the main loop is single-threaded. For multi-threaded environments, consider using atomic operations.
Parameters
nodeIdThe ID of the node generating the message
Returns
A unique message ID

◆ jsonObjectSize()

size_t painlessmesh::gateway::GatewayDataPackage::jsonObjectSize ( ) const
inlinevirtual

Calculate JSON object size for ArduinoJson v6.

Used for buffer allocation when serializing.

Returns
Estimated size in bytes

Implements painlessmesh::protocol::PackageInterface.

Member Data Documentation

◆ contentType

TSTRING painlessmesh::gateway::GatewayDataPackage::contentType = "application/json"

MIME content type.

Describes the format of the payload. Common values:

  • "application/json"
  • "text/plain"
  • "application/octet-stream"

◆ destination

TSTRING painlessmesh::gateway::GatewayDataPackage::destination = ""

Destination URL or endpoint.

The Internet destination for this data. Examples:

  • "https://api.example.com/sensor"
  • "mqtt://broker.example.com/topic"
  • "wss://ws.example.com/stream"

◆ messageId

uint32_t painlessmesh::gateway::GatewayDataPackage::messageId = 0

Unique message identifier.

Generated using generateMessageId() to ensure uniqueness across the mesh. Used for tracking, acknowledgment, and deduplication.

◆ numPackageFields

constexpr int painlessmesh::gateway::GatewayDataPackage::numPackageFields = 10
staticconstexpr

Number of additional JSON fields in this package.

Used for jsonObjectSize() calculation in ArduinoJson v6. Count: msgId, origin, ts, prio, dest_url, payload, content, retry, ack, nonce = 10 fields

◆ originNode

uint32_t painlessmesh::gateway::GatewayDataPackage::originNode = 0

Node ID that originated this message.

The node that created the message, which may differ from the 'from' field during relay operations.

◆ payload

TSTRING painlessmesh::gateway::GatewayDataPackage::payload = ""

Application payload data.

The actual data to send to the destination. Format depends on contentType (JSON, binary, etc.).

◆ priority

uint8_t painlessmesh::gateway::GatewayDataPackage::priority = static_cast<uint8_t>(GatewayPriority::PRIORITY_NORMAL)

Message priority (0=CRITICAL, 1=HIGH, 2=NORMAL, 3=LOW)

Determines processing order at the gateway. Use GatewayPriority enum for type-safe values.

◆ requestNonce

uint32_t painlessmesh::gateway::GatewayDataPackage::requestNonce = 0

Random value drawn once per sendToInternet() call.

The same on every attempt at that call, and part of its X-Request-Id and Idempotency-Key (requestIdFor()). messageId alone repeats: its counter is 16 bits, so a node that sends more than 65,535 requests in one boot reissues old ids, and a service that remembers keys would drop the new request as a repeat. 0 from a node that predates the field. JSON key "nonce", omitted when 0.

◆ requiresAck

bool painlessmesh::gateway::GatewayDataPackage::requiresAck = false

Whether acknowledgment is required.

When true, the gateway should send a response back confirming successful delivery to the Internet destination.

◆ retryCount

uint8_t painlessmesh::gateway::GatewayDataPackage::retryCount = 0

Number of relay attempts.

Incremented each time the message is relayed. Can be used for hop counting and loop detection.

◆ timestamp

uint32_t painlessmesh::gateway::GatewayDataPackage::timestamp = 0

Creation timestamp.

Mesh time when the message was created. Used for TTL calculations and ordering.


The documentation for this class was generated from the following file: