sync/generated/sync_fsm.c
author Edouard Tisserant <edouard@pep-project.org>
Fri, 09 Dec 2016 01:36:57 +0100
changeset 1477 c0d761ae1fd9
parent 1476 61323550db84
child 1490 e375c22bc587
permissions -rw-r--r--
sync: obey new app notification enum names, this time with corresponding actions generated
vb@1476
     1
#include "pEp_internal.h"
edouard@1467
     2
#include "sync_fsm.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@1477
    92
                    cond_result = keyElectionWon(session, partner);
edouard@1477
    93
                    if (cond_result < 0)
edouard@1477
    94
                        return cond_result;
edouard@1477
    95
                    if (cond_result) {
edouard@1477
    96
                        status = notifyInitFormGroup(session, state, partner, NULL);
edouard@1477
    97
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1477
    98
                            return (int) invalid_out_of_memory;
edouard@1477
    99
                        if (status != PEP_STATUS_OK)
edouard@1477
   100
                            return (int) invalid_action;
edouard@1477
   101
                    }
edouard@1477
   102
                    else {
edouard@1477
   103
                        status = notifyInitAddOurDevice(session, state, partner, NULL);
edouard@1477
   104
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1477
   105
                            return (int) invalid_out_of_memory;
edouard@1477
   106
                        if (status != PEP_STATUS_OK)
edouard@1477
   107
                            return (int) invalid_action;
edouard@1477
   108
                    }
edouard@1467
   109
                    break;
edouard@1467
   110
                }
edouard@1467
   111
                case HandshakeRejected:
edouard@1467
   112
                {
edouard@1467
   113
                    status = rejectHandshake(session, state, partner, NULL);
edouard@1467
   114
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   115
                        return (int) invalid_out_of_memory;
edouard@1467
   116
                    if (status != PEP_STATUS_OK)
edouard@1467
   117
                        return (int) invalid_action;
edouard@1467
   118
                    if(session->sync_state_payload){
edouard@1467
   119
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   120
                        session->sync_state_payload = NULL;
edouard@1467
   121
                    }
edouard@1467
   122
                    return Sole;
edouard@1467
   123
                }
edouard@1467
   124
                case HandshakeAccepted:
edouard@1467
   125
                {
edouard@1467
   126
                    status = acceptHandshake(session, state, partner, NULL);
edouard@1467
   127
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   128
                        return (int) invalid_out_of_memory;
edouard@1467
   129
                    if (status != PEP_STATUS_OK)
edouard@1467
   130
                        return (int) invalid_action;
edouard@1467
   131
                    cond_result = keyElectionWon(session, partner);
edouard@1467
   132
                    if (cond_result < 0)
edouard@1467
   133
                        return cond_result;
edouard@1467
   134
                    if (cond_result) {
edouard@1467
   135
                        status = sendGroupKeys(session, state, partner, NULL);
edouard@1467
   136
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   137
                            return (int) invalid_out_of_memory;
edouard@1467
   138
                        if (status != PEP_STATUS_OK)
edouard@1467
   139
                            return (int) invalid_action;
edouard@1477
   140
                        status = notifyAcceptedGroupCreated(session, state, partner, NULL);
edouard@1467
   141
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   142
                            return (int) invalid_out_of_memory;
edouard@1467
   143
                        if (status != PEP_STATUS_OK)
edouard@1467
   144
                            return (int) invalid_action;
edouard@1467
   145
                        if(session->sync_state_payload){
edouard@1467
   146
                            free_identity((Identity)session->sync_state_payload);
edouard@1467
   147
                            session->sync_state_payload = NULL;
edouard@1467
   148
                        }
edouard@1467
   149
                        return Grouped;
edouard@1467
   150
                    }
edouard@1467
   151
                    if(session->sync_state_payload){
edouard@1467
   152
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   153
                        session->sync_state_payload = NULL;
edouard@1467
   154
                    }
edouard@1467
   155
                    session->sync_state_payload = identity_dup(partner);
edouard@1467
   156
                    return WaitForGroupKeysSole;
edouard@1467
   157
                }
edouard@1467
   158
                case Cancel:
edouard@1467
   159
                {
edouard@1467
   160
                    if(session->sync_state_payload){
edouard@1467
   161
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   162
                        session->sync_state_payload = NULL;
edouard@1467
   163
                    }
edouard@1467
   164
                    return Sole;
edouard@1467
   165
                }
edouard@1467
   166
                case Timeout:
edouard@1467
   167
                {
edouard@1477
   168
                    status = notifyTimeout(session, state, expected, NULL);
edouard@1467
   169
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   170
                        return (int) invalid_out_of_memory;
edouard@1467
   171
                    if (status != PEP_STATUS_OK)
edouard@1467
   172
                        return (int) invalid_action;
edouard@1467
   173
                    if(session->sync_state_payload){
edouard@1467
   174
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   175
                        session->sync_state_payload = NULL;
edouard@1467
   176
                    }
edouard@1467
   177
                    return Sole;
edouard@1467
   178
                }
edouard@1467
   179
                default:
edouard@1467
   180
                    return (DeviceState_state) invalid_event;
edouard@1467
   181
            }
edouard@1467
   182
            break;
edouard@1467
   183
        }
edouard@1467
   184
        case WaitForGroupKeysSole:
edouard@1467
   185
        {
edouard@1467
   186
            Identity expected = (Identity)session->sync_state_payload;
edouard@1467
   187
            *timeout = 600;
edouard@1467
   188
            switch (event) {
edouard@1467
   189
                case Init: break;
edouard@1467
   190
                case GroupKeys:
edouard@1467
   191
                {
edouard@1467
   192
                    status = storeGroupKeys(session, state, partner, extra /*keys*/);
edouard@1467
   193
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   194
                        return (int) invalid_out_of_memory;
edouard@1467
   195
                    if (status != PEP_STATUS_OK)
edouard@1467
   196
                        return (int) invalid_action;
edouard@1477
   197
                    status = notifyAcceptedDeviceAdded(session, state, partner, NULL);
edouard@1467
   198
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   199
                        return (int) invalid_out_of_memory;
edouard@1467
   200
                    if (status != PEP_STATUS_OK)
edouard@1467
   201
                        return (int) invalid_action;
edouard@1467
   202
                    if(session->sync_state_payload){
edouard@1467
   203
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   204
                        session->sync_state_payload = NULL;
edouard@1467
   205
                    }
edouard@1467
   206
                    return Grouped;
edouard@1467
   207
                }
edouard@1467
   208
                case Timeout:
edouard@1467
   209
                {
edouard@1477
   210
                    status = notifyTimeout(session, state, expected, NULL);
edouard@1467
   211
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   212
                        return (int) invalid_out_of_memory;
edouard@1467
   213
                    if (status != PEP_STATUS_OK)
edouard@1467
   214
                        return (int) invalid_action;
edouard@1467
   215
                    if(session->sync_state_payload){
edouard@1467
   216
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   217
                        session->sync_state_payload = NULL;
edouard@1467
   218
                    }
edouard@1467
   219
                    return Sole;
edouard@1467
   220
                }
edouard@1467
   221
                default:
edouard@1467
   222
                    return (DeviceState_state) invalid_event;
edouard@1467
   223
            }
edouard@1467
   224
            break;
edouard@1467
   225
        }
edouard@1467
   226
        case Grouped:
edouard@1467
   227
        {
edouard@1467
   228
            *timeout = 0;
edouard@1467
   229
            switch (event) {
edouard@1467
   230
                case Init:
edouard@1467
   231
                {
edouard@1467
   232
                    status = enterGroup(session, state, NULL, NULL);
edouard@1467
   233
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   234
                        return (int) invalid_out_of_memory;
edouard@1467
   235
                    if (status != PEP_STATUS_OK)
edouard@1467
   236
                        return (int) invalid_action;
edouard@1467
   237
                    break;
edouard@1467
   238
                }
edouard@1467
   239
                case KeyGen:
edouard@1467
   240
                {
edouard@1467
   241
                    status = sendGroupUpdate(session, state, NULL, NULL);
edouard@1467
   242
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   243
                        return (int) invalid_out_of_memory;
edouard@1467
   244
                    if (status != PEP_STATUS_OK)
edouard@1467
   245
                        return (int) invalid_action;
edouard@1467
   246
                    break;
edouard@1467
   247
                }
edouard@1467
   248
                case CannotDecrypt:
edouard@1467
   249
                {
edouard@1467
   250
                    status = sendUpdateRequest(session, state, NULL, NULL);
edouard@1467
   251
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   252
                        return (int) invalid_out_of_memory;
edouard@1467
   253
                    if (status != PEP_STATUS_OK)
edouard@1467
   254
                        return (int) invalid_action;
edouard@1467
   255
                    break;
edouard@1467
   256
                }
edouard@1467
   257
                case UpdateRequest:
edouard@1467
   258
                {
edouard@1467
   259
                    status = sendGroupUpdate(session, state, NULL, NULL);
edouard@1467
   260
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   261
                        return (int) invalid_out_of_memory;
edouard@1467
   262
                    if (status != PEP_STATUS_OK)
edouard@1467
   263
                        return (int) invalid_action;
edouard@1467
   264
                    break;
edouard@1467
   265
                }
edouard@1467
   266
                case Beacon:
edouard@1467
   267
                {
edouard@1467
   268
                    status = sendHandshakeRequest(session, state, partner, NULL);
edouard@1467
   269
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   270
                        return (int) invalid_out_of_memory;
edouard@1467
   271
                    if (status != PEP_STATUS_OK)
edouard@1467
   272
                        return (int) invalid_action;
edouard@1467
   273
                    break;
edouard@1467
   274
                }
edouard@1467
   275
                case HandshakeRequest:
edouard@1467
   276
                {
edouard@1467
   277
                    status = sendHandshakeRequest(session, state, partner, NULL);
edouard@1467
   278
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   279
                        return (int) invalid_out_of_memory;
edouard@1467
   280
                    if (status != PEP_STATUS_OK)
edouard@1467
   281
                        return (int) invalid_action;
edouard@1467
   282
                    session->sync_state_payload = identity_dup(partner);
edouard@1467
   283
                    return HandshakingGrouped;
edouard@1467
   284
                }
edouard@1467
   285
                case GroupUpdate:
edouard@1467
   286
                {
edouard@1467
   287
                    status = storeGroupKeys(session, state, partner, extra /*keys*/);
edouard@1467
   288
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   289
                        return (int) invalid_out_of_memory;
edouard@1467
   290
                    if (status != PEP_STATUS_OK)
edouard@1467
   291
                        return (int) invalid_action;
edouard@1467
   292
                    break;
edouard@1467
   293
                }
edouard@1467
   294
                default:
edouard@1467
   295
                    return (DeviceState_state) invalid_event;
edouard@1467
   296
            }
edouard@1467
   297
            break;
edouard@1467
   298
        }
edouard@1467
   299
        case HandshakingGrouped:
edouard@1467
   300
        {
edouard@1467
   301
            Identity expected = (Identity)session->sync_state_payload;
edouard@1467
   302
            *timeout = 600;
edouard@1467
   303
            switch (event) {
edouard@1467
   304
                case Init:
edouard@1467
   305
                {
edouard@1477
   306
                    status = notifyInitAddOurDevice(session, state, partner, NULL);
edouard@1467
   307
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   308
                        return (int) invalid_out_of_memory;
edouard@1467
   309
                    if (status != PEP_STATUS_OK)
edouard@1467
   310
                        return (int) invalid_action;
edouard@1467
   311
                    break;
edouard@1467
   312
                }
edouard@1467
   313
                case HandshakeRejected:
edouard@1467
   314
                {
edouard@1467
   315
                    status = rejectHandshake(session, state, partner, NULL);
edouard@1467
   316
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   317
                        return (int) invalid_out_of_memory;
edouard@1467
   318
                    if (status != PEP_STATUS_OK)
edouard@1467
   319
                        return (int) invalid_action;
edouard@1467
   320
                    if(session->sync_state_payload){
edouard@1467
   321
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   322
                        session->sync_state_payload = NULL;
edouard@1467
   323
                    }
edouard@1467
   324
                    return Grouped;
edouard@1467
   325
                }
edouard@1467
   326
                case HandshakeAccepted:
edouard@1467
   327
                {
edouard@1467
   328
                    status = acceptHandshake(session, state, partner, NULL);
edouard@1467
   329
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   330
                        return (int) invalid_out_of_memory;
edouard@1467
   331
                    if (status != PEP_STATUS_OK)
edouard@1467
   332
                        return (int) invalid_action;
edouard@1467
   333
                    status = sendGroupKeys(session, state, partner, NULL);
edouard@1467
   334
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   335
                        return (int) invalid_out_of_memory;
edouard@1467
   336
                    if (status != PEP_STATUS_OK)
edouard@1467
   337
                        return (int) invalid_action;
edouard@1467
   338
                    if(session->sync_state_payload){
edouard@1467
   339
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   340
                        session->sync_state_payload = NULL;
edouard@1467
   341
                    }
edouard@1467
   342
                    return Grouped;
edouard@1467
   343
                }
edouard@1467
   344
                case Timeout:
edouard@1467
   345
                {
edouard@1477
   346
                    status = notifyTimeout(session, state, expected, NULL);
edouard@1467
   347
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   348
                        return (int) invalid_out_of_memory;
edouard@1467
   349
                    if (status != PEP_STATUS_OK)
edouard@1467
   350
                        return (int) invalid_action;
edouard@1467
   351
                    if(session->sync_state_payload){
edouard@1467
   352
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   353
                        session->sync_state_payload = NULL;
edouard@1467
   354
                    }
edouard@1467
   355
                    return Grouped;
edouard@1467
   356
                }
edouard@1467
   357
                default:
edouard@1467
   358
                    return (DeviceState_state) invalid_event;
edouard@1467
   359
            }
edouard@1467
   360
            break;
edouard@1467
   361
        }
edouard@1467
   362
        default:
edouard@1467
   363
            return (DeviceState_state) invalid_state;
edouard@1467
   364
    }
edouard@1467
   365
edouard@1467
   366
    return state;
edouard@1467
   367
}
edouard@1467
   368