adding sending
authorVolker Birk <vb@pep-project.org>
Sun, 29 May 2016 12:21:50 +0200
changeset 6903ae86b68fdfe
parent 689 bce702a2bfa0
child 691 67c247f1da64
adding sending
asn.1/devicegroup.asn1
src/pEpEngine.h
src/pEp_internal.h
src/sync.c
src/sync.h
src/sync_actions.c
src/sync_driver.c
src/sync_fsm.c
src/sync_fsm.h
sync/Makefile
sync/gen_actions_skeleton.ysl2
sync/gen_statemachine.ysl2
     1.1 --- a/asn.1/devicegroup.asn1	Fri May 27 11:46:24 2016 +0200
     1.2 +++ b/asn.1/devicegroup.asn1	Sun May 29 12:21:50 2016 +0200
     1.3 @@ -8,17 +8,20 @@
     1.4  
     1.5  Beacon ::= SEQUENCE {
     1.6      header Header,
     1.7 +    state INTEGER,
     1.8      me Identity
     1.9  }
    1.10  
    1.11  HandshakeRequest ::= SEQUENCE {
    1.12      header Header,
    1.13 +    state INTEGER,
    1.14      me Identity,
    1.15      partner Identity
    1.16  }
    1.17  
    1.18  OwnKeys ::= SEQUENCE {
    1.19      header Header,
    1.20 +    state INTEGER,
    1.21      me Identity,
    1.22      keylist KeyList
    1.23  }
     2.1 --- a/src/pEpEngine.h	Fri May 27 11:46:24 2016 +0200
     2.2 +++ b/src/pEpEngine.h	Sun May 29 12:21:50 2016 +0200
     2.3 @@ -67,6 +67,9 @@
     2.4  
     2.5      PEP_PHRASE_NOT_FOUND                            = 0x0701,
     2.6  
     2.7 +    PEP_SEND_FUNCTION_NOT_REGISTERED                = 0x0801,
     2.8 +    PEP_CANNOT_ENCODE                               = 0x0802,
     2.9 +
    2.10      PEP_COMMIT_FAILED                               = 0xff01,
    2.11  
    2.12      PEP_CANNOT_CREATE_TEMP_FILE                     = -5,
     3.1 --- a/src/pEp_internal.h	Fri May 27 11:46:24 2016 +0200
     3.2 +++ b/src/pEp_internal.h	Sun May 29 12:21:50 2016 +0200
     3.3 @@ -122,6 +122,9 @@
     3.4      messageToSend_t messageToSend;
     3.5      showHandshake_t showHandshake;
     3.6  
     3.7 +    // sync state machine
     3.8 +    DeviceState_state sync_state;
     3.9 +
    3.10      // runtime config
    3.11  
    3.12      bool passive_mode;
     4.1 --- a/src/sync.c	Fri May 27 11:46:24 2016 +0200
     4.2 +++ b/src/sync.c	Sun May 29 12:21:50 2016 +0200
     4.3 @@ -3,8 +3,6 @@
     4.4  #include <memory.h>
     4.5  #include <assert.h>
     4.6  
     4.7 -#include "sync_fsm.h"
     4.8 -
     4.9  
    4.10  DYNAMIC_API PEP_STATUS register_sync_callbacks(
    4.11          PEP_SESSION session,
    4.12 @@ -37,13 +35,13 @@
    4.13  
    4.14      switch (result) {
    4.15          case SYNC_HANDSHAKE_CANCEL:
    4.16 -            fsm_DeviceState_inject(session, Cancel);
    4.17 +            fsm_DeviceState_inject(session, Cancel, NULL, 0);
    4.18              break;
    4.19          case SYNC_HANDSHAKE_ACCEPTED:
    4.20 -            fsm_DeviceState_inject(session, HandshakeAccepted);
    4.21 +            fsm_DeviceState_inject(session, HandshakeAccepted, NULL, 0);
    4.22              break;
    4.23          case SYNC_HANDSHAKE_REJECTED:
    4.24 -            fsm_DeviceState_inject(session, HandshakeRejected);
    4.25 +            fsm_DeviceState_inject(session, HandshakeRejected, NULL, 0);
    4.26              break;
    4.27          default:
    4.28              return PEP_ILLEGAL_VALUE;
     5.1 --- a/src/sync.h	Fri May 27 11:46:24 2016 +0200
     5.2 +++ b/src/sync.h	Sun May 29 12:21:50 2016 +0200
     5.3 @@ -1,6 +1,7 @@
     5.4  #pragma once
     5.5  
     5.6  #include "message.h"
     5.7 +#include "sync_fsm.h"
     5.8  
     5.9  
    5.10  // this module is for being used WITHOUT the Transport API in transport.h
     6.1 --- a/src/sync_actions.c	Fri May 27 11:46:24 2016 +0200
     6.2 +++ b/src/sync_actions.c	Sun May 29 12:21:50 2016 +0200
     6.3 @@ -1,8 +1,11 @@
     6.4  // Actions for DeviceState state machine
     6.5  
     6.6  #include <assert.h>
     6.7 +#include "pEp_internal.h"
     6.8  #include "keymanagement.h"
     6.9 +#include "message.h"
    6.10  #include "sync_fsm.h"
    6.11 +#include "baseprotocol.h"
    6.12  #include "map_asn1.h"
    6.13  #include "../asn.1/Beacon.h"
    6.14  #include "../asn.1/HandshakeRequest.h"
    6.15 @@ -13,21 +16,33 @@
    6.16  //
    6.17  //  params:
    6.18  //      session (in)        session handle
    6.19 +//      state (in)          state the state machine is in
    6.20  //      partner (in)        (must be NULL)
    6.21  //
    6.22  //  returns:
    6.23  //      PEP_STATUS_OK or any other value on error
    6.24  
    6.25 -PEP_STATUS sendBeacon(PEP_SESSION session, const Identity partner)
    6.26 +PEP_STATUS sendBeacon(
    6.27 +        PEP_SESSION session,
    6.28 +        DeviceState_state state,
    6.29 +        const Identity partner
    6.30 +    )
    6.31  {
    6.32      PEP_STATUS status = PEP_STATUS_OK;
    6.33 +    Beacon_t *msg = NULL;
    6.34 +    char *payload = NULL;
    6.35 +    message *_message = NULL;
    6.36  
    6.37      assert(session);
    6.38      assert(!partner);
    6.39      if (!(session && !partner))
    6.40          return PEP_ILLEGAL_VALUE;
    6.41  
    6.42 -    Beacon_t *msg = (Beacon_t *) calloc(1, sizeof(Beacon_t));
    6.43 +    assert(session->messageToSend);
    6.44 +    if (!session->messageToSend)
    6.45 +        return PEP_SEND_FUNCTION_NOT_REGISTERED;
    6.46 +
    6.47 +    msg = (Beacon_t *) calloc(1, sizeof(Beacon_t));
    6.48      assert(msg);
    6.49      if (!msg)
    6.50          goto enomem;
    6.51 @@ -38,6 +53,8 @@
    6.52          goto error;
    6.53      msg->header.sequence = (long) seq;
    6.54  
    6.55 +    msg->state = (long) state;
    6.56 +
    6.57      pEp_identity *me = new_identity(NULL, NULL, NULL, NULL);
    6.58      if (!me)
    6.59          goto enomem;
    6.60 @@ -47,12 +64,31 @@
    6.61      if (Identity_from_Struct(me, &msg->me) == NULL)
    6.62          goto enomem;
    6.63  
    6.64 +    ssize_t size = uper_encode_to_new_buffer(&asn_DEF_HandshakeRequest,
    6.65 +            NULL, msg, (void **) &payload);
    6.66 +    if (size == -1) {
    6.67 +        status = PEP_CANNOT_ENCODE;
    6.68 +        goto error;
    6.69 +    }
    6.70 +
    6.71 +    status = prepare_message(me, partner, payload, size, &_message);
    6.72 +    if (status != PEP_STATUS_OK)
    6.73 +        goto error;
    6.74 +    payload = NULL;
    6.75 +
    6.76 +    status = session->messageToSend(session->sync_obj, _message);
    6.77 +
    6.78 +    free_message(_message);
    6.79 +    ASN_STRUCT_FREE(asn_DEF_Beacon, msg);
    6.80 +
    6.81      return status;
    6.82  
    6.83  enomem:
    6.84      status = PEP_OUT_OF_MEMORY;
    6.85  error:
    6.86      ASN_STRUCT_FREE(asn_DEF_Beacon, msg);
    6.87 +    free(payload);
    6.88 +    free_message(_message);
    6.89      return status;
    6.90  }
    6.91  
    6.92 @@ -61,21 +97,33 @@
    6.93  //
    6.94  //  params:
    6.95  //      session (in)        session handle
    6.96 +//      state (in)          state the state machine is in
    6.97  //      partner (in)        partner in sync
    6.98  //
    6.99  //  returns:
   6.100  //      PEP_STATUS_OK or any other value on error
   6.101  
   6.102 -PEP_STATUS sendHandshakeRequest(PEP_SESSION session, const Identity partner)
   6.103 +PEP_STATUS sendHandshakeRequest(
   6.104 +        PEP_SESSION session,
   6.105 +        DeviceState_state state,
   6.106 +        const Identity partner
   6.107 +    )
   6.108  {
   6.109      PEP_STATUS status = PEP_STATUS_OK;
   6.110 +    HandshakeRequest_t *msg = NULL;
   6.111 +    char *payload = NULL;
   6.112 +    message *_message = NULL;
   6.113  
   6.114      assert(session);
   6.115      assert(partner);
   6.116      if (!(session && partner))
   6.117          return PEP_ILLEGAL_VALUE;
   6.118  
   6.119 -    HandshakeRequest_t *msg = (HandshakeRequest_t *) calloc(1, sizeof(HandshakeRequest_t));
   6.120 +    assert(session->messageToSend);
   6.121 +    if (!session->messageToSend)
   6.122 +        return PEP_SEND_FUNCTION_NOT_REGISTERED;
   6.123 +
   6.124 +    msg = (HandshakeRequest_t *) calloc(1, sizeof(HandshakeRequest_t));
   6.125      assert(msg);
   6.126      if (!msg)
   6.127          goto enomem;
   6.128 @@ -86,6 +134,8 @@
   6.129          goto error;
   6.130      msg->header.sequence = (long) seq;
   6.131  
   6.132 +    msg->state = (long) state;
   6.133 +
   6.134      pEp_identity *me = new_identity(NULL, NULL, NULL, NULL);
   6.135      if (!me)
   6.136          goto enomem;
   6.137 @@ -98,12 +148,31 @@
   6.138      if (Identity_from_Struct(partner, &msg->partner) == NULL)
   6.139          goto enomem;
   6.140  
   6.141 +    ssize_t size = uper_encode_to_new_buffer(&asn_DEF_HandshakeRequest,
   6.142 +            NULL, msg, (void **) &payload);
   6.143 +    if (size == -1) {
   6.144 +        status = PEP_CANNOT_ENCODE;
   6.145 +        goto error;
   6.146 +    }
   6.147 +
   6.148 +    status = prepare_message(me, partner, payload, size, &_message);
   6.149 +    if (status != PEP_STATUS_OK)
   6.150 +        goto error;
   6.151 +    payload = NULL;
   6.152 +
   6.153 +    status = session->messageToSend(session->sync_obj, _message);
   6.154 +
   6.155 +    free_message(_message);
   6.156 +    ASN_STRUCT_FREE(asn_DEF_HandshakeRequest, msg);
   6.157 +
   6.158      return status;
   6.159  
   6.160  enomem:
   6.161      status = PEP_OUT_OF_MEMORY;
   6.162  error:
   6.163      ASN_STRUCT_FREE(asn_DEF_HandshakeRequest, msg);
   6.164 +    free(payload);
   6.165 +    free_message(_message);
   6.166      return status;
   6.167  }
   6.168  
   6.169 @@ -112,12 +181,17 @@
   6.170  //
   6.171  //  params:
   6.172  //      session (in)        session handle
   6.173 +//      state (in)          state the state machine is in
   6.174  //      partner (in)        partner in sync
   6.175  //
   6.176  //  returns:
   6.177  //      PEP_STATUS_OK or any other value on error
   6.178  
   6.179 -PEP_STATUS showHandshake(PEP_SESSION session, const Identity partner)
   6.180 +PEP_STATUS showHandshake(
   6.181 +        PEP_SESSION session,
   6.182 +        DeviceState_state state,
   6.183 +        const Identity partner
   6.184 +    )
   6.185  {
   6.186      PEP_STATUS status = PEP_STATUS_OK;
   6.187  
   6.188 @@ -143,12 +217,17 @@
   6.189  //
   6.190  //  params:
   6.191  //      session (in)        session handle
   6.192 +//      state (in)          state the state machine is in
   6.193  //      partner (in)        partner in sync
   6.194  //
   6.195  //  returns:
   6.196  //      PEP_STATUS_OK or any other value on error
   6.197  
   6.198 -PEP_STATUS reject(PEP_SESSION session, const Identity partner)
   6.199 +PEP_STATUS reject(
   6.200 +        PEP_SESSION session,
   6.201 +        DeviceState_state state,
   6.202 +        const Identity partner
   6.203 +    )
   6.204  {
   6.205      PEP_STATUS status = PEP_STATUS_OK;
   6.206  
   6.207 @@ -174,12 +253,17 @@
   6.208  //
   6.209  //  params:
   6.210  //      session (in)        session handle
   6.211 +//      state (in)          state the state machine is in
   6.212  //      partner (in)        partner in sync
   6.213  //
   6.214  //  returns:
   6.215  //      PEP_STATUS_OK or any other value on error
   6.216  
   6.217 -PEP_STATUS storeGroupKeys(PEP_SESSION session, const Identity partner)
   6.218 +PEP_STATUS storeGroupKeys(
   6.219 +        PEP_SESSION session,
   6.220 +        DeviceState_state state,
   6.221 +        const Identity partner
   6.222 +    )
   6.223  {
   6.224      PEP_STATUS status = PEP_STATUS_OK;
   6.225  
   6.226 @@ -205,21 +289,33 @@
   6.227  //
   6.228  //  params:
   6.229  //      session (in)        session handle
   6.230 +//      state (in)          state the state machine is in
   6.231  //      partner (in)        (must be NULL)
   6.232  //
   6.233  //  returns:
   6.234  //      PEP_STATUS_OK or any other value on error
   6.235  
   6.236 -PEP_STATUS sendOwnKeys(PEP_SESSION session, const Identity partner)
   6.237 +PEP_STATUS sendOwnKeys(
   6.238 +        PEP_SESSION session,
   6.239 +        DeviceState_state state,
   6.240 +        const Identity partner
   6.241 +    )
   6.242  {
   6.243      PEP_STATUS status = PEP_STATUS_OK;
   6.244 +    OwnKeys_t *msg = NULL;
   6.245 +    char *payload = NULL;
   6.246 +    message *_message = NULL;
   6.247  
   6.248      assert(session);
   6.249      assert(!partner);
   6.250      if (!(session && !partner))
   6.251          return PEP_ILLEGAL_VALUE;
   6.252  
   6.253 -    OwnKeys_t *msg = (OwnKeys_t *) calloc(1, sizeof(OwnKeys_t));
   6.254 +    assert(session->messageToSend);
   6.255 +    if (!session->messageToSend)
   6.256 +        return PEP_SEND_FUNCTION_NOT_REGISTERED;
   6.257 +
   6.258 +    msg = (OwnKeys_t *) calloc(1, sizeof(OwnKeys_t));
   6.259      assert(msg);
   6.260      if (!msg)
   6.261          goto enomem;
   6.262 @@ -230,6 +326,8 @@
   6.263          goto error;
   6.264      msg->header.sequence = (long) seq;
   6.265  
   6.266 +    msg->state = (long) state;
   6.267 +
   6.268      pEp_identity *me = new_identity(NULL, NULL, NULL, NULL);
   6.269      if (!me)
   6.270          goto enomem;
   6.271 @@ -246,12 +344,31 @@
   6.272      if (KeyList_from_stringlist(sl, &msg->keylist) == NULL)
   6.273          goto enomem;
   6.274  
   6.275 +    ssize_t size = uper_encode_to_new_buffer(&asn_DEF_HandshakeRequest,
   6.276 +            NULL, msg, (void **) &payload);
   6.277 +    if (size == -1) {
   6.278 +        status = PEP_CANNOT_ENCODE;
   6.279 +        goto error;
   6.280 +    }
   6.281 +
   6.282 +    status = prepare_message(me, partner, payload, size, &_message);
   6.283 +    if (status != PEP_STATUS_OK)
   6.284 +        goto error;
   6.285 +    payload = NULL;
   6.286 +
   6.287 +    status = session->messageToSend(session->sync_obj, _message);
   6.288 +
   6.289 +    free_message(_message);
   6.290 +    ASN_STRUCT_FREE(asn_DEF_OwnKeys, msg);
   6.291 +
   6.292      return status;
   6.293  
   6.294  enomem:
   6.295      status = PEP_OUT_OF_MEMORY;
   6.296  error:
   6.297      ASN_STRUCT_FREE(asn_DEF_OwnKeys, msg);
   6.298 +    free(payload);
   6.299 +    free_message(_message);
   6.300      return status;
   6.301  }
   6.302  
   6.303 @@ -260,12 +377,17 @@
   6.304  //
   6.305  //  params:
   6.306  //      session (in)        session handle
   6.307 +//      state (in)          state the state machine is in
   6.308  //      partner (in)        partner in sync
   6.309  //
   6.310  //  returns:
   6.311  //      PEP_STATUS_OK or any other value on error
   6.312  
   6.313 -PEP_STATUS transmitGroupKeys(PEP_SESSION session, const Identity partner)
   6.314 +PEP_STATUS transmitGroupKeys(
   6.315 +        PEP_SESSION session,
   6.316 +        DeviceState_state state,
   6.317 +        const Identity partner
   6.318 +    )
   6.319  {
   6.320      PEP_STATUS status = PEP_STATUS_OK;
   6.321  
     7.1 --- a/src/sync_driver.c	Fri May 27 11:46:24 2016 +0200
     7.2 +++ b/src/sync_driver.c	Sun May 29 12:21:50 2016 +0200
     7.3 @@ -1,17 +1,22 @@
     7.4  // Driver for DeviceState state machine
     7.5  
     7.6  #include <assert.h>
     7.7 -#include "sync_fsm.h"
     7.8 +#include "pEp_internal.h"
     7.9  
    7.10  
    7.11 -PEP_STATUS fsm_DeviceState_inject(PEP_SESSION session, DeviceState_event event)
    7.12 +PEP_STATUS fsm_DeviceState_inject(
    7.13 +        PEP_SESSION session,
    7.14 +        DeviceState_event event,
    7.15 +        Identity partner,
    7.16 +        DeviceState_state state_partner
    7.17 +    )
    7.18  {
    7.19      PEP_STATUS status = PEP_STATUS_OK;
    7.20  
    7.21 -    static DeviceState_state state = InitState;
    7.22 -    static Identity partner = NULL;
    7.23 +    session->sync_state = InitState;
    7.24 +    session->sync_state = fsm_DeviceState(session, session->sync_state,
    7.25 +            event, partner, state_partner);
    7.26  
    7.27 -    state = fsm_DeviceState(session, state, event, partner);
    7.28      return status;
    7.29  }
    7.30  
     8.1 --- a/src/sync_fsm.c	Fri May 27 11:46:24 2016 +0200
     8.2 +++ b/src/sync_fsm.c	Sun May 29 12:21:50 2016 +0200
     8.3 @@ -6,7 +6,8 @@
     8.4          PEP_SESSION session,
     8.5          DeviceState_state state,
     8.6          DeviceState_event event,
     8.7 -        const Identity partner
     8.8 +        const Identity partner,
     8.9 +        DeviceState_state state_partner
    8.10      )
    8.11  {
    8.12      switch (state) {
    8.13 @@ -22,16 +23,16 @@
    8.14      case Sole:
    8.15          switch (event) {
    8.16              case KeyGen:
    8.17 -                sendBeacon(session, NULL);
    8.18 +                sendBeacon(session, state, NULL);
    8.19                  break;
    8.20              case CannotDecrypt:
    8.21 -                sendBeacon(session, NULL);
    8.22 +                sendBeacon(session, state, NULL);
    8.23                  break;
    8.24              case Beacon:
    8.25 -                sendHandshakeRequest(session, partner);
    8.26 +                sendHandshakeRequest(session, state, partner);
    8.27                  break;
    8.28              case HandshakeRequest:
    8.29 -                sendHandshakeRequest(session, partner);
    8.30 +                sendHandshakeRequest(session, state, partner);
    8.31                  return HandshakingSole;
    8.32          default:
    8.33              return invalid_event;
    8.34 @@ -41,10 +42,10 @@
    8.35      case HandshakingSole:
    8.36          switch (event) {
    8.37              case Init:
    8.38 -                showHandshake(session, partner);
    8.39 +                showHandshake(session, state, partner);
    8.40                  break;
    8.41              case HandshakeRejected:
    8.42 -                reject(session, partner);
    8.43 +                reject(session, state, partner);
    8.44                  return Sole;
    8.45              case HandshakeAccepted:
    8.46                  return WaitForGroupKeys;
    8.47 @@ -56,12 +57,12 @@
    8.48      case WaitForGroupKeys:
    8.49          switch (event) {
    8.50              case ReceiveGroupKeys:
    8.51 -                storeGroupKeys(session, partner);
    8.52 +                storeGroupKeys(session, state, partner);
    8.53                  return Grouped;
    8.54              case Cancel:
    8.55                  return Sole;
    8.56              case Reject:
    8.57 -                reject(session, partner);
    8.58 +                reject(session, state, partner);
    8.59                  return Sole;
    8.60          default:
    8.61              return invalid_event;
    8.62 @@ -71,20 +72,20 @@
    8.63      case Grouped:
    8.64          switch (event) {
    8.65              case KeyGen:
    8.66 -                sendOwnKeys(session, NULL);
    8.67 +                sendOwnKeys(session, state, NULL);
    8.68                  break;
    8.69              case HandshakeRequest:
    8.70 -                sendHandshakeRequest(session, partner);
    8.71 -                showHandshake(session, partner);
    8.72 +                sendHandshakeRequest(session, state, partner);
    8.73 +                showHandshake(session, state, partner);
    8.74                  break;
    8.75              case HandshakeRejected:
    8.76 -                reject(session, partner);
    8.77 +                reject(session, state, partner);
    8.78                  break;
    8.79              case HandshakeAccepted:
    8.80 -                transmitGroupKeys(session, partner);
    8.81 +                transmitGroupKeys(session, state, partner);
    8.82                  break;
    8.83              case Reject:
    8.84 -                reject(session, NULL);
    8.85 +                reject(session, state, NULL);
    8.86                  break;
    8.87          default:
    8.88              return invalid_event;
     9.1 --- a/src/sync_fsm.h	Fri May 27 11:46:24 2016 +0200
     9.2 +++ b/src/sync_fsm.h	Sun May 29 12:21:50 2016 +0200
     9.3 @@ -23,6 +23,7 @@
     9.4  // states
     9.5  
     9.6  typedef enum _DeviceState_state {
     9.7 +    DeviceState_state_NONE = 0,
     9.8      InitState, 
     9.9      Sole, 
    9.10      HandshakingSole, 
    9.11 @@ -33,6 +34,7 @@
    9.12  // events
    9.13  
    9.14  typedef enum _DeviceState_event {
    9.15 +    DeviceState_event_NONE = 0,
    9.16      Init, 
    9.17      KeyGen, 
    9.18      CannotDecrypt, 
    9.19 @@ -47,13 +49,13 @@
    9.20  
    9.21  // actions
    9.22  
    9.23 -PEP_STATUS sendBeacon(PEP_SESSION session, const Identity partner);
    9.24 -PEP_STATUS sendHandshakeRequest(PEP_SESSION session, const Identity partner);
    9.25 -PEP_STATUS showHandshake(PEP_SESSION session, const Identity partner);
    9.26 -PEP_STATUS reject(PEP_SESSION session, const Identity partner);
    9.27 -PEP_STATUS storeGroupKeys(PEP_SESSION session, const Identity partner);
    9.28 -PEP_STATUS sendOwnKeys(PEP_SESSION session, const Identity partner);
    9.29 -PEP_STATUS transmitGroupKeys(PEP_SESSION session, const Identity partner);
    9.30 +PEP_STATUS sendBeacon(PEP_SESSION session, DeviceState_state state, const Identity partner);
    9.31 +PEP_STATUS sendHandshakeRequest(PEP_SESSION session, DeviceState_state state, const Identity partner);
    9.32 +PEP_STATUS showHandshake(PEP_SESSION session, DeviceState_state state, const Identity partner);
    9.33 +PEP_STATUS reject(PEP_SESSION session, DeviceState_state state, const Identity partner);
    9.34 +PEP_STATUS storeGroupKeys(PEP_SESSION session, DeviceState_state state, const Identity partner);
    9.35 +PEP_STATUS sendOwnKeys(PEP_SESSION session, DeviceState_state state, const Identity partner);
    9.36 +PEP_STATUS transmitGroupKeys(PEP_SESSION session, DeviceState_state state, const Identity partner);
    9.37  
    9.38  // state machine
    9.39  
    9.40 @@ -61,12 +63,18 @@
    9.41          PEP_SESSION session,
    9.42          DeviceState_state state,
    9.43          DeviceState_event event,
    9.44 -        const Identity partner
    9.45 +        const Identity partner,
    9.46 +        DeviceState_state state_partner
    9.47      );
    9.48  
    9.49  // driver
    9.50  
    9.51 -PEP_STATUS fsm_DeviceState_inject(PEP_SESSION session, DeviceState_event event);
    9.52 +PEP_STATUS fsm_DeviceState_inject(
    9.53 +        PEP_SESSION session,
    9.54 +        DeviceState_event event,
    9.55 +        Identity partner,
    9.56 +        DeviceState_state state_partner
    9.57 +    );
    9.58  
    9.59  #ifdef __cplusplus
    9.60  }
    10.1 --- a/sync/Makefile	Fri May 27 11:46:24 2016 +0200
    10.2 +++ b/sync/Makefile	Sun May 29 12:21:50 2016 +0200
    10.3 @@ -2,9 +2,9 @@
    10.4  
    10.5  all: ../src/sync_fsm.c
    10.6  
    10.7 -skeleton: ../src/sync_actions.c
    10.8 +skeleton: ../src/sync_actions.c.skeleton
    10.9  
   10.10 -../src/sync_actions.c: sync.fsm gen_actions_skeleton.ysl2 fsm.yml2 functions.ysl2
   10.11 +../src/sync_actions.c.skeleton: sync.fsm gen_actions_skeleton.ysl2 fsm.yml2 functions.ysl2
   10.12  	$(YML2PROC) -y gen_actions_skeleton.ysl2 $< -o $@
   10.13  
   10.14  ../src/sync_fsm.c: sync.fsm gen_statemachine.ysl2 fsm.yml2 functions.ysl2
   10.15 @@ -13,4 +13,4 @@
   10.16  .PHONY: clean
   10.17  
   10.18  clean:
   10.19 -	rm -f *.xml *.xsl ../src/sync_fsm.*
   10.20 +	rm -f *.xml *.xsl ../src/sync_fsm.* ../src/*.skeleton
    11.1 --- a/sync/gen_actions_skeleton.ysl2	Fri May 27 11:46:24 2016 +0200
    11.2 +++ b/sync/gen_actions_skeleton.ysl2	Sun May 29 12:21:50 2016 +0200
    11.3 @@ -10,47 +10,34 @@
    11.4      include ./functions.ysl2
    11.5  
    11.6      template "/protocol/fsm" {
    11.7 -    document "../src/sync_driver.c", "text"
    11.8      ||
    11.9 -    // Driver for «@name» state machine
   11.10 -
   11.11 -    #include <assert.h>
   11.12 -    #include "sync_fsm.h"
   11.13 -
   11.14 -
   11.15 -    PEP_STATUS fsm_«@name»_inject(PEP_SESSION session, «@name»_event event)
   11.16 -    {
   11.17 -        PEP_STATUS status = PEP_STATUS_OK;
   11.18 -
   11.19 -        static «@name»_state state = InitState;
   11.20 -        static Identity partner = NULL;
   11.21 -
   11.22 -        state = fsm_«@name»(session, state, event, partner);
   11.23 -        return status;
   11.24 -    }
   11.25 -
   11.26 -    ||
   11.27 -    ||
   11.28 +    `` const "name", "@name"
   11.29      // Actions for «@name» state machine
   11.30  
   11.31      #include <assert.h>
   11.32 +    #include "pEp_internal.h"
   11.33      #include "keymanagement.h"
   11.34 +    #include "message.h"
   11.35      #include "sync_fsm.h"
   11.36 +    #include "baseprotocol.h"
   11.37      #include "map_asn1.h"
   11.38      `` for "func:distinctName(//action)" if "substring(@name, 1, 4) = 'send'" | #include "../asn.1/«substring(@name, 5, 255)».h"
   11.39  
   11.40 -    `` for "func:distinctName(//action)" call "action" with "action", ".";
   11.41 +    `` for "func:distinctName(//action)" call "action" with "action", ".", with "fsm", "$name";
   11.42  
   11.43      ||
   11.44      }
   11.45  
   11.46      function "action" {
   11.47          param "action";
   11.48 +        param "fsm";
   11.49          choose {
   11.50              when "substring($action/@name, 1, 4) = 'send'"
   11.51 -                call "send_action" with "action", "$action";
   11.52 +                call "send_action" with "action", "$action",
   11.53 +                     with "fsm", "$fsm";
   11.54              otherwise
   11.55 -                call "other_action" with "action", "$action";
   11.56 +                call "other_action" with "action", "$action",
   11.57 +                     with "fsm", "$fsm";
   11.58          }
   11.59      }
   11.60  
   11.61 @@ -75,6 +62,7 @@
   11.62  
   11.63      function "other_action" {
   11.64          param "action";
   11.65 +        param "fsm";
   11.66  
   11.67          ||
   11.68  
   11.69 @@ -82,13 +70,18 @@
   11.70          //
   11.71          //  params:
   11.72          //      session (in)        session handle
   11.73 +        //      state (in)          state the state machine is in
   11.74          `` if "parm"        | //      partner (in)        partner in sync
   11.75          `` if "not(parm)"   | //      partner (in)        (must be NULL)
   11.76          //
   11.77          //  returns:
   11.78          //      PEP_STATUS_OK or any other value on error
   11.79  
   11.80 -        PEP_STATUS «$action/@name»(PEP_SESSION session, const Identity partner)
   11.81 +        PEP_STATUS «$action/@name»(
   11.82 +                PEP_SESSION session,
   11.83 +                «$fsm»_state state,
   11.84 +                const Identity partner
   11.85 +            )
   11.86          {
   11.87              PEP_STATUS status = PEP_STATUS_OK;
   11.88  
   11.89 @@ -111,6 +104,7 @@
   11.90  
   11.91      function "send_action" {
   11.92          param "action";
   11.93 +        param "fsm";
   11.94          const "name", "substring($action/@name, 5, 255)";
   11.95  
   11.96          ||
   11.97 @@ -119,19 +113,31 @@
   11.98          //
   11.99          //  params:
  11.100          //      session (in)        session handle
  11.101 +        //      state (in)          state the state machine is in
  11.102          `` if "parm"        | //      partner (in)        partner in sync
  11.103          `` if "not(parm)"   | //      partner (in)        (must be NULL)
  11.104          //
  11.105          //  returns:
  11.106          //      PEP_STATUS_OK or any other value on error
  11.107  
  11.108 -        PEP_STATUS «$action/@name»(PEP_SESSION session, const Identity partner)
  11.109 +        PEP_STATUS «$action/@name»(
  11.110 +                PEP_SESSION session,
  11.111 +                «$fsm»_state state,
  11.112 +                const Identity partner
  11.113 +            )
  11.114          {
  11.115              PEP_STATUS status = PEP_STATUS_OK;
  11.116 +            «$name»_t *msg = NULL;
  11.117 +            char *payload = NULL;
  11.118 +            message *_message = NULL;
  11.119  
  11.120              `` call "paramcheck" with "partner", "parm/partner";
  11.121  
  11.122 -            «$name»_t *msg = («$name»_t *) calloc(1, sizeof(«$name»_t));
  11.123 +            assert(session->messageToSend);
  11.124 +            if (!session->messageToSend)
  11.125 +                return PEP_SEND_FUNCTION_NOT_REGISTERED;
  11.126 +
  11.127 +            msg = («$name»_t *) calloc(1, sizeof(«$name»_t));
  11.128              assert(msg);
  11.129              if (!msg)
  11.130                  goto enomem;
  11.131 @@ -142,6 +148,8 @@
  11.132                  goto error;
  11.133              msg->header.sequence = (long) seq;
  11.134  
  11.135 +            msg->state = (long) state;
  11.136 +
  11.137              pEp_identity *me = new_identity(NULL, NULL, NULL, NULL);
  11.138              if (!me)
  11.139                  goto enomem;
  11.140 @@ -160,12 +168,31 @@
  11.141           `` if "$name='OwnKeys'"|> if (KeyList_from_stringlist(sl, &msg->keylist) == NULL)
  11.142           `` if "$name='OwnKeys'"|>> goto enomem;
  11.143  
  11.144 +            ssize_t size = uper_encode_to_new_buffer(&asn_DEF_HandshakeRequest,
  11.145 +                    NULL, msg, (void **) &payload);
  11.146 +            if (size == -1) {
  11.147 +                status = PEP_CANNOT_ENCODE;
  11.148 +                goto error;
  11.149 +            }
  11.150 +
  11.151 +            status = prepare_message(me, partner, payload, size, &_message);
  11.152 +            if (status != PEP_STATUS_OK)
  11.153 +                goto error;
  11.154 +            payload = NULL;
  11.155 +
  11.156 +            status = session->messageToSend(session->sync_obj, _message);
  11.157 +
  11.158 +            free_message(_message);
  11.159 +            ASN_STRUCT_FREE(asn_DEF_«$name», msg);
  11.160 +
  11.161              return status;
  11.162  
  11.163          enomem:
  11.164              status = PEP_OUT_OF_MEMORY;
  11.165          error:
  11.166              ASN_STRUCT_FREE(asn_DEF_«$name», msg);
  11.167 +            free(payload);
  11.168 +            free_message(_message);
  11.169              return status;
  11.170          }
  11.171  
    12.1 --- a/sync/gen_statemachine.ysl2	Fri May 27 11:46:24 2016 +0200
    12.2 +++ b/sync/gen_statemachine.ysl2	Sun May 29 12:21:50 2016 +0200
    12.3 @@ -37,18 +37,21 @@
    12.4          // states
    12.5  
    12.6          typedef enum _«@name»_state {
    12.7 +            «@name»_state_NONE = 0,
    12.8          `` for "func:distinctName(state)" |> «@name»`if "position()!=last()" > , `
    12.9          } «@name»_state;
   12.10  
   12.11          // events
   12.12  
   12.13          typedef enum _«@name»_event {
   12.14 +            «@name»_event_NONE = 0,
   12.15          `` for "func:distinctName(state/event)" |> «@name»`if "position()!=last()" > , `
   12.16          } «@name»_event;
   12.17  
   12.18          // actions
   12.19  
   12.20 -        `` for "func:distinctName(//action)" | PEP_STATUS «@name»(PEP_SESSION session, const Identity partner);
   12.21 +        `` const "name", "@name"
   12.22 +        `` for "func:distinctName(//action)" | PEP_STATUS «@name»(PEP_SESSION session, «$name»_state state, const Identity partner);
   12.23  
   12.24          // state machine
   12.25  
   12.26 @@ -56,18 +59,49 @@
   12.27                  PEP_SESSION session,
   12.28                  «@name»_state state,
   12.29                  «@name»_event event,
   12.30 -                const Identity partner
   12.31 +                const Identity partner,
   12.32 +                «@name»_state state_partner
   12.33              );
   12.34  
   12.35          // driver
   12.36  
   12.37 -        PEP_STATUS fsm_«@name»_inject(PEP_SESSION session, «@name»_event event);
   12.38 +        PEP_STATUS fsm_«@name»_inject(
   12.39 +                PEP_SESSION session,
   12.40 +                «@name»_event event,
   12.41 +                Identity partner,
   12.42 +                «@name»_state state_partner
   12.43 +            );
   12.44  
   12.45          #ifdef __cplusplus
   12.46          }
   12.47          #endif
   12.48  
   12.49          ||
   12.50 +        document "../src/sync_driver.c", "text"
   12.51 +        ||
   12.52 +        // Driver for «@name» state machine
   12.53 +
   12.54 +        #include <assert.h>
   12.55 +        #include "pEp_internal.h"
   12.56 +
   12.57 +
   12.58 +        PEP_STATUS fsm_«@name»_inject(
   12.59 +                PEP_SESSION session,
   12.60 +                «@name»_event event,
   12.61 +                Identity partner,
   12.62 +                «@name»_state state_partner
   12.63 +            )
   12.64 +        {
   12.65 +            PEP_STATUS status = PEP_STATUS_OK;
   12.66 +
   12.67 +            session->sync_state = InitState;
   12.68 +            session->sync_state = fsm_«@name»(session, session->sync_state,
   12.69 +                    event, partner, state_partner);
   12.70 +
   12.71 +            return status;
   12.72 +        }
   12.73 +
   12.74 +        ||
   12.75          ||
   12.76          #include "sync_fsm.h"
   12.77  
   12.78 @@ -77,7 +111,8 @@
   12.79                  PEP_SESSION session,
   12.80                  «@name»_state state,
   12.81                  «@name»_event event,
   12.82 -                const Identity partner
   12.83 +                const Identity partner,
   12.84 +                «@name»_state state_partner
   12.85              )
   12.86          {
   12.87              switch (state) {
   12.88 @@ -113,7 +148,7 @@
   12.89  
   12.90      template "action" {
   12.91          indent(0);
   12.92 -        > «@name»(session, 
   12.93 +        > «@name»(session, state, 
   12.94          choose {
   12.95              when "parm" > «name(parm/*)»
   12.96              otherwise > NULL