@@ -21,6 +21,10 @@ LOG_MODULE_REGISTER(net_mqtt_sn, CONFIG_MQTT_SN_LOG_LEVEL);
21
21
NET_BUF_POOL_FIXED_DEFINE (mqtt_sn_messages , MQTT_SN_NET_BUFS , CONFIG_MQTT_SN_LIB_MAX_PAYLOAD_SIZE ,
22
22
0 , NULL );
23
23
24
+ /**
25
+ * A struct to track attempts for actions that require acknowledgment,
26
+ * i.e. topic registering, subscribing, or unsubscribing.
27
+ */
24
28
struct mqtt_sn_confirmable {
25
29
int64_t last_attempt ;
26
30
uint16_t msg_id ;
@@ -403,6 +407,13 @@ static void mqtt_sn_sleep_internal(struct mqtt_sn_client *client)
403
407
}
404
408
}
405
409
410
+ /**
411
+ * @brief Internal function to send a SUBSCRIBE message for a topic.
412
+ *
413
+ * @param client
414
+ * @param topic
415
+ * @param dup DUP flag - see MQTT-SN spec
416
+ */
406
417
static void mqtt_sn_do_subscribe (struct mqtt_sn_client * client , struct mqtt_sn_topic * topic ,
407
418
bool dup )
408
419
{
@@ -438,6 +449,12 @@ static void mqtt_sn_do_subscribe(struct mqtt_sn_client *client, struct mqtt_sn_t
438
449
encode_and_send (client , & p , 0 );
439
450
}
440
451
452
+ /**
453
+ * @brief Internal function to send an UNSUBSCRIBE message for a topic.
454
+ *
455
+ * @param client
456
+ * @param topic
457
+ */
441
458
static void mqtt_sn_do_unsubscribe (struct mqtt_sn_client * client , struct mqtt_sn_topic * topic )
442
459
{
443
460
struct mqtt_sn_param p = {.type = MQTT_SN_MSG_TYPE_UNSUBSCRIBE };
@@ -470,6 +487,12 @@ static void mqtt_sn_do_unsubscribe(struct mqtt_sn_client *client, struct mqtt_sn
470
487
encode_and_send (client , & p , 0 );
471
488
}
472
489
490
+ /**
491
+ * @brief Internal function to a register a topic with the MQTT-SN gateway.
492
+ *
493
+ * @param client
494
+ * @param topic
495
+ */
473
496
static void mqtt_sn_do_register (struct mqtt_sn_client * client , struct mqtt_sn_topic * topic )
474
497
{
475
498
struct mqtt_sn_param p = {.type = MQTT_SN_MSG_TYPE_REGISTER };
@@ -498,6 +521,15 @@ static void mqtt_sn_do_register(struct mqtt_sn_client *client, struct mqtt_sn_to
498
521
encode_and_send (client , & p , 0 );
499
522
}
500
523
524
+ /**
525
+ * @brief Internal function to send a PUBLISH message.
526
+ *
527
+ * Note that this function does not do sanity checks regarding the pub's topic.
528
+ *
529
+ * @param client
530
+ * @param pub
531
+ * @param dup DUP flag - see MQTT-SN spec.
532
+ */
501
533
static void mqtt_sn_do_publish (struct mqtt_sn_client * client , struct mqtt_sn_publish * pub , bool dup )
502
534
{
503
535
struct mqtt_sn_param p = {.type = MQTT_SN_MSG_TYPE_PUBLISH };
@@ -525,6 +557,11 @@ static void mqtt_sn_do_publish(struct mqtt_sn_client *client, struct mqtt_sn_pub
525
557
encode_and_send (client , & p , 0 );
526
558
}
527
559
560
+ /**
561
+ * @brief Internal function to send a SEARCHGW message.
562
+ *
563
+ * @param client
564
+ */
528
565
static void mqtt_sn_do_searchgw (struct mqtt_sn_client * client )
529
566
{
530
567
struct mqtt_sn_param p = {.type = MQTT_SN_MSG_TYPE_SEARCHGW };
@@ -534,6 +571,11 @@ static void mqtt_sn_do_searchgw(struct mqtt_sn_client *client)
534
571
encode_and_send (client , & p , CONFIG_MQTT_SN_LIB_BROADCAST_RADIUS );
535
572
}
536
573
574
+ /**
575
+ * @brief Internal function to send a GWINFO message.
576
+ *
577
+ * @param client
578
+ */
537
579
static void mqtt_sn_do_gwinfo (struct mqtt_sn_client * client )
538
580
{
539
581
struct mqtt_sn_param response = {.type = MQTT_SN_MSG_TYPE_GWINFO };
@@ -555,6 +597,11 @@ static void mqtt_sn_do_gwinfo(struct mqtt_sn_client *client)
555
597
encode_and_send (client , & response , client -> radius_gwinfo );
556
598
}
557
599
600
+ /**
601
+ * @brief Internal function to send a PINGREQ message.
602
+ *
603
+ * @param client
604
+ */
558
605
static void mqtt_sn_do_ping (struct mqtt_sn_client * client )
559
606
{
560
607
struct mqtt_sn_param p = {.type = MQTT_SN_MSG_TYPE_PINGREQ };
@@ -579,12 +626,21 @@ static void mqtt_sn_do_ping(struct mqtt_sn_client *client)
579
626
}
580
627
}
581
628
629
+ /**
630
+ * @brief Process all publish tasks in the queue.
631
+ *
632
+ * @param client
633
+ * @param next_cycle will be set to the time when the next action is required
634
+ *
635
+ * @retval 0 on success
636
+ * @retval -ETIMEDOUT when a publish task ran out of retries
637
+ */
582
638
static int process_pubs (struct mqtt_sn_client * client , int64_t * next_cycle )
583
639
{
584
640
struct mqtt_sn_publish * pub , * pubs ;
585
641
const int64_t now = k_uptime_get ();
586
642
int64_t next_attempt ;
587
- bool dup ;
643
+ bool dup ; /* dup flag if message is resent */
588
644
589
645
SYS_SLIST_FOR_EACH_CONTAINER_SAFE (& client -> publish , pub , pubs , next ) {
590
646
LOG_HEXDUMP_DBG (pub -> topic -> name , pub -> topic -> namelen ,
@@ -599,6 +655,7 @@ static int process_pubs(struct mqtt_sn_client *client, int64_t *next_cycle)
599
655
dup = true;
600
656
}
601
657
658
+ /* Check if action is due */
602
659
if (next_attempt <= now ) {
603
660
switch (pub -> topic -> state ) {
604
661
case MQTT_SN_TOPIC_STATE_REGISTERING :
@@ -627,6 +684,7 @@ static int process_pubs(struct mqtt_sn_client *client, int64_t *next_cycle)
627
684
}
628
685
}
629
686
687
+ /* Remember time when next action is due */
630
688
if (next_attempt > now && (* next_cycle == 0 || next_attempt < * next_cycle )) {
631
689
* next_cycle = next_attempt ;
632
690
}
@@ -637,12 +695,21 @@ static int process_pubs(struct mqtt_sn_client *client, int64_t *next_cycle)
637
695
return 0 ;
638
696
}
639
697
698
+ /**
699
+ * @brief Process all topic tasks in the queue.
700
+ *
701
+ * @param client
702
+ * @param next_cycle will be set to the time when the next action is required
703
+ *
704
+ * @retval 0 on success
705
+ * @retval -ETIMEDOUT when a publish task ran out of retries
706
+ */
640
707
static int process_topics (struct mqtt_sn_client * client , int64_t * next_cycle )
641
708
{
642
709
struct mqtt_sn_topic * topic ;
643
710
const int64_t now = k_uptime_get ();
644
711
int64_t next_attempt ;
645
- bool dup ;
712
+ bool dup ; /* dup flag if message is resent */
646
713
647
714
SYS_SLIST_FOR_EACH_CONTAINER (& client -> topic , topic , next ) {
648
715
LOG_HEXDUMP_DBG (topic -> name , topic -> namelen , "Processing topic" );
@@ -655,6 +722,7 @@ static int process_topics(struct mqtt_sn_client *client, int64_t *next_cycle)
655
722
dup = true;
656
723
}
657
724
725
+ /* Check if action is due */
658
726
if (next_attempt <= now ) {
659
727
switch (topic -> state ) {
660
728
case MQTT_SN_TOPIC_STATE_SUBSCRIBING :
@@ -695,6 +763,7 @@ static int process_topics(struct mqtt_sn_client *client, int64_t *next_cycle)
695
763
}
696
764
}
697
765
766
+ /* Remember time when next action is due */
698
767
if (next_attempt > now && (* next_cycle == 0 || next_attempt < * next_cycle )) {
699
768
* next_cycle = next_attempt ;
700
769
}
@@ -705,6 +774,14 @@ static int process_topics(struct mqtt_sn_client *client, int64_t *next_cycle)
705
774
return 0 ;
706
775
}
707
776
777
+ /**
778
+ * @brief Housekeeping task for the client ping
779
+ *
780
+ * @param client
781
+ * @param next_cycle will be set to the time when the next action is required
782
+ * @retval 0 on success
783
+ * @retval -ETIMEDOUT if client ran out of ping retries
784
+ */
708
785
static int process_ping (struct mqtt_sn_client * client , int64_t * next_cycle )
709
786
{
710
787
const int64_t now = k_uptime_get ();
@@ -743,6 +820,13 @@ static int process_ping(struct mqtt_sn_client *client, int64_t *next_cycle)
743
820
return 0 ;
744
821
}
745
822
823
+ /**
824
+ * @brief Housekeeping task for the gateway search
825
+ *
826
+ * @param client
827
+ * @param next_cycle will be set to the time when the next action is required
828
+ * @retval 0 on success
829
+ */
746
830
static int process_search (struct mqtt_sn_client * client , int64_t * next_cycle )
747
831
{
748
832
const int64_t now = k_uptime_get ();
@@ -774,6 +858,13 @@ static int process_search(struct mqtt_sn_client *client, int64_t *next_cycle)
774
858
return 0 ;
775
859
}
776
860
861
+ /**
862
+ * @brief Housekeeping task for gateway advertisements
863
+ *
864
+ * @param client
865
+ * @param next_cycle will be set to the time when the next action is required
866
+ * @return int
867
+ */
777
868
static int process_advertise (struct mqtt_sn_client * client , int64_t * next_cycle )
778
869
{
779
870
const int64_t now = k_uptime_get ();
@@ -798,6 +889,11 @@ static int process_advertise(struct mqtt_sn_client *client, int64_t *next_cycle)
798
889
return 0 ;
799
890
}
800
891
892
+ /**
893
+ * @brief Housekeeping task that is called by workqueue item
894
+ *
895
+ * @param wrk The work item
896
+ */
801
897
static void process_work (struct k_work * wrk )
802
898
{
803
899
struct mqtt_sn_client * client ;
0 commit comments