sync/gen_statemachine.ysl2
author Krista Grothoff <krista@pep-project.org>
Mon, 24 Oct 2016 14:35:42 +0200
branchENGINE-128
changeset 1325 5f2e643a4fd7
parent 1264 7e69c59a659b
child 1355 5cc70d0bc348
permissions -rw-r--r--
ENGINE-128: MIME_encrypt/decrypt convenience functions almost up and running/tested! Need to fix bug in default first.
     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         // event injector
    95 
    96         PEP_STATUS inject_DeviceState_event(
    97             PEP_SESSION session, 
    98             DeviceState_event event,
    99             Identity partner,
   100             void *extra);
   101 
   102         // message receiver
   103         
   104         PEP_STATUS receive_DeviceState_msg(
   105                 PEP_SESSION session, 
   106                 message *src, 
   107                 PEP_rating rating, 
   108                 stringlist_t *keylist
   109             );
   110 
   111         // state machine
   112 
   113         «@name»_state fsm_«@name»(
   114                 PEP_SESSION session,
   115                 «@name»_state state,
   116                 «@name»_event event,
   117                 Identity partner,
   118                 void *extra
   119             );
   120 
   121         // driver
   122 
   123         DYNAMIC_API PEP_STATUS fsm_«@name»_inject(
   124                 PEP_SESSION session,
   125                 «@name»_event event,
   126                 Identity partner,
   127                 void *extra
   128             );
   129 
   130         #ifdef __cplusplus
   131         }
   132         #endif
   133 
   134         ||
   135         }
   136         document "../src/{@filename}_driver.c", "text"
   137         ||
   138         // Driver for «@name» state machine
   139 
   140         #include <assert.h>
   141         #include "pEp_internal.h"
   142 
   143 
   144         DYNAMIC_API PEP_STATUS fsm_«@name»_inject(
   145                 PEP_SESSION session,
   146                 «@name»_event event,
   147                 Identity partner,
   148                 void *extra
   149             )
   150         {
   151             assert(session);
   152             if (!session)
   153                 return PEP_ILLEGAL_VALUE;
   154 
   155             while(true)
   156             {
   157                 «@name»_state new_state = fsm_«@name»(session,
   158                     session->«@filename»_state, event, partner, extra);
   159 
   160                 if (new_state == «@name»_state_invalid_out_of_memory)
   161                     return PEP_OUT_OF_MEMORY;
   162 
   163                 if (new_state < 0)
   164                     return PEP_SYNC_STATEMACHINE_ERROR - new_state;
   165                 
   166                 if (new_state == session->«@filename»_state)
   167                     break;
   168 
   169                 event = Init;
   170                 extra = NULL;
   171                 session->«@filename»_state = new_state;
   172             } 
   173 
   174             return PEP_STATUS_OK;
   175         }
   176 
   177         ||
   178         document "../src/{@filename}_fsm.c", "text"
   179         ||
   180         #include "«@filename»_fsm.h"
   181 
   182         // state machine for «@name»
   183 
   184         «@name»_state fsm_«@name»(
   185                 PEP_SESSION session,
   186                 «@name»_state state,
   187                 «@name»_event event,
   188                 Identity partner,
   189                 void *extra
   190             )
   191         {
   192             int cond_result;
   193             PEP_STATUS status = PEP_STATUS_OK;
   194 
   195             switch (state) {
   196             `` apply "state", 2
   197                 default:
   198                     return («@name»_state) invalid_state;
   199             }
   200 
   201             return state;
   202         }
   203 
   204         ||
   205     }
   206 
   207     template "state"
   208     ||
   209     case «@name»:
   210         switch (event) {
   211         `` if "not(event[@name='Init'])" |>> case Init: break;
   212         `` apply "event", 2
   213             default:
   214                 return («../@name»_state) invalid_event;
   215         }
   216         break;
   217 
   218     ||
   219 
   220     template "event"
   221     ||
   222     case «@name»:
   223     `` apply "action|transition|condition";
   224     `` if "name(*[position()=last()]) != 'transition'" |> break;
   225     ||
   226 
   227     template "action" {
   228         indent(0);
   229         > status = «@name»(session, state, 
   230         choose {
   231             when "parm" > «name(parm/*)»
   232             otherwise > NULL
   233         }
   234         choose {
   235             when "parm[2]" > , extra /*«name(parm[2]/*)»*/
   236             otherwise > , NULL
   237         }
   238         > );\n
   239         | if (status == PEP_OUT_OF_MEMORY)
   240         |> return (int) invalid_out_of_memory;
   241         | if (status != PEP_STATUS_OK)
   242         |> return (int) invalid_action;
   243     }
   244 
   245     template "condition" {
   246         | cond_result = «@name»(session`apply "parm", 0`);
   247         | if (cond_result < 0)
   248         |> return cond_result;
   249         | if (cond_result) {
   250         apply "action|transition|condition";
   251         | }
   252     }
   253 
   254     template "parm" choose {
   255         when "count(*) = 1"
   256             > , «name(*)»
   257         otherwise
   258             > , «name(*[1])» «name(*[2])»
   259     }
   260 
   261     template "transition" | return «@target»;
   262 }
   263