svghmi/svghmi.c
author Edouard Tisserant
Wed, 18 Sep 2019 11:09:35 +0200
branchsvghmi
changeset 2779 75c6a31caca6
parent 2777 cdf6584953a0
child 2788 2ed9ff826d03
permissions -rw-r--r--
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.
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
2765
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    11
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    12
/* PLC reads from that buffer */
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    13
static char rbuf[HMI_BUFFER_SIZE];
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    14
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    15
/* PLC writes to that buffer */
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    16
static char wbuf[HMI_BUFFER_SIZE];
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    17
2768
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    18
%(extern_variables_declarations)s
2765
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    19
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
    20
#define ticktime_ns %(PLC_ticktime)d
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
    21
uint16_t ticktime_ms = (ticktime_ns>1000000)?
2768
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    22
                     ticktime_ns/1000000:
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    23
                     1;
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    24
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    25
typedef enum {
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    26
    buf_free = 0,
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    27
    buf_set,
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    28
    buf_tosend
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    29
} buf_state_t;
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    30
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    31
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
    32
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
    33
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
    34
    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
    35
    __IEC_types_enum type;
2765
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    36
    uint32_t buf_index;
2768
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    37
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    38
    /* 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
    39
    long wlock;
2768
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    40
    /* zero means not subscribed */
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    41
    uint16_t refresh_period_ms;
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    42
    uint16_t age_ms;
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    43
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    44
    buf_state_t wstate;
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    45
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    46
    /* 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
    47
    long rlock;
2768
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    48
    buf_state_t rstate;
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    49
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
    50
} 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
    51
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
    52
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
    53
%(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
    54
};
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
    55
2776
246ae685ab65 SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2775
diff changeset
    56
static char sendbuf[HMI_BUFFER_SIZE];
246ae685ab65 SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2775
diff changeset
    57
2765
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    58
typedef void(*hmi_tree_iterator)(hmi_tree_item_t*);
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    59
void traverse_hmi_tree(hmi_tree_iterator fp)
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    60
{
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    61
    unsigned int i;
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    62
    for(i = 0; i < sizeof(hmi_tree_item)/sizeof(hmi_tree_item_t); i++){
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    63
        hmi_tree_item_t *dsc = &hmi_tree_item[i];
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    64
        if(dsc->type != UNKNOWN_ENUM) 
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    65
            (*fp)(dsc);
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    66
    }
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    67
}
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    68
2767
302347f48193 svghmi.c : deduplicated variable access code borrowed from plc_debug.c. Added targets/var_access.c.
Edouard Tisserant
parents: 2766
diff changeset
    69
#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
    70
2767
302347f48193 svghmi.c : deduplicated variable access code borrowed from plc_debug.c. Added targets/var_access.c.
Edouard Tisserant
parents: 2766
diff changeset
    71
%(var_access_code)s
2765
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
    72
2766
3f3b1b8ccba4 SVGHMI: Added iterators in svghmi.c copy-pasted form plc_debug.c
Edouard Tisserant
parents: 2765
diff changeset
    73
void write_iterator(hmi_tree_item_t *dsc)
3f3b1b8ccba4 SVGHMI: Added iterators in svghmi.c copy-pasted form plc_debug.c
Edouard Tisserant
parents: 2765
diff changeset
    74
{
3f3b1b8ccba4 SVGHMI: Added iterators in svghmi.c copy-pasted form plc_debug.c
Edouard Tisserant
parents: 2765
diff changeset
    75
    void *dest_p = &wbuf[dsc->buf_index];
3f3b1b8ccba4 SVGHMI: Added iterators in svghmi.c copy-pasted form plc_debug.c
Edouard Tisserant
parents: 2765
diff changeset
    76
    void *real_value_p = NULL;
3f3b1b8ccba4 SVGHMI: Added iterators in svghmi.c copy-pasted form plc_debug.c
Edouard Tisserant
parents: 2765
diff changeset
    77
    char flags = 0;
3f3b1b8ccba4 SVGHMI: Added iterators in svghmi.c copy-pasted form plc_debug.c
Edouard Tisserant
parents: 2765
diff changeset
    78
3f3b1b8ccba4 SVGHMI: Added iterators in svghmi.c copy-pasted form plc_debug.c
Edouard Tisserant
parents: 2765
diff changeset
    79
    void *visible_value_p = UnpackVar(dsc, &real_value_p, &flags);
3f3b1b8ccba4 SVGHMI: Added iterators in svghmi.c copy-pasted form plc_debug.c
Edouard Tisserant
parents: 2765
diff changeset
    80
2768
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    81
    /* Try take lock */
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    82
    long was_locked = AtomicCompareExchange(&dsc->wlock, 0, 1);
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    83
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    84
    if(was_locked) {
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    85
        /* was locked. give up*/
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    86
        return;
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    87
    }
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    88
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
    89
    if(dsc->wstate == buf_set){
2768
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    90
        /* if being subscribed */
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    91
        if(dsc->refresh_period_ms){
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    92
            if(dsc->age_ms + ticktime_ms < dsc->refresh_period_ms){
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    93
                dsc->age_ms += ticktime_ms;
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    94
            }else{
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    95
                dsc->wstate = buf_tosend;
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    96
            }
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    97
        }
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
    98
    }
2776
246ae685ab65 SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2775
diff changeset
    99
2768
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   100
    /* if new value differs from previous one */
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   101
    if(memcmp(dest_p, visible_value_p, __get_type_enum_size(dsc->type)) != 0){
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   102
        /* copy and flag as set */
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   103
        memcpy(dest_p, visible_value_p, __get_type_enum_size(dsc->type));
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   104
        if(dsc->wstate == buf_free) {
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   105
            dsc->wstate = buf_set;
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   106
            dsc->age_ms = 0;
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   107
        }
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   108
        global_write_dirty = 1;
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   109
    }
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   110
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   111
    /* unlock - use AtomicComparExchange to have memory barrier */
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   112
    AtomicCompareExchange(&dsc->wlock, 1, 0);
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   113
}
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   114
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   115
struct timespec sending_now;
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   116
struct timespec next_sending;
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   117
void send_iterator(hmi_tree_item_t *dsc)
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   118
{
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   119
    while(AtomicCompareExchange(&dsc->wlock, 0, 1)) sched_yield();
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   120
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   121
    // check for variable being modified
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
   122
    if(dsc->wstate == buf_tosend){
2768
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   123
        // send 
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   124
2775
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   125
        // TODO pack data in buffer
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   126
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   127
        dsc->wstate = buf_free;
2768
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   128
    }
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   129
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   130
    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
   131
}
3f3b1b8ccba4 SVGHMI: Added iterators in svghmi.c copy-pasted form plc_debug.c
Edouard Tisserant
parents: 2765
diff changeset
   132
3f3b1b8ccba4 SVGHMI: Added iterators in svghmi.c copy-pasted form plc_debug.c
Edouard Tisserant
parents: 2765
diff changeset
   133
void read_iterator(hmi_tree_item_t *dsc)
3f3b1b8ccba4 SVGHMI: Added iterators in svghmi.c copy-pasted form plc_debug.c
Edouard Tisserant
parents: 2765
diff changeset
   134
{
3f3b1b8ccba4 SVGHMI: Added iterators in svghmi.c copy-pasted form plc_debug.c
Edouard Tisserant
parents: 2765
diff changeset
   135
    void *src_p = &rbuf[dsc->buf_index];
3f3b1b8ccba4 SVGHMI: Added iterators in svghmi.c copy-pasted form plc_debug.c
Edouard Tisserant
parents: 2765
diff changeset
   136
    void *real_value_p = NULL;
3f3b1b8ccba4 SVGHMI: Added iterators in svghmi.c copy-pasted form plc_debug.c
Edouard Tisserant
parents: 2765
diff changeset
   137
    char flags = 0;
3f3b1b8ccba4 SVGHMI: Added iterators in svghmi.c copy-pasted form plc_debug.c
Edouard Tisserant
parents: 2765
diff changeset
   138
3f3b1b8ccba4 SVGHMI: Added iterators in svghmi.c copy-pasted form plc_debug.c
Edouard Tisserant
parents: 2765
diff changeset
   139
    void *visible_value_p = UnpackVar(dsc, &real_value_p, &flags);
3f3b1b8ccba4 SVGHMI: Added iterators in svghmi.c copy-pasted form plc_debug.c
Edouard Tisserant
parents: 2765
diff changeset
   140
2768
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   141
2766
3f3b1b8ccba4 SVGHMI: Added iterators in svghmi.c copy-pasted form plc_debug.c
Edouard Tisserant
parents: 2765
diff changeset
   142
    memcpy(visible_value_p, src_p, __get_type_enum_size(dsc->type));
2765
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
   143
}
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
   144
2777
cdf6584953a0 SVGHMI: WIP for python<->C data exchange : message from browser hit the C side.
Edouard Tisserant
parents: 2776
diff changeset
   145
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
   146
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
   147
2776
246ae685ab65 SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2775
diff changeset
   148
static int continue_collect;
246ae685ab65 SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2775
diff changeset
   149
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
   150
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
   151
{
2765
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
   152
    bzero(rbuf,sizeof(rbuf));
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
   153
    bzero(wbuf,sizeof(wbuf));
2775
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   154
    continue_collect = 1;
2765
887aba5ef178 SVGHMI: svghmi.c now has mutex, iterator, and read/write buffer.
Edouard Tisserant
parents: 2764
diff changeset
   155
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
   156
    return 0;
2750
2694170cd88e intermediate commit, work in progress
Edouard Tisserant
parents:
diff changeset
   157
}
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
   158
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
   159
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
   160
{
2777
cdf6584953a0 SVGHMI: WIP for python<->C data exchange : message from browser hit the C side.
Edouard Tisserant
parents: 2776
diff changeset
   161
    pthread_mutex_lock(&svghmi_send_WakeCondLock);
2775
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   162
    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
   163
    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
   164
    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
   165
}
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
   166
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
   167
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
   168
{
2768
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   169
    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
   170
}
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
   171
b75cc2cf4e50 SVGHMI: draft for svghmi.c. It has all PLC variables pointed in HMI tree in an array.
Edouard Tisserant
parents: 2754
diff changeset
   172
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
   173
{
2768
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   174
    global_write_dirty = 0;
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   175
    traverse_hmi_tree(write_iterator);
31785529a657 SVGHMI: Intermediate state while working on svghmi.c
Edouard Tisserant
parents: 2767
diff changeset
   176
    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
   177
        pthread_cond_signal(&svghmi_send_WakeCond);
2775
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   178
    }
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   179
}
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   180
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   181
/* 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
   182
int svghmi_send_collect(uint32_t *size, char **ptr){
2775
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   183
2776
246ae685ab65 SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2775
diff changeset
   184
    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
   185
    pthread_mutex_lock(&svghmi_send_WakeCondLock);
2775
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   186
    do_collect = continue_collect;
2776
246ae685ab65 SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2775
diff changeset
   187
    if(do_collect){
2777
cdf6584953a0 SVGHMI: WIP for python<->C data exchange : message from browser hit the C side.
Edouard Tisserant
parents: 2776
diff changeset
   188
        pthread_cond_wait(&svghmi_send_WakeCond, &svghmi_send_WakeCondLock);
2775
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   189
        do_collect = continue_collect;
2776
246ae685ab65 SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2775
diff changeset
   190
    }
2777
cdf6584953a0 SVGHMI: WIP for python<->C data exchange : message from browser hit the C side.
Edouard Tisserant
parents: 2776
diff changeset
   191
    pthread_mutex_unlock(&svghmi_send_WakeCondLock);
2775
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   192
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   193
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   194
    if(do_collect) {
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   195
        traverse_hmi_tree(send_iterator);
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   196
        /* TODO set ptr and size to something  */
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   197
        return 0;
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   198
    }
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   199
    else
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   200
    {
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   201
        return EINTR;
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   202
    }
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   203
}
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   204
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
   205
int svghmi_recv_dispatch(uint32_t size, char *ptr){
2777
cdf6584953a0 SVGHMI: WIP for python<->C data exchange : message from browser hit the C side.
Edouard Tisserant
parents: 2776
diff changeset
   206
    printf("%%*s",size,ptr);
2775
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   207
    /* TODO something with ptr and size
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   208
        - subscribe
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   209
         or
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   210
        - spread values
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
}
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2771
diff changeset
   213