Skip to content

Commit a6e70bf

Browse files
committed
Partial implementation of _unsub() -- it removes the subscription from the mgos_mqtt_conn->subscriptions list, but does not yet call mg_mqtt_unsubscribe() because that causes SIGSEGV
1 parent 765465a commit a6e70bf

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

include/mgos_mqtt.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ typedef void (*sub_handler_t)(struct mg_connection *nc, const char *topic,
9696
*/
9797
void mgos_mqtt_sub(const char *topic, sub_handler_t, void *ud);
9898

99+
/*
100+
* Unsubscribe on a topic on a configured MQTT server.
101+
*/
102+
bool mgos_mqtt_unsub(const char *topic);
103+
99104
/*
100105
* Returns number of pending bytes to send.
101106
*/

include/mgos_mqtt_conn.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ uint16_t mgos_mqtt_conn_pubf(struct mgos_mqtt_conn *c, const char *topic,
103103
void mgos_mqtt_conn_sub(struct mgos_mqtt_conn *c, const char *topic, int qos,
104104
mg_event_handler_t handler, void *user_data);
105105

106+
/*
107+
* Unsubscribe from a specific topic.
108+
*/
109+
bool mgos_mqtt_conn_unsub(struct mgos_mqtt_conn *c, const char *topic);
110+
106111
/*
107112
* Returns number of pending bytes to send.
108113
*/

src/mgos_mqtt.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ void mgos_mqtt_sub(const char *topic, sub_handler_t handler, void *user_data) {
149149
mgos_mqtt_global_subscribe(mg_mk_str(topic), mqttsubtrampoline, sd);
150150
}
151151

152+
bool mgos_mqtt_unsub(const char *topic) {
153+
return mgos_mqtt_conn_unsub(s_conn, topic);
154+
}
155+
152156
size_t mgos_mqtt_num_unsent_bytes(void) {
153157
return mgos_mqtt_conn_num_unsent_bytes(s_conn);
154158
}

src/mgos_mqtt_conn.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,24 @@ void mgos_mqtt_conn_sub(struct mgos_mqtt_conn *c, const char *topic, int qos,
631631
mgos_mqtt_conn_sub_s(c, mg_mk_str(topic), qos, handler, user_data);
632632
}
633633

634+
bool mgos_mqtt_conn_unsub(struct mgos_mqtt_conn *c, const char *topic) {
635+
struct mgos_mqtt_subscription *s;
636+
637+
SLIST_FOREACH(s, &c->subscriptions, next) {
638+
if (0 == strcmp(s->topic.p, topic)) {
639+
LOG(LL_INFO,
640+
("MQTT%d unsub %.*s", c->conn_id, (int) s->topic.len, s->topic.p));
641+
// mg_mqtt_unsubscribe(c->nc, (char **)&topic, 1, mgos_mqtt_conn_get_packet_id(c));
642+
SLIST_REMOVE(&c->subscriptions, s, mgos_mqtt_subscription, next);
643+
mg_strfree(&s->topic);
644+
free(s->user_data);
645+
free(s);
646+
return true;
647+
}
648+
}
649+
return false;
650+
}
651+
634652
size_t mgos_mqtt_conn_num_unsent_bytes(struct mgos_mqtt_conn *c) {
635653
size_t num_bytes = 0;
636654
if (c == NULL) return 0;

0 commit comments

Comments
 (0)