src/sync.h
author Edouard Tisserant <edouard@pep-project.org>
Fri, 09 Dec 2016 01:36:57 +0100
changeset 1477 c0d761ae1fd9
parent 1475 f76bc25570df
child 1482 dae999ee1a41
permissions -rw-r--r--
sync: obey new app notification enum names, this time with corresponding actions generated
vb@1471
     1
/*
vb@1471
     2
====================================
vb@1471
     3
Engine/adapter/app KeySync interface 
vb@1471
     4
====================================
vb@1471
     5
vb@1471
     6
In the engine, KeySync is implemented through a state machine [1]. KeySync
vb@1471
     7
state machine is driven [2] by events, triggering actions [3] and transitions
vb@1471
     8
to new states. Events happens on decryption of email messages, on key
vb@1471
     9
generation, on user interaction through the app and in case of timeout when
vb@1471
    10
staying too long in some particular states.
vb@1471
    11
vb@1471
    12
To use KeySync, the adapter has to create a session dedicated to handle the
vb@1471
    13
protocol, register some callbacks [4] to the engine, and then call protocol's
vb@1471
    14
event consumer loop [5] in a dedicated thread. KeySync actions are executed
vb@1471
    15
as callback invoked from that loop : send pEp messages through app's transport
vb@1471
    16
and display KeySync status and handshake to the user.
vb@1471
    17
vb@1471
    18
When a session is attached [6] to a KeySync session, decryption of pEp (email)
vb@1471
    19
messages in that session may trigger operations in attached KeySync session. In
vb@1471
    20
case of an adapter capable to serve multiple apps, each app is associated to a
vb@1471
    21
different KeySync session, and sessions created for use in that app are
vb@1471
    22
attached to that session.
vb@1471
    23
vb@1471
    24
KeySync messages [7], not to be confused with pEp (email) messages, are either
vb@1471
    25
directly events to be processed by the state machine or KeySync payloads
vb@1471
    26
collected from decrypted messages. They are jobs to be processed by the state
vb@1471
    27
machine.
vb@1471
    28
vb@1471
    29
KeySync messages can be emitted by multiple session, and could naturally come
vb@1471
    30
from different threads. They must be serialized in a locked queue. Attached
vb@1471
    31
sessions inject [8] KeySync messages in the queue. Protocol loop retrieves [9]
vb@1471
    32
them from the queue. KeySync message is received [10] by the state machine,
vb@1471
    33
where event eventually deduced from payload.
vb@1471
    34
vb@1471
    35
A state timeout event is a particular case. It doesn't traverse the queue, and
vb@1471
    36
isn't emitted by a session. It is triggered by a timeout on the retrieve
vb@1471
    37
operation. Value of the timeout is determined when entering a new state, and is
vb@1471
    38
passed as a parameter of the call to the blocking queue retrieve operation on 
vb@1471
    39
next protocol loop iteraton.
vb@1471
    40
vb@1471
    41
[1] sync/device_group.fsm , src/sync_fsm.c (generated)
vb@1471
    42
[2] src/sync_driver.c (generated)
vb@1471
    43
[3] src/sync_actions.c , src/sync_send_actions.c (generated)
vb@1471
    44
[4] register_sync_callbacks()
vb@1471
    45
[5] do_sync_protocol()
vb@1471
    46
[6] attach_sync_session()
vb@1471
    47
[7] type sync_msg_t
vb@1471
    48
[8] callback inject_sync_msg
vb@1471
    49
[9] callback retrieve_next_sync_msg
vb@1471
    50
[10] receive_sync_msg() (src/sync_impl.c)
vb@1471
    51
vb@1471
    52
*/
vb@1471
    53
vb@572
    54
#pragma once
vb@572
    55
vb@572
    56
#include "message.h"
vb@690
    57
#include "sync_fsm.h"
vb@572
    58
vb@1470
    59
vb@1470
    60
// this module is for being used WITHOUT the Transport API in transport.h
vb@1470
    61
// DO NOT USE IT WHEN USING Transport API!
vb@1470
    62
vb@1470
    63
vb@572
    64
#ifdef __cplusplus
vb@572
    65
extern "C" {
vb@572
    66
#endif
vb@572
    67
vb@1043
    68
// messageToSend() - send a message
vb@572
    69
//
vb@572
    70
//  parameters:
vb@599
    71
//      obj (in)        object handle (implementation defined)
vb@598
    72
//      msg (in)        message struct with message to send
vb@572
    73
//
vb@572
    74
//  return value:
vb@604
    75
//      PEP_STATUS_OK or any other value on error
vb@991
    76
//
vb@991
    77
//  caveat:
vb@991
    78
//      the ownership of msg goes to the callee
vb@572
    79
vb@991
    80
typedef PEP_STATUS (*messageToSend_t)(void *obj, message *msg);
vb@572
    81
edouard@1477
    82
// TODO add this to generated code.
edouard@1459
    83
typedef enum _sync_handshake_signal {
vb@1473
    84
    SYNC_NOTIFY_UNDEFINED = 0,
vb@1470
    85
vb@1470
    86
    // request show handshake dialog
vb@1473
    87
    SYNC_NOTIFY_INIT_ADD_OUR_DEVICE,
vb@1473
    88
    SYNC_NOTIFY_INIT_ADD_OTHER_DEVICE,
vb@1473
    89
    SYNC_NOTIFY_INIT_FORM_GROUP,
vb@1470
    90
edouard@1477
    91
    // handshake process timed out
vb@1475
    92
    SYNC_NOTIFY_TIMEOUT,
vb@1470
    93
vb@1470
    94
    // handshake accepted by user
vb@1473
    95
    SYNC_NOTIFY_ACCEPTED_DEVICE_ADDED,
edouard@1477
    96
    SYNC_NOTIFY_ACCEPTED_GROUP_CREATED
vb@1470
    97
edouard@1459
    98
} sync_handshake_signal;
vb@572
    99
edouard@1459
   100
// notifyHandshake() - notify UI about sync handshaking process
vb@572
   101
//
vb@572
   102
//  parameters:
vb@599
   103
//      obj (in)        object handle (implementation defined)
vb@1003
   104
//      me (in)         own identity
vb@572
   105
//      partner (in)    identity of partner
edouard@1459
   106
//      signal (in)     reason of the notification
vb@572
   107
//
vb@572
   108
//  return value:
vb@604
   109
//      PEP_STATUS_OK or any other value on error
vb@991
   110
//
vb@991
   111
//  caveat:
vb@991
   112
//      ownership of self and partner go to the callee
vb@572
   113
edouard@1459
   114
typedef PEP_STATUS (*notifyHandshake_t)(
vb@599
   115
        void *obj,
vb@1003
   116
        pEp_identity *me,
edouard@1459
   117
        pEp_identity *partner,
edouard@1459
   118
        sync_handshake_signal signal
vb@609
   119
    );
vb@609
   120
edouard@1459
   121
typedef enum _sync_handshake_result {
edouard@1459
   122
    SYNC_HANDSHAKE_CANCEL = -1,
edouard@1459
   123
    SYNC_HANDSHAKE_ACCEPTED = 0,
edouard@1459
   124
    SYNC_HANDSHAKE_REJECTED = 1
edouard@1459
   125
} sync_handshake_result;
vb@609
   126
vb@609
   127
// deliverHandshakeResult() - give the result of the handshake dialog
vb@609
   128
//
vb@609
   129
//  parameters:
vb@1043
   130
//      session (in)        session handle
vb@1043
   131
//      result (in)         handshake result
vb@609
   132
vb@679
   133
DYNAMIC_API PEP_STATUS deliverHandshakeResult(
vb@609
   134
        PEP_SESSION session,
edouard@1160
   135
        Identity partner,
vb@609
   136
        sync_handshake_result result
vb@572
   137
    );
vb@572
   138
edouard@1172
   139
// sync_msg_t - items queued for serialized handling by protocol engine
edouard@1172
   140
typedef struct _sync_msg_t sync_msg_t;
vb@572
   141
vb@1043
   142
// inject_sync_msg - inject sync protocol message
vb@1043
   143
//
vb@1043
   144
//  parameters:
vb@1043
   145
//      msg (in)            message to inject
vb@1043
   146
//      management (in)     application defined
vb@1043
   147
//
vb@1043
   148
//  return value:
vb@1043
   149
//      0 if msg could be stored successfully or nonzero otherwise
vb@1043
   150
vb@1043
   151
typedef int (*inject_sync_msg_t)(void *msg, void *management);
vb@1043
   152
vb@1043
   153
vb@1470
   154
// retrieve_next_sync_msg - receive next sync message
vb@1043
   155
//
vb@1043
   156
//  parameters:
vb@1043
   157
//      management (in)     application defined
vb@1470
   158
//      timeout (in,out)    do not wait longer than timeout for message
vb@1043
   159
//
vb@1043
   160
//  return value:
edouard@1445
   161
//      next message or :
vb@1471
   162
//      NULL and timeout == 0 for termination
vb@1471
   163
//      NULL and timeout != 0 for timeout occurence
vb@1043
   164
edouard@1445
   165
typedef void *(*retrieve_next_sync_msg_t)(void *management, time_t *timeout);
vb@1043
   166
vb@1043
   167
vb@572
   168
// register_sync_callbacks() - register adapter's callbacks
vb@572
   169
//
vb@572
   170
//  parameters:
vb@599
   171
//      session (in)                session where to store obj handle
edouard@1462
   172
//      management (in)             application defined
vb@599
   173
//      messageToSend (in)          callback for sending message
edouard@1462
   174
//      notifyHandshake (in)        callback for doing the handshake
vb@1043
   175
//      retrieve_next_sync_msg (in) callback for receiving sync messages
vb@572
   176
//
vb@572
   177
//  return value:
vb@572
   178
//      PEP_STATUS_OK or any other value on errror
vb@573
   179
//
vb@573
   180
//  caveat:
vb@573
   181
//      call that BEFORE you're using any other part of the engine
vb@572
   182
vb@572
   183
DYNAMIC_API PEP_STATUS register_sync_callbacks(
vb@599
   184
        PEP_SESSION session,
edouard@1462
   185
        void *management,
vb@597
   186
        messageToSend_t messageToSend,
edouard@1459
   187
        notifyHandshake_t notifyHandshake,
vb@1043
   188
        inject_sync_msg_t inject_sync_msg,
vb@1043
   189
        retrieve_next_sync_msg_t retrieve_next_sync_msg
vb@572
   190
    );
vb@572
   191
edouard@1236
   192
// attach_sync_session() - attach session to a session running keysync state machine 
edouard@1236
   193
//
edouard@1236
   194
//  parameters:
edouard@1236
   195
//      session (in)                session to attach
edouard@1236
   196
//      sync_session (in)           session running keysync
edouard@1236
   197
//
edouard@1236
   198
//  return value:
edouard@1236
   199
//      PEP_STATUS_OK or any other value on errror
edouard@1236
   200
//
edouard@1236
   201
//  caveat:
edouard@1236
   202
//      register_sync_callbacks must have been called on sync_session
edouard@1236
   203
//      call that BEFORE you're using that session in any other part of the engine
edouard@1236
   204
edouard@1236
   205
DYNAMIC_API PEP_STATUS attach_sync_session(
edouard@1236
   206
        PEP_SESSION session,
edouard@1236
   207
        PEP_SESSION sync_session
edouard@1236
   208
    );
edouard@1236
   209
edouard@1236
   210
// detach_sync_session() - detach previously attached sync session
edouard@1236
   211
//
edouard@1236
   212
//  parameters:
edouard@1236
   213
//      session (in)                session to detach 
edouard@1236
   214
edouard@1236
   215
DYNAMIC_API PEP_STATUS detach_sync_session(PEP_SESSION session);
vb@572
   216
vb@572
   217
// unregister_sync_callbacks() - unregister adapter's callbacks
vb@602
   218
//
vb@602
   219
//  parameters:
edouard@1236
   220
//      session (in)                session to unregister
vb@572
   221
vb@602
   222
DYNAMIC_API void unregister_sync_callbacks(PEP_SESSION session);
vb@572
   223
vb@1116
   224
// do_sync_protocol() - function to be run on an extra thread
vb@1043
   225
//
vb@1043
   226
//  parameters:
vb@1043
   227
//      session                 pEp session to use
vb@1043
   228
//      retrieve_next_sync_msg  pointer to retrieve_next_identity() callback
vb@1043
   229
//                              which returns at least a valid address field in
vb@1043
   230
//                              the identity struct
edouard@1462
   231
//      obj                     application defined sync object
vb@1043
   232
//
vb@1043
   233
//  return value:
vb@1043
   234
//      PEP_STATUS_OK if thread has to terminate successfully or any other
vb@1043
   235
//      value on failure
vb@1043
   236
//
vb@1043
   237
//  caveat:
vb@1043
   238
//      to ensure proper working of this library, a thread has to be started
vb@1043
   239
//      with this function immediately after initialization
vb@1043
   240
vb@1043
   241
DYNAMIC_API PEP_STATUS do_sync_protocol(
vb@1043
   242
        PEP_SESSION session,
edouard@1462
   243
        void *obj
vb@1043
   244
    );
vb@1043
   245
Edouard@1203
   246
// free_sync_msg() - free sync_msg_t struct when not passed to do_sync_protocol  
Edouard@1203
   247
//
Edouard@1203
   248
//  parameters:
Edouard@1203
   249
//      sync_msg (in)            pointer to sync_msg_t struct to free
Edouard@1203
   250
Edouard@1203
   251
DYNAMIC_API void free_sync_msg(sync_msg_t *sync_msg);
vb@1043
   252
vb@1116
   253
// decode_sync_msg() - decode sync message from PER into XER
vb@1116
   254
//
vb@1116
   255
//  parameters:
vb@1116
   256
//      data (in)               PER encoded data
vb@1116
   257
//      size (in)               size of PER encoded data
vb@1116
   258
//      text (out)              XER text of the same sync message
vb@1116
   259
vb@1116
   260
DYNAMIC_API PEP_STATUS decode_sync_msg(
vb@1116
   261
        const char *data,
vb@1116
   262
        size_t size,
vb@1116
   263
        char **text
vb@1116
   264
    );
vb@1116
   265
vb@1116
   266
vb@1128
   267
// encode_sync_msg() - encode sync message from XER into PER
vb@1128
   268
//
vb@1128
   269
//  parameters:
vb@1128
   270
//      text (in)               string with XER text of the sync message
vb@1128
   271
//      data (out)              PER encoded data
vb@1128
   272
//      size (out)              size of PER encoded data
vb@1128
   273
vb@1128
   274
DYNAMIC_API PEP_STATUS encode_sync_msg(
vb@1128
   275
        const char *text,
vb@1128
   276
        char **data,
vb@1128
   277
        size_t *size
vb@1128
   278
    );
vb@1128
   279
vb@1128
   280
vb@572
   281
#ifdef __cplusplus
vb@572
   282
}
vb@572
   283
#endif
vb@572
   284