sync/gen_actions.ysl2
changeset 1467 ff7c60d14af0
parent 1412 51341b8d69d0
child 1477 c0d761ae1fd9
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/sync/gen_actions.ysl2	Thu Dec 08 00:51:41 2016 +0100
     1.3 @@ -0,0 +1,221 @@
     1.4 +// generate actions skeleton
     1.5 +
     1.6 +// Copyleft (c) 2016, p≡p foundation
     1.7 +
     1.8 +// Written by Volker Birk
     1.9 +
    1.10 +include yslt.yml2
    1.11 +
    1.12 +tstylesheet {
    1.13 +    include standardlib.ysl2
    1.14 +    include ./functions.ysl2
    1.15 +
    1.16 +    template "/protocol" {
    1.17 +        apply "fsm", mode=send, 0;
    1.18 +        apply "fsm", mode=other, 0;
    1.19 +    }
    1.20 +
    1.21 +    template "fsm", mode=send document "generated/{@filename}_send_actions.c", "text" {
    1.22 +        const "name", "@name";
    1.23 +        const "filename", "@filename";
    1.24 +        ||
    1.25 +        // Send Actions for «@name» state machine
    1.26 +
    1.27 +        #include <assert.h>
    1.28 +        #include "pEp_internal.h"
    1.29 +        #include "keymanagement.h"
    1.30 +        #include "message.h"
    1.31 +        #include "«@filename»_fsm.h"
    1.32 +        #include "baseprotocol.h"
    1.33 +        #include "map_asn1.h"
    1.34 +        #include "../asn.1/DeviceGroup-Protocol.h"
    1.35 +        #include "sync_impl.h"
    1.36 +        ||
    1.37 +        for "func:distinctName(//action)"
    1.38 +            if "substring(@name, 1, 4) = 'send'"
    1.39 +                | #include "../asn.1/«substring(@name, 5, 255)».h"
    1.40 +        |
    1.41 +        for "func:distinctName(//action)"
    1.42 +            if "substring(@name, 1, 4) = 'send'"
    1.43 +                call "send_action"
    1.44 +                    with "action", ".",
    1.45 +                    with "fsm", "$name",
    1.46 +                    with "filename", "$filename";
    1.47 +    }
    1.48 +
    1.49 +    template "fsm", mode=other document "skeletons/{@filename}_actions.c", "text" {
    1.50 +        const "name", "@name";
    1.51 +        const "filename", "@filename";
    1.52 +        ||
    1.53 +        // Actions for «@name» state machine
    1.54 +
    1.55 +        #include <assert.h>
    1.56 +        #include "pEp_internal.h"
    1.57 +        #include "keymanagement.h"
    1.58 +        #include "message.h"
    1.59 +        #include "«@filename»_fsm.h"
    1.60 +        #include "../asn.1/DeviceGroup-Protocol.h"
    1.61 +
    1.62 +        ||
    1.63 +        for "func:distinctName(//action)"
    1.64 +            if "substring(@name, 1, 4) != 'send'"
    1.65 +                call "other_action"
    1.66 +                    with "action", ".",
    1.67 +                    with "fsm", "$name",
    1.68 +                    with "filename", "$filename";
    1.69 +    }
    1.70 +
    1.71 +    function "paramcheck" {
    1.72 +        param "partner";
    1.73 +        |> assert(session);
    1.74 +        choose {
    1.75 +            when "$partner"
    1.76 +            ||
    1.77 +                assert(partner);
    1.78 +                if (!(session && partner))
    1.79 +                    return PEP_ILLEGAL_VALUE;
    1.80 +            ||
    1.81 +            otherwise
    1.82 +            ||
    1.83 +                assert(!partner);
    1.84 +                if (!(session && !partner))
    1.85 +                    return PEP_ILLEGAL_VALUE;
    1.86 +            ||
    1.87 +        }
    1.88 +    }
    1.89 +
    1.90 +    function "other_action" {
    1.91 +        param "action";
    1.92 +        param "fsm";
    1.93 +        param "filename", "'###'";
    1.94 +
    1.95 +        ||
    1.96 +
    1.97 +        // «$action/@name»() - 
    1.98 +        //
    1.99 +        //  params:
   1.100 +        //      session (in)        session handle
   1.101 +        //      state (in)          state the state machine is in
   1.102 +        `` if "parm"        | //      partner (in)        partner to communicate with
   1.103 +        `` if "not(parm)"   | //      partner (in)        (must be NULL)
   1.104 +        //
   1.105 +        //  returns:
   1.106 +        //      PEP_STATUS_OK or any other value on error
   1.107 +
   1.108 +        PEP_STATUS «$action/@name»(
   1.109 +                PEP_SESSION session,
   1.110 +                «$fsm»_state state,
   1.111 +                Identity partner,
   1.112 +                void *extra
   1.113 +            )
   1.114 +        {
   1.115 +            PEP_STATUS status = PEP_STATUS_OK;
   1.116 +
   1.117 +            `` call "paramcheck" with "partner", "parm/partner";
   1.118 +
   1.119 +            // working code
   1.120 +
   1.121 +            // free extra
   1.122 +            return status;
   1.123 +
   1.124 +        enomem:
   1.125 +            status = PEP_OUT_OF_MEMORY;
   1.126 +        error:
   1.127 +            // free extra
   1.128 +            return status;
   1.129 +        }
   1.130 +
   1.131 +        ||
   1.132 +    }
   1.133 +
   1.134 +    function "send_action" {
   1.135 +        param "action";
   1.136 +        param "fsm";
   1.137 +        param "filename", "'###'";
   1.138 +        const "name", "substring($action/@name, 5, 255)";
   1.139 +        const "lname", "concat(yml:lcase(substring($name, 1, 1)), substring($name, 2))";
   1.140 +
   1.141 +        ||
   1.142 +
   1.143 +        // «$action/@name»() - send «$name» message
   1.144 +        //
   1.145 +        //  params:
   1.146 +        //      session (in)        session handle
   1.147 +        //      state (in)          state the state machine is in
   1.148 +        `` if "parm"        | //      partner (in)        partner to communicate with
   1.149 +        `` if "not(parm)"   | //      partner (in)        (must be NULL)
   1.150 +        //
   1.151 +        //  returns:
   1.152 +        //      PEP_STATUS_OK or any other value on error
   1.153 +
   1.154 +        PEP_STATUS «$action/@name»(
   1.155 +                PEP_SESSION session,
   1.156 +                «$fsm»_state state,
   1.157 +                Identity partner,
   1.158 +                void *extra
   1.159 +            )
   1.160 +        {
   1.161 +            assert(session && state);
   1.162 +            if (!(session && state))
   1.163 +                return PEP_ILLEGAL_VALUE;
   1.164 +
   1.165 +            PEP_STATUS status = PEP_STATUS_OK;
   1.166 +            `` if "$name='GroupKeys' or $name='GroupUpdate'" |> identity_list *kl = new_identity_list(NULL);
   1.167 +
   1.168 +            DeviceGroup_Protocol_t *msg = new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR_«$lname»);
   1.169 +            if (!msg)
   1.170 +                goto enomem;
   1.171 +        ||
   1.172 +        choose {
   1.173 +            when "$name='GroupKeys' or $name='GroupUpdate'" {
   1.174 +                |
   1.175 +                |> status = _own_identities_retrieve(session, &kl, PEP_idf_not_for_sync);
   1.176 +                |> if (status != PEP_STATUS_OK)
   1.177 +                |>> goto error;
   1.178 +                |> if (IdentityList_from_identity_list(kl, &msg->payload.choice.«$lname».ownIdentities) == NULL)
   1.179 +                |>> goto enomem;
   1.180 +            }
   1.181 +        }
   1.182 +        choose {
   1.183 +            when "$name='GroupKeys' or $name='HandshakeRequest'" {
   1.184 +                |
   1.185 +                |> if (Identity_from_Struct(partner,
   1.186 +                |>                          &msg->payload.choice.«$lname».partner) == NULL)
   1.187 +                |>> goto enomem;
   1.188 +            }
   1.189 +        }
   1.190 +        ||
   1.191 +
   1.192 +        ||
   1.193 +        choose {
   1.194 +            when "count(/protocol/unencrypted/*[name()=$action/@name]) = 0"
   1.195 +                |> bool encrypted = true;
   1.196 +            otherwise
   1.197 +                |> bool encrypted = false;
   1.198 +        }
   1.199 +        choose {
   1.200 +            when "count(/protocol/broadcast/*[name()=$action/@name]) = 0"
   1.201 +                |> status = unicast_msg(session, partner, state, msg, encrypted);
   1.202 +            otherwise
   1.203 +                |> status = multicast_self_msg(session, state, msg, encrypted);
   1.204 +        }
   1.205 +        ||
   1.206 +            if (status != PEP_STATUS_OK)
   1.207 +                goto error;
   1.208 +
   1.209 +            `` if "$name='GroupKeys' or $name='GroupUpdate'" |> free_identity_list(kl);
   1.210 +            free_DeviceGroup_Protocol_msg(msg);
   1.211 +            return PEP_STATUS_OK;
   1.212 +
   1.213 +        enomem:
   1.214 +            status = PEP_OUT_OF_MEMORY;
   1.215 +        error:
   1.216 +            free_DeviceGroup_Protocol_msg(msg);
   1.217 +            `` if "$name='GroupKeys'" |> free_identity_list(kl);
   1.218 +            return status;
   1.219 +        }
   1.220 +
   1.221 +        ||
   1.222 +    }
   1.223 +}
   1.224 +