sync/gen_statemachine.ysl2
author Volker Birk <vb@pep.foundation>
Sat, 20 Aug 2016 14:45:19 +0200
branchkeysync
changeset 1043 f786ee22e281
parent 985 087d5d60c082
child 1046 73be5d9af815
permissions -rw-r--r--
empty address allowed for myself()
vb@577
     1
// generate state machine code
vb@577
     2
vb@577
     3
// Copyleft (c) 2016, p≡p foundation
vb@577
     4
vb@577
     5
// Written by Volker Birk
vb@577
     6
vb@577
     7
include yslt.yml2
vb@577
     8
vb@577
     9
tstylesheet {
vb@623
    10
    include ./functions.ysl2
Edouard@605
    11
vb@811
    12
    template "/protocol" {
vb@811
    13
        document "../src/Makefile.protocols", "text"
vb@811
    14
            apply "fsm", 0, mode="make";
vb@811
    15
        apply "fsm", 0, mode=gen;
vb@811
    16
    }
vb@811
    17
vb@811
    18
    template "fsm", mode=make
vb@811
    19
    ||
vb@811
    20
    «@filename»_fsm.c: ../sync/devicegroup.fsm
vb@811
    21
    \tmake -C ../«@filename»
vb@811
    22
    ||
vb@811
    23
vb@811
    24
    template "fsm", mode=gen {
vb@807
    25
        document "../src/{@filename}_fsm.h", "text" {
vb@609
    26
        ||
vb@609
    27
        #pragma once
vb@577
    28
vb@609
    29
        // state machine for «@name»
vb@577
    30
vb@609
    31
        #include "pEpEngine.h"
vb@952
    32
        #include "message.h"
vb@654
    33
        
vb@654
    34
        #ifdef __cplusplus
vb@654
    35
        extern "C" {
vb@654
    36
        #endif
vb@608
    37
vb@609
    38
        // types
Edouard@605
    39
vb@609
    40
        typedef pEp_identity * Identity;
vb@939
    41
        typedef stringlist_t * Stringlist;
vb@939
    42
        typedef union _param { Identity partner; stringlist_t *keylist; } param_t;
vb@583
    43
vb@609
    44
        // error values
vb@577
    45
vb@609
    46
        typedef enum _fsm_error {
vb@964
    47
            // these error values are corresponding to
vb@964
    48
            // PEP_SYNC_STATEMACHINE_ERROR - value
vb@743
    49
            invalid_state = -2,
vb@959
    50
            invalid_event = -3,
vb@959
    51
            invalid_condition = -4,
vb@964
    52
            invalid_action = -5,
vb@964
    53
vb@964
    54
            // out of memory condition
vb@964
    55
            invalid_out_of_memory = -128
vb@609
    56
        } fsm_error;
vb@583
    57
vb@951
    58
        // conditions
vb@951
    59
vb@959
    60
        `` for "func:distinctName(condition)" | int «@name»(PEP_SESSION session`apply "parm", 0`);
vb@951
    61
vb@609
    62
        // states
vb@578
    63
vb@609
    64
        typedef enum _«@name»_state {
vb@1043
    65
            // error values also in this namespace
vb@1043
    66
            «@name»_state_invalid_state = (int) invalid_state,
vb@1043
    67
            «@name»_state_invalid_event = (int) invalid_event,
vb@1043
    68
            «@name»_state_invalid_condition = (int) invalid_condition,
vb@1043
    69
            «@name»_state_invalid_action = (int) invalid_action,
vb@1043
    70
            «@name»_state_invalid_out_of_memory = (int) invalid_out_of_memory,
vb@1043
    71
vb@951
    72
            «@name»_state_NONE = 0,
vb@623
    73
        `` for "func:distinctName(state)" |> «@name»`if "position()!=last()" > , `
vb@609
    74
        } «@name»_state;
vb@583
    75
vb@609
    76
        // events
vb@578
    77
vb@609
    78
        typedef enum _«@name»_event {
vb@951
    79
            «@name»_event_NONE = 0,
vb@711
    80
        ||
vb@711
    81
        for "func:distinctName(state/event[not(not(/protocol/fsm/tag/@name=@name))])" {
vb@711
    82
            const "name", "@name";
vb@711
    83
            |> «$name» = «/protocol/fsm/tag[@name=$name]/@id»,
vb@711
    84
        }
vb@711
    85
        for "func:distinctName(state/event[not(/protocol/fsm/tag/@name=@name)])"
vb@711
    86
            |> «@name»`if "position()!=last()" > , `
vb@711
    87
        ||
vb@609
    88
        } «@name»_event;
vb@583
    89
vb@609
    90
        // actions
vb@582
    91
vb@690
    92
        `` const "name", "@name"
vb@939
    93
        `` for "func:distinctName(//action)" | PEP_STATUS «@name»(PEP_SESSION session, «$name»_state state, Identity partner, void *extra);
Edouard@605
    94
vb@951
    95
        // message receiver
vb@951
    96
        
vb@952
    97
        PEP_STATUS receive_«@name»_msg(PEP_SESSION session, message *msg);
vb@951
    98
vb@626
    99
        // state machine
vb@626
   100
vb@626
   101
        «@name»_state fsm_«@name»(
vb@627
   102
                PEP_SESSION session,
vb@626
   103
                «@name»_state state,
vb@626
   104
                «@name»_event event,
vb@939
   105
                Identity partner,
vb@951
   106
                void *extra
vb@626
   107
            );
vb@626
   108
vb@609
   109
        // driver
Edouard@605
   110
vb@782
   111
        DYNAMIC_API PEP_STATUS fsm_«@name»_inject(
vb@690
   112
                PEP_SESSION session,
vb@690
   113
                «@name»_event event,
vb@690
   114
                Identity partner,
vb@951
   115
                void *extra
vb@690
   116
            );
Edouard@605
   117
vb@654
   118
        #ifdef __cplusplus
vb@654
   119
        }
vb@654
   120
        #endif
vb@654
   121
vb@609
   122
        ||
vb@711
   123
        }
vb@807
   124
        document "../src/{@filename}_driver.c", "text"
vb@690
   125
        ||
vb@690
   126
        // Driver for «@name» state machine
vb@690
   127
vb@690
   128
        #include <assert.h>
vb@690
   129
        #include "pEp_internal.h"
vb@690
   130
vb@690
   131
vb@743
   132
        DYNAMIC_API PEP_STATUS fsm_«@name»_inject(
vb@690
   133
                PEP_SESSION session,
vb@690
   134
                «@name»_event event,
vb@690
   135
                Identity partner,
vb@951
   136
                void *extra
vb@690
   137
            )
vb@690
   138
        {
vb@951
   139
            assert(session);
vb@951
   140
            if (!session)
vb@951
   141
                return PEP_ILLEGAL_VALUE;
vb@690
   142
vb@964
   143
            «@name»_state state = fsm_«@name»(session,
vb@964
   144
                    session->«@filename»_state, event, partner, extra);
vb@1043
   145
            if (state == «@name»_state_invalid_out_of_memory)
vb@964
   146
                return PEP_OUT_OF_MEMORY;
vb@964
   147
            if (state < 0)
vb@964
   148
                return PEP_SYNC_STATEMACHINE_ERROR - state;
vb@690
   149
vb@964
   150
            session->«@filename»_state = state;
vb@951
   151
            return PEP_STATUS_OK;
vb@690
   152
        }
vb@690
   153
vb@690
   154
        ||
vb@807
   155
        document "../src/{@filename}_fsm.c", "text"
vb@609
   156
        ||
vb@807
   157
        #include "«@filename»_fsm.h"
vb@609
   158
vb@609
   159
        // state machine for «@name»
vb@609
   160
vb@609
   161
        «@name»_state fsm_«@name»(
vb@627
   162
                PEP_SESSION session,
vb@609
   163
                «@name»_state state,
vb@609
   164
                «@name»_event event,
vb@939
   165
                Identity partner,
vb@951
   166
                void *extra
vb@609
   167
            )
vb@609
   168
        {
vb@964
   169
            int cond_result;
vb@959
   170
            PEP_STATUS status = PEP_STATUS_OK;
vb@959
   171
vb@609
   172
            switch (state) {
vb@951
   173
            `` apply "state", 2
vb@609
   174
                default:
vb@743
   175
                    return («@name»_state) invalid_state;
vb@609
   176
            }
vb@609
   177
vb@609
   178
            return state;
vb@577
   179
        }
vb@582
   180
vb@609
   181
        ||
vb@577
   182
    }
vb@577
   183
vb@580
   184
    template "state"
vb@577
   185
    ||
vb@577
   186
    case «@name»:
vb@578
   187
        switch (event) {
vb@578
   188
        `` apply "event", 2
vb@951
   189
            default:
vb@951
   190
                return («../@name»_state) invalid_event;
vb@578
   191
        }
vb@577
   192
        break;
vb@577
   193
vb@577
   194
    ||
vb@578
   195
vb@578
   196
    template "event"
vb@578
   197
    ||
vb@578
   198
    case «@name»:
vb@951
   199
    `` apply "action|transition|condition";
vb@582
   200
    `` if "name(*[position()=last()]) != 'transition'" |> break;
vb@578
   201
    ||
vb@578
   202
vb@582
   203
    template "action" {
vb@582
   204
        indent(0);
vb@959
   205
        > status = «@name»(session, state, 
vb@582
   206
        choose {
vb@582
   207
            when "parm" > «name(parm/*)»
vb@582
   208
            otherwise > NULL
vb@582
   209
        }
vb@939
   210
        > , NULL);\n
vb@964
   211
        | if (status == PEP_OUT_OF_MEMORY)
vb@1043
   212
        |> return (int) invalid_out_of_memory;
vb@959
   213
        | if (status != PEP_STATUS_OK)
vb@1043
   214
        |> return (int) invalid_action;
vb@582
   215
    }
vb@582
   216
vb@951
   217
    template "condition" {
vb@959
   218
        | cond_result = «@name»(session`apply "parm", 0`);
vb@964
   219
        | if (cond_result < 0)
vb@964
   220
        |> return cond_result;
vb@959
   221
        | if (cond_result) {
vb@951
   222
        apply "action|transition|condition";
vb@951
   223
        | }
vb@951
   224
    }
vb@951
   225
vb@951
   226
    template "parm" choose {
vb@951
   227
        when "count(*) = 1"
vb@951
   228
            > , «name(*)»
vb@951
   229
        otherwise
vb@951
   230
            > , «name(*[1])» «name(*[2])»
vb@951
   231
    }
vb@951
   232
vb@578
   233
    template "transition" | return «@target»;
vb@577
   234
}
vb@577
   235