author | Edouard Tisserant <edouard@beremiz.fr> |
Thu, 08 Aug 2024 14:56:13 +0200 | |
changeset 4004 | 19f8192b7d68 |
parent 4002 | 6c2b80b4515d |
child 4005 | 482452574fb4 |
permissions | -rw-r--r-- |
3995
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
1 |
/* code generated by beremiz MQTT extension */ |
3979
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
2 |
|
3984
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
3 |
#include <stdint.h> |
3994
c399fe412dbd
MQTT: WIP, prevent crash after disconnect. Disable auto-reconnect on connection lost callback.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3993
diff
changeset
|
4 |
#include <unistd.h> |
3984
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
5 |
#include <pthread.h> |
3990
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
6 |
#include <string.h> |
3994
c399fe412dbd
MQTT: WIP, prevent crash after disconnect. Disable auto-reconnect on connection lost callback.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3993
diff
changeset
|
7 |
#include <stdio.h> |
3984
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
8 |
|
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
9 |
#include "MQTTClient.h" |
3980
96acfee19caf
MQTT WIP: intermediate state, implementing MQTT client C code generator.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3979
diff
changeset
|
10 |
#include "MQTTClientPersistence.h" |
3979
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
11 |
|
3995
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
12 |
#define _Log(level, ...) \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
13 |
{{ \ |
3998
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
14 |
char mstr[256]; \ |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
15 |
snprintf(mstr, 255, __VA_ARGS__); \ |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
16 |
LogMessage(level, mstr, strlen(mstr)); \ |
3995
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
17 |
printf(__VA_ARGS__); \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
18 |
fflush(stdout); \ |
3979
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
19 |
}} |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
20 |
|
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
21 |
#define LogInfo(...) _Log(LOG_INFO, __VA_ARGS__); |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
22 |
#define LogError(...) _Log(LOG_CRITICAL, __VA_ARGS__); |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
23 |
#define LogWarning(...) _Log(LOG_WARNING, __VA_ARGS__); |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
24 |
|
3998
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
25 |
// Selected debug level for paho stack |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
26 |
// can be: |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
27 |
// MQTTCLIENT_TRACE_PROTOCOL, MQTTCLIENT_TRACE_MAXIMUM, MQTTCLIENT_TRACE_ERROR |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
28 |
#define MQTT_DEBUG_LEVEL MQTTCLIENT_TRACE_ERROR |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
29 |
|
3984
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
30 |
void trace_callback(enum MQTTCLIENT_TRACE_LEVELS level, char* message) |
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
31 |
{{ |
3998
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
32 |
if(level >= MQTT_DEBUG_LEVEL) |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
33 |
{{ |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
34 |
int beremiz_log_level = (level >= MQTTCLIENT_TRACE_ERROR ) ? LOG_CRITICAL : |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
35 |
(level > MQTTCLIENT_TRACE_MINIMUM) ? LOG_WARNING : |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
36 |
LOG_INFO; |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
37 |
_Log(beremiz_log_level,"Paho MQTT Trace : %s\n", message); |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
38 |
}} |
3984
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
39 |
}} |
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
40 |
|
3989
987c69b1582f
MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3988
diff
changeset
|
41 |
#define CHANGED 1 |
987c69b1582f
MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3988
diff
changeset
|
42 |
#define UNCHANGED 0 |
987c69b1582f
MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3988
diff
changeset
|
43 |
|
3995
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
44 |
#define DECL_VAR(iec_type, C_type, c_loc_name) \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
45 |
static C_type PLC_##c_loc_name##_buf = 0; \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
46 |
static C_type MQTT_##c_loc_name##_buf = 0; \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
47 |
static int MQTT_##c_loc_name##_state = UNCHANGED; /* systematically published at init */ \ |
3984
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
48 |
C_type *c_loc_name = &PLC_##c_loc_name##_buf; |
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
49 |
|
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
50 |
{decl} |
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
51 |
|
3986
98bd0bb33ce4
MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3984
diff
changeset
|
52 |
static MQTTClient client; |
98bd0bb33ce4
MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3984
diff
changeset
|
53 |
#ifdef USE_MQTT_5 |
98bd0bb33ce4
MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3984
diff
changeset
|
54 |
static MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer5; |
98bd0bb33ce4
MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3984
diff
changeset
|
55 |
#else |
98bd0bb33ce4
MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3984
diff
changeset
|
56 |
static MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer; |
98bd0bb33ce4
MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3984
diff
changeset
|
57 |
#endif |
3989
987c69b1582f
MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3988
diff
changeset
|
58 |
|
987c69b1582f
MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3988
diff
changeset
|
59 |
/* condition to quit publish thread */ |
987c69b1582f
MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3988
diff
changeset
|
60 |
static int MQTT_stop_thread = 0; |
987c69b1582f
MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3988
diff
changeset
|
61 |
|
987c69b1582f
MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3988
diff
changeset
|
62 |
/* condition to wakeup publish thread */ |
987c69b1582f
MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3988
diff
changeset
|
63 |
static int MQTT_any_pub_var_changed = 0; |
987c69b1582f
MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3988
diff
changeset
|
64 |
|
4002
6c2b80b4515d
MQTT: WIP, re-organized disconnection handling, now keep track of connection status and reconnect accordingly.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3998
diff
changeset
|
65 |
/* Keep track of connection state */ |
6c2b80b4515d
MQTT: WIP, re-organized disconnection handling, now keep track of connection status and reconnect accordingly.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3998
diff
changeset
|
66 |
static volatile int MQTT_is_disconnected = 1; |
6c2b80b4515d
MQTT: WIP, re-organized disconnection handling, now keep track of connection status and reconnect accordingly.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3998
diff
changeset
|
67 |
|
3997
364e09f5faea
MQTT: WIP, destroys paho client during failure handling at init and at cleanup. Use mutex to protect retrieve.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3996
diff
changeset
|
68 |
/* mutex to keep incoming PLC data consistent */ |
364e09f5faea
MQTT: WIP, destroys paho client during failure handling at init and at cleanup. Use mutex to protect retrieve.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3996
diff
changeset
|
69 |
static pthread_mutex_t MQTT_retrieve_mutex = PTHREAD_MUTEX_INITIALIZER; |
364e09f5faea
MQTT: WIP, destroys paho client during failure handling at init and at cleanup. Use mutex to protect retrieve.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3996
diff
changeset
|
70 |
|
364e09f5faea
MQTT: WIP, destroys paho client during failure handling at init and at cleanup. Use mutex to protect retrieve.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3996
diff
changeset
|
71 |
/* mutex to keep outgoing PLC data consistent, and protect MQTT_any_pub_var_changed */ |
3998
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
72 |
static pthread_mutex_t MQTT_thread_wakeup_mutex = PTHREAD_MUTEX_INITIALIZER; |
3989
987c69b1582f
MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3988
diff
changeset
|
73 |
|
987c69b1582f
MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3988
diff
changeset
|
74 |
/* wakeup publish thread when PLC changed published variable */ |
3998
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
75 |
static pthread_cond_t MQTT_thread_wakeup = PTHREAD_COND_INITIALIZER; |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
76 |
|
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
77 |
/* thread that handles publish and reconnection */ |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
78 |
static pthread_t MQTT_thread; |
3986
98bd0bb33ce4
MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3984
diff
changeset
|
79 |
|
3995
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
80 |
#define INIT_TOPIC(topic, iec_type, c_loc_name) \ |
3990
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
81 |
{{#topic, &MQTT_##c_loc_name##_buf, &MQTT_##c_loc_name##_state, iec_type##_ENUM}}, |
3984
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
82 |
|
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
83 |
static struct {{ |
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
84 |
const char *topic; //null terminated topic string |
3990
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
85 |
void *mqtt_pdata; // pointer to data from/for MQTT stack |
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
86 |
int *mqtt_pchanged; // pointer to changed flag |
3984
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
87 |
__IEC_types_enum vartype; |
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
88 |
}} topics [] = {{ |
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
89 |
{topics} |
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
90 |
}}; |
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
91 |
|
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
92 |
void __cleanup_{locstr}(void) |
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
93 |
{{ |
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
94 |
int rc; |
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
95 |
|
3993
78f9abfb32a0
MQTT: Stop publish thread properly on PLC stop
Edouard Tisserant <edouard@beremiz.fr>
parents:
3992
diff
changeset
|
96 |
/* stop publish thread */ |
78f9abfb32a0
MQTT: Stop publish thread properly on PLC stop
Edouard Tisserant <edouard@beremiz.fr>
parents:
3992
diff
changeset
|
97 |
MQTT_stop_thread = 1; |
3998
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
98 |
if (pthread_mutex_lock(&MQTT_thread_wakeup_mutex) == 0){{ |
3993
78f9abfb32a0
MQTT: Stop publish thread properly on PLC stop
Edouard Tisserant <edouard@beremiz.fr>
parents:
3992
diff
changeset
|
99 |
/* unblock publish thread so that it can stop normally */ |
3998
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
100 |
pthread_cond_signal(&MQTT_thread_wakeup); |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
101 |
pthread_mutex_unlock(&MQTT_thread_wakeup_mutex); |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
102 |
}} |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
103 |
pthread_join(MQTT_thread, NULL); |
3984
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
104 |
|
3986
98bd0bb33ce4
MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3984
diff
changeset
|
105 |
#ifdef USE_MQTT_5 |
3984
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
106 |
if (rc = MQTTClient_disconnect5(client, 5000, MQTTREASONCODE_SUCCESS, NULL) != MQTTCLIENT_SUCCESS) |
3986
98bd0bb33ce4
MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3984
diff
changeset
|
107 |
#else |
98bd0bb33ce4
MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3984
diff
changeset
|
108 |
if (rc = MQTTClient_disconnect(client, 5000) != MQTTCLIENT_SUCCESS) |
98bd0bb33ce4
MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3984
diff
changeset
|
109 |
#endif |
3984
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
110 |
{{ |
3995
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
111 |
LogError("MQTT Failed to disconnect, return code %d\n", rc); |
3979
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
112 |
}} |
3984
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
113 |
MQTTClient_destroy(&client); |
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
114 |
}} |
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
115 |
|
3998
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
116 |
void connectionLost(void* context, char* reason) |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
117 |
{{ |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
118 |
int rc; |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
119 |
LogWarning("ConnectionLost, reconnecting\\n"); |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
120 |
if (pthread_mutex_lock(&MQTT_thread_wakeup_mutex) == 0){{ |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
121 |
/* unblock publish thread so that it can reconnect */ |
4002
6c2b80b4515d
MQTT: WIP, re-organized disconnection handling, now keep track of connection status and reconnect accordingly.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3998
diff
changeset
|
122 |
MQTT_is_disconnected = 1; |
3998
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
123 |
pthread_cond_signal(&MQTT_thread_wakeup); |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
124 |
pthread_mutex_unlock(&MQTT_thread_wakeup_mutex); |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
125 |
}} |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
126 |
}} |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
127 |
|
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
128 |
|
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
129 |
|
3984
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
130 |
int messageArrived(void *context, char *topicName, int topicLen, MQTTClient_message *message) |
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
131 |
{{ |
3990
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
132 |
int low = 0; |
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
133 |
int size = sizeof(topics) / sizeof(topics[0]); |
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
134 |
int high = size - 1; |
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
135 |
int mid; |
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
136 |
|
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
137 |
// bisect topic among subscribed topics |
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
138 |
while (low <= high) {{ |
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
139 |
int res; |
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
140 |
mid = low + (high - low) / 2; |
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
141 |
res = strncmp(topics[mid].topic, topicName, topicLen); |
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
142 |
|
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
143 |
// Check if key is present at mid |
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
144 |
if (res == 0) |
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
145 |
goto found; |
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
146 |
|
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
147 |
// If key greater, ignore left half |
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
148 |
if (res < 0) |
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
149 |
low = mid + 1; |
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
150 |
|
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
151 |
// If key is smaller, ignore right half |
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
152 |
else |
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
153 |
high = mid - 1; |
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
154 |
}} |
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
155 |
// If we reach here, then the element was not present |
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
156 |
LogWarning("MQTT unknown topic: %s", topicName); |
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
157 |
goto exit; |
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
158 |
|
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
159 |
found: |
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
160 |
if(__get_type_enum_size(topics[mid].vartype) == message->payloadlen){{ |
3997
364e09f5faea
MQTT: WIP, destroys paho client during failure handling at init and at cleanup. Use mutex to protect retrieve.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3996
diff
changeset
|
161 |
if (pthread_mutex_lock(&MQTT_retrieve_mutex) == 0){{ |
364e09f5faea
MQTT: WIP, destroys paho client during failure handling at init and at cleanup. Use mutex to protect retrieve.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3996
diff
changeset
|
162 |
memcpy(topics[mid].mqtt_pdata, (char*)message->payload, message->payloadlen); |
364e09f5faea
MQTT: WIP, destroys paho client during failure handling at init and at cleanup. Use mutex to protect retrieve.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3996
diff
changeset
|
163 |
*topics[mid].mqtt_pchanged = 1; |
364e09f5faea
MQTT: WIP, destroys paho client during failure handling at init and at cleanup. Use mutex to protect retrieve.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3996
diff
changeset
|
164 |
pthread_mutex_unlock(&MQTT_retrieve_mutex); |
364e09f5faea
MQTT: WIP, destroys paho client during failure handling at init and at cleanup. Use mutex to protect retrieve.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3996
diff
changeset
|
165 |
}} |
3990
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
166 |
}} else {{ |
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
167 |
LogWarning("MQTT wrong payload size for topic: %s. Should be %d, but got %d.", |
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
168 |
topicName, __get_type_enum_size(topics[mid].vartype), message->payloadlen); |
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
169 |
}} |
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
170 |
exit: |
3984
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
171 |
MQTTClient_freeMessage(&message); |
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
172 |
MQTTClient_free(topicName); |
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
173 |
return 1; |
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
174 |
}} |
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
175 |
|
3995
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
176 |
#define INIT_NoAuth() \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
177 |
LogInfo("MQTT Init no auth\n"); |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
178 |
|
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
179 |
#define INIT_x509(PrivateKey, Certificate) \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
180 |
LogInfo("MQTT Init x509 %s,%s\n", PrivateKey, Certificate); |
3984
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
181 |
/* TODO */ |
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
182 |
|
3995
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
183 |
#define INIT_UserPassword(User, Password) \ |
3998
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
184 |
LogInfo("MQTT Init UserPassword %s,%s\n", User, Password); \ |
3995
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
185 |
conn_opts.username = User; \ |
3990
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
186 |
conn_opts.password = Password; |
3984
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
187 |
|
3986
98bd0bb33ce4
MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3984
diff
changeset
|
188 |
#ifdef USE_MQTT_5 |
3995
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
189 |
#define _SUBSCRIBE(Topic, QoS) \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
190 |
MQTTResponse response = MQTTClient_subscribe5(client, #Topic, QoS, NULL, NULL); \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
191 |
/* when using MQTT5 responce code is 1 for some reason even if no error */ \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
192 |
rc = response.reasonCode == 1 ? MQTTCLIENT_SUCCESS : response.reasonCode; \ |
3986
98bd0bb33ce4
MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3984
diff
changeset
|
193 |
MQTTResponse_free(response); |
98bd0bb33ce4
MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3984
diff
changeset
|
194 |
#else |
3995
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
195 |
#define _SUBSCRIBE(Topic, QoS) \ |
3986
98bd0bb33ce4
MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3984
diff
changeset
|
196 |
rc = MQTTClient_subscribe(client, #Topic, QoS); |
98bd0bb33ce4
MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3984
diff
changeset
|
197 |
#endif |
98bd0bb33ce4
MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3984
diff
changeset
|
198 |
|
3995
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
199 |
#define INIT_SUBSCRIPTION(Topic, QoS) \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
200 |
{{ \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
201 |
int rc; \ |
3998
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
202 |
_SUBSCRIBE(Topic, QoS) \ |
3995
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
203 |
if (rc != MQTTCLIENT_SUCCESS) \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
204 |
{{ \ |
3998
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
205 |
LogError("MQTT client failed to subscribe to '%s', return code %d\n", #Topic, rc); \ |
3995
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
206 |
}} \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
207 |
}} |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
208 |
|
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
209 |
|
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
210 |
#ifdef USE_MQTT_5 |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
211 |
#define _PUBLISH(Topic, QoS, C_type, c_loc_name, Retained) \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
212 |
MQTTResponse response = MQTTClient_publish5(client, #Topic, sizeof(C_type), \ |
3998
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
213 |
&MQTT_##c_loc_name##_buf, QoS, Retained, NULL, NULL); \ |
3995
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
214 |
rc = response.reasonCode; \ |
3987
cec48fc7ccd0
MQTT: WIP, now publishes initial values at init.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3986
diff
changeset
|
215 |
MQTTResponse_free(response); |
cec48fc7ccd0
MQTT: WIP, now publishes initial values at init.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3986
diff
changeset
|
216 |
#else |
3995
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
217 |
#define _PUBLISH(Topic, QoS, C_type, c_loc_name, Retained) \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
218 |
rc = MQTTClient_publish(client, #Topic, sizeof(C_type), \ |
3987
cec48fc7ccd0
MQTT: WIP, now publishes initial values at init.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3986
diff
changeset
|
219 |
&PLC_##c_loc_name##_buf, QoS, Retained, NULL); |
cec48fc7ccd0
MQTT: WIP, now publishes initial values at init.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3986
diff
changeset
|
220 |
#endif |
cec48fc7ccd0
MQTT: WIP, now publishes initial values at init.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3986
diff
changeset
|
221 |
|
3995
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
222 |
#define INIT_PUBLICATION(Topic, QoS, C_type, c_loc_name, Retained) \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
223 |
{{ \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
224 |
int rc; \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
225 |
_PUBLISH(Topic, QoS, C_type, c_loc_name, Retained) \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
226 |
if (rc != MQTTCLIENT_SUCCESS) \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
227 |
{{ \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
228 |
LogError("MQTT client failed to init publication of '%s', return code %d\n", #Topic, rc);\ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
229 |
/* TODO update status variable accordingly */ \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
230 |
}} \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
231 |
}} |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
232 |
|
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
233 |
#define PUBLISH_CHANGE(Topic, QoS, C_type, c_loc_name, Retained) \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
234 |
if(MQTT_##c_loc_name##_state == CHANGED) \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
235 |
{{ \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
236 |
int rc; \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
237 |
_PUBLISH(Topic, QoS, C_type, c_loc_name, Retained) \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
238 |
if (rc != MQTTCLIENT_SUCCESS) \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
239 |
{{ \ |
3998
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
240 |
LogError("MQTT client failed to publish '%s', return code %d\n", #Topic, rc); \ |
3995
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
241 |
/* TODO update status variable accordingly */ \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
242 |
}} else {{ \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
243 |
MQTT_##c_loc_name##_state = UNCHANGED; \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
244 |
}} \ |
3989
987c69b1582f
MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3988
diff
changeset
|
245 |
}} |
987c69b1582f
MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3988
diff
changeset
|
246 |
|
3998
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
247 |
static int _connect_mqtt(void) |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
248 |
{{ |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
249 |
int rc; |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
250 |
|
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
251 |
#ifdef USE_MQTT_5 |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
252 |
MQTTProperties props = MQTTProperties_initializer; |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
253 |
MQTTProperties willProps = MQTTProperties_initializer; |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
254 |
MQTTResponse response = MQTTResponse_initializer; |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
255 |
|
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
256 |
response = MQTTClient_connect5(client, &conn_opts, &props, &willProps); |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
257 |
rc = response.reasonCode; |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
258 |
MQTTResponse_free(response); |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
259 |
#else |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
260 |
rc = MQTTClient_connect(client, &conn_opts); |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
261 |
#endif |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
262 |
|
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
263 |
if (rc != MQTTCLIENT_SUCCESS) {{ |
4002
6c2b80b4515d
MQTT: WIP, re-organized disconnection handling, now keep track of connection status and reconnect accordingly.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3998
diff
changeset
|
264 |
MQTT_is_disconnected = 1; |
3998
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
265 |
return rc; |
4002
6c2b80b4515d
MQTT: WIP, re-organized disconnection handling, now keep track of connection status and reconnect accordingly.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3998
diff
changeset
|
266 |
}}else{{ |
6c2b80b4515d
MQTT: WIP, re-organized disconnection handling, now keep track of connection status and reconnect accordingly.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3998
diff
changeset
|
267 |
MQTT_is_disconnected = 0; |
3998
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
268 |
}} |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
269 |
|
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
270 |
{init_pubsub} |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
271 |
|
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
272 |
return MQTTCLIENT_SUCCESS; |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
273 |
}} |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
274 |
|
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
275 |
static void *__MQTT_thread_proc(void *_unused) {{ |
3989
987c69b1582f
MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3988
diff
changeset
|
276 |
int rc = 0; |
3998
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
277 |
|
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
278 |
while((rc = pthread_mutex_lock(&MQTT_thread_wakeup_mutex)) == 0 && !MQTT_stop_thread){{ |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
279 |
int do_publish; |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
280 |
|
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
281 |
pthread_cond_wait(&MQTT_thread_wakeup, &MQTT_thread_wakeup_mutex); |
4002
6c2b80b4515d
MQTT: WIP, re-organized disconnection handling, now keep track of connection status and reconnect accordingly.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3998
diff
changeset
|
282 |
|
6c2b80b4515d
MQTT: WIP, re-organized disconnection handling, now keep track of connection status and reconnect accordingly.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3998
diff
changeset
|
283 |
if(MQTT_is_disconnected) |
6c2b80b4515d
MQTT: WIP, re-organized disconnection handling, now keep track of connection status and reconnect accordingly.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3998
diff
changeset
|
284 |
{{ |
6c2b80b4515d
MQTT: WIP, re-organized disconnection handling, now keep track of connection status and reconnect accordingly.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3998
diff
changeset
|
285 |
/* TODO growing retry delay */ |
6c2b80b4515d
MQTT: WIP, re-organized disconnection handling, now keep track of connection status and reconnect accordingly.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3998
diff
changeset
|
286 |
/* TODO max retry delay as config parameter */ |
6c2b80b4515d
MQTT: WIP, re-organized disconnection handling, now keep track of connection status and reconnect accordingly.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3998
diff
changeset
|
287 |
sleep(5); |
6c2b80b4515d
MQTT: WIP, re-organized disconnection handling, now keep track of connection status and reconnect accordingly.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3998
diff
changeset
|
288 |
rc = _connect_mqtt(); |
6c2b80b4515d
MQTT: WIP, re-organized disconnection handling, now keep track of connection status and reconnect accordingly.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3998
diff
changeset
|
289 |
if (rc == MQTTCLIENT_SUCCESS) {{ |
6c2b80b4515d
MQTT: WIP, re-organized disconnection handling, now keep track of connection status and reconnect accordingly.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3998
diff
changeset
|
290 |
LogInfo("MQTT Reconnected\n"); |
6c2b80b4515d
MQTT: WIP, re-organized disconnection handling, now keep track of connection status and reconnect accordingly.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3998
diff
changeset
|
291 |
}} else {{ |
6c2b80b4515d
MQTT: WIP, re-organized disconnection handling, now keep track of connection status and reconnect accordingly.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3998
diff
changeset
|
292 |
LogError("MQTT Reconnect Failed, return code %d\n", rc); |
6c2b80b4515d
MQTT: WIP, re-organized disconnection handling, now keep track of connection status and reconnect accordingly.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3998
diff
changeset
|
293 |
}} |
6c2b80b4515d
MQTT: WIP, re-organized disconnection handling, now keep track of connection status and reconnect accordingly.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3998
diff
changeset
|
294 |
}} |
6c2b80b4515d
MQTT: WIP, re-organized disconnection handling, now keep track of connection status and reconnect accordingly.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3998
diff
changeset
|
295 |
if(!MQTT_is_disconnected && MQTT_any_pub_var_changed) |
3996
4eb23bb4bc2f
MQTT: Implements reconnecting in publish thread in case thread is waken-up but client is disconnected.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3995
diff
changeset
|
296 |
{{ |
3989
987c69b1582f
MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3988
diff
changeset
|
297 |
/* publish changes, and reset variable's state to UNCHANGED */ |
987c69b1582f
MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3988
diff
changeset
|
298 |
{publish_changes} |
3998
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
299 |
MQTT_any_pub_var_changed = 0; |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
300 |
}} |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
301 |
|
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
302 |
pthread_mutex_unlock(&MQTT_thread_wakeup_mutex); |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
303 |
|
3993
78f9abfb32a0
MQTT: Stop publish thread properly on PLC stop
Edouard Tisserant <edouard@beremiz.fr>
parents:
3992
diff
changeset
|
304 |
if(MQTT_stop_thread) break; |
3989
987c69b1582f
MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3988
diff
changeset
|
305 |
}} |
987c69b1582f
MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3988
diff
changeset
|
306 |
|
987c69b1582f
MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3988
diff
changeset
|
307 |
if(!MQTT_stop_thread){{ |
987c69b1582f
MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3988
diff
changeset
|
308 |
/* if thread exits outside of normal shutdown, report error*/ |
3995
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
309 |
LogError("MQTT client thread exited unexpectedly, return code %d\n", rc); |
3989
987c69b1582f
MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3988
diff
changeset
|
310 |
}} |
987c69b1582f
MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3988
diff
changeset
|
311 |
}} |
987c69b1582f
MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3988
diff
changeset
|
312 |
|
3979
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
313 |
int __init_{locstr}(int argc,char **argv) |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
314 |
{{ |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
315 |
char *uri = "{uri}"; |
3980
96acfee19caf
MQTT WIP: intermediate state, implementing MQTT client C code generator.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3979
diff
changeset
|
316 |
char *clientID = "{clientID}"; |
96acfee19caf
MQTT WIP: intermediate state, implementing MQTT client C code generator.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3979
diff
changeset
|
317 |
int rc; |
3984
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
318 |
|
3990
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
319 |
MQTTClient_createOptions createOpts = MQTTClient_createOptions_initializer; |
3984
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
320 |
|
3986
98bd0bb33ce4
MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3984
diff
changeset
|
321 |
#ifdef USE_MQTT_5 |
3990
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
322 |
conn_opts.MQTTVersion = MQTTVERSION_5; |
3984
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
323 |
conn_opts.cleanstart = 1; |
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
324 |
|
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
325 |
createOpts.MQTTVersion = MQTTVERSION_5; |
3986
98bd0bb33ce4
MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3984
diff
changeset
|
326 |
#else |
3990
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
327 |
conn_opts.cleansession = 1; |
3986
98bd0bb33ce4
MQTT: WIP, now connects to broker. Added MQTT 3 support and protocol version selection in IDE.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3984
diff
changeset
|
328 |
#endif |
3984
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
329 |
|
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
330 |
MQTTClient_setTraceCallback(trace_callback); |
3998
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
331 |
MQTTClient_setTraceLevel(MQTT_DEBUG_LEVEL); |
3984
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
332 |
|
3990
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
333 |
rc = MQTTClient_createWithOptions( |
3984
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
334 |
&client, uri, clientID, MQTTCLIENT_PERSISTENCE_NONE, NULL, &createOpts); |
3990
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
335 |
if (rc != MQTTCLIENT_SUCCESS) |
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
336 |
{{ |
3995
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
337 |
LogError("MQTT Failed to create client, return code %d\n", rc); |
3997
364e09f5faea
MQTT: WIP, destroys paho client during failure handling at init and at cleanup. Use mutex to protect retrieve.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3996
diff
changeset
|
338 |
goto exit_error; |
3984
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
339 |
}} |
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
340 |
|
3998
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
341 |
rc = MQTTClient_setCallbacks(client, NULL, connectionLost, messageArrived, NULL); |
3990
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
342 |
if (rc != MQTTCLIENT_SUCCESS) |
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
343 |
{{ |
3995
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
344 |
LogError("MQTT Failed to set callbacks, return code %d\n", rc); |
3997
364e09f5faea
MQTT: WIP, destroys paho client during failure handling at init and at cleanup. Use mutex to protect retrieve.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3996
diff
changeset
|
345 |
goto exit_error; |
3990
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
346 |
}} |
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
347 |
|
3998
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
348 |
{init} |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
349 |
|
3990
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
350 |
rc = _connect_mqtt(); |
3998
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
351 |
if (rc == MQTTCLIENT_SUCCESS) {{ |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
352 |
LogInfo("MQTT Connected\n"); |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
353 |
}} else {{ |
3995
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
354 |
LogError("MQTT Connect Failed, return code %d\n", rc); |
3998
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
355 |
// Connect error at init is fine, publish thread will retry later |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
356 |
}} |
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
357 |
|
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
358 |
/* start MQTT thread */ |
3997
364e09f5faea
MQTT: WIP, destroys paho client during failure handling at init and at cleanup. Use mutex to protect retrieve.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3996
diff
changeset
|
359 |
MQTT_stop_thread = 0; |
3998
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
360 |
rc = pthread_create(&MQTT_thread, NULL, &__MQTT_thread_proc, NULL); |
3997
364e09f5faea
MQTT: WIP, destroys paho client during failure handling at init and at cleanup. Use mutex to protect retrieve.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3996
diff
changeset
|
361 |
if (rc != 0) {{ |
364e09f5faea
MQTT: WIP, destroys paho client during failure handling at init and at cleanup. Use mutex to protect retrieve.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3996
diff
changeset
|
362 |
LogError("MQTT cannot create thread, return code %d\n", rc); |
364e09f5faea
MQTT: WIP, destroys paho client during failure handling at init and at cleanup. Use mutex to protect retrieve.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3996
diff
changeset
|
363 |
goto exit_error; |
364e09f5faea
MQTT: WIP, destroys paho client during failure handling at init and at cleanup. Use mutex to protect retrieve.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3996
diff
changeset
|
364 |
}} |
3984
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
365 |
|
3979
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
366 |
return 0; |
3997
364e09f5faea
MQTT: WIP, destroys paho client during failure handling at init and at cleanup. Use mutex to protect retrieve.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3996
diff
changeset
|
367 |
|
364e09f5faea
MQTT: WIP, destroys paho client during failure handling at init and at cleanup. Use mutex to protect retrieve.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3996
diff
changeset
|
368 |
exit_error: |
364e09f5faea
MQTT: WIP, destroys paho client during failure handling at init and at cleanup. Use mutex to protect retrieve.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3996
diff
changeset
|
369 |
MQTTClient_destroy(&client); |
364e09f5faea
MQTT: WIP, destroys paho client during failure handling at init and at cleanup. Use mutex to protect retrieve.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3996
diff
changeset
|
370 |
return rc; |
3979
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
371 |
}} |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
372 |
|
3995
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
373 |
#define READ_VALUE(c_loc_name, C_type) \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
374 |
if(MQTT_##c_loc_name##_state == CHANGED){{ \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
375 |
/* TODO care about endianess */ \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
376 |
PLC_##c_loc_name##_buf = MQTT_##c_loc_name##_buf; \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
377 |
MQTT_##c_loc_name##_state = UNCHANGED; \ |
3990
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
378 |
}} |
3979
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
379 |
|
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
380 |
void __retrieve_{locstr}(void) |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
381 |
{{ |
3997
364e09f5faea
MQTT: WIP, destroys paho client during failure handling at init and at cleanup. Use mutex to protect retrieve.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3996
diff
changeset
|
382 |
if (pthread_mutex_trylock(&MQTT_retrieve_mutex) == 0){{ |
3979
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
383 |
{retrieve} |
3997
364e09f5faea
MQTT: WIP, destroys paho client during failure handling at init and at cleanup. Use mutex to protect retrieve.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3996
diff
changeset
|
384 |
pthread_mutex_unlock(&MQTT_retrieve_mutex); |
3990
24656e0e8732
MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents:
3989
diff
changeset
|
385 |
}} |
3984
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
386 |
}} |
883a85b9ebcc
MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3981
diff
changeset
|
387 |
|
3995
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
388 |
#define WRITE_VALUE(c_loc_name, C_type) \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
389 |
/* TODO care about endianess */ \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
390 |
if(MQTT_##c_loc_name##_buf != PLC_##c_loc_name##_buf){{ \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
391 |
MQTT_##c_loc_name##_buf = PLC_##c_loc_name##_buf; \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
392 |
MQTT_##c_loc_name##_state = CHANGED; \ |
84a668564748
MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3994
diff
changeset
|
393 |
MQTT_any_pub_var_changed = 1; \ |
3989
987c69b1582f
MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3988
diff
changeset
|
394 |
}} |
3979
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
395 |
|
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
396 |
void __publish_{locstr}(void) |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
397 |
{{ |
3998
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
398 |
if (pthread_mutex_trylock(&MQTT_thread_wakeup_mutex) == 0){{ |
3989
987c69b1582f
MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3988
diff
changeset
|
399 |
MQTT_any_pub_var_changed = 0; |
987c69b1582f
MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3988
diff
changeset
|
400 |
/* copy PLC_* variables to MQTT_*, and mark those who changed */ |
3979
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
401 |
{publish} |
3989
987c69b1582f
MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3988
diff
changeset
|
402 |
/* if any change detcted, unblock publish thread */ |
4002
6c2b80b4515d
MQTT: WIP, re-organized disconnection handling, now keep track of connection status and reconnect accordingly.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3998
diff
changeset
|
403 |
if(MQTT_any_pub_var_changed || MQTT_is_disconnected){{ |
3998
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
404 |
pthread_cond_signal(&MQTT_thread_wakeup); |
3989
987c69b1582f
MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3988
diff
changeset
|
405 |
}} |
3998
0145c60b9560
MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3997
diff
changeset
|
406 |
pthread_mutex_unlock(&MQTT_thread_wakeup_mutex); |
3989
987c69b1582f
MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3988
diff
changeset
|
407 |
}} else {{ |
987c69b1582f
MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3988
diff
changeset
|
408 |
/* TODO if couldn't lock mutex set status variable accordingly */ |
987c69b1582f
MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents:
3988
diff
changeset
|
409 |
}} |
3979
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
410 |
}} |
76295adcf940
WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff
changeset
|
411 |