sync/generated/sync_fsm.c
author Edouard Tisserant <edouard@pep-project.org>
Thu, 15 Dec 2016 15:43:14 +0100
changeset 1490 e375c22bc587
parent 1477 c0d761ae1fd9
child 1523 bc4a9401af51
permissions -rw-r--r--
added verbose sync FSM debug logging
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@1490
    22
            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=InitState")
edouard@1467
    23
            switch (event) {
edouard@1467
    24
                case Init:
edouard@1467
    25
                {
edouard@1490
    26
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=InitState", "event=Init")
edouard@1467
    27
                    cond_result = storedGroupKeys(session);
edouard@1490
    28
                    #ifndef NDEBUG
edouard@1490
    29
                    char resstr[11] = {0,};
edouard@1490
    30
                    snprintf(resstr,10,"result=%d",cond_result);
edouard@1490
    31
                    #endif
edouard@1490
    32
                    DEBUG_LOG("FSM condition", "sync_fsm.c, state=InitState, event=Init, condition=storedGroupKeys", resstr)
edouard@1467
    33
                    if (cond_result < 0)
edouard@1467
    34
                        return cond_result;
edouard@1467
    35
                    if (cond_result) {
edouard@1490
    36
                        DEBUG_LOG("FSM transition", "sync_fsm.c, state=InitState, event=Init", "target=Grouped")
edouard@1467
    37
                        return Grouped;
edouard@1467
    38
                    }
edouard@1490
    39
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=InitState, event=Init", "target=Sole")
edouard@1467
    40
                    return Sole;
edouard@1467
    41
                }
edouard@1467
    42
                default:
edouard@1467
    43
                    return (DeviceState_state) invalid_event;
edouard@1467
    44
            }
edouard@1467
    45
            break;
edouard@1467
    46
        }
edouard@1467
    47
        case Sole:
edouard@1467
    48
        {
edouard@1467
    49
            *timeout = 0;
edouard@1490
    50
            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=Sole")
edouard@1467
    51
            switch (event) {
edouard@1490
    52
                case Init: DEBUG_LOG("FSM event", "sync_fsm.c, state=Sole", "event=Init") break;
edouard@1467
    53
                case KeyGen:
edouard@1467
    54
                {
edouard@1490
    55
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Sole", "event=KeyGen")
edouard@1490
    56
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Sole, event=KeyGen", "action=sendBeacon")
edouard@1467
    57
                    status = sendBeacon(session, state, NULL, NULL);
edouard@1467
    58
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
    59
                        return (int) invalid_out_of_memory;
edouard@1467
    60
                    if (status != PEP_STATUS_OK)
edouard@1467
    61
                        return (int) invalid_action;
edouard@1467
    62
                    break;
edouard@1467
    63
                }
edouard@1467
    64
                case CannotDecrypt:
edouard@1467
    65
                {
edouard@1490
    66
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Sole", "event=CannotDecrypt")
edouard@1490
    67
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Sole, event=CannotDecrypt", "action=sendBeacon")
edouard@1467
    68
                    status = sendBeacon(session, state, NULL, NULL);
edouard@1467
    69
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
    70
                        return (int) invalid_out_of_memory;
edouard@1467
    71
                    if (status != PEP_STATUS_OK)
edouard@1467
    72
                        return (int) invalid_action;
edouard@1467
    73
                    break;
edouard@1467
    74
                }
edouard@1467
    75
                case Beacon:
edouard@1467
    76
                {
edouard@1490
    77
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Sole", "event=Beacon")
edouard@1490
    78
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Sole, event=Beacon", "action=sendHandshakeRequest")
edouard@1467
    79
                    status = sendHandshakeRequest(session, state, partner, NULL);
edouard@1467
    80
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
    81
                        return (int) invalid_out_of_memory;
edouard@1467
    82
                    if (status != PEP_STATUS_OK)
edouard@1467
    83
                        return (int) invalid_action;
edouard@1467
    84
                    break;
edouard@1467
    85
                }
edouard@1467
    86
                case HandshakeRequest:
edouard@1467
    87
                {
edouard@1490
    88
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Sole", "event=HandshakeRequest")
edouard@1490
    89
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Sole, event=HandshakeRequest", "action=sendHandshakeRequest")
edouard@1467
    90
                    status = sendHandshakeRequest(session, state, partner, NULL);
edouard@1467
    91
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
    92
                        return (int) invalid_out_of_memory;
edouard@1467
    93
                    if (status != PEP_STATUS_OK)
edouard@1467
    94
                        return (int) invalid_action;
edouard@1467
    95
                    session->sync_state_payload = identity_dup(partner);
edouard@1490
    96
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=Sole, event=HandshakeRequest", "target=HandshakingSole")
edouard@1467
    97
                    return HandshakingSole;
edouard@1467
    98
                }
edouard@1467
    99
                default:
edouard@1467
   100
                    return (DeviceState_state) invalid_event;
edouard@1467
   101
            }
edouard@1467
   102
            break;
edouard@1467
   103
        }
edouard@1467
   104
        case HandshakingSole:
edouard@1467
   105
        {
edouard@1467
   106
            Identity expected = (Identity)session->sync_state_payload;
edouard@1467
   107
            *timeout = 600;
edouard@1490
   108
            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=HandshakingSole")
edouard@1467
   109
            switch (event) {
edouard@1467
   110
                case Init:
edouard@1467
   111
                {
edouard@1490
   112
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingSole", "event=Init")
edouard@1477
   113
                    cond_result = keyElectionWon(session, partner);
edouard@1490
   114
                    #ifndef NDEBUG
edouard@1490
   115
                    char resstr[11] = {0,};
edouard@1490
   116
                    snprintf(resstr,10,"result=%d",cond_result);
edouard@1490
   117
                    #endif
edouard@1490
   118
                    DEBUG_LOG("FSM condition", "sync_fsm.c, state=HandshakingSole, event=Init, condition=keyElectionWon", resstr)
edouard@1477
   119
                    if (cond_result < 0)
edouard@1477
   120
                        return cond_result;
edouard@1477
   121
                    if (cond_result) {
edouard@1490
   122
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=Init", "action=notifyInitFormGroup")
edouard@1477
   123
                        status = notifyInitFormGroup(session, state, partner, NULL);
edouard@1477
   124
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1477
   125
                            return (int) invalid_out_of_memory;
edouard@1477
   126
                        if (status != PEP_STATUS_OK)
edouard@1477
   127
                            return (int) invalid_action;
edouard@1477
   128
                    }
edouard@1477
   129
                    else {
edouard@1490
   130
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=Init", "action=notifyInitAddOurDevice")
edouard@1477
   131
                        status = notifyInitAddOurDevice(session, state, partner, NULL);
edouard@1477
   132
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1477
   133
                            return (int) invalid_out_of_memory;
edouard@1477
   134
                        if (status != PEP_STATUS_OK)
edouard@1477
   135
                            return (int) invalid_action;
edouard@1477
   136
                    }
edouard@1467
   137
                    break;
edouard@1467
   138
                }
edouard@1467
   139
                case HandshakeRejected:
edouard@1467
   140
                {
edouard@1490
   141
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingSole", "event=HandshakeRejected")
edouard@1490
   142
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=HandshakeRejected", "action=rejectHandshake")
edouard@1467
   143
                    status = rejectHandshake(session, state, partner, NULL);
edouard@1467
   144
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   145
                        return (int) invalid_out_of_memory;
edouard@1467
   146
                    if (status != PEP_STATUS_OK)
edouard@1467
   147
                        return (int) invalid_action;
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@1490
   152
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingSole, event=HandshakeRejected", "target=Sole")
edouard@1467
   153
                    return Sole;
edouard@1467
   154
                }
edouard@1467
   155
                case HandshakeAccepted:
edouard@1467
   156
                {
edouard@1490
   157
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingSole", "event=HandshakeAccepted")
edouard@1490
   158
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "action=acceptHandshake")
edouard@1467
   159
                    status = acceptHandshake(session, state, partner, NULL);
edouard@1467
   160
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   161
                        return (int) invalid_out_of_memory;
edouard@1467
   162
                    if (status != PEP_STATUS_OK)
edouard@1467
   163
                        return (int) invalid_action;
edouard@1467
   164
                    cond_result = keyElectionWon(session, partner);
edouard@1490
   165
                    #ifndef NDEBUG
edouard@1490
   166
                    char resstr[11] = {0,};
edouard@1490
   167
                    snprintf(resstr,10,"result=%d",cond_result);
edouard@1490
   168
                    #endif
edouard@1490
   169
                    DEBUG_LOG("FSM condition", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted, condition=keyElectionWon", resstr)
edouard@1467
   170
                    if (cond_result < 0)
edouard@1467
   171
                        return cond_result;
edouard@1467
   172
                    if (cond_result) {
edouard@1490
   173
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "action=sendGroupKeys")
edouard@1467
   174
                        status = sendGroupKeys(session, state, partner, NULL);
edouard@1467
   175
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   176
                            return (int) invalid_out_of_memory;
edouard@1467
   177
                        if (status != PEP_STATUS_OK)
edouard@1467
   178
                            return (int) invalid_action;
edouard@1490
   179
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "action=notifyAcceptedGroupCreated")
edouard@1477
   180
                        status = notifyAcceptedGroupCreated(session, state, partner, NULL);
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
                        if(session->sync_state_payload){
edouard@1467
   186
                            free_identity((Identity)session->sync_state_payload);
edouard@1467
   187
                            session->sync_state_payload = NULL;
edouard@1467
   188
                        }
edouard@1490
   189
                        DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "target=Grouped")
edouard@1467
   190
                        return Grouped;
edouard@1467
   191
                    }
edouard@1467
   192
                    if(session->sync_state_payload){
edouard@1467
   193
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   194
                        session->sync_state_payload = NULL;
edouard@1467
   195
                    }
edouard@1467
   196
                    session->sync_state_payload = identity_dup(partner);
edouard@1490
   197
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "target=WaitForGroupKeysSole")
edouard@1467
   198
                    return WaitForGroupKeysSole;
edouard@1467
   199
                }
edouard@1467
   200
                case Cancel:
edouard@1467
   201
                {
edouard@1490
   202
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingSole", "event=Cancel")
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@1490
   207
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingSole, event=Cancel", "target=Sole")
edouard@1467
   208
                    return Sole;
edouard@1467
   209
                }
edouard@1467
   210
                case Timeout:
edouard@1467
   211
                {
edouard@1490
   212
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingSole", "event=Timeout")
edouard@1490
   213
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=Timeout", "action=notifyTimeout")
edouard@1477
   214
                    status = notifyTimeout(session, state, expected, NULL);
edouard@1467
   215
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   216
                        return (int) invalid_out_of_memory;
edouard@1467
   217
                    if (status != PEP_STATUS_OK)
edouard@1467
   218
                        return (int) invalid_action;
edouard@1467
   219
                    if(session->sync_state_payload){
edouard@1467
   220
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   221
                        session->sync_state_payload = NULL;
edouard@1467
   222
                    }
edouard@1490
   223
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingSole, event=Timeout", "target=Sole")
edouard@1467
   224
                    return Sole;
edouard@1467
   225
                }
edouard@1467
   226
                default:
edouard@1467
   227
                    return (DeviceState_state) invalid_event;
edouard@1467
   228
            }
edouard@1467
   229
            break;
edouard@1467
   230
        }
edouard@1467
   231
        case WaitForGroupKeysSole:
edouard@1467
   232
        {
edouard@1467
   233
            Identity expected = (Identity)session->sync_state_payload;
edouard@1467
   234
            *timeout = 600;
edouard@1490
   235
            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=WaitForGroupKeysSole")
edouard@1467
   236
            switch (event) {
edouard@1490
   237
                case Init: DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForGroupKeysSole", "event=Init") break;
edouard@1467
   238
                case GroupKeys:
edouard@1467
   239
                {
edouard@1490
   240
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForGroupKeysSole", "event=GroupKeys")
edouard@1490
   241
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysSole, event=GroupKeys", "action=storeGroupKeys")
edouard@1467
   242
                    status = storeGroupKeys(session, state, partner, extra /*keys*/);
edouard@1467
   243
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   244
                        return (int) invalid_out_of_memory;
edouard@1467
   245
                    if (status != PEP_STATUS_OK)
edouard@1467
   246
                        return (int) invalid_action;
edouard@1490
   247
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysSole, event=GroupKeys", "action=notifyAcceptedDeviceAdded")
edouard@1477
   248
                    status = notifyAcceptedDeviceAdded(session, state, partner, NULL);
edouard@1467
   249
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   250
                        return (int) invalid_out_of_memory;
edouard@1467
   251
                    if (status != PEP_STATUS_OK)
edouard@1467
   252
                        return (int) invalid_action;
edouard@1467
   253
                    if(session->sync_state_payload){
edouard@1467
   254
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   255
                        session->sync_state_payload = NULL;
edouard@1467
   256
                    }
edouard@1490
   257
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=WaitForGroupKeysSole, event=GroupKeys", "target=Grouped")
edouard@1467
   258
                    return Grouped;
edouard@1467
   259
                }
edouard@1467
   260
                case Timeout:
edouard@1467
   261
                {
edouard@1490
   262
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForGroupKeysSole", "event=Timeout")
edouard@1490
   263
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysSole, event=Timeout", "action=notifyTimeout")
edouard@1477
   264
                    status = notifyTimeout(session, state, expected, NULL);
edouard@1467
   265
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   266
                        return (int) invalid_out_of_memory;
edouard@1467
   267
                    if (status != PEP_STATUS_OK)
edouard@1467
   268
                        return (int) invalid_action;
edouard@1467
   269
                    if(session->sync_state_payload){
edouard@1467
   270
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   271
                        session->sync_state_payload = NULL;
edouard@1467
   272
                    }
edouard@1490
   273
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=WaitForGroupKeysSole, event=Timeout", "target=Sole")
edouard@1467
   274
                    return Sole;
edouard@1467
   275
                }
edouard@1467
   276
                default:
edouard@1467
   277
                    return (DeviceState_state) invalid_event;
edouard@1467
   278
            }
edouard@1467
   279
            break;
edouard@1467
   280
        }
edouard@1467
   281
        case Grouped:
edouard@1467
   282
        {
edouard@1467
   283
            *timeout = 0;
edouard@1490
   284
            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=Grouped")
edouard@1467
   285
            switch (event) {
edouard@1467
   286
                case Init:
edouard@1467
   287
                {
edouard@1490
   288
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=Init")
edouard@1490
   289
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=Init", "action=enterGroup")
edouard@1467
   290
                    status = enterGroup(session, state, NULL, NULL);
edouard@1467
   291
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   292
                        return (int) invalid_out_of_memory;
edouard@1467
   293
                    if (status != PEP_STATUS_OK)
edouard@1467
   294
                        return (int) invalid_action;
edouard@1467
   295
                    break;
edouard@1467
   296
                }
edouard@1467
   297
                case KeyGen:
edouard@1467
   298
                {
edouard@1490
   299
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=KeyGen")
edouard@1490
   300
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=KeyGen", "action=sendGroupUpdate")
edouard@1467
   301
                    status = sendGroupUpdate(session, state, NULL, NULL);
edouard@1467
   302
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   303
                        return (int) invalid_out_of_memory;
edouard@1467
   304
                    if (status != PEP_STATUS_OK)
edouard@1467
   305
                        return (int) invalid_action;
edouard@1467
   306
                    break;
edouard@1467
   307
                }
edouard@1467
   308
                case CannotDecrypt:
edouard@1467
   309
                {
edouard@1490
   310
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=CannotDecrypt")
edouard@1490
   311
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=CannotDecrypt", "action=sendUpdateRequest")
edouard@1467
   312
                    status = sendUpdateRequest(session, state, NULL, NULL);
edouard@1467
   313
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   314
                        return (int) invalid_out_of_memory;
edouard@1467
   315
                    if (status != PEP_STATUS_OK)
edouard@1467
   316
                        return (int) invalid_action;
edouard@1467
   317
                    break;
edouard@1467
   318
                }
edouard@1467
   319
                case UpdateRequest:
edouard@1467
   320
                {
edouard@1490
   321
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=UpdateRequest")
edouard@1490
   322
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=UpdateRequest", "action=sendGroupUpdate")
edouard@1467
   323
                    status = sendGroupUpdate(session, state, NULL, NULL);
edouard@1467
   324
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   325
                        return (int) invalid_out_of_memory;
edouard@1467
   326
                    if (status != PEP_STATUS_OK)
edouard@1467
   327
                        return (int) invalid_action;
edouard@1467
   328
                    break;
edouard@1467
   329
                }
edouard@1467
   330
                case Beacon:
edouard@1467
   331
                {
edouard@1490
   332
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=Beacon")
edouard@1490
   333
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=Beacon", "action=sendHandshakeRequest")
edouard@1467
   334
                    status = sendHandshakeRequest(session, state, partner, NULL);
edouard@1467
   335
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   336
                        return (int) invalid_out_of_memory;
edouard@1467
   337
                    if (status != PEP_STATUS_OK)
edouard@1467
   338
                        return (int) invalid_action;
edouard@1467
   339
                    break;
edouard@1467
   340
                }
edouard@1467
   341
                case HandshakeRequest:
edouard@1467
   342
                {
edouard@1490
   343
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=HandshakeRequest")
edouard@1490
   344
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=HandshakeRequest", "action=sendHandshakeRequest")
edouard@1467
   345
                    status = sendHandshakeRequest(session, state, partner, NULL);
edouard@1467
   346
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   347
                        return (int) invalid_out_of_memory;
edouard@1467
   348
                    if (status != PEP_STATUS_OK)
edouard@1467
   349
                        return (int) invalid_action;
edouard@1467
   350
                    session->sync_state_payload = identity_dup(partner);
edouard@1490
   351
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=Grouped, event=HandshakeRequest", "target=HandshakingGrouped")
edouard@1467
   352
                    return HandshakingGrouped;
edouard@1467
   353
                }
edouard@1467
   354
                case GroupUpdate:
edouard@1467
   355
                {
edouard@1490
   356
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=GroupUpdate")
edouard@1490
   357
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=GroupUpdate", "action=storeGroupKeys")
edouard@1467
   358
                    status = storeGroupKeys(session, state, partner, extra /*keys*/);
edouard@1467
   359
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   360
                        return (int) invalid_out_of_memory;
edouard@1467
   361
                    if (status != PEP_STATUS_OK)
edouard@1467
   362
                        return (int) invalid_action;
edouard@1467
   363
                    break;
edouard@1467
   364
                }
edouard@1467
   365
                default:
edouard@1467
   366
                    return (DeviceState_state) invalid_event;
edouard@1467
   367
            }
edouard@1467
   368
            break;
edouard@1467
   369
        }
edouard@1467
   370
        case HandshakingGrouped:
edouard@1467
   371
        {
edouard@1467
   372
            Identity expected = (Identity)session->sync_state_payload;
edouard@1467
   373
            *timeout = 600;
edouard@1490
   374
            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=HandshakingGrouped")
edouard@1467
   375
            switch (event) {
edouard@1467
   376
                case Init:
edouard@1467
   377
                {
edouard@1490
   378
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingGrouped", "event=Init")
edouard@1490
   379
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=Init", "action=notifyInitAddOurDevice")
edouard@1477
   380
                    status = notifyInitAddOurDevice(session, state, partner, NULL);
edouard@1467
   381
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   382
                        return (int) invalid_out_of_memory;
edouard@1467
   383
                    if (status != PEP_STATUS_OK)
edouard@1467
   384
                        return (int) invalid_action;
edouard@1467
   385
                    break;
edouard@1467
   386
                }
edouard@1467
   387
                case HandshakeRejected:
edouard@1467
   388
                {
edouard@1490
   389
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingGrouped", "event=HandshakeRejected")
edouard@1490
   390
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeRejected", "action=rejectHandshake")
edouard@1467
   391
                    status = rejectHandshake(session, state, partner, NULL);
edouard@1467
   392
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   393
                        return (int) invalid_out_of_memory;
edouard@1467
   394
                    if (status != PEP_STATUS_OK)
edouard@1467
   395
                        return (int) invalid_action;
edouard@1467
   396
                    if(session->sync_state_payload){
edouard@1467
   397
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   398
                        session->sync_state_payload = NULL;
edouard@1467
   399
                    }
edouard@1490
   400
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeRejected", "target=Grouped")
edouard@1467
   401
                    return Grouped;
edouard@1467
   402
                }
edouard@1467
   403
                case HandshakeAccepted:
edouard@1467
   404
                {
edouard@1490
   405
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingGrouped", "event=HandshakeAccepted")
edouard@1490
   406
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeAccepted", "action=acceptHandshake")
edouard@1467
   407
                    status = acceptHandshake(session, state, partner, NULL);
edouard@1467
   408
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   409
                        return (int) invalid_out_of_memory;
edouard@1467
   410
                    if (status != PEP_STATUS_OK)
edouard@1467
   411
                        return (int) invalid_action;
edouard@1490
   412
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeAccepted", "action=sendGroupKeys")
edouard@1467
   413
                    status = sendGroupKeys(session, state, partner, NULL);
edouard@1467
   414
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   415
                        return (int) invalid_out_of_memory;
edouard@1467
   416
                    if (status != PEP_STATUS_OK)
edouard@1467
   417
                        return (int) invalid_action;
edouard@1467
   418
                    if(session->sync_state_payload){
edouard@1467
   419
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   420
                        session->sync_state_payload = NULL;
edouard@1467
   421
                    }
edouard@1490
   422
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeAccepted", "target=Grouped")
edouard@1467
   423
                    return Grouped;
edouard@1467
   424
                }
edouard@1467
   425
                case Timeout:
edouard@1467
   426
                {
edouard@1490
   427
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingGrouped", "event=Timeout")
edouard@1490
   428
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=Timeout", "action=notifyTimeout")
edouard@1477
   429
                    status = notifyTimeout(session, state, expected, NULL);
edouard@1467
   430
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   431
                        return (int) invalid_out_of_memory;
edouard@1467
   432
                    if (status != PEP_STATUS_OK)
edouard@1467
   433
                        return (int) invalid_action;
edouard@1467
   434
                    if(session->sync_state_payload){
edouard@1467
   435
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   436
                        session->sync_state_payload = NULL;
edouard@1467
   437
                    }
edouard@1490
   438
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingGrouped, event=Timeout", "target=Grouped")
edouard@1467
   439
                    return Grouped;
edouard@1467
   440
                }
edouard@1467
   441
                default:
edouard@1467
   442
                    return (DeviceState_state) invalid_event;
edouard@1467
   443
            }
edouard@1467
   444
            break;
edouard@1467
   445
        }
edouard@1467
   446
        default:
edouard@1467
   447
            return (DeviceState_state) invalid_state;
edouard@1467
   448
    }
edouard@1467
   449
edouard@1467
   450
    return state;
edouard@1467
   451
}
edouard@1467
   452