sync/gen_actions_skeleton.ysl2
author Volker Birk <vb@pep-project.org>
Fri, 29 Jul 2016 10:45:13 +0200
branchkeysync
changeset 939 ed7dfdae3031
parent 883 096ff09932ad
child 951 dec407deb10e
permissions -rw-r--r--
changing message_api: adding flags to encrypt_message()
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@623
   114
vb@623
   115
            return status;
vb@650
   116
vb@650
   117
        enomem:
vb@650
   118
            status = PEP_OUT_OF_MEMORY;
vb@650
   119
        error:
vb@650
   120
            // free...
vb@650
   121
            return status;
vb@650
   122
        }
vb@650
   123
vb@650
   124
        ||
vb@650
   125
    }
vb@650
   126
vb@650
   127
    function "send_action" {
vb@650
   128
        param "action";
vb@690
   129
        param "fsm";
vb@807
   130
        param "filename", "'###'";
vb@650
   131
        const "name", "substring($action/@name, 5, 255)";
vb@650
   132
vb@650
   133
        ||
vb@650
   134
vb@650
   135
        // «$action/@name»() - send «$name» message
vb@650
   136
        //
vb@650
   137
        //  params:
vb@650
   138
        //      session (in)        session handle
vb@690
   139
        //      state (in)          state the state machine is in
vb@807
   140
        `` if "parm"        | //      partner (in)        partner to communicate with
vb@650
   141
        `` if "not(parm)"   | //      partner (in)        (must be NULL)
vb@650
   142
        //
vb@650
   143
        //  returns:
vb@650
   144
        //      PEP_STATUS_OK or any other value on error
vb@650
   145
vb@690
   146
        PEP_STATUS «$action/@name»(
vb@690
   147
                PEP_SESSION session,
vb@690
   148
                «$fsm»_state state,
vb@939
   149
                Identity partner,
vb@939
   150
                void *extra
vb@690
   151
            )
vb@650
   152
        {
vb@650
   153
            PEP_STATUS status = PEP_STATUS_OK;
vb@690
   154
            «$name»_t *msg = NULL;
vb@690
   155
            char *payload = NULL;
vb@690
   156
            message *_message = NULL;
vb@743
   157
            pEp_identity *me = NULL;
vb@650
   158
vb@657
   159
            `` call "paramcheck" with "partner", "parm/partner";
vb@650
   160
vb@690
   161
            assert(session->messageToSend);
vb@690
   162
            if (!session->messageToSend)
vb@690
   163
                return PEP_SEND_FUNCTION_NOT_REGISTERED;
vb@690
   164
vb@690
   165
            msg = («$name»_t *) calloc(1, sizeof(«$name»_t));
vb@650
   166
            assert(msg);
vb@650
   167
            if (!msg)
vb@650
   168
                goto enomem;
vb@650
   169
vb@652
   170
            int32_t seq;
vb@652
   171
            status = sequence_value(session, "DeviceGroup", &seq);
vb@652
   172
            if (status != PEP_STATUS_OK)
vb@652
   173
                goto error;
vb@654
   174
            msg->header.sequence = (long) seq;
vb@652
   175
vb@690
   176
            msg->state = (long) state;
vb@690
   177
vb@743
   178
            me = new_identity(NULL, NULL, NULL, NULL);
vb@654
   179
            if (!me)
vb@654
   180
                goto enomem;
vb@654
   181
            status = myself(session, me);
vb@654
   182
            if (status != PEP_STATUS_OK)
vb@654
   183
                goto error;
vb@655
   184
            if (Identity_from_Struct(me, &msg->me) == NULL)
vb@655
   185
                goto enomem;
vb@851
   186
        ||
vb@851
   187
        if "parm or $name='OwnKeys'" |
vb@851
   188
        if "parm/partner"   |> if (Identity_from_Struct(partner, &msg->partner) == NULL)
vb@851
   189
        if "parm/partner"   |>> goto enomem;
vb@851
   190
        if "$name='OwnKeys'"|> stringlist_t *sl;
vb@851
   191
        if "$name='OwnKeys'"|> status = own_key_retrieve(session, &sl);
vb@851
   192
        if "$name='OwnKeys'"|> if (status != PEP_STATUS_OK)
vb@851
   193
        if "$name='OwnKeys'"|>> goto error;
vb@851
   194
        if "$name='OwnKeys'"|> if (KeyList_from_stringlist(sl, &msg->keylist) == NULL)
vb@851
   195
        if "$name='OwnKeys'"|>> goto enomem;
vb@851
   196
        ||
vb@665
   197
vb@692
   198
            if (asn_check_constraints(&asn_DEF_«$name», msg, NULL, NULL)) {
vb@692
   199
                status = PEP_CONTRAINTS_VIOLATED;
vb@692
   200
                goto error;
vb@692
   201
            }
vb@692
   202
vb@692
   203
            ssize_t size = uper_encode_to_new_buffer(&asn_DEF_«$name»,
vb@690
   204
                    NULL, msg, (void **) &payload);
vb@690
   205
            if (size == -1) {
vb@690
   206
                status = PEP_CANNOT_ENCODE;
vb@690
   207
                goto error;
vb@690
   208
            }
vb@690
   209
vb@690
   210
            status = prepare_message(me, partner, payload, size, &_message);
vb@690
   211
            if (status != PEP_STATUS_OK)
vb@690
   212
                goto error;
vb@690
   213
            payload = NULL;
vb@690
   214
vb@743
   215
            free_identity(me);
vb@743
   216
            me = NULL;
vb@743
   217
vb@807
   218
            status = session->messageToSend(session->«$filename»_obj, _message);
vb@690
   219
vb@690
   220
            free_message(_message);
vb@690
   221
            ASN_STRUCT_FREE(asn_DEF_«$name», msg);
vb@690
   222
vb@650
   223
            return status;
vb@650
   224
vb@650
   225
        enomem:
vb@650
   226
            status = PEP_OUT_OF_MEMORY;
vb@650
   227
        error:
vb@650
   228
            ASN_STRUCT_FREE(asn_DEF_«$name», msg);
vb@690
   229
            free(payload);
vb@690
   230
            free_message(_message);
vb@743
   231
            free_identity(me);
vb@650
   232
            return status;
vb@623
   233
        }
vb@623
   234
vb@623
   235
        ||
vb@623
   236
    }
vb@623
   237
}
vb@623
   238