src/sync.h
author Edouard Tisserant <edouard@pep-project.org>
Thu, 01 Dec 2016 17:56:05 +0100
branchENGINE-133
changeset 1459 ab329d7db8d7
parent 1445 5d233bcdde76
child 1462 b93663bfc7c6
permissions -rw-r--r--
ENGINE-133 added more feedback for the app in case of handshake success or failure, because of timeout
vb@572
     1
#pragma once
vb@572
     2
vb@572
     3
#include "message.h"
vb@690
     4
#include "sync_fsm.h"
vb@572
     5
vb@572
     6
vb@572
     7
// this module is for being used WITHOUT the Transport API in transport.h
vb@572
     8
// DO NOT USE IT WHEN USING Transport API!
vb@572
     9
vb@572
    10
vb@572
    11
#ifdef __cplusplus
vb@572
    12
extern "C" {
vb@572
    13
#endif
vb@572
    14
vb@1043
    15
// messageToSend() - send a message
vb@572
    16
//
vb@572
    17
//  parameters:
vb@599
    18
//      obj (in)        object handle (implementation defined)
vb@598
    19
//      msg (in)        message struct with message to send
vb@572
    20
//
vb@572
    21
//  return value:
vb@604
    22
//      PEP_STATUS_OK or any other value on error
vb@991
    23
//
vb@991
    24
//  caveat:
vb@991
    25
//      the ownership of msg goes to the callee
vb@572
    26
vb@991
    27
typedef PEP_STATUS (*messageToSend_t)(void *obj, message *msg);
vb@572
    28
edouard@1459
    29
typedef enum _sync_handshake_signal {
edouard@1459
    30
    SYNC_HANDSHAKE_DISMISS_DIALOG = 0,
edouard@1459
    31
    SYNC_HANDSHAKE_SHOW_DIALOG = 1,
edouard@1459
    32
    SYNC_HANDSHAKE_SUCCESS = 2,
edouard@1459
    33
    SYNC_HANDSHAKE_FAILURE = 3,
edouard@1459
    34
    SYNC_DEVICE_ADDED = 4,
edouard@1459
    35
    SYNC_GROUP_CREATED = 5
edouard@1459
    36
} sync_handshake_signal;
vb@572
    37
edouard@1459
    38
// notifyHandshake() - notify UI about sync handshaking process
vb@572
    39
//
vb@572
    40
//  parameters:
vb@599
    41
//      obj (in)        object handle (implementation defined)
vb@1003
    42
//      me (in)         own identity
vb@572
    43
//      partner (in)    identity of partner
edouard@1459
    44
//      signal (in)     reason of the notification
vb@572
    45
//
vb@572
    46
//  return value:
vb@604
    47
//      PEP_STATUS_OK or any other value on error
vb@991
    48
//
vb@991
    49
//  caveat:
vb@991
    50
//      ownership of self and partner go to the callee
vb@572
    51
edouard@1459
    52
typedef PEP_STATUS (*notifyHandshake_t)(
vb@599
    53
        void *obj,
vb@1003
    54
        pEp_identity *me,
edouard@1459
    55
        pEp_identity *partner,
edouard@1459
    56
        sync_handshake_signal signal
vb@609
    57
    );
vb@609
    58
edouard@1459
    59
typedef enum _sync_handshake_result {
edouard@1459
    60
    SYNC_HANDSHAKE_CANCEL = -1,
edouard@1459
    61
    SYNC_HANDSHAKE_ACCEPTED = 0,
edouard@1459
    62
    SYNC_HANDSHAKE_REJECTED = 1
edouard@1459
    63
} sync_handshake_result;
vb@609
    64
vb@609
    65
// deliverHandshakeResult() - give the result of the handshake dialog
vb@609
    66
//
vb@609
    67
//  parameters:
vb@1043
    68
//      session (in)        session handle
vb@1043
    69
//      result (in)         handshake result
vb@609
    70
vb@679
    71
DYNAMIC_API PEP_STATUS deliverHandshakeResult(
vb@609
    72
        PEP_SESSION session,
edouard@1160
    73
        Identity partner,
vb@609
    74
        sync_handshake_result result
vb@572
    75
    );
vb@572
    76
edouard@1172
    77
// sync_msg_t - items queued for serialized handling by protocol engine
edouard@1172
    78
typedef struct _sync_msg_t sync_msg_t;
vb@572
    79
vb@1043
    80
// inject_sync_msg - inject sync protocol message
vb@1043
    81
//
vb@1043
    82
//  parameters:
vb@1043
    83
//      msg (in)            message to inject
vb@1043
    84
//      management (in)     application defined
vb@1043
    85
//
vb@1043
    86
//  return value:
vb@1043
    87
//      0 if msg could be stored successfully or nonzero otherwise
vb@1043
    88
vb@1043
    89
typedef int (*inject_sync_msg_t)(void *msg, void *management);
vb@1043
    90
vb@1043
    91
vb@1043
    92
// retrieve_next_sync_msg - receive next sync message
vb@1043
    93
//
vb@1043
    94
//  parameters:
vb@1043
    95
//      management (in)     application defined
edouard@1445
    96
//      timeout (in,out)    do not wait longer than timeout for message
vb@1043
    97
//
vb@1043
    98
//  return value:
edouard@1445
    99
//      next message or :
edouard@1445
   100
//      NULL + timeout == 0 for termination
edouard@1445
   101
//      NULL + timeout != 0 for timeout occurence
vb@1043
   102
edouard@1445
   103
typedef void *(*retrieve_next_sync_msg_t)(void *management, time_t *timeout);
vb@1043
   104
vb@1043
   105
vb@572
   106
// register_sync_callbacks() - register adapter's callbacks
vb@572
   107
//
vb@572
   108
//  parameters:
vb@599
   109
//      session (in)                session where to store obj handle
vb@599
   110
//      obj (in)                    object handle (implementation defined)
vb@599
   111
//      messageToSend (in)          callback for sending message
edouard@1459
   112
//      notifyHandshake (in)          callback for doing the handshake
vb@1043
   113
//      retrieve_next_sync_msg (in) callback for receiving sync messages
vb@572
   114
//
vb@572
   115
//  return value:
vb@572
   116
//      PEP_STATUS_OK or any other value on errror
vb@573
   117
//
vb@573
   118
//  caveat:
vb@573
   119
//      call that BEFORE you're using any other part of the engine
vb@572
   120
vb@572
   121
DYNAMIC_API PEP_STATUS register_sync_callbacks(
vb@599
   122
        PEP_SESSION session,
vb@599
   123
        void *obj,
vb@597
   124
        messageToSend_t messageToSend,
edouard@1459
   125
        notifyHandshake_t notifyHandshake,
vb@1043
   126
        inject_sync_msg_t inject_sync_msg,
vb@1043
   127
        retrieve_next_sync_msg_t retrieve_next_sync_msg
vb@572
   128
    );
vb@572
   129
edouard@1236
   130
// attach_sync_session() - attach session to a session running keysync state machine 
edouard@1236
   131
//
edouard@1236
   132
//  parameters:
edouard@1236
   133
//      session (in)                session to attach
edouard@1236
   134
//      sync_session (in)           session running keysync
edouard@1236
   135
//
edouard@1236
   136
//  return value:
edouard@1236
   137
//      PEP_STATUS_OK or any other value on errror
edouard@1236
   138
//
edouard@1236
   139
//  caveat:
edouard@1236
   140
//      register_sync_callbacks must have been called on sync_session
edouard@1236
   141
//      call that BEFORE you're using that session in any other part of the engine
edouard@1236
   142
edouard@1236
   143
DYNAMIC_API PEP_STATUS attach_sync_session(
edouard@1236
   144
        PEP_SESSION session,
edouard@1236
   145
        PEP_SESSION sync_session
edouard@1236
   146
    );
edouard@1236
   147
edouard@1236
   148
// detach_sync_session() - detach previously attached sync session
edouard@1236
   149
//
edouard@1236
   150
//  parameters:
edouard@1236
   151
//      session (in)                session to detach 
edouard@1236
   152
edouard@1236
   153
DYNAMIC_API PEP_STATUS detach_sync_session(PEP_SESSION session);
vb@572
   154
vb@572
   155
// unregister_sync_callbacks() - unregister adapter's callbacks
vb@602
   156
//
vb@602
   157
//  parameters:
edouard@1236
   158
//      session (in)                session to unregister
vb@572
   159
vb@602
   160
DYNAMIC_API void unregister_sync_callbacks(PEP_SESSION session);
vb@572
   161
vb@1116
   162
// do_sync_protocol() - function to be run on an extra thread
vb@1043
   163
//
vb@1043
   164
//  parameters:
vb@1043
   165
//      session                 pEp session to use
vb@1043
   166
//      retrieve_next_sync_msg  pointer to retrieve_next_identity() callback
vb@1043
   167
//                              which returns at least a valid address field in
vb@1043
   168
//                              the identity struct
vb@1043
   169
//      management              management data to give to keymanagement
vb@1043
   170
//                              (implementation defined)
vb@1043
   171
//
vb@1043
   172
//  return value:
vb@1043
   173
//      PEP_STATUS_OK if thread has to terminate successfully or any other
vb@1043
   174
//      value on failure
vb@1043
   175
//
vb@1043
   176
//  caveat:
vb@1043
   177
//      to ensure proper working of this library, a thread has to be started
vb@1043
   178
//      with this function immediately after initialization
vb@1043
   179
//      do_keymanagement() calls retrieve_next_identity(management)
vb@1043
   180
vb@1043
   181
DYNAMIC_API PEP_STATUS do_sync_protocol(
vb@1043
   182
        PEP_SESSION session,
vb@1043
   183
        void *management
vb@1043
   184
    );
vb@1043
   185
Edouard@1203
   186
// free_sync_msg() - free sync_msg_t struct when not passed to do_sync_protocol  
Edouard@1203
   187
//
Edouard@1203
   188
//  parameters:
Edouard@1203
   189
//      sync_msg (in)            pointer to sync_msg_t struct to free
Edouard@1203
   190
Edouard@1203
   191
DYNAMIC_API void free_sync_msg(sync_msg_t *sync_msg);
vb@1043
   192
vb@1116
   193
// decode_sync_msg() - decode sync message from PER into XER
vb@1116
   194
//
vb@1116
   195
//  parameters:
vb@1116
   196
//      data (in)               PER encoded data
vb@1116
   197
//      size (in)               size of PER encoded data
vb@1116
   198
//      text (out)              XER text of the same sync message
vb@1116
   199
vb@1116
   200
DYNAMIC_API PEP_STATUS decode_sync_msg(
vb@1116
   201
        const char *data,
vb@1116
   202
        size_t size,
vb@1116
   203
        char **text
vb@1116
   204
    );
vb@1116
   205
vb@1116
   206
vb@1128
   207
// encode_sync_msg() - encode sync message from XER into PER
vb@1128
   208
//
vb@1128
   209
//  parameters:
vb@1128
   210
//      text (in)               string with XER text of the sync message
vb@1128
   211
//      data (out)              PER encoded data
vb@1128
   212
//      size (out)              size of PER encoded data
vb@1128
   213
vb@1128
   214
DYNAMIC_API PEP_STATUS encode_sync_msg(
vb@1128
   215
        const char *text,
vb@1128
   216
        char **data,
vb@1128
   217
        size_t *size
vb@1128
   218
    );
vb@1128
   219
vb@1128
   220
vb@572
   221
#ifdef __cplusplus
vb@572
   222
}
vb@572
   223
#endif
vb@572
   224