@ -53,6 +53,7 @@ Contributors:
# include "tls_mosq.h"
# include "tls_mosq.h"
# include "util_mosq.h"
# include "util_mosq.h"
# include "will_mosq.h"
# include "will_mosq.h"
# include "utlist.h"
# ifdef WITH_BRIDGE
# ifdef WITH_BRIDGE
@ -204,6 +205,7 @@ static int bridge__connect_step1(struct mosquitto *context)
char * notification_topic ;
char * notification_topic ;
size_t notification_topic_len ;
size_t notification_topic_len ;
uint8_t notification_payload ;
uint8_t notification_payload ;
struct mosquitto__bridge_topic * cur_topic ;
int i ;
int i ;
uint8_t qos ;
uint8_t qos ;
@ -230,16 +232,16 @@ static int bridge__connect_step1(struct mosquitto *context)
*/
*/
sub__clean_session ( context ) ;
sub__clean_session ( context ) ;
for ( i = 0 ; i < context - > bridge - > topic_count ; i + + ) {
LL_FOREACH ( context - > bridge - > topics , cur_topic ) {
if ( c ontext- > bridge - > topics [ i ] . direction = = bd_out | | context - > bridge - > topics [ i ] . direction = = bd_both ) {
if ( c ur_topic- > direction = = bd_out | | cur_topic - > direction = = bd_both ) {
log__printf ( NULL , MOSQ_LOG_DEBUG , " Bridge %s doing local SUBSCRIBE on topic %s " , context - > id , c ontext- > bridge - > topics [ i ] . local_topic ) ;
log__printf ( NULL , MOSQ_LOG_DEBUG , " Bridge %s doing local SUBSCRIBE on topic %s " , context - > id , c ur_topic- > local_topic ) ;
if ( c ontext- > bridge - > topics [ i ] . qos > context - > max_qos ) {
if ( c ur_topic- > qos > context - > max_qos ) {
qos = context - > max_qos ;
qos = context - > max_qos ;
} else {
} else {
qos = c ontext- > bridge - > topics [ i ] . qos ;
qos = c ur_topic- > qos ;
}
}
if ( sub__add ( context ,
if ( sub__add ( context ,
c ontext- > bridge - > topics [ i ] . local_topic ,
c ur_topic- > local_topic ,
qos ,
qos ,
0 ,
0 ,
MQTT_SUB_OPT_NO_LOCAL | MQTT_SUB_OPT_RETAIN_AS_PUBLISHED ,
MQTT_SUB_OPT_NO_LOCAL | MQTT_SUB_OPT_RETAIN_AS_PUBLISHED ,
@ -247,7 +249,7 @@ static int bridge__connect_step1(struct mosquitto *context)
return 1 ;
return 1 ;
}
}
retain__queue ( context ,
retain__queue ( context ,
c ontext- > bridge - > topics [ i ] . local_topic ,
c ur_topic- > local_topic ,
qos , 0 ) ;
qos , 0 ) ;
}
}
}
}
@ -398,10 +400,10 @@ int bridge__connect_step3(struct mosquitto *context)
int bridge__connect ( struct mosquitto * context )
int bridge__connect ( struct mosquitto * context )
{
{
int rc , rc2 ;
int rc , rc2 ;
int i ;
char * notification_topic = NULL ;
char * notification_topic = NULL ;
size_t notification_topic_len ;
size_t notification_topic_len ;
uint8_t notification_payload ;
uint8_t notification_payload ;
struct mosquitto__bridge_topic * cur_topic ;
uint8_t qos ;
uint8_t qos ;
mosquitto_property topic_alias_max , * topic_alias_max_prop = NULL ;
mosquitto_property topic_alias_max , * topic_alias_max_prop = NULL ;
@ -428,16 +430,16 @@ int bridge__connect(struct mosquitto *context)
*/
*/
sub__clean_session ( context ) ;
sub__clean_session ( context ) ;
for ( i = 0 ; i < context - > bridge - > topic_count ; i + + ) {
LL_FOREACH ( context - > bridge - > topics , cur_topic ) {
if ( c ontext- > bridge - > topics [ i ] . direction = = bd_out | | context - > bridge - > topics [ i ] . direction = = bd_both ) {
if ( c ur_topic- > direction = = bd_out | | cur_topic - > direction = = bd_both ) {
log__printf ( NULL , MOSQ_LOG_DEBUG , " Bridge %s doing local SUBSCRIBE on topic %s " , context - > id , c ontext- > bridge - > topics [ i ] . local_topic ) ;
log__printf ( NULL , MOSQ_LOG_DEBUG , " Bridge %s doing local SUBSCRIBE on topic %s " , context - > id , c ur_topic- > local_topic ) ;
if ( c ontext- > bridge - > topics [ i ] . qos > context - > max_qos ) {
if ( c ur_topic- > qos > context - > max_qos ) {
qos = context - > max_qos ;
qos = context - > max_qos ;
} else {
} else {
qos = c ontext- > bridge - > topics [ i ] . qos ;
qos = c ur_topic- > qos ;
}
}
if ( sub__add ( context ,
if ( sub__add ( context ,
c ontext- > bridge - > topics [ i ] . local_topic ,
c ur_topic- > local_topic ,
qos ,
qos ,
0 ,
0 ,
MQTT_SUB_OPT_NO_LOCAL | MQTT_SUB_OPT_RETAIN_AS_PUBLISHED ,
MQTT_SUB_OPT_NO_LOCAL | MQTT_SUB_OPT_RETAIN_AS_PUBLISHED ,
@ -548,10 +550,10 @@ int bridge__connect(struct mosquitto *context)
int bridge__on_connect ( struct mosquitto * context )
int bridge__on_connect ( struct mosquitto * context )
{
{
int i ;
char * notification_topic ;
char * notification_topic ;
size_t notification_topic_len ;
size_t notification_topic_len ;
char notification_payload ;
char notification_payload ;
struct mosquitto__bridge_topic * cur_topic ;
int sub_opts ;
int sub_opts ;
bool retain = true ;
bool retain = true ;
uint8_t qos ;
uint8_t qos ;
@ -594,12 +596,13 @@ int bridge__on_connect(struct mosquitto *context)
mosquitto__free ( notification_topic ) ;
mosquitto__free ( notification_topic ) ;
}
}
}
}
for ( i = 0 ; i < context - > bridge - > topic_count ; i + + ) {
if ( context - > bridge - > topics [ i ] . direction = = bd_in | | context - > bridge - > topics [ i ] . direction = = bd_both ) {
LL_FOREACH ( context - > bridge - > topics , cur_topic ) {
if ( context - > bridge - > topics [ i ] . qos > context - > max_qos ) {
if ( cur_topic - > direction = = bd_in | | cur_topic - > direction = = bd_both ) {
if ( cur_topic - > qos > context - > max_qos ) {
sub_opts = context - > max_qos ;
sub_opts = context - > max_qos ;
} else {
} else {
sub_opts = c ontext- > bridge - > topics [ i ] . qos ;
sub_opts = c ur_topic- > qos ;
}
}
if ( context - > bridge - > protocol_version = = mosq_p_mqtt5 ) {
if ( context - > bridge - > protocol_version = = mosq_p_mqtt5 ) {
sub_opts = sub_opts
sub_opts = sub_opts
@ -607,12 +610,12 @@ int bridge__on_connect(struct mosquitto *context)
| MQTT_SUB_OPT_RETAIN_AS_PUBLISHED
| MQTT_SUB_OPT_RETAIN_AS_PUBLISHED
| MQTT_SUB_OPT_SEND_RETAIN_ALWAYS ;
| MQTT_SUB_OPT_SEND_RETAIN_ALWAYS ;
}
}
if ( send__subscribe ( context , NULL , 1 , & c ontext- > bridge - > topics [ i ] . remote_topic , sub_opts , NULL ) ) {
if ( send__subscribe ( context , NULL , 1 , & c ur_topic- > remote_topic , sub_opts , NULL ) ) {
return 1 ;
return 1 ;
}
}
} else {
} else {
if ( context - > bridge - > attempt_unsubscribe ) {
if ( context - > bridge - > attempt_unsubscribe ) {
if ( send__unsubscribe ( context , NULL , 1 , & c ontext- > bridge - > topics [ i ] . remote_topic , NULL ) ) {
if ( send__unsubscribe ( context , NULL , 1 , & c ur_topic- > remote_topic , NULL ) ) {
/* direction = inwards only. This means we should not be subscribed
/* direction = inwards only. This means we should not be subscribed
* to the topic . It is possible that we used to be subscribed to
* to the topic . It is possible that we used to be subscribed to
* this topic so unsubscribe . */
* this topic so unsubscribe . */
@ -621,15 +624,15 @@ int bridge__on_connect(struct mosquitto *context)
}
}
}
}
}
}
for ( i = 0 ; i < context - > bridge - > topic_count ; i + + ) {
LL_FOREACH ( context - > bridge - > topics , cur_topic ) {
if ( c ontext- > bridge - > topics [ i ] . direction = = bd_out | | context - > bridge - > topics [ i ] . direction = = bd_both ) {
if ( c ur_topic- > direction = = bd_out | | cur_topic - > direction = = bd_both ) {
if ( c ontext- > bridge - > topics [ i ] . qos > context - > max_qos ) {
if ( c ur_topic- > qos > context - > max_qos ) {
qos = context - > max_qos ;
qos = context - > max_qos ;
} else {
} else {
qos = c ontext- > bridge - > topics [ i ] . qos ;
qos = c ur_topic- > qos ;
}
}
retain__queue ( context ,
retain__queue ( context ,
c ontext- > bridge - > topics [ i ] . local_topic ,
c ur_topic- > local_topic ,
qos , 0 ) ;
qos , 0 ) ;
}
}
}
}
@ -769,17 +772,6 @@ void bridge__cleanup(struct mosquitto *context)
mosquitto__free ( context - > bridge - > addresses ) ;
mosquitto__free ( context - > bridge - > addresses ) ;
context - > bridge - > addresses = NULL ;
context - > bridge - > addresses = NULL ;
for ( i = 0 ; i < context - > bridge - > topic_count ; i + + ) {
mosquitto__free ( context - > bridge - > topics [ i ] . topic ) ;
mosquitto__free ( context - > bridge - > topics [ i ] . local_prefix ) ;
mosquitto__free ( context - > bridge - > topics [ i ] . remote_prefix ) ;
mosquitto__free ( context - > bridge - > topics [ i ] . local_topic ) ;
mosquitto__free ( context - > bridge - > topics [ i ] . remote_topic ) ;
}
mosquitto__free ( context - > bridge - > topics ) ;
context - > bridge - > topics = NULL ;
config__bridge_cleanup ( context - > bridge ) ;
config__bridge_cleanup ( context - > bridge ) ;
context - > bridge = NULL ;
context - > bridge = NULL ;
}
}