Skip to content

Commit 08a6880

Browse files
Add feature flag to set max payload size
1 parent 3b18596 commit 08a6880

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

mqttrust_core/Cargo.toml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,17 @@ dns-lookup = "1.0.3"
4343
env_logger = "0.9.0"
4444

4545
[features]
46-
default = []
46+
default = ["max_payload_size_4096"]
47+
max_payload_size_2048 = []
48+
max_payload_size_4096 = []
49+
max_payload_size_8192 = []
50+
4751

4852
std = []
4953

50-
defmt-impl = ["defmt", "mqttrust/defmt-impl", "heapless/defmt-impl", "fugit/defmt"]
54+
defmt-impl = [
55+
"defmt",
56+
"mqttrust/defmt-impl",
57+
"heapless/defmt-impl",
58+
"fugit/defmt",
59+
]

mqttrust_core/src/eventloop.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::max_payload::MAX_PAYLOAD_SIZE;
12
use crate::options::Broker;
23
use crate::packet::SerializedPacket;
34
use crate::state::{MqttConnectionStatus, MqttState};
@@ -417,7 +418,7 @@ impl<S> NetworkHandle<S> {
417418
#[derive(Debug)]
418419
struct PacketBuffer {
419420
range: RangeTo<usize>,
420-
buffer: Vec<u8, 4096>,
421+
buffer: Vec<u8, { MAX_PAYLOAD_SIZE }>,
421422
}
422423

423424
impl PacketBuffer {

mqttrust_core/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub(crate) mod fmt;
88

99
mod client;
1010
mod eventloop;
11+
mod max_payload;
1112
mod options;
1213
mod packet;
1314
mod state;
@@ -18,6 +19,7 @@ pub use client::Client;
1819
use core::convert::TryFrom;
1920
pub use eventloop::EventLoop;
2021
use heapless::{String, Vec};
22+
use max_payload::MAX_PAYLOAD_SIZE;
2123
pub use mqttrust::encoding::v4::{Pid, Publish, QoS, QosPid, Suback};
2224
pub use mqttrust::*;
2325
pub use options::{Broker, MqttOptions};
@@ -30,7 +32,7 @@ pub struct PublishNotification {
3032
pub qospid: QoS,
3133
pub retain: bool,
3234
pub topic_name: String<256>,
33-
pub payload: Vec<u8, 4096>,
35+
pub payload: Vec<u8, MAX_PAYLOAD_SIZE>,
3436
}
3537

3638
/// Includes incoming packets from the network and other interesting events

mqttrust_core/src/max_payload.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#[cfg(not(any(
2+
feature = "max_payload_size_2048",
3+
feature = "max_payload_size_4096",
4+
feature = "max_payload_size_8192"
5+
)))]
6+
pub const MAX_PAYLOAD_SIZE: usize = 4096;
7+
8+
#[cfg(feature = "max_payload_size_2048")]
9+
pub const MAX_PAYLOAD_SIZE: usize = 2048;
10+
11+
#[cfg(feature = "max_payload_size_4096")]
12+
pub const MAX_PAYLOAD_SIZE: usize = 4096;
13+
14+
#[cfg(feature = "max_payload_size_8192")]
15+
pub const MAX_PAYLOAD_SIZE: usize = 8192;

0 commit comments

Comments
 (0)