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
8f355d5d
Commit
8f355d5d
authored
Mar 09, 2016
by
philip
Browse files
Fix problem with subscribing to multiple topics
parent
dd6359e7
Changes
3
Show whitespace changes
Inline
Side-by-side
app/modules/mqtt.c
View file @
8f355d5d
...
...
@@ -1154,51 +1154,49 @@ static int mqtt_socket_subscribe( lua_State* L ) {
NODE_DBG
(
"subscribe table
\n
"
);
lua_pushnil
(
L
);
/* first key */
uint8_t
temp_buf
[
MQTT_BUF_SIZE
];
uint32_t
temp_pos
=
0
;
int
topic_count
=
0
;
uint8_t
overflow
=
0
;
while
(
lua_next
(
L
,
stack
)
!=
0
)
{
topic
=
luaL_checkstring
(
L
,
-
2
);
qos
=
luaL_checkinteger
(
L
,
-
1
);
temp_msg
=
mqtt_msg_subscribe
(
&
mud
->
mqtt_state
.
mqtt_connection
,
topic
,
qos
,
&
msg_id
);
if
(
topic_count
==
0
)
{
temp_msg
=
mqtt_msg_subscribe_init
(
&
mud
->
mqtt_state
.
mqtt_connection
,
&
msg_id
);
}
temp_msg
=
mqtt_msg_subscribe_topic
(
&
mud
->
mqtt_state
.
mqtt_connection
,
topic
,
qos
);
topic_count
++
;
NODE_DBG
(
"topic: %s - qos: %d, length: %d
\n
"
,
topic
,
qos
,
temp_msg
->
length
);
if
(
temp_pos
+
temp_msg
->
length
>
MQTT_BUF_SIZE
)
{
if
(
temp_msg
->
length
==
0
)
{
lua_pop
(
L
,
1
);
overflow
=
1
;
break
;
// too long message for the outbuffer.
}
c_memcpy
(
temp_buf
+
temp_pos
,
temp_msg
->
data
,
temp_msg
->
length
);
temp_pos
+=
temp_msg
->
length
;
lua_pop
(
L
,
1
);
}
if
(
temp_pos
==
0
){
luaL_error
(
L
,
"invalid data"
);
lua_pushboolean
(
L
,
0
);
return
1
;
if
(
topic_count
==
0
){
return
luaL_error
(
L
,
"no topics found"
);
}
if
(
overflow
!=
0
){
luaL_error
(
L
,
"buffer overflow, can't enqueue all subscriptions"
);
lua_pushboolean
(
L
,
0
);
return
1
;
return
luaL_error
(
L
,
"buffer overflow, can't enqueue all subscriptions"
);
}
temp_msg
=
mqtt_msg_subscribe_fini
(
&
mud
->
mqtt_state
.
mqtt_connection
);
if
(
temp_msg
->
length
==
0
)
{
return
luaL_error
(
L
,
"buffer overflow, can't enqueue all subscriptions"
);
}
c_memcpy
(
temp_buffer
,
temp_buf
,
temp_pos
);
temp_msg
->
data
=
temp_buffer
;
temp_msg
->
length
=
temp_pos
;
stack
++
;
}
else
{
NODE_DBG
(
"subscribe string
\n
"
);
topic
=
luaL_checklstring
(
L
,
stack
,
&
il
);
stack
++
;
if
(
topic
==
NULL
){
luaL_error
(
L
,
"need topic name"
);
lua_pushboolean
(
L
,
0
);
return
1
;
return
luaL_error
(
L
,
"need topic name"
);
}
qos
=
luaL_checkinteger
(
L
,
stack
);
temp_msg
=
mqtt_msg_subscribe
(
&
mud
->
mqtt_state
.
mqtt_connection
,
topic
,
qos
,
&
msg_id
);
...
...
@@ -1257,17 +1255,13 @@ static int mqtt_socket_publish( lua_State* L )
}
if
(
!
mud
->
connected
){
luaL_error
(
L
,
"not connected"
);
lua_pushboolean
(
L
,
0
);
return
1
;
return
luaL_error
(
L
,
"not connected"
);
}
const
char
*
topic
=
luaL_checklstring
(
L
,
stack
,
&
l
);
stack
++
;
if
(
topic
==
NULL
){
luaL_error
(
L
,
"need topic"
);
lua_pushboolean
(
L
,
0
);
return
1
;
return
luaL_error
(
L
,
"need topic"
);
}
const
char
*
payload
=
luaL_checklstring
(
L
,
stack
,
&
l
);
...
...
app/mqtt/mqtt_msg.c
View file @
8f355d5d
...
...
@@ -402,14 +402,19 @@ mqtt_message_t* mqtt_msg_pubcomp(mqtt_connection_t* connection, uint16_t message
return
fini_message
(
connection
,
MQTT_MSG_TYPE_PUBCOMP
,
0
,
0
,
0
);
}
mqtt_message_t
*
mqtt_msg_subscribe
(
mqtt_connection_t
*
connection
,
const
char
*
topic
,
int
qos
,
uint16_t
*
message_id
)
mqtt_message_t
*
mqtt_msg_subscribe
_init
(
mqtt_connection_t
*
connection
,
uint16_t
*
message_id
)
{
init_message
(
connection
);
if
(
topic
==
NULL
||
topic
[
0
]
==
'\0'
)
if
(
(
*
message_id
=
append_message_id
(
connection
,
0
))
==
0
)
return
fail_message
(
connection
);
if
((
*
message_id
=
append_message_id
(
connection
,
0
))
==
0
)
return
&
connection
->
message
;
}
mqtt_message_t
*
mqtt_msg_subscribe_topic
(
mqtt_connection_t
*
connection
,
const
char
*
topic
,
int
qos
)
{
if
(
topic
==
NULL
||
topic
[
0
]
==
'\0'
)
return
fail_message
(
connection
);
if
(
append_string
(
connection
,
topic
,
c_strlen
(
topic
))
<
0
)
...
...
@@ -419,9 +424,29 @@ mqtt_message_t* mqtt_msg_subscribe(mqtt_connection_t* connection, const char* to
return
fail_message
(
connection
);
connection
->
buffer
[
connection
->
message
.
length
++
]
=
qos
;
return
&
connection
->
message
;
}
mqtt_message_t
*
mqtt_msg_subscribe_fini
(
mqtt_connection_t
*
connection
)
{
return
fini_message
(
connection
,
MQTT_MSG_TYPE_SUBSCRIBE
,
0
,
1
,
0
);
}
mqtt_message_t
*
mqtt_msg_subscribe
(
mqtt_connection_t
*
connection
,
const
char
*
topic
,
int
qos
,
uint16_t
*
message_id
)
{
mqtt_message_t
*
result
;
result
=
mqtt_msg_subscribe_init
(
connection
,
message_id
);
if
(
result
->
length
!=
0
)
{
result
=
mqtt_msg_subscribe_topic
(
connection
,
topic
,
qos
);
}
if
(
result
->
length
!=
0
)
{
result
=
mqtt_msg_subscribe_fini
(
connection
);
}
return
result
;
}
mqtt_message_t
*
mqtt_msg_unsubscribe
(
mqtt_connection_t
*
connection
,
const
char
*
topic
,
uint16_t
*
message_id
)
{
init_message
(
connection
);
...
...
app/mqtt/mqtt_msg.h
View file @
8f355d5d
...
...
@@ -120,6 +120,10 @@ mqtt_message_t* mqtt_msg_pingreq(mqtt_connection_t* connection);
mqtt_message_t
*
mqtt_msg_pingresp
(
mqtt_connection_t
*
connection
);
mqtt_message_t
*
mqtt_msg_disconnect
(
mqtt_connection_t
*
connection
);
mqtt_message_t
*
mqtt_msg_subscribe_init
(
mqtt_connection_t
*
connection
,
uint16_t
*
message_id
);
mqtt_message_t
*
mqtt_msg_subscribe_topic
(
mqtt_connection_t
*
connection
,
const
char
*
topic
,
int
qos
);
mqtt_message_t
*
mqtt_msg_subscribe_fini
(
mqtt_connection_t
*
connection
);
#ifdef __cplusplus
}
...
...
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