mqtt/mqtt_template.c
author Edouard Tisserant <edouard@beremiz.fr>
Thu, 26 Sep 2024 15:28:10 +0200
changeset 4022 dee0efbf8452
parent 4021 07504d4c40ee
child 4023 b344393859df
permissions -rw-r--r--
MQTT: strip unsupported types from type choice dropdown, better wrong type detection in build.
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>
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
     8
#include <errno.h>
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
     9
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
    10
#include "frozen.h"
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
    11
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
    12
#include "MQTTClient.h"
3980
96acfee19caf MQTT WIP: intermediate state, implementing MQTT client C code generator.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3979
diff changeset
    13
#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
    14
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
    15
#include "POUS.h"
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
    16
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
    17
#define _Log(level, ...)                                                                          \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
    18
    {{                                                                                            \
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
    19
        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
    20
        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
    21
        LogMessage(level, mstr, strlen(mstr));                                                    \
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    22
    }}
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    23
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
    24
#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
    25
#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
    26
#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
    27
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
    28
// 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
    29
// 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
    30
// 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
    31
#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
    32
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
    33
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
    34
{{
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
    35
    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
    36
    {{
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
        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
    38
                                (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
    39
                                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
    40
        _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
    41
    }}
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
    42
}}
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
    43
3989
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
    44
#define CHANGED 1
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
    45
#define UNCHANGED 0
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
    46
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
    47
#define DECL_VAR(iec_type, C_type, c_loc_name)                                                     \
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
    48
static C_type PLC_##c_loc_name##_buf;                                                              \
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
    49
static C_type MQTT_##c_loc_name##_buf;                                                             \
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
    50
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
    51
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
    52
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
    53
{decl}
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
    54
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
    55
/* JSON topic content encoding macros matching "json_decl" in substitution*/
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
    56
4018
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    57
#define printf_fmt_BOOL   "%B"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    58
#define printf_fmt_SINT   "%hhd"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    59
#define printf_fmt_USINT  "%uhhd"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    60
#define printf_fmt_INT    "%hd" 
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    61
#define printf_fmt_UINT   "%uhd"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    62
#define printf_fmt_DINT   "%d" 
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    63
#define printf_fmt_UDINT  "%ud"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    64
#define printf_fmt_LINT   "%ld"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    65
#define printf_fmt_ULINT  "%uld"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    66
#define printf_fmt_REAL   "%f"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    67
#define printf_fmt_LREAL  "%Lf"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    68
#define printf_fmt_STRING "%.*Q"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    69
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    70
#define printf_fmt_separator ", "
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    71
4019
ad2b84071881 MQTT: do not convert to UPPERCASE json keys.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4018
diff changeset
    72
#define printf_fmt_SIMPLE(C_type, C_name, name, _A) #name " : " printf_fmt_##C_type
ad2b84071881 MQTT: do not convert to UPPERCASE json keys.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4018
diff changeset
    73
#define printf_fmt_OBJECT(C_type, C_name, name, _A) #name " : {{ " TYPE_##C_type(printf_fmt, _A) " }}"
4020
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
    74
#define printf_fmt_ARRAY(C_type, C_name, name, _A) #name " : [ " TYPE_##C_type(printf_fmt, _A) " ]"
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
    75
#define printf_fmt_ARRAY_SIMPLE(C_type, index, _A) printf_fmt_##C_type
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
    76
#define printf_fmt_ARRAY_OBJECT(C_type, index, _A) "{{ " TYPE_##C_type(printf_fmt, _A) " }}"
4018
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    77
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    78
#define scanf_fmt_BOOL   "%B"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    79
#define scanf_fmt_SINT   "%hhd"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    80
#define scanf_fmt_USINT  "%uhhd"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    81
#define scanf_fmt_INT    "%hd" 
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    82
#define scanf_fmt_UINT   "%uhd"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    83
#define scanf_fmt_DINT   "%d" 
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    84
#define scanf_fmt_UDINT  "%ud"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    85
#define scanf_fmt_LINT   "%ld"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    86
#define scanf_fmt_ULINT  "%uld"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    87
#define scanf_fmt_REAL   "%f"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    88
#define scanf_fmt_LREAL  "%Lf"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    89
#define scanf_fmt_STRING "%M"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    90
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    91
#define scanf_fmt_separator ", "
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    92
4019
ad2b84071881 MQTT: do not convert to UPPERCASE json keys.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4018
diff changeset
    93
#define scanf_fmt_SIMPLE(C_type, C_name, name, _A) #name " : " scanf_fmt_##C_type
ad2b84071881 MQTT: do not convert to UPPERCASE json keys.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4018
diff changeset
    94
#define scanf_fmt_OBJECT(C_type, C_name, name, _A) #name " : {{ " TYPE_##C_type(scanf_fmt, _A) " }}"
4020
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
    95
#define scanf_fmt_ARRAY(C_type, C_name, name, _A) #name " : [ " TYPE_##C_type(scanf_fmt, _A) " ]"
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
    96
#define scanf_fmt_ARRAY_SIMPLE(C_type, index, _A) scanf_fmt_##C_type
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
    97
#define scanf_fmt_ARRAY_OBJECT(C_type, index, _A) "{{ " TYPE_##C_type(scanf_fmt, _A) " }}"
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
    98
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
    99
#define   scanf_arg_BOOL(arg) arg
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   100
#define   scanf_arg_SINT(arg) arg
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   101
#define  scanf_arg_USINT(arg) arg
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   102
#define    scanf_arg_INT(arg) arg
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   103
#define   scanf_arg_UINT(arg) arg
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   104
#define   scanf_arg_DINT(arg) arg
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   105
#define  scanf_arg_UDINT(arg) arg
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   106
#define   scanf_arg_LINT(arg) arg
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   107
#define  scanf_arg_ULINT(arg) arg
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   108
#define   scanf_arg_REAL(arg) arg
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   109
#define  scanf_arg_LREAL(arg) arg
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   110
#define scanf_arg_STRING(arg) scan_string, arg
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   111
4016
9e4a3a3ea5c0 MQTT: fix invocation of json_scanf (use pointers, not values...)
Edouard Tisserant <edouard@beremiz.fr>
parents: 4015
diff changeset
   112
#define scanf_args_separator ,
9e4a3a3ea5c0 MQTT: fix invocation of json_scanf (use pointers, not values...)
Edouard Tisserant <edouard@beremiz.fr>
parents: 4015
diff changeset
   113
4020
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   114
#define scanf_args_SIMPLE(C_type, C_name, name, data_ptr) scanf_arg_##C_type(&data_ptr->C_name)
4019
ad2b84071881 MQTT: do not convert to UPPERCASE json keys.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4018
diff changeset
   115
#define scanf_args_OBJECT(C_type, C_name, name, data_ptr) TYPE_##C_type(scanf_args, (&data_ptr->C_name))
4020
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   116
#define scanf_args_ARRAY(C_type, C_name, name, data_ptr) TYPE_##C_type(scanf_args, data_ptr->C_name.table)
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   117
#define scanf_args_ARRAY_SIMPLE(C_type, index, data_ptr) scanf_arg_##C_type(&data_ptr[index])
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   118
#define scanf_args_ARRAY_OBJECT(C_type, index, data_ptr) TYPE_##C_type(scanf_args, (&data_ptr[index]))
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   119
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   120
#define   printf_arg_BOOL(arg) arg
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   121
#define   printf_arg_SINT(arg) arg
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   122
#define  printf_arg_USINT(arg) arg
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   123
#define    printf_arg_INT(arg) arg
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   124
#define   printf_arg_UINT(arg) arg
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   125
#define   printf_arg_DINT(arg) arg
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   126
#define  printf_arg_UDINT(arg) arg
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   127
#define   printf_arg_LINT(arg) arg
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   128
#define  printf_arg_ULINT(arg) arg
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   129
#define   printf_arg_REAL(arg) arg
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   130
#define  printf_arg_LREAL(arg) arg
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   131
#define printf_arg_STRING(arg) arg.len, arg.body 
4018
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
   132
4016
9e4a3a3ea5c0 MQTT: fix invocation of json_scanf (use pointers, not values...)
Edouard Tisserant <edouard@beremiz.fr>
parents: 4015
diff changeset
   133
#define printf_args_separator ,
9e4a3a3ea5c0 MQTT: fix invocation of json_scanf (use pointers, not values...)
Edouard Tisserant <edouard@beremiz.fr>
parents: 4015
diff changeset
   134
4020
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   135
#define printf_args_SIMPLE(C_type, C_name, name, data_ptr) printf_arg_##C_type(data_ptr->C_name)
4019
ad2b84071881 MQTT: do not convert to UPPERCASE json keys.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4018
diff changeset
   136
#define printf_args_OBJECT(C_type, C_name, name, data_ptr) TYPE_##C_type(printf_args, (&data_ptr->C_name))
4020
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   137
#define printf_args_ARRAY(C_type, C_name, name, data_ptr) TYPE_##C_type(printf_args, (&data_ptr->C_name.table))
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   138
#define printf_args_ARRAY_SIMPLE(C_type, index, data_ptr) printf_arg_##C_type(data_ptr[index])
205d5379b35e MQTT: add JSON fixed size 1D array support. WIP. Parsing array still broken.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4019
diff changeset
   139
#define printf_args_ARRAY_OBJECT(C_type, index, data_ptr) TYPE_##C_type(printf_args, (data_ptr[index]))
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   140
4018
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
   141
static void scan_string(const char *str, int len, void *user_data) {{
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
   142
	IEC_STRING *iecstr = (IEC_STRING*)user_data;
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
   143
	__strlen_t ieclen = len > STR_MAX_LEN ? STR_MAX_LEN : len;
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
   144
	memcpy(iecstr->body, str, ieclen);
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
   145
    iecstr->len = ieclen;
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
   146
}}
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
   147
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   148
#define DECL_JSON_INPUT(C_type, c_loc_name) \
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   149
int json_parse_##c_loc_name(char *json, const int len, void *void_ptr) {{ \
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   150
    C_type *struct_ptr = (C_type *)void_ptr; \
4018
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
   151
    return json_scanf(json, len, "{{" TYPE_##C_type(scanf_fmt,) "}}", TYPE_##C_type(scanf_args, struct_ptr)); \
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   152
}}
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   153
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   154
/* Pre-allocated json output buffer for json_printf */
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   155
#define json_out_size 1<<12 // 4K
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   156
static char json_out_buf[json_out_size] = {{0,}};
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   157
static int json_out_len = 0;
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   158
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   159
#define DECL_JSON_OUTPUT(C_type, c_loc_name) \
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   160
int json_gen_##c_loc_name(C_type *struct_ptr) {{ \
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   161
    struct json_out out = JSON_OUT_BUF(json_out_buf, json_out_size); \
4018
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
   162
    json_out_len = json_printf(&out, "{{" TYPE_##C_type(printf_fmt,) "}}", TYPE_##C_type(printf_args, struct_ptr)); \
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   163
    if(json_out_len > json_out_size){{ \
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   164
        json_out_len = 0; \
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   165
        return -EOVERFLOW; \
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   166
    }} \
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   167
    return 0; \
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   168
}}
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   169
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   170
{json_decl}
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   171
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
   172
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
   173
#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
   174
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
   175
#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
   176
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
   177
#endif
3989
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   178
4005
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   179
MQTTClient_SSLOptions ssl_opts = MQTTClient_SSLOptions_initializer;
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   180
3989
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   181
/* 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
   182
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
   183
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   184
/* 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
   185
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
   186
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
   187
/* 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
   188
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
   189
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
   190
/* 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
   191
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
   192
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
   193
/* 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
   194
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
   195
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   196
/* 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
   197
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
   198
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
   199
/* 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
   200
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
   201
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   202
#define INIT_TOPIC(topic, iec_type, c_loc_name) \
4015
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   203
{{#topic, sizeof(#topic)-1, &MQTT_##c_loc_name##_buf, &MQTT_##c_loc_name##_state, .is_json_type=0, .vartype = iec_type##_ENUM}},
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   204
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   205
#define INIT_JSON_TOPIC(topic, iec_type, c_loc_name) \
4015
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   206
{{#topic, sizeof(#topic)-1, &MQTT_##c_loc_name##_buf, &MQTT_##c_loc_name##_state, .is_json_type=1, .json_parse_func=json_parse_##c_loc_name}},
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   207
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   208
typedef int (*json_parse_func_t)(char *json, int len, void *void_ptr);
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   209
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   210
static struct {{
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   211
    const char *topic; //null terminated topic string
4015
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   212
    const unsigned int topic_len;
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   213
    void *mqtt_pdata; // pointer to data from/for MQTT stack
4015
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   214
    int *mqtt_pstate; // pointer to changed flag
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   215
    int is_json_type;
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   216
    union {{
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   217
       __IEC_types_enum vartype;
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   218
       json_parse_func_t json_parse_func;
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   219
    }};
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   220
}} topics [] = {{
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   221
{topics}
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   222
}};
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   223
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   224
void __cleanup_{locstr}(void)
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   225
{{
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   226
    int rc;
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   227
3993
78f9abfb32a0 MQTT: Stop publish thread properly on PLC stop
Edouard Tisserant <edouard@beremiz.fr>
parents: 3992
diff changeset
   228
    /* stop publish thread */
78f9abfb32a0 MQTT: Stop publish thread properly on PLC stop
Edouard Tisserant <edouard@beremiz.fr>
parents: 3992
diff changeset
   229
    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
   230
    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
   231
        /* 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
   232
        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
   233
        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
   234
    }}
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
   235
    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
   236
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
   237
#ifdef USE_MQTT_5
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   238
    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
   239
#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
   240
    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
   241
#endif
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   242
    {{
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   243
        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
   244
    }}
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   245
    MQTTClient_destroy(&client);
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   246
}}
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   247
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
   248
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
   249
{{
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
    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
   251
    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
   252
    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
   253
        /* 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
   254
        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
   255
        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
   256
        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
   257
    }}
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
}}
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
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
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   262
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
   263
{{
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   264
    int low = 0;
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   265
    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
   266
    int high = size - 1;
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   267
    int mid;
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   268
    int is_json_type;
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   269
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   270
    // bisect topic among subscribed topics
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   271
    while (low <= high) {{
4015
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   272
        int res, len, delta_len;
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   273
        mid = low + (high - low) / 2;
4015
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   274
        if(topicLen == 0) {{
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   275
            // zero topic len means null-terminated
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   276
            topicLen = strlen(topicName);
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   277
        }}
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   278
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   279
        len = topics[mid].topic_len;
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   280
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   281
        // keep track of length difference
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   282
        delta_len = len - topicLen;
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   283
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   284
        // len = min(len, topicLen)
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   285
        len = topicLen > len ? len : topicLen;
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   286
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   287
        // compare strings as far as possible
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   288
        res = strncmp(topics[mid].topic, topicName, len);
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   289
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   290
        // if partial comparison matches, longest string is the greatest
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   291
        if (res == 0)
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   292
            // update res to continue bisection
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   293
            res = delta_len;
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   294
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   295
        // 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
   296
        if (res == 0)
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   297
            goto found;
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   298
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   299
        // If key greater, ignore left half
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   300
        if (res < 0)
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   301
            low = mid + 1;
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   302
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   303
        // 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
   304
        else
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   305
            high = mid - 1;
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   306
    }}
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   307
    // If we reach here, then the element was not present
4015
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   308
    LogWarning("MQTT unknown topic: %s\n", topicName);
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   309
    goto exit;
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   310
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   311
found:
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   312
    
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   313
    is_json_type = topics[mid].is_json_type;
4015
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   314
    if(is_json_type || (__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
   315
        if (pthread_mutex_lock(&MQTT_retrieve_mutex) == 0){{
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   316
            if(is_json_type){{
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   317
                (topics[mid].json_parse_func)((char*)message->payload, message->payloadlen, topics[mid].mqtt_pdata);
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   318
            }} else {{
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   319
                memcpy(topics[mid].mqtt_pdata, (char*)message->payload, message->payloadlen);
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   320
            }}
4015
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   321
            *topics[mid].mqtt_pstate = CHANGED;
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
   322
            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
   323
        }}
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   324
    }} else {{
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   325
        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
   326
            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
   327
    }}
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   328
exit:
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   329
    MQTTClient_freeMessage(&message);
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   330
    MQTTClient_free(topicName);
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   331
    return 1;
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   332
}}
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   333
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   334
#define INIT_NoAuth()                                                                             \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   335
    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
   336
4005
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   337
#define INIT_x509(Verify, KeyStore, TrustStore)                                                   \
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   338
    LogInfo("MQTT Init x509 with %s,%s\n", KeyStore?KeyStore:"NULL", TrustStore?TrustStore:"NULL")\
4005
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   339
    ssl_opts.verify = Verify;                                                                     \
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   340
    ssl_opts.keyStore = KeyStore;                                                                 \
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   341
    ssl_opts.trustStore = TrustStore;                                                             \
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   342
    conn_opts.ssl = &ssl_opts;
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   343
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   344
#define INIT_PSK(Secret, ID)                                                                      \
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   345
    LogError("MQTT PSK NOT IMPLEMENTED\n")                                                        \
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   346
    /* LogInfo("MQTT Init PSK for ID %s\n", ID) */                                                \
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   347
    /* ssl_opts.ssl_psk_cb = TODO; */                                                             \
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   348
    /* ssl_opts.ssl_psk_context = TODO; */                                                        \
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   349
    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
   350
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   351
#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
   352
    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
   353
    conn_opts.username = User;                                                                    \
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   354
    conn_opts.password = Password;
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   355
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
   356
#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
   357
#define _SUBSCRIBE(Topic, QoS)                                                                    \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   358
        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
   359
        /* 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
   360
        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
   361
        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
   362
#else
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   363
#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
   364
        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
   365
#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
   366
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   367
#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
   368
    {{                                                                                            \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   369
        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
   370
        _SUBSCRIBE(Topic, QoS)                                                                    \
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   371
        if (rc != MQTTCLIENT_SUCCESS)                                                             \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   372
        {{                                                                                        \
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
   373
            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
   374
        }}                                                                                        \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   375
    }}
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   376
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   377
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   378
#ifdef USE_MQTT_5
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   379
#define _PUBLISH(Topic, QoS, cstring_size, cstring_ptr, Retained)                                 \
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   380
        MQTTResponse response = MQTTClient_publish5(client, #Topic, cstring_size,                  \
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   381
            cstring_ptr, 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
   382
        rc = response.reasonCode;                                                                 \
3987
cec48fc7ccd0 MQTT: WIP, now publishes initial values at init.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3986
diff changeset
   383
        MQTTResponse_free(response);
cec48fc7ccd0 MQTT: WIP, now publishes initial values at init.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3986
diff changeset
   384
#else
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   385
#define _PUBLISH(Topic, QoS, cstring_size, cstring_ptr, Retained)                                 \
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   386
        rc = MQTTClient_publish(client, #Topic, cstring_size,                                     \
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   387
            cstring_ptr, QoS, Retained, NULL);
3987
cec48fc7ccd0 MQTT: WIP, now publishes initial values at init.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3986
diff changeset
   388
#endif
cec48fc7ccd0 MQTT: WIP, now publishes initial values at init.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3986
diff changeset
   389
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   390
#define PUBLISH_SIMPLE(Topic, QoS, C_type, c_loc_name, Retained)                                  \
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   391
        _PUBLISH(Topic, QoS, sizeof(C_type), &MQTT_##c_loc_name##_buf, Retained)
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   392
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   393
#define PUBLISH_JSON(Topic, QoS, C_type, c_loc_name, Retained)                                    \
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   394
        int res = json_gen_##c_loc_name(&MQTT_##c_loc_name##_buf);                                \
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   395
        if(res == 0) {{                                                                           \
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   396
            _PUBLISH(Topic, QoS, json_out_len, json_out_buf, Retained)                            \
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   397
        }}
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   398
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   399
#define INIT_PUBLICATION(encoding, Topic, QoS, C_type, c_loc_name, Retained)                      \
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   400
    {{                                                                                            \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   401
        int rc;                                                                                   \
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   402
        PUBLISH_##encoding(Topic, QoS, C_type, c_loc_name, Retained)                              \
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   403
        if (rc != MQTTCLIENT_SUCCESS)                                                             \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   404
        {{                                                                                        \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   405
            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
   406
            /* 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
   407
        }}                                                                                        \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   408
    }}
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   409
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   410
#define PUBLISH_CHANGE(encoding, Topic, QoS, C_type, c_loc_name, Retained)                        \
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   411
    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
   412
    {{                                                                                            \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   413
        int rc;                                                                                   \
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   414
        PUBLISH_##encoding(Topic, QoS, C_type, c_loc_name, Retained)                              \
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   415
        if (rc != MQTTCLIENT_SUCCESS)                                                             \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   416
        {{                                                                                        \
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
   417
            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
   418
            /* 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
   419
        }} else {{                                                                                \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   420
            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
   421
        }}                                                                                        \
3989
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   422
    }}
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   423
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
   424
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
   425
{{
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
   426
    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
   427
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
   428
#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
   429
    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
   430
    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
   431
    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
   432
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
   433
    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
   434
    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
   435
    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
   436
#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
   437
    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
   438
#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
   439
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
   440
    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
   441
        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
   442
        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
   443
    }}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
   444
        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
   445
    }}
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
   446
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
   447
{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
   448
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
   449
    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
   450
}}
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
   451
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
   452
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
   453
    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
   454
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
   455
    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
   456
        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
   457
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
   458
        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
   459
        
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
   460
        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
   461
        {{
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
   462
            /* 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
   463
            /* 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
   464
            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
   465
            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
   466
            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
   467
                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
   468
            }} 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
   469
                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
   470
            }}
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
   471
        }} 
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
   472
        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
   473
        {{
3989
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   474
            /* 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
   475
{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
   476
            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
   477
        }}
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
   478
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
   479
        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
   480
3993
78f9abfb32a0 MQTT: Stop publish thread properly on PLC stop
Edouard Tisserant <edouard@beremiz.fr>
parents: 3992
diff changeset
   481
        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
   482
    }}
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   483
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   484
    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
   485
        /* 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
   486
        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
   487
    }}
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   488
}}
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   489
    
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   490
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
   491
{{
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   492
    char *uri = "{uri}";
3980
96acfee19caf MQTT WIP: intermediate state, implementing MQTT client C code generator.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3979
diff changeset
   493
    char *clientID = "{clientID}";
96acfee19caf MQTT WIP: intermediate state, implementing MQTT client C code generator.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3979
diff changeset
   494
    int rc;
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   495
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   496
    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
   497
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
   498
#ifdef USE_MQTT_5
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   499
    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
   500
    conn_opts.cleanstart = 1;
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   501
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   502
    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
   503
#else
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   504
    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
   505
#endif
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   506
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   507
    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
   508
    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
   509
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   510
    rc = MQTTClient_createWithOptions(
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   511
        &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
   512
    if (rc != MQTTCLIENT_SUCCESS)
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   513
    {{
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   514
        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
   515
        goto exit_error;
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   516
    }}
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   517
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
   518
    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
   519
    if (rc != MQTTCLIENT_SUCCESS)
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   520
    {{
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   521
        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
   522
        goto exit_error;
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   523
    }}
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   524
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
   525
{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
   526
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   527
    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
   528
	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
   529
		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
   530
	}} else {{
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   531
        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
   532
        // 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
   533
    }}
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
   534
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
   535
    /* 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
   536
    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
   537
    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
   538
    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
   539
        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
   540
        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
   541
    }}
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   542
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   543
    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
   544
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
   545
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
   546
    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
   547
    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
   548
}}
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   549
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   550
#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
   551
    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
   552
        /* TODO care about endianess */ \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   553
        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
   554
        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
   555
    }}
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   556
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   557
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
   558
{{
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
   559
    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
   560
{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
   561
        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
   562
    }}
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   563
}}
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   564
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   565
#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
   566
    /* TODO care about endianess */ \
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   567
    if(memcmp(&MQTT_##c_loc_name##_buf, &PLC_##c_loc_name##_buf, sizeof(C_type))){{ \
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   568
        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
   569
        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
   570
        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
   571
    }}
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   572
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   573
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
   574
{{
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
   575
    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
   576
        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
   577
        /* 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
   578
{publish}
3989
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   579
        /* 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
   580
        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
   581
            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
   582
        }}
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
   583
        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
   584
    }} else {{
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   585
        /* 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
   586
    }}
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   587
}}
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   588