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