sync/gen_statemachine.ysl2
author Edouard Tisserant <edouard@pep-project.org>
Sun, 11 Sep 2016 22:40:59 +0200
branchkeysync
changeset 1157 7d383681ef2f
parent 1152 fde1a69448d2
child 1162 f8db5c47b7f3
permissions -rw-r--r--
sync_driver now is responsible to duplicate partner identity, and transmit ownership to each state invoked. fsm_DeviceState_inject doesn't take ownership anymore
     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             while(true)
   143             {
   144                 Identity _partner = partner ? identity_dup(partner) : NULL;
   145 
   146                 «@name»_state new_state = fsm_«@name»(session,
   147                     session->«@filename»_state, event, _partner, extra);
   148 
   149                 if (new_state == «@name»_state_invalid_out_of_memory)
   150                     return PEP_OUT_OF_MEMORY;
   151 
   152                 if (new_state < 0)
   153                     return PEP_SYNC_STATEMACHINE_ERROR - new_state;
   154                 
   155                 if (new_state == session->«@filename»_state)
   156                     break;
   157 
   158                 event = Init;
   159                 extra = NULL;
   160                 session->«@filename»_state = new_state;
   161             } 
   162 
   163             return PEP_STATUS_OK;
   164         }
   165 
   166         ||
   167         document "../src/{@filename}_fsm.c", "text"
   168         ||
   169         #include "«@filename»_fsm.h"
   170 
   171         // state machine for «@name»
   172 
   173         «@name»_state fsm_«@name»(
   174                 PEP_SESSION session,
   175                 «@name»_state state,
   176                 «@name»_event event,
   177                 Identity partner,
   178                 void *extra
   179             )
   180         {
   181             int cond_result;
   182             PEP_STATUS status = PEP_STATUS_OK;
   183 
   184             switch (state) {
   185             `` apply "state", 2
   186                 default:
   187                     return («@name»_state) invalid_state;
   188             }
   189 
   190             return state;
   191         }
   192 
   193         ||
   194     }
   195 
   196     template "state"
   197     ||
   198     case «@name»:
   199         switch (event) {
   200         `` if "not(event[@name='Init'])" |>> case Init: break;
   201         `` apply "event", 2
   202             default:
   203                 return («../@name»_state) invalid_event;
   204         }
   205         break;
   206 
   207     ||
   208 
   209     template "event"
   210     ||
   211     case «@name»:
   212     `` apply "action|transition|condition";
   213     `` if "name(*[position()=last()]) != 'transition'" |> break;
   214     ||
   215 
   216     template "action" {
   217         indent(0);
   218         > status = «@name»(session, state, 
   219         choose {
   220             when "parm" > «name(parm/*)»
   221             otherwise > NULL
   222         }
   223         > , NULL);\n
   224         | if (status == PEP_OUT_OF_MEMORY)
   225         |> return (int) invalid_out_of_memory;
   226         | if (status != PEP_STATUS_OK)
   227         |> return (int) invalid_action;
   228     }
   229 
   230     template "condition" {
   231         | cond_result = «@name»(session`apply "parm", 0`);
   232         | if (cond_result < 0)
   233         |> return cond_result;
   234         | if (cond_result) {
   235         apply "action|transition|condition";
   236         | }
   237     }
   238 
   239     template "parm" choose {
   240         when "count(*) = 1"
   241             > , «name(*)»
   242         otherwise
   243             > , «name(*[1])» «name(*[2])»
   244     }
   245 
   246     template "transition" | return «@target»;
   247 }
   248