sync/gen_statemachine.ysl2
author Volker Birk <vb@pep.foundation>
Tue, 21 Aug 2018 20:28:07 +0200
branchsync
changeset 2865 96aaa55b2a54
parent 2854 f4728cd6a9b8
child 2868 d812e7a24b52
permissions -rw-r--r--
reworking send message
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@2841
   338
vb@2854
   339
            status = update_«@name»_message(session, msg);
vb@2841
   340
            if (status)
vb@2841
   341
                goto the_end;
vb@2841
   342
vb@2841
   343
            size_t size = 0;
vb@2841
   344
            status = encode_«@name»_message(msg, &data, &size);
vb@2841
   345
            if (status)
vb@2841
   346
                goto the_end;
vb@2841
   347
vb@2846
   348
            status = base_prepare_message(
vb@2843
   349
                    session->«yml:lcase(@name)»_state.common.from,
vb@2843
   350
                    session->«yml:lcase(@name)»_state.common.from,
vb@2841
   351
                    data,
vb@2841
   352
                    size,
vb@2841
   353
                    &m
vb@2841
   354
                );
vb@2841
   355
            if (status)
vb@2841
   356
                goto the_end;
vb@2841
   357
            
vb@2843
   358
            status = session->messageToSend(session->«yml:lcase(@name)»_obj, m);
vb@2841
   359
vb@2841
   360
        the_end:
vb@2841
   361
            free_message(m);
vb@2841
   362
            free(data);
vb@2841
   363
            free_«@name»_message(msg);
vb@2840
   364
            return status;
vb@2840
   365
        }
vb@2840
   366
vb@2829
   367
        PEP_STATUS recv_«@name»_event(
vb@2836
   368
                PEP_SESSION session,
vb@2839
   369
                «@name»_event_t *ev
vb@2829
   370
            )
vb@2829
   371
        {
vb@2829
   372
            assert(session && ev);
vb@2829
   373
            if (!(session && ev))
vb@2829
   374
                return PEP_ILLEGAL_VALUE;
vb@2829
   375
vb@2829
   376
            PEP_STATUS status = PEP_STATUS_OK;
vb@2829
   377
vb@2829
   378
            if (ev->event < Extra) {
vb@2829
   379
                «@name»_PR fsm = (int) None;
vb@2829
   380
                int event = None;
vb@2829
   381
vb@2829
   382
                status = update_«@name»_state(session, ev->msg, &fsm, &event);
vb@2829
   383
                if (status)
vb@2829
   384
                    goto error;
vb@2829
   385
vb@2829
   386
                if (ev->fsm) {
vb@2829
   387
                    if (ev->fsm != fsm |`> |` ev->event != event) {
vb@2844
   388
                        status = PEP_«yml:ucase(@name)»_ILLEGAL_MESSAGE;
vb@2829
   389
                        goto error;
vb@2829
   390
                    }
vb@2829
   391
                }
vb@2829
   392
                else {
vb@2829
   393
                    if (ev->event) {
vb@2844
   394
                        status = PEP_«yml:ucase(@name)»_ILLEGAL_MESSAGE;
vb@2829
   395
                        goto error;
vb@2829
   396
                    }
vb@2829
   397
                    ev->fsm = fsm;
vb@2829
   398
                    ev->event = event;
vb@2829
   399
                }
vb@2829
   400
            }
vb@2829
   401
vb@2829
   402
            free_«@name»_message(ev->msg);
vb@2829
   403
            free(ev);
vb@2829
   404
            status = «@name»_driver(session, ev->fsm, ev->event);
vb@2829
   405
            return status;
vb@2829
   406
vb@2829
   407
        error:
vb@2829
   408
            free_«@name»_message(ev->msg);
vb@2829
   409
            free(ev);
vb@2829
   410
            return status;
vb@2829
   411
        }
vb@2829
   412
vb@2829
   413
        ||
vb@2829
   414
vb@2829
   415
        apply "fsm", 0, mode=gen;
vb@2829
   416
    }
vb@2829
   417
vb@2838
   418
    template "fsm", mode=event
vb@2838
   419
    {
vb@2838
   420
    ||
vb@2843
   421
    case «../@name»_PR_«yml:lcase(@name)»: {
vb@2839
   422
        switch (msg->choice.«yml:lcase(@name)».payload.present) {
vb@2838
   423
    ||
vb@2838
   424
    for "message"
vb@2838
   425
    ||
vb@2838
   426
            case «../@name»__payload_PR_«yml:mixedCase(@name)»:
vb@2838
   427
                ev->event = «@name»;
vb@2838
   428
                break;
vb@2838
   429
    ||
vb@2838
   430
    ||
vb@2838
   431
            default:
vb@2838
   432
                // unknown message type
vb@2838
   433
                free(ev);
vb@2838
   434
                return NULL;
vb@2838
   435
        }
vb@2838
   436
        break;
vb@2838
   437
    }
vb@2838
   438
vb@2838
   439
    ||
vb@2838
   440
    }
vb@2838
   441
vb@2829
   442
    template "fsm", mode=driver
vb@2829
   443
    ||
vb@2843
   444
    case «../@name»_PR_«yml:lcase(@name)»: {
vb@2839
   445
        int state = session->«yml:lcase(../@name)»_state.«yml:lcase(@name)».state;
vb@2839
   446
        next_state = fsm_«@name»(session, state, event);
vb@2838
   447
        if (next_state > None) {
vb@2838
   448
            session->«yml:lcase(../@name)»_state.«yml:lcase(@name)».state = next_state;
vb@2838
   449
            event = Init;
vb@2838
   450
        }
vb@2838
   451
        else if (next_state < None) {
vb@2829
   452
            return PEP_STATEMACHINE_ERROR - state;
vb@2838
   453
        }
vb@2829
   454
        break;
vb@2829
   455
    }
vb@2829
   456
vb@2829
   457
    ||
vb@2829
   458
vb@2829
   459
    template "fsm", mode=gen {
vb@2829
   460
        document "generated/{@name}_fsm.h", "text" {
vb@2829
   461
        ||
vb@2829
   462
        // This file is under GNU General Public License 3.0
vb@2829
   463
        // see LICENSE.txt
vb@2829
   464
vb@2829
   465
        #pragma once
vb@2829
   466
vb@2829
   467
        #include "«../@name»_impl.h"
vb@2829
   468
vb@2829
   469
        #ifdef __cplusplus
vb@2829
   470
        extern "C" {
vb@2829
   471
        #endif
vb@2829
   472
vb@2829
   473
        // state machine for «@name»
krista@2271
   474
krista@2271
   475
        // states
krista@2271
   476
krista@2271
   477
        typedef enum _«@name»_state {
vb@2829
   478
            «@name»_state_None = None,
vb@2829
   479
            «@name»_state_Init = Init,
vb@2829
   480
        ||
vb@2829
   481
        for "func:distinctName(state[not(@name='InitState')])"
vb@2829
   482
            |> «@name»`if "position()!=last()" > , `
vb@2829
   483
        ||
krista@2271
   484
        } «@name»_state;
krista@2271
   485
krista@2271
   486
        // events
krista@2271
   487
krista@2271
   488
        typedef enum _«@name»_event {
vb@2829
   489
            «@name»_event_None = None,
vb@2829
   490
            «@name»_event_Init = Init,
krista@2271
   491
        ||
vb@2829
   492
        for "func:distinctName(state/event[not(not(../../message/@name=@name))])" {
krista@2271
   493
            const "name", "@name";
vb@2829
   494
            |> «$name» = «/protocol/fsm/message[@name=$name]/@id»,
krista@2271
   495
        }
vb@2830
   496
        for "func:distinctName(state/event[not(not(../../external/@name=@name))])" {
vb@2830
   497
            const "name", "@name";
vb@2830
   498
            |> «$name» = «/protocol/fsm/external[@name=$name]/@id»,
vb@2830
   499
        }
vb@2829
   500
        |> «@name»_event_Extra = Extra,
vb@2830
   501
        for "func:distinctName(state/event[not(../../message/@name=@name or ../../external/@name=@name)])" {
vb@2829
   502
            if "@name!='Init'"
vb@2829
   503
                |> «@name»`if "position()!=last()" > , `
vb@2829
   504
        }
krista@2271
   505
        ||
krista@2271
   506
        } «@name»_event;
krista@2271
   507
vb@2829
   508
        // state machine
krista@2271
   509
vb@2829
   510
        const char *«@name»_state_name(int state);
krista@2271
   511
vb@2838
   512
        // the state machine function is returning the next state in case of a
vb@2838
   513
        // transition or None for staying
vb@2838
   514
krista@2271
   515
        «@name»_state fsm_«@name»(
krista@2271
   516
                PEP_SESSION session,
krista@2271
   517
                «@name»_state state,
vb@2829
   518
                «@name»_event event
krista@2271
   519
            );
krista@2271
   520
krista@2271
   521
        #ifdef __cplusplus
krista@2271
   522
        }
krista@2271
   523
        #endif
krista@2271
   524
krista@2271
   525
        ||
krista@2271
   526
        }
vb@2829
   527
vb@2829
   528
        document "generated/{@name}_fsm.c", "text" {
krista@2271
   529
        ||
vb@2829
   530
        // This file is under GNU General Public License 3.0
vb@2829
   531
        // see LICENSE.txt
vb@2829
   532
vb@2829
   533
        #include "«@name»_fsm.h"
vb@2847
   534
        #include <stdlib.h>
krista@2271
   535
vb@2829
   536
        const char *«@name»_state_name(int state)
vb@2829
   537
        {
vb@2829
   538
            switch (state) {
vb@2829
   539
                case End:
vb@2829
   540
                    return "End";
vb@2829
   541
                case None:
vb@2829
   542
                    return "None";
vb@2829
   543
                case Init:
vb@2829
   544
                    return "InitState";
vb@2829
   545
        ||
vb@2829
   546
        for "func:distinctName(state[not(@name='InitState')])" {
vb@2829
   547
            |>> case «@name»:
vb@2829
   548
            |>>> return "«@name»";
vb@2829
   549
        }
vb@2829
   550
        ||
vb@2829
   551
                default:
vb@2829
   552
                    return "unknown state";
vb@2829
   553
            }
vb@2829
   554
        }
vb@2829
   555
vb@2829
   556
        static char *_str(int n, bool hex)
vb@2829
   557
        {
vb@2829
   558
            char *buf = calloc(1, 24);
vb@2829
   559
            assert(buf);
vb@2829
   560
            if (!buf)
vb@2829
   561
                return NULL;
krista@2271
   562
vb@2829
   563
            if (hex)
vb@2829
   564
                snprintf(buf, 24, "%.4x", n);
vb@2829
   565
            else
vb@2829
   566
                snprintf(buf, 24, "%d", n);
vb@2829
   567
            return buf;
vb@2829
   568
        }
krista@2271
   569
vb@2829
   570
        #define «@name»_ERR_LOG(t, d) log_event(session, (t), "«@name»", (d), "error")
vb@2829
   571
vb@2829
   572
        static PEP_STATUS _«@name»_ERR_LOG_int(PEP_SESSION session, char *t, int n, bool hex)
vb@2829
   573
        {
vb@2829
   574
            char *_buf = _str(n, hex);
vb@2829
   575
            if (!_buf)
vb@2829
   576
                return PEP_OUT_OF_MEMORY;
vb@2829
   577
            PEP_STATUS status = «@name»_ERR_LOG(t, _buf);
vb@2829
   578
            free(_buf);
vb@2829
   579
            return status;
vb@2829
   580
        }
vb@2829
   581
vb@2829
   582
        #define «@name»_ERR_LOG_INT(t, n) _«@name»_ERR_LOG_int(session, (t), (n), false)
vb@2829
   583
        #define «@name»_ERR_LOG_HEX(t, n) _«@name»_ERR_LOG_int(session, (t), (n), true)
vb@2829
   584
vb@2829
   585
        #ifndef SERVICE_LOG
vb@2829
   586
        // SERVICE LOG is meant to check session->service_log in runtime config;
vb@2829
   587
        // for older engines log more than needed
vb@2829
   588
        #define SERVICE_LOG(session, t, n, d) log_event((session), (t), (n), (d), "service")
vb@2829
   589
        #endif 
vb@2829
   590
vb@2829
   591
        #define «@name»_SERVICE_LOG(t, d) SERVICE_LOG(session, (t), "«@name»", (d))
vb@2829
   592
vb@2829
   593
        «@name»_state fsm_«@name»(
krista@2271
   594
                PEP_SESSION session,
vb@2829
   595
                «@name»_state state,
vb@2829
   596
                «@name»_event event
krista@2271
   597
            )
krista@2271
   598
        {
krista@2271
   599
            assert(session);
krista@2271
   600
            if (!session)
vb@2829
   601
                return invalid_state;
krista@2271
   602
krista@2271
   603
            switch (state) {
vb@2829
   604
                case None:
vb@2838
   605
                    return «@name»_state_Init;
vb@2829
   606
                
vb@2829
   607
                `` apply "state", 2, mode=fsm
krista@2271
   608
                default:
vb@2838
   609
                    «@name»_ERR_LOG_INT("invalid state", state);
vb@2829
   610
                    return invalid_state;
krista@2271
   611
            }
vb@2829
   612
            
vb@2838
   613
            return None;
krista@2271
   614
        }
krista@2271
   615
krista@2271
   616
        ||
vb@2829
   617
        }
krista@2271
   618
    }
vb@2829
   619
    
vb@2829
   620
    template "state", mode=fsm {
vb@2829
   621
        choose {
vb@2829
   622
            when "@name='InitState'" | case «../@name»_state_Init:
vb@2829
   623
            otherwise | case «@name»:
vb@2829
   624
        }
krista@2271
   625
        ||
vb@2829
   626
            «../@name»_SERVICE_LOG("in state", "«@name»");
krista@2271
   627
vb@2829
   628
            switch (event) {
vb@2829
   629
                case None:
vb@2829
   630
                    «../@name»_SERVICE_LOG("received None event", "ignoring");
vb@2838
   631
                    break;
vb@2829
   632
     
vb@2838
   633
        ||
vb@2838
   634
        if "not(event[@name='Init'])"
vb@2838
   635
        ||
vb@2838
   636
                case Init:
vb@2838
   637
                    // nothing to do
vb@2838
   638
                    break;
vb@2838
   639
vb@2838
   640
        ||
vb@2838
   641
        ||
vb@2829
   642
                `` apply "event", 2, mode=fsm
vb@2829
   643
                default:
vb@2838
   644
                    «../@name»_ERR_LOG_INT("invalid event", event);
vb@2829
   645
                    return invalid_event;
vb@2829
   646
            }
vb@2829
   647
            break;
krista@2271
   648
krista@2271
   649
        ||
vb@2829
   650
    }
vb@2829
   651
vb@2829
   652
    template "event", mode=fsm {
vb@2829
   653
        | case «@name»: {
vb@2865
   654
        if "condition|action|send" |> PEP_STATUS status;
vb@2829
   655
        if "condition" |> bool result = false;
vb@2865
   656
        if "condition|action|send" |
vb@2829
   657
        ||
vb@2829
   658
            «../../@name»_SERVICE_LOG("received event", "«@name»");
vb@2865
   659
            `` apply "transition|action|condition|send";
vb@2829
   660
        ||
vb@2829
   661
        if "name(*[last()])!='transition'" {
vb@2829
   662
            |
vb@2843
   663
            |> «../../@name»_SERVICE_LOG("remaining in state", "«../@name»");
vb@2829
   664
            |> break;
vb@2829
   665
        }
vb@2829
   666
        ||
vb@2829
   667
        }
vb@2829
   668
        
vb@2829
   669
        ||
vb@2829
   670
    }
vb@2829
   671
vb@2829
   672
    template "transition" {
vb@2865
   673
        const "fsm", "ancestor::fsm";
krista@2271
   674
        ||
krista@2271
   675
vb@2829
   676
        «$fsm/@name»_SERVICE_LOG("transition to state", "«@target»");
vb@2838
   677
        return «@target»;
krista@2271
   678
        ||
vb@2829
   679
    }
vb@2829
   680
vb@2865
   681
    template "send" {
vb@2865
   682
        const "fsm", "ancestor::fsm";
vb@2865
   683
        const "protocol", "ancestor::protocol";
vb@2865
   684
        ||
krista@2271
   685
vb@2865
   686
        «$fsm/@name»_SERVICE_LOG("send message", "«@name»");
vb@2865
   687
        status = send_«$protocol/@name»_message(session, «$fsm/@id», «$fsm/@name»__payload_PR_«yml:mixedCase(@name)»);
vb@2865
   688
        ||
vb@2865
   689
    }
krista@2271
   690
vb@2865
   691
    template "action" {
vb@2865
   692
        const "fsm", "ancestor::fsm";
krista@2271
   693
        ||
vb@2865
   694
vb@2865
   695
        «$fsm/@name»_SERVICE_LOG("do action", "«@name»");
vb@2865
   696
        status = «@name»(session);
vb@2829
   697
        if (status) {
vb@2829
   698
            «$fsm/@name»_ERR_LOG_HEX("executing action «@name»() failed", status);
vb@2829
   699
            return invalid_action;
krista@2271
   700
        }
krista@2271
   701
        ||
krista@2271
   702
    }
krista@2271
   703
vb@2829
   704
    template "condition" {
vb@2865
   705
        const "fsm", "ancestor::fsm";
krista@2271
   706
        ||
krista@2271
   707
vb@2829
   708
        status = «@name»(session, &result);
vb@2829
   709
        if (status) {
vb@2829
   710
            «$fsm/@name»_ERR_LOG_HEX("computing condition «@name» failed", status);
vb@2829
   711
            return invalid_condition;
krista@2271
   712
        }
vb@2829
   713
        if (result) {
vb@2843
   714
            «$fsm/@name»_SERVICE_LOG("condition applies", "«@name»");
vb@2829
   715
        ||
vb@2865
   716
        apply "transition|action|condition|send";
krista@2271
   717
        | }
krista@2271
   718
    }
krista@2271
   719
}
krista@2271
   720