mqtt/mqtt_template.c
author Edouard Tisserant <edouard@beremiz.fr>
Fri, 30 Aug 2024 11:50:23 +0200
changeset 4008 f30573e98600
parent 4005 482452574fb4
child 4012 6337c9c2c379
permissions -rw-r--r--
IDE: allow structures to be located.
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
4005
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
    59
MQTTClient_SSLOptions ssl_opts = MQTTClient_SSLOptions_initializer;
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
    60
3989
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
    61
/* 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
    62
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
    63
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
    64
/* 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
    65
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
    66
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
    67
/* 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
    68
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
    69
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
    70
/* 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
    71
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
    72
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
    73
/* 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
    74
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
    75
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
    76
/* 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
    77
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
    78
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
    79
/* 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
    80
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
    81
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
    82
#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
    83
{{#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
    84
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
    85
static struct {{
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
    86
    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
    87
    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
    88
    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
    89
    __IEC_types_enum vartype;
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
    90
}} topics [] = {{
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
    91
{topics}
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
    92
}};
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
void __cleanup_{locstr}(void)
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
    95
{{
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
    96
    int rc;
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
    97
3993
78f9abfb32a0 MQTT: Stop publish thread properly on PLC stop
Edouard Tisserant <edouard@beremiz.fr>
parents: 3992
diff changeset
    98
    /* stop publish thread */
78f9abfb32a0 MQTT: Stop publish thread properly on PLC stop
Edouard Tisserant <edouard@beremiz.fr>
parents: 3992
diff changeset
    99
    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
   100
    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
   101
        /* 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
   102
        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
   103
        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
   104
    }}
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
   105
    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
   106
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
#ifdef USE_MQTT_5
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   108
    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
   109
#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
   110
    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
   111
#endif
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   112
    {{
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   113
        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
   114
    }}
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   115
    MQTTClient_destroy(&client);
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   116
}}
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   117
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
   118
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
   119
{{
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
    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
   121
    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
   122
    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
   123
        /* 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
   124
        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
   125
        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
   126
        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
   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
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
   130
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
   131
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   132
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
   133
{{
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   134
    int low = 0;
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   135
    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
   136
    int high = size - 1;
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   137
    int mid;
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   138
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   139
    // bisect topic among subscribed topics
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   140
    while (low <= high) {{
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   141
        int res;
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   142
        mid = low + (high - low) / 2;
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   143
        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
   144
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   145
        // 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
   146
        if (res == 0)
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   147
            goto found;
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   148
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   149
        // If key greater, ignore left half
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   150
        if (res < 0)
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   151
            low = mid + 1;
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   152
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   153
        // 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
   154
        else
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   155
            high = mid - 1;
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   156
    }}
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   157
    // 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
   158
    LogWarning("MQTT unknown topic: %s", topicName);
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   159
    goto exit;
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   160
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   161
found:
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   162
    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
   163
        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
   164
            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
   165
            *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
   166
            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
   167
        }}
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   168
    }} else {{
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   169
        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
   170
            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
   171
    }}
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   172
exit:
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   173
    MQTTClient_freeMessage(&message);
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   174
    MQTTClient_free(topicName);
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   175
    return 1;
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   176
}}
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   177
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   178
#define INIT_NoAuth()                                                                             \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   179
    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
   180
4005
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   181
#define INIT_x509(Verify, KeyStore, TrustStore)                                                   \
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   182
    LogInfo("MQTT Init x509 with %s,%s\n", KeyStore, TrustStore)                                  \
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   183
    ssl_opts.verify = Verify;                                                                     \
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   184
    ssl_opts.keyStore = KeyStore;                                                                 \
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   185
    ssl_opts.trustStore = TrustStore;                                                             \
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   186
    conn_opts.ssl = &ssl_opts;
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   187
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   188
#define INIT_PSK(Secret, ID)                                                                      \
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   189
    LogError("MQTT PSK NOT IMPLEMENTED\n")                                                        \
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   190
    /* LogInfo("MQTT Init PSK for ID %s\n", ID) */                                                \
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   191
    /* ssl_opts.ssl_psk_cb = TODO; */                                                             \
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   192
    /* ssl_opts.ssl_psk_context = TODO; */                                                        \
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   193
    conn_opts.ssl = &ssl_opts;
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   194
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   195
#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
   196
    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
   197
    conn_opts.username = User;                                                                    \
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   198
    conn_opts.password = Password;
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   199
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
   200
#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
   201
#define _SUBSCRIBE(Topic, QoS)                                                                    \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   202
        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
   203
        /* 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
   204
        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
   205
        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
   206
#else
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   207
#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
   208
        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
   209
#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
   210
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   211
#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
   212
    {{                                                                                            \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   213
        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
   214
        _SUBSCRIBE(Topic, QoS)                                                                    \
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   215
        if (rc != MQTTCLIENT_SUCCESS)                                                             \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   216
        {{                                                                                        \
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
   217
            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
   218
        }}                                                                                        \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   219
    }}
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   220
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   221
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   222
#ifdef USE_MQTT_5
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   223
#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
   224
        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
   225
            &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
   226
        rc = response.reasonCode;                                                                 \
3987
cec48fc7ccd0 MQTT: WIP, now publishes initial values at init.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3986
diff changeset
   227
        MQTTResponse_free(response);
cec48fc7ccd0 MQTT: WIP, now publishes initial values at init.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3986
diff changeset
   228
#else
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   229
#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
   230
        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
   231
            &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
   232
#endif
cec48fc7ccd0 MQTT: WIP, now publishes initial values at init.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3986
diff changeset
   233
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   234
#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
   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
        {{                                                                                        \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   240
            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
   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
        }}                                                                                        \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   243
    }}
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   244
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   245
#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
   246
    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
   247
    {{                                                                                            \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   248
        int rc;                                                                                   \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   249
        _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
   250
        if (rc != MQTTCLIENT_SUCCESS)                                                             \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   251
        {{                                                                                        \
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
   252
            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
   253
            /* 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
   254
        }} else {{                                                                                \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   255
            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
   256
        }}                                                                                        \
3989
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   257
    }}
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   258
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
   259
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
   260
{{
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
    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
   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
#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
   264
    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
   265
    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
   266
    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
   267
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
    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
   269
    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
   270
    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
   271
#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
   272
    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
   273
#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
   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
    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
   276
        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
   277
        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
   278
    }}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
   279
        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
   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
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
   282
{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
   283
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
   284
    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
   285
}}
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
   286
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
   287
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
   288
    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
   289
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
   290
    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
   291
        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
   292
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
   293
        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
   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)
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
   296
        {{
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
   297
            /* 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
   298
            /* 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
   299
            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
   300
            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
   301
            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
   302
                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
   303
            }} 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
   304
                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
   305
            }}
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
   306
        }} 
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
   307
        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
   308
        {{
3989
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   309
            /* 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
   310
{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
   311
            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
   312
        }}
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
   313
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
   314
        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
   315
3993
78f9abfb32a0 MQTT: Stop publish thread properly on PLC stop
Edouard Tisserant <edouard@beremiz.fr>
parents: 3992
diff changeset
   316
        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
   317
    }}
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   318
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   319
    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
   320
        /* 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
   321
        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
   322
    }}
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   323
}}
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   324
    
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   325
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
   326
{{
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   327
    char *uri = "{uri}";
3980
96acfee19caf MQTT WIP: intermediate state, implementing MQTT client C code generator.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3979
diff changeset
   328
    char *clientID = "{clientID}";
96acfee19caf MQTT WIP: intermediate state, implementing MQTT client C code generator.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3979
diff changeset
   329
    int rc;
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   330
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   331
    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
   332
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
   333
#ifdef USE_MQTT_5
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   334
    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
   335
    conn_opts.cleanstart = 1;
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   336
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   337
    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
   338
#else
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   339
    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
   340
#endif
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   341
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   342
    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
   343
    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
   344
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   345
    rc = MQTTClient_createWithOptions(
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   346
        &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
   347
    if (rc != MQTTCLIENT_SUCCESS)
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   348
    {{
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   349
        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
   350
        goto exit_error;
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   351
    }}
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   352
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
   353
    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
   354
    if (rc != MQTTCLIENT_SUCCESS)
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   355
    {{
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   356
        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
   357
        goto exit_error;
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   358
    }}
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   359
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
{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
   361
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   362
    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
   363
	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
   364
		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
   365
	}} else {{
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   366
        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
   367
        // 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
   368
    }}
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
   369
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
   370
    /* 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
   371
    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
   372
    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
   373
    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
   374
        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
   375
        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
   376
    }}
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   377
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   378
    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
   379
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
   380
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
   381
    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
   382
    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
   383
}}
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   384
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   385
#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
   386
    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
   387
        /* TODO care about endianess */ \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   388
        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
   389
        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
   390
    }}
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   391
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   392
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
   393
{{
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
   394
    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
   395
{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
   396
        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
   397
    }}
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   398
}}
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   399
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   400
#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
   401
    /* TODO care about endianess */ \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   402
    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
   403
        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
   404
        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
   405
        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
   406
    }}
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   407
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   408
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
   409
{{
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
   410
    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
   411
        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
   412
        /* 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
   413
{publish}
3989
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   414
        /* 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
   415
        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
   416
            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
   417
        }}
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
   418
        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
   419
    }} else {{
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   420
        /* 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
   421
    }}
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   422
}}
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   423