forked from ThadHouse/MinimalNtCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNtCoreNatives.cs
More file actions
349 lines (277 loc) · 10.1 KB
/
NtCoreNatives.cs
File metadata and controls
349 lines (277 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
using System;
using System.Runtime.InteropServices;
namespace MinimalNtCore
{
public enum LogLevel {
Critical = 50,
Error = 40,
Warning = 30,
Info = 20,
Debug = 10,
Debug1 = 9,
Debug2 = 8,
Debug3 = 7,
Debug4 = 6
}
[StructLayout(LayoutKind.Sequential)]
public unsafe struct WpiString
{
public byte* str;
public UIntPtr len;
}
[StructLayout(LayoutKind.Sequential)]
public unsafe struct NativeNtLogMessage
{
public uint level;
public WpiString filename;
public uint line;
public WpiString message;
}
[StructLayout(LayoutKind.Sequential)]
public struct NativeTimeSyncEventData
{
public long serverTimeOffset;
public long rtt2;
public int valid;
}
[StructLayout(LayoutKind.Sequential)]
public struct NativeNetworkTableValue
{
public NtType type;
public long lastChange;
public long serverTime;
public NtValueUnion data;
[StructLayout(LayoutKind.Explicit)]
public struct NtValueUnion
{
[FieldOffset(0)]
public int valueBoolean;
[FieldOffset(0)]
public long valueInt;
[FieldOffset(0)]
public float valueFloat;
[FieldOffset(0)]
public double valueDouble;
[FieldOffset(0)]
public WpiString valueString;
[FieldOffset(0)]
public NtValueRaw valueRaw;
[FieldOffset(0)]
public NtValueBooleanArray arrBoolean;
[FieldOffset(0)]
public NtValueDoubleArray arrDouble;
[FieldOffset(0)]
public NtValueFloatArray arrFloat;
[FieldOffset(0)]
public NtValueIntArray arrInt;
[FieldOffset(0)]
public NtValueStringArray arrString;
public unsafe struct NtValueRaw
{
public byte* data;
public UIntPtr size;
}
public unsafe struct NtValueBooleanArray
{
public int* arr;
public UIntPtr size;
}
public unsafe struct NtValueDoubleArray
{
public double* arr;
public UIntPtr size;
}
public unsafe struct NtValueFloatArray
{
public float* arr;
public UIntPtr size;
}
public unsafe struct NtValueIntArray
{
public long* arr;
public UIntPtr size;
}
public unsafe struct NtValueStringArray
{
public WpiString* arr;
public UIntPtr size;
}
}
}
[StructLayout(LayoutKind.Sequential)]
public struct NativeValueEventData
{
public int topic;
public int subentry;
public NativeNetworkTableValue value;
}
[StructLayout(LayoutKind.Sequential)]
public struct NativeTopicInfo
{
public int topic;
public WpiString name;
public NtType type;
public WpiString typeStr;
public WpiString properties;
}
[StructLayout(LayoutKind.Sequential)]
public struct NativeConnectionInfo
{
public WpiString remoteId;
public WpiString remoteIp;
public uint remotePort;
public ulong lastUpdate;
public uint protocolVersion;
}
[StructLayout(LayoutKind.Sequential)]
public unsafe struct NativeNtEvent
{
public uint listenerHandle;
public uint flags;
public NtEventUnion data;
[StructLayout(LayoutKind.Explicit)]
public struct NtEventUnion
{
[FieldOffset(0)]
public NativeConnectionInfo connInfo;
[FieldOffset(0)]
public NativeTopicInfo topicInfo;
[FieldOffset(0)]
public NativeValueEventData valueData;
[FieldOffset(0)]
public NativeNtLogMessage logMessage;
[FieldOffset(0)]
public NativeTimeSyncEventData timeSyncData;
}
}
public enum NtType
{
NT_UNASSIGNED = 0,
NT_BOOLEAN = 0x01,
NT_DOUBLE = 0x02,
NT_STRING = 0x04,
NT_RAW = 0x08,
NT_BOOLEAN_ARRAY = 0x10,
NT_DOUBLE_ARRAY = 0x20,
NT_STRING_ARRAY = 0x40,
NT_RPC = 0x80,
NT_INTEGER = 0x100,
NT_FLOAT = 0x200,
NT_INTEGER_ARRAY = 0x400,
NT_FLOAT_ARRAY = 0x800
}
[StructLayout(LayoutKind.Sequential)]
public struct NativePubSubOptions
{
/**
* Structure size. Must be set to sizeof(NT_NativePubSubOptions).
*/
public uint structSize;
/**
* Polling storage size for a subscription. Specifies the maximum number of
* updates NetworkTables should store between calls to the subscriber's
* ReadQueue() function. If zero, defaults to 1 if sendAll is false, 20 if
* sendAll is true.
*/
public uint pollStorage;
/**
* How frequently changes will be sent over the network, in seconds.
* NetworkTables may send more frequently than this (e.g. use a combined
* minimum period for all values) or apply a restricted range to this value.
* The default is 100 ms.
*/
public double periodic;
/**
* For subscriptions, if non-zero, value updates for ReadQueue() are not
* queued for this publisher.
*/
public uint excludePublisher;
/**
* Send all value changes over the network.
*/
public int sendAll;
/**
* For subscriptions, don't ask for value changes (only topic announcements).
*/
public int topicsOnly;
/**
* Perform prefix match on subscriber topic names. Is ignored/overridden by
* Subscribe() functions; only present in struct for the purposes of getting
* information about subscriptions.
*/
public int prefixMatch;
/**
* Preserve duplicate value changes (rather than ignoring them).
*/
public int keepDuplicates;
/**
* For subscriptions, if remote value updates should not be queued for
* ReadQueue(). See also disableLocal.
*/
public int disableRemote;
/**
* For subscriptions, if local value updates should not be queued for
* ReadQueue(). See also disableRemote.
*/
public int disableLocal;
/**
* For entries, don't queue (for ReadQueue) value updates for the entry's
* internal publisher.
*/
public int excludeSelf;
/**
* For subscriptions, don't share the existence of the subscription with the
* network. Note this means updates will not be received from the network
* unless another subscription overlaps with this one, and the subscription
* will not appear in metatopics.
*/
public int hidden;
}
public unsafe class NtCoreNatives
{
public const int NT_DEFAULT_PORT4 = 5810;
public const string NT_LIBRARY = "ntcore";
[DllImport(NT_LIBRARY)]
public static extern uint NT_GetDefaultInstance();
[DllImport(NT_LIBRARY)]
public static extern void NT_StartClient4(uint inst, WpiString* identity);
[DllImport(NT_LIBRARY)]
public static extern void NT_SetServerTeam(uint inst, uint team, uint port);
[DllImport(NT_LIBRARY)]
public static extern int NT_IsConnected(uint inst);
[DllImport(NT_LIBRARY)]
public static extern uint NT_GetTopic(uint inst, WpiString* name);
[DllImport(NT_LIBRARY)]
public static extern int NT_SetInteger(uint publisher, long time, long value);
[DllImport(NT_LIBRARY)]
public static extern long NT_GetInteger(uint subscriber, long defaultValue);
[DllImport(NT_LIBRARY)]
public static extern int NT_SetDouble(uint publisher, long time, double value);
[DllImport(NT_LIBRARY)]
public static extern double NT_GetDouble(uint subscriber, double defaultValue);
[DllImport(NT_LIBRARY)]
public static extern int NT_SetFloatArray(uint publisher, long time, float* value, UIntPtr len);
[DllImport(NT_LIBRARY)]
public static extern float* NT_GetFloatArray(uint subscriber, float* defaultValue, UIntPtr defaultLen, UIntPtr* len);
[DllImport(NT_LIBRARY)]
public static extern void NT_FreeFloatArray(float* value);
[DllImport(NT_LIBRARY)]
public static extern uint NT_Subscribe(uint topic, NtType type, WpiString* typeStr, NativePubSubOptions* options);
[DllImport(NT_LIBRARY)]
public static extern uint NT_Publish(uint topic, NtType type, WpiString* typeStr, NativePubSubOptions* options);
[DllImport(NT_LIBRARY)]
public static extern void NT_SetServerMulti(uint inst, UIntPtr count,
WpiString* server_names,
uint* ports);
[DllImport(NT_LIBRARY)]
public static extern uint NT_CreateListenerPoller(uint inst);
[DllImport(NT_LIBRARY)]
public static extern void NT_DestroyListenerPoller(uint poller);
[DllImport(NT_LIBRARY)]
public static extern NativeNtEvent* NT_ReadListenerQueue(uint poller, UIntPtr* len);
[DllImport(NT_LIBRARY)]
public static extern uint NT_AddPolledLogger(uint poller, uint minLevel, uint maxLevel);
[DllImport(NT_LIBRARY)]
public static extern void NT_DisposeEventArray(NativeNtEvent* arr, UIntPtr count);
}
}