sync/gen_actions_skeleton.ysl2
author Roker <roker@pep-project.org>
Tue, 20 Sep 2016 15:54:57 +0200
branchroker-linux
changeset 1168 4d4e3ecbaf8b
parent 883 096ff09932ad
child 939 ed7dfdae3031
permissions -rw-r--r--
merge "default" into my branch
     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 ./functions.ysl2
    11 
    12     template "/protocol" {
    13         apply "fsm", mode=send, 0;
    14         apply "fsm", mode=other, 0;
    15     }
    16 
    17     template "fsm", mode=send document "../src/{@filename}_send_actions.c", "text" {
    18         const "name", "@name";
    19         const "filename", "@filename";
    20         ||
    21         // Send Actions for «@name» state machine
    22 
    23         #include <assert.h>
    24         #include "pEp_internal.h"
    25         #include "keymanagement.h"
    26         #include "message.h"
    27         #include "«@filename»_fsm.h"
    28         #include "baseprotocol.h"
    29         #include "map_asn1.h"
    30         ||
    31         for "func:distinctName(//action)"
    32             if "substring(@name, 1, 4) = 'send'"
    33                 | #include "../asn.1/«substring(@name, 5, 255)».h"
    34         |
    35         for "func:distinctName(//action)"
    36             if "substring(@name, 1, 4) = 'send'"
    37                 call "send_action"
    38                     with "action", ".",
    39                     with "fsm", "$name",
    40                     with "filename", "$filename";
    41     }
    42 
    43     template "fsm", mode=other document "../src/{@filename}_actions.c.skeleton", "text" {
    44         const "name", "@name";
    45         const "filename", "@filename";
    46         ||
    47         // Actions for «@name» state machine
    48 
    49         #include <assert.h>
    50         #include "pEp_internal.h"
    51         #include "keymanagement.h"
    52         #include "message.h"
    53         #include "«@filename»_fsm.h"
    54 
    55         ||
    56         for "func:distinctName(//action)"
    57             if "substring(@name, 1, 4) != 'send'"
    58                 call "other_action"
    59                     with "action", ".",
    60                     with "fsm", "$name",
    61                     with "filename", "$filename";
    62     }
    63 
    64     function "paramcheck" {
    65         param "partner";
    66         |> assert(session);
    67         choose {
    68             when "$partner"
    69             ||
    70                 assert(partner);
    71                 if (!(session && partner))
    72                     return PEP_ILLEGAL_VALUE;
    73             ||
    74             otherwise
    75             ||
    76                 assert(!partner);
    77                 if (!(session && !partner))
    78                     return PEP_ILLEGAL_VALUE;
    79             ||
    80         }
    81     }
    82 
    83     function "other_action" {
    84         param "action";
    85         param "fsm";
    86         param "filename", "'###'";
    87 
    88         ||
    89 
    90         // «$action/@name»() - 
    91         //
    92         //  params:
    93         //      session (in)        session handle
    94         //      state (in)          state the state machine is in
    95         `` if "parm"        | //      partner (in)        partner to communicate with
    96         `` if "not(parm)"   | //      partner (in)        (must be NULL)
    97         //
    98         //  returns:
    99         //      PEP_STATUS_OK or any other value on error
   100 
   101         PEP_STATUS «$action/@name»(
   102                 PEP_SESSION session,
   103                 «$fsm»_state state,
   104                 const Identity partner
   105             )
   106         {
   107             PEP_STATUS status = PEP_STATUS_OK;
   108 
   109             `` call "paramcheck" with "partner", "parm/partner";
   110 
   111             // working code
   112 
   113 
   114             return status;
   115 
   116         enomem:
   117             status = PEP_OUT_OF_MEMORY;
   118         error:
   119             // free...
   120             return status;
   121         }
   122 
   123         ||
   124     }
   125 
   126     function "send_action" {
   127         param "action";
   128         param "fsm";
   129         param "filename", "'###'";
   130         const "name", "substring($action/@name, 5, 255)";
   131 
   132         ||
   133 
   134         // «$action/@name»() - send «$name» message
   135         //
   136         //  params:
   137         //      session (in)        session handle
   138         //      state (in)          state the state machine is in
   139         `` if "parm"        | //      partner (in)        partner to communicate with
   140         `` if "not(parm)"   | //      partner (in)        (must be NULL)
   141         //
   142         //  returns:
   143         //      PEP_STATUS_OK or any other value on error
   144 
   145         PEP_STATUS «$action/@name»(
   146                 PEP_SESSION session,
   147                 «$fsm»_state state,
   148                 const Identity partner
   149             )
   150         {
   151             PEP_STATUS status = PEP_STATUS_OK;
   152             «$name»_t *msg = NULL;
   153             char *payload = NULL;
   154             message *_message = NULL;
   155             pEp_identity *me = NULL;
   156 
   157             `` call "paramcheck" with "partner", "parm/partner";
   158 
   159             assert(session->messageToSend);
   160             if (!session->messageToSend)
   161                 return PEP_SEND_FUNCTION_NOT_REGISTERED;
   162 
   163             msg = («$name»_t *) calloc(1, sizeof(«$name»_t));
   164             assert(msg);
   165             if (!msg)
   166                 goto enomem;
   167 
   168             int32_t seq;
   169             status = sequence_value(session, "DeviceGroup", &seq);
   170             if (status != PEP_STATUS_OK)
   171                 goto error;
   172             msg->header.sequence = (long) seq;
   173 
   174             msg->state = (long) state;
   175 
   176             me = new_identity(NULL, NULL, NULL, NULL);
   177             if (!me)
   178                 goto enomem;
   179             status = myself(session, me);
   180             if (status != PEP_STATUS_OK)
   181                 goto error;
   182             if (Identity_from_Struct(me, &msg->me) == NULL)
   183                 goto enomem;
   184         ||
   185         if "parm or $name='OwnKeys'" |
   186         if "parm/partner"   |> if (Identity_from_Struct(partner, &msg->partner) == NULL)
   187         if "parm/partner"   |>> goto enomem;
   188         if "$name='OwnKeys'"|> stringlist_t *sl;
   189         if "$name='OwnKeys'"|> status = own_key_retrieve(session, &sl);
   190         if "$name='OwnKeys'"|> if (status != PEP_STATUS_OK)
   191         if "$name='OwnKeys'"|>> goto error;
   192         if "$name='OwnKeys'"|> if (KeyList_from_stringlist(sl, &msg->keylist) == NULL)
   193         if "$name='OwnKeys'"|>> goto enomem;
   194         ||
   195 
   196             if (asn_check_constraints(&asn_DEF_«$name», msg, NULL, NULL)) {
   197                 status = PEP_CONTRAINTS_VIOLATED;
   198                 goto error;
   199             }
   200 
   201             ssize_t size = uper_encode_to_new_buffer(&asn_DEF_«$name»,
   202                     NULL, msg, (void **) &payload);
   203             if (size == -1) {
   204                 status = PEP_CANNOT_ENCODE;
   205                 goto error;
   206             }
   207 
   208             status = prepare_message(me, partner, payload, size, &_message);
   209             if (status != PEP_STATUS_OK)
   210                 goto error;
   211             payload = NULL;
   212 
   213             free_identity(me);
   214             me = NULL;
   215 
   216             status = session->messageToSend(session->«$filename»_obj, _message);
   217 
   218             free_message(_message);
   219             ASN_STRUCT_FREE(asn_DEF_«$name», msg);
   220 
   221             return status;
   222 
   223         enomem:
   224             status = PEP_OUT_OF_MEMORY;
   225         error:
   226             ASN_STRUCT_FREE(asn_DEF_«$name», msg);
   227             free(payload);
   228             free_message(_message);
   229             free_identity(me);
   230             return status;
   231         }
   232 
   233         ||
   234     }
   235 }
   236