sync/generated/sync_fsm.c
author Edouard Tisserant <edouard@pep-project.org>
Thu, 08 Dec 2016 00:51:41 +0100
changeset 1467 ff7c60d14af0
child 1476 61323550db84
permissions -rw-r--r--
sync: documentation, moved generated code, updated .hgignore, cleaning
edouard@1467
     1
#include "sync_fsm.h"
edouard@1467
     2
#include "pEp_internal.h"
edouard@1467
     3
edouard@1467
     4
// state machine for DeviceState
edouard@1467
     5
edouard@1467
     6
DeviceState_state fsm_DeviceState(
edouard@1467
     7
        PEP_SESSION session,
edouard@1467
     8
        DeviceState_state state,
edouard@1467
     9
        DeviceState_event event,
edouard@1467
    10
        Identity partner,
edouard@1467
    11
        void *extra,
edouard@1467
    12
        time_t *timeout
edouard@1467
    13
    )
edouard@1467
    14
{
edouard@1467
    15
    int cond_result;
edouard@1467
    16
    PEP_STATUS status = PEP_STATUS_OK;
edouard@1467
    17
edouard@1467
    18
    switch (state) {
edouard@1467
    19
        case InitState:
edouard@1467
    20
        {
edouard@1467
    21
            *timeout = 0;
edouard@1467
    22
            switch (event) {
edouard@1467
    23
                case Init:
edouard@1467
    24
                {
edouard@1467
    25
                    cond_result = storedGroupKeys(session);
edouard@1467
    26
                    if (cond_result < 0)
edouard@1467
    27
                        return cond_result;
edouard@1467
    28
                    if (cond_result) {
edouard@1467
    29
                        return Grouped;
edouard@1467
    30
                    }
edouard@1467
    31
                    return Sole;
edouard@1467
    32
                }
edouard@1467
    33
                default:
edouard@1467
    34
                    return (DeviceState_state) invalid_event;
edouard@1467
    35
            }
edouard@1467
    36
            break;
edouard@1467
    37
        }
edouard@1467
    38
        case Sole:
edouard@1467
    39
        {
edouard@1467
    40
            *timeout = 0;
edouard@1467
    41
            switch (event) {
edouard@1467
    42
                case Init: break;
edouard@1467
    43
                case KeyGen:
edouard@1467
    44
                {
edouard@1467
    45
                    status = sendBeacon(session, state, NULL, NULL);
edouard@1467
    46
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
    47
                        return (int) invalid_out_of_memory;
edouard@1467
    48
                    if (status != PEP_STATUS_OK)
edouard@1467
    49
                        return (int) invalid_action;
edouard@1467
    50
                    break;
edouard@1467
    51
                }
edouard@1467
    52
                case CannotDecrypt:
edouard@1467
    53
                {
edouard@1467
    54
                    status = sendBeacon(session, state, NULL, NULL);
edouard@1467
    55
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
    56
                        return (int) invalid_out_of_memory;
edouard@1467
    57
                    if (status != PEP_STATUS_OK)
edouard@1467
    58
                        return (int) invalid_action;
edouard@1467
    59
                    break;
edouard@1467
    60
                }
edouard@1467
    61
                case Beacon:
edouard@1467
    62
                {
edouard@1467
    63
                    status = sendHandshakeRequest(session, state, partner, NULL);
edouard@1467
    64
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
    65
                        return (int) invalid_out_of_memory;
edouard@1467
    66
                    if (status != PEP_STATUS_OK)
edouard@1467
    67
                        return (int) invalid_action;
edouard@1467
    68
                    break;
edouard@1467
    69
                }
edouard@1467
    70
                case HandshakeRequest:
edouard@1467
    71
                {
edouard@1467
    72
                    status = sendHandshakeRequest(session, state, partner, NULL);
edouard@1467
    73
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
    74
                        return (int) invalid_out_of_memory;
edouard@1467
    75
                    if (status != PEP_STATUS_OK)
edouard@1467
    76
                        return (int) invalid_action;
edouard@1467
    77
                    session->sync_state_payload = identity_dup(partner);
edouard@1467
    78
                    return HandshakingSole;
edouard@1467
    79
                }
edouard@1467
    80
                default:
edouard@1467
    81
                    return (DeviceState_state) invalid_event;
edouard@1467
    82
            }
edouard@1467
    83
            break;
edouard@1467
    84
        }
edouard@1467
    85
        case HandshakingSole:
edouard@1467
    86
        {
edouard@1467
    87
            Identity expected = (Identity)session->sync_state_payload;
edouard@1467
    88
            *timeout = 600;
edouard@1467
    89
            switch (event) {
edouard@1467
    90
                case Init:
edouard@1467
    91
                {
edouard@1467
    92
                    status = showHandshake(session, state, partner, NULL);
edouard@1467
    93
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
    94
                        return (int) invalid_out_of_memory;
edouard@1467
    95
                    if (status != PEP_STATUS_OK)
edouard@1467
    96
                        return (int) invalid_action;
edouard@1467
    97
                    break;
edouard@1467
    98
                }
edouard@1467
    99
                case HandshakeRejected:
edouard@1467
   100
                {
edouard@1467
   101
                    status = rejectHandshake(session, state, partner, NULL);
edouard@1467
   102
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   103
                        return (int) invalid_out_of_memory;
edouard@1467
   104
                    if (status != PEP_STATUS_OK)
edouard@1467
   105
                        return (int) invalid_action;
edouard@1467
   106
                    if(session->sync_state_payload){
edouard@1467
   107
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   108
                        session->sync_state_payload = NULL;
edouard@1467
   109
                    }
edouard@1467
   110
                    return Sole;
edouard@1467
   111
                }
edouard@1467
   112
                case HandshakeAccepted:
edouard@1467
   113
                {
edouard@1467
   114
                    status = acceptHandshake(session, state, partner, NULL);
edouard@1467
   115
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   116
                        return (int) invalid_out_of_memory;
edouard@1467
   117
                    if (status != PEP_STATUS_OK)
edouard@1467
   118
                        return (int) invalid_action;
edouard@1467
   119
                    cond_result = keyElectionWon(session, partner);
edouard@1467
   120
                    if (cond_result < 0)
edouard@1467
   121
                        return cond_result;
edouard@1467
   122
                    if (cond_result) {
edouard@1467
   123
                        status = sendGroupKeys(session, state, partner, NULL);
edouard@1467
   124
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   125
                            return (int) invalid_out_of_memory;
edouard@1467
   126
                        if (status != PEP_STATUS_OK)
edouard@1467
   127
                            return (int) invalid_action;
edouard@1467
   128
                        status = handshakeGroupCreated(session, state, partner, NULL);
edouard@1467
   129
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   130
                            return (int) invalid_out_of_memory;
edouard@1467
   131
                        if (status != PEP_STATUS_OK)
edouard@1467
   132
                            return (int) invalid_action;
edouard@1467
   133
                        if(session->sync_state_payload){
edouard@1467
   134
                            free_identity((Identity)session->sync_state_payload);
edouard@1467
   135
                            session->sync_state_payload = NULL;
edouard@1467
   136
                        }
edouard@1467
   137
                        return Grouped;
edouard@1467
   138
                    }
edouard@1467
   139
                    if(session->sync_state_payload){
edouard@1467
   140
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   141
                        session->sync_state_payload = NULL;
edouard@1467
   142
                    }
edouard@1467
   143
                    session->sync_state_payload = identity_dup(partner);
edouard@1467
   144
                    return WaitForGroupKeysSole;
edouard@1467
   145
                }
edouard@1467
   146
                case Cancel:
edouard@1467
   147
                {
edouard@1467
   148
                    if(session->sync_state_payload){
edouard@1467
   149
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   150
                        session->sync_state_payload = NULL;
edouard@1467
   151
                    }
edouard@1467
   152
                    return Sole;
edouard@1467
   153
                }
edouard@1467
   154
                case Timeout:
edouard@1467
   155
                {
edouard@1467
   156
                    status = dismissHandshake(session, state, expected, NULL);
edouard@1467
   157
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   158
                        return (int) invalid_out_of_memory;
edouard@1467
   159
                    if (status != PEP_STATUS_OK)
edouard@1467
   160
                        return (int) invalid_action;
edouard@1467
   161
                    if(session->sync_state_payload){
edouard@1467
   162
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   163
                        session->sync_state_payload = NULL;
edouard@1467
   164
                    }
edouard@1467
   165
                    return Sole;
edouard@1467
   166
                }
edouard@1467
   167
                default:
edouard@1467
   168
                    return (DeviceState_state) invalid_event;
edouard@1467
   169
            }
edouard@1467
   170
            break;
edouard@1467
   171
        }
edouard@1467
   172
        case WaitForGroupKeysSole:
edouard@1467
   173
        {
edouard@1467
   174
            Identity expected = (Identity)session->sync_state_payload;
edouard@1467
   175
            *timeout = 600;
edouard@1467
   176
            switch (event) {
edouard@1467
   177
                case Init: break;
edouard@1467
   178
                case GroupKeys:
edouard@1467
   179
                {
edouard@1467
   180
                    status = storeGroupKeys(session, state, partner, extra /*keys*/);
edouard@1467
   181
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   182
                        return (int) invalid_out_of_memory;
edouard@1467
   183
                    if (status != PEP_STATUS_OK)
edouard@1467
   184
                        return (int) invalid_action;
edouard@1467
   185
                    status = handshakeSuccess(session, state, partner, NULL);
edouard@1467
   186
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   187
                        return (int) invalid_out_of_memory;
edouard@1467
   188
                    if (status != PEP_STATUS_OK)
edouard@1467
   189
                        return (int) invalid_action;
edouard@1467
   190
                    if(session->sync_state_payload){
edouard@1467
   191
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   192
                        session->sync_state_payload = NULL;
edouard@1467
   193
                    }
edouard@1467
   194
                    return Grouped;
edouard@1467
   195
                }
edouard@1467
   196
                case Timeout:
edouard@1467
   197
                {
edouard@1467
   198
                    status = handshakeFailure(session, state, expected, NULL);
edouard@1467
   199
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   200
                        return (int) invalid_out_of_memory;
edouard@1467
   201
                    if (status != PEP_STATUS_OK)
edouard@1467
   202
                        return (int) invalid_action;
edouard@1467
   203
                    if(session->sync_state_payload){
edouard@1467
   204
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   205
                        session->sync_state_payload = NULL;
edouard@1467
   206
                    }
edouard@1467
   207
                    return Sole;
edouard@1467
   208
                }
edouard@1467
   209
                default:
edouard@1467
   210
                    return (DeviceState_state) invalid_event;
edouard@1467
   211
            }
edouard@1467
   212
            break;
edouard@1467
   213
        }
edouard@1467
   214
        case Grouped:
edouard@1467
   215
        {
edouard@1467
   216
            *timeout = 0;
edouard@1467
   217
            switch (event) {
edouard@1467
   218
                case Init:
edouard@1467
   219
                {
edouard@1467
   220
                    status = enterGroup(session, state, NULL, NULL);
edouard@1467
   221
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   222
                        return (int) invalid_out_of_memory;
edouard@1467
   223
                    if (status != PEP_STATUS_OK)
edouard@1467
   224
                        return (int) invalid_action;
edouard@1467
   225
                    break;
edouard@1467
   226
                }
edouard@1467
   227
                case KeyGen:
edouard@1467
   228
                {
edouard@1467
   229
                    status = sendGroupUpdate(session, state, NULL, NULL);
edouard@1467
   230
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   231
                        return (int) invalid_out_of_memory;
edouard@1467
   232
                    if (status != PEP_STATUS_OK)
edouard@1467
   233
                        return (int) invalid_action;
edouard@1467
   234
                    break;
edouard@1467
   235
                }
edouard@1467
   236
                case CannotDecrypt:
edouard@1467
   237
                {
edouard@1467
   238
                    status = sendUpdateRequest(session, state, NULL, NULL);
edouard@1467
   239
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   240
                        return (int) invalid_out_of_memory;
edouard@1467
   241
                    if (status != PEP_STATUS_OK)
edouard@1467
   242
                        return (int) invalid_action;
edouard@1467
   243
                    break;
edouard@1467
   244
                }
edouard@1467
   245
                case UpdateRequest:
edouard@1467
   246
                {
edouard@1467
   247
                    status = sendGroupUpdate(session, state, NULL, NULL);
edouard@1467
   248
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   249
                        return (int) invalid_out_of_memory;
edouard@1467
   250
                    if (status != PEP_STATUS_OK)
edouard@1467
   251
                        return (int) invalid_action;
edouard@1467
   252
                    break;
edouard@1467
   253
                }
edouard@1467
   254
                case Beacon:
edouard@1467
   255
                {
edouard@1467
   256
                    status = sendHandshakeRequest(session, state, partner, NULL);
edouard@1467
   257
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   258
                        return (int) invalid_out_of_memory;
edouard@1467
   259
                    if (status != PEP_STATUS_OK)
edouard@1467
   260
                        return (int) invalid_action;
edouard@1467
   261
                    break;
edouard@1467
   262
                }
edouard@1467
   263
                case HandshakeRequest:
edouard@1467
   264
                {
edouard@1467
   265
                    status = sendHandshakeRequest(session, state, partner, NULL);
edouard@1467
   266
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   267
                        return (int) invalid_out_of_memory;
edouard@1467
   268
                    if (status != PEP_STATUS_OK)
edouard@1467
   269
                        return (int) invalid_action;
edouard@1467
   270
                    session->sync_state_payload = identity_dup(partner);
edouard@1467
   271
                    return HandshakingGrouped;
edouard@1467
   272
                }
edouard@1467
   273
                case GroupUpdate:
edouard@1467
   274
                {
edouard@1467
   275
                    status = storeGroupKeys(session, state, partner, extra /*keys*/);
edouard@1467
   276
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   277
                        return (int) invalid_out_of_memory;
edouard@1467
   278
                    if (status != PEP_STATUS_OK)
edouard@1467
   279
                        return (int) invalid_action;
edouard@1467
   280
                    break;
edouard@1467
   281
                }
edouard@1467
   282
                default:
edouard@1467
   283
                    return (DeviceState_state) invalid_event;
edouard@1467
   284
            }
edouard@1467
   285
            break;
edouard@1467
   286
        }
edouard@1467
   287
        case HandshakingGrouped:
edouard@1467
   288
        {
edouard@1467
   289
            Identity expected = (Identity)session->sync_state_payload;
edouard@1467
   290
            *timeout = 600;
edouard@1467
   291
            switch (event) {
edouard@1467
   292
                case Init:
edouard@1467
   293
                {
edouard@1467
   294
                    status = showHandshake(session, state, partner, NULL);
edouard@1467
   295
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   296
                        return (int) invalid_out_of_memory;
edouard@1467
   297
                    if (status != PEP_STATUS_OK)
edouard@1467
   298
                        return (int) invalid_action;
edouard@1467
   299
                    break;
edouard@1467
   300
                }
edouard@1467
   301
                case HandshakeRejected:
edouard@1467
   302
                {
edouard@1467
   303
                    status = rejectHandshake(session, state, partner, NULL);
edouard@1467
   304
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   305
                        return (int) invalid_out_of_memory;
edouard@1467
   306
                    if (status != PEP_STATUS_OK)
edouard@1467
   307
                        return (int) invalid_action;
edouard@1467
   308
                    if(session->sync_state_payload){
edouard@1467
   309
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   310
                        session->sync_state_payload = NULL;
edouard@1467
   311
                    }
edouard@1467
   312
                    return Grouped;
edouard@1467
   313
                }
edouard@1467
   314
                case HandshakeAccepted:
edouard@1467
   315
                {
edouard@1467
   316
                    status = acceptHandshake(session, state, partner, NULL);
edouard@1467
   317
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   318
                        return (int) invalid_out_of_memory;
edouard@1467
   319
                    if (status != PEP_STATUS_OK)
edouard@1467
   320
                        return (int) invalid_action;
edouard@1467
   321
                    status = sendGroupKeys(session, state, partner, NULL);
edouard@1467
   322
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   323
                        return (int) invalid_out_of_memory;
edouard@1467
   324
                    if (status != PEP_STATUS_OK)
edouard@1467
   325
                        return (int) invalid_action;
edouard@1467
   326
                    status = handshakeDeviceAdded(session, state, partner, NULL);
edouard@1467
   327
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   328
                        return (int) invalid_out_of_memory;
edouard@1467
   329
                    if (status != PEP_STATUS_OK)
edouard@1467
   330
                        return (int) invalid_action;
edouard@1467
   331
                    if(session->sync_state_payload){
edouard@1467
   332
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   333
                        session->sync_state_payload = NULL;
edouard@1467
   334
                    }
edouard@1467
   335
                    return Grouped;
edouard@1467
   336
                }
edouard@1467
   337
                case Timeout:
edouard@1467
   338
                {
edouard@1467
   339
                    status = handshakeFailure(session, state, expected, NULL);
edouard@1467
   340
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   341
                        return (int) invalid_out_of_memory;
edouard@1467
   342
                    if (status != PEP_STATUS_OK)
edouard@1467
   343
                        return (int) invalid_action;
edouard@1467
   344
                    if(session->sync_state_payload){
edouard@1467
   345
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   346
                        session->sync_state_payload = NULL;
edouard@1467
   347
                    }
edouard@1467
   348
                    return Grouped;
edouard@1467
   349
                }
edouard@1467
   350
                default:
edouard@1467
   351
                    return (DeviceState_state) invalid_event;
edouard@1467
   352
            }
edouard@1467
   353
            break;
edouard@1467
   354
        }
edouard@1467
   355
        default:
edouard@1467
   356
            return (DeviceState_state) invalid_state;
edouard@1467
   357
    }
edouard@1467
   358
edouard@1467
   359
    return state;
edouard@1467
   360
}
edouard@1467
   361