Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
Nodemcu Firmware
Commits
aa50eca4
Commit
aa50eca4
authored
Mar 06, 2016
by
jfollas
Browse files
Refactoring of MQTT module to consolidate duplicate code into a function
- per @pjsg's suggestion
parent
0abe2fe9
Changes
2
Hide whitespace changes
Inline
Side-by-side
app/modules/mqtt.c
View file @
aa50eca4
...
@@ -79,6 +79,7 @@ typedef struct lmqtt_userdata
...
@@ -79,6 +79,7 @@ typedef struct lmqtt_userdata
static
sint8
socket_connect
(
struct
espconn
*
pesp_conn
);
static
sint8
socket_connect
(
struct
espconn
*
pesp_conn
);
static
void
mqtt_socket_reconnected
(
void
*
arg
,
sint8_t
err
);
static
void
mqtt_socket_reconnected
(
void
*
arg
,
sint8_t
err
);
static
void
mqtt_socket_connected
(
void
*
arg
);
static
void
mqtt_socket_connected
(
void
*
arg
);
static
void
mqtt_connack_fail
(
lmqtt_userdata
*
mud
,
int
reason_code
);
static
void
mqtt_socket_disconnected
(
void
*
arg
)
// tcp only
static
void
mqtt_socket_disconnected
(
void
*
arg
)
// tcp only
{
{
...
@@ -198,6 +199,21 @@ static void deliver_publish(lmqtt_userdata * mud, uint8_t* message, int length)
...
@@ -198,6 +199,21 @@ static void deliver_publish(lmqtt_userdata * mud, uint8_t* message, int length)
NODE_DBG
(
"leave deliver_publish.
\n
"
);
NODE_DBG
(
"leave deliver_publish.
\n
"
);
}
}
static
void
mqtt_connack_fail
(
lmqtt_userdata
*
mud
,
int
reason_code
)
{
if
(
mud
->
cb_connect_fail_ref
==
LUA_NOREF
)
return
;
if
(
mud
->
self_ref
==
LUA_NOREF
)
return
;
if
(
mud
->
L
==
NULL
)
return
;
lua_rawgeti
(
mud
->
L
,
LUA_REGISTRYINDEX
,
mud
->
cb_connect_fail_ref
);
lua_rawgeti
(
mud
->
L
,
LUA_REGISTRYINDEX
,
mud
->
self_ref
);
// pass the userdata(client) to callback func in lua
lua_pushinteger
(
mud
->
L
,
reason_code
);
lua_call
(
mud
->
L
,
2
,
0
);
}
static
void
mqtt_socket_received
(
void
*
arg
,
char
*
pdata
,
unsigned
short
len
)
static
void
mqtt_socket_received
(
void
*
arg
,
char
*
pdata
,
unsigned
short
len
)
{
{
NODE_DBG
(
"enter mqtt_socket_received.
\n
"
);
NODE_DBG
(
"enter mqtt_socket_received.
\n
"
);
...
@@ -245,17 +261,7 @@ READPACKET:
...
@@ -245,17 +261,7 @@ READPACKET:
espconn_disconnect
(
pesp_conn
);
espconn_disconnect
(
pesp_conn
);
}
}
if
(
mud
->
cb_connect_fail_ref
==
LUA_NOREF
)
mqtt_connack_fail
(
mud
,
MQTT_CONN_FAIL_NOT_A_CONNACK_MSG
);
break
;
if
(
mud
->
self_ref
==
LUA_NOREF
)
break
;
if
(
mud
->
L
==
NULL
)
break
;
lua_rawgeti
(
mud
->
L
,
LUA_REGISTRYINDEX
,
mud
->
cb_connect_fail_ref
);
lua_rawgeti
(
mud
->
L
,
LUA_REGISTRYINDEX
,
mud
->
self_ref
);
// pass the userdata(client) to callback func in lua
lua_pushinteger
(
mud
->
L
,
MQTT_CONN_FAIL_NOT_A_CONNACK_MSG
);
lua_call
(
mud
->
L
,
2
,
0
);
break
;
break
;
...
@@ -275,17 +281,7 @@ READPACKET:
...
@@ -275,17 +281,7 @@ READPACKET:
espconn_disconnect
(
pesp_conn
);
espconn_disconnect
(
pesp_conn
);
}
}
if
(
mud
->
cb_connect_fail_ref
==
LUA_NOREF
)
mqtt_connack_fail
(
mud
,
mqtt_get_connect_ret_code
(
in_buffer
));
break
;
if
(
mud
->
self_ref
==
LUA_NOREF
)
break
;
if
(
mud
->
L
==
NULL
)
break
;
lua_rawgeti
(
mud
->
L
,
LUA_REGISTRYINDEX
,
mud
->
cb_connect_fail_ref
);
lua_rawgeti
(
mud
->
L
,
LUA_REGISTRYINDEX
,
mud
->
self_ref
);
// pass the userdata(client) to callback func in lua
lua_pushinteger
(
mud
->
L
,
mqtt_get_connect_ret_code
(
in_buffer
));
// CONNACK response code contains the reason why failure occurred
lua_call
(
mud
->
L
,
2
,
0
);
break
;
break
;
...
@@ -591,26 +587,12 @@ void mqtt_socket_timer(void *arg)
...
@@ -591,26 +587,12 @@ void mqtt_socket_timer(void *arg)
if
(
mud
->
connState
==
MQTT_INIT
){
// socket connect time out.
if
(
mud
->
connState
==
MQTT_INIT
){
// socket connect time out.
NODE_DBG
(
"Can not connect to broker.
\n
"
);
NODE_DBG
(
"Can not connect to broker.
\n
"
);
os_timer_disarm
(
&
mud
->
mqttTimer
);
os_timer_disarm
(
&
mud
->
mqttTimer
);
mqtt_connack_fail
(
mud
,
MQTT_CONN_FAIL_SERVER_NOT_FOUND
);
if
(
mud
->
cb_connect_fail_ref
!=
LUA_NOREF
&&
mud
->
self_ref
!=
LUA_NOREF
&&
mud
->
L
!=
NULL
)
{
lua_rawgeti
(
mud
->
L
,
LUA_REGISTRYINDEX
,
mud
->
cb_connect_fail_ref
);
lua_rawgeti
(
mud
->
L
,
LUA_REGISTRYINDEX
,
mud
->
self_ref
);
// pass the userdata(client) to callback func in lua
lua_pushinteger
(
mud
->
L
,
MQTT_CONN_FAIL_SERVER_NOT_FOUND
);
lua_call
(
mud
->
L
,
2
,
0
);
}
}
else
if
(
mud
->
connState
==
MQTT_CONNECT_SENDING
){
// MQTT_CONNECT send time out.
}
else
if
(
mud
->
connState
==
MQTT_CONNECT_SENDING
){
// MQTT_CONNECT send time out.
NODE_DBG
(
"sSend MQTT_CONNECT failed.
\n
"
);
NODE_DBG
(
"sSend MQTT_CONNECT failed.
\n
"
);
mud
->
connState
=
MQTT_INIT
;
mud
->
connState
=
MQTT_INIT
;
mqtt_connack_fail
(
mud
,
MQTT_CONN_FAIL_TIMEOUT_SENDING
);
if
(
mud
->
cb_connect_fail_ref
!=
LUA_NOREF
&&
mud
->
self_ref
!=
LUA_NOREF
&&
mud
->
L
!=
NULL
)
{
lua_rawgeti
(
mud
->
L
,
LUA_REGISTRYINDEX
,
mud
->
cb_connect_fail_ref
);
lua_rawgeti
(
mud
->
L
,
LUA_REGISTRYINDEX
,
mud
->
self_ref
);
// pass the userdata(client) to callback func in lua
lua_pushinteger
(
mud
->
L
,
MQTT_CONN_FAIL_TIMEOUT_SENDING
);
lua_call
(
mud
->
L
,
2
,
0
);
}
#ifdef CLIENT_SSL_ENABLE
#ifdef CLIENT_SSL_ENABLE
if
(
mud
->
secure
)
if
(
mud
->
secure
)
...
@@ -637,14 +619,7 @@ void mqtt_socket_timer(void *arg)
...
@@ -637,14 +619,7 @@ void mqtt_socket_timer(void *arg)
{
{
espconn_disconnect
(
mud
->
pesp_conn
);
espconn_disconnect
(
mud
->
pesp_conn
);
}
}
mqtt_connack_fail
(
mud
,
MQTT_CONN_FAIL_TIMEOUT_RECEIVING
);
if
(
mud
->
cb_connect_fail_ref
!=
LUA_NOREF
&&
mud
->
self_ref
!=
LUA_NOREF
&&
mud
->
L
!=
NULL
)
{
lua_rawgeti
(
mud
->
L
,
LUA_REGISTRYINDEX
,
mud
->
cb_connect_fail_ref
);
lua_rawgeti
(
mud
->
L
,
LUA_REGISTRYINDEX
,
mud
->
self_ref
);
// pass the userdata(client) to callback func in lua
lua_pushinteger
(
mud
->
L
,
MQTT_CONN_FAIL_TIMEOUT_RECEIVING
);
lua_call
(
mud
->
L
,
2
,
0
);
}
}
else
if
(
mud
->
connState
==
MQTT_DATA
){
}
else
if
(
mud
->
connState
==
MQTT_DATA
){
msg_queue_t
*
pending_msg
=
msg_peek
(
&
(
mud
->
mqtt_state
.
pending_msg_q
));
msg_queue_t
*
pending_msg
=
msg_peek
(
&
(
mud
->
mqtt_state
.
pending_msg_q
));
if
(
pending_msg
){
if
(
pending_msg
){
...
@@ -982,12 +957,7 @@ static sint8 socket_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
...
@@ -982,12 +957,7 @@ static sint8 socket_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
if
(
pesp_conn
!=
NULL
)
{
if
(
pesp_conn
!=
NULL
)
{
lmqtt_userdata
*
mud
=
(
lmqtt_userdata
*
)
pesp_conn
->
reverse
;
lmqtt_userdata
*
mud
=
(
lmqtt_userdata
*
)
pesp_conn
->
reverse
;
if
(
mud
!=
NULL
)
{
if
(
mud
!=
NULL
)
{
if
(
mud
->
cb_connect_fail_ref
!=
LUA_NOREF
&&
mud
->
self_ref
!=
LUA_NOREF
&&
mud
->
L
!=
NULL
)
{
mqtt_connack_fail
(
mud
,
MQTT_CONN_FAIL_DNS
);
lua_rawgeti
(
mud
->
L
,
LUA_REGISTRYINDEX
,
mud
->
cb_connect_fail_ref
);
lua_rawgeti
(
mud
->
L
,
LUA_REGISTRYINDEX
,
mud
->
self_ref
);
// pass the userdata(client) to callback func in lua
lua_pushinteger
(
mud
->
L
,
MQTT_CONN_FAIL_DNS
);
lua_call
(
mud
->
L
,
2
,
0
);
}
}
}
}
}
...
...
docs/en/modules/mqtt.md
View file @
aa50eca4
...
@@ -41,7 +41,7 @@ m:on("message", function(client, topic, data)
...
@@ -41,7 +41,7 @@ m:on("message", function(client, topic, data)
end
)
end
)
-- for TLS: m:connect("192.168.11.118", secure-port, 1)
-- for TLS: m:connect("192.168.11.118", secure-port, 1)
m
:
connect
(
"192.168.11.118"
,
188
0
,
0
,
function
(
client
)
print
(
"connected"
)
end
,
m
:
connect
(
"192.168.11.118"
,
188
3
,
0
,
function
(
client
)
print
(
"connected"
)
end
,
function
(
client
,
reason
)
print
(
"failed reason: "
..
reason
)
end
)
function
(
client
,
reason
)
print
(
"failed reason: "
..
reason
)
end
)
-- Calling subscribe/publish only makes sense once the connection
-- Calling subscribe/publish only makes sense once the connection
...
...
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