-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
mqtt_client.c
1105 lines (885 loc) · 26.3 KB
/
mqtt_client.c
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2018, Erik Moqvist
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* This file is part of the Simba project.
*/
#include "simba.h"
/**
* Implements MQTT version 3.11.
*/
/** Control packet types. */
#define MQTT_CONNECT 1
#define MQTT_CONNACK 2
#define MQTT_PUBLISH 3
#define MQTT_PUBACK 4
#define MQTT_PUBREC 5
#define MQTT_PUBREL 6
#define MQTT_PUBCOMP 7
#define MQTT_SUBSCRIBE 8
#define MQTT_SUBACK 9
#define MQTT_UNSUBSCRIBE 10
#define MQTT_UNSUBACK 11
#define MQTT_PINGREQ 12
#define MQTT_PINGRESP 13
#define MQTT_DISCONNECT 14
/** Connection flags. */
#define CLEAN_SESSION 0x2
#define WILL_FLAG 0x4
#define WILL_QOS_1 0x8
#define WILL_QOS_2 0x10
#define WILL_RETAIN 0x20
#define PASSWORD_FLAG 0x40
#define USER_NAME_FLAG 0x80
#define CONNECTION_ACCEPTED 0
#define CONTROL_CONNECT 0
#define CONTROL_DISCONNECT 1
#define CONTROL_PING 2
#define CONTROL_PUBLISH 3
#define CONTROL_SUBSCRIBE 4
#define CONTROL_UNSUBSCRIBE 5
#define CONTROL_NONE 6
//! Length of a MQTT CONNECT variable header.
#define CONNECT_VAR_HDR_LEN 10
static const char *message_fmt[] = {
"forbidden",
"connect",
"connack",
"publish",
"puback",
"pubrec",
"pubrel",
"pubcomp",
"subscribe",
"suback",
"unsubscribe",
"unsuback",
"pingreq",
"pingresp",
"disconnect",
"forbidden"
};
/** Extract Most Significant Byte from a 16bit value. */
#define MSB(b) ((b >> 8) & 0xff)
/** Extract Least Significant Byte from a 16bit value. */
#define LSB(b) (b & 0xff)
/**
* Write a single variable length string with header to the server.
*/
static int write_mqtt_string(struct mqtt_client_t *self_p,
struct mqtt_string_t *mqtt_string)
{
int res;
uint8_t buf[2];
if (mqtt_string->size == 0 || mqtt_string->buf_p == NULL) {
return (-EINVAL);
}
if (mqtt_string->size > 0xffff) {
return (-EINVAL);
}
/* Write length header */
buf[0] = MSB(mqtt_string->size);
buf[1] = LSB(mqtt_string->size);
if (chan_write(self_p->transport.out_p, &buf[0], 2) != 2) {
return (-EIO);
}
res = chan_write(self_p->transport.out_p,
mqtt_string->buf_p,
mqtt_string->size);
if (res != mqtt_string->size) {
return (-EIO);
}
return (0);
}
/**
* Write the fixed header of the MQTT message to the server.
*/
static int write_fixed_header(struct mqtt_client_t *self_p,
int type,
int flags,
size_t size)
{
uint8_t buf[5];
int pos;
uint8_t encoded_byte;
log_object_print(self_p->log_object_p,
LOG_DEBUG,
OSTR("Writing MQTT message '%s' to the server.\r\n"),
message_fmt[type]);
buf[0] = (type << 4) | flags;
pos = 1;
do {
/* Encode the variable length size field. */
encoded_byte = (size % 128);
size /= 128;
/* If there are more data to encode, set the top bit of this
byte. */
if (size > 0) {
encoded_byte |= 0x80;
}
buf[pos] = encoded_byte;
pos++;
} while (size > 0);
if (chan_write(self_p->transport.out_p, &buf[0], pos) != pos) {
return (-EIO);
}
return (0);
}
/**
* Read the fixed header of a MQTT message from the server.
*/
static int read_fixed_header(struct mqtt_client_t *self_p,
int *type_p,
int *flags_p,
size_t *size_p)
{
uint8_t byte;
long multiplier;
if (chan_read(self_p->transport.in_p, &byte, 1) != 1) {
return (-EIO);
}
*type_p = ((byte >> 4) & 0xf);
*flags_p = (byte & 0xf);
/* Read the variablie size field. */
multiplier = 1;
*size_p = 0;
do {
if (chan_read(self_p->transport.in_p, &byte, 1) != 1) {
return (-EIO);
}
*size_p += (byte & 0x7f) * multiplier;
multiplier *= 128;
if (multiplier > 128L * 128L * 128L) {
return (-1);
}
} while ((byte & 0x80) != 0);
return (0);
}
/**
* Send the connect message to the server.
*/
static int handle_control_connect(struct mqtt_client_t *self_p)
{
struct mqtt_conn_options_t *options_p;
struct mqtt_conn_options_t default_options;
int res = 0, payload_length = 0;
uint8_t buf[CONNECT_VAR_HDR_LEN], flags = 0;
/*
* Note: Each payload string requires a 2 byte length header, so
* that must be accounted for in the payload length hence "+ 2" a
* number of length calculations below.
*/
if (queue_read(&self_p->control.in,
&options_p,
sizeof(options_p)) != sizeof(options_p)) {
return (-1);
}
if (options_p == NULL) {
options_p = &default_options;
memset(options_p, 0, sizeof(*options_p));
}
/*
* We currently do not support ressuming session, so force clean
* session.
*/
flags = CLEAN_SESSION;
/*
* Be sure that 'will' topic and payload are both either set or
* unset.
*/
if ((options_p->will.topic.size == 0) !=
(options_p->will.payload.size == 0)) {
return (-EINVAL);
}
/*
* As per MQTT-3.1.3-3 a Client ID is required, so if the user has
* not specified one, we set one.
*
* We should preferably set a random client id, but we don't
* currently have an way to get a random number on all platforms.
*/
if (options_p->client_id.size == 0) {
options_p->client_id.buf_p = "simba_mqtt";
options_p->client_id.size = strlen(options_p->client_id.buf_p);
}
/*
* Calculate payload length first as we need to send length in
* CONNECT header. We also set flags as they are also in the
* header.
*/
payload_length = options_p->client_id.size + 2;
if (options_p->will.topic.size > 0) {
flags |= WILL_FLAG;
if (options_p->will.qos == mqtt_qos_1_t) {
flags |= WILL_QOS_1;
} else if (options_p->will.qos == mqtt_qos_2_t) {
flags |= WILL_QOS_2;
}
payload_length += options_p->will.topic.size + 2;
payload_length += options_p->will.payload.size + 2;
}
if (options_p->user_name.size > 0) {
flags |= USER_NAME_FLAG;
payload_length += options_p->user_name.size + 2;
}
if (options_p->password.size > 0) {
flags |= PASSWORD_FLAG;
payload_length += options_p->password.size + 2;
}
if (options_p->keep_alive_s == 0) {
options_p->keep_alive_s = DEFAULT_KEEP_ALIVE_S;
}
/* Write the fixed header. */
res = write_fixed_header(self_p,
MQTT_CONNECT,
0,
CONNECT_VAR_HDR_LEN + payload_length);
if (res != 0) {
return (res);
}
/* Write the variable header. */
buf[0] = 0; /* Protocol Name - Length MSB */
buf[1] = 4; /* Protocol Name - Length LSB */
buf[2] = 'M'; /* Protocol Name */
buf[3] = 'Q'; /* Protocol Name */
buf[4] = 'T'; /* Protocol Name */
buf[5] = 'T'; /* Protocol Name */
buf[6] = 4; /* Protocol Level */
buf[7] = flags; /* Connect Flags */
buf[8] = MSB(options_p->keep_alive_s); /* Keep Alive MSB */
buf[9] = LSB(options_p->keep_alive_s); /* Keep Alive LSB */
if (chan_write(self_p->transport.out_p, &buf[0], CONNECT_VAR_HDR_LEN)
!= CONNECT_VAR_HDR_LEN) {
return (-EIO);
}
/* Write paylaod string by string. */
res = write_mqtt_string(self_p, &options_p->client_id);
if (res != 0) {
return (res);
}
if (options_p->will.topic.size > 0) {
res = write_mqtt_string(self_p, &options_p->will.topic);
if (res != 0) {
return (res);
}
res = write_mqtt_string(self_p, &options_p->will.payload);
if (res != 0) {
return (res);
}
}
if (options_p->user_name.size > 0) {
res = write_mqtt_string(self_p, &options_p->user_name);
if (res != 0) {
return (res);
}
}
if (options_p->password.size > 0) {
res = write_mqtt_string(self_p, &options_p->password);
if (res != 0) {
return (res);
}
}
self_p->message.type = CONTROL_CONNECT;
return (0);
}
/**
* Handle the connack message from the server.
*/
static int handle_response_connack(struct mqtt_client_t *self_p,
size_t size)
{
char buf[2];
if (self_p->message.type != CONTROL_CONNECT) {
return (-1);
}
self_p->message.type = CONTROL_NONE;
if (size != 2) {
return (-EMSGSIZE);
}
if (chan_read(self_p->transport.in_p, &buf[0], size) != size) {
return (-EIO);
}
if (buf[0] != 0) {
return (-1);
}
if (buf[1] != CONNECTION_ACCEPTED) {
return (-1);
}
self_p->state = mqtt_client_state_connected_t;
return (0);
}
/**
* Send the disconnect message to the server.
*/
static int handle_control_disconnect(struct mqtt_client_t *self_p)
{
if (write_fixed_header(self_p, MQTT_DISCONNECT, 0, 0) != 0) {
return (-1);
}
self_p->state = mqtt_client_state_disconnected_t;
return (0);
}
/**
* Send the ping message to the server.
*/
static int handle_control_ping(struct mqtt_client_t *self_p)
{
int res;
/* Write the ping request packet. */
res = write_fixed_header(self_p, MQTT_PINGREQ, 0, 0);
if (res != 0) {
return (res);
}
self_p->message.type = CONTROL_PING;
return (0);
}
/**
* Handle the ping message from the server.
*/
static int handle_response_ping(struct mqtt_client_t *self_p,
size_t size)
{
if (self_p->message.type != CONTROL_PING) {
return (-1);
}
self_p->message.type = CONTROL_NONE;
if (size != 0) {
return (-EMSGSIZE);
}
return (0);
}
/**
* Send the publish message to the server.
*/
static int handle_control_publish(struct mqtt_client_t *self_p)
{
int res = 0;
uint8_t buf[2];
struct mqtt_application_message_t *message_p;
size_t size;
if (queue_read(&self_p->control.in,
&message_p,
sizeof(message_p)) != sizeof(message_p)) {
return (-1);
}
/* Write the fixed header. */
size = (message_p->topic.size + message_p->payload.size + 2);
if (message_p->qos > 0) {
size += 2;
}
res = write_fixed_header(self_p,
MQTT_PUBLISH,
(message_p->qos << 1),
size);
if (res != 0) {
return (res);
}
/* Write the variable header. */
buf[0] = 0;
buf[1] = message_p->topic.size;
if (chan_write(self_p->transport.out_p, &buf[0], 2) != 2) {
return (-EIO);
}
if (chan_write(self_p->transport.out_p,
message_p->topic.buf_p,
message_p->topic.size) != message_p->topic.size) {
return (-EIO);
}
if (message_p->qos > 0) {
buf[0] = 0;
buf[1] = 1;
if (chan_write(self_p->transport.out_p, &buf[0], 2) != 2) {
return (-EIO);
}
}
/* Write the payload. */
if (message_p->payload.size > 0) {
if (chan_write(self_p->transport.out_p,
message_p->payload.buf_p,
message_p->payload.size) != message_p->payload.size) {
return (-EIO);
}
}
self_p->message.type = CONTROL_PUBLISH;
return (0);
}
/**
* Handle the puback message from the server.
*/
static int handle_response_puback(struct mqtt_client_t *self_p,
size_t size)
{
uint8_t buf[2];
if (self_p->message.type != CONTROL_PUBLISH) {
return (-1);
}
self_p->message.type = CONTROL_NONE;
if (size != 2) {
return (-EMSGSIZE);
}
if (chan_read(self_p->transport.in_p, &buf[0], size) != size) {
return (-EIO);
}
if (buf[0] != 0) {
return (-1);
}
if (buf[1] != 1) {
return (-1);
}
return (0);
}
/**
* Send the subscribe message to the server.
*/
static int handle_control_subscribe(struct mqtt_client_t *self_p)
{
int res = 0;
uint8_t buf[2];
struct mqtt_application_message_t *message_p;
if (queue_read(&self_p->control.in,
&message_p,
sizeof(message_p)) != sizeof(message_p)) {
return (-1);
}
/* Write the fixed header to the server. */
res = write_fixed_header(self_p,
MQTT_SUBSCRIBE,
2,
message_p->topic.size + 5);
if (res != 0) {
return (res);
}
/* Write the packet identifier. */
buf[0] = 0;
buf[1] = 1;
if (chan_write(self_p->transport.out_p, &buf[0], 2) != 2) {
return (-EIO);
}
/* Write the topic filter length. */
buf[0] = ((message_p->topic.size >> 8) & 0xff);
buf[1] = (message_p->topic.size & 0xff);
if (chan_write(self_p->transport.out_p, &buf[0], 2) != 2) {
return (-EIO);
}
/* Write the topic filter. */
if (chan_write(self_p->transport.out_p,
message_p->topic.buf_p,
message_p->topic.size) != message_p->topic.size) {
return (-EIO);
}
/* Write the topic filter QoS. */
buf[0] = message_p->qos;
if (chan_write(self_p->transport.out_p, &buf[0], 1) != 1) {
return (-EIO);
}
self_p->message.type = CONTROL_SUBSCRIBE;
return (0);
}
/**
* Handle the suback message from the server.
*/
static int handle_response_suback(struct mqtt_client_t *self_p,
size_t size)
{
uint8_t buf[3];
if (self_p->message.type != CONTROL_SUBSCRIBE) {
return (-1);
}
self_p->message.type = CONTROL_NONE;
if (size != 3) {
return (-EMSGSIZE);
}
if (chan_read(self_p->transport.in_p, &buf[0], size) != size) {
return (-EIO);
}
if (buf[0] != 0) {
return (-1);
}
if (buf[1] != 1) {
return (-1);
}
if (buf[2] > 2) {
return (-1);
}
return (0);
}
/**
* Send the unsubscribe message to the server.
*/
static int handle_control_unsubscribe(struct mqtt_client_t *self_p)
{
int res = 0;
uint8_t buf[2];
struct mqtt_application_message_t *message_p;
if (queue_read(&self_p->control.in,
&message_p,
sizeof(message_p)) != sizeof(message_p)) {
return (-1);
}
/* Write the fixed header to the server. */
res = write_fixed_header(self_p,
MQTT_UNSUBSCRIBE,
2,
message_p->topic.size + 4);
if (res != 0) {
return (res);
}
/* Write the packet identifier. */
buf[0] = 0;
buf[1] = 2;
if (chan_write(self_p->transport.out_p, &buf[0], 2) != 2) {
return (-EIO);
}
/* Write the topic filter length. */
buf[0] = ((message_p->topic.size >> 8) & 0xff);
buf[1] = (message_p->topic.size & 0xff);
if (chan_write(self_p->transport.out_p, &buf[0], 2) != 2) {
return (-EIO);
}
/* Write the topic filter. */
if (chan_write(self_p->transport.out_p,
message_p->topic.buf_p,
message_p->topic.size) != message_p->topic.size) {
return (-EIO);
}
self_p->message.type = CONTROL_UNSUBSCRIBE;
return (0);
}
/**
* Handle the unsuback message from the server.
*/
static int handle_response_unsuback(struct mqtt_client_t *self_p,
size_t size)
{
uint8_t buf[3];
if (self_p->message.type != CONTROL_UNSUBSCRIBE) {
return (-1);
}
self_p->message.type = CONTROL_NONE;
if (size != 2) {
return (-EMSGSIZE);
}
if (chan_read(self_p->transport.in_p, &buf[0], size) != size) {
return (-EIO);
}
if (buf[0] != 0) {
return (-1);
}
if (buf[1] != 2) {
return (-1);
}
return (0);
}
/**
* Handle the publish message from the server.
*/
static int handle_publish(struct mqtt_client_t *self_p,
size_t size,
int flags)
{
int res;
size_t topic_size;
size_t payload_size;
uint8_t buf[2];
uint8_t qos;
char topic[128];
/* Read the variable header. */
if (chan_read(self_p->transport.in_p, buf, 2) != 2) {
return (-EIO);
}
topic_size = (((size_t)buf[0] << 8) | buf[1]);
if (topic_size > sizeof(topic) - 1) {
return (-EMSGSIZE);
}
/* Read the topic. */
if (chan_read(self_p->transport.in_p,
topic,
topic_size) != topic_size) {
return (-EIO);
}
topic[topic_size] = '\0';
qos = ((flags >> 1) & 0x3);
log_object_print(self_p->log_object_p,
LOG_DEBUG,
OSTR("QoS: %d, Flags: 0x%02x.\r\n"),
qos,
flags);
if (qos == 0) {
payload_size = (size - topic_size - 2);
} else {
/* Read the packet identifier. */
if (chan_read(self_p->transport.in_p, buf, 2) != 2) {
return (-EIO);
}
if (qos == 1) {
res = write_fixed_header(self_p, MQTT_PUBACK, 0, 2);
} else if (qos == 2) {
res = write_fixed_header(self_p, MQTT_PUBREC, 0, 2);
} else {
res = (-EPROTO);
}
if (res != 0) {
return (res);
}
/* Write the variable header. */
if (chan_write(self_p->transport.out_p, &buf[0], 2) != 2) {
return (-EIO);
}
payload_size = (size - topic_size - 4);
}
if (self_p->on_publish(self_p,
topic,
self_p->transport.in_p,
payload_size) != 0) {
return (-1);
}
return (0);
}
/**
* Read a control message.
*/
static int read_control_message(struct mqtt_client_t *self_p)
{
int res = -1;
char type;
if (queue_read(&self_p->control.in,
&type,
sizeof(type)) != sizeof(type)) {
return (-1);
}
switch (self_p->state) {
case mqtt_client_state_disconnected_t:
{
switch (type) {
case CONTROL_CONNECT:
res = handle_control_connect(self_p);
break;
default:
break;
}
}
break;
case mqtt_client_state_connected_t:
{
switch (type) {
case CONTROL_DISCONNECT:
res = handle_control_disconnect(self_p);
chan_write(&self_p->control.out, &res, sizeof(res));
break;
case CONTROL_PING:
res = handle_control_ping(self_p);
break;
case CONTROL_PUBLISH:
res = handle_control_publish(self_p);
break;
case CONTROL_SUBSCRIBE:
res = handle_control_subscribe(self_p);
break;
case CONTROL_UNSUBSCRIBE:
res = handle_control_unsubscribe(self_p);
break;
default:
break;
}
}
break;
default:
break;
}
return (0);
}
/**
* Read a MQTT message from the server.
*/
static int read_server_message(struct mqtt_client_t *self_p)
{
int res;
int type;
int flags;
size_t size;
res = 0;
flags = 0;
size = 0;
if (read_fixed_header(self_p, &type, &flags, &size) != 0) {
return (-EIO);
}
log_object_print(self_p->log_object_p,
LOG_DEBUG,
OSTR("Read MQTT message '%s' from the server.\r\n"),
message_fmt[type]);
switch (type) {
case MQTT_CONNACK:
res = handle_response_connack(self_p, size);
chan_write(&self_p->control.out, &res, sizeof(res));
break;
case MQTT_PUBACK:
res = handle_response_puback(self_p, size);
chan_write(&self_p->control.out, &res, sizeof(res));
break;
case MQTT_PUBREC:
case MQTT_PUBREL:
case MQTT_PUBCOMP:
break;
case MQTT_SUBACK:
res = handle_response_suback(self_p, size);
chan_write(&self_p->control.out, &res, sizeof(res));
break;
case MQTT_UNSUBACK:
res = handle_response_unsuback(self_p, size);
chan_write(&self_p->control.out, &res, sizeof(res));
break;
case MQTT_PINGRESP:
res = handle_response_ping(self_p, size);
chan_write(&self_p->control.out, &res, sizeof(res));
break;
case MQTT_PUBLISH:
res = handle_publish(self_p, size, flags);
break;
default:
break;
}
return (res);
}
/**
* Default on_error callback used if application has not provided one.
*/
static int default_on_error(struct mqtt_client_t *self_p,
int error)
{
log_object_print(self_p->log_object_p,
LOG_ERROR,
OSTR("mqtt_client error: %d.\r\n"),
error);
return (0);
}
int mqtt_client_init(struct mqtt_client_t *self_p,
const char *name_p,
struct log_object_t *log_object_p,
void *transport_out_p,
void *transport_in_p,
mqtt_on_publish_t on_publish,
mqtt_on_error_t on_error)
{
if (on_error == NULL) {
on_error = default_on_error;
}
self_p->name_p = name_p;
self_p->log_object_p = log_object_p;
self_p->state = mqtt_client_state_disconnected_t;
self_p->message.type = CONTROL_NONE;
self_p->transport.out_p = transport_out_p;
self_p->transport.in_p = transport_in_p;
queue_init(&self_p->control.out, NULL, 0);
queue_init(&self_p->control.in, NULL, 0);
self_p->on_publish = on_publish;
self_p->on_error = on_error;
return (0);
}
static int control_routine(struct mqtt_client_t *self_p,
char type,
void *buf_p,
size_t size)