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