Skip to content

Commit 23701b9

Browse files
cpqcesantabot
authored andcommitted
mgos_mqtt_set_connect_fn
PUBLISHED_FROM=1ece63694e6155410c58406292920d87c7498fe0
1 parent 0a52e6d commit 23701b9

File tree

6 files changed

+23
-29
lines changed

6 files changed

+23
-29
lines changed

include/mgos_mqtt.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@ void mgos_mqtt_global_subscribe(const struct mg_str topic,
3939
void mgos_mqtt_add_global_handler(mg_event_handler_t handler, void *ud);
4040

4141
/*
42-
* Set authentication callback. It is invoked when CONNECT message is about to
43-
* be sent, values from *user and *pass, if non-NULL, will be sent along.
44-
* Note: *user and *pass must be heap-allocated and will be free()d.
42+
* Set connect callback. It is invoked when CONNECT message is about to
43+
* be sent. The callback is responsible to call `mg_send_mqtt_handshake_opt()`
4544
*/
46-
typedef void (*mgos_mqtt_auth_callback_t)(char **client_id, char **user,
47-
char **pass, void *arg);
48-
void mgos_mqtt_set_auth_callback(mgos_mqtt_auth_callback_t cb, void *cb_arg);
45+
typedef void (*mgos_mqtt_connect_fn_t)(struct mg_connection *c,
46+
const char *client_id,
47+
struct mg_send_mqtt_handshake_opts *opts,
48+
void *fn_arg);
49+
void mgos_mqtt_set_connect_fn(mgos_mqtt_connect_fn_t cb, void *fn_arg);
4950

5051
/*
5152
* Returns current MQTT connection if it is established; otherwise returns

lib/cc3200/libmqtt.a

-7.82 KB
Binary file not shown.

lib/cc3220/libmqtt.a

-7.35 KB
Binary file not shown.

lib/esp32/libmqtt.a

-862 Bytes
Binary file not shown.

lib/esp8266/libmqtt.a

-794 Bytes
Binary file not shown.

src/mgos_mqtt.c

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ static int s_reconnect_timeout_ms = 0;
4747
static mgos_timer_id s_reconnect_timer_id = MGOS_INVALID_TIMER_ID;
4848
static struct mg_connection *s_conn = NULL;
4949
static bool s_connected = false;
50-
static mgos_mqtt_auth_callback_t s_auth_cb = NULL;
51-
static void *s_auth_cb_arg = NULL;
50+
static mgos_mqtt_connect_fn_t s_connect_fn = NULL;
51+
static void *s_connect_fn_arg = NULL;
5252
static int s_max_qos = 2;
5353

5454
SLIST_HEAD(topic_handlers, topic_handler) s_topic_handlers;
@@ -121,30 +121,23 @@ static void mgos_mqtt_ev(struct mg_connection *nc, int ev, void *ev_data,
121121
if (!success) break;
122122
struct mg_send_mqtt_handshake_opts opts;
123123
memset(&opts, 0, sizeof(opts));
124-
char *cb_client_id = NULL, *cb_user = NULL, *cb_pass = NULL;
125-
if (s_auth_cb != NULL) {
126-
s_auth_cb(&cb_client_id, &cb_user, &cb_pass, s_auth_cb_arg);
127-
opts.user_name = cb_user;
128-
opts.password = cb_pass;
129-
} else {
130-
opts.user_name = mgos_sys_config_get_mqtt_user();
131-
opts.password = mgos_sys_config_get_mqtt_pass();
132-
}
124+
// char *cb_client_id = NULL, *cb_user = NULL, *cb_pass = NULL;
125+
opts.user_name = mgos_sys_config_get_mqtt_user();
126+
opts.password = mgos_sys_config_get_mqtt_pass();
133127
if (mgos_sys_config_get_mqtt_clean_session()) {
134128
opts.flags |= MG_MQTT_CLEAN_SESSION;
135129
}
136130
opts.keep_alive = mgos_sys_config_get_mqtt_keep_alive();
137131
opts.will_topic = mgos_sys_config_get_mqtt_will_topic();
138132
opts.will_message = mgos_sys_config_get_mqtt_will_message();
139-
const char *client_id =
140-
(cb_client_id != NULL ? cb_client_id
141-
: (mgos_sys_config_get_mqtt_client_id() != NULL
142-
? mgos_sys_config_get_mqtt_client_id()
143-
: mgos_sys_config_get_device_id()));
144-
mg_send_mqtt_handshake_opt(nc, client_id, opts);
145-
free(cb_client_id);
146-
free(cb_user);
147-
free(cb_pass);
133+
const char *client_id = (mgos_sys_config_get_mqtt_client_id() != NULL
134+
? mgos_sys_config_get_mqtt_client_id()
135+
: mgos_sys_config_get_device_id());
136+
if (s_connect_fn != NULL) {
137+
s_connect_fn(nc, client_id, &opts, s_connect_fn_arg);
138+
} else {
139+
mg_send_mqtt_handshake_opt(nc, client_id, opts);
140+
}
148141
break;
149142
}
150143
case MG_EV_CLOSE: {
@@ -220,9 +213,9 @@ void mgos_mqtt_add_global_handler(mg_event_handler_t handler, void *ud) {
220213
SLIST_INSERT_HEAD(&s_global_handlers, gh, entries);
221214
}
222215

223-
void mgos_mqtt_set_auth_callback(mgos_mqtt_auth_callback_t cb, void *cb_arg) {
224-
s_auth_cb = cb;
225-
s_auth_cb_arg = cb_arg;
216+
void mgos_mqtt_set_connect_fn(mgos_mqtt_connect_fn_t fn, void *fn_arg) {
217+
s_connect_fn = fn;
218+
s_connect_fn_arg = fn_arg;
226219
}
227220

228221
static void mgos_mqtt_net_ev(enum mgos_net_event ev,

0 commit comments

Comments
 (0)