sync/generated/sync_fsm.c
author Edouard Tisserant <edouard@pep-project.org>
Mon, 20 Feb 2017 10:29:45 +0100
branchKeySyncWaitForAccept
changeset 1590 6e007351ccde
parent 1588 1a43a7eddf90
child 1596 f7c8b7401911
permissions -rw-r--r--
KeySync : added composit state payload to hold multiple state parameters
vb@1476
     1
#include "pEp_internal.h"
edouard@1467
     2
#include "sync_fsm.h"
edouard@1467
     3
edouard@1590
     4
// local definitions for DeviceState's state machine 
edouard@1590
     5
edouard@1590
     6
        typedef struct _SoleBeaconed_state_payload {
edouard@1590
     7
            Identity expected;
edouard@1590
     8
        } SoleBeaconed_state_payload_t;
edouard@1590
     9
        
edouard@1590
    10
        typedef struct _HandshakingSole_state_payload {
edouard@1590
    11
            Identity expected;
edouard@1590
    12
        } HandshakingSole_state_payload_t;
edouard@1590
    13
        
edouard@1590
    14
        typedef struct _WaitForGroupKeysSole_state_payload {
edouard@1590
    15
            Identity expected;
edouard@1590
    16
        } WaitForGroupKeysSole_state_payload_t;
edouard@1590
    17
        
edouard@1590
    18
        typedef struct _GroupedBeaconed_state_payload {
edouard@1590
    19
            Identity expected;
edouard@1590
    20
        } GroupedBeaconed_state_payload_t;
edouard@1590
    21
        
edouard@1590
    22
        typedef struct _HandshakingGrouped_state_payload {
edouard@1590
    23
            Identity expected;
edouard@1590
    24
        } HandshakingGrouped_state_payload_t;
edouard@1590
    25
        
edouard@1590
    26
        typedef struct _WaitForGroupKeysGrouped_state_payload {
edouard@1590
    27
            Identity expected;
edouard@1590
    28
        } WaitForGroupKeysGrouped_state_payload_t;
edouard@1590
    29
        
edouard@1590
    30
edouard@1467
    31
// state machine for DeviceState
edouard@1467
    32
edouard@1467
    33
DeviceState_state fsm_DeviceState(
edouard@1467
    34
        PEP_SESSION session,
edouard@1467
    35
        DeviceState_state state,
edouard@1467
    36
        DeviceState_event event,
edouard@1467
    37
        Identity partner,
edouard@1467
    38
        void *extra,
edouard@1467
    39
        time_t *timeout
edouard@1467
    40
    )
edouard@1467
    41
{
edouard@1467
    42
    int cond_result;
edouard@1467
    43
    PEP_STATUS status = PEP_STATUS_OK;
edouard@1467
    44
edouard@1467
    45
    switch (state) {
edouard@1467
    46
        case InitState:
edouard@1467
    47
        {
edouard@1590
    48
            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=InitState")
edouard@1467
    49
            *timeout = 0;
edouard@1467
    50
            switch (event) {
edouard@1467
    51
                case Init:
edouard@1467
    52
                {
edouard@1490
    53
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=InitState", "event=Init")
edouard@1574
    54
                    cond_result = deviceGrouped(session);
edouard@1490
    55
                    #ifndef NDEBUG
edouard@1490
    56
                    char resstr[11] = {0,};
edouard@1490
    57
                    snprintf(resstr,10,"result=%d",cond_result);
edouard@1490
    58
                    #endif
edouard@1574
    59
                    DEBUG_LOG("FSM condition", "sync_fsm.c, state=InitState, event=Init, condition=deviceGrouped", resstr)
edouard@1467
    60
                    if (cond_result < 0)
edouard@1467
    61
                        return cond_result;
edouard@1467
    62
                    if (cond_result) {
edouard@1490
    63
                        DEBUG_LOG("FSM transition", "sync_fsm.c, state=InitState, event=Init", "target=Grouped")
edouard@1467
    64
                        return Grouped;
edouard@1467
    65
                    }
edouard@1490
    66
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=InitState, event=Init", "target=Sole")
edouard@1467
    67
                    return Sole;
edouard@1467
    68
                }
edouard@1467
    69
                default:
edouard@1467
    70
                    return (DeviceState_state) invalid_event;
edouard@1467
    71
            }
edouard@1467
    72
            break;
edouard@1467
    73
        }
edouard@1467
    74
        case Sole:
edouard@1467
    75
        {
edouard@1590
    76
            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=Sole")
edouard@1467
    77
            *timeout = 0;
edouard@1467
    78
            switch (event) {
edouard@1590
    79
                case Init: 
edouard@1590
    80
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Sole", "event=Init") 
edouard@1590
    81
                    break;
edouard@1467
    82
                case KeyGen:
edouard@1467
    83
                {
edouard@1490
    84
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Sole", "event=KeyGen")
edouard@1490
    85
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Sole, event=KeyGen", "action=sendBeacon")
edouard@1467
    86
                    status = sendBeacon(session, state, NULL, NULL);
edouard@1467
    87
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
    88
                        return (int) invalid_out_of_memory;
edouard@1467
    89
                    if (status != PEP_STATUS_OK)
edouard@1467
    90
                        return (int) invalid_action;
edouard@1467
    91
                    break;
edouard@1467
    92
                }
edouard@1467
    93
                case CannotDecrypt:
edouard@1467
    94
                {
edouard@1490
    95
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Sole", "event=CannotDecrypt")
edouard@1490
    96
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Sole, event=CannotDecrypt", "action=sendBeacon")
edouard@1467
    97
                    status = sendBeacon(session, state, NULL, NULL);
edouard@1467
    98
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
    99
                        return (int) invalid_out_of_memory;
edouard@1467
   100
                    if (status != PEP_STATUS_OK)
edouard@1467
   101
                        return (int) invalid_action;
edouard@1467
   102
                    break;
edouard@1467
   103
                }
edouard@1467
   104
                case Beacon:
edouard@1467
   105
                {
edouard@1490
   106
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Sole", "event=Beacon")
edouard@1490
   107
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Sole, event=Beacon", "action=sendHandshakeRequest")
edouard@1467
   108
                    status = sendHandshakeRequest(session, state, partner, NULL);
edouard@1467
   109
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   110
                        return (int) invalid_out_of_memory;
edouard@1467
   111
                    if (status != PEP_STATUS_OK)
edouard@1467
   112
                        return (int) invalid_action;
edouard@1590
   113
                    session->sync_state_payload = malloc(sizeof(SoleBeaconed_state_payload_t));
edouard@1590
   114
                    assert(session->sync_state_payload);
edouard@1590
   115
                    if(!session->sync_state_payload) return (DeviceState_state) invalid_out_of_memory;
edouard@1590
   116
                    ((SoleBeaconed_state_payload_t*)session->sync_state_payload)->expected =
edouard@1590
   117
                        identity_dup(partner);
edouard@1523
   118
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=Sole, event=Beacon", "target=SoleBeaconed")
edouard@1523
   119
                    return SoleBeaconed;
edouard@1467
   120
                }
edouard@1467
   121
                case HandshakeRequest:
edouard@1467
   122
                {
edouard@1490
   123
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Sole", "event=HandshakeRequest")
edouard@1490
   124
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Sole, event=HandshakeRequest", "action=sendHandshakeRequest")
edouard@1467
   125
                    status = sendHandshakeRequest(session, state, partner, NULL);
edouard@1467
   126
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   127
                        return (int) invalid_out_of_memory;
edouard@1467
   128
                    if (status != PEP_STATUS_OK)
edouard@1467
   129
                        return (int) invalid_action;
edouard@1590
   130
                    session->sync_state_payload = malloc(sizeof(HandshakingSole_state_payload_t));
edouard@1590
   131
                    assert(session->sync_state_payload);
edouard@1590
   132
                    if(!session->sync_state_payload) return (DeviceState_state) invalid_out_of_memory;
edouard@1590
   133
                    ((HandshakingSole_state_payload_t*)session->sync_state_payload)->expected =
edouard@1590
   134
                        identity_dup(partner);
edouard@1490
   135
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=Sole, event=HandshakeRequest", "target=HandshakingSole")
edouard@1467
   136
                    return HandshakingSole;
edouard@1467
   137
                }
edouard@1467
   138
                default:
edouard@1467
   139
                    return (DeviceState_state) invalid_event;
edouard@1467
   140
            }
edouard@1467
   141
            break;
edouard@1467
   142
        }
edouard@1523
   143
        case SoleBeaconed:
edouard@1523
   144
        {
edouard@1590
   145
            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=SoleBeaconed")
edouard@1590
   146
            assert(session->sync_state_payload);
edouard@1590
   147
            if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
edouard@1590
   148
            Identity expected = ((SoleBeaconed_state_payload_t*)session->sync_state_payload)->expected;
edouard@1523
   149
            *timeout = 600;
edouard@1523
   150
            switch (event) {
edouard@1590
   151
                case Init: 
edouard@1590
   152
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=SoleBeaconed", "event=Init") 
edouard@1590
   153
                    break;
edouard@1523
   154
                case KeyGen:
edouard@1523
   155
                {
edouard@1523
   156
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=SoleBeaconed", "event=KeyGen")
edouard@1523
   157
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=SoleBeaconed, event=KeyGen", "action=sendBeacon")
edouard@1523
   158
                    status = sendBeacon(session, state, NULL, NULL);
edouard@1523
   159
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   160
                        return (int) invalid_out_of_memory;
edouard@1523
   161
                    if (status != PEP_STATUS_OK)
edouard@1523
   162
                        return (int) invalid_action;
edouard@1590
   163
                    assert(session->sync_state_payload);
edouard@1590
   164
                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
edouard@1590
   165
                    free_identity(((SoleBeaconed_state_payload_t*)session->sync_state_payload)->expected);
edouard@1590
   166
                    free(session->sync_state_payload);
edouard@1590
   167
                    session->sync_state_payload = NULL;
edouard@1523
   168
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=SoleBeaconed, event=KeyGen", "target=Sole")
edouard@1523
   169
                    return Sole;
edouard@1523
   170
                }
edouard@1523
   171
                case CannotDecrypt:
edouard@1523
   172
                {
edouard@1523
   173
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=SoleBeaconed", "event=CannotDecrypt")
edouard@1523
   174
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=SoleBeaconed, event=CannotDecrypt", "action=sendBeacon")
edouard@1523
   175
                    status = sendBeacon(session, state, NULL, NULL);
edouard@1523
   176
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   177
                        return (int) invalid_out_of_memory;
edouard@1523
   178
                    if (status != PEP_STATUS_OK)
edouard@1523
   179
                        return (int) invalid_action;
edouard@1590
   180
                    assert(session->sync_state_payload);
edouard@1590
   181
                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
edouard@1590
   182
                    free_identity(((SoleBeaconed_state_payload_t*)session->sync_state_payload)->expected);
edouard@1590
   183
                    free(session->sync_state_payload);
edouard@1590
   184
                    session->sync_state_payload = NULL;
edouard@1523
   185
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=SoleBeaconed, event=CannotDecrypt", "target=Sole")
edouard@1523
   186
                    return Sole;
edouard@1523
   187
                }
edouard@1523
   188
                case Beacon:
edouard@1523
   189
                {
edouard@1523
   190
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=SoleBeaconed", "event=Beacon")
edouard@1523
   191
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=SoleBeaconed, event=Beacon", "action=sendHandshakeRequest")
edouard@1523
   192
                    status = sendHandshakeRequest(session, state, partner, NULL);
edouard@1523
   193
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   194
                        return (int) invalid_out_of_memory;
edouard@1523
   195
                    if (status != PEP_STATUS_OK)
edouard@1523
   196
                        return (int) invalid_action;
edouard@1590
   197
                    assert(session->sync_state_payload);
edouard@1590
   198
                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
edouard@1590
   199
                    free_identity(((SoleBeaconed_state_payload_t*)session->sync_state_payload)->expected);
edouard@1590
   200
                    free(session->sync_state_payload);
edouard@1590
   201
                    session->sync_state_payload = NULL;
edouard@1590
   202
                    session->sync_state_payload = malloc(sizeof(SoleBeaconed_state_payload_t));
edouard@1590
   203
                    assert(session->sync_state_payload);
edouard@1590
   204
                    if(!session->sync_state_payload) return (DeviceState_state) invalid_out_of_memory;
edouard@1590
   205
                    ((SoleBeaconed_state_payload_t*)session->sync_state_payload)->expected =
edouard@1590
   206
                        identity_dup(partner);
edouard@1523
   207
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=SoleBeaconed, event=Beacon", "target=SoleBeaconed")
edouard@1523
   208
                    return SoleBeaconed;
edouard@1523
   209
                }
edouard@1523
   210
                case HandshakeRequest:
edouard@1523
   211
                {
edouard@1523
   212
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=SoleBeaconed", "event=HandshakeRequest")
edouard@1523
   213
                    cond_result = sameIdentities(session, partner, expected);
edouard@1523
   214
                    #ifndef NDEBUG
edouard@1523
   215
                    char resstr[11] = {0,};
edouard@1523
   216
                    snprintf(resstr,10,"result=%d",cond_result);
edouard@1523
   217
                    #endif
edouard@1523
   218
                    DEBUG_LOG("FSM condition", "sync_fsm.c, state=SoleBeaconed, event=HandshakeRequest, condition=sameIdentities", resstr)
edouard@1523
   219
                    if (cond_result < 0)
edouard@1523
   220
                        return cond_result;
edouard@1523
   221
                    if (cond_result) {
edouard@1523
   222
                    }
edouard@1523
   223
                    else {
edouard@1523
   224
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=SoleBeaconed, event=HandshakeRequest", "action=sendHandshakeRequest")
edouard@1523
   225
                        status = sendHandshakeRequest(session, state, partner, NULL);
edouard@1523
   226
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   227
                            return (int) invalid_out_of_memory;
edouard@1523
   228
                        if (status != PEP_STATUS_OK)
edouard@1523
   229
                            return (int) invalid_action;
edouard@1523
   230
                    }
edouard@1590
   231
                    assert(session->sync_state_payload);
edouard@1590
   232
                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
edouard@1590
   233
                    free_identity(((SoleBeaconed_state_payload_t*)session->sync_state_payload)->expected);
edouard@1590
   234
                    free(session->sync_state_payload);
edouard@1590
   235
                    session->sync_state_payload = NULL;
edouard@1590
   236
                    session->sync_state_payload = malloc(sizeof(HandshakingSole_state_payload_t));
edouard@1590
   237
                    assert(session->sync_state_payload);
edouard@1590
   238
                    if(!session->sync_state_payload) return (DeviceState_state) invalid_out_of_memory;
edouard@1590
   239
                    ((HandshakingSole_state_payload_t*)session->sync_state_payload)->expected =
edouard@1590
   240
                        identity_dup(partner);
edouard@1523
   241
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=SoleBeaconed, event=HandshakeRequest", "target=HandshakingSole")
edouard@1523
   242
                    return HandshakingSole;
edouard@1523
   243
                }
edouard@1523
   244
                case Timeout:
edouard@1523
   245
                {
edouard@1523
   246
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=SoleBeaconed", "event=Timeout")
edouard@1590
   247
                    assert(session->sync_state_payload);
edouard@1590
   248
                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
edouard@1590
   249
                    free_identity(((SoleBeaconed_state_payload_t*)session->sync_state_payload)->expected);
edouard@1590
   250
                    free(session->sync_state_payload);
edouard@1590
   251
                    session->sync_state_payload = NULL;
edouard@1523
   252
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=SoleBeaconed, event=Timeout", "target=Sole")
edouard@1523
   253
                    return Sole;
edouard@1523
   254
                }
edouard@1523
   255
                default:
edouard@1523
   256
                    return (DeviceState_state) invalid_event;
edouard@1523
   257
            }
edouard@1523
   258
            break;
edouard@1523
   259
        }
edouard@1467
   260
        case HandshakingSole:
edouard@1467
   261
        {
edouard@1590
   262
            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=HandshakingSole")
edouard@1590
   263
            assert(session->sync_state_payload);
edouard@1590
   264
            if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
edouard@1590
   265
            Identity expected = ((HandshakingSole_state_payload_t*)session->sync_state_payload)->expected;
edouard@1467
   266
            *timeout = 600;
edouard@1467
   267
            switch (event) {
edouard@1467
   268
                case Init:
edouard@1467
   269
                {
edouard@1490
   270
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingSole", "event=Init")
edouard@1477
   271
                    cond_result = keyElectionWon(session, partner);
edouard@1490
   272
                    #ifndef NDEBUG
edouard@1490
   273
                    char resstr[11] = {0,};
edouard@1490
   274
                    snprintf(resstr,10,"result=%d",cond_result);
edouard@1490
   275
                    #endif
edouard@1490
   276
                    DEBUG_LOG("FSM condition", "sync_fsm.c, state=HandshakingSole, event=Init, condition=keyElectionWon", resstr)
edouard@1477
   277
                    if (cond_result < 0)
edouard@1477
   278
                        return cond_result;
edouard@1477
   279
                    if (cond_result) {
edouard@1490
   280
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=Init", "action=notifyInitFormGroup")
edouard@1477
   281
                        status = notifyInitFormGroup(session, state, partner, NULL);
edouard@1477
   282
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1477
   283
                            return (int) invalid_out_of_memory;
edouard@1477
   284
                        if (status != PEP_STATUS_OK)
edouard@1477
   285
                            return (int) invalid_action;
edouard@1477
   286
                    }
edouard@1477
   287
                    else {
edouard@1490
   288
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=Init", "action=notifyInitAddOurDevice")
edouard@1477
   289
                        status = notifyInitAddOurDevice(session, state, partner, NULL);
edouard@1477
   290
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1477
   291
                            return (int) invalid_out_of_memory;
edouard@1477
   292
                        if (status != PEP_STATUS_OK)
edouard@1477
   293
                            return (int) invalid_action;
edouard@1477
   294
                    }
edouard@1467
   295
                    break;
edouard@1467
   296
                }
edouard@1467
   297
                case HandshakeRejected:
edouard@1467
   298
                {
edouard@1490
   299
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingSole", "event=HandshakeRejected")
edouard@1490
   300
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=HandshakeRejected", "action=rejectHandshake")
edouard@1467
   301
                    status = rejectHandshake(session, state, partner, NULL);
edouard@1467
   302
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   303
                        return (int) invalid_out_of_memory;
edouard@1467
   304
                    if (status != PEP_STATUS_OK)
edouard@1467
   305
                        return (int) invalid_action;
edouard@1590
   306
                    assert(session->sync_state_payload);
edouard@1590
   307
                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
edouard@1590
   308
                    free_identity(((HandshakingSole_state_payload_t*)session->sync_state_payload)->expected);
edouard@1590
   309
                    free(session->sync_state_payload);
edouard@1590
   310
                    session->sync_state_payload = NULL;
edouard@1490
   311
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingSole, event=HandshakeRejected", "target=Sole")
edouard@1467
   312
                    return Sole;
edouard@1467
   313
                }
edouard@1467
   314
                case HandshakeAccepted:
edouard@1467
   315
                {
edouard@1490
   316
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingSole", "event=HandshakeAccepted")
edouard@1588
   317
                    cond_result = sameIdentities(session, partner, expected);
edouard@1490
   318
                    #ifndef NDEBUG
edouard@1490
   319
                    char resstr[11] = {0,};
edouard@1490
   320
                    snprintf(resstr,10,"result=%d",cond_result);
edouard@1490
   321
                    #endif
edouard@1588
   322
                    DEBUG_LOG("FSM condition", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted, condition=sameIdentities", resstr)
edouard@1467
   323
                    if (cond_result < 0)
edouard@1467
   324
                        return cond_result;
edouard@1467
   325
                    if (cond_result) {
edouard@1588
   326
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "action=acceptHandshake")
edouard@1588
   327
                        status = acceptHandshake(session, state, partner, NULL);
edouard@1566
   328
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1566
   329
                            return (int) invalid_out_of_memory;
edouard@1566
   330
                        if (status != PEP_STATUS_OK)
edouard@1566
   331
                            return (int) invalid_action;
edouard@1588
   332
                        cond_result = keyElectionWon(session, partner);
edouard@1588
   333
                        #ifndef NDEBUG
edouard@1588
   334
                        char resstr[11] = {0,};
edouard@1588
   335
                        snprintf(resstr,10,"result=%d",cond_result);
edouard@1588
   336
                        #endif
edouard@1588
   337
                        DEBUG_LOG("FSM condition", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted, condition=keyElectionWon", resstr)
edouard@1588
   338
                        if (cond_result < 0)
edouard@1588
   339
                            return cond_result;
edouard@1588
   340
                        if (cond_result) {
edouard@1588
   341
                            DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "action=makeGroup")
edouard@1588
   342
                            status = makeGroup(session, state, NULL, NULL);
edouard@1588
   343
                            if (status == PEP_OUT_OF_MEMORY)
edouard@1588
   344
                                return (int) invalid_out_of_memory;
edouard@1588
   345
                            if (status != PEP_STATUS_OK)
edouard@1588
   346
                                return (int) invalid_action;
edouard@1588
   347
                            DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "action=sendGroupKeys")
edouard@1588
   348
                            status = sendGroupKeys(session, state, partner, NULL);
edouard@1588
   349
                            if (status == PEP_OUT_OF_MEMORY)
edouard@1588
   350
                                return (int) invalid_out_of_memory;
edouard@1588
   351
                            if (status != PEP_STATUS_OK)
edouard@1588
   352
                                return (int) invalid_action;
edouard@1588
   353
                            DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "action=notifyAcceptedGroupCreated")
edouard@1588
   354
                            status = notifyAcceptedGroupCreated(session, state, partner, NULL);
edouard@1588
   355
                            if (status == PEP_OUT_OF_MEMORY)
edouard@1588
   356
                                return (int) invalid_out_of_memory;
edouard@1588
   357
                            if (status != PEP_STATUS_OK)
edouard@1588
   358
                                return (int) invalid_action;
edouard@1590
   359
                            assert(session->sync_state_payload);
edouard@1590
   360
                            if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
edouard@1590
   361
                            free_identity(((HandshakingSole_state_payload_t*)session->sync_state_payload)->expected);
edouard@1590
   362
                            free(session->sync_state_payload);
edouard@1590
   363
                            session->sync_state_payload = NULL;
edouard@1588
   364
                            DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "target=Grouped")
edouard@1588
   365
                            return Grouped;
edouard@1588
   366
                        }
edouard@1590
   367
                        assert(session->sync_state_payload);
edouard@1590
   368
                        if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
edouard@1590
   369
                        free_identity(((HandshakingSole_state_payload_t*)session->sync_state_payload)->expected);
edouard@1590
   370
                        free(session->sync_state_payload);
edouard@1590
   371
                        session->sync_state_payload = NULL;
edouard@1590
   372
                        session->sync_state_payload = malloc(sizeof(WaitForGroupKeysSole_state_payload_t));
edouard@1590
   373
                        assert(session->sync_state_payload);
edouard@1590
   374
                        if(!session->sync_state_payload) return (DeviceState_state) invalid_out_of_memory;
edouard@1590
   375
                        ((WaitForGroupKeysSole_state_payload_t*)session->sync_state_payload)->expected =
edouard@1590
   376
                            identity_dup(partner);
edouard@1588
   377
                        DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "target=WaitForGroupKeysSole")
edouard@1588
   378
                        return WaitForGroupKeysSole;
edouard@1467
   379
                    }
edouard@1590
   380
                    assert(session->sync_state_payload);
edouard@1590
   381
                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
edouard@1590
   382
                    free_identity(((HandshakingSole_state_payload_t*)session->sync_state_payload)->expected);
edouard@1590
   383
                    free(session->sync_state_payload);
edouard@1590
   384
                    session->sync_state_payload = NULL;
edouard@1588
   385
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingSole, event=HandshakeAccepted", "target=Sole")
edouard@1588
   386
                    return Sole;
edouard@1467
   387
                }
edouard@1467
   388
                case Cancel:
edouard@1467
   389
                {
edouard@1490
   390
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingSole", "event=Cancel")
edouard@1590
   391
                    assert(session->sync_state_payload);
edouard@1590
   392
                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
edouard@1590
   393
                    free_identity(((HandshakingSole_state_payload_t*)session->sync_state_payload)->expected);
edouard@1590
   394
                    free(session->sync_state_payload);
edouard@1590
   395
                    session->sync_state_payload = NULL;
edouard@1490
   396
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingSole, event=Cancel", "target=Sole")
edouard@1467
   397
                    return Sole;
edouard@1467
   398
                }
edouard@1467
   399
                case Timeout:
edouard@1467
   400
                {
edouard@1490
   401
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingSole", "event=Timeout")
edouard@1490
   402
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=Timeout", "action=notifyTimeout")
edouard@1477
   403
                    status = notifyTimeout(session, state, expected, 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@1555
   408
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingSole, event=Timeout", "action=sendBeacon")
edouard@1555
   409
                    status = sendBeacon(session, state, NULL, NULL);
edouard@1555
   410
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1555
   411
                        return (int) invalid_out_of_memory;
edouard@1555
   412
                    if (status != PEP_STATUS_OK)
edouard@1555
   413
                        return (int) invalid_action;
edouard@1590
   414
                    assert(session->sync_state_payload);
edouard@1590
   415
                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
edouard@1590
   416
                    free_identity(((HandshakingSole_state_payload_t*)session->sync_state_payload)->expected);
edouard@1590
   417
                    free(session->sync_state_payload);
edouard@1590
   418
                    session->sync_state_payload = NULL;
edouard@1490
   419
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingSole, event=Timeout", "target=Sole")
edouard@1467
   420
                    return Sole;
edouard@1467
   421
                }
edouard@1467
   422
                default:
edouard@1467
   423
                    return (DeviceState_state) invalid_event;
edouard@1467
   424
            }
edouard@1467
   425
            break;
edouard@1467
   426
        }
edouard@1467
   427
        case WaitForGroupKeysSole:
edouard@1467
   428
        {
edouard@1590
   429
            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=WaitForGroupKeysSole")
edouard@1590
   430
            assert(session->sync_state_payload);
edouard@1590
   431
            if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
edouard@1590
   432
            Identity expected = ((WaitForGroupKeysSole_state_payload_t*)session->sync_state_payload)->expected;
edouard@1467
   433
            *timeout = 600;
edouard@1467
   434
            switch (event) {
edouard@1590
   435
                case Init: 
edouard@1590
   436
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForGroupKeysSole", "event=Init") 
edouard@1590
   437
                    break;
edouard@1467
   438
                case GroupKeys:
edouard@1467
   439
                {
edouard@1490
   440
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForGroupKeysSole", "event=GroupKeys")
edouard@1523
   441
                    cond_result = sameIdentities(session, partner, expected);
edouard@1523
   442
                    #ifndef NDEBUG
edouard@1523
   443
                    char resstr[11] = {0,};
edouard@1523
   444
                    snprintf(resstr,10,"result=%d",cond_result);
edouard@1523
   445
                    #endif
edouard@1523
   446
                    DEBUG_LOG("FSM condition", "sync_fsm.c, state=WaitForGroupKeysSole, event=GroupKeys, condition=sameIdentities", resstr)
edouard@1523
   447
                    if (cond_result < 0)
edouard@1523
   448
                        return cond_result;
edouard@1523
   449
                    if (cond_result) {
edouard@1523
   450
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysSole, event=GroupKeys", "action=storeGroupKeys")
edouard@1523
   451
                        status = storeGroupKeys(session, state, partner, extra /*keys*/);
edouard@1523
   452
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   453
                            return (int) invalid_out_of_memory;
edouard@1523
   454
                        if (status != PEP_STATUS_OK)
edouard@1523
   455
                            return (int) invalid_action;
edouard@1566
   456
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysSole, event=GroupKeys", "action=sendGroupUpdate")
edouard@1566
   457
                        status = sendGroupUpdate(session, state, NULL, NULL);
edouard@1566
   458
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1566
   459
                            return (int) invalid_out_of_memory;
edouard@1566
   460
                        if (status != PEP_STATUS_OK)
edouard@1566
   461
                            return (int) invalid_action;
edouard@1523
   462
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysSole, event=GroupKeys", "action=notifyAcceptedDeviceAdded")
edouard@1523
   463
                        status = notifyAcceptedDeviceAdded(session, state, partner, NULL);
edouard@1523
   464
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   465
                            return (int) invalid_out_of_memory;
edouard@1523
   466
                        if (status != PEP_STATUS_OK)
edouard@1523
   467
                            return (int) invalid_action;
edouard@1590
   468
                        assert(session->sync_state_payload);
edouard@1590
   469
                        if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
edouard@1590
   470
                        free_identity(((WaitForGroupKeysSole_state_payload_t*)session->sync_state_payload)->expected);
edouard@1590
   471
                        free(session->sync_state_payload);
edouard@1590
   472
                        session->sync_state_payload = NULL;
edouard@1523
   473
                        DEBUG_LOG("FSM transition", "sync_fsm.c, state=WaitForGroupKeysSole, event=GroupKeys", "target=Grouped")
edouard@1523
   474
                        return Grouped;
edouard@1467
   475
                    }
edouard@1523
   476
                    break;
edouard@1467
   477
                }
edouard@1467
   478
                case Timeout:
edouard@1467
   479
                {
edouard@1490
   480
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForGroupKeysSole", "event=Timeout")
edouard@1490
   481
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysSole, event=Timeout", "action=notifyTimeout")
edouard@1477
   482
                    status = notifyTimeout(session, state, expected, NULL);
edouard@1467
   483
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   484
                        return (int) invalid_out_of_memory;
edouard@1467
   485
                    if (status != PEP_STATUS_OK)
edouard@1467
   486
                        return (int) invalid_action;
edouard@1590
   487
                    assert(session->sync_state_payload);
edouard@1590
   488
                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
edouard@1590
   489
                    free_identity(((WaitForGroupKeysSole_state_payload_t*)session->sync_state_payload)->expected);
edouard@1590
   490
                    free(session->sync_state_payload);
edouard@1590
   491
                    session->sync_state_payload = NULL;
edouard@1490
   492
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=WaitForGroupKeysSole, event=Timeout", "target=Sole")
edouard@1467
   493
                    return Sole;
edouard@1467
   494
                }
edouard@1467
   495
                default:
edouard@1467
   496
                    return (DeviceState_state) invalid_event;
edouard@1467
   497
            }
edouard@1467
   498
            break;
edouard@1467
   499
        }
edouard@1467
   500
        case Grouped:
edouard@1467
   501
        {
edouard@1590
   502
            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=Grouped")
edouard@1467
   503
            *timeout = 0;
edouard@1467
   504
            switch (event) {
edouard@1590
   505
                case Init: 
edouard@1590
   506
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=Init") 
edouard@1590
   507
                    break;
edouard@1467
   508
                case KeyGen:
edouard@1467
   509
                {
edouard@1490
   510
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=KeyGen")
edouard@1490
   511
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=KeyGen", "action=sendGroupUpdate")
edouard@1467
   512
                    status = sendGroupUpdate(session, state, NULL, NULL);
edouard@1467
   513
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   514
                        return (int) invalid_out_of_memory;
edouard@1467
   515
                    if (status != PEP_STATUS_OK)
edouard@1467
   516
                        return (int) invalid_action;
edouard@1467
   517
                    break;
edouard@1467
   518
                }
edouard@1467
   519
                case CannotDecrypt:
edouard@1467
   520
                {
edouard@1490
   521
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=CannotDecrypt")
edouard@1490
   522
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=CannotDecrypt", "action=sendUpdateRequest")
edouard@1467
   523
                    status = sendUpdateRequest(session, state, NULL, NULL);
edouard@1467
   524
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   525
                        return (int) invalid_out_of_memory;
edouard@1467
   526
                    if (status != PEP_STATUS_OK)
edouard@1467
   527
                        return (int) invalid_action;
edouard@1586
   528
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=CannotDecrypt", "action=sendBeacon")
edouard@1586
   529
                    status = sendBeacon(session, state, NULL, NULL);
edouard@1586
   530
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1586
   531
                        return (int) invalid_out_of_memory;
edouard@1586
   532
                    if (status != PEP_STATUS_OK)
edouard@1586
   533
                        return (int) invalid_action;
edouard@1467
   534
                    break;
edouard@1467
   535
                }
edouard@1467
   536
                case UpdateRequest:
edouard@1467
   537
                {
edouard@1490
   538
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=UpdateRequest")
edouard@1490
   539
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=UpdateRequest", "action=sendGroupUpdate")
edouard@1467
   540
                    status = sendGroupUpdate(session, state, NULL, NULL);
edouard@1467
   541
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   542
                        return (int) invalid_out_of_memory;
edouard@1467
   543
                    if (status != PEP_STATUS_OK)
edouard@1467
   544
                        return (int) invalid_action;
edouard@1467
   545
                    break;
edouard@1467
   546
                }
edouard@1467
   547
                case Beacon:
edouard@1467
   548
                {
edouard@1490
   549
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=Beacon")
edouard@1490
   550
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=Beacon", "action=sendHandshakeRequest")
edouard@1467
   551
                    status = sendHandshakeRequest(session, state, partner, NULL);
edouard@1467
   552
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   553
                        return (int) invalid_out_of_memory;
edouard@1467
   554
                    if (status != PEP_STATUS_OK)
edouard@1467
   555
                        return (int) invalid_action;
edouard@1590
   556
                    session->sync_state_payload = malloc(sizeof(GroupedBeaconed_state_payload_t));
edouard@1590
   557
                    assert(session->sync_state_payload);
edouard@1590
   558
                    if(!session->sync_state_payload) return (DeviceState_state) invalid_out_of_memory;
edouard@1590
   559
                    ((GroupedBeaconed_state_payload_t*)session->sync_state_payload)->expected =
edouard@1590
   560
                        identity_dup(partner);
edouard@1523
   561
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=Grouped, event=Beacon", "target=GroupedBeaconed")
edouard@1523
   562
                    return GroupedBeaconed;
edouard@1467
   563
                }
edouard@1467
   564
                case HandshakeRequest:
edouard@1467
   565
                {
edouard@1490
   566
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=HandshakeRequest")
edouard@1490
   567
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=HandshakeRequest", "action=sendHandshakeRequest")
edouard@1467
   568
                    status = sendHandshakeRequest(session, state, partner, NULL);
edouard@1467
   569
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   570
                        return (int) invalid_out_of_memory;
edouard@1467
   571
                    if (status != PEP_STATUS_OK)
edouard@1467
   572
                        return (int) invalid_action;
edouard@1590
   573
                    session->sync_state_payload = malloc(sizeof(HandshakingGrouped_state_payload_t));
edouard@1590
   574
                    assert(session->sync_state_payload);
edouard@1590
   575
                    if(!session->sync_state_payload) return (DeviceState_state) invalid_out_of_memory;
edouard@1590
   576
                    ((HandshakingGrouped_state_payload_t*)session->sync_state_payload)->expected =
edouard@1590
   577
                        identity_dup(partner);
edouard@1490
   578
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=Grouped, event=HandshakeRequest", "target=HandshakingGrouped")
edouard@1467
   579
                    return HandshakingGrouped;
edouard@1467
   580
                }
edouard@1467
   581
                case GroupUpdate:
edouard@1467
   582
                {
edouard@1490
   583
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=Grouped", "event=GroupUpdate")
edouard@1586
   584
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=Grouped, event=GroupUpdate", "action=storeGroupUpdate")
edouard@1586
   585
                    status = storeGroupUpdate(session, state, partner, extra /*keys*/);
edouard@1467
   586
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   587
                        return (int) invalid_out_of_memory;
edouard@1467
   588
                    if (status != PEP_STATUS_OK)
edouard@1467
   589
                        return (int) invalid_action;
edouard@1467
   590
                    break;
edouard@1467
   591
                }
edouard@1467
   592
                default:
edouard@1467
   593
                    return (DeviceState_state) invalid_event;
edouard@1467
   594
            }
edouard@1467
   595
            break;
edouard@1467
   596
        }
edouard@1523
   597
        case GroupedBeaconed:
edouard@1523
   598
        {
edouard@1590
   599
            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=GroupedBeaconed")
edouard@1590
   600
            assert(session->sync_state_payload);
edouard@1590
   601
            if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
edouard@1590
   602
            Identity expected = ((GroupedBeaconed_state_payload_t*)session->sync_state_payload)->expected;
edouard@1523
   603
            *timeout = 600;
edouard@1523
   604
            switch (event) {
edouard@1590
   605
                case Init: 
edouard@1590
   606
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=Init") 
edouard@1590
   607
                    break;
edouard@1523
   608
                case KeyGen:
edouard@1523
   609
                {
edouard@1523
   610
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=KeyGen")
edouard@1523
   611
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupedBeaconed, event=KeyGen", "action=sendGroupUpdate")
edouard@1523
   612
                    status = sendGroupUpdate(session, state, NULL, NULL);
edouard@1523
   613
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   614
                        return (int) invalid_out_of_memory;
edouard@1523
   615
                    if (status != PEP_STATUS_OK)
edouard@1523
   616
                        return (int) invalid_action;
edouard@1523
   617
                    break;
edouard@1523
   618
                }
edouard@1523
   619
                case CannotDecrypt:
edouard@1523
   620
                {
edouard@1523
   621
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=CannotDecrypt")
edouard@1523
   622
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupedBeaconed, event=CannotDecrypt", "action=sendUpdateRequest")
edouard@1523
   623
                    status = sendUpdateRequest(session, state, NULL, NULL);
edouard@1523
   624
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   625
                        return (int) invalid_out_of_memory;
edouard@1523
   626
                    if (status != PEP_STATUS_OK)
edouard@1523
   627
                        return (int) invalid_action;
edouard@1586
   628
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupedBeaconed, event=CannotDecrypt", "action=sendBeacon")
edouard@1586
   629
                    status = sendBeacon(session, state, NULL, NULL);
edouard@1586
   630
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1586
   631
                        return (int) invalid_out_of_memory;
edouard@1586
   632
                    if (status != PEP_STATUS_OK)
edouard@1586
   633
                        return (int) invalid_action;
edouard@1523
   634
                    break;
edouard@1523
   635
                }
edouard@1523
   636
                case UpdateRequest:
edouard@1523
   637
                {
edouard@1523
   638
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=UpdateRequest")
edouard@1523
   639
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupedBeaconed, event=UpdateRequest", "action=sendGroupUpdate")
edouard@1523
   640
                    status = sendGroupUpdate(session, state, NULL, NULL);
edouard@1523
   641
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   642
                        return (int) invalid_out_of_memory;
edouard@1523
   643
                    if (status != PEP_STATUS_OK)
edouard@1523
   644
                        return (int) invalid_action;
edouard@1523
   645
                    break;
edouard@1523
   646
                }
edouard@1523
   647
                case Beacon:
edouard@1523
   648
                {
edouard@1523
   649
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=Beacon")
edouard@1523
   650
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupedBeaconed, event=Beacon", "action=sendHandshakeRequest")
edouard@1523
   651
                    status = sendHandshakeRequest(session, state, partner, NULL);
edouard@1523
   652
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   653
                        return (int) invalid_out_of_memory;
edouard@1523
   654
                    if (status != PEP_STATUS_OK)
edouard@1523
   655
                        return (int) invalid_action;
edouard@1590
   656
                    assert(session->sync_state_payload);
edouard@1590
   657
                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
edouard@1590
   658
                    free_identity(((GroupedBeaconed_state_payload_t*)session->sync_state_payload)->expected);
edouard@1590
   659
                    free(session->sync_state_payload);
edouard@1590
   660
                    session->sync_state_payload = NULL;
edouard@1590
   661
                    session->sync_state_payload = malloc(sizeof(GroupedBeaconed_state_payload_t));
edouard@1590
   662
                    assert(session->sync_state_payload);
edouard@1590
   663
                    if(!session->sync_state_payload) return (DeviceState_state) invalid_out_of_memory;
edouard@1590
   664
                    ((GroupedBeaconed_state_payload_t*)session->sync_state_payload)->expected =
edouard@1590
   665
                        identity_dup(partner);
edouard@1523
   666
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=GroupedBeaconed, event=Beacon", "target=GroupedBeaconed")
edouard@1523
   667
                    return GroupedBeaconed;
edouard@1523
   668
                }
edouard@1523
   669
                case HandshakeRequest:
edouard@1523
   670
                {
edouard@1523
   671
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=HandshakeRequest")
edouard@1523
   672
                    cond_result = sameIdentities(session, partner, expected);
edouard@1523
   673
                    #ifndef NDEBUG
edouard@1523
   674
                    char resstr[11] = {0,};
edouard@1523
   675
                    snprintf(resstr,10,"result=%d",cond_result);
edouard@1523
   676
                    #endif
edouard@1523
   677
                    DEBUG_LOG("FSM condition", "sync_fsm.c, state=GroupedBeaconed, event=HandshakeRequest, condition=sameIdentities", resstr)
edouard@1523
   678
                    if (cond_result < 0)
edouard@1523
   679
                        return cond_result;
edouard@1523
   680
                    if (cond_result) {
edouard@1523
   681
                    }
edouard@1523
   682
                    else {
edouard@1523
   683
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupedBeaconed, event=HandshakeRequest", "action=sendHandshakeRequest")
edouard@1523
   684
                        status = sendHandshakeRequest(session, state, partner, NULL);
edouard@1523
   685
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   686
                            return (int) invalid_out_of_memory;
edouard@1523
   687
                        if (status != PEP_STATUS_OK)
edouard@1523
   688
                            return (int) invalid_action;
edouard@1523
   689
                    }
edouard@1590
   690
                    assert(session->sync_state_payload);
edouard@1590
   691
                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
edouard@1590
   692
                    free_identity(((GroupedBeaconed_state_payload_t*)session->sync_state_payload)->expected);
edouard@1590
   693
                    free(session->sync_state_payload);
edouard@1590
   694
                    session->sync_state_payload = NULL;
edouard@1590
   695
                    session->sync_state_payload = malloc(sizeof(HandshakingGrouped_state_payload_t));
edouard@1590
   696
                    assert(session->sync_state_payload);
edouard@1590
   697
                    if(!session->sync_state_payload) return (DeviceState_state) invalid_out_of_memory;
edouard@1590
   698
                    ((HandshakingGrouped_state_payload_t*)session->sync_state_payload)->expected =
edouard@1590
   699
                        identity_dup(partner);
edouard@1523
   700
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=GroupedBeaconed, event=HandshakeRequest", "target=HandshakingGrouped")
edouard@1523
   701
                    return HandshakingGrouped;
edouard@1523
   702
                }
edouard@1523
   703
                case GroupUpdate:
edouard@1523
   704
                {
edouard@1523
   705
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=GroupUpdate")
edouard@1588
   706
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=GroupedBeaconed, event=GroupUpdate", "action=storeGroupUpdate")
edouard@1588
   707
                    status = storeGroupUpdate(session, state, partner, extra /*keys*/);
edouard@1523
   708
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1523
   709
                        return (int) invalid_out_of_memory;
edouard@1523
   710
                    if (status != PEP_STATUS_OK)
edouard@1523
   711
                        return (int) invalid_action;
edouard@1523
   712
                    break;
edouard@1523
   713
                }
edouard@1523
   714
                case Timeout:
edouard@1523
   715
                {
edouard@1523
   716
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=GroupedBeaconed", "event=Timeout")
edouard@1590
   717
                    assert(session->sync_state_payload);
edouard@1590
   718
                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
edouard@1590
   719
                    free_identity(((GroupedBeaconed_state_payload_t*)session->sync_state_payload)->expected);
edouard@1590
   720
                    free(session->sync_state_payload);
edouard@1590
   721
                    session->sync_state_payload = NULL;
edouard@1523
   722
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=GroupedBeaconed, event=Timeout", "target=Grouped")
edouard@1523
   723
                    return Grouped;
edouard@1523
   724
                }
edouard@1523
   725
                default:
edouard@1523
   726
                    return (DeviceState_state) invalid_event;
edouard@1523
   727
            }
edouard@1523
   728
            break;
edouard@1523
   729
        }
edouard@1467
   730
        case HandshakingGrouped:
edouard@1467
   731
        {
edouard@1590
   732
            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=HandshakingGrouped")
edouard@1590
   733
            assert(session->sync_state_payload);
edouard@1590
   734
            if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
edouard@1590
   735
            Identity expected = ((HandshakingGrouped_state_payload_t*)session->sync_state_payload)->expected;
edouard@1467
   736
            *timeout = 600;
edouard@1467
   737
            switch (event) {
edouard@1467
   738
                case Init:
edouard@1467
   739
                {
edouard@1490
   740
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingGrouped", "event=Init")
edouard@1586
   741
                    cond_result = keyElectionWon(session, partner);
edouard@1586
   742
                    #ifndef NDEBUG
edouard@1586
   743
                    char resstr[11] = {0,};
edouard@1586
   744
                    snprintf(resstr,10,"result=%d",cond_result);
edouard@1586
   745
                    #endif
edouard@1586
   746
                    DEBUG_LOG("FSM condition", "sync_fsm.c, state=HandshakingGrouped, event=Init, condition=keyElectionWon", resstr)
edouard@1586
   747
                    if (cond_result < 0)
edouard@1586
   748
                        return cond_result;
edouard@1586
   749
                    if (cond_result) {
edouard@1586
   750
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=Init", "action=notifyInitAddOtherDevice")
edouard@1586
   751
                        status = notifyInitAddOtherDevice(session, state, partner, NULL);
edouard@1586
   752
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1586
   753
                            return (int) invalid_out_of_memory;
edouard@1586
   754
                        if (status != PEP_STATUS_OK)
edouard@1586
   755
                            return (int) invalid_action;
edouard@1586
   756
                    }
edouard@1586
   757
                    else {
edouard@1586
   758
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=Init", "action=notifyInitMoveOurDevice")
edouard@1586
   759
                        status = notifyInitMoveOurDevice(session, state, partner, NULL);
edouard@1586
   760
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1586
   761
                            return (int) invalid_out_of_memory;
edouard@1586
   762
                        if (status != PEP_STATUS_OK)
edouard@1586
   763
                            return (int) invalid_action;
edouard@1586
   764
                    }
edouard@1467
   765
                    break;
edouard@1467
   766
                }
edouard@1467
   767
                case HandshakeRejected:
edouard@1467
   768
                {
edouard@1490
   769
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingGrouped", "event=HandshakeRejected")
edouard@1490
   770
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeRejected", "action=rejectHandshake")
edouard@1467
   771
                    status = rejectHandshake(session, state, partner, NULL);
edouard@1467
   772
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   773
                        return (int) invalid_out_of_memory;
edouard@1467
   774
                    if (status != PEP_STATUS_OK)
edouard@1467
   775
                        return (int) invalid_action;
edouard@1590
   776
                    assert(session->sync_state_payload);
edouard@1590
   777
                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
edouard@1590
   778
                    free_identity(((HandshakingGrouped_state_payload_t*)session->sync_state_payload)->expected);
edouard@1590
   779
                    free(session->sync_state_payload);
edouard@1590
   780
                    session->sync_state_payload = NULL;
edouard@1490
   781
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeRejected", "target=Grouped")
edouard@1467
   782
                    return Grouped;
edouard@1467
   783
                }
edouard@1467
   784
                case HandshakeAccepted:
edouard@1467
   785
                {
edouard@1490
   786
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingGrouped", "event=HandshakeAccepted")
edouard@1490
   787
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeAccepted", "action=acceptHandshake")
edouard@1467
   788
                    status = acceptHandshake(session, state, partner, NULL);
edouard@1467
   789
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   790
                        return (int) invalid_out_of_memory;
edouard@1467
   791
                    if (status != PEP_STATUS_OK)
edouard@1467
   792
                        return (int) invalid_action;
edouard@1586
   793
                    cond_result = keyElectionWon(session, partner);
edouard@1586
   794
                    #ifndef NDEBUG
edouard@1586
   795
                    char resstr[11] = {0,};
edouard@1586
   796
                    snprintf(resstr,10,"result=%d",cond_result);
edouard@1586
   797
                    #endif
edouard@1586
   798
                    DEBUG_LOG("FSM condition", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeAccepted, condition=keyElectionWon", resstr)
edouard@1586
   799
                    if (cond_result < 0)
edouard@1586
   800
                        return cond_result;
edouard@1586
   801
                    if (cond_result) {
edouard@1586
   802
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeAccepted", "action=sendGroupKeys")
edouard@1586
   803
                        status = sendGroupKeys(session, state, partner, NULL);
edouard@1586
   804
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1586
   805
                            return (int) invalid_out_of_memory;
edouard@1586
   806
                        if (status != PEP_STATUS_OK)
edouard@1586
   807
                            return (int) invalid_action;
edouard@1586
   808
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeAccepted", "action=notifyAcceptedDeviceAdded")
edouard@1586
   809
                        status = notifyAcceptedDeviceAdded(session, state, partner, NULL);
edouard@1586
   810
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1586
   811
                            return (int) invalid_out_of_memory;
edouard@1586
   812
                        if (status != PEP_STATUS_OK)
edouard@1586
   813
                            return (int) invalid_action;
edouard@1590
   814
                        assert(session->sync_state_payload);
edouard@1590
   815
                        if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
edouard@1590
   816
                        free_identity(((HandshakingGrouped_state_payload_t*)session->sync_state_payload)->expected);
edouard@1590
   817
                        free(session->sync_state_payload);
edouard@1590
   818
                        session->sync_state_payload = NULL;
edouard@1586
   819
                        DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeAccepted", "target=Grouped")
edouard@1586
   820
                        return Grouped;
edouard@1586
   821
                    }
edouard@1590
   822
                    assert(session->sync_state_payload);
edouard@1590
   823
                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
edouard@1590
   824
                    free_identity(((HandshakingGrouped_state_payload_t*)session->sync_state_payload)->expected);
edouard@1590
   825
                    free(session->sync_state_payload);
edouard@1590
   826
                    session->sync_state_payload = NULL;
edouard@1590
   827
                    session->sync_state_payload = malloc(sizeof(WaitForGroupKeysGrouped_state_payload_t));
edouard@1590
   828
                    assert(session->sync_state_payload);
edouard@1590
   829
                    if(!session->sync_state_payload) return (DeviceState_state) invalid_out_of_memory;
edouard@1590
   830
                    ((WaitForGroupKeysGrouped_state_payload_t*)session->sync_state_payload)->expected =
edouard@1590
   831
                        identity_dup(partner);
edouard@1586
   832
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeAccepted", "target=WaitForGroupKeysGrouped")
edouard@1586
   833
                    return WaitForGroupKeysGrouped;
edouard@1467
   834
                }
edouard@1574
   835
                case Cancel:
edouard@1574
   836
                {
edouard@1574
   837
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingGrouped", "event=Cancel")
edouard@1590
   838
                    assert(session->sync_state_payload);
edouard@1590
   839
                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
edouard@1590
   840
                    free_identity(((HandshakingGrouped_state_payload_t*)session->sync_state_payload)->expected);
edouard@1590
   841
                    free(session->sync_state_payload);
edouard@1590
   842
                    session->sync_state_payload = NULL;
edouard@1574
   843
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingGrouped, event=Cancel", "target=Grouped")
edouard@1574
   844
                    return Grouped;
edouard@1574
   845
                }
edouard@1467
   846
                case Timeout:
edouard@1467
   847
                {
edouard@1490
   848
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingGrouped", "event=Timeout")
edouard@1490
   849
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=HandshakingGrouped, event=Timeout", "action=notifyTimeout")
edouard@1477
   850
                    status = notifyTimeout(session, state, expected, NULL);
edouard@1467
   851
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1467
   852
                        return (int) invalid_out_of_memory;
edouard@1467
   853
                    if (status != PEP_STATUS_OK)
edouard@1467
   854
                        return (int) invalid_action;
edouard@1590
   855
                    assert(session->sync_state_payload);
edouard@1590
   856
                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
edouard@1590
   857
                    free_identity(((HandshakingGrouped_state_payload_t*)session->sync_state_payload)->expected);
edouard@1590
   858
                    free(session->sync_state_payload);
edouard@1590
   859
                    session->sync_state_payload = NULL;
edouard@1490
   860
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingGrouped, event=Timeout", "target=Grouped")
edouard@1467
   861
                    return Grouped;
edouard@1467
   862
                }
edouard@1467
   863
                default:
edouard@1467
   864
                    return (DeviceState_state) invalid_event;
edouard@1467
   865
            }
edouard@1467
   866
            break;
edouard@1467
   867
        }
edouard@1586
   868
        case WaitForGroupKeysGrouped:
edouard@1586
   869
        {
edouard@1590
   870
            DEBUG_LOG("Entering FSM state", "sync_fsm.c", "state=WaitForGroupKeysGrouped")
edouard@1590
   871
            assert(session->sync_state_payload);
edouard@1590
   872
            if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
edouard@1590
   873
            Identity expected = ((WaitForGroupKeysGrouped_state_payload_t*)session->sync_state_payload)->expected;
edouard@1586
   874
            *timeout = 600;
edouard@1586
   875
            switch (event) {
edouard@1590
   876
                case Init: 
edouard@1590
   877
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForGroupKeysGrouped", "event=Init") 
edouard@1590
   878
                    break;
edouard@1586
   879
                case GroupKeys:
edouard@1586
   880
                {
edouard@1586
   881
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForGroupKeysGrouped", "event=GroupKeys")
edouard@1586
   882
                    cond_result = sameIdentities(session, partner, expected);
edouard@1586
   883
                    #ifndef NDEBUG
edouard@1586
   884
                    char resstr[11] = {0,};
edouard@1586
   885
                    snprintf(resstr,10,"result=%d",cond_result);
edouard@1586
   886
                    #endif
edouard@1586
   887
                    DEBUG_LOG("FSM condition", "sync_fsm.c, state=WaitForGroupKeysGrouped, event=GroupKeys, condition=sameIdentities", resstr)
edouard@1586
   888
                    if (cond_result < 0)
edouard@1586
   889
                        return cond_result;
edouard@1586
   890
                    if (cond_result) {
edouard@1586
   891
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysGrouped, event=GroupKeys", "action=storeGroupKeys")
edouard@1586
   892
                        status = storeGroupKeys(session, state, partner, extra /*keys*/);
edouard@1586
   893
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1586
   894
                            return (int) invalid_out_of_memory;
edouard@1586
   895
                        if (status != PEP_STATUS_OK)
edouard@1586
   896
                            return (int) invalid_action;
edouard@1586
   897
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysGrouped, event=GroupKeys", "action=sendGroupUpdate")
edouard@1586
   898
                        status = sendGroupUpdate(session, state, NULL, NULL);
edouard@1586
   899
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1586
   900
                            return (int) invalid_out_of_memory;
edouard@1586
   901
                        if (status != PEP_STATUS_OK)
edouard@1586
   902
                            return (int) invalid_action;
edouard@1586
   903
                        DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysGrouped, event=GroupKeys", "action=notifyAcceptedDeviceMoved")
edouard@1586
   904
                        status = notifyAcceptedDeviceMoved(session, state, partner, NULL);
edouard@1586
   905
                        if (status == PEP_OUT_OF_MEMORY)
edouard@1586
   906
                            return (int) invalid_out_of_memory;
edouard@1586
   907
                        if (status != PEP_STATUS_OK)
edouard@1586
   908
                            return (int) invalid_action;
edouard@1590
   909
                        assert(session->sync_state_payload);
edouard@1590
   910
                        if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
edouard@1590
   911
                        free_identity(((WaitForGroupKeysGrouped_state_payload_t*)session->sync_state_payload)->expected);
edouard@1590
   912
                        free(session->sync_state_payload);
edouard@1590
   913
                        session->sync_state_payload = NULL;
edouard@1586
   914
                        DEBUG_LOG("FSM transition", "sync_fsm.c, state=WaitForGroupKeysGrouped, event=GroupKeys", "target=Grouped")
edouard@1586
   915
                        return Grouped;
edouard@1586
   916
                    }
edouard@1586
   917
                    break;
edouard@1586
   918
                }
edouard@1586
   919
                case Timeout:
edouard@1586
   920
                {
edouard@1586
   921
                    DEBUG_LOG("FSM event", "sync_fsm.c, state=WaitForGroupKeysGrouped", "event=Timeout")
edouard@1586
   922
                    DEBUG_LOG("FSM action", "sync_fsm.c, state=WaitForGroupKeysGrouped, event=Timeout", "action=notifyTimeout")
edouard@1586
   923
                    status = notifyTimeout(session, state, expected, NULL);
edouard@1586
   924
                    if (status == PEP_OUT_OF_MEMORY)
edouard@1586
   925
                        return (int) invalid_out_of_memory;
edouard@1586
   926
                    if (status != PEP_STATUS_OK)
edouard@1586
   927
                        return (int) invalid_action;
edouard@1590
   928
                    assert(session->sync_state_payload);
edouard@1590
   929
                    if(!session->sync_state_payload) return (DeviceState_state) invalid_state;
edouard@1590
   930
                    free_identity(((WaitForGroupKeysGrouped_state_payload_t*)session->sync_state_payload)->expected);
edouard@1590
   931
                    free(session->sync_state_payload);
edouard@1590
   932
                    session->sync_state_payload = NULL;
edouard@1586
   933
                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=WaitForGroupKeysGrouped, event=Timeout", "target=Grouped")
edouard@1586
   934
                    return Grouped;
edouard@1586
   935
                }
edouard@1586
   936
                default:
edouard@1586
   937
                    return (DeviceState_state) invalid_event;
edouard@1586
   938
            }
edouard@1586
   939
            break;
edouard@1586
   940
        }
edouard@1467
   941
        default:
edouard@1467
   942
            return (DeviceState_state) invalid_state;
edouard@1467
   943
    }
edouard@1467
   944
edouard@1467
   945
    return state;
edouard@1467
   946
}
edouard@1467
   947