sync/gen_statemachine.ysl2
author Volker Birk <vb@pep.foundation>
Wed, 31 Aug 2016 09:38:09 +0200
branchkeysync
changeset 1099 113463c3e85a
parent 1047 2a4a2ec1340b
child 1152 fde1a69448d2
permissions -rw-r--r--
adding some security
     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" {
    13         document "../src/Makefile.protocols", "text"
    14             apply "fsm", 0, mode="make";
    15         apply "fsm", 0, mode=gen;
    16     }
    17 
    18     template "fsm", mode=make
    19     ||
    20     «@filename»_fsm.c: ../sync/devicegroup.fsm
    21     \tmake -C ../«@filename»
    22     ||
    23 
    24     template "fsm", mode=gen {
    25         document "../src/{@filename}_fsm.h", "text" {
    26         ||
    27         #pragma once
    28 
    29         // state machine for «@name»
    30 
    31         #include "message_api.h"
    32         
    33         #ifdef __cplusplus
    34         extern "C" {
    35         #endif
    36 
    37         // types
    38 
    39         typedef pEp_identity * Identity;
    40         typedef stringlist_t * Stringlist;
    41         typedef union _param { Identity partner; stringlist_t *keylist; } param_t;
    42 
    43         // error values
    44 
    45         typedef enum _fsm_error {
    46             // these error values are corresponding to
    47             // PEP_SYNC_STATEMACHINE_ERROR - value
    48             invalid_state = -2,
    49             invalid_event = -3,
    50             invalid_condition = -4,
    51             invalid_action = -5,
    52 
    53             // out of memory condition
    54             invalid_out_of_memory = -128
    55         } fsm_error;
    56 
    57         // conditions
    58 
    59         `` for "func:distinctName(condition)" | int «@name»(PEP_SESSION session`apply "parm", 0`);
    60 
    61         // states
    62 
    63         typedef enum _«@name»_state {
    64             // error values also in this namespace
    65             «@name»_state_invalid_state = (int) invalid_state,
    66             «@name»_state_invalid_event = (int) invalid_event,
    67             «@name»_state_invalid_condition = (int) invalid_condition,
    68             «@name»_state_invalid_action = (int) invalid_action,
    69             «@name»_state_invalid_out_of_memory = (int) invalid_out_of_memory,
    70 
    71             «@name»_state_NONE = 0,
    72         `` for "func:distinctName(state)" |> «@name»`if "position()!=last()" > , `
    73         } «@name»_state;
    74 
    75         // events
    76 
    77         typedef enum _«@name»_event {
    78             «@name»_event_NONE = 0,
    79         ||
    80         for "func:distinctName(state/event[not(not(/protocol/fsm/tag/@name=@name))])" {
    81             const "name", "@name";
    82             |> «$name» = «/protocol/fsm/tag[@name=$name]/@id»,
    83         }
    84         for "func:distinctName(state/event[not(/protocol/fsm/tag/@name=@name)])"
    85             |> «@name»`if "position()!=last()" > , `
    86         ||
    87         } «@name»_event;
    88 
    89         // actions
    90 
    91         `` const "name", "@name"
    92         `` for "func:distinctName(//action)" | PEP_STATUS «@name»(PEP_SESSION session, «$name»_state state, Identity partner, void *extra);
    93 
    94         // message receiver
    95         
    96         PEP_STATUS receive_«@name»_msg(PEP_SESSION session, message *msg, PEP_rating rating);
    97 
    98         // state machine
    99 
   100         «@name»_state fsm_«@name»(
   101                 PEP_SESSION session,
   102                 «@name»_state state,
   103                 «@name»_event event,
   104                 Identity partner,
   105                 void *extra
   106             );
   107 
   108         // driver
   109 
   110         DYNAMIC_API PEP_STATUS fsm_«@name»_inject(
   111                 PEP_SESSION session,
   112                 «@name»_event event,
   113                 Identity partner,
   114                 void *extra
   115             );
   116 
   117         #ifdef __cplusplus
   118         }
   119         #endif
   120 
   121         ||
   122         }
   123         document "../src/{@filename}_driver.c", "text"
   124         ||
   125         // Driver for «@name» state machine
   126 
   127         #include <assert.h>
   128         #include "pEp_internal.h"
   129 
   130 
   131         DYNAMIC_API PEP_STATUS fsm_«@name»_inject(
   132                 PEP_SESSION session,
   133                 «@name»_event event,
   134                 Identity partner,
   135                 void *extra
   136             )
   137         {
   138             assert(session);
   139             if (!session)
   140                 return PEP_ILLEGAL_VALUE;
   141 
   142             «@name»_state state = fsm_«@name»(session,
   143                     session->«@filename»_state, event, partner, extra);
   144             if (state == «@name»_state_invalid_out_of_memory)
   145                 return PEP_OUT_OF_MEMORY;
   146             if (state < 0)
   147                 return PEP_SYNC_STATEMACHINE_ERROR - state;
   148 
   149             session->«@filename»_state = state;
   150             return PEP_STATUS_OK;
   151         }
   152 
   153         ||
   154         document "../src/{@filename}_fsm.c", "text"
   155         ||
   156         #include "«@filename»_fsm.h"
   157 
   158         // state machine for «@name»
   159 
   160         «@name»_state fsm_«@name»(
   161                 PEP_SESSION session,
   162                 «@name»_state state,
   163                 «@name»_event event,
   164                 Identity partner,
   165                 void *extra
   166             )
   167         {
   168             int cond_result;
   169             PEP_STATUS status = PEP_STATUS_OK;
   170 
   171             switch (state) {
   172             `` apply "state", 2
   173                 default:
   174                     return («@name»_state) invalid_state;
   175             }
   176 
   177             return state;
   178         }
   179 
   180         ||
   181     }
   182 
   183     template "state"
   184     ||
   185     case «@name»:
   186         switch (event) {
   187         `` apply "event", 2
   188             default:
   189                 return («../@name»_state) invalid_event;
   190         }
   191         break;
   192 
   193     ||
   194 
   195     template "event"
   196     ||
   197     case «@name»:
   198     `` apply "action|transition|condition";
   199     `` if "name(*[position()=last()]) != 'transition'" |> break;
   200     ||
   201 
   202     template "action" {
   203         indent(0);
   204         > status = «@name»(session, state, 
   205         choose {
   206             when "parm" > «name(parm/*)»
   207             otherwise > NULL
   208         }
   209         > , NULL);\n
   210         | if (status == PEP_OUT_OF_MEMORY)
   211         |> return (int) invalid_out_of_memory;
   212         | if (status != PEP_STATUS_OK)
   213         |> return (int) invalid_action;
   214     }
   215 
   216     template "condition" {
   217         | cond_result = «@name»(session`apply "parm", 0`);
   218         | if (cond_result < 0)
   219         |> return cond_result;
   220         | if (cond_result) {
   221         apply "action|transition|condition";
   222         | }
   223     }
   224 
   225     template "parm" choose {
   226         when "count(*) = 1"
   227             > , «name(*)»
   228         otherwise
   229             > , «name(*[1])» «name(*[2])»
   230     }
   231 
   232     template "transition" | return «@target»;
   233 }
   234