sync/gen_statemachine.ysl2
author Volker Birk <vb@pep.foundation>
Wed, 22 Aug 2018 15:28:03 +0200
branchsync
changeset 2869 e06e7024a1cd
parent 2868 d812e7a24b52
child 2870 9c0567977ed2
permissions -rw-r--r--
...
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@2838
    54
        DYNAMIC_API «@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@2838
    62
        DYNAMIC_API 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@2838
    81
        DYNAMIC_API «@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@2838
   109
        DYNAMIC_API 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@2829
   120
        ||
vb@2829
   121
        // This file is under GNU General Public License 3.0
vb@2829
   122
        // see LICENSE.txt
krista@2271
   123
krista@2271
   124
        #pragma once
krista@2271
   125
vb@2829
   126
        #include "fsm_common.h"
krista@2271
   127
        #include "message_api.h"
vb@2839
   128
        #include "«@name»_event.h"
krista@2271
   129
        
krista@2271
   130
        #ifdef __cplusplus
krista@2271
   131
        extern "C" {
krista@2271
   132
        #endif
krista@2271
   133
krista@2271
   134
        // conditions
krista@2271
   135
vb@2829
   136
        ||
vb@2829
   137
        for "func:distinctName(*//condition)"
vb@2829
   138
            | PEP_STATUS «@name»(PEP_SESSION session, bool *result);
vb@2829
   139
        ||
vb@2834
   140
vb@2829
   141
        // actions
vb@2829
   142
vb@2829
   143
        ||
vb@2865
   144
        for "func:distinctName(*//action)"
vb@2829
   145
            | PEP_STATUS «@name»(PEP_SESSION session);
vb@2829
   146
        ||
vb@2829
   147
vb@2835
   148
        // notify state machine from event
vb@2835
   149
        // use state to generate «@name» message if necessary
vb@2829
   150
vb@2835
   151
        PEP_STATUS «@name»_notify(
vb@2829
   152
                PEP_SESSION session, 
vb@2829
   153
                «@name»_PR fsm,
vb@2829
   154
                int message_type
vb@2829
   155
            );
vb@2829
   156
vb@2839
   157
        // send message about an event to communication partners using state
vb@2829
   158
vb@2829
   159
        PEP_STATUS send_«@name»_message(
vb@2829
   160
                PEP_SESSION session, 
vb@2829
   161
                «@name»_PR fsm,
vb@2841
   162
                int message_type
vb@2829
   163
            );
vb@2829
   164
vb@2838
   165
        // receive message and store it in state
vb@2829
   166
vb@2839
   167
        PEP_STATUS recv_«@name»_event(
vb@2836
   168
                PEP_SESSION session,
vb@2839
   169
                «@name»_event_t *ev
vb@2829
   170
            );
vb@2829
   171
    
vb@2829
   172
        // state machine driver
vb@2829
   173
        // if fsm or event set to 0 use fields in src if present
vb@2829
   174
vb@2829
   175
        PEP_STATUS «@name»_driver(
vb@2829
   176
                PEP_SESSION session,
vb@2829
   177
                «@name»_PR fsm,
vb@2829
   178
                int event
vb@2829
   179
            );
vb@2829
   180
vb@2829
   181
        PEP_STATUS inject_«@name»_event(
vb@2829
   182
                PEP_SESSION session, 
vb@2829
   183
                «@name»_PR fsm,
vb@2829
   184
                int event
vb@2829
   185
            );
vb@2829
   186
vb@2829
   187
vb@2829
   188
        #ifdef __cplusplus
vb@2829
   189
        }
vb@2829
   190
        #endif
vb@2829
   191
vb@2829
   192
        ||
vb@2829
   193
        }
vb@2829
   194
vb@2829
   195
        document "generated/{@name}_impl.c", "text"
vb@2829
   196
        ||
vb@2829
   197
        // This file is under GNU General Public License 3.0
vb@2829
   198
        // see LICENSE.txt
vb@2829
   199
    
vb@2829
   200
        #include "«@name»_impl.h"
vb@2829
   201
        #include "pEp_internal.h"
vb@2839
   202
        #include "«@name»_event.h"
vb@2841
   203
        #include "«@name»_codec.h"
vb@2841
   204
        #include "baseprotocol.h"
vb@2829
   205
        `` for "fsm" | #include "«@name»_fsm.h"
vb@2829
   206
vb@2829
   207
        PEP_STATUS «@name»_driver(
vb@2829
   208
                PEP_SESSION session,
vb@2829
   209
                «@name»_PR fsm,
vb@2829
   210
                int event
vb@2829
   211
            )
vb@2829
   212
        {
vb@2829
   213
            assert(session && fsm);
vb@2829
   214
            if (!(session && fsm))
vb@2829
   215
                return PEP_ILLEGAL_VALUE;
vb@2829
   216
vb@2838
   217
            int next_state = None;
vb@2838
   218
            do {
vb@2838
   219
                switch (fsm) {
vb@2838
   220
                    `` apply "fsm", 3, mode=driver               
vb@2838
   221
                    default:
vb@2838
   222
                        return PEP_ILLEGAL_VALUE;
vb@2838
   223
                }
vb@2838
   224
            }  while (next_state);
vb@2829
   225
vb@2829
   226
            return PEP_STATUS_OK;
vb@2829
   227
        }
vb@2829
   228
vb@2829
   229
        PEP_STATUS inject_«@name»_event(
vb@2829
   230
                PEP_SESSION session, 
vb@2829
   231
                «@name»_PR fsm,
vb@2829
   232
                int event
vb@2829
   233
            )
vb@2829
   234
        {
vb@2829
   235
            «@name»_t *msg = NULL;
vb@2829
   236
            «@name»_event_t *ev = NULL;
vb@2829
   237
vb@2829
   238
            assert(session && fsm > 0 && event > None);
vb@2829
   239
            if (!(session && fsm > 0 && event > None))
vb@2829
   240
                return PEP_ILLEGAL_VALUE;
vb@2829
   241
vb@2829
   242
            PEP_STATUS status = PEP_STATUS_OK;
vb@2829
   243
vb@2839
   244
            if (!session->inject_«yml:lcase(@name)»_event) {
vb@2829
   245
               status = PEP_«yml:ucase(@name)»_NO_INJECT_CALLBACK;
vb@2829
   246
               goto error;
vb@2829
   247
            }
vb@2829
   248
vb@2829
   249
            if (event < Extra) {
vb@2829
   250
                msg = new_«@name»_message(fsm, event);
vb@2829
   251
                if (!msg) {
vb@2829
   252
                    status = PEP_OUT_OF_MEMORY;
vb@2829
   253
                    goto error;
vb@2829
   254
                }
vb@2829
   255
vb@2854
   256
                status = update_«@name»_message(session, msg);
vb@2829
   257
                if (status)
vb@2829
   258
                    goto error;
vb@2829
   259
            }
vb@2829
   260
vb@2829
   261
            ev = («@name»_event_t *) calloc(1, sizeof(«@name»_event_t));
vb@2829
   262
            assert(ev);
vb@2829
   263
            if (!ev) {
vb@2829
   264
                status = PEP_OUT_OF_MEMORY;
vb@2829
   265
                goto error;
vb@2829
   266
            }
vb@2829
   267
            
vb@2829
   268
            ev->fsm = fsm;
vb@2829
   269
            ev->event = event;
vb@2829
   270
            ev->msg = msg;
vb@2829
   271
vb@2839
   272
            int result = session->inject_«yml:lcase(@name)»_event(ev,
vb@2829
   273
                    session->«yml:lcase(@name)»_management);
vb@2829
   274
            if (result) {
vb@2829
   275
                status = PEP_STATEMACHINE_ERROR;
vb@2829
   276
                goto error;
vb@2829
   277
            }
vb@2829
   278
vb@2829
   279
            goto the_end;
vb@2829
   280
vb@2829
   281
        error:
vb@2829
   282
            free(ev);
vb@2829
   283
            free_«@name»_message(msg);
vb@2829
   284
vb@2829
   285
        the_end:
vb@2829
   286
            return status;
vb@2829
   287
        }
vb@2829
   288
vb@2835
   289
        PEP_STATUS «@name»_notify(
vb@2829
   290
                PEP_SESSION session, 
vb@2829
   291
                «@name»_PR fsm,
vb@2829
   292
                int message_type
vb@2829
   293
            )
vb@2829
   294
        {
vb@2829
   295
            assert(session && fsm > 0 && message_type > 1 && message_type < Extra);
vb@2829
   296
            if (!(session && fsm > 0 && message_type > 1 && message_type < Extra))
vb@2829
   297
                return PEP_ILLEGAL_VALUE;
vb@2829
   298
vb@2829
   299
            PEP_STATUS status = PEP_STATUS_OK;
vb@2829
   300
vb@2829
   301
            «@name»_t *msg = new_«@name»_message(fsm, message_type);
vb@2829
   302
            if (!msg) {
vb@2829
   303
                status = PEP_OUT_OF_MEMORY;
vb@2829
   304
                goto error;
vb@2829
   305
            }
vb@2829
   306
vb@2854
   307
            status = update_«@name»_message(session, msg);
vb@2829
   308
            if (status)
vb@2829
   309
                goto error;
vb@2829
   310
vb@2829
   311
            goto the_end;
vb@2829
   312
vb@2829
   313
        error:
vb@2829
   314
            free_«@name»_message(msg);
vb@2829
   315
vb@2829
   316
        the_end:
vb@2829
   317
            return status;
vb@2829
   318
        }
vb@2829
   319
vb@2840
   320
        PEP_STATUS send_«@name»_message(
vb@2840
   321
                PEP_SESSION session, 
vb@2840
   322
                «@name»_PR fsm,
vb@2841
   323
                int message_type
vb@2840
   324
            )
vb@2840
   325
        {
vb@2840
   326
            PEP_STATUS status = PEP_STATUS_OK;
vb@2840
   327
vb@2841
   328
            assert(session && fsm > None && message_type > None);
vb@2841
   329
            if (!(session && fsm > None && message_type > None))
vb@2840
   330
                return PEP_ILLEGAL_VALUE;
vb@2840
   331
            
vb@2854
   332
            «@name»_t *msg = new_«@name»_message(fsm, message_type);
vb@2841
   333
            if (!msg)
vb@2841
   334
                return PEP_OUT_OF_MEMORY;
vb@2840
   335
vb@2841
   336
            char *data = NULL;
vb@2841
   337
            message *m = NULL;
vb@2868
   338
            identity_list *channels = NULL;
vb@2841
   339
vb@2854
   340
            status = update_«@name»_message(session, msg);
vb@2841
   341
            if (status)
vb@2841
   342
                goto the_end;
vb@2841
   343
vb@2841
   344
            size_t size = 0;
vb@2841
   345
            status = encode_«@name»_message(msg, &data, &size);
vb@2841
   346
            if (status)
vb@2841
   347
                goto the_end;
vb@2841
   348
vb@2868
   349
            switch (message_type) {
vb@2868
   350
                // these messages are broadcasted
vb@2868
   351
                `` for "fsm/message[@type='broadcast']" |>> case «@name»:
vb@2868
   352
                    status = _own_identities_retrieve(session, &channels, PEP_idf_not_for_sync);
vb@2868
   353
                    if (!status)
vb@2868
   354
                        goto the_end;
vb@2868
   355
                    break;
vb@2868
   356
vb@2868
   357
                // all other go unicast
vb@2868
   358
                default: {
vb@2869
   359
                    if (!session->«yml:lcase(@name)»_state.common.from `> |`|
vb@2868
   360
                        (session->«yml:lcase(@name)»_state.common.from->flags &
vb@2869
   361
                        PEP_idf_not_for_sync))
vb@2868
   362
                    {
vb@2868
   363
                        status = PEP_SYNC_NO_CHANNEL;
vb@2868
   364
                        goto the_end;
vb@2868
   365
                    }
vb@2868
   366
vb@2868
   367
                    pEp_identity *channel = identity_dup(session->«yml:lcase(@name)»_state.common.from);
vb@2868
   368
                    if (!channel) {
vb@2868
   369
                        status = PEP_OUT_OF_MEMORY;
vb@2868
   370
                        goto the_end;
vb@2868
   371
                    }
vb@2868
   372
vb@2868
   373
                    channels = new_identity_list(channel);
vb@2868
   374
                    if (!channels) {
vb@2868
   375
                        status = PEP_OUT_OF_MEMORY;
vb@2868
   376
                        goto the_end;
vb@2868
   377
                    }
vb@2868
   378
                }
vb@2868
   379
            }
vb@2868
   380
vb@2868
   381
            for (identity_list *li = channels; li && li->ident ; li = li->next) {
vb@2868
   382
                status = base_prepare_message(
vb@2868
   383
                        li->ident,
vb@2868
   384
                        li->ident,
vb@2868
   385
                        data,
vb@2868
   386
                        size,
vb@2868
   387
                        &m
vb@2868
   388
                    );
vb@2868
   389
                if (status)
vb@2868
   390
                    goto the_end;
vb@2868
   391
                
vb@2868
   392
                status = session->messageToSend(session->«yml:lcase(@name)»_obj, m);
vb@2868
   393
                free_message(m);
vb@2868
   394
                m = NULL;
vb@2868
   395
            }
vb@2841
   396
vb@2841
   397
        the_end:
vb@2868
   398
            free_identity_list(channels);
vb@2841
   399
            free_message(m);
vb@2841
   400
            free(data);
vb@2841
   401
            free_«@name»_message(msg);
vb@2840
   402
            return status;
vb@2840
   403
        }
vb@2840
   404
vb@2829
   405
        PEP_STATUS recv_«@name»_event(
vb@2836
   406
                PEP_SESSION session,
vb@2839
   407
                «@name»_event_t *ev
vb@2829
   408
            )
vb@2829
   409
        {
vb@2829
   410
            assert(session && ev);
vb@2829
   411
            if (!(session && ev))
vb@2829
   412
                return PEP_ILLEGAL_VALUE;
vb@2829
   413
vb@2829
   414
            PEP_STATUS status = PEP_STATUS_OK;
vb@2829
   415
vb@2829
   416
            if (ev->event < Extra) {
vb@2829
   417
                «@name»_PR fsm = (int) None;
vb@2829
   418
                int event = None;
vb@2829
   419
vb@2829
   420
                status = update_«@name»_state(session, ev->msg, &fsm, &event);
vb@2829
   421
                if (status)
vb@2829
   422
                    goto error;
vb@2829
   423
vb@2829
   424
                if (ev->fsm) {
vb@2829
   425
                    if (ev->fsm != fsm |`> |` ev->event != event) {
vb@2844
   426
                        status = PEP_«yml:ucase(@name)»_ILLEGAL_MESSAGE;
vb@2829
   427
                        goto error;
vb@2829
   428
                    }
vb@2829
   429
                }
vb@2829
   430
                else {
vb@2829
   431
                    if (ev->event) {
vb@2844
   432
                        status = PEP_«yml:ucase(@name)»_ILLEGAL_MESSAGE;
vb@2829
   433
                        goto error;
vb@2829
   434
                    }
vb@2829
   435
                    ev->fsm = fsm;
vb@2829
   436
                    ev->event = event;
vb@2829
   437
                }
vb@2829
   438
            }
vb@2829
   439
vb@2829
   440
            free_«@name»_message(ev->msg);
vb@2829
   441
            free(ev);
vb@2829
   442
            status = «@name»_driver(session, ev->fsm, ev->event);
vb@2829
   443
            return status;
vb@2829
   444
vb@2829
   445
        error:
vb@2829
   446
            free_«@name»_message(ev->msg);
vb@2829
   447
            free(ev);
vb@2829
   448
            return status;
vb@2829
   449
        }
vb@2829
   450
vb@2829
   451
        ||
vb@2829
   452
vb@2829
   453
        apply "fsm", 0, mode=gen;
vb@2829
   454
    }
vb@2829
   455
vb@2838
   456
    template "fsm", mode=event
vb@2838
   457
    {
vb@2838
   458
    ||
vb@2843
   459
    case «../@name»_PR_«yml:lcase(@name)»: {
vb@2839
   460
        switch (msg->choice.«yml:lcase(@name)».payload.present) {
vb@2838
   461
    ||
vb@2838
   462
    for "message"
vb@2838
   463
    ||
vb@2838
   464
            case «../@name»__payload_PR_«yml:mixedCase(@name)»:
vb@2838
   465
                ev->event = «@name»;
vb@2838
   466
                break;
vb@2838
   467
    ||
vb@2838
   468
    ||
vb@2838
   469
            default:
vb@2838
   470
                // unknown message type
vb@2838
   471
                free(ev);
vb@2838
   472
                return NULL;
vb@2838
   473
        }
vb@2838
   474
        break;
vb@2838
   475
    }
vb@2838
   476
vb@2838
   477
    ||
vb@2838
   478
    }
vb@2838
   479
vb@2829
   480
    template "fsm", mode=driver
vb@2829
   481
    ||
vb@2843
   482
    case «../@name»_PR_«yml:lcase(@name)»: {
vb@2839
   483
        int state = session->«yml:lcase(../@name)»_state.«yml:lcase(@name)».state;
vb@2839
   484
        next_state = fsm_«@name»(session, state, event);
vb@2838
   485
        if (next_state > None) {
vb@2838
   486
            session->«yml:lcase(../@name)»_state.«yml:lcase(@name)».state = next_state;
vb@2838
   487
            event = Init;
vb@2838
   488
        }
vb@2838
   489
        else if (next_state < None) {
vb@2829
   490
            return PEP_STATEMACHINE_ERROR - state;
vb@2838
   491
        }
vb@2829
   492
        break;
vb@2829
   493
    }
vb@2829
   494
vb@2829
   495
    ||
vb@2829
   496
vb@2829
   497
    template "fsm", mode=gen {
vb@2829
   498
        document "generated/{@name}_fsm.h", "text" {
vb@2829
   499
        ||
vb@2829
   500
        // This file is under GNU General Public License 3.0
vb@2829
   501
        // see LICENSE.txt
vb@2829
   502
vb@2829
   503
        #pragma once
vb@2829
   504
vb@2829
   505
        #include "«../@name»_impl.h"
vb@2829
   506
vb@2829
   507
        #ifdef __cplusplus
vb@2829
   508
        extern "C" {
vb@2829
   509
        #endif
vb@2829
   510
vb@2829
   511
        // state machine for «@name»
krista@2271
   512
krista@2271
   513
        // states
krista@2271
   514
krista@2271
   515
        typedef enum _«@name»_state {
vb@2829
   516
            «@name»_state_None = None,
vb@2829
   517
            «@name»_state_Init = Init,
vb@2829
   518
        ||
vb@2829
   519
        for "func:distinctName(state[not(@name='InitState')])"
vb@2829
   520
            |> «@name»`if "position()!=last()" > , `
vb@2829
   521
        ||
krista@2271
   522
        } «@name»_state;
krista@2271
   523
krista@2271
   524
        // events
krista@2271
   525
krista@2271
   526
        typedef enum _«@name»_event {
vb@2829
   527
            «@name»_event_None = None,
vb@2829
   528
            «@name»_event_Init = Init,
krista@2271
   529
        ||
vb@2829
   530
        for "func:distinctName(state/event[not(not(../../message/@name=@name))])" {
krista@2271
   531
            const "name", "@name";
vb@2829
   532
            |> «$name» = «/protocol/fsm/message[@name=$name]/@id»,
krista@2271
   533
        }
vb@2830
   534
        for "func:distinctName(state/event[not(not(../../external/@name=@name))])" {
vb@2830
   535
            const "name", "@name";
vb@2830
   536
            |> «$name» = «/protocol/fsm/external[@name=$name]/@id»,
vb@2830
   537
        }
vb@2829
   538
        |> «@name»_event_Extra = Extra,
vb@2830
   539
        for "func:distinctName(state/event[not(../../message/@name=@name or ../../external/@name=@name)])" {
vb@2829
   540
            if "@name!='Init'"
vb@2829
   541
                |> «@name»`if "position()!=last()" > , `
vb@2829
   542
        }
krista@2271
   543
        ||
krista@2271
   544
        } «@name»_event;
krista@2271
   545
vb@2829
   546
        // state machine
krista@2271
   547
vb@2829
   548
        const char *«@name»_state_name(int state);
krista@2271
   549
vb@2838
   550
        // the state machine function is returning the next state in case of a
vb@2838
   551
        // transition or None for staying
vb@2838
   552
krista@2271
   553
        «@name»_state fsm_«@name»(
krista@2271
   554
                PEP_SESSION session,
krista@2271
   555
                «@name»_state state,
vb@2829
   556
                «@name»_event event
krista@2271
   557
            );
krista@2271
   558
krista@2271
   559
        #ifdef __cplusplus
krista@2271
   560
        }
krista@2271
   561
        #endif
krista@2271
   562
krista@2271
   563
        ||
krista@2271
   564
        }
vb@2829
   565
vb@2829
   566
        document "generated/{@name}_fsm.c", "text" {
krista@2271
   567
        ||
vb@2829
   568
        // This file is under GNU General Public License 3.0
vb@2829
   569
        // see LICENSE.txt
krista@2271
   570
vb@2829
   571
        #include "«@name»_fsm.h"
vb@2847
   572
        #include <stdlib.h>
krista@2271
   573
vb@2829
   574
        const char *«@name»_state_name(int state)
vb@2829
   575
        {
vb@2829
   576
            switch (state) {
vb@2829
   577
                case End:
vb@2829
   578
                    return "End";
vb@2829
   579
                case None:
vb@2829
   580
                    return "None";
vb@2829
   581
                case Init:
vb@2829
   582
                    return "InitState";
vb@2829
   583
        ||
vb@2829
   584
        for "func:distinctName(state[not(@name='InitState')])" {
vb@2829
   585
            |>> case «@name»:
vb@2829
   586
            |>>> return "«@name»";
vb@2829
   587
        }
vb@2829
   588
        ||
vb@2829
   589
                default:
vb@2829
   590
                    return "unknown state";
vb@2829
   591
            }
vb@2829
   592
        }
krista@2271
   593
vb@2829
   594
        static char *_str(int n, bool hex)
vb@2829
   595
        {
vb@2829
   596
            char *buf = calloc(1, 24);
vb@2829
   597
            assert(buf);
vb@2829
   598
            if (!buf)
vb@2829
   599
                return NULL;
vb@2829
   600
vb@2829
   601
            if (hex)
vb@2829
   602
                snprintf(buf, 24, "%.4x", n);
vb@2829
   603
            else
vb@2829
   604
                snprintf(buf, 24, "%d", n);
vb@2829
   605
            return buf;
vb@2829
   606
        }
vb@2829
   607
vb@2829
   608
        #define «@name»_ERR_LOG(t, d) log_event(session, (t), "«@name»", (d), "error")
vb@2829
   609
vb@2829
   610
        static PEP_STATUS _«@name»_ERR_LOG_int(PEP_SESSION session, char *t, int n, bool hex)
vb@2829
   611
        {
vb@2829
   612
            char *_buf = _str(n, hex);
vb@2829
   613
            if (!_buf)
vb@2829
   614
                return PEP_OUT_OF_MEMORY;
vb@2829
   615
            PEP_STATUS status = «@name»_ERR_LOG(t, _buf);
vb@2829
   616
            free(_buf);
vb@2829
   617
            return status;
vb@2829
   618
        }
vb@2829
   619
vb@2829
   620
        #define «@name»_ERR_LOG_INT(t, n) _«@name»_ERR_LOG_int(session, (t), (n), false)
vb@2829
   621
        #define «@name»_ERR_LOG_HEX(t, n) _«@name»_ERR_LOG_int(session, (t), (n), true)
vb@2829
   622
vb@2829
   623
        #ifndef SERVICE_LOG
vb@2829
   624
        // SERVICE LOG is meant to check session->service_log in runtime config;
vb@2829
   625
        // for older engines log more than needed
vb@2829
   626
        #define SERVICE_LOG(session, t, n, d) log_event((session), (t), (n), (d), "service")
vb@2829
   627
        #endif 
vb@2829
   628
vb@2829
   629
        #define «@name»_SERVICE_LOG(t, d) SERVICE_LOG(session, (t), "«@name»", (d))
vb@2829
   630
vb@2829
   631
        «@name»_state fsm_«@name»(
krista@2271
   632
                PEP_SESSION session,
vb@2829
   633
                «@name»_state state,
vb@2829
   634
                «@name»_event event
krista@2271
   635
            )
krista@2271
   636
        {
krista@2271
   637
            assert(session);
krista@2271
   638
            if (!session)
vb@2829
   639
                return invalid_state;
krista@2271
   640
krista@2271
   641
            switch (state) {
vb@2829
   642
                case None:
vb@2838
   643
                    return «@name»_state_Init;
vb@2829
   644
                
vb@2829
   645
                `` apply "state", 2, mode=fsm
krista@2271
   646
                default:
vb@2838
   647
                    «@name»_ERR_LOG_INT("invalid state", state);
vb@2829
   648
                    return invalid_state;
krista@2271
   649
            }
vb@2829
   650
            
vb@2838
   651
            return None;
krista@2271
   652
        }
krista@2271
   653
krista@2271
   654
        ||
vb@2829
   655
        }
vb@2829
   656
    }
vb@2829
   657
    
vb@2829
   658
    template "state", mode=fsm {
vb@2829
   659
        choose {
vb@2829
   660
            when "@name='InitState'" | case «../@name»_state_Init:
vb@2829
   661
            otherwise | case «@name»:
vb@2829
   662
        }
vb@2829
   663
        ||
vb@2829
   664
            «../@name»_SERVICE_LOG("in state", "«@name»");
vb@2829
   665
vb@2829
   666
            switch (event) {
vb@2829
   667
                case None:
vb@2829
   668
                    «../@name»_SERVICE_LOG("received None event", "ignoring");
vb@2838
   669
                    break;
vb@2829
   670
     
vb@2838
   671
        ||
vb@2838
   672
        if "not(event[@name='Init'])"
vb@2838
   673
        ||
vb@2838
   674
                case Init:
vb@2838
   675
                    // nothing to do
vb@2838
   676
                    break;
vb@2838
   677
vb@2838
   678
        ||
vb@2838
   679
        ||
vb@2829
   680
                `` apply "event", 2, mode=fsm
vb@2829
   681
                default:
vb@2838
   682
                    «../@name»_ERR_LOG_INT("invalid event", event);
vb@2829
   683
                    return invalid_event;
vb@2829
   684
            }
vb@2829
   685
            break;
vb@2829
   686
vb@2829
   687
        ||
krista@2271
   688
    }
krista@2271
   689
vb@2829
   690
    template "event", mode=fsm {
vb@2829
   691
        | case «@name»: {
vb@2865
   692
        if "condition|action|send" |> PEP_STATUS status;
vb@2829
   693
        if "condition" |> bool result = false;
vb@2865
   694
        if "condition|action|send" |
krista@2271
   695
        ||
vb@2829
   696
            «../../@name»_SERVICE_LOG("received event", "«@name»");
vb@2865
   697
            `` apply "transition|action|condition|send";
vb@2829
   698
        ||
vb@2829
   699
        if "name(*[last()])!='transition'" {
vb@2829
   700
            |
vb@2843
   701
            |> «../../@name»_SERVICE_LOG("remaining in state", "«../@name»");
vb@2829
   702
            |> break;
vb@2829
   703
        }
vb@2829
   704
        ||
vb@2829
   705
        }
vb@2829
   706
        
vb@2829
   707
        ||
vb@2829
   708
    }
vb@2829
   709
vb@2829
   710
    template "transition" {
vb@2865
   711
        const "fsm", "ancestor::fsm";
krista@2271
   712
        ||
krista@2271
   713
vb@2829
   714
        «$fsm/@name»_SERVICE_LOG("transition to state", "«@target»");
vb@2838
   715
        return «@target»;
krista@2271
   716
        ||
vb@2829
   717
    }
krista@2271
   718
vb@2865
   719
    template "send" {
vb@2865
   720
        const "fsm", "ancestor::fsm";
vb@2865
   721
        const "protocol", "ancestor::protocol";
vb@2865
   722
        ||
vb@2865
   723
vb@2865
   724
        «$fsm/@name»_SERVICE_LOG("send message", "«@name»");
vb@2865
   725
        status = send_«$protocol/@name»_message(session, «$fsm/@id», «$fsm/@name»__payload_PR_«yml:mixedCase(@name)»);
vb@2865
   726
        ||
vb@2865
   727
    }
vb@2865
   728
vb@2829
   729
    template "action" {
vb@2865
   730
        const "fsm", "ancestor::fsm";
vb@2865
   731
        ||
vb@2829
   732
vb@2865
   733
        «$fsm/@name»_SERVICE_LOG("do action", "«@name»");
vb@2865
   734
        status = «@name»(session);
vb@2829
   735
        if (status) {
vb@2829
   736
            «$fsm/@name»_ERR_LOG_HEX("executing action «@name»() failed", status);
vb@2829
   737
            return invalid_action;
krista@2271
   738
        }
krista@2271
   739
        ||
krista@2271
   740
    }
krista@2271
   741
vb@2829
   742
    template "condition" {
vb@2865
   743
        const "fsm", "ancestor::fsm";
krista@2271
   744
        ||
krista@2271
   745
vb@2829
   746
        status = «@name»(session, &result);
vb@2829
   747
        if (status) {
vb@2829
   748
            «$fsm/@name»_ERR_LOG_HEX("computing condition «@name» failed", status);
vb@2829
   749
            return invalid_condition;
krista@2271
   750
        }
vb@2829
   751
        if (result) {
vb@2843
   752
            «$fsm/@name»_SERVICE_LOG("condition applies", "«@name»");
krista@2271
   753
        ||
vb@2865
   754
        apply "transition|action|condition|send";
krista@2271
   755
        | }
krista@2271
   756
    }
krista@2271
   757
}
krista@2271
   758