sync/gen_statemachine.ysl2
author Volker Birk <vb@pep-project.org>
Sun, 15 May 2016 21:23:36 +0200
changeset 627 ef1fdfde8ced
parent 626 64a89bd12469
child 654 0aa96447e38f
permissions -rw-r--r--
adding session handles
     1 // generate state machine code
     2 
     3 // Copyleft (c) 2016, p≡p foundation
     4 
     5 // Written by Volker Birk
     6 
     7 include yslt.yml2
     8 
     9 tstylesheet {
    10     include ./functions.ysl2
    11 
    12     template "/protocol/fsm" {
    13         document "../src/sync_fsm.h", "text"
    14         ||
    15         #pragma once
    16 
    17         // state machine for «@name»
    18 
    19         #include "pEpEngine.h"
    20 
    21         // types
    22 
    23         typedef pEp_identity * Identity;
    24         typedef union _param { const Identity partner; const stringlist_t *keylist; } param_t;
    25 
    26         // error values
    27 
    28         typedef enum _fsm_error {
    29             invalid_state = -1,
    30             invalid_event = -2
    31         } fsm_error;
    32 
    33         // states
    34 
    35         typedef enum _«@name»_state {
    36         `` for "func:distinctName(state)" |> «@name»`if "position()!=last()" > , `
    37         } «@name»_state;
    38 
    39         // events
    40 
    41         typedef enum _«@name»_event {
    42         `` for "func:distinctName(state/event)" |> «@name»`if "position()!=last()" > , `
    43         } «@name»_event;
    44 
    45         // actions
    46 
    47         `` for "func:distinctName(//action)" | PEP_STATUS «@name»(PEP_SESSION session, const Identity partner);
    48 
    49         // state machine
    50 
    51         «@name»_state fsm_«@name»(
    52                 PEP_SESSION session,
    53                 «@name»_state state,
    54                 «@name»_event event,
    55                 const Identity partner
    56             );
    57 
    58         // driver
    59 
    60         PEP_STATUS fsm_«@name»_inject(PEP_SESSION session, «@name»_event event);
    61 
    62         ||
    63         ||
    64         #include "sync_fsm.h"
    65 
    66         // state machine for «@name»
    67 
    68         «@name»_state fsm_«@name»(
    69                 PEP_SESSION session,
    70                 «@name»_state state,
    71                 «@name»_event event,
    72                 const Identity partner
    73             )
    74         {
    75             switch (state) {
    76                 `` apply "state"
    77                 default:
    78                     return invalid_state;
    79             }
    80 
    81             return state;
    82         }
    83 
    84         ||
    85     }
    86 
    87     template "state"
    88     ||
    89     case «@name»:
    90         switch (event) {
    91         `` apply "event", 2
    92         default:
    93             return invalid_event;
    94         }
    95         break;
    96 
    97     ||
    98 
    99     template "event"
   100     ||
   101     case «@name»:
   102     `` apply "action|transition";
   103     `` if "name(*[position()=last()]) != 'transition'" |> break;
   104     ||
   105 
   106     template "action" {
   107         indent(0);
   108         > «@name»(session, 
   109         choose {
   110             when "parm" > «name(parm/*)»
   111             otherwise > NULL
   112         }
   113         > );\n
   114     }
   115 
   116     template "transition" | return «@target»;
   117 }
   118