sync/gen_actions.ysl2
author Krista 'DarthMama' Bennett <krista@pep.foundation>
Tue, 09 Apr 2019 09:26:27 +0200
changeset 3473 9cd6a0ad3c74
parent 2287 026ab4dae779
child 2829 e444c3c960bb
permissions -rw-r--r--
ENGINE-538: added DB repair to default - no use risking it.
krista@2271
     1
// This file is under GNU General Public License 3.0
krista@2271
     2
// see LICENSE.txt
krista@2271
     3
krista@2271
     4
// generate actions skeleton
krista@2271
     5
krista@2271
     6
// Copyleft (c) 2016, p≡p foundation
krista@2271
     7
krista@2271
     8
// Written by Volker Birk
krista@2271
     9
krista@2271
    10
include yslt.yml2
krista@2271
    11
krista@2271
    12
tstylesheet {
krista@2271
    13
    include standardlib.ysl2
krista@2271
    14
    include ./functions.ysl2
krista@2271
    15
krista@2271
    16
    template "/protocol" {
krista@2271
    17
        apply "fsm", mode=send, 0;
krista@2271
    18
        apply "fsm", mode=other, 0;
krista@2271
    19
    }
krista@2271
    20
krista@2271
    21
    template "fsm", mode=send document "generated/{@filename}_send_actions.c", "text" {
krista@2271
    22
        const "name", "@name";
krista@2271
    23
        const "filename", "@filename";
krista@2271
    24
        ||
krista@2271
    25
        // Send Actions for «@name» state machine
krista@2271
    26
krista@2271
    27
        #include <assert.h>
krista@2271
    28
        #include "pEp_internal.h"
krista@2271
    29
        #include "keymanagement.h"
krista@2271
    30
        #include "message.h"
krista@2271
    31
        #include "«@filename»_fsm.h"
krista@2271
    32
        #include "baseprotocol.h"
krista@2271
    33
        #include "map_asn1.h"
krista@2271
    34
        #include "../asn.1/DeviceGroup-Protocol.h"
krista@2271
    35
        #include "sync_impl.h"
krista@2271
    36
        ||
krista@2271
    37
        for "func:distinctName(//action)"
krista@2271
    38
            if "substring(@name, 1, 4) = 'send'"
krista@2271
    39
                | #include "../asn.1/«substring(@name, 5, 255)».h"
krista@2271
    40
        |
krista@2271
    41
        for "func:distinctName(//action)"
krista@2271
    42
            if "substring(@name, 1, 4) = 'send'"
krista@2271
    43
                call "send_action"
krista@2271
    44
                    with "action", ".",
krista@2271
    45
                    with "fsm", "$name",
krista@2271
    46
                    with "filename", "$filename";
krista@2271
    47
krista@2271
    48
        ||
krista@2271
    49
krista@2271
    50
        PEP_STATUS _notifyHandshake(
krista@2271
    51
                PEP_SESSION session,
krista@2271
    52
                Identity partner,
krista@2271
    53
                sync_handshake_signal signal
krista@2271
    54
            );
krista@2271
    55
        ||
krista@2271
    56
krista@2271
    57
        for "func:distinctName(//action)"
krista@2271
    58
            if "substring(@name, 1, 6) = 'notify'"
krista@2271
    59
                call "notify_action"
krista@2271
    60
                    with "action", ".",
krista@2271
    61
                    with "fsm", "$name",
krista@2271
    62
                    with "filename", "$filename";
krista@2271
    63
    }
krista@2271
    64
krista@2271
    65
    template "fsm", mode=other document "skeletons/{@filename}_actions.c", "text" {
krista@2271
    66
        const "name", "@name";
krista@2271
    67
        const "filename", "@filename";
krista@2271
    68
        ||
krista@2271
    69
        // Actions for «@name» state machine
krista@2271
    70
krista@2271
    71
        #include <assert.h>
krista@2271
    72
        #include "pEp_internal.h"
krista@2271
    73
        #include "keymanagement.h"
krista@2271
    74
        #include "message.h"
krista@2271
    75
        #include "«@filename»_fsm.h"
krista@2271
    76
        #include "../asn.1/DeviceGroup-Protocol.h"
krista@2271
    77
krista@2271
    78
        ||
krista@2271
    79
        for "func:distinctName(//action)"
krista@2271
    80
            if "substring(@name, 1, 4) != 'send'"
krista@2271
    81
                call "other_action"
krista@2271
    82
                    with "action", ".",
krista@2271
    83
                    with "fsm", "$name",
krista@2271
    84
                    with "filename", "$filename";
krista@2271
    85
    }
krista@2271
    86
krista@2271
    87
    function "paramcheck" {
krista@2271
    88
        param "partner";
krista@2271
    89
        |> assert(session);
krista@2271
    90
        choose {
krista@2271
    91
            when "$partner"
krista@2271
    92
            ||
krista@2271
    93
                assert(partner);
krista@2271
    94
                if (!(session && partner))
krista@2271
    95
                    return PEP_ILLEGAL_VALUE;
krista@2271
    96
            ||
krista@2271
    97
            otherwise
krista@2271
    98
            ||
krista@2271
    99
                assert(!partner);
krista@2271
   100
                if (!(session && !partner))
krista@2271
   101
                    return PEP_ILLEGAL_VALUE;
krista@2271
   102
            ||
krista@2271
   103
        }
krista@2271
   104
    }
krista@2271
   105
krista@2271
   106
    function "other_action" {
krista@2271
   107
        param "action";
krista@2271
   108
        param "fsm";
krista@2271
   109
        param "filename", "'###'";
krista@2271
   110
krista@2271
   111
        ||
krista@2271
   112
krista@2271
   113
        // «$action/@name»() - 
krista@2271
   114
        //
krista@2271
   115
        //  params:
krista@2271
   116
        //      session (in)        session handle
krista@2271
   117
        //      state (in)          state the state machine is in
krista@2271
   118
        `` if "parm"        | //      partner (in)        partner to communicate with
krista@2271
   119
        `` if "not(parm)"   | //      partner (in)        (must be NULL)
krista@2271
   120
        //
krista@2271
   121
        //  returns:
krista@2271
   122
        //      PEP_STATUS_OK or any other value on error
krista@2271
   123
krista@2271
   124
        PEP_STATUS «$action/@name»(
krista@2271
   125
                PEP_SESSION session,
krista@2271
   126
                «$fsm»_state state,
krista@2271
   127
                Identity partner,
krista@2271
   128
                void *extra
krista@2271
   129
            )
krista@2271
   130
        {
krista@2271
   131
            PEP_STATUS status = PEP_STATUS_OK;
krista@2271
   132
krista@2271
   133
            `` call "paramcheck" with "partner", "parm/partner";
krista@2271
   134
krista@2271
   135
            // working code
krista@2271
   136
krista@2271
   137
            // free extra
krista@2271
   138
            return status;
krista@2271
   139
krista@2271
   140
        enomem:
krista@2271
   141
            status = PEP_OUT_OF_MEMORY;
krista@2271
   142
        error:
krista@2271
   143
            // free extra
krista@2271
   144
            return status;
krista@2271
   145
        }
krista@2271
   146
krista@2271
   147
        ||
krista@2271
   148
    }
krista@2271
   149
krista@2271
   150
    function "send_action" {
krista@2271
   151
        param "action";
krista@2271
   152
        param "fsm";
krista@2271
   153
        param "filename", "'###'";
krista@2271
   154
        const "name", "substring($action/@name, 5, 255)";
krista@2271
   155
        const "lname", "concat(yml:lcase(substring($name, 1, 1)), substring($name, 2))";
krista@2271
   156
krista@2271
   157
        ||
krista@2271
   158
krista@2271
   159
        // «$action/@name»() - send «$name» message
krista@2271
   160
        //
krista@2271
   161
        //  params:
krista@2271
   162
        //      session (in)        session handle
krista@2271
   163
        //      state (in)          state the state machine is in
krista@2271
   164
        `` if "parm"        | //      partner (in)        partner to communicate with
krista@2271
   165
        `` if "not(parm)"   | //      partner (in)        (must be NULL)
krista@2271
   166
        //
krista@2271
   167
        //  returns:
krista@2271
   168
        //      PEP_STATUS_OK or any other value on error
krista@2271
   169
krista@2271
   170
        PEP_STATUS «$action/@name»(
krista@2271
   171
                PEP_SESSION session,
krista@2271
   172
                «$fsm»_state state,
krista@2271
   173
                Identity partner,
krista@2271
   174
                void *extra
krista@2271
   175
            )
krista@2271
   176
        {
krista@2271
   177
            assert(session && state);
krista@2271
   178
            if (!(session && state))
krista@2271
   179
                return PEP_ILLEGAL_VALUE;
krista@2271
   180
krista@2271
   181
            PEP_STATUS status = PEP_STATUS_OK;
krista@2271
   182
            `` if "$name='GroupKeys' or $name='GroupUpdate'" |> identity_list *kl = new_identity_list(NULL);
krista@2271
   183
krista@2271
   184
            DeviceGroup_Protocol_t *msg = new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR_«$lname»);
krista@2271
   185
            if (!msg)
krista@2271
   186
                goto enomem;
krista@2271
   187
        ||
krista@2271
   188
        choose {
krista@2271
   189
            when "$name='GroupKeys' or $name='GroupUpdate'" {
krista@2271
   190
                |
krista@2271
   191
                |> status = _own_identities_retrieve(session, &kl, PEP_idf_not_for_sync);
krista@2271
   192
                |> if (status != PEP_STATUS_OK)
krista@2271
   193
                |>> goto error;
krista@2271
   194
                |> if (IdentityList_from_identity_list(kl, &msg->payload.choice.«$lname».ownIdentities) == NULL)
krista@2271
   195
                |>> goto enomem;
krista@2271
   196
            }
krista@2271
   197
        }
krista@2271
   198
        choose {
krista@2271
   199
            when "$name='GroupKeys' or $name='HandshakeRequest'" {
krista@2271
   200
                |
krista@2271
   201
                |> msg->payload.choice.«$lname».partner_id = 
krista@2271
   202
                |>     OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
krista@2271
   203
                |>                              partner->user_id, -1);
krista@2271
   204
                |> if (partner->user_id && !msg->payload.choice.«$lname».partner_id)
krista@2271
   205
                |>    goto enomem;
krista@2271
   206
                |
krista@2271
   207
                |> char *devgrp = NULL;
krista@2271
   208
                |> status = get_device_group(session, &devgrp);
krista@2271
   209
                |> if (status == PEP_STATUS_OK && devgrp && devgrp[0])
krista@2271
   210
                |> msg->payload.choice.«$lname».group_id = 
krista@2271
   211
                |>     OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
krista@2271
   212
                |>                              devgrp, -1);
krista@2271
   213
                |> free(devgrp);
krista@2271
   214
                |> if (devgrp && !msg->payload.choice.«$lname».partner_id)
krista@2271
   215
                |>    goto enomem;
krista@2271
   216
            }
krista@2271
   217
        }
krista@2271
   218
        ||
krista@2271
   219
krista@2271
   220
        ||
krista@2271
   221
        choose {
krista@2271
   222
            when "count(/protocol/unencrypted/*[name()=$action/@name]) = 0"
krista@2271
   223
                |> bool encrypted = true;
krista@2271
   224
            otherwise
krista@2271
   225
                |> bool encrypted = false;
krista@2271
   226
        }
krista@2271
   227
        choose {
krista@2271
   228
            when "count(/protocol/broadcast/*[name()=$action/@name]) = 0"
krista@2271
   229
                |> status = unicast_msg(session, partner, state, msg, encrypted);
krista@2271
   230
            otherwise
krista@2271
   231
                |> status = multicast_self_msg(session, state, msg, encrypted);
krista@2271
   232
        }
krista@2271
   233
        ||
krista@2271
   234
            if (status != PEP_STATUS_OK)
krista@2271
   235
                goto error;
krista@2271
   236
krista@2271
   237
            `` if "$name='GroupKeys' or $name='GroupUpdate'" |> free_identity_list(kl);
krista@2271
   238
            free_DeviceGroup_Protocol_msg(msg);
krista@2271
   239
            return PEP_STATUS_OK;
krista@2271
   240
krista@2271
   241
        enomem:
krista@2271
   242
            status = PEP_OUT_OF_MEMORY;
krista@2271
   243
        error:
krista@2271
   244
            free_DeviceGroup_Protocol_msg(msg);
krista@2271
   245
            `` if "$name='GroupKeys'" |> free_identity_list(kl);
krista@2271
   246
            return status;
krista@2271
   247
        }
krista@2271
   248
krista@2271
   249
        ||
krista@2271
   250
    }
krista@2271
   251
krista@2271
   252
    function "UnCamelUp" {
krista@2271
   253
        param "text";
krista@2271
   254
        const "tokens", "str:tokenize($text, '')";
krista@2271
   255
krista@2271
   256
        for "$tokens" {
krista@2271
   257
            choose {
krista@2271
   258
                when "contains('ABCDEFGHIJKLMNOPQRSTUVWXYZ',.)" > _«.»
krista@2271
   259
                otherwise value "yml:ucase(.)";
krista@2271
   260
            }
krista@2271
   261
        }
krista@2271
   262
    }
krista@2271
   263
krista@2271
   264
    function "notify_action" {
krista@2271
   265
        param "action";
krista@2271
   266
        param "fsm";
krista@2271
   267
        param "filename", "'###'";
krista@2271
   268
        const "name", "substring($action/@name, 7, 255)";
krista@2271
   269
        const "uname" call "UnCamelUp" with "text", "$name";
krista@2271
   270
        ||
krista@2271
   271
krista@2271
   272
        // «$action/@name»() - notify «$name» to app
krista@2271
   273
        //
krista@2271
   274
        //  params:
krista@2271
   275
        //      session (in)        session handle
krista@2271
   276
        //      state (in)          state the state machine is in
krista@2271
   277
        //      partner (in)        partner to communicate with
krista@2271
   278
        //
krista@2271
   279
        //  returns:
krista@2271
   280
        //      PEP_STATUS_OK or any other value on error
krista@2271
   281
krista@2271
   282
        PEP_STATUS «$action/@name»(
krista@2271
   283
                PEP_SESSION session,
krista@2271
   284
                «$fsm»_state state,
krista@2271
   285
                Identity partner,
krista@2271
   286
                void *extra
krista@2271
   287
            )
krista@2271
   288
        {
krista@2271
   289
            assert(session && state);
krista@2271
   290
            assert(extra == NULL);
krista@2271
   291
            if (!(session && state && extra == NULL))
krista@2271
   292
                return PEP_ILLEGAL_VALUE;
krista@2271
   293
krista@2271
   294
            return _notifyHandshake(session, partner, SYNC_NOTIFY«$uname»);
krista@2271
   295
        }
krista@2271
   296
krista@2271
   297
        ||
krista@2271
   298
    }
krista@2271
   299
}
krista@2271
   300