sync/gen_statemachine.ysl2
author Edouard Tisserant
Thu, 12 May 2016 12:58:40 +0200
changeset 610 1e3d39516f43
parent 609 a22a400611ca
child 617 0caa732cf9a4
permissions -rw-r--r--
Sync : Moved decode/encode to separate target file(s)
     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 
    11     template "/protocol/fsm" {
    12         document "../src/sync_fsm.h", "text"
    13         ||
    14         #pragma once
    15 
    16         // state machine for «@name»
    17 
    18         #include "pEpEngine.h"
    19 
    20         // types
    21 
    22         typedef pEp_identity * Identity;
    23         typedef union _param { const Identity partner; const stringlist_t *keylist; } param_t;
    24 
    25         // error values
    26 
    27         typedef enum _fsm_error {
    28             invalid_state = -1,
    29             invalid_event = -2
    30         } fsm_error;
    31 
    32         // states
    33 
    34         typedef enum _«@name»_state {
    35         `` for "set:distinct(state/@name)" |> «.»`if "position()!=last()" > , `
    36         } «@name»_state;
    37 
    38         // events
    39 
    40         typedef enum _«@name»_event {
    41         `` for "set:distinct(state/event/@name)" |> «.»`if "position()!=last()" > , `
    42         } «@name»_event;
    43 
    44         // actions
    45 
    46         `` for "set:distinct(state/event//action/@name)" | void «.»(const Identity partner);
    47 
    48         // driver
    49 
    50         void fsm_«@name»_inject(PEP_SESSION session, «@name»_event event);
    51 
    52         ||
    53         ||
    54         #include "sync_fsm.h"
    55 
    56         // state machine for «@name»
    57 
    58         «@name»_state fsm_«@name»(
    59                 «@name»_state state,
    60                 «@name»_event event,
    61                 const Identity partner
    62             )
    63         {
    64             switch (state) {
    65                 `` apply "state"
    66                 default:
    67                     return invalid_state;
    68             }
    69 
    70             return state;
    71         }
    72 
    73         ||
    74     }
    75 
    76     template "state"
    77     ||
    78     case «@name»:
    79         switch (event) {
    80         `` apply "event", 2
    81         default:
    82             return invalid_event;
    83         }
    84         break;
    85 
    86     ||
    87 
    88     template "event"
    89     ||
    90     case «@name»:
    91     `` apply "action|transition";
    92     `` if "name(*[position()=last()]) != 'transition'" |> break;
    93     ||
    94 
    95     template "action" {
    96         indent(0);
    97         > «@name»(
    98         choose {
    99             when "parm" > «name(parm/*)»
   100             otherwise > NULL
   101         }
   102         > );\n
   103     }
   104 
   105     template "transition" | return «@target»;
   106 }
   107