sync/gen_actions_skeleton.ysl2
author Volker Birk <vb@pep-project.org>
Sun, 29 May 2016 12:21:50 +0200
changeset 690 3ae86b68fdfe
parent 666 4bac9007324a
child 692 6f7f1b7627bc
permissions -rw-r--r--
adding sending
     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/fsm" {
    13     ||
    14     `` const "name", "@name"
    15     // Actions for «@name» state machine
    16 
    17     #include <assert.h>
    18     #include "pEp_internal.h"
    19     #include "keymanagement.h"
    20     #include "message.h"
    21     #include "sync_fsm.h"
    22     #include "baseprotocol.h"
    23     #include "map_asn1.h"
    24     `` for "func:distinctName(//action)" if "substring(@name, 1, 4) = 'send'" | #include "../asn.1/«substring(@name, 5, 255)».h"
    25 
    26     `` for "func:distinctName(//action)" call "action" with "action", ".", with "fsm", "$name";
    27 
    28     ||
    29     }
    30 
    31     function "action" {
    32         param "action";
    33         param "fsm";
    34         choose {
    35             when "substring($action/@name, 1, 4) = 'send'"
    36                 call "send_action" with "action", "$action",
    37                      with "fsm", "$fsm";
    38             otherwise
    39                 call "other_action" with "action", "$action",
    40                      with "fsm", "$fsm";
    41         }
    42     }
    43 
    44     function "paramcheck" {
    45         param "partner";
    46         |> assert(session);
    47         choose {
    48             when "$partner"
    49             ||
    50                 assert(partner);
    51                 if (!(session && partner))
    52                     return PEP_ILLEGAL_VALUE;
    53             ||
    54             otherwise
    55             ||
    56                 assert(!partner);
    57                 if (!(session && !partner))
    58                     return PEP_ILLEGAL_VALUE;
    59             ||
    60         }
    61     }
    62 
    63     function "other_action" {
    64         param "action";
    65         param "fsm";
    66 
    67         ||
    68 
    69         // «$action/@name»() - 
    70         //
    71         //  params:
    72         //      session (in)        session handle
    73         //      state (in)          state the state machine is in
    74         `` if "parm"        | //      partner (in)        partner in sync
    75         `` if "not(parm)"   | //      partner (in)        (must be NULL)
    76         //
    77         //  returns:
    78         //      PEP_STATUS_OK or any other value on error
    79 
    80         PEP_STATUS «$action/@name»(
    81                 PEP_SESSION session,
    82                 «$fsm»_state state,
    83                 const Identity partner
    84             )
    85         {
    86             PEP_STATUS status = PEP_STATUS_OK;
    87 
    88             `` call "paramcheck" with "partner", "parm/partner";
    89 
    90             // working code
    91 
    92 
    93             return status;
    94 
    95         enomem:
    96             status = PEP_OUT_OF_MEMORY;
    97         error:
    98             // free...
    99             return status;
   100         }
   101 
   102         ||
   103     }
   104 
   105     function "send_action" {
   106         param "action";
   107         param "fsm";
   108         const "name", "substring($action/@name, 5, 255)";
   109 
   110         ||
   111 
   112         // «$action/@name»() - send «$name» message
   113         //
   114         //  params:
   115         //      session (in)        session handle
   116         //      state (in)          state the state machine is in
   117         `` if "parm"        | //      partner (in)        partner in sync
   118         `` if "not(parm)"   | //      partner (in)        (must be NULL)
   119         //
   120         //  returns:
   121         //      PEP_STATUS_OK or any other value on error
   122 
   123         PEP_STATUS «$action/@name»(
   124                 PEP_SESSION session,
   125                 «$fsm»_state state,
   126                 const Identity partner
   127             )
   128         {
   129             PEP_STATUS status = PEP_STATUS_OK;
   130             «$name»_t *msg = NULL;
   131             char *payload = NULL;
   132             message *_message = NULL;
   133 
   134             `` call "paramcheck" with "partner", "parm/partner";
   135 
   136             assert(session->messageToSend);
   137             if (!session->messageToSend)
   138                 return PEP_SEND_FUNCTION_NOT_REGISTERED;
   139 
   140             msg = («$name»_t *) calloc(1, sizeof(«$name»_t));
   141             assert(msg);
   142             if (!msg)
   143                 goto enomem;
   144 
   145             int32_t seq;
   146             status = sequence_value(session, "DeviceGroup", &seq);
   147             if (status != PEP_STATUS_OK)
   148                 goto error;
   149             msg->header.sequence = (long) seq;
   150 
   151             msg->state = (long) state;
   152 
   153             pEp_identity *me = new_identity(NULL, NULL, NULL, NULL);
   154             if (!me)
   155                 goto enomem;
   156             status = myself(session, me);
   157             if (status != PEP_STATUS_OK)
   158                 goto error;
   159             if (Identity_from_Struct(me, &msg->me) == NULL)
   160                 goto enomem;
   161          `` if "parm or $name='OwnKeys'" |
   162          `` if "parm/partner"   |> if (Identity_from_Struct(partner, &msg->partner) == NULL)
   163          `` if "parm/partner"   |>> goto enomem;
   164          `` if "$name='OwnKeys'"|> stringlist_t *sl;
   165          `` if "$name='OwnKeys'"|> status = own_key_retrieve(session, &sl);
   166          `` if "$name='OwnKeys'"|> if (status != PEP_STATUS_OK)
   167          `` if "$name='OwnKeys'"|>> goto error;
   168          `` if "$name='OwnKeys'"|> if (KeyList_from_stringlist(sl, &msg->keylist) == NULL)
   169          `` if "$name='OwnKeys'"|>> goto enomem;
   170 
   171             ssize_t size = uper_encode_to_new_buffer(&asn_DEF_HandshakeRequest,
   172                     NULL, msg, (void **) &payload);
   173             if (size == -1) {
   174                 status = PEP_CANNOT_ENCODE;
   175                 goto error;
   176             }
   177 
   178             status = prepare_message(me, partner, payload, size, &_message);
   179             if (status != PEP_STATUS_OK)
   180                 goto error;
   181             payload = NULL;
   182 
   183             status = session->messageToSend(session->sync_obj, _message);
   184 
   185             free_message(_message);
   186             ASN_STRUCT_FREE(asn_DEF_«$name», msg);
   187 
   188             return status;
   189 
   190         enomem:
   191             status = PEP_OUT_OF_MEMORY;
   192         error:
   193             ASN_STRUCT_FREE(asn_DEF_«$name», msg);
   194             free(payload);
   195             free_message(_message);
   196             return status;
   197         }
   198 
   199         ||
   200     }
   201 }
   202