forked from njh/NanodeMQTT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NanodeMQTT.cpp
495 lines (429 loc) · 12.4 KB
/
NanodeMQTT.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
* Copyright (c) 2011-2012, Nicholas Humfrey.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <NanodeMQTT.h>
#ifdef MQTT_DEBUG
static int serial_putc( char c, FILE * )
{
Serial.write( c );
return c;
}
#endif
NanodeMQTT::NanodeMQTT(NanodeUIP *uip)
{
this->uip = uip;
memset(this->client_id, 0, MQTT_MAX_CLIENT_ID_LEN+1);
this->port = MQTT_DEFAULT_PORT;
this->keep_alive = MQTT_DEFAULT_KEEP_ALIVE;
this->message_id = 0;
this->state = MQTT_STATE_DISCONNECTED;
this->ping_pending = 0;
this->blocking_mode = 1;
this->error_code = 0;
this->payload_length = 0;
this->payload_retain = 0;
this->subscribe_topic = NULL;
this->callback = NULL;
#ifdef MQTT_DEBUG
fdevopen( &serial_putc, 0 );
#endif
}
static void mqtt_appcall(void)
{
NanodeMQTT *mqtt = ((struct mqtt_app_state *)&(uip_conn->appstate))->mqtt;
if (uip_aborted()) {
MQTT_DEBUG_PRINTLN("TCP: Connection Aborted");
mqtt->tcp_closed();
}
if (uip_timedout()) {
MQTT_DEBUG_PRINTLN("TCP: Connection Timed Out");
mqtt->tcp_closed();
}
if (uip_closed()) {
MQTT_DEBUG_PRINTLN("TCP: Connection Closed");
mqtt->tcp_closed();
}
if (uip_connected()) {
MQTT_DEBUG_PRINTLN("TCP: Connection Established");
mqtt->tcp_connected();
}
if (uip_acked()) {
MQTT_DEBUG_PRINTLN("TCP: Acked");
mqtt->tcp_acked();
}
if (uip_newdata()) {
MQTT_DEBUG_PRINTLN("TCP: New Data");
mqtt->tcp_receive();
}
if (uip_rexmit()) {
MQTT_DEBUG_PRINTLN("TCP: Retransmit");
mqtt->tcp_transmit();
}
if (uip_poll()) {
mqtt->poll();
}
mqtt->check_timeout();
}
void NanodeMQTT::set_client_id(const char* client_id)
{
strncpy(this->client_id, client_id, MQTT_MAX_CLIENT_ID_LEN);
}
void NanodeMQTT::set_server_addr(byte a, byte b, byte c, byte d)
{
uip_ipaddr(&this->addr, a,b,c,d);
}
void NanodeMQTT::set_server_port(uint16_t port)
{
this->port = port;
}
void NanodeMQTT::set_keep_alive(uint16_t secs)
{
this->keep_alive = secs;
}
void NanodeMQTT::set_blocking_mode(uint8_t blocking)
{
this->blocking_mode = blocking;
}
void NanodeMQTT::set_callback(mqtt_callback_t callback)
{
this->callback = callback;
}
// FIXME: add support for WILL
void NanodeMQTT::connect()
{
struct uip_conn *conn;
// Set the client ID to the MAC address, if none is set
if (this->client_id[0] == '\0') {
uip->get_mac_str(this->client_id);
}
conn = uip_connect(&this->addr, UIP_HTONS(this->port), mqtt_appcall);
if (conn) {
struct mqtt_app_state *s = (struct mqtt_app_state *)&(conn->appstate);
s->mqtt = this;
this->state = MQTT_STATE_WAITING;
// Set the keep-alive timers
timer_set(&this->transmit_timer, CLOCK_SECOND * this->keep_alive);
timer_set(&this->receive_timer, CLOCK_SECOND * this->keep_alive);
// If in blocking mode - loop until we are connected
if (this->blocking_mode) {
while (this->state == MQTT_STATE_WAITING ||
this->state == MQTT_STATE_CONNECTING ||
this->state == MQTT_STATE_CONNECT_SENT)
{
uip->poll();
}
}
}
}
uint8_t NanodeMQTT::connected()
{
switch(this->state) {
case MQTT_STATE_CONNECTED:
case MQTT_STATE_PUBLISHING:
case MQTT_STATE_SUBSCRIBING:
case MQTT_STATE_SUBSCRIBE_SENT:
case MQTT_STATE_PINGING:
return 1;
default:
return 0;
}
}
uint8_t NanodeMQTT::get_state()
{
return this->state;
}
int8_t NanodeMQTT::get_error_code()
{
return this->error_code;
}
void NanodeMQTT::disconnect()
{
MQTT_DEBUG_PRINTLN("disconnect()");
if (this->connected()) {
this->state = MQTT_STATE_DISCONNECTING;
this->tcp_transmit();
}
}
void NanodeMQTT::publish(const char* topic, const char* payload)
{
publish(topic, (uint8_t*)payload, strlen(payload));
}
void NanodeMQTT::publish(const char* topic, const uint8_t* payload, uint8_t plength)
{
publish(topic, payload, plength, 0);
}
void NanodeMQTT::publish(const char* topic, const uint8_t* payload, uint8_t plength, uint8_t retained)
{
// FIXME: check that payload isn't bigger than UIP_APPDATASIZE (or 127 bytes)
// FIXME: can we avoid this extra buffer?
strcpy(this->payload_topic, topic);
memcpy(this->payload, payload, plength);
this->payload_retain = retained;
this->payload_length = plength;
// If in blocking mode - loop until message has been published
if (this->blocking_mode) {
while (this->connected() && this->payload_length != 0) {
uip->poll();
}
}
}
// ** End of the public API **
void NanodeMQTT::subscribe(const char* topic)
{
this->subscribe_topic = topic;
// If in blocking mode - loop until we have subscribed
if (this->blocking_mode) {
while (this->connected() && this->subscribe_topic != NULL) {
uip->poll();
}
}
}
void NanodeMQTT::init_packet(uint8_t header)
{
buf = (uint8_t *)uip_appdata;
pos = 0;
buf[pos++] = header;
buf[pos++] = 0x00; // Packet length
}
void NanodeMQTT::append_byte(uint8_t b)
{
buf[pos++] = b;
}
void NanodeMQTT::append_word(uint16_t s)
{
buf[pos++] = highByte(s);
buf[pos++] = lowByte(s);
}
void NanodeMQTT::append_string(const char* str)
{
// FIXME: support strings longer than 255 bytes
const char* ptr = str;
uint8_t len = 0;
pos += 2;
while (*ptr) {
buf[pos++] = *ptr++;
len++;
}
buf[pos-len-2] = 0;
buf[pos-len-1] = len;
}
void NanodeMQTT::append_data(uint8_t *data, uint8_t data_len)
{
memcpy(&buf[pos], data, data_len);
pos += data_len;
}
void NanodeMQTT::send_packet()
{
// FIXME: support packets longer than 127 bytes
// Store the size of the packet (minus the fixed header)
buf[1] = pos - 2;
uip_send(buf, pos);
// Restart the packet send timer
timer_restart(&this->transmit_timer);
}
void NanodeMQTT::tcp_connected()
{
this->state = MQTT_STATE_CONNECTING;
this->tcp_transmit();
}
void NanodeMQTT::tcp_acked()
{
switch(this->state) {
case MQTT_STATE_CONNECTING:
this->state = MQTT_STATE_CONNECT_SENT;
break;
case MQTT_STATE_PUBLISHING:
this->state = MQTT_STATE_CONNECTED;
this->payload_length = 0;
break;
case MQTT_STATE_PINGING:
this->state = MQTT_STATE_CONNECTED;
this->ping_pending = 0;
break;
case MQTT_STATE_SUBSCRIBING:
this->state = MQTT_STATE_SUBSCRIBE_SENT;
this->subscribe_topic = NULL;
break;
case MQTT_STATE_DISCONNECTING:
this->state = MQTT_STATE_DISCONNECTED;
uip_close();
break;
default:
MQTT_DEBUG_PRINTLN("TCP: ack in unknown state");
break;
}
}
void NanodeMQTT::tcp_receive()
{
uint8_t *buf = (uint8_t *)uip_appdata;
uint8_t type = buf[0] & 0xF0;
if (uip_datalen() == 0)
return;
// FIXME: check that packet isn't too long?
// FIXME: support packets longer than 127 bytes
// FIXME: support multiple MQTT packets in single IP packet
switch(type) {
case MQTT_TYPE_CONNACK: {
uint8_t code = buf[3];
if (code == 0) {
MQTT_DEBUG_PRINTLN("MQTT: Connected!");
this->state = MQTT_STATE_CONNECTED;
} else {
MQTT_DEBUG_PRINTF("MQTT: Connection failed (%u)\n", code);
uip_close();
this->state = MQTT_STATE_DISCONNECTED;
this->error_code = code;
}
break;
}
case MQTT_TYPE_SUBACK:
MQTT_DEBUG_PRINTLN("MQTT: Subscribed!");
// FIXME: check current state before changing state
this->state = MQTT_STATE_CONNECTED;
break;
case MQTT_TYPE_PINGRESP:
MQTT_DEBUG_PRINTLN("MQTT: Pong!");
this->ping_pending = 0;
break;
case MQTT_TYPE_PUBLISH:
if (this->callback) {
uint8_t tl = buf[3];
// FIXME: is there a way we can NULL-terminate the string in the packet buffer?
char topic[tl+1];
memcpy(topic, &buf[4], tl);
topic[tl] = 0;
this->callback(topic, buf+4+tl, buf[1]-2-tl);
}
break;
default:
MQTT_DEBUG_PRINTF("MQTT: received unknown packet type (%u)\n", (type >> 4));
break;
}
// Restart the packet receive timer
// FIXME: this should only be restarted when a valid packet is received
timer_restart(&this->receive_timer);
}
void NanodeMQTT::tcp_closed()
{
this->state = MQTT_STATE_DISCONNECTED;
uip_close();
// FIXME: re-establish connection automatically
}
void NanodeMQTT::poll()
{
if (this->state == MQTT_STATE_CONNECTED) {
if (this->payload_length) {
this->state = MQTT_STATE_PUBLISHING;
this->tcp_transmit();
} else if (this->subscribe_topic) {
this->state = MQTT_STATE_SUBSCRIBING;
this->message_id++;
this->tcp_transmit();
} else if (this->ping_pending) {
this->state = MQTT_STATE_PINGING;
this->tcp_transmit();
}
}
}
void NanodeMQTT::check_timeout()
{
#ifdef MQTT_DEBUG
if (timer_expired(&this->transmit_timer))
MQTT_DEBUG_PRINTF("MQTT: not transmitted for %u seconds\n", this->keep_alive);
if (timer_expired(&this->receive_timer))
MQTT_DEBUG_PRINTF("MQTT: not received for %u seconds\n", this->keep_alive);
#endif
if (timer_expired(&this->transmit_timer) || timer_expired(&this->receive_timer)) {
if (this->connected()) {
if (!this->ping_pending) {
// Send ping on the next poll
this->ping_pending = 1;
// Give some extra time to send and receive the ping
// FIXME: think of a better way of doing this - takes too long to timeout
timer_restart(&this->receive_timer);
timer_restart(&this->transmit_timer);
} else {
MQTT_DEBUG_PRINTLN("MQTT: Timed out.");
this->disconnect();
}
} else {
MQTT_DEBUG_PRINTLN("MQTT: Aborting after time-out.");
this->state = MQTT_STATE_DISCONNECTED;
uip_abort();
}
}
}
// Transmit/re-transmit packet for the current state
void NanodeMQTT::tcp_transmit()
{
switch(this->state) {
case MQTT_STATE_CONNECTING:
MQTT_DEBUG_PRINTLN("MQTT: sending CONNECT");
init_packet(MQTT_TYPE_CONNECT);
append_string("MQIsdp");
append_byte(MQTT_PROTOCOL_VERSION);
append_byte(MQTT_FLAG_CLEAN); // Connect flags
append_word(this->keep_alive);
append_string(this->client_id);
send_packet();
break;
case MQTT_STATE_PUBLISHING: {
uint8_t header = MQTT_TYPE_PUBLISH;
if (payload_retain)
header |= MQTT_FLAG_RETAIN;
MQTT_DEBUG_PRINTLN("MQTT: sending PUBLISH");
init_packet(header);
append_string(this->payload_topic);
append_data(this->payload, this->payload_length);
send_packet();
break;
}
case MQTT_STATE_SUBSCRIBING:
MQTT_DEBUG_PRINTLN("MQTT: sending SUBSCRIBE");
init_packet(MQTT_TYPE_SUBSCRIBE);
append_word(this->message_id);
append_string(this->subscribe_topic);
append_byte(0x00); // We only support QOS 0
send_packet();
break;
case MQTT_STATE_PINGING:
MQTT_DEBUG_PRINTLN("MQTT: sending PINGREQ");
init_packet(MQTT_TYPE_PINGREQ);
send_packet();
break;
case MQTT_STATE_DISCONNECTING:
MQTT_DEBUG_PRINTLN("MQTT: sending DISCONNECT");
init_packet(MQTT_TYPE_DISCONNECT);
send_packet();
break;
default:
MQTT_DEBUG_PRINTF("MQTT: Unable to transmit in state %u.\n", this->state);
break;
}
}