Fix #1434 - set will QoS in CONNECT message correctly, and rename qos -> will_qos in struct mg_mqtt_opts

This commit is contained in:
Sergey Lyubka 2021-12-23 11:00:18 +00:00
parent f29807aad8
commit 26a97ec36c
8 changed files with 9 additions and 8 deletions

View File

@ -80,7 +80,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
int main(void) { int main(void) {
struct mg_mgr mgr; struct mg_mgr mgr;
struct mg_mqtt_opts opts = {.qos = s_qos}; struct mg_mqtt_opts opts = {.clean = true};
bool done = false; bool done = false;
mg_mgr_init(&mgr); // Initialise event manager mg_mgr_init(&mgr); // Initialise event manager
LOG(LL_INFO, ("Connecting to %s", s_url)); // Inform that we're starting LOG(LL_INFO, ("Connecting to %s", s_url)); // Inform that we're starting

View File

@ -65,7 +65,8 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
// Timer function - recreate client connection if it is closed // Timer function - recreate client connection if it is closed
static void timer_fn(void *arg) { static void timer_fn(void *arg) {
struct mg_mgr *mgr = (struct mg_mgr *) arg; struct mg_mgr *mgr = (struct mg_mgr *) arg;
struct mg_mqtt_opts opts = {.qos = s_qos, struct mg_mqtt_opts opts = {.clean = true,
.will_qos = s_qos,
.will_topic = mg_str(s_pub_topic), .will_topic = mg_str(s_pub_topic),
.will_message = mg_str("goodbye")}; .will_message = mg_str("goodbye")};
if (s_conn == NULL) s_conn = mg_mqtt_connect(mgr, s_url, &opts, fn, NULL); if (s_conn == NULL) s_conn = mg_mqtt_connect(mgr, s_url, &opts, fn, NULL);

View File

@ -28,7 +28,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
} else if (ev == MG_EV_WS_OPEN) { } else if (ev == MG_EV_WS_OPEN) {
// WS connection established. Perform MQTT login // WS connection established. Perform MQTT login
LOG(LL_INFO, ("Connected to WS. Logging in to MQTT...")); LOG(LL_INFO, ("Connected to WS. Logging in to MQTT..."));
struct mg_mqtt_opts opts = {.qos = 1, struct mg_mqtt_opts opts = {.will_qos = 1,
.will_topic = mg_str(s_topic), .will_topic = mg_str(s_topic),
.will_message = mg_str("goodbye")}; .will_message = mg_str("goodbye")};
size_t len = c->send.len; size_t len = c->send.len;

View File

@ -2108,7 +2108,7 @@ void mg_mqtt_login(struct mg_connection *c, struct mg_mqtt_opts *opts) {
char rnd[9], client_id[16]; char rnd[9], client_id[16];
struct mg_str cid = opts->client_id; struct mg_str cid = opts->client_id;
uint32_t total_len = 7 + 1 + 2 + 2; uint32_t total_len = 7 + 1 + 2 + 2;
uint8_t connflag = (uint8_t) ((opts->qos & 3) << 1); uint8_t connflag = (uint8_t) ((opts->will_qos & 3) << 3);
if (cid.len == 0) { if (cid.len == 0) {
mg_random(rnd, sizeof(rnd)); mg_random(rnd, sizeof(rnd));

View File

@ -1053,7 +1053,7 @@ struct mg_mqtt_opts {
struct mg_str client_id; // Client ID struct mg_str client_id; // Client ID
struct mg_str will_topic; // Will topic struct mg_str will_topic; // Will topic
struct mg_str will_message; // Will message struct mg_str will_message; // Will message
uint8_t qos; // Quality of service uint8_t will_qos; // Will message quality of service
bool will_retain; // Retain last will bool will_retain; // Retain last will
bool clean; // Use clean session, 0 or 1 bool clean; // Use clean session, 0 or 1
uint16_t keepalive; // Keep-alive timer in seconds uint16_t keepalive; // Keep-alive timer in seconds

View File

@ -35,7 +35,7 @@ void mg_mqtt_login(struct mg_connection *c, struct mg_mqtt_opts *opts) {
char rnd[9], client_id[16]; char rnd[9], client_id[16];
struct mg_str cid = opts->client_id; struct mg_str cid = opts->client_id;
uint32_t total_len = 7 + 1 + 2 + 2; uint32_t total_len = 7 + 1 + 2 + 2;
uint8_t connflag = (uint8_t) ((opts->qos & 3) << 1); uint8_t connflag = (uint8_t) ((opts->will_qos & 3) << 3);
if (cid.len == 0) { if (cid.len == 0) {
mg_random(rnd, sizeof(rnd)); mg_random(rnd, sizeof(rnd));

View File

@ -24,7 +24,7 @@ struct mg_mqtt_opts {
struct mg_str client_id; // Client ID struct mg_str client_id; // Client ID
struct mg_str will_topic; // Will topic struct mg_str will_topic; // Will topic
struct mg_str will_message; // Will message struct mg_str will_message; // Will message
uint8_t qos; // Quality of service uint8_t will_qos; // Will message quality of service
bool will_retain; // Retain last will bool will_retain; // Retain last will
bool clean; // Use clean session, 0 or 1 bool clean; // Use clean session, 0 or 1
uint16_t keepalive; // Keep-alive timer in seconds uint16_t keepalive; // Keep-alive timer in seconds

View File

@ -333,8 +333,8 @@ static void test_mqtt(void) {
// Set params // Set params
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
memset(&opts, 0, sizeof(opts)); memset(&opts, 0, sizeof(opts));
opts.qos = 1;
opts.clean = true; opts.clean = true;
opts.will_qos = 1;
opts.will_retain = true; opts.will_retain = true;
opts.keepalive = 20; opts.keepalive = 20;
opts.client_id = mg_str("mg_client"); opts.client_id = mg_str("mg_client");