src/sync.c
branchkeysync
changeset 1043 f786ee22e281
parent 987 923fdc4de690
child 1078 5861b53e0536
     1.1 --- a/src/sync.c	Tue Aug 16 19:20:20 2016 +0200
     1.2 +++ b/src/sync.c	Sat Aug 20 14:45:19 2016 +0200
     1.3 @@ -5,17 +5,27 @@
     1.4  
     1.5  #include "../asn.1/DeviceGroup-Protocol.h"
     1.6  
     1.7 +// receive_sync_msg is defined in the sync_actions
     1.8 +
     1.9 +PEP_STATUS receive_sync_msg(
    1.10 +        PEP_SESSION session,
    1.11 +        DeviceGroup_Protocol_t *msg
    1.12 +    );
    1.13  
    1.14  DYNAMIC_API PEP_STATUS register_sync_callbacks(
    1.15          PEP_SESSION session,
    1.16          void *obj,
    1.17          messageToSend_t messageToSend,
    1.18 -        showHandshake_t showHandshake
    1.19 +        showHandshake_t showHandshake,
    1.20 +        inject_sync_msg_t inject_sync_msg,
    1.21 +        retrieve_next_sync_msg_t retrieve_next_sync_msg
    1.22      )
    1.23  {
    1.24      session->sync_obj = obj;
    1.25      session->messageToSend = messageToSend;
    1.26      session->showHandshake = showHandshake;
    1.27 +    session->inject_sync_msg = inject_sync_msg;
    1.28 +    session->retrieve_next_sync_msg = retrieve_next_sync_msg;
    1.29  
    1.30      // start state machine
    1.31      session->sync_state = InitState;
    1.32 @@ -34,6 +44,7 @@
    1.33      session->sync_obj = NULL;
    1.34      session->messageToSend = NULL;
    1.35      session->showHandshake = NULL;
    1.36 +    session->retrieve_next_sync_msg = NULL;
    1.37  }
    1.38  
    1.39  DYNAMIC_API PEP_STATUS deliverHandshakeResult(
    1.40 @@ -45,20 +56,53 @@
    1.41      if (!session)
    1.42          return PEP_ILLEGAL_VALUE;
    1.43  
    1.44 +    PEP_STATUS status = PEP_STATUS_OK;
    1.45 +
    1.46      switch (result) {
    1.47          case SYNC_HANDSHAKE_CANCEL:
    1.48 -            fsm_DeviceState_inject(session, Cancel, NULL, 0);
    1.49 +            status = fsm_DeviceState_inject(session, Cancel, NULL, 0);
    1.50              break;
    1.51          case SYNC_HANDSHAKE_ACCEPTED:
    1.52 -            fsm_DeviceState_inject(session, HandshakeAccepted, NULL, 0);
    1.53 +            status = fsm_DeviceState_inject(session, HandshakeAccepted, NULL, 0);
    1.54              break;
    1.55          case SYNC_HANDSHAKE_REJECTED:
    1.56 -            fsm_DeviceState_inject(session, HandshakeRejected, NULL, 0);
    1.57 +            status = fsm_DeviceState_inject(session, HandshakeRejected, NULL, 0);
    1.58              break;
    1.59          default:
    1.60              return PEP_ILLEGAL_VALUE;
    1.61      }
    1.62  
    1.63 +    return status;
    1.64 +}
    1.65 +
    1.66 +DYNAMIC_API PEP_STATUS do_sync_protocol(
    1.67 +        PEP_SESSION session,
    1.68 +        void *management
    1.69 +    )
    1.70 +{
    1.71 +    DeviceGroup_Protocol_t *msg;
    1.72 +    PEP_STATUS status = PEP_STATUS_OK;
    1.73 +
    1.74 +    assert(session && session->retrieve_next_sync_msg);
    1.75 +    assert(management);
    1.76 +
    1.77 +    if (!(session && session->retrieve_next_sync_msg) || !management)
    1.78 +        return PEP_ILLEGAL_VALUE;
    1.79 +
    1.80 +    log_event(session, "sync_protocol thread started", "pEp sync protocol", NULL, NULL);
    1.81 +
    1.82 +    while ((msg = (DeviceGroup_Protocol_t *) session->retrieve_next_sync_msg(management))) 
    1.83 +    {
    1.84 +        if ((status = receive_sync_msg(session, msg) != PEP_STATUS_OK)) {
    1.85 +            char buffer[MAX_LINELENGTH];
    1.86 +            memset(buffer, 0, MAX_LINELENGTH);
    1.87 +            snprintf(buffer, MAX_LINELENGTH, "problem with msg received: %d\n", (int) status);
    1.88 +            log_event(session, buffer, "pEp sync protocol", NULL, NULL);
    1.89 +        }
    1.90 +    }
    1.91 +
    1.92 +    log_event(session, "sync_protocol thread shutdown", "pEp sync protocol", NULL, NULL);
    1.93 +
    1.94      return PEP_STATUS_OK;
    1.95  }
    1.96