src/sync_fsm.c
author Volker Birk <vb@pep.foundation>
Tue, 02 Aug 2016 12:06:45 +0200
branchkeysync
changeset 964 ef02296c41ca
parent 959 c1c463ecf30c
child 1043 f786ee22e281
permissions -rw-r--r--
store group keys
vb@616
     1
#include "sync_fsm.h"
vb@616
     2
vb@616
     3
// state machine for DeviceState
vb@616
     4
vb@616
     5
DeviceState_state fsm_DeviceState(
vb@627
     6
        PEP_SESSION session,
vb@616
     7
        DeviceState_state state,
vb@616
     8
        DeviceState_event event,
vb@939
     9
        Identity partner,
vb@951
    10
        void *extra
vb@616
    11
    )
vb@616
    12
{
vb@964
    13
    int cond_result;
vb@959
    14
    PEP_STATUS status = PEP_STATUS_OK;
vb@959
    15
vb@616
    16
    switch (state) {
vb@951
    17
        case InitState:
vb@951
    18
            switch (event) {
vb@951
    19
                case Init:
vb@959
    20
                    cond_result = storedGroupKeys(session);
vb@964
    21
                    if (cond_result < 0)
vb@964
    22
                        return cond_result;
vb@959
    23
                    if (cond_result) {
vb@951
    24
                        return Grouped;
vb@951
    25
                    }
vb@951
    26
                    return Sole;
vb@951
    27
                default:
vb@951
    28
                    return (DeviceState_state) invalid_event;
vb@951
    29
            }
vb@951
    30
            break;
vb@951
    31
        
vb@951
    32
        case Sole:
vb@951
    33
            switch (event) {
vb@951
    34
                case KeyGen:
vb@959
    35
                    status = sendBeacon(session, state, NULL, NULL);
vb@964
    36
                    if (status == PEP_OUT_OF_MEMORY)
vb@964
    37
                        return invalid_out_of_memory;
vb@959
    38
                    if (status != PEP_STATUS_OK)
vb@959
    39
                        return invalid_action;
vb@951
    40
                    break;
vb@951
    41
                case CannotDecrypt:
vb@959
    42
                    status = sendBeacon(session, state, NULL, NULL);
vb@964
    43
                    if (status == PEP_OUT_OF_MEMORY)
vb@964
    44
                        return invalid_out_of_memory;
vb@959
    45
                    if (status != PEP_STATUS_OK)
vb@959
    46
                        return invalid_action;
vb@951
    47
                    break;
vb@951
    48
                case Beacon:
vb@959
    49
                    status = sendHandshakeRequest(session, state, partner, NULL);
vb@964
    50
                    if (status == PEP_OUT_OF_MEMORY)
vb@964
    51
                        return invalid_out_of_memory;
vb@959
    52
                    if (status != PEP_STATUS_OK)
vb@959
    53
                        return invalid_action;
vb@951
    54
                    break;
vb@951
    55
                case HandshakeRequest:
vb@959
    56
                    status = sendHandshakeRequest(session, state, partner, NULL);
vb@964
    57
                    if (status == PEP_OUT_OF_MEMORY)
vb@964
    58
                        return invalid_out_of_memory;
vb@959
    59
                    if (status != PEP_STATUS_OK)
vb@959
    60
                        return invalid_action;
vb@951
    61
                    return HandshakingSole;
vb@951
    62
                default:
vb@951
    63
                    return (DeviceState_state) invalid_event;
vb@951
    64
            }
vb@951
    65
            break;
vb@951
    66
        
vb@951
    67
        case HandshakingSole:
vb@951
    68
            switch (event) {
vb@951
    69
                case Init:
vb@959
    70
                    status = showHandshake(session, state, partner, NULL);
vb@964
    71
                    if (status == PEP_OUT_OF_MEMORY)
vb@964
    72
                        return invalid_out_of_memory;
vb@959
    73
                    if (status != PEP_STATUS_OK)
vb@959
    74
                        return invalid_action;
vb@951
    75
                    break;
vb@951
    76
                case HandshakeRejected:
vb@959
    77
                    status = reject(session, state, partner, NULL);
vb@964
    78
                    if (status == PEP_OUT_OF_MEMORY)
vb@964
    79
                        return invalid_out_of_memory;
vb@959
    80
                    if (status != PEP_STATUS_OK)
vb@959
    81
                        return invalid_action;
vb@951
    82
                    return Sole;
vb@951
    83
                case HandshakeAccepted:
vb@959
    84
                    cond_result = keyElectionWon(session, partner);
vb@964
    85
                    if (cond_result < 0)
vb@964
    86
                        return cond_result;
vb@959
    87
                    if (cond_result) {
vb@951
    88
                        return Grouped;
vb@951
    89
                    }
vb@951
    90
                    return WaitForGroupKeys;
vb@951
    91
                default:
vb@951
    92
                    return (DeviceState_state) invalid_event;
vb@951
    93
            }
vb@951
    94
            break;
vb@951
    95
        
vb@951
    96
        case WaitForGroupKeys:
vb@951
    97
            switch (event) {
vb@951
    98
                case GroupKeys:
vb@959
    99
                    status = storeGroupKeys(session, state, partner, NULL);
vb@964
   100
                    if (status == PEP_OUT_OF_MEMORY)
vb@964
   101
                        return invalid_out_of_memory;
vb@959
   102
                    if (status != PEP_STATUS_OK)
vb@959
   103
                        return invalid_action;
vb@951
   104
                    return Grouped;
vb@951
   105
                case Cancel:
vb@951
   106
                    return Sole;
vb@951
   107
                case Reject:
vb@959
   108
                    status = reject(session, state, partner, NULL);
vb@964
   109
                    if (status == PEP_OUT_OF_MEMORY)
vb@964
   110
                        return invalid_out_of_memory;
vb@959
   111
                    if (status != PEP_STATUS_OK)
vb@959
   112
                        return invalid_action;
vb@951
   113
                    return Sole;
vb@951
   114
                default:
vb@951
   115
                    return (DeviceState_state) invalid_event;
vb@951
   116
            }
vb@951
   117
            break;
vb@951
   118
        
vb@951
   119
        case Grouped:
vb@951
   120
            switch (event) {
vb@951
   121
                case KeyGen:
vb@959
   122
                    status = sendGroupKeys(session, state, NULL, NULL);
vb@964
   123
                    if (status == PEP_OUT_OF_MEMORY)
vb@964
   124
                        return invalid_out_of_memory;
vb@959
   125
                    if (status != PEP_STATUS_OK)
vb@959
   126
                        return invalid_action;
vb@951
   127
                    break;
vb@951
   128
                case HandshakeRequest:
vb@959
   129
                    status = sendHandshakeRequest(session, state, partner, NULL);
vb@964
   130
                    if (status == PEP_OUT_OF_MEMORY)
vb@964
   131
                        return invalid_out_of_memory;
vb@959
   132
                    if (status != PEP_STATUS_OK)
vb@959
   133
                        return invalid_action;
vb@959
   134
                    status = showHandshake(session, state, partner, NULL);
vb@964
   135
                    if (status == PEP_OUT_OF_MEMORY)
vb@964
   136
                        return invalid_out_of_memory;
vb@959
   137
                    if (status != PEP_STATUS_OK)
vb@959
   138
                        return invalid_action;
vb@951
   139
                    break;
vb@951
   140
                case HandshakeRejected:
vb@959
   141
                    status = reject(session, state, partner, NULL);
vb@964
   142
                    if (status == PEP_OUT_OF_MEMORY)
vb@964
   143
                        return invalid_out_of_memory;
vb@959
   144
                    if (status != PEP_STATUS_OK)
vb@959
   145
                        return invalid_action;
vb@951
   146
                    break;
vb@951
   147
                case Hand:
vb@951
   148
                    break;
vb@951
   149
                case Reject:
vb@959
   150
                    status = reject(session, state, NULL, NULL);
vb@964
   151
                    if (status == PEP_OUT_OF_MEMORY)
vb@964
   152
                        return invalid_out_of_memory;
vb@959
   153
                    if (status != PEP_STATUS_OK)
vb@959
   154
                        return invalid_action;
vb@951
   155
                    break;
vb@951
   156
                default:
vb@951
   157
                    return (DeviceState_state) invalid_event;
vb@951
   158
            }
vb@951
   159
            break;
vb@951
   160
        
vb@616
   161
        default:
vb@743
   162
            return (DeviceState_state) invalid_state;
vb@616
   163
    }
vb@616
   164
vb@616
   165
    return state;
vb@616
   166
}
vb@807
   167