sync/generated/sync_fsm.c
author Edouard Tisserant <edouard@pep-project.org>
Thu, 16 Feb 2017 12:43:35 +0100
branchGroupMerge
changeset 1586 599deda50386
parent 1574 12b7a45140d3
child 1588 1a43a7eddf90
permissions -rw-r--r--
KeySync: added device group merge. Builds but still untested.
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@1574
    27
                    cond_result = deviceGrouped(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@1574
    32
                    DEBUG_LOG("FSM condition", "sync_fsm.c, state=InitState, event=Init, condition=deviceGrouped", 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@1523
    84
                    session->sync_state_payload = identity_dup(partner);
edouard@1523
    85
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=Sole, event=Beacon", "target=SoleBeaconed")
edouard@1523
    86
                    return SoleBeaconed;
edouard@1467
    87
                }
edouard@1467
    88
                case HandshakeRequest:
edouard@1467
    89
                {
edouard@1490
    90
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Sole", "event=HandshakeRequest")
edouard@1490
    91
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Sole, event=HandshakeRequest", "action=sendHandshakeRequest")
edouard@1467
    92
                    status = sendHandshakeRequest(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
                    session->sync_state_payload = identity_dup(partner);
edouard@1490
    98
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=Sole, event=HandshakeRequest", "target=HandshakingSole")
edouard@1467
    99
                    return HandshakingSole;
edouard@1467
   100
                }
edouard@1467
   101
                default:
edouard@1467
   102
                    return (DeviceState_state) invalid_event;
edouard@1467
   103
            }
edouard@1467
   104
            break;
edouard@1467
   105
        }
edouard@1523
   106
        case SoleBeaconed:
edouard@1523
   107
        {
edouard@1523
   108
            Identity expected = (Identity)session->sync_state_payload;
edouard@1523
   109
            *timeout = 600;
edouard@1523
   110
            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=SoleBeaconed")
edouard@1523
   111
            switch (event) {
edouard@1523
   112
                case Init: DEBUG_LOG("FSM event", "sync_fsm.c, state=SoleBeaconed", "event=Init") break;
edouard@1523
   113
                case KeyGen:
edouard@1523
   114
                {
edouard@1523
   115
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=SoleBeaconed", "event=KeyGen")
edouard@1523
   116
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=SoleBeaconed, event=KeyGen", "action=sendBeacon")
edouard@1523
   117
                    status = sendBeacon(session, state, NULL, NULL);
edouard@1523
   118
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   119
                        return (int) invalid_out_of_memory;
edouard@1523
   120
                    if (status != PEP_STATUS_OK)
edouard@1523
   121
                        return (int) invalid_action;
edouard@1523
   122
                    if(session->sync_state_payload){
edouard@1523
   123
                        free_identity((Identity)session->sync_state_payload);
edouard@1523
   124
                        session->sync_state_payload = NULL;
edouard@1523
   125
                    }
edouard@1523
   126
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=SoleBeaconed, event=KeyGen", "target=Sole")
edouard@1523
   127
                    return Sole;
edouard@1523
   128
                }
edouard@1523
   129
                case CannotDecrypt:
edouard@1523
   130
                {
edouard@1523
   131
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=SoleBeaconed", "event=CannotDecrypt")
edouard@1523
   132
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=SoleBeaconed, event=CannotDecrypt", "action=sendBeacon")
edouard@1523
   133
                    status = sendBeacon(session, state, NULL, NULL);
edouard@1523
   134
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   135
                        return (int) invalid_out_of_memory;
edouard@1523
   136
                    if (status != PEP_STATUS_OK)
edouard@1523
   137
                        return (int) invalid_action;
edouard@1523
   138
                    if(session->sync_state_payload){
edouard@1523
   139
                        free_identity((Identity)session->sync_state_payload);
edouard@1523
   140
                        session->sync_state_payload = NULL;
edouard@1523
   141
                    }
edouard@1523
   142
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=SoleBeaconed, event=CannotDecrypt", "target=Sole")
edouard@1523
   143
                    return Sole;
edouard@1523
   144
                }
edouard@1523
   145
                case Beacon:
edouard@1523
   146
                {
edouard@1523
   147
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=SoleBeaconed", "event=Beacon")
edouard@1523
   148
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=SoleBeaconed, event=Beacon", "action=sendHandshakeRequest")
edouard@1523
   149
                    status = sendHandshakeRequest(session, state, partner, NULL);
edouard@1523
   150
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   151
                        return (int) invalid_out_of_memory;
edouard@1523
   152
                    if (status != PEP_STATUS_OK)
edouard@1523
   153
                        return (int) invalid_action;
edouard@1523
   154
                    if(session->sync_state_payload){
edouard@1523
   155
                        free_identity((Identity)session->sync_state_payload);
edouard@1523
   156
                        session->sync_state_payload = NULL;
edouard@1523
   157
                    }
edouard@1523
   158
                    session->sync_state_payload = identity_dup(partner);
edouard@1523
   159
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=SoleBeaconed, event=Beacon", "target=SoleBeaconed")
edouard@1523
   160
                    return SoleBeaconed;
edouard@1523
   161
                }
edouard@1523
   162
                case HandshakeRequest:
edouard@1523
   163
                {
edouard@1523
   164
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=SoleBeaconed", "event=HandshakeRequest")
edouard@1523
   165
                    cond_result = sameIdentities(session, partner, expected);
edouard@1523
   166
                    #ifndef NDEBUG
edouard@1523
   167
                    char resstr[11] = {0,};
edouard@1523
   168
                    snprintf(resstr,10,"result=%d",cond_result);
edouard@1523
   169
                    #endif
edouard@1523
   170
                    DEBUG_LOG("FSM condition", "sync_fsm.c, state=SoleBeaconed, event=HandshakeRequest, condition=sameIdentities", resstr)
edouard@1523
   171
                    if (cond_result < 0)
edouard@1523
   172
                        return cond_result;
edouard@1523
   173
                    if (cond_result) {
edouard@1523
   174
                    }
edouard@1523
   175
                    else {
edouard@1523
   176
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=SoleBeaconed, event=HandshakeRequest", "action=sendHandshakeRequest")
edouard@1523
   177
                        status = sendHandshakeRequest(session, state, partner, NULL);
edouard@1523
   178
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   179
                            return (int) invalid_out_of_memory;
edouard@1523
   180
                        if (status != PEP_STATUS_OK)
edouard@1523
   181
                            return (int) invalid_action;
edouard@1523
   182
                    }
edouard@1523
   183
                    if(session->sync_state_payload){
edouard@1523
   184
                        free_identity((Identity)session->sync_state_payload);
edouard@1523
   185
                        session->sync_state_payload = NULL;
edouard@1523
   186
                    }
edouard@1523
   187
                    session->sync_state_payload = identity_dup(partner);
edouard@1523
   188
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=SoleBeaconed, event=HandshakeRequest", "target=HandshakingSole")
edouard@1523
   189
                    return HandshakingSole;
edouard@1523
   190
                }
edouard@1523
   191
                case Timeout:
edouard@1523
   192
                {
edouard@1523
   193
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=SoleBeaconed", "event=Timeout")
edouard@1523
   194
                    if(session->sync_state_payload){
edouard@1523
   195
                        free_identity((Identity)session->sync_state_payload);
edouard@1523
   196
                        session->sync_state_payload = NULL;
edouard@1523
   197
                    }
edouard@1523
   198
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=SoleBeaconed, event=Timeout", "target=Sole")
edouard@1523
   199
                    return Sole;
edouard@1523
   200
                }
edouard@1523
   201
                default:
edouard@1523
   202
                    return (DeviceState_state) invalid_event;
edouard@1523
   203
            }
edouard@1523
   204
            break;
edouard@1523
   205
        }
edouard@1467
   206
        case HandshakingSole:
edouard@1467
   207
        {
edouard@1467
   208
            Identity expected = (Identity)session->sync_state_payload;
edouard@1467
   209
            *timeout = 600;
edouard@1490
   210
            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=HandshakingSole")
edouard@1467
   211
            switch (event) {
edouard@1467
   212
                case Init:
edouard@1467
   213
                {
edouard@1490
   214
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingSole", "event=Init")
edouard@1477
   215
                    cond_result = keyElectionWon(session, partner);
edouard@1490
   216
                    #ifndef NDEBUG
edouard@1490
   217
                    char resstr[11] = {0,};
edouard@1490
   218
                    snprintf(resstr,10,"result=%d",cond_result);
edouard@1490
   219
                    #endif
edouard@1490
   220
                    DEBUG_LOG("FSM condition", "sync_fsm.c, state=HandshakingSole, event=Init, condition=keyElectionWon", resstr)
edouard@1477
   221
                    if (cond_result < 0)
edouard@1477
   222
                        return cond_result;
edouard@1477
   223
                    if (cond_result) {
edouard@1490
   224
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=Init", "action=notifyInitFormGroup")
edouard@1477
   225
                        status = notifyInitFormGroup(session, state, partner, NULL);
edouard@1477
   226
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1477
   227
                            return (int) invalid_out_of_memory;
edouard@1477
   228
                        if (status != PEP_STATUS_OK)
edouard@1477
   229
                            return (int) invalid_action;
edouard@1477
   230
                    }
edouard@1477
   231
                    else {
edouard@1490
   232
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=Init", "action=notifyInitAddOurDevice")
edouard@1477
   233
                        status = notifyInitAddOurDevice(session, state, partner, NULL);
edouard@1477
   234
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1477
   235
                            return (int) invalid_out_of_memory;
edouard@1477
   236
                        if (status != PEP_STATUS_OK)
edouard@1477
   237
                            return (int) invalid_action;
edouard@1477
   238
                    }
edouard@1467
   239
                    break;
edouard@1467
   240
                }
edouard@1467
   241
                case HandshakeRejected:
edouard@1467
   242
                {
edouard@1490
   243
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingSole", "event=HandshakeRejected")
edouard@1490
   244
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=HandshakeRejected", "action=rejectHandshake")
edouard@1467
   245
                    status = rejectHandshake(session, state, partner, NULL);
edouard@1467
   246
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   247
                        return (int) invalid_out_of_memory;
edouard@1467
   248
                    if (status != PEP_STATUS_OK)
edouard@1467
   249
                        return (int) invalid_action;
edouard@1467
   250
                    if(session->sync_state_payload){
edouard@1467
   251
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   252
                        session->sync_state_payload = NULL;
edouard@1467
   253
                    }
edouard@1490
   254
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingSole, event=HandshakeRejected", "target=Sole")
edouard@1467
   255
                    return Sole;
edouard@1467
   256
                }
edouard@1467
   257
                case HandshakeAccepted:
edouard@1467
   258
                {
edouard@1490
   259
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingSole", "event=HandshakeAccepted")
edouard@1490
   260
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "action=acceptHandshake")
edouard@1467
   261
                    status = acceptHandshake(session, state, partner, NULL);
edouard@1467
   262
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   263
                        return (int) invalid_out_of_memory;
edouard@1467
   264
                    if (status != PEP_STATUS_OK)
edouard@1467
   265
                        return (int) invalid_action;
edouard@1467
   266
                    cond_result = keyElectionWon(session, partner);
edouard@1490
   267
                    #ifndef NDEBUG
edouard@1490
   268
                    char resstr[11] = {0,};
edouard@1490
   269
                    snprintf(resstr,10,"result=%d",cond_result);
edouard@1490
   270
                    #endif
edouard@1490
   271
                    DEBUG_LOG("FSM condition", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted, condition=keyElectionWon", resstr)
edouard@1467
   272
                    if (cond_result < 0)
edouard@1467
   273
                        return cond_result;
edouard@1467
   274
                    if (cond_result) {
edouard@1586
   275
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "action=makeGroup")
edouard@1586
   276
                        status = makeGroup(session, state, NULL, NULL);
edouard@1566
   277
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1566
   278
                            return (int) invalid_out_of_memory;
edouard@1566
   279
                        if (status != PEP_STATUS_OK)
edouard@1566
   280
                            return (int) invalid_action;
edouard@1490
   281
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "action=sendGroupKeys")
edouard@1467
   282
                        status = sendGroupKeys(session, state, partner, NULL);
edouard@1467
   283
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   284
                            return (int) invalid_out_of_memory;
edouard@1467
   285
                        if (status != PEP_STATUS_OK)
edouard@1467
   286
                            return (int) invalid_action;
edouard@1490
   287
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "action=notifyAcceptedGroupCreated")
edouard@1477
   288
                        status = notifyAcceptedGroupCreated(session, state, partner, NULL);
edouard@1467
   289
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   290
                            return (int) invalid_out_of_memory;
edouard@1467
   291
                        if (status != PEP_STATUS_OK)
edouard@1467
   292
                            return (int) invalid_action;
edouard@1467
   293
                        if(session->sync_state_payload){
edouard@1467
   294
                            free_identity((Identity)session->sync_state_payload);
edouard@1467
   295
                            session->sync_state_payload = NULL;
edouard@1467
   296
                        }
edouard@1490
   297
                        DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "target=Grouped")
edouard@1467
   298
                        return Grouped;
edouard@1467
   299
                    }
edouard@1467
   300
                    if(session->sync_state_payload){
edouard@1467
   301
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   302
                        session->sync_state_payload = NULL;
edouard@1467
   303
                    }
edouard@1467
   304
                    session->sync_state_payload = identity_dup(partner);
edouard@1490
   305
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "target=WaitForGroupKeysSole")
edouard@1467
   306
                    return WaitForGroupKeysSole;
edouard@1467
   307
                }
edouard@1467
   308
                case Cancel:
edouard@1467
   309
                {
edouard@1490
   310
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingSole", "event=Cancel")
edouard@1467
   311
                    if(session->sync_state_payload){
edouard@1467
   312
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   313
                        session->sync_state_payload = NULL;
edouard@1467
   314
                    }
edouard@1490
   315
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingSole, event=Cancel", "target=Sole")
edouard@1467
   316
                    return Sole;
edouard@1467
   317
                }
edouard@1467
   318
                case Timeout:
edouard@1467
   319
                {
edouard@1490
   320
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingSole", "event=Timeout")
edouard@1490
   321
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=Timeout", "action=notifyTimeout")
edouard@1477
   322
                    status = notifyTimeout(session, state, expected, NULL);
edouard@1467
   323
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   324
                        return (int) invalid_out_of_memory;
edouard@1467
   325
                    if (status != PEP_STATUS_OK)
edouard@1467
   326
                        return (int) invalid_action;
edouard@1555
   327
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=Timeout", "action=sendBeacon")
edouard@1555
   328
                    status = sendBeacon(session, state, NULL, NULL);
edouard@1555
   329
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1555
   330
                        return (int) invalid_out_of_memory;
edouard@1555
   331
                    if (status != PEP_STATUS_OK)
edouard@1555
   332
                        return (int) invalid_action;
edouard@1467
   333
                    if(session->sync_state_payload){
edouard@1467
   334
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   335
                        session->sync_state_payload = NULL;
edouard@1467
   336
                    }
edouard@1490
   337
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingSole, event=Timeout", "target=Sole")
edouard@1467
   338
                    return Sole;
edouard@1467
   339
                }
edouard@1467
   340
                default:
edouard@1467
   341
                    return (DeviceState_state) invalid_event;
edouard@1467
   342
            }
edouard@1467
   343
            break;
edouard@1467
   344
        }
edouard@1467
   345
        case WaitForGroupKeysSole:
edouard@1467
   346
        {
edouard@1467
   347
            Identity expected = (Identity)session->sync_state_payload;
edouard@1467
   348
            *timeout = 600;
edouard@1490
   349
            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=WaitForGroupKeysSole")
edouard@1467
   350
            switch (event) {
edouard@1490
   351
                case Init: DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForGroupKeysSole", "event=Init") break;
edouard@1467
   352
                case GroupKeys:
edouard@1467
   353
                {
edouard@1490
   354
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForGroupKeysSole", "event=GroupKeys")
edouard@1523
   355
                    cond_result = sameIdentities(session, partner, expected);
edouard@1523
   356
                    #ifndef NDEBUG
edouard@1523
   357
                    char resstr[11] = {0,};
edouard@1523
   358
                    snprintf(resstr,10,"result=%d",cond_result);
edouard@1523
   359
                    #endif
edouard@1523
   360
                    DEBUG_LOG("FSM condition", "sync_fsm.c, state=WaitForGroupKeysSole, event=GroupKeys, condition=sameIdentities", resstr)
edouard@1523
   361
                    if (cond_result < 0)
edouard@1523
   362
                        return cond_result;
edouard@1523
   363
                    if (cond_result) {
edouard@1523
   364
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysSole, event=GroupKeys", "action=storeGroupKeys")
edouard@1523
   365
                        status = storeGroupKeys(session, state, partner, extra /*keys*/);
edouard@1523
   366
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   367
                            return (int) invalid_out_of_memory;
edouard@1523
   368
                        if (status != PEP_STATUS_OK)
edouard@1523
   369
                            return (int) invalid_action;
edouard@1566
   370
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysSole, event=GroupKeys", "action=sendGroupUpdate")
edouard@1566
   371
                        status = sendGroupUpdate(session, state, NULL, NULL);
edouard@1566
   372
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1566
   373
                            return (int) invalid_out_of_memory;
edouard@1566
   374
                        if (status != PEP_STATUS_OK)
edouard@1566
   375
                            return (int) invalid_action;
edouard@1523
   376
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysSole, event=GroupKeys", "action=notifyAcceptedDeviceAdded")
edouard@1523
   377
                        status = notifyAcceptedDeviceAdded(session, state, partner, NULL);
edouard@1523
   378
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   379
                            return (int) invalid_out_of_memory;
edouard@1523
   380
                        if (status != PEP_STATUS_OK)
edouard@1523
   381
                            return (int) invalid_action;
edouard@1523
   382
                        if(session->sync_state_payload){
edouard@1523
   383
                            free_identity((Identity)session->sync_state_payload);
edouard@1523
   384
                            session->sync_state_payload = NULL;
edouard@1523
   385
                        }
edouard@1523
   386
                        DEBUG_LOG("FSM transition", "sync_fsm.c, state=WaitForGroupKeysSole, event=GroupKeys", "target=Grouped")
edouard@1523
   387
                        return Grouped;
edouard@1467
   388
                    }
edouard@1523
   389
                    break;
edouard@1467
   390
                }
edouard@1467
   391
                case Timeout:
edouard@1467
   392
                {
edouard@1490
   393
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForGroupKeysSole", "event=Timeout")
edouard@1490
   394
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysSole, event=Timeout", "action=notifyTimeout")
edouard@1477
   395
                    status = notifyTimeout(session, state, expected, NULL);
edouard@1467
   396
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   397
                        return (int) invalid_out_of_memory;
edouard@1467
   398
                    if (status != PEP_STATUS_OK)
edouard@1467
   399
                        return (int) invalid_action;
edouard@1467
   400
                    if(session->sync_state_payload){
edouard@1467
   401
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   402
                        session->sync_state_payload = NULL;
edouard@1467
   403
                    }
edouard@1490
   404
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=WaitForGroupKeysSole, event=Timeout", "target=Sole")
edouard@1467
   405
                    return Sole;
edouard@1467
   406
                }
edouard@1467
   407
                default:
edouard@1467
   408
                    return (DeviceState_state) invalid_event;
edouard@1467
   409
            }
edouard@1467
   410
            break;
edouard@1467
   411
        }
edouard@1467
   412
        case Grouped:
edouard@1467
   413
        {
edouard@1467
   414
            *timeout = 0;
edouard@1490
   415
            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=Grouped")
edouard@1467
   416
            switch (event) {
edouard@1566
   417
                case Init: DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=Init") break;
edouard@1467
   418
                case KeyGen:
edouard@1467
   419
                {
edouard@1490
   420
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=KeyGen")
edouard@1490
   421
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=KeyGen", "action=sendGroupUpdate")
edouard@1467
   422
                    status = sendGroupUpdate(session, state, NULL, NULL);
edouard@1467
   423
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   424
                        return (int) invalid_out_of_memory;
edouard@1467
   425
                    if (status != PEP_STATUS_OK)
edouard@1467
   426
                        return (int) invalid_action;
edouard@1467
   427
                    break;
edouard@1467
   428
                }
edouard@1467
   429
                case CannotDecrypt:
edouard@1467
   430
                {
edouard@1490
   431
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=CannotDecrypt")
edouard@1490
   432
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=CannotDecrypt", "action=sendUpdateRequest")
edouard@1467
   433
                    status = sendUpdateRequest(session, state, NULL, NULL);
edouard@1467
   434
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   435
                        return (int) invalid_out_of_memory;
edouard@1467
   436
                    if (status != PEP_STATUS_OK)
edouard@1467
   437
                        return (int) invalid_action;
edouard@1586
   438
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=CannotDecrypt", "action=sendBeacon")
edouard@1586
   439
                    status = sendBeacon(session, state, NULL, NULL);
edouard@1586
   440
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1586
   441
                        return (int) invalid_out_of_memory;
edouard@1586
   442
                    if (status != PEP_STATUS_OK)
edouard@1586
   443
                        return (int) invalid_action;
edouard@1467
   444
                    break;
edouard@1467
   445
                }
edouard@1467
   446
                case UpdateRequest:
edouard@1467
   447
                {
edouard@1490
   448
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=UpdateRequest")
edouard@1490
   449
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=UpdateRequest", "action=sendGroupUpdate")
edouard@1467
   450
                    status = sendGroupUpdate(session, state, NULL, NULL);
edouard@1467
   451
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   452
                        return (int) invalid_out_of_memory;
edouard@1467
   453
                    if (status != PEP_STATUS_OK)
edouard@1467
   454
                        return (int) invalid_action;
edouard@1467
   455
                    break;
edouard@1467
   456
                }
edouard@1467
   457
                case Beacon:
edouard@1467
   458
                {
edouard@1490
   459
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=Beacon")
edouard@1490
   460
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=Beacon", "action=sendHandshakeRequest")
edouard@1467
   461
                    status = sendHandshakeRequest(session, state, partner, NULL);
edouard@1467
   462
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   463
                        return (int) invalid_out_of_memory;
edouard@1467
   464
                    if (status != PEP_STATUS_OK)
edouard@1467
   465
                        return (int) invalid_action;
edouard@1523
   466
                    session->sync_state_payload = identity_dup(partner);
edouard@1523
   467
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=Grouped, event=Beacon", "target=GroupedBeaconed")
edouard@1523
   468
                    return GroupedBeaconed;
edouard@1467
   469
                }
edouard@1467
   470
                case HandshakeRequest:
edouard@1467
   471
                {
edouard@1490
   472
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=HandshakeRequest")
edouard@1490
   473
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=HandshakeRequest", "action=sendHandshakeRequest")
edouard@1467
   474
                    status = sendHandshakeRequest(session, state, partner, NULL);
edouard@1467
   475
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   476
                        return (int) invalid_out_of_memory;
edouard@1467
   477
                    if (status != PEP_STATUS_OK)
edouard@1467
   478
                        return (int) invalid_action;
edouard@1467
   479
                    session->sync_state_payload = identity_dup(partner);
edouard@1490
   480
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=Grouped, event=HandshakeRequest", "target=HandshakingGrouped")
edouard@1467
   481
                    return HandshakingGrouped;
edouard@1467
   482
                }
edouard@1467
   483
                case GroupUpdate:
edouard@1467
   484
                {
edouard@1490
   485
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=GroupUpdate")
edouard@1586
   486
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=GroupUpdate", "action=storeGroupUpdate")
edouard@1586
   487
                    status = storeGroupUpdate(session, state, partner, extra /*keys*/);
edouard@1467
   488
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   489
                        return (int) invalid_out_of_memory;
edouard@1467
   490
                    if (status != PEP_STATUS_OK)
edouard@1467
   491
                        return (int) invalid_action;
edouard@1467
   492
                    break;
edouard@1467
   493
                }
edouard@1467
   494
                default:
edouard@1467
   495
                    return (DeviceState_state) invalid_event;
edouard@1467
   496
            }
edouard@1467
   497
            break;
edouard@1467
   498
        }
edouard@1523
   499
        case GroupedBeaconed:
edouard@1523
   500
        {
edouard@1523
   501
            Identity expected = (Identity)session->sync_state_payload;
edouard@1523
   502
            *timeout = 600;
edouard@1523
   503
            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=GroupedBeaconed")
edouard@1523
   504
            switch (event) {
edouard@1523
   505
                case Init: DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=Init") break;
edouard@1523
   506
                case KeyGen:
edouard@1523
   507
                {
edouard@1523
   508
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=KeyGen")
edouard@1523
   509
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupedBeaconed, event=KeyGen", "action=sendGroupUpdate")
edouard@1523
   510
                    status = sendGroupUpdate(session, state, NULL, NULL);
edouard@1523
   511
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   512
                        return (int) invalid_out_of_memory;
edouard@1523
   513
                    if (status != PEP_STATUS_OK)
edouard@1523
   514
                        return (int) invalid_action;
edouard@1523
   515
                    break;
edouard@1523
   516
                }
edouard@1523
   517
                case CannotDecrypt:
edouard@1523
   518
                {
edouard@1523
   519
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=CannotDecrypt")
edouard@1523
   520
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupedBeaconed, event=CannotDecrypt", "action=sendUpdateRequest")
edouard@1523
   521
                    status = sendUpdateRequest(session, state, NULL, NULL);
edouard@1523
   522
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   523
                        return (int) invalid_out_of_memory;
edouard@1523
   524
                    if (status != PEP_STATUS_OK)
edouard@1523
   525
                        return (int) invalid_action;
edouard@1586
   526
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupedBeaconed, event=CannotDecrypt", "action=sendBeacon")
edouard@1586
   527
                    status = sendBeacon(session, state, NULL, NULL);
edouard@1586
   528
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1586
   529
                        return (int) invalid_out_of_memory;
edouard@1586
   530
                    if (status != PEP_STATUS_OK)
edouard@1586
   531
                        return (int) invalid_action;
edouard@1523
   532
                    break;
edouard@1523
   533
                }
edouard@1523
   534
                case UpdateRequest:
edouard@1523
   535
                {
edouard@1523
   536
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=UpdateRequest")
edouard@1523
   537
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupedBeaconed, event=UpdateRequest", "action=sendGroupUpdate")
edouard@1523
   538
                    status = sendGroupUpdate(session, state, NULL, NULL);
edouard@1523
   539
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   540
                        return (int) invalid_out_of_memory;
edouard@1523
   541
                    if (status != PEP_STATUS_OK)
edouard@1523
   542
                        return (int) invalid_action;
edouard@1523
   543
                    break;
edouard@1523
   544
                }
edouard@1523
   545
                case Beacon:
edouard@1523
   546
                {
edouard@1523
   547
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=Beacon")
edouard@1523
   548
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupedBeaconed, event=Beacon", "action=sendHandshakeRequest")
edouard@1523
   549
                    status = sendHandshakeRequest(session, state, partner, NULL);
edouard@1523
   550
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   551
                        return (int) invalid_out_of_memory;
edouard@1523
   552
                    if (status != PEP_STATUS_OK)
edouard@1523
   553
                        return (int) invalid_action;
edouard@1523
   554
                    if(session->sync_state_payload){
edouard@1523
   555
                        free_identity((Identity)session->sync_state_payload);
edouard@1523
   556
                        session->sync_state_payload = NULL;
edouard@1523
   557
                    }
edouard@1523
   558
                    session->sync_state_payload = identity_dup(partner);
edouard@1523
   559
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=GroupedBeaconed, event=Beacon", "target=GroupedBeaconed")
edouard@1523
   560
                    return GroupedBeaconed;
edouard@1523
   561
                }
edouard@1523
   562
                case HandshakeRequest:
edouard@1523
   563
                {
edouard@1523
   564
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=HandshakeRequest")
edouard@1523
   565
                    cond_result = sameIdentities(session, partner, expected);
edouard@1523
   566
                    #ifndef NDEBUG
edouard@1523
   567
                    char resstr[11] = {0,};
edouard@1523
   568
                    snprintf(resstr,10,"result=%d",cond_result);
edouard@1523
   569
                    #endif
edouard@1523
   570
                    DEBUG_LOG("FSM condition", "sync_fsm.c, state=GroupedBeaconed, event=HandshakeRequest, condition=sameIdentities", resstr)
edouard@1523
   571
                    if (cond_result < 0)
edouard@1523
   572
                        return cond_result;
edouard@1523
   573
                    if (cond_result) {
edouard@1523
   574
                    }
edouard@1523
   575
                    else {
edouard@1523
   576
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupedBeaconed, event=HandshakeRequest", "action=sendHandshakeRequest")
edouard@1523
   577
                        status = sendHandshakeRequest(session, state, partner, NULL);
edouard@1523
   578
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   579
                            return (int) invalid_out_of_memory;
edouard@1523
   580
                        if (status != PEP_STATUS_OK)
edouard@1523
   581
                            return (int) invalid_action;
edouard@1523
   582
                    }
edouard@1523
   583
                    if(session->sync_state_payload){
edouard@1523
   584
                        free_identity((Identity)session->sync_state_payload);
edouard@1523
   585
                        session->sync_state_payload = NULL;
edouard@1523
   586
                    }
edouard@1523
   587
                    session->sync_state_payload = identity_dup(partner);
edouard@1523
   588
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=GroupedBeaconed, event=HandshakeRequest", "target=HandshakingGrouped")
edouard@1523
   589
                    return HandshakingGrouped;
edouard@1523
   590
                }
edouard@1523
   591
                case GroupUpdate:
edouard@1523
   592
                {
edouard@1523
   593
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=GroupUpdate")
edouard@1523
   594
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupedBeaconed, event=GroupUpdate", "action=storeGroupKeys")
edouard@1523
   595
                    status = storeGroupKeys(session, state, partner, extra /*keys*/);
edouard@1523
   596
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   597
                        return (int) invalid_out_of_memory;
edouard@1523
   598
                    if (status != PEP_STATUS_OK)
edouard@1523
   599
                        return (int) invalid_action;
edouard@1523
   600
                    break;
edouard@1523
   601
                }
edouard@1523
   602
                case Timeout:
edouard@1523
   603
                {
edouard@1523
   604
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=Timeout")
edouard@1523
   605
                    if(session->sync_state_payload){
edouard@1523
   606
                        free_identity((Identity)session->sync_state_payload);
edouard@1523
   607
                        session->sync_state_payload = NULL;
edouard@1523
   608
                    }
edouard@1523
   609
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=GroupedBeaconed, event=Timeout", "target=Grouped")
edouard@1523
   610
                    return Grouped;
edouard@1523
   611
                }
edouard@1523
   612
                default:
edouard@1523
   613
                    return (DeviceState_state) invalid_event;
edouard@1523
   614
            }
edouard@1523
   615
            break;
edouard@1523
   616
        }
edouard@1467
   617
        case HandshakingGrouped:
edouard@1467
   618
        {
edouard@1467
   619
            Identity expected = (Identity)session->sync_state_payload;
edouard@1467
   620
            *timeout = 600;
edouard@1490
   621
            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=HandshakingGrouped")
edouard@1467
   622
            switch (event) {
edouard@1467
   623
                case Init:
edouard@1467
   624
                {
edouard@1490
   625
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingGrouped", "event=Init")
edouard@1586
   626
                    cond_result = keyElectionWon(session, partner);
edouard@1586
   627
                    #ifndef NDEBUG
edouard@1586
   628
                    char resstr[11] = {0,};
edouard@1586
   629
                    snprintf(resstr,10,"result=%d",cond_result);
edouard@1586
   630
                    #endif
edouard@1586
   631
                    DEBUG_LOG("FSM condition", "sync_fsm.c, state=HandshakingGrouped, event=Init, condition=keyElectionWon", resstr)
edouard@1586
   632
                    if (cond_result < 0)
edouard@1586
   633
                        return cond_result;
edouard@1586
   634
                    if (cond_result) {
edouard@1586
   635
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=Init", "action=notifyInitAddOtherDevice")
edouard@1586
   636
                        status = notifyInitAddOtherDevice(session, state, partner, NULL);
edouard@1586
   637
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1586
   638
                            return (int) invalid_out_of_memory;
edouard@1586
   639
                        if (status != PEP_STATUS_OK)
edouard@1586
   640
                            return (int) invalid_action;
edouard@1586
   641
                    }
edouard@1586
   642
                    else {
edouard@1586
   643
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=Init", "action=notifyInitMoveOurDevice")
edouard@1586
   644
                        status = notifyInitMoveOurDevice(session, state, partner, NULL);
edouard@1586
   645
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1586
   646
                            return (int) invalid_out_of_memory;
edouard@1586
   647
                        if (status != PEP_STATUS_OK)
edouard@1586
   648
                            return (int) invalid_action;
edouard@1586
   649
                    }
edouard@1467
   650
                    break;
edouard@1467
   651
                }
edouard@1467
   652
                case HandshakeRejected:
edouard@1467
   653
                {
edouard@1490
   654
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingGrouped", "event=HandshakeRejected")
edouard@1490
   655
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeRejected", "action=rejectHandshake")
edouard@1467
   656
                    status = rejectHandshake(session, state, partner, NULL);
edouard@1467
   657
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   658
                        return (int) invalid_out_of_memory;
edouard@1467
   659
                    if (status != PEP_STATUS_OK)
edouard@1467
   660
                        return (int) invalid_action;
edouard@1467
   661
                    if(session->sync_state_payload){
edouard@1467
   662
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   663
                        session->sync_state_payload = NULL;
edouard@1467
   664
                    }
edouard@1490
   665
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeRejected", "target=Grouped")
edouard@1467
   666
                    return Grouped;
edouard@1467
   667
                }
edouard@1467
   668
                case HandshakeAccepted:
edouard@1467
   669
                {
edouard@1490
   670
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingGrouped", "event=HandshakeAccepted")
edouard@1490
   671
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeAccepted", "action=acceptHandshake")
edouard@1467
   672
                    status = acceptHandshake(session, state, partner, NULL);
edouard@1467
   673
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   674
                        return (int) invalid_out_of_memory;
edouard@1467
   675
                    if (status != PEP_STATUS_OK)
edouard@1467
   676
                        return (int) invalid_action;
edouard@1586
   677
                    cond_result = keyElectionWon(session, partner);
edouard@1586
   678
                    #ifndef NDEBUG
edouard@1586
   679
                    char resstr[11] = {0,};
edouard@1586
   680
                    snprintf(resstr,10,"result=%d",cond_result);
edouard@1586
   681
                    #endif
edouard@1586
   682
                    DEBUG_LOG("FSM condition", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeAccepted, condition=keyElectionWon", resstr)
edouard@1586
   683
                    if (cond_result < 0)
edouard@1586
   684
                        return cond_result;
edouard@1586
   685
                    if (cond_result) {
edouard@1586
   686
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeAccepted", "action=sendGroupKeys")
edouard@1586
   687
                        status = sendGroupKeys(session, state, partner, NULL);
edouard@1586
   688
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1586
   689
                            return (int) invalid_out_of_memory;
edouard@1586
   690
                        if (status != PEP_STATUS_OK)
edouard@1586
   691
                            return (int) invalid_action;
edouard@1586
   692
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeAccepted", "action=notifyAcceptedDeviceAdded")
edouard@1586
   693
                        status = notifyAcceptedDeviceAdded(session, state, partner, NULL);
edouard@1586
   694
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1586
   695
                            return (int) invalid_out_of_memory;
edouard@1586
   696
                        if (status != PEP_STATUS_OK)
edouard@1586
   697
                            return (int) invalid_action;
edouard@1586
   698
                        if(session->sync_state_payload){
edouard@1586
   699
                            free_identity((Identity)session->sync_state_payload);
edouard@1586
   700
                            session->sync_state_payload = NULL;
edouard@1586
   701
                        }
edouard@1586
   702
                        DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeAccepted", "target=Grouped")
edouard@1586
   703
                        return Grouped;
edouard@1586
   704
                    }
edouard@1467
   705
                    if(session->sync_state_payload){
edouard@1467
   706
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   707
                        session->sync_state_payload = NULL;
edouard@1467
   708
                    }
edouard@1586
   709
                    session->sync_state_payload = identity_dup(partner);
edouard@1586
   710
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeAccepted", "target=WaitForGroupKeysGrouped")
edouard@1586
   711
                    return WaitForGroupKeysGrouped;
edouard@1467
   712
                }
edouard@1574
   713
                case Cancel:
edouard@1574
   714
                {
edouard@1574
   715
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingGrouped", "event=Cancel")
edouard@1574
   716
                    if(session->sync_state_payload){
edouard@1574
   717
                        free_identity((Identity)session->sync_state_payload);
edouard@1574
   718
                        session->sync_state_payload = NULL;
edouard@1574
   719
                    }
edouard@1574
   720
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingGrouped, event=Cancel", "target=Grouped")
edouard@1574
   721
                    return Grouped;
edouard@1574
   722
                }
edouard@1467
   723
                case Timeout:
edouard@1467
   724
                {
edouard@1490
   725
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingGrouped", "event=Timeout")
edouard@1490
   726
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=Timeout", "action=notifyTimeout")
edouard@1477
   727
                    status = notifyTimeout(session, state, expected, NULL);
edouard@1467
   728
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   729
                        return (int) invalid_out_of_memory;
edouard@1467
   730
                    if (status != PEP_STATUS_OK)
edouard@1467
   731
                        return (int) invalid_action;
edouard@1467
   732
                    if(session->sync_state_payload){
edouard@1467
   733
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   734
                        session->sync_state_payload = NULL;
edouard@1467
   735
                    }
edouard@1490
   736
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingGrouped, event=Timeout", "target=Grouped")
edouard@1467
   737
                    return Grouped;
edouard@1467
   738
                }
edouard@1467
   739
                default:
edouard@1467
   740
                    return (DeviceState_state) invalid_event;
edouard@1467
   741
            }
edouard@1467
   742
            break;
edouard@1467
   743
        }
edouard@1586
   744
        case WaitForGroupKeysGrouped:
edouard@1586
   745
        {
edouard@1586
   746
            Identity expected = (Identity)session->sync_state_payload;
edouard@1586
   747
            *timeout = 600;
edouard@1586
   748
            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=WaitForGroupKeysGrouped")
edouard@1586
   749
            switch (event) {
edouard@1586
   750
                case Init: DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForGroupKeysGrouped", "event=Init") break;
edouard@1586
   751
                case GroupKeys:
edouard@1586
   752
                {
edouard@1586
   753
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForGroupKeysGrouped", "event=GroupKeys")
edouard@1586
   754
                    cond_result = sameIdentities(session, partner, expected);
edouard@1586
   755
                    #ifndef NDEBUG
edouard@1586
   756
                    char resstr[11] = {0,};
edouard@1586
   757
                    snprintf(resstr,10,"result=%d",cond_result);
edouard@1586
   758
                    #endif
edouard@1586
   759
                    DEBUG_LOG("FSM condition", "sync_fsm.c, state=WaitForGroupKeysGrouped, event=GroupKeys, condition=sameIdentities", resstr)
edouard@1586
   760
                    if (cond_result < 0)
edouard@1586
   761
                        return cond_result;
edouard@1586
   762
                    if (cond_result) {
edouard@1586
   763
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysGrouped, event=GroupKeys", "action=storeGroupKeys")
edouard@1586
   764
                        status = storeGroupKeys(session, state, partner, extra /*keys*/);
edouard@1586
   765
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1586
   766
                            return (int) invalid_out_of_memory;
edouard@1586
   767
                        if (status != PEP_STATUS_OK)
edouard@1586
   768
                            return (int) invalid_action;
edouard@1586
   769
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysGrouped, event=GroupKeys", "action=sendGroupUpdate")
edouard@1586
   770
                        status = sendGroupUpdate(session, state, NULL, NULL);
edouard@1586
   771
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1586
   772
                            return (int) invalid_out_of_memory;
edouard@1586
   773
                        if (status != PEP_STATUS_OK)
edouard@1586
   774
                            return (int) invalid_action;
edouard@1586
   775
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysGrouped, event=GroupKeys", "action=notifyAcceptedDeviceMoved")
edouard@1586
   776
                        status = notifyAcceptedDeviceMoved(session, state, partner, NULL);
edouard@1586
   777
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1586
   778
                            return (int) invalid_out_of_memory;
edouard@1586
   779
                        if (status != PEP_STATUS_OK)
edouard@1586
   780
                            return (int) invalid_action;
edouard@1586
   781
                        if(session->sync_state_payload){
edouard@1586
   782
                            free_identity((Identity)session->sync_state_payload);
edouard@1586
   783
                            session->sync_state_payload = NULL;
edouard@1586
   784
                        }
edouard@1586
   785
                        DEBUG_LOG("FSM transition", "sync_fsm.c, state=WaitForGroupKeysGrouped, event=GroupKeys", "target=Grouped")
edouard@1586
   786
                        return Grouped;
edouard@1586
   787
                    }
edouard@1586
   788
                    break;
edouard@1586
   789
                }
edouard@1586
   790
                case Timeout:
edouard@1586
   791
                {
edouard@1586
   792
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForGroupKeysGrouped", "event=Timeout")
edouard@1586
   793
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysGrouped, event=Timeout", "action=notifyTimeout")
edouard@1586
   794
                    status = notifyTimeout(session, state, expected, NULL);
edouard@1586
   795
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1586
   796
                        return (int) invalid_out_of_memory;
edouard@1586
   797
                    if (status != PEP_STATUS_OK)
edouard@1586
   798
                        return (int) invalid_action;
edouard@1586
   799
                    if(session->sync_state_payload){
edouard@1586
   800
                        free_identity((Identity)session->sync_state_payload);
edouard@1586
   801
                        session->sync_state_payload = NULL;
edouard@1586
   802
                    }
edouard@1586
   803
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=WaitForGroupKeysGrouped, event=Timeout", "target=Grouped")
edouard@1586
   804
                    return Grouped;
edouard@1586
   805
                }
edouard@1586
   806
                default:
edouard@1586
   807
                    return (DeviceState_state) invalid_event;
edouard@1586
   808
            }
edouard@1586
   809
            break;
edouard@1586
   810
        }
edouard@1467
   811
        default:
edouard@1467
   812
            return (DeviceState_state) invalid_state;
edouard@1467
   813
    }
edouard@1467
   814
edouard@1467
   815
    return state;
edouard@1467
   816
}
edouard@1467
   817