sync/gen_statemachine.ysl2
author Edouard Tisserant <edouard@pep-project.org>
Sun, 18 Sep 2016 16:55:53 +0200
branchkeysync
changeset 1165 05c831cfab65
parent 1162 f8db5c47b7f3
child 1166 f128fb9fe444
permissions -rw-r--r--
Sync message decrypt rating could't be evaluated properly without knowing peer's user_id, itslef encoded inside the encrypted blob. Peer identity rating is then re-evaluated after unpacking.
     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_DeviceState_msg(
    97                 PEP_SESSION session, 
    98                 message *src, 
    99                 PEP_rating rating, 
   100                 stringlist_t *keylist
   101             );
   102 
   103         // state machine
   104 
   105         «@name»_state fsm_«@name»(
   106                 PEP_SESSION session,
   107                 «@name»_state state,
   108                 «@name»_event event,
   109                 Identity partner,
   110                 void *extra
   111             );
   112 
   113         // driver
   114 
   115         DYNAMIC_API PEP_STATUS fsm_«@name»_inject(
   116                 PEP_SESSION session,
   117                 «@name»_event event,
   118                 Identity partner,
   119                 void *extra
   120             );
   121 
   122         #ifdef __cplusplus
   123         }
   124         #endif
   125 
   126         ||
   127         }
   128         document "../src/{@filename}_driver.c", "text"
   129         ||
   130         // Driver for «@name» state machine
   131 
   132         #include <assert.h>
   133         #include "pEp_internal.h"
   134 
   135 
   136         DYNAMIC_API PEP_STATUS fsm_«@name»_inject(
   137                 PEP_SESSION session,
   138                 «@name»_event event,
   139                 Identity partner,
   140                 void *extra
   141             )
   142         {
   143             assert(session);
   144             if (!session)
   145                 return PEP_ILLEGAL_VALUE;
   146 
   147             while(true)
   148             {
   149                 «@name»_state new_state = fsm_«@name»(session,
   150                     session->«@filename»_state, event, partner, extra);
   151 
   152                 if (new_state == «@name»_state_invalid_out_of_memory)
   153                     return PEP_OUT_OF_MEMORY;
   154 
   155                 if (new_state < 0)
   156                     return PEP_SYNC_STATEMACHINE_ERROR - new_state;
   157                 
   158                 if (new_state == session->«@filename»_state)
   159                     break;
   160 
   161                 event = Init;
   162                 extra = NULL;
   163                 session->«@filename»_state = new_state;
   164             } 
   165 
   166             return PEP_STATUS_OK;
   167         }
   168 
   169         ||
   170         document "../src/{@filename}_fsm.c", "text"
   171         ||
   172         #include "«@filename»_fsm.h"
   173 
   174         // state machine for «@name»
   175 
   176         «@name»_state fsm_«@name»(
   177                 PEP_SESSION session,
   178                 «@name»_state state,
   179                 «@name»_event event,
   180                 Identity partner,
   181                 void *extra
   182             )
   183         {
   184             int cond_result;
   185             PEP_STATUS status = PEP_STATUS_OK;
   186 
   187             switch (state) {
   188             `` apply "state", 2
   189                 default:
   190                     return («@name»_state) invalid_state;
   191             }
   192 
   193             return state;
   194         }
   195 
   196         ||
   197     }
   198 
   199     template "state"
   200     ||
   201     case «@name»:
   202         switch (event) {
   203         `` if "not(event[@name='Init'])" |>> case Init: break;
   204         `` apply "event", 2
   205             default:
   206                 return («../@name»_state) invalid_event;
   207         }
   208         break;
   209 
   210     ||
   211 
   212     template "event"
   213     ||
   214     case «@name»:
   215     `` apply "action|transition|condition";
   216     `` if "name(*[position()=last()]) != 'transition'" |> break;
   217     ||
   218 
   219     template "action" {
   220         indent(0);
   221         > status = «@name»(session, state, 
   222         choose {
   223             when "parm" > «name(parm/*)»
   224             otherwise > NULL
   225         }
   226         > , NULL);\n
   227         | if (status == PEP_OUT_OF_MEMORY)
   228         |> return (int) invalid_out_of_memory;
   229         | if (status != PEP_STATUS_OK)
   230         |> return (int) invalid_action;
   231     }
   232 
   233     template "condition" {
   234         | cond_result = «@name»(session`apply "parm", 0`);
   235         | if (cond_result < 0)
   236         |> return cond_result;
   237         | if (cond_result) {
   238         apply "action|transition|condition";
   239         | }
   240     }
   241 
   242     template "parm" choose {
   243         when "count(*) = 1"
   244             > , «name(*)»
   245         otherwise
   246             > , «name(*[1])» «name(*[2])»
   247     }
   248 
   249     template "transition" | return «@target»;
   250 }
   251