--- a/mqtt/mqtt_client_gen.py Wed Jul 10 11:10:05 2024 +0200
+++ b/mqtt/mqtt_client_gen.py Fri Jul 12 11:24:06 2024 +0200
@@ -330,22 +330,18 @@
#include "MQTTClientPersistence.h"
-#define _Log(level, ...) \\
+#define _Log(level, ...) \\ /* snprintf(mstr, 255, __VA_ARGS__); */ \\
/* LogMessage(level, mstr, strlen(mstr)); */ \\
- printf(__VA_ARGS__); \\
+ printf(__VA_ARGS__); \\ #define LogInfo(...) _Log(LOG_INFO, __VA_ARGS__);
#define LogError(...) _Log(LOG_CRITICAL, __VA_ARGS__);
#define LogWarning(...) _Log(LOG_WARNING, __VA_ARGS__);
-static MQTTClient client;
-static MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer5;
-static pthread_mutex_t clientMutex; // mutex to keep PLC data consistent
void trace_callback(enum MQTTCLIENT_TRACE_LEVELS level, char* message)
LogWarning("Paho MQTT Trace : %d, %s\\n", level, message);
@@ -358,7 +354,15 @@
-#define INIT_TOPIC(topic, iec_type, c_loc_name) \\
+static MQTTClient client; +static MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer5; +static MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer; +static pthread_mutex_t clientMutex; // mutex to keep PLC data consistent +#define INIT_TOPIC(topic, iec_type, c_loc_name) \\ {{#topic, &MQTT_##c_loc_name##_buf, iec_type##_ENUM}},
@@ -372,6 +376,8 @@
static int _connect_mqtt(void)
MQTTProperties props = MQTTProperties_initializer;
MQTTProperties willProps = MQTTProperties_initializer;
MQTTResponse response = MQTTResponse_initializer;
@@ -379,6 +385,9 @@
response = MQTTClient_connect5(client, &conn_opts, &props, &willProps);
rc = response.reasonCode;
MQTTResponse_free(response);
+ rc = MQTTClient_connect(client, &conn_opts); @@ -389,7 +398,11 @@
/* TODO stop publish thread */
if (rc = MQTTClient_disconnect5(client, 5000, MQTTREASONCODE_SUCCESS, NULL) != MQTTCLIENT_SUCCESS)
+ if (rc = MQTTClient_disconnect(client, 5000) != MQTTCLIENT_SUCCESS) LogError("MQTT Failed to disconnect, return code %d\\n", rc);
@@ -415,25 +428,33 @@
- LogInfo("MQTT Init no auth");
+ LogInfo("MQTT Init no auth\\n"); #define INIT_x509(PrivateKey, Certificate) \\
- LogInfo("MQTT Init x509 %s,%s", PrivateKey, Certificate);
+ LogInfo("MQTT Init x509 %s,%s\\n", PrivateKey, Certificate); #define INIT_UserPassword(User, Password) \\
- LogInfo("MQTT Init UserPassword %s,%s", User, Password); \\
+ LogInfo("MQTT Init UserPassword %s,%s\\n", User, Password); \\ conn_opts.username = User; \\
conn_opts.password = Password;
+#define MY_SUBSCRIBE(Topic, QoS) \\ + MQTTResponse response = MQTTClient_subscribe5(client, #Topic, QoS, NULL, NULL); \\ + rc = response.reasonCode; \\ + MQTTResponse_free(response); +#define MY_SUBSCRIBE(Topic, QoS) \\ + rc = MQTTClient_subscribe(client, #Topic, QoS); #define INIT_SUBSCRIPTION(Topic, QoS) \\
- MQTTResponse response = MQTTClient_subscribe5(client, #Topic, QoS, NULL, NULL); \\
- rc = response.reasonCode; \\
- MQTTResponse_free(response); \\
+ MY_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); \\ @@ -445,10 +466,14 @@
MQTTClient_createOptions createOpts = MQTTClient_createOptions_initializer;
conn_opts.MQTTVersion = MQTTVERSION_5;
conn_opts.cleanstart = 1;
createOpts.MQTTVersion = MQTTVERSION_5;
+ conn_opts.cleansession = 1; MQTTClient_setTraceCallback(trace_callback);
MQTTClient_setTraceLevel(MQTTCLIENT_TRACE_ERROR);
@@ -465,18 +490,19 @@
rc = MQTTClient_setCallbacks(client, NULL, connectionLost, messageArrived, NULL);
if (rc != MQTTCLIENT_SUCCESS)
- LogError("MQTT Failed to set callbacks %d", rc);
+ LogError("MQTT Failed to set callbacks, return code %d\\n", rc);
if (rc != MQTTCLIENT_SUCCESS) {{
- LogError("MQTT Init Failed %d", rc);
+ LogError("MQTT Connect Failed, return code %d\\n", rc); /* TODO start publish thread */
@@ -504,7 +530,7 @@
@@ -514,9 +540,15 @@
+ # Use Config's "MQTTVersion" to switch between protocol version at build time + formatdict["decl"] += """ +#define USE_MQTT_5""".format(**config) AuthType = config["AuthType"]
formatdict["init"] += """
@@ -549,12 +581,13 @@
READ_VALUE({c_loc_name}, {C_type})""".format(**locals())
if direction == "output":
+ # TODO: publish at init # formatdict["init"] += " NOTHING ! publish doesn't need init. "
formatdict["publish"] += """
WRITE_VALUE({c_loc_name}, {C_type})""".format(**locals())
Ccode = template.format(**formatdict)
if __name__ == "__main__":
@@ -572,6 +605,7 @@
config["URI"] = sys.argv[1] if argc>1 else "tcp://localhost:1883"
config["clientID"] = sys.argv[2] if argc>2 else ""
config["AuthType"] = None
+ config["UseMQTT5"] = True @@ -595,7 +629,7 @@
frame, message="Generate file as ...", defaultDir=os.getcwd(),
wildcard="C (*.c)|*.c", style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT
@@ -620,9 +654,9 @@
int main(int argc, char *argv[]) {
@@ -655,7 +689,7 @@
frame, message="Save file as ...", defaultDir=os.getcwd(),
wildcard="CSV (*.csv)|*.csv", style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT