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