sync/gen_statemachine.ysl2
author Edouard Tisserant <edouard@pep-project.org>
Sun, 11 Sep 2016 22:40:59 +0200
branchkeysync
changeset 1157 7d383681ef2f
parent 1152 fde1a69448d2
child 1162 f8db5c47b7f3
permissions -rw-r--r--
sync_driver now is responsible to duplicate partner identity, and transmit ownership to each state invoked. fsm_DeviceState_inject doesn't take ownership anymore
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
edouard@1152
   142
            while(true)
edouard@1152
   143
            {
edouard@1157
   144
                Identity _partner = partner ? identity_dup(partner) : NULL;
edouard@1157
   145
edouard@1152
   146
                «@name»_state new_state = fsm_«@name»(session,
edouard@1157
   147
                    session->«@filename»_state, event, _partner, extra);
edouard@1157
   148
edouard@1152
   149
                if (new_state == «@name»_state_invalid_out_of_memory)
edouard@1152
   150
                    return PEP_OUT_OF_MEMORY;
edouard@1157
   151
edouard@1152
   152
                if (new_state < 0)
edouard@1152
   153
                    return PEP_SYNC_STATEMACHINE_ERROR - new_state;
edouard@1152
   154
                
edouard@1157
   155
                if (new_state == session->«@filename»_state)
edouard@1152
   156
                    break;
vb@690
   157
edouard@1157
   158
                event = Init;
edouard@1157
   159
                extra = NULL;
edouard@1152
   160
                session->«@filename»_state = new_state;
edouard@1152
   161
            } 
edouard@1152
   162
vb@951
   163
            return PEP_STATUS_OK;
vb@690
   164
        }
vb@690
   165
vb@690
   166
        ||
vb@807
   167
        document "../src/{@filename}_fsm.c", "text"
vb@609
   168
        ||
vb@807
   169
        #include "«@filename»_fsm.h"
vb@609
   170
vb@609
   171
        // state machine for «@name»
vb@609
   172
vb@609
   173
        «@name»_state fsm_«@name»(
vb@627
   174
                PEP_SESSION session,
vb@609
   175
                «@name»_state state,
vb@609
   176
                «@name»_event event,
vb@939
   177
                Identity partner,
vb@951
   178
                void *extra
vb@609
   179
            )
vb@609
   180
        {
vb@964
   181
            int cond_result;
vb@959
   182
            PEP_STATUS status = PEP_STATUS_OK;
vb@959
   183
vb@609
   184
            switch (state) {
vb@951
   185
            `` apply "state", 2
vb@609
   186
                default:
vb@743
   187
                    return («@name»_state) invalid_state;
vb@609
   188
            }
vb@609
   189
vb@609
   190
            return state;
vb@577
   191
        }
vb@582
   192
vb@609
   193
        ||
vb@577
   194
    }
vb@577
   195
vb@580
   196
    template "state"
vb@577
   197
    ||
vb@577
   198
    case «@name»:
vb@578
   199
        switch (event) {
edouard@1157
   200
        `` if "not(event[@name='Init'])" |>> case Init: break;
vb@578
   201
        `` apply "event", 2
vb@951
   202
            default:
vb@951
   203
                return («../@name»_state) invalid_event;
vb@578
   204
        }
vb@577
   205
        break;
vb@577
   206
vb@577
   207
    ||
vb@578
   208
vb@578
   209
    template "event"
vb@578
   210
    ||
vb@578
   211
    case «@name»:
vb@951
   212
    `` apply "action|transition|condition";
vb@582
   213
    `` if "name(*[position()=last()]) != 'transition'" |> break;
vb@578
   214
    ||
vb@578
   215
vb@582
   216
    template "action" {
vb@582
   217
        indent(0);
vb@959
   218
        > status = «@name»(session, state, 
vb@582
   219
        choose {
vb@582
   220
            when "parm" > «name(parm/*)»
vb@582
   221
            otherwise > NULL
vb@582
   222
        }
vb@939
   223
        > , NULL);\n
vb@964
   224
        | if (status == PEP_OUT_OF_MEMORY)
vb@1043
   225
        |> return (int) invalid_out_of_memory;
vb@959
   226
        | if (status != PEP_STATUS_OK)
vb@1043
   227
        |> return (int) invalid_action;
vb@582
   228
    }
vb@582
   229
vb@951
   230
    template "condition" {
vb@959
   231
        | cond_result = «@name»(session`apply "parm", 0`);
vb@964
   232
        | if (cond_result < 0)
vb@964
   233
        |> return cond_result;
vb@959
   234
        | if (cond_result) {
vb@951
   235
        apply "action|transition|condition";
vb@951
   236
        | }
vb@951
   237
    }
vb@951
   238
vb@951
   239
    template "parm" choose {
vb@951
   240
        when "count(*) = 1"
vb@951
   241
            > , «name(*)»
vb@951
   242
        otherwise
vb@951
   243
            > , «name(*[1])» «name(*[2])»
vb@951
   244
    }
vb@951
   245
vb@578
   246
    template "transition" | return «@target»;
vb@577
   247
}
vb@577
   248