ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
mqtt_subscription_fingerprint.h
Go to the documentation of this file.
1#pragma once
2
3#include <atomic>
4#include <cstddef>
5#include <cstdint>
6
8
9namespace esphome::mqtt {
10
12inline uint32_t add_subscription_fingerprint(uint32_t fingerprint, const char *topic, uint8_t qos) {
13 return fingerprint + fnv1a_hash_extend(fnv1a_hash(topic), qos);
14}
15
17static constexpr uint32_t UNTRUSTED_SESSION_FINGERPRINT = 0;
18
20constexpr bool session_has_subscriptions(bool session_present, uint32_t trusted, uint32_t current) {
21 return session_present && trusted != UNTRUSTED_SESSION_FINGERPRINT && trusted == current;
22}
23
25static constexpr uint8_t SUBACK_FAILURE = 0x80;
26
28constexpr bool subscribe_granted(uint8_t return_code) { return return_code < SUBACK_FAILURE; }
29
35
39 public:
40 void reset() {
41 this->received_.store(0, std::memory_order_relaxed);
42 this->granted_.store(0, std::memory_order_relaxed);
43 }
44 void add(uint8_t return_code) {
45 if (subscribe_granted(return_code))
46 this->granted_.store(this->granted_.load(std::memory_order_relaxed) + 1, std::memory_order_relaxed);
47 // Released after granted_, so a reader that sees this SUBACK in received_ also sees it in granted_
48 this->received_.store(this->received_.load(std::memory_order_relaxed) + 1, std::memory_order_release);
49 }
50 uint16_t received() const { return this->received_.load(std::memory_order_acquire); }
52 SubackTally tally(uint16_t sent) const {
53 const uint16_t received = this->received();
54 if (received < sent)
56 // More than sent means a SUBACK from an earlier connection was counted, so none can be trusted
57 return received == sent && this->granted_.load(std::memory_order_relaxed) == sent
60 }
61
62 protected:
63 std::atomic<uint16_t> received_{0};
64 std::atomic<uint16_t> granted_{0};
65};
66
68static constexpr size_t INBOUND_SLOTS_PER_SUBSCRIBE = 2;
70static constexpr size_t INBOUND_SLOTS_PER_SUBSCRIBE_WITH_ACK = INBOUND_SLOTS_PER_SUBSCRIBE + 1;
71
73constexpr bool subscribe_fits(uint16_t sent, uint16_t answered, size_t free_slots,
74 size_t slots_per_subscribe = INBOUND_SLOTS_PER_SUBSCRIBE) {
75 const auto diff = static_cast<int16_t>(sent - answered);
76 const size_t unanswered = diff > 0 ? diff : 0;
77 return (unanswered + 1) * slots_per_subscribe <= free_slots;
78}
79
81constexpr uint16_t rebase_sent(uint16_t sent, uint16_t answered) {
82 return static_cast<int16_t>(answered - sent) > 0 ? answered : sent;
83}
84
85} // namespace esphome::mqtt
SUBACKs received on the current connection.
SubackTally tally(uint16_t sent) const
Compares the SUBACKs received with the number of SUBSCRIBEs sent on this connection.
constexpr uint16_t rebase_sent(uint16_t sent, uint16_t answered)
The sent count raised to the SUBACKs counted, so stray SUBACKs give no credit; wrap safe.
uint32_t add_subscription_fingerprint(uint32_t fingerprint, const char *topic, uint8_t qos)
Adds one subscription to a fingerprint of a set of subscriptions; the order they are added in does no...
constexpr bool session_has_subscriptions(bool session_present, uint32_t trusted, uint32_t current)
True when a resumed session holds in full the subscriptions it is trusted with, and they are the curr...
constexpr bool subscribe_granted(uint8_t return_code)
True when a SUBACK return code grants the subscription.
constexpr bool subscribe_fits(uint16_t sent, uint16_t answered, size_t free_slots, size_t slots_per_subscribe=INBOUND_SLOTS_PER_SUBSCRIBE)
Whether one more SUBSCRIBE fits; wrap safe.
constexpr uint32_t fnv1a_hash_extend(uint32_t hash, const char *str)
Extend a FNV-1a hash with additional string data.
Definition helpers.h:995
constexpr uint32_t fnv1a_hash(const char *str)
Calculate a FNV-1a hash of str.
Definition helpers.h:1018