src/sync.c
author Volker Birk <vb@pep.foundation>
Sat, 20 Aug 2016 14:45:19 +0200
branchkeysync
changeset 1043 f786ee22e281
parent 987 923fdc4de690
child 1078 5861b53e0536
permissions -rw-r--r--
empty address allowed for myself()
vb@572
     1
#include "pEp_internal.h"
vb@572
     2
vb@572
     3
#include <memory.h>
vb@572
     4
#include <assert.h>
vb@572
     5
vb@951
     6
#include "../asn.1/DeviceGroup-Protocol.h"
vb@951
     7
vb@1043
     8
// receive_sync_msg is defined in the sync_actions
vb@1043
     9
vb@1043
    10
PEP_STATUS receive_sync_msg(
vb@1043
    11
        PEP_SESSION session,
vb@1043
    12
        DeviceGroup_Protocol_t *msg
vb@1043
    13
    );
vb@572
    14
vb@572
    15
DYNAMIC_API PEP_STATUS register_sync_callbacks(
vb@599
    16
        PEP_SESSION session,
vb@599
    17
        void *obj,
vb@597
    18
        messageToSend_t messageToSend,
vb@1043
    19
        showHandshake_t showHandshake,
vb@1043
    20
        inject_sync_msg_t inject_sync_msg,
vb@1043
    21
        retrieve_next_sync_msg_t retrieve_next_sync_msg
vb@572
    22
    )
vb@572
    23
{
vb@599
    24
    session->sync_obj = obj;
vb@604
    25
    session->messageToSend = messageToSend;
vb@604
    26
    session->showHandshake = showHandshake;
vb@1043
    27
    session->inject_sync_msg = inject_sync_msg;
vb@1043
    28
    session->retrieve_next_sync_msg = retrieve_next_sync_msg;
vb@572
    29
vb@976
    30
    // start state machine
vb@976
    31
    session->sync_state = InitState;
vb@976
    32
    PEP_STATUS status = fsm_DeviceState_inject(session, Init, NULL, NULL);
vb@987
    33
    if (status != PEP_STATUS_OK)
vb@987
    34
        unregister_sync_callbacks(session);
vb@976
    35
vb@976
    36
    return status;
vb@572
    37
}
vb@572
    38
vb@572
    39
DYNAMIC_API void unregister_sync_callbacks(PEP_SESSION session) {
vb@986
    40
    // stop state machine
vb@986
    41
    session->sync_state = DeviceState_state_NONE;
vb@986
    42
vb@986
    43
    // unregister
vb@602
    44
    session->sync_obj = NULL;
vb@604
    45
    session->messageToSend = NULL;
vb@604
    46
    session->showHandshake = NULL;
vb@1043
    47
    session->retrieve_next_sync_msg = NULL;
vb@572
    48
}
vb@572
    49
vb@679
    50
DYNAMIC_API PEP_STATUS deliverHandshakeResult(
vb@609
    51
        PEP_SESSION session,
vb@609
    52
        sync_handshake_result result
vb@609
    53
    )
vb@609
    54
{
vb@609
    55
    assert(session);
vb@609
    56
    if (!session)
vb@609
    57
        return PEP_ILLEGAL_VALUE;
vb@609
    58
vb@1043
    59
    PEP_STATUS status = PEP_STATUS_OK;
vb@1043
    60
vb@609
    61
    switch (result) {
vb@609
    62
        case SYNC_HANDSHAKE_CANCEL:
vb@1043
    63
            status = fsm_DeviceState_inject(session, Cancel, NULL, 0);
vb@609
    64
            break;
vb@609
    65
        case SYNC_HANDSHAKE_ACCEPTED:
vb@1043
    66
            status = fsm_DeviceState_inject(session, HandshakeAccepted, NULL, 0);
vb@609
    67
            break;
vb@609
    68
        case SYNC_HANDSHAKE_REJECTED:
vb@1043
    69
            status = fsm_DeviceState_inject(session, HandshakeRejected, NULL, 0);
vb@609
    70
            break;
vb@609
    71
        default:
vb@609
    72
            return PEP_ILLEGAL_VALUE;
vb@609
    73
    }
vb@609
    74
vb@1043
    75
    return status;
vb@1043
    76
}
vb@1043
    77
vb@1043
    78
DYNAMIC_API PEP_STATUS do_sync_protocol(
vb@1043
    79
        PEP_SESSION session,
vb@1043
    80
        void *management
vb@1043
    81
    )
vb@1043
    82
{
vb@1043
    83
    DeviceGroup_Protocol_t *msg;
vb@1043
    84
    PEP_STATUS status = PEP_STATUS_OK;
vb@1043
    85
vb@1043
    86
    assert(session && session->retrieve_next_sync_msg);
vb@1043
    87
    assert(management);
vb@1043
    88
vb@1043
    89
    if (!(session && session->retrieve_next_sync_msg) || !management)
vb@1043
    90
        return PEP_ILLEGAL_VALUE;
vb@1043
    91
vb@1043
    92
    log_event(session, "sync_protocol thread started", "pEp sync protocol", NULL, NULL);
vb@1043
    93
vb@1043
    94
    while ((msg = (DeviceGroup_Protocol_t *) session->retrieve_next_sync_msg(management))) 
vb@1043
    95
    {
vb@1043
    96
        if ((status = receive_sync_msg(session, msg) != PEP_STATUS_OK)) {
vb@1043
    97
            char buffer[MAX_LINELENGTH];
vb@1043
    98
            memset(buffer, 0, MAX_LINELENGTH);
vb@1043
    99
            snprintf(buffer, MAX_LINELENGTH, "problem with msg received: %d\n", (int) status);
vb@1043
   100
            log_event(session, buffer, "pEp sync protocol", NULL, NULL);
vb@1043
   101
        }
vb@1043
   102
    }
vb@1043
   103
vb@1043
   104
    log_event(session, "sync_protocol thread shutdown", "pEp sync protocol", NULL, NULL);
vb@1043
   105
vb@609
   106
    return PEP_STATUS_OK;
vb@609
   107
}
vb@609
   108