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