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