KeySync : reworked some SQL statements and condition checking if a device is grouped.
authorEdouard Tisserant <edouard@pep-project.org>
Sun, 12 Feb 2017 22:45:02 +0100
changeset 157412b7a45140d3
parent 1573 e3c1acfac537
child 1575 ae0c4b0953a5
KeySync : reworked some SQL statements and condition checking if a device is grouped.
src/pEpEngine.c
src/sync_actions.c
src/sync_impl.c
sync/devicegroup.fsm
sync/generated/sync_fsm.c
sync/generated/sync_fsm.h
     1.1 --- a/src/pEpEngine.c	Wed Feb 08 14:24:12 2017 +0100
     1.2 +++ b/src/pEpEngine.c	Sun Feb 12 22:45:02 2017 +0100
     1.3 @@ -42,8 +42,6 @@
     1.4      "update person set device_group = ?1 "
     1.5      "where id = '" PEP_OWN_USERID "';";
     1.6  
     1.7 -// TODO leave group
     1.8 -
     1.9  static const char *sql_get_device_group = 
    1.10      "select device_group from person "
    1.11      "where id = '" PEP_OWN_USERID "';";
    1.12 @@ -1199,14 +1197,18 @@
    1.13      int result;
    1.14  
    1.15      assert(session);
    1.16 -    assert(group_name);
    1.17  
    1.18      if (!(session && group_name))
    1.19          return PEP_ILLEGAL_VALUE;
    1.20  
    1.21      sqlite3_reset(session->set_device_group);
    1.22 -    sqlite3_bind_text(session->set_device_group, 1, group_name, -1,
    1.23 -            SQLITE_STATIC);
    1.24 +    if(group_name){
    1.25 +        sqlite3_bind_text(session->set_device_group, 1, group_name, -1,
    1.26 +                SQLITE_STATIC);
    1.27 +    } else {
    1.28 +        sqlite3_bind_null(session->set_device_group, 1);
    1.29 +    }
    1.30 +
    1.31      result = sqlite3_step(session->set_device_group);
    1.32      sqlite3_reset(session->set_device_group);
    1.33      if (result != SQLITE_DONE)
    1.34 @@ -1231,10 +1233,12 @@
    1.35      result = sqlite3_step(session->get_device_group);
    1.36      switch (result) {
    1.37      case SQLITE_ROW: {
    1.38 -        *group_name = strdup(
    1.39 -            (const char *) sqlite3_column_text(session->get_device_group, 0));
    1.40 -            if(*group_name == NULL)
    1.41 -                status = PEP_OUT_OF_MEMORY;
    1.42 +        const char *_group_name = (const char *)sqlite3_column_text(session->get_device_group, 0);
    1.43 +        if(_group_name){
    1.44 +            *group_name = strdup(_group_name);
    1.45 +                if(*group_name == NULL)
    1.46 +                    status = PEP_OUT_OF_MEMORY;
    1.47 +        }
    1.48          break;
    1.49      }
    1.50   
     2.1 --- a/src/sync_actions.c	Wed Feb 08 14:24:12 2017 +0100
     2.2 +++ b/src/sync_actions.c	Sun Feb 12 22:45:02 2017 +0100
     2.3 @@ -12,46 +12,24 @@
     2.4  
     2.5  // conditions
     2.6  
     2.7 -// TODO : move that SQL statement with other prepared SQL statements in pEpEngine.c
     2.8 -static const char *sql_stored_group_keys =
     2.9 -        "select count(device_group) from person where id = '" PEP_OWN_USERID "';"; 
    2.10 -
    2.11 -// TODO  is that necessary ?
    2.12 -static int _stored_group_keys(void *_gc, int count, char **text, char **name)
    2.13 -{
    2.14 -    assert(_gc);
    2.15 -    assert(count == 1);
    2.16 -    assert(text && text[0]);
    2.17 -    if (!(_gc && count == 1 && text && text[0]))
    2.18 -        return -1;
    2.19 -
    2.20 -    bool *gc = (bool *) _gc;
    2.21 -    *gc = atoi(text[0]) != 0;
    2.22 -    return 0;
    2.23 -}
    2.24 -
    2.25 -int storedGroupKeys(PEP_SESSION session)
    2.26 +int deviceGrouped(PEP_SESSION session)
    2.27  {
    2.28      assert(session);
    2.29      if (!session)
    2.30          return invalid_condition; // error
    2.31  
    2.32 -    bool gc = false;
    2.33 -    int int_result = sqlite3_exec(
    2.34 -        session->db,
    2.35 -        sql_stored_group_keys,
    2.36 -        _stored_group_keys,
    2.37 -        &gc,
    2.38 -        NULL
    2.39 -    );
    2.40 -    assert(int_result == SQLITE_OK);
    2.41 -    if (int_result != SQLITE_OK)
    2.42 -        return invalid_condition; // error
    2.43 +    char *devgrp = NULL;
    2.44 +    int res = 0;
    2.45 +    PEP_STATUS status;
    2.46  
    2.47 -    if (gc)
    2.48 -        return 1;
    2.49 -    else
    2.50 -        return 0;
    2.51 +    status = get_device_group(session, &devgrp);
    2.52 +
    2.53 +    if (status == PEP_STATUS_OK && devgrp && devgrp[0])
    2.54 +        res = 1;
    2.55 +
    2.56 +    free(devgrp);
    2.57 +
    2.58 +    return res;
    2.59  }
    2.60  
    2.61  int keyElectionWon(PEP_SESSION session, Identity partner)
    2.62 @@ -63,7 +41,7 @@
    2.63  
    2.64      // an already existing group always wins
    2.65  
    2.66 -    if (storedGroupKeys(session)) {
    2.67 +    if (deviceGrouped(session)) {
    2.68          assert(!(partner->flags & PEP_idf_devicegroup));
    2.69          return 1;
    2.70      }
    2.71 @@ -317,3 +295,30 @@
    2.72      
    2.73      return status;
    2.74  }
    2.75 +
    2.76 +// leaveGroup() - 
    2.77 +//
    2.78 +//  params:
    2.79 +//      session (in)        session handle
    2.80 +//      state (in)          state the state machine is in
    2.81 +//      partner (in)        ignored
    2.82 +//      extra (in)          ignored
    2.83 +//
    2.84 +//  returns:
    2.85 +//      PEP_STATUS_OK or any other value on error
    2.86 +
    2.87 +PEP_STATUS leaveGroup(
    2.88 +        PEP_SESSION session,
    2.89 +        DeviceState_state state,
    2.90 +        Identity partner,
    2.91 +        void *extra
    2.92 +    )
    2.93 +{
    2.94 +    PEP_STATUS status = PEP_STATUS_OK;
    2.95 +
    2.96 +    assert(session);
    2.97 +
    2.98 +    status = set_device_group(session, NULL);
    2.99 +    
   2.100 +    return status;
   2.101 +}
     3.1 --- a/src/sync_impl.c	Wed Feb 08 14:24:12 2017 +0100
     3.2 +++ b/src/sync_impl.c	Sun Feb 12 22:45:02 2017 +0100
     3.3 @@ -603,7 +603,7 @@
     3.4  
     3.5      msg->header.state = (long) state;
     3.6  
     3.7 -    bool devicegroup = storedGroupKeys(session);
     3.8 +    bool devicegroup = deviceGrouped(session);
     3.9      if (devicegroup)
    3.10          msg->header.devicegroup = 1;
    3.11      else
     4.1 --- a/sync/devicegroup.fsm	Wed Feb 08 14:24:12 2017 +0100
     4.2 +++ b/sync/devicegroup.fsm	Sun Feb 12 22:45:02 2017 +0100
     4.3 @@ -18,13 +18,13 @@
     4.4      unencrypted sendBeacon;
     4.5  
     4.6      fsm DeviceState filename=sync {
     4.7 -        condition storedGroupKeys();
     4.8 +        condition deviceGrouped();
     4.9          condition keyElectionWon(Identity partner);
    4.10          condition sameIdentities(Identity a, Identity b);
    4.11  
    4.12          state InitState {
    4.13              on Init {
    4.14 -                if storedGroupKeys()
    4.15 +                if deviceGrouped()
    4.16                      go Grouped;
    4.17                  go Sole;
    4.18              }
    4.19 @@ -77,7 +77,6 @@
    4.20                      do notifyInitAddOurDevice(partner);
    4.21                  }
    4.22              }
    4.23 -
    4.24              on HandshakeRejected(Identity partner) {
    4.25                  do rejectHandshake(partner);
    4.26                  go Sole;
    4.27 @@ -174,6 +173,7 @@
    4.28  
    4.29                  go Grouped;
    4.30              }
    4.31 +            on Cancel go Grouped;
    4.32              on Timeout {
    4.33                  do notifyTimeout(expected);
    4.34                  go Grouped;
     5.1 --- a/sync/generated/sync_fsm.c	Wed Feb 08 14:24:12 2017 +0100
     5.2 +++ b/sync/generated/sync_fsm.c	Sun Feb 12 22:45:02 2017 +0100
     5.3 @@ -24,12 +24,12 @@
     5.4                  case Init:
     5.5                  {
     5.6                      DEBUG_LOG("FSM event", "sync_fsm.c, state=InitState", "event=Init")
     5.7 -                    cond_result = storedGroupKeys(session);
     5.8 +                    cond_result = deviceGrouped(session);
     5.9                      #ifndef NDEBUG
    5.10                      char resstr[11] = {0,};
    5.11                      snprintf(resstr,10,"result=%d",cond_result);
    5.12                      #endif
    5.13 -                    DEBUG_LOG("FSM condition", "sync_fsm.c, state=InitState, event=Init, condition=storedGroupKeys", resstr)
    5.14 +                    DEBUG_LOG("FSM condition", "sync_fsm.c, state=InitState, event=Init, condition=deviceGrouped", resstr)
    5.15                      if (cond_result < 0)
    5.16                          return cond_result;
    5.17                      if (cond_result) {
    5.18 @@ -663,6 +663,16 @@
    5.19                      DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeAccepted", "target=Grouped")
    5.20                      return Grouped;
    5.21                  }
    5.22 +                case Cancel:
    5.23 +                {
    5.24 +                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingGrouped", "event=Cancel")
    5.25 +                    if(session->sync_state_payload){
    5.26 +                        free_identity((Identity)session->sync_state_payload);
    5.27 +                        session->sync_state_payload = NULL;
    5.28 +                    }
    5.29 +                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingGrouped, event=Cancel", "target=Grouped")
    5.30 +                    return Grouped;
    5.31 +                }
    5.32                  case Timeout:
    5.33                  {
    5.34                      DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingGrouped", "event=Timeout")
     6.1 --- a/sync/generated/sync_fsm.h	Wed Feb 08 14:24:12 2017 +0100
     6.2 +++ b/sync/generated/sync_fsm.h	Sun Feb 12 22:45:02 2017 +0100
     6.3 @@ -30,7 +30,7 @@
     6.4  
     6.5  // conditions
     6.6  
     6.7 -int storedGroupKeys(PEP_SESSION session);
     6.8 +int deviceGrouped(PEP_SESSION session);
     6.9  int keyElectionWon(PEP_SESSION session, Identity partner);
    6.10  int sameIdentities(PEP_SESSION session, Identity a, Identity b);
    6.11