src/sync.c
author Volker Birk <vb@pep.foundation>
Mon, 29 Aug 2016 17:39:35 +0200
branchkeysync
changeset 1091 0571ce4b97a2
parent 1078 5861b53e0536
child 1116 c9a03f2423c1
permissions -rw-r--r--
fixes
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@1091
    24
    unsigned char uuid[16];
vb@1091
    25
    uuid_generate_random(uuid);
vb@1091
    26
    uuid_unparse_upper(uuid, sync_uuid);
vb@1091
    27
vb@599
    28
    session->sync_obj = obj;
vb@604
    29
    session->messageToSend = messageToSend;
vb@604
    30
    session->showHandshake = showHandshake;
vb@1043
    31
    session->inject_sync_msg = inject_sync_msg;
vb@1043
    32
    session->retrieve_next_sync_msg = retrieve_next_sync_msg;
vb@572
    33
vb@976
    34
    // start state machine
vb@976
    35
    session->sync_state = InitState;
vb@976
    36
    PEP_STATUS status = fsm_DeviceState_inject(session, Init, NULL, NULL);
vb@987
    37
    if (status != PEP_STATUS_OK)
vb@987
    38
        unregister_sync_callbacks(session);
vb@976
    39
vb@976
    40
    return status;
vb@572
    41
}
vb@572
    42
vb@572
    43
DYNAMIC_API void unregister_sync_callbacks(PEP_SESSION session) {
vb@986
    44
    // stop state machine
vb@986
    45
    session->sync_state = DeviceState_state_NONE;
vb@986
    46
vb@986
    47
    // unregister
vb@602
    48
    session->sync_obj = NULL;
vb@604
    49
    session->messageToSend = NULL;
vb@604
    50
    session->showHandshake = NULL;
vb@1043
    51
    session->retrieve_next_sync_msg = NULL;
vb@572
    52
}
vb@572
    53
vb@679
    54
DYNAMIC_API PEP_STATUS deliverHandshakeResult(
vb@609
    55
        PEP_SESSION session,
vb@609
    56
        sync_handshake_result result
vb@609
    57
    )
vb@609
    58
{
vb@609
    59
    assert(session);
vb@609
    60
    if (!session)
vb@609
    61
        return PEP_ILLEGAL_VALUE;
vb@609
    62
vb@1043
    63
    PEP_STATUS status = PEP_STATUS_OK;
vb@1043
    64
vb@609
    65
    switch (result) {
vb@609
    66
        case SYNC_HANDSHAKE_CANCEL:
vb@1043
    67
            status = fsm_DeviceState_inject(session, Cancel, NULL, 0);
vb@609
    68
            break;
vb@609
    69
        case SYNC_HANDSHAKE_ACCEPTED:
vb@1043
    70
            status = fsm_DeviceState_inject(session, HandshakeAccepted, NULL, 0);
vb@609
    71
            break;
vb@609
    72
        case SYNC_HANDSHAKE_REJECTED:
vb@1043
    73
            status = fsm_DeviceState_inject(session, HandshakeRejected, NULL, 0);
vb@609
    74
            break;
vb@609
    75
        default:
vb@609
    76
            return PEP_ILLEGAL_VALUE;
vb@609
    77
    }
vb@609
    78
vb@1043
    79
    return status;
vb@1043
    80
}
vb@1043
    81
vb@1043
    82
DYNAMIC_API PEP_STATUS do_sync_protocol(
vb@1043
    83
        PEP_SESSION session,
vb@1043
    84
        void *management
vb@1043
    85
    )
vb@1043
    86
{
vb@1078
    87
    DeviceGroup_Protocol_t *msg = NULL;
vb@1043
    88
    PEP_STATUS status = PEP_STATUS_OK;
vb@1043
    89
vb@1043
    90
    assert(session && session->retrieve_next_sync_msg);
vb@1043
    91
    assert(management);
vb@1043
    92
vb@1043
    93
    if (!(session && session->retrieve_next_sync_msg) || !management)
vb@1043
    94
        return PEP_ILLEGAL_VALUE;
vb@1043
    95
vb@1043
    96
    log_event(session, "sync_protocol thread started", "pEp sync protocol", NULL, NULL);
vb@1043
    97
vb@1043
    98
    while ((msg = (DeviceGroup_Protocol_t *) session->retrieve_next_sync_msg(management))) 
vb@1043
    99
    {
vb@1043
   100
        if ((status = receive_sync_msg(session, msg) != PEP_STATUS_OK)) {
vb@1043
   101
            char buffer[MAX_LINELENGTH];
vb@1043
   102
            memset(buffer, 0, MAX_LINELENGTH);
vb@1043
   103
            snprintf(buffer, MAX_LINELENGTH, "problem with msg received: %d\n", (int) status);
vb@1043
   104
            log_event(session, buffer, "pEp sync protocol", NULL, NULL);
vb@1043
   105
        }
vb@1043
   106
    }
vb@1043
   107
vb@1043
   108
    log_event(session, "sync_protocol thread shutdown", "pEp sync protocol", NULL, NULL);
vb@1043
   109
vb@609
   110
    return PEP_STATUS_OK;
vb@609
   111
}
vb@609
   112