sync/gen_statemachine.ysl2
author Volker Birk <vb@pep-project.org>
Wed, 18 May 2016 12:54:22 +0200
changeset 654 0aa96447e38f
parent 627 ef1fdfde8ced
child 690 3ae86b68fdfe
permissions -rw-r--r--
...
     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         #ifdef __cplusplus
    22         extern "C" {
    23         #endif
    24 
    25         // types
    26 
    27         typedef pEp_identity * Identity;
    28         typedef union _param { const Identity partner; const stringlist_t *keylist; } param_t;
    29 
    30         // error values
    31 
    32         typedef enum _fsm_error {
    33             invalid_state = -1,
    34             invalid_event = -2
    35         } fsm_error;
    36 
    37         // states
    38 
    39         typedef enum _«@name»_state {
    40         `` for "func:distinctName(state)" |> «@name»`if "position()!=last()" > , `
    41         } «@name»_state;
    42 
    43         // events
    44 
    45         typedef enum _«@name»_event {
    46         `` for "func:distinctName(state/event)" |> «@name»`if "position()!=last()" > , `
    47         } «@name»_event;
    48 
    49         // actions
    50 
    51         `` for "func:distinctName(//action)" | PEP_STATUS «@name»(PEP_SESSION session, const Identity partner);
    52 
    53         // state machine
    54 
    55         «@name»_state fsm_«@name»(
    56                 PEP_SESSION session,
    57                 «@name»_state state,
    58                 «@name»_event event,
    59                 const Identity partner
    60             );
    61 
    62         // driver
    63 
    64         PEP_STATUS fsm_«@name»_inject(PEP_SESSION session, «@name»_event event);
    65 
    66         #ifdef __cplusplus
    67         }
    68         #endif
    69 
    70         ||
    71         ||
    72         #include "sync_fsm.h"
    73 
    74         // state machine for «@name»
    75 
    76         «@name»_state fsm_«@name»(
    77                 PEP_SESSION session,
    78                 «@name»_state state,
    79                 «@name»_event event,
    80                 const Identity partner
    81             )
    82         {
    83             switch (state) {
    84                 `` apply "state"
    85                 default:
    86                     return invalid_state;
    87             }
    88 
    89             return state;
    90         }
    91 
    92         ||
    93     }
    94 
    95     template "state"
    96     ||
    97     case «@name»:
    98         switch (event) {
    99         `` apply "event", 2
   100         default:
   101             return invalid_event;
   102         }
   103         break;
   104 
   105     ||
   106 
   107     template "event"
   108     ||
   109     case «@name»:
   110     `` apply "action|transition";
   111     `` if "name(*[position()=last()]) != 'transition'" |> break;
   112     ||
   113 
   114     template "action" {
   115         indent(0);
   116         > «@name»(session, 
   117         choose {
   118             when "parm" > «name(parm/*)»
   119             otherwise > NULL
   120         }
   121         > );\n
   122     }
   123 
   124     template "transition" | return «@target»;
   125 }
   126