sync/gen_statemachine.ysl2
author Volker Birk <vb@pep-project.org>
Sun, 29 May 2016 12:21:50 +0200
changeset 690 3ae86b68fdfe
parent 654 0aa96447e38f
child 711 d88dea0f5448
permissions -rw-r--r--
adding sending
     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             «@name»_state_NONE = 0,
    41         `` for "func:distinctName(state)" |> «@name»`if "position()!=last()" > , `
    42         } «@name»_state;
    43 
    44         // events
    45 
    46         typedef enum _«@name»_event {
    47             «@name»_event_NONE = 0,
    48         `` for "func:distinctName(state/event)" |> «@name»`if "position()!=last()" > , `
    49         } «@name»_event;
    50 
    51         // actions
    52 
    53         `` const "name", "@name"
    54         `` for "func:distinctName(//action)" | PEP_STATUS «@name»(PEP_SESSION session, «$name»_state state, const Identity partner);
    55 
    56         // state machine
    57 
    58         «@name»_state fsm_«@name»(
    59                 PEP_SESSION session,
    60                 «@name»_state state,
    61                 «@name»_event event,
    62                 const Identity partner,
    63                 «@name»_state state_partner
    64             );
    65 
    66         // driver
    67 
    68         PEP_STATUS fsm_«@name»_inject(
    69                 PEP_SESSION session,
    70                 «@name»_event event,
    71                 Identity partner,
    72                 «@name»_state state_partner
    73             );
    74 
    75         #ifdef __cplusplus
    76         }
    77         #endif
    78 
    79         ||
    80         document "../src/sync_driver.c", "text"
    81         ||
    82         // Driver for «@name» state machine
    83 
    84         #include <assert.h>
    85         #include "pEp_internal.h"
    86 
    87 
    88         PEP_STATUS fsm_«@name»_inject(
    89                 PEP_SESSION session,
    90                 «@name»_event event,
    91                 Identity partner,
    92                 «@name»_state state_partner
    93             )
    94         {
    95             PEP_STATUS status = PEP_STATUS_OK;
    96 
    97             session->sync_state = InitState;
    98             session->sync_state = fsm_«@name»(session, session->sync_state,
    99                     event, partner, state_partner);
   100 
   101             return status;
   102         }
   103 
   104         ||
   105         ||
   106         #include "sync_fsm.h"
   107 
   108         // state machine for «@name»
   109 
   110         «@name»_state fsm_«@name»(
   111                 PEP_SESSION session,
   112                 «@name»_state state,
   113                 «@name»_event event,
   114                 const Identity partner,
   115                 «@name»_state state_partner
   116             )
   117         {
   118             switch (state) {
   119                 `` apply "state"
   120                 default:
   121                     return invalid_state;
   122             }
   123 
   124             return state;
   125         }
   126 
   127         ||
   128     }
   129 
   130     template "state"
   131     ||
   132     case «@name»:
   133         switch (event) {
   134         `` apply "event", 2
   135         default:
   136             return invalid_event;
   137         }
   138         break;
   139 
   140     ||
   141 
   142     template "event"
   143     ||
   144     case «@name»:
   145     `` apply "action|transition";
   146     `` if "name(*[position()=last()]) != 'transition'" |> break;
   147     ||
   148 
   149     template "action" {
   150         indent(0);
   151         > «@name»(session, state, 
   152         choose {
   153             when "parm" > «name(parm/*)»
   154             otherwise > NULL
   155         }
   156         > );\n
   157     }
   158 
   159     template "transition" | return «@target»;
   160 }
   161