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