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