30 #define MQTT_KEEPALIVE 15
56 #ifdef _GLIBCXX_FUNCTIONAL
57 typedef std::function<bool(Client&)> payload_callback_t;
59 typedef bool(*payload_callback_t)(Client&);
69 Client* _stream_client;
70 payload_callback_t _payload_callback;
75 _packet_id(0), _need_packet_id(false),
139 bool send(Client& client);
142 message_type
type(
void)
const {
return _type; }
145 bool has_stream(
void)
const {
return _stream_client != NULL; }
165 String _will_message;
166 String _username, _password;
170 uint32_t variable_header_length(
void)
const;
171 void write_variable_header(uint8_t *buf, uint32_t& bufpos)
const;
172 uint32_t payload_length(
void)
const;
173 void write_payload(uint8_t *buf, uint32_t& bufpos)
const;
175 message_type response_type(
void)
const {
return CONNACK; }
187 Connect&
set_will(String willTopic, String willMessage, uint8_t willQos = 0,
bool willRetain =
false) {
188 _will_topic = willTopic; _will_message = willMessage; _will_qos = willQos; _will_retain = willRetain;
210 bool _session_present;
225 uint32_t _payload_len;
228 uint32_t variable_header_length(
void)
const;
229 void write_variable_header(uint8_t *buf, uint32_t& bufpos)
const;
230 uint32_t payload_length(
void)
const;
231 void write_payload(uint8_t *buf, uint32_t& bufpos)
const;
233 message_type response_type(
void)
const;
239 _payload(payload), _payload_len(length),
244 Publish(uint8_t flags, uint8_t* data, uint32_t length);
247 Publish(uint8_t flags, Client& client, uint32_t remaining_length);
257 Publish(String topic, String payload);
265 Publish(String topic, uint8_t* payload, uint32_t length) :
266 Publish(topic, payload, length, false)
275 Publish(String topic, payload_callback_t pcb, uint32_t length);
278 Publish(String topic,
const __FlashStringHelper* payload);
285 bool retain(
void)
const {
return _flags & 0x01; }
292 uint8_t
qos(
void)
const {
return (_flags >> 1) & 0x03; }
299 bool dup(
void)
const {
return (_flags >> 3) & 0x01; }
301 Publish&
set_dup(
bool d =
true) { _flags = (_flags & ~0x08) | (d ? 0x08 : 0);
return *
this; }
306 String
topic(
void)
const {
return _topic; }
312 uint8_t*
payload(
void)
const {
return _payload; }
321 Publish
Publish_P(String topic, PGM_P payload, uint32_t length);
342 uint32_t variable_header_length(
void)
const;
343 void write_variable_header(uint8_t *buf, uint32_t& bufpos)
const;
345 message_type response_type(
void)
const {
return PUBREL; }
362 uint32_t variable_header_length(
void)
const;
363 void write_variable_header(uint8_t *buf, uint32_t& bufpos)
const;
365 message_type response_type(
void)
const {
return PUBCOMP; }
382 uint32_t variable_header_length(
void)
const;
383 void write_variable_header(uint8_t *buf, uint32_t& bufpos)
const;
403 uint32_t variable_header_length(
void)
const;
404 void write_variable_header(uint8_t *buf, uint32_t& bufpos)
const;
405 uint32_t payload_length(
void)
const;
406 void write_payload(uint8_t *buf, uint32_t& bufpos)
const;
408 message_type response_type(
void)
const {
return SUBACK; }
415 Subscribe(String topic, uint8_t qos = 0);
435 SubscribeAck(Client& client, uint32_t remaining_length);
443 uint32_t
num_rcs(
void)
const {
return _num_rcs; }
446 uint8_t
rc(uint8_t i)
const {
return _rcs[i]; }
460 uint32_t variable_header_length(
void)
const;
461 void write_variable_header(uint8_t *buf, uint32_t& bufpos)
const;
462 uint32_t payload_length(
void)
const;
463 void write_payload(uint8_t *buf, uint32_t& bufpos)
const;
465 message_type response_type(
void)
const {
return UNSUBACK; }
496 message_type response_type(
void)
const {
return PINGRESP; }
Main do-everything class that sketches will use.
Definition: PubSubClient.h:20
String topic(void) const
Get the topic string.
Definition: MQTT.h:306
Unsubscribe()
Constructor that starts with an empty list of unsubscriptions.
Definition: MQTT.cpp:653
friend Message * readPacket(Client &client)
Parser.
Definition: MQTT.cpp:180
friend Publish Publish_P(String topic, PGM_P payload, uint32_t length)
A function made to look like a constructor, reading the payload from flash.
Definition: MQTT.cpp:373
Subscribe & add_topic(String topic, uint8_t qos=0)
Add another topic and optional QoS level.
Definition: MQTT.cpp:589
uint8_t rc(uint8_t i) const
Get a return code.
Definition: MQTT.h:446
Subscribe to one or more topics.
Definition: MQTT.h:398
virtual uint32_t variable_header_length(void) const
Length of variable header.
Definition: MQTT.h:113
bool dup(void) const
Get dup flag.
Definition: MQTT.h:299
Disconnect from the broker.
Definition: MQTT.h:519
Disconnect()
Constructor.
Definition: MQTT.h:522
bool send(Client &client)
Send the message out.
Definition: MQTT.cpp:143
uint32_t num_rcs(void) const
Get the number of return codes available.
Definition: MQTT.h:443
message_type type(void) const
Get the message type.
Definition: MQTT.h:142
virtual void write_variable_header(uint8_t *buf, uint32_t &bufpos) const
Write variable header.
Definition: MQTT.h:120
Response to Unsubscribe.
Definition: MQTT.h:483
bool _need_packet_id
Not all message types use a packet id, but most do.
Definition: MQTT.h:68
Response to Ping.
Definition: MQTT.h:508
Publish & set_dup(bool d=true)
Set dup flag.
Definition: MQTT.h:301
void write_fixed_header(uint8_t *buf, uint32_t &bufpos, uint32_t rlength) const
Write the fixed header to a buffer.
Definition: MQTT.cpp:115
uint16_t keepalive(void) const
Get the keepalive period.
Definition: MQTT.h:200
Subscribe()
Constructor that starts an empty list of subscriptions.
Definition: MQTT.cpp:568
Connect(String cid)
Connect with a client ID.
Definition: MQTT.cpp:269
virtual uint32_t payload_length(void) const
Length of payload.
Definition: MQTT.h:123
Message sent when connecting to a broker.
Definition: MQTT.h:157
friend Message * readPacket(Client &client)
Parser.
Definition: MQTT.cpp:180
virtual ~Message()
Virtual destructor.
Definition: MQTT.h:80
Message(message_type t, uint8_t f=0)
Private constructor from type and flags.
Definition: MQTT.h:73
uint8_t fixed_header_length(uint32_t rlength) const
Length of the fixed header.
Definition: MQTT.cpp:104
Response to PublishRel.
Definition: MQTT.h:380
Unsubscribe from one or more topics.
Definition: MQTT.h:455
Publish & unset_dup(void)
Unset dup flag.
Definition: MQTT.h:303
Connect & set_auth(String u, String p)
Set the username and password for authentication.
Definition: MQTT.h:195
Publish(String topic, uint8_t *payload, uint32_t length)
Constructor from arbitrary payload.
Definition: MQTT.h:265
friend Message * readPacket(Client &client)
Parser.
Definition: MQTT.cpp:180
Response to PublishRec.
Definition: MQTT.h:360
virtual void write_payload(uint8_t *buf, uint32_t &bufpos) const
Write payload.
Definition: MQTT.h:130
Response to Subscribe.
Definition: MQTT.h:426
Publish Publish_P(String topic, PGM_P payload, uint32_t length)
A function made to look like a constructor, reading the payload from flash.
Definition: MQTT.cpp:373
Response to Connect.
Definition: MQTT.h:208
Client * payload_stream(void) const
Get the network stream for reading the payload.
Definition: MQTT.h:317
friend Message * readPacket(Client &client)
Parser.
Definition: MQTT.cpp:180
Response to Publish when qos == 1.
Definition: MQTT.h:325
Connect & unset_will(void)
Unset the "will" flag and associated attributes.
Definition: MQTT.h:192
friend Message * readPacket(Client &client)
Parser.
Definition: MQTT.cpp:180
Publish & unset_retain(void)
Unset retain flag.
Definition: MQTT.h:289
Ping the broker.
Definition: MQTT.h:494
void set_packet_id(uint16_t pid)
Set the packet id.
Definition: MQTT.h:100
bool need_packet_id(void) const
Does this message need a packet id before being sent?
Definition: MQTT.h:97
Connect & set_clean_session(bool cs=true)
Set the "clear session" flag.
Definition: MQTT.h:182
Publish a payload to a topic.
Definition: MQTT.h:221
uint16_t packet_id(void) const
Get the packet id.
Definition: MQTT.h:103
Unsubscribe & add_topic(String topic)
Add another topic to unsubscribe from.
Definition: MQTT.cpp:673
bool retain(void) const
Get retain flag.
Definition: MQTT.h:285
Publish & set_qos(uint8_t q)
Set QoS value.
Definition: MQTT.cpp:431
String payload_string(void) const
Get the payload as a string.
Definition: MQTT.cpp:443
bool has_stream(void) const
Does this message have a network stream for reading the (large) payload?
Definition: MQTT.h:145
uint32_t payload_len(void) const
Get the payload length.
Definition: MQTT.h:314
PingResp()
Constructor.
Definition: MQTT.h:511
Publish & set_retain(bool r=true)
Set retain flag.
Definition: MQTT.h:287
friend Message * readPacket(Client &client)
Parser.
Definition: MQTT.cpp:180
uint8_t * payload(void) const
Get the payload pointer.
Definition: MQTT.h:312
Connect & set_will(String willTopic, String willMessage, uint8_t willQos=0, bool willRetain=false)
Set the "will" flag and associated attributes.
Definition: MQTT.h:187
First response to Publish when qos == 2.
Definition: MQTT.h:340
Publish & unset_qos(void)
Unset QoS value.
Definition: MQTT.h:296
Abstract base class.
Definition: MQTT.h:63
Ping()
Constructor.
Definition: MQTT.h:500
Connect & set_keepalive(uint16_t k)
Set the keepalive period.
Definition: MQTT.h:202
friend Message * readPacket(Client &client)
Parser.
Definition: MQTT.cpp:180
uint8_t next_rc(void) const
Get the next return code from a stream.
Definition: MQTT.cpp:647
void write_packet_id(uint8_t *buf, uint32_t &bufpos) const
Write the packet id to a buffer.
Definition: MQTT.cpp:139
uint8_t qos(void) const
Get QoS value.
Definition: MQTT.h:292
Message * readPacket(Client &client)
Parser.
Definition: MQTT.cpp:180
friend Message * readPacket(Client &client)
Parser.
Definition: MQTT.cpp:180
virtual message_type response_type(void) const
Message type to expect in response to this message.
Definition: MQTT.h:133
Connect & unset_clean_session(void)
Unset the "clear session" flag.
Definition: MQTT.h:184
Connect & unset_auth(void)
Unset the username and password for authentication.
Definition: MQTT.h:197
namespace for classes representing MQTT messages
Definition: MQTT.cpp:23