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