merge
authorEdouard Tisserant
Sun, 29 May 2016 19:02:14 +0200
changeset 6960b3ad1c40304
parent 695 b8d403779089
parent 692 6f7f1b7627bc
child 698 870bbc6ea19c
merge
src/pEpEngine.h
src/pEp_internal.h
     1.1 --- a/asn.1/devicegroup.asn1	Sun May 29 00:11:56 2016 +0200
     1.2 +++ b/asn.1/devicegroup.asn1	Sun May 29 19:02:14 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	Sun May 29 00:11:56 2016 +0200
     2.2 +++ b/src/pEpEngine.h	Sun May 29 19:02:14 2016 +0200
     2.3 @@ -67,6 +67,10 @@
     2.4  
     2.5      PEP_PHRASE_NOT_FOUND                            = 0x0701,
     2.6  
     2.7 +    PEP_SEND_FUNCTION_NOT_REGISTERED                = 0x0801,
     2.8 +    PEP_CONTRAINTS_VIOLATED                         = 0x0802,
     2.9 +    PEP_CANNOT_ENCODE                               = 0x0803,
    2.10 +
    2.11      PEP_COMMIT_FAILED                               = 0xff01,
    2.12  
    2.13      PEP_CANNOT_CREATE_TEMP_FILE                     = -5,
     3.1 --- a/src/pEp_internal.h	Sun May 29 00:11:56 2016 +0200
     3.2 +++ b/src/pEp_internal.h	Sun May 29 19:02:14 2016 +0200
     3.3 @@ -126,6 +126,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	Sun May 29 00:11:56 2016 +0200
     4.2 +++ b/src/sync.c	Sun May 29 19:02:14 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	Sun May 29 00:11:56 2016 +0200
     5.2 +++ b/src/sync.h	Sun May 29 19:02:14 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	Sun May 29 00:11:56 2016 +0200
     6.2 +++ b/src/sync_actions.c	Sun May 29 19:02:14 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,36 @@
    6.61      if (Identity_from_Struct(me, &msg->me) == NULL)
    6.62          goto enomem;
    6.63  
    6.64 +    if (asn_check_constraints(&asn_DEF_Beacon, msg, NULL, NULL)) {
    6.65 +        status = PEP_CONTRAINTS_VIOLATED;
    6.66 +        goto error;
    6.67 +    }
    6.68 +
    6.69 +    ssize_t size = uper_encode_to_new_buffer(&asn_DEF_Beacon,
    6.70 +            NULL, msg, (void **) &payload);
    6.71 +    if (size == -1) {
    6.72 +        status = PEP_CANNOT_ENCODE;
    6.73 +        goto error;
    6.74 +    }
    6.75 +
    6.76 +    status = prepare_message(me, partner, payload, size, &_message);
    6.77 +    if (status != PEP_STATUS_OK)
    6.78 +        goto error;
    6.79 +    payload = NULL;
    6.80 +
    6.81 +    status = session->messageToSend(session->sync_obj, _message);
    6.82 +
    6.83 +    free_message(_message);
    6.84 +    ASN_STRUCT_FREE(asn_DEF_Beacon, msg);
    6.85 +
    6.86      return status;
    6.87  
    6.88  enomem:
    6.89      status = PEP_OUT_OF_MEMORY;
    6.90  error:
    6.91      ASN_STRUCT_FREE(asn_DEF_Beacon, msg);
    6.92 +    free(payload);
    6.93 +    free_message(_message);
    6.94      return status;
    6.95  }
    6.96  
    6.97 @@ -61,21 +102,33 @@
    6.98  //
    6.99  //  params:
   6.100  //      session (in)        session handle
   6.101 +//      state (in)          state the state machine is in
   6.102  //      partner (in)        partner in sync
   6.103  //
   6.104  //  returns:
   6.105  //      PEP_STATUS_OK or any other value on error
   6.106  
   6.107 -PEP_STATUS sendHandshakeRequest(PEP_SESSION session, const Identity partner)
   6.108 +PEP_STATUS sendHandshakeRequest(
   6.109 +        PEP_SESSION session,
   6.110 +        DeviceState_state state,
   6.111 +        const Identity partner
   6.112 +    )
   6.113  {
   6.114      PEP_STATUS status = PEP_STATUS_OK;
   6.115 +    HandshakeRequest_t *msg = NULL;
   6.116 +    char *payload = NULL;
   6.117 +    message *_message = NULL;
   6.118  
   6.119      assert(session);
   6.120      assert(partner);
   6.121      if (!(session && partner))
   6.122          return PEP_ILLEGAL_VALUE;
   6.123  
   6.124 -    HandshakeRequest_t *msg = (HandshakeRequest_t *) calloc(1, sizeof(HandshakeRequest_t));
   6.125 +    assert(session->messageToSend);
   6.126 +    if (!session->messageToSend)
   6.127 +        return PEP_SEND_FUNCTION_NOT_REGISTERED;
   6.128 +
   6.129 +    msg = (HandshakeRequest_t *) calloc(1, sizeof(HandshakeRequest_t));
   6.130      assert(msg);
   6.131      if (!msg)
   6.132          goto enomem;
   6.133 @@ -86,6 +139,8 @@
   6.134          goto error;
   6.135      msg->header.sequence = (long) seq;
   6.136  
   6.137 +    msg->state = (long) state;
   6.138 +
   6.139      pEp_identity *me = new_identity(NULL, NULL, NULL, NULL);
   6.140      if (!me)
   6.141          goto enomem;
   6.142 @@ -98,12 +153,36 @@
   6.143      if (Identity_from_Struct(partner, &msg->partner) == NULL)
   6.144          goto enomem;
   6.145  
   6.146 +    if (asn_check_constraints(&asn_DEF_HandshakeRequest, msg, NULL, NULL)) {
   6.147 +        status = PEP_CONTRAINTS_VIOLATED;
   6.148 +        goto error;
   6.149 +    }
   6.150 +
   6.151 +    ssize_t size = uper_encode_to_new_buffer(&asn_DEF_HandshakeRequest,
   6.152 +            NULL, msg, (void **) &payload);
   6.153 +    if (size == -1) {
   6.154 +        status = PEP_CANNOT_ENCODE;
   6.155 +        goto error;
   6.156 +    }
   6.157 +
   6.158 +    status = prepare_message(me, partner, payload, size, &_message);
   6.159 +    if (status != PEP_STATUS_OK)
   6.160 +        goto error;
   6.161 +    payload = NULL;
   6.162 +
   6.163 +    status = session->messageToSend(session->sync_obj, _message);
   6.164 +
   6.165 +    free_message(_message);
   6.166 +    ASN_STRUCT_FREE(asn_DEF_HandshakeRequest, msg);
   6.167 +
   6.168      return status;
   6.169  
   6.170  enomem:
   6.171      status = PEP_OUT_OF_MEMORY;
   6.172  error:
   6.173      ASN_STRUCT_FREE(asn_DEF_HandshakeRequest, msg);
   6.174 +    free(payload);
   6.175 +    free_message(_message);
   6.176      return status;
   6.177  }
   6.178  
   6.179 @@ -112,12 +191,17 @@
   6.180  //
   6.181  //  params:
   6.182  //      session (in)        session handle
   6.183 +//      state (in)          state the state machine is in
   6.184  //      partner (in)        partner in sync
   6.185  //
   6.186  //  returns:
   6.187  //      PEP_STATUS_OK or any other value on error
   6.188  
   6.189 -PEP_STATUS showHandshake(PEP_SESSION session, const Identity partner)
   6.190 +PEP_STATUS showHandshake(
   6.191 +        PEP_SESSION session,
   6.192 +        DeviceState_state state,
   6.193 +        const Identity partner
   6.194 +    )
   6.195  {
   6.196      PEP_STATUS status = PEP_STATUS_OK;
   6.197  
   6.198 @@ -143,12 +227,17 @@
   6.199  //
   6.200  //  params:
   6.201  //      session (in)        session handle
   6.202 +//      state (in)          state the state machine is in
   6.203  //      partner (in)        partner in sync
   6.204  //
   6.205  //  returns:
   6.206  //      PEP_STATUS_OK or any other value on error
   6.207  
   6.208 -PEP_STATUS reject(PEP_SESSION session, const Identity partner)
   6.209 +PEP_STATUS reject(
   6.210 +        PEP_SESSION session,
   6.211 +        DeviceState_state state,
   6.212 +        const Identity partner
   6.213 +    )
   6.214  {
   6.215      PEP_STATUS status = PEP_STATUS_OK;
   6.216  
   6.217 @@ -174,12 +263,17 @@
   6.218  //
   6.219  //  params:
   6.220  //      session (in)        session handle
   6.221 +//      state (in)          state the state machine is in
   6.222  //      partner (in)        partner in sync
   6.223  //
   6.224  //  returns:
   6.225  //      PEP_STATUS_OK or any other value on error
   6.226  
   6.227 -PEP_STATUS storeGroupKeys(PEP_SESSION session, const Identity partner)
   6.228 +PEP_STATUS storeGroupKeys(
   6.229 +        PEP_SESSION session,
   6.230 +        DeviceState_state state,
   6.231 +        const Identity partner
   6.232 +    )
   6.233  {
   6.234      PEP_STATUS status = PEP_STATUS_OK;
   6.235  
   6.236 @@ -205,21 +299,33 @@
   6.237  //
   6.238  //  params:
   6.239  //      session (in)        session handle
   6.240 +//      state (in)          state the state machine is in
   6.241  //      partner (in)        (must be NULL)
   6.242  //
   6.243  //  returns:
   6.244  //      PEP_STATUS_OK or any other value on error
   6.245  
   6.246 -PEP_STATUS sendOwnKeys(PEP_SESSION session, const Identity partner)
   6.247 +PEP_STATUS sendOwnKeys(
   6.248 +        PEP_SESSION session,
   6.249 +        DeviceState_state state,
   6.250 +        const Identity partner
   6.251 +    )
   6.252  {
   6.253      PEP_STATUS status = PEP_STATUS_OK;
   6.254 +    OwnKeys_t *msg = NULL;
   6.255 +    char *payload = NULL;
   6.256 +    message *_message = NULL;
   6.257  
   6.258      assert(session);
   6.259      assert(!partner);
   6.260      if (!(session && !partner))
   6.261          return PEP_ILLEGAL_VALUE;
   6.262  
   6.263 -    OwnKeys_t *msg = (OwnKeys_t *) calloc(1, sizeof(OwnKeys_t));
   6.264 +    assert(session->messageToSend);
   6.265 +    if (!session->messageToSend)
   6.266 +        return PEP_SEND_FUNCTION_NOT_REGISTERED;
   6.267 +
   6.268 +    msg = (OwnKeys_t *) calloc(1, sizeof(OwnKeys_t));
   6.269      assert(msg);
   6.270      if (!msg)
   6.271          goto enomem;
   6.272 @@ -230,6 +336,8 @@
   6.273          goto error;
   6.274      msg->header.sequence = (long) seq;
   6.275  
   6.276 +    msg->state = (long) state;
   6.277 +
   6.278      pEp_identity *me = new_identity(NULL, NULL, NULL, NULL);
   6.279      if (!me)
   6.280          goto enomem;
   6.281 @@ -246,12 +354,36 @@
   6.282      if (KeyList_from_stringlist(sl, &msg->keylist) == NULL)
   6.283          goto enomem;
   6.284  
   6.285 +    if (asn_check_constraints(&asn_DEF_OwnKeys, msg, NULL, NULL)) {
   6.286 +        status = PEP_CONTRAINTS_VIOLATED;
   6.287 +        goto error;
   6.288 +    }
   6.289 +
   6.290 +    ssize_t size = uper_encode_to_new_buffer(&asn_DEF_OwnKeys,
   6.291 +            NULL, msg, (void **) &payload);
   6.292 +    if (size == -1) {
   6.293 +        status = PEP_CANNOT_ENCODE;
   6.294 +        goto error;
   6.295 +    }
   6.296 +
   6.297 +    status = prepare_message(me, partner, payload, size, &_message);
   6.298 +    if (status != PEP_STATUS_OK)
   6.299 +        goto error;
   6.300 +    payload = NULL;
   6.301 +
   6.302 +    status = session->messageToSend(session->sync_obj, _message);
   6.303 +
   6.304 +    free_message(_message);
   6.305 +    ASN_STRUCT_FREE(asn_DEF_OwnKeys, msg);
   6.306 +
   6.307      return status;
   6.308  
   6.309  enomem:
   6.310      status = PEP_OUT_OF_MEMORY;
   6.311  error:
   6.312      ASN_STRUCT_FREE(asn_DEF_OwnKeys, msg);
   6.313 +    free(payload);
   6.314 +    free_message(_message);
   6.315      return status;
   6.316  }
   6.317  
   6.318 @@ -260,12 +392,17 @@
   6.319  //
   6.320  //  params:
   6.321  //      session (in)        session handle
   6.322 +//      state (in)          state the state machine is in
   6.323  //      partner (in)        partner in sync
   6.324  //
   6.325  //  returns:
   6.326  //      PEP_STATUS_OK or any other value on error
   6.327  
   6.328 -PEP_STATUS transmitGroupKeys(PEP_SESSION session, const Identity partner)
   6.329 +PEP_STATUS transmitGroupKeys(
   6.330 +        PEP_SESSION session,
   6.331 +        DeviceState_state state,
   6.332 +        const Identity partner
   6.333 +    )
   6.334  {
   6.335      PEP_STATUS status = PEP_STATUS_OK;
   6.336  
     7.1 --- a/src/sync_driver.c	Sun May 29 00:11:56 2016 +0200
     7.2 +++ b/src/sync_driver.c	Sun May 29 19:02:14 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	Sun May 29 00:11:56 2016 +0200
     8.2 +++ b/src/sync_fsm.c	Sun May 29 19:02:14 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	Sun May 29 00:11:56 2016 +0200
     9.2 +++ b/src/sync_fsm.h	Sun May 29 19:02:14 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	Sun May 29 00:11:56 2016 +0200
    10.2 +++ b/sync/Makefile	Sun May 29 19:02:14 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	Sun May 29 00:11:56 2016 +0200
    11.2 +++ b/sync/gen_actions_skeleton.ysl2	Sun May 29 19:02:14 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,36 @@
  11.141           `` if "$name='OwnKeys'"|> if (KeyList_from_stringlist(sl, &msg->keylist) == NULL)
  11.142           `` if "$name='OwnKeys'"|>> goto enomem;
  11.143  
  11.144 +            if (asn_check_constraints(&asn_DEF_«$name», msg, NULL, NULL)) {
  11.145 +                status = PEP_CONTRAINTS_VIOLATED;
  11.146 +                goto error;
  11.147 +            }
  11.148 +
  11.149 +            ssize_t size = uper_encode_to_new_buffer(&asn_DEF_«$name»,
  11.150 +                    NULL, msg, (void **) &payload);
  11.151 +            if (size == -1) {
  11.152 +                status = PEP_CANNOT_ENCODE;
  11.153 +                goto error;
  11.154 +            }
  11.155 +
  11.156 +            status = prepare_message(me, partner, payload, size, &_message);
  11.157 +            if (status != PEP_STATUS_OK)
  11.158 +                goto error;
  11.159 +            payload = NULL;
  11.160 +
  11.161 +            status = session->messageToSend(session->sync_obj, _message);
  11.162 +
  11.163 +            free_message(_message);
  11.164 +            ASN_STRUCT_FREE(asn_DEF_«$name», msg);
  11.165 +
  11.166              return status;
  11.167  
  11.168          enomem:
  11.169              status = PEP_OUT_OF_MEMORY;
  11.170          error:
  11.171              ASN_STRUCT_FREE(asn_DEF_«$name», msg);
  11.172 +            free(payload);
  11.173 +            free_message(_message);
  11.174              return status;
  11.175          }
  11.176  
    12.1 --- a/sync/gen_statemachine.ysl2	Sun May 29 00:11:56 2016 +0200
    12.2 +++ b/sync/gen_statemachine.ysl2	Sun May 29 19:02:14 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