mqtt/mqtt_template.c
author Edouard Tisserant <edouard.tisserant@gmail.com>
Wed, 13 Nov 2024 23:10:36 +0100
changeset 4037 4127c64df073
parent 4023 b344393859df
permissions -rw-r--r--
python runtime: update ServicePublisher to current Zeroconf API
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
4023
b344393859df MQTT: Add status global variable MQTT_STATUS_n, one per MQTT client
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
    47
extern INT CONFIG__MQTT_STATUS_{locstr};
b344393859df MQTT: Add status global variable MQTT_STATUS_n, one per MQTT client
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
    48
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
    49
#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
    50
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
    51
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
    52
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
    53
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
    54
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
    55
{decl}
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
    56
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
    57
/* 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
    58
4018
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    59
#define printf_fmt_BOOL   "%B"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    60
#define printf_fmt_SINT   "%hhd"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    61
#define printf_fmt_USINT  "%uhhd"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    62
#define printf_fmt_INT    "%hd" 
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    63
#define printf_fmt_UINT   "%uhd"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    64
#define printf_fmt_DINT   "%d" 
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    65
#define printf_fmt_UDINT  "%ud"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    66
#define printf_fmt_LINT   "%ld"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    67
#define printf_fmt_ULINT  "%uld"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    68
#define printf_fmt_REAL   "%f"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    69
#define printf_fmt_LREAL  "%Lf"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    70
#define printf_fmt_STRING "%.*Q"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    71
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    72
#define printf_fmt_separator ", "
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    73
4019
ad2b84071881 MQTT: do not convert to UPPERCASE json keys.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4018
diff changeset
    74
#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
    75
#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
    76
#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
    77
#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
    78
#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
    79
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    80
#define scanf_fmt_BOOL   "%B"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    81
#define scanf_fmt_SINT   "%hhd"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    82
#define scanf_fmt_USINT  "%uhhd"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    83
#define scanf_fmt_INT    "%hd" 
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    84
#define scanf_fmt_UINT   "%uhd"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    85
#define scanf_fmt_DINT   "%d" 
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    86
#define scanf_fmt_UDINT  "%ud"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    87
#define scanf_fmt_LINT   "%ld"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    88
#define scanf_fmt_ULINT  "%uld"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    89
#define scanf_fmt_REAL   "%f"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    90
#define scanf_fmt_LREAL  "%Lf"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    91
#define scanf_fmt_STRING "%M"
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    92
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    93
#define scanf_fmt_separator ", "
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
    94
4019
ad2b84071881 MQTT: do not convert to UPPERCASE json keys.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4018
diff changeset
    95
#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
    96
#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
    97
#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
    98
#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
    99
#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
   100
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_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
   102
#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
   103
#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
   104
#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
   105
#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
   106
#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
   107
#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
   108
#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
   109
#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
   110
#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
   111
#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
   112
#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
   113
4016
9e4a3a3ea5c0 MQTT: fix invocation of json_scanf (use pointers, not values...)
Edouard Tisserant <edouard@beremiz.fr>
parents: 4015
diff changeset
   114
#define scanf_args_separator ,
9e4a3a3ea5c0 MQTT: fix invocation of json_scanf (use pointers, not values...)
Edouard Tisserant <edouard@beremiz.fr>
parents: 4015
diff changeset
   115
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_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
   117
#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
   118
#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
   119
#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
   120
#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
   121
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_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
   123
#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
   124
#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
   125
#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
   126
#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
   127
#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
   128
#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
   129
#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
   130
#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
   131
#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
   132
#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
   133
#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
   134
4016
9e4a3a3ea5c0 MQTT: fix invocation of json_scanf (use pointers, not values...)
Edouard Tisserant <edouard@beremiz.fr>
parents: 4015
diff changeset
   135
#define printf_args_separator ,
9e4a3a3ea5c0 MQTT: fix invocation of json_scanf (use pointers, not values...)
Edouard Tisserant <edouard@beremiz.fr>
parents: 4015
diff changeset
   136
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_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
   138
#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
   139
#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
   140
#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
   141
#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
   142
4018
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
   143
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
   144
	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
   145
	__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
   146
	memcpy(iecstr->body, str, ieclen);
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
   147
    iecstr->len = ieclen;
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
   148
}}
fa60dde482ca MQTT: added support for strings in JSON payload only.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4017
diff changeset
   149
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   150
#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
   151
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
   152
    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
   153
    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
   154
}}
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   155
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   156
/* 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
   157
#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
   158
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
   159
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
   160
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   161
#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
   162
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
   163
    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
   164
    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
   165
    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
   166
        json_out_len = 0; \
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   167
        return -EOVERFLOW; \
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
    return 0; \
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   170
}}
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   171
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   172
{json_decl}
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   173
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
   174
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
   175
#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
   176
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
   177
#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
   178
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
   179
#endif
3989
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   180
4005
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   181
MQTTClient_SSLOptions ssl_opts = MQTTClient_SSLOptions_initializer;
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   182
3989
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   183
/* 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
   184
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
   185
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   186
/* 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
   187
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
   188
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
   189
/* 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
   190
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
   191
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
   192
/* 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
   193
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
   194
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
   195
/* 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
   196
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
   197
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   198
/* 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
   199
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
   200
0145c60b9560 MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3997
diff changeset
   201
/* 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
   202
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
   203
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   204
#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
   205
{{#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
   206
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   207
#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
   208
{{#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
   209
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   210
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
   211
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   212
static struct {{
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   213
    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
   214
    const unsigned int topic_len;
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   215
    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
   216
    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
   217
    int is_json_type;
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   218
    union {{
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   219
       __IEC_types_enum vartype;
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   220
       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
   221
    }};
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   222
}} topics [] = {{
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   223
{topics}
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   224
}};
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
void __cleanup_{locstr}(void)
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   227
{{
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   228
    int rc;
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   229
3993
78f9abfb32a0 MQTT: Stop publish thread properly on PLC stop
Edouard Tisserant <edouard@beremiz.fr>
parents: 3992
diff changeset
   230
    /* stop publish thread */
78f9abfb32a0 MQTT: Stop publish thread properly on PLC stop
Edouard Tisserant <edouard@beremiz.fr>
parents: 3992
diff changeset
   231
    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
   232
    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
   233
        /* 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
   234
        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
   235
        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
   236
    }}
0145c60b9560 MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3997
diff changeset
   237
    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
   238
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
#ifdef USE_MQTT_5
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   240
    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
   241
#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
   242
    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
   243
#endif
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   244
    {{
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   245
        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
   246
    }}
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   247
    MQTTClient_destroy(&client);
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   248
}}
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   249
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
   250
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
   251
{{
0145c60b9560 MQTT: WIP, PLC 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
    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
   253
    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
   254
    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
   255
        /* 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
   256
        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
   257
        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
   258
        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
   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
0145c60b9560 MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3997
diff changeset
   262
0145c60b9560 MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3997
diff changeset
   263
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   264
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
   265
{{
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   266
    int low = 0;
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   267
    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
   268
    int high = size - 1;
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   269
    int mid;
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   270
    int is_json_type;
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   271
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   272
    // bisect topic among subscribed topics
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   273
    while (low <= high) {{
4015
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   274
        int res, len, delta_len;
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   275
        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
   276
        if(topicLen == 0) {{
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   277
            // 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
   278
            topicLen = strlen(topicName);
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   279
        }}
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
        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
   282
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   283
        // 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
   284
        delta_len = len - topicLen;
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   285
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   286
        // len = min(len, topicLen)
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   287
        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
   288
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   289
        // 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
   290
        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
   291
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   292
        // 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
   293
        if (res == 0)
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   294
            // 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
   295
            res = delta_len;
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   296
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   297
        // 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
   298
        if (res == 0)
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   299
            goto found;
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   300
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   301
        // If key greater, ignore left half
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   302
        if (res < 0)
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   303
            low = mid + 1;
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   304
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   305
        // 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
   306
        else
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   307
            high = mid - 1;
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   308
    }}
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   309
    // 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
   310
    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
   311
    goto exit;
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   312
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   313
found:
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   314
    
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   315
    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
   316
    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
   317
        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
   318
            if(is_json_type){{
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   319
                (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
   320
            }} else {{
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   321
                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
   322
            }}
4015
ab96d56f9ae9 MQTT: fix bisection failing when topics start with same substring.
Edouard Tisserant <edouard@beremiz.fr>
parents: 4012
diff changeset
   323
            *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
   324
            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
   325
        }}
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   326
    }} else {{
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   327
        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
   328
            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
   329
    }}
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   330
exit:
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   331
    MQTTClient_freeMessage(&message);
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   332
    MQTTClient_free(topicName);
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   333
    return 1;
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   334
}}
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   335
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   336
#define INIT_NoAuth()                                                                             \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   337
    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
   338
4005
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   339
#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
   340
    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
   341
    ssl_opts.verify = Verify;                                                                     \
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   342
    ssl_opts.keyStore = KeyStore;                                                                 \
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   343
    ssl_opts.trustStore = TrustStore;                                                             \
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   344
    conn_opts.ssl = &ssl_opts;
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   345
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   346
#define INIT_PSK(Secret, ID)                                                                      \
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   347
    LogError("MQTT PSK NOT IMPLEMENTED\n")                                                        \
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   348
    /* LogInfo("MQTT Init PSK for ID %s\n", ID) */                                                \
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   349
    /* ssl_opts.ssl_psk_cb = TODO; */                                                             \
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   350
    /* ssl_opts.ssl_psk_context = TODO; */                                                        \
482452574fb4 MQTT: add SSL support
Edouard Tisserant <edouard@beremiz.fr>
parents: 4002
diff changeset
   351
    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
   352
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   353
#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
   354
    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
   355
    conn_opts.username = User;                                                                    \
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   356
    conn_opts.password = Password;
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   357
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
   358
#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
   359
#define _SUBSCRIBE(Topic, QoS)                                                                    \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   360
        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
   361
        /* 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
   362
        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
   363
        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
   364
#else
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   365
#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
   366
        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
   367
#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
   368
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   369
#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
   370
    {{                                                                                            \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   371
        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
   372
        _SUBSCRIBE(Topic, QoS)                                                                    \
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   373
        if (rc != MQTTCLIENT_SUCCESS)                                                             \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   374
        {{                                                                                        \
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
   375
            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
   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
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   379
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   380
#ifdef USE_MQTT_5
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   381
#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
   382
        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
   383
            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
   384
        rc = response.reasonCode;                                                                 \
3987
cec48fc7ccd0 MQTT: WIP, now publishes initial values at init.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3986
diff changeset
   385
        MQTTResponse_free(response);
cec48fc7ccd0 MQTT: WIP, now publishes initial values at init.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3986
diff changeset
   386
#else
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   387
#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
   388
        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
   389
            cstring_ptr, QoS, Retained, NULL);
3987
cec48fc7ccd0 MQTT: WIP, now publishes initial values at init.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3986
diff changeset
   390
#endif
cec48fc7ccd0 MQTT: WIP, now publishes initial values at init.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3986
diff changeset
   391
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   392
#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
   393
        _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
   394
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   395
#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
   396
        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
   397
        if(res == 0) {{                                                                           \
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   398
            _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
   399
        }}
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   400
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   401
#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
   402
    {{                                                                                            \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   403
        int rc;                                                                                   \
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   404
        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
   405
        if (rc != MQTTCLIENT_SUCCESS)                                                             \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   406
        {{                                                                                        \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   407
            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
   408
            /* 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
   409
        }}                                                                                        \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   410
    }}
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   411
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   412
#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
   413
    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
   414
    {{                                                                                            \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   415
        int rc;                                                                                   \
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   416
        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
   417
        if (rc != MQTTCLIENT_SUCCESS)                                                             \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   418
        {{                                                                                        \
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
   419
            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
   420
            /* 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
   421
        }} else {{                                                                                \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   422
            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
   423
        }}                                                                                        \
3989
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   424
    }}
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   425
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
   426
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
   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
    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
   429
0145c60b9560 MQTT: WIP, PLC 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
#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
   431
    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
   432
    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
   433
    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
   434
0145c60b9560 MQTT: WIP, PLC 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
    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
   436
    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
   437
    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
   438
#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
   439
    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
   440
#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
   441
0145c60b9560 MQTT: WIP, PLC 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
    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
   443
        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
   444
        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
   445
    }}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
   446
        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
   447
    }}
0145c60b9560 MQTT: WIP, PLC 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
{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
   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
    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
   452
}}
0145c60b9560 MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3997
diff changeset
   453
0145c60b9560 MQTT: WIP, PLC 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
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
   455
    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
   456
0145c60b9560 MQTT: WIP, PLC 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
    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
   458
        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
   459
0145c60b9560 MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3997
diff changeset
   460
        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
   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
        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
   463
        {{
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
            /* 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
   465
            /* 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
   466
            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
   467
            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
   468
            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
   469
                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
   470
            }} 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
   471
                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
   472
            }}
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
   473
        }} 
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
   474
        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
   475
        {{
3989
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   476
            /* 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
   477
{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
   478
            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
   479
        }}
0145c60b9560 MQTT: WIP, PLC 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
0145c60b9560 MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3997
diff changeset
   481
        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
   482
3993
78f9abfb32a0 MQTT: Stop publish thread properly on PLC stop
Edouard Tisserant <edouard@beremiz.fr>
parents: 3992
diff changeset
   483
        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
   484
    }}
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   485
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   486
    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
   487
        /* 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
   488
        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
   489
    }}
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   490
}}
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   491
    
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   492
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
   493
{{
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   494
    char *uri = "{uri}";
3980
96acfee19caf MQTT WIP: intermediate state, implementing MQTT client C code generator.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3979
diff changeset
   495
    char *clientID = "{clientID}";
96acfee19caf MQTT WIP: intermediate state, implementing MQTT client C code generator.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3979
diff changeset
   496
    int rc;
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   497
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   498
    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
   499
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
   500
#ifdef USE_MQTT_5
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   501
    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
   502
    conn_opts.cleanstart = 1;
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   503
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   504
    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
   505
#else
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   506
    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
   507
#endif
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   508
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   509
    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
   510
    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
   511
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   512
    rc = MQTTClient_createWithOptions(
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   513
        &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
   514
    if (rc != MQTTCLIENT_SUCCESS)
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   515
    {{
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   516
        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
   517
        goto exit_error;
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   518
    }}
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   519
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
   520
    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
   521
    if (rc != MQTTCLIENT_SUCCESS)
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   522
    {{
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   523
        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
   524
        goto exit_error;
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   525
    }}
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   526
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
   527
{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
   528
3990
24656e0e8732 MQTT: WIP. Added handling of received messages (subscriptions)
Edouard Tisserant <edouard@beremiz.fr>
parents: 3989
diff changeset
   529
    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
   530
	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
   531
		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
   532
	}} else {{
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   533
        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
   534
        // 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
   535
    }}
0145c60b9560 MQTT: WIP, PLC starts even if MQTT client can't connect broker, and publish+subscribe again when reconnecting.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3997
diff changeset
   536
0145c60b9560 MQTT: WIP, PLC 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
    /* 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
   538
    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
   539
    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
   540
    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
   541
        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
   542
        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
   543
    }}
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   544
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   545
    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
   546
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
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
   548
    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
   549
    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
   550
}}
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   551
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   552
#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
   553
    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
   554
        /* TODO care about endianess */ \
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   555
        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
   556
        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
   557
    }}
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   558
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   559
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
   560
{{
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
    if (pthread_mutex_trylock(&MQTT_retrieve_mutex) == 0){{
4023
b344393859df MQTT: Add status global variable MQTT_STATUS_n, one per MQTT client
Edouard Tisserant <edouard@beremiz.fr>
parents: 4021
diff changeset
   562
        CONFIG__MQTT_STATUS_{locstr} = MQTT_is_disconnected ? 0 : 1;
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   563
{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
   564
        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
   565
    }}
3984
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   566
}}
883a85b9ebcc MQTT: still WIP, generated C code builds and link.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3981
diff changeset
   567
3995
84a668564748 MQTT: take C part away from python code for readability.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3994
diff changeset
   568
#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
   569
    /* TODO care about endianess */ \
4012
6337c9c2c379 MQTT: add JSON payload corresponding to IEC Structured types
Edouard Tisserant <edouard@beremiz.fr>
parents: 4005
diff changeset
   570
    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
   571
        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
   572
        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
   573
        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
   574
    }}
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   575
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   576
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
   577
{{
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
   578
    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
   579
        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
   580
        /* 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
   581
{publish}
3989
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   582
        /* 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
   583
        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
   584
            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
   585
        }}
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
   586
        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
   587
    }} else {{
987c69b1582f MQTT: WIP, added publish thread waking-up when published variables change.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3988
diff changeset
   588
        /* 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
   589
    }}
3979
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   590
}}
76295adcf940 WIP: Add skeleton for MQTT extension based on part of OPC-UA extension.
Edouard Tisserant <edouard@beremiz.fr>
parents:
diff changeset
   591