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
     1 #include "pEp_internal.h"
     2 
     3 #include <memory.h>
     4 #include <assert.h>
     5 
     6 #include "../asn.1/DeviceGroup-Protocol.h"
     7 
     8 // receive_sync_msg is defined in the sync_actions
     9 
    10 PEP_STATUS receive_sync_msg(
    11         PEP_SESSION session,
    12         DeviceGroup_Protocol_t *msg
    13     );
    14 
    15 DYNAMIC_API PEP_STATUS register_sync_callbacks(
    16         PEP_SESSION session,
    17         void *obj,
    18         messageToSend_t messageToSend,
    19         showHandshake_t showHandshake,
    20         inject_sync_msg_t inject_sync_msg,
    21         retrieve_next_sync_msg_t retrieve_next_sync_msg
    22     )
    23 {
    24     unsigned char uuid[16];
    25     uuid_generate_random(uuid);
    26     uuid_unparse_upper(uuid, sync_uuid);
    27 
    28     session->sync_obj = obj;
    29     session->messageToSend = messageToSend;
    30     session->showHandshake = showHandshake;
    31     session->inject_sync_msg = inject_sync_msg;
    32     session->retrieve_next_sync_msg = retrieve_next_sync_msg;
    33 
    34     // start state machine
    35     session->sync_state = InitState;
    36     PEP_STATUS status = fsm_DeviceState_inject(session, Init, NULL, NULL);
    37     if (status != PEP_STATUS_OK)
    38         unregister_sync_callbacks(session);
    39 
    40     return status;
    41 }
    42 
    43 DYNAMIC_API void unregister_sync_callbacks(PEP_SESSION session) {
    44     // stop state machine
    45     session->sync_state = DeviceState_state_NONE;
    46 
    47     // unregister
    48     session->sync_obj = NULL;
    49     session->messageToSend = NULL;
    50     session->showHandshake = NULL;
    51     session->retrieve_next_sync_msg = NULL;
    52 }
    53 
    54 DYNAMIC_API PEP_STATUS deliverHandshakeResult(
    55         PEP_SESSION session,
    56         sync_handshake_result result
    57     )
    58 {
    59     assert(session);
    60     if (!session)
    61         return PEP_ILLEGAL_VALUE;
    62 
    63     PEP_STATUS status = PEP_STATUS_OK;
    64 
    65     switch (result) {
    66         case SYNC_HANDSHAKE_CANCEL:
    67             status = fsm_DeviceState_inject(session, Cancel, NULL, 0);
    68             break;
    69         case SYNC_HANDSHAKE_ACCEPTED:
    70             status = fsm_DeviceState_inject(session, HandshakeAccepted, NULL, 0);
    71             break;
    72         case SYNC_HANDSHAKE_REJECTED:
    73             status = fsm_DeviceState_inject(session, HandshakeRejected, NULL, 0);
    74             break;
    75         default:
    76             return PEP_ILLEGAL_VALUE;
    77     }
    78 
    79     return status;
    80 }
    81 
    82 DYNAMIC_API PEP_STATUS do_sync_protocol(
    83         PEP_SESSION session,
    84         void *management
    85     )
    86 {
    87     DeviceGroup_Protocol_t *msg = NULL;
    88     PEP_STATUS status = PEP_STATUS_OK;
    89 
    90     assert(session && session->retrieve_next_sync_msg);
    91     assert(management);
    92 
    93     if (!(session && session->retrieve_next_sync_msg) || !management)
    94         return PEP_ILLEGAL_VALUE;
    95 
    96     log_event(session, "sync_protocol thread started", "pEp sync protocol", NULL, NULL);
    97 
    98     while ((msg = (DeviceGroup_Protocol_t *) session->retrieve_next_sync_msg(management))) 
    99     {
   100         if ((status = receive_sync_msg(session, msg) != PEP_STATUS_OK)) {
   101             char buffer[MAX_LINELENGTH];
   102             memset(buffer, 0, MAX_LINELENGTH);
   103             snprintf(buffer, MAX_LINELENGTH, "problem with msg received: %d\n", (int) status);
   104             log_event(session, buffer, "pEp sync protocol", NULL, NULL);
   105         }
   106     }
   107 
   108     log_event(session, "sync_protocol thread shutdown", "pEp sync protocol", NULL, NULL);
   109 
   110     return PEP_STATUS_OK;
   111 }
   112