Annotate

mod_pubsub_mqtt/mqtt.lib.lua @ 5931:d194d1012fd3

Updating dox for mod_rest. Ideas expressed / clarified: 1) Making clear that mod_rest isn't to be installed under VirtualHosts AND as a component. 2) Understanding some of the implications of this choice: A) Changes to user authentication B) How it affects subdomains 3) More consistent use of domain names for clarity. 4) Using different heading sizes to show scope of section. Essentially, I added all the tidbits I had to clarify in getting this to work in my own example.
author Ben Smith <bens@effortlessis.com>
date Mon, 13 May 2024 13:25:13 -0700
parent 5857:58df53eefa28
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5857
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
1 local bit = require "util.bitcompat";
1240
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local stream_mt = {};
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 stream_mt.__index = stream_mt;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 function stream_mt:read_bytes(n_bytes)
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 module:log("debug", "Reading %d bytes... (buffer: %d)", n_bytes, #self.buffer);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local data = self.buffer;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 if not data then
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 module:log("debug", "No data, pausing.");
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 data = coroutine.yield();
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 module:log("debug", "Have %d bytes of data now (want %d)", #data, n_bytes);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 if #data >= n_bytes then
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 data, self.buffer = data:sub(1, n_bytes), data:sub(n_bytes+1);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 elseif #data < n_bytes then
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 module:log("debug", "Not enough data (only %d bytes out of %d), pausing.", #data, n_bytes);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 self.buffer = data..coroutine.yield();
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 module:log("debug", "Now we have %d bytes, reading...", #data);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 return self:read_bytes(n_bytes);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 module:log("debug", "Returning %d bytes (buffer: %d)", #data, #self.buffer);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 return data;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 function stream_mt:read_string()
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 local len1, len2 = self:read_bytes(2):byte(1,2);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 local len = bit.lshift(len1, 8) + len2;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 return self:read_bytes(len), len+2;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31
5857
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
32 function stream_mt:read_word()
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
33 local len1, len2 = self:read_bytes(2):byte(1,2);
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
34 local result = bit.lshift(len1, 8) + len2;
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
35 module:log("debug", "read_word(%02x, %02x) = %04x (%d)", len1, len2, result, result);
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
36 return result;
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
37 end
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
38
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
39 local function hasbit(byte, n_bit)
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
40 return bit.band(byte, 2^n_bit) ~= 0;
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
41 end
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
42
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
43 local function encode_string(str)
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
44 return string.char(bit.band(#str, 0xff00), bit.band(#str, 0x00ff))..str;
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
45 end
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
46
1240
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 local packet_type_codes = {
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 "connect", "connack",
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 "publish", "puback", "pubrec", "pubrel", "pubcomp",
5857
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
50 "subscribe", "suback", "unsubscribe", "unsuback",
1240
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 "pingreq", "pingresp",
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 "disconnect"
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 };
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 function stream_mt:read_packet()
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 local packet = {};
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 local header = self:read_bytes(1):byte();
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 packet.type = packet_type_codes[bit.rshift(bit.band(header, 0xf0), 4)];
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 packet.dup = bit.band(header, 0x08) == 0x08;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 packet.qos = bit.rshift(bit.band(header, 0x06), 1);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 packet.retain = bit.band(header, 0x01) == 0x01;
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1240
diff changeset
62
1240
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 -- Get length
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 local length, multiplier = 0, 1;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 repeat
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 local digit = self:read_bytes(1):byte();
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 length = length + bit.band(digit, 0x7f)*multiplier;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 multiplier = multiplier*128;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 until bit.band(digit, 0x80) == 0;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 packet.length = length;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 if packet.type == "connect" then
5114
d2a84e6aed2b mod_pubsub_mqtt: Switch to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 1343
diff changeset
72 if self:read_string() ~= "MQTT" then
1240
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 module:log("warn", "Unexpected packet signature!");
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 packet.type = nil; -- Invalid packet
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 else
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 packet.version = self:read_bytes(1):byte();
5857
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
77 module:log("debug", "ver: %02x", packet.version);
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
78 if packet.version ~= 0x04 then
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
79 module:log("warn", "MQTT version mismatch (got %02x, we support %02x", packet.version, 0x04);
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
80 end
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
81 local flags = self:read_bytes(1):byte();
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
82 module:log("debug", "flags: %02x", flags);
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
83 packet.keepalive_timer = self:read_bytes(2):byte();
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
84 module:log("debug", "keepalive: %d", packet.keepalive_timer);
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
85 packet.connect_flags = {};
1240
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 length = length - 11;
5857
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
87 packet.connect_flags = {
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
88 clean_session = hasbit(flags, 1);
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
89 will = hasbit(flags, 2);
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
90 will_qos = bit.band(bit.rshift(flags, 2), 0x02);
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
91 will_retain = hasbit(flags, 5);
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
92 user_name = hasbit(flags, 7);
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
93 password = hasbit(flags, 6);
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
94 };
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
95 module:log("debug", "%s", require "util.serialization".serialize(packet.connect_flags, "debug"));
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
96 module:log("debug", "Reading client_id...");
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
97 packet.client_id = self:read_string();
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
98 if packet.connect_flags.will then
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
99 module:log("debug", "Reading will...");
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
100 packet.will = {
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
101 topic = self:read_string();
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
102 message = self:read_string();
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
103 qos = packet.connect_flags.will_qos;
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
104 retain = packet.connect_flags.will_retain;
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
105 };
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
106 end
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
107 if packet.connect_flags.user_name then
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
108 module:log("debug", "Reading username...");
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
109 packet.username = self:read_string();
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
110 end
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
111 if packet.connect_flags.password then
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
112 module:log("debug", "Reading password...");
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
113 packet.password = self:read_string();
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
114 end
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
115 module:log("debug", "Done parsing connect!");
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
116 length = 0; -- No payload left
1240
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 elseif packet.type == "publish" then
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 packet.topic = self:read_string();
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 length = length - (#packet.topic+2);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 if packet.qos == 1 or packet.qos == 2 then
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 packet.id = self:read_bytes(2);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 length = length - 2;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 elseif packet.type == "subscribe" then
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 if packet.qos == 1 or packet.qos == 2 then
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 packet.id = self:read_bytes(2);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 length = length - 2;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 local topics = {};
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 while length > 0 do
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 local topic, len = self:read_string();
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 table.insert(topics, topic);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 self:read_bytes(1); -- QoS not used
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 length = length - (len+1);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 packet.topics = topics;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 if length > 0 then
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 packet.data = self:read_bytes(length);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 end
5857
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
142 module:log("debug", "MQTT packet complete!");
1240
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 return packet;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 local function new_parser(self)
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 return coroutine.wrap(function (data)
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 self.buffer = data;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 while true do
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 data = coroutine.yield(self:read_packet());
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 module:log("debug", "Parser: %d new bytes", #data);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 self.buffer = (self.buffer or "")..data;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 end);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 function stream_mt:feed(data)
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 local packets = {};
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 local packet = self.parser(data);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 while packet do
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 module:log("debug", "Received packet");
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 table.insert(packets, packet);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 packet = self.parser("");
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 module:log("debug", "Returning %d packets", #packets);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 return packets;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 local function new_stream()
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 local stream = setmetatable({}, stream_mt);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 stream.parser = new_parser(stream);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 return stream;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 local function serialize_packet(packet)
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 local type_num = 0;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 for i, v in ipairs(packet_type_codes) do -- FIXME: I'm so tired right now.
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 if v == packet.type then
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 type_num = i;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 break;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 local header = string.char(bit.lshift(type_num, 4));
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1240
diff changeset
184
1240
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 if packet.type == "publish" then
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 local topic = packet.topic or "";
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 packet.data = string.char(bit.band(#topic, 0xff00), bit.band(#topic, 0x00ff))..topic..packet.data;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 elseif packet.type == "suback" then
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 local t = {};
5857
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
190 for i, result_code in ipairs(packet.results) do
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
191 table.insert(t, string.char(result_code));
1240
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 end
5857
58df53eefa28 mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents: 5114
diff changeset
193 packet.data = packet.id..table.concat(t);
1240
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 end
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1240
diff changeset
195
1240
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 -- Get length
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 local length = #(packet.data or "");
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 repeat
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199 local digit = length%128;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 length = math.floor(length/128);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 if length > 0 then
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 digit = bit.bor(digit, 0x80);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 header = header..string.char(digit); -- FIXME: ...
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 until length <= 0;
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1240
diff changeset
206
1240
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207 return header..(packet.data or "");
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 return {
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 new_stream = new_stream;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212 serialize_packet = serialize_packet;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213 };