sync/gen_actions_skeleton.ysl2
author Volker Birk <vb@pep.foundation>
Mon, 01 Aug 2016 09:14:47 +0200
branchkeysync
changeset 955 060272dfaaeb
parent 951 dec407deb10e
child 985 087d5d60c082
permissions -rw-r--r--
...
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@939
   104
                Identity partner,
vb@939
   105
                void *extra
vb@690
   106
            )
vb@623
   107
        {
vb@623
   108
            PEP_STATUS status = PEP_STATUS_OK;
vb@623
   109
vb@657
   110
            `` call "paramcheck" with "partner", "parm/partner";
vb@623
   111
vb@623
   112
            // working code
vb@623
   113
vb@951
   114
            free_identity(partner);
vb@951
   115
            // free extra
vb@623
   116
            return status;
vb@650
   117
vb@650
   118
        enomem:
vb@650
   119
            status = PEP_OUT_OF_MEMORY;
vb@650
   120
        error:
vb@951
   121
            free_identity(partner);
vb@951
   122
            // free extra
vb@650
   123
            return status;
vb@650
   124
        }
vb@650
   125
vb@650
   126
        ||
vb@650
   127
    }
vb@650
   128
vb@650
   129
    function "send_action" {
vb@650
   130
        param "action";
vb@690
   131
        param "fsm";
vb@807
   132
        param "filename", "'###'";
vb@650
   133
        const "name", "substring($action/@name, 5, 255)";
vb@650
   134
vb@650
   135
        ||
vb@650
   136
vb@650
   137
        // «$action/@name»() - send «$name» message
vb@650
   138
        //
vb@650
   139
        //  params:
vb@650
   140
        //      session (in)        session handle
vb@690
   141
        //      state (in)          state the state machine is in
vb@807
   142
        `` if "parm"        | //      partner (in)        partner to communicate with
vb@650
   143
        `` if "not(parm)"   | //      partner (in)        (must be NULL)
vb@650
   144
        //
vb@650
   145
        //  returns:
vb@650
   146
        //      PEP_STATUS_OK or any other value on error
vb@650
   147
vb@690
   148
        PEP_STATUS «$action/@name»(
vb@690
   149
                PEP_SESSION session,
vb@690
   150
                «$fsm»_state state,
vb@939
   151
                Identity partner,
vb@939
   152
                void *extra
vb@690
   153
            )
vb@650
   154
        {
vb@650
   155
            PEP_STATUS status = PEP_STATUS_OK;
vb@690
   156
            «$name»_t *msg = NULL;
vb@690
   157
            char *payload = NULL;
vb@690
   158
            message *_message = NULL;
vb@743
   159
            pEp_identity *me = NULL;
vb@955
   160
            `` if "$name='GroupKeys'" |> identity_list *kl = NULL;
vb@650
   161
vb@657
   162
            `` call "paramcheck" with "partner", "parm/partner";
vb@650
   163
vb@690
   164
            assert(session->messageToSend);
vb@951
   165
            if (!session->messageToSend) {
vb@951
   166
                status = PEP_SEND_FUNCTION_NOT_REGISTERED;
vb@951
   167
                goto error;
vb@951
   168
            }
vb@690
   169
vb@690
   170
            msg = («$name»_t *) calloc(1, sizeof(«$name»_t));
vb@650
   171
            assert(msg);
vb@650
   172
            if (!msg)
vb@650
   173
                goto enomem;
vb@650
   174
vb@652
   175
            int32_t seq;
vb@652
   176
            status = sequence_value(session, "DeviceGroup", &seq);
vb@652
   177
            if (status != PEP_STATUS_OK)
vb@652
   178
                goto error;
vb@654
   179
            msg->header.sequence = (long) seq;
vb@652
   180
vb@951
   181
            bool devicegroup = storedGroupKeys(session);
vb@951
   182
            if (devicegroup) { // default is FALSE
vb@951
   183
                BOOLEAN_t *dg = malloc(sizeof(BOOLEAN_t));
vb@951
   184
                assert(dg);
vb@951
   185
                if (!dg)
vb@951
   186
                    goto enomem;
vb@951
   187
vb@951
   188
                *dg = 1;
vb@951
   189
                msg->header.devicegroup = dg;
vb@951
   190
            }
vb@951
   191
vb@951
   192
            msg->header.state = (long) state;
vb@690
   193
vb@743
   194
            me = new_identity(NULL, NULL, NULL, NULL);
vb@654
   195
            if (!me)
vb@654
   196
                goto enomem;
vb@654
   197
            status = myself(session, me);
vb@654
   198
            if (status != PEP_STATUS_OK)
vb@654
   199
                goto error;
vb@951
   200
            if (Identity_from_Struct(me, &msg->header.me) == NULL)
vb@655
   201
                goto enomem;
vb@851
   202
        ||
vb@955
   203
        if "$name='GroupKeys'" {
vb@951
   204
            |
vb@955
   205
            |> status = own_identities_retrieve(session, &kl);
vb@951
   206
            |> if (status != PEP_STATUS_OK)
vb@951
   207
            |>> goto error;
vb@955
   208
            |> if (IdentityList_from_identity_list(kl, &msg->ownIdentities) == NULL)
vb@951
   209
            |>> goto enomem;
vb@951
   210
        }
vb@851
   211
        ||
vb@665
   212
vb@692
   213
            if (asn_check_constraints(&asn_DEF_«$name», msg, NULL, NULL)) {
vb@692
   214
                status = PEP_CONTRAINTS_VIOLATED;
vb@692
   215
                goto error;
vb@692
   216
            }
vb@692
   217
vb@692
   218
            ssize_t size = uper_encode_to_new_buffer(&asn_DEF_«$name»,
vb@690
   219
                    NULL, msg, (void **) &payload);
vb@690
   220
            if (size == -1) {
vb@690
   221
                status = PEP_CANNOT_ENCODE;
vb@690
   222
                goto error;
vb@690
   223
            }
vb@690
   224
vb@690
   225
            status = prepare_message(me, partner, payload, size, &_message);
vb@690
   226
            if (status != PEP_STATUS_OK)
vb@690
   227
                goto error;
vb@690
   228
            payload = NULL;
vb@690
   229
vb@743
   230
            free_identity(me);
vb@743
   231
            me = NULL;
vb@743
   232
vb@807
   233
            status = session->messageToSend(session->«$filename»_obj, _message);
vb@690
   234
vb@690
   235
            free_message(_message);
vb@690
   236
            ASN_STRUCT_FREE(asn_DEF_«$name», msg);
vb@951
   237
            free_identity(partner);
vb@955
   238
            `` if "$name='GroupKeys'" |> free_identity_list(kl);
vb@650
   239
            return status;
vb@650
   240
vb@650
   241
        enomem:
vb@650
   242
            status = PEP_OUT_OF_MEMORY;
vb@650
   243
        error:
vb@650
   244
            ASN_STRUCT_FREE(asn_DEF_«$name», msg);
vb@690
   245
            free(payload);
vb@690
   246
            free_message(_message);
vb@743
   247
            free_identity(me);
vb@955
   248
            free_identity(partner);
vb@955
   249
            `` if "$name='GroupKeys'" |> free_identity_list(kl);
vb@650
   250
            return status;
vb@623
   251
        }
vb@623
   252
vb@623
   253
        ||
vb@623
   254
    }
vb@623
   255
}
vb@623
   256