Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongoose
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
esp
mongoose
Commits
b5b672ec
Commit
b5b672ec
authored
May 18, 2016
by
Marko Mikulicic
Committed by
rojer
May 18, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement MQTT connect username/password
PUBLISHED_FROM=be604dc6b3650c78daaa02ea4db8cd067069b5e1
parent
d41419c8
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
68 additions
and
21 deletions
+68
-21
mqtt_client.c
examples/mqtt_client/mqtt_client.c
+43
-18
mongoose.c
mongoose.c
+25
-3
No files found.
examples/mqtt_client/mqtt_client.c
View file @
b5b672ec
...
@@ -17,12 +17,14 @@
...
@@ -17,12 +17,14 @@
#include "mongoose.h"
#include "mongoose.h"
struct
mg_mqtt_topic_expression
topic_expressions
[]
=
{
static
const
char
*
s_address
=
"localhost:1883"
;
{
"/stuff"
,
0
}
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
)
{
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
;
(
void
)
nc
;
#if 0
#if 0
...
@@ -31,17 +33,25 @@ static void ev_handler(struct mg_connection *nc, int ev, void *p) {
...
@@ -31,17 +33,25 @@ static void ev_handler(struct mg_connection *nc, int ev, void *p) {
#endif
#endif
switch
(
ev
)
{
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_set_protocol_mqtt
(
nc
);
mg_send_mqtt_handshake
(
nc
,
"dummy"
);
mg_send_mqtt_handshake
_opt
(
nc
,
"dummy"
,
opts
);
break
;
break
;
}
case
MG_EV_MQTT_CONNACK
:
case
MG_EV_MQTT_CONNACK
:
if
(
msg
->
connack_ret_code
!=
MG_EV_MQTT_CONNACK_ACCEPTED
)
{
if
(
msg
->
connack_ret_code
!=
MG_EV_MQTT_CONNACK_ACCEPTED
)
{
printf
(
"Got mqtt connection error: %d
\n
"
,
msg
->
connack_ret_code
);
printf
(
"Got mqtt connection error: %d
\n
"
,
msg
->
connack_ret_code
);
exit
(
1
);
exit
(
1
);
}
}
printf
(
"Subscribing to '/stuff'
\n
"
);
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
;
break
;
case
MG_EV_MQTT_PUBACK
:
case
MG_EV_MQTT_PUBACK
:
printf
(
"Message publishing acknowledged (msg_id: %d)
\n
"
,
msg
->
message_id
);
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) {
...
@@ -49,38 +59,53 @@ static void ev_handler(struct mg_connection *nc, int ev, void *p) {
case
MG_EV_MQTT_SUBACK
:
case
MG_EV_MQTT_SUBACK
:
printf
(
"Subscription acknowledged, forwarding to '/test'
\n
"
);
printf
(
"Subscription acknowledged, forwarding to '/test'
\n
"
);
break
;
break
;
case
MG_EV_MQTT_PUBLISH
:
case
MG_EV_MQTT_PUBLISH
:
{
{
#if 0
#if 0
char hex[1024] = {0};
char hex[1024] = {0};
mg_hexdump(nc->recv_mbuf.buf, msg->payload.len, hex, sizeof(hex));
mg_hexdump(nc->recv_mbuf.buf, msg->payload.len, hex, sizeof(hex));
printf("Got incoming message %s:\n%s", msg->topic, hex);
printf("Got incoming message %s:\n%s", msg->topic, hex);
#else
#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
#endif
printf
(
"Forwarding to /test
\n
"
);
printf
(
"Forwarding to /test
\n
"
);
mg_mqtt_publish
(
nc
,
"/test"
,
65
,
MG_MQTT_QOS
(
0
),
msg
->
payload
.
p
,
msg
->
payload
.
len
);
mg_mqtt_publish
(
nc
,
"/test"
,
65
,
MG_MQTT_QOS
(
0
),
msg
->
payload
.
p
,
}
msg
->
payload
.
len
);
break
;
break
;
}
case
MG_EV_CLOSE
:
case
MG_EV_CLOSE
:
printf
(
"Connection closed
\n
"
);
printf
(
"Connection closed
\n
"
);
exit
(
1
);
exit
(
1
);
}
}
}
}
int
main
(
void
)
{
int
main
(
int
argc
,
char
**
argv
)
{
struct
mg_mgr
mgr
;
struct
mg_mgr
mgr
;
const
char
*
address
=
"localhost:1883"
;
int
i
;
mg_mgr_init
(
&
mgr
,
NULL
);
mg_mgr_init
(
&
mgr
,
NULL
);
if
(
mg_connect
(
&
mgr
,
address
,
ev_handler
)
==
NULL
)
{
/* Parse command line arguments */
fprintf
(
stderr
,
"mg_connect(%s) failed
\n
"
,
address
);
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
);
exit
(
EXIT_FAILURE
);
}
}
for
(;;)
{
for
(;;)
{
mg_mgr_poll
(
&
mgr
,
1000
);
mg_mgr_poll
(
&
mgr
,
1000
);
}
}
}
}
mongoose.c
View file @
b5b672ec
...
@@ -8399,6 +8399,8 @@ int mg_rpc_parse_reply(const char *buf, int len, struct json_token *toks,
...
@@ -8399,6 +8399,8 @@ int mg_rpc_parse_reply(const char *buf, int len, struct json_token *toks,
#ifndef MG_DISABLE_MQTT
#ifndef MG_DISABLE_MQTT
#include <string.h>
/* Amalgamated: #include "mongoose/src/internal.h" */
/* Amalgamated: #include "mongoose/src/internal.h" */
/* Amalgamated: #include "mongoose/src/mqtt.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,
...
@@ -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
header
=
MG_MQTT_CMD_CONNECT
<<
4
;
uint8_t
rem_len
;
uint8_t
rem_len
;
uint16_t
keep_alive
;
uint16_t
keep_alive
;
uint16_t
client_id_
len
;
uint16_t
len
;
/*
/*
* 9: version_header(len, magic_string, version_number), 1: flags, 2:
* 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,
...
@@ -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
);
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
,
&
header
,
1
);
mg_send
(
nc
,
&
rem_len
,
1
);
mg_send
(
nc
,
&
rem_len
,
1
);
mg_send
(
nc
,
"
\00\06
MQIsdp
\03
"
,
9
);
mg_send
(
nc
,
"
\00\06
MQIsdp
\03
"
,
9
);
...
@@ -8529,9 +8540,20 @@ void mg_send_mqtt_handshake_opt(struct mg_connection *nc, const char *client_id,
...
@@ -8529,9 +8540,20 @@ void mg_send_mqtt_handshake_opt(struct mg_connection *nc, const char *client_id,
keep_alive
=
htons
(
opts
.
keep_alive
);
keep_alive
=
htons
(
opts
.
keep_alive
);
mg_send
(
nc
,
&
keep_alive
,
2
);
mg_send
(
nc
,
&
keep_alive
,
2
);
client_id_
len
=
htons
(
strlen
(
client_id
));
len
=
htons
(
strlen
(
client_id
));
mg_send
(
nc
,
&
client_id_
len
,
2
);
mg_send
(
nc
,
&
len
,
2
);
mg_send
(
nc
,
client_id
,
strlen
(
client_id
));
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
,
static
void
mg_mqtt_prepend_header
(
struct
mg_connection
*
nc
,
uint8_t
cmd
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment