sync/gen_actions_skeleton.ysl2
author Krista Grothoff <krista@pep-project.org>
Mon, 24 Oct 2016 14:35:42 +0200
branchENGINE-128
changeset 1325 5f2e643a4fd7
parent 1281 efff88dcdc59
child 1412 51341b8d69d0
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 actions skeleton
     2 
     3 // Copyleft (c) 2016, p≡p foundation
     4 
     5 // Written by Volker Birk
     6 
     7 include yslt.yml2
     8 
     9 tstylesheet {
    10     include standardlib.ysl2
    11     include ./functions.ysl2
    12 
    13     template "/protocol" {
    14         apply "fsm", mode=send, 0;
    15         apply "fsm", mode=other, 0;
    16     }
    17 
    18     template "fsm", mode=send document "../src/{@filename}_send_actions.c", "text" {
    19         const "name", "@name";
    20         const "filename", "@filename";
    21         ||
    22         // Send Actions for «@name» state machine
    23 
    24         #include <assert.h>
    25         #include "pEp_internal.h"
    26         #include "keymanagement.h"
    27         #include "message.h"
    28         #include "«@filename»_fsm.h"
    29         #include "baseprotocol.h"
    30         #include "map_asn1.h"
    31         #include "../asn.1/DeviceGroup-Protocol.h"
    32         #include "sync_impl.h"
    33         ||
    34         for "func:distinctName(//action)"
    35             if "substring(@name, 1, 4) = 'send'"
    36                 | #include "../asn.1/«substring(@name, 5, 255)».h"
    37         |
    38         for "func:distinctName(//action)"
    39             if "substring(@name, 1, 4) = 'send'"
    40                 call "send_action"
    41                     with "action", ".",
    42                     with "fsm", "$name",
    43                     with "filename", "$filename";
    44     }
    45 
    46     template "fsm", mode=other document "../src/{@filename}_actions.c.skeleton", "text" {
    47         const "name", "@name";
    48         const "filename", "@filename";
    49         ||
    50         // Actions for «@name» state machine
    51 
    52         #include <assert.h>
    53         #include "pEp_internal.h"
    54         #include "keymanagement.h"
    55         #include "message.h"
    56         #include "«@filename»_fsm.h"
    57         #include "../asn.1/DeviceGroup-Protocol.h"
    58 
    59         ||
    60         for "func:distinctName(//action)"
    61             if "substring(@name, 1, 4) != 'send'"
    62                 call "other_action"
    63                     with "action", ".",
    64                     with "fsm", "$name",
    65                     with "filename", "$filename";
    66     }
    67 
    68     function "paramcheck" {
    69         param "partner";
    70         |> assert(session);
    71         choose {
    72             when "$partner"
    73             ||
    74                 assert(partner);
    75                 if (!(session && partner))
    76                     return PEP_ILLEGAL_VALUE;
    77             ||
    78             otherwise
    79             ||
    80                 assert(!partner);
    81                 if (!(session && !partner))
    82                     return PEP_ILLEGAL_VALUE;
    83             ||
    84         }
    85     }
    86 
    87     function "other_action" {
    88         param "action";
    89         param "fsm";
    90         param "filename", "'###'";
    91 
    92         ||
    93 
    94         // «$action/@name»() - 
    95         //
    96         //  params:
    97         //      session (in)        session handle
    98         //      state (in)          state the state machine is in
    99         `` if "parm"        | //      partner (in)        partner to communicate with
   100         `` if "not(parm)"   | //      partner (in)        (must be NULL)
   101         //
   102         //  returns:
   103         //      PEP_STATUS_OK or any other value on error
   104 
   105         PEP_STATUS «$action/@name»(
   106                 PEP_SESSION session,
   107                 «$fsm»_state state,
   108                 Identity partner,
   109                 void *extra
   110             )
   111         {
   112             PEP_STATUS status = PEP_STATUS_OK;
   113 
   114             `` call "paramcheck" with "partner", "parm/partner";
   115 
   116             // working code
   117 
   118             // free extra
   119             return status;
   120 
   121         enomem:
   122             status = PEP_OUT_OF_MEMORY;
   123         error:
   124             // free extra
   125             return status;
   126         }
   127 
   128         ||
   129     }
   130 
   131     function "send_action" {
   132         param "action";
   133         param "fsm";
   134         param "filename", "'###'";
   135         const "name", "substring($action/@name, 5, 255)";
   136         const "lname", "concat(yml:lcase(substring($name, 1, 1)), substring($name, 2))";
   137 
   138         ||
   139 
   140         // «$action/@name»() - send «$name» message
   141         //
   142         //  params:
   143         //      session (in)        session handle
   144         //      state (in)          state the state machine is in
   145         `` if "parm"        | //      partner (in)        partner to communicate with
   146         `` if "not(parm)"   | //      partner (in)        (must be NULL)
   147         //
   148         //  returns:
   149         //      PEP_STATUS_OK or any other value on error
   150 
   151         PEP_STATUS «$action/@name»(
   152                 PEP_SESSION session,
   153                 «$fsm»_state state,
   154                 Identity partner,
   155                 void *extra
   156             )
   157         {
   158             assert(session && state);
   159             if (!(session && state))
   160                 return PEP_ILLEGAL_VALUE;
   161 
   162             PEP_STATUS status = PEP_STATUS_OK;
   163             `` if "$name='GroupKeys' or $name='GroupUpdate'" |> identity_list *kl = new_identity_list(NULL);
   164 
   165             DeviceGroup_Protocol_t *msg = new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR_«$lname»);
   166             if (!msg)
   167                 goto enomem;
   168         ||
   169         choose {
   170             when "$name='GroupKeys' or $name='GroupUpdate'" {
   171                 |
   172                 |> status = own_identities_retrieve(session, &kl);
   173                 |> if (status != PEP_STATUS_OK)
   174                 |>> goto error;
   175                 |> if (IdentityList_from_identity_list(kl, &msg->payload.choice.«$lname».ownIdentities) == NULL)
   176                 |>> goto enomem;
   177             }
   178         }
   179         choose {
   180             when "$name='GroupKeys' or $name='HandshakeRequest'" {
   181                 |
   182                 |> if (Identity_from_Struct(partner,
   183                 |>                          &msg->payload.choice.«$lname».partner) == NULL)
   184                 |>> goto enomem;
   185             }
   186         }
   187         ||
   188 
   189         ||
   190         choose {
   191             when "count(/protocol/unencrypted/*[name()=$action/@name]) = 0"
   192                 |> bool encrypted = true;
   193             otherwise
   194                 |> bool encrypted = false;
   195         }
   196         choose {
   197             when "count(/protocol/broadcast/*[name()=$action/@name]) = 0"
   198                 |> status = unicast_msg(session, partner, state, msg, encrypted);
   199             otherwise
   200                 |> status = multicast_self_msg(session, state, msg, encrypted);
   201         }
   202         ||
   203             if (status != PEP_STATUS_OK)
   204                 goto error;
   205 
   206             `` if "$name='GroupKeys' or $name='GroupUpdate'" |> free_identity_list(kl);
   207             free_DeviceGroup_Protocol_msg(msg);
   208             return PEP_STATUS_OK;
   209 
   210         enomem:
   211             status = PEP_OUT_OF_MEMORY;
   212         error:
   213             free_DeviceGroup_Protocol_msg(msg);
   214             `` if "$name='GroupKeys'" |> free_identity_list(kl);
   215             return status;
   216         }
   217 
   218         ||
   219     }
   220 }
   221