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