sync handshake dialog async
authorVolker Birk <vb@pep-project.org>
Thu, 12 May 2016 11:21:57 +0200
changeset 609a22a400611ca
parent 608 3bf69b382303
child 610 1e3d39516f43
child 611 d74ad0ae95c6
sync handshake dialog async
src/Makefile
src/sync.c
src/sync.h
src/sync_fsm.c
sync/gen_statemachine.ysl2
     1.1 --- a/src/Makefile	Thu May 12 10:45:54 2016 +0200
     1.2 +++ b/src/Makefile	Thu May 12 11:21:57 2016 +0200
     1.3 @@ -52,7 +52,8 @@
     1.4  	$(error Unknown OpenPGP library : $(OPENPGP))
     1.5  endif
     1.6  
     1.7 -ALL_SOURCE=$(subst $(NO_SOURCE),,$(wildcard *.c)) sync_fsm.c
     1.8 +ALL_SOURCE=$(subst $(NO_SOURCE),,$(wildcard *.c))
     1.9 +
    1.10  DEPENDS=$(subst .c,.d,$(ALL_SOURCE))
    1.11  ALL_OBJECTS=$(subst .c,.o,$(ALL_SOURCE))
    1.12  
     2.1 --- a/src/sync.c	Thu May 12 10:45:54 2016 +0200
     2.2 +++ b/src/sync.c	Thu May 12 11:21:57 2016 +0200
     2.3 @@ -3,6 +3,8 @@
     2.4  #include <memory.h>
     2.5  #include <assert.h>
     2.6  
     2.7 +#include "sync_fsm.h"
     2.8 +
     2.9  
    2.10  DYNAMIC_API PEP_STATUS register_sync_callbacks(
    2.11          PEP_SESSION session,
    2.12 @@ -24,3 +26,29 @@
    2.13      session->showHandshake = NULL;
    2.14  }
    2.15  
    2.16 +PEP_STATUS deliverHandshakeResult(
    2.17 +        PEP_SESSION session,
    2.18 +        sync_handshake_result result
    2.19 +    )
    2.20 +{
    2.21 +    assert(session);
    2.22 +    if (!session)
    2.23 +        return PEP_ILLEGAL_VALUE;
    2.24 +
    2.25 +    switch (result) {
    2.26 +        case SYNC_HANDSHAKE_CANCEL:
    2.27 +            fsm_DeviceState_inject(session, Cancel);
    2.28 +            break;
    2.29 +        case SYNC_HANDSHAKE_ACCEPTED:
    2.30 +            fsm_DeviceState_inject(session, HandshakeAccepted);
    2.31 +            break;
    2.32 +        case SYNC_HANDSHAKE_REJECTED:
    2.33 +            fsm_DeviceState_inject(session, HandshakeRejected);
    2.34 +            break;
    2.35 +        default:
    2.36 +            return PEP_ILLEGAL_VALUE;
    2.37 +    }
    2.38 +
    2.39 +    return PEP_STATUS_OK;
    2.40 +}
    2.41 +
     3.1 --- a/src/sync.h	Thu May 12 10:45:54 2016 +0200
     3.2 +++ b/src/sync.h	Thu May 12 11:21:57 2016 +0200
     3.3 @@ -29,13 +29,12 @@
     3.4      SYNC_HANDSHAKE_REJECTED = 1
     3.5  } sync_handshake_result;
     3.6  
     3.7 -// showHandshake() - do a handshake and deliver the result
     3.8 +// showHandshake() - do a handshake by showing the handshake dialog
     3.9  //
    3.10  //  parameters:
    3.11  //      obj (in)        object handle (implementation defined)
    3.12  //      self (in)       own identity
    3.13  //      partner (in)    identity of partner
    3.14 -//      result (out)    result of handshake
    3.15  //
    3.16  //  return value:
    3.17  //      PEP_STATUS_OK or any other value on error
    3.18 @@ -43,8 +42,19 @@
    3.19  typedef PEP_STATUS (*showHandshake_t)(
    3.20          void *obj,
    3.21          const pEp_identity *self,
    3.22 -        const pEp_identity *partner,
    3.23 -        sync_handshake_result *result
    3.24 +        const pEp_identity *partner
    3.25 +    );
    3.26 +
    3.27 +
    3.28 +// deliverHandshakeResult() - give the result of the handshake dialog
    3.29 +//
    3.30 +//  parameters:
    3.31 +//      session (in)    session handle
    3.32 +//      result (in)     handshake result
    3.33 +
    3.34 +PEP_STATUS deliverHandshakeResult(
    3.35 +        PEP_SESSION session,
    3.36 +        sync_handshake_result result
    3.37      );
    3.38  
    3.39  
     4.1 --- a/src/sync_fsm.c	Thu May 12 10:45:54 2016 +0200
     4.2 +++ b/src/sync_fsm.c	Thu May 12 11:21:57 2016 +0200
     4.3 @@ -1,67 +1,7 @@
     4.4 +#include "sync_fsm.h"
     4.5 +
     4.6  // state machine for DeviceState
     4.7  
     4.8 -#include "pEpEngine.h"
     4.9 -
    4.10 -// types
    4.11 -
    4.12 -typedef pEp_identity * Identity;
    4.13 -typedef union _param { const Identity partner; const stringlist_t *keylist; } param_t;
    4.14 -
    4.15 -// error values
    4.16 -
    4.17 -typedef enum _fsm_error {
    4.18 -    invalid_state = -1,
    4.19 -    invalid_event = -2
    4.20 -} fsm_error;
    4.21 -
    4.22 -// states
    4.23 -
    4.24 -typedef enum _DeviceState_state {
    4.25 -    Sole, 
    4.26 -    HandshakingSole, 
    4.27 -    WaitForGroupKeys, 
    4.28 -    Grouped
    4.29 -} DeviceState_state;
    4.30 -
    4.31 -// events
    4.32 -
    4.33 -typedef enum _DeviceState_event {
    4.34 -    KeyGen, 
    4.35 -    CannotDecrypt, 
    4.36 -    Beacon, 
    4.37 -    HandshakeRequest, 
    4.38 -    Init, 
    4.39 -    HandshakeRejected, 
    4.40 -    HandshakeAccepted, 
    4.41 -    ReceiveGroupKeys, 
    4.42 -    Cancel, 
    4.43 -    Reject
    4.44 -} DeviceState_event;
    4.45 -
    4.46 -// actions
    4.47 -
    4.48 -void sendBeacon(const Identity partner);
    4.49 -void sendHandshakeRequest(const Identity partner);
    4.50 -void showHandshake(const Identity partner);
    4.51 -void reject(const Identity partner);
    4.52 -void storeGroupKeys(const Identity partner);
    4.53 -void sendOwnKeys(const Identity partner);
    4.54 -void transmitGroupKeys(const Identity partner);
    4.55 -
    4.56 -// decoders
    4.57 -
    4.58 -void decodeBeacon(void);
    4.59 -void decodeHandshakeRequest(Identity partner);
    4.60 -void decodeOwnKeys(void);
    4.61 -
    4.62 -// encoders 
    4.63 -
    4.64 -void encodeBeacon(void);
    4.65 -void encodeHandshakeRequest(Identity partner);
    4.66 -void encodeOwnKeys(void);
    4.67 -
    4.68 -// state machine
    4.69 -
    4.70  DeviceState_state fsm_DeviceState(
    4.71          DeviceState_state state,
    4.72          DeviceState_event event,
     5.1 --- a/sync/gen_statemachine.ysl2	Thu May 12 10:45:54 2016 +0200
     5.2 +++ b/sync/gen_statemachine.ysl2	Thu May 12 11:21:57 2016 +0200
     5.3 @@ -11,67 +11,78 @@
     5.4      key "ActionByNames", "/protocol/fsm/state/event//action", "@name";
     5.5      variable "UniqueActions", "/protocol/fsm/state/event//action[generate-id()=generate-id(key('ActionByNames',@name)[1])]";
     5.6  
     5.7 -    template "/protocol/fsm"
     5.8 -    ||
     5.9 -    // state machine for «@name»
    5.10 +    template "/protocol/fsm" {
    5.11 +        document "../src/sync_fsm.h", "text"
    5.12 +        ||
    5.13 +        #pragma once
    5.14  
    5.15 -    #include "pEpEngine.h"
    5.16 +        // state machine for «@name»
    5.17  
    5.18 -    // types
    5.19 +        #include "pEpEngine.h"
    5.20  
    5.21 -    typedef pEp_identity * Identity;
    5.22 -    typedef union _param { const Identity partner; const stringlist_t *keylist; } param_t;
    5.23 +        // types
    5.24  
    5.25 -    // error values
    5.26 +        typedef pEp_identity * Identity;
    5.27 +        typedef union _param { const Identity partner; const stringlist_t *keylist; } param_t;
    5.28  
    5.29 -    typedef enum _fsm_error {
    5.30 -        invalid_state = -1,
    5.31 -        invalid_event = -2
    5.32 -    } fsm_error;
    5.33 +        // error values
    5.34  
    5.35 -    // states
    5.36 +        typedef enum _fsm_error {
    5.37 +            invalid_state = -1,
    5.38 +            invalid_event = -2
    5.39 +        } fsm_error;
    5.40  
    5.41 -    typedef enum _«@name»_state {
    5.42 -    `` for "set:distinct(state/@name)" |> «.»`if "position()!=last()" > , `
    5.43 -    } «@name»_state;
    5.44 +        // states
    5.45  
    5.46 -    // events
    5.47 +        typedef enum _«@name»_state {
    5.48 +        `` for "set:distinct(state/@name)" |> «.»`if "position()!=last()" > , `
    5.49 +        } «@name»_state;
    5.50  
    5.51 -    typedef enum _«@name»_event {
    5.52 -    `` for "set:distinct(state/event/@name)" |> «.»`if "position()!=last()" > , `
    5.53 -    } «@name»_event;
    5.54 +        // events
    5.55  
    5.56 -    // actions
    5.57 +        typedef enum _«@name»_event {
    5.58 +        `` for "set:distinct(state/event/@name)" |> «.»`if "position()!=last()" > , `
    5.59 +        } «@name»_event;
    5.60  
    5.61 -    `` for "set:distinct(state/event//action/@name)" | void «.»(const Identity partner);
    5.62 +        // actions
    5.63  
    5.64 -    // decoders
    5.65 +        `` for "set:distinct(state/event//action/@name)" | void «.»(const Identity partner);
    5.66  
    5.67 -    `` apply "$UniqueActions[starts-with(@name,'send')]",0 mode="proto" with "pfx", "'decode'"
    5.68 +        // driver
    5.69  
    5.70 -    // encoders 
    5.71 +        void fsm_«@name»_inject(PEP_SESSION session, «@name»_event event);
    5.72  
    5.73 -    `` apply "$UniqueActions[starts-with(@name,'send')]",0 mode="proto" with "pfx", "'encode'"
    5.74 +        // decoders
    5.75  
    5.76 -    // state machine
    5.77 +        `` apply "$UniqueActions[starts-with(@name,'send')]",0 mode="proto" with "pfx", "'decode'"
    5.78  
    5.79 -    «@name»_state fsm_«@name»(
    5.80 -            «@name»_state state,
    5.81 -            «@name»_event event,
    5.82 -            const Identity partner
    5.83 -        )
    5.84 -    {
    5.85 -        switch (state) {
    5.86 -            `` apply "state"
    5.87 -            default:
    5.88 -                return invalid_state;
    5.89 +        // encoders 
    5.90 +
    5.91 +        `` apply "$UniqueActions[starts-with(@name,'send')]",0 mode="proto" with "pfx", "'encode'"
    5.92 +        ||
    5.93 +        ||
    5.94 +        #include "sync_fsm.h"
    5.95 +
    5.96 +        // state machine for «@name»
    5.97 +
    5.98 +        «@name»_state fsm_«@name»(
    5.99 +                «@name»_state state,
   5.100 +                «@name»_event event,
   5.101 +                const Identity partner
   5.102 +            )
   5.103 +        {
   5.104 +            switch (state) {
   5.105 +                `` apply "state"
   5.106 +                default:
   5.107 +                    return invalid_state;
   5.108 +            }
   5.109 +
   5.110 +            return state;
   5.111          }
   5.112  
   5.113 -        return state;
   5.114 +        ||
   5.115      }
   5.116  
   5.117 -    ||
   5.118 -
   5.119      template "state"
   5.120      ||
   5.121      case «@name»: