sync/gen_actions_skeleton.ysl2
author Volker Birk <vb@pep.foundation>
Mon, 08 Aug 2016 17:32:51 +0200
branchkeysync
changeset 985 087d5d60c082
parent 955 060272dfaaeb
child 991 b230b6418b52
permissions -rw-r--r--
moving #include and installing headers
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@985
    30
        #include "../asn.1/DeviceGroup-Protocol.h"
vb@851
    31
        ||
vb@851
    32
        for "func:distinctName(//action)"
vb@851
    33
            if "substring(@name, 1, 4) = 'send'"
vb@851
    34
                | #include "../asn.1/«substring(@name, 5, 255)».h"
vb@851
    35
        |
vb@851
    36
        for "func:distinctName(//action)"
vb@883
    37
            if "substring(@name, 1, 4) = 'send'"
vb@883
    38
                call "send_action"
vb@883
    39
                    with "action", ".",
vb@883
    40
                    with "fsm", "$name",
vb@883
    41
                    with "filename", "$filename";
vb@623
    42
    }
vb@623
    43
vb@883
    44
    template "fsm", mode=other document "../src/{@filename}_actions.c.skeleton", "text" {
vb@883
    45
        const "name", "@name";
vb@883
    46
        const "filename", "@filename";
vb@883
    47
        ||
vb@883
    48
        // Actions for «@name» state machine
vb@883
    49
vb@883
    50
        #include <assert.h>
vb@883
    51
        #include "pEp_internal.h"
vb@883
    52
        #include "keymanagement.h"
vb@883
    53
        #include "message.h"
vb@883
    54
        #include "«@filename»_fsm.h"
vb@985
    55
        #include "../asn.1/DeviceGroup-Protocol.h"
vb@883
    56
vb@883
    57
        ||
vb@883
    58
        for "func:distinctName(//action)"
vb@883
    59
            if "substring(@name, 1, 4) != 'send'"
vb@883
    60
                call "other_action"
vb@883
    61
                    with "action", ".",
vb@883
    62
                    with "fsm", "$name",
vb@851
    63
                    with "filename", "$filename";
vb@650
    64
    }
vb@650
    65
vb@657
    66
    function "paramcheck" {
vb@657
    67
        param "partner";
vb@666
    68
        |> assert(session);
vb@657
    69
        choose {
vb@657
    70
            when "$partner"
vb@657
    71
            ||
vb@665
    72
                assert(partner);
vb@665
    73
                if (!(session && partner))
vb@665
    74
                    return PEP_ILLEGAL_VALUE;
vb@657
    75
            ||
vb@657
    76
            otherwise
vb@657
    77
            ||
vb@665
    78
                assert(!partner);
vb@665
    79
                if (!(session && !partner))
vb@665
    80
                    return PEP_ILLEGAL_VALUE;
vb@657
    81
            ||
vb@657
    82
        }
vb@657
    83
    }
vb@657
    84
vb@650
    85
    function "other_action" {
vb@650
    86
        param "action";
vb@690
    87
        param "fsm";
vb@807
    88
        param "filename", "'###'";
vb@623
    89
vb@623
    90
        ||
vb@650
    91
vb@657
    92
        // «$action/@name»() - 
vb@623
    93
        //
vb@623
    94
        //  params:
vb@627
    95
        //      session (in)        session handle
vb@690
    96
        //      state (in)          state the state machine is in
vb@807
    97
        `` if "parm"        | //      partner (in)        partner to communicate with
vb@623
    98
        `` if "not(parm)"   | //      partner (in)        (must be NULL)
vb@623
    99
        //
vb@623
   100
        //  returns:
vb@623
   101
        //      PEP_STATUS_OK or any other value on error
vb@623
   102
vb@690
   103
        PEP_STATUS «$action/@name»(
vb@690
   104
                PEP_SESSION session,
vb@690
   105
                «$fsm»_state state,
vb@939
   106
                Identity partner,
vb@939
   107
                void *extra
vb@690
   108
            )
vb@623
   109
        {
vb@623
   110
            PEP_STATUS status = PEP_STATUS_OK;
vb@623
   111
vb@657
   112
            `` call "paramcheck" with "partner", "parm/partner";
vb@623
   113
vb@623
   114
            // working code
vb@623
   115
vb@951
   116
            free_identity(partner);
vb@951
   117
            // free extra
vb@623
   118
            return status;
vb@650
   119
vb@650
   120
        enomem:
vb@650
   121
            status = PEP_OUT_OF_MEMORY;
vb@650
   122
        error:
vb@951
   123
            free_identity(partner);
vb@951
   124
            // free extra
vb@650
   125
            return status;
vb@650
   126
        }
vb@650
   127
vb@650
   128
        ||
vb@650
   129
    }
vb@650
   130
vb@650
   131
    function "send_action" {
vb@650
   132
        param "action";
vb@690
   133
        param "fsm";
vb@807
   134
        param "filename", "'###'";
vb@650
   135
        const "name", "substring($action/@name, 5, 255)";
vb@650
   136
vb@650
   137
        ||
vb@650
   138
vb@650
   139
        // «$action/@name»() - send «$name» message
vb@650
   140
        //
vb@650
   141
        //  params:
vb@650
   142
        //      session (in)        session handle
vb@690
   143
        //      state (in)          state the state machine is in
vb@807
   144
        `` if "parm"        | //      partner (in)        partner to communicate with
vb@650
   145
        `` if "not(parm)"   | //      partner (in)        (must be NULL)
vb@650
   146
        //
vb@650
   147
        //  returns:
vb@650
   148
        //      PEP_STATUS_OK or any other value on error
vb@650
   149
vb@690
   150
        PEP_STATUS «$action/@name»(
vb@690
   151
                PEP_SESSION session,
vb@690
   152
                «$fsm»_state state,
vb@939
   153
                Identity partner,
vb@939
   154
                void *extra
vb@690
   155
            )
vb@650
   156
        {
vb@650
   157
            PEP_STATUS status = PEP_STATUS_OK;
vb@690
   158
            «$name»_t *msg = NULL;
vb@690
   159
            char *payload = NULL;
vb@690
   160
            message *_message = NULL;
vb@743
   161
            pEp_identity *me = NULL;
vb@955
   162
            `` if "$name='GroupKeys'" |> identity_list *kl = NULL;
vb@650
   163
vb@657
   164
            `` call "paramcheck" with "partner", "parm/partner";
vb@650
   165
vb@690
   166
            assert(session->messageToSend);
vb@951
   167
            if (!session->messageToSend) {
vb@951
   168
                status = PEP_SEND_FUNCTION_NOT_REGISTERED;
vb@951
   169
                goto error;
vb@951
   170
            }
vb@690
   171
vb@690
   172
            msg = («$name»_t *) calloc(1, sizeof(«$name»_t));
vb@650
   173
            assert(msg);
vb@650
   174
            if (!msg)
vb@650
   175
                goto enomem;
vb@650
   176
vb@652
   177
            int32_t seq;
vb@652
   178
            status = sequence_value(session, "DeviceGroup", &seq);
vb@652
   179
            if (status != PEP_STATUS_OK)
vb@652
   180
                goto error;
vb@654
   181
            msg->header.sequence = (long) seq;
vb@652
   182
vb@951
   183
            bool devicegroup = storedGroupKeys(session);
vb@951
   184
            if (devicegroup) { // default is FALSE
vb@951
   185
                BOOLEAN_t *dg = malloc(sizeof(BOOLEAN_t));
vb@951
   186
                assert(dg);
vb@951
   187
                if (!dg)
vb@951
   188
                    goto enomem;
vb@951
   189
vb@951
   190
                *dg = 1;
vb@951
   191
                msg->header.devicegroup = dg;
vb@951
   192
            }
vb@951
   193
vb@951
   194
            msg->header.state = (long) state;
vb@690
   195
vb@743
   196
            me = new_identity(NULL, NULL, NULL, NULL);
vb@654
   197
            if (!me)
vb@654
   198
                goto enomem;
vb@654
   199
            status = myself(session, me);
vb@654
   200
            if (status != PEP_STATUS_OK)
vb@654
   201
                goto error;
vb@951
   202
            if (Identity_from_Struct(me, &msg->header.me) == NULL)
vb@655
   203
                goto enomem;
vb@851
   204
        ||
vb@955
   205
        if "$name='GroupKeys'" {
vb@951
   206
            |
vb@955
   207
            |> status = own_identities_retrieve(session, &kl);
vb@951
   208
            |> if (status != PEP_STATUS_OK)
vb@951
   209
            |>> goto error;
vb@955
   210
            |> if (IdentityList_from_identity_list(kl, &msg->ownIdentities) == NULL)
vb@951
   211
            |>> goto enomem;
vb@951
   212
        }
vb@851
   213
        ||
vb@665
   214
vb@692
   215
            if (asn_check_constraints(&asn_DEF_«$name», msg, NULL, NULL)) {
vb@692
   216
                status = PEP_CONTRAINTS_VIOLATED;
vb@692
   217
                goto error;
vb@692
   218
            }
vb@692
   219
vb@692
   220
            ssize_t size = uper_encode_to_new_buffer(&asn_DEF_«$name»,
vb@690
   221
                    NULL, msg, (void **) &payload);
vb@690
   222
            if (size == -1) {
vb@690
   223
                status = PEP_CANNOT_ENCODE;
vb@690
   224
                goto error;
vb@690
   225
            }
vb@690
   226
vb@690
   227
            status = prepare_message(me, partner, payload, size, &_message);
vb@690
   228
            if (status != PEP_STATUS_OK)
vb@690
   229
                goto error;
vb@690
   230
            payload = NULL;
vb@690
   231
vb@743
   232
            free_identity(me);
vb@743
   233
            me = NULL;
vb@743
   234
vb@807
   235
            status = session->messageToSend(session->«$filename»_obj, _message);
vb@690
   236
vb@690
   237
            free_message(_message);
vb@690
   238
            ASN_STRUCT_FREE(asn_DEF_«$name», msg);
vb@951
   239
            free_identity(partner);
vb@955
   240
            `` if "$name='GroupKeys'" |> free_identity_list(kl);
vb@650
   241
            return status;
vb@650
   242
vb@650
   243
        enomem:
vb@650
   244
            status = PEP_OUT_OF_MEMORY;
vb@650
   245
        error:
vb@650
   246
            ASN_STRUCT_FREE(asn_DEF_«$name», msg);
vb@690
   247
            free(payload);
vb@690
   248
            free_message(_message);
vb@743
   249
            free_identity(me);
vb@955
   250
            free_identity(partner);
vb@955
   251
            `` if "$name='GroupKeys'" |> free_identity_list(kl);
vb@650
   252
            return status;
vb@623
   253
        }
vb@623
   254
vb@623
   255
        ||
vb@623
   256
    }
vb@623
   257
}
vb@623
   258