svghmi/svghmi.c
author Edouard Tisserant
Fri, 06 May 2022 11:02:03 +0200
changeset 3472 2fb9849c6721
parent 3400 c2b46d0965ca
child 3613 7af7a23e4adb
permissions -rw-r--r--
SVGHMI: fix syntax error in sprintf.js and force makefile to regen xslt files when sprintf.js changes
2765
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
     1
#include <pthread.h>
2776
246ae685ab65 SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2775
diff changeset
     2
#include <errno.h>
2764
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
     3
#include "iec_types_all.h"
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
     4
#include "POUS.h"
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
     5
#include "config.h"
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
     6
#include "beremiz.h"
2750
2694170cd88e intermediate commit, work in progress
Edouard Tisserant
parents:
diff changeset
     7
2768
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
     8
#define DEFAULT_REFRESH_PERIOD_MS 100
2765
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
     9
#define HMI_BUFFER_SIZE %(buffer_size)d
2776
246ae685ab65 SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2775
diff changeset
    10
#define HMI_ITEM_COUNT %(item_count)d
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    11
#define HMI_HASH_SIZE 8
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 2863
diff changeset
    12
#define MAX_CONNECTIONS %(max_connections)d
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    13
#define MAX_CON_INDEX MAX_CONNECTIONS - 1
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 2863
diff changeset
    14
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    15
static uint8_t hmi_hash[HMI_HASH_SIZE] = {%(hmi_hash_ints)s};
2765
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    16
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    17
/* PLC reads from that buffer */
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    18
static char rbuf[HMI_BUFFER_SIZE];
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    19
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    20
/* PLC writes to that buffer */
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    21
static char wbuf[HMI_BUFFER_SIZE];
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    22
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    23
/* worst biggest send buffer. FIXME : use dynamic alloc ? */
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    24
static char sbuf[HMI_HASH_SIZE +  HMI_BUFFER_SIZE + (HMI_ITEM_COUNT * sizeof(uint32_t))];
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    25
static unsigned int sbufidx;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    26
2768
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    27
%(extern_variables_declarations)s
2765
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    28
2771
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents: 2768
diff changeset
    29
#define ticktime_ns %(PLC_ticktime)d
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    30
static uint16_t ticktime_ms = (ticktime_ns>1000000)?
2768
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    31
                     ticktime_ns/1000000:
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    32
                     1;
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    33
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    34
typedef enum {
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    35
    buf_free = 0,
2805
e521e0d133d5 SVGHMI: fixed HMI->PLC dataflow : not updates as expected, and not initialized properly after subscribe.
Edouard Tisserant
parents: 2802
diff changeset
    36
    buf_new,
2768
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    37
    buf_set,
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    38
    buf_tosend
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    39
} buf_state_t;
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    40
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    41
static int global_write_dirty = 0;
3374
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
    42
static long hmitree_rlock = 0;
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
    43
static long hmitree_wlock = 0;
2764
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
    44
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    45
typedef struct hmi_tree_item_s hmi_tree_item_t;
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    46
struct hmi_tree_item_s{
2764
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
    47
    void *ptr;
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
    48
    __IEC_types_enum type;
2765
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    49
    uint32_t buf_index;
2768
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    50
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    51
    /* retrieve/read/recv */
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    52
    buf_state_t rstate;
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    53
2768
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    54
    /* publish/write/send */
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 2863
diff changeset
    55
    buf_state_t wstate[MAX_CONNECTIONS];
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    56
2768
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    57
    /* zero means not subscribed */
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 2863
diff changeset
    58
    uint16_t refresh_period_ms[MAX_CONNECTIONS];
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 2863
diff changeset
    59
    uint16_t age_ms[MAX_CONNECTIONS];
2768
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    60
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    61
    /* dual linked list for subscriptions */
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    62
    hmi_tree_item_t *subscriptions_next;
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    63
    hmi_tree_item_t *subscriptions_prev;
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    64
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    65
    /* single linked list for changes from HMI */
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    66
    hmi_tree_item_t *incoming_prev;
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    67
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    68
};
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    69
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    70
#define HMITREE_ITEM_INITIALIZER(cpath,type,buf_index) {        \
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    71
    &(cpath),                             /*ptr*/               \
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    72
    type,                                 /*type*/              \
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    73
    buf_index,                            /*buf_index*/         \
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    74
    buf_free,                             /*rstate*/            \
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    75
    {[0 ... MAX_CON_INDEX] = buf_free},   /*wstate*/            \
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    76
    {[0 ... MAX_CON_INDEX] = 0},          /*refresh_period_ms*/ \
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    77
    {[0 ... MAX_CON_INDEX] = 0},          /*age_ms*/            \
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    78
    NULL,                                 /*subscriptions_next*/\
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    79
    NULL,                                 /*subscriptions_prev*/\
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    80
    NULL}                                 /*incoming_next*/
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    81
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    82
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    83
/* entry for dual linked list for HMI subscriptions */
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    84
/* points to the end of the list */
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    85
static hmi_tree_item_t  *subscriptions_tail = NULL;
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    86
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    87
/* entry for single linked list for changes from HMI */
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    88
/* points to the end of the list */
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    89
static hmi_tree_item_t *incoming_tail = NULL;
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    90
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    91
static hmi_tree_item_t hmi_tree_items[] = {
2764
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
    92
%(variable_decl_array)s
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
    93
};
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
    94
2767
302347f48193 svghmi.c : deduplicated variable access code borrowed from plc_debug.c. Added targets/var_access.c.
Edouard Tisserant
parents: 2766
diff changeset
    95
#define __Unpack_desc_type hmi_tree_item_t
2766
3f3b1b8ccba4 SVGHMI: Added iterators in svghmi.c copy-pasted form plc_debug.c
Edouard Tisserant
parents: 2765
diff changeset
    96
2767
302347f48193 svghmi.c : deduplicated variable access code borrowed from plc_debug.c. Added targets/var_access.c.
Edouard Tisserant
parents: 2766
diff changeset
    97
%(var_access_code)s
2765
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    98
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
    99
static int write_iterator(hmi_tree_item_t *dsc)
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   100
{
3400
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   101
    uint32_t session_index = 0;
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   102
    int value_changed = 0;
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   103
    void *dest_p = NULL;
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   104
    void *value_p = NULL;
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   105
    size_t sz = 0;
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   106
    while(session_index < MAX_CONNECTIONS) {
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   107
        if(dsc->wstate[session_index] == buf_set){
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   108
            /* if being subscribed */
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   109
            if(dsc->refresh_period_ms[session_index]){
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   110
                if(dsc->age_ms[session_index] + ticktime_ms < dsc->refresh_period_ms[session_index]){
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   111
                    dsc->age_ms[session_index] += ticktime_ms;
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   112
                }else{
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   113
                    dsc->wstate[session_index] = buf_tosend;
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   114
                    global_write_dirty = 1;
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   115
                }
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   116
            }
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   117
        }
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   118
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   119
        /* variable is sample only if just subscribed
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   120
           or already subscribed and having value change */
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   121
        int do_sample = 0;
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   122
        int just_subscribed = dsc->wstate[session_index] == buf_new;
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   123
        if(!just_subscribed){
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   124
            int already_subscribed = dsc->refresh_period_ms[session_index] > 0;
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   125
            if(already_subscribed){
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   126
                if(!value_changed){
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   127
                    if(!value_p){
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   128
                        UnpackVar(dsc, &value_p, NULL, &sz);
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   129
                        if(__Is_a_string(dsc)){
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   130
                            sz = ((STRING*)value_p)->len + 1;
3374
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   131
                        }
3400
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   132
                        dest_p = &wbuf[dsc->buf_index];
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   133
                    }
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   134
                    value_changed = memcmp(dest_p, value_p, sz) != 0;
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   135
                    do_sample = value_changed;
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   136
                }else{
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   137
                    do_sample = 1;
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   138
                }
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   139
            }
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   140
        } else {
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   141
            do_sample = 1;
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   142
        }
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   143
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   144
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   145
        if(do_sample){
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   146
            if(dsc->wstate[session_index] != buf_set && dsc->wstate[session_index] != buf_tosend) {
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   147
                if(dsc->wstate[session_index] == buf_new \
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   148
                   || ticktime_ms > dsc->refresh_period_ms[session_index]){
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   149
                    dsc->wstate[session_index] = buf_tosend;
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   150
                    global_write_dirty = 1;
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   151
                } else {
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   152
                    dsc->wstate[session_index] = buf_set;
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   153
                }
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   154
                dsc->age_ms[session_index] = 0;
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   155
            }
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   156
        }
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   157
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   158
        session_index++;
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   159
    }
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   160
    /* copy value if changed (and subscribed) */
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   161
    if(value_changed)
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   162
        memcpy(dest_p, value_p, sz);
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   163
    return 0;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   164
}
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   165
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   166
static int send_iterator(uint32_t index, hmi_tree_item_t *dsc, uint32_t session_index)
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   167
{
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   168
    if(dsc->wstate[session_index] == buf_tosend)
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   169
    {
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   170
        uint32_t sz = __get_type_enum_size(dsc->type);
2799
f5da343b9b63 SVGHMI: Many fixes. Subscriptions to HMItree seems to be working, and dispatch function is called in JS with good data. Bidirectional communication now really working.
Edouard Tisserant
parents: 2798
diff changeset
   171
        if(sbufidx + sizeof(uint32_t) + sz <=  sizeof(sbuf))
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   172
        {
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   173
            void *src_p = &wbuf[dsc->buf_index];
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   174
            void *dst_p = &sbuf[sbufidx];
2826
1e5abecc3cde SVGHMI : support for HMI_STRING and HMI_BOOL
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2822
diff changeset
   175
            if(__Is_a_string(dsc)){
1e5abecc3cde SVGHMI : support for HMI_STRING and HMI_BOOL
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2822
diff changeset
   176
                sz = ((STRING*)src_p)->len + 1;
1e5abecc3cde SVGHMI : support for HMI_STRING and HMI_BOOL
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2822
diff changeset
   177
            }
2799
f5da343b9b63 SVGHMI: Many fixes. Subscriptions to HMItree seems to be working, and dispatch function is called in JS with good data. Bidirectional communication now really working.
Edouard Tisserant
parents: 2798
diff changeset
   178
            /* TODO : force into little endian */
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   179
            memcpy(dst_p, &index, sizeof(uint32_t));
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   180
            memcpy(dst_p + sizeof(uint32_t), src_p, sz);
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   181
            dsc->wstate[session_index] = buf_free;
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   182
            sbufidx += sizeof(uint32_t) /* index */ + sz;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   183
        }
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   184
        else
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   185
        {
2799
f5da343b9b63 SVGHMI: Many fixes. Subscriptions to HMItree seems to be working, and dispatch function is called in JS with good data. Bidirectional communication now really working.
Edouard Tisserant
parents: 2798
diff changeset
   186
            printf("BUG!!! %%d + %%ld + %%d >  %%ld \n", sbufidx, sizeof(uint32_t), sz,  sizeof(sbuf));
2862
a108677bd3d0 SVGHMI: whitespaces
Edouard Tisserant
parents: 2829
diff changeset
   187
            return EOVERFLOW;
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   188
        }
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   189
    }
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   190
2862
a108677bd3d0 SVGHMI: whitespaces
Edouard Tisserant
parents: 2829
diff changeset
   191
    return 0;
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   192
}
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   193
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   194
static int read_iterator(hmi_tree_item_t *dsc)
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   195
{
3374
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   196
    if(dsc->rstate == buf_set)
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   197
    {
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   198
        void *src_p = &rbuf[dsc->buf_index];
3397
75920c99ffc9 SVGHMI: Adapt svghmi.c to changes in UnpackVar
Edouard Tisserant
parents: 3374
diff changeset
   199
        void *value_p = NULL;
75920c99ffc9 SVGHMI: Adapt svghmi.c to changes in UnpackVar
Edouard Tisserant
parents: 3374
diff changeset
   200
        size_t sz = 0;
75920c99ffc9 SVGHMI: Adapt svghmi.c to changes in UnpackVar
Edouard Tisserant
parents: 3374
diff changeset
   201
        UnpackVar(dsc, &value_p, NULL, &sz);
75920c99ffc9 SVGHMI: Adapt svghmi.c to changes in UnpackVar
Edouard Tisserant
parents: 3374
diff changeset
   202
        memcpy(value_p, src_p, sz);
3374
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   203
        dsc->rstate = buf_free;
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   204
    }
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   205
    return 0;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   206
}
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   207
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 2863
diff changeset
   208
void update_refresh_period(hmi_tree_item_t *dsc, uint32_t session_index, uint16_t refresh_period_ms)
2768
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   209
{
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   210
    uint32_t other_session_index = 0;
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   211
    int previously_subscribed = 0;
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   212
    int session_only_subscriber = 0;
3400
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   213
    int session_already_subscriber = 0;
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   214
    int needs_subscription_for_session = (refresh_period_ms != 0);
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   215
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   216
    while(other_session_index < session_index) {
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   217
        previously_subscribed |= (dsc->refresh_period_ms[other_session_index++] != 0);
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   218
    }
3400
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   219
    session_already_subscriber = (dsc->refresh_period_ms[other_session_index++] != 0);
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   220
    while(other_session_index < MAX_CONNECTIONS) {
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   221
        previously_subscribed |= (dsc->refresh_period_ms[other_session_index++] != 0);
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   222
    }
3400
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   223
    session_only_subscriber = session_already_subscriber && !previously_subscribed;
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   224
    previously_subscribed |= session_already_subscriber;
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   225
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   226
    if(needs_subscription_for_session) {
3400
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   227
        if(!session_already_subscriber)
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 2863
diff changeset
   228
        {
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 2863
diff changeset
   229
            dsc->wstate[session_index] = buf_new;
2863
e120d6985a2f SVGHMI: fix wrong updates of HMI variables on each change, especially when not subscribed.
Edouard Tisserant
parents: 2862
diff changeset
   230
        }
3400
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   231
        /* item is appended to list only when no session was previously subscribed */
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   232
        if(!previously_subscribed){
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   233
            /* append subsciption to list */
3400
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   234
            if(subscriptions_tail != NULL){ 
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   235
                /* if list wasn't empty, link with previous tail*/
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   236
                subscriptions_tail->subscriptions_next = dsc;
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   237
            }
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   238
            dsc->subscriptions_prev = subscriptions_tail;
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   239
            subscriptions_tail = dsc;
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   240
            dsc->subscriptions_next = NULL;
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   241
        }
2805
e521e0d133d5 SVGHMI: fixed HMI->PLC dataflow : not updates as expected, and not initialized properly after subscribe.
Edouard Tisserant
parents: 2802
diff changeset
   242
    } else {
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 2863
diff changeset
   243
        dsc->wstate[session_index] = buf_free;
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   244
        /* item is removed from list only when session was the only one remaining */
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   245
        if (session_only_subscriber) {
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   246
            if(dsc->subscriptions_next == NULL){ /* remove tail  */
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   247
                /* re-link tail to previous */
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   248
                subscriptions_tail = dsc->subscriptions_prev;
3400
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   249
                if(subscriptions_tail != NULL){
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   250
                    subscriptions_tail->subscriptions_next = NULL;
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   251
                }
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   252
            } else if(dsc->subscriptions_prev == NULL){ /* remove head  */
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   253
                dsc->subscriptions_next->subscriptions_prev = NULL;
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   254
            } else { /* remove entry in between other entries */
3400
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   255
                /* re-link previous and next node */
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   256
                dsc->subscriptions_next->subscriptions_prev = dsc->subscriptions_prev;
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   257
                dsc->subscriptions_prev->subscriptions_next = dsc->subscriptions_next;
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   258
            }
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   259
            /* unnecessary
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   260
            dsc->subscriptions_next = NULL;
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   261
            dsc->subscriptions_prev = NULL;
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   262
            */
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   263
        }
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 2863
diff changeset
   264
    }
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 2863
diff changeset
   265
    dsc->refresh_period_ms[session_index] = refresh_period_ms;
2766
3f3b1b8ccba4 SVGHMI: Added iterators in svghmi.c copy-pasted form plc_debug.c
Edouard Tisserant
parents: 2765
diff changeset
   266
}
3f3b1b8ccba4 SVGHMI: Added iterators in svghmi.c copy-pasted form plc_debug.c
Edouard Tisserant
parents: 2765
diff changeset
   267
3294
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3281
diff changeset
   268
static void *svghmi_handle;
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3281
diff changeset
   269
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3281
diff changeset
   270
void SVGHMI_SuspendFromPythonThread(void)
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3281
diff changeset
   271
{
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3281
diff changeset
   272
    wait_RT_to_nRT_signal(svghmi_handle);
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3281
diff changeset
   273
}
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3281
diff changeset
   274
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3281
diff changeset
   275
void SVGHMI_WakeupFromRTThread(void)
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3281
diff changeset
   276
{
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3281
diff changeset
   277
    unblock_RT_to_nRT_signal(svghmi_handle);
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3281
diff changeset
   278
}
2775
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   279
3281
1fc4274de64e SVGHMI: Fixed halting problem when there is no session opened.
Edouard Tisserant
parents: 3273
diff changeset
   280
int svghmi_continue_collect;
2776
246ae685ab65 SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2775
diff changeset
   281
2764
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
   282
int __init_svghmi()
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
   283
{
3295
0375d801fff7 Runtime+SVGHMI: Add generic wakeup of threads from PLC thread to windows implementation of plc_main.c. Also added nRT_reschedule to abstract sched_yield.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3294
diff changeset
   284
    memset(rbuf,0,sizeof(rbuf));
0375d801fff7 Runtime+SVGHMI: Add generic wakeup of threads from PLC thread to windows implementation of plc_main.c. Also added nRT_reschedule to abstract sched_yield.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3294
diff changeset
   285
    memset(wbuf,0,sizeof(wbuf));
2819
3b99c908f43b SVGHMI: change collect/send thread looping condition to fix infinite loop in some cases
Edouard Tisserant
parents: 2817
diff changeset
   286
3281
1fc4274de64e SVGHMI: Fixed halting problem when there is no session opened.
Edouard Tisserant
parents: 3273
diff changeset
   287
    svghmi_continue_collect = 1;
2765
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
   288
3294
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3281
diff changeset
   289
    /* create svghmi_pipe */
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3281
diff changeset
   290
    svghmi_handle = create_RT_to_nRT_signal("SVGHMI_pipe");
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3281
diff changeset
   291
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   292
    if(!svghmi_handle)
3294
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3281
diff changeset
   293
        return 1;
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3281
diff changeset
   294
2764
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
   295
    return 0;
2750
2694170cd88e intermediate commit, work in progress
Edouard Tisserant
parents:
diff changeset
   296
}
2764
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
   297
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
   298
void __cleanup_svghmi()
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
   299
{
3281
1fc4274de64e SVGHMI: Fixed halting problem when there is no session opened.
Edouard Tisserant
parents: 3273
diff changeset
   300
    svghmi_continue_collect = 0;
2820
d9b5303d43dc SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents: 2819
diff changeset
   301
    SVGHMI_WakeupFromRTThread();
3294
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3281
diff changeset
   302
    delete_RT_to_nRT_signal(svghmi_handle);
2764
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
   303
}
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
   304
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
   305
void __retrieve_svghmi()
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
   306
{
3374
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   307
    if(AtomicCompareExchange(&hmitree_rlock, 0, 1) == 0) {
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   308
        hmi_tree_item_t *dsc = incoming_tail;
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   309
        /* iterate through read list (changes from HMI) */
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   310
        while(dsc){
3400
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   311
            hmi_tree_item_t *_dsc = dsc->incoming_prev;
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   312
            read_iterator(dsc);
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   313
            /* unnecessary
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   314
            dsc->incoming_prev = NULL;
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   315
            */
3400
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   316
            dsc = _dsc;
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   317
        }
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   318
        /* flush read list */
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   319
        incoming_tail = NULL;
3374
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   320
        AtomicCompareExchange(&hmitree_rlock, 1, 0);
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   321
    }
2764
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
   322
}
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
   323
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
   324
void __publish_svghmi()
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
   325
{
2768
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   326
    global_write_dirty = 0;
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   327
3374
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   328
    if(AtomicCompareExchange(&hmitree_wlock, 0, 1) == 0) {
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   329
        hmi_tree_item_t *dsc = subscriptions_tail;
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   330
        while(dsc){
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   331
            write_iterator(dsc);
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   332
            dsc = dsc->subscriptions_prev;
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   333
        }
3374
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   334
        AtomicCompareExchange(&hmitree_wlock, 1, 0);
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   335
    }
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   336
2768
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   337
    if(global_write_dirty) {
2820
d9b5303d43dc SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents: 2819
diff changeset
   338
        SVGHMI_WakeupFromRTThread();
2775
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   339
    }
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   340
}
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   341
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   342
/* PYTHON CALLS */
3271
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   343
int svghmi_wait(void){
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   344
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   345
    SVGHMI_SuspendFromPythonThread();
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   346
}
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   347
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 2863
diff changeset
   348
int svghmi_send_collect(uint32_t session_index, uint32_t *size, char **ptr){
2775
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   349
3400
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   350
3281
1fc4274de64e SVGHMI: Fixed halting problem when there is no session opened.
Edouard Tisserant
parents: 3273
diff changeset
   351
    if(svghmi_continue_collect) {
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   352
        int res;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   353
        sbufidx = HMI_HASH_SIZE;
3374
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   354
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   355
        while(AtomicCompareExchange(&hmitree_wlock, 0, 1)){
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   356
            nRT_reschedule();
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   357
        }
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   358
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   359
        hmi_tree_item_t *dsc = subscriptions_tail;
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   360
        while(dsc){
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   361
            uint32_t index = dsc - hmi_tree_items;
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   362
            res = send_iterator(index, dsc, session_index);
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   363
            if(res != 0){
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   364
                break;
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   365
            }
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   366
            dsc = dsc->subscriptions_prev;
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   367
        }
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   368
        if(res == 0)
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   369
        {
2799
f5da343b9b63 SVGHMI: Many fixes. Subscriptions to HMItree seems to be working, and dispatch function is called in JS with good data. Bidirectional communication now really working.
Edouard Tisserant
parents: 2798
diff changeset
   370
            if(sbufidx > HMI_HASH_SIZE){
f5da343b9b63 SVGHMI: Many fixes. Subscriptions to HMItree seems to be working, and dispatch function is called in JS with good data. Bidirectional communication now really working.
Edouard Tisserant
parents: 2798
diff changeset
   371
                memcpy(&sbuf[0], &hmi_hash[0], HMI_HASH_SIZE);
f5da343b9b63 SVGHMI: Many fixes. Subscriptions to HMItree seems to be working, and dispatch function is called in JS with good data. Bidirectional communication now really working.
Edouard Tisserant
parents: 2798
diff changeset
   372
                *ptr = &sbuf[0];
f5da343b9b63 SVGHMI: Many fixes. Subscriptions to HMItree seems to be working, and dispatch function is called in JS with good data. Bidirectional communication now really working.
Edouard Tisserant
parents: 2798
diff changeset
   373
                *size = sbufidx;
3374
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   374
                AtomicCompareExchange(&hmitree_wlock, 1, 0);
2799
f5da343b9b63 SVGHMI: Many fixes. Subscriptions to HMItree seems to be working, and dispatch function is called in JS with good data. Bidirectional communication now really working.
Edouard Tisserant
parents: 2798
diff changeset
   375
                return 0;
f5da343b9b63 SVGHMI: Many fixes. Subscriptions to HMItree seems to be working, and dispatch function is called in JS with good data. Bidirectional communication now really working.
Edouard Tisserant
parents: 2798
diff changeset
   376
            }
3374
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   377
            AtomicCompareExchange(&hmitree_wlock, 1, 0);
2799
f5da343b9b63 SVGHMI: Many fixes. Subscriptions to HMItree seems to be working, and dispatch function is called in JS with good data. Bidirectional communication now really working.
Edouard Tisserant
parents: 2798
diff changeset
   378
            return ENODATA;
f5da343b9b63 SVGHMI: Many fixes. Subscriptions to HMItree seems to be working, and dispatch function is called in JS with good data. Bidirectional communication now really working.
Edouard Tisserant
parents: 2798
diff changeset
   379
        }
3374
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   380
        AtomicCompareExchange(&hmitree_wlock, 1, 0);
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   381
        return res;
2775
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   382
    }
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   383
    else
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   384
    {
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   385
        return EINTR;
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   386
    }
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   387
}
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   388
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   389
typedef enum {
3374
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   390
    unset = -1,
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   391
    setval = 0,
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   392
    reset = 1,
2798
ddb2c4668a6b SVGHMI : many details about communication implemented in JS, with side effects.
Edouard Tisserant
parents: 2789
diff changeset
   393
    subscribe = 2
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   394
} cmd_from_JS;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   395
3271
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   396
int svghmi_reset(uint32_t session_index){
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   397
    hmi_tree_item_t *dsc = subscriptions_tail;
3400
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   398
    while(AtomicCompareExchange(&hmitree_wlock, 0, 1)){
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   399
        nRT_reschedule();
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   400
    }
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   401
    while(dsc){
3400
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   402
        hmi_tree_item_t *_dsc = dsc->subscriptions_prev;
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   403
        update_refresh_period(dsc, session_index, 0);
3400
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   404
        dsc = _dsc;
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   405
    }
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   406
    AtomicCompareExchange(&hmitree_wlock, 1, 0);
3271
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   407
    return 1;
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   408
}
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   409
2822
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2820
diff changeset
   410
// Returns :
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2820
diff changeset
   411
//   0 is OK, <0 is error, 1 is heartbeat
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 2863
diff changeset
   412
int svghmi_recv_dispatch(uint32_t session_index, uint32_t size, const uint8_t *ptr){
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   413
    const uint8_t* cursor = ptr + HMI_HASH_SIZE;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   414
    const uint8_t* end = ptr + size;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   415
2822
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2820
diff changeset
   416
    int was_hearbeat = 0;
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   417
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   418
    /* match hmitree fingerprint */
2789
ba0dd2ec6dc4 SVGHMI: now built.
Edouard Tisserant
parents: 2788
diff changeset
   419
    if(size <= HMI_HASH_SIZE || memcmp(ptr, hmi_hash, HMI_HASH_SIZE) != 0)
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   420
    {
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   421
        printf("svghmi_recv_dispatch MISMATCH !!\n");
2822
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2820
diff changeset
   422
        return -EINVAL;
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   423
    }
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   424
3374
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   425
    int ret;
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   426
    int got_wlock = 0;
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   427
    int got_rlock = 0;
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   428
    cmd_from_JS cmd_old = unset;
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   429
    cmd_from_JS cmd = unset;
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   430
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   431
    while(cursor < end)
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   432
    {
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   433
        uint32_t progress;
3374
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   434
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   435
        cmd_old = cmd;
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   436
        cmd = *(cursor++);
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   437
3400
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   438
3374
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   439
        if(cmd_old != cmd){
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   440
            if(got_wlock){
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   441
                AtomicCompareExchange(&hmitree_wlock, 1, 0);
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   442
                got_wlock = 0;
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   443
            }
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   444
            if(got_rlock){
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   445
                AtomicCompareExchange(&hmitree_rlock, 1, 0);
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   446
                got_rlock = 0;
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   447
            }
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   448
        }
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   449
        switch(cmd)
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   450
        {
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   451
            case setval:
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   452
            {
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   453
                uint32_t index = *(uint32_t*)(cursor);
2789
ba0dd2ec6dc4 SVGHMI: now built.
Edouard Tisserant
parents: 2788
diff changeset
   454
                uint8_t const *valptr = cursor + sizeof(uint32_t);
2862
a108677bd3d0 SVGHMI: whitespaces
Edouard Tisserant
parents: 2829
diff changeset
   455
3400
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   456
2822
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2820
diff changeset
   457
                if(index == heartbeat_index)
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2820
diff changeset
   458
                    was_hearbeat = 1;
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   459
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   460
                if(index < HMI_ITEM_COUNT)
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   461
                {
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   462
                    hmi_tree_item_t *dsc = &hmi_tree_items[index];
3397
75920c99ffc9 SVGHMI: Adapt svghmi.c to changes in UnpackVar
Edouard Tisserant
parents: 3374
diff changeset
   463
                    size_t sz = 0;
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   464
                    void *dst_p = &rbuf[dsc->buf_index];
2829
4c2c50f60730 SVGHMI : HMI_STRING now also supported from HMI to PLC
Edouard Tisserant
parents: 2828
diff changeset
   465
4c2c50f60730 SVGHMI : HMI_STRING now also supported from HMI to PLC
Edouard Tisserant
parents: 2828
diff changeset
   466
                    if(__Is_a_string(dsc)){
4c2c50f60730 SVGHMI : HMI_STRING now also supported from HMI to PLC
Edouard Tisserant
parents: 2828
diff changeset
   467
                        sz = ((STRING*)valptr)->len + 1;
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   468
                    } else {
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   469
                        UnpackVar(dsc, NULL, NULL, &sz);
2829
4c2c50f60730 SVGHMI : HMI_STRING now also supported from HMI to PLC
Edouard Tisserant
parents: 2828
diff changeset
   470
                    }
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   471
2802
64e6f73b9859 SVGHMI - Fixed svghmi.{c,js} about HMI -> PLC data unpack.
Edouard Tisserant
parents: 2799
diff changeset
   472
                    if((valptr + sz) <= end)
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   473
                    {
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   474
                        // rescheduling spinlock until free
3374
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   475
                        if(!got_rlock){
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   476
                            while(AtomicCompareExchange(&hmitree_rlock, 0, 1)){
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   477
                                nRT_reschedule();
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   478
                            }
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   479
                            got_rlock=1;
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   480
                        }
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   481
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   482
                        memcpy(dst_p, valptr, sz);
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   483
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   484
                        /* check that rstate is not already buf_set */
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   485
                        if(dsc->rstate != buf_set){
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   486
                            dsc->rstate = buf_set;
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   487
                            /* append entry to read list (changes from HMI) */
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   488
                            dsc->incoming_prev = incoming_tail;
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   489
                            incoming_tail = dsc;
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   490
                        }
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   491
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   492
                        progress = sz + sizeof(uint32_t) /* index */;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   493
                    }
2862
a108677bd3d0 SVGHMI: whitespaces
Edouard Tisserant
parents: 2829
diff changeset
   494
                    else
2799
f5da343b9b63 SVGHMI: Many fixes. Subscriptions to HMItree seems to be working, and dispatch function is called in JS with good data. Bidirectional communication now really working.
Edouard Tisserant
parents: 2798
diff changeset
   495
                    {
3374
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   496
                        ret = -EINVAL;
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   497
                        goto exit_free;
2799
f5da343b9b63 SVGHMI: Many fixes. Subscriptions to HMItree seems to be working, and dispatch function is called in JS with good data. Bidirectional communication now really working.
Edouard Tisserant
parents: 2798
diff changeset
   498
                    }
f5da343b9b63 SVGHMI: Many fixes. Subscriptions to HMItree seems to be working, and dispatch function is called in JS with good data. Bidirectional communication now really working.
Edouard Tisserant
parents: 2798
diff changeset
   499
                }
2862
a108677bd3d0 SVGHMI: whitespaces
Edouard Tisserant
parents: 2829
diff changeset
   500
                else
2799
f5da343b9b63 SVGHMI: Many fixes. Subscriptions to HMItree seems to be working, and dispatch function is called in JS with good data. Bidirectional communication now really working.
Edouard Tisserant
parents: 2798
diff changeset
   501
                {
3374
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   502
                    ret = -EINVAL;
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   503
                    goto exit_free;
2799
f5da343b9b63 SVGHMI: Many fixes. Subscriptions to HMItree seems to be working, and dispatch function is called in JS with good data. Bidirectional communication now really working.
Edouard Tisserant
parents: 2798
diff changeset
   504
                }
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   505
            }
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   506
            break;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   507
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   508
            case reset:
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   509
            {
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   510
                progress = 0;
3374
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   511
                if(!got_wlock){
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   512
                    while(AtomicCompareExchange(&hmitree_wlock, 0, 1)){
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   513
                        nRT_reschedule();
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   514
                    }
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   515
                    got_wlock = 1;
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   516
                }
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   517
                {
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   518
                    hmi_tree_item_t *dsc = subscriptions_tail;
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   519
                    while(dsc){
3400
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   520
                        hmi_tree_item_t *_dsc = dsc->subscriptions_prev;
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   521
                        update_refresh_period(dsc, session_index, 0);
3400
c2b46d0965ca Fix bug introduced in previous commit : dual link list wasn't append and remove wasn't implemented correctly. Removed debug code, enhanced variable names and comments.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3399
diff changeset
   522
                        dsc = _dsc;
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   523
                    }
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   524
                }
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   525
            }
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   526
            break;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   527
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   528
            case subscribe:
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   529
            {
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   530
                uint32_t index = *(uint32_t*)(cursor);
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   531
                uint16_t refresh_period_ms = *(uint32_t*)(cursor + sizeof(uint32_t));
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   532
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   533
                if(index < HMI_ITEM_COUNT)
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   534
                {
3374
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   535
                    if(!got_wlock){
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   536
                        while(AtomicCompareExchange(&hmitree_wlock, 0, 1)){
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   537
                            nRT_reschedule();
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   538
                        }
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   539
                        got_wlock = 1;
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   540
                    }
3399
95e0b926a8c3 SVGHMI: optimization of C part : stop traversing the whole HMI tree, use dual linked list for subscriptions and single linked list for changes from HMI. Intermediate commit, still crashing in some cases.
Edouard Tisserant
parents: 3397
diff changeset
   541
                    hmi_tree_item_t *dsc = &hmi_tree_items[index];
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 2863
diff changeset
   542
                    update_refresh_period(dsc, session_index, refresh_period_ms);
2799
f5da343b9b63 SVGHMI: Many fixes. Subscriptions to HMItree seems to be working, and dispatch function is called in JS with good data. Bidirectional communication now really working.
Edouard Tisserant
parents: 2798
diff changeset
   543
                }
2862
a108677bd3d0 SVGHMI: whitespaces
Edouard Tisserant
parents: 2829
diff changeset
   544
                else
2799
f5da343b9b63 SVGHMI: Many fixes. Subscriptions to HMItree seems to be working, and dispatch function is called in JS with good data. Bidirectional communication now really working.
Edouard Tisserant
parents: 2798
diff changeset
   545
                {
3374
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   546
                    ret = -EINVAL;
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   547
                    goto exit_free;
2799
f5da343b9b63 SVGHMI: Many fixes. Subscriptions to HMItree seems to be working, and dispatch function is called in JS with good data. Bidirectional communication now really working.
Edouard Tisserant
parents: 2798
diff changeset
   548
                }
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   549
2862
a108677bd3d0 SVGHMI: whitespaces
Edouard Tisserant
parents: 2829
diff changeset
   550
                progress = sizeof(uint32_t) /* index */ +
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   551
                           sizeof(uint16_t) /* refresh period */;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   552
            }
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   553
            break;
2799
f5da343b9b63 SVGHMI: Many fixes. Subscriptions to HMItree seems to be working, and dispatch function is called in JS with good data. Bidirectional communication now really working.
Edouard Tisserant
parents: 2798
diff changeset
   554
            default:
f5da343b9b63 SVGHMI: Many fixes. Subscriptions to HMItree seems to be working, and dispatch function is called in JS with good data. Bidirectional communication now really working.
Edouard Tisserant
parents: 2798
diff changeset
   555
                printf("svghmi_recv_dispatch unknown %%d\n",cmd);
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   556
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   557
        }
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   558
        cursor += progress;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   559
    }
3374
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   560
    ret = was_hearbeat;
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   561
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   562
exit_free:
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   563
    if(got_wlock){
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   564
        AtomicCompareExchange(&hmitree_wlock, 1, 0);
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   565
        got_wlock = 0;
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   566
    }
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   567
    if(got_rlock){
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   568
        AtomicCompareExchange(&hmitree_rlock, 1, 0);
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   569
        got_rlock = 0;
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   570
    }
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   571
    return ret;
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   572
}
9a82918e063c SVGHMI: optimize HMI tree handling C code to lower CPU usage when traversing large trees
Edouard Tisserant
parents: 3295
diff changeset
   573