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