sync/generated/sync_fsm.c
author Edouard Tisserant <edouard@pep-project.org>
Thu, 05 Jan 2017 13:05:00 +0100
branchENGINE-135
changeset 1523 bc4a9401af51
parent 1490 e375c22bc587
child 1555 7ffa6cf9d054
permissions -rw-r--r--
ENGINE-135 added *Beaconed states to avoid sending handshake requests twice
vb@1476
     1
#include "pEp_internal.h"
edouard@1467
     2
#include "sync_fsm.h"
edouard@1467
     3
edouard@1467
     4
// state machine for DeviceState
edouard@1467
     5
edouard@1467
     6
DeviceState_state fsm_DeviceState(
edouard@1467
     7
        PEP_SESSION session,
edouard@1467
     8
        DeviceState_state state,
edouard@1467
     9
        DeviceState_event event,
edouard@1467
    10
        Identity partner,
edouard@1467
    11
        void *extra,
edouard@1467
    12
        time_t *timeout
edouard@1467
    13
    )
edouard@1467
    14
{
edouard@1467
    15
    int cond_result;
edouard@1467
    16
    PEP_STATUS status = PEP_STATUS_OK;
edouard@1467
    17
edouard@1467
    18
    switch (state) {
edouard@1467
    19
        case InitState:
edouard@1467
    20
        {
edouard@1467
    21
            *timeout = 0;
edouard@1490
    22
            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=InitState")
edouard@1467
    23
            switch (event) {
edouard@1467
    24
                case Init:
edouard@1467
    25
                {
edouard@1490
    26
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=InitState", "event=Init")
edouard@1467
    27
                    cond_result = storedGroupKeys(session);
edouard@1490
    28
                    #ifndef NDEBUG
edouard@1490
    29
                    char resstr[11] = {0,};
edouard@1490
    30
                    snprintf(resstr,10,"result=%d",cond_result);
edouard@1490
    31
                    #endif
edouard@1490
    32
                    DEBUG_LOG("FSM condition", "sync_fsm.c, state=InitState, event=Init, condition=storedGroupKeys", resstr)
edouard@1467
    33
                    if (cond_result < 0)
edouard@1467
    34
                        return cond_result;
edouard@1467
    35
                    if (cond_result) {
edouard@1490
    36
                        DEBUG_LOG("FSM transition", "sync_fsm.c, state=InitState, event=Init", "target=Grouped")
edouard@1467
    37
                        return Grouped;
edouard@1467
    38
                    }
edouard@1490
    39
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=InitState, event=Init", "target=Sole")
edouard@1467
    40
                    return Sole;
edouard@1467
    41
                }
edouard@1467
    42
                default:
edouard@1467
    43
                    return (DeviceState_state) invalid_event;
edouard@1467
    44
            }
edouard@1467
    45
            break;
edouard@1467
    46
        }
edouard@1467
    47
        case Sole:
edouard@1467
    48
        {
edouard@1467
    49
            *timeout = 0;
edouard@1490
    50
            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=Sole")
edouard@1467
    51
            switch (event) {
edouard@1490
    52
                case Init: DEBUG_LOG("FSM event", "sync_fsm.c, state=Sole", "event=Init") break;
edouard@1467
    53
                case KeyGen:
edouard@1467
    54
                {
edouard@1490
    55
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Sole", "event=KeyGen")
edouard@1490
    56
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Sole, event=KeyGen", "action=sendBeacon")
edouard@1467
    57
                    status = sendBeacon(session, state, NULL, NULL);
edouard@1467
    58
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
    59
                        return (int) invalid_out_of_memory;
edouard@1467
    60
                    if (status != PEP_STATUS_OK)
edouard@1467
    61
                        return (int) invalid_action;
edouard@1467
    62
                    break;
edouard@1467
    63
                }
edouard@1467
    64
                case CannotDecrypt:
edouard@1467
    65
                {
edouard@1490
    66
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Sole", "event=CannotDecrypt")
edouard@1490
    67
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Sole, event=CannotDecrypt", "action=sendBeacon")
edouard@1467
    68
                    status = sendBeacon(session, state, NULL, NULL);
edouard@1467
    69
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
    70
                        return (int) invalid_out_of_memory;
edouard@1467
    71
                    if (status != PEP_STATUS_OK)
edouard@1467
    72
                        return (int) invalid_action;
edouard@1467
    73
                    break;
edouard@1467
    74
                }
edouard@1467
    75
                case Beacon:
edouard@1467
    76
                {
edouard@1490
    77
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Sole", "event=Beacon")
edouard@1490
    78
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Sole, event=Beacon", "action=sendHandshakeRequest")
edouard@1467
    79
                    status = sendHandshakeRequest(session, state, partner, NULL);
edouard@1467
    80
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
    81
                        return (int) invalid_out_of_memory;
edouard@1467
    82
                    if (status != PEP_STATUS_OK)
edouard@1467
    83
                        return (int) invalid_action;
edouard@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@1490
   275
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "action=sendGroupKeys")
edouard@1467
   276
                        status = sendGroupKeys(session, state, partner, NULL);
edouard@1467
   277
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   278
                            return (int) invalid_out_of_memory;
edouard@1467
   279
                        if (status != PEP_STATUS_OK)
edouard@1467
   280
                            return (int) invalid_action;
edouard@1490
   281
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "action=notifyAcceptedGroupCreated")
edouard@1477
   282
                        status = notifyAcceptedGroupCreated(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@1467
   287
                        if(session->sync_state_payload){
edouard@1467
   288
                            free_identity((Identity)session->sync_state_payload);
edouard@1467
   289
                            session->sync_state_payload = NULL;
edouard@1467
   290
                        }
edouard@1490
   291
                        DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "target=Grouped")
edouard@1467
   292
                        return Grouped;
edouard@1467
   293
                    }
edouard@1467
   294
                    if(session->sync_state_payload){
edouard@1467
   295
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   296
                        session->sync_state_payload = NULL;
edouard@1467
   297
                    }
edouard@1467
   298
                    session->sync_state_payload = identity_dup(partner);
edouard@1490
   299
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "target=WaitForGroupKeysSole")
edouard@1467
   300
                    return WaitForGroupKeysSole;
edouard@1467
   301
                }
edouard@1467
   302
                case Cancel:
edouard@1467
   303
                {
edouard@1490
   304
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingSole", "event=Cancel")
edouard@1467
   305
                    if(session->sync_state_payload){
edouard@1467
   306
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   307
                        session->sync_state_payload = NULL;
edouard@1467
   308
                    }
edouard@1490
   309
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingSole, event=Cancel", "target=Sole")
edouard@1467
   310
                    return Sole;
edouard@1467
   311
                }
edouard@1467
   312
                case Timeout:
edouard@1467
   313
                {
edouard@1490
   314
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingSole", "event=Timeout")
edouard@1490
   315
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=Timeout", "action=notifyTimeout")
edouard@1477
   316
                    status = notifyTimeout(session, state, expected, NULL);
edouard@1467
   317
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   318
                        return (int) invalid_out_of_memory;
edouard@1467
   319
                    if (status != PEP_STATUS_OK)
edouard@1467
   320
                        return (int) invalid_action;
edouard@1467
   321
                    if(session->sync_state_payload){
edouard@1467
   322
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   323
                        session->sync_state_payload = NULL;
edouard@1467
   324
                    }
edouard@1490
   325
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingSole, event=Timeout", "target=Sole")
edouard@1467
   326
                    return Sole;
edouard@1467
   327
                }
edouard@1467
   328
                default:
edouard@1467
   329
                    return (DeviceState_state) invalid_event;
edouard@1467
   330
            }
edouard@1467
   331
            break;
edouard@1467
   332
        }
edouard@1467
   333
        case WaitForGroupKeysSole:
edouard@1467
   334
        {
edouard@1467
   335
            Identity expected = (Identity)session->sync_state_payload;
edouard@1467
   336
            *timeout = 600;
edouard@1490
   337
            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=WaitForGroupKeysSole")
edouard@1467
   338
            switch (event) {
edouard@1490
   339
                case Init: DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForGroupKeysSole", "event=Init") break;
edouard@1467
   340
                case GroupKeys:
edouard@1467
   341
                {
edouard@1490
   342
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForGroupKeysSole", "event=GroupKeys")
edouard@1523
   343
                    cond_result = sameIdentities(session, partner, expected);
edouard@1523
   344
                    #ifndef NDEBUG
edouard@1523
   345
                    char resstr[11] = {0,};
edouard@1523
   346
                    snprintf(resstr,10,"result=%d",cond_result);
edouard@1523
   347
                    #endif
edouard@1523
   348
                    DEBUG_LOG("FSM condition", "sync_fsm.c, state=WaitForGroupKeysSole, event=GroupKeys, condition=sameIdentities", resstr)
edouard@1523
   349
                    if (cond_result < 0)
edouard@1523
   350
                        return cond_result;
edouard@1523
   351
                    if (cond_result) {
edouard@1523
   352
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysSole, event=GroupKeys", "action=storeGroupKeys")
edouard@1523
   353
                        status = storeGroupKeys(session, state, partner, extra /*keys*/);
edouard@1523
   354
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   355
                            return (int) invalid_out_of_memory;
edouard@1523
   356
                        if (status != PEP_STATUS_OK)
edouard@1523
   357
                            return (int) invalid_action;
edouard@1523
   358
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysSole, event=GroupKeys", "action=notifyAcceptedDeviceAdded")
edouard@1523
   359
                        status = notifyAcceptedDeviceAdded(session, state, partner, NULL);
edouard@1523
   360
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   361
                            return (int) invalid_out_of_memory;
edouard@1523
   362
                        if (status != PEP_STATUS_OK)
edouard@1523
   363
                            return (int) invalid_action;
edouard@1523
   364
                        if(session->sync_state_payload){
edouard@1523
   365
                            free_identity((Identity)session->sync_state_payload);
edouard@1523
   366
                            session->sync_state_payload = NULL;
edouard@1523
   367
                        }
edouard@1523
   368
                        DEBUG_LOG("FSM transition", "sync_fsm.c, state=WaitForGroupKeysSole, event=GroupKeys", "target=Grouped")
edouard@1523
   369
                        return Grouped;
edouard@1467
   370
                    }
edouard@1523
   371
                    break;
edouard@1467
   372
                }
edouard@1467
   373
                case Timeout:
edouard@1467
   374
                {
edouard@1490
   375
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForGroupKeysSole", "event=Timeout")
edouard@1490
   376
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysSole, event=Timeout", "action=notifyTimeout")
edouard@1477
   377
                    status = notifyTimeout(session, state, expected, NULL);
edouard@1467
   378
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   379
                        return (int) invalid_out_of_memory;
edouard@1467
   380
                    if (status != PEP_STATUS_OK)
edouard@1467
   381
                        return (int) invalid_action;
edouard@1467
   382
                    if(session->sync_state_payload){
edouard@1467
   383
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   384
                        session->sync_state_payload = NULL;
edouard@1467
   385
                    }
edouard@1490
   386
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=WaitForGroupKeysSole, event=Timeout", "target=Sole")
edouard@1467
   387
                    return Sole;
edouard@1467
   388
                }
edouard@1467
   389
                default:
edouard@1467
   390
                    return (DeviceState_state) invalid_event;
edouard@1467
   391
            }
edouard@1467
   392
            break;
edouard@1467
   393
        }
edouard@1467
   394
        case Grouped:
edouard@1467
   395
        {
edouard@1467
   396
            *timeout = 0;
edouard@1490
   397
            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=Grouped")
edouard@1467
   398
            switch (event) {
edouard@1467
   399
                case Init:
edouard@1467
   400
                {
edouard@1490
   401
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=Init")
edouard@1490
   402
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=Init", "action=enterGroup")
edouard@1467
   403
                    status = enterGroup(session, state, NULL, NULL);
edouard@1467
   404
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   405
                        return (int) invalid_out_of_memory;
edouard@1467
   406
                    if (status != PEP_STATUS_OK)
edouard@1467
   407
                        return (int) invalid_action;
edouard@1467
   408
                    break;
edouard@1467
   409
                }
edouard@1467
   410
                case KeyGen:
edouard@1467
   411
                {
edouard@1490
   412
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=KeyGen")
edouard@1490
   413
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=KeyGen", "action=sendGroupUpdate")
edouard@1467
   414
                    status = sendGroupUpdate(session, state, NULL, NULL);
edouard@1467
   415
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   416
                        return (int) invalid_out_of_memory;
edouard@1467
   417
                    if (status != PEP_STATUS_OK)
edouard@1467
   418
                        return (int) invalid_action;
edouard@1467
   419
                    break;
edouard@1467
   420
                }
edouard@1467
   421
                case CannotDecrypt:
edouard@1467
   422
                {
edouard@1490
   423
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=CannotDecrypt")
edouard@1490
   424
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=CannotDecrypt", "action=sendUpdateRequest")
edouard@1467
   425
                    status = sendUpdateRequest(session, state, NULL, NULL);
edouard@1467
   426
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   427
                        return (int) invalid_out_of_memory;
edouard@1467
   428
                    if (status != PEP_STATUS_OK)
edouard@1467
   429
                        return (int) invalid_action;
edouard@1467
   430
                    break;
edouard@1467
   431
                }
edouard@1467
   432
                case UpdateRequest:
edouard@1467
   433
                {
edouard@1490
   434
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=UpdateRequest")
edouard@1490
   435
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=UpdateRequest", "action=sendGroupUpdate")
edouard@1467
   436
                    status = sendGroupUpdate(session, state, NULL, NULL);
edouard@1467
   437
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   438
                        return (int) invalid_out_of_memory;
edouard@1467
   439
                    if (status != PEP_STATUS_OK)
edouard@1467
   440
                        return (int) invalid_action;
edouard@1467
   441
                    break;
edouard@1467
   442
                }
edouard@1467
   443
                case Beacon:
edouard@1467
   444
                {
edouard@1490
   445
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=Beacon")
edouard@1490
   446
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=Beacon", "action=sendHandshakeRequest")
edouard@1467
   447
                    status = sendHandshakeRequest(session, state, partner, NULL);
edouard@1467
   448
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   449
                        return (int) invalid_out_of_memory;
edouard@1467
   450
                    if (status != PEP_STATUS_OK)
edouard@1467
   451
                        return (int) invalid_action;
edouard@1523
   452
                    session->sync_state_payload = identity_dup(partner);
edouard@1523
   453
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=Grouped, event=Beacon", "target=GroupedBeaconed")
edouard@1523
   454
                    return GroupedBeaconed;
edouard@1467
   455
                }
edouard@1467
   456
                case HandshakeRequest:
edouard@1467
   457
                {
edouard@1490
   458
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=HandshakeRequest")
edouard@1490
   459
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=HandshakeRequest", "action=sendHandshakeRequest")
edouard@1467
   460
                    status = sendHandshakeRequest(session, state, partner, NULL);
edouard@1467
   461
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   462
                        return (int) invalid_out_of_memory;
edouard@1467
   463
                    if (status != PEP_STATUS_OK)
edouard@1467
   464
                        return (int) invalid_action;
edouard@1467
   465
                    session->sync_state_payload = identity_dup(partner);
edouard@1490
   466
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=Grouped, event=HandshakeRequest", "target=HandshakingGrouped")
edouard@1467
   467
                    return HandshakingGrouped;
edouard@1467
   468
                }
edouard@1467
   469
                case GroupUpdate:
edouard@1467
   470
                {
edouard@1490
   471
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=GroupUpdate")
edouard@1490
   472
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=GroupUpdate", "action=storeGroupKeys")
edouard@1467
   473
                    status = storeGroupKeys(session, state, partner, extra /*keys*/);
edouard@1467
   474
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   475
                        return (int) invalid_out_of_memory;
edouard@1467
   476
                    if (status != PEP_STATUS_OK)
edouard@1467
   477
                        return (int) invalid_action;
edouard@1467
   478
                    break;
edouard@1467
   479
                }
edouard@1467
   480
                default:
edouard@1467
   481
                    return (DeviceState_state) invalid_event;
edouard@1467
   482
            }
edouard@1467
   483
            break;
edouard@1467
   484
        }
edouard@1523
   485
        case GroupedBeaconed:
edouard@1523
   486
        {
edouard@1523
   487
            Identity expected = (Identity)session->sync_state_payload;
edouard@1523
   488
            *timeout = 600;
edouard@1523
   489
            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=GroupedBeaconed")
edouard@1523
   490
            switch (event) {
edouard@1523
   491
                case Init: DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=Init") break;
edouard@1523
   492
                case KeyGen:
edouard@1523
   493
                {
edouard@1523
   494
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=KeyGen")
edouard@1523
   495
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupedBeaconed, event=KeyGen", "action=sendGroupUpdate")
edouard@1523
   496
                    status = sendGroupUpdate(session, state, NULL, NULL);
edouard@1523
   497
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   498
                        return (int) invalid_out_of_memory;
edouard@1523
   499
                    if (status != PEP_STATUS_OK)
edouard@1523
   500
                        return (int) invalid_action;
edouard@1523
   501
                    break;
edouard@1523
   502
                }
edouard@1523
   503
                case CannotDecrypt:
edouard@1523
   504
                {
edouard@1523
   505
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=CannotDecrypt")
edouard@1523
   506
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupedBeaconed, event=CannotDecrypt", "action=sendUpdateRequest")
edouard@1523
   507
                    status = sendUpdateRequest(session, state, NULL, NULL);
edouard@1523
   508
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   509
                        return (int) invalid_out_of_memory;
edouard@1523
   510
                    if (status != PEP_STATUS_OK)
edouard@1523
   511
                        return (int) invalid_action;
edouard@1523
   512
                    break;
edouard@1523
   513
                }
edouard@1523
   514
                case UpdateRequest:
edouard@1523
   515
                {
edouard@1523
   516
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=UpdateRequest")
edouard@1523
   517
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupedBeaconed, event=UpdateRequest", "action=sendGroupUpdate")
edouard@1523
   518
                    status = sendGroupUpdate(session, state, NULL, NULL);
edouard@1523
   519
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   520
                        return (int) invalid_out_of_memory;
edouard@1523
   521
                    if (status != PEP_STATUS_OK)
edouard@1523
   522
                        return (int) invalid_action;
edouard@1523
   523
                    break;
edouard@1523
   524
                }
edouard@1523
   525
                case Beacon:
edouard@1523
   526
                {
edouard@1523
   527
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=Beacon")
edouard@1523
   528
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupedBeaconed, event=Beacon", "action=sendHandshakeRequest")
edouard@1523
   529
                    status = sendHandshakeRequest(session, state, partner, NULL);
edouard@1523
   530
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   531
                        return (int) invalid_out_of_memory;
edouard@1523
   532
                    if (status != PEP_STATUS_OK)
edouard@1523
   533
                        return (int) invalid_action;
edouard@1523
   534
                    if(session->sync_state_payload){
edouard@1523
   535
                        free_identity((Identity)session->sync_state_payload);
edouard@1523
   536
                        session->sync_state_payload = NULL;
edouard@1523
   537
                    }
edouard@1523
   538
                    session->sync_state_payload = identity_dup(partner);
edouard@1523
   539
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=GroupedBeaconed, event=Beacon", "target=GroupedBeaconed")
edouard@1523
   540
                    return GroupedBeaconed;
edouard@1523
   541
                }
edouard@1523
   542
                case HandshakeRequest:
edouard@1523
   543
                {
edouard@1523
   544
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=HandshakeRequest")
edouard@1523
   545
                    cond_result = sameIdentities(session, partner, expected);
edouard@1523
   546
                    #ifndef NDEBUG
edouard@1523
   547
                    char resstr[11] = {0,};
edouard@1523
   548
                    snprintf(resstr,10,"result=%d",cond_result);
edouard@1523
   549
                    #endif
edouard@1523
   550
                    DEBUG_LOG("FSM condition", "sync_fsm.c, state=GroupedBeaconed, event=HandshakeRequest, condition=sameIdentities", resstr)
edouard@1523
   551
                    if (cond_result < 0)
edouard@1523
   552
                        return cond_result;
edouard@1523
   553
                    if (cond_result) {
edouard@1523
   554
                    }
edouard@1523
   555
                    else {
edouard@1523
   556
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupedBeaconed, event=HandshakeRequest", "action=sendHandshakeRequest")
edouard@1523
   557
                        status = sendHandshakeRequest(session, state, partner, NULL);
edouard@1523
   558
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   559
                            return (int) invalid_out_of_memory;
edouard@1523
   560
                        if (status != PEP_STATUS_OK)
edouard@1523
   561
                            return (int) invalid_action;
edouard@1523
   562
                    }
edouard@1523
   563
                    if(session->sync_state_payload){
edouard@1523
   564
                        free_identity((Identity)session->sync_state_payload);
edouard@1523
   565
                        session->sync_state_payload = NULL;
edouard@1523
   566
                    }
edouard@1523
   567
                    session->sync_state_payload = identity_dup(partner);
edouard@1523
   568
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=GroupedBeaconed, event=HandshakeRequest", "target=HandshakingGrouped")
edouard@1523
   569
                    return HandshakingGrouped;
edouard@1523
   570
                }
edouard@1523
   571
                case GroupUpdate:
edouard@1523
   572
                {
edouard@1523
   573
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=GroupUpdate")
edouard@1523
   574
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupedBeaconed, event=GroupUpdate", "action=storeGroupKeys")
edouard@1523
   575
                    status = storeGroupKeys(session, state, partner, extra /*keys*/);
edouard@1523
   576
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   577
                        return (int) invalid_out_of_memory;
edouard@1523
   578
                    if (status != PEP_STATUS_OK)
edouard@1523
   579
                        return (int) invalid_action;
edouard@1523
   580
                    break;
edouard@1523
   581
                }
edouard@1523
   582
                case Timeout:
edouard@1523
   583
                {
edouard@1523
   584
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=Timeout")
edouard@1523
   585
                    if(session->sync_state_payload){
edouard@1523
   586
                        free_identity((Identity)session->sync_state_payload);
edouard@1523
   587
                        session->sync_state_payload = NULL;
edouard@1523
   588
                    }
edouard@1523
   589
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=GroupedBeaconed, event=Timeout", "target=Grouped")
edouard@1523
   590
                    return Grouped;
edouard@1523
   591
                }
edouard@1523
   592
                default:
edouard@1523
   593
                    return (DeviceState_state) invalid_event;
edouard@1523
   594
            }
edouard@1523
   595
            break;
edouard@1523
   596
        }
edouard@1467
   597
        case HandshakingGrouped:
edouard@1467
   598
        {
edouard@1467
   599
            Identity expected = (Identity)session->sync_state_payload;
edouard@1467
   600
            *timeout = 600;
edouard@1490
   601
            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=HandshakingGrouped")
edouard@1467
   602
            switch (event) {
edouard@1467
   603
                case Init:
edouard@1467
   604
                {
edouard@1490
   605
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingGrouped", "event=Init")
edouard@1490
   606
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=Init", "action=notifyInitAddOurDevice")
edouard@1477
   607
                    status = notifyInitAddOurDevice(session, state, partner, NULL);
edouard@1467
   608
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   609
                        return (int) invalid_out_of_memory;
edouard@1467
   610
                    if (status != PEP_STATUS_OK)
edouard@1467
   611
                        return (int) invalid_action;
edouard@1467
   612
                    break;
edouard@1467
   613
                }
edouard@1467
   614
                case HandshakeRejected:
edouard@1467
   615
                {
edouard@1490
   616
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingGrouped", "event=HandshakeRejected")
edouard@1490
   617
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeRejected", "action=rejectHandshake")
edouard@1467
   618
                    status = rejectHandshake(session, state, partner, NULL);
edouard@1467
   619
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   620
                        return (int) invalid_out_of_memory;
edouard@1467
   621
                    if (status != PEP_STATUS_OK)
edouard@1467
   622
                        return (int) invalid_action;
edouard@1467
   623
                    if(session->sync_state_payload){
edouard@1467
   624
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   625
                        session->sync_state_payload = NULL;
edouard@1467
   626
                    }
edouard@1490
   627
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeRejected", "target=Grouped")
edouard@1467
   628
                    return Grouped;
edouard@1467
   629
                }
edouard@1467
   630
                case HandshakeAccepted:
edouard@1467
   631
                {
edouard@1490
   632
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingGrouped", "event=HandshakeAccepted")
edouard@1490
   633
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeAccepted", "action=acceptHandshake")
edouard@1467
   634
                    status = acceptHandshake(session, state, partner, NULL);
edouard@1467
   635
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   636
                        return (int) invalid_out_of_memory;
edouard@1467
   637
                    if (status != PEP_STATUS_OK)
edouard@1467
   638
                        return (int) invalid_action;
edouard@1490
   639
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeAccepted", "action=sendGroupKeys")
edouard@1467
   640
                    status = sendGroupKeys(session, state, partner, NULL);
edouard@1467
   641
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   642
                        return (int) invalid_out_of_memory;
edouard@1467
   643
                    if (status != PEP_STATUS_OK)
edouard@1467
   644
                        return (int) invalid_action;
edouard@1467
   645
                    if(session->sync_state_payload){
edouard@1467
   646
                        free_identity((Identity)session->sync_state_payload);
edouard@1467
   647
                        session->sync_state_payload = NULL;
edouard@1467
   648
                    }
edouard@1490
   649
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeAccepted", "target=Grouped")
edouard@1467
   650
                    return Grouped;
edouard@1467
   651
                }
edouard@1467
   652
                case Timeout:
edouard@1467
   653
                {
edouard@1490
   654
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingGrouped", "event=Timeout")
edouard@1490
   655
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=Timeout", "action=notifyTimeout")
edouard@1477
   656
                    status = notifyTimeout(session, state, expected, 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=Timeout", "target=Grouped")
edouard@1467
   666
                    return Grouped;
edouard@1467
   667
                }
edouard@1467
   668
                default:
edouard@1467
   669
                    return (DeviceState_state) invalid_event;
edouard@1467
   670
            }
edouard@1467
   671
            break;
edouard@1467
   672
        }
edouard@1467
   673
        default:
edouard@1467
   674
            return (DeviceState_state) invalid_state;
edouard@1467
   675
    }
edouard@1467
   676
edouard@1467
   677
    return state;
edouard@1467
   678
}
edouard@1467
   679