sync/generated/sync_fsm.c
author Edouard Tisserant <edouard@pep-project.org>
Sat, 18 Feb 2017 23:08:34 +0100
branchGroupMerge
changeset 1588 1a43a7eddf90
parent 1586 599deda50386
child 1590 6e007351ccde
permissions -rw-r--r--
KeySync fsm : fixed storeGroupKeys instead of storeGroupUpdate, added more check, fixed wrong parameter type for storeGroupKeys
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@1588
   260
                    cond_result = sameIdentities(session, partner, expected);
edouard@1490
   261
                    #ifndef NDEBUG
edouard@1490
   262
                    char resstr[11] = {0,};
edouard@1490
   263
                    snprintf(resstr,10,"result=%d",cond_result);
edouard@1490
   264
                    #endif
edouard@1588
   265
                    DEBUG_LOG("FSM condition", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted, condition=sameIdentities", resstr)
edouard@1467
   266
                    if (cond_result < 0)
edouard@1467
   267
                        return cond_result;
edouard@1467
   268
                    if (cond_result) {
edouard@1588
   269
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "action=acceptHandshake")
edouard@1588
   270
                        status = acceptHandshake(session, state, partner, NULL);
edouard@1566
   271
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1566
   272
                            return (int) invalid_out_of_memory;
edouard@1566
   273
                        if (status != PEP_STATUS_OK)
edouard@1566
   274
                            return (int) invalid_action;
edouard@1588
   275
                        cond_result = keyElectionWon(session, partner);
edouard@1588
   276
                        #ifndef NDEBUG
edouard@1588
   277
                        char resstr[11] = {0,};
edouard@1588
   278
                        snprintf(resstr,10,"result=%d",cond_result);
edouard@1588
   279
                        #endif
edouard@1588
   280
                        DEBUG_LOG("FSM condition", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted, condition=keyElectionWon", resstr)
edouard@1588
   281
                        if (cond_result < 0)
edouard@1588
   282
                            return cond_result;
edouard@1588
   283
                        if (cond_result) {
edouard@1588
   284
                            DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "action=makeGroup")
edouard@1588
   285
                            status = makeGroup(session, state, NULL, NULL);
edouard@1588
   286
                            if (status == PEP_OUT_OF_MEMORY)
edouard@1588
   287
                                return (int) invalid_out_of_memory;
edouard@1588
   288
                            if (status != PEP_STATUS_OK)
edouard@1588
   289
                                return (int) invalid_action;
edouard@1588
   290
                            DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "action=sendGroupKeys")
edouard@1588
   291
                            status = sendGroupKeys(session, state, partner, NULL);
edouard@1588
   292
                            if (status == PEP_OUT_OF_MEMORY)
edouard@1588
   293
                                return (int) invalid_out_of_memory;
edouard@1588
   294
                            if (status != PEP_STATUS_OK)
edouard@1588
   295
                                return (int) invalid_action;
edouard@1588
   296
                            DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "action=notifyAcceptedGroupCreated")
edouard@1588
   297
                            status = notifyAcceptedGroupCreated(session, state, partner, NULL);
edouard@1588
   298
                            if (status == PEP_OUT_OF_MEMORY)
edouard@1588
   299
                                return (int) invalid_out_of_memory;
edouard@1588
   300
                            if (status != PEP_STATUS_OK)
edouard@1588
   301
                                return (int) invalid_action;
edouard@1588
   302
                            if(session->sync_state_payload){
edouard@1588
   303
                                free_identity((Identity)session->sync_state_payload);
edouard@1588
   304
                                session->sync_state_payload = NULL;
edouard@1588
   305
                            }
edouard@1588
   306
                            DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "target=Grouped")
edouard@1588
   307
                            return Grouped;
edouard@1588
   308
                        }
edouard@1467
   309
                        if(session->sync_state_payload){
edouard@1467
   310
                            free_identity((Identity)session->sync_state_payload);
edouard@1467
   311
                            session->sync_state_payload = NULL;
edouard@1467
   312
                        }
edouard@1588
   313
                        session->sync_state_payload = identity_dup(partner);
edouard@1588
   314
                        DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "target=WaitForGroupKeysSole")
edouard@1588
   315
                        return WaitForGroupKeysSole;
edouard@1467
   316
                    }
edouard@1467
   317
                    if(session->sync_state_payload){
edouard@1467
   318
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   319
                        session->sync_state_payload = NULL;
edouard@1467
   320
                    }
edouard@1588
   321
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "target=Sole")
edouard@1588
   322
                    return Sole;
edouard@1467
   323
                }
edouard@1467
   324
                case Cancel:
edouard@1467
   325
                {
edouard@1490
   326
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingSole", "event=Cancel")
edouard@1467
   327
                    if(session->sync_state_payload){
edouard@1467
   328
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   329
                        session->sync_state_payload = NULL;
edouard@1467
   330
                    }
edouard@1490
   331
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingSole, event=Cancel", "target=Sole")
edouard@1467
   332
                    return Sole;
edouard@1467
   333
                }
edouard@1467
   334
                case Timeout:
edouard@1467
   335
                {
edouard@1490
   336
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingSole", "event=Timeout")
edouard@1490
   337
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=Timeout", "action=notifyTimeout")
edouard@1477
   338
                    status = notifyTimeout(session, state, expected, NULL);
edouard@1467
   339
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   340
                        return (int) invalid_out_of_memory;
edouard@1467
   341
                    if (status != PEP_STATUS_OK)
edouard@1467
   342
                        return (int) invalid_action;
edouard@1555
   343
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=Timeout", "action=sendBeacon")
edouard@1555
   344
                    status = sendBeacon(session, state, NULL, NULL);
edouard@1555
   345
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1555
   346
                        return (int) invalid_out_of_memory;
edouard@1555
   347
                    if (status != PEP_STATUS_OK)
edouard@1555
   348
                        return (int) invalid_action;
edouard@1467
   349
                    if(session->sync_state_payload){
edouard@1467
   350
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   351
                        session->sync_state_payload = NULL;
edouard@1467
   352
                    }
edouard@1490
   353
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingSole, event=Timeout", "target=Sole")
edouard@1467
   354
                    return Sole;
edouard@1467
   355
                }
edouard@1467
   356
                default:
edouard@1467
   357
                    return (DeviceState_state) invalid_event;
edouard@1467
   358
            }
edouard@1467
   359
            break;
edouard@1467
   360
        }
edouard@1467
   361
        case WaitForGroupKeysSole:
edouard@1467
   362
        {
edouard@1467
   363
            Identity expected = (Identity)session->sync_state_payload;
edouard@1467
   364
            *timeout = 600;
edouard@1490
   365
            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=WaitForGroupKeysSole")
edouard@1467
   366
            switch (event) {
edouard@1490
   367
                case Init: DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForGroupKeysSole", "event=Init") break;
edouard@1467
   368
                case GroupKeys:
edouard@1467
   369
                {
edouard@1490
   370
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForGroupKeysSole", "event=GroupKeys")
edouard@1523
   371
                    cond_result = sameIdentities(session, partner, expected);
edouard@1523
   372
                    #ifndef NDEBUG
edouard@1523
   373
                    char resstr[11] = {0,};
edouard@1523
   374
                    snprintf(resstr,10,"result=%d",cond_result);
edouard@1523
   375
                    #endif
edouard@1523
   376
                    DEBUG_LOG("FSM condition", "sync_fsm.c, state=WaitForGroupKeysSole, event=GroupKeys, condition=sameIdentities", resstr)
edouard@1523
   377
                    if (cond_result < 0)
edouard@1523
   378
                        return cond_result;
edouard@1523
   379
                    if (cond_result) {
edouard@1523
   380
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysSole, event=GroupKeys", "action=storeGroupKeys")
edouard@1523
   381
                        status = storeGroupKeys(session, state, partner, extra /*keys*/);
edouard@1523
   382
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   383
                            return (int) invalid_out_of_memory;
edouard@1523
   384
                        if (status != PEP_STATUS_OK)
edouard@1523
   385
                            return (int) invalid_action;
edouard@1566
   386
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysSole, event=GroupKeys", "action=sendGroupUpdate")
edouard@1566
   387
                        status = sendGroupUpdate(session, state, NULL, NULL);
edouard@1566
   388
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1566
   389
                            return (int) invalid_out_of_memory;
edouard@1566
   390
                        if (status != PEP_STATUS_OK)
edouard@1566
   391
                            return (int) invalid_action;
edouard@1523
   392
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysSole, event=GroupKeys", "action=notifyAcceptedDeviceAdded")
edouard@1523
   393
                        status = notifyAcceptedDeviceAdded(session, state, partner, NULL);
edouard@1523
   394
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   395
                            return (int) invalid_out_of_memory;
edouard@1523
   396
                        if (status != PEP_STATUS_OK)
edouard@1523
   397
                            return (int) invalid_action;
edouard@1523
   398
                        if(session->sync_state_payload){
edouard@1523
   399
                            free_identity((Identity)session->sync_state_payload);
edouard@1523
   400
                            session->sync_state_payload = NULL;
edouard@1523
   401
                        }
edouard@1523
   402
                        DEBUG_LOG("FSM transition", "sync_fsm.c, state=WaitForGroupKeysSole, event=GroupKeys", "target=Grouped")
edouard@1523
   403
                        return Grouped;
edouard@1467
   404
                    }
edouard@1523
   405
                    break;
edouard@1467
   406
                }
edouard@1467
   407
                case Timeout:
edouard@1467
   408
                {
edouard@1490
   409
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForGroupKeysSole", "event=Timeout")
edouard@1490
   410
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysSole, event=Timeout", "action=notifyTimeout")
edouard@1477
   411
                    status = notifyTimeout(session, state, expected, NULL);
edouard@1467
   412
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   413
                        return (int) invalid_out_of_memory;
edouard@1467
   414
                    if (status != PEP_STATUS_OK)
edouard@1467
   415
                        return (int) invalid_action;
edouard@1467
   416
                    if(session->sync_state_payload){
edouard@1467
   417
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   418
                        session->sync_state_payload = NULL;
edouard@1467
   419
                    }
edouard@1490
   420
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=WaitForGroupKeysSole, event=Timeout", "target=Sole")
edouard@1467
   421
                    return Sole;
edouard@1467
   422
                }
edouard@1467
   423
                default:
edouard@1467
   424
                    return (DeviceState_state) invalid_event;
edouard@1467
   425
            }
edouard@1467
   426
            break;
edouard@1467
   427
        }
edouard@1467
   428
        case Grouped:
edouard@1467
   429
        {
edouard@1467
   430
            *timeout = 0;
edouard@1490
   431
            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=Grouped")
edouard@1467
   432
            switch (event) {
edouard@1566
   433
                case Init: DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=Init") break;
edouard@1467
   434
                case KeyGen:
edouard@1467
   435
                {
edouard@1490
   436
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=KeyGen")
edouard@1490
   437
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=KeyGen", "action=sendGroupUpdate")
edouard@1467
   438
                    status = sendGroupUpdate(session, state, NULL, NULL);
edouard@1467
   439
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   440
                        return (int) invalid_out_of_memory;
edouard@1467
   441
                    if (status != PEP_STATUS_OK)
edouard@1467
   442
                        return (int) invalid_action;
edouard@1467
   443
                    break;
edouard@1467
   444
                }
edouard@1467
   445
                case CannotDecrypt:
edouard@1467
   446
                {
edouard@1490
   447
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=CannotDecrypt")
edouard@1490
   448
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=CannotDecrypt", "action=sendUpdateRequest")
edouard@1467
   449
                    status = sendUpdateRequest(session, state, NULL, NULL);
edouard@1467
   450
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   451
                        return (int) invalid_out_of_memory;
edouard@1467
   452
                    if (status != PEP_STATUS_OK)
edouard@1467
   453
                        return (int) invalid_action;
edouard@1586
   454
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=CannotDecrypt", "action=sendBeacon")
edouard@1586
   455
                    status = sendBeacon(session, state, NULL, NULL);
edouard@1586
   456
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1586
   457
                        return (int) invalid_out_of_memory;
edouard@1586
   458
                    if (status != PEP_STATUS_OK)
edouard@1586
   459
                        return (int) invalid_action;
edouard@1467
   460
                    break;
edouard@1467
   461
                }
edouard@1467
   462
                case UpdateRequest:
edouard@1467
   463
                {
edouard@1490
   464
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=UpdateRequest")
edouard@1490
   465
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=UpdateRequest", "action=sendGroupUpdate")
edouard@1467
   466
                    status = sendGroupUpdate(session, state, NULL, NULL);
edouard@1467
   467
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   468
                        return (int) invalid_out_of_memory;
edouard@1467
   469
                    if (status != PEP_STATUS_OK)
edouard@1467
   470
                        return (int) invalid_action;
edouard@1467
   471
                    break;
edouard@1467
   472
                }
edouard@1467
   473
                case Beacon:
edouard@1467
   474
                {
edouard@1490
   475
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=Beacon")
edouard@1490
   476
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=Beacon", "action=sendHandshakeRequest")
edouard@1467
   477
                    status = sendHandshakeRequest(session, state, partner, NULL);
edouard@1467
   478
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   479
                        return (int) invalid_out_of_memory;
edouard@1467
   480
                    if (status != PEP_STATUS_OK)
edouard@1467
   481
                        return (int) invalid_action;
edouard@1523
   482
                    session->sync_state_payload = identity_dup(partner);
edouard@1523
   483
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=Grouped, event=Beacon", "target=GroupedBeaconed")
edouard@1523
   484
                    return GroupedBeaconed;
edouard@1467
   485
                }
edouard@1467
   486
                case HandshakeRequest:
edouard@1467
   487
                {
edouard@1490
   488
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=HandshakeRequest")
edouard@1490
   489
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=HandshakeRequest", "action=sendHandshakeRequest")
edouard@1467
   490
                    status = sendHandshakeRequest(session, state, partner, NULL);
edouard@1467
   491
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   492
                        return (int) invalid_out_of_memory;
edouard@1467
   493
                    if (status != PEP_STATUS_OK)
edouard@1467
   494
                        return (int) invalid_action;
edouard@1467
   495
                    session->sync_state_payload = identity_dup(partner);
edouard@1490
   496
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=Grouped, event=HandshakeRequest", "target=HandshakingGrouped")
edouard@1467
   497
                    return HandshakingGrouped;
edouard@1467
   498
                }
edouard@1467
   499
                case GroupUpdate:
edouard@1467
   500
                {
edouard@1490
   501
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=GroupUpdate")
edouard@1586
   502
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=GroupUpdate", "action=storeGroupUpdate")
edouard@1586
   503
                    status = storeGroupUpdate(session, state, partner, extra /*keys*/);
edouard@1467
   504
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   505
                        return (int) invalid_out_of_memory;
edouard@1467
   506
                    if (status != PEP_STATUS_OK)
edouard@1467
   507
                        return (int) invalid_action;
edouard@1467
   508
                    break;
edouard@1467
   509
                }
edouard@1467
   510
                default:
edouard@1467
   511
                    return (DeviceState_state) invalid_event;
edouard@1467
   512
            }
edouard@1467
   513
            break;
edouard@1467
   514
        }
edouard@1523
   515
        case GroupedBeaconed:
edouard@1523
   516
        {
edouard@1523
   517
            Identity expected = (Identity)session->sync_state_payload;
edouard@1523
   518
            *timeout = 600;
edouard@1523
   519
            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=GroupedBeaconed")
edouard@1523
   520
            switch (event) {
edouard@1523
   521
                case Init: DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=Init") break;
edouard@1523
   522
                case KeyGen:
edouard@1523
   523
                {
edouard@1523
   524
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=KeyGen")
edouard@1523
   525
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupedBeaconed, event=KeyGen", "action=sendGroupUpdate")
edouard@1523
   526
                    status = sendGroupUpdate(session, state, NULL, NULL);
edouard@1523
   527
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   528
                        return (int) invalid_out_of_memory;
edouard@1523
   529
                    if (status != PEP_STATUS_OK)
edouard@1523
   530
                        return (int) invalid_action;
edouard@1523
   531
                    break;
edouard@1523
   532
                }
edouard@1523
   533
                case CannotDecrypt:
edouard@1523
   534
                {
edouard@1523
   535
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=CannotDecrypt")
edouard@1523
   536
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupedBeaconed, event=CannotDecrypt", "action=sendUpdateRequest")
edouard@1523
   537
                    status = sendUpdateRequest(session, state, NULL, NULL);
edouard@1523
   538
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   539
                        return (int) invalid_out_of_memory;
edouard@1523
   540
                    if (status != PEP_STATUS_OK)
edouard@1523
   541
                        return (int) invalid_action;
edouard@1586
   542
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupedBeaconed, event=CannotDecrypt", "action=sendBeacon")
edouard@1586
   543
                    status = sendBeacon(session, state, NULL, NULL);
edouard@1586
   544
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1586
   545
                        return (int) invalid_out_of_memory;
edouard@1586
   546
                    if (status != PEP_STATUS_OK)
edouard@1586
   547
                        return (int) invalid_action;
edouard@1523
   548
                    break;
edouard@1523
   549
                }
edouard@1523
   550
                case UpdateRequest:
edouard@1523
   551
                {
edouard@1523
   552
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=UpdateRequest")
edouard@1523
   553
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupedBeaconed, event=UpdateRequest", "action=sendGroupUpdate")
edouard@1523
   554
                    status = sendGroupUpdate(session, state, NULL, NULL);
edouard@1523
   555
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   556
                        return (int) invalid_out_of_memory;
edouard@1523
   557
                    if (status != PEP_STATUS_OK)
edouard@1523
   558
                        return (int) invalid_action;
edouard@1523
   559
                    break;
edouard@1523
   560
                }
edouard@1523
   561
                case Beacon:
edouard@1523
   562
                {
edouard@1523
   563
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=Beacon")
edouard@1523
   564
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupedBeaconed, event=Beacon", "action=sendHandshakeRequest")
edouard@1523
   565
                    status = sendHandshakeRequest(session, state, partner, NULL);
edouard@1523
   566
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   567
                        return (int) invalid_out_of_memory;
edouard@1523
   568
                    if (status != PEP_STATUS_OK)
edouard@1523
   569
                        return (int) invalid_action;
edouard@1523
   570
                    if(session->sync_state_payload){
edouard@1523
   571
                        free_identity((Identity)session->sync_state_payload);
edouard@1523
   572
                        session->sync_state_payload = NULL;
edouard@1523
   573
                    }
edouard@1523
   574
                    session->sync_state_payload = identity_dup(partner);
edouard@1523
   575
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=GroupedBeaconed, event=Beacon", "target=GroupedBeaconed")
edouard@1523
   576
                    return GroupedBeaconed;
edouard@1523
   577
                }
edouard@1523
   578
                case HandshakeRequest:
edouard@1523
   579
                {
edouard@1523
   580
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=HandshakeRequest")
edouard@1523
   581
                    cond_result = sameIdentities(session, partner, expected);
edouard@1523
   582
                    #ifndef NDEBUG
edouard@1523
   583
                    char resstr[11] = {0,};
edouard@1523
   584
                    snprintf(resstr,10,"result=%d",cond_result);
edouard@1523
   585
                    #endif
edouard@1523
   586
                    DEBUG_LOG("FSM condition", "sync_fsm.c, state=GroupedBeaconed, event=HandshakeRequest, condition=sameIdentities", resstr)
edouard@1523
   587
                    if (cond_result < 0)
edouard@1523
   588
                        return cond_result;
edouard@1523
   589
                    if (cond_result) {
edouard@1523
   590
                    }
edouard@1523
   591
                    else {
edouard@1523
   592
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupedBeaconed, event=HandshakeRequest", "action=sendHandshakeRequest")
edouard@1523
   593
                        status = sendHandshakeRequest(session, state, partner, NULL);
edouard@1523
   594
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   595
                            return (int) invalid_out_of_memory;
edouard@1523
   596
                        if (status != PEP_STATUS_OK)
edouard@1523
   597
                            return (int) invalid_action;
edouard@1523
   598
                    }
edouard@1523
   599
                    if(session->sync_state_payload){
edouard@1523
   600
                        free_identity((Identity)session->sync_state_payload);
edouard@1523
   601
                        session->sync_state_payload = NULL;
edouard@1523
   602
                    }
edouard@1523
   603
                    session->sync_state_payload = identity_dup(partner);
edouard@1523
   604
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=GroupedBeaconed, event=HandshakeRequest", "target=HandshakingGrouped")
edouard@1523
   605
                    return HandshakingGrouped;
edouard@1523
   606
                }
edouard@1523
   607
                case GroupUpdate:
edouard@1523
   608
                {
edouard@1523
   609
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=GroupUpdate")
edouard@1588
   610
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupedBeaconed, event=GroupUpdate", "action=storeGroupUpdate")
edouard@1588
   611
                    status = storeGroupUpdate(session, state, partner, extra /*keys*/);
edouard@1523
   612
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   613
                        return (int) invalid_out_of_memory;
edouard@1523
   614
                    if (status != PEP_STATUS_OK)
edouard@1523
   615
                        return (int) invalid_action;
edouard@1523
   616
                    break;
edouard@1523
   617
                }
edouard@1523
   618
                case Timeout:
edouard@1523
   619
                {
edouard@1523
   620
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=Timeout")
edouard@1523
   621
                    if(session->sync_state_payload){
edouard@1523
   622
                        free_identity((Identity)session->sync_state_payload);
edouard@1523
   623
                        session->sync_state_payload = NULL;
edouard@1523
   624
                    }
edouard@1523
   625
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=GroupedBeaconed, event=Timeout", "target=Grouped")
edouard@1523
   626
                    return Grouped;
edouard@1523
   627
                }
edouard@1523
   628
                default:
edouard@1523
   629
                    return (DeviceState_state) invalid_event;
edouard@1523
   630
            }
edouard@1523
   631
            break;
edouard@1523
   632
        }
edouard@1467
   633
        case HandshakingGrouped:
edouard@1467
   634
        {
edouard@1467
   635
            Identity expected = (Identity)session->sync_state_payload;
edouard@1467
   636
            *timeout = 600;
edouard@1490
   637
            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=HandshakingGrouped")
edouard@1467
   638
            switch (event) {
edouard@1467
   639
                case Init:
edouard@1467
   640
                {
edouard@1490
   641
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingGrouped", "event=Init")
edouard@1586
   642
                    cond_result = keyElectionWon(session, partner);
edouard@1586
   643
                    #ifndef NDEBUG
edouard@1586
   644
                    char resstr[11] = {0,};
edouard@1586
   645
                    snprintf(resstr,10,"result=%d",cond_result);
edouard@1586
   646
                    #endif
edouard@1586
   647
                    DEBUG_LOG("FSM condition", "sync_fsm.c, state=HandshakingGrouped, event=Init, condition=keyElectionWon", resstr)
edouard@1586
   648
                    if (cond_result < 0)
edouard@1586
   649
                        return cond_result;
edouard@1586
   650
                    if (cond_result) {
edouard@1586
   651
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=Init", "action=notifyInitAddOtherDevice")
edouard@1586
   652
                        status = notifyInitAddOtherDevice(session, state, partner, NULL);
edouard@1586
   653
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1586
   654
                            return (int) invalid_out_of_memory;
edouard@1586
   655
                        if (status != PEP_STATUS_OK)
edouard@1586
   656
                            return (int) invalid_action;
edouard@1586
   657
                    }
edouard@1586
   658
                    else {
edouard@1586
   659
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=Init", "action=notifyInitMoveOurDevice")
edouard@1586
   660
                        status = notifyInitMoveOurDevice(session, state, partner, NULL);
edouard@1586
   661
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1586
   662
                            return (int) invalid_out_of_memory;
edouard@1586
   663
                        if (status != PEP_STATUS_OK)
edouard@1586
   664
                            return (int) invalid_action;
edouard@1586
   665
                    }
edouard@1467
   666
                    break;
edouard@1467
   667
                }
edouard@1467
   668
                case HandshakeRejected:
edouard@1467
   669
                {
edouard@1490
   670
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingGrouped", "event=HandshakeRejected")
edouard@1490
   671
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeRejected", "action=rejectHandshake")
edouard@1467
   672
                    status = rejectHandshake(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@1467
   677
                    if(session->sync_state_payload){
edouard@1467
   678
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   679
                        session->sync_state_payload = NULL;
edouard@1467
   680
                    }
edouard@1490
   681
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeRejected", "target=Grouped")
edouard@1467
   682
                    return Grouped;
edouard@1467
   683
                }
edouard@1467
   684
                case HandshakeAccepted:
edouard@1467
   685
                {
edouard@1490
   686
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingGrouped", "event=HandshakeAccepted")
edouard@1490
   687
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeAccepted", "action=acceptHandshake")
edouard@1467
   688
                    status = acceptHandshake(session, state, partner, NULL);
edouard@1467
   689
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   690
                        return (int) invalid_out_of_memory;
edouard@1467
   691
                    if (status != PEP_STATUS_OK)
edouard@1467
   692
                        return (int) invalid_action;
edouard@1586
   693
                    cond_result = keyElectionWon(session, partner);
edouard@1586
   694
                    #ifndef NDEBUG
edouard@1586
   695
                    char resstr[11] = {0,};
edouard@1586
   696
                    snprintf(resstr,10,"result=%d",cond_result);
edouard@1586
   697
                    #endif
edouard@1586
   698
                    DEBUG_LOG("FSM condition", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeAccepted, condition=keyElectionWon", resstr)
edouard@1586
   699
                    if (cond_result < 0)
edouard@1586
   700
                        return cond_result;
edouard@1586
   701
                    if (cond_result) {
edouard@1586
   702
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeAccepted", "action=sendGroupKeys")
edouard@1586
   703
                        status = sendGroupKeys(session, state, partner, NULL);
edouard@1586
   704
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1586
   705
                            return (int) invalid_out_of_memory;
edouard@1586
   706
                        if (status != PEP_STATUS_OK)
edouard@1586
   707
                            return (int) invalid_action;
edouard@1586
   708
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeAccepted", "action=notifyAcceptedDeviceAdded")
edouard@1586
   709
                        status = notifyAcceptedDeviceAdded(session, state, partner, NULL);
edouard@1586
   710
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1586
   711
                            return (int) invalid_out_of_memory;
edouard@1586
   712
                        if (status != PEP_STATUS_OK)
edouard@1586
   713
                            return (int) invalid_action;
edouard@1586
   714
                        if(session->sync_state_payload){
edouard@1586
   715
                            free_identity((Identity)session->sync_state_payload);
edouard@1586
   716
                            session->sync_state_payload = NULL;
edouard@1586
   717
                        }
edouard@1586
   718
                        DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeAccepted", "target=Grouped")
edouard@1586
   719
                        return Grouped;
edouard@1586
   720
                    }
edouard@1467
   721
                    if(session->sync_state_payload){
edouard@1467
   722
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   723
                        session->sync_state_payload = NULL;
edouard@1467
   724
                    }
edouard@1586
   725
                    session->sync_state_payload = identity_dup(partner);
edouard@1586
   726
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeAccepted", "target=WaitForGroupKeysGrouped")
edouard@1586
   727
                    return WaitForGroupKeysGrouped;
edouard@1467
   728
                }
edouard@1574
   729
                case Cancel:
edouard@1574
   730
                {
edouard@1574
   731
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingGrouped", "event=Cancel")
edouard@1574
   732
                    if(session->sync_state_payload){
edouard@1574
   733
                        free_identity((Identity)session->sync_state_payload);
edouard@1574
   734
                        session->sync_state_payload = NULL;
edouard@1574
   735
                    }
edouard@1574
   736
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingGrouped, event=Cancel", "target=Grouped")
edouard@1574
   737
                    return Grouped;
edouard@1574
   738
                }
edouard@1467
   739
                case Timeout:
edouard@1467
   740
                {
edouard@1490
   741
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingGrouped", "event=Timeout")
edouard@1490
   742
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=Timeout", "action=notifyTimeout")
edouard@1477
   743
                    status = notifyTimeout(session, state, expected, NULL);
edouard@1467
   744
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   745
                        return (int) invalid_out_of_memory;
edouard@1467
   746
                    if (status != PEP_STATUS_OK)
edouard@1467
   747
                        return (int) invalid_action;
edouard@1467
   748
                    if(session->sync_state_payload){
edouard@1467
   749
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   750
                        session->sync_state_payload = NULL;
edouard@1467
   751
                    }
edouard@1490
   752
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingGrouped, event=Timeout", "target=Grouped")
edouard@1467
   753
                    return Grouped;
edouard@1467
   754
                }
edouard@1467
   755
                default:
edouard@1467
   756
                    return (DeviceState_state) invalid_event;
edouard@1467
   757
            }
edouard@1467
   758
            break;
edouard@1467
   759
        }
edouard@1586
   760
        case WaitForGroupKeysGrouped:
edouard@1586
   761
        {
edouard@1586
   762
            Identity expected = (Identity)session->sync_state_payload;
edouard@1586
   763
            *timeout = 600;
edouard@1586
   764
            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=WaitForGroupKeysGrouped")
edouard@1586
   765
            switch (event) {
edouard@1586
   766
                case Init: DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForGroupKeysGrouped", "event=Init") break;
edouard@1586
   767
                case GroupKeys:
edouard@1586
   768
                {
edouard@1586
   769
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForGroupKeysGrouped", "event=GroupKeys")
edouard@1586
   770
                    cond_result = sameIdentities(session, partner, expected);
edouard@1586
   771
                    #ifndef NDEBUG
edouard@1586
   772
                    char resstr[11] = {0,};
edouard@1586
   773
                    snprintf(resstr,10,"result=%d",cond_result);
edouard@1586
   774
                    #endif
edouard@1586
   775
                    DEBUG_LOG("FSM condition", "sync_fsm.c, state=WaitForGroupKeysGrouped, event=GroupKeys, condition=sameIdentities", resstr)
edouard@1586
   776
                    if (cond_result < 0)
edouard@1586
   777
                        return cond_result;
edouard@1586
   778
                    if (cond_result) {
edouard@1586
   779
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysGrouped, event=GroupKeys", "action=storeGroupKeys")
edouard@1586
   780
                        status = storeGroupKeys(session, state, partner, extra /*keys*/);
edouard@1586
   781
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1586
   782
                            return (int) invalid_out_of_memory;
edouard@1586
   783
                        if (status != PEP_STATUS_OK)
edouard@1586
   784
                            return (int) invalid_action;
edouard@1586
   785
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysGrouped, event=GroupKeys", "action=sendGroupUpdate")
edouard@1586
   786
                        status = sendGroupUpdate(session, state, NULL, NULL);
edouard@1586
   787
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1586
   788
                            return (int) invalid_out_of_memory;
edouard@1586
   789
                        if (status != PEP_STATUS_OK)
edouard@1586
   790
                            return (int) invalid_action;
edouard@1586
   791
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysGrouped, event=GroupKeys", "action=notifyAcceptedDeviceMoved")
edouard@1586
   792
                        status = notifyAcceptedDeviceMoved(session, state, partner, NULL);
edouard@1586
   793
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1586
   794
                            return (int) invalid_out_of_memory;
edouard@1586
   795
                        if (status != PEP_STATUS_OK)
edouard@1586
   796
                            return (int) invalid_action;
edouard@1586
   797
                        if(session->sync_state_payload){
edouard@1586
   798
                            free_identity((Identity)session->sync_state_payload);
edouard@1586
   799
                            session->sync_state_payload = NULL;
edouard@1586
   800
                        }
edouard@1586
   801
                        DEBUG_LOG("FSM transition", "sync_fsm.c, state=WaitForGroupKeysGrouped, event=GroupKeys", "target=Grouped")
edouard@1586
   802
                        return Grouped;
edouard@1586
   803
                    }
edouard@1586
   804
                    break;
edouard@1586
   805
                }
edouard@1586
   806
                case Timeout:
edouard@1586
   807
                {
edouard@1586
   808
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForGroupKeysGrouped", "event=Timeout")
edouard@1586
   809
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysGrouped, event=Timeout", "action=notifyTimeout")
edouard@1586
   810
                    status = notifyTimeout(session, state, expected, NULL);
edouard@1586
   811
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1586
   812
                        return (int) invalid_out_of_memory;
edouard@1586
   813
                    if (status != PEP_STATUS_OK)
edouard@1586
   814
                        return (int) invalid_action;
edouard@1586
   815
                    if(session->sync_state_payload){
edouard@1586
   816
                        free_identity((Identity)session->sync_state_payload);
edouard@1586
   817
                        session->sync_state_payload = NULL;
edouard@1586
   818
                    }
edouard@1586
   819
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=WaitForGroupKeysGrouped, event=Timeout", "target=Grouped")
edouard@1586
   820
                    return Grouped;
edouard@1586
   821
                }
edouard@1586
   822
                default:
edouard@1586
   823
                    return (DeviceState_state) invalid_event;
edouard@1586
   824
            }
edouard@1586
   825
            break;
edouard@1586
   826
        }
edouard@1467
   827
        default:
edouard@1467
   828
            return (DeviceState_state) invalid_state;
edouard@1467
   829
    }
edouard@1467
   830
edouard@1467
   831
    return state;
edouard@1467
   832
}
edouard@1467
   833