--- a/mqtt/mqtt_client_gen.py Tue Jul 23 11:05:46 2024 +0200
+++ b/mqtt/mqtt_client_gen.py Tue Jul 23 15:30:04 2024 +0200
@@ -314,6 +314,7 @@
@@ -344,7 +345,7 @@
formatdict["decl"] += """
DECL_VAR({iec_type}, {C_type}, {c_loc_name})""".format(**locals())
- formatdict["init"] += """
+ formatdict["init_pubsub"] += """ INIT_PUBLICATION({Topic}, {QoS}, {C_type}, {c_loc_name}, {Retained})""".format(**locals())
formatdict["publish"] += """
WRITE_VALUE({c_loc_name}, {C_type})""".format(**locals())
@@ -360,7 +361,7 @@
DECL_VAR({iec_type}, {C_type}, {c_loc_name})""".format(**locals())
formatdict["topics"] += """
INIT_TOPIC({Topic}, {iec_type}, {c_loc_name})""".format(**locals())
- formatdict["init"] += """
+ formatdict["init_pubsub"] += """ INIT_SUBSCRIPTION({Topic}, {QoS})""".format(**locals())
formatdict["retrieve"] += """
READ_VALUE({c_loc_name}, {C_type})""".format(**locals())
--- a/mqtt/mqtt_template.c Tue Jul 23 11:05:46 2024 +0200
+++ b/mqtt/mqtt_template.c Tue Jul 23 15:30:04 2024 +0200
@@ -11,9 +11,9 @@
#define _Log(level, ...) \
- /* char mstr[256]; */ \
- /* snprintf(mstr, 255, __VA_ARGS__); */ \
- /* LogMessage(level, mstr, strlen(mstr)); */ \
+ snprintf(mstr, 255, __VA_ARGS__); \ + LogMessage(level, mstr, strlen(mstr)); \ @@ -22,9 +22,20 @@
#define LogError(...) _Log(LOG_CRITICAL, __VA_ARGS__);
#define LogWarning(...) _Log(LOG_WARNING, __VA_ARGS__);
+// Selected debug level for paho stack +// MQTTCLIENT_TRACE_PROTOCOL, MQTTCLIENT_TRACE_MAXIMUM, MQTTCLIENT_TRACE_ERROR +#define MQTT_DEBUG_LEVEL MQTTCLIENT_TRACE_ERROR void trace_callback(enum MQTTCLIENT_TRACE_LEVELS level, char* message)
- LogInfo("Paho MQTT Trace : %d, %s\n", level, message);
+ if(level >= MQTT_DEBUG_LEVEL) + int beremiz_log_level = (level >= MQTTCLIENT_TRACE_ERROR ) ? LOG_CRITICAL : + (level > MQTTCLIENT_TRACE_MINIMUM) ? LOG_WARNING : + _Log(beremiz_log_level,"Paho MQTT Trace : %s\n", message); @@ -55,13 +66,13 @@
static pthread_mutex_t MQTT_retrieve_mutex = PTHREAD_MUTEX_INITIALIZER;
/* mutex to keep outgoing PLC data consistent, and protect MQTT_any_pub_var_changed */
-static pthread_mutex_t MQTT_publish_mutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t MQTT_thread_wakeup_mutex = PTHREAD_MUTEX_INITIALIZER; /* wakeup publish thread when PLC changed published variable */
-static pthread_cond_t MQTT_new_data = PTHREAD_COND_INITIALIZER;
+static pthread_cond_t MQTT_thread_wakeup = PTHREAD_COND_INITIALIZER;
-static pthread_t publishThread;
+/* thread that handles publish and reconnection */ +static pthread_t MQTT_thread; #define INIT_TOPIC(topic, iec_type, c_loc_name) \
{{#topic, &MQTT_##c_loc_name##_buf, &MQTT_##c_loc_name##_state, iec_type##_ENUM}},
@@ -75,37 +86,18 @@
-static int _connect_mqtt(void)
- MQTTProperties props = MQTTProperties_initializer;
- MQTTProperties willProps = MQTTProperties_initializer;
- MQTTResponse response = MQTTResponse_initializer;
- response = MQTTClient_connect5(client, &conn_opts, &props, &willProps);
- rc = response.reasonCode;
- MQTTResponse_free(response);
- rc = MQTTClient_connect(client, &conn_opts);
void __cleanup_{locstr}(void)
/* stop publish thread */
- if (pthread_mutex_lock(&MQTT_publish_mutex) == 0){{
+ if (pthread_mutex_lock(&MQTT_thread_wakeup_mutex) == 0){{ /* unblock publish thread so that it can stop normally */
- pthread_cond_signal(&MQTT_new_data);
- pthread_mutex_unlock(&MQTT_publish_mutex);
+ pthread_cond_signal(&MQTT_thread_wakeup); + pthread_mutex_unlock(&MQTT_thread_wakeup_mutex); - pthread_join(publishThread, NULL);
+ pthread_join(MQTT_thread, NULL); if (rc = MQTTClient_disconnect5(client, 5000, MQTTREASONCODE_SUCCESS, NULL) != MQTTCLIENT_SUCCESS)
@@ -118,6 +110,19 @@
MQTTClient_destroy(&client);
+void connectionLost(void* context, char* reason) + LogWarning("ConnectionLost, reconnecting\\n"); + if (pthread_mutex_lock(&MQTT_thread_wakeup_mutex) == 0){{ + /* unblock publish thread so that it can reconnect */ + pthread_cond_signal(&MQTT_thread_wakeup); + pthread_mutex_unlock(&MQTT_thread_wakeup_mutex); int messageArrived(void *context, char *topicName, int topicLen, MQTTClient_message *message)
@@ -172,7 +177,7 @@
#define INIT_UserPassword(User, Password) \
- LogInfo("MQTT Init UserPassword %s,%s\n", User, Password); \
+ LogInfo("MQTT Init UserPassword %s,%s\n", User, Password); \ conn_opts.username = User; \
conn_opts.password = Password;
@@ -190,10 +195,10 @@
#define INIT_SUBSCRIPTION(Topic, QoS) \
- _SUBSCRIBE(Topic, QoS) \
+ _SUBSCRIBE(Topic, QoS) \ if (rc != MQTTCLIENT_SUCCESS) \
- LogError("MQTT client failed to subscribe to '%s', return code %d\n", #Topic, rc); \
+ LogError("MQTT client failed to subscribe to '%s', return code %d\n", #Topic, rc); \ @@ -201,7 +206,7 @@
#define _PUBLISH(Topic, QoS, C_type, c_loc_name, Retained) \
MQTTResponse response = MQTTClient_publish5(client, #Topic, sizeof(C_type), \
- &MQTT_##c_loc_name##_buf, QoS, Retained, NULL, NULL); \
+ &MQTT_##c_loc_name##_buf, QoS, Retained, NULL, NULL); \ rc = response.reasonCode; \
MQTTResponse_free(response);
@@ -228,35 +233,68 @@
_PUBLISH(Topic, QoS, C_type, c_loc_name, Retained) \
if (rc != MQTTCLIENT_SUCCESS) \
- LogError("MQTT client failed to publish '%s', return code %d\n", #Topic, rc); \
+ LogError("MQTT client failed to publish '%s', return code %d\n", #Topic, rc); \ /* TODO update status variable accordingly */ \
MQTT_##c_loc_name##_state = UNCHANGED; \
-static void *__publish_thread(void *_unused) {{
+static int _connect_mqtt(void) + MQTTProperties props = MQTTProperties_initializer; + MQTTProperties willProps = MQTTProperties_initializer; + MQTTResponse response = MQTTResponse_initializer; + response = MQTTClient_connect5(client, &conn_opts, &props, &willProps); + rc = response.reasonCode; + MQTTResponse_free(response); + rc = MQTTClient_connect(client, &conn_opts); + if (rc != MQTTCLIENT_SUCCESS) {{ + return MQTTCLIENT_SUCCESS; +static void *__MQTT_thread_proc(void *_unused) {{ - while((rc = pthread_mutex_lock(&MQTT_publish_mutex)) == 0 && !MQTT_stop_thread){{
- pthread_cond_wait(&MQTT_new_data, &MQTT_publish_mutex);
+ while((rc = pthread_mutex_lock(&MQTT_thread_wakeup_mutex)) == 0 && !MQTT_stop_thread){{ + pthread_cond_wait(&MQTT_thread_wakeup, &MQTT_thread_wakeup_mutex); + is_connected = MQTTClient_isConnected(client); + do_publish = MQTT_any_pub_var_changed && is_connected; - int is_connected = MQTTClient_isConnected(client);
- if(MQTT_any_pub_var_changed && is_connected){{
/* publish changes, and reset variable's state to UNCHANGED */
- MQTT_any_pub_var_changed = 0;
- }} else if(!is_connected){{
- if (rc != MQTTCLIENT_SUCCESS) {{
- LogError("MQTT Reconnect Failed, return code %d\n", rc);
+ MQTT_any_pub_var_changed = 0; + pthread_mutex_unlock(&MQTT_thread_wakeup_mutex); + if (rc == MQTTCLIENT_SUCCESS) {{ + LogInfo("MQTT Reconnected\n"); + LogError("MQTT Reconnect Failed, return code %d\n", rc); - pthread_mutex_unlock(&MQTT_publish_mutex);
if(MQTT_stop_thread) break;
@@ -284,8 +322,7 @@
MQTTClient_setTraceCallback(trace_callback);
- MQTTClient_setTraceLevel(MQTTCLIENT_TRACE_ERROR);
+ MQTTClient_setTraceLevel(MQTT_DEBUG_LEVEL); rc = MQTTClient_createWithOptions(
&client, uri, clientID, MQTTCLIENT_PERSISTENCE_NONE, NULL, &createOpts);
@@ -295,24 +332,26 @@
- rc = MQTTClient_setCallbacks(client, NULL, NULL, messageArrived, NULL);
+ rc = MQTTClient_setCallbacks(client, NULL, connectionLost, messageArrived, NULL); if (rc != MQTTCLIENT_SUCCESS)
LogError("MQTT Failed to set callbacks, return code %d\n", rc);
- if (rc != MQTTCLIENT_SUCCESS) {{
+ if (rc == MQTTCLIENT_SUCCESS) {{ + LogInfo("MQTT Connected\n"); LogError("MQTT Connect Failed, return code %d\n", rc);
+ // Connect error at init is fine, publish thread will retry later
- /* start publish thread */
+ /* start MQTT thread */ - rc = pthread_create(&publishThread, NULL, &__publish_thread, NULL);
+ rc = pthread_create(&MQTT_thread, NULL, &__MQTT_thread_proc, NULL); LogError("MQTT cannot create thread, return code %d\n", rc);
@@ -350,15 +389,15 @@
void __publish_{locstr}(void)
- if (pthread_mutex_trylock(&MQTT_publish_mutex) == 0){{
+ if (pthread_mutex_trylock(&MQTT_thread_wakeup_mutex) == 0){{ MQTT_any_pub_var_changed = 0;
/* copy PLC_* variables to MQTT_*, and mark those who changed */
/* if any change detcted, unblock publish thread */
if(MQTT_any_pub_var_changed){{
- pthread_cond_signal(&MQTT_new_data);
+ pthread_cond_signal(&MQTT_thread_wakeup); - pthread_mutex_unlock(&MQTT_publish_mutex);
+ pthread_mutex_unlock(&MQTT_thread_wakeup_mutex); /* TODO if couldn't lock mutex set status variable accordingly */