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