svghmi/svghmi.c
author Edouard Tisserant
Wed, 02 Oct 2019 11:31:02 +0200
branchsvghmi
changeset 2789 ba0dd2ec6dc4
parent 2788 2ed9ff826d03
child 2798 ddb2c4668a6b
permissions -rw-r--r--
SVGHMI: now built.
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
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    12
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
    13
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    14
/* PLC reads from that buffer */
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    15
static char rbuf[HMI_BUFFER_SIZE];
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 writes to that buffer */
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    18
static char wbuf[HMI_BUFFER_SIZE];
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    19
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    20
/* TODO change that in case of multiclient... */
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    21
/* 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
    22
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
    23
static unsigned int sbufidx;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    24
2768
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    25
%(extern_variables_declarations)s
2765
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    26
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
    27
#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
    28
static uint16_t ticktime_ms = (ticktime_ns>1000000)?
2768
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    29
                     ticktime_ns/1000000:
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    30
                     1;
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    31
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    32
typedef enum {
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    33
    buf_free = 0,
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    34
    buf_set,
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    35
    buf_tosend
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    36
} buf_state_t;
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    37
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    38
static int global_write_dirty = 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
    39
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
    40
typedef struct {
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
    41
    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
    42
    __IEC_types_enum type;
2765
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    43
    uint32_t buf_index;
2768
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    44
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    45
    /* publish/write/send */
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
    46
    long wlock;
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    47
    buf_state_t wstate;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    48
2768
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    49
    /* zero means not subscribed */
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    50
    uint16_t refresh_period_ms;
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    51
    uint16_t age_ms;
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    52
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    53
    /* retrieve/read/recv */
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
    54
    long rlock;
2768
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    55
    buf_state_t rstate;
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    56
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
    57
} hmi_tree_item_t;
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
    58
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
    59
static hmi_tree_item_t hmi_tree_item[] = {
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
    60
%(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
    61
};
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
    62
2789
ba0dd2ec6dc4 SVGHMI: now built.
Edouard Tisserant
parents: 2788
diff changeset
    63
typedef int(*hmi_tree_iterator)(uint32_t, hmi_tree_item_t*);
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    64
static int traverse_hmi_tree(hmi_tree_iterator fp)
2765
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    65
{
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    66
    unsigned int i;
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    67
    for(i = 0; i < sizeof(hmi_tree_item)/sizeof(hmi_tree_item_t); i++){
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    68
        int res;
2765
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    69
        hmi_tree_item_t *dsc = &hmi_tree_item[i];
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    70
        if(res = (*fp)(i, dsc))
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    71
            return res;
2765
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    72
    }
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    73
}
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    74
2767
302347f48193 svghmi.c : deduplicated variable access code borrowed from plc_debug.c. Added targets/var_access.c.
Edouard Tisserant
parents: 2766
diff changeset
    75
#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
    76
2767
302347f48193 svghmi.c : deduplicated variable access code borrowed from plc_debug.c. Added targets/var_access.c.
Edouard Tisserant
parents: 2766
diff changeset
    77
%(var_access_code)s
2765
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    78
2789
ba0dd2ec6dc4 SVGHMI: now built.
Edouard Tisserant
parents: 2788
diff changeset
    79
static int write_iterator(uint32_t index, 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
    80
{
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    81
    if(AtomicCompareExchange(&dsc->wlock, 0, 1) == 0)
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    82
    {
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    83
        if(dsc->wstate == buf_set){
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    84
            /* if being subscribed */
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    85
            if(dsc->refresh_period_ms){
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    86
                if(dsc->age_ms + ticktime_ms < dsc->refresh_period_ms){
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    87
                    dsc->age_ms += ticktime_ms;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    88
                }else{
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    89
                    dsc->wstate = buf_tosend;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    90
                }
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    91
            }
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    92
        }
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    93
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    94
        void *dest_p = &wbuf[dsc->buf_index];
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    95
        void *real_value_p = NULL;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    96
        char flags = 0;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    97
        void *visible_value_p = UnpackVar(dsc, &real_value_p, &flags);
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    98
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    99
        /* if new value differs from previous one */
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   100
        USINT sz = __get_type_enum_size(dsc->type);
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   101
        if(memcmp(dest_p, visible_value_p, sz) != 0){
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   102
            /* copy and flag as set */
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   103
            memcpy(dest_p, visible_value_p, sz);
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   104
            if(dsc->wstate == buf_free) {
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   105
                dsc->wstate = buf_set;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   106
                dsc->age_ms = 0;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   107
            }
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   108
            global_write_dirty = 1;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   109
        }
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   110
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   111
        AtomicCompareExchange(&dsc->wlock, 1, 0);
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   112
    }
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   113
    // else ... : PLC can't wait, variable will be updated next turn
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   114
    return 0;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   115
}
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   116
2789
ba0dd2ec6dc4 SVGHMI: now built.
Edouard Tisserant
parents: 2788
diff changeset
   117
static int send_iterator(uint32_t index, 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
   118
{
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   119
    int res = 0;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   120
    while(AtomicCompareExchange(&dsc->wlock, 0, 1)) sched_yield();
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   121
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   122
    if(dsc->wstate == buf_tosend)
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   123
    {
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   124
        uint32_t sz = __get_type_enum_size(dsc->type);
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   125
        if(sbufidx + sizeof(uint32_t) + sz <  sizeof(sbuf))
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   126
        {
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   127
            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
   128
            void *dst_p = &sbuf[sbufidx];
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   129
            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
   130
            memcpy(dst_p + sizeof(uint32_t), src_p, sz);
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   131
            dsc->wstate = buf_free;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   132
            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
   133
        }
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   134
        else
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   135
        {
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   136
            res = EOVERFLOW;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   137
        }
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   138
    }
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   139
2768
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   140
    AtomicCompareExchange(&dsc->wlock, 1, 0);
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   141
    return res; 
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   142
}
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   143
2789
ba0dd2ec6dc4 SVGHMI: now built.
Edouard Tisserant
parents: 2788
diff changeset
   144
static int read_iterator(uint32_t index, 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
   145
{
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   146
    if(AtomicCompareExchange(&dsc->rlock, 0, 1) == 0)
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   147
    {
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   148
        if(dsc->rstate == buf_set)
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   149
        {
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   150
            void *src_p = &rbuf[dsc->buf_index];
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   151
            void *real_value_p = NULL;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   152
            char flags = 0;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   153
            void *visible_value_p = UnpackVar(dsc, &real_value_p, &flags);
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   154
            memcpy(real_value_p, src_p, __get_type_enum_size(dsc->type));
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   155
            dsc->rstate = buf_free;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   156
        }
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   157
        AtomicCompareExchange(&dsc->rlock, 1, 0);
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   158
    }
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   159
    // else ... : PLC can't wait, variable will be updated next turn
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   160
    return 0;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   161
}
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   162
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   163
inline void update_refresh_period(hmi_tree_item_t *dsc, uint16_t refresh_period_ms)
2768
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   164
{
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   165
    while(AtomicCompareExchange(&dsc->wlock, 0, 1)) sched_yield();
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   166
    dsc->refresh_period_ms = refresh_period_ms;
2768
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   167
    AtomicCompareExchange(&dsc->wlock, 1, 0);
2766
3f3b1b8ccba4 SVGHMI: Added iterators in svghmi.c copy-pasted form plc_debug.c
Edouard Tisserant
parents: 2765
diff changeset
   168
}
3f3b1b8ccba4 SVGHMI: Added iterators in svghmi.c copy-pasted form plc_debug.c
Edouard Tisserant
parents: 2765
diff changeset
   169
2789
ba0dd2ec6dc4 SVGHMI: now built.
Edouard Tisserant
parents: 2788
diff changeset
   170
static int reset_iterator(uint32_t index, hmi_tree_item_t *dsc)
ba0dd2ec6dc4 SVGHMI: now built.
Edouard Tisserant
parents: 2788
diff changeset
   171
{
ba0dd2ec6dc4 SVGHMI: now built.
Edouard Tisserant
parents: 2788
diff changeset
   172
    update_refresh_period(dsc, 0);
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   173
    return 0;
2765
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
   174
}
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
   175
2777
cdf6584953a0 SVGHMI: WIP for python<->C data exchange : message from browser hit the C side.
Edouard Tisserant
parents: 2776
diff changeset
   176
static pthread_cond_t svghmi_send_WakeCond = PTHREAD_COND_INITIALIZER;
cdf6584953a0 SVGHMI: WIP for python<->C data exchange : message from browser hit the C side.
Edouard Tisserant
parents: 2776
diff changeset
   177
static pthread_mutex_t svghmi_send_WakeCondLock = PTHREAD_MUTEX_INITIALIZER;
2775
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   178
2776
246ae685ab65 SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2775
diff changeset
   179
static int continue_collect;
246ae685ab65 SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2775
diff changeset
   180
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
   181
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
   182
{
2765
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
   183
    bzero(rbuf,sizeof(rbuf));
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
   184
    bzero(wbuf,sizeof(wbuf));
2775
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   185
    continue_collect = 1;
2765
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
   186
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
   187
    return 0;
2750
2694170cd88e intermediate commit, work in progress
Edouard Tisserant
parents:
diff changeset
   188
}
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
   189
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
   190
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
   191
{
2777
cdf6584953a0 SVGHMI: WIP for python<->C data exchange : message from browser hit the C side.
Edouard Tisserant
parents: 2776
diff changeset
   192
    pthread_mutex_lock(&svghmi_send_WakeCondLock);
2775
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   193
    continue_collect = 0;
2777
cdf6584953a0 SVGHMI: WIP for python<->C data exchange : message from browser hit the C side.
Edouard Tisserant
parents: 2776
diff changeset
   194
    pthread_cond_signal(&svghmi_send_WakeCond);
cdf6584953a0 SVGHMI: WIP for python<->C data exchange : message from browser hit the C side.
Edouard Tisserant
parents: 2776
diff changeset
   195
    pthread_mutex_unlock(&svghmi_send_WakeCondLock);
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
   196
}
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
   197
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
   198
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
   199
{
2768
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   200
    traverse_hmi_tree(read_iterator);
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
   201
}
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
   202
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
   203
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
   204
{
2768
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   205
    global_write_dirty = 0;
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   206
    traverse_hmi_tree(write_iterator);
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   207
    if(global_write_dirty) {
2777
cdf6584953a0 SVGHMI: WIP for python<->C data exchange : message from browser hit the C side.
Edouard Tisserant
parents: 2776
diff changeset
   208
        pthread_cond_signal(&svghmi_send_WakeCond);
2775
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   209
    }
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   210
}
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   211
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   212
/* PYTHON CALLS */
2779
75c6a31caca6 SVGHMI: Work In Progress : fixed pointer types in ctypes interface, cleaned up server startup and cleanup code, changed document type to XHTML, cleaner JS script : encapsulated in a function and in CDATA.
Edouard Tisserant
parents: 2777
diff changeset
   213
int svghmi_send_collect(uint32_t *size, char **ptr){
2775
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   214
2776
246ae685ab65 SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2775
diff changeset
   215
    int do_collect;
2777
cdf6584953a0 SVGHMI: WIP for python<->C data exchange : message from browser hit the C side.
Edouard Tisserant
parents: 2776
diff changeset
   216
    pthread_mutex_lock(&svghmi_send_WakeCondLock);
2775
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   217
    do_collect = continue_collect;
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   218
    if(do_collect)
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   219
    {
2777
cdf6584953a0 SVGHMI: WIP for python<->C data exchange : message from browser hit the C side.
Edouard Tisserant
parents: 2776
diff changeset
   220
        pthread_cond_wait(&svghmi_send_WakeCond, &svghmi_send_WakeCondLock);
2775
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   221
        do_collect = continue_collect;
2776
246ae685ab65 SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2775
diff changeset
   222
    }
2777
cdf6584953a0 SVGHMI: WIP for python<->C data exchange : message from browser hit the C side.
Edouard Tisserant
parents: 2776
diff changeset
   223
    pthread_mutex_unlock(&svghmi_send_WakeCondLock);
2775
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   224
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   225
    if(do_collect) {
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   226
        int res;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   227
        memcpy(&sbuf[0], &hmi_hash[0], HMI_HASH_SIZE);
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   228
        sbufidx = HMI_HASH_SIZE;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   229
        if((res = traverse_hmi_tree(send_iterator)) == 0)
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   230
        {
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   231
            *ptr = &sbuf[0];
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   232
            *size = sbufidx;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   233
        }
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   234
        return res;
2775
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   235
    }
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   236
    else
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   237
    {
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   238
        return EINTR;
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   239
    }
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   240
}
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   241
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   242
typedef enum {
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   243
    setval = 0,
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   244
    reset = 1,
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   245
    subscribe = 2,
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   246
    unsubscribe = 3
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   247
} cmd_from_JS;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   248
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   249
int svghmi_recv_dispatch(uint32_t size, const uint8_t *ptr){
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   250
    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
   251
    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
   252
2789
ba0dd2ec6dc4 SVGHMI: now built.
Edouard Tisserant
parents: 2788
diff changeset
   253
    printf("svghmi_recv_dispatch %%d\n",size);
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   254
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   255
    /* match hmitree fingerprint */
2789
ba0dd2ec6dc4 SVGHMI: now built.
Edouard Tisserant
parents: 2788
diff changeset
   256
    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
   257
    {
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   258
        printf("svghmi_recv_dispatch MISMATCH !!\n");
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   259
        return EINVAL;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   260
    }
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   261
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   262
    while(cursor < end)
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   263
    {
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   264
        uint32_t progress;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   265
        cmd_from_JS cmd = *(cursor++);
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   266
        switch(cmd)
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   267
        {
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   268
            case setval:
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   269
            {
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   270
                uint32_t index = *(uint32_t*)(cursor);
2789
ba0dd2ec6dc4 SVGHMI: now built.
Edouard Tisserant
parents: 2788
diff changeset
   271
                uint8_t const *valptr = cursor + sizeof(uint32_t);
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   272
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   273
                if(index < HMI_ITEM_COUNT)
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   274
                {
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   275
                    hmi_tree_item_t *dsc = &hmi_tree_item[index];
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   276
                    void *real_value_p = NULL;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   277
                    char flags = 0;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   278
                    void *visible_value_p = UnpackVar(dsc, &real_value_p, &flags);
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   279
                    void *dst_p = &rbuf[dsc->buf_index];
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   280
                    uint32_t sz = __get_type_enum_size(dsc->type);
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   281
2789
ba0dd2ec6dc4 SVGHMI: now built.
Edouard Tisserant
parents: 2788
diff changeset
   282
                    if((valptr + sz) < end)
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   283
                    {
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   284
                        // rescheduling spinlock until free
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   285
                        while(AtomicCompareExchange(&dsc->rlock, 0, 1)) sched_yield();
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   286
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   287
                        memcpy(dst_p, valptr, sz);
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   288
                        dsc->rstate = buf_set;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   289
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   290
                        AtomicCompareExchange(&dsc->rlock, 1, 0);
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   291
                        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
   292
                    }
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   293
                    else return -EINVAL;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   294
                }
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   295
                else return -EINVAL;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   296
            }
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   297
            break;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   298
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   299
            case reset:
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   300
            {
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   301
                progress = 0;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   302
                traverse_hmi_tree(reset_iterator);
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   303
            }
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   304
            break;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   305
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   306
            case subscribe:
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   307
            {
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   308
                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
   309
                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
   310
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   311
                if(index < HMI_ITEM_COUNT)
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   312
                {
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   313
                    hmi_tree_item_t *dsc = &hmi_tree_item[index];
2789
ba0dd2ec6dc4 SVGHMI: now built.
Edouard Tisserant
parents: 2788
diff changeset
   314
                    update_refresh_period(dsc, refresh_period_ms);
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   315
                }
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   316
                else return -EINVAL;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   317
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   318
                progress = sizeof(uint32_t) /* index */ + 
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   319
                           sizeof(uint16_t) /* refresh period */;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   320
            }
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   321
            break;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   322
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   323
            case unsubscribe:
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   324
            {
2789
ba0dd2ec6dc4 SVGHMI: now built.
Edouard Tisserant
parents: 2788
diff changeset
   325
                uint32_t index = *(uint32_t*)(cursor);
ba0dd2ec6dc4 SVGHMI: now built.
Edouard Tisserant
parents: 2788
diff changeset
   326
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   327
                if(index < HMI_ITEM_COUNT)
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   328
                {
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   329
                    hmi_tree_item_t *dsc = &hmi_tree_item[index];
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   330
                    reset_iterator(index, dsc);
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   331
                }
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   332
                else return -EINVAL;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   333
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   334
                progress = sizeof(uint32_t) /* index */;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   335
            }
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   336
            break;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   337
        }
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   338
        cursor += progress;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   339
    }
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   340
    return 0;
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   341
}
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
   342