-
Notifications
You must be signed in to change notification settings - Fork 3
/
CoreImpl.cpp
executable file
·1560 lines (1406 loc) · 50.4 KB
/
CoreImpl.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
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
#include <stdio.h>
#include "CoreImpl.h"
//#define CORE_LOG 1 // for logging during production
#define CORE_DEBUG 1 // for debugging
#if CORE_LOG || CORE_DEBUG
#endif
bool CoreImpl::begin() {
if (persistent != nullptr && mqtt != nullptr && mqttsn != nullptr && timeout_system != nullptr && advertise_system != nullptr) {
if (persistent->begin() && mqtt->begin() && mqttsn->begin()) {
uint32_t timeout_heartbeat_duration = (uint32_t) persistent->get_timeout_check_duration() * (uint32_t) 1000;
timeout_system->set_heartbeat(timeout_heartbeat_duration);
uint32_t advertise_heartbeat_duration = (uint32_t) persistent->get_advertise_duration() * (uint32_t) 1000;
advertise_system->set_heartbeat(advertise_heartbeat_duration);
uint8_t gw_id = 0;
if (!persistent->get_gateway_id(&gw_id)) {
return false;
}
return true;
}
}
return false;
}
void CoreImpl::setPersistent(PersistentInterface *persistent) {
this->persistent = persistent;
}
void CoreImpl::setMqttMessageHandler(MqttMessageHandlerInterface *mqtt) {
this->mqtt = mqtt;
}
void CoreImpl::setMqttSnMessageHandler(MqttSnMessageHandler *mqttsn) {
this->mqttsn = mqttsn;
}
void CoreImpl::setLogger(LoggerInterface *logger) {
this->logger = logger;
}
void CoreImpl::setSystem(System *system) {
this->timeout_system = system;
}
void CoreImpl::setDurationSystem(System *system) {
this->advertise_system = system;
}
void CoreImpl::loop() {
if (!persistent->is_mqttsn_online() || !persistent->is_mqtt_online()) {
if (persistent->is_mqttsn_online() && !persistent->is_mqtt_online()) {
// only mqttsn is online => disconnect all connected client
process_mqtt_offline_procedure();
return;
} else if (!persistent->is_mqttsn_online() && persistent->is_mqtt_online()) {
// only mqtt is online => publish will-message and disconnect
process_mqttsn_offline_procedure();
set_all_clients_lost();
return;
} else {
// both are disconnected
set_all_clients_lost();
timeout_system->exit();
}
}
bool has_advertise_beaten = advertise_system->has_beaten();
#if CORE_DEBUG
if (has_advertise_beaten) {
uint16_t duration = (uint16_t) (advertise_system->get_heartbeat() / (uint32_t) 1000 );
uint8_t gw_id = 0;
if(!persistent->get_gateway_id(&gw_id)){
// no gateway id given - end program
set_all_clients_lost();
timeout_system->exit();
}
mqttsn->send_advertise(gw_id, duration);
logger->start_log("send ADVERTISE (d", 1);
char buf[24];
memset(&buf, 0, sizeof(buf));
sprintf(buf, "%d", duration);
logger->append_log(buf);
logger->append_log(")");
}
#endif
bool has_heart_beaten = timeout_system->has_beaten();
#if CORE_DEBUG
if (has_heart_beaten) {
logger->start_log("check client for timeout", 3);
}
#endif
// create buckets for to the client data
device_address last_client_address;
memset(&last_client_address, 0, sizeof(device_address));
char client_id[24];
device_address address;
CLIENT_STATUS status;
uint32_t timeout;
uint32_t duration;
uint32_t elapsed_time = 0;
if (has_heart_beaten) {
elapsed_time = timeout_system->get_elapsed_time();
}
persistent->get_last_client_address(&last_client_address);
bool is_last_client_address_empty = true;
for (uint8_t i = 0; i < sizeof(device_address); i++) {
if (last_client_address.bytes[i] != 0) {
is_last_client_address_empty = false;
break;
}
}
if (is_last_client_address_empty) {
#if CORE_DEBUG
logger->log("no clients connected", 3);
#endif
return;
}
persistent->get_nth_client(0, client_id, &address, &status, &duration, &timeout);
uint64_t i = 1;
do {
handle_client_publishes(status, client_id, &address);
persistent->start_client_transaction(&address);
if (status == AWAKE && !persistent->has_client_publishes()) {
persistent->set_client_state(ASLEEP);
uint8_t transaction_return = persistent->apply_transaction();
if (transaction_return == SUCCESS) {
mqttsn->send_pingresp(&address);
}
} else {
uint8_t transaction_return = persistent->apply_transaction();
}
if (has_heart_beaten) {
handle_timeout(status, duration, elapsed_time, client_id, address, timeout);
}
persistent->get_nth_client(i++, client_id, &address, &status, &duration, &timeout);
bool is_address_empty = true;
for (uint8_t i = 0; i < sizeof(device_address); i++) {
if (address.bytes[i] != 0) {
is_address_empty = false;
break;
}
}
if (is_address_empty) {
return;
}
} while (memcmp(&last_client_address, &address, sizeof(device_address)) != 0);
handle_client_publishes(status, client_id, &address);
persistent->start_client_transaction(&address);
if (status == AWAKE && !persistent->has_client_publishes()) {
persistent->set_client_state(ASLEEP);
uint8_t transaction_return = persistent->apply_transaction();
if (transaction_return == SUCCESS) {
mqttsn->send_pingresp(&address);
}
} else {
uint8_t transaction_return = persistent->apply_transaction();
}
if (has_heart_beaten) {
handle_timeout(status, duration, elapsed_time, client_id, address, timeout);
}
}
void
CoreImpl::handle_timeout(const CLIENT_STATUS &status, uint32_t duration, uint32_t elapsed_time, char *client_id,
device_address &address,
uint32_t &timeout) {
if (status == ACTIVE || status == ASLEEP || status == AWAKE) {
persistent->start_client_transaction(&address);
timeout += elapsed_time;
uint64_t tolerance_timeout = 0;
if (duration > 60000) { // greater or smaller 60 seconds
// +10%
tolerance_timeout = (uint64_t) (duration + (duration / 10.0));
} else {
// +50%
tolerance_timeout = (uint64_t) (duration + (duration / 2.0));
}
persistent->set_timeout(timeout);
#if CORE_DEBUG
char buf[24];
memset(&buf, 0, sizeof(buf));
persistent->get_client_id((char *) &buf);
logger->start_log(buf, 3);
logger->append_log(" ");
append_device_address(&address);
logger->append_log(" seconds since last message: ");
sprintf(buf, "%d", timeout);
logger->append_log(buf);
#endif
if (timeout > tolerance_timeout) {
persistent->set_client_state(LOST);
#if CORE_LOG
logger->start_log("Client ", 1);
char client_id_buf[24];
char uint16_buf[20];
memset(&client_id_buf, 0, sizeof(client_id_buf));
persistent->get_client_id((char *) &client_id_buf);
logger->append_log(" (d");
sprintf(uint16_buf, "%d", duration);
logger->append_log(uint16_buf);
logger->append_log(", t");
sprintf(uint16_buf, "%d", timeout);
logger->append_log(uint16_buf);
logger->append_log(")");
logger->append_log(" has exceeded timeout, lost");
#endif
if (persistent->has_client_will()) {
#if CORE_LOG
logger->append_log(" - will");
#endif
char willtopic[255];
memset(&willtopic, 0, sizeof(willtopic));
uint8_t willmsg[255];
memset(&willmsg, 0, sizeof(willmsg));
uint8_t willmsg_length = 0;
uint8_t qos = 0;
bool retain = false;
persistent->get_client_will((char *) willtopic, (uint8_t *) &willmsg, &willmsg_length, &qos,
&retain);
#if CORE_LOG
logger->append_log(" (q");
sprintf(uint16_buf, "%d", qos);
logger->append_log(uint16_buf);
logger->append_log(", r");
logger->append_log(retain ? "1" : "0");
logger->append_log(", ... (");
sprintf(uint16_buf, "%d", willmsg_length);
logger->append_log(uint16_buf);
logger->append_log(" bytes), '");
logger->append_log(willtopic);
logger->append_log("')'");
#endif
// NOT_SUPPORTED: will message retry
bool will_publish_result = mqtt->publish(willtopic, (const uint8_t *) willmsg, willmsg_length, qos,
retain);
#if CORE_LOG
if (will_publish_result) {
logger->append_log(" - SEND");
} else {
logger->append_log(" - SEND FAILURE");
}
#endif
remove_client_subscriptions(client_id);
persistent->delete_client(client_id);
} else {
logger->append_log(" - no will");
}
}
persistent->apply_transaction();
}
}
int16_t
CoreImpl::add_client(const char *client_id, uint16_t duration, bool clean_session, device_address *address) {
#if CORE_LOG
char uint16_buffer[20];
logger->start_log("New client connected", 1);
logger->append_log(" from ");
append_device_address(address);
logger->append_log(" as ");
logger->append_log(client_id);
logger->append_log(" (c");
logger->append_log(clean_session ? "1" : "0");
logger->append_log(", d");
sprintf(uint16_buffer, "%d", duration);
logger->append_log(uint16_buffer);
logger->append_log(")");
#endif
if (!persistent->is_mqtt_online()) {
#if CORE_LOG
logger->append_log(" - REJECTED MQTT OFFLINE");
#endif
return ZERO;
}
persistent->start_client_transaction(client_id);
if (persistent->client_exist()) {
if (!clean_session) {
persistent->reset_client(client_id, address, ((uint32_t) duration) * 1000);
} else {
// before we can remove the client, we must unsubscribe from all subscriptions
this->remove_client_subscriptions(client_id);
persistent->delete_client(client_id);
persistent->add_client(client_id, address, ((uint32_t) duration) * 1000);
}
} else {
persistent->add_client(client_id, address, ((uint32_t) duration) * 1000);
}
uint8_t result = persistent->apply_transaction();
if (result == SUCCESS) {
#if CORE_LOG
logger->set_current_log_lvl(1);
logger->append_log(" - SUCCESS");
#endif
return SUCCESS;
} else if (result == FULL) {
#if CORE_LOG
logger->set_current_log_lvl(1);
logger->append_log(" - REJECTED FULL");
#endif
return FULL;
}
#if CORE_LOG
logger->set_current_log_lvl(1);
logger->append_log(" - REJECTED");
#endif
return ZERO;
}
int16_t CoreImpl::remove_will(device_address *address) {
persistent->start_client_transaction(address);
persistent->delete_will();
uint8_t result = persistent->apply_transaction();
if (result == SUCCESS) {
return SUCCESS;
} else if (result == FULL) {
return FULL;
}
return ZERO;
}
int16_t CoreImpl::add_will_topic(device_address *address, char *will_topic, bool retain, uint8_t qos) {
if (!persistent->is_mqtt_online()) {
return ZERO;
}
persistent->start_client_transaction(address);
if (persistent->get_client_await_message_type() != MQTTSN_WILLTOPIC ||
persistent->get_client_await_message_type() == MQTTSN_PINGREQ) {
#if CORE_DEBUG
logger->log("NOT WAITING FOR WILLTOPIC", 3);
#endif
persistent->apply_transaction();
return ZERO;
}
persistent->set_client_willtopic(will_topic, qos, retain);
uint8_t result = persistent->apply_transaction();
if (result == SUCCESS) {
return SUCCESS;
} else if (result == FULL) {
return FULL;
}
return ZERO;
}
int16_t CoreImpl::add_will_msg(device_address *address, uint8_t *will_msg, uint8_t will_msg_len) {
if (!persistent->is_mqtt_online()) {
return ZERO;
}
persistent->start_client_transaction(address);
if (persistent->get_client_await_message_type() != MQTTSN_WILLMSG ||
persistent->get_client_await_message_type() == MQTTSN_PINGREQ) {
#if CORE_DEBUG
logger->log("NOT WAITING FOR WILLMSG", 3);
#endif
persistent->apply_transaction();
return ZERO;
}
persistent->set_client_willmessage(will_msg, will_msg_len);
uint8_t result = persistent->apply_transaction();
if (result == SUCCESS) {
return SUCCESS;
} else if (result == FULL) {
return FULL;
}
return ZERO;
}
int16_t CoreImpl::await_message(device_address *address, message_type type) {
persistent->start_client_transaction(address);
persistent->set_client_await_message(type);
uint8_t result = persistent->apply_transaction();
if (result == SUCCESS) {
return SUCCESS;
} else if (result == FULL) {
return FULL;
}
return ZERO;
}
int16_t CoreImpl::register_topic(device_address *address, char *topic_name, uint16_t *new_topic_id) {
#if CORE_LOG
logger->start_log("Received REGISTER", 1);
#endif
if (!persistent->is_mqtt_online()) {
#if CORE_LOG
logger->append_log(" - REJECTED MQTT OFFLINE");
#endif
return ZERO;
}
persistent->start_client_transaction(address);
#if CORE_LOG
if (persistent->client_exist()) {
logger->set_current_log_lvl(1);
logger->append_log(" from ");
char client_id[24];
memset(client_id, 0, sizeof(client_id));
persistent->get_client_id(client_id);
logger->append_log(client_id);
} else {
logger->set_current_log_lvl(1);
append_device_address(address);
}
char uint16_buffer[20];
logger->append_log(" (");
logger->append_log("'");
logger->append_log(topic_name);
logger->append_log("'");
#endif
persistent->add_client_registration(topic_name, new_topic_id);
uint8_t result = persistent->apply_transaction();
if (result == FULL) {
#if CORE_LOG
logger->set_current_log_lvl(1);
logger->append_log(") - FULL");
#endif
return FULL;
} else if (result == CLIENTNONEXISTENCE) {
#if CORE_LOG
logger->set_current_log_lvl(1);
logger->append_log(") - ClIENT NOT CONNECTED");
#endif
return CLIENTNONEXISTENCE;
}
if (result == SUCCESS) {
#if CORE_LOG
logger->set_current_log_lvl(1);
logger->append_log(", ");
logger->append_log("t");
sprintf(uint16_buffer, "%d", *new_topic_id);
logger->append_log(uint16_buffer);
logger->append_log(") - SUCCESS");
#endif
return SUCCESS;
}
#if CORE_LOG
logger->set_current_log_lvl(1);
logger->append_log(") - ClIENT NOT CONNECTED");
#endif
return ZERO;
}
int16_t
CoreImpl::publish(device_address *address, const uint8_t *data, uint16_t data_len, uint16_t msg_id,
uint16_t topic_id,
bool short_topic, bool retain, int8_t qos, bool dup, uint16_t *new_topic_id) {
#if CORE_LOG
logger->start_log("Received PUBLISH", 1);
#endif
if (!persistent->is_mqtt_online()) {
#if CORE_LOG
logger->append_log(" - REJECTED MQTT OFFLINE");
#endif
return ZERO;
}
persistent->start_client_transaction(address);
#if CORE_LOG
if (persistent->client_exist()) {
logger->set_current_log_lvl(1);
logger->append_log(" from ");
char client_id[24];
memset(client_id, 0, sizeof(client_id));
persistent->get_client_id(client_id);
logger->append_log(client_id);
} else {
logger->set_current_log_lvl(1);
append_device_address(address);
}
char uint16_buffer[20];
logger->append_log(" (q");
sprintf(uint16_buffer, "%d", qos);
logger->append_log(uint16_buffer);
logger->append_log(", p");
logger->append_log(short_topic ? "0" : "1");
logger->append_log(", r");
logger->append_log(retain ? "1" : "0");
logger->append_log(", d");
logger->append_log(dup ? "1" : "0");
logger->append_log(", t");
sprintf(uint16_buffer, "%d", topic_id);
logger->append_log(uint16_buffer);
logger->append_log(", m");
sprintf(uint16_buffer, "%d", msg_id);
logger->append_log(uint16_buffer);
logger->append_log(", ...(");
sprintf(uint16_buffer, "%d", data_len);
logger->append_log(uint16_buffer);
logger->append_log(" bytes)");
#endif
if (!persistent->client_exist() ||
(persistent->client_exist() &&
(persistent->get_client_status() == DISCONNECTED ||
persistent->get_client_status() == LOST))) {
// client does not exists
// or client is not in any connection state
if (qos == -1 && !short_topic) {
char *topic_name = persistent->get_predefined_topic_name(topic_id);
if (topic_name != nullptr) {
#if CORE_LOG
logger->append_log(", '");
logger->append_log(topic_name);
logger->append_log("')");
#endif
qos = 0;
bool mqtt_result = mqtt->publish(topic_name, data, data_len, (uint8_t) qos, retain);
uint8_t result = persistent->apply_transaction();
if (mqtt_result && result == SUCCESS) {
#if CORE_LOG
logger->append_log(" - SEND");
#endif
return SUCCESS;
}
#if CORE_LOG
logger->append_log(" - SEND FAILURE");
#endif
return ZERO;
}
#if CORE_LOG
if (topic_name == nullptr) {
logger->append_log(", unknown topic id)");
logger->append_log(" - FAILURE");
}
#endif
}
uint8_t result = persistent->apply_transaction();
#if CORE_LOG
logger->append_log(")");
#endif
if (result == SUCCESS) {
#if CORE_LOG
logger->append_log(" - NOT CONNECTED CLIENT PUBLISHES QOS M1 TO NOT PREDEFINED TOPIC ID");
#endif
return CLIENTNONEXISTENCE;
}
#if CORE_LOG
logger->append_log(" - INTERNAL ERROR");
#endif
return ZERO;
}
const char *topic_name = nullptr;
if (short_topic) {
topic_name = persistent->get_topic_name(topic_id);
} else {
topic_name = persistent->get_predefined_topic_name(topic_id);
}
if (topic_name == nullptr) {
uint8_t result = persistent->apply_transaction();
#if CORE_LOG
logger->append_log(", unknown topic id)");
logger->append_log(" - FAILURE");
#endif
if (result == SUCCESS) {
return TOPICIDNONEXISTENCE;
}
return ZERO;
}
#if CORE_LOG
logger->append_log(", '");
logger->append_log(topic_name);
logger->append_log("')");
#endif
uint8_t result = persistent->apply_transaction();
if (result != SUCCESS) {
#if CORE_LOG
logger->append_log(" - INTERNAL ERROR");
#endif
return ZERO;
}
if (qos == -1) {
qos = 0;
}
bool mqtt_result = mqtt->publish(topic_name, data, data_len, (uint8_t) qos, retain);
if (!mqtt_result) {
#if CORE_LOG
logger->append_log(" - SEND FAILURE");
#endif
return ZERO;
}
#if CORE_LOG
logger->append_log(" - SEND");
#endif
return SUCCESS;
}
int16_t
CoreImpl::add_subscription(device_address *address, uint16_t topic_id, uint16_t msg_id, bool short_topic,
uint8_t qos, bool dup, uint16_t *new_topic_id, uint8_t *granted_qos) {
#if CORE_LOG
logger->start_log("Received SUBSCRIBE", 1);
#endif
if (!persistent->is_mqtt_online()) {
#if CORE_LOG
logger->append_log(" - REJECTED MQTT OFFLINE");
#endif
return ZERO;
}
persistent->start_client_transaction(address);
#if CORE_LOG
if (persistent->client_exist()) {
logger->set_current_log_lvl(1);
logger->append_log(" from ");
char client_id[24];
memset(client_id, 0, sizeof(client_id));
persistent->get_client_id(client_id);
logger->append_log(client_id);
} else {
logger->set_current_log_lvl(1);
append_device_address(address);
}
char uint16_buffer[20];
logger->append_log(" (q");
sprintf(uint16_buffer, "%d", qos);
logger->append_log(uint16_buffer);
logger->append_log(", p");
logger->append_log(short_topic ? "0" : "1");
logger->append_log(", d");
logger->append_log(dup ? "1" : "0");
logger->append_log(", t");
sprintf(uint16_buffer, "%d", topic_id);
logger->append_log(uint16_buffer);
logger->append_log(", m");
sprintf(uint16_buffer, "%d", msg_id);
logger->append_log(uint16_buffer);
#endif
if (!persistent->client_exist()) {
uint8_t result = persistent->apply_transaction();
#if CORE_LOG
logger->set_current_log_lvl(1);
logger->append_log(") - CLIENT NOT CONNECTED");
#endif
return ZERO;
}
bool subscribe = false;
*granted_qos = 0;
if (qos >= 1) {
*granted_qos = 1;
}
// NOT_SUPPORTED: at the moment we only grant a maximum qos subscription of 1
const char *topic_name = nullptr;
if (short_topic) {
topic_name = persistent->get_topic_name(topic_id);
if (topic_name != nullptr) {
*new_topic_id = topic_id;
}
} else {
topic_name = persistent->get_predefined_topic_name(topic_id);
if (topic_name != nullptr) {
persistent->add_client_registration((char *) topic_name, new_topic_id);
}
}
if (topic_name == nullptr) {
uint8_t result = persistent->apply_transaction();
#if CORE_LOG
logger->set_current_log_lvl(1);
logger->append_log(") - TOPIC ID UNKNOWN");
#endif
return ZERO;
}
#if CORE_LOG
logger->append_log(", '");
logger->append_log(topic_name);
logger->append_log("', t");
sprintf(uint16_buffer, "%d", *new_topic_id);
logger->append_log(uint16_buffer);
#endif
if (!persistent->is_subscribed(topic_name)) {
persistent->add_subscription(topic_name, *new_topic_id, *granted_qos);
persistent->increment_global_subscription_count(topic_name);
uint32_t subscription_count = persistent->get_global_topic_subscription_count(topic_name);
#if CORE_LOG
logger->append_log(", c");
sprintf(uint16_buffer, "%d", subscription_count);
logger->append_log(uint16_buffer);
#endif
if (subscription_count == 1) { // we are the first to subscribe to this topic.
subscribe = true;
}
} else {
#if CORE_LOG
logger->set_current_log_lvl(1);
logger->append_log(") - ALREADY SUBSCRIBED");
#endif
}
// else client has already a subscription to the topic
uint8_t result = persistent->apply_transaction();
if (result == FULL) {
#if CORE_LOG
logger->set_current_log_lvl(1);
logger->append_log(") - FULL");
#endif
return FULL;
} else if (result == CLIENTNONEXISTENCE) {
#if CORE_LOG
logger->set_current_log_lvl(1);
logger->append_log(") - CLIENT NOT CONNECTED");
#endif
return CLIENTNONEXISTENCE;
} else if (result == CLIENTSUBSCRIPTIONFULL) {
#if CORE_LOG
logger->set_current_log_lvl(1);
logger->append_log(") - CLIENT SUBSCRIPTION FULL");
#endif
return CLIENTSUBSCRIPTIONFULL;
} else if (result == TOPICIDNONEXISTENCE) {
#if CORE_LOG
logger->set_current_log_lvl(1);
logger->append_log(") - TOPIC ID UNKNOWN");
#endif
return TOPICIDNONEXISTENCE;
}
if (subscribe) {
if (!mqtt->subscribe(topic_name, 1)) {
persistent->decrement_global_subscription_count(topic_name);
#if CORE_LOG
logger->set_current_log_lvl(1);
logger->append_log(") - SUBSCRIPTION FAILED");
#endif
return ZERO;
}
}
if (result == SUCCESS) {
#if CORE_LOG
logger->set_current_log_lvl(1);
logger->append_log(") - SUCCESS");
#endif
return SUCCESS;
}
#if CORE_LOG
logger->set_current_log_lvl(1);
logger->append_log(") - INTERNAL ERROR");
#endif
return ZERO;
}
int16_t
CoreImpl::delete_subscription(device_address *address, uint16_t topic_id, uint16_t msg_id, bool short_topic,
bool dup) {
persistent->start_client_transaction(address);
const char *topic_name = nullptr;
if (short_topic) {
topic_name = persistent->get_topic_name(topic_id);
} else {
topic_name = persistent->get_predefined_topic_name(topic_id);
}
bool unsubscribe = false;
if (persistent->is_subscribed(topic_name)) {
persistent->delete_subscription(topic_id);
persistent->decrement_global_subscription_count(topic_name);
uint32_t subscription_count = persistent->get_global_topic_subscription_count(topic_name);
if (subscription_count == 0) { // we are the first to subscribe to this topic.
unsubscribe = true;
}
}
uint8_t result = persistent->apply_transaction();
if (result == FULL) {
return FULL;
} else if (result == CLIENTNONEXISTENCE) {
return CLIENTNONEXISTENCE;
} else if (result == TOPICIDNONEXISTENCE) {
return TOPICIDNONEXISTENCE;
}
if (unsubscribe) {
if (!mqtt->unsubscribe(topic_name)) {
return ZERO;
}
}
if (result == SUCCESS) {
return SUCCESS;
}
return ZERO;
}
int16_t CoreImpl::delete_subscription(device_address *address, char *topic_name, uint16_t msg_id, bool dup) {
persistent->start_client_transaction(address);
uint16_t topic_id = persistent->get_topic_id(topic_name);
bool unsubscribe = false;
if (persistent->is_subscribed(topic_name)) {
persistent->delete_subscription(topic_id);
persistent->decrement_global_subscription_count(topic_name);
uint32_t subscription_count = persistent->get_global_topic_subscription_count(topic_name);
if (subscription_count == 0) { // we are the last to unsubscribe to this topic.
unsubscribe = true;
}
}
uint8_t result = persistent->apply_transaction();
if (result == FULL) {
return FULL;
} else if (result == CLIENTNONEXISTENCE) {
return CLIENTNONEXISTENCE;
} else if (result == TOPICIDNONEXISTENCE) {
return TOPICIDNONEXISTENCE;
}
if (unsubscribe) {
if (!mqtt->unsubscribe(topic_name)) {
return ZERO;
}
}
if (result == SUCCESS) {
return SUCCESS;
}
return ZERO;
}
int16_t CoreImpl::set_disconnected(device_address *address) {
persistent->start_client_transaction(address);
#if CORE_LOG
logger->start_log("Received DISCONNECT", 1);
if (persistent->client_exist()) {
logger->set_current_log_lvl(1);
logger->append_log(" from ");
char client_id[24];
memset(client_id, 0, sizeof(client_id));
persistent->get_client_id(client_id);
logger->append_log(client_id);
} else {
logger->set_current_log_lvl(1);
append_device_address(address);
}
#endif
persistent->set_client_state(DISCONNECTED);
uint8_t result = persistent->apply_transaction();
if (result == CLIENTNONEXISTENCE) {
#if CORE_LOG
logger->set_current_log_lvl(1);
logger->append_log(" - CLIENT NOT CONNECTED");
#endif
return CLIENTNONEXISTENCE;
}
if (result == SUCCESS) {
#if CORE_LOG
logger->set_current_log_lvl(1);
logger->append_log(" - SUCCESS");
#endif
return SUCCESS;
}
#if CORE_LOG
logger->set_current_log_lvl(1);
logger->append_log(" - INTERNAL ERROR");
#endif
return ZERO;
}
int16_t CoreImpl::set_asleep(device_address *address, uint16_t duration) {
#if CORE_LOG
logger->start_log("Received DISCONNECT", 1);
if (persistent->client_exist()) {
logger->set_current_log_lvl(1);
logger->append_log(" from ");
char client_id[24];
memset(client_id, 0, sizeof(client_id));
persistent->get_client_id(client_id);
logger->append_log(client_id);
} else {
logger->set_current_log_lvl(1);
append_device_address(address);
}
char uint16_buf[20];
sprintf(uint16_buf, "%d", duration);
logger->append_log(" (d");
logger->append_log(uint16_buf);
logger->append_log(")");
#endif
if (!persistent->is_mqtt_online()) {
#if CORE_LOG
logger->append_log(" - REJECTED MQTT OFFLINE");
#endif
return ZERO;
}
persistent->start_client_transaction(address);
persistent->set_client_state(ASLEEP);
persistent->set_client_duration(((uint32_t) duration) * 1000); // save it in milliseconds
uint8_t result = persistent->apply_transaction();
if (result == CLIENTNONEXISTENCE) {
#if CORE_LOG
logger->set_current_log_lvl(1);
logger->append_log(" - CLIENT NOT CONNECTED");
#endif
return CLIENTNONEXISTENCE;
}
if (result == SUCCESS) {
#if CORE_LOG
logger->set_current_log_lvl(1);
logger->append_log(" - SUCCESS");
#endif
return SUCCESS;
}
#if CORE_LOG
logger->set_current_log_lvl(1);
logger->append_log(" - INTERNAL ERROR");
#endif
return ZERO;
}
int16_t CoreImpl::set_asleep(device_address *address) { // TODO NEVER USED REMOVE!
if (!persistent->is_mqtt_online()) {
return ZERO;
}
persistent->start_client_transaction(address);
persistent->set_client_state(ASLEEP);
uint8_t result = persistent->apply_transaction();
if (result == CLIENTNONEXISTENCE) {
return CLIENTNONEXISTENCE;
}
if (result == SUCCESS) {
return SUCCESS;
}
return ZERO;
}
int16_t CoreImpl::notify_regack_arrived(device_address *address, uint16_t topic_id, uint16_t msg_id,
return_code_t return_code) {
persistent->start_client_transaction(address);
if (persistent->get_client_await_message_type() == MQTTSN_REGACK &&
persistent->get_client_await_msg_id() == msg_id) {
if (return_code == ACCEPTED) {
persistent->set_topic_known(topic_id, true);
persistent->set_client_await_message(MQTTSN_PINGREQ);
} else if (return_code == REJECTED_CONGESTION) {
persistent->set_topic_known(topic_id, false);
persistent->set_client_await_message(MQTTSN_PINGREQ);
} else if (return_code == REJECTED_INVALID_TOPIC_ID) {
// register topic to client again, but first fix subscriptions and publishes
// this is very very big inconsistency: implement later
// NOT_SUPPORTED: implement me later
// NOT_SUPPORTED delete subscriptions, publishes and registrations with the topic_id
#if CORE_DEBUG
logger->log("FATAL ERROR: regack return_code: REJECTED_INVALID_TOPIC_ID ", 0);
#endif
persistent->set_topic_known(topic_id, true);
persistent->apply_transaction();
return ZERO;
} else if (return_code == REJECTED_NOT_SUPPORTED) {
// NOT_SUPPORTED delete subscriptions, publishes and registrations with the topic_id
#if CORE_DEBUG
logger->log("FATAL ERROR: regack return_code: REJECTED_NOT_SUPPORTED ", 0);
#endif
persistent->set_topic_known(topic_id, true);
persistent->apply_transaction();
return ZERO;
} else {
// unkown return_code, return ZERO
persistent->apply_transaction();