1/*
2 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25/*!
26 @header SOSEngine.h - Manifest managent engine and decision making for
27 object syncing protocol.
28 */
29
30#ifndef _SEC_SOSENGINE_H_
31#define _SEC_SOSENGINE_H_
32
33#include <SecureObjectSync/SOSDataSource.h>
34#include <SecureObjectSync/SOSMessage.h>
35#include <dispatch/dispatch.h>
36
37__BEGIN_DECLS
38
39enum {
40    kSOSEngineInvalidMessageError = 1,
41    kSOSEngineInternalError = 2,
42};
43
44typedef struct __OpaqueSOSPeer *SOSPeerRef;
45
46typedef void (^SOSEnginePeerMessageSentBlock)(bool success);
47
48//Remove me!
49void SOSEngineCircleChanged_locked(SOSEngineRef engine, CFStringRef myPeerID, CFArrayRef trustedPeers, CFArrayRef untrustedPeers);
50
51
52// Return a new engine instance for a given data source.
53SOSEngineRef SOSEngineCreate(SOSDataSourceRef dataSource, CFErrorRef *error);
54
55// Return a snapshot of the current manifest of the engines data source.
56SOSManifestRef SOSEngineCopyManifest(SOSEngineRef engine, CFErrorRef *error);
57
58// Remove removals and add additions to the (cached) local manifest, and update all peers accordingly
59bool SOSEngineUpdateLocalManifest(SOSEngineRef engine, SOSDataSourceTransactionSource source, struct SOSDigestVector *removals, struct SOSDigestVector *additions, CFErrorRef *error);
60
61// Store manifest indexed by it's own digest.  Can be retrieved with SOSEngineGetManifestForDigest()
62void SOSEngineAddManifest(SOSEngineRef engine, SOSManifestRef manifest);
63
64// Retrive a digest stored with SOSEngineAddManifest()
65SOSManifestRef SOSEngineGetManifestForDigest(SOSEngineRef engine, CFDataRef digest);
66
67// Return the digest for a patched manifest (which is stored in the cache already).
68CFDataRef SOSEnginePatchRecordAndCopyDigest(SOSEngineRef engine, SOSManifestRef base, SOSManifestRef removals, SOSManifestRef additions, CFErrorRef *error);
69
70//Set/Get coders
71bool SOSEngineSetCoderData(SOSEngineRef engine, CFStringRef peer_id, CFDataRef data, CFErrorRef *error);
72CFDataRef SOSEngineGetCoderData(SOSEngineRef engine, CFStringRef peer_id);
73
74//Get peer state
75CFMutableDictionaryRef SOSEngineGetPeerState(SOSEngineRef engine, CFStringRef peerID);
76
77// Dispose of an engine when it's no longer needed.
78void SOSEngineDispose(SOSEngineRef engine);
79
80// Handle incoming message from a remote peer.
81bool SOSEngineHandleMessage(SOSEngineRef engine, CFStringRef peerID,
82                            CFDataRef message, CFErrorRef *error);
83
84void SOSEngineCircleChanged(SOSEngineRef engine, CFStringRef myPeerID, CFArrayRef trustedPeers, CFArrayRef untrustedPeers);
85
86// Return a message to be sent for the current state.  Returns NULL on errors,
87// return a zero length CFDataRef if there is nothing to send.
88// If *ProposedManifest is set the caller is responsible for updating their
89// proposed manifest upon successful transmission of the message.
90CFDataRef SOSEngineCreateMessageToSyncToPeer(SOSEngineRef engine, CFStringRef peerID, SOSEnginePeerMessageSentBlock *sentBlock, CFErrorRef *error);
91
92CFStringRef SOSEngineGetMyID(SOSEngineRef engine);
93bool SOSEnginePeerDidConnect(SOSEngineRef engine, CFStringRef peerID, CFErrorRef *error);
94
95void logRawMessage(CFDataRef message, bool sending, uint64_t seqno);
96
97// TODO: TEMPORARY: Get the list of IDs for cleanup, this shouldn't be used instead transport should iterate KVS.
98CFArrayRef SOSEngineGetPeerIDs(SOSEngineRef engine);
99
100__END_DECLS
101
102#endif /* !_SEC_SOSENGINE_H_ */
103