sync/gen_statemachine.ysl2
author Volker Birk <vb@pep.foundation>
Fri, 05 Oct 2018 19:56:02 +0200
branchsync
changeset 3020 0b732f55530a
parent 2944 ae169deb521c
child 3062 4ab13f23726f
permissions -rw-r--r--
remove the necessity to install Sync.h
krista@2271
     1
// This file is under GNU General Public License 3.0
krista@2271
     2
// see LICENSE.txt
krista@2271
     3
krista@2271
     4
// generate state machine code
krista@2271
     5
vb@2840
     6
// Copyleft (c) 2016 - 2018, p≡p foundation
krista@2271
     7
krista@2271
     8
// Written by Volker Birk
krista@2271
     9
krista@2271
    10
include yslt.yml2
krista@2271
    11
krista@2271
    12
tstylesheet {
krista@2271
    13
    include standardlib.ysl2
krista@2271
    14
    include ./functions.ysl2
krista@2271
    15
krista@2271
    16
    template "/protocol" {
vb@2838
    17
        document "generated/{@name}_event.h", "text"
vb@2838
    18
        ||
vb@2838
    19
        // This file is under GNU General Public License 3.0
vb@2838
    20
        // see LICENSE.txt
vb@2838
    21
vb@2838
    22
        #pragma once
vb@2838
    23
vb@2838
    24
        #include "dynamic_api.h"
vb@2838
    25
vb@2838
    26
        #ifdef __cplusplus
vb@2838
    27
        extern "C" {
vb@2838
    28
        #endif
vb@2838
    29
vb@3020
    30
        typedef struct «@name» «yml:ucase(@name)»;
vb@3020
    31
        typedef int «yml:ucase(@name)»_PR;
vb@2838
    32
vb@2847
    33
        typedef struct «@name»_event {
vb@3020
    34
            «yml:ucase(@name)»_PR fsm;
vb@2838
    35
            int event;
vb@3020
    36
            «yml:ucase(@name)» *msg;
vb@2838
    37
        } «@name»_event_t;
vb@2838
    38
vb@2838
    39
 
vb@2838
    40
        // new_«@name»_event() - allocate a new «@name»_event
vb@2838
    41
        //
vb@2838
    42
        //  parameters:
vb@2838
    43
        //      fsm (in)        finite state machine the event is for
vb@2838
    44
        //      event (in)      event or None
vb@2838
    45
        //      msg (in)        message to compute event from
vb@2838
    46
        //
vb@2838
    47
        //  return value:
vb@2838
    48
        //      pointer to new event or NULL in case of failure
vb@2838
    49
        //
vb@2838
    50
        //  caveat:
vb@2838
    51
        //      event must be valid for fsm or None
vb@2838
    52
        //      in case msg is given event will be calculated out of message
vb@2838
    53
vb@3020
    54
        «@name»_event_t *new_«@name»_event(«yml:ucase(@name)»_PR fsm, int event, «yml:ucase(@name)» *msg);
vb@2838
    55
vb@2910
    56
        #define «yml:ucase(@name)»_TIMEOUT_EVENT new_«@name»_event(«@name»_PR_NOTHING, 0, NULL);
vb@2910
    57
vb@2838
    58
vb@2838
    59
        // free_«@name»_event() - free memory occupied by event
vb@2838
    60
        //
vb@2838
    61
        //  parameters:
vb@2838
    62
        //      ev (in)         event to free
vb@2838
    63
vb@2899
    64
        void free_«@name»_event(«@name»_event_t *ev);
vb@2838
    65
vb@2838
    66
vb@2838
    67
        #ifdef __cplusplus
vb@2838
    68
        }
vb@2838
    69
        #endif
vb@2838
    70
vb@2838
    71
        ||
vb@2838
    72
vb@2838
    73
        document "generated/{@name}_event.c", "text"
vb@2838
    74
        ||
vb@2838
    75
        // This file is under GNU General Public License 3.0
vb@2838
    76
        // see LICENSE.txt
vb@2838
    77
vb@2838
    78
        #include "pEp_internal.h"
vb@2838
    79
        #include "«@name»_event.h"
vb@2838
    80
        #include "«@name»_func.h"
vb@2839
    81
        `` for "fsm" | #include "«@name»_fsm.h"
vb@2838
    82
vb@3020
    83
        «@name»_event_t *new_«@name»_event(«yml:ucase(@name)»_PR fsm, int event, «@name»_t *msg)
vb@2838
    84
        {
vb@2838
    85
            «@name»_event_t *ev = («@name»_event_t *) calloc(1, sizeof(«@name»_event_t));
vb@2838
    86
            assert(ev);
vb@2838
    87
            if (!ev)
vb@2838
    88
                return NULL;
vb@2838
    89
vb@2838
    90
            ev->fsm = fsm;
vb@2838
    91
            ev->event = event;
vb@2838
    92
            ev->msg = msg;
vb@2838
    93
vb@2838
    94
            if (msg) {
vb@2838
    95
                switch (fsm) {
vb@2838
    96
                    `` apply "fsm", 3, mode=event
vb@2838
    97
                    default:
vb@2838
    98
                        // unknown protocol
vb@2838
    99
                        free(ev);
vb@2838
   100
                        return NULL;
vb@2838
   101
                }
vb@2838
   102
            }
vb@2838
   103
vb@2838
   104
            return ev;
vb@2838
   105
        }
vb@2838
   106
vb@2899
   107
        void free_«@name»_event(«@name»_event_t *ev)
vb@2838
   108
        {
vb@2838
   109
            if (ev) {
vb@2838
   110
                free_«@name»_message(ev->msg);
vb@2838
   111
                free(ev);
vb@2838
   112
            }
vb@2838
   113
        }
vb@2838
   114
vb@2838
   115
        ||
vb@2838
   116
vb@2829
   117
        document "generated/{@name}_impl.h", "text" {
vb@2870
   118
            ||
vb@2870
   119
            // This file is under GNU General Public License 3.0
vb@2870
   120
            // see LICENSE.txt
krista@2271
   121
vb@2870
   122
            #pragma once
krista@2271
   123
vb@2870
   124
            #include "fsm_common.h"
vb@2889
   125
            #include "«@name»_event.h"
vb@2870
   126
            #include "message_api.h"
vb@3020
   127
            #include "../asn.1/«@name».h"
vb@2870
   128
            
vb@2909
   129
            #define «yml:ucase(@name)»_THRESHOLD «@threshold»
vb@2909
   130
            `` for "fsm" | #define «yml:ucase(@name)»_THRESHOLD «@threshold»
vb@2909
   131
vb@2870
   132
            #ifdef __cplusplus
vb@2870
   133
            extern "C" {
vb@2870
   134
            #endif
vb@2870
   135
vb@2870
   136
            // conditions
vb@2870
   137
vb@2870
   138
            ||
vb@2870
   139
            for "func:distinctName(*//condition)"
vb@2870
   140
                | PEP_STATUS «@name»(PEP_SESSION session, bool *result);
vb@2870
   141
            ||
vb@2870
   142
vb@2870
   143
            // actions
vb@2870
   144
vb@2870
   145
            ||
vb@2870
   146
            for "func:distinctName(*//action)"
vb@2870
   147
                | PEP_STATUS «@name»(PEP_SESSION session);
vb@2870
   148
            ||
vb@2870
   149
vb@2914
   150
            // timeout handler
vb@2914
   151
            
vb@2914
   152
            ||
vb@2914
   153
            for "fsm[@threshold > 0]"
vb@2914
   154
                | PEP_STATUS «@name»TimeoutHandler(PEP_SESSION session);
vb@2914
   155
            ||
vb@2914
   156
vb@2870
   157
            // send message about an event to communication partners using state
vb@2870
   158
vb@2870
   159
            PEP_STATUS send_«@name»_message(
vb@2870
   160
                    PEP_SESSION session, 
vb@2870
   161
                    «@name»_PR fsm,
vb@2870
   162
                    int message_type
vb@2870
   163
                );
vb@2870
   164
vb@2870
   165
            // receive message and store it in state
vb@2870
   166
vb@2870
   167
            PEP_STATUS recv_«@name»_event(
vb@2870
   168
                    PEP_SESSION session,
vb@2870
   169
                    «@name»_event_t *ev
vb@2870
   170
                );
krista@2271
   171
        
vb@2870
   172
            // state machine driver
vb@2870
   173
            // if fsm or event set to 0 use fields in src if present
krista@2271
   174
vb@2870
   175
            PEP_STATUS «@name»_driver(
vb@2870
   176
                    PEP_SESSION session,
vb@2870
   177
                    «@name»_PR fsm,
vb@2870
   178
                    int event
vb@2870
   179
                );
krista@2271
   180
vb@2888
   181
            // API being used by the engine internally
vb@2888
   182
vb@2888
   183
            // call this if you need to signal an external event
vb@2888
   184
vb@2880
   185
            PEP_STATUS signal_«@name»_event(
vb@2870
   186
                    PEP_SESSION session, 
vb@2870
   187
                    «@name»_PR fsm,
vb@2870
   188
                    int event
vb@2870
   189
                );
vb@2888
   190
            
vb@2889
   191
            // call this if you are a transport and are receiving
vb@2889
   192
            // a «@name» message
vb@2834
   193
vb@2888
   194
            PEP_STATUS signal_«@name»_message(
vb@2888
   195
                    PEP_SESSION session, 
vb@2888
   196
                    PEP_rating rating,
vb@2888
   197
                    const char *data,
vb@2888
   198
                    size_t size
vb@2888
   199
                );
vb@2829
   200
vb@2870
   201
            #ifdef __cplusplus
vb@2870
   202
            }
vb@2870
   203
            #endif
vb@2829
   204
vb@2870
   205
            ||
vb@2829
   206
        }
vb@2829
   207
vb@2870
   208
        document "generated/{@name}_impl.c", "text" {
vb@2870
   209
            ||
vb@2870
   210
            // This file is under GNU General Public License 3.0
vb@2870
   211
            // see LICENSE.txt
vb@2870
   212
        
vb@2870
   213
            #include "«@name»_impl.h"
vb@2870
   214
            #include "pEp_internal.h"
vb@2870
   215
            #include "«@name»_event.h"
vb@2899
   216
            #include "«yml:lcase(@name)»_codec.h"
vb@2870
   217
            #include "baseprotocol.h"
vb@2870
   218
            `` for "fsm" | #include "«@name»_fsm.h"
vb@2829
   219
vb@2909
   220
            `` apply "fsm", 0, mode=timeout
vb@2870
   221
            PEP_STATUS «@name»_driver(
vb@2870
   222
                    PEP_SESSION session,
vb@2870
   223
                    «@name»_PR fsm,
vb@2870
   224
                    int event
vb@2870
   225
                )
vb@2870
   226
            {
vb@2910
   227
                assert(session);
vb@2910
   228
                if (!session)
vb@2870
   229
                    return PEP_ILLEGAL_VALUE;
vb@2829
   230
vb@2909
   231
                switch (fsm) {
vb@2910
   232
                    case None:
vb@2910
   233
                        if (!event) {
vb@2910
   234
                            // timeout occured
vb@2910
   235
                        `` for "fsm" |>>>> «../@name»_driver(session, «../@name»_PR_«yml:lcase(@name)», None);
vb@2910
   236
                            return PEP_STATUS_OK;
vb@2910
   237
                        }
vb@2910
   238
                        return PEP_ILLEGAL_VALUE;
vb@2910
   239
vb@2909
   240
                    `` apply "fsm", mode=reset_state_machine;
vb@2909
   241
                    default:
vb@2909
   242
                        return PEP_ILLEGAL_VALUE;
vb@2909
   243
                }
vb@2909
   244
vb@2870
   245
                int next_state = None;
vb@2870
   246
                do {
vb@2870
   247
                    switch (fsm) {
vb@2870
   248
                        `` apply "fsm", 3, mode=driver               
vb@2870
   249
                        default:
vb@2870
   250
                            return PEP_ILLEGAL_VALUE;
vb@2870
   251
                    }
vb@2870
   252
                }  while (next_state);
vb@2829
   253
vb@2870
   254
                return PEP_STATUS_OK;
vb@2829
   255
            }
vb@2829
   256
vb@2880
   257
            PEP_STATUS signal_«@name»_event(
vb@2870
   258
                    PEP_SESSION session, 
vb@2870
   259
                    «@name»_PR fsm,
vb@2870
   260
                    int event
vb@2870
   261
                )
vb@2870
   262
            {
vb@2870
   263
                «@name»_t *msg = NULL;
vb@2870
   264
                «@name»_event_t *ev = NULL;
vb@2870
   265
vb@2870
   266
                assert(session && fsm > 0 && event > None);
vb@2870
   267
                if (!(session && fsm > 0 && event > None))
vb@2870
   268
                    return PEP_ILLEGAL_VALUE;
vb@2870
   269
vb@2870
   270
                PEP_STATUS status = PEP_STATUS_OK;
vb@2870
   271
vb@2888
   272
                if (!session->inject_«yml:lcase(@name)»_event)
vb@2888
   273
                   return PEP_«yml:ucase(@name)»_NO_INJECT_CALLBACK;
vb@2870
   274
vb@2870
   275
                if (event < Extra) {
vb@2870
   276
                    msg = new_«@name»_message(fsm, event);
vb@2870
   277
                    if (!msg) {
vb@2870
   278
                        status = PEP_OUT_OF_MEMORY;
vb@2888
   279
                        goto the_end;
vb@2870
   280
                    }
vb@2870
   281
vb@2870
   282
                    status = update_«@name»_message(session, msg);
vb@2870
   283
                    if (status)
vb@2888
   284
                        goto the_end;
vb@2870
   285
                }
vb@2870
   286
vb@2888
   287
                ev = new_«@name»_event(fsm, event, msg);
vb@2870
   288
                if (!ev) {
vb@2870
   289
                    status = PEP_OUT_OF_MEMORY;
vb@2888
   290
                    goto the_end;
vb@2870
   291
                }
vb@2870
   292
vb@2870
   293
                int result = session->inject_«yml:lcase(@name)»_event(ev,
vb@2870
   294
                        session->«yml:lcase(@name)»_management);
vb@2870
   295
                if (result) {
vb@2870
   296
                    status = PEP_STATEMACHINE_ERROR;
vb@2888
   297
                    goto the_end;
vb@2870
   298
                }
vb@2888
   299
                return PEP_STATUS_OK;
vb@2870
   300
vb@2870
   301
            the_end:
vb@2888
   302
                free_«@name»_event(ev);
vb@2888
   303
                free_«@name»_message(msg);
vb@2870
   304
                return status;
vb@2870
   305
            }
vb@2870
   306
vb@2888
   307
            PEP_STATUS signal_«@name»_message(
vb@2870
   308
                    PEP_SESSION session, 
vb@2888
   309
                    PEP_rating rating,
vb@2888
   310
                    const char *data,
vb@2888
   311
                    size_t size
vb@2870
   312
                )
vb@2870
   313
            {
vb@2888
   314
                assert(session && data && size);
vb@2888
   315
                if (!(session && data && size))
vb@2870
   316
                    return PEP_ILLEGAL_VALUE;
vb@2870
   317
vb@2888
   318
                if (!session->inject_«yml:lcase(@name)»_event)
vb@2888
   319
                   return PEP_«yml:ucase(@name)»_NO_INJECT_CALLBACK;
vb@2870
   320
vb@2888
   321
                «@name»_t *msg = NULL;
vb@2888
   322
                PEP_STATUS status = decode_«@name»_message(data, size, &msg);
vb@2888
   323
                if (status)
vb@2888
   324
                    return status;
vb@2888
   325
vb@2890
   326
                «@name»_event_t *ev = NULL;
vb@2890
   327
vb@2890
   328
                switch (msg->present) {
vb@2890
   329
                    `` apply "fsm", 2, mode=signal_message
vb@2890
   330
                    default:
vb@2890
   331
                        status = PEP_«yml:ucase(@name)»_ILLEGAL_MESSAGE;
vb@2890
   332
                        goto the_end;
vb@2890
   333
                }
vb@2890
   334
vb@2890
   335
                ev = new_«@name»_event(None, None, msg);
vb@2888
   336
                if (!ev) {
vb@2829
   337
                    status = PEP_OUT_OF_MEMORY;
vb@2888
   338
                    goto the_end;
vb@2829
   339
                }
vb@2829
   340
vb@2888
   341
                int result = session->inject_«yml:lcase(@name)»_event(ev,
vb@2888
   342
                        session->«yml:lcase(@name)»_management);
vb@2888
   343
                if (result) {
vb@2888
   344
                    status = PEP_STATEMACHINE_ERROR;
vb@2888
   345
                    goto the_end;
vb@2888
   346
                }
vb@2888
   347
                return PEP_STATUS_OK;
vb@2870
   348
vb@2870
   349
            the_end:
vb@2888
   350
                free_«@name»_event(ev);
vb@2888
   351
                free_«@name»_message(msg);
vb@2870
   352
                return status;
vb@2829
   353
            }
vb@2829
   354
vb@2870
   355
            PEP_STATUS send_«@name»_message(
vb@2870
   356
                    PEP_SESSION session, 
vb@2870
   357
                    «@name»_PR fsm,
vb@2870
   358
                    int message_type
vb@2870
   359
                )
vb@2870
   360
            {
vb@2870
   361
                PEP_STATUS status = PEP_STATUS_OK;
vb@2829
   362
vb@2870
   363
                assert(session && fsm > None && message_type > None);
vb@2870
   364
                if (!(session && fsm > None && message_type > None))
vb@2870
   365
                    return PEP_ILLEGAL_VALUE;
vb@2870
   366
                
vb@2870
   367
                «@name»_t *msg = new_«@name»_message(fsm, message_type);
vb@2870
   368
                if (!msg)
vb@2870
   369
                    return PEP_OUT_OF_MEMORY;
vb@2870
   370
vb@2870
   371
                char *data = NULL;
vb@2870
   372
                message *m = NULL;
vb@2870
   373
                identity_list *channels = NULL;
vb@2870
   374
vb@2870
   375
                status = update_«@name»_message(session, msg);
vb@2870
   376
                if (status)
vb@2870
   377
                    goto the_end;
vb@2870
   378
vb@2870
   379
                size_t size = 0;
vb@2870
   380
                status = encode_«@name»_message(msg, &data, &size);
vb@2870
   381
                if (status)
vb@2870
   382
                    goto the_end;
vb@2870
   383
vb@2870
   384
                switch (message_type) {
vb@2871
   385
                    // these messages are being broadcasted
vb@2877
   386
                    `` for "fsm/message[@type='broadcast']" |>> case «../@name»__payload_PR_«yml:mixedCase(@name)»:
vb@2871
   387
                        status = _own_identities_retrieve(session, &channels, PEP_idf_not_for_«yml:lcase(@name)»);
vb@2875
   388
                        if (status)
vb@2870
   389
                            goto the_end;
vb@2871
   390
vb@2871
   391
                        if (!(channels && channels->ident)) {
vb@2871
   392
                            status = PEP_«yml:ucase(@name)»_NO_CHANNEL;
vb@2871
   393
                            goto the_end;
vb@2871
   394
                        }
vb@2870
   395
                        break;
vb@2870
   396
vb@2877
   397
                    // these go anycast; previously used address is sticky (unicast)
vb@2877
   398
                    `` for "fsm/message[@type='anycast']" |>> case «../@name»__payload_PR_«yml:mixedCase(@name)»:
vb@2870
   399
                        if (!session->«yml:lcase(@name)»_state.common.from `> |`|
vb@2870
   400
                            (session->«yml:lcase(@name)»_state.common.from->flags &
vb@2871
   401
                            PEP_idf_not_for_«yml:lcase(@name)»)) {
vb@2870
   402
vb@2870
   403
                            // no address available yet, try to find one
vb@2871
   404
                            status = _own_identities_retrieve(session, &channels, PEP_idf_not_for_«yml:lcase(@name)»);
vb@2870
   405
                            if (!status)
vb@2870
   406
                                goto the_end;
vb@2870
   407
                            break;
vb@2870
   408
vb@2870
   409
                            if (channels && channels->ident) {
vb@2870
   410
                                // only need the first one
vb@2870
   411
                                free_identity_list(channels->next);
vb@2870
   412
                                channels->next = NULL;
vb@2870
   413
                            }
vb@2870
   414
                            else {
vb@2871
   415
                                status = PEP_«yml:ucase(@name)»_NO_CHANNEL;
vb@2870
   416
                                goto the_end;
vb@2870
   417
                            }
vb@2870
   418
                        }
vb@2870
   419
                        else {
vb@2870
   420
                            pEp_identity *channel = identity_dup(session->«yml:lcase(@name)»_state.common.from);
vb@2870
   421
                            if (!channel) {
vb@2870
   422
                                status = PEP_OUT_OF_MEMORY;
vb@2870
   423
                                goto the_end;
vb@2870
   424
                            }
vb@2870
   425
vb@2870
   426
                            channels = new_identity_list(channel);
vb@2870
   427
                            if (!channels) {
vb@2870
   428
                                status = PEP_OUT_OF_MEMORY;
vb@2870
   429
                                goto the_end;
vb@2870
   430
                            }
vb@2870
   431
                        }
vb@2877
   432
vb@2877
   433
                    default:
vb@2877
   434
                        status = PEP_«yml:ucase(@name)»_ILLEGAL_MESSAGE;
vb@2877
   435
                        goto the_end;
vb@2870
   436
                }
vb@2870
   437
vb@2870
   438
                for (identity_list *li = channels; li && li->ident ; li = li->next) {
vb@2878
   439
                    message *_m = NULL;
vb@2944
   440
                    char *_data = NULL;
vb@2944
   441
                    
vb@2944
   442
                    _data = malloc(size);
vb@2944
   443
                    assert(_data);
vb@2944
   444
                    if (!_data) {
vb@2944
   445
                        status = PEP_OUT_OF_MEMORY;
vb@2944
   446
                        goto the_end;
vb@2944
   447
                    }
vb@2944
   448
                    memcpy(_data, data, size);
vb@2870
   449
                    status = base_prepare_message(
vb@2870
   450
                            li->ident,
vb@2870
   451
                            li->ident,
vb@2944
   452
                            _data,
vb@2870
   453
                            size,
vb@2878
   454
                            &_m
vb@2870
   455
                        );
vb@2944
   456
                    if (status) {
vb@2944
   457
                        free(_data);
vb@2870
   458
                        goto the_end;
vb@2944
   459
                    }
vb@2875
   460
vb@2878
   461
                    switch (message_type) {
vb@2878
   462
                    `` for "fsm/message[@security='unencrypted']" |>>> case «../@name»__payload_PR_«yml:mixedCase(@name)»:
vb@2878
   463
                            m = _m;
vb@2878
   464
                            break;
vb@2878
   465
vb@2878
   466
                        default:
vb@2878
   467
                            status = encrypt_message(session, _m, NULL, &m, PEP_enc_PEP, 0);
vb@2879
   468
                            if (status) {
vb@2899
   469
                                status = PEP_«yml:ucase(@name)»_CANNOT_ENCRYPT;
vb@2878
   470
                                goto the_end;
vb@2879
   471
                            }
vb@2878
   472
                            free_message(_m);
vb@2878
   473
                    }
vb@2878
   474
vb@2899
   475
                    status = session->messageToSend(m);
vb@2870
   476
                    m = NULL;
vb@2870
   477
                }
vb@2870
   478
vb@2870
   479
            the_end:
vb@2870
   480
                free_identity_list(channels);
vb@2870
   481
                free_message(m);
vb@2870
   482
                free(data);
vb@2870
   483
                free_«@name»_message(msg);
vb@2870
   484
                return status;
vb@2829
   485
            }
vb@2829
   486
vb@2870
   487
            PEP_STATUS recv_«@name»_event(
vb@2870
   488
                    PEP_SESSION session,
vb@2870
   489
                    «@name»_event_t *ev
vb@2870
   490
                )
vb@2870
   491
            {
vb@2870
   492
                assert(session && ev);
vb@2870
   493
                if (!(session && ev))
vb@2870
   494
                    return PEP_ILLEGAL_VALUE;
vb@2829
   495
vb@2870
   496
                PEP_STATUS status = PEP_STATUS_OK;
vb@2888
   497
                «@name»_PR fsm = (int) None;
vb@2888
   498
                int event = None;
vb@2829
   499
vb@2910
   500
                if (ev->event > None && ev->event < Extra) {
vb@2870
   501
                    status = update_«@name»_state(session, ev->msg, &fsm, &event);
vb@2870
   502
                    if (status)
vb@2888
   503
                        goto the_end;
vb@2829
   504
vb@2870
   505
                    if (ev->fsm) {
vb@2870
   506
                        if (ev->fsm != fsm |`> |` ev->event != event) {
vb@2870
   507
                            status = PEP_«yml:ucase(@name)»_ILLEGAL_MESSAGE;
vb@2888
   508
                            goto the_end;
vb@2870
   509
                        }
vb@2870
   510
                    }
vb@2888
   511
                    else if (ev->event) {
vb@2888
   512
                        status = PEP_«yml:ucase(@name)»_ILLEGAL_MESSAGE;
vb@2888
   513
                        goto the_end;
vb@2870
   514
                    }
vb@2870
   515
                }
vb@2888
   516
                else {
vb@2888
   517
                    fsm = ev->fsm;
vb@2888
   518
                    event = ev->event;
vb@2888
   519
                }
vb@2829
   520
vb@2888
   521
                status = «@name»_driver(session, fsm, event);
vb@2870
   522
vb@2888
   523
            the_end:
vb@2888
   524
                free_«@name»_event(ev);
vb@2870
   525
                return status;
vb@2829
   526
            }
vb@2829
   527
vb@2870
   528
            ||
vb@2829
   529
        }
vb@2829
   530
vb@2829
   531
        apply "fsm", 0, mode=gen;
vb@2829
   532
    }
vb@2829
   533
vb@2909
   534
    template "fsm", mode=timeout
vb@2909
   535
    ||
vb@2909
   536
    static bool _«@name»_timeout(int state)
vb@2909
   537
    {
vb@2909
   538
        static int last_state = None;
vb@2909
   539
        static time_t switch_time = 0;
vb@2909
   540
vb@2909
   541
        if (state > Init) {
vb@2909
   542
            if (state == last_state) {
vb@2909
   543
                if (time(NULL) - switch_time > «yml:ucase(@name)»_THRESHOLD) {
vb@2909
   544
                    last_state = None;
vb@2909
   545
                    switch_time = 0;
vb@2909
   546
                    return true;
vb@2909
   547
                }
vb@2909
   548
            }
vb@2909
   549
            else {
vb@2909
   550
                last_state = state;
vb@2909
   551
                switch_time = time(NULL);
vb@2909
   552
            }
vb@2909
   553
        }
vb@2909
   554
        else {
vb@2909
   555
            last_state = None;
vb@2909
   556
            switch_time = 0;
vb@2909
   557
        }
vb@2909
   558
vb@2909
   559
        return false;
vb@2909
   560
    }
vb@2909
   561
vb@2909
   562
    ||
vb@2909
   563
vb@2909
   564
    template "fsm", mode=reset_state_machine
vb@2909
   565
    ||
vb@2909
   566
        case «../@name»_PR_«yml:lcase(@name)»: {
vb@2909
   567
            int state = session->«yml:lcase(../@name)»_state.«yml:lcase(@name)».state;
vb@2909
   568
            switch (state) {
vb@2909
   569
                `` for "state[@name!='InitState' and @timeout != 'off']" |>>> case «@name»:
vb@2909
   570
                    if (_«@name»_timeout(state)) {
vb@2909
   571
                        session->«yml:lcase(../@name)»_state.«yml:lcase(@name)».state = Init;
vb@2909
   572
                        event = Init;
vb@2914
   573
                        `` if "@threshold > 0" |>>>>> «@name»TimeoutHandler(session);
vb@2909
   574
                    }
vb@2909
   575
                    break;
vb@2909
   576
                
vb@2909
   577
                default:
vb@2909
   578
                    _«@name»_timeout(None);
vb@2909
   579
            }
vb@2909
   580
            break;
vb@2909
   581
        }
vb@2909
   582
vb@2909
   583
    ||
vb@2909
   584
vb@2890
   585
    template "fsm", mode=signal_message
vb@2890
   586
    {
vb@2890
   587
        ||
vb@2890
   588
        case «../@name»_PR_«yml:lcase(@name)»:
vb@2890
   589
            switch (msg->choice.«yml:lcase(@name)».payload.present) {
vb@2891
   590
        ||
vb@2891
   591
        if "message[@security='unencrypted']" {
vb@2891
   592
            |         // these messages are going untested
vb@2891
   593
            for "message[@security='unencrypted']"
vb@2891
   594
                |>> case «../@name»__payload_PR_«yml:mixedCase(@name)»:
vb@2891
   595
            ||
vb@2891
   596
                       break;
vb@2890
   597
vb@2891
   598
            ||
vb@2891
   599
        }
vb@2891
   600
        if "message[@security='untrusted']"
vb@2891
   601
        ||
vb@2890
   602
                // these messages must arrive encrypted
vb@2890
   603
        `` for "message[@security='untrusted']" |>> case «../@name»__payload_PR_«yml:mixedCase(@name)»:
vb@2890
   604
                    if (rating < PEP_rating_reliable) {
vb@2890
   605
                        status = PEP_«yml:ucase(ancestor::protocol/@name)»_ILLEGAL_MESSAGE;
vb@2890
   606
                        goto the_end;
vb@2890
   607
                    }
vb@2890
   608
                    break;
vb@2890
   609
vb@2891
   610
        ||
vb@2891
   611
        if "message[@security='trusted']"
vb@2891
   612
        ||
vb@2890
   613
                // these messages must come through a trusted channel
vb@2890
   614
        `` for "message[@security='trusted']" |>> case «ancestor::fsm/@name»__payload_PR_«yml:mixedCase(@name)»:
vb@2890
   615
                    if (rating < PEP_rating_trusted) {
vb@2890
   616
                        status = PEP_«yml:ucase(ancestor::protocol/@name)»_ILLEGAL_MESSAGE;
vb@2890
   617
                        goto the_end;
vb@2890
   618
                    }
vb@2890
   619
                    break;
vb@2890
   620
vb@2890
   621
                default:
vb@2890
   622
                    status = PEP_«yml:ucase(ancestor::protocol/@name)»_ILLEGAL_MESSAGE;
vb@2890
   623
                    goto the_end;
vb@2890
   624
            }
vb@2890
   625
            break;
vb@2890
   626
vb@2890
   627
        ||
vb@2890
   628
    }
vb@2890
   629
vb@2838
   630
    template "fsm", mode=event
vb@2838
   631
    {
vb@2838
   632
    ||
vb@2843
   633
    case «../@name»_PR_«yml:lcase(@name)»: {
vb@2839
   634
        switch (msg->choice.«yml:lcase(@name)».payload.present) {
vb@2838
   635
    ||
vb@2838
   636
    for "message"
vb@2838
   637
    ||
vb@2838
   638
            case «../@name»__payload_PR_«yml:mixedCase(@name)»:
vb@2838
   639
                ev->event = «@name»;
vb@2838
   640
                break;
vb@2838
   641
    ||
vb@2838
   642
    ||
vb@2838
   643
            default:
vb@2838
   644
                // unknown message type
vb@2838
   645
                free(ev);
vb@2838
   646
                return NULL;
vb@2838
   647
        }
vb@2838
   648
        break;
vb@2838
   649
    }
vb@2838
   650
vb@2838
   651
    ||
vb@2838
   652
    }
vb@2838
   653
vb@2829
   654
    template "fsm", mode=driver
vb@2829
   655
    ||
vb@2843
   656
    case «../@name»_PR_«yml:lcase(@name)»: {
vb@2839
   657
        int state = session->«yml:lcase(../@name)»_state.«yml:lcase(@name)».state;
vb@2839
   658
        next_state = fsm_«@name»(session, state, event);
vb@2838
   659
        if (next_state > None) {
vb@2838
   660
            session->«yml:lcase(../@name)»_state.«yml:lcase(@name)».state = next_state;
vb@2838
   661
            event = Init;
vb@2838
   662
        }
vb@2838
   663
        else if (next_state < None) {
vb@2829
   664
            return PEP_STATEMACHINE_ERROR - state;
vb@2838
   665
        }
vb@2829
   666
        break;
vb@2829
   667
    }
vb@2829
   668
vb@2829
   669
    ||
vb@2829
   670
vb@2829
   671
    template "fsm", mode=gen {
vb@2829
   672
        document "generated/{@name}_fsm.h", "text" {
vb@2829
   673
        ||
vb@2829
   674
        // This file is under GNU General Public License 3.0
vb@2829
   675
        // see LICENSE.txt
vb@2829
   676
vb@2829
   677
        #pragma once
vb@2829
   678
vb@2829
   679
        #include "«../@name»_impl.h"
vb@2829
   680
vb@2829
   681
        #ifdef __cplusplus
vb@2829
   682
        extern "C" {
vb@2829
   683
        #endif
vb@2829
   684
vb@2829
   685
        // state machine for «@name»
krista@2271
   686
krista@2271
   687
        // states
krista@2271
   688
krista@2271
   689
        typedef enum _«@name»_state {
vb@2829
   690
            «@name»_state_None = None,
vb@2829
   691
            «@name»_state_Init = Init,
vb@2829
   692
        ||
vb@2829
   693
        for "func:distinctName(state[not(@name='InitState')])"
vb@2829
   694
            |> «@name»`if "position()!=last()" > , `
vb@2829
   695
        ||
krista@2271
   696
        } «@name»_state;
krista@2271
   697
krista@2271
   698
        // events
krista@2271
   699
krista@2271
   700
        typedef enum _«@name»_event {
vb@2829
   701
            «@name»_event_None = None,
vb@2829
   702
            «@name»_event_Init = Init,
krista@2271
   703
        ||
vb@2829
   704
        for "func:distinctName(state/event[not(not(../../message/@name=@name))])" {
krista@2271
   705
            const "name", "@name";
vb@2829
   706
            |> «$name» = «/protocol/fsm/message[@name=$name]/@id»,
krista@2271
   707
        }
vb@2830
   708
        for "func:distinctName(state/event[not(not(../../external/@name=@name))])" {
vb@2830
   709
            const "name", "@name";
vb@2830
   710
            |> «$name» = «/protocol/fsm/external[@name=$name]/@id»,
vb@2830
   711
        }
vb@2829
   712
        |> «@name»_event_Extra = Extra,
vb@2830
   713
        for "func:distinctName(state/event[not(../../message/@name=@name or ../../external/@name=@name)])" {
vb@2829
   714
            if "@name!='Init'"
vb@2829
   715
                |> «@name»`if "position()!=last()" > , `
vb@2829
   716
        }
krista@2271
   717
        ||
krista@2271
   718
        } «@name»_event;
krista@2271
   719
vb@2829
   720
        // state machine
krista@2271
   721
vb@2829
   722
        const char *«@name»_state_name(int state);
vb@2881
   723
        const char *«@name»_event_name(int event);
krista@2271
   724
vb@2838
   725
        // the state machine function is returning the next state in case of a
vb@2838
   726
        // transition or None for staying
vb@2838
   727
krista@2271
   728
        «@name»_state fsm_«@name»(
krista@2271
   729
                PEP_SESSION session,
krista@2271
   730
                «@name»_state state,
vb@2829
   731
                «@name»_event event
krista@2271
   732
            );
krista@2271
   733
krista@2271
   734
        #ifdef __cplusplus
krista@2271
   735
        }
krista@2271
   736
        #endif
krista@2271
   737
krista@2271
   738
        ||
krista@2271
   739
        }
vb@2829
   740
vb@2829
   741
        document "generated/{@name}_fsm.c", "text" {
krista@2271
   742
        ||
vb@2829
   743
        // This file is under GNU General Public License 3.0
vb@2829
   744
        // see LICENSE.txt
krista@2271
   745
vb@2829
   746
        #include "«@name»_fsm.h"
vb@2847
   747
        #include <stdlib.h>
krista@2271
   748
vb@2829
   749
        const char *«@name»_state_name(int state)
vb@2829
   750
        {
vb@2829
   751
            switch (state) {
vb@2829
   752
                case End:
vb@2829
   753
                    return "End";
vb@2829
   754
                case None:
vb@2829
   755
                    return "None";
vb@2829
   756
                case Init:
vb@2829
   757
                    return "InitState";
vb@2829
   758
        ||
vb@2829
   759
        for "func:distinctName(state[not(@name='InitState')])" {
vb@2829
   760
            |>> case «@name»:
vb@2829
   761
            |>>> return "«@name»";
vb@2829
   762
        }
vb@2829
   763
        ||
vb@2829
   764
                default:
vb@2829
   765
                    return "unknown state";
vb@2829
   766
            }
vb@2829
   767
        }
krista@2271
   768
vb@2881
   769
        const char *«@name»_event_name(int event)
vb@2881
   770
        {
vb@2881
   771
            switch (event) {
vb@2881
   772
                case None:
vb@2881
   773
                    return "None";
vb@2881
   774
                case Init:
vb@2881
   775
                    return "Init";
vb@2881
   776
        ||
vb@2881
   777
        for "func:distinctName(state/event[not(@name='Init')])" {
vb@2881
   778
            |>> case «@name»:
vb@2881
   779
            |>>> return "«@name»";
vb@2881
   780
        }
vb@2881
   781
        ||
vb@2881
   782
                default:
vb@2881
   783
                    return "unknown event";
vb@2881
   784
            }
vb@2881
   785
        }
vb@2881
   786
vb@2881
   787
vb@2829
   788
        static char *_str(int n, bool hex)
vb@2829
   789
        {
vb@2829
   790
            char *buf = calloc(1, 24);
vb@2829
   791
            assert(buf);
vb@2829
   792
            if (!buf)
vb@2829
   793
                return NULL;
vb@2829
   794
vb@2829
   795
            if (hex)
vb@2829
   796
                snprintf(buf, 24, "%.4x", n);
vb@2829
   797
            else
vb@2829
   798
                snprintf(buf, 24, "%d", n);
vb@2829
   799
            return buf;
vb@2829
   800
        }
vb@2829
   801
vb@2829
   802
        #define «@name»_ERR_LOG(t, d) log_event(session, (t), "«@name»", (d), "error")
vb@2829
   803
vb@2829
   804
        static PEP_STATUS _«@name»_ERR_LOG_int(PEP_SESSION session, char *t, int n, bool hex)
vb@2829
   805
        {
vb@2829
   806
            char *_buf = _str(n, hex);
vb@2829
   807
            if (!_buf)
vb@2829
   808
                return PEP_OUT_OF_MEMORY;
vb@2829
   809
            PEP_STATUS status = «@name»_ERR_LOG(t, _buf);
vb@2829
   810
            free(_buf);
vb@2829
   811
            return status;
vb@2829
   812
        }
vb@2829
   813
vb@2829
   814
        #define «@name»_ERR_LOG_INT(t, n) _«@name»_ERR_LOG_int(session, (t), (n), false)
vb@2829
   815
        #define «@name»_ERR_LOG_HEX(t, n) _«@name»_ERR_LOG_int(session, (t), (n), true)
vb@2829
   816
vb@2829
   817
        #ifndef SERVICE_LOG
vb@2829
   818
        // SERVICE LOG is meant to check session->service_log in runtime config;
vb@2829
   819
        // for older engines log more than needed
vb@2829
   820
        #define SERVICE_LOG(session, t, n, d) log_event((session), (t), (n), (d), "service")
vb@2829
   821
        #endif 
vb@2829
   822
vb@2829
   823
        #define «@name»_SERVICE_LOG(t, d) SERVICE_LOG(session, (t), "«@name»", (d))
vb@2829
   824
vb@2829
   825
        «@name»_state fsm_«@name»(
krista@2271
   826
                PEP_SESSION session,
vb@2829
   827
                «@name»_state state,
vb@2829
   828
                «@name»_event event
krista@2271
   829
            )
krista@2271
   830
        {
krista@2271
   831
            assert(session);
krista@2271
   832
            if (!session)
vb@2829
   833
                return invalid_state;
krista@2271
   834
krista@2271
   835
            switch (state) {
vb@2829
   836
                case None:
vb@2838
   837
                    return «@name»_state_Init;
vb@2829
   838
                
vb@2829
   839
                `` apply "state", 2, mode=fsm
krista@2271
   840
                default:
vb@2838
   841
                    «@name»_ERR_LOG_INT("invalid state", state);
vb@2829
   842
                    return invalid_state;
krista@2271
   843
            }
vb@2829
   844
            
vb@2838
   845
            return None;
krista@2271
   846
        }
krista@2271
   847
krista@2271
   848
        ||
vb@2829
   849
        }
vb@2829
   850
    }
vb@2829
   851
    
vb@2829
   852
    template "state", mode=fsm {
vb@2829
   853
        choose {
vb@2829
   854
            when "@name='InitState'" | case «../@name»_state_Init:
vb@2829
   855
            otherwise | case «@name»:
vb@2829
   856
        }
vb@2829
   857
        ||
vb@2829
   858
            «../@name»_SERVICE_LOG("in state", "«@name»");
vb@2829
   859
vb@2829
   860
            switch (event) {
vb@2829
   861
                case None:
vb@2829
   862
                    «../@name»_SERVICE_LOG("received None event", "ignoring");
vb@2838
   863
                    break;
vb@2829
   864
     
vb@2838
   865
        ||
vb@2838
   866
        if "not(event[@name='Init'])"
vb@2838
   867
        ||
vb@2838
   868
                case Init:
vb@2838
   869
                    // nothing to do
vb@2838
   870
                    break;
vb@2838
   871
vb@2838
   872
        ||
vb@2838
   873
        ||
vb@2829
   874
                `` apply "event", 2, mode=fsm
vb@2829
   875
                default:
vb@2885
   876
                    // ignore events not handled here
vb@2885
   877
                    «../@name»_SERVICE_LOG("ignoring event", KeySync_event_name(event));
vb@2885
   878
                    break;
vb@2829
   879
            }
vb@2829
   880
            break;
vb@2829
   881
vb@2829
   882
        ||
krista@2271
   883
    }
krista@2271
   884
vb@2829
   885
    template "event", mode=fsm {
vb@2829
   886
        | case «@name»: {
vb@2865
   887
        if "condition|action|send" |> PEP_STATUS status;
vb@2829
   888
        if "condition" |> bool result = false;
vb@2865
   889
        if "condition|action|send" |
krista@2271
   890
        ||
vb@2829
   891
            «../../@name»_SERVICE_LOG("received event", "«@name»");
vb@2907
   892
            `` apply "transition|action|condition|else|send";
vb@2829
   893
        ||
vb@2829
   894
        if "name(*[last()])!='transition'" {
vb@2829
   895
            |
vb@2843
   896
            |> «../../@name»_SERVICE_LOG("remaining in state", "«../@name»");
vb@2829
   897
            |> break;
vb@2829
   898
        }
vb@2829
   899
        ||
vb@2829
   900
        }
vb@2829
   901
        
vb@2829
   902
        ||
vb@2829
   903
    }
vb@2829
   904
vb@2829
   905
    template "transition" {
vb@2865
   906
        const "fsm", "ancestor::fsm";
krista@2271
   907
        ||
krista@2271
   908
vb@2829
   909
        «$fsm/@name»_SERVICE_LOG("transition to state", "«@target»");
vb@2838
   910
        return «@target»;
krista@2271
   911
        ||
vb@2829
   912
    }
krista@2271
   913
vb@2865
   914
    template "send" {
vb@2865
   915
        const "fsm", "ancestor::fsm";
vb@2865
   916
        const "protocol", "ancestor::protocol";
vb@2865
   917
        ||
vb@2865
   918
vb@2865
   919
        «$fsm/@name»_SERVICE_LOG("send message", "«@name»");
vb@2865
   920
        status = send_«$protocol/@name»_message(session, «$fsm/@id», «$fsm/@name»__payload_PR_«yml:mixedCase(@name)»);
vb@2865
   921
        ||
vb@2865
   922
    }
vb@2865
   923
vb@2829
   924
    template "action" {
vb@2865
   925
        const "fsm", "ancestor::fsm";
vb@2865
   926
        ||
vb@2829
   927
vb@2865
   928
        «$fsm/@name»_SERVICE_LOG("do action", "«@name»");
vb@2865
   929
        status = «@name»(session);
vb@2829
   930
        if (status) {
vb@2829
   931
            «$fsm/@name»_ERR_LOG_HEX("executing action «@name»() failed", status);
vb@2829
   932
            return invalid_action;
krista@2271
   933
        }
krista@2271
   934
        ||
krista@2271
   935
    }
krista@2271
   936
vb@2829
   937
    template "condition" {
vb@2865
   938
        const "fsm", "ancestor::fsm";
krista@2271
   939
        ||
krista@2271
   940
vb@2829
   941
        status = «@name»(session, &result);
vb@2829
   942
        if (status) {
vb@2829
   943
            «$fsm/@name»_ERR_LOG_HEX("computing condition «@name» failed", status);
vb@2829
   944
            return invalid_condition;
krista@2271
   945
        }
vb@2829
   946
        if (result) {
vb@2843
   947
            «$fsm/@name»_SERVICE_LOG("condition applies", "«@name»");
krista@2271
   948
        ||
vb@2907
   949
        apply "transition|action|condition|else|send";
vb@2907
   950
        | }
vb@2907
   951
    }
vb@2907
   952
vb@2907
   953
    template "else" {
vb@2907
   954
        if "not(name(preceding-sibling::*[last()]) = 'condition')"
vb@2907
   955
            error "else without if";
vb@2907
   956
vb@2907
   957
        | else {
vb@2907
   958
        |> «ancestor::fsm/@name»_SERVICE_LOG("condition does not apply", "«preceding-sibling::*[last()]/@name»");
vb@2907
   959
        apply "transition|action|condition|else|send";
krista@2271
   960
        | }
krista@2271
   961
    }
krista@2271
   962
}
krista@2271
   963