mirror of
https://github.com/cesanta/mongoose.git
synced 2024-11-24 19:19:00 +08:00
Implement MQTT connect username/password
PUBLISHED_FROM=be604dc6b3650c78daaa02ea4db8cd067069b5e1
This commit is contained in:
parent
d41419c8d3
commit
b5b672ec89
@ -17,12 +17,14 @@
|
||||
|
||||
#include "mongoose.h"
|
||||
|
||||
struct mg_mqtt_topic_expression topic_expressions[] = {
|
||||
{"/stuff", 0}
|
||||
};
|
||||
static const char *s_address = "localhost:1883";
|
||||
static const char *s_user_name = NULL;
|
||||
static const char *s_password = NULL;
|
||||
|
||||
struct mg_mqtt_topic_expression topic_expressions[] = {{"/stuff", 0}};
|
||||
|
||||
static void ev_handler(struct mg_connection *nc, int ev, void *p) {
|
||||
struct mg_mqtt_message *msg = (struct mg_mqtt_message *)p;
|
||||
struct mg_mqtt_message *msg = (struct mg_mqtt_message *) p;
|
||||
(void) nc;
|
||||
|
||||
#if 0
|
||||
@ -31,17 +33,25 @@ static void ev_handler(struct mg_connection *nc, int ev, void *p) {
|
||||
#endif
|
||||
|
||||
switch (ev) {
|
||||
case MG_EV_CONNECT:
|
||||
case MG_EV_CONNECT: {
|
||||
struct mg_send_mqtt_handshake_opts opts;
|
||||
memset(&opts, 0, sizeof(opts));
|
||||
opts.user_name = s_user_name;
|
||||
opts.password = s_password;
|
||||
|
||||
mg_set_protocol_mqtt(nc);
|
||||
mg_send_mqtt_handshake(nc, "dummy");
|
||||
mg_send_mqtt_handshake_opt(nc, "dummy", opts);
|
||||
break;
|
||||
}
|
||||
case MG_EV_MQTT_CONNACK:
|
||||
if (msg->connack_ret_code != MG_EV_MQTT_CONNACK_ACCEPTED) {
|
||||
printf("Got mqtt connection error: %d\n", msg->connack_ret_code);
|
||||
exit(1);
|
||||
}
|
||||
printf("Subscribing to '/stuff'\n");
|
||||
mg_mqtt_subscribe(nc, topic_expressions, sizeof(topic_expressions)/sizeof(*topic_expressions), 42);
|
||||
mg_mqtt_subscribe(nc, topic_expressions,
|
||||
sizeof(topic_expressions) / sizeof(*topic_expressions),
|
||||
42);
|
||||
break;
|
||||
case MG_EV_MQTT_PUBACK:
|
||||
printf("Message publishing acknowledged (msg_id: %d)\n", msg->message_id);
|
||||
@ -49,38 +59,53 @@ static void ev_handler(struct mg_connection *nc, int ev, void *p) {
|
||||
case MG_EV_MQTT_SUBACK:
|
||||
printf("Subscription acknowledged, forwarding to '/test'\n");
|
||||
break;
|
||||
case MG_EV_MQTT_PUBLISH:
|
||||
{
|
||||
case MG_EV_MQTT_PUBLISH: {
|
||||
#if 0
|
||||
char hex[1024] = {0};
|
||||
mg_hexdump(nc->recv_mbuf.buf, msg->payload.len, hex, sizeof(hex));
|
||||
printf("Got incoming message %s:\n%s", msg->topic, hex);
|
||||
#else
|
||||
printf("Got incoming message %s: %.*s\n", msg->topic, (int)msg->payload.len, msg->payload.p);
|
||||
printf("Got incoming message %s: %.*s\n", msg->topic,
|
||||
(int) msg->payload.len, msg->payload.p);
|
||||
#endif
|
||||
|
||||
printf("Forwarding to /test\n");
|
||||
mg_mqtt_publish(nc, "/test", 65, MG_MQTT_QOS(0), msg->payload.p, msg->payload.len);
|
||||
}
|
||||
printf("Forwarding to /test\n");
|
||||
mg_mqtt_publish(nc, "/test", 65, MG_MQTT_QOS(0), msg->payload.p,
|
||||
msg->payload.len);
|
||||
break;
|
||||
}
|
||||
case MG_EV_CLOSE:
|
||||
printf("Connection closed\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
int main(int argc, char **argv) {
|
||||
struct mg_mgr mgr;
|
||||
const char *address = "localhost:1883";
|
||||
int i;
|
||||
|
||||
mg_mgr_init(&mgr, NULL);
|
||||
|
||||
if (mg_connect(&mgr, address, ev_handler) == NULL) {
|
||||
fprintf(stderr, "mg_connect(%s) failed\n", address);
|
||||
/* Parse command line arguments */
|
||||
for (i = 1; i < argc; i++) {
|
||||
if (strcmp(argv[i], "-h") == 0) {
|
||||
s_address = argv[i + 1];
|
||||
i++;
|
||||
} else if (strcmp(argv[i], "-u") == 0) {
|
||||
s_user_name = argv[i + 1];
|
||||
i++;
|
||||
} else if (strcmp(argv[i], "-p") == 0) {
|
||||
s_password = argv[i + 1];
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
if (mg_connect(&mgr, s_address, ev_handler) == NULL) {
|
||||
fprintf(stderr, "mg_connect(%s) failed\n", s_address);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
for(;;) {
|
||||
for (;;) {
|
||||
mg_mgr_poll(&mgr, 1000);
|
||||
}
|
||||
}
|
||||
|
28
mongoose.c
28
mongoose.c
@ -8399,6 +8399,8 @@ int mg_rpc_parse_reply(const char *buf, int len, struct json_token *toks,
|
||||
|
||||
#ifndef MG_DISABLE_MQTT
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/* Amalgamated: #include "mongoose/src/internal.h" */
|
||||
/* Amalgamated: #include "mongoose/src/mqtt.h" */
|
||||
|
||||
@ -8509,7 +8511,7 @@ void mg_send_mqtt_handshake_opt(struct mg_connection *nc, const char *client_id,
|
||||
uint8_t header = MG_MQTT_CMD_CONNECT << 4;
|
||||
uint8_t rem_len;
|
||||
uint16_t keep_alive;
|
||||
uint16_t client_id_len;
|
||||
uint16_t len;
|
||||
|
||||
/*
|
||||
* 9: version_header(len, magic_string, version_number), 1: flags, 2:
|
||||
@ -8518,6 +8520,15 @@ void mg_send_mqtt_handshake_opt(struct mg_connection *nc, const char *client_id,
|
||||
*/
|
||||
rem_len = 9 + 1 + 2 + 2 + strlen(client_id);
|
||||
|
||||
if (opts.user_name != NULL) {
|
||||
opts.flags |= MG_MQTT_HAS_USER_NAME;
|
||||
rem_len += strlen(opts.user_name) + 2;
|
||||
}
|
||||
if (opts.password != NULL) {
|
||||
opts.flags |= MG_MQTT_HAS_PASSWORD;
|
||||
rem_len += strlen(opts.password) + 2;
|
||||
}
|
||||
|
||||
mg_send(nc, &header, 1);
|
||||
mg_send(nc, &rem_len, 1);
|
||||
mg_send(nc, "\00\06MQIsdp\03", 9);
|
||||
@ -8529,9 +8540,20 @@ void mg_send_mqtt_handshake_opt(struct mg_connection *nc, const char *client_id,
|
||||
keep_alive = htons(opts.keep_alive);
|
||||
mg_send(nc, &keep_alive, 2);
|
||||
|
||||
client_id_len = htons(strlen(client_id));
|
||||
mg_send(nc, &client_id_len, 2);
|
||||
len = htons(strlen(client_id));
|
||||
mg_send(nc, &len, 2);
|
||||
mg_send(nc, client_id, strlen(client_id));
|
||||
|
||||
if (opts.flags & MG_MQTT_HAS_USER_NAME) {
|
||||
len = htons(strlen(opts.user_name));
|
||||
mg_send(nc, &len, 2);
|
||||
mg_send(nc, opts.user_name, strlen(opts.user_name));
|
||||
}
|
||||
if (opts.flags & MG_MQTT_HAS_PASSWORD) {
|
||||
len = htons(strlen(opts.password));
|
||||
mg_send(nc, &len, 2);
|
||||
mg_send(nc, opts.password, strlen(opts.password));
|
||||
}
|
||||
}
|
||||
|
||||
static void mg_mqtt_prepend_header(struct mg_connection *nc, uint8_t cmd,
|
||||
|
Loading…
Reference in New Issue
Block a user