src/sync.c
branchkeysync
changeset 1236 991afc1aa2a1
parent 1173 5a8002f941e0
child 1259 e5bc79871fbb
     1.1 --- a/src/sync.c	Fri Sep 30 17:25:15 2016 +0200
     1.2 +++ b/src/sync.c	Mon Oct 03 19:08:15 2016 +0200
     1.3 @@ -6,9 +6,6 @@
     1.4  #include "asn1_helper.h"
     1.5  #include "../asn.1/DeviceGroup-Protocol.h"
     1.6  
     1.7 -static void *static_sync_obj = NULL;
     1.8 -static inject_sync_msg_t static_inject_sync_msg = NULL;
     1.9 -
    1.10  // receive_sync_msg is defined in the sync_actions
    1.11  
    1.12  PEP_STATUS receive_sync_msg(
    1.13 @@ -25,9 +22,13 @@
    1.14          retrieve_next_sync_msg_t retrieve_next_sync_msg
    1.15      )
    1.16  {
    1.17 +    assert(session && obj && messageToSend && showHandshake && inject_sync_msg && retrieve_next_sync_msg);
    1.18 +    if (!(session && obj && messageToSend && showHandshake && inject_sync_msg && retrieve_next_sync_msg))
    1.19 +        return PEP_ILLEGAL_VALUE;
    1.20 +
    1.21      unsigned char uuid[16];
    1.22      uuid_generate_random(uuid);
    1.23 -    uuid_unparse_upper(uuid, sync_uuid);
    1.24 +    uuid_unparse_upper(uuid, session->sync_uuid);
    1.25  
    1.26      session->sync_obj = obj;
    1.27      session->messageToSend = messageToSend;
    1.28 @@ -35,9 +36,6 @@
    1.29      session->inject_sync_msg = inject_sync_msg;
    1.30      session->retrieve_next_sync_msg = retrieve_next_sync_msg;
    1.31  
    1.32 -    static_sync_obj = obj;
    1.33 -    static_inject_sync_msg = inject_sync_msg;
    1.34 -
    1.35      // start state machine
    1.36      session->sync_state = InitState;
    1.37      PEP_STATUS status = fsm_DeviceState_inject(session, Init, NULL, NULL);
    1.38 @@ -47,24 +45,49 @@
    1.39      return status;
    1.40  }
    1.41  
    1.42 +DYNAMIC_API PEP_STATUS attach_sync_session(
    1.43 +        PEP_SESSION session,
    1.44 +        PEP_SESSION sync_session
    1.45 +    )
    1.46 +{
    1.47 +    assert(session && sync_session && sync_session->sync_obj && sync_session->inject_sync_msg );
    1.48 +    if (!(session && sync_session && sync_session->sync_obj && sync_session->inject_sync_msg ))
    1.49 +        return PEP_ILLEGAL_VALUE;
    1.50 +
    1.51 +    memcpy(session->sync_uuid, sync_session->sync_uuid, 37);
    1.52 +
    1.53 +    session->sync_obj = sync_session->sync_obj;
    1.54 +    session->inject_sync_msg = sync_session->inject_sync_msg;
    1.55 +
    1.56 +    return PEP_STATUS_OK;
    1.57 +}
    1.58 +
    1.59 +DYNAMIC_API PEP_STATUS detach_sync_session(PEP_SESSION session)
    1.60 +{
    1.61 +    assert(session && session->sync_obj && session->inject_sync_msg );
    1.62 +    if (!(session && session->sync_obj && session->inject_sync_msg ))
    1.63 +        return PEP_ILLEGAL_VALUE;
    1.64 +
    1.65 +    memset(session->sync_uuid, 0, 37);
    1.66 +
    1.67 +    session->sync_obj = NULL;
    1.68 +    session->inject_sync_msg = NULL;
    1.69 +
    1.70 +    return PEP_STATUS_OK;
    1.71 +}
    1.72 +
    1.73  int call_inject_sync_msg(PEP_SESSION session, void *msg)
    1.74  {
    1.75      if(session->inject_sync_msg && session->sync_obj)
    1.76          return session->inject_sync_msg(msg, session->sync_obj);
    1.77 -    else if(static_inject_sync_msg && static_sync_obj)
    1.78 -        return static_inject_sync_msg(msg, static_sync_obj);
    1.79      else
    1.80         return PEP_SYNC_NO_INJECT_CALLBACK;
    1.81  }
    1.82  
    1.83 -
    1.84  DYNAMIC_API void unregister_sync_callbacks(PEP_SESSION session) {
    1.85      // stop state machine
    1.86      session->sync_state = DeviceState_state_NONE;
    1.87  
    1.88 -    static_sync_obj = NULL;
    1.89 -    static_inject_sync_msg = NULL;
    1.90 -
    1.91      // unregister
    1.92      session->sync_obj = NULL;
    1.93      session->messageToSend = NULL;