sMQTTBroker
sMQTTMessage.h
1#ifndef SMQTTMESSAGE_FILE
2#define SMQTTMESSAGE_FILE
3
4#include<vector>
5#include<string>
6
7#if (__cplusplus >= 201703L)
8#define MAYBE_UNUSED [[maybe_unused]]
9#else
10#define MAYBE_UNUSED
11#endif
12
13enum sMQTTError
14{
15 sMQTTOk = 0,
16 sMQTTNowhereToSend = 1,
17 sMQTTInvalidMessage = 2,
18};
19
20MAYBE_UNUSED static const char *debugMessageType[] = {
21 "Unknown",
22 "Connect",
23 "ConnAck",
24 "Publish",
25 "PubAck",
26 "PubRec",
27 "PubRel",
28 "PubComp",
29 "Subscribe",
30 "SubAck",
31 "UnSubscribe",
32 "UnSuback",
33 "PingReq",
34 "PingResp",
35 "Disconnect"
36};
37
38class sMQTTClient;
40{
41public:
42 enum Type
43 {
44 Unknown = 0,
45 Connect = 0x10,
46 ConnAck = 0x20,
47 Publish = 0x30,
48 PubAck = 0x40,
49 PubRec = 0x50,
50 PubRel = 0x60,
51 PubComp=0x70,
52 Subscribe = 0x80,
53 SubAck = 0x90,
54 UnSubscribe = 0xA0,
55 UnSuback = 0xB0,
56 PingReq = 0xC0,
57 PingResp = 0xD0,
58 Disconnect = 0xE0
59 };
60 enum State
61 {
62 FixedHeader = 0,
63 Length = 1,
64 VariableHeader = 2,
65 PayLoad = 3,
66 Complete = 4,
67 Error = 5,
68 Create = 6
69 };
71 sMQTTMessage(Type t, unsigned char bits_d3_d0 = 0);
72 void incoming(char byte);
73 void add(char byte) { incoming(byte); }
74 void add(const char* p, size_t len, bool addLength = true)
75 {
76 if (addLength)
77 {
78 buffer.reserve(buffer.size() + len + 2);
79 incoming(len >> 8);
80 incoming(len & 0xFF);
81 }
82 while (len--) incoming(*p++);
83 }
84 void add(const std::string &str) { add(str.c_str(), str.size()); }
85 const char* end() const { return &buffer[0] + buffer.size(); }
86 const char* getVHeader() const { return &buffer[vheader]; }
87 void reset();
88 Type type() const
89 {
90 return state == Complete ? static_cast<Type>(buffer[0] & 0XF0) : Unknown;
91 }
92 unsigned char QoS() {
93 return (buffer[0] & 0x6)>>1;
94 }
95 bool isRetained() {
96 return buffer[0] & 0x1;
97 }
98 sMQTTError sendTo(sMQTTClient *, bool needRecalc=true);
99 // buff is MSB/LSB/STRING
100 // output buff+=2, len=length(str)
101 static void getString(const char* &buff, unsigned short &len);
102private:
103 void create(Type type)
104 {
105 buffer.push_back((char)type);
106 buffer.push_back('\0'); // reserved for msg length
107 vheader = 2;
108 size = 0;
109 state = Create;
110 }
111 int encodeLength(char* msb, int length) const;
112
113 std::vector<char> buffer;
114 State state;
115 unsigned short multiplyer;
116 unsigned short size;
117 unsigned char vheader;
118};
119#endif
Main Client class.
Definition: sMQTTClient.h:22
Definition: sMQTTMessage.h:40