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